Component/IRC/Plugin/FollowTail.pm

filter: Statementstmt 39.3% 24 / 61 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 46.6% 7 / 15 Podpod 33.3% 1 / 3 totaltotal 34.0% 32 / 94 CC 8 SCAR 22.8
CC
8
Cov
32%
new
30.0
_start
12.0
_shutdown
6.0
CRAP 9.8
« Component/IRC/Plugin/CycleEmpty.pm ↑ Summary Component/IRC/Plugin/NickServID.pm »
1 package POE::Component::IRC::Plugin::FollowTail;
2 1 our $AUTHORITY = 'cpan:HINRIK';
3 1 $POE::Component::IRC::Plugin::FollowTail::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 File::Glob ':glob';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use File::Spec::Functions 'rel2abs';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use POE qw(Wheel::FollowTail);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use POE::Component::IRC::Plugin qw( :ALL );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub new {
13 0 ▶ my ($package) = shift;
Subroutine new: not called CC=5 SCAR=34.0
Pod documented
14 0 ▶ croak "$package requires an even number of arguments" if @_ & 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ & 1 0 0 0
15 0 my %args = @_;
16 0 $args{lc $_} = delete $args{$_} for keys %args;
17  
18 0 ▶ die "$package requires a 'filename' attribute" if !defined $args{filename};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"filename"} 0 0 0
19 0 $args{filename} = bsd_glob($args{filename});
20 0 ▶ die "File '$args{filename}' does not exist" if !-e $args{filename};
Branch: 1 branch
BranchTrueFalseTotal
unless -e $args{"filename"} 0 0 0
21 0 $args{filename} = rel2abs($args{filename});
22  
23 0 return bless \%args, $package;
24 }
25  
26 sub PCI_register {
27 0 ▶ my ($self, $irc) = splice @_, 0, 2;
Subroutine PCI_register: not called CC=1 SCAR=6.9
Pod undocumented
28 0 $self->{irc} = $irc;
29 0 POE::Session->create(
30 object_states => [
31 $self => [ qw(_start _shutdown _input _error _reset) ],
32 ],
33 );
34  
35 0 return 1;
36 }
37  
38 sub PCI_unregister {
39 0 ▶ my ($self, $irc) = splice @_, 0, 2;
Subroutine PCI_unregister: not called CC=1 SCAR=6.9
Pod undocumented
40 0 delete $self->{irc};
41 0 $poe_kernel->post( $self->{session_id} => '_shutdown' );
42 0 $poe_kernel->refcount_decrement( $self->{session_id}, __PACKAGE__ );
43 0 return 1;
44 }
45  
46 sub _start {
47 0 ▶ my ($kernel, $self) = @_[KERNEL, OBJECT];
Subroutine _start: not called CC=3 SCAR=24.8
48  
49 0 $self->{session_id} = $_[SESSION]->ID();
50 0 $kernel->refcount_increment( $self->{session_id}, __PACKAGE__ );
51  
52 $self->{wheel} = POE::Wheel::FollowTail->new(
53 Filename => $self->{filename},
54 InputEvent => '_input',
55 ErrorEvent => '_error',
56 ResetEvent => '_reset',
57 ( defined $self->{filter} && $self->{filter}->isa('POE::Filter')
58 ? ( Filter => $self->{filter} )
59 0 ▶ : ()
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'filter'} && $self->{'filter'}->isa('POE::Filter') ? : 0 0 0
Condition0%0/3
defined $self->{"filter"} and $self->{"filter"}->isa("POE::Filter")
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"filter"} and $self->{"filter"}->isa("POE::Filter")
defined $self->{"filter"} $self->{"filter"}->isa("POE::Filter")
Truth table0%0/3
A and B
A= defined $self->{"filter"}B= $self->{"filter"}->isa("POE::Filter")
ABresult
0X0
100
111
60 ),
61 );
62  
63 0 return;
64 }
65  
66 sub _shutdown {
67 0 ▶ my ($kernel, $self, $term) = @_[KERNEL, OBJECT, ARG0];
Subroutine _shutdown: not called CC=2 SCAR=17.9
68 0 delete $self->{wheel};
69 0 ▶ $kernel->refcount_decrement( $self->{session_id}, __PACKAGE__ ) if $term;
Branch: 1 branch
BranchTrueFalseTotal
if $term 0 0 0
70 0 return;
71 }
72  
73 sub _input {
74 0 ▶ my ($kernel, $self, $input) = @_[KERNEL, OBJECT, ARG0];
Subroutine _input: not called CC=1 SCAR=6.9
75 0 $self->{irc}->send_event( 'irc_tail_input', $self->{filename}, $input );
76 0 return;
77 }
78  
79 sub _error {
80 0 ▶ my ($kernel, $self) = @_[KERNEL, OBJECT];
Subroutine _error: not called CC=1 SCAR=6.9
81 0 $self->{irc}->send_event( 'irc_tail_error', $self->{filename}, @_[ARG0..ARG2] );
82 0 $kernel->yield('_shutdown','TERM');
83 0 return;
84 }
85  
86 sub _reset {
87 0 ▶ my ($kernel, $self) = @_[KERNEL, OBJECT];
Subroutine _reset: not called CC=1 SCAR=6.9
88 0 $self->{irc}->send_event( 'irc_tail_reset', $self->{filename} );
89 0 return;
90 }
91  
92 1 1;
93  
94 =encoding utf8
95  
96 =head1 NAME
97  
98 POE::Component::IRC::Plugin::FollowTail - A PoCo-IRC plugin to follow the tail
99 of an ever-growing file
100  
101 =head1 SYNOPSIS
102  
103 use POE qw(Component::IRC Component::IRC::Plugin::FollowTail);
104  
105 my $nickname = 'Flibble' . $$;
106 my $ircname = 'Flibble the Sailor Bot';
107 my $ircserver = 'irc.blahblahblah.irc';
108 my $filename = '/some/such/file/here';
109 my @channels = ( '#Blah', '#Foo', '#Bar' );
110  
111 my $irc = POE::Component::IRC->spawn(
112 nick => $nickname,
113 server => $ircserver,
114 port => $port,
115 ircname => $ircname,
116 ) or die "Oh noooo! $!";
117  
118 POE::Session->create(
119 package_states => [
120 main => [ qw(_start irc_001 irc_tail_input irc_tail_error irc_tail_reset) ],
121 ],
122 );
123  
124 $poe_kernel->run();
125  
126 sub _start {
127 $irc->plugin_add( 'FollowTail' => POE::Component::IRC::Plugin::FollowTail->new(
128 filename => $filename,
129 ));
130 $irc->yield( register => 'all' );
131 $irc->yield( connect => { } );
132 return;
133 }
134  
135 sub irc_001 {
136 $irc->yield( join => $_ ) for @channels;
137 return;
138 }
139  
140 sub irc_tail_input {
141 my ($kernel, $sender, $filename, $input) = @_[KERNEL, SENDER, ARG0, ARG1];
142 $kernel->post( $sender, 'privmsg', $_, "$filename: $input" ) for @channels;
143 return;
144 }
145  
146 sub irc_tail_error {
147 my ($kernel, $sender, $filename, $errnum, $errstring)
148 = @_[KERNEL, SENDER, ARG0 .. ARG2];
149 $kernel->post( $sender, 'privmsg', $_, "$filename: ERROR: $errnum $errstring" ) for @channels;
150 $irc->plugin_del( 'FollowTail' );
151 return;
152 }
153  
154 sub irc_tail_reset {
155 my ($kernel, $sender, $filename) = @_[KERNEL, SENDER, ARG0];
156 $kernel->post( $sender, 'privmsg', $_, "$filename: RESET EVENT" ) for @channels;
157 return;
158 }
159  
160 =head1 DESCRIPTION
161  
162 POE::Component::IRC::Plugin::FollowTail is a L<POE::Component::IRC|POE::Component::IRC>
163 plugin that uses L<POE::Wheel::FollowTail|POE::Wheel::FollowTail> to follow
164 the end of an ever-growing file. It generates C<irc_tail_> prefixed events for
165 each new record that is appended to its file.
166  
167 =head1 METHODS
168  
169 =head2 C<new>
170  
171 Takes two arguments:
172  
173 B<'filename'>, the name of the file to tail, mandatory;
174  
175 B<'filter'>, a POE::Filter object to pass to POE::Wheel::FollowTail, optional;
176  
177 Returns a plugin object suitable for feeding to
178 L<POE::Component::IRC|POE::Component::IRC>'s C<plugin_add> method.
179  
180 =head1 OUTPUT EVENTS
181  
182 The plugin generates the following additional
183 L<POE::Component::IRC|POE::Component::IRC> events:
184  
185 =head2 C<irc_tail_input>
186  
187 Emitted for every complete record read. C<ARG0> will be the filename,
188 C<ARG1> the record which was read.
189  
190 =head2 C<irc_tail_error>
191  
192 Emitted whenever an error occurs. C<ARG0> will be the filename, C<ARG1>
193 and C<ARG2> hold numeric and string values for $!, respectively.
194  
195 =head2 C<irc_tail_reset>
196  
197 Emitted every time a file is reset. C<ARG0> will be the filename.
198  
199 =head1 AUTHOR
200  
201 Chris 'BinGOs' Williams
202  
203 =head1 SEE ALSO
204  
205 L<POE::Component::IRC|POE::Component::IRC>
206  
207 L<POE::Wheel::FollowTail|POE::Wheel::FollowTail>
208  
209 =cut
« Component/IRC/Plugin/CycleEmpty.pm ↑ Summary Component/IRC/Plugin/NickServID.pm »