File Coverage

blib/lib/Net/SMS/MessageNet.pm
Criterion Covered Total %
statement 15 86 17.4
branch 0 36 0.0
condition 0 6 0.0
subroutine 5 9 55.5
pod 3 3 100.0
total 23 140 16.4


line stmt bran cond sub pod time code
1             package Net::SMS::MessageNet;
2              
3 1     1   1653 use LWP();
  1         68385  
  1         28  
4 1     1   1056 use HTTP::Cookies();
  1         8945  
  1         30  
5 1     1   24 use URI::Escape();
  1         3  
  1         26  
6 1     1   7 use warnings;
  1         3  
  1         53  
7 1     1   18 use strict;
  1         3  
  1         2081  
8             our ($VERSION) = '0.65';
9             our (@ISA) = qw(Exporter);
10             our (@EXPORT) = qw(send_sms);
11              
12             sub new {
13 0     0 1   my ($class, $user_name, $password, $params) = @_;
14 0           my ($self) = {};
15 0           my ($timeout) = 60;
16 0 0         unless ($user_name) {
17 0           die("The user_name must be supplied\n");
18             }
19 0 0         if (ref $user_name) {
20 0           die("The user_name must be a scalar\n");
21             }
22 0           $self->{user_name} = $user_name;
23 0 0         unless ($password) {
24 0           die("The password must be supplied\n");
25             }
26 0 0         if (ref $password) {
27 0           die("The password must be a scalar\n");
28             }
29 0           $self->{password} = $password;
30 0 0         if (exists $params->{timeout}) {
31 0 0 0       if (($params->{timeout}) && ($params->{timeout} =~ /^\d+$/)) {
32 0           $timeout = $params->{timeout};
33             } else {
34 0           die("The 'timeout' parameter must be a number\n");
35             }
36             }
37 0           my ($name) = "Net::SMS::MessageNet $VERSION "; # a space causes the default LWP User Agent to be appended.
38 0 0         if (exists $params->{user_agent}) {
39 0 0 0       if (($params->{user_agent}) && ($params->{user_agent} =~ /\S/)) {
40 0           $name = $params->{user_agent};
41             }
42             }
43 0           my ($ua) = new LWP::UserAgent( timeout => $timeout,
44             keep_alive => 1 );
45 0           $ua->agent($name);
46 0           my ($cookieJar) = new HTTP::Cookies( hide_cookie2 => 1 );
47 0           $ua->cookie_jar($cookieJar);
48 0           $ua->requests_redirectable([ 'GET' ]);
49 0           $self->{_ua} = $ua;
50 0           bless $self, $class;
51 0           return ($self);
52             }
53              
54             sub send {
55 0     0 1   my ($self, $phone_number, $message) = @_;
56 0 0         if ((length ($message)) > 160) {
57 0           die("Message greater than 160 characters\n");
58             }
59 0 0         unless ($phone_number =~ /^\d+$/) {
60 0           die("Phone number must be all numbers. The country code should be included\n");
61             }
62 0           my ($url);
63 0           eval { require Net::HTTPS; };
  0            
64 0 0         if ($@) {
65 0 0         if ($^W) {
66 0           warn("Using insecure means to send sms to messagenet.com.au\n");
67             }
68 0           $url = 'http://www.messagenet.com.au/dotnet/Lodge.asmx/LodgeSMSMessage';
69             } else {
70 0 0         unless (defined $ENV{HTTPS_VERSION}) {
71 0           $ENV{HTTPS_VERSION} = '3';
72             }
73 0 0         unless (defined $ENV{HTTPS_CA_DIR}) {
74 0           $ENV{HTTPS_CA_DIR} = 'certs';
75             }
76 0           $url = 'https://www.messagenet.com.au/dotnet/Lodge.asmx/LodgeSMSMessage';
77             }
78 0           my ($request) = new HTTP::Request('POST' => $url);
79 0           $request->content_type('application/x-www-form-urlencoded');
80 0           my ($ua) = $self->{_ua};
81 0           my ($username) = $self->{user_name};
82 0           my ($password) = $self->{password};
83 0           $request->content("Username=" . URI::Escape::uri_escape($username) . '&Pwd=' . URI::Escape::uri_escape($password) . '&PhoneNumber=' . URI::Escape::uri_escape($phone_number) . '&PhoneMessage=' . URI::Escape::uri_escape($message));
84 0           my ($response);
85 0           eval {
86 0     0     local $SIG{'ALRM'} = sub { die("Timeout\n"); };
  0            
87 0           alarm $ua->timeout();
88 0           $response = $ua->request($request);
89 0           alarm 0;
90             };
91 0 0         if ($@) {
92 0           die("Failed to get a response from '$url':$@\n");
93             }
94 0 0         unless ($response->is_success()) {
95 0           die("Failed to get a successful response from sms attempt\n");
96             }
97 0           my ($response_as_string) = $response->as_string();
98 0 0         if ($response_as_string =~ /]+>([^<]+)<\/string>/) {
99 0           my ($actual_message) = $1;
100 0 0         if ($actual_message eq 'Message sent successfully.') {
101             } else {
102 0           die("Failed to send sms:$actual_message\n");
103             }
104             } else {
105 0           die("Unrecognisable response\n");
106             }
107             }
108              
109             sub send_sms {
110 0     0 1   my ($user_name, $password, $phone_number, $message) = @_;
111 0           __PACKAGE__->new($user_name, $password)->send($phone_number, $message);
112             }