summaryrefslogtreecommitdiff
path: root/localkannel-dispatch
blob: 4f801d9e32d84acd1e1a67adadd4f215cc91a4cd (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="+36307418241" # 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. add)
  198. if [ "help" = "$1" ]; then
  199. $sbindir/localsendsms "$to" "Usage:
  200. /add ID PHONE [MSG]...]
  201. Register PHONE as ID,
  202. and (if included) send MSG to subscribers."
  203. exit 0
  204. fi
  205. id="$(echo "$1" | perl -ne '/^(\d\d)$/ and print $1;')"
  206. [ -n "$id" ] || exit1 "wrong or missing id: must be 2 digits."
  207. shift
  208. # FIXME: avoid juggling with leading plus here when no longer stripped from input
  209. # phone="$(echo "$1" | perl -ne 's/^(?=[^+])/+36/;' -e '/^(\+\d\d\d+)$/ and print $1;')"
  210. phone="$(echo "$1" | perl -ne '/^(\d\d\d+)$/ and print $1;')"
  211. [ -n "$phone" ] || exit1 "wrong or missing phone number: must be only digits with leading + and at least 3 digits."
  212. targetfrom=$(lastservicephone "\+$phone")
  213. [ -n "$targetfrom" ] || exit1 "unknown phone +$phone: it must have been used with the system recently."
  214. phone="+$phone"
  215. shift
  216. str=$($sbindir/localsmsadmin add user "$id" "$phone")
  217. $sbindir/localsendsms "$to" "[info] \"$cmd\": $str."
  218. exit 0
  219. ;;
  220. # FIXME: generalize this ugly hacks somehow, and secure against random use
  221. go|gotest|so|sotest)
  222. case "$cmd" in
  223. go*)
  224. basecmd="go"
  225. realcmd="hello"
  226. ;;
  227. so*)
  228. basecmd="so"
  229. realcmd="szia"
  230. ;;
  231. esac
  232. if [ "help" = "$1" ]; then
  233. $sbindir/localsendsms "$to" "Usage:
  234. /$basecmd PHONE... [MSG...]
  235. /${basecmd}test PHONE... [MSG...]
  236. /$basecmd help
  237. Start thread \"$realcmd\" on behalf of each PHONE,
  238. and (if included) send MSG to subscribers."
  239. exit 0
  240. fi
  241. [ -n "$1" ] || exit1 "target phone number missing (try \"/$cmd help\" for usage)"
  242. case "$cmd" in
  243. gotest|sotest)
  244. phones="$gotestphones"
  245. ;;
  246. go|so)
  247. phones="$gophones"
  248. ;;
  249. esac
  250. # FIXME: avoid juggling with leading plus here when no longer stripped from input
  251. # FIXME: make country code optional again when plus no longer stripped from input
  252. # FIXME: Avoid hardcoding national prefix
  253. while [ -n "$1" ]; do
  254. # targetphone="$(echo "$1" | perl -ne 's/^(?=[^+])/+36/;' -e '/^(\+\d\d\d+)$/ and print $1;')"
  255. targetphone="$(echo "$1" | perl -ne '/^(\d\d\d+)$/ and print $1;')"
  256. [ -n "$targetphone" ] || continue
  257. # Sanity check
  258. targetfrom=$(lastservicephone "\+$targetphone")
  259. [ -n "$targetfrom" ] || exit1 "refusing to redirect: unknown target phone number +$targetphone."
  260. targetphones="${targetphones:+$targetphones }\+$targetphone"
  261. shift
  262. done
  263. # send responses (threaded one last, to work in debug mode)
  264. if [ -n "$*" ]; then
  265. for phone in $phones; do
  266. $sbindir/localsendsms "$phone" "[$cmd] $@"
  267. done
  268. $sbindir/localsendsms "$to" "[info] \"$cmd\" thread started for $targetphones and trailing message forwarded to subscribers."
  269. else
  270. $sbindir/localsendsms "$to" "[info] \"$cmd\" thread started for $targetphones (no trailing message passed to subscribers)."
  271. fi
  272. for phone in $targetphones; do
  273. SMS_PHONE=$(lastservicephone "$phone") $sbindir/localmarkdown2sms "$phone" "$realcmd"
  274. done
  275. exit 0
  276. ;;
  277. esac
  278. candidates=$(cd "$vardir/user" && grep -lF "$to" */phone | perl -pe "s,/phone$,,") || candidates=
  279. [ -z "$candidates" ] || $(grep -vqF "$candidates" "$vardir/group/admin/user") || admin=true
  280. if [ -n "$ADMIN_OK" ] && [ -n "$admin" ]; then
  281. str="$($sbindir/localsmsadmin "$cmd" "$@" 2>&1)" || exit1 "$str"
  282. $sbindir/localsendsms "$to" $str
  283. exit 0
  284. elif [ "help" = "$cmd" ]; then
  285. $sbindir/localsendsms "$to" "Commands:
  286. /ping [...]
  287. /COMMAND help
  288. /help
  289. try e.g. \"/del help\"."
  290. exit 0
  291. fi
  292. ;;
  293. esac
  294. #FIXME: if sender has a mission, check for "done": process missionpending and report result to mission members
  295. # yb)
  296. # s="$(links -dump "http://www.yubnub.org/parser/parse?command=$@")"
  297. # $sbindir/localsendsms "$to" "$s"
  298. # exit 0
  299. # ;;
  300. $sbindir/localmarkdown2sms "$to" "$app_raw" "$@"