blib/lib/SMS/Send/DE/MeinBMW.pm

filter: Statementstmt 34.5% 39 / 113 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 60.0% 12 / 20 Podpod 100% 2 / 2 totaltotal 28.0% 53 / 189 CC 26 SCAR 31.5
CC
26
Cov
24%
_get_login
56.0
send_sms
43.5
_PASSWORD
30.0
CRAP 23.4
↑ Summary
1 package SMS::Send::DE::MeinBMW;
2  
3 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 $VERSION = '0.06';
5 }
6  
7 2 ▶use base 'SMS::Send::Driver';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use HTTP::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use HTTP::Cookies;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use HTML::Form;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 my $RE_BADLOGIN = qr/Sie konnten nicht authentifiziert werden/;
18 2 my $root_page = 'https://www.meinbmw.de';
19 2 my $login_page = 'https://www.meinbmw.de/Home/tabid/36/ctl/Login/Default.aspx';
20 2 my $sms_page =
21 'https://www.meinbmw.de/DownloadsServices/Services/SMSService/tabid/80/Default.aspx';
22  
23 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
24 0 my $class = shift;
25 0 my %params = @_;
26  
27 # Get the login
28 0 my $login = $class->_LOGIN( delete $params{_login} );
29 0 my $password = $class->_PASSWORD( delete $params{_password} );
30  
31 0 my $ua = LWP::UserAgent->new;
32  
33 # follow posts
34 0 push @{ $ua->requests_redirectable }, 'POST';
35  
36 # lie about the agent
37 0 $ua->agent('Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)');
38 0 $ua->cookie_jar( HTTP::Cookies->new );
39  
40 # Create the object, saving any private params for later
41 0 my $self = bless {
42 ua => $ua,
43 login => $login,
44 password => $password,
45 private => \%params,
46  
47 # State variables
48 logged_in => '',
49 }, $class;
50  
51 0 $self;
52 }
53  
54 0 ▶sub _get_login {
Subroutine _get_login: not called CC=7 SCAR=40.3
55 0 my $self = shift;
56  
57 0 my $ua = $self->{ua};
58  
59 #get session code and cookies!
60 0 my $res = $ua->request( POST $login_page);
61  
62 0 ▶ $res->is_success || Carp::croak("HTTP Error: $login_page\n$res->status_line");
Branch: 1 branch
BranchTrueFalseTotal
$res->is_success or Carp::croak("HTTP Error: $login_page\n$res->status_line") 0 0 0
63  
64 0 my $html = $res->content;
65 0 my $f = HTML::Form->parse( $html, $login_page );
66  
67 0 for ( $f->param ) {
68 0 ▶ $f->find_input($_)->value( $self->{login} ) if /username$/i;
Branch: 1 branch
BranchTrueFalseTotal
if /username$/i 0 0 0
69 0 ▶ $f->find_input($_)->value( $self->{password} ) if /password$/i;
Branch: 1 branch
BranchTrueFalseTotal
if /password$/i 0 0 0
70 }
71  
72 0 $res = $ua->request( $f->click );
73  
74 0 ▶ if ( $res->is_success && $res->content =~ /logout/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success and $res->content =~ /logout/i) { } 0 0 0
Condition0%0/3
$res->is_success and $res->content =~ /logout/i
!ll&&!rl&&r
000
MC/DC0%0/2
$res->is_success and $res->content =~ /logout/i
$res->is_success $res->content =~ /logout/i
Truth table0%0/3
A and B
A= $res->is_successB= $res->content =~ /logout/i
ABresult
0X0
100
111
75 0 return 1;
76 }
77 0 else { Carp::croak( "Couldn't log in: ", $res->status_line ); }
78  
79 0 return 0;
80 }
81  
82 0 ▶sub _send_login {
Subroutine _send_login: not called CC=4 SCAR=30.0
83 0 my $self = shift;
84  
85 # Shortcut if logged in
86 0 ▶ return 1 if $self->{logged_in};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"logged_in"} 0 0 0
87  
88 # Get to the login page
89 0 $self->_get_login;
90  
91 # Submit the login form
92 0 my $res = $self->{ua}->request( GET $login_page );
93  
94 0 ▶ $res->is_success || Carp::croak("HTTP Error: $login_page");
Branch: 1 branch
BranchTrueFalseTotal
$res->is_success or Carp::croak("HTTP Error: $login_page") 0 0 0
95  
96 0 ▶ if ( $res->content =~ $RE_BADLOGIN ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->content =~ /$RE_BADLOGIN/) 0 0 0
97 0 Carp::croak('Invalid login and/or password');
98 }
99  
100 0 $self->{logged_in} = 1;
101 0 return 1;
102 }
103  
104 ##
105 # send_sms
106  
107 0 ▶sub send_sms {
Subroutine send_sms: not called CC=7 SCAR=37.7
Pod documented
108 0 my $self = shift;
109 0 my %params = @_;
110  
111 # Get the message and destination
112 0 my $message = $self->_MESSAGE( delete $params{text} );
113 0 my $recipient = $self->_TO( delete $params{to} );
114  
115 # Make sure we are logged in
116 0 $self->_send_login;
117  
118 0 ▶ my $free_chars = do { use bytes; 160 - length($message) };
Subroutine BEGIN: called CC=1 SCAR=0.6
119  
120 0 my $res = $self->{ua}->request( GET $sms_page);
121  
122 0 ▶ $res->is_success || Carp::croak("HTTP Error: $sms_page\n$res->status_line");
Branch: 1 branch
BranchTrueFalseTotal
$res->is_success or Carp::croak("HTTP Error: $sms_page\n$res->status_line") 0 0 0
123  
124 0 my $html = $res->content;
125  
126 0 my $f = HTML::Form->parse( $html, $sms_page );
127  
128 0 for ( $f->param ) {
129 0 ▶ $f->find_input($_)->value($recipient) if /phone$/i;
Branch: 1 branch
BranchTrueFalseTotal
if /phone$/i 0 0 0
130 0 ▶ $f->find_input($_)->value($message) if /subject$/i;
Branch: 1 branch
BranchTrueFalseTotal
if /subject$/i 0 0 0
131 }
132  
133 0 $res = $self->{ua}->request( $f->click );
134
135 0 ▶ unless ( $res->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->is_success) 0 0 0
136 0 Carp::croak("HTTP request returned failure when sending SMS request");
137 }
138  
139 # Check if the SMS limit isn't reached
140 0 ▶ return 0 unless $res->{_content} =~ /Ihre SMS wurde an .+ versendet/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->{"_content"} =~ /Ihre SMS wurde an .+ versendet/i 0 0 0
141  
142 # Fire-and-forget, we don't know for sure.
143 0 return 1;
144 }
145  
146 ###############################################
147 # Internal
148  
149 0 ▶sub _LOGIN {
Subroutine _LOGIN: not called CC=4 SCAR=30.0
150 0 ▶ my $class = ref $_[0] ? ref shift : shift;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
151 0 my $email = shift;
152 0 ▶ unless ( defined $email ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $email) 0 0 0
153 0 Carp::croak("Did not provide a login emailaddress");
154 }
155 0 ▶ unless ( $email =~ /^.+\@\w+\.\w+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($email =~ /^.+\@\w+\.\w+$/) 0 0 0
156 0 Carp::croak("Login does nnot look like a emailaddress");
157 }
158 0 return $email;
159 }
160  
161 0 ▶sub _PASSWORD {
Subroutine _PASSWORD: not called CC=5 SCAR=34.0
162 0 ▶ my $class = ref $_[0] ? ref shift : shift;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
163 0 my $password = shift;
164 0 ▶ unless ( defined $password and !ref $password and length $password ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $password and not ref $password and length $password) 0 0 0
Condition0%0/6
defined $password and not ref $password and length $password
!ll&&!rl&&r
000
defined $password and not ref $password
!ll&&!rl&&r
000
MC/DC0%0/3
defined $password and not ref $password and length $password
defined $password not ref $password length $password
Truth table0%0/4
A and not(B) and C
A= defined $passwordB= not ref $passwordC= length $password
ABCresult
0XX0
10X0
1100
1111
165 0 Carp::croak("Did not provide a password");
166 }
167 0 return $password;
168 }
169  
170 0 ▶sub _MESSAGE {
Subroutine _MESSAGE: not called CC=3 SCAR=19.2
171 2 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
172 0 ▶ my $class = ref $_[0] ? ref shift : shift;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
173 0 my $message = shift;
174 0 ▶ unless ( length($message) <= 160 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $message <= 160) 0 0 0
175 0 Carp::croak("Message length limit is 160 characters");
176 }
177 0 return $message;
178 }
179  
180 0 ▶sub _TO {
Subroutine _TO: not called CC=2 SCAR=17.9
181 0 ▶ my $class = ref $_[0] ? ref shift : shift;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
182 0 my $to = shift;
183  
184 # International numbers need their + removed
185 0 $to =~ y/0123456789//cd;
186  
187 0 return $to;
188 }
189  
190 2 1;
191 __END__
↑ Summary