MultiProxy.pm

filter: Statementstmt 37.1% 49 / 132 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 65.3% 17 / 26 Podpod 33.3% 1 / 3 totaltotal 33.6% 67 / 199 CC 21 SCAR 32.7
CC
21
Cov
29%
_client_input
90.0
_start
42.0
new
12.0
CRAP 26.2
« MultiProxy/Recall.pm ↑ Summary MultiProxy/Away.pm »
1 package POE::Component::IRC::Plugin::MultiProxy;
2 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 $POE::Component::IRC::Plugin::MultiProxy::AUTHORITY = 'cpan:HINRIK';
4 }
5 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 $POE::Component::IRC::Plugin::MultiProxy::VERSION = '0.01';
7 }
8  
9 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Digest::MD5 qw(md5_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use POE;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use POE::Component::IRC::Plugin::MultiProxy::Away;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use POE::Component::IRC::Plugin::MultiProxy::ClientManager;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use POE::Component::IRC::Plugin::MultiProxy::Recall;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use POE::Component::IRC::Plugin::MultiProxy::State;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use POE::Filter::IRCD;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use POE::Filter::Line;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use POE::Filter::Stackable;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use POE::Wheel::ReadWrite;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use POE::Wheel::SocketFactory;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use Socket qw(inet_ntoa);
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 1 my $CRYPT_SALT = 'erxpnUyerCerugbaNgfhW';
26  
27 0 ▶sub new {
Subroutine new: not called CC=3 SCAR=24.8
Pod documented
28 0 my ($package, %params) = @_;
29 0 my $self = bless \%params, $package;
30  
31 0 ▶ if (!defined $self->{Password}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"Password"}) 0 0 0
32 0 croak __PACKAGE__.' requires a Password argument';
33 }
34 0 ▶ if (!defined $self->{Listen_port}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"Listen_port"}) 0 0 0
35 0 croak __PACKAGE__.' requires a Listen_port argument';
36 }
37 0 return $self;
38 }
39  
40 0 ▶sub PCI_register {
Subroutine PCI_register: not called CC=3 SCAR=24.8
Pod undocumented
41 0 my ($self, $irc, %args) = @_;
42 0 $self->{net2irc}{$args{network}} = $irc;
43 0 $self->{irc2net}{$irc} = $args{network};
44  
45 $self->{plugins}{ $args{network} } = [
46 [MultiProxyState => POE::Component::IRC::Plugin::MultiProxy::State->new()],
47 [MultiProxyAway => POE::Component::IRC::Plugin::MultiProxy::Away->new(
48 Message => $self->{Away_msg}
49 )],
50 [MultiProxyRecall => POE::Component::IRC::Plugin::MultiProxy::Recall->new(
51 Mode => $self->{Recall_mode},
52 0 )],
53 [MultiProxyClientManager => POE::Component::IRC::Plugin::MultiProxy::ClientManager->new()],
54 ];
55  
56 0 for my $plugin (@{ $self->{plugins}{ $args{network} } }) {
57 0 my ($name, $object) = @$plugin;
58 0 $irc->plugin_add($name, $object);
59 }
60  
61 0 ▶ if (!$self->{registered}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"registered"}) 0 0 0
62 0 POE::Session->create(
63 object_states => [
64 $self => [qw(
65 _start
66 _client_error
67 _client_input
68 _listener_accept
69 _listener_failed
70 _shutdown
71 )],
72 ],
73 );
74 }
75  
76 0 $self->{registered}++;
77  
78 0 return 1;
79 }
80  
81 0 ▶sub PCI_unregister {
Subroutine PCI_unregister: not called CC=3 SCAR=24.8
Pod undocumented
82 0 my ($self, $irc) = @_;
83 0 my $network = delete $self->{irc2net}{$irc};
84  
85 0 $self->{registered}--;
86 0 ▶ $poe_kernel->call($self->{session_id}, '_shutdown') if !$self->{registered};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"registered"} 0 0 0
87  
88 0 for my $plugin (@{ $self->{plugins}{$network} }) {
89 0 $irc->plugin_del($plugin->[1]);
90 }
91 0 delete $self->{net2irc}{$network};
92  
93 0 return 1;
94 }
95  
96 0 ▶sub _start {
Subroutine _start: not called CC=6 SCAR=37.4
97 0 my ($self) = $_[OBJECT];
98  
99 0 $self->{session_id} = $_[SESSION]->ID;
100 $self->{filter} = POE::Filter::Stackable->new(
101 Filters => [
102 POE::Filter::Line->new(),
103 POE::Filter::IRCD->new(),
104 ],
105 0 ▶ ) if !defined $self->{filter};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"filter"} 0 0 0
106  
107 $self->{listener} = POE::Wheel::SocketFactory->new(
108 BindAddress => $self->{Listen_host},
109 BindPort => $self->{Listen_port},
110 0 SuccessEvent => '_listener_accept',
111 FailureEvent => '_listener_failed',
112 Reuse => 'yes',
113 );
114  
115 0 ▶ if (defined $self->{SSL_key} && defined $self->{SSL_cert}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"SSL_key"} and defined $self->{"SSL_cert"}) 0 0 0
Condition0%0/3
defined $self->{"SSL_key"} and defined $self->{"SSL_cert"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"SSL_key"} and defined $self->{"SSL_cert"}
defined $self->{"SSL_key"} defined $self->{"SSL_cert"}
Truth table0%0/3
A and B
A= defined $self->{"SSL_key"}B= defined $self->{"SSL_cert"}
ABresult
0X0
100
111
116 0 require POE::Component::SSLify;
117 0 POE::Component::SSLify->import(qw(Server_SSLify SSLify_Options));
118  
119 0 eval { SSLify_Options($self->{SSL_key}, $self->{SSL_cert}) };
120 0 chomp $@;
121 0 ▶ die "Unable to load SSL key ($self->{SSL_key}) or certificate ($self->{SSL_cert}): $@\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
122  
123 0 eval { $self->{listener} = Server_SSLify($self->{listener}) };
124 0 chomp $@;
125 0 ▶ die "Unable to SSLify the listener: $@\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
126 }
127  
128 0 return;
129 }
130  
131 0 ▶sub _shutdown {
Subroutine _shutdown: not called CC=2 SCAR=17.9
132 0 my ($self) = $_[OBJECT];
133 0 delete $self->{$_} for qw(wheels listener session_id);
134 0 return;
135 }
136  
137 0 ▶sub _client_error {
Subroutine _client_error: not called CC=1 SCAR=6.9
138 0 my ($self, $id) = @_[OBJECT, ARG3];
139 0 delete $self->{wheels}{$id};
140 0 return;
141 }
142  
143 0 ▶sub _client_input {
Subroutine _client_input: not called CC=9 SCAR=45.0
144 0 my ($self, $input, $id) = @_[OBJECT, ARG0, ARG1];
145 0 my $info = $self->{wheels}{$id};
146  
147 0 ▶ if ($input->{command} =~ /(PASS)/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($input->{'command'} =~ /(PASS)/) { } 0 0 0
elsif ($input->{'command'} =~ /(NICK|USER)/) { } 0 0 0
148 0 $info->{lc $1} = $input->{params}[0];
149 }
150 elsif ($input->{command} =~ /(NICK|USER)/) {
151 0 $info->{lc $1} = $input->{params}[0];
152 0 $info->{registered}++;
153 }
154  
155 0 ▶ if ($info->{registered} == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info->{"registered"} == 2) 0 0 0
156 AUTH: {
157 0 ▶ last AUTH if !defined $info->{pass};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $info->{"pass"} 0 0 0
158 0 ▶ $info->{pass} = md5_hex($info->{pass}, $CRYPT_SALT) if length $self->{Password} == 32;
Branch: 1 branch
BranchTrueFalseTotal
if length $self->{"Password"} == 32 0 0 0
159 0 ▶ last AUTH unless $info->{pass} eq $self->{Password};
Branch: 1 branch
BranchTrueFalseTotal
unless $info->{"pass"} eq $self->{"Password"} 0 0 0
160 0 ▶ last AUTH unless my $irc = $self->{net2irc}{ $info->{nick} };
Branch: 1 branch
BranchTrueFalseTotal
unless my $irc = $self->{"net2irc"}{$info->{"nick"}} 0 0 0
161  
162 0 $info->{wheel}->put("$info->{nick} NICK :".$irc->nick_name);
163 0 my $clients = $self->{plugins}{ $info->{nick} }[-1][1];
164 0 $clients->add_client($info->{socket});
165 0 $irc->send_event(irc_proxy_authed => $id);
166 0 delete $self->{wheels}{$id};
167 0 return;
168 }
169  
170 # wrong password or nick (network), dump the client
171 0 ▶ $info->{wheel}->put('ERROR :Closing Link: * [' . ( $info->{user} || 'unknown' ) . '@' . $info->{ip} . '] (Unauthorised connection)' );
Condition0%0/2
$info->{'user'} || 'unknown'
l!l
00
MC/DC0%0/1
$info->{'user'} || 'unknown'
$info->{'user'}
Truth table0%0/2
A
A= $info->{'user'}
Aresult
11
00
172 0 delete $self->{wheels}{$id};
173 }
174  
175 0 return;
176 }
177  
178 0 ▶sub _listener_failed {
Subroutine _listener_failed: not called CC=1 SCAR=6.9
179 0 my ($self, $error) = @_[OBJECT, ARG2];
180 0 warn "Failed to spawn listener: $error; aborted\n";
181 0 $poe_kernel->call($self->{session_id}, '_shutdown');
182 0 return;
183 }
184  
185 0 ▶sub _listener_accept {
Subroutine _listener_accept: not called CC=1 SCAR=6.9
186 0 my ($self, $socket, $peer_addr) = @_[OBJECT, ARG0, ARG1];
187  
188 my $wheel = POE::Wheel::ReadWrite->new(
189 Handle => $socket,
190 InputFilter => $self->{filter},
191 0 OutputFilter => POE::Filter::Line->new(),
192 InputEvent => '_client_input',
193 ErrorEvent => '_client_error',
194 );
195  
196 0 my $id = $wheel->ID();
197 0 $self->{wheels}{$id}{wheel} = $wheel;
198 0 $self->{wheels}{$id}{ip} = inet_ntoa($peer_addr);
199 0 $self->{wheels}{$id}{registered} = 0;
200 0 $self->{wheels}{$id}{socket} = $socket;
201  
202 0 return;
203 }
204  
205 1 1;
206  
207 =encoding utf8
208  
209 =head1 NAME
210  
211 POE::Component::IRC::Plugin::MultiProxy - A multi-server IRC proxy
212  
213 =head1 SYNOPSIS
214  
215 use POE::Component::IRC::Plugin::MultiProxy;
216  
217 my $proxy = POE::Component::IRC::Plugin::MultiProxy->new(
218 Listen_port = 12345,
219 Password = 'foobar',
220 );
221  
222 $irc->plugin_add(
223 MultiProxy => $proxy,
224 network => 'freenode',
225 );
226  
227 =head1 METHODS
228  
229 =head2 C<new>
230  
231 Creates a new MultiProxy plugin object. Takes the following arguments:
232  
233 B<'Password'> (required), the password you will use when connecting to the
234 proxy.
235  
236 B<'Listen_port'> (required), the port you want the proxy to listen on.
237  
238 B<'Listen_host'> (optional), the host you want the proxy to listen on.
239 Defaults to '0.0.0.0'.
240  
241 B<'Away_msg'> (optional), the away message you want to use when no clients
242 are connected.
243  
244 B<'SSL_key'>, the name of a file containing an SSL key for the listener to
245 use, if you want to enable SSL.
246  
247 B<'SSL_cert'>, the name of a file containing an SSL certificate for the
248 listener to use, if you want to enable SSL.
249  
250 B<'Recall_mode'>, how you want messages to be recalled. Available modes are:
251  
252 =over 4
253  
254 =item B<'missed'> (the default): MultiProxy will only recall the channel
255 messages you missed since the last time you detached from MultiProxy.
256  
257 =item B<'none'>: MultiProxy will not recall any channel messages.
258  
259 =item B<'all'>: MultiProxy will recall all channel messages.
260  
261 =back
262  
263 B<Note>: MultiProxy will always recall I<private messages> that you missed while
264 you were away, regardless of this option.
265  
266 =head1 TODO
267  
268 Look into using L<POE::Component::Server::IRC|POE::Component::Server::IRC> as
269 an intermediary for multiple clients.
270  
271 Keep recall messages away from prying eyes, instead of in F</tmp>.
272  
273 Add proper tests.
274  
275 =head1 AUTHOR
276  
277 Hinrik E<Ouml>rn SigurE<eth>sson, hinrik.sig@gmail.com
278  
279 =head1 LICENSE AND COPYRIGHT
280  
281 Copyright 2008-2010 Hinrik E<Ouml>rn SigurE<eth>sson
282  
283 This program is free software, you can redistribute it and/or modify
284 it under the same terms as Perl itself.
285  
286 =head1 SEE ALSO
287  
288 Other useful IRC bouncers:
289  
290 =over
291  
292 =item L<http://miau.sourceforge.net>
293  
294 =item L<http://znc.sourceforge.net>
295  
296 =item L<http://code.google.com/p/dircproxy/>
297  
298 =item L<http://www.ctrlproxy.org>
299  
300 =item L<http://www.psybnc.at>
301  
302 =item L<http://irssi.org/documentation/proxy>
303  
304 =item L<http://freshmeat.net/projects/bip>
305  
306 =back
307  
308 =cut
« MultiProxy/Recall.pm ↑ Summary MultiProxy/Away.pm »