File Coverage

blib/lib/POEIKC/Plugin/IRC.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


line stmt bran cond sub pod time code
1             package POEIKC::Plugin::IRC;
2              
3 1     1   5 use strict;
  1         2  
  1         31  
4 1     1   87 use 5.008_001;
  1         3  
  1         40  
5             our $VERSION = '0.00_02';
6              
7 1     1   2964 use Data::Dumper;
  1         11740  
  1         80  
8 1     1   1049 use Class::Inspector;
  1         3909  
  1         40  
9 0           use POE qw(
10             Sugar::Args
11             Loop::IO_Poll
12             Component::IKC::Client
13             Component::IRC::State
14             Component::IRC::Plugin::AutoJoin
15 1     1   441 );
  0            
16              
17             use POEIKC::Daemon::Utility;
18              
19             sub spawn
20             {
21             my $class = shift;
22             my $self = {
23             @_
24             };
25             $class = ref $class if ref $class;
26             bless $self,$class ;
27             POEIKC::Daemon::Utility::_DEBUG_log($self);
28             my $session = POE::Session->create(
29             object_states => [ $self => Class::Inspector->methods(__PACKAGE__) ],
30             );
31             return $session->ID;
32             }
33              
34             sub _start {
35             my $poe = sweet_args ;
36             my $kernel = $poe->kernel ;
37             my $session = $poe->session ;
38             my $object = $poe->object ;
39             my $alias = 'IKC_IRC';
40             $kernel->alias_set($alias);
41              
42             $kernel->sig( HUP => '_stop' );
43             $kernel->sig( INT => '_stop' );
44             $kernel->sig( TERM => '_stop' );
45             $kernel->sig( KILL => '_stop' );
46              
47             $kernel->call(
48             IKC =>
49             publish => $alias, [
50             grep {/_respond$/} @{Class::Inspector->methods(__PACKAGE__)}
51             ],
52             );
53              
54             POEIKC::Daemon::Utility::_DEBUG_log("alias_list"=>$kernel->alias_list());
55             }
56              
57             sub _stop {
58             my $poe = sweet_args;
59             }
60              
61              
62             sub test {
63             my $poe = sweet_args;
64             my $kernel = $poe->kernel;
65             my $heap = $poe->heap;
66             my $session = $poe->session ;
67             my $object = $poe->object ;
68             POEIKC::Daemon::Utility::_DEBUG_log($kernel->alias_list());
69              
70             }
71              
72              
73             sub message_respond {
74             my $poe = sweet_args;
75             my $kernel = $poe->kernel;
76             my $heap = $poe->heap;
77             my $session = $poe->session ;
78             my $object = $poe->object ;
79             my ($request) = @{$poe->args};
80             POEIKC::Daemon::Utility::_DEBUG_log($request);
81             my ($param, $rsvp) = @{$request};
82              
83             POEIKC::Daemon::Utility::_DEBUG_log($param);
84              
85             $kernel->yield(privmsg_notice=>$param);
86             $kernel->post( IKC => post => $rsvp, 1 );
87             }
88              
89             sub status_respond {
90             my $poe = sweet_args;
91             my $kernel = $poe->kernel;
92             my $heap = $poe->heap;
93             my $session = $poe->session ;
94             my $object = $poe->object ;
95             my ($request) = @{$poe->args};
96             my ($param, $rsvp) = @{$request};
97             $kernel->post( IKC => post => $rsvp, $object->{status} );
98             #POEIKC::Daemon::Utility::_DEBUG_log($object);
99             }
100              
101              
102             sub privmsg_notice {
103             my $poe = sweet_args;
104             my $kernel = $poe->kernel;
105             my $heap = $poe->heap;
106             my $session = $poe->session ;
107             my $object = $poe->object ;
108             my $sender = $poe->sender ;
109             my $irc = $object->{irc};
110             my ($pn, $conf, @msg) = (ref $poe->args->[0]->[0] eq 'HASH') ? (undef, @{$poe->args->[0]}) : @{$poe->args->[0]};
111             @msg = @{$msg[0]} if ref $msg[0] eq 'ARRAY';
112              
113             $conf = eval $conf unless (ref $conf);
114             $conf->{ lc $_ } = delete $conf->{$_} for keys %$conf;
115              
116             POEIKC::Daemon::Utility::_DEBUG_log($conf);
117             POEIKC::Daemon::Utility::_DEBUG_log("($pn, $conf, @msg)");
118              
119             my $irchost = $conf->{connect}->{server};
120             my $channel = $conf->{channel};
121              
122             POEIKC::Daemon::Utility::_DEBUG_log($irchost);
123              
124             my @msglist;
125             push @msglist, /\n/ ? split /\n/, $_ : $_ for(@msg);
126             $pn ||= @msglist > 1 ? 'notice' : 'privmsg';
127             $pn = lc $pn;
128             SWITCH: {
129             ( not $object->{status}->{irc_connect} ) and do {
130             $object->{status}->{irc_connect}++;
131             $object->{status}->{join}->{$channel}++;
132             $kernel->yield('irc_connect', $conf);
133             push @{$object->{status}->{msg}->{$channel}}, [$pn,\@msglist];
134             last;};
135             (not $object->{status}->{join}->{$channel}) and do {
136             $object->{status}->{join}->{$channel}++;
137             $irc->yield(join => $channel, $conf->{channel_key});
138             push @{$object->{status}->{msg}->{$channel}}, [$pn,\@msglist];
139             last;};
140             (not $object->{status}->{joined}->{$channel}) and do {
141             push @{$object->{status}->{msg}->{$channel}}, [$pn,\@msglist];
142             last;};
143             (@{$object->{status}->{msg}->{$channel}} >= 1) and do {
144             push @{$object->{status}->{msg}->{$channel}}, [$pn,\@msglist];
145             last;};
146             # else
147             $kernel->yield(put=>$pn, $channel, \@msglist);
148             }
149             POEIKC::Daemon::Utility::_DEBUG_log($object->{status}->{msg});
150             }
151              
152             sub put {
153             my $poe = sweet_args;
154             my $kernel = $poe->kernel;
155             my $object = $poe->object ;
156             my $irc = $object->{irc};
157             my ($pn, $channel, $msg) = @{$poe->args};
158             POEIKC::Daemon::Utility::_DEBUG_log("irc_join: ($pn, $channel, $msg)");
159             $irc->yield($pn => $channel, $_) for @$msg;
160             #$irc->yield($pn => $channel, shift(@$msg));
161             #$kernel->delay(put => 0.1, $pn, $channel, $msg) if @$msg;
162             }
163              
164             sub irc_connect {
165             my $poe = sweet_args;
166             my $kernel = $poe->kernel;
167             my $heap = $poe->heap;
168             my $session = $poe->session ;
169             my $object = $poe->object ;
170             my ($conf) = @{$poe->args};
171             my $channel = $conf->{channel};
172             my $key = $conf->{channel_key};
173             POEIKC::Daemon::Utility::_DEBUG_log($channel);
174              
175             my $irc = POE::Component::IRC::State->spawn(
176             nick => 'poeikc_bot',
177             username => 'poeikc_bot',
178             ircname => 'poeikc_bot',
179             server => 'localhost',
180             port => '6667',
181             #flood=>1,
182             %{$conf->{connect} || {}}
183             );
184             $irc->plugin_add('AutoJoin',
185             POE::Component::IRC::Plugin::AutoJoin->new(
186             Channels => $conf->{channel_key} ? {$channel=>$key} : [ $channel ],
187             )
188             );
189             $irc->yield(register => 'all');
190             $irc->yield('connect');
191              
192             my $irchost = $conf->{connect}->{server};
193             $object->{irc} = $irc;
194             }
195              
196              
197              
198             sub irc_join {
199             my $poe = sweet_args;
200             my $kernel = $poe->kernel;
201             my $heap = $poe->heap;
202             my $session = $poe->session ;
203             my $object = $poe->object ;
204             my $sender = $poe->sender ;
205             my $nick = (split /!/, $_[ARG0])[0];
206             my $channel = $_[ARG1];
207              
208             $object->{status}->{joined}->{$channel}++;
209             my $irc = $sender->get_heap();
210             POEIKC::Daemon::Utility::_DEBUG_log("irc_join: $channel");
211              
212             if ($nick eq $irc->nick_name()) {
213             if($object->{status}->{msg} and $object->{status}->{msg}->{$channel}){
214             while ( my $ary = shift @{$object->{status}->{msg}->{$channel}} ){
215             my ($pn, $msg) = @$ary;
216             POEIKC::Daemon::Utility::_DEBUG_log("irc_join: ($pn, $msg)");
217             $kernel->yield(put=>$pn, $channel, $msg);
218             }
219             }
220             }
221             }
222              
223              
224              
225             1;
226             __END__