summaryrefslogtreecommitdiff
path: root/localkannel-dispatch
blob: 8a891d6d12fecc379a0b6cdb25b89a9758441076 (plain)
  1. #!/bin/sh
  2. set -e
  3. exit1() {
  4. echo >&2 "Error: $1"
  5. echo >&2 "Exiting..."
  6. exit 1
  7. }
  8. simpleword() {
  9. # echo "$1" | tr '[A-Z]' '[a-z]' | egrep '^[a-z][a-z0-9-]+$' || exit 1
  10. echo "$1" | tr '[A-Z]' '[a-z]' | egrep '^[a-zæøåÆØÅ!@/][a-zæøåÆØÅ0-9-]+$' || exit 1
  11. }
  12. uriunescape() {
  13. if [ 0 = "$urldecode" ]; then
  14. echo "$@"
  15. else
  16. echo "$@" | perl -e 'use URI::Escape; print lc(uri_unescape(<STDIN>));'
  17. fi
  18. }
  19. #from=`uriunescape "$1" | sed -e 's/\+/ /g'`; shift
  20. from=`uriunescape "$1"`; shift
  21. #to=`uriunescape "$1" | sed -e 's/\+/ /g'`; shift
  22. to=`uriunescape "$1"`; shift
  23. #set -- `uriunescape "$@" | sed -e 's/\+/ /g'`
  24. set -- `uriunescape "$@"`
  25. app_raw="$1"
  26. app=`simpleword "$1"`; shift
  27. export debug
  28. SMS_PHONE="$from"
  29. urldecode=0
  30. export SMS_PHONE urldecode
  31. case "$SMS_REALM" in
  32. hello)
  33. provider="local"
  34. # provider="coolsms"
  35. # provider="clickatell"
  36. mdpath="/home/hearth/public_webdata/hearth/content/sms"
  37. export mdpath
  38. ;;
  39. cyber)
  40. provider="local"
  41. mdpath="/home/cyberhus/public_webdata/mdsms/content/sms"
  42. export mdpath
  43. ;;
  44. *)
  45. exit1 "unknown realm \"$SMS_REALM\""
  46. ;;
  47. esac
  48. case "$provider" in
  49. local)
  50. export SMS_SMSC
  51. ;;
  52. coolsms)
  53. unset SMS_SMSC
  54. SMS_URL="https://sms.coolsmsc.dk:8081/"
  55. SMS_USER="drjones1"
  56. SMS_PW="rahYSeGY"
  57. SMS_CP="cp1252"
  58. SMS_MSGTAG="message"
  59. export SMS_URL SMS_USER SMS_PW SMS_CP SMS_MSGTAG
  60. ;;
  61. clickatell)
  62. # unset SMS_SMSC
  63. # SMS_URL="https://api.clickatell.com/http/sendmsg"
  64. # SMS_USER="jonas"
  65. # SMS_PW="inaritab6"
  66. # SMS_CP="cp1252"
  67. # SMS_USERTAG="user"
  68. # SMS_API="3231280"
  69. # SMS_CONCAT="3"
  70. # export SMS_URL SMS_USER SMS_PW SMS_CP SMS_USERTAG SMS_API SMS_CONCAT
  71. SMS_SMSC="X"
  72. SMS_DLR_MASK="7"
  73. 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"
  74. stripprefix="1"
  75. export SMS_SMSC SMS_DLR_MASK SMS_DLR_URL stripprefix
  76. ;;
  77. *)
  78. exit1 "unknown provider \"$provider\""
  79. ;;
  80. esac
  81. # by now sms connection should work, so let sender get errors too
  82. exit1() {
  83. echo >&2 "Error: $1"
  84. /usr/local/sbin/localsendsms "$to" "Error: $1"
  85. echo >&2 "Exiting..."
  86. exit 1
  87. }
  88. return1() {
  89. echo >&2 "Error: $1"
  90. echo "$1"
  91. exit 1
  92. }
  93. vardir="/var/lib/kannel/hello"
  94. findobjects() {
  95. set -e
  96. object="$1"; shift || return1 "Internal error in routine \"findobjects\" (empty object)!"
  97. limit="$@"
  98. regex="$(echo "$limit" | perl -ne '/^\/(.+)\/$/ and print $1')"
  99. args="$(echo "$limit" | perl -ne 's/.*?([a-z0-9]+).*?/ $1/g and print')"
  100. [ -d "$vardir/$object" ] || return
  101. if [ -z "$*" ]; then
  102. cd "$vardir/$object" && find * -maxdepth 1 -type d
  103. elif [ -n "$regex" ]; then
  104. cd "$vardir/$object" && find * -maxdepth 1 -type d -regex "$regex"
  105. elif [ -n $args ]; then
  106. cd "$vardir/$object" && ls -1d $args
  107. else
  108. return1 "Internal error in routine \"findobjects\" (illegal limit)!"
  109. fi
  110. }
  111. addobject() {
  112. set -e
  113. object="$1"
  114. name="$2"
  115. oldname="$(findobjects "$object" "$name")" || return1 "$oldname"
  116. [ -z "$oldname" ] || return1 "$object \"$name\" already exist."
  117. [ -n "$name" ] || return1 "Internal error in routine \"addobject\" (empty name)!"
  118. mkdir -p "$vardir/$object/$name"
  119. chgrp --reference="$vardir" "$vardir/$object/$name"
  120. echo "$name"
  121. }
  122. case "$app" in
  123. @*)
  124. [ -n "$REDIRECT_OK" ] || exit1 "Redirection not permitted here!"
  125. to="$(echo "$app" | perl -pe 's/^@//;' -e 's/^([^+])/+45\1/')"
  126. # TODO: silence errors - or better: bounce those back to original sender
  127. /usr/local/sbin/localmarkdown2sms "$to" "$@"
  128. ;;
  129. # !*)
  130. # [ -n "$GROUP_OK" ] || exit1 "Redirection to group not permitted here!"
  131. # group="$(echo "$app" | perl -pe 's/^!//;')"
  132. # for to in $(echo "$group" | perl ...); do
  133. # /usr/local/sbin/localmarkdown2sms "$to" "$@"
  134. # done
  135. # ;;
  136. /*)
  137. [ -n "$ADMIN_OK" ] || exit1 "Administrative commands not permitted here!"
  138. cmd="$(echo "$app" | perl -pe 's/^\///;')"
  139. case "$cmd" in
  140. add|create)
  141. case "$1" in
  142. user|group)
  143. object="$1"; shift || exit1 "Internal error while adding object."
  144. name="$(addobject "$object" "$@")" || exit1 "Failed adding $object${name+: $name}!"
  145. /usr/local/sbin/localsendsms "$to" "$object \"$name\" added."
  146. ;;
  147. *)
  148. exit1 "Cannot add \"$1\" (try \"add user\" or \"add group\" instead)."
  149. ;;
  150. esac
  151. ;;
  152. status)
  153. msg="$(/usr/local/sbin/localshowkannelstatus)"
  154. /usr/local/sbin/localsendsms "$to" "$msg"
  155. ;;
  156. *)
  157. exit1 "unknown command \"$cmd\""
  158. ;;
  159. esac
  160. ;;
  161. yb)
  162. s="$(links -dump "http://www.yubnub.org/parser/parse?command=$@")"
  163. /usr/local/sbin/localsendsms "$to" "$s"
  164. ;;
  165. *)
  166. /usr/local/sbin/localmarkdown2sms "$to" "$app_raw" "$@"
  167. ;;
  168. esac