#!/bin/sh set -e vardir=/var/lib/kannel/hello sbindir=/usr/local/sbin exit0() { [ -n "$1" ] && echo "$1" exit 0 } exit1() { response="${1:+Error: }${1:-Internal error!}" # FIXME: pass error messages via stderr (not stdout) # echo >&2 "$response" echo "$response" [ -z "$SMS_SMSC$SMS_URL" ] || $sbindir/localsendsms "$to" "$response" exit 1 } simpleword() { # echo "$1" | tr '[A-Z]' '[a-z]' | egrep '^[a-z][a-z0-9-]+$' || exit 1 echo "$1" | tr '[A-Z]' '[a-z]' | egrep '^[a-zæøåÆØÅ!@/][a-zæøåÆØÅ0-9-]+$' || exit 1 } uriunescape() { if [ 0 = "$urldecode" ]; then echo "$@" else echo "$@" | perl -e 'use URI::Escape; print lc(uri_unescape());' fi } lastservicephone() { logfile=/var/log/kannel/smsbox.log tac $logfile $logfile.1 \ | perl -ne "/INFO: Starting to service <.*> from <\+$1> to <([^<>]*)>/ and print \$1 and exit 0" } #from=`uriunescape "$1" | sed -e 's/\+/ /g'`; shift from=`uriunescape "$1"`; shift #to=`uriunescape "$1" | sed -e 's/\+/ /g'`; shift to=`uriunescape "$1"`; shift set -- `uriunescape "$@" | sed -e 's/\+/ /g'` #set -- `uriunescape "$@"` app_raw="$1" app=`simpleword "$1"`; shift export debug SMS_PHONE="$from" urldecode=0 export SMS_PHONE urldecode case "$SMS_REALM" in test) provider="dummy" mdpath="/home/hearth/public_webdata/hearth/content/sms" export mdpath ;; hello) provider="local" # provider="coolsms" # provider="clickatell" mdpath="/home/hearth/public_webdata/hearth/content/sms" export mdpath ;; hellobudapest) provider="clickatell" mdpath="/home/www-hearth/public_webdata/hellobudapest/content/sms" export mdpath ;; hellotorino) provider="clickatell" mdpath="/home/www-hearth/public_webdata/hellotorino/content/sms" export mdpath ;; cyber) provider="local" mdpath="/home/cyberhus/public_webdata/mdsms/content/sms" export mdpath ;; *) exit1 "unknown realm \"$SMS_REALM\"!" ;; esac case "$provider" in dummy) dummy=1 ADMIN_OK=1 export ADMIN_OK dummy ;; local) export SMS_SMSC ;; coolsms) unset SMS_SMSC SMS_URL="https://sms.coolsmsc.dk:8081/" SMS_USER="drjones1" SMS_PW="rahYSeGY" SMS_CP="cp1252" SMS_MSGTAG="message" export SMS_URL SMS_USER SMS_PW SMS_CP SMS_MSGTAG ;; # supports concatenation and req_feat (e.g 48: sender ID) # does not support DLR, and only concatenates max. 3 smses clickatell) unset SMS_SMSC SMS_URL="https://api.clickatell.com/http/sendmsg" SMS_USER="jonas" SMS_PW="inaritab6" SMS_CP="iso8859-1" SMS_USERTAG="user" SMS_API="3231280" SMS_CONCAT="3" SMS_ESCALATE="1" SMS_VALIDITY="1" SMS_REQ_FEAT="24611" # 1+2+32+8192+16384 = text+8bit+numeric_src+dlr+concat stripprefix="1" export SMS_URL SMS_USER SMS_PW SMS_CP SMS_USERTAG SMS_API SMS_CONCAT SMS_ESCALATE SMS_VALIDITY SMS_REQ_FEAT stripprefix ;; # supports DLR # Kannel clickatell profile supports concatenation but not req_feat # Kannel generic profile supports req_feat but not concatenation clickatell_via_kannel) SMS_SMSC="X" SMS_CONCATENATION="1" SMS_DLR_MASK="7" SMS_DLR_URL="http://helloearth.jones.dk/test.cgi?type=dlr&msgid=XXX&smsid=%I&from=%p&to=%P&time=%t&unixtime=%T&dlr=%d&dlrmsg=%A" SMS_CP="iso8859-1" SMS_VALIDITY="1" SMS_BINFO="req_feat=24611&concat=3&escalate=1%validity=1" # 1+2+32+8192+16384 = text+8bit+numeric_src+dlr+concat stripprefix="1" export SMS_SMSC SMS_CONCATENATION SMS_DLR_MASK SMS_DLR_URL SMS_CP SMS_VALIDITY SMS_BINFO stripprefix ;; routo_via_kannel) SMS_SMSC="Y" export SMS_SMSC ;; *) exit1 "unknown provider \"$provider\"!" ;; esac # FIXME: some safety net against abusing this imposter feature #if [ -n "$REDIRECT_OK" ]; then case "$app" in @*) # FIXME: Avoid hardcoding national prefix targetphone="$(echo "$app" | perl -pe 's/^@//;' -e 's/^([^+])/+39\1/')" # TODO: silence errors - or better: bounce those back to original sender targetfrom=$(lastservicephone "$targetphone") [ -n "$targetfrom" ] || exit1 "refusing to redirect: unknown target phone number \"$targetphone\"." SMS_PHONE="$targetfrom" $sbindir/localmarkdown2sms "$targetphone" "$@" exit 0 ;; esac #fi if [ -n "$GROUP_OK" ]; then case "$app" in !*) group="$(echo "$app" | perl -pe 's/^!//;')" members=$(cd "$vardir/user" && grep -lF "$group" */group | perl -pe "s,/group$,,") || members= [ -n "$members" ] || exit1 "Message redirected for none: no members resolved in group \"$group\"!" for member in $members; do phone=$(head -n 1 "$vardir/user/$member/phone") || nophone="${nophone+$nophone }$member" [ -z "$phone" ] || $sbindir/localmarkdown2sms "$phone" "$@" done [ -n "$members" ] || exit1 "Message redirected for none: no members resolved in group \"$group\"!" membercount=$(echo "$members" | wc --word) [ -z "$nophone" ] || nophonecount=$(echo "$nophone" | wc --word) [ -z "$nophone" ] || exit1 "Message redirected for $membercount members of group \"$group\", but failed for $nophonecount of them (could not resolve phone number)!" exit0 "Message redirected for $membercount members of group \"$group\"." ;; esac fi case "$app" in /*) cmd="$(echo "$app" | perl -pe 's/^\///;')" case "$cmd" in ping) if [ "help" = "$1" ]; then $sbindir/localsendsms "$to" "Usage: /ping [...] Respond \"pong\" and echo back any addition input." exit 0 fi $sbindir/localsendsms "$to" pong "$@" exit 0 ;; # FIXME: generalize this ugly hacks somehow, and secure against random use go|gotest|so|sotest) case "$cmd" in go*) realcmd="go" ;; so*) realcmd="so" ;; esac if [ "help" = "$1" ]; then $sbindir/localsendsms "$to" "Usage: /$realcmd PHONE MSG... /${realcmd}test PHONE MSG... /$realcmd help Send \"[$cmd] MSG...\" to ${realcmd}test subscribers, and start thread \"$realcmd\" on behalf of PHONE." exit 0 fi [ -n "$1" ] || exit1 "phone number missing (try \"/$cmd help\" for usage)" [ -n "$2" ] || exit1 "message missing (try \"/$cmd help\" for usage)" case "$cmd" in gotest|sotest) info1phone="+393453915741" # Jonas info2phone="+393453805531" # Jacob info3phone="+393453801457" # Vera ;; go|so) info1phone="+393407574815" # Alessandra info2phone="+393407574813" # Vela info3phone= ;; esac targetphone="$(echo "$1" | perl -pe 's/^@//;' -e 's/^([^+])/+39\1/')"; shift # Sanity check + set sender number info1from=$(lastservicephone "$info1phone") [ -n "$info1from" ] || exit1 "refusing to redirect: unknown info phone number \"$info1phone\"." info2from=$(lastservicephone "$info2phone") [ -n "$info2from" ] || exit1 "refusing to redirect: unknown info phone number \"$info2phone\"." if [ -n "$info3phone" ]; then info3from=$(lastservicephone "$info3phone") [ -n "$info3from" ] || exit1 "refusing to redirect: unknown info phone number \"$info3phone\"." fi targetfrom=$(lastservicephone "$targetphone") [ -n "$targetfrom" ] || exit1 "refusing to redirect: unknown target phone number \"$targetphone\"." # send responses (threaded one last, to work in debug mode) SMS_PHONE="$info1from" $sbindir/localsendsms "$info1phone" "[$cmd] $@" SMS_PHONE="$info2from" $sbindir/localsendsms "$info2phone" "[$cmd] $@" if [ -n "$info3phone" ]; then SMS_PHONE="$info3from" $sbindir/localsendsms "$info2phone" "[$cmd] $@" fi SMS_PHONE="$targetfrom" $sbindir/localmarkdown2sms "$targetphone" "/$realcmd" exit 0 ;; esac candidates=$(cd "$vardir/user" && grep -lF "$to" */phone | perl -pe "s,/phone$,,") || candidates= [ -z "$candidates" ] || $(grep -vqF "$candidates" "$vardir/group/admin/user") || admin=true if [ -n "$ADMIN_OK" ] && [ -n "$admin" ]; then str="$($sbindir/localsmsadmin "$cmd" "$@" 2>&1)" || exit1 "$str" $sbindir/localsendsms "$to" $str exit 0 elif [ "help" = "$cmd" ]; then $sbindir/localsendsms "$to" "Commands: /ping [...] /COMMAND help /help try e.g. \"/del help\"." exit 0 fi ;; esac #FIXME: if sender has a mission, check for "done": process missionpending and report result to mission members # yb) # s="$(links -dump "http://www.yubnub.org/parser/parse?command=$@")" # $sbindir/localsendsms "$to" "$s" # exit 0 # ;; $sbindir/localmarkdown2sms "$to" "$app_raw" "$@"