summaryrefslogtreecommitdiff
path: root/localkannel-dispatch
blob: a3e46fff42a1a56ffddecf664a176523f1a18301 (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 -MText::Unidecode -MEncode \
  23. -e 'print lc(decode("GSM0338", encode("GSM0338", decode("UTF-8", $_),' \
  24. -e 'sub {$a=unidecode(chr $_[0]); Encode::_utf8_off($a); $a;}' \
  25. -e ')));'
  26. }
  27. uriunescape() {
  28. if [ 0 = "$urldecode" ]; then
  29. echo "$@"
  30. else
  31. echo "$@" | perl -MURI::Escape -ne 'print lc(uri_unescape($_))'
  32. fi
  33. }
  34. # Resolve var from "variable = value" pair in file below /etc/local
  35. getfilevar() {
  36. file="/etc/local/$1"
  37. var="$2"
  38. grep -m1 "^$var" "$file" | awk -F '(= )' '{print $2}'
  39. }
  40. lastservicephone() {
  41. logfile=/var/log/kannel/smsbox.log
  42. tac $logfile $logfile.1 \
  43. | perl -ne "/INFO: Starting to service <.*> from <$1> to <([^<>]*)>/ and print \$1 and exit 0"
  44. }
  45. #from=`uriunescape "$1" | sed -e 's/\+/ /g'`; shift
  46. from=`uriunescape "$1"`; shift
  47. #to=`uriunescape "$1" | sed -e 's/\+/ /g'`; shift
  48. to=`uriunescape "$1"`; shift
  49. # FIXME: avoid stripping ALL plusses
  50. set -- `uriunescape "$@" | sed -e 's/\+/ /g'`
  51. #set -- `uriunescape "$@"`
  52. app_raw="$1"
  53. app=`lctransliterate "$1"`; shift
  54. export debug
  55. SMS_PHONE="$from"
  56. urldecode=0
  57. export SMS_PHONE urldecode
  58. case "$SMS_REALM" in
  59. test)
  60. provider="dummy"
  61. mdpath="/home/hearth/public_webdata/hearth/content/sms"
  62. export mdpath
  63. ;;
  64. hello)
  65. provider="local"
  66. # provider="coolsms"
  67. # provider="clickatell"
  68. mdpath="/home/hearth/public_webdata/hearth/content/sms"
  69. export mdpath
  70. ;;
  71. hellobudapest)
  72. # provider="clickatell"
  73. provider="local"
  74. addtestphones="+36307418279" # Jonas
  75. addphones=""
  76. gotestphones="+36307418279" # Jonas
  77. gophones=""
  78. SMS_ERRFROM="+36307418279" # Jonas
  79. SMS_ERRTO1="+36307418279" # Jonas
  80. SMS_ERRTO2="+36307418241" # Emma
  81. # SMS_ERRTO2="+36703782127" # Emma
  82. mdpath="/home/www-hearth/public_webdata/hellobudapest/content/sms"
  83. export SMS_ERRFROM SMS_ERRTO1 SMS_ERRTO2 mdpath
  84. ;;
  85. hellotorino)
  86. provider="clickatell"
  87. gotestphones="+393453915741 +393453805531 +393453801457" # Jonas, Jacob, Vera
  88. gophones="+393407574815 +393407574813 +393381467977 +393478617029" # Alessandra, Vela, Marco, Silvia
  89. SMS_ERRFROM="+393453915741" # Jonas
  90. SMS_ERRTO1="+393453801457" # Vera
  91. SMS_ERRTO2="+393483281187" # Rescue
  92. mdpath="/home/www-hearth/public_webdata/hellotorino/content/sms"
  93. export SMS_ERRFROM SMS_ERRTO1 SMS_ERRTO2 mdpath
  94. ;;
  95. cyber)
  96. provider="local"
  97. mdpath="/home/cyberhus/public_webdata/mdsms/content/sms"
  98. export mdpath
  99. ;;
  100. *)
  101. exit1 "unknown realm \"$SMS_REALM\"!"
  102. ;;
  103. esac
  104. case "$provider" in
  105. dummy)
  106. dummy=1
  107. ADMIN_OK=1
  108. export ADMIN_OK dummy
  109. ;;
  110. local)
  111. export SMS_SMSC
  112. ;;
  113. coolsms)
  114. unset SMS_SMSC
  115. SMS_URL="https://sms.coolsmsc.dk:8081/"
  116. SMS_USER=$(getfilevar coolsms user)
  117. SMS_PW=$(getfilevar coolsms pw)
  118. SMS_CP="cp1252"
  119. SMS_MSGTAG="message"
  120. export SMS_URL SMS_USER SMS_PW SMS_CP SMS_MSGTAG
  121. ;;
  122. # supports concatenation and req_feat (e.g 48: sender ID)
  123. # does not support DLR, and only concatenates max. 3 smses
  124. clickatell)
  125. unset SMS_SMSC
  126. SMS_URL="https://api.clickatell.com/http/sendmsg"
  127. SMS_USER=$(getfilevar clickatell user)
  128. SMS_PW=$(getfilevar clickatell pw)
  129. SMS_CP="iso8859-1"
  130. SMS_USERTAG="user"
  131. SMS_API=$(getfilevar clickatell api)
  132. SMS_CALLBACK="3"
  133. SMS_CONCAT="3"
  134. SMS_ESCALATE="1"
  135. SMS_VALIDITY="1"
  136. SMS_REQ_FEAT="24611" # 1+2+32+8192+16384 = text+8bit+numeric_src+dlr+concat
  137. stripprefix="1"
  138. 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
  139. ;;
  140. # supports DLR
  141. # Kannel clickatell profile supports concatenation but not req_feat
  142. # Kannel generic profile supports req_feat but not concatenation
  143. clickatell_via_kannel)
  144. SMS_SMSC="X"
  145. SMS_CONCATENATION="1"
  146. SMS_DLR_MASK="7"
  147. 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"
  148. SMS_CP="iso8859-1"
  149. SMS_VALIDITY="1"
  150. SMS_BINFO="req_feat=24611&concat=3&escalate=1%validity=1" # 1+2+32+8192+16384 = text+8bit+numeric_src+dlr+concat
  151. stripprefix="1"
  152. export SMS_SMSC SMS_CONCATENATION SMS_DLR_MASK SMS_DLR_URL SMS_CP SMS_VALIDITY SMS_BINFO stripprefix
  153. ;;
  154. routo_via_kannel)
  155. SMS_SMSC="Y"
  156. export SMS_SMSC
  157. ;;
  158. *)
  159. exit1 "unknown provider \"$provider\"!"
  160. ;;
  161. esac
  162. # FIXME: some safety net against abusing this imposter feature
  163. #if [ -n "$REDIRECT_OK" ]; then
  164. case "$app" in
  165. @*)
  166. # FIXME: Avoid hardcoding national prefix
  167. targetphone="$(echo "$app" | perl -pe 's/^@//;' -e 's/^([^+])/+36\1/')"
  168. # TODO: silence errors - or better: bounce those back to original sender
  169. targetfrom=$(lastservicephone "$targetphone")
  170. [ -n "$targetfrom" ] || exit1 "refusing to redirect: unknown target phone number \"$targetphone\"."
  171. SMS_PHONE="$targetfrom" $sbindir/localmarkdown2sms "$targetphone" "$@"
  172. exit 0
  173. ;;
  174. esac
  175. #fi
  176. # FIXME: some safety net against abuse
  177. #if [ -n "$GROUP_OK" ]; then
  178. case "$app" in
  179. !*)
  180. group="$(echo "$app" | perl -pe 's/^!//;')"
  181. members=$(cd "$vardir/user" && grep -lF "$group" */group | perl -pe "s,/group$,,") || members=
  182. # FIXME: check that sender is member
  183. # TODO: permit non-member as sender if $REDIRECT_OK
  184. [ -n "$members" ] || exit1 "Message not sent: no members found in group \"$group\"!"
  185. for member in $members; do
  186. phone=$(head -n 1 "$vardir/user/$member/phone") || nophone="${nophone+$nophone }$member"
  187. [ -z "$phone" ] || $sbindir/localsendsms "$phone" "$*"
  188. done
  189. membercount=$(echo "$members" | wc --word)
  190. [ -z "$nophone" ] || nophonecount=$(echo "$nophone" | wc --word)
  191. [ -z "$nophone" ] || exit1 "Message sent to $membercount members of group \"$group\", but failed for $nophonecount of them (could not resolve phone number)!"
  192. exit0 "Message sent to all $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" "$@"