summaryrefslogtreecommitdiff
path: root/localkannel-dispatch
blob: ac2aae42d4bee53847aa2968aa51eed83bb35f0e (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. export SMS_SMSC SMS_DLR_MASK SMS_DLR_URL
  75. ;;
  76. *)
  77. exit1 "unknown provider \"$provider\""
  78. ;;
  79. esac
  80. # by now sms connection should work, so let sender get errors too
  81. exit1() {
  82. echo >&2 "Error: $1"
  83. /usr/local/sbin/localsendsms "$to" "Error: $1"
  84. echo >&2 "Exiting..."
  85. exit 1
  86. }
  87. return1() {
  88. echo >&2 "Error: $1"
  89. echo "$1"
  90. exit 1
  91. }
  92. vardir="/var/lib/kannel/hello"
  93. findobjects() {
  94. set -e
  95. object="$1"; shift || return1 "Internal error in routine \"findobjects\" (empty object)!"
  96. limit="$@"
  97. regex="$(echo "$limit" | perl -ne '/^\/(.+)\/$/ and print $1')"
  98. args="$(echo "$limit" | perl -ne 's/.*?([a-z0-9]+).*?/ $1/g and print')"
  99. [ -d "$vardir/$object" ] || return
  100. if [ -z "$*" ]; then
  101. cd "$vardir/$object" && find * -maxdepth 1 -type d
  102. elif [ -n "$regex" ]; then
  103. cd "$vardir/$object" && find * -maxdepth 1 -type d -regex "$regex"
  104. elif [ -n $args ]; then
  105. cd "$vardir/$object" && ls -1d $args
  106. else
  107. return1 "Internal error in routine \"findobjects\" (illegal limit)!"
  108. fi
  109. }
  110. addobject() {
  111. set -e
  112. object="$1"
  113. name="$2"
  114. oldname="$(findobjects "$object" "$name")" || return1 "$oldname"
  115. [ -z "$oldname" ] || return1 "$object \"$name\" already exist."
  116. [ -n "$name" ] || return1 "Internal error in routine \"addobject\" (empty name)!"
  117. mkdir -p "$vardir/$object/$name"
  118. chgrp --reference="$vardir" "$vardir/$object/$name"
  119. echo "$name"
  120. }
  121. case "$app" in
  122. @*)
  123. [ -n "$REDIRECT_OK" ] || exit1 "Redirection not permitted here!"
  124. to="$(echo "$app" | perl -pe 's/^@//;' -e 's/^([^+])/+45\1/')"
  125. # TODO: silence errors - or better: bounce those back to original sender
  126. /usr/local/sbin/localmarkdown2sms "$to" "$@"
  127. ;;
  128. # !*)
  129. # [ -n "$GROUP_OK" ] || exit1 "Redirection to group not permitted here!"
  130. # group="$(echo "$app" | perl -pe 's/^!//;')"
  131. # for to in $(echo "$group" | perl ...); do
  132. # /usr/local/sbin/localmarkdown2sms "$to" "$@"
  133. # done
  134. # ;;
  135. /*)
  136. [ -n "$ADMIN_OK" ] || exit1 "Administrative commands not permitted here!"
  137. cmd="$(echo "$app" | perl -pe 's/^\///;')"
  138. case "$cmd" in
  139. add|create)
  140. case "$1" in
  141. user|group)
  142. object="$1"; shift || exit1 "Internal error while adding object."
  143. name="$(addobject "$object" "$@")" || exit1 "Failed adding $object${name+: $name}!"
  144. /usr/local/sbin/localsendsms "$to" "$object \"$name\" added."
  145. ;;
  146. *)
  147. exit1 "Cannot add \"$1\" (try \"add user\" or \"add group\" instead)."
  148. ;;
  149. esac
  150. ;;
  151. status)
  152. msg="$(/usr/local/sbin/localshowkannelstatus)"
  153. /usr/local/sbin/localsendsms "$to" "$msg"
  154. ;;
  155. *)
  156. exit1 "unknown command \"$cmd\""
  157. ;;
  158. esac
  159. ;;
  160. yb)
  161. s="$(links -dump "http://www.yubnub.org/parser/parse?command=$@")"
  162. /usr/local/sbin/localsendsms "$to" "$s"
  163. ;;
  164. *)
  165. /usr/local/sbin/localmarkdown2sms "$to" "$app_raw" "$@"
  166. ;;
  167. esac