line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Net::SMS::Mach; |
2
|
|
|
|
|
|
|
BEGIN { |
3
|
1
|
|
|
1
|
|
24121
|
$Net::SMS::Mach::VERSION = '0.02'; |
4
|
|
|
|
|
|
|
} |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
# ABSTRACT: Send SMS messages via the Mach HTTP API |
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
9
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
35
|
|
9
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
41
|
|
10
|
|
|
|
|
|
|
|
11
|
1
|
|
|
1
|
|
11
|
use Carp; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
114
|
|
12
|
1
|
|
|
1
|
|
117957
|
use HTTP::Request::Common; |
|
1
|
|
|
|
|
52297
|
|
|
1
|
|
|
|
|
113
|
|
13
|
1
|
|
|
1
|
|
11350
|
use LWP::UserAgent; |
|
1
|
|
|
|
|
46904
|
|
|
1
|
|
|
|
|
38
|
|
14
|
1
|
|
|
1
|
|
1075
|
use Encode qw(encode decode); |
|
1
|
|
|
|
|
15486
|
|
|
1
|
|
|
|
|
109
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
use constant { |
17
|
1
|
|
|
|
|
545
|
PROVIDER1 => "http://gw1.promessaging.com/sms.php", |
18
|
|
|
|
|
|
|
PROVIDER2 => "http://gw2.promessaging.com/sms.php", |
19
|
|
|
|
|
|
|
TIMEOUT => 10 |
20
|
1
|
|
|
1
|
|
8
|
}; |
|
1
|
|
|
|
|
2
|
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub new { |
23
|
1
|
|
|
1
|
1
|
428
|
my ($class, %args) = @_; |
24
|
|
|
|
|
|
|
|
25
|
1
|
50
|
33
|
|
|
7
|
if (! exists $args{userid} || ! exists $args{password}) { |
26
|
0
|
|
|
|
|
0
|
Carp::croak("${class}->new() requires username and password as parameters\n"); |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
1
|
|
|
|
|
3
|
my $self = \%args; |
30
|
1
|
|
|
|
|
5
|
bless $self, $class; |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub send_sms { |
34
|
0
|
|
|
0
|
1
|
|
my ($self, %args) = @_; |
35
|
|
|
|
|
|
|
|
36
|
0
|
|
|
|
|
|
my $ua = LWP::UserAgent->new(); |
37
|
0
|
|
|
|
|
|
$ua->timeout(TIMEOUT); |
38
|
0
|
|
|
|
|
|
$ua->agent("Net::SMS::Mach/$Net::SMS::Mach::VERSION"); |
39
|
|
|
|
|
|
|
|
40
|
0
|
|
|
|
|
|
$args{number} =~ s{\D}{}g; |
41
|
|
|
|
|
|
|
|
42
|
0
|
|
|
|
|
|
my $message = $args{message}; |
43
|
0
|
|
|
|
|
|
my $enc; |
44
|
0
|
0
|
|
|
|
|
if ($args{encode}) { |
45
|
0
|
|
|
|
|
|
$enc = "ucs"; |
46
|
0
|
|
|
|
|
|
$message = encode_ucs($message); |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
0
|
|
|
|
|
|
my $hash = { |
50
|
|
|
|
|
|
|
dnr => "+$args{number}", |
51
|
|
|
|
|
|
|
snr => $args{sender}, |
52
|
|
|
|
|
|
|
msg => $message, |
53
|
|
|
|
|
|
|
encoding => $enc, |
54
|
|
|
|
|
|
|
}; |
55
|
|
|
|
|
|
|
|
56
|
0
|
0
|
|
|
|
|
my $url = $args{backup_server} ? PROVIDER2 : PROVIDER1; |
57
|
0
|
|
|
|
|
|
my $resp = $ua->request(POST $url, [ id => $self->{userid}, pw => $self->{password}, %$hash ]); |
58
|
0
|
|
|
|
|
|
my $as_string = $resp->as_string; |
59
|
|
|
|
|
|
|
|
60
|
0
|
0
|
|
|
|
|
if (! $resp->is_success) { |
61
|
0
|
|
|
|
|
|
my $status = $resp->status_line; |
62
|
0
|
|
|
|
|
|
warn "HTTP request failed: $status\n$as_string\n"; |
63
|
0
|
|
|
|
|
|
return 0; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
0
|
|
|
|
|
|
my $res = $resp->content; |
67
|
0
|
|
|
|
|
|
chomp($res); |
68
|
|
|
|
|
|
|
|
69
|
0
|
|
|
|
|
|
my $return = 1; |
70
|
0
|
0
|
|
|
|
|
unless ($res =~ /^\+OK/) { |
71
|
0
|
|
|
|
|
|
warn "Failed: $res\n"; |
72
|
0
|
|
|
|
|
|
$return = 0; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
0
|
0
|
|
|
|
|
return wantarray ? ($return, $res) : $return; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub encode_ucs |
80
|
|
|
|
|
|
|
{ |
81
|
0
|
|
|
0
|
0
|
|
my $message = shift; |
82
|
|
|
|
|
|
|
|
83
|
0
|
|
|
|
|
|
utf8::decode($message); |
84
|
0
|
|
|
|
|
|
utf8::decode($message); |
85
|
0
|
|
|
|
|
|
utf8::upgrade($message); |
86
|
|
|
|
|
|
|
|
87
|
0
|
|
|
|
|
|
my $encoded = unpack('H*', encode('UCS-2BE', $message)); |
88
|
|
|
|
|
|
|
|
89
|
0
|
|
|
|
|
|
return $encoded; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
1; |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
=pod |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
=head1 NAME |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
Net::SMS::Mach - Send SMS messages via the Mach HTTP API |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=head1 VERSION |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
version 0.02 |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=head1 SYNOPSIS |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
# Create a testing sender |
108
|
|
|
|
|
|
|
my $sms = Net::SMS::Mach->new( |
109
|
|
|
|
|
|
|
userid => '123456', password => 'testpass' |
110
|
|
|
|
|
|
|
); |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
# Send a message |
113
|
|
|
|
|
|
|
my ($sent, $status) = $sms->send_sms( |
114
|
|
|
|
|
|
|
message => "All your base are belong to us", |
115
|
|
|
|
|
|
|
number => '1234567890', |
116
|
|
|
|
|
|
|
sender => '+441234567890', |
117
|
|
|
|
|
|
|
); |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
$sent will contain a true / false if the sending worked, |
120
|
|
|
|
|
|
|
$status will contain the status message from the provider. |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
# If you just want a true / false if it workes, use : |
123
|
|
|
|
|
|
|
my $sent = $sms->send_sms( |
124
|
|
|
|
|
|
|
message => "All your base are belong to us", |
125
|
|
|
|
|
|
|
number => '1234567890', |
126
|
|
|
|
|
|
|
); |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
# If your message is utf8 encoded, or you are unsure if it's iso-8859-1, |
129
|
|
|
|
|
|
|
# use the encode flag to get the message UCS encoded. |
130
|
|
|
|
|
|
|
my ($sent, $status, $desc) = $sms->send_sms( |
131
|
|
|
|
|
|
|
message => "All your base are belong to us", |
132
|
|
|
|
|
|
|
number => '1234567890', |
133
|
|
|
|
|
|
|
encode => 1, |
134
|
|
|
|
|
|
|
); |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
if ($sent) { |
137
|
|
|
|
|
|
|
# Success, message sent |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
else { |
140
|
|
|
|
|
|
|
# Something failed |
141
|
|
|
|
|
|
|
warn("Failed : $status"); |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=head1 DESCRIPTION |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
Perl module to send SMS messages through the HTTP API provided by Mach |
147
|
|
|
|
|
|
|
(mach.com). |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=head1 METHODS |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=head2 new |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
new( userid => '123456', password => 'testpass' ) |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
Nothing fancy. You need to supply your username and password |
156
|
|
|
|
|
|
|
in the constructor, or it will complain loudly. |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=head2 send_sms |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
send_sms(number => $phone_number, message => $message, encode => 0, backup_server => 1) |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
Uses the API to send a message given in C<$message> to |
163
|
|
|
|
|
|
|
the phone number given in C<$phone_number>. |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
Phone number should be given with only digits, or with a "+" prefix. |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
=over 4 |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
=item C<1234567890> |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
=back |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
Returns a true / false value and a status message. The message is "success" if the server has accepted your query. |
174
|
|
|
|
|
|
|
This does not mean that the message has been delivered. |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=head1 SEE ALSO |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
Mach website, http://www.mach.com/ |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
=head1 AUTHOR |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
Terje Kristensen |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
This software is Copyright (c) 2011 by Opera Software ASA. |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
This is free software, licensed under: |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
The (three-clause) BSD License |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
=cut |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
__END__ |