Debian Codegraph @

t/04_plugins/12_autojoin/05_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 => 10;
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 ));
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_disconnected
39 irc_chan_mode
40 irc_kick
41 )],
42 ],
43 );
44
45 $poe_kernel->run();
46
47 sub _start {
48 my ($kernel) = $_[KERNEL];
49
50 $ircd->yield('register', 'all');
51 $ircd->yield('add_listener');
52 $kernel->delay(_shutdown => 60, 'Timed out');
53 }
54
55 sub ircd_listener_failure {
56 my ($kernel, $op, $reason) = @_[KERNEL, ARG1, ARG3];
57 $kernel->yield('_shutdown', "$op: $reason");
58 }
59
60 sub ircd_listener_add {
61 my ($kernel, $heap, $port) = @_[KERNEL, HEAP, ARG0];
62 $heap->{port} = $port;
63
64 $bot1->yield(register => 'all');
65 $bot1->yield(connect => {
66 nick => 'TestBot1',
67 server => '127.0.0.1',
68 port => $port,
69 });
70
71 }
72
73 sub irc_001 {
74 my $irc = $_[SENDER]->get_heap();
75 pass($irc->nick_name() . ' logged in');
76
77 if ($irc == $bot1) {
78 $irc->yield(join => '#testchannel');
79 }
80 }
81
82 sub irc_join {
83 my ($sender, $heap, $who, $where) = @_[SENDER, HEAP, ARG0, ARG1];
84 my $nick = ( split /!/, $who )[0];
85 my $irc = $sender->get_heap();
86 return if $nick ne $irc->nick_name();
87
88 is($where, '#testchannel', "$nick joined $where");
89
90 if ($nick eq 'TestBot1') {
91 $bot1->yield(mode => $where, '+k secret');
92 }
93 else {
94 $heap->{bot2_joined}++;
95 if ($heap->{bot2_joined} == 1) {
96 $bot1->yield(mode => $where, '+k topsecret');
97 }
98 else {
99 $bot1->yield('quit');
100 $bot2->yield('quit');
101 }
102 }
103 }
104
105 sub irc_chan_mode {
106 my ($heap, $where, $mode) = @_[HEAP, ARG1, ARG2];
107 return if $bot1 != $_[SENDER]->get_heap();
108
109 if ($mode eq '+k') {
110 pass("$where key set");
111 $heap->{key_set}++;
112
113 if ($heap->{key_set} == 1) {
114 $bot2->yield(register => 'all');
115 $bot2->yield(connect => {
116 nick => 'TestBot2',
117 server => '127.0.0.1',
118 port => $heap->{port},
119 });
120 }
121 else {
122 $bot1->yield(kick => $where, 'TestBot2');
123 }
124 }
125 }
126
127 sub irc_kick {
128 my ($sender, $where, $victim) = @_[SENDER, ARG1, ARG2];
129 my $irc = $sender->get_heap();
130 return if $victim ne $irc->nick_name();
131 pass("$victim kicked from $where");
132 }
133
134 sub irc_disconnected {
135 my ($kernel, $heap) = @_[KERNEL, HEAP];
136 pass('irc_disconnected');
137 $heap->{count}++;
138 $kernel->yield('_shutdown') if $heap->{count} == 2;
139 }
140
141 sub _shutdown {
142 my ($kernel, $error) = @_[KERNEL, ARG0];
143 fail($error) if defined $error;
144
145 $kernel->alarm_remove_all();
146 $ircd->yield('shutdown');
147 $bot1->yield('shutdown');
148 $bot2->yield('shutdown');
149 }
150