Label/Proxy.pm

filter: Statementstmt 54.3% 69 / 127 Branchbran 19.0% 8 / 42 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 46.6% 14 / 30 Podpod 0.0% 0 / 7 totaltotal 42.9% 91 / 212 CC 24 SCAR 19.4
CC
24
Cov
45%
new
20.0
run
12.0
__ANON__
12.0
CRAP 7.0
↑ Summary Label.pm »
1 package Net::Gmail::IMAP::Label::Proxy;
2 {
3 4 $Net::Gmail::IMAP::Label::Proxy::VERSION = '0.007';
4 }
5  
6 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 use POE qw(Component::Server::TCP Component::Client::TCP
9 4 ▶ Filter::Stackable Filter::Map);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use POE::Component::SSLify qw( Client_SSLify );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Regexp::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use Encode::IMAPUTF7;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use Encode qw/decode encode_utf8/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 4 ▶use constant DEFAULT_LOCALPORT => 10143;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 ▶use constant LINESEP => "\x0D\x0A";
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶use constant GMAIL_HOST => 'imap.gmail.com';
Subroutine BEGIN: called CC=1 SCAR=0.0
19 4 ▶use constant GMAIL_PORT => 993; # IMAPS port
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 # options
22 # * localport : (0..65535) - port to start local side of proxy
23 # * verbose : (0..4) - logging level
24 sub new {
25 0 ▶ my $class = shift;
Subroutine new: not called CC=4 SCAR=30.0
Pod undocumented
26 0 ▶ ref($class) and croak "class name needed";
Branch: 1 branch
BranchTrueFalseTotal
ref $class and croak("class name needed") 0 0 0
27 0 my %opts = @_;
28 0 my $self = {};
29 0 bless $self, $class;
30 0 ▶ $self->{verbose} = $opts{verbose} // 0;
Condition0%0/2
$opts{"verbose"} // 0
l!l
00
MC/DC0%0/1
$opts{"verbose"} // 0
$opts{"verbose"}
Truth table0%0/2
A
A= $opts{"verbose"}
Aresult
11
00
31 0 ▶ $self->{localport} = $opts{localport} // DEFAULT_LOCALPORT;
Condition0%0/2
$opts{"localport"} // 10143
l!l
00
MC/DC0%0/1
$opts{"localport"} // 10143
$opts{"localport"}
Truth table0%0/2
A
A= $opts{"localport"}
Aresult
11
00
32 0 $self;
33 }
34  
35 sub run {
36 0 ▶ my ($self) = @_;
Subroutine run: not called CC=3 SCAR=24.8
Pod undocumented
37 0 ▶ $self->init() unless $self->{_init};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_init"} 0 0 0
38 0 ▶ $self->{verbose} and carp 'running';
Branch: 1 branch
BranchTrueFalseTotal
$self->{"verbose"} and carp("running") 0 0 0
39 0 $poe_kernel->run();
40 }
41  
42 # Spawn the forwarder server on port given in by localport option. When new
43 # connections arrive, spawn clients to connect them to their destination.
44 sub init {
45 0 ▶ my ($self) = @_;
Subroutine init: not called CC=1 SCAR=6.9
Pod undocumented
46 POE::Component::Server::TCP->new(
47 Port => $self->{localport},
48 ClientConnected => sub {
49 0 ▶ my ($heap, $session) = @_[HEAP, SESSION];
Subroutine __ANON__: not called CC=2 SCAR=17.9
50 0 ▶ $self->{verbose} > 0 and logevent('server got connection', $session);
Branch: 1 branch
BranchTrueFalseTotal
$self->{"verbose"} > 0 and logevent("server got connection", $session) 0 0 0
51 0 $heap->{client_id} = $self->spawn_client_side();
52 },
53 ClientFilter => POE::Filter::Stackable->new(
54 Filters => [
55 POE::Filter::Line->new( Literal => LINESEP),
56 POE::Filter::Map->new( Get => \&get_label, Put => \&put_label ),
57 ]),
58 ClientInput => sub {
59 0 ▶ my ($kernel, $session, $heap, $input) = @_[KERNEL, SESSION, HEAP, ARG0];
Subroutine __ANON__: not called CC=3 SCAR=24.8
60 0 ▶ $self->{verbose} > 2 and logevent('server got input', $session, $self->{verbose} > 3 ? $input : undef);
Branch: 2 branches
BranchTrueFalseTotal
$self->{"verbose"} > 2 and logevent("server got input", $session, $self->{"verbose"} > 3 ? $input : undef) 0 0 0
$self->{'verbose'} > 3 ? : 0 0 0
61 0 $kernel->post($heap->{client_id} => send_stuff => $input);
62 },
63 ClientDisconnected => sub {
64 0 ▶ my ($kernel, $session, $heap) = @_[KERNEL, SESSION, HEAP];
Subroutine __ANON__: not called CC=2 SCAR=17.9
65 0 ▶ $self->{verbose} > 0 and logevent('server got disconnect', $session);
Branch: 1 branch
BranchTrueFalseTotal
$self->{"verbose"} > 0 and logevent("server got disconnect", $session) 0 0 0
66 0 $kernel->post($heap->{client_id} => "shutdown");
67 },
68 InlineStates => {
69 send_stuff => sub {
70 0 ▶ my ($heap, $stuff) = @_[HEAP, ARG0];
Subroutine __ANON__: not called CC=3 SCAR=24.8
71 0 ▶ $self->{verbose} > 2 and logevent("sending to server", $_[SESSION], $self->{verbose} > 3 ? $stuff : undef );
Branch: 2 branches
BranchTrueFalseTotal
$self->{"verbose"} > 2 and logevent("sending to server", $_[1], $self->{"verbose"} > 3 ? $stuff : undef) 0 0 0
$self->{'verbose'} > 3 ? : 0 0 0
72 0 eval { $heap->{client}->put($stuff); };
73 },
74 },
75 0 );
76 0 $self->{_init} = 1; # set init flag
77 }
78  
79 sub spawn_client_side {
80 0 ▶ my ($self) = @_;
Subroutine spawn_client_side: not called CC=1 SCAR=6.9
Pod undocumented
81 POE::Component::Client::TCP->new(
82 RemoteAddress => GMAIL_HOST,
83 PreConnect => sub {
84 # Convert the socket into an SSL socket.
85 0 ▶ my $socket = eval { Client_SSLify($_[ARG0]) };
Subroutine __ANON__: not called CC=2 SCAR=17.9
86 0 ▶ return if $@; # Disconnect if SSL failed.
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
87 0 return $socket;
88 },
89 RemotePort => GMAIL_PORT, # IMAPS port
90 Filter => POE::Filter::Line->new( Literal => LINESEP),
91 Started => sub {
92 0 ▶ $_[HEAP]->{server_id} = $_[SENDER]->ID;
Subroutine __ANON__: not called CC=1 SCAR=6.9
93 },
94 Connected => sub {
95 0 ▶ my ($heap, $session) = @_[HEAP, SESSION];
Subroutine __ANON__: not called CC=2 SCAR=17.9
96 0 ▶ $self->{verbose} > 0 and logevent('client connected', $session);
Branch: 1 branch
BranchTrueFalseTotal
$self->{"verbose"} > 0 and logevent("client connected", $session) 0 0 0
97 0 eval { $heap->{server}->put(''); };
98 },
99 ServerInput => sub {
100 0 ▶ my ($kernel, $heap, $session, $input) = @_[KERNEL, HEAP, SESSION, ARG0];
Subroutine __ANON__: not called CC=3 SCAR=24.8
101 0 ▶ $self->{verbose} > 1 and logevent('client got input', $session, $self->{verbose} > 2 ? $input : undef);
Branch: 2 branches
BranchTrueFalseTotal
$self->{"verbose"} > 1 and logevent("client got input", $session, $self->{"verbose"} > 2 ? $input : undef) 0 0 0
$self->{'verbose'} > 2 ? : 0 0 0
102 # TODO: check capabilities?
103 0 $kernel->post($heap->{server_id} => send_stuff => $input);
104 },
105 Disconnected => sub {
106 0 ▶ my ($kernel, $heap, $session) = @_[KERNEL, HEAP, SESSION];
Subroutine __ANON__: not called CC=2 SCAR=17.9
107 0 ▶ $self->{verbose} > 0 and logevent('client disconnected', $session);
Branch: 1 branch
BranchTrueFalseTotal
$self->{"verbose"} > 0 and logevent("client disconnected", $session) 0 0 0
108 0 $kernel->post($heap->{server_id} => 'shutdown');
109 },
110 ConnectError => sub {
111 0 ▶ my ($operation, $error_number, $error_string) = @_[ARG0..ARG2];
Subroutine __ANON__: not called CC=1 SCAR=6.9
112 0 my $id = $_[SESSION]->ID;
113 0 print STDERR "Client $id: $operation error $error_number occurred: $error_string\n";
114 0 $_[KERNEL]->post($_[HEAP]->{server_id} => 'shutdown');
115 },
116 InlineStates => {
117 send_stuff => sub {
118 0 ▶ my ($heap, $stuff) = @_[HEAP, ARG0];
Subroutine __ANON__: not called CC=3 SCAR=24.8
119 0 ▶ $self->{verbose} > 2 and logevent("sending to client", $_[SESSION], $self->{verbose} > 3 ? $stuff : undef);
Branch: 2 branches
BranchTrueFalseTotal
$self->{"verbose"} > 2 and logevent("sending to client", $_[1], $self->{"verbose"} > 3 ? $stuff : undef) 0 0 0
$self->{'verbose'} > 3 ? : 0 0 0
120 0 eval { $heap->{server}->put($stuff); };
121 },
122 },
123 0 );
124 }
125  
126 sub logevent {
127 0 ▶ my ($state, $session, $arg) = @_;
Subroutine logevent: not called CC=2 SCAR=17.9
Pod undocumented
128 0 my $id = $session->ID();
129 0 print "session $id $state ";
130 0 ▶ print ": $arg" if (defined $arg);
Branch: 1 branch
BranchTrueFalseTotal
if defined $arg 0 0 0
131 0 print "\n";
132 }
133  
134 sub get_label {
135 4 ▶ my $data = shift;
Subroutine get_label: called CC=3 SCAR=11.0
Pod undocumented
136 4 ▶ if($data =~ /^\w+ FETCH/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($data =~ /^\w+ FETCH/) { } 1 3 4
elsif ($data =~ /^\w+ UID FETCH (\d+) \(?BODY.PEEK\[\]\)?$/) { } 2 1 3
137 1 $data =~ s,(BODY\.PEEK\[[^\]]*\]),$1 X-GM-LABELS,;
138 } elsif($data =~ /^\w+ UID FETCH (\d+) \(?BODY.PEEK\[\]\)?$/) {
139 2 $data =~ s,\(?(BODY.PEEK\[\])\)?,($1 X-GM-LABELS),;
140 }
141 4 return $data;
142 }
143  
144 sub put_label {
145 16 ▶ my $data = shift;
Subroutine put_label: called CC=3 SCAR=11.0
Pod undocumented
146 16 my $fetch_re = qr/^\* \d+ FETCH.*{\d+}$/;
147 16 my $label_re = qr/(?:[^() "]+)|$RE{delimited}{-delim=>'"'}/;
148 16 my $fetch_gm_label = qr/^(\* \d+ FETCH.*)(X-GM-LABELS \((?:(?:$label_re\s+)*$label_re)?\) ?)(.*){(\d+)}$/;
149 16 ▶ if( $data =~ $fetch_gm_label ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data =~ /$fetch_gm_label/) 14 2 16
150 14 my $octets = $4;
151 14 my $new_fetch = "$1$3";
152 #print "$new_fetch\n";
153 14 (my $x_label = $2) =~ /\((.*)\)/;
154 14 $x_label = $1;
155 14 $x_label =~ s,"\\\\Important"\s*,,;
156 14 $x_label =~ s,"\\\\Sent"\s*,,;
157 14 $x_label =~ s,"\\\\Starred"\s*,,;
158 14 $x_label =~ s,"\\\\Inbox",INBOX,;
159 14 $x_label =~ s,^\s+,,; $x_label =~ s,\s+$,,; # trim
160 # Gmail sends IMAP's modified UTF-7,
161 # need to convert to UTF-8 to satisfy
162 # <http://tools.ietf.org/html/rfc5738> in mutt
163 14 $x_label = decode('IMAP-UTF-7', $x_label);
164 14 ▶ if(length($x_label) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $x_label > 0) { } 11 3 14
165 11 $x_label = "X-Label: $x_label";
166 #print "$x_label\n";
167 11 $octets += length(encode_utf8($x_label))+length(LINESEP); # 2 more for line separator
168 11 $new_fetch .= "{$octets}";
169 11 $new_fetch .= LINESEP;
170 11 $new_fetch .= $x_label;
171 } else {
172 3 $new_fetch .= "{$octets}";
173 }
174 14 return $new_fetch;
175 }
176 2 return $data;
177 }
178  
179 4 1;
180  
181 # vim:ts=4:sw=4
↑ Summary Label.pm »