File Coverage

blib/lib/POE/Component/IRC/Plugin/MultiProxy/ClientManager.pm
Criterion Covered Total %
statement 23 131 17.5
branch 0 52 0.0
condition 0 18 0.0
subroutine 9 24 37.5
pod 2 5 40.0
total 34 230 14.7


line stmt bran cond sub pod time code
1             package POE::Component::IRC::Plugin::MultiProxy::ClientManager;
2             BEGIN {
3 1     1   1305 $POE::Component::IRC::Plugin::MultiProxy::ClientManager::AUTHORITY = 'cpan:HINRIK';
4             }
5             BEGIN {
6 1     1   20 $POE::Component::IRC::Plugin::MultiProxy::ClientManager::VERSION = '0.01';
7             }
8              
9 1     1   8 use strict;
  1         2  
  1         35  
10 1     1   8 use warnings FATAL => 'all';
  1         2  
  1         40  
11 1     1   5 use Carp;
  1         1  
  1         86  
12 1     1   1071 use POE qw(Filter::Line Filter::Stackable);
  1         64886  
  1         8  
13 1     1   123871 use POE::Component::IRC::Common qw( u_irc );
  1         25695  
  1         98  
14 1     1   13 use POE::Component::IRC::Plugin qw( :ALL );
  1         2  
  1         163  
15 1     1   1078 use POE::Filter::IRCD;
  1         2150  
  1         2076  
