diff options
author | root <root@jones.dk> | 2010-05-04 12:03:48 +0200 |
---|---|---|
committer | root <root@jones.dk> | 2010-05-04 12:03:48 +0200 |
commit | b33708b7bad20a5ced676f80f16188bb523613a6 (patch) | |
tree | 89d129547c733d4c485372451b0b1c1f35bbf353 /localsendsms | |
parent | 154d5bd7eb031369256a24a29684e87df98a2e24 (diff) |
Add/update sms- and Kannel-related scripts.
Diffstat (limited to 'localsendsms')
-rwxr-xr-x | localsendsms | 95 |
1 files changed, 95 insertions, 0 deletions
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; |