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