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