Component/IRC/Plugin/BotAddressed.pm

filter: Statementstmt 100% 57 / 57 Branchbran 57.1% 8 / 14 Conditioncond 60.0% 3 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 100% 9 / 9 Podpod 20.0% 1 / 5 totaltotal 83.8% 78 / 93 CC 13 SCAR 14.4
CC
13
Cov
89%
S_ctcp_action
6.1
S_public
6.1
new
3.0
CRAP 4.2
« Component/IRC/Qnet.pm ↑ Summary Component/IRC/Plugin/CycleEmpty.pm »
1 package POE::Component::IRC::Plugin::BotAddressed;
2 3 $POE::Component::IRC::Plugin::BotAddressed::VERSION = '6.95';
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use POE::Component::IRC::Plugin qw( :ALL );
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶sub new {
Subroutine new: called CC=3 SCAR=11.1
Pod documented
9 2 my ($package) = shift;
10 2 ▶ croak "$package requires an even number of arguments" if @_ & 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ & 1 0 2 2
11 2 my %args = @_;
12  
13 2 $args{lc $_} = delete $args{$_} for keys %args;
14 2 return bless \%args, $package;
15 }
16  
17 2 ▶sub PCI_register {
Subroutine PCI_register: called CC=1 SCAR=0.0
Pod undocumented
18 2 my ($self, $irc) = splice @_, 0, 2;
19 2 $irc->plugin_register( $self, 'SERVER', qw(ctcp_action public) );
20 2 return 1;
21 }
22  
23 2 ▶sub PCI_unregister {
Subroutine PCI_unregister: called CC=1 SCAR=0.0
Pod undocumented
24 2 return 1;
25 }
26  
27 1 ▶sub S_ctcp_action {
Subroutine S_ctcp_action: called CC=6 SCAR=18.1
Pod undocumented
28 1 my ($self, $irc) = splice @_, 0, 2;
29 1 my $who = ${ $_[0] };
30 1 my $recipients = ${ $_[1] };
31 1 my $what = ${ $_[2] };
32 1 my $me = $irc->nick_name();
33 1 ▶ my $chantypes = join('', @{ $irc->isupport('CHANTYPES') || ['#', '&']});
Condition50%1/2
$irc->isupport('CHANTYPES') || ['#', '&']
l!l
10
MC/DC0%0/1
$irc->isupport('CHANTYPES') || ['#', '&']
$irc->isupport('CHANTYPES')
Truth table50%1/2
A
A= $irc->isupport('CHANTYPES')
Aresult
11
00
34  
35 1 my $eat = PCI_EAT_NONE;
36 1 ▶ return $eat if $what !~ /$me/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $what =~ /$me/i 0 1 1
37  
38 1 for my $recipient (@{ $recipients }) {
39 1 ▶ if ($recipient =~ /^[$chantypes]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($recipient =~ /^[$chantypes]/) 1 0 1
40 1 ▶ $eat = PCI_EAT_ALL if $self->{eat};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"eat"} 0 1 1
41 1 $irc->send_event_next(irc_bot_mentioned_action => $who => [$recipient] => $what);
42 }
43 }
44  
45 1 return $eat;
46 }
47  
48 3 ▶sub S_public {
Subroutine S_public: called CC=6 SCAR=18.0
Pod undocumented
49 3 my ($self, $irc) = splice @_, 0, 2;
50 3 my $who = ${ $_[0] };
51 3 my $channels = ${ $_[1] };
52 3 my $what = ${ $_[2] };
53 3 my $me = $irc->nick_name();
54 3 my ($cmd) = $what =~ m/^\s*[@%]?\Q$me\E[:,;.!?~]?\s*(.*)$/i;
55  
56 3 ▶ return PCI_EAT_NONE if !defined $cmd && $what !~ /$me/i;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cmd or $what =~ /$me/i 0 3 3
Condition66%2/3
defined $cmd or $what =~ /$me/i
l!l&&r!l&&!r
210
MC/DC0%0/2
defined $cmd or $what =~ /$me/i
defined $cmd $what =~ /$me/i
Truth table66%2/3
A or B
A= defined $cmdB= $what =~ /$me/i
ABresult
1X1
011
000
57  
58 3 for my $channel (@{ $channels }) {
59 3 ▶ if (defined $cmd) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cmd) { } 2 1 3
60 2 $irc->send_event_next(irc_bot_addressed => $who => [$channel] => $cmd );
61 }
62 else {
63 1 $irc->send_event_next(irc_bot_mentioned => $who => [$channel] => $what);
64 }
65 }
66  
67 3 ▶ return $self->{eat} ? PCI_EAT_ALL : PCI_EAT_NONE;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'eat'} ? : 0 3 3
68 }
69  
70 3 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<POE::Component::IRC|POE::Component::IRC> plugin. It watches for public
106 channel traffic (i.e. C<irc_public> and C<irc_ctcp_action>) and will generate
107 an C<irc_bot_addressed>, C<irc_bot_mentioned> or C<irc_bot_mentioned_action>
108 event if its name comes up in channel discussion.
109  
110 =head1 METHODS
111  
112 =head2 C<new>
113  
114 One optional argument:
115  
116 B<'eat'>, set to true to make the plugin eat the C<irc_public> /
117 C<irc_ctcp_action>
118 event and only generate an appropriate event, default is false.
119  
120 Returns a plugin object suitable for feeding to
121 L<POE::Component::IRC|POE::Component::IRC>'s C<plugin_add> method.
122  
123 =head1 OUTPUT EVENTS
124  
125 =head2 C<irc_bot_addressed>
126  
127 Has the same parameters passed as L<C<irc_ctcp_public>|POE::Component::IRC/irc_public>.
128 C<ARG2> 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<irc_bot_mentioned>
133  
134 Has the same parameters passed as L<C<irc_public>|POE::Component::IRC/irc_public>.
135  
136 =head2 C<irc_bot_mentioned_action>
137  
138 Has the same parameters passed as L<C<irc_ctcp_action>|POE::Component::IRC/irc_ctcp_*>.
139  
140 =head1 AUTHOR
141  
142 Chris 'BinGOs' Williams <chris@bingosnet.co.uk>
143  
144 =cut
« Component/IRC/Qnet.pm ↑ Summary Component/IRC/Plugin/CycleEmpty.pm »