File Coverage

blib/lib/POE/Component/IRC/Plugin/CTCP.pm
Criterion Covered Total %
statement 72 80 90.0
branch 14 28 50.0
condition 2 6 33.3
subroutine 17 17 100.0
pod 2 10 20.0
total 107 141 75.8


line stmt bran cond sub pod time code
1             package POE::Component::IRC::Plugin::CTCP;
2             our $AUTHORITY = 'cpan:HINRIK';
3             $POE::Component::IRC::Plugin::CTCP::VERSION = '6.91';
4 7     7   2744 use strict;
  7         15  
  7         234  
5 7     7   35 use warnings FATAL => 'all';
  7         12  
  7         310  
6 7     7   44 use Carp;
  7         14  
  7         447  
7 7     7   46 use POE::Component::IRC;
  7         13  
  7         266  
8 7     7   97 use POE::Component::IRC::Plugin qw( :ALL );
  7         17  
  7         1042  
9 7     7   42 use POSIX qw(strftime);
  7         14  
  7         80  
10              
11             sub new {
12 3     3 1 1969 my ($package) = shift;
13 3 50       13 croak "$package requires an even number of arguments" if @_ & 1;
14 3         10 my %args = @_;
15              
16 3         24 $args{ lc $_ } = delete $args{ $_ } for keys %args;
17 3 50 33     32 $args{eat} = 1 if !defined ( $args{eat} ) || $args{eat} eq '0';
18 3         14 return bless \%args, $package;
19             }
20              
21             sub PCI_register {
22 3     3 0 1141 my ($self,$irc) = splice @_, 0, 2;
23              
24 3         10 $self->{irc} = $irc;
25 3         13 $irc->plugin_register( $self, 'SERVER', qw(ctcp_version ctcp_clientinfo ctcp_userinfo ctcp_time ctcp_ping ctcp_source) );
26              
27 3         139 return 1;
28             }
29              
30             sub PCI_unregister {
31 3     3 0 1201 delete $_[0]->{irc};
32 3         9 return 1;
33             }
34              
35             ## no critic (TestingAndDebugging::ProhibitNoStrict)
36             sub S_ctcp_version {
37 1     1 0 40 my ($self, $irc) = splice @_, 0, 2;
38 1         3 my $nick = ( split /!/, ${ $_[0] } )[0];
  1         4  
39              
40 1         3 my $our_version;
41             {
42 7     7   2338 no strict 'vars';
  7         16  
  7         4788  
  1         2  
43 1 50 33     7 if (defined $POE::Component::IRC::VERSION
44             && $POE::Component::IRC::VERSION ne '1, set by base.pm') {
45 1         2 $our_version = 'dev-git';
46             }
47             else {
48 0         0 $our_version = $POE::Component::IRC::VERSION;
49             }
50             }
51              
52             $irc->yield( ctcpreply => $nick => 'VERSION ' . ( defined $self->{version}
53             ? $self->{version}
54 1 50       6 : "POE::Component::IRC-$our_version"
55             ));
56 1 50       94 return PCI_EAT_CLIENT if $self->eat();
57 0         0 return PCI_EAT_NONE;
58             }
59              
60             sub S_ctcp_time {
61 1     1 0 36 my ($self, $irc) = splice @_, 0, 2;
62 1         3 my $nick = ( split /!/, ${ $_[0] } )[0];
  1         4  
63              
64 1         70 $irc->yield(ctcpreply => $nick => strftime('TIME %a, %d %b %Y %H:%M:%S %z', localtime));
65              
66 1 50       99 return PCI_EAT_CLIENT if $self->eat();
67 0         0 return PCI_EAT_NONE;
68             }
69              
70             sub S_ctcp_ping {
71 1     1 0 35 my ($self,$irc) = splice @_, 0, 2;
72 1         2 my $nick = ( split /!/, ${ $_[0] } )[0];
  1         3  
73 1         2 my $timestamp = ${ $_[2] };
  1         2  
74              
75 1         5 $irc->yield( ctcpreply => $nick => 'PING ' . $timestamp );
76              
77 1 50       90 return PCI_EAT_CLIENT if $self->eat();
78 0         0 return PCI_EAT_NONE;
79             }
80              
81             sub S_ctcp_clientinfo {
82 1     1 0 37 my ($self, $irc) = splice @_, 0, 2;
83 1         3 my $nick = ( split /!/, ${ $_[0] } )[0];
  1         4  
84              
85             $irc->yield(ctcpreply => $nick => 'CLIENTINFO ' . ($self->{clientinfo}
86             ? $self->{clientinfo}
87 1 50       7 : 'http://search.cpan.org/perldoc?POE::Component::IRC::Plugin::CTCP'
88             ));
89              
90 1 50       93 return PCI_EAT_CLIENT if $self->eat();
91 0         0 return PCI_EAT_NONE;
92             }
93              
94             sub S_ctcp_userinfo {
95 1     1 0 38 my ($self, $irc) = splice @_, 0, 2;
96 1         3 my $nick = ( split /!/, ${ $_[0] } )[0];
  1         4  
97              
98 1 50       8 $irc->yield( ctcpreply => $nick => 'USERINFO ' . ( $self->{userinfo} ? $self->{userinfo} : 'm33p' ) );
99              
100 1 50       89 return PCI_EAT_CLIENT if $self->eat();
101 0         0 return PCI_EAT_NONE;
102             }
103              
104             sub S_ctcp_source {
105 1     1 0 36 my ($self, $irc) = splice @_, 0, 2;
106 1         2 my $nick = ( split /!/, ${ $_[0] } )[0];
  1         4  
107              
108             $irc->yield( ctcpreply => $nick => 'SOURCE ' . ($self->{source}
109             ? $self->{source}
110 1 50       7 : 'http://search.cpan.org/dist/POE-Component-IRC'
111             ));
112              
113 1 50       92 return PCI_EAT_CLIENT if $self->eat();
114 0         0 return PCI_EAT_NONE;
115             }
116              
117             sub eat {
118 6     6 1 13 my $self = shift;
119 6         7 my $value = shift;
120              
121 6 50       37 return $self->{eat} if !defined $value;
122 0           return $self->{eat} = $value;
123             }
124              
125             1;
126              
127             =encoding utf8
128              
129             =head1 NAME
130              
131             POE::Component::IRC::Plugin::CTCP - A PoCo-IRC plugin that auto-responds to CTCP requests
132              
133             =head1 SYNOPSIS
134              
135             use strict;
136             use warnings;
137             use POE qw(Component::IRC Component::IRC::Plugin::CTCP);
138              
139             my $nickname = 'Flibble' . $$;
140             my $ircname = 'Flibble the Sailor Bot';
141             my $ircserver = 'irc.blahblahblah.irc';
142             my $port = 6667;
143              
144             my $irc = POE::Component::IRC->spawn(
145             nick => $nickname,
146             server => $ircserver,
147             port => $port,
148             ircname => $ircname,
149             ) or die "Oh noooo! $!";
150              
151             POE::Session->create(
152             package_states => [
153             main => [ qw(_start) ],
154             ],
155             );
156              
157             $poe_kernel->run();
158              
159             sub _start {
160             # Create and load our CTCP plugin
161             $irc->plugin_add( 'CTCP' => POE::Component::IRC::Plugin::CTCP->new(
162             version => $ircname,
163             userinfo => $ircname,
164             ));
165              
166             $irc->yield( register => 'all' );
167             $irc->yield( connect => { } );
168             return:
169             }
170              
171             =head1 DESCRIPTION
172              
173             POE::Component::IRC::Plugin::CTCP is a L
174             plugin. It watches for C, C,
175             C, C and C events and
176             autoresponds on your behalf.
177              
178             =head1 METHODS
179              
180             =head2 C
181              
182             Takes a number of optional arguments:
183              
184             B<'version'>, a string to send in response to C. Default is
185             PoCo-IRC and version;
186              
187             B<'clientinfo'>, a string to send in response to C.
188             Default is L.
189              
190             B<'userinfo'>, a string to send in response to C. Default
191             is 'm33p';
192              
193             B<'source'>, a string to send in response to C. Default is
194             L.
195              
196             B<'eat'>, by default the plugin uses PCI_EAT_CLIENT, set this to 0 to disable
197             this behaviour;
198              
199             Returns a plugin object suitable for feeding to
200             L's C method.
201              
202             =head2 C
203              
204             With no arguments, returns true or false on whether the plugin is "eating" CTCP
205             events that it has dealt with. An argument will set "eating" to on or off
206             appropriately, depending on whether the value is true or false.
207              
208             =head1 AUTHOR
209              
210             Chris 'BinGOs' Williams
211              
212             =head1 SEE ALSO
213              
214             CTCP Specification L.
215              
216             =cut