1use strict;
2use warnings;
3use POE qw(Wheel::SocketFactory);
4use POE::Component::IRC;
5use POE::Component::Server::IRC;
6use Test::More tests => 4;
7
8my $bot1 = POE::Component::IRC->spawn(flood => 1);
9my $ircd = POE::Component::Server::IRC->spawn(
10    Auth         => 0,
11    AntiFlood    => 0,
12    plugin_debug => 1,
13);
14
15POE::Session->create(
16    package_states => [
17        main => [qw(
18            _start
19            ircd_listener_add
20            ircd_listener_failure
21            _shutdown
22            irc_001
23            irc_433
24            irc_error
25            irc_disconnected
26        )],
27    ],
28);
29
30$poe_kernel->run();
31
32sub _start {
33    my ($kernel, $heap) = @_[KERNEL, HEAP];
34    $ircd->yield('register', 'all');
35    $ircd->add_listener();
36    $ircd->add_auth(
37        mask     => "*",
38        password => 'foo',
39    );
40    $kernel->delay(_shutdown => 60, 'Timed out');
41}
42
43sub ircd_listener_failure {
44    my ($kernel, $op, $reason) = @_[KERNEL, ARG1, ARG3];
45    $kernel->yield('_shutdown', "$op: $reason");
46}
47
48sub ircd_listener_add {
49    my ($kernel, $port) = @_[KERNEL, ARG0];
50
51    $bot1->yield(register => 'all');
52    $bot1->yield(connect => {
53        nick    => 'TestBot1',
54        server  => '127.0.0.1',
55        port    => $port,
56        ircname => 'Test test bot',
57    });
58}
59
60sub irc_001 {
61    my $irc = $_[SENDER]->get_heap();
62    fail("Shouldn't be able to log in");
63}
64
65sub irc_error {
66    my ($error) = $_[ARG0];
67    like($error, qr/not authorized/, 'Not authorized to connect');
68}
69
70sub irc_433 {
71    my $irc = $_[SENDER]->get_heap();
72    fail("The nick shouldn't be taken");
73}
74
75sub irc_disconnected {
76    my ($kernel, $sender, $heap) = @_[KERNEL, SENDER, HEAP];
77    my $irc = $sender->get_heap();
78
79    pass('Disconnected');
80    $heap->{count}++;
81    $irc->yield('connect') if $heap->{count} == 1;
82    $kernel->yield('_shutdown') if $heap->{count} == 2;
83}
84
85sub _shutdown {
86    my ($kernel) = $_[KERNEL];
87
88    $kernel->alarm_remove_all();
89    $ircd->yield('shutdown');
90    $bot1->yield('shutdown');
91}
92