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