O2.pm

filter: Statementstmt 34.5% 28 / 81 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 42.8% 6 / 14 Podpod 20.0% 1 / 5 totaltotal 27.3% 35 / 128 CC 17 SCAR 25.6
CC
17
Cov
26%
_check_length
30.0
_init
20.0
AUTOLOAD
13.3
CRAP 12.9
↑ Summary o2.pl »
1 package Net::SMS::O2;
2  
3 1 $VERSION = '0.020';
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Net::SMS::Web;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 #------------------------------------------------------------------------------
12 #
13 # POD
14 #
15 #------------------------------------------------------------------------------
16  
17 =head1 NAME
18  
19 Net::SMS::O2 - a module to send SMS messages using the O2 web2sms
20 gateway (L<http://www.o2.co.uk/>).
21  
22 =head1 SYNOPSIS
23  
24 my $sms = Net::SMS::O2->new(
25 autotruncate => 1,
26 username => 'yourname',
27 password => 'yourpassword',
28 recipient => 07713123456,
29 subject => 'a test',
30 message => 'a test message',
31 );
32  
33 $sms->verbose( 1 );
34 $sms->message( 'a different message' );
35 print "sending message to mobile number ", $sms->recipient();
36  
37 $sms->send_sms();
38  
39 =head1 DESCRIPTION
40  
41 A perl module to send SMS messages, using the O2 web2sms gateway. This
42 module will only work with mobile phone numbers that have been registered with
43 O2 (L<http://www.o2.co.uk/>) and uses form submission to a URL that may be
44 subject to change. The O2 service is currently only available to UK mobile
45 phone users.
46  
47 There is a maximum length for SMS subject + message (115 for O2). If the sum
48 of subject and message lengths exceed this, the behaviour of the
49 Net::SMS::O2 objects depends on the value of the 'autotruncate' argument to
50 the constructor. If this is a true value, then the subject / message will be
51 truncated to 115 characters. If false, the object will throw an exception
52 (die). If you set notruncate to 1, then the module won't check the message
53 length, and you are on your own!
54  
55 Pragma: no-cache
56 Via: 1.0 www3 (HTTP::Proxy/0.07)
57 Accept: application/vnd.ms-excel, application/msword, application/vnd.ms-powerpoint, image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*
58 Accept-Language: en-gb
59 Host: sendtxt.o2.co.uk
60 Referer: http://sendtxt.o2.co.uk/webOriginate/action/viewHomePage
61 User-Agent: Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0; Q312461)
62 Content-Length: 128
63 Content-Type: application/x-www-form-urlencoded
64 Cookie: anonP3=Ave; username=awrigley; QE3=UmFuZG9tSVYa8uqjNRdebN1ccKhWTkav; rAT=qCsxqa-8q_UNEMeYxegp6dxlG3Pm_mPi; rID=44413; JSESSIONID=2flHC7azFO4dDFoUUdkrm5QD4x8ocdbpgpVzGgVVVouWlL24iKvO!-1057879407
65  
66  
67 contacts=&msisdns=07713986247&to=07713986247&subject=test&howtosend=freetxt&message=test&replyType=inbox&enableReply=on&x=16&y=1
68  
69 =cut
70  
71 #------------------------------------------------------------------------------
72 #
73 # Package globals
74 #
75 #------------------------------------------------------------------------------
76  
77 1 ▶use vars qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
78 @ISA
79 $LOGIN_URL1
80 $LOGIN_URL2
81 $QUOTA_URL
82 $SEND_URL
83 %REQUIRED_KEYS
84 %LEGAL_KEYS
85 $MAX_CHARS
86 1 );
87  
88 1 @ISA = qw( Net::SMS::Web );
89  
90 1 $SEND_URL = 'http://sendtxt.o2.co.uk/webOriginate/action/sendMessage';
91 1 $QUOTA_URL = 'http://sendtxt.o2.co.uk/webOriginate/action/viewHomePage';
92 1 $LOGIN_URL1 = 'https://gordon.genie.co.uk/login/mblogin';
93 1 $LOGIN_URL2 = "https://zarkov.shop.o2.co.uk/login/bglogin";
94  
95 1 %REQUIRED_KEYS = (
96 username => 1,
97 password => 1,
98 );
99  
100 1 %LEGAL_KEYS = (
101 username => 1,
102 password => 1,
103 recipient => 1,
104 subject => 1,
105 message => 1,
106 verbose => 1,
107 audit_trail => 1,
108 );
109  
110 1 $MAX_CHARS = 115;
111  
112 #------------------------------------------------------------------------------
113 #
114 # Constructor
115 #
116 #------------------------------------------------------------------------------
117  
118 =head1 CONSTRUCTOR
119  
120 The constructor for Net::SMS::O2 takes the following arguments as hash
121 values (see L<SYNOPSIS|"SYNOPSIS">):
122  
123 =head2 autotruncate (OPTIONAL)
124  
125 O2 has a upper limit on the length of the subject + message (115). If
126 autotruncate is true, subject and message are truncated to 115 if the sum of
127 their lengths exceeds 115. The heuristic for this is simply to treat subject
128 and message as a string and truncate it (i.e. if length(subject) >= 115 then
129 message is truncated to 0. Thanks to Mark Zealey <mark@itsolve.co.uk> for this
130 suggestion. The default for this is false.
131  
132 =head2 notruncate (OPTIONAL)
133  
134 Of course, if you don't believe the O2 web interface about maximum character
135 length, then you can set this option.
136  
137 =head2 username (REQUIRED)
138  
139 The O2 username for the user (assuming that the user is already registered
140 at L<http://www.o2.co.uk/>.
141  
142 =head2 password (REQUIRED)
143  
144 The O2 password for the user (assuming that the user is already registered
145 at L<http://www.o2.co.uk/>.
146  
147 =head2 recipient (REQUIRED)
148  
149 Mobile number for the intended SMS recipient.
150  
151 =head2 subject (REQUIRED)
152  
153 SMS message subject.
154  
155 =head2 message (REQUIRED)
156  
157 SMS message body.
158  
159 =head2 verbose (OPTIONAL)
160  
161 If true, various soothing messages are sent to STDERR. Defaults to false.
162  
163 =cut
164  
165 sub new
166 0 ▶{
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
167 0 my $class = shift;
168 0 my $self = $class->SUPER::new( @_ );
169 0 $self->_init( @_ );
170 0 return $self;
171 }
172  
173 #------------------------------------------------------------------------------
174 #
175 # AUTOLOAD - to set / get object attributes
176 #
177 #------------------------------------------------------------------------------
178  
179 =head1 AUTOLOAD
180  
181 All of the constructor arguments can be got / set using accessor methods. E.g.:
182  
183 $old_message = $self->message;
184 $self->message( $new_message );
185  
186 =cut
187  
188 sub AUTOLOAD
189 0 ▶{
Subroutine AUTOLOAD: not called CC=4 SCAR=25.8
190 0 my $self = shift;
191 0 my $value = shift;
192  
193 1 ▶ use vars qw( $AUTOLOAD );
Subroutine BEGIN: called CC=1 SCAR=0.0
194 0 my $key = $AUTOLOAD;
195 0 $key =~ s/.*:://;
196 0 ▶ return if $key eq 'DESTROY';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "DESTROY" 0 0 0
197 croak ref($self), ": unknown method $AUTOLOAD\n"
198 0 ▶ unless $LEGAL_KEYS{ $key }
Branch: 1 branch
BranchTrueFalseTotal
unless $Net::SMS::O2::LEGAL_KEYS{$key} 0 0 0
199 ;
200 0 ▶ if ( defined( $value ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 0 0
201 {
202 0 $self->{$key} = $value;
203 }
204 0 return $self->{$key};
205 }
206  
207 =head1 METHODS
208  
209 =head2 send_sms
210  
211 This method is invoked to actually send the SMS message that corresponds to the
212 constructor arguments.
213  
214 =cut
215  
216 sub get_form
217 0 ▶{
Subroutine get_form: not called CC=2 SCAR=17.9
Pod undocumented
218 0 my $self = shift;
219 0 my $response = $self->response();
220 0 my %params;
221 0 ▶ while ( $response =~ /name="([^"]+)"\s+value="([^"]+)"/g )
Branch: 1 branch
BranchTrueFalseTotal
if ($response =~ /name="([^"]+)"\s+value="([^"]+)"/g) 0 0 0
222 {
223 0 $params{$1} = $2;
224 }
225 0 return \%params;
226 }
227  
228 sub login
229 0 ▶{
Subroutine login: not called CC=2 SCAR=17.9
Pod undocumented
230 0 my $self = shift;
231 0 my $dest = shift;
232  
233 0 ▶ return if $self->{is_logged_in};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"is_logged_in"} 0 0 0
234 $self->action( Net::SMS::Web::Action->new(
235 url => $LOGIN_URL1,
236 method => 'GET',
237 params => {
238 dest => $dest,
239 username => $self->{username},
240 password => $self->{password},
241 }
242 0 ) );
243 # my $params = $self->get_form();
244 # $self->action( Net::SMS::Web::Action->new(
245 # url => $LOGIN_URL2,
246 # method => 'POST',
247 # params => $params,
248 # ) );
249  
250 0 $self->{is_logged_in} = 1;
251 }
252  
253 sub quota
254 0 ▶{
Subroutine quota: not called CC=3 SCAR=24.8
Pod undocumented
255 0 my $self = shift;
256 0 ▶ my $type = shift || 'free';
Condition0%0/2
shift() || 'free'
l!l
00
MC/DC0%0/1
shift() || 'free'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
257 0 $self->login( $QUOTA_URL );
258 0 $self->action( Net::SMS::Web::Action->new(
259 url => $QUOTA_URL,
260 method => 'GET',
261 ) );
262 0 ▶ if ( $self->response() =~ /Use $type TXT - you have (\d+) TXT remaining/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->response =~ /Use $type TXT - you have (\d+) TXT remaining/) 0 0 0
263 {
264 0 return $1;
265 }
266 0 die "Can't determine quota";
267 }
268  
269 sub send_sms
270 0 ▶{
Subroutine send_sms: not called CC=3 SCAR=24.8
Pod documented
271 0 my $self = shift;
272  
273 0 $self->login( $SEND_URL );
274 $self->action( Net::SMS::Web::Action->new(
275 url => $SEND_URL,
276 method => 'POST',
277 params => {
278 msisdns => $self->{recipient},
279 contacts => '',
280 to => $self->{recipient},
281 subject => $self->{subject} || '',
282 howtosend => 'freetxt',
283 replyType => 'none',
284 message => $self->{message},
285 }
286 0 ▶ ) );
Condition0%0/2
$self->{'subject'} || ''
l!l
00
MC/DC0%0/1
$self->{'subject'} || ''
$self->{'subject'}
Truth table0%0/2
A
A= $self->{'subject'}
Aresult
11
00
287 0 ▶ if ( $self->response =~ /Your message has been sent successfully/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->response =~ /Your message has been sent successfully/) { } 0 0 0
288 {
289 0 return 1;
290 }
291 else
292 {
293 0 return 0;
294 }
295 }
296  
297 sub _check_length
298 0 ▶{
Subroutine _check_length: not called CC=5 SCAR=34.0
299 0 my $self = shift;
300 0 $self->{message_length} = 0;
301 0 ▶ if ( $self->{autotruncate} )
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'autotruncate'}) { } 0 0 0
elsif (not $self->{'notruncate'}) { } 0 0 0
302 {
303 # Chop the message down the the correct length. Also supports subjects
304 # > $MAX_CHARS, but I think it's a bit stupid to send one, anyway ...
305 # - Mark Zealey
306 0 $self->{subject} = substr $self->{subject}, 0, $MAX_CHARS;
307 $self->{message} =
308 substr $self->{message}, 0, $MAX_CHARS - length $self->{subject}
309 0 ;
310 0 $self->{message_length} += length $self->{$_} for qw/subject message/;
311 }
312 elsif ( ! $self->{notruncate} )
313 {
314 $self->{message_length} =
315 length( $self->{subject} ) + length( $self->{message} )
316 0 ;
317 0 ▶ if ( $self->{message_length} > $MAX_CHARS )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"message_length"} > $Net::SMS::O2::MAX_CHARS) 0 0 0
318 {
319 0 croak ref($self),
320 ": total message length (subject + message) is too long ",
321 "(> $MAX_CHARS)\n"
322 ;
323 }
324 }
325 }
326  
327 sub _init
328 0 ▶{
Subroutine _init: not called CC=4 SCAR=30.0
329 0 my $self = shift;
330 0 my %keys = @_;
331  
332 0 for ( keys %REQUIRED_KEYS )
333 {
334 0 ▶ croak ref($self), ": $_ field is required\n" unless $keys{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless $keys{$_} 0 0 0
335 }
336 0 for ( keys %keys )
337 {
338 0 $self->{$_} = $keys{$_};
339 }
340 0 $self->_check_length();
341 }
342  
343 #------------------------------------------------------------------------------
344 #
345 # More POD ...
346 #
347 #------------------------------------------------------------------------------
348  
349 =head1 SEE ALSO
350  
351 L<Net::SMS::Web>.
352  
353 =head1 BUGS
354  
355 Bugs can be submitted to the CPAN RT bug tracker either via email
356 (bug-net-sms-o2@rt.cpan.org) or web
357 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-SMS-O2>. There is also a
358 sourceforge project at L<http://sourceforge.net/projects/net-sms-web/>.
359  
360 =head1 AUTHOR
361  
362 Ave Wrigley <Ave.Wrigley@itn.co.uk>
363  
364 =head1 COPYRIGHT
365  
366 Copyright (c) 2001 Ave Wrigley. All rights reserved. This program is free
367 software; you can redistribute it and/or modify it under the same terms as Perl
368 itself.
369  
370 =cut
371  
372 1 1;
↑ Summary o2.pl »