#!/bin/sh set -e vardir=/var/lib/kannel/hello sbindir=/usr/local/sbin exit0() { [ -n "$1" ] && echo "$1" exit 0 } exit1() { [ -z "$1" ] || echo "Error: $1" || echo "Internal error!" 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 } #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 ;; 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 ;; clickatell) # unset SMS_SMSC # SMS_URL="https://api.clickatell.com/http/sendmsg" # SMS_USER="jonas" # SMS_PW="inaritab6" # SMS_CP="cp1252" # SMS_USERTAG="user" # SMS_API="3231280" # SMS_CONCAT="3" # export SMS_URL SMS_USER SMS_PW SMS_CP SMS_USERTAG SMS_API SMS_CONCAT SMS_SMSC="X" 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_VALIDITY="1" stripprefix="1" export SMS_SMSC SMS_DLR_MASK SMS_DLR_URL SMS_VALIDITY stripprefix ;; routo) SMS_SMSC="Y" export SMS_SMSC ;; *) exit1 "unknown provider \"$provider\"!" ;; esac if [ -n "$REDIRECT_OK" ]; then case "$app" in @*) to="$(echo "$app" | perl -pe 's/^@//;' -e 's/^([^+])/+45\1/')" # TODO: silence errors - or better: bounce those back to original sender $sbindir/localmarkdown2sms "$to" "$@" 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 /*) 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 cmd="$(echo "$app" | perl -pe 's/^\///;')" str="$($sbindir/localsmsadmin "$cmd" "$@" 2>&1)" || exit1 "$str" $sbindir/localsendsms "$to" $str exit 0 fi ;; esac # yb) # s="$(links -dump "http://www.yubnub.org/parser/parse?command=$@")" # $sbindir/localsendsms "$to" "$s" # exit 0 # ;; $sbindir/localmarkdown2sms "$to" "$app_raw" "$@"