WWW/SMS/o2UK.pm untested

filter: Statementstmt 0.0% 0 / 108 Branchbran 0.0% 0 / 102 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 225 CC 52 SCAR 78.1
CC
52
Cov
0%
_send
2756.0
MAXLENGTH
2.0
hnd_error
2.0
CRAP 2471.1
1 #!/usr/bin/perl -w
2 #Copyright (c) 2001 Giulio Motta. All rights reserved.
3 #http://www-sms.sourceforge.net/
4 #This program is free software; you can redistribute it and/or
5 #modify it under the same terms as Perl itself.
6 #
7 # required are openssl and NET::SSL; more details in the help prepended below
8 # you have to sign up with O2 (previous genie) as well
9 # which is free at the moment and you can send 100 sms/month free
10 # you have to pass the username/password or the file which contains it
11 # dunno all the sim cards provided yet: o2 and vodafone work
12 # Sat Nov 2 13:01:57 GMT 2002, Andre Howe, perl@andreh.de
13  
14 package WWW::SMS::o2UK;
15  
16 require Exporter;
17 @ISA = qw(Exporter);
18 @EXPORT = qw();
19 @EXPORT_OK = qw(@PREFIXES _send MAXLENGTH);
20 undef @PREFIXES;
21 $VERSION = '1.00';
22  
23 @PREFIXES = (Telephone::Number->new('44', undef, undef));
24  
25 my $debug = 0; # to switch on more debug output - is handled in levels 0 = no, 1 = low, n= high
26 my $send_sms = 1; # set this to one to turn off sending the sms in case you want to debug
27 # something but not wasting your free sms
28  
29 sub MAXLENGTH () {115}
30  
31 sub hnd_error {
32 $_ = shift;
33 $WWW::SMS::Error = "Failed at step $_ of module o2UK.pm\n";
34 return 0;
35 }
36  
37 sub _send {
38 my $self = shift;
39  
40 use HTTP::Request::Common qw(GET POST);
41 use HTTP::Cookies;
42 use LWP::UserAgent;
43  
44 $ua = LWP::UserAgent->new;
45 $ua->agent('Mozilla/5.0');
46 $ua->proxy('http', $self->{proxy}) if ($self->{proxy});
47 $ua->cookie_jar(HTTP::Cookies->new(
48 file => $self->{cookie_jar},
49 autosave => 1
50 )
51 );
52  
53 # preparing the message for the maximum length available
54 $self->{smstext} = substr($self->{smstext}, 0, MAXLENGTH - 1) if (length($self->{smstext})>MAXLENGTH);
55  
56 #
57 #STEP 1 - checking if all the modules are installed
58 #
59 my $step = 1;
60 # @todo ahe - check here for the modules needed e.g. Net::Ssl
61 print "\n STEP[$step] finished succesfully :-)\n" if $debug;
62 #
63 #STEP 1 - END
64 #
65  
66 #
67 #STEP 2 - checking if the username and password is passed
68 #
69 $step = 2;
70 return &hnd_error($step) unless (($self->{username} && $self->{password}));
71 print "\n STEP[$step] finished succesfully :-)\n" if $debug;
72 #
73 #STEP 2 - END
74 #
75  
76 #
77 #STEP 3 - first login to O2
78 #
79 $step = 3;
80 $req = POST 'https://gordon.genie.co.uk/login/mblogin',
81 [
82 'username' => $self->{username},
83 'password' => $self->{password}
84 ];
85  
86 my $file = $ua->request($req)->as_string;
87 print $file if ($debug >= 3);
88 return &hnd_error($step) unless ($file =~ /firstname/s);
89  
90 my $firstname = $1 if ($file =~ m/"firstname"\s+value="(.*)"/); print "\n firstname = $firstname" if ($debug >= 2);
91 my $username = $1 if ($file =~ m/"username"\s+value="(.*)"/); print "\n username = $username" if ($debug >= 2);
92 my $dest = $1 if ($file =~ m/"dest"\s+value="(.*)"/); print "\n dest = $dest" if ($debug >= 2);
93 my $mID = $1 if ($file =~ m/"mID"\s+value="(.*)"/); print "\n mID = $mID" if ($debug >= 2);
94 my $mAT = $1 if ($file =~ m/"mAT"\s+value="(.*)"/); print "\n mAT = $mAT" if ($debug >= 2);
95 my $QE3 = $1 if ($file =~ m/"QE3"\s+value="(.*)"/); print "\n QE3 = $QE3 \n" if ($debug >= 2);
96  
97 print "\n STEP[$step] finished succesfully :-)\n" if $debug;
98 #
99 #STEP 3 - END
100 #
101  
102 #
103 #STEP 4 - second login to O2
104 #
105 $step = 4;
106 $req = POST 'https://ming.mediamessaging.o2.co.uk/login/mm_bglogin',
107 [
108 'firstname' => $firstname,
109 'username' => $username,
110 'dest' => $dest,
111 'mID' => $mID,
112 'mAT' => $mAT,
113 'QE3' => $QE3
114 ];
115  
116 $file = $ua->request($req)->as_string;
117 print $file if ($debug >= 3);
118 return &hnd_error($step) unless ($file =~ /firstname/s);
119  
120 $firstname = $1 if ($file =~ m/"firstname"\s+value="(.*)"/); print "\n firstname = $firstname" if ($debug >= 2);
121 $username = $1 if ($file =~ m/"username"\s+value="(.*)"/); print "\n username = $username" if ($debug >= 2);
122 $dest = $1 if ($file =~ m/"dest"\s+value="(.*)"/); print "\n dest = $dest" if ($debug >= 2);
123 $mID = $1 if ($file =~ m/"mID"\s+value="(.*)"/); print "\n mID = $mID" if ($debug >= 2);
124 $mAT = $1 if ($file =~ m/"mAT"\s+value="(.*)"/); print "\n mAT = $mAT" if ($debug >= 2);
125 $QE3 = $1 if ($file =~ m/"QE3"\s+value="(.*)"/); print "\n QE3 = $QE3 \n" if ($debug >= 2);
126  
127 print "\n STEP[$step] finished succesfully :-)\n" if $debug;
128 #
129 #STEP 4 - END
130 #
131  
132 #
133 #STEP 5 - third login
134 #
135 $step = 5;
136 $req = POST 'https://zarkov.shop.o2.co.uk/login/bglogin',
137 [
138 'firstname' => $firstname,
139 'username' => $username,
140 'dest' => $dest,
141 'mID' => $mID,
142 'mAT' => $mAT,
143 'QE3' => $QE3
144 ];
145  
146 $file = $ua->request($req)->as_string;
147 print $file if ($debug >= 3);
148 return &hnd_error($step) unless ($file =~ /<META HTTP-EQUIV=\"Refresh\" CONTENT="0; URL=http/s);
149  
150 print "\n STEP[$step] finished succesfully :-)\n" if $debug;
151 #
152 #STEP 5
153 #
154  
155 #
156 #STEP 6 - checking if the login was successful
157 #
158 $step = 6;
159 $req = GET 'http://www.O2.co.uk';
160 $file = $ua->request($req)->as_string;
161 print $file if ($debug >= 3);
162 return &hnd_error($step) unless ($file =~ /Welcome back,/s);
163 print "\n STEP[$step] finished succesfully :-)\n" if $debug;
164 #
165 #STEP 6 - END
166 #
167  
168  
169 #
170 #STEP 7 - checking if we still have free sms on this account available
171 #
172 $step = 7;
173 $req = GET 'http://sendtxt.genie.co.uk/cgi-bin/sms/send_sms.cgi';
174 $file = $ua->request($req)->as_string;
175 print $file if ($debug >= 3 );
176 my $free_sms_left_this_month = $1 if ($file =~ /You have (\d+) messages of your Free quota/);
177 print "\n free_sms_left_this_month = [$free_sms_left_this_month]\n";
178 # @todo ahe - have never tested the next two lines in case that there are no messages left
179 return &hnd+error($step+777) if ( $free_sms_left_this_month == '0');
180 return &hnd_error($step) unless ($file =~ /messages of your Free quota left to send this month./s);
181 print "\n STEP[$step] finished succesfully :-)\n" if $debug;
182 #
183 #STEP 7 - END
184 #
185  
186 #
187 #STEP 8 - sending SMS - finally :-)
188 #
189 $step = 8;
190 $req = POST 'http://sendtxt.genie.co.uk/cgi-bin/sms/send_sms.cgi',
191 [
192 'contact' => '',
193 'RECIPIENT' => '0' . $self->{prefix} . $self->{telnum} ,
194 'SUBJECT' => '',
195 'MESSAGE' => $self->{smstext} ,
196 'action' => 'Send',
197 'check' => '0',
198 'numbers' => '',
199 'noOfPhones' => '' ,
200 ];
201  
202 if($send_sms) {
203 $req->headers->referer('http://www.O2.co.uk');
204 $file = $ua->request($req)->as_string;
205 print $file if ($debug >= 3);
206 }
207  
208 # logout - http://www.o2.co.uk/logout.html
209 $req = GET 'http://www.o2.co.uk/logout.html';
210 my $res = $ua->request($req);
211 print $res->code."\n" if $debug;
212  
213 # @todo ahe - is this the right address/filename/?? for the removal ??
214 $ua->cookie_jar->clear('sendtxt.genie.co.uk');
215  
216 return &hnd_error($step) unless ($file =~ /message%2520has%2520been%2520sent/s);
217 print "\n STEP[$step] finished succesfully :-)\n" if $debug;
218 #
219 #STEP 8 - END
220 #
221  
222 1;
223 }
224  
225 1;
226  
227 =head2 Install
228  
229 perl modules can be found at http://search.cpan.org/
230  
231  
232 =head2 openssl
233  
234  
235 get the latest openssl from http://www.openssl.org
236  
237 linux:/usr/src # tar -zxvf openssl-0.9.6g.tar.gz
238  
239 $ ./config
240 $ make
241 $ make test
242 $ make install
243  
244 =head2 NET::SSL
245  
246 get latest Net::Ssl from http://search.cpan.org/author/CHAMAS/
247 linux:/usr/src # tar -zxvf Crypt-SSLeay-0.45.tar.gz
248 > perl Makefile.PL
249 > make
250 > make test
251 > make install
252  
253 and test it :-)
254  
255 # lwp-request https://www.nodeworks.com
256  
257 use LWP::UserAgent;
258 my $ua = new LWP::UserAgent;
259 my $req = new HTTP::Request('GET', 'https://www.nodeworks.com');
260 my $res = $ua->request($req);
261 print $res->code."\n";
262  
263 res code should be 200 in case of success
264  
265 =head2 WWW::SMS
266  
267 get latest Www::Sms from http://search.cpan.org/author/GIULIENK/
268  
269 update SMS.pm with the new provider if wanted
270 copy o2UK.pm into your /WWW/SMS/ perl library directory, e.g. into:
271 linux:/usr/lib/perl5/site_perl/5.6.1/WWW/SMS>
272  
273 you have to sign up with O2 http://www.o2.co.uk/ (previous genie)
274 which is free at the moment and you can send 100 sms/month free
275 you have to pass the username/password or the file which contains it
276 dunno all the sim cards provided yet: o2 and vodafone work
277  
278 and test it :-)
279  
280 use WWW::SMS;
281 #international prefix, operator prefix, phone number, message text
282 my $sms = WWW::SMS->new('44', '7777', '123456', 'This is a test.','username' => 'your_o2_username', 'password' => 'your_o2_password');
283 for ( $sms->gateways() ) {
284 print "Trying $_...\n";
285 if ( $sms->send($_) ) { # try to send a sms...
286 last; # until it succeds ;)
287 } else {
288 print $WWW::SMS::Error; # unlucky!
289 }
290 }
291  
292 =cut