#!/bin/sh
if [ x"$1" == x"" ]; then
echo "Usage: $0 {0|1|2} (0=voicemail off, 1=voicemail on, 2=voicemail status)"
exit 1
fi
IP=127.0.0.1
PASSWD=$(/bin/allcfgconv -C ar7 -c -o - | sed -ne '/^webui[[:space:]]*{/,/^}/{/=/{s/[[:space:]]*=[[:space:]]*/=/;s/^[[:space:]]*//;p}}' | grep '^password=' | cut -d '"' -f2)
CURL=$(which curl)
login(){
# neues Loginverfahren seit xx.04.76
if [ -e /var/html/html/login_sid.xml ];then
#[ -f /tmp/sid ] && cgisid="$(cat /tmp/sid)"
eval "$($CURL -s "http://$IP/cgi-bin/webcm" -d "getpage=../html/login_sid.xml" -d "sid=$cgisid" | sed -n 's,.*<\(Challenge\|iswriteaccess\|SID\)>\([^<]*\).*,\1=\2;,p')"
if [ "$iswriteaccess" = 0 ] && [ "$SID" = "0000000000000000" -o -z "$SID" ]; then
chksum="$(echo -n "$Challenge-$PASSWD" | sed -e 's,.,&\n,g' | tr '\n' '\0' | md5sum)"
eval "$($CURL -s "http://$IP/cgi-bin/webcm" -d "getpage=../html/login_sid.xml" -d "login:command/response=$Challenge-${chksum%% *}" | sed -n 's,.*<SID>\(.*\)</SID>.*,SID=\1,p')"
cgisid="$SID"
#echo -n "$cgisid" >/tmp/sid
fi
fi
}
voicemail_status(){
status=$($CURL \
-s "http://$IP/cgi-bin/webcm" \
-d "login:command/password=$PASSWD" \
-d "sid=$cgisid" \
-d "getpage=../html/de/menus/menu2.html" \
-d "errorpage=../html/de/menus/menu2.html" \
-d "var:pagename=fon1tam" \
-d "var:menu=home" |\
grep "tam:settings/TAM0/Active" | sed -n 's,.*value="\([^\"]*\).*,\1,p')
if [ "$status" = 0 ]; then
echo "Voicemail is disabled."
else
echo "Voicemail is enabled."
fi
}
voicemail_change(){
$CURL \
-s "http://$IP/cgi-bin/webcm" \
-d "login:command/password=$PASSWD" \
-d "sid=$cgisid" \
-d "getpage=../html/de/menus/menu2.html&errorpage=../html/de/menus/menu2.html&var:pagename=fon1tam&var:errorpagename=fondevices&var:menu=home" \
-d "tam:settings/TAM0/Active=$1" \
1>/dev/null
}
login
if [ x"$1" != x"2" ]; then
voicemail_change $1
voicemail_status
else
voicemail_status
fi