From 9adbf437e2886e01943e658fd7c88819fb321666 Mon Sep 17 00:00:00 2001 From: root Date: Sat, 24 Oct 2009 15:19:08 +0200 Subject: Add env flag urldecode to unescape variables from kannel. --- localmarkdown2sms | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'localmarkdown2sms') diff --git a/localmarkdown2sms b/localmarkdown2sms index 06aa589..215f010 100755 --- a/localmarkdown2sms +++ b/localmarkdown2sms @@ -23,7 +23,7 @@ use strict; use warnings; -use Env qw[$debug $info $warn $dummy $nosleep]; +use Env qw[$debug $info $warn $dummy $nosleep $urldecode]; use Log::Log4perl qw(:easy); use File::Spec; use File::Slurp; @@ -47,6 +47,17 @@ my ($path) = shift @ARGV; my ($phone) = shift @ARGV; my ($key) = lc (shift @ARGV); +# decode data passed from kannel +if ($urldecode) { + $phone = uri_unescape($phone); + $key = uri_unescape($phone); + @ARGV = uri_unescape(@ARGV); +} + +# strip international prefix +# (prefix is optional some places and illegal at other places - forgot where) +$phone =~ s/\+/ /g; + # strip non-word chars from keyword (and use only first chunk of word chars) $key =~ s/.*?(\w+).*?/$1/; -- cgit v1.2.3 From e3f7693b7ba8ccb030a4044e81872136c4581b82 Mon Sep 17 00:00:00 2001 From: root Date: Sat, 24 Oct 2009 15:46:23 +0200 Subject: Replace first argument with env flag mdpath, add santity check, and unescape ARGV only once. --- localmarkdown2sms | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'localmarkdown2sms') diff --git a/localmarkdown2sms b/localmarkdown2sms index 215f010..be74724 100755 --- a/localmarkdown2sms +++ b/localmarkdown2sms @@ -41,18 +41,16 @@ my $sms_pw = $ENV{SMS_PW} || "foobar"; my $sms_phone = $ENV{SMS_PHONE}; my $sms_smsc = $ENV{SMS_SMSC}; my $sms_msgtag = $ENV{SMS_MSGTAG} || "text"; +my $path = $ENV{mdpath}; my (%file, %delay, %reply); -my ($path) = shift @ARGV; -my ($phone) = shift @ARGV; -my ($key) = lc (shift @ARGV); -# decode data passed from kannel +# decode data if passed from kannel if ($urldecode) { - $phone = uri_unescape($phone); - $key = uri_unescape($phone); @ARGV = uri_unescape(@ARGV); } +my ($phone) = shift @ARGV; +my ($key) = lc (shift @ARGV); # strip international prefix # (prefix is optional some places and illegal at other places - forgot where) @@ -71,6 +69,11 @@ if ($debug) { Log::Log4perl->easy_init($ERROR); } +unless ($path and -f $path) { + ERROR "environment variable \"mdpath\" missing or wrong."; + exit 1; +} + foreach my $file (read_dir( $path )) { my ($key, $i, $skipkeysection, $skipcontent); # suppress repeated warnings for same issue -- cgit v1.2.3 From 3287001c6fe552cd190bdfcbe35b4a6670647a78 Mon Sep 17 00:00:00 2001 From: root Date: Mon, 26 Oct 2009 10:24:07 +0100 Subject: Fix check env flag mdpath (it is a directory, not a file). --- localmarkdown2sms | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'localmarkdown2sms') diff --git a/localmarkdown2sms b/localmarkdown2sms index be74724..896b34f 100755 --- a/localmarkdown2sms +++ b/localmarkdown2sms @@ -69,7 +69,7 @@ if ($debug) { Log::Log4perl->easy_init($ERROR); } -unless ($path and -f $path) { +unless ($path and -d $path) { ERROR "environment variable \"mdpath\" missing or wrong."; exit 1; } -- cgit v1.2.3 From 225d65b2fd31b793fb74feb90587f1fefd183169 Mon Sep 17 00:00:00 2001 From: root Date: Tue, 27 Oct 2009 03:21:21 +0100 Subject: Add env flag SMS_CP, and transliterate too complex characters. --- localmarkdown2sms | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'localmarkdown2sms') diff --git a/localmarkdown2sms b/localmarkdown2sms index 896b34f..7293583 100755 --- a/localmarkdown2sms +++ b/localmarkdown2sms @@ -28,7 +28,8 @@ use Log::Log4perl qw(:easy); use File::Spec; use File::Slurp; use Time::Duration::Parse; -use Encode; +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; use Proc::Daemon; @@ -41,6 +42,7 @@ my $sms_pw = $ENV{SMS_PW} || "foobar"; 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 $path = $ENV{mdpath}; my (%file, %delay, %reply); @@ -172,6 +174,19 @@ sub tidymsg { return $msg; } +# 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) = @_; @@ -184,7 +199,7 @@ sub sendmsg { . '&to=' . uri_escape($phone); $url .= '&from=' . uri_escape($sms_phone) if ($sms_phone); $url .= '&smsc=' . uri_escape($sms_smsc) if ($sms_smsc); - $url .= '&' . $sms_msgtag . '=' . uri_escape(encode("cp1252", $msg)); + $url .= '&' . $sms_msgtag . '=' . uri_escape(transliterate($sms_cp, $msg)); my $response = $ua->request(HTTP::Request->new('GET', $url)); unless ($response->is_success) { ERROR $response->status_line; -- cgit v1.2.3 From b33708b7bad20a5ced676f80f16188bb523613a6 Mon Sep 17 00:00:00 2001 From: root Date: Tue, 4 May 2010 12:03:48 +0200 Subject: Add/update sms- and Kannel-related scripts. --- localfilterkannelsmsboxlog | 36 +++++++++ localkannel-dispatch | 179 +++++++++++++++++++++++++++++++++++++++++++++ localmarkdown2sms | 12 ++- localsendsms | 95 ++++++++++++++++++++++++ localshowkannelsms | 22 ++++++ localshowkannelstatus | 22 ++++++ 6 files changed, 364 insertions(+), 2 deletions(-) create mode 100755 localfilterkannelsmsboxlog create mode 100755 localkannel-dispatch create mode 100755 localsendsms create mode 100755 localshowkannelsms create mode 100755 localshowkannelstatus (limited to 'localmarkdown2sms') 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());' + 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 +# Copyright 2009-2010, Jonas Smedegaard # # 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 +# +# 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 +# +# 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"; /^(...................)(??$/ 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"; /^(...................)(??$/ 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 () { + /^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 (); + +print "$i phones:\n$a"; +print "\n$j series:\n$b" if ($j); -- cgit v1.2.3