blib/lib/Net/Ewtoo/Bot.pm

filter: Statementstmt 13.5% 16 / 118 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 16.6% 4 / 24 Podpod 0.0% 0 / 18 totaltotal 9.9% 20 / 201 CC 19 SCAR 28.8
CC
19
Cov
10%
login
42.0
listen
42.0
logout
30.0
CRAP 17.8
↑ Summary
1 #!/usr/bin/perl
2 # $Id: Bot.pm,v 1.14 2002/04/27 19:25:32 jodrell Exp $
3 # Copyright (c) 2002 Gavin Brown. All rights reserved. This program is
4 # free software; you can redistribute it and/or modify it under the same
5 # terms as Perl itself.
6  
7 1 ▶use IO::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 package Net::Ewtoo::Bot;
12 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 $VERSION = '0.16';
14  
15 =pod
16  
17 =head1 NAME
18  
19 Net::Ewtoo::Bot - a Ewtoo-compatible talker robot client module
20  
21 =head1 SYNOPSIS
22  
23 use Net::Ewtoo::Bot;
24  
25 my $NAME = 'jodbot';
26  
27 my $bot = new Net::Ewtoo::Bot;
28  
29 $bot->add_trigger("(.+?) says '$NAME, (.+?)'", \&handle_say);
30 $bot->set_delay('range', 0, 5);
31  
32 $bot->login($host, $port, $user, $pass);
33 $bot->say("Hi! I'm the $NAME robot!");
34  
35 $bot->listen();
36  
37 $bot->logout();
38  
39 exit;
40  
41 sub handle_say {
42 my ($sayer, $said) = @_;
43 if ($said eq 'hello') {
44 $bot->say("Why hello $sayer!");
45 } elsif ($said eq 'please go away') {
46 $bot->say("OK, bye!");
47 $bot->logout();
48 }
49 return;
50 }
51  
52 =head1 DESCRIPTION
53  
54 Net::Ewtoo::Bot provides an object-oriented interface to Ewtoo (I<http://www.ewtoo.org/>) type talker systems. The module provides support for the most common Ewtoo talker commands, as well as input pattern matching and callback triggers and timers.
55  
56 =head1 INSTALLATION
57  
58 To install this package, just change to the directory which you created by untarring the package, and type the following:
59  
60 perl Makefile.PL
61 make test
62 make
63 make install
64  
65 This will copy Bot.pm to your perl library directory for use by all perl scripts. You probably must be root to do this, unless you have installed a personal copy of perl.
66  
67 =head1 METHODS
68  
69 $bot->login($host, $port, $user, $pass);
70  
71 This logs the bot into the $host:$port talker using $user and $pass. The bot will send extra carriage returns to bypass MOTDs and saved messages.
72  
73 Any defined login subroutines are executed at this point.
74  
75 $bot->logout($message);
76  
77 Sends the "QUIT" command (in capitals for compatability with MBA4), and closes the socket. Any defined logout subroutines are executed beforehand. If $message is defined, the bot calls the "mquit" command with $message as its argument.
78  
79 $bot->set_delay($type, $lower, $upper);
80  
81 This method sets the delay between between the calling of a method and its execution. This is useful for adding a realistic delay during communications with another user. $type can be either 'fixed', in which case the delay is always $lower (in seconds) and $upper is ignored, or 'range', in which case the delay will be a random number of seconds between $lower and $upper.
82  
83 $bot->add_trigger($pattern, $callback);
84  
85 This method adds a trigger used by the listen() method. When a line of input is received that matches $pattern, $callback is executed. The arguments to $callback are any captured substrings you define in your pattern, which is a regular perl regexp (without the trailing and leading slashes).
86  
87 $bot->delete_trigger($pattern);
88  
89 Removes the trigger associated with $pattern from the trigger list.
90  
91 $bot->def_login($callback);
92  
93 Specifies a subroutine with $callback that will be executed after the bot logs in.
94  
95 $bot->def_logout($callback);
96  
97 Specifies a subroutine with $callback that will be executed before the bot logs out.
98  
99 $bot->listen($verbose);
100  
101 listen() reads input from the talker and executes triggers as necessary. If $verbose is set to 1, then any input received is printed to STDOUT.
102  
103 $bot->break();
104  
105 $break() sets a flag that tells the listen() method to finish and return.
106  
107 $bot->say($str);
108  
109 A convenience function that makes the bot say $str.
110  
111 $bot->think($str);
112  
113 A convenience function that makes the bot think $str.
114  
115 $bot->shout($str);
116  
117 A convenience function that makes the bot shout $str.
118  
119 $bot->tell($user, $str);
120  
121 A convenience function that makes the bot tell $str to $user.
122  
123 $bot->command($cmd);
124  
125 Allows the calling of an arbitrary talker command.
126  
127 $bot->getline();
128  
129 Reads a single line of input from the talker.
130  
131 =head1 COPYRIGHT
132  
133 This module is (c) 2001,2002 Gavin Brown (I<gavin.brown@uk.com>), with additional input and advice from Richard Lawrence (I<richard@fourteenminutes.com>).
134  
135 This module is licensed under the same terms as Perl itself.
136  
137 =head1 TO DO
138  
139 Implement a timing mechanism for scheduled stuff.
140  
141 =head1 SEE ALSO
142  
143 The Ewtoo website at I<http://www.ewtoo.org>, and the PlayGround Plus website at I<http://pgplus.ewtoo.org/>.
144  
145 =cut
146  
147 1 my $socket = new IO::Handle;
148 1 $socket->autoflush(1);
149  
150 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
151 0 my $class = shift;
152 0 my $self = {};
153 0 bless($self, $class);
154 0 return $self;
155 }
156  
157 0 ▶sub login {
Subroutine login: not called CC=6 SCAR=37.4
Pod undocumented
158 0 my ($self, $host, $port, $user, $pass) = @_;
159 0 ▶ $socket = IO::Socket::INET->new( PeerAddr => $host,
Branch: 1 branch
BranchTrueFalseTotal
$socket = "IO::Socket::INET"->new("PeerAddr", $host, "PeerPort", $port, "Proto", "tcp", "Timeout", 10) or warn "${host}:${port}: $@" and return undef 0 0 0
Condition0%0/5
($socket = 'IO::Socket::INET'->new('PeerAddr', $host, 'PeerPort', $port, 'Proto', 'tcp', 'Timeout', 10)) || warn("${host}:${port}: $@") && (return undef)
l!l&&r!l&&!r
000
warn "${host}:${port}: $@" and return undef
!ll
00
MC/DC0%0/2
($socket = 'IO::Socket::INET'->new('PeerAddr', $host, 'PeerPort', $port, 'Proto', 'tcp', 'Timeout', 10)) || warn("${host}:${port}: $@") && (return undef)
($socket = 'IO::Socket::INET'->new('PeerAddr', $host, 'PeerPort', $port, 'Proto', 'tcp', 'Timeout', 10)) warn "${host}:${port}: $@"
Truth table0%0/3
A || B
A= ($socket = 'IO::Socket::INET'->new('PeerAddr', $host, 'PeerPort', $port, 'Proto', 'tcp', 'Timeout', 10))B= warn "${host}:${port}: $@"
ABresult
1X1
011
000
160 PeerPort => $port,
161 Proto => 'tcp',
162 Timeout => 10 ) or warn("$host:$port: $@") and return undef;
163 0 print $socket "$user\n$pass\n\n";
164 0 ▶ if (defined($self->{_login_subs}) && scalar(@{$self->{_login_subs}}) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"_login_subs"} and scalar @{$self->{"_login_subs"};} > 0) 0 0 0
Condition0%0/3
defined $self->{"_login_subs"} and scalar @{$self->{"_login_subs"};} > 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"_login_subs"} and scalar @{$self->{"_login_subs"};} > 0
defined $self->{"_login_subs"} scalar @{$self->{"_login_subs"};} > 0
Truth table0%0/3
A and B
A= defined $self->{"_login_subs"}B= scalar @{$self->{"_login_subs"};} > 0
ABresult
0X0
100
111
165 0 foreach my $sub(@{$self->{_login_subs}}) {
166 0 &{$sub}();
167 }
168 }
169 0 return;
170 }
171  
172 0 ▶sub logout {
Subroutine logout: not called CC=5 SCAR=34.0
Pod undocumented
173 0 my ($self, $message) = @_;
174 0 ▶ if (defined($self->{_logout_subs}) && scalar(@{$self->{_logout_subs}}) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"_logout_subs"} and scalar @{$self->{"_logout_subs"};} > 0) 0 0 0
Condition0%0/3
defined $self->{"_logout_subs"} and scalar @{$self->{"_logout_subs"};} > 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"_logout_subs"} and scalar @{$self->{"_logout_subs"};} > 0
defined $self->{"_logout_subs"} scalar @{$self->{"_logout_subs"};} > 0
Truth table0%0/3
A and B
A= defined $self->{"_logout_subs"}B= scalar @{$self->{"_logout_subs"};} > 0
ABresult
0X0
100
111
175 0 foreach my $sub(@{$self->{_logout_subs}}) {
176 0 &{$sub}();
177 }
178 }
179 0 ▶ if ($message ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($message ne '') { } 0 0 0
180 0 print $socket "mquit $message\n";
181 } else {
182 0 print $socket "QUIT\n";
183 }
184 0 close($socket);
185 0 return;
186 }
187  
188 0 ▶sub set_delay {
Subroutine set_delay: not called CC=2 SCAR=17.9
Pod undocumented
189 0 my ($self, $mode, $lower, $upper) = @_;
190 0 ▶ if ($mode =~ /^(fixed|range)$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode =~ /^(fixed|range)$/i) { } 0 0 0
191 0 $self->{_mode} = lc($mode);
192 0 $self->{_range} = [$lower, $upper];
193 } else {
194 0 die("Invalid delay mode");
195 }
196 0 return;
197 }
198  
199 0 ▶sub add_trigger {
Subroutine add_trigger: not called CC=1 SCAR=6.9
Pod undocumented
200 0 my ($self, $pattern, $sub) = @_;
201 0 $self->{_patterns}{$pattern} = $sub;
202 0 return;
203 }
204  
205 0 ▶sub delete_trigger {
Subroutine delete_trigger: not called CC=1 SCAR=6.9
Pod undocumented
206 0 my ($self, $pattern) = @_;
207 0 delete $self->{_patterns}{$pattern};
208 0 return;
209 }
210  
211 ### these two methods are kept in but don't work - i haven't come up with a neat
212 ### way to add a timing system that doesn't interrupt the program's flow and
213 ### doesn't use threads.
214  
215 0 ▶sub add_timer {
Subroutine add_timer: not called CC=1 SCAR=6.9
Pod undocumented
216 0 my ($self, $interval, $sub) = @_;
217 0 push(@{$self->{_timers}}, { interval => $interval, sub => $sub, init => time() });
218 0 return scalar(@{$self->{_timers}}) - 1;
219 }
220  
221 0 ▶sub delete_timer {
Subroutine delete_timer: not called CC=2 SCAR=17.9
Pod undocumented
222 0 my ($self, $timer_no) = @_;
223 0 ▶ if (${$self->{_timers}}[$timer_no]) {
Branch: 1 branch
BranchTrueFalseTotal
if (do { $$self{'_timers'} }->[$timer_no]) { } 0 0 0
224 0 undef ${$self->{_timers}}[$timer_no];
225 } else {
226 0 die("Invalid timer ID '$timer_no'");
227 }
228 0 return;
229 }
230  
231 0 ▶sub def_login {
Subroutine def_login: not called CC=1 SCAR=6.9
Pod undocumented
232 0 my ($self, $sub) = shift;
233 0 push(@{$self->{_login_subs}}, $sub);
234 0 return;
235 }
236  
237 0 ▶sub def_logout {
Subroutine def_logout: not called CC=1 SCAR=6.9
Pod undocumented
238 0 my ($self, $sub) = shift;
239 0 push(@{$self->{_logout_subs}}, $sub);
240 0 return;
241 }
242  
243 0 ▶sub listen {
Subroutine listen: not called CC=6 SCAR=37.4
Pod undocumented
244 0 my ($self, $print) = @_;
245 0 $self->{_listen} = 1;
246 0 ▶ while (<$socket>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $socket)) 0 0 0
247 0 $_ = $self->_clean_input($_);
248 0 ▶ print if ($print == 1);
Branch: 1 branch
BranchTrueFalseTotal
if $print == 1 0 0 0
249 0 ▶ return if $self->{_listen} != 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_listen"} != 1 0 0 0
250 0 foreach my $pattern(sort keys %{$self->{_patterns}}) {
251 0 ▶ if (my @matches = (/$pattern/i)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@matches) = /$pattern/i) 0 0 0
252 0 &{$self->{_patterns}{$pattern}}(@matches);
253 }
254 }
255 }
256 0 return;
257 }
258  
259 0 ▶sub break {
Subroutine break: not called CC=1 SCAR=6.9
Pod undocumented
260 0 my $self = shift;
261 0 $self->{_listen} = 0;
262 0 return;
263 }
264  
265 0 ▶sub say {
Subroutine say: not called CC=1 SCAR=6.9
Pod undocumented
266 0 my ($self, $str) = @_;
267 0 $self->command("say $str");
268 0 return;
269 }
270  
271 0 ▶sub think {
Subroutine think: not called CC=1 SCAR=6.9
Pod undocumented
272 0 my ($self, $str) = @_;
273 0 $self->command("think $str");
274 0 return;
275 }
276  
277 0 ▶sub shout {
Subroutine shout: not called CC=1 SCAR=6.9
Pod undocumented
278 0 my ($self, $str) = @_;
279 0 $self->command("shout $str");
280 0 return;
281 }
282  
283 0 ▶sub tell {
Subroutine tell: not called CC=1 SCAR=6.9
Pod undocumented
284 0 my ($self, $target, $str) = @_;
285 0 $self->command("tell $target $str");
286 0 return;
287 }
288  
289 0 ▶sub command {
Subroutine command: not called CC=1 SCAR=6.9
Pod undocumented
290 0 my ($self, $str) = @_;
291 0 $self->_delay();
292 0 print $socket "$str\n";
293 0 return;
294 }
295  
296 0 ▶sub getline {
Subroutine getline: not called CC=1 SCAR=6.9
Pod undocumented
297 0 my $self = shift;
298 0 return $self->_clean_input(<$socket>);
299 }
300  
301 0 ▶sub _delay {
Subroutine _delay: not called CC=3 SCAR=24.8
302 0 my $self = shift;
303 0 ▶ if ($self->{_mode} eq 'fixed') {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'_mode'} eq 'fixed') { } 0 0 0
elsif ($self->{'_mode'} eq 'range') { } 0 0 0
304 0 sleep(${$self->{_range}}[0]);
305 } elsif ($self->{_mode} eq 'range') {
306 0 sleep(${$self->{_range}}[0] + int(rand(${$self->{_range}}[1])));
307 }
308 0 return;
309 }
310  
311 # this kills any ANSI escape characters and colour codes in the supplied string:
312 0 ▶sub _clean_input {
Subroutine _clean_input: not called CC=1 SCAR=6.9
313 0 my ($self, $str) = @_;
314 0 $str =~ s/\[(.+?)m//ig;
315 0 return $str;
316 }
317  
318 1 1;
↑ Summary