summaryrefslogtreecommitdiff
path: root/localkannel-dispatch
blob: 2a9927c1fa03f13efde10c172fd15581828ca1e3 (plain)
  1. #!/bin/sh
  2. set -e
  3. vardir=/var/lib/kannel/hello
  4. sbindir=/usr/local/sbin
  5. exit0() {
  6. [ -n "$1" ] && echo "$1"
  7. exit 0
  8. }
  9. exit1() {
  10. response="${1:+Error: }${1:-Internal error!}"
  11. # FIXME: pass error messages via stderr (not stdout)
  12. # echo >&2 "$response"
  13. echo "$response"
  14. [ -z "$SMS_SMSC$SMS_URL" ] || $sbindir/localsendsms "$to" "$response"
  15. exit 1
  16. }
  17. simpleword() {
  18. echo "$1" | perl -ne '/([a-zæøåÆØÅ!@\/][a-zæøåÆØÅ0-9-]+)/i and print lc($1)'
  19. }
  20. uriunescape() {
  21. if [ 0 = "$urldecode" ]; then
  22. echo "$@"
  23. else
  24. echo "$@" | perl -e 'use URI::Escape; print lc(uri_unescape(<STDIN>));'
  25. fi
  26. }
  27. lastservicephone() {
  28. logfile=/var/log/kannel/smsbox.log
  29. tac $logfile $logfile.1 \
  30. | perl -ne "/INFO: Starting to service <.*> from <\+$1> to <([^<>]*)>/ and print \$1 and exit 0"
  31. }
  32. #from=`uriunescape "$1" | sed -e 's/\+/ /g'`; shift
  33. from=`uriunescape "$1"`; shift
  34. #to=`uriunescape "$1" | sed -e 's/\+/ /g'`; shift
  35. to=`uriunescape "$1"`; shift
  36. set -- `uriunescape "$@" | sed -e 's/\+/ /g'`
  37. #set -- `uriunescape "$@"`
  38. app_raw="$1"
  39. app=`simpleword "$1"`; shift
  40. export debug
  41. SMS_PHONE="$from"
  42. urldecode=0
  43. export SMS_PHONE urldecode
  44. case "$SMS_REALM" in
  45. test)
  46. provider="dummy"
  47. mdpath="/home/hearth/public_webdata/hearth/content/sms"
  48. export mdpath
  49. ;;
  50. hello)
  51. provider="local"
  52. # provider="coolsms"
  53. # provider="clickatell"
  54. mdpath="/home/hearth/public_webdata/hearth/content/sms"
  55. export mdpath
  56. ;;
  57. hellobudapest)
  58. provider="clickatell"
  59. mdpath="/home/www-hearth/public_webdata/hellobudapest/content/sms"
  60. export mdpath
  61. ;;
  62. hellotorino)
  63. provider="clickatell"
  64. gotestphones="+393453915741 +393453805531 +393453801457" # Jonas, Jacob, Vera
  65. gophones="+393407574815 +393407574813" # Alessandra, Vela
  66. SMS_ERRFROM="+393453915741"
  67. SMS_ERRTO1="+393453801457"
  68. SMS_ERRTO2="+393483281187"
  69. mdpath="/home/www-hearth/public_webdata/hellotorino/content/sms"
  70. export SMS_ERRFROM SMS_ERRTO1 SMS_ERRTO2 mdpath
  71. ;;
  72. cyber)
  73. provider="local"
  74. mdpath="/home/cyberhus/public_webdata/mdsms/content/sms"
  75. export mdpath
  76. ;;
  77. *)
  78. exit1 "unknown realm \"$SMS_REALM\"!"
  79. ;;
  80. esac
  81. case "$provider" in
  82. dummy)
  83. dummy=1
  84. ADMIN_OK=1
  85. export ADMIN_OK dummy
  86. ;;
  87. local)
  88. export SMS_SMSC
  89. ;;
  90. coolsms)
  91. unset SMS_SMSC
  92. SMS_URL="https://sms.coolsmsc.dk:8081/"
  93. SMS_USER="drjones1"
  94. SMS_PW="rahYSeGY"
  95. SMS_CP="cp1252"
  96. SMS_MSGTAG="message"
  97. export SMS_URL SMS_USER SMS_PW SMS_CP SMS_MSGTAG
  98. ;;
  99. # supports concatenation and req_feat (e.g 48: sender ID)
  100. # does not support DLR, and only concatenates max. 3 smses
  101. clickatell)
  102. unset SMS_SMSC
  103. SMS_URL="https://api.clickatell.com/http/sendmsg"
  104. SMS_USER="jonas"
  105. SMS_PW="inaritab6"
  106. SMS_CP="iso8859-1"
  107. SMS_USERTAG="user"
  108. SMS_API="3231280"
  109. SMS_CALLBACK="3"
  110. SMS_CONCAT="3"
  111. SMS_ESCALATE="1"
  112. SMS_VALIDITY="1"
  113. SMS_REQ_FEAT="24611" # 1+2+32+8192+16384 = text+8bit+numeric_src+dlr+concat
  114. stripprefix="1"
  115. export SMS_URL SMS_USER SMS_PW SMS_CP SMS_USERTAG SMS_API SMS_CALLBACK SMS_CONCAT SMS_ESCALATE SMS_VALIDITY SMS_REQ_FEAT stripprefix
  116. ;;
  117. # supports DLR
  118. # Kannel clickatell profile supports concatenation but not req_feat
  119. # Kannel generic profile supports req_feat but not concatenation
  120. clickatell_via_kannel)
  121. SMS_SMSC="X"
  122. SMS_CONCATENATION="1"
  123. SMS_DLR_MASK="7"
  124. 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"
  125. SMS_CP="iso8859-1"
  126. SMS_VALIDITY="1"
  127. SMS_BINFO="req_feat=24611&concat=3&escalate=1%validity=1" # 1+2+32+8192+16384 = text+8bit+numeric_src+dlr+concat
  128. stripprefix="1"
  129. export SMS_SMSC SMS_CONCATENATION SMS_DLR_MASK SMS_DLR_URL SMS_CP SMS_VALIDITY SMS_BINFO stripprefix
  130. ;;
  131. routo_via_kannel)
  132. SMS_SMSC="Y"
  133. export SMS_SMSC
  134. ;;
  135. *)
  136. exit1 "unknown provider \"$provider\"!"
  137. ;;
  138. esac
  139. # FIXME: some safety net against abusing this imposter feature
  140. #if [ -n "$REDIRECT_OK" ]; then
  141. case "$app" in
  142. @*)
  143. # FIXME: Avoid hardcoding national prefix
  144. targetphone="$(echo "$app" | perl -pe 's/^@//;' -e 's/^([^+])/+39\1/')"
  145. # TODO: silence errors - or better: bounce those back to original sender
  146. targetfrom=$(lastservicephone "$targetphone")
  147. [ -n "$targetfrom" ] || exit1 "refusing to redirect: unknown target phone number \"$targetphone\"."
  148. SMS_PHONE="$targetfrom" $sbindir/localmarkdown2sms "$targetphone" "$@"
  149. exit 0
  150. ;;
  151. esac
  152. #fi
  153. if [ -n "$GROUP_OK" ]; then
  154. case "$app" in
  155. !*)
  156. group="$(echo "$app" | perl -pe 's/^!//;')"
  157. members=$(cd "$vardir/user" && grep -lF "$group" */group | perl -pe "s,/group$,,") || members=
  158. [ -n "$members" ] || exit1 "Message redirected for none: no members resolved in group \"$group\"!"
  159. for member in $members; do
  160. phone=$(head -n 1 "$vardir/user/$member/phone") || nophone="${nophone+$nophone }$member"
  161. [ -z "$phone" ] || $sbindir/localmarkdown2sms "$phone" "$@"
  162. done
  163. [ -n "$members" ] || exit1 "Message redirected for none: no members resolved in group \"$group\"!"
  164. membercount=$(echo "$members" | wc --word)
  165. [ -z "$nophone" ] || nophonecount=$(echo "$nophone" | wc --word)
  166. [ -z "$nophone" ] || exit1 "Message redirected for $membercount members of group \"$group\", but failed for $nophonecount of them (could not resolve phone number)!"
  167. exit0 "Message redirected for $membercount members of group \"$group\"."
  168. ;;
  169. esac
  170. fi
  171. case "$app" in
  172. /*)
  173. cmd="$(echo "$app" | perl -pe 's/^\///;')"
  174. case "$cmd" in
  175. ping)
  176. if [ "help" = "$1" ]; then
  177. $sbindir/localsendsms "$to" "Usage:
  178. /ping [...]
  179. Respond \"pong\" and echo back any addition input."
  180. exit 0
  181. fi
  182. $sbindir/localsendsms "$to" pong "$@"
  183. exit 0
  184. ;;
  185. # FIXME: generalize this ugly hacks somehow, and secure against random use
  186. go|gotest|so|sotest)
  187. case "$cmd" in
  188. go*)
  189. realcmd="go"
  190. ;;
  191. so*)
  192. realcmd="so"
  193. ;;
  194. esac
  195. if [ "help" = "$1" ]; then
  196. $sbindir/localsendsms "$to" "Usage:
  197. /$realcmd PHONE [MSG...]
  198. /${realcmd}test PHONE [MSG...]
  199. /$realcmd help
  200. Send \"[$cmd] MSG...\" to ${realcmd}test subscribers,
  201. and start thread \"$realcmd\" on behalf of PHONE."
  202. exit 0
  203. fi
  204. [ -n "$1" ] || exit1 "target phone number missing (try \"/$cmd help\" for usage)"
  205. case "$cmd" in
  206. gotest|sotest)
  207. phones="$gotestphones"
  208. ;;
  209. go|so)
  210. phones="$gophones"
  211. ;;
  212. esac
  213. targetphone="$(echo "$1" | perl -pe 's/^@//;' -e 's/^([^+])/+39\1/')"; shift
  214. # Sanity check + set sender number
  215. targetfrom=$(lastservicephone "$targetphone")
  216. [ -n "$targetfrom" ] || exit1 "refusing to redirect: unknown target phone number \"$targetphone\"."
  217. # send responses (threaded one last, to work in debug mode)
  218. if [ -n "$*" ]; then
  219. for phone in $phones; do
  220. $sbindir/localsendsms "$phone" "[$cmd] $@"
  221. done
  222. $sbindir/localsendsms "$to" "[info] \"$cmd\" thread started for \"$targetphone\", and trailing message forwarded to subscribers."
  223. else
  224. $sbindir/localsendsms "$to" "[info] \"$cmd\" thread started for \"$targetphone\" (no trailing message, so nothing forwarded to subscribers)."
  225. fi
  226. SMS_PHONE="$targetfrom" $sbindir/localmarkdown2sms "$targetphone" "/$realcmd"
  227. exit 0
  228. ;;
  229. esac
  230. candidates=$(cd "$vardir/user" && grep -lF "$to" */phone | perl -pe "s,/phone$,,") || candidates=
  231. [ -z "$candidates" ] || $(grep -vqF "$candidates" "$vardir/group/admin/user") || admin=true
  232. if [ -n "$ADMIN_OK" ] && [ -n "$admin" ]; then
  233. str="$($sbindir/localsmsadmin "$cmd" "$@" 2>&1)" || exit1 "$str"
  234. $sbindir/localsendsms "$to" $str
  235. exit 0
  236. elif [ "help" = "$cmd" ]; then
  237. $sbindir/localsendsms "$to" "Commands:
  238. /ping [...]
  239. /COMMAND help
  240. /help
  241. try e.g. \"/del help\"."
  242. exit 0
  243. fi
  244. ;;
  245. esac
  246. #FIXME: if sender has a mission, check for "done": process missionpending and report result to mission members
  247. # yb)
  248. # s="$(links -dump "http://www.yubnub.org/parser/parse?command=$@")"
  249. # $sbindir/localsendsms "$to" "$s"
  250. # exit 0
  251. # ;;
  252. $sbindir/localmarkdown2sms "$to" "$app_raw" "$@"