File Coverage

blib/lib/App/Pocoirc/Status.pm
Criterion Covered Total %
statement 14 16 87.5
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 20 22 90.9


line stmt bran cond sub pod time code
1             package App::Pocoirc::Status;
2             BEGIN {
3 1     1   1189 $App::Pocoirc::Status::AUTHORITY = 'cpan:HINRIK';
4             }
5             {
6             $App::Pocoirc::Status::VERSION = '0.47';
7             }
8              
9 1     1   10 use strict;
  1         3  
  1         46  
10 1     1   6 use warnings FATAL => 'all';
  1         2  
  1         67  
11 1     1   7 use Carp;
  1         3  
  1         105  
12 1     1   5389 use IRC::Utils qw(decode_irc strip_color strip_formatting numeric_to_name);
  1         49158  
  1         154  
13 1     1   570 use POE::Component::IRC::Plugin qw(PCI_EAT_NONE);
  0            
  0            
14             use Scalar::Util qw(looks_like_number);
15              
16             sub new {
17             my ($package) = shift;
18             croak "$package requires an even number of arguments" if @_ & 1;
19             return bless { @_ }, $package;
20             }
21              
22             sub PCI_register {
23             my ($self, $irc, %args) = @_;
24              
25             $irc->raw_events(1);
26             $irc->plugin_register($self, 'SERVER', 'all');
27             $irc->plugin_register($self, 'USER', 'all');
28             return 1;
29             }
30              
31             sub PCI_unregister {
32             return 1;
33             }
34              
35             sub verbose {
36             my ($self, $value) = @_;
37             $self->{Verbose} = $value;
38             return;
39             }
40              
41             sub trace {
42             my ($self, $value) = @_;
43             $self->{Trace} = $value;
44             return;
45             }
46              
47             sub _normalize {
48             my ($line) = @_;
49             $line = decode_irc($line);
50             $line = strip_color($line);
51             $line = strip_formatting($line);
52             return $line;
53             }
54              
55             sub _dump {
56             my ($arg) = @_;
57              
58             if (ref $arg eq 'ARRAY') {
59             my @elems;
60             for my $elem (@$arg) {
61             push @elems, _dump($elem);
62             }
63             return '['. join(', ', @elems) .']';
64             }
65             elsif (ref $arg eq 'HASH') {
66             my @pairs;
67             for my $key (keys %$arg) {
68             push @pairs, [$key, _dump($arg->{$key})];
69             }
70             return '{'. join(', ', map { "$_->[0] => $_->[1]" } @pairs) .'}';
71             }
72             elsif (ref $arg) {
73             require overload;
74             return overload::StrVal($arg);
75             }
76             elsif (defined $arg) {
77             return $arg if looks_like_number($arg);
78             return "'".decode_irc($arg)."'";
79             }
80             else {
81             return 'undef';
82             }
83             }
84              
85             sub _event_debug {
86             my ($self, $irc, $args, $event) = @_;
87              
88             if (!defined $event) {
89             $event = (caller(1))[3];
90             $event =~ s/.*:://;
91             }
92              
93             pop @$args;
94             my @output;
95             for my $i (0..$#{ $args }) {
96             push @output, "ARG$i: " . _dump(${ $args->[$i] });
97             }
98              
99             $irc->send_event_next('irc_plugin_status', $self, 'debug', "$event: ".join(', ', @output));
100             return;
101             }
102              
103             sub S_connected {
104             my ($self, $irc) = splice @_, 0, 2;
105             my $address = ${ $_[0] };
106             $self->_event_debug($irc, \@_) if $self->{Trace};
107             $irc->send_event_next('irc_plugin_status', $self, 'normal', "Connected to server $address");
108             return PCI_EAT_NONE;
109             }
110              
111             sub S_disconnected {
112             my ($self, $irc) = splice @_, 0, 2;
113             my $server = ${ $_[0] };
114             $self->_event_debug($irc, \@_) if $self->{Trace};
115             $irc->send_event_next('irc_plugin_status', $self, 'normal', "Disconnected from server $server");
116             return PCI_EAT_NONE;
117             }
118              
119             sub S_snotice {
120             my ($self, $irc) = splice @_, 0, 2;
121             my $notice = _normalize(${ $_[0] });
122             $self->_event_debug($irc, \@_) if $self->{Trace};
123             $irc->send_event_next('irc_plugin_status', $self, 'normal', "Server notice: $notice");
124             return PCI_EAT_NONE;
125             }
126              
127             sub S_notice {
128             my ($self, $irc) = splice @_, 0, 2;
129             my $sender = _normalize(${ $_[0] });
130             my $notice = _normalize(${ $_[2] });
131              
132             $self->_event_debug($irc, \@_) if $self->{Trace};
133             if (defined $irc->server_name() && $sender ne $irc->server_name()) {
134             return PCI_EAT_NONE;
135             }
136              
137             $irc->send_event_next('irc_plugin_status', $self, 'normal', "Server notice: $notice");
138             return PCI_EAT_NONE;
139             }
140              
141             sub S_001 {
142             my ($self, $irc) = splice @_, 0, 2;
143             my $server = ${ $_[0] };
144             my $nick = $irc->nick_name();
145             my $event = 'S_001 ('.numeric_to_name('001').')';
146             $self->_event_debug($irc, \@_, $event) if $self->{Trace};
147             $irc->send_event_next('irc_plugin_status', $self, 'normal', "Logged in to server $server with nick $nick");
148             return PCI_EAT_NONE;
149             }
150              
151             sub S_identified {
152             my ($self, $irc) = splice @_, 0, 2;
153             my $nick = $irc->nick_name();
154             $self->_event_debug($irc, \@_) if $self->{Trace};
155             $irc->send_event_next('irc_plugin_status', $self, 'normal', "Identified with NickServ as $nick");
156             return PCI_EAT_NONE;
157             }
158              
159             sub S_isupport {
160             my ($self, $irc) = splice @_, 0, 2;
161             my $isupport = ${ $_[0] };
162             my $network = $isupport->isupport('NETWORK');
163             $self->_event_debug($irc, \@_) if $self->{Trace};
164              
165             if (!$self->{Dynamic} && defined $network && length $network) {
166             $irc->send_event_next('irc_network', $network);
167             }
168             return PCI_EAT_NONE;
169             }
170              
171             sub S_nick {
172             my ($self, $irc) = splice @_, 0, 2;
173             my $user = _normalize(${ $_[0] });
174             my $newnick = _normalize(${ $_[1] });
175             my $oldnick = (split /!/, $user)[0];
176              
177             $self->_event_debug($irc, \@_) if $self->{Trace};
178             return PCI_EAT_NONE if $newnick ne $irc->nick_name();
179             $irc->send_event_next('irc_plugin_status', $self, 'normal', "Nickname changed from $oldnick to $newnick");
180             return PCI_EAT_NONE;
181             }
182              
183             sub S_join {
184             my ($self, $irc) = splice @_, 0, 2;
185             my $user = _normalize(${ $_[0] });
186             my $chan = _normalize(${ $_[1] });
187             my $nick = (split /!/, $user)[0];
188              
189             $self->_event_debug($irc, \@_) if $self->{Trace};
190             return PCI_EAT_NONE if $nick ne $irc->nick_name();
191             $irc->send_event_next('irc_plugin_status', $self, 'normal', "Joined channel $chan");
192             return PCI_EAT_NONE;
193             }
194              
195             sub S_part {
196             my ($self, $irc) = splice @_, 0, 2;
197             my $user = _normalize(${ $_[0] });
198             my $chan = _normalize(${ $_[1] });
199             my $reason = ref $_[2] eq 'SCALAR' ? _normalize(${ $_[2] }) : '';
200             my $nick = (split /!/, $user)[0];
201              
202             $self->_event_debug($irc, \@_) if $self->{Trace};
203             return PCI_EAT_NONE if $nick ne $irc->nick_name();
204             my $msg = "Parted channel $chan";
205             $msg .= " ($reason)" if $reason ne '';
206             $irc->send_event_next('irc_plugin_status', $self, 'normal', $msg);
207             return PCI_EAT_NONE;
208             }
209              
210             sub S_kick {
211             my ($self, $irc) = splice @_, 0, 2;
212             my $kicker = _normalize(${ $_[0] });
213             my $chan = _normalize(${ $_[1] });
214             my $victim = _normalize(${ $_[2] });
215             my $reason = _normalize(${ $_[3] });
216             $kicker = (split /!/, $kicker)[0];
217              
218             $self->_event_debug($irc, \@_) if $self->{Trace};
219             return PCI_EAT_NONE if $victim ne $irc->nick_name();
220             my $msg = "Kicked from $chan by $kicker";
221             $msg .= " ($reason)" if length $reason;
222             $irc->send_event_next('irc_plugin_status', $self, 'normal', $msg);
223             return PCI_EAT_NONE;
224             }
225              
226             sub S_error {
227             my ($self, $irc) = splice @_, 0, 2;
228             my $error = _normalize(${ $_[0] });
229             $self->_event_debug($irc, \@_) if $self->{Trace};
230             $irc->send_event_next('irc_plugin_status', $self, 'normal', "Error from IRC server: $error");
231             return PCI_EAT_NONE;
232             }
233              
234             sub S_quit {
235             my ($self, $irc) = splice @_, 0, 2;
236             my $user = _normalize(${ $_[0] });
237             my $reason = _normalize(${ $_[1] });
238             my $nick = (split /!/, $user)[0];
239              
240             $self->_event_debug($irc, \@_) if $self->{Trace};
241             return PCI_EAT_NONE if $nick ne $irc->nick_name();
242             my $msg = 'Quit from IRC';
243             $msg .= " ($reason)" if length $reason;
244             $irc->send_event_next('irc_plugin_status', $self, 'normal', $msg);
245             return PCI_EAT_NONE;
246             }
247              
248             sub S_socketerr {
249             my ($self, $irc) = splice @_, 0, 2;
250             my $reason = _normalize(${ $_[0] });
251             $self->_event_debug($irc, \@_) if $self->{Trace};
252             $irc->send_event_next('irc_plugin_status', $self, 'normal', "Failed to connect to server: $reason");
253             return PCI_EAT_NONE;
254             }
255              
256             sub S_socks_failed {
257             my ($self, $irc) = splice @_, 0, 2;
258             my $reason = _normalize(${ $_[0] });
259             $self->_event_debug($irc, \@_) if $self->{Trace};
260             $irc->send_event_next('irc_plugin_status', $self, 'normal', "Failed to connect to SOCKS server: $reason");
261             return PCI_EAT_NONE;
262             }
263              
264             sub S_socks_rejected {
265             my ($self, $irc) = splice @_, 0, 2;
266             my $code = ${ $_[0] };
267             $self->_event_debug($irc, \@_) if $self->{Trace};
268             $irc->send_event_next('irc_plugin_status', $self, 'normal', "Connection rejected by SOCKS server (code $code)");
269             return PCI_EAT_NONE;
270             }
271              
272             sub S_raw {
273             my ($self, $irc) = splice @_, 0, 2;
274             my $raw = _normalize(${ $_[0] });
275             return PCI_EAT_NONE if !$self->{Verbose};
276             $irc->send_event_next('irc_plugin_status', $self, 'debug', "<<< $raw");
277             return PCI_EAT_NONE;
278             }
279              
280             sub S_raw_out {
281             my ($self, $irc) = splice @_, 0, 2;
282             my $raw = _normalize(${ $_[0] });
283             return PCI_EAT_NONE if !$self->{Verbose};
284             $irc->send_event_next('irc_plugin_status', $self, 'debug', ">>> $raw");
285             return PCI_EAT_NONE;
286             }
287              
288             sub _default {
289             my ($self, $irc, $event) = splice @_, 0, 3;
290             return PCI_EAT_NONE if !$self->{Trace};
291             return PCI_EAT_NONE if $event =~ /^S_plugin_/;
292              
293             if (my ($numeric) = $event =~ /^[SU]_(\d+)$/) {
294             my $name = numeric_to_name($numeric);
295             $event .= " ($name)" if defined $name;
296             }
297              
298             $self->_event_debug($irc, \@_, $event) if $self->{Trace};
299             return PCI_EAT_NONE;
300             }
301              
302             1;
303              
304             =encoding utf8
305              
306             =head1 NAME
307              
308             App::Pocoirc::Status - A PoCo-IRC plugin which logs IRC status
309              
310             =head1 DESCRIPTION
311              
312             This plugin is used internally by L. No need for
313             you to use it.
314              
315             =head1 AUTHOR
316              
317             Hinrik Ern SigurEsson, hinrik.sig@gmail.com
318              
319             =cut