summaryrefslogtreecommitdiff
path: root/localkannel-dispatch
blob: 3a9be68f9a534417e42bd2e1be4c4e810818acfc (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. 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_CALLBACK="3"
  109. SMS_CONCAT="3"
  110. SMS_ESCALATE="1"
  111. SMS_VALIDITY="1"
  112. SMS_REQ_FEAT="24611" # 1+2+32+8192+16384 = text+8bit+numeric_src+dlr+concat
  113. stripprefix="1"
  114. 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
  115. ;;
  116. # supports DLR
  117. # Kannel clickatell profile supports concatenation but not req_feat
  118. # Kannel generic profile supports req_feat but not concatenation
  119. clickatell_via_kannel)
  120. SMS_SMSC="X"
  121. SMS_CONCATENATION="1"
  122. SMS_DLR_MASK="7"
  123. 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"
  124. SMS_CP="iso8859-1"
  125. SMS_VALIDITY="1"
  126. SMS_BINFO="req_feat=24611&concat=3&escalate=1%validity=1" # 1+2+32+8192+16384 = text+8bit+numeric_src+dlr+concat
  127. stripprefix="1"
  128. export SMS_SMSC SMS_CONCATENATION SMS_DLR_MASK SMS_DLR_URL SMS_CP SMS_VALIDITY SMS_BINFO stripprefix
  129. ;;
  130. routo_via_kannel)
  131. SMS_SMSC="Y"
  132. export SMS_SMSC
  133. ;;
  134. *)
  135. exit1 "unknown provider \"$provider\"!"
  136. ;;
  137. esac
  138. # FIXME: some safety net against abusing this imposter feature
  139. #if [ -n "$REDIRECT_OK" ]; then
  140. case "$app" in
  141. @*)
  142. # FIXME: Avoid hardcoding national prefix
  143. targetphone="$(echo "$app" | perl -pe 's/^@//;' -e 's/^([^+])/+39\1/')"
  144. # TODO: silence errors - or better: bounce those back to original sender
  145. targetfrom=$(lastservicephone "$targetphone")
  146. [ -n "$targetfrom" ] || exit1 "refusing to redirect: unknown target phone number \"$targetphone\"."
  147. SMS_PHONE="$targetfrom" $sbindir/localmarkdown2sms "$targetphone" "$@"
  148. exit 0
  149. ;;
  150. esac
  151. #fi
  152. if [ -n "$GROUP_OK" ]; then
  153. case "$app" in
  154. !*)
  155. group="$(echo "$app" | perl -pe 's/^!//;')"
  156. members=$(cd "$vardir/user" && grep -lF "$group" */group | perl -pe "s,/group$,,") || members=
  157. [ -n "$members" ] || exit1 "Message redirected for none: no members resolved in group \"$group\"!"
  158. for member in $members; do
  159. phone=$(head -n 1 "$vardir/user/$member/phone") || nophone="${nophone+$nophone }$member"
  160. [ -z "$phone" ] || $sbindir/localmarkdown2sms "$phone" "$@"
  161. done
  162. [ -n "$members" ] || exit1 "Message redirected for none: no members resolved in group \"$group\"!"
  163. membercount=$(echo "$members" | wc --word)
  164. [ -z "$nophone" ] || nophonecount=$(echo "$nophone" | wc --word)
  165. [ -z "$nophone" ] || exit1 "Message redirected for $membercount members of group \"$group\", but failed for $nophonecount of them (could not resolve phone number)!"
  166. exit0 "Message redirected for $membercount members of group \"$group\"."
  167. ;;
  168. esac
  169. fi
  170. case "$app" in
  171. /*)
  172. cmd="$(echo "$app" | perl -pe 's/^\///;')"
  173. case "$cmd" in
  174. ping)
  175. if [ "help" = "$1" ]; then
  176. $sbindir/localsendsms "$to" "Usage:
  177. /ping [...]
  178. Respond \"pong\" and echo back any addition input."
  179. exit 0
  180. fi
  181. $sbindir/localsendsms "$to" pong "$@"
  182. exit 0
  183. ;;
  184. # FIXME: generalize this ugly hacks somehow, and secure against random use
  185. go|gotest|so|sotest)
  186. case "$cmd" in
  187. go*)
  188. realcmd="go"
  189. ;;
  190. so*)
  191. realcmd="so"
  192. ;;
  193. esac
  194. if [ "help" = "$1" ]; then
  195. $sbindir/localsendsms "$to" "Usage:
  196. /$realcmd PHONE [MSG...]
  197. /${realcmd}test PHONE [MSG...]
  198. /$realcmd help
  199. Send \"[$cmd] MSG...\" to ${realcmd}test subscribers,
  200. and start thread \"$realcmd\" on behalf of PHONE."
  201. exit 0
  202. fi
  203. [ -n "$1" ] || exit1 "target phone number 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. if [ -n "$*" ]; then
  230. SMS_PHONE="$info1from" $sbindir/localsendsms "$info1phone" "[$cmd] $@"
  231. SMS_PHONE="$info2from" $sbindir/localsendsms "$info2phone" "[$cmd] $@"
  232. if [ -n "$info3phone" ]; then
  233. SMS_PHONE="$info3from" $sbindir/localsendsms "$info2phone" "[$cmd] $@"
  234. fi
  235. fi
  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" "$@"