blib/lib/Bot/IKCBot/Pluggable.pm untested

filter: Statementstmt 0.0% 0 / 69 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 107 CC 6 SCAR 27.8
CC
6
Cov
0%
notice
42.0
run
2.0
start_state_ikc
2.0
CRAP 16.1
1 package Bot::IKCBot::Pluggable;
2  
3 use warnings;
4 use strict;
5  
6 our $VERSION = '0.02';
7  
8 use base qw( Bot::BasicBot::Pluggable );
9 use POE;
10 use POE::Session;
11 use POE::Component::IKC::Server;
12  
13 our $STATE_TABLE = {
14 say => 'hearsay',
15 notice => 'hearnotice',
16 };
17  
18 sub run {
19 my $self = shift;
20  
21 POE::Component::IKC::Server::create_ikc_server(
22 ip => $self->{ikc_ip},
23 port => $self->{ikc_port},
24 name => 'IKC',
25 );
26  
27 POE::Session->create(
28 object_states => [
29 $self => {
30 _start => 'start_state_ikc',
31 %$STATE_TABLE,
32 }
33 ]
34 );
35  
36 $self->SUPER::run($self);
37 }
38  
39 sub start_state_ikc {
40 my($self, $kernel, $session) = @_[ OBJECT, KERNEL, SESSION ];
41 $self->{kernel} = $kernel;
42 $self->{session} = $session;
43  
44 $kernel->alias_set($self->{ALIASNAME}."_IKC");
45  
46 $kernel->call( IKC => publish => $self->{ALIASNAME}."_IKC" => [keys %$STATE_TABLE] );
47 }
48  
49 sub hearsay {
50 my($self, $arg) = @_[ OBJECT, ARG0 ];
51 $self->say($arg);
52 }
53  
54 sub hearnotice {
55 my($self, $arg) = @_[ OBJECT, ARG0 ];
56 $self->notice($arg);
57 }
58  
59 # just Bot::BasicBot::say =~ s/privmsg/notice/g
60 sub notice {
61 # If we're called without an object ref, then we're handling saying
62 # stuff from inside a forked subroutine, so we'll freeze it, and toss
63 # it out on STDOUT so that POE::Wheel::Run's handler can pick it up.
64 if ( !ref( $_[0] ) ) {
65 print $_[0] . "\n";
66 return 1;
67 }
68  
69 # Otherwise, this is a standard object method
70  
71 my $self = shift;
72 my $args;
73 if (ref($_[0])) {
74 $args = shift;
75 } else {
76 my %args = @_;
77 $args = \%args;
78 }
79  
80 my $body = $args->{body};
81  
82 # add the "Foo: bar" at the start
83 $body = "$args->{who}: $body"
84 if ( $args->{channel} ne "msg" and $args->{address} );
85  
86 # work out who we're going to send the message to
87 my $who = ( $args->{channel} eq "msg" ) ? $args->{who} : $args->{channel};
88  
89 unless ( $who && $body ) {
90 print STDERR "Can't NOTICE without target and body\n";
91 print STDERR " called from ".([caller]->[0])." line ".([caller]->[2])."\n";
92 print STDERR " who = '$who'\n body = '$body'\n";
93 return;
94 }
95  
96 # if we have a long body, split it up..
97 local $Text::Wrap::columns = 300;
98 local $Text::Wrap::unexpand = 0; # no tabs
99 my $wrapped = Text::Wrap::wrap('', '..', $body); # =~ m!(.{1,300})!g;
100 # I think the Text::Wrap docs lie - it doesn't do anything special
101 # in list context
102 my @bodies = split(/\n+/, $wrapped);
103  
104 # post an event that will send the message
105 for my $body (@bodies) {
106 my ($who, $body) = $self->charset_encode($who, $body);
107 #warn "$who => $body\n";
108 $poe_kernel->post( $self->{IRCNAME}, 'notice', $who, $body );
109 }
110 }
111  
112 # use notice instead of say (privmsg) when bot's reply
113 sub reply {
114 my $self = shift;
115 my ($mess, $body) = @_;
116 my %hash = %$mess;
117 $hash{body} = $body;
118 return $self->notice(%hash);
119 }
120  
121 1;
122  
123 __END__