diff options
author | Jonas Smedegaard <dr@jones.dk> | 2010-05-04 14:25:34 +0200 |
---|---|---|
committer | Jonas Smedegaard <dr@jones.dk> | 2010-05-04 14:25:34 +0200 |
commit | 966b29c496ef6851496641334f922584633b0a01 (patch) | |
tree | 11b884e34d364b80f708e6621c0a7acc5aaa4401 /localmarkdown2sms | |
parent | 7e505c5c9a7aa6b6bf89d32467f3f4c41ff8ce33 (diff) | |
parent | b33708b7bad20a5ced676f80f16188bb523613a6 (diff) |
Merge coreander:/srv/git/source.jones.dk/sbin
Diffstat (limited to 'localmarkdown2sms')
-rwxr-xr-x | localmarkdown2sms | 49 |
1 files changed, 43 insertions, 6 deletions
diff --git a/localmarkdown2sms b/localmarkdown2sms index 001e1a6..6843195 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 @@ -23,12 +23,13 @@ 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; 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; @@ -37,17 +38,31 @@ 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); -my ($path) = shift @ARGV; + +# decode data if passed from kannel +if ($urldecode) { + @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) +$phone =~ s/\+/ /g; + # strip non-word chars from keyword (and use only first chunk of word chars) $key =~ s/.*?(\w+).*?/$1/; @@ -61,6 +76,11 @@ if ($debug) { Log::Log4perl->easy_init($ERROR); } +unless ($path and -d $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 @@ -159,6 +179,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) = @_; @@ -166,12 +199,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 .= '&' . $sms_msgtag . '=' . uri_escape(encode("cp1252", $msg)); + $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; |