blib/lib/Net/SMS/MyTMN.pm

filter: Statementstmt 23.9% 17 / 71 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 30 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 42.8% 3 / 7 Podpod 100% 2 / 2 totaltotal 15.0% 22 / 146 CC 24 SCAR 43.4
CC
24
Cov
14%
_valid
132.0
sms_mytmn
72.0
check_mytmn
42.0
CRAP 76.5
↑ Summary
1 package Net::SMS::MyTMN;
2  
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 require WWW::Mechanize;
6 2 require Exporter;
7  
8 =head1 NAME
9  
10 Net::SMS::MyTMN - Send SMS trough MyTMN!
11  
12 =head1 VERSION
13  
14 Version 0.08
15  
16 =cut
17  
18 2 ▶use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 2 $VERSION = '0.08';
21 2 @ISA = qw(Exporter);
22 2 @EXPORT = qw();
23 2 %EXPORT_TAGS = (
24 DEFAULT => [qw//],
25 VALID => [qw/sms_mytmn check_sms/],
26 );
27 2 @EXPORT_OK = qw/sms_mytmn check_sms/;
28  
29 =head1 SYNOPSIS
30  
31 Quick and dirty way to send SMS trough portuguese mobile operator.
32 Note: you need to have an account on www.mytmn.pt
33  
34 #!/usr/bin/perl
35 use strict;
36 use warnings;
37  
38 use Net::SMS::MyTMN;
39  
40 print Net::SMS::MyTMN::sms_mytmn({
41 'username' => '960000000',
42 'password' => 'password',
43 'targets' => [960000000,910000000],
44 'message' => 'message goes here!',
45 });
46  
47 print Net::SMS::MyTMN::check_mytmn({
48 'username' => '960000000',
49 'password' => 'password',
50 });
51  
52  
53 =head1 EXPORT
54  
55 sms_mytmn() to send messages and check_sms() to check how many free sms are left
56 for the current month.
57  
58 =head1 FUNCTIONS
59  
60 =head2 sms_mytmn()
61  
62 The following 4 parameters are *REQUIRED*:
63  
64 username : probably your phone number
65 password : the password you use to log onto www.mytmn.pt
66 targets : up to five phone numbers
67 message : a string with the message you're about to send
68  
69 Return a string: "Message sent"
70  
71 =head2 check_mytmn()
72  
73 The following 2 parameters are *REQUIRED*:
74  
75 username : probably your phone number
76 password : the password you use to log onto www.mytmn.pt
77  
78 Returns the number of messages left.
79  
80 =cut
81  
82 0 ▶sub sms_mytmn {
Subroutine sms_mytmn: not called CC=8 SCAR=42.8
Pod documented
83  
84 0 my $self = shift;
85  
86 0 ▶ my $username = ( $self->{'username'} || undef );
Condition0%0/2
$self->{'username'} || undef
l!l
00
MC/DC0%0/1
$self->{'username'} || undef
$self->{'username'}
Truth table0%0/2
A
A= $self->{'username'}
Aresult
11
00
87 0 ▶ my $password = ( $self->{'password'} || undef );
Condition0%0/2
$self->{'password'} || undef
l!l
00
MC/DC0%0/1
$self->{'password'} || undef
$self->{'password'}
Truth table0%0/2
A
A= $self->{'password'}
Aresult
11
00
88 0 ▶ my $targets = ( $self->{'targets'} || undef );
Condition0%0/2
$self->{'targets'} || undef
l!l
00
MC/DC0%0/1
$self->{'targets'} || undef
$self->{'targets'}
Truth table0%0/2
A
A= $self->{'targets'}
Aresult
11
00
89 0 ▶ my $message = ( $self->{'message'} || undef );
Condition0%0/2
$self->{'message'} || undef
l!l
00
MC/DC0%0/1
$self->{'message'} || undef
$self->{'message'}
Truth table0%0/2
A
A= $self->{'message'}
Aresult
11
00
90  
91 0 my $valid = _valid(
92 {
93 'username' => $username,
94 'password' => $password,
95 'targets' => $targets,
96 'message' => $message,
97 }
98 );
99  
100 0 ▶ return $valid unless !$valid;
Branch: 1 branch
BranchTrueFalseTotal
if $valid 0 0 0
101  
102 0 my $target_fields = {
103 'message' => $message,
104 'event' => 'confirmSend',
105 };
106  
107 0 my $i = 1;
108 0 foreach ( @{$targets} ) {
109 0 $target_fields->{ 'phoneNumber' . $i } = $_;
110 0 $i++;
111 }
112  
113 0 my $mech = WWW::Mechanize->new();
114  
115 0 $mech->get('http://www.tmn.pt:80/portal/site/tmn');
116  
117 0 $mech->submit_form(
118 form_number => 1,
119 fields => {
120 'usr' => $username,
121 'pwd' => $password,
122 },
123 );
124  
125 0 sleep 1;
126  
127 0 my $r = $mech->get('http://my.tmn.pt/web/easysms/EasySmsConfirmSend.po');
128  
129 0 my ($idsessao) = $r->content() =~ /\.*tmnsessionid\%3D(\w{32})\.*/gmx;
130  
131 0 ▶ die "Cannot read sessionid!\n" unless $idsessao;
Branch: 1 branch
BranchTrueFalseTotal
unless $idsessao 0 0 0
132  
133 0 $mech->get(
134 qq|http://my.tmn.pt/web/easysms/EasySms.po?silentauthdone=1&tmnsessionid=$idsessao|
135 );
136  
137 0 my $rr = $mech->submit_form(
138 form_name => 'easySmsForm',
139 fields => $target_fields,
140 );
141  
142 0 $mech->submit_form( form_name => 'headerForm' );
143  
144 0 undef $mech;
145  
146 0 return qq|Message sent\n|;
147  
148 }
149  
150 0 ▶sub check_mytmn {
Subroutine check_mytmn: not called CC=6 SCAR=37.4
Pod documented
151  
152 0 my $self = shift;
153  
154 0 ▶ my $username = ( $self->{'username'} || undef );
Condition0%0/2
$self->{'username'} || undef
l!l
00
MC/DC0%0/1
$self->{'username'} || undef
$self->{'username'}
Truth table0%0/2
A
A= $self->{'username'}
Aresult
11
00
155 0 ▶ my $password = ( $self->{'password'} || undef );
Condition0%0/2
$self->{'password'} || undef
l!l
00
MC/DC0%0/1
$self->{'password'} || undef
$self->{'password'}
Truth table0%0/2
A
A= $self->{'password'}
Aresult
11
00
156  
157 0 my $valid = _valid(
158 {
159 'username' => $username,
160 'password' => $password,
161 'targets' => ['960000000'],
162 'message' => '-',
163 }
164 );
165  
166 0 ▶ return $valid unless !$valid;
Branch: 1 branch
BranchTrueFalseTotal
if $valid 0 0 0
167  
168 0 my $mech = WWW::Mechanize->new();
169  
170 0 $mech->get('http://www.tmn.pt:80/portal/site/tmn');
171  
172 0 $mech->submit_form(
173 form_number => 1,
174 fields => {
175 'usr' => $username,
176 'pwd' => $password,
177 },
178 );
179  
180 0 sleep 1;
181  
182 0 my $r = $mech->get('http://my.tmn.pt/web/easysms/EasySmsConfirmSend.po');
183  
184 0 my ($idsessao) = $r->content() =~ /\.*tmnsessionid\%3D(\w{32})\.*/gmx;
185  
186 0 ▶ die "Cannot read sessionid!\n" unless $idsessao;
Branch: 1 branch
BranchTrueFalseTotal
unless $idsessao 0 0 0
187  
188 0 $r = $mech->get(
189 qq|http://my.tmn.pt/web/easysms/EasySms.po?silentauthdone=1&tmnsessionid=$idsessao|
190 );
191  
192 0 my ($free) = $r->content() =~ /.*messagesCanBeSent">(\d+)<\//igmx;
193 0 ▶ $free = q|0| unless $free;
Branch: 1 branch
BranchTrueFalseTotal
unless $free 0 0 0
194  
195 0 undef $mech;
196  
197 0 return $free;
198  
199 }
200  
201 0 ▶sub _valid {
Subroutine _valid: not called CC=11 SCAR=48.8
202  
203 0 my $self = shift;
204  
205 return _errors(1)
206 unless $self->{'username'}
207 0 ▶ && $self->{'username'} =~ /^96\d{7}$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"username"} and $self->{"username"} =~ /^96\d{7}$/ 0 0 0
Condition0%0/3
$self->{"username"} and $self->{"username"} =~ /^96\d{7}$/
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"username"} and $self->{"username"} =~ /^96\d{7}$/
$self->{"username"} $self->{"username"} =~ /^96\d{7}$/
Truth table0%0/3
A and B
A= $self->{"username"}B= $self->{"username"} =~ /^96\d{7}$/
ABresult
0X0
100
111
208  
209 return _errors(2)
210 unless $self->{'password'}
211 0 ▶ && $self->{'password'} =~
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"password"} and $self->{"password"} =~ /^\w+|\W+$/ 0 0 0
Condition0%0/3
$self->{"password"} and $self->{"password"} =~ /^\w+|\W+$/
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"password"} and $self->{"password"} =~ /^\w+|\W+$/
$self->{"password"} $self->{"password"} =~ /^\w+|\W+$/
Truth table0%0/3
A and B
A= $self->{"password"}B= $self->{"password"} =~ /^\w+|\W+$/
ABresult
0X0
100
111
212 /^\w+|\W+$/; # need to check wich characters are allowed
213  
214 return _errors(3)
215 unless $self->{'targets'}
216 && ref( $self->{'targets'} ) eq 'ARRAY'
217 0 && scalar @{ $self->{'targets'} } >= 1
218 0 ▶ && scalar @{ $self->{'targets'} } <= 5;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"targets"} and ref $self->{"targets"} eq "ARRAY" and scalar @{$self->{"targets"};} >= 1 and scalar @{$self->{"targets"};} <= 5 0 0 0
Condition0%0/9
$self->{"targets"} and ref $self->{"targets"} eq "ARRAY" and scalar @{$self->{"targets"};} >= 1 and scalar @{$self->{"targets"};} <= 5
!ll&&!rl&&r
000
$self->{"targets"} and ref $self->{"targets"} eq "ARRAY" and scalar @{$self->{"targets"};} >= 1
!ll&&!rl&&r
000
$self->{"targets"} and ref $self->{"targets"} eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/4
$self->{"targets"} and ref $self->{"targets"} eq "ARRAY" and scalar @{$self->{"targets"};} >= 1 and scalar @{$self->{"targets"};} <= 5
$self->{"targets"} ref $self->{"targets"} eq "ARRAY" scalar @{$self->{"targets"};} >= 1 scalar @{$self->{"targets"};} <= 5
Truth table0%0/5
A and B and C and D
A= $self->{"targets"}B= ref $self->{"targets"} eq "ARRAY"C= scalar @{$self->{"targets"};} >= 1D= scalar @{$self->{"targets"};} <= 5
ABCDresult
0XXX0
10XX0
110X0
11100
11111
219  
220 return _errors(4)
221 unless $self->{'message'}
222 0 ▶ && length $self->{'message'} <= 140;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"message"} and length $self->{"message"} <= 140 0 0 0
Condition0%0/3
$self->{"message"} and length $self->{"message"} <= 140
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"message"} and length $self->{"message"} <= 140
$self->{"message"} length $self->{"message"} <= 140
Truth table0%0/3
A and B
A= $self->{"message"}B= length $self->{"message"} <= 140
ABresult
0X0
100
111
223  
224 0 return undef;
225 }
226  
227 0 ▶sub _errors {
Subroutine _errors: not called CC=2 SCAR=17.9
228  
229 0 my $self = shift;
230 0 ▶ return if !$self;
Branch: 1 branch
BranchTrueFalseTotal
unless $self 0 0 0
231  
232 0 my $errors = {
233 1 => qq|Missing or invalid username\n|,
234 2 => qq|Missing or invalid password\n|,
235 3 => qq|Missing or invalid targets\n|,
236 4 => qq|Missing or invalid message\n|,
237 };
238  
239 0 return $errors->{$self};
240 }
241  
242 =head1 AUTHOR
243  
244 Miguel Santinho, C<< <msantinho at simplicidade.com> >>
245  
246 =head1 BUGS
247  
248 Please report any bugs or feature requests to
249 C<bug-net-sms-mytmn at rt.cpan.org>, or through the web interface at
250 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-SMS-MyTMN>.
251 I will be notified, and then you'll automatically be notified of progress on
252 your bug as I make changes.
253  
254 =head1 SUPPORT
255  
256 Note that this module requires www::mechanize to automate the proccess of logging in
257 and sending SMSs trough the www.mytmn.pt. If the operator makes any changes to
258 the forms they use, this module will stop working.
259 If that happens, please, report that to the author's e-mail.
260  
261  
262 =head1 COPYRIGHT & LICENSE
263  
264 Copyright 2007 Miguel Santinho, all rights reserved.
265  
266 This program is free software; you can redistribute it and/or modify it
267 under the same terms as Perl itself.
268  
269 =cut
270  
271 2 1; # End of Net::SMS::MyTMN
↑ Summary