blib/lib/SMS/Send/IN/NICSMS.pm

filter: Statementstmt 39.0% 32 / 82 Branchbran 10.5% 4 / 38 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 61.5% 8 / 13 Podpod 100% 2 / 2 totaltotal 32.8% 46 / 140 CC 21 SCAR 31.6
CC
21
Cov
29%
_TO
56.0
_send_method
42.0
send_sms
20.0
CRAP 23.5
↑ Summary
1 package SMS::Send::IN::NICSMS;
2  
3 # ABSTRACT: SMS::Send driver to send messages via NIC's SMS Gateway ( https://smsgw.sms.gov.in )
4  
5 3 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 ▶use base 'SMS::Send::Driver';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 our $VERSION = '1.00'; # VERSION
14 3 our $AUTHORITY = 'cpan:INDRADG'; # AUTHORITY
15  
16 1 ▶sub new {
Subroutine new: called CC=5 SCAR=16.7
Pod documented
17 1 my ( $class, %args ) = @_;
18  
19 # check we have the necessary credentials parameters
20 1 ▶ die "Username needs to be passed as 'username'" unless ( $args{_login} );
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"_login"} 0 1 1
21 1 ▶ die "PIN / password needs to be passed as 'password'" unless ( $args{_password} );
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"_password"} 0 1 1
22 1 ▶ die "SenderID / DLT Header needs to be passsed as 'signature'" unless ( $args{_signature} );
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"_signature"} 0 1 1
23 1 ▶ die "19-digit DLT Entity ID needs to be passed as 'dlt_entity_id'" unless ( $args{_dlt_entity_id} );
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"_dlt_entity_id"} 0 1 1
24  
25 # build the object
26 1 my $self = bless {
27 _endpoint => 'https://smsgw.sms.gov.in/failsafe/HttpLink',
28 _debug => 0,
29 %args
30 }, $class;
31  
32 # get an LWP user agent ready
33 # FIXME - bypassing SSL cert check due to NIC's cert chain borkage
34 1 $self->{ua} = LWP::UserAgent->new;
35 $self->{ua}->ssl_opts(
36 1 SSL_verify_mode => 0,
37 verify_hostname => 0,
38 );
39
40 1 return $self;
41 }
42  
43 0 ▶sub _send_method {
Subroutine _send_method: not called CC=6 SCAR=37.4
44 0 my ( $self, @args ) = @_;
45  
46 0 my @params;
47 0 ▶ while (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 0 0 0
48 0 my $key = shift @args;
49 0 my $val = shift @args;
50 0 push( @params, join( '=', uri_escape($key), uri_escape($val) ) );
51 0 ▶ print STDERR ">>> Arg $key = $val\n" if ( $self->{_debug} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_debug"} 0 0 0
52 }
53 0 my $url = join( '?', $self->{_endpoint}, join( '&', @params ) );
54 0 ▶ print STDERR ">>> GET $url\n" if ( $self->{_debug} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_debug"} 0 0 0
55  
56 0 my $res = $self->{ua}->get($url);
57  
58 0 my $errorbroker = $self->_ERRORHANDLER ( $res );
59  
60 0 ▶ if ( $errorbroker ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($errorbroker) { } 0 0 0
61 0 print STDERR "<<< Message sent successfully\n";
62 0 ▶ print STDERR "<<< SMS gateway response : $res->{_content}\n" if ( $self->{_debug} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_debug"} 0 0 0
63 0 return 1;
64 } else {
65 0 print STDERR "<<< Message send failed\n";
66 0 print STDERR "<<< SMS gateway response : $res->{_content}\n";
67 0 return;
68 }
69 }
70  
71 0 ▶sub send_sms {
Subroutine send_sms: not called CC=4 SCAR=30.0
Pod documented
72  
73 0 my ( $self, %args ) = @_;
74  
75 # FIXME : ugly hack to handle passing the DLT_TEMPLATE_ID for Koha ILS
76 0 ▶ if ( exists $self->{_iskohainstance} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'_iskohainstance'}) { } 0 0 0
77 0 ▶ if ( $self->{_iskohainstance} eq "yes" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_iskohainstance'} eq 'yes') { } 0 0 0
78 # check if $args{text} is carrying the DLT_TEMPLATE_ID at the start of the message
79 # else throw an error
80 # As on date the IDs are 19 digit numbers
81  
82 0 ▶ die "19-digit DLT_TEMPLATE_ID not found at start of message!" unless ( $args{text} =~ /^\d{19}/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"text"} =~ /^\d{19}/ 0 0 0
83  
84 0 $self->{_dlt_template_id} = substr( $args{text}, 0, 19);
85  
86 0 $args{text} =~ s!^\d{19}!!;
87 } else {
88 0 die "Incorrect value for the key 'iskohainstance' in IN/NICSMS.yaml. Check file!";
89 }
90 } else {
91 0 $self->{_dlt_template_id} = $args{_dlt_template_id};
92 }
93  
94 # check for message for 160 char limit
95 0 my $text = $self->_MESSAGETEXT ( $args{text} );
96
97 # check destination number for well-formedness under NNP 2003 schema
98 0 my $to = $self->_TO ( $args{to} );
99  
100 $self->_send_method(
101 username => $self->{_login},
102 pin => $self->{_password},
103 mnumber => $args{to},
104 message => $args{text},
105 signature => $self->{_signature},
106 dlt_entity_id => $self->{_dlt_entity_id},
107 dlt_template_id => $self->{_dlt_template_id},
108 0 );
109 }
110  
111 # -----------------------------------------------------
112 # internal sanitization routines
113 # -----------------------------------------------------
114  
115 0 ▶sub _MESSAGETEXT {
Subroutine _MESSAGETEXT: not called CC=2 SCAR=10.1
116 0 my ( $self, $text ) = @_;
117 3 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
118 0 ▶ die "Message length over limit. Max length is 160 characters" unless ( length($text) <= 160 );
Branch: 1 branch
BranchTrueFalseTotal
unless length $text <= 160 0 0 0
119 } # check for 160 char length of message text
120  
121 # As per National Numbering Plan 2003, Indian mobile phone numbers have to be in
122 # [9|8|7]XXXXXXXXX format. So we need to sanitize our input. The driver expects
123 # number string in 91XXXXXXXXXX format
124  
125 0 ▶sub _TO {
Subroutine _TO: not called CC=7 SCAR=40.3
126 0 my ( $self, $dest ) = @_;
127  
128 0 my $checkseries;
129 my $countrycode;
130  
131 # strip out any NaN characters
132 0 $dest =~ s/[^\d]//g;
133  
134 # strip leading zero as some have the habit of inputing numbers as 0XXXXXXXXXX
135 0 $dest =~ s/^0+//g;
136  
137 # check destination number length and format for well-formedness and fix common issues.
138 0 ▶ if ( length($dest) == 12 or length($dest) == 10 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $dest == 12 or length $dest == 10) { } 0 0 0
Condition0%0/3
length $dest == 12 or length $dest == 10
l!l&&r!l&&!r
000
MC/DC0%0/2
length $dest == 12 or length $dest == 10
length $dest == 12 length $dest == 10
Truth table0%0/3
A or B
A= length $dest == 12B= length $dest == 10
ABresult
1X1
011
000
139 0 ▶ if ( length($dest) == 12 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $dest == 12) 0 0 0
140 0 $countrycode = substr $dest, 0, 2;
141 0 ▶ die "Country code incorrect, needs to be 91 for India" unless ( $countrycode eq '91' );
Branch: 1 branch
BranchTrueFalseTotal
unless $countrycode eq 91 0 0 0
142 }
143 0 ▶ if ( length($dest) == 10 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $dest == 10) 0 0 0
144 0 $countrycode = "91";
145 0 $dest = $countrycode . $dest; #bring it up to 91XXXXXXXXXX
146 }
147  
148 # check for 9,8,7,6 series numbering under NNP 2003
149 # see https://en.wikipedia.org/wiki/Mobile_telephone_numbering_in_India
150 #
151 0 $checkseries = substr $dest, 2, 1;
152 0 ▶ die "Invalid phone number as per National Numbering Plan 2003" unless ( $checkseries =~ /[9|8|7|6]/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $checkseries =~ /[9|8|7|6]/ 0 0 0
153 } else {
154 0 die "Invalid phone number format";
155 }
156 0 return $dest;
157 }
158  
159 0 ▶sub _ERRORHANDLER {
Subroutine _ERRORHANDLER: not called CC=2 SCAR=17.9
160 0 my ( $self, $res ) = @_;
161  
162 # check for "~code=API000 " in the response string signifying a successful send
163  
164 0 ▶ if ( $res->content =~ /~code=API000 / ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->content =~ /~code=API000 /) { } 0 0 0
165 0 return 1;
166 } else {
167 0 return;
168 }
169 }
170  
171 3 1;
172  
173 __END__
↑ Summary