summaryrefslogtreecommitdiff
path: root/localkannel-dispatch
blob: c967368e4963884adda02e5ffe06b671ad82b64e (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. grepescape() {
  18. echo "$1" | perl -pe 's/(?=[+])/\\/g'
  19. }
  20. # Based on Text::Unidecode bug#8017: http://rt.cpan.org/Ticket/Display.html?id=8017#txn-322351
  21. lctransliterate() {
  22. echo "$1" | perl -n \
  23. -e 'use Text::Unidecode;' \
  24. -e 'use Encode 2.12 qw(encode decode _utf8_off);' \
  25. -e 'print lc(decode("GSM0338", encode("GSM0338", decode("UTF-8", $_),' \
  26. -e 'sub {$a=unidecode(chr $_[0]); _utf8_off($a); $a;}' \
  27. -e ')));'
  28. return $res;
  29. }
  30. uriunescape() {
  31. if [ 0 = "$urldecode" ]; then
  32. echo "$@"
  33. else
  34. echo "$@" | perl -e 'use URI::Escape; print lc(uri_unescape(<STDIN>));'
  35. fi
  36. }
  37. # Resolve var from "variable = value" pair in file below /etc/local
  38. getfilevar() {
  39. file="/etc/local/$1"
  40. var="$2"
  41. grep -m1 "^$var" "$file" | awk -F '(= )' '{print $2}'
  42. }
  43. lastservicephone() {
  44. logfile=/var/log/kannel/smsbox.log
  45. tac $logfile $logfile.1 \
  46. | perl -ne "/INFO: Starting to service <.*> from <$1> to <([^<>]*)>/ and print \$1 and exit 0"
  47. }
  48. #from=`uriunescape "$1" | sed -e 's/\+/ /g'`; shift
  49. from=`uriunescape "$1"`; shift
  50. #to=`uriunescape "$1" | sed -e 's/\+/ /g'`; shift
  51. to=`uriunescape "$1"`; shift
  52. # FIXME: avoid stripping ALL plusses
  53. set -- `uriunescape "$@" | sed -e 's/\+/ /g'`
  54. #set -- `uriunescape "$@"`
  55. app_raw="$1"
  56. app=`lctransliterate "$1"`; shift
  57. export debug
  58. SMS_PHONE="$from"
  59. urldecode=0
  60. export SMS_PHONE urldecode
  61. case "$SMS_REALM" in
  62. test)
  63. provider="dummy"
  64. mdpath="/home/hearth/public_webdata/hearth/content/sms"
  65. export mdpath
  66. ;;
  67. hello)
  68. provider="local"
  69. # provider="coolsms"
  70. # provider="clickatell"
  71. mdpath="/home/hearth/public_webdata/hearth/content/sms"
  72. export mdpath
  73. ;;
  74. hellobudapest)
  75. provider="clickatell"
  76. addtestphones="+36307418279" # Jonas
  77. addphones=""
  78. gotestphones="+36307418279" # Jonas
  79. gophones=""
  80. SMS_ERRFROM="+36307418279" # Jonas
  81. SMS_ERRTO1="+36307418279" # Jonas
  82. SMS_ERRTO2="+36307418241" # Emma
  83. # SMS_ERRTO2="+36703782127" # Emma
  84. mdpath="/home/www-hearth/public_webdata/hellobudapest/content/sms"
  85. export SMS_ERRFROM SMS_ERRTO1 SMS_ERRTO2 mdpath
  86. ;;
  87. hellotorino)
  88. provider="clickatell"
  89. gotestphones="+393453915741 +393453805531 +393453801457" # Jonas, Jacob, Vera
  90. gophones="+393407574815 +393407574813 +393381467977 +393478617029" # Alessandra, Vela, Marco, Silvia
  91. SMS_ERRFROM="+393453915741" # Jonas
  92. SMS_ERRTO1="+393453801457" # Vera
  93. SMS_ERRTO2="+393483281187" # Rescue
  94. mdpath="/home/www-hearth/public_webdata/hellotorino/content/sms"
  95. export SMS_ERRFROM SMS_ERRTO1 SMS_ERRTO2 mdpath
  96. ;;
  97. cyber)
  98. provider="local"
  99. mdpath="/home/cyberhus/public_webdata/mdsms/content/sms"
  100. export mdpath
  101. ;;
  102. *)
  103. exit1 "unknown realm \"$SMS_REALM\"!"
  104. ;;
  105. esac
  106. case "$provider" in
  107. dummy)
  108. dummy=1
  109. ADMIN_OK=1
  110. export ADMIN_OK dummy
  111. ;;
  112. local)
  113. export SMS_SMSC
  114. ;;
  115. coolsms)
  116. unset SMS_SMSC
  117. SMS_URL="https://sms.coolsmsc.dk:8081/"
  118. SMS_USER=$(getfilevar coolsms user)
  119. SMS_PW=$(getfilevar coolsms pw)
  120. SMS_CP="cp1252"
  121. SMS_MSGTAG="message"
  122. export SMS_URL SMS_USER SMS_PW SMS_CP SMS_MSGTAG
  123. ;;
  124. # supports concatenation and req_feat (e.g 48: sender ID)
  125. # does not support DLR, and only concatenates max. 3 smses
  126. clickatell)
  127. unset SMS_SMSC
  128. SMS_URL="https://api.clickatell.com/http/sendmsg"
  129. SMS_USER=$(getfilevar clickatell user)
  130. SMS_PW=$(getfilevar clickatell pw)
  131. SMS_CP="iso8859-1"
  132. SMS_USERTAG="user"
  133. SMS_API=$(getfilevar clickatell api)
  134. SMS_CALLBACK="3"
  135. SMS_CONCAT="3"
  136. SMS_ESCALATE="1"
  137. SMS_VALIDITY="1"
  138. SMS_REQ_FEAT="24611" # 1+2+32+8192+16384 = text+8bit+numeric_src+dlr+concat
  139. stripprefix="1"
  140. 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
  141. ;;
  142. # supports DLR
  143. # Kannel clickatell profile supports concatenation but not req_feat
  144. # Kannel generic profile supports req_feat but not concatenation
  145. clickatell_via_kannel)
  146. SMS_SMSC="X"
  147. SMS_CONCATENATION="1"
  148. SMS_DLR_MASK="7"
  149. 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"
  150. SMS_CP="iso8859-1"
  151. SMS_VALIDITY="1"
  152. SMS_BINFO="req_feat=24611&concat=3&escalate=1%validity=1" # 1+2+32+8192+16384 = text+8bit+numeric_src+dlr+concat
  153. stripprefix="1"
  154. export SMS_SMSC SMS_CONCATENATION SMS_DLR_MASK SMS_DLR_URL SMS_CP SMS_VALIDITY SMS_BINFO stripprefix
  155. ;;
  156. routo_via_kannel)
  157. SMS_SMSC="Y"
  158. export SMS_SMSC
  159. ;;
  160. *)
  161. exit1 "unknown provider \"$provider\"!"
  162. ;;
  163. esac
  164. # FIXME: some safety net against abusing this imposter feature
  165. #if [ -n "$REDIRECT_OK" ]; then
  166. case "$app" in
  167. @*)
  168. # FIXME: Avoid hardcoding national prefix
  169. targetphone="$(echo "$app" | perl -pe 's/^@//;' -e 's/^([^+])/+36\1/')"
  170. # TODO: silence errors - or better: bounce those back to original sender
  171. targetfrom=$(lastservicephone "$targetphone")
  172. [ -n "$targetfrom" ] || exit1 "refusing to redirect: unknown target phone number \"$targetphone\"."
  173. SMS_PHONE="$targetfrom" $sbindir/localmarkdown2sms "$targetphone" "$@"
  174. exit 0
  175. ;;
  176. esac
  177. #fi
  178. # FIXME: some safety net against abuse
  179. #if [ -n "$GROUP_OK" ]; then
  180. case "$app" in
  181. !*)
  182. group="$(echo "$app" | perl -pe 's/^!//;')"
  183. members=$(cd "$vardir/user" && grep -lF "$group" */group | perl -pe "s,/group$,,") || members=
  184. # FIXME: check that sender is member
  185. # TODO: permit non-member as sender if $REDIRECT_OK
  186. [ -n "$members" ] || exit1 "Message not sent: no members found in group \"$group\"!"
  187. for member in $members; do
  188. phone=$(head -n 1 "$vardir/user/$member/phone") || nophone="${nophone+$nophone }$member"
  189. [ -z "$phone" ] || $sbindir/localsendsms "$phone" "$*"
  190. done
  191. membercount=$(echo "$members" | wc --word)
  192. [ -z "$nophone" ] || nophonecount=$(echo "$nophone" | wc --word)
  193. [ -z "$nophone" ] || exit1 "Message sent to $membercount members of group \"$group\", but failed for $nophonecount of them (could not resolve phone number)!"
  194. exit0 "Message sent to all $membercount members of group \"$group\"."
  195. ;;
  196. esac
  197. #fi
  198. case "$app" in
  199. /*)
  200. cmd="$(echo "$app" | perl -pe 's/^\///;')"
  201. case "$cmd" in
  202. ping)
  203. if [ "help" = "$1" ]; then
  204. $sbindir/localsendsms "$to" "Usage:
  205. /ping [...]
  206. Respond \"pong\" and echo back any addition input."
  207. exit 0
  208. fi
  209. $sbindir/localsendsms "$to" pong "$@"
  210. exit 0
  211. ;;
  212. add|addtest)
  213. if [ "help" = "$1" ]; then
  214. $sbindir/localsendsms "$to" "Usage:
  215. /add ID PHONE [MSG]...]
  216. Register PHONE as ID,
  217. and (if included) send MSG to subscribers."
  218. exit 0
  219. fi
  220. id="$(echo "$1" | perl -ne '/^(\d\d)$/ and print $1;')"
  221. [ -n "$id" ] || exit1 "wrong or missing id: must be 2 digits."
  222. shift
  223. # FIXME: avoid juggling with leading plus here when no longer stripped from input
  224. # phone="$(echo "$1" | perl -ne 's/^(?=[^+])/+36/;' -e '/^(\+\d\d\d+)$/ and print $1;')"
  225. phone="$(echo "$1" | perl -ne '/^(\d\d\d+)$/ and print $1;')"
  226. [ -n "$phone" ] || exit1 "wrong or missing phone number: must be only digits with leading + and at least 3 digits."
  227. targetfrom=$(lastservicephone "\+$phone")
  228. [ -n "$targetfrom" ] || exit1 "unknown phone +$phone: it must have been used with the system recently."
  229. phone="+$phone"
  230. shift
  231. case "$cmd" in
  232. addtest)
  233. msgphones="$addtestphones"
  234. ;;
  235. add)
  236. msgphones="$addphones"
  237. ;;
  238. esac
  239. str=$($sbindir/localsmsadmin add user "$id" "$phone")
  240. $sbindir/localsendsms "$to" "[info] \"$cmd\": $str."
  241. if [ -n "$*" ]; then
  242. for msgphone in $msgphones; do
  243. $sbindir/localsendsms "$msgphone" "[info] $id $phone $@"
  244. done
  245. $sbindir/localsendsms "$to" "[info] $id $phone added/updated. trailing message forwarded to subscribers."
  246. else
  247. $sbindir/localsendsms "$to" "[info] $id $phone added/updated. (no trailing message passed to subscribers)."
  248. fi
  249. exit 0
  250. ;;
  251. # FIXME: generalize this ugly hacks somehow, and secure against random use
  252. go|gotest|so|sotest)
  253. case "$cmd" in
  254. go*)
  255. basecmd="go"
  256. realcmd="hello"
  257. ;;
  258. so*)
  259. basecmd="so"
  260. realcmd="szia"
  261. ;;
  262. esac
  263. if [ "help" = "$1" ]; then
  264. $sbindir/localsendsms "$to" "Usage:
  265. /$basecmd PHONE... [MSG...]
  266. /${basecmd}test PHONE... [MSG...]
  267. /$basecmd help
  268. Start thread \"$realcmd\" on behalf of each PHONE,
  269. and (if included) send MSG to subscribers."
  270. exit 0
  271. fi
  272. [ -n "$1" ] || exit1 "target phone number missing (try \"/$cmd help\" for usage)"
  273. case "$cmd" in
  274. gotest|sotest)
  275. phones="$gotestphones"
  276. ;;
  277. go|so)
  278. phones="$gophones"
  279. ;;
  280. esac
  281. # FIXME: avoid juggling with leading plus here when no longer stripped from input
  282. # FIXME: make country code optional again when plus no longer stripped from input
  283. # FIXME: Avoid hardcoding national prefix
  284. while [ -n "$1" ]; do
  285. # targetphone="$(echo "$1" | perl -ne 's/^(?=[^+])/+36/;' -e '/^(\+\d\d\d+)$/ and print $1;')"
  286. targetphone="$(echo "$1" | perl -ne '/^(\d\d\d+)$/ and print $1;')"
  287. [ -n "$targetphone" ] || continue
  288. # Sanity check
  289. targetfrom=$(lastservicephone "\+$targetphone")
  290. [ -n "$targetfrom" ] || exit1 "refusing to redirect: unknown target phone number +$targetphone."
  291. targetphones="${targetphones:+$targetphones }\+$targetphone"
  292. shift
  293. done
  294. # send responses (threaded one last, to work in debug mode)
  295. if [ -n "$*" ]; then
  296. for phone in $phones; do
  297. $sbindir/localsendsms "$phone" "[$cmd] $@"
  298. done
  299. $sbindir/localsendsms "$to" "[info] \"$cmd\" thread started for $targetphones and trailing message forwarded to subscribers."
  300. else
  301. $sbindir/localsendsms "$to" "[info] \"$cmd\" thread started for $targetphones (no trailing message passed to subscribers)."
  302. fi
  303. for phone in $targetphones; do
  304. SMS_PHONE=$(lastservicephone "$phone") $sbindir/localmarkdown2sms "$phone" "$realcmd"
  305. done
  306. exit 0
  307. ;;
  308. esac
  309. candidates=$(cd "$vardir/user" && grep -lF "$to" */phone | perl -pe "s,/phone$,,") || candidates=
  310. [ -z "$candidates" ] || $(grep -vqF "$candidates" "$vardir/group/admin/user") || admin=true
  311. if [ -n "$ADMIN_OK" ] && [ -n "$admin" ]; then
  312. str="$($sbindir/localsmsadmin "$cmd" "$@" 2>&1)" || exit1 "$str"
  313. $sbindir/localsendsms "$to" $str
  314. exit 0
  315. elif [ "help" = "$cmd" ]; then
  316. $sbindir/localsendsms "$to" "Commands:
  317. /ping [...]
  318. /COMMAND help
  319. /help
  320. try e.g. \"/del help\"."
  321. exit 0
  322. fi
  323. ;;
  324. esac
  325. #FIXME: if sender has a mission, check for "done": process missionpending and report result to mission members
  326. # yb)
  327. # s="$(links -dump "http://www.yubnub.org/parser/parse?command=$@")"
  328. # $sbindir/localsendsms "$to" "$s"
  329. # exit 0
  330. # ;;
  331. # Bail out if another thread already active targeted same user
  332. if pgrep -f "$(grepescape "$to")"; then
  333. for errorto in $SMS_ERRTO1 $SMS_ERRTO2; do
  334. SMS_PHONE="$SMS_ERRFROM" $sbindir/localsendsms "$errorto" "[warning] silently suppressed \"$app_raw\" for $to busy already"
  335. done
  336. exit 0
  337. fi
  338. $sbindir/localmarkdown2sms "$to" "$app_raw" "$@"