File Coverage

blib/lib/Net/SMS/Optimus.pm
Criterion Covered Total %
statement 21 53 39.6
branch 0 24 0.0
condition n/a
subroutine 7 8 87.5
pod 1 1 100.0
total 29 86 33.7


line stmt bran cond sub pod time code
1             package Net::SMS::Optimus;
2              
3 1     1   23922 use warnings;
  1         2  
  1         28  
4 1     1   4 use strict;
  1         2  
  1         30  
5 1     1   4 use Carp;
  1         6  
  1         72  
6              
7 1     1   1219 use LWP::UserAgent;
  1         56132  
  1         39  
8 1     1   4471 use HTTP::Cookies;
  1         9042  
  1         36  
9 1     1   1120 use HTML::Form;
  1         43415  
  1         32  
10 1     1   9 use URI;
  1         2  
  1         467  
11              
12             =head1 NAME
13              
14             Net::SMS::Optimus - Send SMS through www.optimus.pt
15              
16             =head1 VERSION
17              
18             Version 0.05
19              
20             =cut
21              
22             our $VERSION = '0.05';
23             our (@ISA) = qw/Exporter/;
24             our (@EXPORT) = qw/send_sms/;
25              
26             =head1 SYNOPSIS
27              
28             This module exports just one function that
29             is responsible for sending your sms through
30             www.optimus.pt portal.
31              
32             use Net::SMS::Optimus;
33              
34             send_sms($username, $password, $number, $msg);
35              
36             =head1 EXPORT
37              
38             send_sms
39              
40             =head1 FUNCTIONS
41              
42             =head2 send_sms
43              
44             This function does all the magic. It receives the
45             following arguments:
46              
47             =over 4
48              
49             =item username
50              
51             The username to the portal
52              
53             =item password
54              
55             ...
56              
57             =item number
58              
59             A string containing the destination number
60              
61             =item message
62              
63             A string (no longer than 152 chars) containing the
64             message to be sent.
65              
66             =back
67              
68             The operation uses various phases, doing several
69             connections to the website, trying to mimic a real
70             human SMS send. If anything goes wrong, it croaks
71             and returns.
72              
73             =cut
74              
75             sub send_sms {
76 0     0 1   my ($username, $password, $number, $message) = @_;
77              
78             # Do some checks
79 0 0         croak "Username required" unless $username;
80 0 0         croak "Password required" unless $password;
81 0 0         croak "Number required" unless $number;
82 0 0         croak "You must specify a message, asshole" unless $message;
83 0 0         croak "Your message should be no more than 152 chars" unless (length $message) <= 152;
84              
85             # Initialize the browser
86 0           my $browser = LWP::UserAgent->new(
87             requests_redirectable => ['GET', 'HEAD', 'POST']
88             );
89 0           $browser->cookie_jar( {} );
90 0           $browser->env_proxy;
91            
92             # Fase 1: Login
93 0           my $res = $browser->get('http://optimus.pt/particulares/omeuoptimus/');
94 0 0         $res->is_success or die "Error reading from www.optimus.pt (Phase 1)\n";
95            
96 0           my @forms = HTML::Form->parse($res);
97 0           @forms = grep $_->attr("id") eq "aspnetForm", @forms;
98 0 0         die "No login form found (Phase 1)\n" unless @forms;
99 0           my $form = shift @forms;
100            
101 0           $form->value('ctl00$MainContentPlaceHolder$UserAuth1$TxtUsername', $username);
102 0           $form->value('ctl00$MainContentPlaceHolder$UserAuth1$TxtPassword', $password);
103            
104 0           $res = $browser->request($form->click);
105 0 0         $res->is_success or die "Error submiting login form (Phase 1)\n";
106            
107 0 0         $res->content =~ /Logout/ or die "Check username and/or password (Phase 1)\n";
108            
109             # Fase 2: Obter a form de SMS
110 0           $res->content =~ /id\=\"totalFreeSms\"\>(\d+)\
111 0           print "Free SMS: $1\n";
112            
113 0           @forms = HTML::Form->parse($res);
114 0           @forms = grep $_->attr("id") eq "aspnetForm", @forms;
115 0 0         die "No sms form found (Phase 2)\n" unless @forms;
116 0           $form = shift @forms;
117            
118             # Fase 3: Preencher a form e enviar
119 0           my $url = URI->new('http://optimus.pt/OMeuOptimus/OptimusOnlineAjaxCalls/SendSms.aspx');
120 0           $url->query_form(
121             To => $number,
122             Text => $message,
123             Type => 'normal'
124             );
125 0           $res = $browser->get($url);
126 0 0         $res->is_success or die "Error submiting SMS form (Phase 3)\n";
127            
128 0 0         $res->content =~ /Resultados\>\
129 0           print "SMS Sent :)\n";
130             }
131              
132             =head1 AUTHOR
133              
134             Ruben Fonseca, C<< >>
135              
136             =head1 BUGS
137              
138             Please report any bugs or feature requests to
139             C, or through the web interface at
140             L.
141             I will be notified, and then you'll automatically be notified of progress on
142             your bug as I make changes.
143              
144             =head1 SUPPORT
145              
146             You can find documentation for this module with the perldoc command.
147              
148             perldoc Net::SMS::Optimus
149              
150             You can also look for information at:
151              
152             =over 4
153              
154             =item * AnnoCPAN: Annotated CPAN documentation
155              
156             L
157              
158             =item * CPAN Ratings
159              
160             L
161              
162             =item * RT: CPAN's request tracker
163              
164             L
165              
166             =item * Search CPAN
167              
168             L
169              
170             =back
171              
172             =head1 ACKNOWLEDGEMENTS
173              
174             =head1 COPYRIGHT & LICENSE
175              
176             Copyright 2007 Ruben Fonseca, all rights reserved.
177              
178             This program is free software; you can redistribute it and/or modify it
179             under the same terms as Perl itself.
180              
181             =cut
182              
183             1; # End of Net::SMS::Optimus