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

filter: Statementstmt 75.2% 176 / 234 Branchbran 64.7% 88 / 136 Conditioncond 65.7% 46 / 70 MC/DCmcdc 35.9% 14 / 39 Subroutinesub 83.3% 20 / 24 Podpod 25.0% 4 / 16 totaltotal 67.0% 348 / 519 CC 98 SCAR 36.2
CC
98
Cov
70%
fallback
73.5
told
29.0
parseFeed
23.2
CRAP 37.4
« blib/lib/Bot/BasicBot/Pluggable/Module/Google.pm ↑ Summary blib/lib/Bot/BasicBot/Pluggable/Module/Loader.pm »
1 package Bot::BasicBot::Pluggable::Module::Infobot;
2 2 $Bot::BasicBot::Pluggable::Module::Infobot::VERSION = '1.11';
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use base qw(Bot::BasicBot::Pluggable::Module);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use LWP::UserAgent ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # this one is a complete bugger to build
12 2 eval { require XML::Feed };
13 2 ▶our $HAS_XML_FEED = $@ ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 2 2
14 2 ▶use constant PROT => "ibprot_";
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use constant INFO => "infobot_";
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 sub init {
18 2 ▶ my $self = shift;
Subroutine init: called CC=1 SCAR=0.0
Pod documented
19 2 $self->config(
20 {
21 user_allow_searching => 0,
22 user_min_length => 3,
23 user_max_length => 25,
24 user_num_results => 20,
25 user_passive_answer => 0,
26 user_passive_learn => 0,
27 user_require_question => 1,
28 user_http_timeout => 10,
29 user_rss_items => 5,
30 user_stopwords =>
31 "here|how|it|something|that|this|what|when|where|which|who|why",
32 user_unknown_responses =>
33 "Dunno.|I give up.|I have no idea.|No clue. Sorry.|Search me, bub.|Sorry, I don't know.",
34 db_version => "1",
35 }
36 );
37  
38 # record what we've asked other bots.
39 2 $self->{remote_infobot} = {};
40 }
41  
42 sub help {
43 return
44 1 ▶"An infobot. See http://search.mcpan.org/perldoc?Bot::BasicBot::Pluggable::Module::Infobot.";
Subroutine help: called CC=1 SCAR=0.0
Pod documented
45 }
46  
47 sub told {
48 75 ▶ my ( $self, $mess ) = @_;
Subroutine told: called CC=19 SCAR=33.7
Pod documented
49 75 local $self->{mess} = $mess;
50 75 my $body = $mess->{body};
51 75 ▶ return unless defined $body;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $body 0 75 75
52  
53 # looks like an infobot reply.
54 75 ▶ if ( $body =~ s/^:INFOBOT:REPLY (\S+) (.*)$// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body =~ s/^:INFOBOT:REPLY (\S+) (.*)$//) 0 75 75
55 0 return $self->infobot_reply( $1, $2, $mess );
56 }
57  
58 # direct commands must be addressed.
59 75 ▶ return unless $mess->{address};
Branch: 1 branch
BranchTrueFalseTotal
unless $mess->{"address"} 11 64 75
60  
61 # forget a particular factoid.
62 64 ▶ if ( $body =~ /^forget\s+(.*)$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body =~ /^forget\s+(.*)$/i) 3 61 64
63 3 ▶ unless ($self->protection_status($mess, $1)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->protection_status($mess, $1)) 3 0 3
64 3 ▶ return $self->delete_factoid($1)
Branch: 1 branch
BranchTrueFalseTotal
$self->delete_factoid($1) ? : 2 1 3
65 ? "I forgot about $1."
66 : "I don't know anything about $1.";
67 }
68 }
69  
70 # ask another bot for facts.
71 61 ▶ if ( $body =~ /^ask\s+(\S+)\s+about\s+(.*)$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body =~ /^ask\s+(\S+)\s+about\s+(.*)$/i) 0 61 61
72 0 $self->ask_factoid( $2, $1, $mess );
73 0 return "I'll ask $1 about $2.";
74 }
75  
76 # tell someone else about a factoid
77 61 ▶ if ( $body =~ /^tell\s+(\S+)\s+about\s+(.*)$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body =~ /^tell\s+(\S+)\s+about\s+(.*)$/i) 1 60 61
78 1 $self->tell_factoid( $2, $1, $mess );
79 1 return "Told $1 about $2.";
80 }
81  
82 # search for a particular factoid.
83 60 ▶ if ( $body =~ /^search\s+for\s+(.*)$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body =~ /^search\s+for\s+(.*)$/i) 6 54 60
84 6 ▶ return "privmsg only, please" unless ( $mess->{channel} eq "msg" );
Branch: 1 branch
BranchTrueFalseTotal
unless $mess->{"channel"} eq "msg" 1 5 6
85 5 ▶ return "searching disabled" unless $self->get("user_allow_searching");
Branch: 1 branch
BranchTrueFalseTotal
unless $self->get("user_allow_searching") 1 4 5
86 4 my @results = $self->search_factoid( split( /\s+/, $1 ) );
87 4 ▶ unless (@results) { return "I don't know anything about $1."; }
Branch: 1 branch
BranchTrueFalseTotal
unless (@results) 1 3 4
88 3 ▶ $#results = $self->get("user_num_results") - 1
Branch: 1 branch
BranchTrueFalseTotal
unless $#results < $self->get("user_num_results") 1 2 3
89 unless $#results < $self->get("user_num_results");
90 3 return "I know about: " . join( ", ", map { "'$_'" } @results ) . ".";
91 }
92  
93 54 ▶ if ($self->authed( $mess->{who} )) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->authed($mess->{"who"})) 0 54 54
94 # protect a particular factoid.
95 0 ▶ if ( $body =~ /^protect\s+(.*)$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body =~ /^protect\s+(.*)$/i) 0 0 0
96 0 ▶ return $self->protect_factoid($1)
Branch: 1 branch
BranchTrueFalseTotal
$self->protect_factoid($1) ? : 0 0 0
97 ? "Protected $1."
98 : "Already protected.";
99 }
100  
101 # unprotect a particular factoid.
102 0 ▶ if ( $body =~ /^unprotect\s+(.*)$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body =~ /^unprotect\s+(.*)$/i) 0 0 0
103 0 ▶ return $self->unprotect_factoid($1)
Branch: 1 branch
BranchTrueFalseTotal
$self->unprotect_factoid($1) ? : 0 0 0
104 ? "Unprotected $1."
105 : "Was not protected";
106 }
107 }
108 }
109  
110 sub fallback {
111 65 ▶ my ( $self, $mess ) = @_;
Subroutine fallback: called CC=48 SCAR=43.0
Pod documented
112 65 local $self->{mess} = $mess;
113 65 ▶ my $body = $mess->{body} || "";
Condition50%1/2
$mess->{'body'} || ''
l!l
650
MC/DC0%0/1
$mess->{'body'} || ''
$mess->{'body'}
Truth table50%1/2
A
A= $mess->{'body'}
Aresult
11
00
114  
115 65 ▶ my $is_priv = !defined $mess->{channel} || $mess->{channel} eq 'msg';
Condition33%1/3
!defined($mess->{'channel'}) || $mess->{'channel'} eq 'msg'
l!l&&r!l&&!r
0065
MC/DC0%0/2
!defined($mess->{'channel'}) || $mess->{'channel'} eq 'msg'
!defined($mess->{'channel'}) $mess->{'channel'} eq 'msg'
Truth table33%1/3
not(A) || B
A= !defined($mess->{'channel'})B= $mess->{'channel'} eq 'msg'
ABresult
1X1
011
000
116  
117 # request starts with "my", so we'll look for
118 # a valid factoid for "$mess->{who}'s $object".
119 65 $body =~ s/^my /$mess->{who}'s /;
120  
121 my %stopwords =
122 65 map { lc($_) => 1 }
123 split( /\s*[\s,\|]\s*/, $self->get("user_stopwords") );
124  
125 # checks to see if something starts
126 # <word> (is|are)
127 # and then removes if if <word> is a stopword
128 # this means that we treat "what is foo?" as "foo?"
129 65 ▶ if ( $body =~ /^(.*?)\s+(is|are)\s+(.*)$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body =~ /^(.*?)\s+(is|are)\s+(.*)$/i) 30 35 65
130 30 ▶ $body =~ s/^(.*?)\s+(is|are)\s+//i if $stopwords{$1};
Branch: 1 branch
BranchTrueFalseTotal
if $stopwords{$1} 5 25 30
131 }
132  
133 # answer a factoid. this is a crazy check which ensures we will ONLY answer
134 # a factoid if a) there is, or isn't, a question mark, b) we have, or haven't,
135 # been addressed, c) the factoid is bigger and smaller than our requirements,
136 # and d) that it doesn't look like a to-be-learned factoid (which is important
137 # if the user has disabled the requiring of the question mark for answering.)
138 65 ▶ my $readdress = $mess->{channel} ne 'msg' && $body =~ s/\s+@\s+(\S+)[.]?\s*$// ? $1 : '';
Branch: 1 branch
BranchTrueFalseTotal
$mess->{'channel'} ne 'msg' && $body =~ s/\s+\@\s+(\S+)[.]?\s*$// ? : 0 65 65
Condition33%1/3
$mess->{"channel"} ne "msg" and $body =~ s/\s+\@\s+(\S+)[.]?\s*$//
!ll&&!rl&&r
0650
MC/DC0%0/2
$mess->{"channel"} ne "msg" and $body =~ s/\s+\@\s+(\S+)[.]?\s*$//
$mess->{"channel"} ne "msg" $body =~ s/\s+\@\s+(\S+)[.]?\s*$//
Truth table33%1/3
A and B
A= $mess->{"channel"} ne "msg"B= $body =~ s/\s+\@\s+(\S+)[.]?\s*$//
ABresult
0X0
100
111
139 65 ▶ my $body_regexp =
Branch: 1 branch
BranchTrueFalseTotal
$self->get('user_require_question') && !$is_priv ? : 65 0 65
Condition33%1/3
$self->get("user_require_question") and not $is_priv
!ll&&!rl&&r
0065
MC/DC0%0/2
$self->get("user_require_question") and not $is_priv
$self->get("user_require_question") not $is_priv
Truth table33%1/3
A and not(B)
A= $self->get("user_require_question")B= not $is_priv
ABresult
0X0
100
111
140 $self->get("user_require_question") && !$is_priv ? qr/\?+$/ : qr/[.!?]*$/;
141 65 ▶ if ( $body =~ s/$body_regexp//
Branch: 1 branch
BranchTrueFalseTotal
if ($body =~ s/$body_regexp// and $mess->{"address"} || $self->get("user_passive_answer") and length $body >= $self->get("user_min_length") and length $body <= $self->get("user_max_length") and not $body =~ /^(.*?)\s+(is|are)\s+(.*)$/i) 36 29 65
Condition80%12/15
$body =~ s/$body_regexp// and $mess->{"address"} || $self->get("user_passive_answer") and length $body >= $self->get("user_min_length") and length $body <= $self->get("user_max_length") and not $body =~ /^(.*?)\s+(is|are)\s+(.*)$/i
!ll&&!rl&&r
29036
$body =~ s/$body_regexp// and $mess->{"address"} || $self->get("user_passive_answer") and length $body >= $self->get("user_min_length") and length $body <= $self->get("user_max_length")
!ll&&!rl&&r
29036
$body =~ s/$body_regexp// and $mess->{"address"} || $self->get("user_passive_answer") and length $body >= $self->get("user_min_length")
!ll&&!rl&&r
29036
$body =~ s/$body_regexp// and $mess->{"address"} || $self->get("user_passive_answer")
!ll&&!rl&&r
27236
$mess->{"address"} or $self->get("user_passive_answer")
l!l&&r!l&&!r
3062
MC/DC50%3/6
$body =~ s/$body_regexp// and $mess->{"address"} || $self->get("user_passive_answer") and length $body >= $self->get("user_min_length") and length $body <= $self->get("user_max_length") and not $body =~ /^(.*?)\s+(is|are)\s+(.*)$/i
$body =~ s/$body_regexp// $mess->{"address"} $self->get("user_passive_answer") length $body >= $self->get("user_min_length") length $body <= $self->get("user_max_length") not $body =~ /^(.*?)\s+(is|are)\s+(.*)$/i
Truth table40%4/10
A and B or C and D and E and not(F)
A= $body =~ s/$body_regexp//B= $mess->{"address"}C= $self->get("user_passive_answer")D= length $body >= $self->get("user_min_length")E= length $body <= $self->get("user_max_length")F= not $body =~ /^(.*?)\s+(is|are)\s+(.*)$/i
ABCDEFresult
0XXXXX0
100XXX0
11X0XX0
1010XX0
11X10X0
10110X0
11X1100
1011100
11X1111
1011111
142 and ( $mess->{address} or $self->get("user_passive_answer") )
143 and length($body) >= $self->get("user_min_length")
144 and length($body) <= $self->get("user_max_length")
145 and $body !~ /^(.*?)\s+(is|are)\s+(.*)$/i )
146 {
147  
148 # get the factoid and type of relationship
149 36 my ( $is_are, $factoid, $literal ) = $self->get_factoid($body);
150 36 ▶ if ( !$literal && $factoid && $factoid =~ /\|/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $literal and $factoid and $factoid =~ /\|/) 0 36 36
Condition83%5/6
not $literal and $factoid and $factoid =~ /\|/
!ll&&!rl&&r
8280
not $literal and $factoid
!ll&&!rl&&r
4428
MC/DC0%0/3
not $literal and $factoid and $factoid =~ /\|/
not $literal $factoid $factoid =~ /\|/
Truth table75%3/4
not(A) and B and C
A= not $literalB= $factoidC= $factoid =~ /\|/
ABCresult
0XX0
10X0
1100
1111
151 0 my @f = split /\|/, $factoid;
152 0 $factoid = $f[ int( rand( scalar @f ) ) ];
153 }
154  
155 # no factoid?
156 36 ▶ unless ($factoid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($factoid) 4 32 36
157 4 my @unknowns = split( /\|/, $self->get("user_unknown_responses") );
158 4 my $unknown = $unknowns[ int( rand( scalar(@unknowns) ) ) - 1 ];
159 4 ▶ return $mess->{address} ? $unknown : undef;
Branch: 1 branch
BranchTrueFalseTotal
$mess->{'address'} ? : 4 0 4
160 }
161  
162 # variable substitution.
163 32 $factoid =~ s/\$who/$mess->{who}/g;
164  
165 # emote?
166 32 ▶ if ( $factoid =~ s/^<action>\s*//i ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($factoid =~ s/^<action>\s*//i) { } 0 32 32
elsif ($literal) { } 4 28 32
167 $self->bot->emote(
168 {
169 who => $readdress || $mess->{who},
170 channel => $mess->{channel},
171 0 ▶ body => $factoid
Condition0%0/3
$readdress || $mess->{'who'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$readdress || $mess->{'who'}
$readdress $mess->{'who'}
Truth table0%0/3
A || B
A= $readdressB= $mess->{'who'}
ABresult
1X1
011
000
172 }
173 );
174 0 return 1;
175  
176 # replying with, or without a noun? hmMmMmmm?!
177 }
178 elsif ($literal) {
179 4 $body =~ s!^literal\s+!!;
180 4 $factoid = "$body =${is_are}= $factoid";
181 }
182 else {
183 28 ▶ $factoid = $factoid =~ s/^<reply>\s*//i
Branch: 1 branch
BranchTrueFalseTotal
$factoid =~ s/^<reply>\s*//i ? : 2 26 28
184 ? $factoid
185 : "$body $is_are $factoid";
186 }
187 32 ▶ if ($readdress) {
Branch: 1 branch
BranchTrueFalseTotal
if ($readdress) 0 32 32
188 0 my %hash = %$mess;
189 0 $hash{who} = $readdress;
190 0 $self->reply(\%hash, $factoid);
191 0 return 1;
192 }
193 32 return $factoid;
194 }
195  
196 # the only thing left is learning factoids. are we
197 # addressed or are we willing to learn passively?
198 # does it even look like a factoid?
199 29 ▶ return unless ( $mess->{address} or $self->get("user_passive_learn") );
Branch: 1 branch
BranchTrueFalseTotal
unless $mess->{"address"} or $self->get("user_passive_learn") 3 26 29
Condition100%3/3
$mess->{"address"} or $self->get("user_passive_learn")
l!l&&r!l&&!r
2423
MC/DC100%2/2
$mess->{"address"} or $self->get("user_passive_learn")
$mess->{"address"} $self->get("user_passive_learn")
Truth table100%3/3
A or B
A= $mess->{"address"}B= $self->get("user_passive_learn")
ABresult
1X1
011
000
200 return
201 26 ▶ unless ( $body =~ /^(.*?)\s+(is)\s+(.*)$/i
Branch: 1 branch
BranchTrueFalseTotal
unless $body =~ /^(.*?)\s+(is)\s+(.*)$/i or $body =~ /^(.*?)\s+(are)\s+(.*)$/i 2 24 26
Condition100%3/3
$body =~ /^(.*?)\s+(is)\s+(.*)$/i or $body =~ /^(.*?)\s+(are)\s+(.*)$/i
l!l&&r!l&&!r
2222
MC/DC100%2/2
$body =~ /^(.*?)\s+(is)\s+(.*)$/i or $body =~ /^(.*?)\s+(are)\s+(.*)$/i
$body =~ /^(.*?)\s+(is)\s+(.*)$/i $body =~ /^(.*?)\s+(are)\s+(.*)$/i
Truth table100%3/3
A or B
A= $body =~ /^(.*?)\s+(is)\s+(.*)$/iB= $body =~ /^(.*?)\s+(are)\s+(.*)$/i
ABresult
1X1
011
000
202 or $body =~ /^(.*?)\s+(are)\s+(.*)$/i );
203 24 my ( $object, $is_are, $description ) = ( $1, $2, $3 );
204 24 my $literal = ( $object =~ s!^literal\s+!! );
205  
206 # allow corrections and additions.
207 24 my ( $nick, $replace, $also ) = ( $self->bot->nick, 0, 0 );
208 24 ▶ $replace = 1 if ( $object =~ s/no,?\s+//i ); # no, $object is $fact.
Branch: 1 branch
BranchTrueFalseTotal
if $object =~ s/no,?\s+//i 1 23 24
209 24 ▶ $replace = 1
Branch: 1 branch
BranchTrueFalseTotal
if $replace and $object =~ s/^\s*$nick,?\s*//i 0 24 24
Condition66%2/3
$replace and $object =~ s/^\s*$nick,?\s*//i
!ll&&!rl&&r
2310
MC/DC0%0/2
$replace and $object =~ s/^\s*$nick,?\s*//i
$replace $object =~ s/^\s*$nick,?\s*//i
Truth table66%2/3
A and B
A= $replaceB= $object =~ s/^\s*$nick,?\s*//i
ABresult
0X0
100
111
210 if ( $replace and $object =~ s/^\s*$nick,?\s*//i )
211 ; # no, $bot, $object is $fact.
212 24 ▶ $also = 1 if ( $description =~ s/^also\s+//i ); # $object is also $fact.
Branch: 1 branch
BranchTrueFalseTotal
if $description =~ s/^also\s+//i 9 15 24
213  
214 # ignore short, long, and stopword'd factoids.
215 24 ▶ return if length($object) < $self->get("user_min_length");
Branch: 1 branch
BranchTrueFalseTotal
if length $object < $self->get("user_min_length") 0 24 24
216 24 ▶ return if length($object) > $self->get("user_max_length");
Branch: 1 branch
BranchTrueFalseTotal
if length $object > $self->get("user_max_length") 1 23 24
217 23 ▶ foreach ( keys %stopwords ) { return if $object =~ /^$_\b/; }
Branch: 1 branch
BranchTrueFalseTotal
if $object =~ /^$_\b/ 0 276 276
218  
219 # if we're replacing things, remove the factoid first.
220 # $also check supports "no, $bot, $object is also $fact".
221 23 ▶ if ( $replace and !$also ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($replace and not $also) 1 22 23
Condition66%2/3
$replace and not $also
!ll&&!rl&&r
2201
MC/DC50%1/2
$replace and not $also
$replace not $also
Truth table66%2/3
A and not(B)
A= $replaceB= not $also
ABresult
0X0
100
111
222 1 ▶ unless ($self->protection_status($mess, $object)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->protection_status($mess, $object)) 1 0 1
223 1 $self->delete_factoid($object);
224 }
225 }
226  
227 # get any current factoid there might be.
228 23 my ( $type, $current ) = $self->get_factoid($object);
229  
230 # we can't add without explicit instruction,
231 # but shouldn't warn if this is passive.
232 23 ▶ if ( $current and !$also and $mess->{address} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($current and not $also and $mess->{'address'}) { } 2 21 23
elsif ($current and not $also and not $mess->{'address'}) { } 2 38 40
Condition100%6/6
$current and not $also and $mess->{"address"}
!ll&&!rl&&r
1922
$current and not $also
!ll&&!rl&&r
1094
MC/DC100%3/3
$current and not $also and $mess->{"address"}
$current not $also $mess->{"address"}
Truth table100%4/4
A and not(B) and C
A= $currentB= not $alsoC= $mess->{"address"}
ABCresult
0XX0
10X0
1100
1111
233 2 return "... but $object $type $current ...";
234 }
235 ▶ elsif ( $current and !$also and !$mess->{address} ) {
Condition83%5/6
$current and not $also and not $mess->{"address"}
!ll&&!rl&&r
1902
$current and not $also
!ll&&!rl&&r
1092
MC/DC66%2/3
$current and not $also and not $mess->{"address"}
$current not $also not $mess->{"address"}
Truth table75%3/4
A and not(B) and not(C)
A= $currentB= not $alsoC= not $mess->{"address"}
ABCresult
0XX0
10X0
1100
1111
236 2 return;
237 }
238  
239 19 ▶ unless ( $self->protection_status($mess, $object) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->protection_status($mess, $object)) 19 0 19
240 # add this factoid. this comment is absolutely useless. excelsior.
241 19 $self->add_factoid( $object, $is_are, split( /\s+or\s+/, $description ) );
242  
243 # return an ack if we were addressed only
244 19 ▶ return $mess->{address} ? "Okay." : 1;
Branch: 1 branch
BranchTrueFalseTotal
$mess->{'address'} ? : 19 0 19
245 }
246 }
247  
248 sub get_factoid {
249 60 ▶ my ( $self, $object ) = @_;
Subroutine get_factoid: called CC=8 SCAR=20.9
Pod undocumented
250  
251 60 my $literal = ( $object =~ s!^literal\s+!! );
252  
253 # get a list of factoid hashes
254 60 my ( $is_are, @factoids ) = $self->get_raw_factoids($object);
255  
256 60 ▶ return unless @factoids;
Branch: 1 branch
BranchTrueFalseTotal
unless @factoids 14 46 60
257  
258 #print STDERR Dumper(@factoids);
259  
260 # simple is a list of the 'simple' factoids, a is b, etc. These are just
261 # joined together. Alternates are factoids that are an alternative to
262 # the simple factoids, they will randomly be displayed _instead_.
263 46 my ( @simple, @alternatives );
264  
265 46 for (@factoids) {
266 125 ▶ next if $_->{text} =~ m!^\s*$!;
Branch: 1 branch
BranchTrueFalseTotal
if $_->{"text"} =~ /^\s*$/ 0 125 125
267 125 ▶ if ( $_->{alternate} || $_->{alt} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->{'alternate'} or $_->{'alt'}) { } 25 100 125
Condition66%2/3
$_->{"alternate"} or $_->{"alt"}
l!l&&r!l&&!r
250100
MC/DC50%1/2
$_->{"alternate"} or $_->{"alt"}
$_->{"alternate"} $_->{"alt"}
Truth table66%2/3
A or B
A= $_->{"alternate"}B= $_->{"alt"}
ABresult
1X1
011
000
268 25 push @alternatives, $_->{text};
269 }
270 else {
271 100 push @simple, $_->{text};
272 }
273 }
274  
275 46 ▶ if ($literal) {
Branch: 1 branch
BranchTrueFalseTotal
if ($literal) 4 42 46
276 4 my $return .= join " =or= ", ( @simple, map { "|$_" } @alternatives );
277 4 return ( $is_are, $return, 1 );
278 }
279  
280 #print STDERR Dumper(@alternatives);
281  
282 # the simple list is one of the alternatives
283 42 ▶ unshift( @alternatives, join( " or ", @simple ) ) if @simple;
Branch: 1 branch
BranchTrueFalseTotal
if @simple 42 0 42
284  
285 # pick an option at random
286 42 srand();
287 42 my $factoid = $alternatives[ rand(@alternatives) ];
288  
289 #print STDERR "$factoid\n";
290 # if there are any RSS directives, get the feed.
291 # TODO - this could be done in a more general way, with plugins
292 # TODO - this blocks. Bad. you can knock the bot off channel by
293 # giving it an RSS feed that'll take a very long time to return.
294 42 $factoid =~
295 2 s/<(?:rss|atom|feed|xml)\s*=\s*\"?([^>\"]+)\"?>/$self->parseFeed($1)/ieg;
296  
297 42 return ( $is_are, $factoid, 0 );
298 }
299  
300 # for a given key, return the raw hashes that are in the store for this
301 # factoid.
302 sub get_raw_factoids {
303 79 ▶ my ( $self, $object ) = @_;
Subroutine get_raw_factoids: called CC=8 SCAR=26.1
Pod undocumented
304 79 ▶ my $raw = $self->get( INFO . lc($object) )
Branch: 1 branch
BranchTrueFalseTotal
my $raw = $self->get('infobot_' . lc($object)) or return () 24 55 79
305 or return ();
306  
307 #print STDERR Dumper($raw);
308 55 my ( $is_are, @factoids );
309  
310 55 ▶ if ( ref($raw) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $raw) { } 55 0 55
311  
312 # it's a deep structure
313 55 $is_are = $raw->{is_are};
314 @factoids = map {
315 144 ▶ ref $_ && /DBM::Deep::Hash/ ? +{ %$_ } : $_
Branch: 1 branch
BranchTrueFalseTotal
ref $_ && /DBM::Deep::Hash/ ? : 0 144 144
Condition33%1/3
ref $_ and /DBM::Deep::Hash/
!ll&&!rl&&r
01440
MC/DC0%0/2
ref $_ and /DBM::Deep::Hash/
ref $_ /DBM::Deep::Hash/
Truth table33%1/3
A and B
A= ref $_B= /DBM::Deep::Hash/
ABresult
0X0
100
111
316 55 ▶ } @{ $raw->{factoids} || [] };
Branch: 1 branch
BranchTrueFalseTotal
$raw->{"factoids"} or [] 0 55 55
317  
318 }
319 else {
320  
321 # old-style tab seperated thing
322 0 my @strings;
323 0 ( $is_are, @strings ) = split( /\t/, $raw );
324 0 for my $text (@strings) {
325 0 ▶ my $alt = ( $text =~ s/^\|\s*// ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$text =~ s/^\|\s*// ? : 0 0 0
326 0 push @factoids, { alternate => $alt, text => $text };
327 }
328 }
329  
330 55 return ( $is_are, @factoids );
331 }
332  
333 sub add_factoid {
334 19 ▶ my ( $self, $object, $is_are, @factoids ) = @_;
Subroutine add_factoid: called CC=5 SCAR=16.1
Pod undocumented
335  
336 # get the current list, if any
337 19 my ( $current_is_are, @current ) = $self->get_raw_factoids($object);
338  
339 # if there's already an is_are set, use it.
340 19 ▶ $is_are = $current_is_are if ($current_is_are);
Branch: 1 branch
BranchTrueFalseTotal
if $current_is_are 9 10 19
341 19 ▶ $is_are ||= "is"; # defaults
Condition50%1/2
$is_are ||= "is"
l!l
190
MC/DC0%0/1
$is_are ||= "is"
$is_are
Truth table50%1/2
A
A= $is_are
Aresult
11
00
342  
343 # add these factoids to the list, trimming trailing space after |
344 19 for (@factoids) {
345 20 ▶ my $alt = s/^\|\s*// ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
s/^\|\s*// ? : 3 17 20
346 20 push @current,
347 {
348 alternate => $alt,
349 text => $_,
350 };
351 }
352  
353 19 my $set = {
354 is_are => $is_are,
355 factoids => \@current,
356 };
357  
358 # put the list back into the store.
359 19 $self->set( INFO . lc($object), $set );
360  
361 19 return 1;
362 }
363  
364 sub protection_status {
365 23 ▶ my $self = shift;
Subroutine protection_status: called CC=2 SCAR=7.1
Pod undocumented
366 23 my ($mess, $object) = @_;
367 $self->get( PROT . lc($object) ) && !$self->authed( $mess->{who} )
368 23 ▶}
Branch: 1 branch
BranchTrueFalseTotal
$self->get('ibprot_' . lc($object)) and not $self->authed($mess->{"who"}) 0 23 23
369  
370 sub delete_factoid {
371 4 ▶ my ( $self, $object ) = @_;
Subroutine delete_factoid: called CC=2 SCAR=6.9
Pod undocumented
372 4 my $key = INFO . lc($object);
373 4 ▶ if ( $self->get($key) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get($key)) { } 3 1 4
374 3 $self->unset( INFO . lc($object) );
375 3 return 1;
376 }
377 else {
378 1 return 0;
379 }
380 }
381  
382 sub protect_factoid {
383 0 ▶ my ( $self, $object ) = @_;
Subroutine protect_factoid: not called CC=2 SCAR=17.9
Pod undocumented
384 0 my $key = PROT . lc($object);
385 0 ▶ unless ( $self->get($key) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get($key)) { } 0 0 0
386 0 $self->set( $key, 1 );
387 0 return 1;
388 }
389 else {
390 0 return 0;
391 }
392 }
393  
394 sub unprotect_factoid {
395 0 ▶ my ( $self, $object ) = @_;
Subroutine unprotect_factoid: not called CC=2 SCAR=17.9
Pod undocumented
396 0 my $key = PROT . lc($object);
397 0 ▶ if ( $self->get($key) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get($key)) { } 0 0 0
398 0 $self->unset( $key );
399 0 return 1;
400 }
401 else {
402 0 return 0;
403 }
404 }
405  
406 sub ask_factoid {
407 0 ▶ my ( $self, $object, $ask, $mess ) = @_;
Subroutine ask_factoid: not called CC=1 SCAR=6.9
Pod undocumented
408  
409 # unique ID to reference this in future
410 0 my $id = "<" . int( rand(100000) ) . ">";
411  
412 # store the message, so we can reply in context later
413 0 $self->{remote_infobot}{$id} = $mess;
414  
415 # ask, using an infobot protocol, the thing we've been told to ask.
416 # this will hopefully result in a reply coming back later.
417 0 $self->bot->say(
418 who => $ask,
419 channel => 'msg',
420 body => ":INFOBOT:QUERY $id $object"
421 );
422 }
423  
424 sub tell_factoid {
425 1 ▶ my ( $self, $object, $tell, $mess ) = @_;
Subroutine tell_factoid: called CC=1 SCAR=0.0
Pod undocumented
426  
427 1 my ( $is_are, $factoid ) = $self->get_factoid($object);
428 1 my $from = $mess->{who};
429  
430 1 $self->bot->say(
431 who => $tell,
432 channel => 'msg',
433 body => "$from wanted you to know: $object $is_are $factoid"
434 );
435 }
436  
437 sub search_factoid {
438 4 ▶ my ( $self, @terms ) = @_;
Subroutine search_factoid: called CC=3 SCAR=11.0
Pod undocumented
439 4 my @keys;
440 4 for (@terms) {
441 push @keys,
442 6 ▶ map { my $term = $_; $term =~ s/^${\(INFO)}// ? $term : () }
Branch: 1 branch
BranchTrueFalseTotal
$term =~ s/^${\'infobot_';}// ? : 5 0 5
443 $self->store_keys(
444 limit => $self->get("user_num_results"),
445 res => ["$_"]
446 );
447 }
448 4 return @keys;
449 }
450  
451 sub parseFeed {
452 2 ▶ my ( $self, $url ) = @_;
Subroutine parseFeed: called CC=8 SCAR=31.4
Pod undocumented
453  
454 2 my @items;
455 2 eval {
456 2 my $ua = LWP::UserAgent->new();
457 2 $ua->timeout( $self->get('user_http_timeout') );
458 2 $ua->env_proxy;
459 2 my $feed;
460 2 my $response = $ua->get($url);
461 2 ▶ if ( $response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) { } 2 0 2
462 2 ▶ $feed = XML::Feed->parse( \$response->content() )
Branch: 1 branch
BranchTrueFalseTotal
$feed = "XML::Feed"->parse(\$response->content) or die "XML::Feed"->errstr . "\n" 1 0 1
463 or die XML::Feed->errstr . "\n";
464 }
465 else {
466 0 die $response->status_line() . "\n";
467 }
468 0 my @entries = $feed->entries();
469 0 my $max_items = $self->get('user_rss_items');
470 0 ▶ if ( $max_items and $max_items < @entries ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max_items and $max_items < @entries) 0 0 0
Condition0%0/3
$max_items and $max_items < @entries
!ll&&!rl&&r
000
MC/DC0%0/2
$max_items and $max_items < @entries
$max_items $max_items < @entries
Truth table0%0/3
A and B
A= $max_itemsB= $max_items < @entries
ABresult
0X0
100
111
471 0 splice( @entries, $max_items );
472 }
473 0 @items = map { $_->title } @entries;
474 };
475 2 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 2 0 2
476 2 chomp $@;
477 2 return "<< Error parsing RSS from $url: $@ >>";
478 }
479  
480 0 my $ret;
481 0 foreach my $title (@items) {
482 0 $title =~ s/\s+/ /;
483 0 $title =~ s/\n//g;
484 0 $title =~ s/\s+$//;
485 0 $title =~ s/^\s+//;
486 0 $ret .= "${title}; ";
487 }
488 0 $ret =~ s/\s*;\s*$//;
489 0 ▶ return ( $ret =~ m/^<(reply|action)>/ ? $ret : "<reply>$ret" );
Branch: 1 branch
BranchTrueFalseTotal
$ret =~ /^<(reply|action)>/ ? : 0 0 0
490 }
491  
492 # We've been replied to by an infobot.
493 sub infobot_reply {
494 0 ▶ my ( $self, $id, $return, $mess ) = @_;
Subroutine infobot_reply: not called CC=2 SCAR=17.9
Pod undocumented
495  
496 # get the message that caused the ask initially, so we can reply to it
497 # if there wasn't one, just give up.
498 0 ▶ my $infobot_data = $self->{remote_infobot}{$id} or return 1;
Branch: 1 branch
BranchTrueFalseTotal
my $infobot_data = $self->{"remote_infobot"}{$id} or return 1 0 0 0
499  
500 # this is the string that the other infobot returned to us.
501 0 my ( $object, $db, $factoid ) = ( $return =~ /^(.*) =(\w+)=> (.*)$/ );
502  
503 0 $self->set_factoid( $mess->{who}, $object, $db, $factoid );
504  
505 # reply to the original request saying 'we got it'
506 $self->bot->say(
507 channel => $infobot_data->{channel},
508 who => $infobot_data->{who},
509 0 body => "Learnt about $object from $mess->{who}",
510 );
511  
512 0 return 1;
513  
514 }
515  
516 2 1;
517  
518 __END__
519  
520 =head1 NAME
521  
522 Bot::BasicBot::Pluggable::Module::Infobot - infobot clone redone in B::B::P.
523  
524 =head1 VERSION
525  
526 version 1.11
527  
528 =head1 SYNOPSIS
529  
530 Does infobot things - basically remembers and returns factoids. Will ask
531 another infobot about factoids that it doesn't know about, if you want. Due
532 to persistent heckling from the peanut gallery, does things pretty much
533 exactly like the classic infobot, even when they're not necessarily that
534 useful (for example, saying "Okay." rather than "OK, water is wet."). Further
535 infobot backwards compatibility is available through additional packages
536 such as L<Bot::BasicBot::Pluggable::Module::Foldoc>.
537  
538 =head1 IRC USAGE
539  
540 The following examples assume you're running Infobot with its defaults settings,
541 which require the bot to be addressed before learning factoids or answering
542 queries. Modify these settings with the Vars below.
543  
544 <user> bot: water is wet.
545 <bot> user: okay.
546 <user> bot: water?
547 <bot> user: water is wet.
548 <user> bot: water is also blue.
549 <bot> user: okay.
550 <user> bot: water?
551 <bot> user: water is wet or blue.
552 <user> bot: no, water is translucent.
553 <bot> user: okay.
554 <user> bot: water?
555 <bot> user: water is translucent.
556 <user> bot: forget water.
557 <bot> user: I forgot about water.
558 <user> bot: water?
559 <bot> user: No clue. Sorry.
560  
561 A fact that begins with "<reply>" will have the "<noun> is" stripped:
562  
563 <user> bot: what happen is <reply>somebody set us up the bomb.
564 <bot> user: okay.
565 <user> bot: what happen?
566 <bot> user: somebody set us up the bomb.
567  
568 A fact that begins "<action>" will be emoted as a response:
569  
570 <user> bot: be funny is <action>dances silly.
571 <bot> user: okay.
572 <user> bot: be funny?
573 * bot dances silly.
574  
575 Pipes ("|") indicate different possible answers, picked at random:
576  
577 <user> bot: dice is one|two|three|four|five|six
578 <bot> user: okay.
579 <user> bot: dice?
580 <bot> user: two.
581 <user> bot: dice?
582 <bot> user: four.
583
584 You can also use RSS feeds as a response:
585  
586 <user> bot: jerakeen.org is <rss="http://jerakeen.org/rss">.
587 <bot> user: okay.
588 <user> bot: jerakeen.org?
589 <bot> user: jerakeen.org is <item>; <item>; etc....
590  
591 You can also ask the bot to learn a factoid from another bot, as follows:
592  
593 <user> bot: ask bot2 about fact.
594 <bot> user: asking bot2 about fact...
595 <user> bot: fact?
596 <bot> user: fact is very boring.
597  
598 Finally, you can privmsg the bot to search for particular facts:
599  
600 <user> search for options.
601 <bot> I know about 'options indexes', 'charsetoptions override', etc....
602  
603 =head1 METHODS
604  
605  
606  
607 =head1 VARS
608  
609 =over 4
610  
611 =item min_length
612  
613 Defaults to 3; the minimum length a factoid, or inquiry, must be before recognizing it.
614  
615 =item max_length
616  
617 Defaults to 25; the maximum length a factoid, or inquiry, can be before ignoring it.
618  
619 =item num_results
620  
621 Defaults to 20; the number of facts to return for "search for <term>" privmsg.
622  
623 =item passive_answer
624  
625 Defaults to 0; when enabled, the bot will answer factoids without being addressed.
626  
627 =item passive_learn
628  
629 Defaults to 0; when enabled, the bot will learn factoids without being addressed.
630  
631 =item require_question
632  
633 Defaults to 1; determines whether the bot requires a question mark before
634 responding to a factoid. When enabled, the question mark is required (ie. "water?").
635 When disabled, the question mark is entirely optional (ie. "water" would also
636 produce a response).
637  
638 =item stopwords
639  
640 A comma-, space-, or pipe- separated list of words the bot should not learn or
641 answer. This prevents such insanity as the learning of "where is the store?" and
642 "how is your mother?" The default list of stopwords contains "here", "how", "it",
643 "something", "that", "this", "what", "when", "where", "which", "who" and "why").
644  
645 =item unknown_responses
646  
647 A pipe-separated list of responses the bot will randomly choose from when it
648 doesn't know the answer to a question. The default list of response contains
649 "Dunno.", "I give up.", "I have no idea.", "No clue. Sorry.", "Search me, bub.",
650 and "Sorry, I don't know."
651  
652 =item allow_searching
653  
654 Defaults to 0.
655  
656 Searching on large factoid lists is ... problematic.
657  
658 =item http_timeout
659  
660 Time in seconds for an http request to timeout. When this value is
661 set to a very high value, a slow site can disconnect a bot by
662 blocking it. Defaults to 10.
663  
664 =item rss_items
665  
666 Maximal numbers of items returns when using RSS feeds. Defaults to 5.
667  
668 =back
669  
670 =head1 BUGS
671  
672 "is also" doesn't work on <reply>s (ie. "<bot> cheetahs! or <reply>monkies.")
673  
674 "is also" doesn't work on <action>s (same as the previous bug, hobo.)
675  
676 The pipe syntax for random replies doesn't actually work. At all. Um.
677  
678 We should probably make a "choose_random_response" function.
679  
680 "<bot>?" fails, due to removal of <bot> name from $mess->body.
681  
682 "ask" syntax doesn't work in a private message.
683  
684 The tab stops are set to 2, not 4. OHMYGOD.
685  
686 If a "search" fails, the bot doesn't tell you.
687  
688 "search" is case-sensitive.
689  
690 If Title module is loaded, <rss> factoids don't work cos of told/fallback.
691  
692 =head1 REQUIREMENTS
693  
694 URI
695  
696 L<LWP::Simple>
697  
698 L<XML::Feed>
699  
700 =head1 AUTHOR
701  
702 Mario Domgoergen <mdom@cpan.org>
703  
704 This program is free software; you can redistribute it
705 and/or modify it under the same terms as Perl itself.
« blib/lib/Bot/BasicBot/Pluggable/Module/Google.pm ↑ Summary blib/lib/Bot/BasicBot/Pluggable/Module/Loader.pm »