summaryrefslogtreecommitdiff
path: root/localkannel-dispatch
blob: e9e0c7e486387e8bb5c6da04e421d03704ea6dac (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 these ugly hacks somehow, and secure against random use
  181. go)
  182. if [ "help" = "$1" ]; then
  183. $sbindir/localsendsms "$to" "Usage:
  184. /go PHONE MSG...
  185. Start thread \"go\" on behalf of PHONE.
  186. Send \"[go] MSG\" to Alessandra and Vela."
  187. exit 0
  188. fi
  189. [ -n "$1" ] || exit1 "phone number missing (try \"/go help\" for usage)"
  190. [ -n "$2" ] || exit1 "message missing (try \"/go help\" for usage)"
  191. targetphone="$(echo "$1" | perl -pe 's/^@//;' -e 's/^([^+])/+39\1/')"; shift
  192. info1phone="+393273009765" # Alessandra
  193. info2phone="+393276733618" # Vela
  194. targetfrom=$(lastservicephone "$targetphone")
  195. [ -n "$targetfrom" ] || exit1 "refusing to redirect: unknown target phone number \"$targetphone\"."
  196. info1from=$(lastservicephone "$info1phone")
  197. [ -n "$info1from" ] || exit1 "refusing to redirect: unknown info phone number \"$info1phone\"."
  198. info2from=$(lastservicephone "$info2phone")
  199. [ -n "$info2from" ] || exit1 "refusing to redirect: unknown info phone number \"$info2phone\"."
  200. SMS_PHONE="$targetfrom" $sbindir/localmarkdown2sms "$targetphone" "/go"
  201. SMS_PHONE="$info1from" $sbindir/localsendsms "$info1phone" "[go] $@"
  202. SMS_PHONE="$info2from" $sbindir/localsendsms "$info2phone" "[go] $@"
  203. exit 0
  204. ;;
  205. gotest)
  206. if [ "help" = "$1" ]; then
  207. $sbindir/localsendsms "$to" "Usage:
  208. /gotest PHONE MSG...
  209. Start thread \"go\" on behalf of PHONE.
  210. Report MSG to Jonas, Jacob and Vera."
  211. exit 0
  212. fi
  213. [ -n "$1" ] || exit1 "phone number missing (try \"/go help\" for usage)"
  214. [ -n "$2" ] || exit1 "message missing (try \"/go help\" for usage)"
  215. targetphone="$(echo "$1" | perl -pe 's/^@//;' -e 's/^([^+])/+39\1/')"; shift
  216. info1phone="+393453915741" # Jonas
  217. info2phone="+393453805531" # Jacob
  218. info3phone="+393453801457" # Vera
  219. targetfrom=$(lastservicephone "$targetphone")
  220. [ -n "$targetfrom" ] || exit1 "refusing to redirect: unknown target phone number \"$targetphone\"."
  221. info1from=$(lastservicephone "$info1phone")
  222. [ -n "$info1from" ] || exit1 "refusing to redirect: unknown info phone number \"$info1phone\"."
  223. info2from=$(lastservicephone "$info2phone")
  224. [ -n "$info2from" ] || exit1 "refusing to redirect: unknown info phone number \"$info2phone\"."
  225. info3from=$(lastservicephone "$info3phone")
  226. [ -n "$info3from" ] || exit1 "refusing to redirect: unknown info phone number \"$info3phone\"."
  227. SMS_PHONE="$targetfrom" $sbindir/localmarkdown2sms "$targetphone" "/go"
  228. SMS_PHONE="$info1from" $sbindir/localsendsms "$info1phone" "[go] $@"
  229. SMS_PHONE="$info2from" $sbindir/localsendsms "$info2phone" "[go] $@"
  230. SMS_PHONE="$info3from" $sbindir/localsendsms "$info3phone" "[go] $@"
  231. exit 0
  232. ;;
  233. esac
  234. candidates=$(cd "$vardir/user" && grep -lF "$to" */phone | perl -pe "s,/phone$,,") || candidates=
  235. [ -z "$candidates" ] || $(grep -vqF "$candidates" "$vardir/group/admin/user") || admin=true
  236. if [ -n "$ADMIN_OK" ] && [ -n "$admin" ]; then
  237. str="$($sbindir/localsmsadmin "$cmd" "$@" 2>&1)" || exit1 "$str"
  238. $sbindir/localsendsms "$to" $str
  239. exit 0
  240. elif [ "help" = "$cmd" ]; then
  241. $sbindir/localsendsms "$to" "Commands:
  242. /ping [...]
  243. /COMMAND help
  244. /help
  245. try e.g. \"/del help\"."
  246. exit 0
  247. fi
  248. ;;
  249. esac
  250. #FIXME: if sender has a mission, check for "done": process missionpending and report result to mission members
  251. # yb)
  252. # s="$(links -dump "http://www.yubnub.org/parser/parse?command=$@")"
  253. # $sbindir/localsendsms "$to" "$s"
  254. # exit 0
  255. # ;;
  256. $sbindir/localmarkdown2sms "$to" "$app_raw" "$@"