Debian Codegraph @

t/04_plugins/12_autojoin/04_kicked.t

raw
1 use strict;
2 use warnings FATAL => 'all';
3 use lib 't/inc';
4 use POE;
5 use POE::Component::IRC;
6 use POE::Component::IRC::Plugin::AutoJoin;
7 use POE::Component::Server::IRC;
8 use Test::More tests => 8;
9
10 my $bot1 = POE::Component::IRC->spawn(
11 Flood => 1,
12 plugin_debug => 1,
13 );
14 my $bot2 = POE::Component::IRC->spawn(
15 Flood => 1,
16 plugin_debug => 1,
17 );
18 my $ircd = POE::Component::Server::IRC->spawn(
19 Auth => 0,
20 AntiFlood => 0,
21 );
22
23 $bot2->plugin_add(AutoJoin => POE::Component::IRC::Plugin::AutoJoin->new(
24 Channels => [ '#testchannel' ],
25 RejoinOnKick => 1,
26 Rejoin_delay => 1,
27 ));
28
29 POE::Session->create(
30 package_states => [
31 main => [qw(
32 _start
33 ircd_listener_add
34 ircd_listener_failure
35 _shutdown
36 irc_001
37 irc_join
38 irc_kick
39 irc_disconnected
40 )],
41 ],
42 );
43
44 $poe_kernel->run();
45
46 sub _start {
47 my ($kernel) = $_[KERNEL];
48 $ircd->yield('register', 'all');
49 $ircd->yield('add_listener');
50 $kernel->delay(_shutdown => 60, 'Timed out');
51 }
52
53 sub ircd_listener_failure {
54 my ($kernel, $op, $reason) = @_[KERNEL, ARG1, ARG3];
55 $kernel->yield('_shutdown', "$op: $reason");
56 }
57
58 sub ircd_listener_add {
59 my ($kernel, $heap, $port) = @_[KERNEL, HEAP, ARG0];
60 $heap->{port} = $port;
61
62 $bot1->yield(register => 'all');
63 $bot1->yield(connect => {
64 nick => 'TestBot1',
65 server => '127.0.0.1',
66 port => $port,
67 });
68 }
69
70 sub irc_001 {
71 my $irc = $_[SENDER]->get_heap();
72 pass($irc->nick_name(). ' logged in');
73
74 if ($irc == $bot1) {
75 $irc->yield(join => '#testchannel');
76
77 $bot2->yield(register => 'all');
78 $bot2->yield(connect => {
79 nick => 'TestBot2',
80 server => '127.0.0.1',
81 port => $_[HEAP]->{port},
82 });
83 }
84 }
85
86 sub irc_join {
87 my ($sender, $heap, $who, $where) = @_[SENDER, HEAP, ARG0, ARG1];
88 my $nick = ( split /!/, $who )[0];
89 my $irc = $sender->get_heap();
90 return if $nick ne $irc->nick_name();
91
92 is($where, '#testchannel', "$nick joined $where");
93
94 if ($nick eq 'TestBot2') {
95 $heap->{joined}++;
96
97 if ($heap->{joined} == 1) {
98 $bot1->yield(kick => $where, 'TestBot2');
99 }
100 else {
101 $bot1->yield('quit');
102 $bot2->yield('quit');
103 }
104 }
105 }
106
107 sub irc_kick {
108 my ($sender, $where, $victim) = @_[SENDER, ARG1, ARG2];
109 my $irc = $sender->get_heap();
110 return if $victim ne $irc->nick_name();
111 pass("$victim kicked from $where");
112 }
113
114 sub irc_disconnected {
115 my ($kernel, $heap) = @_[KERNEL, HEAP];
116 pass('irc_disconnected');
117 $heap->{count}++;
118 $kernel->yield('_shutdown') if $heap->{count} == 2;
119 }
120
121 sub _shutdown {
122 my ($kernel, $error) = @_[KERNEL, ARG0];
123 fail($error) if defined $error;
124
125 $kernel->alarm_remove_all();
126 $ircd->yield('shutdown');
127 $bot1->yield('shutdown');
128 $bot2->yield('shutdown');
129 }
130