blib/lib/Net/SMS/2Way.pm

filter: Statementstmt 10.3% 20 / 193 Branchbran 0.0% 0 / 90 Conditioncond 0.0% 0 / 25 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 28.5% 4 / 14 Podpod 66.6% 6 / 9 totaltotal 8.6% 30 / 346 CC 65 SCAR 47.1
CC
65
Cov
6%
send_sms
240.0
new
156.0
send_batch
132.0
CRAP 111.0
↑ Summary
1 package Net::SMS::2Way;
2  
3 1 ▶use 5.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 require Exporter;
9  
10 1 our @ISA = qw(Exporter);
11  
12 1 our $VERSION = '0.08'; # 21 Jan. 2009
13  
14 1 our $urls = {
15 ZA => 'http://bulksms.2way.co.za:5567',
16 UK => 'http://www.bulksms.co.uk:5567',
17 ES => 'http://bulksms.com.es:5567',
18 DE => 'http://bulksms.de:5567',
19 US => 'http://usa.bulksms.com:5567',
20 INT => 'http://bulksms.vsms.net:5567'
21 };
22  
23 1 our $default_options = {
24 country => 'ZA',
25 quote => 0,
26 };
27  
28 1 our @bulksms_send_options = qw(
29 sender msg_class dca want_report routing_group source_id repliable strip_dup_recipients
30 stop_dup_id send_time send_time_unixtime scheduling_description test_always_succeed
31 test_always_fail allow_concat_text_sms oncat_text_sms_max_parts
32 );
33  
34 1 our $bulk_sms_send_defaults = {};
35  
36 1 our @mandatory_options = qw(username password);
37  
38 sub new
39 0 ▶{
Subroutine new: not called CC=12 SCAR=50.5
Pod undocumented
40 0 my $class = shift @_;
41 0 my $ref = shift @_;
42 0 my $error;
43
44 0 $ref->{script} = $0;
45
46 # Get settings from config file
47 0 ▶ my $cfg_ref = _parse_config($ref->{config}) if $ref->{config};
Branch: 1 branch
BranchTrueFalseTotal
if $ref->{"config"} 0 0 0
48
49 # Merge settings from config so that the config file settings are overwritten
50 0 foreach my $key (keys(%$cfg_ref))
51 {
52 0 ▶ if ($ref->{$key} eq '')
Branch: 1 branch
BranchTrueFalseTotal
if ($ref->{$key} eq "") 0 0 0
53 {
54 0 $ref->{$key} = $cfg_ref->{$key};
55 }
56 }
57
58 # Add defaults
59 0 foreach my $key (%$default_options)
60 {
61 0 ▶ if ($ref->{$key} eq '')
Branch: 1 branch
BranchTrueFalseTotal
if ($ref->{$key} eq "") 0 0 0
62 {
63 0 $ref->{$key} = $default_options->{$key};
64 }
65 }
66
67 # Add BulkSMS defaults
68 0 foreach my $key (%$bulk_sms_send_defaults)
69 {
70 0 ▶ if ($ref->{$key} eq '')
Branch: 1 branch
BranchTrueFalseTotal
if ($ref->{$key} eq "") 0 0 0
71 {
72 0 $ref->{$key} = $bulk_sms_send_defaults->{$key};
73 }
74 }
75
76 #Check mandatory options
77 0 foreach my $key (@mandatory_options)
78 {
79 0 ▶ if ($ref->{$key} eq '')
Branch: 1 branch
BranchTrueFalseTotal
if ($ref->{$key} eq "") 0 0 0
80 {
81 0 $error .= "Option '$key' does not have a value.\n";
82 }
83 }
84
85 # Is there a proxy ?
86 0 ▶ $ENV{http_proxy} = $ref->{http_proxy} if ($ref->{http_proxy} ne '');
Branch: 1 branch
BranchTrueFalseTotal
if $ref->{"http_proxy"} ne "" 0 0 0
87
88 # Wich base URL to use ?
89 0 $ref->{base_url} = $urls->{$ref->{country}};
90
91 0 ▶ return 0 if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 0 0 0
92
93 0 bless ( $ref, $class );
94 }
95  
96 sub send_batch
97 0 ▶{
Subroutine send_batch: not called CC=11 SCAR=48.8
Pod documented
98 0 my $this = shift @_;
99 0 my $data = shift @_;
100 0 my %data = %$data;
101 0 my $csv_data = "msisdn,message\n";
102 0 my $recipient_count = 0;
103
104 0 foreach my $number ( keys( %data ) )
105 {
106 0 $number =~ s/\D//g; # strip all non-digits
107 0 ▶ next if $number !~ /\d/;
Branch: 1 branch
BranchTrueFalseTotal
unless $number =~ /\d/ 0 0 0
108
109 0 ▶ next if ($this->{sa_numbers_only} > 0 && $number !~ /^(27|0[78])/);
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"sa_numbers_only"} > 0 and not $number =~ /^(27|0[78])/ 0 0 0
Condition0%0/3
$this->{"sa_numbers_only"} > 0 and not $number =~ /^(27|0[78])/
!ll&&!rl&&r
000
MC/DC0%0/2
$this->{"sa_numbers_only"} > 0 and not $number =~ /^(27|0[78])/
$this->{"sa_numbers_only"} > 0 not $number =~ /^(27|0[78])/
Truth table0%0/3
A and not(B)
A= $this->{"sa_numbers_only"} > 0B= not $number =~ /^(27|0[78])/
ABresult
0X0
100
111
110  
111 0 ▶ if ($this->{sa_numbers_only} > 0 && $number =~ /^(27[78])/)
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"sa_numbers_only"} > 0 and $number =~ /^(27[78])/) 0 0 0
Condition0%0/3
$this->{"sa_numbers_only"} > 0 and $number =~ /^(27[78])/
!ll&&!rl&&r
000
MC/DC0%0/2
$this->{"sa_numbers_only"} > 0 and $number =~ /^(27[78])/
$this->{"sa_numbers_only"} > 0 $number =~ /^(27[78])/
Truth table0%0/3
A and B
A= $this->{"sa_numbers_only"} > 0B= $number =~ /^(27[78])/
ABresult
0X0
100
111
112 {
113 # SA cell prefixes as per http://en.wikipedia.org/wiki/Telephone_numbers_in_South_Africa
114 0 $number =~ s/^0(82|83|84|72|73|74|76|78|79)(\d+)/27$1$2/;
115 }
116
117 0 ▶ if ($number eq '')
Branch: 1 branch
BranchTrueFalseTotal
if ($number eq "") 0 0 0
118 {
119 0 $this->{error} = "One of the recipient numbers is invalid: $number";
120 0 return 0;
121 }
122
123 0 $csv_data .= "\"$number\",\"" . $data{$number} . "\"\n";
124 0 $recipient_count++;
125 }
126
127 0 ▶ return 0 unless $recipient_count;
Branch: 1 branch
BranchTrueFalseTotal
unless $recipient_count 0 0 0
128
129 0 my $url = $this->{base_url} . '/eapi/submission/send_batch/1/1.0';
130 0 my $args = { batch_data => $csv_data };
131 0 my @tmp;
132
133 0 ▶ if (! (@tmp = $this->http_post($url, $args)) )
Branch: 1 branch
BranchTrueFalseTotal
unless (@tmp = $this->http_post($url, $args)) 0 0 0
134 {
135 0 $this->send_to_log("WARN: Could not do http_post() for send_batch(): " . $this->{error});
136 0 return 0;
137 }
138  
139 0 my $retval = pop( @tmp );
140 0 my $log_mesg = "SMS batch sent. Results: $retval";
141  
142 0 ▶ $this->send_to_log( $log_mesg ) if $this->{verbose} > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"verbose"} > 0 0 0 0
143
144 0 return $retval;
145 }
146  
147 sub send_sms
148 0 ▶{
Subroutine send_sms: not called CC=15 SCAR=54.8
Pod documented
149 0 my $this = shift @_;
150 0 my $message = shift @_;
151 0 my @recipients = @_;
152
153 # un-comment at your convenience
154 #if (!$message) {
155 # $this->{error} = "Message is empty!\n";
156 # return 0;
157 #}
158  
159 0 foreach my $number (@recipients)
160 {
161 0 $number =~ s/\D//g; # strip all non-digits
162 0 ▶ next if $number !~ /\d/;
Branch: 1 branch
BranchTrueFalseTotal
unless $number =~ /\d/ 0 0 0
163
164 0 ▶ next if ($this->{sa_numbers_only} > 0 && $number !~ /^(27|0[78])/);
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"sa_numbers_only"} > 0 and not $number =~ /^(27|0[78])/ 0 0 0
Condition0%0/3
$this->{"sa_numbers_only"} > 0 and not $number =~ /^(27|0[78])/
!ll&&!rl&&r
000
MC/DC0%0/2
$this->{"sa_numbers_only"} > 0 and not $number =~ /^(27|0[78])/
$this->{"sa_numbers_only"} > 0 not $number =~ /^(27|0[78])/
Truth table0%0/3
A and not(B)
A= $this->{"sa_numbers_only"} > 0B= not $number =~ /^(27|0[78])/
ABresult
0X0
100
111
165
166 0 ▶ if ($this->{sa_numbers_only} > 0 && $number =~ /^(27[78])/)
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"sa_numbers_only"} > 0 and $number =~ /^(27[78])/) 0 0 0
Condition0%0/3
$this->{"sa_numbers_only"} > 0 and $number =~ /^(27[78])/
!ll&&!rl&&r
000
MC/DC0%0/2
$this->{"sa_numbers_only"} > 0 and $number =~ /^(27[78])/
$this->{"sa_numbers_only"} > 0 $number =~ /^(27[78])/
Truth table0%0/3
A and B
A= $this->{"sa_numbers_only"} > 0B= $number =~ /^(27[78])/
ABresult
0X0
100
111
167 {
168 # SA mobile prefixes as per http://en.wikipedia.org/wiki/Telephone_numbers_in_South_Africa
169 0 $number =~ s/^0(82|83|84|72|73|74|76|78|79)(\d+)/27$1$2/;
170 }
171
172 0 ▶ if ($number eq '')
Branch: 1 branch
BranchTrueFalseTotal
if ($number eq "") 0 0 0
173 {
174 0 $this->{error} = "One of the recipient numbers is invalid: $number";
175 0 return 0;
176 }
177
178 }
179
180 0 my $args;
181
182 # Extract all the BulkSMS options
183 0 foreach my $option (@bulksms_send_options)
184 {
185 0 ▶ if ( exists($this->{$option}) && $this->{$option} ne '' )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $this->{$option} and $this->{$option} ne "") 0 0 0
Condition0%0/3
exists $this->{$option} and $this->{$option} ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
exists $this->{$option} and $this->{$option} ne ""
exists $this->{$option} $this->{$option} ne ""
Truth table0%0/3
A and B
A= exists $this->{$option}B= $this->{$option} ne ""
ABresult
0X0
100
111
186 {
187 0 $args->{$option} = $this->{$option};
188 }
189 }
190
191 0 $args->{msisdn} = join(',', @recipients);
192 0 $args->{message} = $message;
193
194 0 my @tmp;
195 0 my $url = $this->{base_url} . '/eapi/submission/send_sms/2/2.0';
196  
197 0 ▶ if ( $this->{quote} > 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"quote"} > 0) 0 0 0
198 {
199 # This is a hack to get a quote on much credits an SMS will cost
200 0 $url = $this->{base_url} . '/eapi/submission/quote_sms/2/2.0';
201 }
202  
203 0 ▶ if (! (@tmp = $this->http_post($url, $args)) )
Branch: 1 branch
BranchTrueFalseTotal
unless (@tmp = $this->http_post($url, $args)) 0 0 0
204 {
205 0 $this->send_to_log("WARN: Could not do http_post() for send_sms(): " . $this->{error});
206 0 return 0;
207 }
208  
209 0 ▶ return pop( @tmp ) if $this->{quote} > 0; # ... for the quote_sms hack
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"quote"} > 0 0 0 0
210  
211 0 my $retval = pop( @tmp );
212 0 my $log_mesg = "SMS sent to " . join(',', @recipients) . ". Results: $retval";
213  
214 0 ▶ $this->send_to_log( $log_mesg ) if $this->{verbose} > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"verbose"} > 0 0 0 0
215
216 0 return $retval;
217 }
218  
219 sub quote_sms
220 0 ▶{
Subroutine quote_sms: not called CC=1 SCAR=6.9
Pod documented
221 0 my $this = shift @_;
222 0 my $message = shift @_;
223 0 my @recipients = @_;
224  
225 0 $this->{quote} = 1;
226  
227 0 my $quotation = $this->send_sms($message, @recipients);
228  
229 0 $this->{quote} = 0;
230  
231 0 return $quotation;
232 }
233  
234 sub get_credits
235 0 ▶{
Subroutine get_credits: not called CC=3 SCAR=24.8
Pod documented
236 0 my $this = shift @_;
237 0 my $url = $this->{base_url} . '/eapi/user/get_credits/1/1.1';
238 0 my @tmp = $this->http_post($url);
239  
240 my ($status, $balance) = split /\|/, pop(@tmp)
241 0 ▶ || ($this->send_to_log("WARN: Could not do http_post() for get_credits(): " . $this->{error}) && return -1);
Condition0%0/5
pop @tmp || $this->send_to_log('WARN: Could not do http_post() for get_credits(): ' . $this->{'error'}) && (return -1)
l!l&&r!l&&!r
000
$this->send_to_log('WARN: Could not do http_post() for get_credits(): ' . $this->{"error"}) and return -1
!ll
00
MC/DC0%0/2
pop @tmp || $this->send_to_log('WARN: Could not do http_post() for get_credits(): ' . $this->{'error'}) && (return -1)
pop @tmp $this->send_to_log('WARN: Could not do http_post() for get_credits(): ' . $this->{"error"})
Truth table0%0/3
A || B
A= pop @tmpB= $this->send_to_log('WARN: Could not do http_post() for get_credits(): ' . $this->{"error"})
ABresult
1X1
011
000
242  
243 0 return $balance;
244 }
245  
246 sub get_inbox
247 0 ▶{
Subroutine get_inbox: not called CC=7 SCAR=40.3
Pod documented
248 0 my $this = shift @_;
249 0 my $last_retrieved_id = shift @_;
250 0 my @tmp;
251  
252 0 ▶ if ( !defined($last_retrieved_id) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $last_retrieved_id) 0 0 0
253 {
254 0 $last_retrieved_id = 0;
255 }
256
257 0 my $url = $this->{base_url} . '/eapi/reception/get_inbox/1/1.0';
258 0 my $args = {last_retrieved_id => $last_retrieved_id};
259  
260 0 ▶ if (! (@tmp = $this->http_post($url, $args)) )
Branch: 1 branch
BranchTrueFalseTotal
unless (@tmp = $this->http_post($url, $args)) 0 0 0
261 {
262 0 $this->send_to_log("WARN: Could not do http_post() for get_inbox(): " . $this->{error});
263 0 return 0;
264 }
265  
266 0 my $end_of_headers_marker = 0;
267 0 my @results;
268  
269 0 foreach my $line (@tmp)
270 {
271 0 ▶ $end_of_headers_marker = 1 if $line =~ /^$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^$/ 0 0 0
272 0 ▶ next if $line =~ /^$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^$/ 0 0 0
273 0 ▶ push (@results, $line) if $end_of_headers_marker == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $end_of_headers_marker == 1 0 0 0
274 }
275  
276 0 return @results;
277 }
278  
279 sub get_report
280 0 ▶{
Subroutine get_report: not called CC=7 SCAR=40.3
Pod documented
281 0 my $this = shift @_;
282 0 my $batch_id = shift @_;
283 0 my @tmp;
284
285 0 ▶ if ( !$batch_id )
Branch: 1 branch
BranchTrueFalseTotal
unless ($batch_id) 0 0 0
286 {
287 0 $this->{error} = "batch_id was not specified\n";
288 0 return 0;
289 }
290
291 0 my $url = $this->{base_url} .= '/eapi/status_reports/get_report/2/2.0?';
292 0 my $args = {batch_id => $batch_id, optional_fields => 'body,completed_time,created_time,credits,origin_address,source_id'};
293  
294 0 ▶ if (! (@tmp = $this->http_post($url, $args)) )
Branch: 1 branch
BranchTrueFalseTotal
unless (@tmp = $this->http_post($url, $args)) 0 0 0
295 {
296 0 $this->send_to_log( "WARN: Could not do http_post() for get_report(): " . $this->{error} );
297 0 return 0;
298 }
299  
300 0 my $end_of_headers_marker = 0;
301 0 my @results;
302  
303 0 foreach my $line (@tmp)
304 {
305 0 ▶ $end_of_headers_marker = 1 if $line =~ /^$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^$/ 0 0 0
306 0 ▶ next if $line =~ /^$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^$/ 0 0 0
307 0 ▶ push (@results, $line) if $end_of_headers_marker == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $end_of_headers_marker == 1 0 0 0
308 }
309
310 0 return @results;
311 }
312  
313 sub http_post
314 0 ▶{
Subroutine http_post: not called CC=8 SCAR=42.8
Pod undocumented
315 0 my $this = shift @_;
316 0 my $url = shift @_;
317 0 my $args = shift @_;
318
319 0 ▶ my $timeout = $this->{timeout} || 30;
Condition0%0/2
$this->{'timeout'} || 30
l!l
00
MC/DC0%0/1
$this->{'timeout'} || 30
$this->{'timeout'}
Truth table0%0/2
A
A= $this->{'timeout'}
Aresult
11
00
320
321 0 my $uagent = LWP::UserAgent->new(timeout => $timeout);
322  
323 0 ▶ if( exists($this->{http_proxy}) && $this->{http_proxy} ne '' )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $this->{"http_proxy"} and $this->{"http_proxy"} ne "") 0 0 0
Condition0%0/3
exists $this->{"http_proxy"} and $this->{"http_proxy"} ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
exists $this->{"http_proxy"} and $this->{"http_proxy"} ne ""
exists $this->{"http_proxy"} $this->{"http_proxy"} ne ""
Truth table0%0/3
A and B
A= exists $this->{"http_proxy"}B= $this->{"http_proxy"} ne ""
ABresult
0X0
100
111
324 {
325 0 $uagent->proxy(['http'], $this->{http_proxy});
326 }
327
328 0 my $request = HTTP::Request->new(POST => $url);
329 0 $request->content_type('application/x-www-form-urlencoded');
330
331 0 my $content = 'username=' . $this->{username} . '&password=' . $this->{password};
332
333 0 foreach my $arg (keys(%$args))
334 {
335 0 $content .= '&' . $arg . '=' . $args->{$arg};
336 }
337
338 0 $request->content($content);
339
340 0 ▶ $this->send_to_log("INFO: URL=$url content=$content") if $this->{verbose} > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"verbose"} > 0 0 0 0
341
342 0 my $response = $uagent->request($request);
343
344 0 ▶ if ($response->is_success)
Branch: 2 branches
BranchTrueFalseTotal
if ($response->is_success) { } 0 0 0
elsif ($response->is_error) { } 0 0 0
345 {
346 0 my @tmp = split( /\n/, $response->as_string );
347 0 return @tmp;
348 }
349 elsif ($response->is_error)
350 {
351 0 $this->{error} = $response->code . ':' . $response->message . "\n";
352 0 return 0;
353 }
354 else
355 {
356 0 $this->{error} = $response->code . ':' . $response->message . ':' . $response->content . "\n";
357 0 return 0;
358 }
359 }
360  
361 sub send_to_log
362 0 ▶{
Subroutine send_to_log: not called CC=4 SCAR=30.0
Pod undocumented
363 0 my $this = shift @_;
364 0 my $message = shift @_;
365
366 0 chomp($message);
367
368 0 ▶ if ($this->{logfile} == -1)
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"logfile"} == -1) 0 0 0
369 {
370 0 return 1;
371 }
372
373 0 ▶ if ($this->{logfile} eq '')
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"logfile"} eq "") 0 0 0
374 {
375 0 $this->{logfile} = "$0.log";
376 }
377
378 0 ▶ open (LGFH, ">>".$this->{logfile}) || die "ERROR: Could not open " . $this->{logfile} . ": $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open LGFH, '>>' . $this->{"logfile"} or die 'ERROR: Could not open ' . $this->{"logfile"} . ": $!\n" 0 0 0
379
380 0 print LGFH scalar(localtime()) . " - $message - $0\n";
381
382 0 close (LGFH);
383 }
384  
385 sub _parse_config
386 0 ▶{
Subroutine _parse_config: not called CC=6 SCAR=37.4
387 0 my $file = shift @_;
388 0 my $cfg_ref;
389
390 0 ▶ open (CFG, $file) || die "ERROR: Could not open $file: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open CFG, $file or die "ERROR: Could not open ${file}: $!\n" 0 0 0
391
392 0 ▶ while (<CFG>)
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline CFG)) 0 0 0
393 {
394 0 chomp;
395
396 0 ▶ next if /^\s+$/; # Ignore lines with just whitespace...
Branch: 1 branch
BranchTrueFalseTotal
if /^\s+$/ 0 0 0
397 0 ▶ next if /^$/; # blank lines...
Branch: 1 branch
BranchTrueFalseTotal
if /^$/ 0 0 0
398 0 ▶ next if /^#/; # and lines that start with a comment.
Branch: 1 branch
BranchTrueFalseTotal
if /^#/ 0 0 0
399
400 0 s/#.*//; # Strip away all comments
401
402 0 s/^\s+//; # Remove leading...
403 0 s/\s+$//; # ...and trailing whitespace
404
405 0 s/\s*=\s*/=/;
406
407 0 my ($var, $val) = split /=/;
408 0 $cfg_ref->{$var} = $val;
409 }
410
411 0 close (CFG);
412
413 0 return $cfg_ref;
414 }
415  
416 1 1;
417  
418 __END__
↑ Summary