16              
17             sub new {
18 0     0 1   my ($package, %self) = @_;
19 0           return bless \%self, $package;
20             }
21              
22             sub PCI_register {
23 0     0 0   my ($self, $irc) = @_;
24              
25 0 0         if (!$irc->isa('POE::Component::IRC::State')) {
26 0           die __PACKAGE__ . " requires PoCo::IRC::State or a subclass thereof\n";
27             }
28              
29 0           for my $plugin (qw(Recall State)) {
30 0           my $full = "POE::Component::IRC::Plugin::MultiProxy::$plugin";
31              
32 0 0         if (!grep { $_->isa($full) } values %{ $irc->plugin_list() } ) {
  0            
  0            
33 0           die __PACKAGE__ . " requires $full\n";
34             }
35             }
36              
37 0           $self->{ircd_filter} = POE::Filter::IRCD->new();
38 0           $self->{wheels} = { };
39              
40 0           ($self->{state}) = grep { $_->isa('POE::Component::IRC::Plugin::MultiProxy::State') } values %{ $irc->plugin_list() };
  0            
  0            
41 0           $self->{irc} = $irc;
42 0           $irc->raw_events(1);
43 0           $irc->plugin_register($self, 'SERVER', qw(raw));
44              
45 0           POE::Session->create(
46             object_states => [
47             $self => [ qw(_start _client_new _client_error _client_input _drop_client) ],
48             ],
49             );
50              
51 0           return 1;
52             }
53              
54             sub PCI_unregister {
55 0     0 0   my ($self, $irc) = @_;
56              
57 0           for my $id (keys %{ $self->{wheels} }) {
  0            
58 0           $poe_kernel->call("$self", '_drop_client', $id);
59             }
60 0           $poe_kernel->alias_remove("$self");
61 0           return 1;
62             }
63              
64             sub add_client {
65 0     0 1   my ($self, $socket) = @_;
66 0           $poe_kernel->call("$self", '_client_new', $socket);
67 0           return;
68             }
69              
70             sub _start {
71 0     0     my ($kernel, $self) = @_[KERNEL, OBJECT];
72 0           $kernel->alias_set("$self");
73 0           return;
74             }
75              
76             sub _client_new {
77 0     0     my ($self, $socket) = @_[OBJECT, ARG0];
78              
79 0           my $filter = POE::Filter::Stackable->new(
80             Filters => [
81             POE::Filter::Line->new(),
82             POE::Filter::IRCD->new(),
83             ]
84             );
85              
86 0           my $wheel = POE::Wheel::ReadWrite->new(
87             Handle => $socket,
88             InputFilter => $filter,
89             OutputFilter => POE::Filter::Line->new(),
90             InputEvent => '_client_input',
91             ErrorEvent => '_client_error',
92             );
93              
94 0           $self->{wheels}{$wheel->ID} = $wheel;
95              
96 0           my ($recall_plug) = grep { $_->isa('POE::Component::IRC::Plugin::MultiProxy::Recall') } values %{ $self->{irc}->plugin_list() };
  0            
  0            
97 0           $wheel->put($recall_plug->recall());
98 0           return;
99             }
100              
101             sub _client_error {
102 0     0     my ($kernel, $wheel_id) = @_[KERNEL, ARG3];
103 0           $kernel->yield('_drop_client', $wheel_id);
104 0           return;
105             }
106              
107             sub _client_input {
108 0     0     my ($kernel, $self, $input, $wheel_id) = @_[KERNEL, OBJECT, ARG0, ARG1];
109 0           my $irc = $self->{irc};
110 0           my $state = $self->{state};
111 0           my $wheel = $self->{wheels}{$wheel_id};
112              
113 0 0         if ($input->{command} eq 'QUIT') {
    0          
    0          
    0          
    0          
    0          
    0          
114 0           $kernel->yield('_drop_client', $wheel->ID);
115 0           return;
116             }
117             elsif ($input->{command} eq 'PING') {
118 0 0         $wheel->put('PONG'.(defined $input->{params}[0] ? " $input->{params}[0]" : ''));
119 0           return;
120             }
121             elsif ($input->{command} eq 'PRIVMSG') {
122 0           my ($recipient, $msg) = @{ $input->{params} }[0..1];
  0            
123 0 0         if ($recipient =~ /^[#&+!]/) {
124             # recreate channel messages from this client for
125             # other clients to see
126 0           my $line = ':' . $irc->nick_long_form($irc->nick_name()) . " PRIVMSG $recipient :$msg";
127              
128 0           for my $other (values %{ $self->{wheels} }) {
  0            
129 0 0         next if $other->ID == $wheel->ID;
130 0           $other->put($line);
131             }
132             }
133             }
134             elsif ($input->{command} eq 'WHO') {
135 0 0 0       if ($input->{params}[0] && $input->{params}[0] !~ tr/*//) {
136 0 0         if (!defined $input->{params}[1]) {
137 0 0 0       if ($input->{params}[0] !~ /^[#&+!]/ || $irc->channel_list($input->{params}[0])) {
138 0     0     $state->enqueue(sub { $self->_put($wheel->ID, @_) }, 'who_reply', $input->{params}[0]);
  0            
139 0           return;
140             }
141             }
142             }
143             }
144             elsif ($input->{command} eq 'MODE') {
145 0 0         if ($input->{params}[0]) {
146 0           my $mapping = $irc->isupport('CASEMAPPING');
147 0 0 0       if (u_irc($input->{params}[0], $mapping) eq u_irc($irc->nick_name(), $mapping)) {
    0          
148 0 0         if (!defined $input->{params}[1]) {
149 0           $wheel->put($state->mode_reply($input->{params}[0]));
150 0           return;
151             }
152             }
153             elsif ($input->{params}[0] =~ /^[#&+!]/ && $irc->channel_list($input->{params}[0])) {
154 0 0 0       if (!defined $input->{params}[1] || $input->{params}[1] =~ /^[eIb]$/) {
155 0     0     $state->enqueue(sub { $self->_put($wheel->ID, @_) }, 'mode_reply', @{ $input->{params} }[0,1]);
  0            
  0            
156 0           return;
157             }
158             }
159             }
160             }
161             elsif ($input->{command} eq 'NAMES') {
162 0 0 0       if ($irc->channel_list($input->{params}[0]) && !defined $input->{params}[1]) {
163 0     0     $state->enqueue(sub { $self->_put($wheel->ID, @_) }, 'names_reply', $input->{params}[0]);
  0            
164 0           return;
165             }
166             }
167             elsif ($input->{command} eq 'TOPIC') {
168 0 0 0       if ($irc->channel_list($input->{params}[0]) && !defined $input->{params}[1]) {
169 0     0     $state->enqueue(sub { $self->_put($wheel->ID, @_) }, 'topic_reply', $input->{params}[0]);
  0            
170 0           return;
171             }
172             }
173              
174 0           $irc->yield(quote => $input->{raw_line});
175              
176 0           return;
177             }
178              
179             sub _drop_client {
180 0     0     my ($self, $wheel_id) = @_[OBJECT, ARG0];
181 0           my $irc = $self->{irc};
182              
183 0 0         if (delete $self->{wheels}{$wheel_id}) {
184 0           $irc->send_event(irc_proxy_close => $wheel_id);
185             }
186 0           return;
187             }
188              
189             sub S_raw {
190 0     0 0   my ($self, $irc) = splice @_, 0, 2;
191 0           my $raw_line = ${ $_[0] };
  0            
192 0 0         return PCI_EAT_NONE if !keys %{ $self->{wheels} };
  0            
193              
194 0           my $input = $self->{ircd_filter}->get( [ $raw_line ] )->[0];
195 0 0         return PCI_EAT_NONE if $input->{command} !~ /^(?:PING|PONG)/;
196 0           $_->put($raw_line) for values %{ $self->{wheels} };
  0            
197 0           return PCI_EAT_NONE;
198             }
199              
200             sub _put {
201 0     0     my ($self, $wheel_id, $raw_line) = @_;
202 0 0         return if !defined $self->{wheels}{$wheel_id};
203 0           $self->{wheels}{$wheel_id}->put($raw_line);
204 0           return;
205             }
206              
207             1;
208              
209             =encoding utf8
210              
211             =head1 NAME
212              
213             POE::Compoent::IRC::Plugin::MultiProxy::ClientManager - A PoCo-IRC plugin which handles a proxy clients
214              
215             =head1 SYNOPSIS
216              
217             use POE::Compoent::IRC::Plugin::MultiProxy::ClientManager;
218              
219             $irc->plugin_add('MultiProxyClient_1', POE::Compoent::IRC::Plugin::MultiProxy::Client->new());
220              
221             =head1 DESCRIPTION
222              
223             POE::Compoent::IRC::Plugin::MultiProxy::Client is a
224             L plugin. It handles a input/output
225             and disconnects from a proxy client.
226              
227             This plugin requires the IRC component to be
228             L or a subclass thereof.
229              
230             =head1 METHODS
231              
232             =head2 C
233              
234             Takes no arguments. Returns a plugin object suitable for feeding to
235             L's C method.
236              
237             =head2 C
238              
239             Takes one argument, the socket of the new proxy client.
240              
241             =head1 AUTHOR
242              
243             Hinrik Ern SigurEsson, hinrik.sig@gmail.com
244              
245             =cut