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