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