summaryrefslogtreecommitdiff
path: root/localkannel-dispatch
blob: f478f6bc0ca347f59c8b44d7446830df26894505 (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" | tr '[A-Z]' '[a-z]' | egrep '^[a-z][a-z0-9-]+$' || exit 1
  19. echo "$1" | tr '[A-Z]' '[a-z]' | egrep '^[a-zæøåÆØÅ!@/][a-zæøåÆØÅ0-9-]+$' || exit 1
  20. }
  21. uriunescape() {
  22. if [ 0 = "$urldecode" ]; then
  23. echo "$@"
  24. else
  25. echo "$@" | perl -e 'use URI::Escape; print lc(uri_unescape(<STDIN>));'
  26. fi
  27. }
  28. lastservicephone() {
  29. logfile=/var/log/kannel/smsbox.log
  30. tac $logfile $logfile.1 \
  31. | perl -ne "/INFO: Starting to service <.*> from <\+$1> to <([^<>]*)>/ and print \$1 and exit 0"
  32. }
  33. #from=`uriunescape "$1" | sed -e 's/\+/ /g'`; shift
  34. from=`uriunescape "$1"`; shift
  35. #to=`uriunescape "$1" | sed -e 's/\+/ /g'`; shift
  36. to=`uriunescape "$1"`; shift
  37. set -- `uriunescape "$@" | sed -e 's/\+/ /g'`
  38. #set -- `uriunescape "$@"`
  39. app_raw="$1"
  40. app=`simpleword "$1"`; shift
  41. export debug
  42. SMS_PHONE="$from"
  43. urldecode=0
  44. export SMS_PHONE urldecode
  45. case "$SMS_REALM" in
  46. test)
  47. provider="dummy"
  48. mdpath="/home/hearth/public_webdata/hearth/content/sms"
  49. export mdpath
  50. ;;
  51. hello)
  52. provider="local"
  53. # provider="coolsms"
  54. # provider="clickatell"
  55. mdpath="/home/hearth/public_webdata/hearth/content/sms"
  56. export mdpath
  57. ;;
  58. hellobudapest)
  59. provider="clickatell"
  60. mdpath="/home/www-hearth/public_webdata/hellobudapest/content/sms"
  61. export mdpath
  62. ;;
  63. hellotorino)
  64. provider="local"
  65. SMS_ERRFROM="+393453915741"
  66. SMS_ERRTO1="+393453801457"
  67. SMS_ERRTO2="+393483281187"
  68. mdpath="/home/www-hearth/public_webdata/hellotorino/content/sms"
  69. export SMS_ERRFROM SMS_ERRTO1 SMS_ERRTO2 mdpath
  70. ;;
  71. cyber)
  72. provider="local"
  73. mdpath="/home/cyberhus/public_webdata/mdsms/content/sms"
  74. export mdpath
  75. ;;
  76. *)
  77. exit1 "unknown realm \"$SMS_REALM\"!"
  78. ;;
  79. esac
  80. case "$provider" in
  81. dummy)
  82. dummy=1
  83. ADMIN_OK=1
  84. export ADMIN_OK dummy
  85. ;;
  86. local)
  87. export SMS_SMSC
  88. ;;
  89. coolsms)
  90. unset SMS_SMSC
  91. SMS_URL="https://sms.coolsmsc.dk:8081/"
  92. SMS_USER="drjones1"
  93. SMS_PW="rahYSeGY"
  94. SMS_CP="cp1252"
  95. SMS_MSGTAG="message"
  96. export SMS_URL SMS_USER SMS_PW SMS_CP SMS_MSGTAG
  97. ;;
  98. # supports concatenation and req_feat (e.g 48: sender ID)
  99. # does not support DLR, and only concatenates max. 3 smses
  100. clickatell)
  101. unset SMS_SMSC
  102. SMS_URL="https://api.clickatell.com/http/sendmsg"
  103. SMS_USER="jonas"
  104. SMS_PW="inaritab6"
  105. SMS_CP="iso8859-1"
  106. SMS_USERTAG="user"
  107. SMS_API="3231280"
  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_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 "phone number missing (try \"/$cmd help\" for usage)"
  203. [ -n "$2" ] || exit1 "message missing (try \"/$cmd help\" for usage)"
  204. case "$cmd" in
  205. gotest|sotest)
  206. info1phone="+393453915741" # Jonas
  207. info2phone="+393453805531" # Jacob
  208. info3phone="+393453801457" # Vera
  209. ;;
  210. go|so)
  211. info1phone="+393407574815" # Alessandra
  212. info2phone="+393407574813" # Vela
  213. info3phone=
  214. ;;
  215. esac
  216. targetphone="$(echo "$1" | perl -pe 's/^@//;' -e 's/^([^+])/+39\1/')"; shift
  217. # Sanity check + set sender number
  218. info1from=$(lastservicephone "$info1phone")
  219. [ -n "$info1from" ] || exit1 "refusing to redirect: unknown info phone number \"$info1phone\"."
  220. info2from=$(lastservicephone "$info2phone")
  221. [ -n "$info2from" ] || exit1 "refusing to redirect: unknown info phone number \"$info2phone\"."
  222. if [ -n "$info3phone" ]; then
  223. info3from=$(lastservicephone "$info3phone")
  224. [ -n "$info3from" ] || exit1 "refusing to redirect: unknown info phone number \"$info3phone\"."
  225. fi
  226. targetfrom=$(lastservicephone "$targetphone")
  227. [ -n "$targetfrom" ] || exit1 "refusing to redirect: unknown target phone number \"$targetphone\"."
  228. # send responses (threaded one last, to work in debug mode)
  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. SMS_PHONE="$targetfrom" $sbindir/localmarkdown2sms "$targetphone" "/$realcmd"
  235. exit 0
  236. ;;
  237. esac
  238. candidates=$(cd "$vardir/user" && grep -lF "$to" */phone | perl -pe "s,/phone$,,") || candidates=
  239. [ -z "$candidates" ] || $(grep -vqF "$candidates" "$vardir/group/admin/user") || admin=true
  240. if [ -n "$ADMIN_OK" ] && [ -n "$admin" ]; then
  241. str="$($sbindir/localsmsadmin "$cmd" "$@" 2>&1)" || exit1 "$str"
  242. $sbindir/localsendsms "$to" $str
  243. exit 0
  244. elif [ "help" = "$cmd" ]; then
  245. $sbindir/localsendsms "$to" "Commands:
  246. /ping [...]
  247. /COMMAND help
  248. /help
  249. try e.g. \"/del help\"."
  250. exit 0
  251. fi
  252. ;;
  253. esac
  254. #FIXME: if sender has a mission, check for "done": process missionpending and report result to mission members
  255. # yb)
  256. # s="$(links -dump "http://www.yubnub.org/parser/parse?command=$@")"
  257. # $sbindir/localsendsms "$to" "$s"
  258. # exit 0
  259. # ;;
  260. $sbindir/localmarkdown2sms "$to" "$app_raw" "$@"