# Include via ". /usr/lib/callmonitor.out"
#
# Listener types and common utilities; separate function for each type
# of listener. Add your own!
#
# These environment variables are set by callmonitor before calling
# calling a listener:
# MSISDN caller's number
# CALLER caller's name
# CALLED number called
# some utilities
# get configuration entries from debug.cfg
get_it() {
local VALUE="$(sed -ne "s/^$1=\(['\"]\?\)\(.*\)\1[ ]*/\2/p" \
/var/flash/debug.cfg)"
echo "${VALUE:-"$2"}"
}
# URL encoding
urlencode() {
echo -e $(echo -n "$*" |
hexdump -v -e '/1 "!%02x"' |
sed -f /proc/self/fd/9 9<<\END )
s/!\(2[1ade]\)/\\x\1/g
s/!\(3[0-9]\)/\\x\1/g
s/!\(4[1-9a-f]\)/\\x\1/g
s/!\(5[0-9af]\)/\\x\1/g
s/!\(6[1-9a-f]\)/\\x\1/g
s/!\(7[0-9a]\)/\\x\1/g
s/!/%/g
END
}
# output an HTTP Authorization header (Basic)
# basic_auth <user> <password>
basic_auth() {
local user="$1" password="$2"
echo -n "$user:$password" | uuencode -m - |
sed -e '1d;2s/^/Authorization: Basic /;3,$s/^/ /;s/$/
/;$d'
}
# default message
default_message() {
cat <<-EOM
$(date +%A', '%d'.'%B' '%Y)
$(date +%R' Uhr - ')Anruf an $CALLED
von $MSISDN
$CALLER
EOM
}
# get matching IPs from multilease and execute a command for each of them
# example: for_ips 192.168.10. dboxpopup "Ring!"
for_leases() {
local IPS="$(fgrep -i "$1" /var/flash/multid.leases | awk '{ print $3 }')"
local COMMAND="$2" IP=
shift 2
for IP in $IPS
do
"$COMMAND" "$IP" "$@" &
done
}
# Usage: getmsg [OPTION]... <HOST> <url-template> [<message>]...
# getmsg [OPTION]... -t <url-template> <host> [<message>]...
# Send a message in a simple HTTP GET request.
#
# -t, --template=FORMAT use this printf-style template to build the URL,
# all following messages are URL-encoded and filled
# into this template
# -p, --port=PORT use a special target port (default 80)
# -w, --timeout=SECONDS set connect timeout (default 3)
# -v, --virtual=VIRT use a different virtual host (default HOST)
# -U, --user=USER user for basic authorization
# -P, --password=PASS password for basic authorization
getmsg() {
local - IP= URL= TEMPLATE= VIRTUAL= USERNAME= PASSWORD= AUTH= TEMP=
local PORT=80 TIMEOUT=3
echo "$@"
TEMP="$(getopt -n getmsg -o U:P:v:t:w:p: \
-l user:,password:,virtual:,port:,template:,timeout: -- "$@")"
if [ $? != 0 ]; then return 1; fi
set -f; eval "set -- $TEMP"; set +f
while true; do
case "$1" in
-U|--user) USERNAME="$2"; shift 2 ;;
-P|--password) PASSWORD="$2"; shift 2 ;;
-v|--virtual) VIRTUAL="$2"; shift 2 ;;
-t|--template) TEMPLATE="$2"; shift 2 ;;
-w|--timeout) TIMEOUT="$2"; shift 2 ;;
-p|--port) PORT="$2"; shift 2 ;;
--) shift; break ;;
*) shift ;; # should never happen
esac
done
if [ $# -eq 0 ]; then echo "Missing hostname or IP" >&2; return 1; fi
IP="$1"; shift
if [ -z "$TEMPLATE" ]; then
if [ $# -eq 0 ]; then echo "Missing template" >&2; return 1; fi
TEMPLATE="$1"; shift
fi
if [ $# -eq 0 ]; then set -- "$(default_message)"; fi
VIRTUAL="${VIRTUAL:-$IP}"
if [ -n "$USERNAME" -o -n "$PASSWORD" ]; then
AUTH="$(basic_auth "$USERNAME" "$PASSWORD")"
fi
echo "$@"
URL="$(set -f; printf "$TEMPLATE" \
$(for arg in "$@"; do urlencode "$arg"; done))"
{
echo -e "GET $URL HTTP/1.0\r"
echo -e "Host: $VIRTUAL\r"
[ -n "$AUTH" ] && echo "$AUTH"
echo -e "\r"
} | nc -w "$TIMEOUT" "$IP" "$PORT"
}
# resolve numbers to names and addresses ([url]www.dasoertliche.de)[/url]
reverse_lookup() {
local NUMBER="$1"
case "$NUMBER" in
0049*) NUMBER="0${NUMBER#0049}" ;;
0*) ;;
*) OKZ="$(get_it OKZ)"; NUMBER="$OKZ$NUMBER" ;;
esac
getmsg -w 5 [url]www.dasoertliche.de[/url] "$NUMBER" \
-t '/DB4Web/es/oetb2suche/home.htm?main=Antwort&s=2&kw_invers=%s' |
sed -e '/<a class="blb" href="home.htm/!d' \
-e 's#
#, #g' -e 's#<[^>]*># #g' \
-e 's#[ ][ ]*# #g' -e 's#^ ##' -e 's# $##' -e 's# ,#,#'
}
# simple *box listeners
dboxpopup() {
getmsg -t "/control/message?popup=%s" "$@"
}
dboxmessage() {
getmsg -t "/control/message?nmsg=%s" "$@"
}
dreammessage() {
getmsg -t "/cgi-bin/xmessage?timeout=15&caption=TELEFONANRUF&body=%s" "$@"
}
# Usage: yac [OPTION]... [MESSAGE]
# Send a message to a yac listener (Yet Another Caller ID Program)
# -p, --port=PORT use a special target port (default 10629)
yac() {
local - IP= MESSAGE= PORT=10629 TEMP
TEMP="$(getopt -n yac -o p: -l port: -- "$@")"
if [ $? != 0 ]; then return 1; fi
set -f; eval "set -- $TEMP"; set +f
while true; do
case "$1" in
-p|--port) PORT="$2"; shift 2 ;;
--) shift; break ;;
*) shift ;; # should never happen
esac
done
if [ $# -eq 0 ]; then echo "Missing hostname or IP" >&2; return 1; fi
IP="$1" MESSAGE="${2-"@CALL$CALLER~$MSISDN"}"
echo -e "$MESSAGE\0\c" | nc -w 2 "$IP" "$PORT"
}
# wake up all devices configured in debug.cfg
etherwakes() {
local etheropt
get_it ETHERWAKES |
while read etheropt
do
/bin/etherwake $etheropt
done
}
# start or stop ssh daemon
droptoggle() {
dropoff || dropon
}
# start ssh daemon
dropon() {
/usr/sbin/dropbear $(get_it DROPBEAR_OPTIONS "-p $(get_it DROPPORT 22)")
}
# stop ssh daemon
dropoff() {
killall dropbear
}
~ #