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