blib/lib/Bot/BasicBot/Pluggable/Module/CPANLinks.pm

filter: Statementstmt 38.6% 17 / 44 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 71.4% 5 / 7 Podpod 100% 2 / 2 totaltotal 29.2% 24 / 82 CC 12 SCAR 46.5
CC
12
Cov
25%
said
156.0
help
2.0
BEGIN
1.0
CRAP 104.4
↑ Summary
1 # A quick Bot::BasicBot::Pluggable module to provide easy links when someone
2 # mentions CPAN modules
3 # David Precious <davidp@preshweb.co.uk>
4  
5 package Bot::BasicBot::Pluggable::Module::CPANLinks;
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use base 'Bot::BasicBot::Pluggable::Module';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Module::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use URI::Title;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 our $VERSION = 0.02;
13  
14 =head1 NAME
15  
16 Bot::BasicBot::Pluggable::Module::CPANLinks - provide links to CPAN module docs
17  
18 =head1 DESCRIPTION
19  
20 A module for L<Bot::BasicBot::Pluggable>-powered IRC bots, to automatically
21 provide links to documentation when people mention certain CPAN modules or
22 keywords. Can be configured to only respond to modules in certain namespaces on
23 a per-channel basis.
24  
25 =head1 HOW IT WORKS
26  
27 If someone's message looks like it contains a CPAN module name (e.g.
28 C<Foo::Bar>), this plugin will look for the bot setting
29 C<filter_$channel> where C<$channel> is the channel that the user
30 spoke in - so, e.g. C<filter_#dancer>. If this setting exists, its
31 value should be a regular expression; if the module name matches this
32 expression, the plugin will look up that module, and, if it exists, provide a
33 link to it (unless it has done so more recently than the configurable
34 threshold).
35  
36 Similarly, if someone says something like "the session keyword" or similar, the
37 plugin will check if C<keywords_$channel> exists and is set to a
38 package name; if it does, it will make sure that package is loaded, check if
39 that package C<can($keyword)>, and if so, will provide a link to the
40 documentation for that keyword.
41  
42 So, for example, in the C<#dancer> channel, C<keywords_#dancer> would
43 be set to C<Dancer> - so if I say "the session keyword", the plugin will check
44 if C<Dancer->can('session')> to check it's a valid keyword, and if so, will
45 provide a link to the docs.
46  
47 WARNING: this setting causes the named package to be loaded; that means that
48 anyone who can configure your bot from IRC can cause a package of their choosing
49 to be loaded into your bot. It must be a package which is already installed on
50 your system, of course, but it deserves that warning.
51  
52 =head1 CONFIGURATION
53  
54 See L<Bot::BasicBot::Pluggable::Module::Vars> for details on how to set the
55 appropriate config options in your bot's store (or do it directly in the DB, if
56 you prefer).
57  
58 The settings to set up are:
59  
60 =over
61  
62 =item C<filter_channelname>
63  
64 Set the pattern which module names must match for a given channel. If this
65 setting doesn't exist for the channel, nothing will be linked.
66  
67 For example, for C<#dancer>, C<filter_#dancer> is set to C<^Dancer>.
68  
69 =item C<keywords_channelname>
70  
71 Set the package from which keywords are linked for a given channel. If this
72 setting doesn't exist for the channel, no keywords will be linked.
73  
74 If you want to use this, set this setting to the name of the package keywords
75 should be looked for in - for instance, for C<#dancer>, C<keywords_#dancer> is
76 set to C<Dancer>. So, if I mention e.g. "see the session keyword", the bot will
77 check if C<Dancer->can('session')>, see that that's a valid keyword, and will
78 provide a link to the docs.
79  
80 Be aware that using this setting means the named package will be loaded; this
81 means anyone who can configure your bot can cause a package of their choosing to
82 be loaded. However, it's loaded by L<Module::Load> at runtime, so no importing
83 will be done, and it can only be a package installed on your system. It's still
84 something to be aware of, though, if you think there's a possibility of code in
85 your C<@INC> that you wouldn't want your bot to load.
86  
87 =item C<dupe_gap>
88  
89 How many seconds between successive mentions of the same module name / keyword
90 to wait before providing a link again. Nobody wants the bot to respond with a
91 link every time a given module name is mentioned. If not set, defaults to a
92 sensible value.
93  
94 =back
95  
96 =cut
97  
98 0 ▶sub help {
Subroutine help: not called CC=1 SCAR=6.9
Pod documented
99 0 return <<HELPMSG;
100 A quick plugin for use on IRC channels to provide links to module /
101 keyword documentation.
102  
103 See the plugin's documentation on CPAN for full details and source:
104 http://p3rl.org/Bot-BasicBot-Pluggable-Module-CPANLinks
105 HELPMSG
106 }
107  
108 1 my %link_said;
109 0 ▶sub said {
Subroutine said: not called CC=12 SCAR=50.5
Pod documented
110 0 my ($self, $mess, $pri) = @_;
111
112 0 ▶ return unless $pri == 2;
Branch: 1 branch
BranchTrueFalseTotal
unless $pri == 2 0 0 0
113 0 my $link;
114 0 ▶ if (my ($module) = $mess->{body} =~ /\b((?:\w+::)+\w+)\b/) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($module) = $mess->{"body"} =~ /\b((?:\w+::)+\w+)\b/) 0 0 0
115 0 warn "Think I have a module mention: " . $module;
116 0 my $key = lc $mess->{channel};
117 my $filter_pattern = $self->get("filter_" . lc $mess->{channel})
118 0 ▶ or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $filter_pattern = $self->get('filter_' . lc($mess->{"channel"})) or return 0 0 0 0
119 0 warn "Checking if this matches pattern $filter_pattern";
120 0 ▶ return 0 unless $module =~ /$filter_pattern/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $module =~ /$filter_pattern/i 0 0 0
121  
122 # OK, this looks like a module name which matches the pattern for this
123 # channel, awesome:
124 0 my $url = "http://p3rl.org/$module";
125 0 my $title = URI::Title::title($url);
126 0 ▶ if ($title) {
Branch: 1 branch
BranchTrueFalseTotal
if ($title) 0 0 0
127 0 $title =~ s/^$module - //;
128 0 $title =~ s/- metacpan.+//;
129 0 $link = "$module is at http://p3rl.org/$module ($title)";
130 }
131 }
132  
133  
134 # OK, if the message contains "the ... keyword" and the channel supports
135 # keywords for a given module (e.g. Dancer), see if this is a valid keyword,
136 # and if so, link to the docs for it
137 0 ▶ if (my $keywords_from
Branch: 1 branch
BranchTrueFalseTotal
if (my $keywords_from = $self->get('keywords_' . lc($mess->{"channel"})) and $mess->{"body"} =~ / ( # match"the keyword forward", "the keyword 'forward", etc the \s keyword \s ['"]? (?<keyword> [a-z_-]+) ['"]? | # or "forward keyword, "'forward' keyword, "forward() keyword" etc \b['"]? (?<keyword> [a-z_-]+) ['"]? (?:\(\))? \s keyword ) /mx) 0 0 0
Condition0%0/3
my $keywords_from = $self->get('keywords_' . lc($mess->{"channel"})) and $mess->{"body"} =~ / ( # match"the keyword forward", "the keyword 'forward", etc the \s keyword \s ['"]? (?<keyword> [a-z_-]+) ['"]? | # or "forward keyword, "'forward' keyword, "forward() keyword" etc \b['"]? (?<keyword> [a-z_-]+) ['"]? (?:\(\))? \s keyword ) /mx
!ll&&!rl&&r
000
MC/DC0%0/2
my $keywords_from = $self->get('keywords_' . lc($mess->{"channel"})) and $mess->{"body"} =~ / ( # match"the keyword forward", "the keyword 'forward", etc the \s keyword \s ['"]? (?<keyword> [a-z_-]+) ['"]? | # or "forward keyword, "'forward' keyword, "forward() keyword" etc \b['"]? (?<keyword> [a-z_-]+) ['"]? (?:\(\))? \s keyword ) /mx
my $keywords_from = $self->get('keywords_' . lc($mess->{"channel"})) $mess->{"body"} =~ / ( # match"the keyword forward", "the keyword 'forward", etc the \s keyword \s ['"]? (?<keyword> [a-z_-]+) ['"]? | # or "forward keyword, "'forward' keyword, "forward() keyword" etc \b['"]? (?<keyword> [a-z_-]+) ['"]? (?:\(\))? \s keyword ) /mx
Truth table0%0/3
A and B
A= my $keywords_from = $self->get('keywords_' . lc($mess->{"channel"}))B= $mess->{"body"} =~ / ( # match"the keyword forward", "the keyword 'forward", etc the \s keyword \s ['"]? (?<keyword> [a-z_-]+) ['"]? | # or "forward keyword, "'forward' keyword, "forward() keyword" etc \b['"]? (?<keyword> [a-z_-]+) ['"]? (?:\(\))? \s keyword ) /mx
ABresult
0X0
100
111
138 = $self->get('keywords_' . lc $mess->{channel})
139 and $mess->{body} =~ m{
140 (
141 # match"the keyword forward", "the keyword 'forward", etc
142 the \s keyword \s ['"]? (?<keyword> [a-z_-]+) ['"]?
143 |
144 # or "forward keyword, "'forward' keyword, "forward() keyword" etc
145 \b['"]? (?<keyword> [a-z_-]+) ['"]? (?:\(\))? \s keyword
146 )
147 }xm
148 ) {
149 0 my $keyword = $+{keyword};
150 0 warn "Mentioned keyword $keyword from $keywords_from, checking it";
151 0 Module::Load::load($keywords_from);
152  
153 0 ▶ if ($keywords_from->can($keyword)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($keywords_from->can($keyword)) 0 0 0
154 0 $link = "The $keyword keyword is documented at "
155 . "http://p3rl.org/$keywords_from#$keyword";
156 }
157 }
158  
159 # Announce the link we found, unless we already did that recently
160 0 ▶ my $threshold_secs = $self->get('dupe_gap') || 600;
Condition0%0/2
$self->get('dupe_gap') || 600
l!l
00
MC/DC0%0/1
$self->get('dupe_gap') || 600
$self->get('dupe_gap')
Truth table0%0/2
A
A= $self->get('dupe_gap')
Aresult
11
00
161 0 ▶ if ($link && (time - $link_said{$link}) > $threshold_secs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($link and time - $link_said{$link} > $threshold_secs) 0 0 0
Condition0%0/3
$link and time - $link_said{$link} > $threshold_secs
!ll&&!rl&&r
000
MC/DC0%0/2
$link and time - $link_said{$link} > $threshold_secs
$link time - $link_said{$link} > $threshold_secs
Truth table0%0/3
A and B
A= $linkB= time - $link_said{$link} > $threshold_secs
ABresult
0X0
100
111
162 0 $link_said{$link} = time;
163 0 return $link;
164 }
165  
166 0 return 0; # This message didn't interest us
167 }
168  
169  
170 =head1 HOSTED OPTION
171  
172 If you like the idea of this but don't want to go to the effort of setting it
173 up, I have a bot on C<irc.perl.org> already running it, which I'd be happy to
174 add to your channel and configure appropriately for you - just drop me a mail.
175  
176  
177 =head1 AUTHOR
178  
179 David Precious (bigpresh) C<< <davidp@preshweb.co.uk> >>
180  
181  
182 =head1 LICENSE AND COPYRIGHT
183  
184 Copyright 2013 David Precious.
185  
186 This program is free software; you can redistribute it and/or modify it
187 under the terms of either: the GNU General Public License as published
188 by the Free Software Foundation; or the Artistic License.
189  
190 See http://dev.perl.org/licenses/ for more information.
191  
192 =cut
193 1 1;
194  
↑ Summary