blib/lib/WebService/Nonoh.pm

filter: Statementstmt 47.4% 141 / 297 Branchbran 0.0% 0 / 84 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 76.2% 45 / 59 Podpod 50.0% 1 / 2 totaltotal 41.3% 187 / 452 CC 52 SCAR 41.9
CC
52
Cov
36%
call
301.1
run
72.0
login
69.9
CRAP 65.9
↑ Summary
1 2 ▶use Moops;
Subroutine __ANON__: called CC=1 SCAR=0.0
2  
3 # nonoh call script (c) Nei<anti.teamidiot.de>
4  
5 =head1 NAME
6  
7 WebService::Nonoh - make nonoh calls from Perl
8  
9 =head1 VERSION
10  
11 version 0.1
12  
13 =head1 SYNOPSIS
14  
15 my $no = WebService::Nonoh->new(
16 service => 'nonoh.net',
17 user => $username,
18 pass => $password,
19 printer => sub { print +shift, ': ', @_, "\n" }
20 );
21 $no->login;
22 $no->bala;
23 $no->call('+1234567', '+7654321');
24 $no->login;
25 $no->sms($username, '+7654321', "Hello Mr.");
26 $no->logout;
27  
28 =head1 DESCRIPTION
29  
30 You can use L<Nonoh|http://www.nonoh.net/> or a compatible web
31 service, many of L<betamax|http://backsla.sh/betamax> might work. This
32 module allows you to send SMS or connect calls from Perl. Please also
33 see L<nonoh> for the docs of command line app.
34  
35 Warning: The websites do have a captcha if you're unlucky. Then this
36 script also won't work...
37  
38 =cut
39 2  
40 2 ▶class WebService::Nonoh 0.1 {
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=2.3
41 2 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 2 ▶ use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 2 ▶ use LWP::ConnCache;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 2 ▶ use WWW::Mechanize;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 2 ▶ use HTML::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 2 ▶ use CGI;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 2 ▶ use Text::Balanced qw(extract_bracketed extract_quotelike extract_codeblock);
Subroutine BEGIN: called CC=1 SCAR=0.0
48 2 ▶ use JSON::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 2 ▶ use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 2 ▶ use Hash::Util qw(lock_ref_keys_plus lock_keys);
Subroutine BEGIN: called CC=1 SCAR=0.0
51 2 ▶ use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 2 ▶ use Crypt::CBC;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 2 ▶ use Crypt::Rijndael;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 2 ▶ use constant INFO => 'INFO';
Subroutine BEGIN: called CC=1 SCAR=0.0
55 2 ▶ use constant STATUS => 'STATUS';
Subroutine BEGIN: called CC=1 SCAR=0.0
56  
57 0 ▶ sub HI {
Subroutine HI: not called CC=2 SCAR=17.9
Pod undocumented
58 0 my $status = shift;
59 0 ▶ print @_, ($status eq INFO ? ' ...' : ''), "\n"
Branch: 1 branch
BranchTrueFalseTotal
$status eq 'INFO' ? : 0 0 0
60 }
61  
62 =head1 METHODS
63  
64 =head2 new
65  
66 create new webservice object. the following settings must be specified:
67  
68 =over
69  
70 =item service
71  
72 website address of service to use, e.g. C<nonoh.net>
73  
74 =item user
75  
76 username for log in
77  
78 =item pass
79  
80 password for your account
81  
82 =item printer
83  
84 connect a function here that is called to print any status messages or
85 progress information. default is to use Perl's C<print> function
86  
87 =back
88  
89 =cut
90  
91 2 has service => (is => 'ro');
92 2 has user => (is => 'ro');
93 2 has pass => (is => 'ro');
94 0 ▶ has printer => (is => 'ro', default => sub{\&HI});
Subroutine __ANON__: not called CC=1 SCAR=1.2
95  
96 =head2 run
97  
98 command line helper function, please see L<nonoh>
99  
100 =cut
101  
102 0 ▶ sub run {
Subroutine run: not called CC=8 SCAR=42.8
Pod documented
103 0 ▶ if (@ARGV && $ARGV[0] eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (@ARGV and $ARGV[0] eq "") 0 0 0
Condition0%0/3
@ARGV and $ARGV[0] eq ""
!ll&&!rl&&r
000
MC/DC0%0/2
@ARGV and $ARGV[0] eq ""
@ARGV $ARGV[0] eq ""
Truth table0%0/3
A and B
A= @ARGVB= $ARGV[0] eq ""
ABresult
0X0
100
111
104 0 splice @ARGV, 0, 1, do "$ENV{HOME}/.nonohrc"
105 }
106 0 my ($service, $user, $pass, $from, $to, $msg) = @ARGV;
107 0 ▶ die "USAGE: $0 SERVICE USER PASS [FROM TO [MESSAGE]]\n" unless @ARGV >= 3;
Branch: 1 branch
BranchTrueFalseTotal
unless @ARGV >= 3 0 0 0
108 0 ▶ die "USAGE: $0 SERVICE USER PASS FROM TO\n" if @ARGV == 4;
Branch: 1 branch
BranchTrueFalseTotal
if @ARGV == 4 0 0 0
109 0 ▶ die "USAGE: $0 SERVICE USER PASS FROM TO MESSAGE\n" if @ARGV >= 7;
Branch: 1 branch
BranchTrueFalseTotal
if @ARGV >= 7 0 0 0
110 0 my $o = __PACKAGE__->new(
111 service => $service,
112 user => $user,
113 pass => $pass
114 );
115 0 $o->login;
116 0 ▶ if ($msg) {
Branch: 2 branches
BranchTrueFalseTotal
if ($msg) { } 0 0 0
elsif ($from) { } 0 0 0
117 0 $o->sms($from, $to, $msg);
118 }
119 elsif ($from) {
120 0 $o->call($from, $to);
121 }
122 else {
123 0 $o->bala(1);
124 }
125 0 $o->logout;
126 }
127  
128 0 ▶ method base { 'https://www.' . $self->service }
Subroutine BEGIN: called CC=1 SCAR=1.6
Subroutine base: not called CC=2 SCAR=9.9
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
129  
130 0 ▶ method rebuild {
Subroutine BEGIN: called CC=1 SCAR=1.2
Subroutine rebuild: not called CC=2 SCAR=12.7
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
131 0 $self->{mech} = WWW::Mechanize->new(autocheck => 1, stack_depth => 0);
132 0 $self->{mech}->conn_cache(LWP::ConnCache->new());
133 0 $self->{script} = [];
134 $self->{hp} = HTML::Parser->new(
135 start_h => [
136 0 ▶ sub { shift->handler(text => $self->{script}, '@{dtext}') }, 'self' ],
Subroutine __ANON__: not called CC=1 SCAR=6.9
137 end_h => [
138 0 ▶ sub { shift->handler(text => undef) }, 'self' ]);
Subroutine __ANON__: not called CC=1 SCAR=6.9
139 0 $self->{hp}->report_tags('script');
140 }
141  
142 0 ▶ method _script_parse($content) {
Subroutine BEGIN: called CC=1 SCAR=3.1
Subroutine _script_parse: not called CC=4 SCAR=24.1
Branch: 3 branches
BranchTrueFalseTotal
@_ ? : 0 0 0
if @_ != 1 0 0 0
$#_ >= 0 ? : 0 0 0
143 0 @{ $self->{script} } = ();
144 0 $self->{hp}->parse($content);
145 }
146  
147 0 ▶ method BUILD {
Subroutine BEGIN: called CC=1 SCAR=1.2
Subroutine BUILD: not called CC=2 SCAR=11.1
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
148 0 $self->rebuild;
149 0 $self->{lx} = XML::LibXML->new();
150 0 $self->{lx}->recover(2);
151 }
152  
153 =head2 login
154  
155 issue log-in request to website
156  
157 =cut
158  
159 0 ▶ method login {
Subroutine BEGIN: called CC=1 SCAR=1.2
Subroutine login: not called CC=9 SCAR=42.5
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
160 0 $self->{mech}->get($self->base . '/login/');
161 0 $self->printer->( INFO, 'checking with ', $self->service );
162 0 $self->_script_parse($self->{mech}->content);
163 0 my ($key, $pass);
164 0 for (@{$self->{script}}) {
165 0 ▶ if (/getDecValue\((["'])([^"']+)\1,\s*(["'])([^"']+)\3\)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/getDecValue\((["'])([^"']+)\1,\s*(["'])([^"']+)\3\)/u) 0 0 0
166 0 ($key, $pass) = ($2, $4);
167 0 last;
168 }
169 }
170 0 ▶ if ($key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key) { } 0 0 0
171 0 $key = Crypt::CBC
172 ->new(-key => $pass, -cipher => 'Rijndael')
173 ->decrypt(decode_base64($key));
174 0 my $doc = $self->{lx}->parse_html_string($self->{mech}->content, { suppress_errors => 1 });
175 0 my (@hiddens) = $doc->findnodes(qq{//div[\@class="myloginform"][\@id="$key"]//input[\@type="hidden"][\@id=\@name]});
176 0 my %form_hidden = map { $_->findvalue('@name') => $_->findvalue('@value') } @hiddens;
177 0 my $nr = 1;
178 0 FORM: for my $form ($self->{mech}->forms) {
179 0 for my $name (keys %form_hidden) {
180 0 ▶ unless (defined $form->value($name) &&
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $form->value($name) and $form->value($name) eq $form_hidden{$name}) 0 0 0
Condition0%0/3
defined $form->value($name) and $form->value($name) eq $form_hidden{$name}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $form->value($name) and $form->value($name) eq $form_hidden{$name}
defined $form->value($name) $form->value($name) eq $form_hidden{$name}
Truth table0%0/3
A and B
A= defined $form->value($name)B= $form->value($name) eq $form_hidden{$name}
ABresult
0X0
100
111
181 $form->value($name) eq $form_hidden{$name}) {
182 0 next FORM;
183 }
184 }
185 0 last;
186 }
187 continue {
188 0 ++$nr;
189 }
190 0 $self->printer->( INFO, "choosing form $nr" );
191 $self->{mech}->submit_form(
192 0 form_number => $nr,
193 fields => +{
194 'login[username]' => $self->user,
195 'login[password]' => $self->pass,
196 });
197 }
198 else {
199 0 $self->printer->( INFO, 'no key found, maybe they are using a new encryption!!!' );
200 $self->{mech}->submit_form(
201 0 with_fields => +{
202 'login[username]' => $self->user,
203 'login[password]' => $self->pass,
204 });
205 }
206  
207 0 $self->printer->( INFO, 'log in ', $self->user );
208 }
209  
210 =head2 sms
211  
212 send SMS, following parameters are expected:
213  
214 =over
215  
216 =item I<$from>
217  
218 a "from" address (must be registered in the web interface first)
219  
220 =item I<$to>
221  
222 phone number to send SMS message to
223  
224 =item I<$message>
225  
226 the message content
227  
228 =back
229  
230 =cut
231  
232 #====== SMS =======
233 0 ▶ method sms($from, $to, $msg) {
Subroutine BEGIN: called CC=1 SCAR=4.1
Subroutine sms: not called CC=8 SCAR=38.9
Branch: 5 branches
BranchTrueFalseTotal
@_ ? : 0 0 0
if @_ != 3 0 0 0
$#_ >= 0 ? : 0 0 0
$#_ >= 1 ? : 0 0 0
$#_ >= 2 ? : 0 0 0
234 0 $self->{mech}->get($self->base . '/web_sms/');
235  
236 $self->{mech}->submit_form(
237 0 with_fields => {
238 message => $msg,
239 callerid => $from,
240 phonenumber => $to,
241 });
242  
243 0 $self->printer->( INFO, 'sms ', $from, ' --> ', $to );
244 0 $self->printer->( STATUS, $msg );
245  
246 0 my $doc = $self->{lx}->parse_html_string($self->{mech}->content, { suppress_errors => 1 });
247 0 for ($doc->findnodes('//span[contains(@class, "error")]')) {
248 0 $self->printer->( STATUS, '>>', $_->textContent )
249 }
250 0 for ($doc->findnodes('//div[contains(@class, "notification")]/div/text()[normalize-space(.)!=""]')) {
251 0 $self->printer->( STATUS, $_->findvalue('normalize-space(.)') );
252 }
253 }
254 #==================
255  
256 =head2 call
257  
258 establish a call between two phone numbers, following parameters are expected:
259  
260 =over
261  
262 =item I<$from>
263  
264 the number that should be called first, in international format
265  
266 =item I<$to>
267  
268 the number that should be called after the first one has been picked
269 up.
270  
271 =back
272  
273 =cut
274  
275 #====== CALL ======
276 0 ▶ method call($from, $to) {
Subroutine BEGIN: called CC=1 SCAR=3.7
Subroutine call: not called CC=18 SCAR=57.1
Branch: 4 branches
BranchTrueFalseTotal
@_ ? : 0 0 0
if @_ != 2 0 0 0
$#_ >= 0 ? : 0 0 0
$#_ >= 1 ? : 0 0 0
277 0 ▶ $self->{mech}->get($self->base . '/phone_to_phone/') unless $self->{mech}->uri =~ '/phone_to_phone';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"mech"}->uri =~ m[/phone_to_phone]u 0 0 0
278  
279 0 my $cl = $self->{mech}->form_with_fields('country_from', 'country_to');
280 0 my $from_cl = join '|', $cl->find_input('country_from')->possible_values;
281 0 my $to_cl = join '|', $cl->find_input('country_to')->possible_values;
282 0 my ($p_from, $n_from) = $from =~ /^(?:\+|00)($from_cl)(\d+)$/;
283 0 my ($p_to, $n_to) = $to =~ /^(?:\+|00)($to_cl)(\d+)$/;
284  
285 $self->{mech}->submit_form(
286 0 with_fields => {
287 phonenumber_from => $n_from,
288 phonenumber_to => $n_to,
289 prefix_from => "+$p_from",
290 prefix_to => "+$p_to",
291 country_from => $p_from,
292 country_to => $p_to,
293 });
294  
295 0 $self->printer->( INFO, 'calling ', "+$p_from $n_from", ' --> ', "+$p_to $n_to" );
296  
297 0 $self->_script_parse($self->{mech}->content);
298  
299 0 my (@errors, @conn, @fin);
300 0 for (@{$self->{script}}) {
301 0 ▶ my $ref = /WebCall/ ? \@conn : [];
Branch: 1 branch
BranchTrueFalseTotal
/WebCall/u ? : 0 0 0
302 0 ▶ while (/errormessages\.push/g) {
Branch: 1 branch
BranchTrueFalseTotal
if (/errormessages\.push/gu) 0 0 0
303 0 my $arg = (extract_bracketed($_))[0];
304 0 $arg = substr $arg, 1, -1;
305 0 $arg = (extract_quotelike($arg))[5];
306 0 push @errors, $arg;
307 }
308 0 ▶ while (/\$\.ajax/g) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\$\.ajax/gu) 0 0 0
309 0 my %st;
310 0 my $arg = (extract_bracketed($_))[0];
311 0 $arg = substr $arg, 1, -1;
312 0 $arg = (extract_codeblock($arg))[0];
313 0 $arg = substr $arg, 1, -1;
314 0 ▶ while ($arg =~ /(\w+)\s*:/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg =~ /(\w+)\s*:/gu) 0 0 0
315 0 my ($key) = $1;
316 0 ▶ if (my $val = (extract_quotelike($arg))[5]) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $val = (&extract_quotelike($arg))[5]) { } 0 0 0
317 0 $st{$key} = $val;
318 } else {
319 0 scalar $arg =~ /\G\s*\w+/gc;
320 0 (extract_bracketed($arg));
321 0 (extract_codeblock($arg));
322 }
323 }
324 0 push @$ref, lock_keys %st;
325 0 ▶ if (/function createFinalConnection/gc) {
Branch: 2 branches
BranchTrueFalseTotal
if (/function createFinalConnection/cgu) { } 0 0 0
elsif (/function createNewConnection/cgu) { } 0 0 0
326 0 $ref = \@fin;
327 } elsif (/function createNewConnection/gc) {
328 0 $ref = \@conn;
329 }
330 }
331 }
332  
333 0 my $orig_flush = $|;
334 0 $|=1;
335  
336 0 for (@conn, @fin) {
337 0 sleep 1;
338 0 my $q = new CGI ($_->{data});
339 $self->{mech}->post($_->{url}, [
340 0 map { my $n = $_; map { $n => $_ } $q->param($n) } $q->param ],
341 'X-Requested-With' => 'XMLHttpRequest');
342 0 my $d = decode_json $self->{mech}->content;
343 0 lock_ref_keys_plus($d, qw(code endcause calculated_balance));
344 $self->printer->( STATUS, '',
345 $d->{code}, '/', $d->{callstate}, "\t", join "\t", #(map {
346 # my $s = $_;
347 # "$s: " . join '/', @{$d}{
348 # map {$s.'Side'.$_} qw(Tariff SetupCharge TariffIntervall)}
349 # } qw(A B)),
350 0 (map { "$_/".$d->{$_}." " } sort { $a cmp $b } keys %$d),
351 0 ▶ ) if defined $d->{code};
Branch: 1 branch
BranchTrueFalseTotal
if defined $d->{"code"} 0 0 0
352 0 ▶ $self->printer->( STATUS, '>>', $errors[$d->{endcause}] ) if $d->{endcause};
Branch: 1 branch
BranchTrueFalseTotal
if $d->{"endcause"} 0 0 0
353 $self->printer->( STATUS, 'balance: ', $d->{calculated_balance} )
354 0 ▶ if defined $d->{calculated_balance};
Branch: 1 branch
BranchTrueFalseTotal
if defined $d->{"calculated_balance"} 0 0 0
355 }
356 0 $|=$orig_flush;
357  
358 }
359 #==================
360  
361 =head2 bala
362  
363 this method enquires your balance and "free days" (free minutes to
364 selected destinations received after charging the account). if the
365 parameter I<$to_printer> is true, it will send output directly to the
366 printer, otherwise a pair of ($balance_string, $freedays_string) is
367 returned.
368  
369 if log-in failed, two empty strings are returned.
370  
371 =cut
372  
373 #====== BALA ======
374 0 ▶ method bala($to_printer) {
Subroutine BEGIN: called CC=1 SCAR=3.1
Subroutine bala: not called CC=5 SCAR=29.5
Branch: 3 branches
BranchTrueFalseTotal
@_ ? : 0 0 0
if @_ != 1 0 0 0
$#_ >= 0 ? : 0 0 0
375 0 my $doc = $self->{lx}->parse_html_string($self->{mech}->content, { suppress_errors => 1 });
376 0 my $res1 = $doc->findvalue('//span[@class="balance"] | //span[@class="low-balance"]');
377 0 my $res2 = $doc->findvalue('//span[@class="freedays"]');
378 0 ▶ if ($to_printer) {
Branch: 1 branch
BranchTrueFalseTotal
if ($to_printer) { } 0 0 0
379 0 $self->printer->( STATUS, $res1 );
380 0 $self->printer->( STATUS, $res2 );
381 }
382 else {
383 0 ($res1, $res2)
384 }
385 }
386  
387 =head2 logout
388  
389 issue log-out request to the website
390  
391 =cut
392  
393 0 ▶ method logout {
Subroutine BEGIN: called CC=1 SCAR=1.2
Subroutine logout: not called CC=2 SCAR=10.5
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
394 0 $self->{mech}->get($self->base . '/logout');
395 0 $self->printer->( INFO, 'log out' );
396 }
397 }
398  
399 =head1 AUTHOR
400  
401 Ailin Nemui E<lt>ailin at devio dot usE<gt>
402  
403 =head1 COPYRIGHT AND LICENSE
404  
405 This software is copyright (c) 2013 by Ailin Nemui.
406  
407 This is free software; you can redistribute it and/or modify it under
408 the same terms as the Perl 5 programming language system itself.
409  
410 =cut
411  
412 2 1
↑ Summary