summaryrefslogtreecommitdiff
path: root/localkannel-dispatch
blob: a1427626ede467b83aaed37d28a2adf9eb3b3aac (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(encode("UTF-8", 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. if [ -n "$GROUP_OK" ]; then
  179. case "$app" in
  180. !*)
  181. group="$(echo "$app" | perl -pe 's/^!//;')"
  182. members=$(cd "$vardir/user" && grep -lF "$group" */group | perl -pe "s,/group$,,") || members=
  183. [ -n "$members" ] || exit1 "Message redirected for none: no members resolved in group \"$group\"!"
  184. for member in $members; do
  185. phone=$(head -n 1 "$vardir/user/$member/phone") || nophone="${nophone+$nophone }$member"
  186. [ -z "$phone" ] || $sbindir/localmarkdown2sms "$phone" "$@"
  187. done
  188. [ -n "$members" ] || exit1 "Message redirected for none: no members resolved in group \"$group\"!"
  189. membercount=$(echo "$members" | wc --word)
  190. [ -z "$nophone" ] || nophonecount=$(echo "$nophone" | wc --word)
  191. [ -z "$nophone" ] || exit1 "Message redirected for $membercount members of group \"$group\", but failed for $nophonecount of them (could not resolve phone number)!"
  192. exit0 "Message redirected for $membercount members of group \"$group\"."
  193. ;;
  194. esac
  195. fi
  196. case "$app" in
  197. /*)
  198. cmd="$(echo "$app" | perl -pe 's/^\///;')"
  199. case "$cmd" in
  200. ping)
  201. if [ "help" = "$1" ]; then
  202. $sbindir/localsendsms "$to" "Usage:
  203. /ping [...]
  204. Respond \"pong\" and echo back any addition input."
  205. exit 0
  206. fi
  207. $sbindir/localsendsms "$to" pong "$@"
  208. exit 0
  209. ;;
  210. add|addtest)
  211. if [ "help" = "$1" ]; then
  212. $sbindir/localsendsms "$to" "Usage:
  213. /add ID PHONE [MSG]...]
  214. Register PHONE as ID,
  215. and (if included) send MSG to subscribers."
  216. exit 0
  217. fi
  218. id="$(echo "$1" | perl -ne '/^(\d\d)$/ and print $1;')"
  219. [ -n "$id" ] || exit1 "wrong or missing id: must be 2 digits."
  220. shift
  221. # FIXME: avoid juggling with leading plus here when no longer stripped from input
  222. # phone="$(echo "$1" | perl -ne 's/^(?=[^+])/+36/;' -e '/^(\+\d\d\d+)$/ and print $1;')"
  223. phone="$(echo "$1" | perl -ne '/^(\d\d\d+)$/ and print $1;')"
  224. [ -n "$phone" ] || exit1 "wrong or missing phone number: must be only digits with leading + and at least 3 digits."
  225. targetfrom=$(lastservicephone "\+$phone")
  226. [ -n "$targetfrom" ] || exit1 "unknown phone +$phone: it must have been used with the system recently."
  227. phone="+$phone"
  228. shift
  229. case "$cmd" in
  230. addtest)
  231. msgphones="$addtestphones"
  232. ;;
  233. add)
  234. msgphones="$addphones"
  235. ;;
  236. esac
  237. str=$($sbindir/localsmsadmin add user "$id" "$phone")
  238. $sbindir/localsendsms "$to" "[info] \"$cmd\": $str."
  239. if [ -n "$*" ]; then
  240. for msgphone in $msgphones; do
  241. $sbindir/localsendsms "$msgphone" "[info] $id $phone $@"
  242. done
  243. $sbindir/localsendsms "$to" "[info] $id $phone added/updated. trailing message forwarded to subscribers."
  244. else
  245. $sbindir/localsendsms "$to" "[info] $id $phone added/updated. (no trailing message passed to subscribers)."
  246. fi
  247. exit 0
  248. ;;
  249. # FIXME: generalize this ugly hacks somehow, and secure against random use
  250. go|gotest|so|sotest)
  251. case "$cmd" in
  252. go*)
  253. basecmd="go"
  254. realcmd="hello"
  255. ;;
  256. so*)
  257. basecmd="so"
  258. realcmd="szia"
  259. ;;
  260. esac
  261. if [ "help" = "$1" ]; then
  262. $sbindir/localsendsms "$to" "Usage:
  263. /$basecmd PHONE... [MSG...]
  264. /${basecmd}test PHONE... [MSG...]
  265. /$basecmd help
  266. Start thread \"$realcmd\" on behalf of each PHONE,
  267. and (if included) send MSG to subscribers."
  268. exit 0
  269. fi
  270. [ -n "$1" ] || exit1 "target phone number missing (try \"/$cmd help\" for usage)"
  271. case "$cmd" in
  272. gotest|sotest)
  273. phones="$gotestphones"
  274. ;;
  275. go|so)
  276. phones="$gophones"
  277. ;;
  278. esac
  279. # FIXME: avoid juggling with leading plus here when no longer stripped from input
  280. # FIXME: make country code optional again when plus no longer stripped from input
  281. # FIXME: Avoid hardcoding national prefix
  282. while [ -n "$1" ]; do
  283. # targetphone="$(echo "$1" | perl -ne 's/^(?=[^+])/+36/;' -e '/^(\+\d\d\d+)$/ and print $1;')"
  284. targetphone="$(echo "$1" | perl -ne '/^(\d\d\d+)$/ and print $1;')"
  285. [ -n "$targetphone" ] || continue
  286. # Sanity check
  287. targetfrom=$(lastservicephone "\+$targetphone")
  288. [ -n "$targetfrom" ] || exit1 "refusing to redirect: unknown target phone number +$targetphone."
  289. targetphones="${targetphones:+$targetphones }\+$targetphone"
  290. shift
  291. done
  292. # send responses (threaded one last, to work in debug mode)
  293. if [ -n "$*" ]; then
  294. for phone in $phones; do
  295. $sbindir/localsendsms "$phone" "[$cmd] $@"
  296. done
  297. $sbindir/localsendsms "$to" "[info] \"$cmd\" thread started for $targetphones and trailing message forwarded to subscribers."
  298. else
  299. $sbindir/localsendsms "$to" "[info] \"$cmd\" thread started for $targetphones (no trailing message passed to subscribers)."
  300. fi
  301. for phone in $targetphones; do
  302. SMS_PHONE=$(lastservicephone "$phone") $sbindir/localmarkdown2sms "$phone" "$realcmd"
  303. done
  304. exit 0
  305. ;;
  306. esac
  307. candidates=$(cd "$vardir/user" && grep -lF "$to" */phone | perl -pe "s,/phone$,,") || candidates=
  308. [ -z "$candidates" ] || $(grep -vqF "$candidates" "$vardir/group/admin/user") || admin=true
  309. if [ -n "$ADMIN_OK" ] && [ -n "$admin" ]; then
  310. str="$($sbindir/localsmsadmin "$cmd" "$@" 2>&1)" || exit1 "$str"
  311. $sbindir/localsendsms "$to" $str
  312. exit 0
  313. elif [ "help" = "$cmd" ]; then
  314. $sbindir/localsendsms "$to" "Commands:
  315. /ping [...]
  316. /COMMAND help
  317. /help
  318. try e.g. \"/del help\"."
  319. exit 0
  320. fi
  321. ;;
  322. esac
  323. #FIXME: if sender has a mission, check for "done": process missionpending and report result to mission members
  324. # yb)
  325. # s="$(links -dump "http://www.yubnub.org/parser/parse?command=$@")"
  326. # $sbindir/localsendsms "$to" "$s"
  327. # exit 0
  328. # ;;
  329. # Bail out if another thread already active targeted same user
  330. if pgrep -f "$(grepescape "$to")"; then
  331. for errorto in $SMS_ERRTO1 $SMS_ERRTO2; do
  332. SMS_PHONE="$SMS_ERRFROM" $sbindir/localsendsms "$errorto" "[warning] silently suppressed \"$app_raw\" for $to busy already"
  333. done
  334. exit 0
  335. fi
  336. $sbindir/localmarkdown2sms "$to" "$app_raw" "$@"