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