Debian Codegraph @

t/04_plugins/12_autojoin/06_kick_ban_password.t

raw
1 use strict;
2 use warnings FATAL => 'all';
3 use lib 't/inc';
4 use POE;
5 use POE::Component::IRC::State;
6 use POE::Component::IRC::Plugin::AutoJoin;
7 use POE::Component::Server::IRC;
8 use Test::More tests => 17;
9
10 my $bot1 = POE::Component::IRC::State->spawn(
11 Flood => 1,
12 plugin_debug => 1,
13 );
14 my $bot2 = POE::Component::IRC::State->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' => 'secret' },
25 RejoinOnKick => 1,
26 Rejoin_delay => 1,
27 Retry_when_banned => 1,
28 ));
29
30 POE::Session->create(
31 package_states => [
32 main => [qw(
33 _start
34 ircd_listener_add
35 ircd_listener_failure
36 _shutdown
37 irc_001
38 irc_join
39 irc_disconnected
40 irc_chan_mode
41 irc_kick
42 )],
43 ],
44 );
45
46 $poe_kernel->run();
47
48 sub _start {
49 my ($kernel) = $_[KERNEL];
50
51 $ircd->yield('register', 'all');
52 $ircd->yield('add_listener');
53 $kernel->delay(_shutdown => 60, 'Timed out');
54 }
55
56 sub ircd_listener_failure {
57 my ($kernel, $op, $reason) = @_[KERNEL, ARG1, ARG3];
58 $kernel->yield('_shutdown', "$op: $reason");
59 }
60
61 sub ircd_listener_add {
62 my ($kernel, $heap, $port) = @_[KERNEL, HEAP, ARG0];
63 $heap->{port} = $port;
64
65 $bot1->yield(register => 'all');
66 $bot1->yield(connect => {
67 nick => 'TestBot1',
68 server => '127.0.0.1',
69 port => $port,
70 });
71
72 }
73
74 sub irc_001 {
75 my $irc = $_[SENDER]->get_heap();
76 pass($irc->nick_name() . ' logged in');
77
78 if ($irc == $bot1) {
79 $irc->yield(join => '#testchannel');
80 $irc->yield(join => '#testchannel2');
81 }
82 }
83
84 sub irc_join {
85 my ($sender, $heap, $who, $where) = @_[SENDER, HEAP, ARG0, ARG1];
86 my $nick = ( split /!/, $who )[0];
87 my $irc = $sender->get_heap();
88 return if $nick ne $irc->nick_name();
89
90 like($where, qr/#testchannel2?/, "$nick joined $where");
91
92 if ($nick eq 'TestBot1') {
93 if ($where eq '#testchannel') {
94 $bot1->yield(mode => $where, '+k secret');
95 }
96 else {
97 $bot1->yield(mode => $where, '+k secret2');
98 }
99 }
100 elsif ($where eq '#testchannel') {
101 $heap->{bot2_joined}++;
102 if ($heap->{bot2_joined} == 1) {
103 $bot1->yield(mode => $where, '+k topsecret');
104 }
105 else {
106 $bot2->yield(join => '#testchannel2', 'secret2');
107 }
108 }
109 else {
110 $heap->{bot2_joined_2}++;
111 if ($heap->{bot2_joined_2} == 1) {
112 $bot1->yield(kick => $where, 'TestBot2');
113 }
114 else {
115 $bot1->yield('quit');
116 $bot2->yield('quit');
117 }
118 }
119 }
120
121 sub irc_chan_mode {
122 my ($heap, $where, $mode) = @_[HEAP, ARG1, ARG2];
123 return if $bot1 != $_[SENDER]->get_heap();
124
125 if ($mode eq '+k') {
126 pass("$where key set");
127 $heap->{key_set}++;
128
129 if ($heap->{key_set} == 2) {
130 $bot2->yield(register => 'all');
131 $bot2->yield(connect => {
132 nick => 'TestBot2',
133 server => '127.0.0.1',
134 port => $heap->{port},
135 });
136 }
137 elsif ($heap->{key_set} == 3) {
138 $bot1->yield(mode => $where, '+b TestBot2!*@*');
139 $bot1->yield(kick => $where, 'TestBot2');
140 }
141 }
142 elsif ($mode eq '+b') {
143 pass('Ban set');
144 }
145 elsif ($mode eq '-b') {
146 pass('Ban removed');
147 }
148 }
149
150 sub irc_kick {
151 my ($sender, $where, $victim) = @_[SENDER, ARG1, ARG2];
152 my $irc = $sender->get_heap();
153 return if $victim ne $irc->nick_name();
154 pass("$victim kicked from $where");
155 $bot1->delay([mode => $where, '-b TestBot2!*@*'], 4);
156 }
157
158 sub irc_disconnected {
159 my ($kernel, $heap) = @_[KERNEL, HEAP];
160 pass('irc_disconnected');
161 $heap->{count}++;
162 $kernel->yield('_shutdown') if $heap->{count} == 2;
163 }
164
165 sub _shutdown {
166 my ($kernel, $error) = @_[KERNEL, ARG0];
167 fail($error) if defined $error;
168
169 $kernel->alarm_remove_all();
170 $ircd->yield('shutdown');
171 $bot1->yield('shutdown');
172 $bot2->yield('shutdown');
173 }
174