File Coverage

blib/lib/SMS/Send/IN/Unicel.pm
Criterion Covered Total %
statement 27 69 39.1
branch 2 28 7.1
condition 0 6 0.0
subroutine 8 13 61.5
pod 2 2 100.0
total 39 118 33.0


line stmt bran cond sub pod time code
1             package SMS::Send::IN::Unicel;
2              
3             # ABSTRACT: SMS::Send driver to send messages via Unicel (http://unicel.in/)
4              
5 3     3   20217 use 5.006;
  3         7  
  3         109  
6 3     3   14 use strict;
  3         3  
  3         95  
7 3     3   22 use warnings;
  3         2  
  3         93  
8 3     3   1900 use LWP::UserAgent;
  3         109928  
  3         92  
9 3     3   21 use URI::Escape;
  3         3  
  3         200  
10              
11 3     3   13 use base 'SMS::Send::Driver';
  3         4  
  3         1345  
12              
13             our $VERSION = '0.01'; # VERSION
14             our $AUTHORITY = 'cpan:INDRADG'; # AUTHORITY
15              
16             # setup error code lookup list for Unicel
17             our %unicel_error_codes = ( "0x200", "Invalid Username or Password",
18             "0x201", "Account suspended due to one of several defined reasons",
19             "0x202", "Invalid Source Address/Sender ID. As per GSM standard, the sender ID should be within 11 characters",
20             "0x203", "Message length exceeded (more than 160 characters) if concat is set to 0 Message",
21             "0x204", "Message length exceeded (more than 459 characters) in concat is set to 1",
22             "0x205", "DRL URL is not set",
23             "0x206", "Only the subscribed service type can be accessed - make sure of the service type you are trying to connect with",
24             "0x207", "Invalid Source IP - kindly check if the IP is responding",
25             "0x208", "Account deactivated/expired",
26             "0x209", "Invalid message length (less than 160 characters) if concat is set to 1",
27             "0x210", "Invalid Parameter values",
28             "0x211", "Invalid Message Length (more than 280 characters)",
29             "0x212", "Invalid Message Length",
30             "0x213", "Invalid Destination Number" );
31              
32             sub new {
33 2     2 1 99 my ( $class, %args ) = @_;
34              
35             # check we have an username and password
36 2 50       8 die "Username needs to be passed as 'uname'" unless ( $args{_login} );
37 2 50       5 die "Password needs to be passed as 'pass'" unless ( $args{_password} );
38              
39             # build the object
40 2         11 my $self = bless {
41             _endpoint => 'https://unicel.in/SendSMS/sendmsg.php',
42             _debug => 0,
43             %args
44             }, $class;
45              
46             # get an LWP user agent ready
47 2         8 $self->{ua} = LWP::UserAgent->new;
48            
49 2         5067 return $self;
50             }
51              
52             sub _send_method {
53 0     0     my ( $self, @args ) = @_;
54              
55 0           my @params;
56 0           while (@args) {
57 0           my $key = shift @args;
58 0           my $val = shift @args;
59 0           push( @params, join( '=', uri_escape($key), uri_escape($val) ) );
60 0 0         print STDERR ">>> Arg $key = $val\n" if ( $self->{_debug} );
61             }
62 0           my $url = join( '?', $self->{_endpoint}, join( '&', @params ) );
63 0 0         print STDERR ">>> GET $url\n" if ( $self->{_debug} );
64              
65 0           my $res = $self->{ua}->get($url);
66              
67 0 0         printf STDERR "<<< Status: %s\n<<< Content: %s\n", $res->code, $res->content if ( $self->{_debug} );
68              
69 0           my $errorbroker = $self->_ERRORHANDLER ( $res, %unicel_error_codes );
70              
71 0 0         die $res->status_line unless ( $res->is_success );
72              
73 0           return $res;
74             }
75              
76             sub send_sms {
77              
78 0     0 1   my ( $self, %args ) = @_;
79            
80             # check for message for 160 char limit
81 0           my $text = $self->_MESSAGETEXT ( $args{text} );
82            
83             # check destination number for well-formedness under NNP 2003 schema
84 0           my $to = $self->_TO ( $args{to} );
85              
86 0           $self->_send_method(
87             uname => $self->{_login},
88             pass => $self->{_password},
89             dest => $args{to},
90             msg => $args{text},
91             );
92             }
93              
94             # -----------------------------------------------------
95             # internal sanitization routines
96             # -----------------------------------------------------
97              
98             sub _MESSAGETEXT {
99 0     0     my ( $self, $text ) = @_;
100 3     3   15 use bytes;
  3         5  
  3         21  
101 0 0         die "Message length over limit. Max length is 160 characters" unless ( length($text) <= 160 );
102             } # check for 160 char length of message text
103              
104             # As per National Numbering Plan 2003, Indian mobile phone numbers have to be in
105             # [9|8|7]XXXXXXXXX format. So we need to sanitize our input. The driver expects
106             # number string in 91XXXXXXXXXX format
107              
108             sub _TO {
109 0     0     my ( $self, $dest ) = @_;
110              
111 0           my $checkseries;
112             my $countrycode;
113              
114             # strip out NaN characters
115 0           $dest =~ s/[^\d]//g;
116              
117             # strip leading zero as some have the habit of inputing numbers as 0XXXXXXXXXX
118 0           $dest =~ s/^0+//g;
119              
120             # check destination number length and format for well-formedness and fix common issues.
121 0 0 0       if ( length($dest) == 12 or length($dest) == 10 ) {
122 0 0         if ( length($dest) == 12 ) {
123 0           $countrycode = substr $dest, 0, 2;
124 0 0         die "Country code incorrect, needs to be 91 for India" unless ( $countrycode eq '91' );
125             }
126 0 0         if ( length($dest) == 10 ) {
127 0           $countrycode = "91";
128 0           $dest = $countrycode . $dest; #bring it up to 91XXXXXXXXXX
129             }
130              
131             # check for 9,8,7 series numbering under NNP 2003
132 0           $checkseries = substr $dest, 2, 1;
133 0 0         die "Invalid phone number as per National Numbering Plan 2003" unless ( $checkseries =~ /[9|8|7]/ );
134             } else {
135 0           die "Invalid phone number format";
136             }
137 0           return $dest;
138             }
139              
140             sub _ERRORHANDLER {
141 0     0     my ( $self, $res, %uecodes ) = @_;
142              
143 0           my $res_content = $res->content;
144              
145             # check for unique MID (message ID) signifying a successful transaction
146              
147 0 0 0       if ( $res_content =~ /^[0-9]+$/ and length( $res_content ) == 19 ) {
148 0           return "Successfully transmitted with MID $res_content\n";
149             } else {
150 0           my $ecode = substr $res_content, 0, 5;
151 0 0         if ( $uecodes{$ecode} ) {
152 0           die "Unicel error $ecode : $uecodes{$ecode}";
153             }
154             }
155             }
156              
157             1;
158              
159             __END__