blib/lib/Mail/Webmail/Gmail.pm

filter: Statementstmt 1.2% 6 / 502 Branchbran 0.0% 0 / 220 Conditioncond 0.0% 0 / 40 MC/DCmcdc 0.0% 0 / 24 Subroutinesub 10.5% 2 / 19 Podpod n/a totaltotal 0.9% 8 / 805 CC 136 SCAR 51.2
CC
136
Cov
1%
get_page
420.0
send_message
210.0
new
182.0
CRAP 166.8
↑ Summary
1 package Mail::Webmail::Gmail;
2
3 1 ▶use lib qw(lib);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5
6 require LWP::UserAgent;
7 require HTTP::Headers;
8 require HTTP::Cookies;
9 require HTTP::Request::Common;
10 require Crypt::SSLeay;
11 require Exporter;
12
13 our $VERSION = "1.09";
14
15 our @ISA = qw(Exporter);
16 our @EXPORT_OK = ();
17 our @EXPORT = ();
18
19 our $USER_AGENT = "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.8) Gecko/20050511 Firefox/1.0.4";
20 our $MAIL_URL = "http://mail.google.com/mail";
21 our $SSL_MAIL_URL = "https://mail.google.com/mail";
22 our $LOGIN_URL = "https://www.google.com/accounts/ServiceLoginBoxAuth";
23
24 our %FOLDERS = (
25 'INBOX' => '^I',
26 'STARRED' => '^T',
27 'SPAM' => '^S',
28 'TRASH' => '^K',
29 );
30
31 0 ▶sub new {
Subroutine new: not called CC=13 SCAR=52.0
32 0 my $class = shift;
33 0 my %args = @_;
34
35 0 my $ua = new LWP::UserAgent( agent => $USER_AGENT, keep_alive => 1 );
36 0 ▶ $ua->timeout( $args{timeout} ) if defined $args{timeout};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"timeout"} 0 0 0
37
38 0 push( @LWP::Protocol::http::EXTRA_SOCK_OPTS, SendTE => 0 );
39
40 my $self = bless {
41 _username => $args{username} || die( 'No username defined' ),
42 _password => $args{password} || die( 'No password defined' ),
43 _login_url => $args{login_server} || $LOGIN_URL,
44 _mail_url => $args{mail_server} || $args{encrypt_session} ? $SSL_MAIL_URL : $MAIL_URL,
45 _proxy_user => $args{proxy_username} || '',
46 _proxy_pass => $args{proxy_password} || '',
47 0 ▶ _proxy_name => $args{proxy_name} || '',
Branch: 1 branch
BranchTrueFalseTotal
$args{'mail_server'} || $args{'encrypt_session'} ? : 0 0 0
Condition0%0/16
$args{'username'} || die('No username defined')
l!l
00
$args{'password'} || die('No password defined')
l!l
00
$args{'login_server'} || $Mail::Webmail::Gmail::LOGIN_URL
l!l&&r!l&&!r
000
$args{"mail_server"} or $args{"encrypt_session"}
l!l&&r!l&&!r
000
$args{'proxy_username'} || ''
l!l
00
$args{'proxy_password'} || ''
l!l
00
$args{'proxy_name'} || ''
l!l
00
MC/DC0%0/1
$args{'username'} || die('No username defined')
$args{'username'}
MC/DC0%0/1
$args{'password'} || die('No password defined')
$args{'password'}
MC/DC0%0/2
$args{'login_server'} || $Mail::Webmail::Gmail::LOGIN_URL
$args{'login_server'} $Mail::Webmail::Gmail::LOGIN_URL
MC/DC0%0/2
$args{"mail_server"} or $args{"encrypt_session"}
$args{"mail_server"} $args{"encrypt_session"}
MC/DC0%0/1
$args{'proxy_username'} || ''
$args{'proxy_username'}
MC/DC0%0/1
$args{'proxy_password'} || ''
$args{'proxy_password'}
MC/DC0%0/1
$args{'proxy_name'} || ''
$args{'proxy_name'}
Truth table0%0/2
A
A= $args{'username'}
Aresult
11
00
Truth table0%0/2
A
A= $args{'password'}
Aresult
11
00
Truth table0%0/3
A || B
A= $args{'login_server'}B= $Mail::Webmail::Gmail::LOGIN_URL
ABresult
1X1
011
000
Truth table0%0/3
A or B
A= $args{"mail_server"}B= $args{"encrypt_session"}
ABresult
1X1
011
000
Truth table0%0/2
A
A= $args{'proxy_username'}
Aresult
11
00
Truth table0%0/2
A
A= $args{'proxy_password'}
Aresult
11
00
Truth table0%0/2
A
A= $args{'proxy_name'}
Aresult
11
00
48 _proxy_enable => 0,
49 _logged_in => 0,
50 _err_str => '',
51 _cookies => { },
52 _ua => $ua,
53 _debug_level => 0,
54 _error => 0,
55 }, $class;
56
57 0 ▶ if ( defined( $args{proxy_name} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"proxy_name"}) 0 0 0
58 0 $self->{_proxy_enable}++;
59 0 ▶ if ( defined( $args{proxy_username} ) && defined( $args{proxy_password} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"proxy_username"} and defined $args{"proxy_password"}) 0 0 0
Condition0%0/3
defined $args{"proxy_username"} and defined $args{"proxy_password"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $args{"proxy_username"} and defined $args{"proxy_password"}
defined $args{"proxy_username"} defined $args{"proxy_password"}
Truth table0%0/3
A and B
A= defined $args{"proxy_username"}B= defined $args{"proxy_password"}
ABresult
0X0
100
111
60 0 $self->{_proxy_enable}++;
61 }
62 }
63
64 0 return $self;
65 }
66
67 0 ▶sub error {
Subroutine error: not called CC=1 SCAR=6.9
68 0 my ( $self ) = @_;
69 0 return( $self->{_error} );
70 }
71
72 0 ▶sub error_msg {
Subroutine error_msg: not called CC=1 SCAR=6.9
73 0 my ( $self ) = @_;
74 0 my $error_msg = $self->{_err_str};
75
76 0 $self->{_error} = 0;
77 0 $self->{_err_str} = '';
78 0 return( $error_msg );
79 }
80
81 0 ▶sub login {
Subroutine login: not called CC=12 SCAR=50.5
82 0 my ( $self ) = @_;
83
84 0 ▶ return 0 if $self->{_logged_in};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_logged_in"} 0 0 0
85
86 0 ▶ if ( $self->{_proxy_enable} && $self->{_proxy_enable} >= 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_proxy_enable"} and $self->{"_proxy_enable"} >= 1) 0 0 0
Condition0%0/3
$self->{"_proxy_enable"} and $self->{"_proxy_enable"} >= 1
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"_proxy_enable"} and $self->{"_proxy_enable"} >= 1
$self->{"_proxy_enable"} $self->{"_proxy_enable"} >= 1
Truth table0%0/3
A and B
A= $self->{"_proxy_enable"}B= $self->{"_proxy_enable"} >= 1
ABresult
0X0
100
111
87 0 $ENV{HTTPS_PROXY} = $self->{_proxy_name};
88 0 ▶ if ( $self->{_proxy_enable} && $self->{_proxy_enable} >= 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_proxy_enable"} and $self->{"_proxy_enable"} >= 2) 0 0 0
Condition0%0/3
$self->{"_proxy_enable"} and $self->{"_proxy_enable"} >= 2
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"_proxy_enable"} and $self->{"_proxy_enable"} >= 2
$self->{"_proxy_enable"} $self->{"_proxy_enable"} >= 2
Truth table0%0/3
A and B
A= $self->{"_proxy_enable"}B= $self->{"_proxy_enable"} >= 2
ABresult
0X0
100
111
89 0 $ENV{HTTPS_PROXY_USERNAME} = $self->{_proxy_user};
90 0 $ENV{HTTPS_PROXY_PASSWORD} = $self->{_proxy_pass};
91 }
92 }
93
94 0 my $req = HTTP::Request->new( POST => $self->{_login_url} );
95 0 $req->header( 'Cookie' => $self->{_cookie} );
96 0 my ( $cookie );
97
98 0 $req->content_type( "application/x-www-form-urlencoded" );
99 0 $req->content( 'Email=' . $self->{_username} . '&Passwd=' . $self->{_password} . '&null=Sign+in' );
100
101 0 my $res = $self->{_ua}->request( $req );
102
103 0 ▶ if ( !$res->is_success() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->is_success) 0 0 0
104 0 $self->{_error} = 1;
105 0 $self->{_err_str} .= "Error: Could not login with those credentials - the request was not a success\n";
106 0 $self->{_err_str} .= " Additionally, HTTP error: " . $res->status_line . "\n";
107 0 return;
108 }
109
110 0 update_tokens( $self, $res );
111 0 ▶ if ( $res->content() !~ /var url = (["'])(.*?)\1/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->content =~ /var url = (["'])(.*?)\1/) 0 0 0
112 0 $self->{_error} = 1;
113 0 $self->{_err_str} .= "Error: Could not login with those credentials - could not find final URL\n";
114 0 $self->{_err_str} .= " Additionally, HTTP error: " . $res->status_line . "\n";
115 0 return;
116 }
117
118 0 my $final_url;
119 0 ( $final_url = $2 ) =~ s/\\u003d/=/;
120
121 0 $req = HTTP::Request->new( GET => $final_url );
122 0 $req->header( 'Cookie' => $self->{_cookie} );
123 0 $res = $self->{_ua}->request( $req );
124 # if ( ( $res->content() !~ /<a href="http:\/\/mail\.google\.com\/mail\?view=(?:.*?)&amp;fs=(?:.*?)" target="_blank"> (?:.*?) <\/a>/ ) &&
125 # ( $res->content() !~ /<a href="http:\/\/mail\.google\.com\/mail">(?:.*?)<\/a>/ ) ) {
126 # $self->{_error} = 1;
127 # $self->{_err_str} .= "Error: Could not login with those credentials - could not find Gmail account.\n";
128 # $self->{_err_str} .= " Additionally, HTTP error: " . $res->status_line . "\n";
129 # return;
130 # }
131 0 update_tokens( $self, $res );
132
133 0 $req = HTTP::Request->new( GET => 'http://mail.google.com/mail?view=pr&amp;fs=1' );
134 0 $req->header( 'Cookie' => $self->{_cookie} );
135 0 $res = $self->{_ua}->request( $req );
136 0 ▶ if ( $res->content() !~ /top.location="(.*?)"/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->content =~ /top.location="(.*?)"/) 0 0 0
137 0 $self->{_error} = 1;
138 0 $self->{_err_str} .= "Error: Could not login with those credentials - could not find 'top.location'\n";
139 0 $self->{_err_str} .= " Additionally, HTTP error: " . $res->status_line . "\n";
140 0 return;
141 }
142 0 update_tokens( $self, $res );
143
144 0 $final_url = ( URI::Escape::uri_unescape( $1 ) );
145 0 ▶ if ( $self->{_proxy_enable} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_proxy_enable"}) 0 0 0
146 0 ▶ if ( $self->{_proxy_enable} >= 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_proxy_enable"} >= 1) 0 0 0
147 0 $self->{_ua}->proxy( 'http', $self->{_proxy_name} );
148 0 delete ( $ENV{HTTPS_PROXY} );
149 }
150 0 ▶ if ( $self->{_proxy_enable} >= 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_proxy_enable"} >= 2) 0 0 0
151 0 delete ( $ENV{HTTPS_PROXY_USERNAME} );
152 0 delete ( $ENV{HTTPS_PROXY_PASSWORD} );
153 }
154 }
155
156 0 $self->{_logged_in} = 1;
157 0 $res = get_page( $self, search => '', start => '', view => '', req_url => $final_url );
158 0 $res = get_page( $self, search => 'inbox' );
159
160 0 return ( 1 );
161 }
162
163 0 ▶sub check_login {
Subroutine check_login: not called CC=3 SCAR=24.8
164 0 my ( $self ) = @_;
165
166 0 ▶ if ( !$self->{_logged_in} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_logged_in"}) 0 0 0
167 0 ▶ unless ( $self->login() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->login) 0 0 0
168 0 $self->{_error} = 1;
169 0 $self->{_err_str} .= "Error: Could not Login.\n";
170 0 return;
171 }
172 }
173 0 return ( $self->{_logged_in} );
174 }
175
176 0 ▶sub update_tokens {
Subroutine update_tokens: not called CC=8 SCAR=42.8
177 0 my ( $self, $res ) = @_;
178
179 0 my $previous = $res->previous();
180 0 ▶ if ( $previous ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($previous) 0 0 0
181 0 update_tokens( $self, $previous );
182 }
183 0 my $header = $res->header( 'Set-Cookie' );
184 0 ▶ if ( defined( $header ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $header) 0 0 0
185 0 my ( @cookies ) = split( ',', $header );
186 0 foreach( @cookies ) {
187 0 $_ =~ s/^\s*//;
188 0 ▶ if ( $_ =~ /(.*?)=(.*?);/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ =~ /(.*?)=(.*?);/) 0 0 0
189 0 ▶ if ( $2 eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($2 eq '') { } 0 0 0
190 0 delete( $self->{_cookies}->{$1} );
191 } else {
192 0 ▶ unless ( $1 =~ /\s/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($1 =~ /\s/) 0 0 0
193 0 ▶ if ( $1 ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 ne '') { } 0 0 0
194 0 $self->{_cookies}->{$1} = $2;
195 } else {
196 0 $self->{_cookies}->{'Session'} = $2;
197 }
198 }
199 }
200 }
201 }
202 0 $self->{_cookie} = join( '; ', map{ "$_=$self->{_cookies}->{$_}"; }( sort keys %{ $self->{_cookies} } ) );
203 }
204 }
205
206 0 ▶sub get_page {
Subroutine get_page: not called CC=20 SCAR=60.4
207 0 my ( $self ) = shift;
208 my ( %args ) = (
209 search => 'all',
210 view => 'tl',
211 start => 0,
212 method => '',
213 req_url => $self->{_mail_url},
214 0 @_, );
215 0 my ( $res, $req, $req_url, @tees );
216
217 0 ▶ unless ( check_login( $self ) ) { return };
Branch: 1 branch
BranchTrueFalseTotal
unless (check_login($self)) 0 0 0
218
219 0 ▶ if ( defined( $args{ 'label' } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"label"}) 0 0 0
220 0 $args{ 'label' } = validate_label( $self, $args{ 'label' } );
221 0 ▶ if ( $self->error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->error) { } 0 0 0
222 0 return;
223 } else {
224 0 $args{ 'cat' } = $args{ 'label' };
225 0 delete( $args{ 'label' } );
226 0 $args{ 'search' } = 'cat';
227 }
228 }
229
230 0 ▶ if ( defined( $args{ 't' } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"t"}) 0 0 0
231 0 ▶ if ( ref( $args{ 't' } ) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args{"t"} eq "ARRAY") 0 0 0
232 0 foreach ( @{ $args{ 't' } } ) {
233 0 push( @tees, 't' );
234 0 push( @tees, $_ );
235 }
236 0 delete( $args{ 't' } );
237 }
238 }
239
240 0 $req_url = $args{ 'req_url' };
241 0 delete( $args{ 'req_url' } );
242
243 0 my ( $url, $method, $view ) = '' x 3;
244
245 0 $method = $args{ 'method' };
246 0 delete( $args{ 'method' } );
247
248 0 ▶ if ( $method eq 'post' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq "post") 0 0 0
249 0 $view = $args{ 'view' };
250 0 delete( $args{ 'view' } );
251 }
252
253 0 foreach ( keys %args ) {
254 0 ▶ if ( defined( $args{ $_ } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{$_}) { } 0 0 0
255 0 ▶ if ( $args{ $_ } eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{$_} eq "") 0 0 0
256 0 delete( $args{ $_ } );
257 }
258 } else {
259 0 delete( $args{ $_ } );
260 }
261 }
262
263 0 ▶ if ( $method eq 'post' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq 'post') { } 0 0 0
264 $req = HTTP::Request::Common::POST( $req_url,
265 Content_Type => 'multipart/form-data',
266 Connection => 'Keep-Alive',
267 'Keep-Alive' => 300,
268 Cookie => $self->{_cookie},
269 0 Content => [ view => $view, %args, @tees ] );
270 0 ▶ if ( $self->{_proxy_enable} && $self->{_proxy_enable} >= 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_proxy_enable"} and $self->{"_proxy_enable"} >= 2) 0 0 0
Condition0%0/3
$self->{"_proxy_enable"} and $self->{"_proxy_enable"} >= 2
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"_proxy_enable"} and $self->{"_proxy_enable"} >= 2
$self->{"_proxy_enable"} $self->{"_proxy_enable"} >= 2
Truth table0%0/3
A and B
A= $self->{"_proxy_enable"}B= $self->{"_proxy_enable"} >= 2
ABresult
0X0
100
111
271 0 $req->proxy_authorization_basic( $self->{_proxy_user}, $self->{_proxy_pass} );
272 }
273 0 $res = $self->{_ua}->request( $req );
274 } else {
275 0 $url = join( '&', map{ "$_=$args{ $_ }"; }( keys %args ) );
276 0 ▶ if ( $url ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url ne "") 0 0 0
277 0 $url = '?' . $url;
278 }
279 0 $req = HTTP::Request->new( GET => $req_url . "$url" );
280 0 $req->header( 'Cookie' => $self->{_cookie} );
281 0 ▶ if ( $self->{_proxy_enable} && $self->{_proxy_enable} >= 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_proxy_enable"} and $self->{"_proxy_enable"} >= 2) 0 0 0
Condition0%0/3
$self->{"_proxy_enable"} and $self->{"_proxy_enable"} >= 2
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"_proxy_enable"} and $self->{"_proxy_enable"} >= 2
$self->{"_proxy_enable"} $self->{"_proxy_enable"} >= 2
Truth table0%0/3
A and B
A= $self->{"_proxy_enable"}B= $self->{"_proxy_enable"} >= 2
ABresult
0X0
100
111
282 0 $req->proxy_authorization_basic( $self->{_proxy_user}, $self->{_proxy_pass} );
283 }
284 0 $res = $self->{_ua}->request( $req );
285 }
286
287 0 ▶ if ( $res ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res) 0 0 0
288 0 ▶ if ( $res->is_success() ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
elsif ($res->previous) { } 0 0 0
289 0 update_tokens( $self, $res );
290 } elsif ( $res->previous() ) {
291 0 update_tokens( $self, $res->previous() );
292 }
293 }
294
295 0 return ( $res );
296 }
297
298 0 ▶sub size_usage {
Subroutine size_usage: not called CC=8 SCAR=42.8
299 0 my ( $self, $res ) = @_;
300
301 0 ▶ unless ( check_login( $self ) ) { return };
Branch: 1 branch
BranchTrueFalseTotal
unless (check_login($self)) 0 0 0
302
303 0 ▶ unless ( $res ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res) 0 0 0
304 0 $res = get_page( $self );
305 }
306
307 0 my %functions = %{ parse_page( $self, $res ) };
308
309 0 ▶ if ( $self->{_error} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_error"}) 0 0 0
310 0 return;
311 }
312
313 0 ▶ if ( $res->is_success() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
314 0 ▶ if ( defined( $functions{ 'qu' } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $functions{'qu'}) { } 0 0 0
315 0 ▶ if ( wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
316 0 pop( @{ $functions{ 'qu' } } );
317 0 foreach ( @{ $functions{ 'qu' } } ) {
318 0 s/"//g;
319 }
320 0 return( @{ $functions{ 'qu' } } );
321 } else {
322 0 $functions{ 'qu' }[0] =~ /"(.*)\s/;
323 0 my $used = $1;
324 0 $functions{ 'qu' }[1] =~ /"(.*)\s/;
325 0 my $size = $1;
326 0 return( $size - $used );
327 }
328 } else {
329 0 $self->{_error} = 1;
330 0 $self->{_err_str} .= "Error: Could not find free space info.\n";
331 0 return;
332 }
333 } else {
334 0 $self->{_error} = 1;
335 0 $self->{_err_str} .= "Error: While requesting: '$res->{_request}->{_uri}'.\n";
336 0 return;
337 }
338 }
339
340 0 ▶sub edit_labels {
Subroutine edit_labels: not called CC=13 SCAR=52.0
341 0 my ( $self ) = shift;
342 0 my ( %args ) = (
343 start => '',
344 search => '',
345 action => '',
346 label => '',
347 new_name => '',
348 view => 'up',
349 method => 'post',
350 @_,
351 );
352
353 0 ▶ unless ( check_login( $self ) ) { return };
Branch: 1 branch
BranchTrueFalseTotal
unless (check_login($self)) 0 0 0
354
355 0 my $action;
356
357 0 ▶ if ( uc( $args{ 'action' } ) eq 'CREATE' ) {
Branch: 5 branches
BranchTrueFalseTotal
if (uc $args{'action'} eq 'CREATE') { } 0 0 0
elsif (uc $args{'action'} eq 'DELETE') { } 0 0 0
elsif (uc $args{'action'} eq 'REMOVE') { } 0 0 0
elsif (uc $args{'action'} eq 'ADD') { } 0 0 0
elsif (uc $args{'action'} eq 'RENAME') { } 0 0 0
358 0 $action = 'cc_';
359 0 $args{ 'new_name' } = '';
360 } elsif ( uc( $args{ 'action' } ) eq 'DELETE' ) {
361 0 $action = 'dc_';
362 0 $args{ 'new_name' } = '';
363 } elsif ( uc( $args{ 'action' } ) eq 'REMOVE' ) {
364 0 $action = 'rc_';
365 0 $args{ 'new_name' } = '';
366 } elsif ( uc( $args{ 'action' } ) eq 'ADD' ) {
367 0 $action = 'ac_';
368 0 $args{ 'new_name' } = '';
369 0 ▶ unless ( defined( $args{ 'msgid' } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{'msgid'}) { } 0 0 0
370 0 $self->{_error} = 1;
371 0 $self->{_err_str} .= "To add a label to a message, you must supply a msgid.\n";
372 0 return;
373 } else {
374 0 $args{ 't' } = $args{ 'msgid' };
375 0 delete( $args{ 'msgid' } );
376 0 $args{ 'search' } = 'all';
377 }
378 } elsif ( uc( $args{ 'action' } ) eq 'RENAME' ) {
379 0 $args{ 'new_name' } = '^' . validate_label( $self, $args{ 'new_name' } );
380 0 ▶ if ( $self->{_error} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_error"}) 0 0 0
381 0 return;
382 }
383 0 $action = 'nc_';
384 } else {
385 0 $self->{_error} = 1;
386 0 $self->{_err_str} .= "Error: No action defined.\n";
387 0 return;
388 }
389
390 0 $args{ 'act' } = $action . validate_label( $self, $args{ 'label' } ) . $args{ 'new_name' };
391 0 ▶ if ( $self->{_error} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_error'}) { } 0 0 0
392 0 return;
393 } else {
394 0 delete( $args{ 'label' } );
395 0 delete( $args{ 'action' } );
396 0 $args{ 'at' } = $self->{_cookies}->{GMAIL_AT};
397 }
398
399 0 my $res = get_page( $self, %args );
400
401 0 ▶ if ( $res->is_success() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
402 0 my %functions = %{ parse_page( $self, $res ) };
403 0 ▶ if ( defined( $functions{ 'ar' } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $functions{'ar'}) { } 0 0 0
404 0 ▶ unless ( $functions{ 'ar' }->[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($functions{'ar'}[0]) { } 0 0 0
405 0 $self->{_error} = 1;
406 0 $self->{_err_str} .= "Error: " . $functions{ 'ar' }->[1] . "\n";
407 0 return;
408 } else {
409 0 return( 1 );
410 }
411 } else {
412 0 $self->{_error} = 1;
413 0 $self->{_err_str} .= "Error: Could not find label success message.\n";
414 0 return;
415 }
416 } else {
417 0 $self->{_error} = 1;
418 0 $self->{_err_str} .= "Error: While requesting: '$res->{_request}->{_uri}'.\n";
419 0 return;
420 }
421 }
422
423 0 ▶sub get_labels {
Subroutine get_labels: not called CC=8 SCAR=42.8
424 0 my ( $self, $res ) = @_;
425
426 0 ▶ unless ( check_login( $self ) ) { return };
Branch: 1 branch
BranchTrueFalseTotal
unless (check_login($self)) 0 0 0
427
428 0 ▶ unless ( $res ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res) 0 0 0
429 0 $res = get_page( $self, search => 'inbox' );
430 }
431
432 0 ▶ if ( $res->is_success() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
433 0 my %functions = %{ parse_page( $self, $res ) };
434
435 0 ▶ if ( $self->{_error} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_error"}) 0 0 0
436 0 return;
437 }
438
439 0 ▶ unless ( defined( $functions{ 'ct' } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $functions{"ct"}) 0 0 0
440 0 return;
441 }
442
443 0 my @fields = @{ extract_fields( $functions{ 'ct' }->[0] ) };
444 0 foreach ( @fields ) {
445 0 $_ = ${ extract_fields( $_ ) }[0];
446 0 $_ = remove_quotes( $_ );
447 }
448 0 ▶ if ( @fields ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fields) { } 0 0 0
449 0 return( @fields );
450 } else {
451 0 $self->{_error} = 1;
452 0 $self->{_err_str} .= "Error: No Labels found.\n";
453 0 return;
454 }
455 } else {
456 0 $self->{_error} = 1;
457 0 $self->{_err_str} .= "Error: While requesting: '$res->{_request}->{_uri}'.\n";
458 0 return;
459 }
460 }
461
462 0 ▶sub validate_label {
Subroutine validate_label: not called CC=8 SCAR=42.8
463 0 my ( $self, $label ) = @_;
464
465 0 ▶ if ( defined( $label ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $label) { } 0 0 0
466 0 $label =~ s/^\s//;
467 0 $label =~ s/\s$//;
468 0 ▶ if ( $label =~ /\^/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($label =~ /\^/) 0 0 0
469 0 my $is_folder = 0;
470 0 foreach ( keys %FOLDERS ) {
471 0 ▶ if ( $FOLDERS{ $_ } eq uc( $label ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Mail::Webmail::Gmail::FOLDERS{$_} eq uc $label) 0 0 0
472 0 $is_folder = 1;
473 }
474 }
475 0 ▶ unless ( $is_folder ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_folder) 0 0 0
476 0 $self->{_error} = 1;
477 0 $self->{_err_str} .= "Error: Labels cannot contain the character '^'.\n";
478 0 return;
479 }
480 }
481 0 ▶ if ( length( $label ) > 40 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $label > 40) 0 0 0
482 0 $self->{_error} = 1;
483 0 $self->{_err_str} .= "Error: Labels cannot contain more than 40 characters.\n";
484 0 return;
485 }
486 0 ▶ if ( length( $label ) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $label == 0) 0 0 0
487 0 $self->{_error} = 1;
488 0 $self->{_err_str} .= "Error: No labels specified.\n";
489 0 return;
490 }
491 0 return( $label );
492 } else {
493 0 $self->{_error} = 1;
494 0 $self->{_err_str} .= "Error: No labels specified.\n";
495 0 return;
496 }
497 }
498
499 0 ▶sub edit_star {
Subroutine edit_star: not called CC=8 SCAR=42.8
500 0 my ( $self ) = shift;
501 0 my ( %args ) = (
502 start => '',
503 action => '',
504 view => 'up',
505 @_,
506 );
507
508 0 ▶ unless ( check_login( $self ) ) { return };
Branch: 1 branch
BranchTrueFalseTotal
unless (check_login($self)) 0 0 0
509
510 0 my $action;
511
512 0 ▶ if ( $args{ 'action' } eq 'add' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'action'} eq 'add') { } 0 0 0
elsif ($args{'action'} eq 'remove') { } 0 0 0
513 0 $args{ 'act' } = 'st';
514 } elsif ( $args{ 'action' } eq 'remove' ) {
515 0 $args{ 'act' } = 'xst';
516 } else {
517 0 $self->{_error} = 1;
518 0 $self->{_err_str} .= "Error: No action defined.\n";
519 0 return;
520 }
521 0 delete( $args{ 'action' } );
522
523 0 ▶ if ( defined( $args{ 'msgid' } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{'msgid'}) { } 0 0 0
524 0 $args{ 'm' } = $args{ 'msgid' };
525 0 delete( $args{ 'msgid' } );
526 } else {
527 0 $self->{_error} = 1;
528 0 $self->{_err_str} .= "Error: No msgid sent.\n";
529 0 return;
530 }
531
532 0 $args{ 'at' } = $self->{_cookies}->{GMAIL_AT};
533
534 0 my $res = get_page( $self, %args );
535
536 0 ▶ if ( $res->is_success() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
537 0 my %functions = %{ parse_page( $self, $res ) };
538 0 ▶ if ( defined( $functions{ 'ar' } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $functions{'ar'}) { } 0 0 0
539 0 ▶ unless ( $functions{ 'ar' }->[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($functions{'ar'}[0]) { } 0 0 0
540 0 $self->{_error} = 1;
541 0 $self->{_err_str} .= "Error: " . $functions{ 'ar' }->[1] . "\n";
542 0 return;
543 } else {
544 0 return( 1 );
545 }
546 } else {
547 0 $self->{_error} = 1;
548 0 $self->{_err_str} .= "Error: Could not find label success message.\n";
549 0 return;
550 }
551 } else {
552 0 $self->{_error} = 1;
553 0 $self->{_err_str} .= "Error: While requesting: '$res->{_request}->{_uri}'.\n";
554 0 return;
555 }
556 }
557
558 0 ▶sub edit_archive {
Subroutine edit_archive: not called CC=8 SCAR=42.8
559 0 my ( $self ) = shift;
560 0 my ( %args ) = (
561 action => '',
562 msgid => '',
563 method => 'post',
564 @_,
565 );
566
567 0 ▶ unless ( check_login( $self ) ) { return };
Branch: 1 branch
BranchTrueFalseTotal
unless (check_login($self)) 0 0 0
568
569 0 ▶ if ( $args{ 'action' } eq 'archive' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'action'} eq 'archive') { } 0 0 0
elsif ($args{'action'} eq 'unarchive') { } 0 0 0
570 0 $args{ 'act' } = 'rc_' . lc( $FOLDERS{ 'INBOX' } );
571 } elsif ( $args{ 'action' } eq 'unarchive' ) {
572 0 $args{ 'act' } = 'ib';
573 } else {
574 0 $self->{_error} = 1;
575 0 $self->{_err_str} .= "Error: No action defined.\n";
576 0 return;
577 }
578 0 delete( $args{ 'action' } );
579
580 0 ▶ if ( defined( $args{ 'msgid' } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{'msgid'}) { } 0 0 0
581 0 $args{ 't' } = $args{ 'msgid' };
582 0 delete( $args{ 'msgid' } );
583 } else {
584 0 $self->{_error} = 1;
585 0 $self->{_err_str} .= "Error: No msgid sent.\n";
586 0 return;
587 }
588
589 0 $args{ 'at' } = $self->{_cookies}->{GMAIL_AT};
590
591 0 my $res = get_page( $self, %args );
592
593 0 ▶ if ( $res->is_success() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
594 0 my %functions = %{ parse_page( $self, $res ) };
595 0 ▶ if ( defined( $functions{ 'ar' } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $functions{'ar'}) { } 0 0 0
596 0 ▶ unless ( $functions{ 'ar' }->[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($functions{'ar'}[0]) { } 0 0 0
597 0 $self->{_error} = 1;
598 0 $self->{_err_str} .= "Error: " . $functions{ 'ar' }->[1] . "\n";
599 0 return;
600 } else {
601 0 return( 1 );
602 }
603 } else {
604 0 $self->{_error} = 1;
605 0 $self->{_err_str} .= "Error: Could not find archive success message.\n";
606 0 return;
607 }
608 } else {
609 0 $self->{_error} = 1;
610 0 $self->{_err_str} .= "Error: While requesting: '$res->{_request}->{_uri}'.\n";
611 0 return;
612 }
613 }
614
615 0 ▶sub multi_email_addr {
Subroutine multi_email_addr: not called CC=2 SCAR=17.9
616 0 my $array_ref = shift;
617
618 0 my $email_list;
619 0 foreach( @{ $array_ref } ) {
620 0 $email_list .= "<$_>, ";
621 }
622 0 return( $email_list );
623 }
624
625 0 ▶sub send_message {
Subroutine send_message: not called CC=14 SCAR=53.5
626 0 my ( $self ) = shift;
627 my ( %args ) = (
628 start => '',
629 search => '',
630 action => '',
631 view => 'sm',
632 cmid => '1' || $_{cmid},
633 to => '' || $_{to},
634 cc => '' || $_{cc},
635 bcc => '' || $_{bcc},
636 subject => '' || $_{subject},
637 msgbody => '' || $_{msgbody},
638 0 method => 'post',
639 @_,
640 );
641
642 0 ▶ unless ( check_login( $self ) ) { return };
Branch: 1 branch
BranchTrueFalseTotal
unless (check_login($self)) 0 0 0
643
644 0 $args{ 'at' } = $self->{_cookies}->{GMAIL_AT};
645
646 0 ▶ if ( ( $args{to} ne '' ) || ( $args{cc} ne '' ) || ( $args{bcc} ne '' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'to'} ne '' or $args{'cc'} ne '' or $args{'bcc'} ne '') { } 0 0 0
Condition0%0/6
$args{"to"} ne "" or $args{"cc"} ne "" or $args{"bcc"} ne ""
l!l&&r!l&&!r
000
$args{"to"} ne "" or $args{"cc"} ne ""
l!l&&r!l&&!r
000
MC/DC0%0/3
$args{"to"} ne "" or $args{"cc"} ne "" or $args{"bcc"} ne ""
$args{"to"} ne "" $args{"cc"} ne "" $args{"bcc"} ne ""
Truth table0%0/4
A or B or C
A= $args{"to"} ne ""B= $args{"cc"} ne ""C= $args{"bcc"} ne ""
ABCresult
1XX1
01X1
0011
0000
647 0 foreach( 'to', 'cc', 'bcc' ) {
648 0 ▶ if ( ref( $args{$_} ) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args{$_} eq "ARRAY") 0 0 0
649 0 $args{$_} = multi_email_addr( $args{$_} );
650 }
651 }
652
653 0 foreach( keys %args ) {
654 0 ▶ if ( defined( $args{ $_ } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{$_}) 0 0 0
655 0 $args{ $_ } =~ s/&/%26/g;
656 }
657 }
658
659 0 my $res = get_page( $self, %args );
660 0 ▶ if ( $res->is_success() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) 0 0 0
661 0 my %functions = %{ parse_page( $self, $res ) };
662
663 0 ▶ if ( $self->{_error} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_error"}) 0 0 0
664 0 return;
665 }
666 0 ▶ unless ( defined( $functions{ 'sr' } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $functions{"sr"}) 0 0 0
667 0 return;
668 }
669 0 ▶ if ( $functions{ 'sr' }->[1] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($functions{'sr'}[1]) { } 0 0 0
670 0 ▶ if ( $functions{ 'sr' }->[3] eq '"0"' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($functions{'sr'}[3] eq '"0"') { } 0 0 0
671 0 $self->{_error} = 1;
672 0 $self->{_err_str} .= "This message has already been sent.\n";
673 0 return;
674 } else {
675 0 $functions{ 'sr' }->[3] =~ s/"//g;
676 0 return( $functions{ 'sr' }->[3] );
677 }
678 } else {
679 0 $self->{_error} = 1;
680 0 $self->{_err_str} .= "Message could not be sent.\n";
681 0 return;
682 }
683 }
684 } else {
685 0 $self->{_error} = 1;
686 0 $self->{_err_str} .= "One of the following must be filled out: to, cc, bcc.\n";
687 0 return;
688 }
689 }
690
691 0 ▶sub get_messages {
Subroutine get_messages: not called CC=13 SCAR=52.0
692 0 my ( $self ) = shift;
693 0 my ( %args ) = (
694 init => 1,
695 start => 0,
696 @_, );
697 0 my ( $res, $req );
698
699 0 ▶ if ( defined( $args{ 'label' } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"label"}) 0 0 0
700 0 $args{ 'label' } = validate_label( $self, $args{ 'label' } );
701 0 ▶ if ( $self->error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->error) { } 0 0 0
702 0 return;
703 } else {
704 0 $args{ 'cat' } = $args{ 'label' };
705 0 delete( $args{ 'label' } );
706 0 $args{ 'search' } = 'cat';
707 }
708 }
709
710 0 ▶ unless ( check_login( $self ) ) { return };
Branch: 1 branch
BranchTrueFalseTotal
unless (check_login($self)) 0 0 0
711
712 0 $res = get_page( $self, %args );
713
714 0 ▶ if ( $res->is_success() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
715 0 my %functions = %{ parse_page( $self, $res ) };
716
717 0 ▶ if ( $self->{_error} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_error"}) 0 0 0
718 0 return;
719 }
720 0 my ( @emails, @letters );
721
722 0 ▶ unless ( defined( $functions{ 't' } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $functions{"t"}) 0 0 0
723 0 return;
724 }
725
726 0 foreach ( @{ $functions{ 't' } } ) {
727 0 my @email_line = @{ extract_fields( $_ ) };
728 0 my %indv_email;
729 0 $indv_email{ 'id' } = remove_quotes( $email_line[0] );
730 0 $indv_email{ 'new' } = remove_quotes( $email_line[1] );
731 0 $indv_email{ 'starred' } = remove_quotes( $email_line[2] );
732 0 $indv_email{ 'date_received' } = remove_quotes( $email_line[3] );
733 0 $indv_email{ 'sender_email' } = remove_quotes( $email_line[4] );
734 0 $indv_email{ 'sender_email' } =~ /\\"(.*?)\\">/;
735 0 $1 =~ /_(?:.*?)_(.*?)$/;
736 0 $indv_email{ 'sender_email' } = $1;
737 0 $indv_email{ 'sender_name' } = remove_quotes( $email_line[4] );
738 0 $indv_email{ 'sender_name' } =~ />(?:<b>)?(.*?)<\//;
739 0 $indv_email{ 'sender_name' } = $1;
740 0 $indv_email{ 'subject' } = remove_quotes( $email_line[6] );
741 0 $indv_email{ 'blurb' } = remove_quotes( $email_line[7] );
742 0 $indv_email{ 'labels' } = [ map{ remove_quotes( $_ ) }@{ extract_fields( $email_line[8] ) } ];
743 0 $email_line[9] = remove_quotes( $email_line[9] );
744 0 ▶ $indv_email{ 'attachments' } = extract_fields( $email_line[9] ) if ( $email_line[9] ne '' );
Branch: 1 branch
BranchTrueFalseTotal
if $email_line[9] ne "" 0 0 0
745 0 push ( @emails, \%indv_email );
746 }
747 0 ▶ if ( ( @emails == @{ $functions{ 'ts' } }[1] ) && ( @{ $functions{ 'ts' } }[0] != @{ $functions{ 'ts' } }[2] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@emails == @{$functions{"ts"};}[1] and @{$functions{"ts"};}[0] != @{$functions{"ts"};}[2]) 0 0 0
Condition0%0/3
@emails == @{$functions{"ts"};}[1] and @{$functions{"ts"};}[0] != @{$functions{"ts"};}[2]
!ll&&!rl&&r
000
MC/DC0%0/2
@emails == @{$functions{"ts"};}[1] and @{$functions{"ts"};}[0] != @{$functions{"ts"};}[2]
@emails == @{$functions{"ts"};}[1] @{$functions{"ts"};}[0] != @{$functions{"ts"};}[2]
Truth table0%0/3
A and B
A= @emails == @{$functions{"ts"};}[1]B= @{$functions{"ts"};}[0] != @{$functions{"ts"};}[2]
ABresult
0X0
100
111
748 0 my $start = $args{ 'start' };
749 0 delete( $args{ 'start' } );
750 0 ▶ if ( $args{ 'cat' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"cat"}) 0 0 0
751 0 $args{ 'label' } = $args{ 'cat' };
752 0 delete ( $args{ 'cat' } );
753 0 delete ( $args{ 'search' } );
754 }
755 0 my $next_page_emails = get_messages( $self, start => ( $start + @emails ), %args );
756 0 ▶ if ( $next_page_emails ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next_page_emails) 0 0 0
757 0 @emails = ( @emails, @{ $next_page_emails } );
758 }
759 }
760 0 return ( \@emails );
761 } else {
762 0 $self->{_error} = 1;
763 0 $self->{_err_str} .= "Error: While requesting: '$res->{_request}->{_uri}'.\n";
764 0 return;
765 }
766 }
767
768 0 ▶sub delete_message {
Subroutine delete_message: not called CC=12 SCAR=50.5
769 0 my ( $self ) = shift;
770 0 my ( %args ) = (
771 act => 'tr',
772 method => 'post',
773 at => '',
774 del_message => 1,
775 @_, );
776
777 0 ▶ if ( defined( $args{ 'msgid' } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{'msgid'}) { } 0 0 0
778 0 $args{ 't' } = $args{ 'msgid' };
779 0 delete( $args{ 'msgid' } );
780 } else {
781 0 $self->{_error} = 1;
782 0 $self->{_err_str} .= "Error: No msgid provided.\n";
783 0 return;
784 }
785
786 0 my $del_message = $args{ 'del_message' };
787 0 delete( $args{ 'del_message' } );
788
789 0 ▶ unless ( check_login( $self ) ) { return };
Branch: 1 branch
BranchTrueFalseTotal
unless (check_login($self)) 0 0 0
790
791 0 $args{ 'at' } = $self->{_cookies}->{GMAIL_AT};
792
793 0 my $res = get_page( $self, %args );
794
795 0 ▶ if ( $res->is_success() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
796 0 my %functions = %{ parse_page( $self, $res ) };
797
798 0 ▶ if ( $self->{_error} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_error"}) 0 0 0
799 0 return;
800 }
801 0 ▶ unless ( defined( $functions{ 'ar' } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $functions{"ar"}) 0 0 0
802 0 return;
803 }
804 0 ▶ if ( $functions{ 'ar' }->[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($functions{'ar'}[0]) { } 0 0 0
805 0 ▶ if ( $del_message ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($del_message) { } 0 0 0
806 0 $args{ 'act' } = 'dl';
807 0 $args{ 'search' } = 'trash';
808 0 $res = get_page( $self, %args );
809 0 ▶ if ( $res->is_success() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
810 0 my %functions = %{ parse_page( $self, $res ) };
811
812 0 ▶ if ( $self->{_error} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_error"}) 0 0 0
813 0 return;
814 }
815 0 ▶ unless ( defined( $functions{ 'ar' } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $functions{"ar"}) 0 0 0
816 0 return;
817 }
818 0 ▶ if ( $functions{ 'ar' }->[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($functions{'ar'}[0]) { } 0 0 0
819 0 return( 1 );
820 } else {
821 0 $self->{_error} = 1;
822 0 $self->{_err_str} .= remove_quotes( $functions{ 'ar'}->[1] ) . "\n";
823 0 return;
824 }
825 } else {
826 0 $self->{_error} = 1;
827 0 $self->{_err_str} .= "Error: While requesting: '$res->{_request}->{_uri}'.\n";
828 0 return;
829 }
830 } else {
831 0 return( 1 );
832 }
833 } else {
834 0 $self->{_error} = 1;
835 0 $self->{_err_str} .= remove_quotes( $functions{ 'ar'}->[1] ) . "\n";
836 0 return;
837 }
838 } else {
839 0 $self->{_error} = 1;
840 0 $self->{_err_str} .= "Error: While requesting: '$res->{_request}->{_uri}'.\n";
841 0 return;
842 }
843 }
844
845 sub get_indv_email {
846 my ( $self ) = shift;
847 my ( %args ) = (
848 view => 'cv',
849 @_, );
850
851 if ( defined( $args{ 'id' } ) && defined( $args{ 'label' } ) ) {
852 $args{ 'label' } = validate_label( $self, $args{ 'label' } );
853 if ( $self->error() ) {
854 return;
855 } else {
856 $args{ 'cat' } = $args{ 'label' };
857 delete( $args{ 'label' } );
858 $args{ 'search' } = 'cat';
859 }
860 $args{ 'th' } = $args{ 'id' };
861 delete( $args{ 'id' } );
862 } elsif ( defined( $args{ 'msg' } ) ) {
863 if ( defined( $args{ 'msg' }->{ 'id' } ) ) {
864 $args{ 'th' } = $args{ 'msg' }->{ 'id' };
865 } else {
866 $self->{_error} = 1;
867 $self->{_err_str} .= "Error: Not a valid msg reference.\n";
868 return;
869 }
870
871 if ( defined( @{ $args{ 'msg' }->{ 'labels' } } ) ) {
872 if ( $args{ 'msg' }->{ 'labels' }->[0] ne '' ) {
873 $args{ 'label' } = validate_label( $self, $args{ 'msg' }->{ 'labels' }->[0] );
874 delete( $args{ 'msg' }->{ 'label' } );
875 if ( $self->error ) {
876 return;
877 } else {
878 if ( $args{ 'label' } =~ /^\^.$/ ) {
879 $args{ 'label' } = cat_to_search( $args{ 'label' } );
880 $args{ 'search' } = $args{ 'label' };
881 } else {
882 $args{ 'cat' } = $args{ 'label' };
883 $args{ 'search' } = 'cat';
884 }
885 delete( $args{ 'label' } );
886 }
887 }
888 }
889 delete( $args{ 'msg' } );
890 } else {
891 $self->{_error} = 1;
892 $self->{_err_str} .= "Error: Must specify either id and label or send a reference to a valid message with msg.\n";
893 return;
894 }
895
896 unless ( check_login( $self ) ) { return };
897
898 my $res = get_page( $self, %args );
899
900 if ( $res->is_success() ) {
901 my %functions = %{ parse_page( $self, $res ) };
902
903 if ( defined( $functions{ 'mi' } ) ) {
904 my %messages;
905 my @thread;
906 foreach ( @{ $functions{ 'mi' } } ) {
907 my %message;
908 my @email = @{ extract_fields( $_ ) };
909 $email[2] = remove_quotes( $email[2] );
910 if ( $email[16] ne '' ) {
911 my @attachments = @{ extract_fields( $email[17] ) };
912 my @files;
913 foreach ( @attachments ) {
914 my @attachment = @{ extract_fields( $_ ) };
915 my %indv_attachment;
916 $indv_attachment{ 'id' } = remove_quotes( $attachment[0] );
917 $indv_attachment{ 'name' } = remove_quotes( $attachment[1] );
918 $indv_attachment{ 'encoding' } = remove_quotes( $attachment[2] );
919 $indv_attachment{ 'th' } = $email[2];
920 push( @files, \%indv_attachment );
921 }
922 $message{ 'attachments' } = \@files;
923 }
924 $message{ 'id' } = $email[2];
925 $message{ 'sender' } = remove_quotes( $email[7] );
926 $message{ 'sent' } = remove_quotes( $email[9] );
927 $message{ 'to' } = remove_quotes( $email[10] );
928 $message{ 'read' } = remove_quotes( $email[14] );
929 $message{ 'subject' } = remove_quotes( $email[15] );
930 if ( $args{ 'th' } eq $email[2] ) {
931 foreach ( @{ $functions{ 'mb' } } ) {
932 my $body = extract_fields( $_ );
933 $message{ 'body' } .= $body->[0];
934 }
935 if ( defined( $functions{ 'cs' } ) ) {
936 if ( $functions{ 'cs' }[8] ne '' ) {
937 $message{ 'ads' } = get_ads( $self, adkey => remove_quotes( $functions{ 'cs' }[8] ) );
938 }
939 }
940 }
941 $messages{ $email[2] } = \%message;
942 }
943 return ( \%messages );
944 }
945
946 } else {
947 $self->{_error} = 1;
948 $self->{_err_str} .= "Error: While requesting: '$res->{_request}->{_uri}'.\n";
949 return;
950 }
951 }
952
953 sub get_mime_email {
954 my ( $self ) = shift;
955 my ( %args ) = (
956 view => 'om',
957 @_, );
958
959 if ( defined( $args{ 'id' } ) && defined( $args{ 'label' } ) ) {
960 $args{ 'label' } = validate_label( $self, $args{ 'label' } );
961 if ( $self->error() ) {
962 return;
963 } else {
964 $args{ 'cat' } = $args{ 'label' };
965 delete( $args{ 'label' } );
966 $args{ 'search' } = 'cat';
967 }
968 $args{ 'th' } = $args{ 'id' };
969 delete( $args{ 'id' } );
970 } elsif ( defined( $args{ 'msg' } ) ) {
971 if ( defined( $args{ 'msg' }->{ 'id' } ) ) {
972 $args{ 'th' } = $args{ 'msg' }->{ 'id' };
973 } else {
974 $self->{_error} = 1;
975 $self->{_err_str} .= "Error: Not a valid msg reference.\n";
976 return;
977 }
978
979 if ( defined( @{ $args{ 'msg' }->{ 'labels' } } ) ) {
980 if ( $args{ 'msg' }->{ 'labels' }->[0] ne '' ) {
981 $args{ 'label' } = validate_label( $self, $args{ 'msg' }->{ 'labels' }->[0] );
982 delete( $args{ 'msg' }->{ 'label' } );
983 if ( $self->error ) {
984 return;
985 } else {
986 if ( $args{ 'label' } =~ /^\^.$/ ) {
987 $args{ 'label' } = cat_to_search( $args{ 'label' } );
988 $args{ 'search' } = $args{ 'label' };
989 } else {
990 $args{ 'cat' } = $args{ 'label' };
991 $args{ 'search' } = 'cat';
992 }
993 delete( $args{ 'label' } );
994 }
995 }
996 }
997 delete( $args{ 'msg' } );
998 } else {
999 $self->{_error} = 1;
1000 $self->{_err_str} .= "Error: Must specify either id and label or send a reference to a valid message with msg.\n";
1001 return;
1002 }
1003
1004 unless ( check_login( $self ) ) { return };
1005
1006 my $res = get_page( $self, %args );
1007
1008 if ( $res->is_success() ) {
1009 my $content = $res->content;
1010 $content =~ s/\r\n/\n/g;
1011 $content =~ s/^(\s*\n)+//;
1012 return $content;
1013 } else {
1014 $self->{_error} = 1;
1015 $self->{_err_str} .= "Error: While requesting: '$res->{_request}->{_uri}'.\n";
1016 return;
1017 }
1018 }
1019
1020 sub get_contacts {
1021 my ( $self ) = shift;
1022 my ( %args ) = (
1023 @_, );
1024 my ( $res, $req );
1025
1026 $args{ 'view' } = 'cl';
1027 $args{ 'search' } = 'contacts';
1028 $args{ 'start' } = undef;
1029 $args{ 'method' } = 'get';
1030 $args{ 'pnl' } = $args{ 'frequent' } ? 'p' : 'a';
1031 delete $args{ 'frequent' };
1032
1033 unless ( check_login( $self ) ) { return };
1034
1035 $res = get_page( $self, %args );
1036
1037 if ( $res->is_success() ) {
1038 my %functions = %{ parse_page( $self, $res ) };
1039
1040 if ( $self->{_error} ) {
1041 return;
1042 }
1043 my ( @contacts );
1044
1045 unless ( defined( $functions{ 'cl' } ) ) {
1046 return;
1047 }
1048
1049 foreach ( @{ $functions{ 'cl' } } ) {
1050 my @contact_line = @{ extract_fields( $_ ) };
1051 my %indv_contact;
1052 $indv_contact{ 'id' } = remove_quotes( $contact_line[1] );
1053 $indv_contact{ 'name1' } = remove_quotes( $contact_line[2] );
1054 $indv_contact{ 'name2' } = remove_quotes( $contact_line[3] );
1055 $indv_contact{ 'email' } = remove_quotes( $contact_line[4] );
1056 $indv_contact{ 'note' } = remove_quotes( $contact_line[5] );
1057 push ( @contacts, \%indv_contact );
1058 }
1059 return ( \@contacts );
1060 } else {
1061 $self->{_error} = 1;
1062 $self->{_err_str} .= "Error: While requesting: '$res->{_request}->{_uri}'.\n";
1063 return;
1064 }
1065 }
1066
1067 sub get_ads {
1068 my ( $self ) = shift;
1069 my ( %args ) = (
1070 adkey => '',
1071 view => 'ad',
1072 search => '',
1073 start => '',
1074 @_, );
1075
1076 unless ( check_login( $self ) ) { return };
1077
1078 if ( defined( $args{ 'adkey' } ) ) {
1079 $args{ 'bb' } = $args{ 'adkey' };
1080 delete( $args{ 'adkey' } );
1081 } else {
1082 $self->{_error} = 1;
1083 $self->{_err_str} .= "Error: No addkey provided.\n";
1084 return;
1085 }
1086
1087 my $res = get_page( $self, %args );
1088 if ( $res->is_success() ) {
1089 my $ad_text = $res->content();
1090 $ad_text =~ s/\n//g;
1091 $ad_text =~ /\[(\[.*?\])\]/;
1092 $ad_text = $1;
1093 my @indv_ads = @{ extract_fields( $ad_text ) };
1094 my @ads;
1095 foreach ( @indv_ads ) {
1096 my @split_ad = @{ extract_fields( $_ ) };
1097 if ( uc( remove_quotes( $split_ad[0] ) ) eq 'A' ) {
1098 $split_ad[5] =~ s/<wbr>.*//i;
1099 my %ad_hash = (
1100 title => remove_quotes( $split_ad[2] ),
1101 body => remove_quotes( $split_ad[3] ),
1102 vendor_link => remove_quotes( $split_ad[5] ),
1103 link => remove_quotes( $split_ad[4] ), );
1104 push( @ads, \%ad_hash );
1105 } elsif ( uc( remove_quotes( $split_ad[0] ) ) eq 'RN' ) {
1106 if ( $split_ad[3] =~ /redir_url=(.*?)\"/ ) {
1107 my $vendor_link = $1;
1108 my %ad_hash = (
1109 title => remove_quotes( $split_ad[1] ),
1110 body => remove_quotes( $split_ad[2] ),
1111 vendor_link => url_unencode( $self, url => $vendor_link ),
1112 link => remove_quotes( $split_ad[3] ), );
1113 push( @ads, \%ad_hash );
1114 }
1115 } elsif ( uc( remove_quotes( $split_ad[0] ) ) eq 'RP' ) {
1116 my %ad_hash = (
1117 title => remove_quotes( $split_ad[1] ),
1118 body => remove_quotes( $split_ad[2] ),
1119 vendor_link => remove_quotes( $split_ad[4] ),
1120 link => remove_quotes( $split_ad[3] ), );
1121 push( @ads, \%ad_hash );
1122 }
1123 }
1124
1125 return( \@ads );
1126 } else {
1127 $self->{_error} = 1;
1128 $self->{_err_str} .= "Error: " . $res->status_line();
1129 }
1130
1131 return;
1132 }
1133
1134 sub url_unencode {
1135 my $self = shift;
1136 my ( %args ) = (
1137 url => '',
1138 @_,
1139 );
1140
1141 if ( $args{ 'url' } ) {
1142 $args{ 'url' } =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack( "C", hex( $1 ) )/eg;
1143 return( $args{ 'url' } );
1144 } else {
1145 $self->{_error} = 1;
1146 $self->{_err_str} .= "Error: Must supply URL to unencode.";
1147 return;
1148 }
1149 }
1150
1151 sub get_attachment {
1152 my ( $self ) = shift;
1153 my ( %args ) = (
1154 view => 'att',
1155 disp => 'attd',
1156 search => '',
1157 @_, );
1158
1159 if ( defined( $args{ 'attid' } ) && defined( $args{ 'msgid' } ) ) {
1160 $args{ 'th' } = $args{ 'msgid' };
1161 delete( $args{ 'msgid' } );
1162 } elsif ( defined( $args{ 'attachment' } ) ) {
1163 if ( defined( $args{ 'attachment' }->{ 'id' } ) ) {
1164 $args{ 'attid' } = $args{ 'attachment' }->{ 'id' };
1165 } else {
1166 $self->{_error} = 1;
1167 $self->{_err_str} .= "Error: Not a valid attachment.1\n";
1168 return;
1169 }
1170 if ( defined( $args{ 'attachment' }->{ 'th' } ) ) {
1171 $args{ 'th' } = $args{ 'attachment' }->{ 'th' };
1172 } else {
1173 $self->{_error} = 1;
1174 $self->{_err_str} .= "Error: Not a valid attachment.2\n";
1175 return;
1176 }
1177 delete( $args{ 'attachment' } );
1178 } else {
1179 $self->{_error} = 1;
1180 $self->{_err_str} .= "Error: Must supply attid and msgid or a reference to an attachment through 'attachment'.\n";
1181 return;
1182 }
1183
1184 unless ( check_login( $self ) ) { return };
1185
1186 my $res = get_page( $self, %args );
1187
1188 if ( $res->is_success() ) {
1189 my $attachment = $res->content();
1190 return( \$attachment );
1191 } else {
1192 $self->{_error} = 1;
1193 $self->{_err_str} .= "Error: While requesting attachment: '$res->{_request}->{_uri}'.\n";
1194 return;
1195 }
1196 }
1197
1198 sub update_prefs {
1199 my ( $self ) = shift;
1200 my ( %args ) = (
1201 view => 'tl',
1202 act => 'prefs',
1203 search => 'inbox',
1204 @_, );
1205
1206 unless ( check_login( $self ) ) { return };
1207
1208 $args{ 'at' } = $self->{_cookies}->{GMAIL_AT};
1209
1210 my ( %pref_mappings ) = (
1211 bx_hs => 'keyboard_shortcuts',
1212 ix_nt => 'max_page_size',
1213 bx_sc => 'indicators',
1214 sx_dn => 'display_name',
1215 bx_ns => 'snippets',
1216 sx_rt => 'reply_to',
1217 sx_sg => 'signature', );
1218
1219 my ( %pref_args ) = (
1220 view => 'pr',
1221 pnl => 'g',
1222 search => '',
1223 start => '',
1224 method => '',
1225 );
1226
1227 my $pref_res = get_page( $self, %pref_args );
1228
1229 if ( $pref_res->is_success() ) {
1230 my %functions = %{ parse_page( $self, $pref_res ) };
1231
1232 if ( $self->{_error} ) {
1233 return;
1234 }
1235
1236 unless ( defined( $functions{ 'p' } ) ) {
1237 return;
1238 }
1239
1240 ### Delete if equal to the string '' ###
1241 foreach ( 'signature', 'reply_to', 'display_name' ) {
1242 if ( defined( $args{ $_ } ) ) {
1243 if ( $args{ $_ } eq '' ) {
1244 $args{ $_ } = '%0A%0D';
1245 }
1246 }
1247 }
1248
1249 ### Load Prefs if not redefined ###
1250 foreach ( @{ $functions{ 'p' } } ) {
1251 my ( @setting ) = @{ extract_fields( $_ ) };
1252 foreach ( @setting ) {
1253 $_ = remove_quotes( $_ );
1254 }
1255 unless ( defined( $args{ $pref_mappings{ $setting[0] } } ) ) {
1256 $args{ 'p_' . $setting[0] } = $setting[1];
1257 } else {
1258 $args{ 'p_' . $setting[0] } = $args{ $pref_mappings{ $setting[0] } };
1259 }
1260 delete( $args{ $pref_mappings{ $setting[0] } } );
1261 }
1262
1263 ### Add preferences to be added ###
1264 my %rev_pref_mappings;
1265 foreach ( keys %pref_mappings ) {
1266 $rev_pref_mappings{ $pref_mappings{ $_ } } = $_;
1267 }
1268 foreach ( keys %args ) {
1269 if ( $rev_pref_mappings{ $_ } ) {
1270 $args{ 'p_' . $rev_pref_mappings{ $_ } } = $args{ $_ };
1271 delete( $args{ $_ } );
1272 }
1273 }
1274
1275 my $res = get_page( $self, %args );
1276 if ( $res->is_success() ) {
1277 my %functions = %{ parse_page( $self, $res ) };
1278 if ( @{ $functions{ 'ar' } }[0] == 1 ) {
1279 return( 1 );
1280 } else {
1281 $self->{_error} = 1;
1282 $self->{_err_str} .= "Error: While updating user preferences: '" . remove_quotes( @{ $functions{ 'ar' } }[1] ) . "'.\n";
1283 return;
1284 }
1285 } else {
1286 $self->{_error} = 1;
1287 $self->{_err_str} .= "Error: While updating user preferences: '$res->{_request}->{_uri}'.\n";
1288 return;
1289 }
1290 } else {
1291 $self->{_error} = 1;
1292 $self->{_err_str} .= "Error: While requesting user preferences: '$pref_res->{_request}->{_uri}'.\n";
1293 return;
1294 }
1295 }
1296
1297 sub recurse_slash {
1298 my ( $field ) = @_;
1299 my $count_slashes = 0;
1300 my $end_slash = 0;
1301 my $cnt = length( $field );
1302
1303 while ( ( $cnt > 0 ) && ( !$end_slash ) ){
1304 $cnt--;
1305 my $char = substr( $field, $cnt, 1 );
1306 if ( $char eq '\\' ) {
1307 if ( $count_slashes ) {
1308 $count_slashes = 0;
1309 } else {
1310 $count_slashes = 1;
1311 }
1312 } else {
1313 $end_slash = 1;
1314 }
1315 }
1316
1317 return( $count_slashes );
1318 }
1319
1320 sub extract_fields {
1321 my ( $line ) = @_;
1322 my @fields;
1323 my $in_quotes = 0;
1324 my $in_brackets = 0;
1325 my $in_brackets_quotes = 0;
1326 my $delim_count = 0;
1327 my $end_field = 0;
1328 my $field = '';
1329 my $char;
1330
1331 my $cnt;
1332 for ( $cnt=0; $cnt < length( $line ); $cnt++ ) {
1333 $char = substr( $line, $cnt, 1 );
1334 if ( $in_quotes ) {
1335 if ( ( $char eq '"' ) && ( !recurse_slash( $field ) ) ) {
1336 $in_quotes = 0;
1337 $end_field = 1;
1338 }
1339 $field .= $char;
1340 } elsif ( $in_brackets ) {
1341 if ( $in_brackets_quotes ) {
1342 if ( ( $char eq '"' ) && ( !recurse_slash( $field ) ) ) {
1343 $in_brackets_quotes = 0;
1344 }
1345 $field .= $char;
1346 } elsif ( $char eq '"' ) {
1347 $in_brackets_quotes = 1;
1348 $field .= $char;
1349 } else {
1350 if ( $char eq '[' ) {
1351 $delim_count++;
1352 $field .= $char;
1353 } elsif ( $char eq ']' ) {
1354 $delim_count--;
1355 if ( $delim_count == 0 ) {
1356 $in_brackets = 0;
1357 $end_field = 1;
1358 if ( $field eq '' ) {
1359 push( @fields, '' );
1360 }
1361 } else {
1362 $field .= $char;
1363 }
1364 } else {
1365 $field .= $char;
1366 }
1367 }
1368 } elsif ( $char eq '"' ) {
1369 $in_quotes = 1;
1370 $field .= $char;
1371 } elsif ( $char eq '[' ) {
1372 $in_brackets = 1;
1373 $delim_count = 1;
1374 } elsif ( $char ne ',' ) {
1375 $field .= $char;
1376 } elsif ( $char eq ',' ) {
1377 $end_field = 1;
1378 }
1379
1380 if ( $end_field ) {
1381 if ( $field ne '' ) {
1382 push ( @fields, $field );
1383 }
1384 $field = '';
1385 $end_field = 0;
1386 }
1387 }
1388
1389 if ( $field ne '' ) {
1390 push ( @fields, $field );
1391 }
1392 return( \@fields );
1393 }
1394
1395 sub remove_quotes {
1396 my ( $field ) = @_;
1397
1398 if ( defined( $field ) ) {
1399 $field =~ s/^"(.*)"$/$1/;
1400 }
1401
1402 return ( $field );
1403 }
1404
1405 sub cat_to_search {
1406 my ( $cat ) = @_;
1407
1408 my %REVERSE_CAT = map{ $FOLDERS{ $_ } => $_ }(keys %FOLDERS);
1409
1410 if ( defined( $REVERSE_CAT{ uc( $cat ) } ) ) {
1411 return( lc( $REVERSE_CAT{ uc( $cat ) } ) );
1412 } else {
1413 return( $cat );
1414 }
1415 }
1416
1417 sub parse_page {
1418 my ( $self, $res ) = @_;
1419
1420 if ( $res->is_success() ) {
1421 my $page;
1422 $res->content() =~ /<!--(.*)\/\/-->/s;
1423 $page = $1;
1424 my ( %functions );
1425 while ( $page =~ /D\((.*?)\);\n/mgs ) {
1426 my $line = $1;
1427 $line =~ s/\n//g;
1428 $line =~ s/^\["(.*?)",?//;
1429 my $function = $1;
1430 $line =~ s/\]$//;
1431 if ( ( uc( $function ) eq 'MI' ) || ( uc( $function ) eq 'MB' ) ) {
1432 $functions{ $function } .= "[$line],";
1433 } else {
1434 $functions{ $function } .= "$line,";
1435 }
1436 }
1437 foreach ( keys %functions ) {
1438 chop( $functions{ $_ } );
1439 my $fields = extract_fields( $functions{ $_ } );
1440 $functions{ $_ } = $fields;
1441 }
1442 return ( \%functions );
1443 } else {
1444 $self->{_error} = 1;
1445 $self->{_err_str} .= "Error: While requesting: '$res->{_request}->{_uri}'.\n";
1446 return;
1447 }
1448 }
1449
1450 1;
1451
1452 __END__
↑ Summary