3 # If you edit this file, please check carefully that the garbage
4 # collection isn't broken. POE is sometimes too clever for our good
5 # in finding references to sessions, and keeps running even after we
13 use constant DELAY => 2;
14 use constant EXPECT_TIMEOUT => 15;
15 use constant RECONNECT_TIMEOUT => 5;
16 use constant THROTTLED_TIMEOUT => 90;
19 # sub POE::Kernel::ASSERT_DEFAULT () { 1 }
20 # sub POE::Kernel::TRACE_DEFAULT () { 1 }
22 use POE::Component::IRC v5.00;
24 # this defines commands that take "zero time" to execute
25 # (specifically, those which do not send commands from the issuing
26 # client to the server)
33 # Create the main session and start POE.
34 # All the empty anonymous subs are just to make POE:Session::ASSERT_STATES happy.
35 POE::Session->create(inline_states =>
37 # POE kernel interaction
38 _start => \&drv_start,
42 print "\nThat's all, folks!";
43 print "(exiting at line $heap->{lineno}: $heap->{line})"
47 _default => \&drv_default,
48 # generic utilities or miscellaneous functions
49 heartbeat => \&drv_heartbeat,
50 timeout_expect => \&drv_timeout_expect,
51 reconnect => \&drv_reconnect,
52 enable_client => sub { $_[ARG0]->{ready} = 1; },
53 disable_client => sub { $_[ARG0]->{ready} = 0; },
54 die => sub { $_[KERNEL]->signal($_[SESSION], 'TERM'); },
55 # client-based command issuers
56 cmd_expect => \&cmd_expect,
57 cmd_join => \&cmd_generic,
58 cmd_mode => \&cmd_generic,
59 cmd_nick => \&cmd_generic,
60 cmd_notice => \&cmd_message,
61 cmd_oper => \&cmd_generic,
62 cmd_part => \&cmd_generic,
63 cmd_privmsg => \&cmd_message,
64 cmd_quit => \&cmd_generic,
66 cmd_sleep => \&cmd_sleep,
67 cmd_wait => \&cmd_wait,
68 # handlers for messages from IRC
69 irc_001 => \&irc_connected, # Welcome to ...
70 irc_snotice => sub {}, # notice from a server (anonymous/our uplink)
71 irc_notice => \&irc_notice, # NOTICE to self or channel
72 irc_msg => \&irc_msg, # PRIVMSG to self
73 irc_public => \&irc_public, # PRIVMSG to channel
74 irc_connected => sub {},
75 irc_ctcp_action => sub {},
76 irc_ctcp_ping => sub {},
77 irc_ctcp_time => sub {},
78 irc_ctcpreply_ping => sub {},
79 irc_ctcpreply_time => sub {},
81 irc_isupport => sub {},
85 irc_mode => \&irc_mode, # MODE change on client or channel
90 irc_registered => sub {},
92 irc_plugin_add => sub {},
93 irc_error => \&irc_error,
94 irc_disconnected => \&irc_disconnected,
95 irc_socketerr => \&irc_socketerr,
103 # Core/bookkeeping test driver functions
106 my ($kernel, $session, $heap) = @_[KERNEL, SESSION, HEAP];
109 $heap->{clients} = {}; # session details, indexed by (short) session name
110 $heap->{sessions} = {}; # session details, indexed by session ref
111 $heap->{servers} = {}; # server addresses, indexed by short names
112 $heap->{macros} = {}; # macros
115 foreach my $arg (@_[ARG0..$#_]) {
116 if ($arg =~ /^-D$/) {
117 $heap->{irc_debug} = 1;
118 } elsif ($arg =~ /^-V$/) {
119 $heap->{verbose} = 1;
120 } elsif ($arg =~ /^-H(.+)$/) {
121 $heap->{local_address} = $1;
123 die "Extra command-line argument $arg\n" if $heap->{script};
124 $heap->{script} = new FileHandle($arg, 'r')
125 or die "Unable to open $arg for reading: $!\n";
128 die "No test name specified\n" unless $heap->{script};
131 $kernel->alias_set('control');
132 $kernel->yield('heartbeat');
136 my ($kernel, $sender, $heap) = @_[KERNEL, SENDER, HEAP];
137 my $script = $heap->{script};
144 $line = delete $heap->{line};
145 } elsif (defined($line = <$script>)) {
146 $heap->{lineno} = $.;
147 print "." unless $heap->{irc_debug};
149 # close all connections
150 foreach my $client (values %{$heap->{clients}}) {
151 $kernel->call($client->{irc}, 'quit', "I fell off the end of my script");
152 $client->{quitting} = 1;
154 # unalias the control session
155 $kernel->alias_remove('control');
156 # die in a few seconds
157 $kernel->delay_set('die', 5);
162 # ignore comments and blank lines
163 next if $line =~ /^\#/ or $line !~ /\S/;
165 # expand any macros in the line
166 $line =~ s/(?<=[^\\])%(\S+?)%/$heap->{macros}->{$1}
167 or die "Use of undefined macro $1 at $heap->{lineno}\n"/eg;
168 # remove any \-escapes
169 $line =~ s/\\(.)/$1/g;
170 # figure out the type of line
171 if ($line =~ /^define (\S+) (.+)$/i) {
173 $heap->{macros}->{$1} = $2;
174 } elsif ($line =~ /^undef (\S+)$/i) {
176 delete $heap->{macros}->{$1};
177 } elsif ($line =~ /^connect (\S+) (\S+) (\S+) (\S+) :(.+)$/i) {
178 # connect a new session (named $1) to server $4
179 my ($name, $nick, $ident, $server, $userinfo, $port) = ($1, $2, $3, $4, $5, 6667);
180 $server = $heap->{servers}->{$server} || $server;
181 if ($server =~ /(.+):(\d+)/) {
185 die "Client with nick $nick already exists (line $heap->{lineno})" if $heap->{clients}->{$nick};
186 my $alias = "client_$name";
193 params => { Nick => $nick,
197 Ircname => $userinfo,
198 Debug => $heap->{irc_debug},
201 $client->{params}->{LocalAddr} = $heap->{local_address}
202 if $heap->{local_address};
203 my $irc = POE::Component::IRC->spawn
207 ) or die "Unable to create new user $nick (line $heap->{lineno}): $!";
208 $client->{irc} = $irc->session_id();
209 $heap->{clients}->{$client->{name}} = $client;
210 $heap->{sessions}->{$irc} = $client;
211 $kernel->call($client->{irc}, 'register', 'all');
212 $kernel->call($client->{irc}, 'connect', $client->{params});
214 } elsif ($line =~ /^sync (.+)$/i) {
215 # do multi-way synchronization between every session named in $1
216 my @synced = split(/,|\s/, $1);
217 # first, check that they exist and are ready
218 foreach my $clnt (@synced) {
219 die "Unknown session name $clnt (line $heap->{lineno})" unless $heap->{clients}->{$clnt};
220 goto REDO unless $heap->{clients}->{$clnt}->{ready};
222 # next we actually send the synchronization signals
223 foreach my $clnt (@synced) {
224 my $client = $heap->{clients}->{$clnt};
225 $client->{sync_wait} = [map { $_ eq $clnt ? () : $heap->{clients}->{$_}->{nick} } @synced];
226 $kernel->call($client->{irc}, 'notice', $client->{sync_wait}, 'SYNC');
227 $kernel->call($sender, 'disable_client', $client);
229 } elsif ($line =~ /^:(\S+) (\S+)(.*)$/i) {
230 # generic command handler
231 my ($names, $cmd, $args) = ($1, lc($2), $3);
232 my (@avail, @unavail);
233 # figure out whether each listed client is available or not
234 foreach my $c (split ',', $names) {
235 my $client = $heap->{clients}->{$c};
237 print "ERROR: Unknown session name $c (line $heap->{lineno}; ignoring)\n";
238 } elsif (($used->{$c} and not $zero_time->{$cmd}) or not $client->{ready}) {
244 # redo command with unavailable clients
246 # This will break if the command can cause a redo for
247 # available clients.. this should be fixed sometime
248 $line = ':'.join(',', @unavail).' '.$cmd.$args;
251 # do command with available clients
253 # split up the argument part of the line
254 $args =~ /^((?:(?: [^:])|[^ ])+)?(?: :(.+))?$/;
255 $args = [($1 ? split(' ', $1) : ()), ($2 ? $2 : ())];
256 # find the client and figure out if we need to wait
257 foreach my $c (@avail) {
258 my $client = $heap->{clients}->{$c};
259 die "Client $c used twice as source (line $heap->{lineno})" if $used->{c} and not $zero_time->{$cmd};
260 $kernel->call($sender, 'cmd_' . $cmd, $client, $args);
261 $used->{$c} = 1 unless $zero_time->{$cmd};
265 die "Unrecognized input line $heap->{lineno}: $line";
269 delete $heap->{redo};
270 $heap->{line} = $line;
274 # issue new heartbeat with appropriate delay
275 $kernel->delay_set('heartbeat', $delay);
278 sub drv_timeout_expect {
279 my ($kernel, $session, $client) = @_[KERNEL, SESSION, ARG0];
280 print "ERROR: Dropping timed-out expectation by $client->{name}: ".join(',', @{$client->{expect}->[0]})."\n";
281 $client->{expect_alarms}->[0] = undef;
282 unexpect($kernel, $session, $client);
286 my ($kernel, $session, $client) = @_[KERNEL, SESSION, ARG0];
287 $kernel->call($client->{irc}, 'connect', $client->{params});
291 my ($kernel, $heap, $sender, $session, $state, $args) = @_[KERNEL, HEAP, SENDER, SESSION, ARG0, ARG1];
292 if ($state =~ /^irc_(\d\d\d)$/) {
293 my $client = $heap->{sessions}->{$sender->get_heap()};
294 if (@{$client->{expect}}
295 and $args->[0] eq $client->{expect}->[0]->[0]
296 and $client->{expect}->[0]->[1] eq "$1") {
297 my $expect = $client->{expect}->[0];
299 $args = $args->[2]; # ->[1] is the entire string, ->[2] is split
300 for (my $x=0; ($x+2<=$#$expect) and ($x<=$#$args) and not $mismatch; $x++) {
301 my $expectation = $expect->[$x+2];
302 if ($args->[$x] !~ /$expectation/i) {
304 print "Mismatch in arg $x: $args->[$x] !~ $expectation\n";
307 unexpect($kernel, $session, $client) unless $mismatch;
311 print "ERROR: Unexpected event $state to test driver (from ".$sender->ID.")\n";
315 # client-based command issuers
318 my ($kernel, $heap, $event, $client, $args) = @_[KERNEL, HEAP, STATE, ARG0, ARG1];
319 die "Missing arguments" unless $#$args >= 1;
320 # translate each target as appropriate (e.g. *sessionname)
321 my @targets = split(/,/, $args->[0]);
322 foreach my $target (@targets) {
323 if ($target =~ /^\*(.+)$/) {
324 my $other = $heap->{clients}->{$1} or die "Unknown session name $1 (line $heap->{lineno})\n";
325 $target = $other->{nick};
328 $kernel->call($client->{irc}, substr($event, 4), \@targets, $args->[1]);
332 my ($kernel, $event, $client, $args) = @_[KERNEL, STATE, ARG0, ARG1];
333 $kernel->call($client->{irc}, substr($event, 4), @$args);
337 my ($kernel, $heap, $client, $args) = @_[KERNEL, HEAP, ARG0, ARG1];
338 die "Missing argument" unless $#$args >= 0;
339 $kernel->call($client->{irc}, 'sl', $args->[0]);
343 my ($kernel, $session, $heap, $client, $args) = @_[KERNEL, SESSION, HEAP, ARG0, ARG1];
344 die "Missing argument" unless $#$args >= 0;
345 $kernel->call($session, 'disable_client', $client);
346 $kernel->delay_set('enable_client', $args->[0], $client);
350 my ($kernel, $session, $heap, $client, $args) = @_[KERNEL, SESSION, HEAP, ARG0, ARG1];
351 die "Missing argument" unless $#$args >= 0;
352 # if argument was comma-delimited, split it up (space-delimited is split by generic parser)
353 $args = [split(/,/, $args->[0])] if $args->[0] =~ /,/;
354 # make sure we only wait if all the other clients are ready
355 foreach my $other (@$args) {
356 if (not $heap->{clients}->{$other}->{ready}) {
361 # disable this client, make the others send SYNC to it
362 $kernel->call($session, 'disable_client', $client);
363 $client->{sync_wait} = [map { $heap->{clients}->{$_}->{nick} } @$args];
364 foreach my $other (@$args) {
365 die "Cannot wait on self" if $other eq $client->{name};
366 $kernel->call($heap->{clients}->{$other}->{irc}, 'notice', $client->{nick}, 'SYNC');
371 my ($kernel, $session, $heap, $client, $args) = @_[KERNEL, SESSION, HEAP, ARG0, ARG1];
372 die "Missing argument" unless $#$args >= 0;
373 push @{$client->{expect}}, $args;
374 push @{$client->{expect_alarms}}, $kernel->delay_set('timeout_expect', EXPECT_TIMEOUT, $client);
375 $kernel->call($session, 'disable_client', $client);
378 # handlers for messages from IRC
381 my ($kernel, $session, $client) = @_;
382 shift @{$client->{expect}};
383 my $alarm_id = shift @{$client->{expect_alarms}};
384 $kernel->alarm_remove($alarm_id) if $alarm_id;
385 $kernel->call($session, 'enable_client', $client) unless @{$client->{expect}};
389 my ($kernel, $session, $heap, $poe_sender, $sender, $text) = @_[KERNEL, SESSION, HEAP, SENDER, ARG0, ARG1];
390 my $client = $heap->{sessions}->{$poe_sender->get_heap()};
391 my $expected = $client->{expect}->[0];
394 return 0 unless $sender =~ /^:?\Q$expected->[0]\E/i;
397 return 0 unless $text =~ /$expected->[2]/i;
399 # drop expectation of event
400 unexpect($kernel, $session, $client);
404 my ($kernel, $session, $heap, $sender) = @_[KERNEL, SESSION, HEAP, SENDER];
405 my $client = $heap->{sessions}->{$sender->get_heap()};
406 print "Client $client->{name} connected to server $_[ARG0]\n"
408 $kernel->call($session, 'enable_client', $client);
411 sub handle_irc_disconnect ($$$$$) {
412 my ($kernel, $session, $heap, $sender, $client) = @_;
413 if ($client->{quitting}) {
414 $kernel->call($sender, 'unregister', 'all');
415 delete $heap->{sessions}->{$sender->get_heap()};
416 delete $heap->{clients}->{$client->{name}};
418 if ($client->{disconnect_expected}) {
419 delete $client->{disconnect_expected};
421 print "Got unexpected disconnect for $client->{name} (nick $client->{nick})\n";
423 $kernel->call($session, 'disable_client', $client);
424 $kernel->delay_set('reconnect', $client->{throttled} ? THROTTLED_TIMEOUT : RECONNECT_TIMEOUT, $client);
425 delete $client->{throttled};
429 sub irc_disconnected {
430 my ($kernel, $session, $heap, $sender, $server) = @_[KERNEL, SESSION, HEAP, SENDER, ARG0];
431 my $client = $heap->{sessions}->{$sender->get_heap()};
432 print "Client $client->{name} disconnected from server $_[ARG0]\n" if $heap->{verbose};
433 handle_irc_disconnect($kernel, $session, $heap, $sender, $client);
437 my ($kernel, $session, $heap, $sender, $msg) = @_[KERNEL, SESSION, HEAP, SENDER, ARG0];
438 my $client = $heap->{sessions}->{$sender->get_heap()};
439 print "Client $client->{name} (re-)connect error: $_[ARG0]\n";
440 handle_irc_disconnect($kernel, $session, $heap, $sender, $client);
444 my ($kernel, $session, $heap, $sender, $from, $to, $text) = @_[KERNEL, SESSION, HEAP, SENDER, ARG0, ARG1, ARG2];
445 my $client = $heap->{sessions}->{$sender->get_heap()};
446 if ($client->{sync_wait} and $text eq 'SYNC') {
450 for ($x=0; $x<=$#{$client->{sync_wait}}; $x++) {
451 last if $from eq $client->{sync_wait}->[$x];
453 # exit if we don't expect them
454 if ($x>$#{$client->{sync_wait}}) {
455 print "Got unexpected SYNC from $from to $client->{name} ($client->{nick})\n";
458 # remove from the list of people we're waiting for
459 splice @{$client->{sync_wait}}, $x, 1;
460 # re-enable client if we're done waiting
461 if ($#{$client->{sync_wait}} == -1) {
462 delete $client->{sync_wait};
463 $kernel->call($session, 'enable_client', $client);
465 } elsif (@{$client->{expect}}
466 and $client->{expect}->[0]->[1] =~ /notice/i) {
467 check_expect(@_[0..ARG0], $text);
472 my ($kernel, $session, $heap, $sender, $from, $to, $text) = @_[KERNEL, SESSION, HEAP, SENDER, ARG0, ARG1, ARG2];
473 my $client = $heap->{sessions}->{$sender->get_heap()};
474 if (@{$client->{expect}}
475 and $client->{expect}->[0]->[1] =~ /msg/i) {
476 check_expect(@_[0..ARG0], $text);
481 my ($kernel, $session, $heap, $sender, $from, $to, $text) = @_[KERNEL, SESSION, HEAP, SENDER, ARG0, ARG1, ARG2];
482 my $client = $heap->{sessions}->{$sender->get_heap()};
483 if (@{$client->{expect}}
484 and $client->{expect}->[0]->[1] =~ /public/i
485 and grep($client->{expect}->[0]->[2], @$to)) {
486 splice @{$client->{expect}->[0]}, 2, 1;
487 check_expect(@_[0..ARG0], $text);
492 my ($kernel, $session, $heap, $sender, $from, $to) = @_[KERNEL, SESSION, HEAP, SENDER, ARG0, ARG1];
493 my $client = $heap->{sessions}->{$sender->get_heap()};
494 if (@{$client->{expect}}
495 and $client->{expect}->[0]->[1] =~ /mode/i
496 and grep($client->{expect}->[0]->[2], $to)) {
497 splice @{$client->{expect}->[0]}, 2, 1;
504 my ($kernel, $session, $heap, $sender, $what) = @_[KERNEL, SESSION, HEAP, SENDER, ARG0];
505 my $client = $heap->{sessions}->{$sender->get_heap()};
506 if (@{$client->{expect}}
507 and $client->{expect}->[0]->[1] =~ /error/i) {
508 splice @{$client->{expect}->[0]}, 2, 1;
509 unexpect($kernel, $session, $client);
510 $client->{disconnect_expected} = 1;
512 print "ERROR: From server to $client->{name}: $what\n";
514 $client->{throttled} = 1 if $what =~ /throttled/i;