Amethyst/Brain/Infobot/Module/Insult.pm untested

filter: Statementstmt 0.0% 0 / 47 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 64 CC 1 SCAR 6.9
CC
1
Cov
0%
new
2.0
action
2.0
handler_read
2.0
CRAP 2.0
1 package Amethyst::Brain::Infobot::Module::Insult;
2  
3 use strict;
4 use vars qw(@ISA);
5 use Socket qw(AF_INET SOCK_STREAM);
6 use POE qw(Wheel::SocketFactory Wheel::ReadWrite
7 Filter::Line Driver::SysRW);
8 use Amethyst::Brain::Infobot::Module;
9  
10 @ISA = qw(Amethyst::Brain::Infobot::Module);
11  
12 sub new {
13 my $class = shift;
14  
15 my $self = $class->SUPER::new(
16 Name => 'Insult',
17 Regex => qr/^insult (.*)$/i,
18 Usage => 'insult (.*)',
19 Description => "Insult someone.",
20 @_
21 );
22  
23 return bless $self, $class;
24 }
25  
26 sub action {
27 my ($self, $message) = @_;
28  
29 my %states = map { $_ => "handler_$_" } qw(
30 _start connect failure read
31 );
32  
33 print STDERR "Creating child session for insult\n";
34  
35 POE::Session->create(
36 package_states => [ ref($self) => \%states ],
37 args => [ $self, $message, $1 ],
38 );
39  
40 return 1;
41 }
42  
43 sub handler_read {
44 my ($kernel, $heap, $session, $data) =
45 @_[KERNEL, HEAP, SESSION, ARG0];
46  
47 my $name = $heap->{Name};
48 $data =~ s/You are/$name is/;
49 chomp($data);
50  
51 my $reply = $heap->{Module}->reply_to($heap->{Message}, $data);
52 $reply->send;
53  
54 delete $heap->{ReadWrite};
55 }
56  
57 sub handler_connect {
58 my ($kernel, $heap, $session, $socket) =
59 @_[KERNEL, HEAP, SESSION, ARG0];
60  
61 delete $heap->{SocketFactory};
62  
63 my $wheel = POE::Wheel::ReadWrite->new(
64 Handle => $socket,
65 Driver => POE::Driver::SysRW->new(),
66 Filter => POE::Filter::Line->new(),
67 InputEvent => 'read',
68 );
69  
70 $heap->{ReadWrite} = $wheel;
71 }
72  
73 sub handler_failure {
74 my ($kernel, $heap, $session, $reason) =
75 @_[KERNEL, HEAP, SESSION, ARG0, ARG2];
76  
77 my $reply = $heap->{Module}->reply_to($heap->{Message},
78 "Insult failure: $reason");
79 $reply->send;
80 }
81  
82 sub handler__start {
83 my ($kernel, $heap, $session, $module, $message, $name) =
84 @_[KERNEL, HEAP, SESSION, ARG0, ARG1, ARG2];
85  
86 $heap->{Module} = $module;
87 $heap->{Message} = $message;
88 $heap->{Name} = $name;
89  
90 my $wheel = new POE::Wheel::SocketFactory(
91 SocketDomain => AF_INET,
92 SocketType => SOCK_STREAM,
93 SocketProtocol => 'tcp',
94 RemoteAddress => 'insulthost.colorado.edu',
95 RemotePort => 1695,
96 SuccessEvent => 'connect',
97 FailureEvent => 'failure',
98 );
99  
100 $heap->{SocketFactory} = $wheel;
101 }
102  
103 1;