summaryrefslogtreecommitdiff
path: root/localkannel-dispatch
blob: b6497cd02cc2bd1d2d97ba2a426df8a5325eae8d (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. simpleword() {
  18. # echo "$1" | tr '[A-Z]' '[a-z]' | egrep '^[a-z][a-z0-9-]+$' || exit 1
  19. echo "$1" | tr '[A-Z]' '[a-z]' | egrep '^[a-zæøåÆØÅ!@/][a-zæøåÆØÅ0-9-]+$' || exit 1
  20. }
  21. uriunescape() {
  22. if [ 0 = "$urldecode" ]; then
  23. echo "$@"
  24. else
  25. echo "$@" | perl -e 'use URI::Escape; print lc(uri_unescape(<STDIN>));'
  26. fi
  27. }
  28. #from=`uriunescape "$1" | sed -e 's/\+/ /g'`; shift
  29. from=`uriunescape "$1"`; shift
  30. #to=`uriunescape "$1" | sed -e 's/\+/ /g'`; shift
  31. to=`uriunescape "$1"`; shift
  32. set -- `uriunescape "$@" | sed -e 's/\+/ /g'`
  33. #set -- `uriunescape "$@"`
  34. app_raw="$1"
  35. app=`simpleword "$1"`; shift
  36. export debug
  37. SMS_PHONE="$from"
  38. urldecode=0
  39. export SMS_PHONE urldecode
  40. case "$SMS_REALM" in
  41. test)
  42. provider="dummy"
  43. mdpath="/home/hearth/public_webdata/hearth/content/sms"
  44. export mdpath
  45. ;;
  46. hello)
  47. provider="local"
  48. # provider="coolsms"
  49. # provider="clickatell"
  50. mdpath="/home/hearth/public_webdata/hearth/content/sms"
  51. export mdpath
  52. ;;
  53. hellobudapest)
  54. provider="clickatell"
  55. mdpath="/home/www-hearth/public_webdata/hellobudapest/content/sms"
  56. export mdpath
  57. ;;
  58. hellotorino)
  59. provider="clickatell"
  60. mdpath="/home/www-hearth/public_webdata/hellotorino/content/sms"
  61. export mdpath
  62. ;;
  63. cyber)
  64. provider="local"
  65. mdpath="/home/cyberhus/public_webdata/mdsms/content/sms"
  66. export mdpath
  67. ;;
  68. *)
  69. exit1 "unknown realm \"$SMS_REALM\"!"
  70. ;;
  71. esac
  72. case "$provider" in
  73. dummy)
  74. dummy=1
  75. ADMIN_OK=1
  76. export ADMIN_OK dummy
  77. ;;
  78. local)
  79. export SMS_SMSC
  80. ;;
  81. coolsms)
  82. unset SMS_SMSC
  83. SMS_URL="https://sms.coolsmsc.dk:8081/"
  84. SMS_USER="drjones1"
  85. SMS_PW="rahYSeGY"
  86. SMS_CP="cp1252"
  87. SMS_MSGTAG="message"
  88. export SMS_URL SMS_USER SMS_PW SMS_CP SMS_MSGTAG
  89. ;;
  90. clickatell)
  91. # unset SMS_SMSC
  92. # SMS_URL="https://api.clickatell.com/http/sendmsg"
  93. # SMS_USER="jonas"
  94. # SMS_PW="inaritab6"
  95. # SMS_CP="cp1252"
  96. # SMS_USERTAG="user"
  97. # SMS_API="3231280"
  98. # SMS_CONCAT="3"
  99. # export SMS_URL SMS_USER SMS_PW SMS_CP SMS_USERTAG SMS_API SMS_CONCAT
  100. SMS_SMSC="X"
  101. SMS_DLR_MASK="7"
  102. 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"
  103. SMS_VALIDITY="1"
  104. stripprefix="1"
  105. export SMS_SMSC SMS_DLR_MASK SMS_DLR_URL SMS_VALIDITY stripprefix
  106. ;;
  107. routo)
  108. SMS_SMSC="Y"
  109. export SMS_SMSC
  110. ;;
  111. *)
  112. exit1 "unknown provider \"$provider\"!"
  113. ;;
  114. esac
  115. if [ -n "$REDIRECT_OK" ]; then
  116. case "$app" in
  117. @*)
  118. to="$(echo "$app" | perl -pe 's/^@//;' -e 's/^([^+])/+45\1/')"
  119. # TODO: silence errors - or better: bounce those back to original sender
  120. $sbindir/localmarkdown2sms "$to" "$@"
  121. exit 0
  122. ;;
  123. esac
  124. fi
  125. if [ -n "$GROUP_OK" ]; then
  126. case "$app" in
  127. !*)
  128. group="$(echo "$app" | perl -pe 's/^!//;')"
  129. members=$(cd "$vardir/user" && grep -lF "$group" */group | perl -pe "s,/group$,,") || members=
  130. [ -n "$members" ] || exit1 "Message redirected for none: no members resolved in group \"$group\"!"
  131. for member in $members; do
  132. phone=$(head -n 1 "$vardir/user/$member/phone") || nophone="${nophone+$nophone }$member"
  133. [ -z "$phone" ] || $sbindir/localmarkdown2sms "$phone" "$@"
  134. done
  135. [ -n "$members" ] || exit1 "Message redirected for none: no members resolved in group \"$group\"!"
  136. membercount=$(echo "$members" | wc --word)
  137. [ -z "$nophone" ] || nophonecount=$(echo "$nophone" | wc --word)
  138. [ -z "$nophone" ] || exit1 "Message redirected for $membercount members of group \"$group\", but failed for $nophonecount of them (could not resolve phone number)!"
  139. exit0 "Message redirected for $membercount members of group \"$group\"."
  140. ;;
  141. esac
  142. fi
  143. case "$app" in
  144. /*)
  145. candidates=$(cd "$vardir/user" && grep -lF "$to" */phone | perl -pe "s,/phone$,,") || candidates=
  146. [ -z "$candidates" ] || $(grep -vqF "$candidates" "$vardir/group/admin/user") || admin=true
  147. if [ -n "$ADMIN_OK" ] && [ -n "$admin" ]; then
  148. cmd="$(echo "$app" | perl -pe 's/^\///;')"
  149. str="$($sbindir/localsmsadmin "$cmd" "$@" 2>&1)" || exit1 "$str"
  150. $sbindir/localsendsms "$to" $str
  151. exit 0
  152. fi
  153. ;;
  154. esac
  155. #FIXME: if sender has a mission, check for "done": process missionpending and report result to mission members
  156. # yb)
  157. # s="$(links -dump "http://www.yubnub.org/parser/parse?command=$@")"
  158. # $sbindir/localsendsms "$to" "$s"
  159. # exit 0
  160. # ;;
  161. $sbindir/localmarkdown2sms "$to" "$app_raw" "$@"