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