1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
use strict;
use warnings FATAL => 'all';
use POE qw(Wheel::SocketFactory);
use POE::Component::IRC;
use Socket qw(AF_INET inet_ntoa SOCK_STREAM unpack_sockaddr_in);
use Test::More tests => 5;
my $bot = POE::Component::IRC->spawn();
my $server = 'irc.libera.chat';
my $nick = "PoCoIRC" . $$;
POE::Session->create(
package_states => [
main => [qw(
_start
_shutdown
_success
_failure
_irc_connect
_time_out
_default
irc_registered
irc_connected
irc_001
irc_465
irc_error
irc_socketerr
irc_disconnected
)],
],
);
$poe_kernel->run();
sub _start {
my ($kernel, $heap) = @_[KERNEL, HEAP];
# Connect manually first to see if our internets are working.
# If not, we can pass the error info to Test::More's skip()
$heap->{sockfactory} = POE::Wheel::SocketFactory->new(
SocketDomain => AF_INET,
SocketType => SOCK_STREAM,
SocketProtocol => 'tcp',
RemoteAddress => $server,
RemotePort => 6667,
SuccessEvent => '_success',
FailureEvent => '_failure',
);
$kernel->delay(_time_out => 40);
$heap->{numeric} = 0;
$heap->{tests} = 5;
}
sub _success {
my ($kernel, $heap) = @_[KERNEL, HEAP];
$heap->{address} = inet_ntoa($_[ARG1]);
$kernel->delay('_time_out');
delete $heap->{sockfactory};
$kernel->yield('_irc_connect');
}
sub _failure {
my ($kernel, $heap, $operation, $errnum, $errstr)
= @_[KERNEL, HEAP, ARG0..ARG2];
delete $heap->{sockfactory};
$kernel->yield(_shutdown => "$operation $errnum $errstr");
}
sub _time_out {
delete $_[HEAP]->{sockfactory};
$poe_kernel->yield(_shutdown => 'Connection timed out');
}
sub _shutdown {
my ($heap, $skip) = @_[HEAP, ARG0];
if ( !$skip && !$heap->{numeric} ) {
$skip = 'Never received a numeric IRC event';
}
SKIP: {
skip $skip, $heap->{tests} if $skip;
}
$poe_kernel->alarm_remove_all();
$bot->yield('shutdown');
}
sub _irc_connect {
my ($heap) = $_[HEAP];
$bot->yield(register => 'all');
$bot->yield(connect => {
server => $heap->{address},
nick => $nick,
});
}
sub irc_registered {
my ($heap, $irc) = @_[HEAP, ARG0];
isa_ok($irc, 'POE::Component::IRC');
$heap->{tests}--;
}
sub irc_connected {
TODO: {
local $TODO = "K-lines or other unforeseen issues could derail this test";
pass('Connected');
};
$_[HEAP]->{tests}--;
}
sub irc_socketerr {
my ($kernel) = $_[KERNEL];
$kernel->yield(_shutdown => $_[ARG0] );
}
sub irc_001 {
my $irc = $_[SENDER]->get_heap();
TODO: {
local $TODO = "K-lines or other unforeseen issues could derail this test";
pass('Logged in');
};
$_[HEAP]->{numeric}++;
$_[HEAP]->{tests}--;
$irc->yield('quit');
}
sub irc_465 {
my $irc = $_[SENDER]->get_heap();
TODO: {
local $TODO = "Hey we is K-lined";
pass('ERR_YOUREBANNEDCREEP');
};
$_[HEAP]->{numeric}++;
$_[HEAP]->{tests}--;
}
sub irc_error {
TODO: {
local $TODO = "K-lines or other unforeseen issues could derail this test";
pass('irc_error');
};
$_[HEAP]->{tests}--;
}
sub irc_disconnected {
my ($kernel, $heap) = @_[KERNEL, HEAP];
TODO: {
local $TODO = "K-lines or other unforeseen issues could derail this test";
pass('Disconnected');
};
$heap->{tests}--;
$kernel->yield('_shutdown');
}
sub _default {
my ($event, $args) = @_[ARG0 .. $#_];
return unless $event =~ m!^irc_\d+!;
$_[HEAP]->{numeric}++;
return;
}
|