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