Component/IRC/Plugin/Logger.pm

filter: Statementstmt 9.1% 39 / 426 Branchbran 0.0% 0 / 84 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 14.6% 12 / 82 Podpod 7.6% 2 / 26 totaltotal 8.3% 53 / 633 CC 57 SCAR 39.0
CC
57
Cov
8%
_log_entry
272.0
PCI_register
156.0
new
20.0
CRAP 49.3
« Component/IRC/Plugin/DCC.pm ↑ Summary Component/IRC/Plugin/Console.pm »
1 package POE::Component::IRC::Plugin::Logger;
2 1 our $AUTHORITY = 'cpan:HINRIK';
3 1 $POE::Component::IRC::Plugin::Logger::VERSION = '6.92';
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Encode::Guess;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Fcntl qw(O_WRONLY O_APPEND O_CREAT);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use File::Glob ':glob';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use File::Spec::Functions qw(catdir catfile rel2abs);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use IRC::Utils qw(lc_irc parse_user strip_color strip_formatting decode_irc);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use POE::Component::IRC::Plugin qw( :ALL );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use POE::Component::IRC::Plugin::BotTraffic;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 sub new {
18 0 ▶ my ($package) = shift;
Subroutine new: not called CC=4 SCAR=30.0
Pod documented
19 0 ▶ croak "$package requires an even number of arguments" if @_ & 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ & 1 0 0 0
20 0 my %self = @_;
21  
22 0 ▶ if (!defined $self{Path} && ref $self{Log_sub} ne 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self{"Path"} and ref $self{"Log_sub"} ne "CODE") 0 0 0
Condition0%0/3
not defined $self{"Path"} and ref $self{"Log_sub"} ne "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $self{"Path"} and ref $self{"Log_sub"} ne "CODE"
not defined $self{"Path"} ref $self{"Log_sub"} ne "CODE"
Truth table0%0/3
not(A) and B
A= not defined $self{"Path"}B= ref $self{"Log_sub"} ne "CODE"
ABresult
0X0
100
111
23 0 die "$package requires a Path";
24 }
25 0 return bless \%self, $package;
26 }
27  
28 sub PCI_register {
29 0 ▶ my ($self, $irc) = @_;
Subroutine PCI_register: not called CC=12 SCAR=50.5
Pod undocumented
30  
31 0 ▶ if (!$irc->isa('POE::Component::IRC::State')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($irc->isa("POE::Component::IRC::State")) 0 0 0
32 0 die __PACKAGE__ . ' requires PoCo::IRC::State or a subclass thereof';
33 }
34  
35 0 ▶ if ( !grep { $_->isa('POE::Component::IRC::Plugin::BotTraffic') } values %{ $irc->plugin_list() } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_->isa("POE::Component::IRC::Plugin::BotTraffic");} values %{$irc->plugin_list;}) 0 0 0
36 0 $irc->plugin_add('BotTraffic', POE::Component::IRC::Plugin::BotTraffic->new());
37 }
38  
39 0 ▶ if ($self->{Restricted}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'Restricted'}) { } 0 0 0
40 0 $self->{dir_perm} = oct 700;
41 0 $self->{file_perm} = oct 600;
42 }
43 else {
44 0 $self->{dir_perm} = oct 755;
45 0 $self->{file_perm} = oct 644;
46  
47 }
48  
49 0 ▶ $self->{Path} = bsd_glob($self->{Path}) if ref $self->{Log_sub} ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->{"Log_sub"} ne "CODE" 0 0 0
50 0 ▶ if (defined $self->{Path} && ! -d $self->{Path}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"Path"} and not -d $self->{"Path"}) 0 0 0
Condition0%0/3
defined $self->{"Path"} and not -d $self->{"Path"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"Path"} and not -d $self->{"Path"}
defined $self->{"Path"} not -d $self->{"Path"}
Truth table0%0/3
A and not(B)
A= defined $self->{"Path"}B= not -d $self->{"Path"}
ABresult
0X0
100
111
51 mkdir $self->{Path}, $self->{dir_perm}
52 0 ▶ or die 'Cannot create directory ' . $self->{Path} . ": $!; aborted";
Branch: 1 branch
BranchTrueFalseTotal
mkdir $self->{"Path"}, $self->{"dir_perm"} or die 'Cannot create directory ' . $self->{"Path"} . ": $!; aborted" 0 0 0
53 0 $self->{Path} = rel2abs($self->{Path});
54 }
55  
56 0 $self->{irc} = $irc;
57 0 $self->{logging} = { };
58 0 ▶ $self->{Private} = 1 if !defined $self->{Private};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"Private"} 0 0 0
59 0 ▶ $self->{Public} = 1 if !defined $self->{Public};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"Public"} 0 0 0
60 0 ▶ $self->{DCC} = 1 if !defined $self->{DCC};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"DCC"} 0 0 0
61 0 ▶ $self->{Format} = $self->default_format() if !defined $self->{Format};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"Format"} 0 0 0
62  
63 0 $irc->plugin_register($self, 'SERVER', qw(001 332 333 chan_mode
64 ctcp_action bot_action bot_msg bot_public bot_notice join kick msg
65 nick part public notice quit topic dcc_start dcc_chat dcc_done));
66 0 $irc->plugin_register($self, 'USER', 'dcc_chat');
67 0 return 1;
68 }
69  
70 sub PCI_unregister {
71 0 ▶ return 1;
Subroutine PCI_unregister: not called CC=1 SCAR=6.9
Pod undocumented
72 }
73  
74 sub S_001 {
75 0 ▶ my ($self, $irc) = splice @_, 0, 2;
Subroutine S_001: not called CC=1 SCAR=6.9
Pod undocumented
76 0 $self->{logging} = { };
77 0 return PCI_EAT_NONE;
78 }
79  
80 sub S_332 {
81 0 ▶ my ($self, $irc) = splice @_, 0, 2;
Subroutine S_332: not called CC=2 SCAR=17.9
Pod undocumented
82 0 my $chan = decode_irc(${ $_[2] }->[0]);
83 0 my $topic = $self->_normalize(${ $_[2] }->[1]);
84  
85 # only log this if we were just joining the channel
86 0 ▶ $self->_log_entry($chan, topic_is => $chan, $topic) if !$irc->channel_list($chan);
Branch: 1 branch
BranchTrueFalseTotal
unless $irc->channel_list($chan) 0 0 0
87 0 return PCI_EAT_NONE;
88 }
89  
90 sub S_333 {
91 0 ▶ my ($self, $irc) = splice @_, 0, 2;
Subroutine S_333: not called CC=2 SCAR=17.9
Pod undocumented
92 0 my ($chan, $user, $time) = @{ ${ $_[2] } };
93 0 $chan = decode_irc($chan);
94  
95 # only log this if we were just joining the channel
96 0 ▶ $self->_log_entry($chan, topic_set_by => $chan, $user, $time) if !$irc->channel_list($chan);
Branch: 1 branch
BranchTrueFalseTotal
unless $irc->channel_list($chan) 0 0 0
97 0 return PCI_EAT_NONE;
98 }
99  
100 sub S_chan_mode {
101 0 ▶ my ($self, $irc) = splice @_, 0, 2;
Subroutine S_chan_mode: not called CC=2 SCAR=17.9
Pod undocumented
102 0 pop @_;
103 0 my $nick = parse_user(${ $_[0] });
104 0 my $chan = decode_irc(${ $_[1] });
105 0 my $mode = ${ $_[2] };
106 0 ▶ my $arg = defined $_[3] ? ${ $_[3] } : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $_[3] ? : 0 0 0
107  
108 0 $self->_log_entry($chan, $mode => $nick, $arg);
109 0 return PCI_EAT_NONE;
110 }
111  
112 sub S_ctcp_action {
113 0 ▶ my ($self, $irc) = splice @_, 0, 2;
Subroutine S_ctcp_action: not called CC=3 SCAR=24.8
Pod undocumented
114 0 my $sender = parse_user(${ $_[0] });
115 0 my $recipients = ${ $_[1] };
116 0 my $msg = $self->_normalize(${ $_[2] });
117  
118 0 for my $recipient (@{ $recipients }) {
119 0 ▶ if ($recipient eq $irc->nick_name()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($recipient eq $irc->nick_name) { } 0 0 0
120 0 $self->_log_entry($sender, action => $sender, $msg);
121 }
122 else {
123 0 $recipient = decode_irc($recipient);
124 0 $self->_log_entry($recipient, action => $sender, $msg);
125 }
126 }
127 0 return PCI_EAT_NONE;
128 }
129  
130 sub S_notice {
131 0 ▶ my ($self, $irc) = splice @_, 0, 2;
Subroutine S_notice: not called CC=3 SCAR=24.8
Pod undocumented
132 0 my $sender = parse_user(${ $_[0] });
133 0 my $targets = ${ $_[1] };
134 0 my $msg = $self->_normalize(${ $_[2] });
135  
136 0 for my $target (@{ $targets }) {
137 0 ▶ if ($target eq $irc->nick_name()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($target eq $irc->nick_name) { } 0 0 0
138 0 $self->_log_entry($sender, notice => $sender, $msg);
139 }
140 else {
141 0 $target = decode_irc($target);
142 0 $self->_log_entry($target, notice => $sender, $msg);
143 }
144 }
145 0 return PCI_EAT_NONE;
146 }
147  
148  
149 sub S_bot_action {
150 0 ▶ my ($self, $irc) = splice @_, 0, 2;
Subroutine S_bot_action: not called CC=2 SCAR=17.9
Pod undocumented
151 0 my $recipients = ${ $_[0] };
152 0 my $msg = $self->_normalize(${ $_[1] });
153  
154 0 for my $recipient (@{ $recipients }) {
155 0 $recipient = decode_irc($recipient);
156 0 $self->_log_entry($recipient, action => $irc->nick_name(), $msg);
157 }
158 0 return PCI_EAT_NONE;
159 }
160  
161 sub S_bot_msg {
162 0 ▶ my ($self, $irc) = splice @_, 0, 2;
Subroutine S_bot_msg: not called CC=2 SCAR=17.9
Pod undocumented
163 0 my $recipients = ${ $_[0] };
164 0 my $msg = $self->_normalize(${ $_[1] });
165  
166 0 for my $recipient (@{ $recipients }) {
167 0 $self->_log_entry($recipient, privmsg => $irc->nick_name(), $msg);
168 }
169 0 return PCI_EAT_NONE;
170 }
171  
172 sub S_bot_public {
173 0 ▶ my ($self, $irc) = splice @_, 0, 2;
Subroutine S_bot_public: not called CC=2 SCAR=17.9
Pod undocumented
174 0 my $channels = ${ $_[0] };
175 0 my $msg = $self->_normalize(${ $_[1] });
176  
177 0 for my $chan (@{ $channels }) {
178 0 $chan = decode_irc($chan);
179 0 $self->_log_entry($chan, privmsg => $irc->nick_name(), $msg);
180 }
181 0 return PCI_EAT_NONE;
182 }
183  
184 sub S_bot_notice {
185 0 ▶ my ($self, $irc) = splice @_, 0, 2;
Subroutine S_bot_notice: not called CC=2 SCAR=17.9
Pod undocumented
186 0 my $targets = ${ $_[0] };
187 0 my $msg = $self->_normalize(${ $_[1] });
188  
189 0 for my $target (@{ $targets }) {
190 0 $target = decode_irc($target);
191 0 $self->_log_entry($target, notice => $irc->nick_name(), $msg);
192 }
193 0 return PCI_EAT_NONE;
194 }
195  
196 sub S_join {
197 0 ▶ my ($self, $irc) = splice @_, 0, 2;
Subroutine S_join: not called CC=1 SCAR=6.9
Pod undocumented
198 0 my ($joiner, $user, $host) = parse_user(${ $_[0] });
199 0 my $chan = decode_irc(${ $_[1] });
200  
201 0 $self->_log_entry($chan, join => $joiner, "$user\@$host", $chan);
202 0 return PCI_EAT_NONE;
203 }
204  
205 sub S_kick {
206 0 ▶ my ($self, $irc) = splice @_, 0, 2;
Subroutine S_kick: not called CC=1 SCAR=6.9
Pod undocumented
207 0 my $kicker = parse_user(${ $_[0] });
208 0 my $chan = decode_irc(${ $_[1] });
209 0 my $victim = ${ $_[2] };
210 0 my $msg = $self->_normalize(${ $_[3] });
211  
212 0 $self->_log_entry($chan, kick => $kicker, $victim, $chan, $msg);
213 0 return PCI_EAT_NONE;
214 }
215  
216 sub S_msg {
217 0 ▶ my ($self, $irc) = splice @_, 0, 2;
Subroutine S_msg: not called CC=1 SCAR=6.9
Pod undocumented
218 0 my $sender = parse_user(${ $_[0] });
219 0 my $msg = $self->_normalize(${ $_[2] });
220  
221 0 $self->_log_entry($sender, privmsg => $sender, $msg);
222 0 return PCI_EAT_NONE;
223 }
224  
225 sub S_nick {
226 0 ▶ my ($self, $irc) = splice @_, 0, 2;
Subroutine S_nick: not called CC=2 SCAR=17.9
Pod undocumented
227 0 my $old_nick = parse_user(${ $_[0] });
228 0 my $new_nick = ${ $_[1] };
229 0 my $channels = ${ $_[2] };
230  
231 0 for my $chan (@{ $channels }) {
232 0 $chan = decode_irc($chan);
233 0 $self->_log_entry($chan, nick_change => $old_nick, $new_nick);
234 }
235 0 return PCI_EAT_NONE;
236 }
237  
238 sub S_part {
239 0 ▶ my ($self, $irc) = splice @_, 0, 2;
Subroutine S_part: not called CC=2 SCAR=17.9
Pod undocumented
240 0 my ($parter, $user, $host) = parse_user(${ $_[0] });
241 0 my $chan = decode_irc(${ $_[1] });
242 0 ▶ my $msg = ref $_[2] eq 'SCALAR' ? ${ $_[2] } : '';
Branch: 1 branch
BranchTrueFalseTotal
ref $_[2] eq 'SCALAR' ? : 0 0 0
243 0 $msg = $self->_normalize($msg);
244  
245 0 $self->_log_entry($chan, part => $parter, "$user\@$host", $chan, $msg);
246 0 return PCI_EAT_NONE;
247 }
248  
249 sub S_public {
250 0 ▶ my ($self, $irc) = splice @_, 0, 2;
Subroutine S_public: not called CC=2 SCAR=17.9
Pod undocumented
251 0 my $sender = parse_user(${ $_[0] });
252 0 my $channels = ${ $_[1] };
253 0 my $msg = $self->_normalize(${ $_[2] });
254  
255 0 for my $chan (@{ $channels }) {
256 0 $chan = decode_irc($chan);
257 0 $self->_log_entry($chan, privmsg => $sender, $msg);
258 }
259 0 return PCI_EAT_NONE;
260 }
261  
262 sub S_quit {
263 0 ▶ my ($self, $irc) = splice @_, 0, 2;
Subroutine S_quit: not called CC=2 SCAR=17.9
Pod undocumented
264 0 my ($quitter, $user, $host) = parse_user(${ $_[0] });
265 0 my $msg = $self->_normalize(${ $_[1] });
266 0 my $channels = ${ $_[2] };
267  
268 0 for my $chan (@{ $channels }) {
269 0 $chan = decode_irc($chan);
270 0 $self->_log_entry($chan, quit => $quitter, "$user\@$host", $msg);
271 }
272 0 return PCI_EAT_NONE;
273 }
274  
275 sub S_topic {
276 0 ▶ my ($self, $irc) = splice @_, 0, 2;
Subroutine S_topic: not called CC=1 SCAR=6.9
Pod undocumented
277 0 my $changer = parse_user(${ $_[0] });
278 0 my $chan = decode_irc(${ $_[1] });
279 0 my $new_topic = $self->_normalize(${ $_[2] });
280  
281 0 $self->_log_entry($chan, topic_change => $changer, $new_topic);
282 0 return PCI_EAT_NONE;
283 }
284  
285 sub S_dcc_start {
286 0 ▶ my ($self, $irc) = splice @_, 0, 2;
Subroutine S_dcc_start: not called CC=2 SCAR=17.9
Pod undocumented
287 0 my $nick = ${ $_[1] };
288 0 my $type = ${ $_[2] };
289 0 my $port = ${ $_[3] };
290 0 my $addr = ${ $_[6] };
291  
292 0 ▶ return PCI_EAT_NONE if $type ne 'CHAT';
Branch: 1 branch
BranchTrueFalseTotal
if $type ne "CHAT" 0 0 0
293 0 $self->_log_entry("=$nick", dcc_start => $nick, "$addr:$port");
294 0 return PCI_EAT_NONE;
295 }
296  
297 sub S_dcc_chat {
298 0 ▶ my ($self, $irc) = splice @_, 0, 2;
Subroutine S_dcc_chat: not called CC=2 SCAR=17.9
Pod undocumented
299 0 my $nick = ${ $_[1] };
300 0 my $msg = $self->_normalize(${ $_[3] });
301  
302 0 ▶ if (my ($action) = $msg =~ /\001ACTION (.*?)\001/) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($action) = $msg =~ /\001ACTION (.*?)\001/) { } 0 0 0
303 0 $self->_log_entry("=$nick", action => $nick, $action);
304 }
305 else {
306 0 $self->_log_entry("=$nick", privmsg => $nick, $msg);
307 }
308 0 return PCI_EAT_NONE;
309 }
310  
311 sub U_dcc_chat {
312 0 ▶ my ($self, $irc) = splice @_, 0, 2;
Subroutine U_dcc_chat: not called CC=4 SCAR=30.0
Pod undocumented
313 0 pop @_;
314 0 my ($id, @lines) = @_;
315 0 $_ = $$_ for @lines;
316 0 my $me = $irc->nick_name();
317  
318 0 my ($dcc) = grep { $_->isa('POE::Component::IRC::Plugin::DCC') } values %{ $irc->plugin_list() };
319 0 my $info = $dcc->dcc_info($$id);
320 0 my $nick = $info->{nick};
321  
322 0 for my $msg (@lines) {
323 0 $msg = $self->_normalize($msg);
324 0 ▶ if (my ($action) = $msg =~ /\001ACTION (.*?)\001/) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($action) = $msg =~ /\001ACTION (.*?)\001/) { } 0 0 0
325 0 $self->_log_entry("=$nick", action => $me, $action);
326 }
327 else {
328 0 $self->_log_entry("=$nick", privmsg => $me, $msg);
329 }
330 }
331 0 return PCI_EAT_NONE;
332 }
333  
334 sub S_dcc_done {
335 0 ▶ my ($self, $irc) = splice @_, 0, 2;
Subroutine S_dcc_done: not called CC=2 SCAR=17.9
Pod undocumented
336 0 my $nick = ${ $_[1] };
337 0 my $type = ${ $_[2] };
338 0 my $port = ${ $_[3] };
339 0 my $addr = ${ $_[7] };
340  
341 0 ▶ return PCI_EAT_NONE if $type ne 'CHAT';
Branch: 1 branch
BranchTrueFalseTotal
if $type ne "CHAT" 0 0 0
342 0 $self->_log_entry("=$nick", dcc_done => $nick, "$addr:$port");
343 0 return PCI_EAT_NONE;
344 }
345  
346 sub _log_entry {
347 0 ▶ my ($self, $context, $type, @args) = @_;
Subroutine _log_entry: not called CC=16 SCAR=56.1
348 0 my ($date, $time) = split / /, (strftime '%Y-%m-%d %H:%M:%S ', localtime);
349 0 $context = lc_irc $context, $self->{irc}->isupport('CASEMAPPING');
350 0 ▶ my $chantypes = join('', @{ $self->{irc}->isupport('CHANTYPES') || ['#', '&']});
Branch: 1 branch
BranchTrueFalseTotal
$self->{"irc"}->isupport("CHANTYPES") or ["#", "&"] 0 0 0
351  
352 0 ▶ if ($context =~ /^[$chantypes]/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($context =~ /^[$chantypes]/) { } 0 0 0
elsif ($context =~ /^=/) { } 0 0 0
353 0 ▶ return if !$self->{Public};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"Public"} 0 0 0
354 }
355 elsif ($context =~ /^=/) {
356 0 ▶ return if !$self->{DCC};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"DCC"} 0 0 0
357 }
358 else {
359 0 ▶ return if !$self->{Private};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"Private"} 0 0 0
360 }
361  
362 0 ▶ return if $type eq 'notice' && !$self->{Notices};
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "notice" and not $self->{"Notices"} 0 0 0
Condition0%0/3
$type eq "notice" and not $self->{"Notices"}
!ll&&!rl&&r
000
MC/DC0%0/2
$type eq "notice" and not $self->{"Notices"}
$type eq "notice" not $self->{"Notices"}
Truth table0%0/3
A and not(B)
A= $type eq "notice"B= not $self->{"Notices"}
ABresult
0X0
100
111
363  
364 0 ▶ if (ref $self->{Log_sub} eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{"Log_sub"} eq "CODE") 0 0 0
365 0 $self->{Log_sub}->($context, $type, @args);
366 0 return;
367 }
368  
369 0 ▶ return if !defined $self->{Format}->{$type};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"Format"}{$type} 0 0 0
370  
371 # slash is problematic in a filename, replace it with underscore
372 0 $context =~ s!/!_!g;
373  
374 0 my $log_file;
375 0 ▶ if ($self->{Sort_by_date}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'Sort_by_date'}) { } 0 0 0
376 0 my $log_dir = catdir($self->{Path}, $context);
377 0 ▶ if (! -d $log_dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $log_dir) 0 0 0
378 mkdir $log_dir, $self->{dir_perm}
379 0 ▶ or die "Couldn't create directory $log_dir: $!; aborted";
Branch: 1 branch
BranchTrueFalseTotal
mkdir $log_dir, $self->{"dir_perm"} or die "Couldn't create directory ${log_dir}: $!; aborted" 0 0 0
380 }
381 0 $log_file = catfile($self->{Path}, $context, "$date.log");
382 }
383 else {
384 0 $log_file = catfile($self->{Path}, "$context.log");
385 }
386  
387 0 $log_file = $self->_open_log($log_file);
388  
389 0 ▶ if (!$self->{logging}->{$context}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"logging"}{$context}) 0 0 0
390 0 print $log_file "***\n*** LOGGING BEGINS\n***\n";
391 0 $self->{logging}->{$context} = 1;
392 }
393 0 my $line = "$time " . $self->{Format}->{$type}->(@args);
394 0 ▶ $line = "$date $line" if !$self->{Sort_by_date};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"Sort_by_date"} 0 0 0
395 0 print $log_file $line, "\n";
396 0 return;
397 }
398  
399 sub _open_log {
400 0 ▶ my ($self, $file_name) = @_;
Subroutine _open_log: not called CC=2 SCAR=17.9
401 sysopen(my $log, $file_name, O_WRONLY|O_APPEND|O_CREAT, $self->{file_perm})
402 0 ▶ or die "Couldn't open or create file '$file_name': $!; aborted";
Branch: 1 branch
BranchTrueFalseTotal
sysopen my $log, $file_name, 1089, $self->{"file_perm"} or die "Couldn't open or create file '${file_name}': $!; aborted" 0 0 0
403 0 binmode($log, ':encoding(utf8)');
404 0 $log->autoflush(1);
405 0 return $log;
406 }
407  
408 sub _normalize {
409 0 ▶ my ($self, $line) = @_;
Subroutine _normalize: not called CC=3 SCAR=24.8
410 0 $line = decode_irc($line);
411 0 ▶ $line = strip_color($line) if $self->{Strip_color};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Strip_color"} 0 0 0
412 0 ▶ $line = strip_formatting($line) if $self->{Strip_formatting};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Strip_formatting"} 0 0 0
413 0 return $line;
414 }
415  
416 sub default_format {
417 return {
418 0 ▶ '+b' => sub { my ($nick, $mask) = @_; "--- $nick sets ban on $mask" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
419 0 ▶ '-b' => sub { my ($nick, $mask) = @_; "--- $nick removes ban on $mask" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
420 0 ▶ '+e' => sub { my ($nick, $mask) = @_; "--- $nick sets exempt on $mask" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
421 0 ▶ '-e' => sub { my ($nick, $mask) = @_; "--- $nick removes exempt on $mask" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
422 0 ▶ '+I' => sub { my ($nick, $mask) = @_; "--- $nick sets invite on $mask" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
423 0 ▶ '-I' => sub { my ($nick, $mask) = @_; "--- $nick removes invite on $mask" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
424 0 ▶ '+h' => sub { my ($nick, $subject) = @_; "--- $nick gives channel half-operator status to $subject" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
425 0 ▶ '-h' => sub { my ($nick, $subject) = @_; "--- $nick removes channel half-operator status from $subject" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
426 0 ▶ '+o' => sub { my ($nick, $subject) = @_; "--- $nick gives channel operator status to $subject" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
427 0 ▶ '-o' => sub { my ($nick, $subject) = @_; "--- $nick removes channel operator status from $subject" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
428 0 ▶ '+v' => sub { my ($nick, $subject) = @_; "--- $nick gives voice to $subject" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
429 0 ▶ '-v' => sub { my ($nick, $subject) = @_; "--- $nick removes voice from $subject" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
430 0 ▶ '+k' => sub { my ($nick, $key) = @_; "--- $nick sets channel keyword to $key" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
431 0 ▶ '-k' => sub { my ($nick) = @_; "--- $nick removes channel keyword" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
432 0 ▶ '+l' => sub { my ($nick, $limit) = @_; "--- $nick sets channel user limit to $limit" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
433 0 ▶ '-l' => sub { my ($nick) = @_; "--- $nick removes channel user limit" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
434 0 ▶ '+i' => sub { my ($nick) = @_; "--- $nick enables invite-only channel status" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
435 0 ▶ '-i' => sub { my ($nick) = @_; "--- $nick disables invite-only channel status" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
436 0 ▶ '+m' => sub { my ($nick) = @_; "--- $nick enables channel moderation" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
437 0 ▶ '-m' => sub { my ($nick) = @_; "--- $nick disables channel moderation" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
438 0 ▶ '+n' => sub { my ($nick) = @_; "--- $nick disables external messages" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
439 0 ▶ '-n' => sub { my ($nick) = @_; "--- $nick enables external messages" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
440 0 ▶ '+p' => sub { my ($nick) = @_; "--- $nick enables private channel status" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
441 0 ▶ '-p' => sub { my ($nick) = @_; "--- $nick disables private channel status" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
442 0 ▶ '+s' => sub { my ($nick) = @_; "--- $nick enables secret channel status" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
443 0 ▶ '-s' => sub { my ($nick) = @_; "--- $nick disables secret channel status" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
444 0 ▶ '+t' => sub { my ($nick) = @_; "--- $nick enables topic protection" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
445 0 ▶ '-t' => sub { my ($nick) = @_; "--- $nick disables topic protection" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
446 0 ▶ nick_change => sub { my ($old_nick, $new_nick) = @_; "--- $old_nick is now known as $new_nick" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
447 0 ▶ topic_is => sub { my ($chan, $topic) = @_; "--- Topic for $chan is: $topic" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
448 0 ▶ topic_change => sub { my ($nick, $topic) = @_; "--- $nick changes the topic to: $topic" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
449 0 ▶ privmsg => sub { my ($nick, $msg) = @_; "<$nick> $msg" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
450 0 ▶ notice => sub { my ($nick, $msg) = @_; ">$nick< $msg" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
451 0 ▶ action => sub { my ($nick, $action) = @_; "* $nick $action" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
452 0 ▶ dcc_start => sub { my ($nick, $address) = @_; "--> Opened DCC chat connection with $nick ($address)" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
453 0 ▶ dcc_done => sub { my ($nick, $address) = @_; "<-- Closed DCC chat connection with $nick ($address)" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
454 0 ▶ join => sub { my ($nick, $userhost, $chan) = @_; "--> $nick ($userhost) joins $chan" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
455 part => sub {
456 0 ▶ my ($nick, $userhost, $chan, $msg) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
457 0 my $line = "<-- $nick ($userhost) leaves $chan";
458 0 ▶ $line .= " ($msg)" if $msg ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $msg ne "" 0 0 0
459 0 return $line;
460 },
461 quit => sub {
462 0 ▶ my ($nick, $userhost, $msg) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
463 0 my $line = "<-- $nick ($userhost) quits";
464 0 ▶ $line .= " ($msg)" if $msg ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $msg ne "" 0 0 0
465 0 return $line;
466 },
467 kick => sub {
468 0 ▶ my ($kicker, $victim, $chan, $msg) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
469 0 my $line = "<-- $kicker kicks $victim from $chan";
470 0 ▶ $line .= " ($msg)" if $msg ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $msg ne "" 0 0 0
471 0 return $line;
472 },
473 topic_set_by => sub {
474 0 ▶ my ($chan, $user, $time) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
475 0 my $date = localtime $time;
476 0 return "--- Topic for $chan was set by $user at $date";
477 },
478 }
479 0 ▶}
Subroutine default_format: not called CC=1 SCAR=6.9
Pod documented
480  
481 1 1;
482  
483 =encoding utf8
484  
485 =head1 NAME
486  
487 POE::Component::IRC::Plugin::Logger - A PoCo-IRC plugin which
488 logs public, private, and DCC chat messages to disk
489  
490 =head1 SYNOPSIS
491  
492 use POE::Component::IRC::Plugin::Logger;
493  
494 $irc->plugin_add('Logger', POE::Component::IRC::Plugin::Logger->new(
495 Path => '/home/me/irclogs',
496 DCC => 0,
497 Private => 0,
498 Public => 1,
499 ));
500  
501 =head1 DESCRIPTION
502  
503 POE::Component::IRC::Plugin::Logger is a L<POE::Component::IRC|POE::Component::IRC>
504 plugin. It logs messages and CTCP ACTIONs to either F<#some_channel.log> or
505 F<some_nickname.log> in the supplied path. In the case of DCC chats, a '=' is
506 prepended to the nickname (like in irssi).
507  
508 The plugin tries to detect UTF-8 encoding of every message or else falls back
509 to CP1252, like irssi (and, supposedly, mIRC) does by default. Resulting log
510 files will be UTF-8 encoded. The default log format is similar to xchat's,
511 except that it's sane and parsable.
512  
513 This plugin requires the IRC component to be L<POE::Component::IRC::State|POE::Component::IRC::State>
514 or a subclass thereof. It also requires a L<POE::Component::IRC::Plugin::BotTraffic|POE::Component::IRC::Plugin::BotTraffic>
515 to be in the plugin pipeline. It will be added automatically if it is not
516 present.
517  
518 =head1 METHODS
519  
520 =head2 C<new>
521  
522 Arguments:
523  
524 B<'Path'>, the place where you want the logs saved.
525  
526 B<'Private'>, whether or not to log private messages. Defaults to 1.
527  
528 B<'Public'>, whether or not to log public messages. Defaults to 1.
529  
530 B<'DCC'>, whether or not to log DCC chats. Defaults to 1.
531  
532 B<'Notices'>, whether or not to log NOTICEs. Defaults to 0.
533  
534 B<'Sort_by_date'>, whether or not to split log files by date, i.e.
535 F<#channel/YYYY-MM-DD.log> instead of F<#channel.log>. If enabled, the date
536 will be omitted from the timestamp. Defaults to 0.
537  
538 B<'Strip_color'>, whether or not to strip all color codes from messages. Defaults
539 to 0.
540  
541 B<'Strip_formatting'>, whether or not to strip all formatting codes from messages.
542 Defaults to 0.
543  
544 B<'Restricted'>, set this to 1 if you want all directories/files to be created
545 without read permissions for other users (i.e. 700 for dirs and 600 for files).
546 Defaults to 1.
547  
548 B<'Format'>, a hash reference representing the log format, if you want to define
549 your own. See the source for details.
550  
551 B<'Log_sub'>, a subroutine reference which can be used to override the file
552 logging. Use this if you want to store logs in a database instead, for
553 example. It will be called with 3 arguments: the context (a channel name or
554 nickname), a type (e.g. 'privmsg' or '+b', and any arguments to that type.
555 You can make use L</default_format> to create logs that match the default
556 log format. B<Note:> You must take care of handling date/time and stripping
557 colors/formatting codes yourself.
558  
559 Returns a plugin object suitable for feeding to
560 L<POE::Component::IRC|POE::Component::IRC>'s C<plugin_add> method.
561  
562 =head2 C<default_format>
563  
564 Returns a hash reference of type/subroutine pairs, for formatting logs
565 according to the default log format.
566  
567 =head1 AUTHOR
568  
569 Hinrik E<Ouml>rn SigurE<eth>sson, hinrik.sig@gmail.com
570  
571 =cut
« Component/IRC/Plugin/DCC.pm ↑ Summary Component/IRC/Plugin/Console.pm »