summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorroot <root@jones.dk>2010-05-04 12:03:48 +0200
committerroot <root@jones.dk>2010-05-04 12:03:48 +0200
commitb33708b7bad20a5ced676f80f16188bb523613a6 (patch)
tree89d129547c733d4c485372451b0b1c1f35bbf353
parent154d5bd7eb031369256a24a29684e87df98a2e24 (diff)
Add/update sms- and Kannel-related scripts.
-rwxr-xr-xlocalfilterkannelsmsboxlog36
-rwxr-xr-xlocalkannel-dispatch179
-rwxr-xr-xlocalmarkdown2sms12
-rwxr-xr-xlocalsendsms95
-rwxr-xr-xlocalshowkannelsms22
-rwxr-xr-xlocalshowkannelstatus22
6 files changed, 364 insertions, 2 deletions
diff --git a/localfilterkannelsmsboxlog b/localfilterkannelsmsboxlog
new file mode 100755
index 0000000..b3ac4a6
--- /dev/null
+++ b/localfilterkannelsmsboxlog
@@ -0,0 +1,36 @@
+#!/usr/bin/perl
+
+use warnings;
+
+while (<>) {
+ s/^\S+\h+//;
+ s/[\[].* INFO: //;
+ s/\+?(45)?(\d{8})\b/$2/g;
+ s/30640(636)\b/UNOTL$1/g;
+ s/30959(593)\b/UNOT$1/g;
+ s/22388(550)\b/SONOF$1/g;
+ s/24803(952)\b/TDC__$1/g;
+ s/28263(532)\b/TELIA$1/g;
+ s/28590(882)\b/TELIA$1/g;
+ s/25822(784)\b/TELE2$1/g;
+ s/29331(323)\b/COOP_$1/g;
+ s/29331(326)\b/COOP_$1/g;
+ s/26806(408)\b/vera_$1/g;
+ s/40917(191)\b/jacob$1/g;
+ s/29903(585)\b/seimi$1/g;
+ s/27846(210)\b/seimi$1/g;
+ s/27846(211)\b/boaz_$1/g;
+ s/27846(212)\b/andré$1/g;
+ s/28265(004)\b/candc$1/g;
+ s/26744(515)\b/krstn$1/g;
+ s/25486(484)\b/jens_$1/g;
+ s/20759(076)\b/sissl$1/g;
+ s/40843(136)\b/jonas$1/g;
+ s/\d{5}(\d{3})\b/*****$1/g;
+# /^(.{8}).*Starting to service <([^>\n]*)> from <([^>\n]*)> to <([^>\n]*)>/ and print "$1 $3->$4\n$2\n";
+ /^(.{5}).*Starting to service <([^>\n]*)> from <([^>\n]*)> to <([^>\n]*)>/ and print "$1<$3: $2\n";
+# /^(.{8}).*sendsms sender:<[^:\n]*:([^>\n]*)> \([^\)\n]*\) to:<([^>\n]*)> msg:<([^>\n]{0,10}[^ >\n]*)/ and print "$1 to $3 via $2: $4\n";
+ /^(.{5}).*sendsms sender:<[^:\n]*:([^>\n]*)> \([^\)\n]*\) to:< *([^>\n]*)> msg:<([^>\n]{0,10}[^ >\n]*)/ and print "$1>$3: $4\n";
+}
+
+1;
diff --git a/localkannel-dispatch b/localkannel-dispatch
new file mode 100755
index 0000000..ac2aae4
--- /dev/null
+++ b/localkannel-dispatch
@@ -0,0 +1,179 @@
+#!/bin/sh
+
+set -e
+
+exit1() {
+ echo >&2 "Error: $1"
+ echo >&2 "Exiting..."
+ exit 1
+}
+
+simpleword() {
+# echo "$1" | tr '[A-Z]' '[a-z]' | egrep '^[a-z][a-z0-9-]+$' || exit 1
+ echo "$1" | tr '[A-Z]' '[a-z]' | egrep '^[a-zæøåÆØÅ!@/][a-zæøåÆØÅ0-9-]+$' || exit 1
+}
+
+uriunescape() {
+ if [ 0 = "$urldecode" ]; then
+ echo "$@"
+ else
+ echo "$@" | perl -e 'use URI::Escape; print lc(uri_unescape(<STDIN>));'
+ fi
+}
+
+#from=`uriunescape "$1" | sed -e 's/\+/ /g'`; shift
+from=`uriunescape "$1"`; shift
+#to=`uriunescape "$1" | sed -e 's/\+/ /g'`; shift
+to=`uriunescape "$1"`; shift
+#set -- `uriunescape "$@" | sed -e 's/\+/ /g'`
+set -- `uriunescape "$@"`
+app_raw="$1"
+app=`simpleword "$1"`; shift
+
+export debug
+
+SMS_PHONE="$from"
+urldecode=0
+export SMS_PHONE urldecode
+
+case "$SMS_REALM" in
+ hello)
+ provider="local"
+# provider="coolsms"
+# provider="clickatell"
+ mdpath="/home/hearth/public_webdata/hearth/content/sms"
+ export mdpath
+ ;;
+ cyber)
+ provider="local"
+ mdpath="/home/cyberhus/public_webdata/mdsms/content/sms"
+ export mdpath
+ ;;
+ *)
+ exit1 "unknown realm \"$SMS_REALM\""
+ ;;
+esac
+case "$provider" in
+ local)
+ export SMS_SMSC
+ ;;
+ coolsms)
+ unset SMS_SMSC
+ SMS_URL="https://sms.coolsmsc.dk:8081/"
+ SMS_USER="drjones1"
+ SMS_PW="rahYSeGY"
+ SMS_CP="cp1252"
+ SMS_MSGTAG="message"
+ export SMS_URL SMS_USER SMS_PW SMS_CP SMS_MSGTAG
+ ;;
+ clickatell)
+# unset SMS_SMSC
+# SMS_URL="https://api.clickatell.com/http/sendmsg"
+# SMS_USER="jonas"
+# SMS_PW="inaritab6"
+# SMS_CP="cp1252"
+# SMS_USERTAG="user"
+# SMS_API="3231280"
+# SMS_CONCAT="3"
+# export SMS_URL SMS_USER SMS_PW SMS_CP SMS_USERTAG SMS_API SMS_CONCAT
+ SMS_SMSC="X"
+ SMS_DLR_MASK="7"
+ 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"
+ export SMS_SMSC SMS_DLR_MASK SMS_DLR_URL
+ ;;
+ *)
+ exit1 "unknown provider \"$provider\""
+ ;;
+esac
+
+# by now sms connection should work, so let sender get errors too
+exit1() {
+ echo >&2 "Error: $1"
+ /usr/local/sbin/localsendsms "$to" "Error: $1"
+ echo >&2 "Exiting..."
+ exit 1
+}
+
+return1() {
+ echo >&2 "Error: $1"
+ echo "$1"
+ exit 1
+}
+
+vardir="/var/lib/kannel/hello"
+findobjects() {
+ set -e
+ object="$1"; shift || return1 "Internal error in routine \"findobjects\" (empty object)!"
+ limit="$@"
+ regex="$(echo "$limit" | perl -ne '/^\/(.+)\/$/ and print $1')"
+ args="$(echo "$limit" | perl -ne 's/.*?([a-z0-9]+).*?/ $1/g and print')"
+ [ -d "$vardir/$object" ] || return
+ if [ -z "$*" ]; then
+ cd "$vardir/$object" && find * -maxdepth 1 -type d
+ elif [ -n "$regex" ]; then
+ cd "$vardir/$object" && find * -maxdepth 1 -type d -regex "$regex"
+ elif [ -n $args ]; then
+ cd "$vardir/$object" && ls -1d $args
+ else
+ return1 "Internal error in routine \"findobjects\" (illegal limit)!"
+ fi
+}
+addobject() {
+ set -e
+ object="$1"
+ name="$2"
+ oldname="$(findobjects "$object" "$name")" || return1 "$oldname"
+ [ -z "$oldname" ] || return1 "$object \"$name\" already exist."
+ [ -n "$name" ] || return1 "Internal error in routine \"addobject\" (empty name)!"
+ mkdir -p "$vardir/$object/$name"
+ chgrp --reference="$vardir" "$vardir/$object/$name"
+ echo "$name"
+}
+
+case "$app" in
+ @*)
+ [ -n "$REDIRECT_OK" ] || exit1 "Redirection not permitted here!"
+ to="$(echo "$app" | perl -pe 's/^@//;' -e 's/^([^+])/+45\1/')"
+# TODO: silence errors - or better: bounce those back to original sender
+ /usr/local/sbin/localmarkdown2sms "$to" "$@"
+ ;;
+# !*)
+# [ -n "$GROUP_OK" ] || exit1 "Redirection to group not permitted here!"
+# group="$(echo "$app" | perl -pe 's/^!//;')"
+# for to in $(echo "$group" | perl ...); do
+# /usr/local/sbin/localmarkdown2sms "$to" "$@"
+# done
+# ;;
+ /*)
+ [ -n "$ADMIN_OK" ] || exit1 "Administrative commands not permitted here!"
+ cmd="$(echo "$app" | perl -pe 's/^\///;')"
+ case "$cmd" in
+ add|create)
+ case "$1" in
+ user|group)
+ object="$1"; shift || exit1 "Internal error while adding object."
+ name="$(addobject "$object" "$@")" || exit1 "Failed adding $object${name+: $name}!"
+ /usr/local/sbin/localsendsms "$to" "$object \"$name\" added."
+ ;;
+ *)
+ exit1 "Cannot add \"$1\" (try \"add user\" or \"add group\" instead)."
+ ;;
+ esac
+ ;;
+ status)
+ msg="$(/usr/local/sbin/localshowkannelstatus)"
+ /usr/local/sbin/localsendsms "$to" "$msg"
+ ;;
+ *)
+ exit1 "unknown command \"$cmd\""
+ ;;
+ esac
+ ;;
+ yb)
+ s="$(links -dump "http://www.yubnub.org/parser/parse?command=$@")"
+ /usr/local/sbin/localsendsms "$to" "$s"
+ ;;
+ *)
+ /usr/local/sbin/localmarkdown2sms "$to" "$app_raw" "$@"
+ ;;
+esac
diff --git a/localmarkdown2sms b/localmarkdown2sms
index 7293583..2f91108 100755
--- a/localmarkdown2sms
+++ b/localmarkdown2sms
@@ -1,7 +1,7 @@
#!/usr/bin/perl
#
# /usr/local/sbin/localmarkdown2sms
-# Copyright 2009 Jonas Smedegaard <dr@jones.dk>
+# Copyright 2009-2010, Jonas Smedegaard <dr@jones.dk>
#
# Send series of messages through Kannel from simplified Markdown files
@@ -37,12 +37,16 @@ use Proc::Daemon;
Proc::Daemon::Init unless ($debug);
my $sms_url = $ENV{SMS_URL} || "http://localhost:13013/cgi-bin/sendsms";
+my $sms_usertag = $ENV{SMS_USERTAG} || "username";
my $sms_user = $ENV{SMS_USER} || "tester";
my $sms_pw = $ENV{SMS_PW} || "foobar";
+my $sms_api = $ENV{SMS_API};
my $sms_phone = $ENV{SMS_PHONE};
my $sms_smsc = $ENV{SMS_SMSC};
my $sms_msgtag = $ENV{SMS_MSGTAG} || "text";
my $sms_cp = $ENV{SMS_CP} || "utf8";
+my $sms_concat = $ENV{SMS_CONCAT};
+my $sms_dlr_mask = $ENV{SMS_DLR_MASK};
my $path = $ENV{mdpath};
my (%file, %delay, %reply);
@@ -194,12 +198,16 @@ sub sendmsg {
my $ua = LWP::UserAgent->new(agent => "localmarkdown2sms");
$ua->timeout(10);
my $url = $sms_url
- . '?username=' . uri_escape($sms_user)
+ . '?' . $sms_usertag . '=' . uri_escape($sms_user)
. '&password=' . uri_escape($sms_pw)
. '&to=' . uri_escape($phone);
+ $url .= '&api_id=' . uri_escape($sms_api) if ($sms_api);
$url .= '&from=' . uri_escape($sms_phone) if ($sms_phone);
$url .= '&smsc=' . uri_escape($sms_smsc) if ($sms_smsc);
+ $url .= '&concat=' . uri_escape($sms_concat) if ($sms_concat);
+ $url .= '&dlr-mask=' . uri_escape($sms_dlr_mask) if ($sms_dlr_mask);
$url .= '&' . $sms_msgtag . '=' . uri_escape(transliterate($sms_cp, $msg));
+ DEBUG "Sending request: $url";
my $response = $ua->request(HTTP::Request->new('GET', $url));
unless ($response->is_success) {
ERROR $response->status_line;
diff --git a/localsendsms b/localsendsms
new file mode 100755
index 0000000..ec1468a
--- /dev/null
+++ b/localsendsms
@@ -0,0 +1,95 @@
+#!/usr/bin/perl
+#
+# /usr/local/sbin/localsendsms
+# Copyright 2009-2010, Jonas Smedegaard <dr@jones.dk>
+#
+# Send a message through Kannel
+
+use strict;
+use warnings;
+
+use Env qw[$debug $info $warn $dummy $urldecode];
+use Log::Log4perl qw(:easy);
+use Text::Unidecode;
+use Encode 2.12 qw(encode decode_utf8 _utf8_off); # need v2.12 to support coderef
+use LWP::UserAgent;
+use URI::Escape;
+
+my $sms_url = $ENV{SMS_URL} || "http://localhost:13013/cgi-bin/sendsms";
+my $sms_usertag = $ENV{SMS_USERTAG} || "username";
+my $sms_user = $ENV{SMS_USER} || "tester";
+my $sms_pw = $ENV{SMS_PW} || "foobar";
+my $sms_api = $ENV{SMS_API};
+my $sms_phone = $ENV{SMS_PHONE};
+my $sms_smsc = $ENV{SMS_SMSC};
+my $sms_msgtag = $ENV{SMS_MSGTAG} || "text";
+my $sms_cp = $ENV{SMS_CP} || "utf8";
+my $sms_concat = $ENV{SMS_CONCAT};
+my $sms_dlr_mask = $ENV{SMS_DLR_MASK};
+my $sms_dlr_url = $ENV{SMS_DLR_URL};
+
+# decode data if passed from kannel
+if ($urldecode) {
+ @ARGV = uri_unescape(@ARGV);
+}
+my ($phone) = shift @ARGV;
+
+# strip international prefix
+# (prefix is optional some places and illegal at other places - forgot where)
+#$phone =~ s/\+/ /g;
+
+if ($debug) {
+ Log::Log4perl->easy_init($DEBUG);
+} elsif ($INFO) {
+ Log::Log4perl->easy_init($INFO);
+} elsif ($WARN) {
+ Log::Log4perl->easy_init($WARN);
+} elsif ($ERROR) {
+ Log::Log4perl->easy_init($ERROR);
+}
+
+# based on Text::Unidecode bug#8017: http://rt.cpan.org/Ticket/Display.html?id=8017#txn-322351
+sub transliterate {
+ my ($tocharset, $string) = @_;
+
+ my $res = encode($tocharset, decode_utf8($string), sub {
+ my $ascii = unidecode(chr $_[0]);
+ _utf8_off($ascii);
+ $ascii;
+ });
+
+ return $res;
+}
+
+sub sendmsg {
+ my ($phone, $desc, $msg) = @_;
+
+ unless ($dummy) {
+ my $ua = LWP::UserAgent->new(agent => "localsendsms");
+ $ua->timeout(10);
+ my $url = $sms_url
+ . '?' . $sms_usertag . '=' . uri_escape($sms_user)
+ . '&password=' . uri_escape($sms_pw)
+ . '&to=' . uri_escape($phone);
+ $url .= '&api_id=' . uri_escape($sms_api) if ($sms_api);
+ $url .= '&from=' . uri_escape($sms_phone) if ($sms_phone);
+ $url .= '&smsc=' . uri_escape($sms_smsc) if ($sms_smsc);
+ $url .= '&concat=' . uri_escape($sms_concat) if ($sms_concat);
+ $url .= '&dlr-mask=' . uri_escape($sms_dlr_mask) if ($sms_dlr_mask);
+ $url .= '&dlr-url=' . uri_escape($sms_dlr_url) if ($sms_dlr_url);
+ $url .= '&' . $sms_msgtag . '=' . uri_escape(transliterate($sms_cp, $msg));
+ DEBUG "Sending request: $url";
+ my $response = $ua->request(HTTP::Request->new('GET', $url));
+ unless ($response->is_success) {
+ ERROR $response->status_line;
+ }
+ DEBUG "Done $desc";
+ } else {
+ print STDERR "\n --> $phone: $desc\n";
+ print STDERR $msg . "\n";
+ }
+}
+
+&sendmsg($phone, "message", "@ARGV");
+
+1;
diff --git a/localshowkannelsms b/localshowkannelsms
new file mode 100755
index 0000000..437b72b
--- /dev/null
+++ b/localshowkannelsms
@@ -0,0 +1,22 @@
+#!/bin/sh
+#
+# /usr/local/sbin/localshowkannelsms
+# Copyright 2010 Jonas Smedegaard <dr@jones.dk>
+#
+# List recently handles sms'es
+#
+
+logfilecount="${logfilecount:-2}"
+
+lslogfiles() {
+ if [ -n "$logfilecount" ]; then
+ find /var/log/kannel -name 'smsbox.log*' | sort -n -t. -k3 -r | tail -n "$logfilecount"
+ else
+ find /var/log/kannel -name 'smsbox.log*' | sort -n -t. -k3 -r
+ fi
+}
+
+#lslogfiles | xargs zcat -f | perl -ne '/^(\S+ +\S+) .*Starting to service <(.*)> from <.*> to <\+4530959593>$/ and print "$1 $2\n"; /^\S+ (\S+) .*sendsms sender:<\S+:\+4530959593> .*msg:<(.*?)>?$/ and print " $1 $2\n"; /^(...................)(?<!\d\d\d\d\-\d\d\-\d\d \d\d:\d\d:\d\d)(.*?)>?$/ and print " $1$2\n"'
+lslogfiles | xargs zcat -f | perl -ne '/^(\S+ +\S+) .*Starting to service <(.*)> from <.*> to <\+4530640636>$/ and print "$1 $2\n"; /^\S+ (\S+) .*sendsms sender:<\S+:\+4530959593> .*msg:<(.*?)>?$/ and print " $1 $2\n"; /^(...................)(?<!\d\d\d\d\-\d\d\-\d\d \d\d:\d\d:\d\d)(.*?)>?$/ and print " $1$2\n"'
+
+exit 0
diff --git a/localshowkannelstatus b/localshowkannelstatus
new file mode 100755
index 0000000..544e4de
--- /dev/null
+++ b/localshowkannelstatus
@@ -0,0 +1,22 @@
+#!/usr/bin/perl
+
+open(LOGFILE,"/etc/kannel/kannel.conf");
+while (<LOGFILE>) {
+ /^smsc-id\h+=\h+(\S+)/ && do {
+ $smsc=$1
+ };
+ /^my-number\h+=\h+\+?(45)?(\S+)/ && do {
+ $i++;
+ $a .= "$2 ($smsc)\n"
+ }
+};
+
+open(PS,"ps ax|");
+do {
+ /perl\s.*markdown2sms.*sms\s*\+?(45)?(\d+)\s+\b(\S+)/ && do {
+ $j++; $b .= "$2: $3\n";
+ };
+} while (<PS>);
+
+print "$i phones:\n$a";
+print "\n$j series:\n$b" if ($j);