File Coverage

blib/lib/POE/Component/IRC/Plugin/BotAddressed.pm
Criterion Covered Total %
statement 55 55 100.0
branch 9 16 56.2
condition 2 3 66.6
subroutine 9 9 100.0
pod 1 5 20.0
total 76 88 86.3


line stmt bran cond sub pod time code
1             package POE::Component::IRC::Plugin::BotAddressed;
2             $POE::Component::IRC::Plugin::BotAddressed::VERSION = '6.95';
3 3     3   3276 use strict;
  3         7  
  3         116  
4 3     3   13 use warnings FATAL => 'all';
  3         6  
  3         194  
5 3     3   15 use Carp;
  3         6  
  3         215  
6 3     3   16 use POE::Component::IRC::Plugin qw( :ALL );
  3         5  
  3         2214  
7              
8             sub new {
9 2     2 1 3178 my ($package) = shift;
10 2 50       12 croak "$package requires an even number of arguments" if @_ & 1;
11 2         6 my %args = @_;
12              
13 2         113 $args{lc $_} = delete $args{$_} for keys %args;
14 2         16 return bless \%args, $package;
15             }
16              
17             sub PCI_register {
18 2     2 0 913 my ($self, $irc) = splice @_, 0, 2;
19 2         15 $irc->plugin_register( $self, 'SERVER', qw(ctcp_action public) );
20 2         106 return 1;
21             }
22              
23             sub PCI_unregister {
24 2     2 0 979 return 1;
25             }
26              
27             sub S_ctcp_action {
28 1     1 0 56 my ($self, $irc) = splice @_, 0, 2;
29 1         3 my $who = ${ $_[0] };
  1         3  
30 1         3 my $recipients = ${ $_[1] };
  1         3  
31 1         2 my $what = ${ $_[2] };
  1         4  
32 1         4 my $me = $irc->nick_name();
33 1 50       2 my $chantypes = join('', @{ $irc->isupport('CHANTYPES') || ['#', '&']});
  1         7  
34              
35 1         3 my $eat = PCI_EAT_NONE;
36 1 50       38 return $eat if $what !~ /$me/i;
37              
38 1         4 for my $recipient (@{ $recipients }) {
  1         94  
39 1 50       34 if ($recipient =~ /^[$chantypes]/) {
40 1 50       5 $eat = PCI_EAT_ALL if $self->{eat};
41 1         7 $irc->send_event_next(irc_bot_mentioned_action => $who => [$recipient] => $what);
42             }
43             }
44              
45 1         29 return $eat;
46             }
47              
48             sub S_public {
49 3     3 0 162 my ($self, $irc) = splice @_, 0, 2;
50 3         7 my $who = ${ $_[0] };
  3         7  
51 3         6 my $channels = ${ $_[1] };
  3         8  
52 3         6 my $what = ${ $_[2] };
  3         7  
53 3         14 my $me = $irc->nick_name();
54 3         207 my ($cmd) = $what =~ m/^\s*[@%]?\Q$me\E[:,;.!?~]?\s*(.*)$/i;
55              
56 3 50 66     31 return PCI_EAT_NONE if !defined $cmd && $what !~ /$me/i;
57              
58 3         8 for my $channel (@{ $channels }) {
  3         8  
59 3 100       10 if (defined $cmd) {
60 2         18 $irc->send_event_next(irc_bot_addressed => $who => [$channel] => $cmd );
61             }
62             else {
63 1         6 $irc->send_event_next(irc_bot_mentioned => $who => [$channel] => $what);
64             }
65             }
66              
67 3 50       115 return $self->{eat} ? PCI_EAT_ALL : PCI_EAT_NONE;
68             }
69              
70             1;
71              
72             =encoding utf8
73              
74             =head1 NAME
75              
76             POE::Component::IRC::Plugin::BotAddressed - A PoCo-IRC plugin that generates
77             events when you are addressed
78              
79             =head1 SYNOPSIS
80              
81             use POE::Component::IRC::Plugin::BotAddressed;
82              
83             $irc->plugin_add( 'BotAddressed', POE::Component::IRC::Plugin::BotAddressed->new() );
84              
85             sub irc_bot_addressed {
86             my ($kernel, $heap) = @_[KERNEL, HEAP];
87             my $nick = ( split /!/, $_[ARG0] )[0];
88             my $channel = $_[ARG1]->[0];
89             my $what = $_[ARG2];
90              
91             print "$nick addressed me in channel $channel with the message '$what'\n";
92             }
93              
94             sub irc_bot_mentioned {
95             my ($nick) = ( split /!/, $_[ARG0] )[0];
96             my ($channel) = $_[ARG1]->[0];
97             my ($what) = $_[ARG2];
98              
99             print "$nick mentioned my name in channel $channel with the message '$what'\n";
100             }
101              
102             =head1 DESCRIPTION
103              
104             POE::Component::IRC::Plugin::BotAddressed is a
105             L plugin. It watches for public
106             channel traffic (i.e. C and C) and will generate
107             an C, C or C
108             event if its name comes up in channel discussion.
109              
110             =head1 METHODS
111              
112             =head2 C
113              
114             One optional argument:
115              
116             B<'eat'>, set to true to make the plugin eat the C /
117             C
118             event and only generate an appropriate event, default is false.
119              
120             Returns a plugin object suitable for feeding to
121             L's C method.
122              
123             =head1 OUTPUT EVENTS
124              
125             =head2 C
126              
127             Has the same parameters passed as L|POE::Component::IRC/irc_public>.
128             C contains the message with the addressed nickname removed, ie. Assuming
129             that your bot is called LameBOT, and someone says 'LameBOT: dance for me',
130             you will actually get 'dance for me'.
131              
132             =head2 C
133              
134             Has the same parameters passed as L|POE::Component::IRC/irc_public>.
135              
136             =head2 C
137              
138             Has the same parameters passed as L|POE::Component::IRC/irc_ctcp_*>.
139              
140             =head1 AUTHOR
141              
142             Chris 'BinGOs' Williams
143              
144             =cut