Skip Menu |

This queue is for tickets about the Net-Telnet CPAN distribution.


Subject: Connections occasionally time out and cause errors
Date: Sat, 19 Sep 2020 23:27:01 -0400
To: bug-Net-Telnet [...] rt.cpan.org
From: Asher Gordon <AsDaGo [...] posteo.net>
Hello, I found this bug in Net::Telnet 3.04, but the supplied patch is patched against 3.04_01 (although it also applies against 3.04). I used Net::Telnet to write a chat bot for the Free Internet Chess Server (freechess.org), called blikII. However, sometimes it seems to timeout, but Net::Telnet does not recognize it as a timeout, and thus does not flag it as such. I believe that the attached patch should fix the problem, but I haven't tested it much. Here is the output of 'perl -v':
This is perl 5, version 30, subversion 3 (v5.30.3) built for x86_64-linux-gnu-thread-multi (with 50 registered patches, see perl -V for more detail) Copyright 1987-2020, Larry Wall Perl may be copied only under the terms of either the Artistic License or the GNU General Public License, which may be found in the Perl 5 source kit. Complete documentation for Perl, including FAQ lists, should be found on this system using "man perl" or "perldoc perl". If you have access to the Internet, point your browser at http://www.perl.org/, the Perl Home Page.
And my operating system version is Debian GNU/Linux 11 "Bullseye". The output of 'uname -a' is as follows: Linux Xanthus 5.8.0-1-amd64 #1 SMP Debian 5.8.7-1 (2020-09-05) x86_64 GNU/Linux Also, I believe the error message was "read error: Connection timed out at <...>" (since errno ($!) was ETIMEDOUT). Here is the patch which (I believe) fixes the problem:
diff -pur Net-Telnet-3.04_01.orig/lib/Net/Telnet.pm Net-Telnet-3.04_01/lib/Net/Telnet.pm --- Net-Telnet-3.04_01.orig/lib/Net/Telnet.pm 2020-02-17 08:12:07.000000000 -0500 +++ Net-Telnet-3.04_01/lib/Net/Telnet.pm 2020-09-19 23:02:58.031015494 -0400 @@ -43,6 +43,7 @@ my $AF_UNSPEC = &_import_af_unspec() || my $AI_ADDRCONFIG = &_import_ai_addrconfig() || 0; my $EAI_BADFLAGS = &_import_eai_badflags() || -1; my $EINTR = &_import_eintr(); +my $ETIMEDOUT = &_import_etimedout(); ## Global variables. use vars qw($VERSION @Telopts); @@ -2602,6 +2603,12 @@ sub _fillbuf { else { next if $! =~ /^interrupted/i; # restart select() } + if (defined $ETIMEDOUT) { + next if $! == $ETIMEDOUT; # restart select() + } + else { + next if $! =~ /timed out/i; # restart select() + } $s->{opened} = ''; return $self->error("read error: $!"); @@ -2628,6 +2635,12 @@ sub _fillbuf { else { next if $! =~ /^interrupted/i; # restart sysread() } + if (defined $ETIMEDOUT) { + next if $! == $ETIMEDOUT; # restart sysread() + } + else { + next if $! =~ /timed out/i; # restart sysread() + } $s->{opened} = ''; return $self->error("read error: $!"); @@ -2778,6 +2791,14 @@ sub _import_eintr { } # end sub _import_eintr +sub _import_etimedout { + local $@; + local $SIG{"__DIE__"} = "DEFAULT"; + + eval "require Errno; Errno::ETIMEDOUT();"; +} # end sub _import_etimedout + + sub _interpret_cr { my ($s, $pos) = @_; my ( @@ -3645,6 +3666,12 @@ sub _put { else { next if $! =~ /^interrupted/i; # restart select() } + if (defined $ETIMEDOUT) { + next if $! == $ETIMEDOUT; # restart select() + } + else { + next if $! =~ /timed out/i; # restart select() + } $s->{opened} = ''; return $self->error("write error: $!"); @@ -3662,6 +3689,12 @@ sub _put { else { next if $! =~ /^interrupted/i; # restart syswrite() } + if (defined $ETIMEDOUT) { + next if $! == $ETIMEDOUT; # restart syswrite() + } + else { + next if $! =~ /timed out/i; # restart syswrite() + } $s->{opened} = ''; return $self->error("write error: $!");
Thanks, Asher -- Hi! I'm a .signature virus! Copy me into your ~/.signature to help me sprea= d! -------- I prefer to send and receive mail encrypted. Please send me your public key, and if you do not have my public key, please let me know. Thanks. GPG fingerprint: 38F3 975C D173 4037 B397 8095 D4C9 C4FC 5460 8E68
Download signature.asc
application/pgp-signature 227b

Message body not shown because it is not plain text.

Asher, thanks for reaching out to me. I understand you are sometimes getting the error message "Connection timed out" indicating the errno is ETIMEDOUT. I do not agree this is a bug. The errno ETIMEDOUT indicates the remote side did not respond to a keepalive. Under this condition the correct action is to assume the connection is lost. I cannot explain why it appears you are getting ETIMEOUT and you are able to continue reading. If the server is on the Internet and you want to send me your code with the problem, I can try to debug the problem.
Subject: Re: [rt.cpan.org #133371] Connections occasionally time out and cause errors
Date: Tue, 22 Sep 2020 01:05:40 -0400
To: bug-Net-Telnet [...] rt.cpan.org
From: Asher Gordon <AsDaGo [...] posteo.net>
Hi Jay, "Jay Rogers via RT" <bug-Net-Telnet@rt.cpan.org> writes: Show quoted text
> I understand you are sometimes getting the error message "Connection > timed out" indicating the errno is ETIMEDOUT. I do not agree this is > a bug. The errno ETIMEDOUT indicates the remote side did not respond > to a keepalive. Under this condition the correct action is to assume > the connection is lost.
Well I don't know a lot about networking, but after applying the patch I attached in my previous message, it seemed to stay connected. But it's possible that happened just by chance too. Show quoted text
> I cannot explain why it appears you are getting ETIMEOUT and you are > able to continue reading. If the server is on the Internet and you > want to send me your code with the problem, I can try to debug the > problem.
I submitted my code to the Savannah software forge (sv.nongnu.org), but it hasn't been accepted yet, so I've attached the code below. It will require some modifications in order to be able to run as an unregistered user on FICS, so it may not be easy for you to test it. I will try running the code without my patch and see if the problem occurs again. I will also try to write a more minimal script that shows the problem. It's also possible that it could be a bug in FICS. Thanks a lot for your help. Here is the code:

Message body is not shown because sender requested not to inline it.

Thanks, Asher -- I hate quotations. -- Ralph Waldo Emerson -------- I prefer to send and receive mail encrypted. Please send me your public key, and if you do not have my public key, please let me know. Thanks. GPG fingerprint: 38F3 975C D173 4037 B397 8095 D4C9 C4FC 5460 8E68
Download signature.asc
application/pgp-signature 227b

Message body not shown because it is not plain text.

Subject: Re: [rt.cpan.org #133371] Connections occasionally time out and cause errors
Date: Tue, 22 Sep 2020 11:18:18 -0400
To: bug-Net-Telnet [...] rt.cpan.org
From: Asher Gordon <AsDaGo [...] posteo.net>
Asher Gordon <AsDaGo@posteo.net> writes: Show quoted text
> I will try running the code without my patch and see if the problem > occurs again.
I tried running the code without my patch, and it failed with an ETIMEDOUT error after about nine hours. So I think I can definitely say that my patch fixed the problem. I'll now try to write a more minimal example and see if I can reproduce the bug with that. Thanks, Asher -- I hate quotations. -- Ralph Waldo Emerson -------- I prefer to send and receive mail encrypted. Please send me your public key, and if you do not have my public key, please let me know. Thanks. GPG fingerprint: 38F3 975C D173 4037 B397 8095 D4C9 C4FC 5460 8E68
Download signature.asc
application/pgp-signature 227b

Message body not shown because it is not plain text.

Subject: Re: [rt.cpan.org #133371] Connections occasionally time out and cause errors
Date: Tue, 22 Sep 2020 12:23:25 -0400
To: bug-Net-Telnet [...] rt.cpan.org
From: Asher Gordon <AsDaGo [...] posteo.net>
Asher Gordon <AsDaGo@posteo.net> writes: Show quoted text
> I'll now try to write a more minimal example and see if I can > reproduce the bug with that.
I now wrote a minimal example. I'm running two instances of it now on FICS. One is named TimeoutBotNoPatch and the other is named TimeoutBotPatched. One is running with my patch and the other is not (can you guess which is which?). I'm going to keep them running for a day or two and see if my patch works on these bots. You can run the code as a guest on FICS, so it should be easy for you to test it if you want. If you want to change the handle that the bot uses, just use the -n option or change the $name variable. Also, I haven't tested this on any other servers besides FICS, so it's possible that the problem only occurs on FICS. It does seem strange that that would be the case, though... Here is the code:
#!/usr/bin/env perl ###################################################################### # TimeoutBot is a bot to test a timeout error # # Copyright (C) 2020 Asher Gordon <AsDaGo@posteo.net> # # # # Copying and distribution of this file, with or without # # modification, are permitted in any medium without royalty provided # # the copyright notice and this notice are preserved. This file is # # offered as-is, without any warranty. # ###################################################################### use strict; use warnings; use Net::Telnet; use Getopt::Long; use subs 'tell'; # Override CORE::tell with Main::tell functions my $name = 'TimeoutBot'; my $host = 'freechess.org'; my $port = 5000; # The time (in seconds) that we need to do something in order to # prevent the server from kicking us off. my $ping_time = 3600; # The time of the last tell. my $last_tell_time; my $usage = <<EOF; Usage: $0 [-n NAME] [-l FILE] [HOST [PORT]] Connect to the chess server at HOST (default $host) through port PORT (default $port) and start running. -n, --name=NAME use NAME to log in rather than $name -l, --log-file=FILE log all I/O to FILE -h, --help print this help and exit EOF GetOptions 'n|name=s' => \$name, 'l|log-file=s' => \my $log_filename, 'h|help' => sub { print $usage; exit } or die $usage; $host = shift if @ARGV; $port = shift if @ARGV; die if @ARGV; die "Invalid handle: $name" unless $name =~ /^[[:alpha:]]+$/; die "Handle too long (must be at most 17 characters): $name" if (length $name) > 17; my $log_file; if (defined $log_filename && ! open $log_file, '>', $log_filename) { warn "Cannot open $log_filename: $!"; undef $log_filename; undef $log_file; } my $telnet; # Die unless the error was a timeout. sub die_unless_timeout { die $_[0] unless $telnet->timed_out; } sub tell { my $user = shift; my $msg = @_ ? "@_" : $_; my $ret = $telnet->cmd("tell $user $msg"); $last_tell_time = time; return $ret; } # Connect to the server. print "Opening connection to $host on port $port\n"; $telnet = new Net::Telnet Host => $host, Port => $port; $telnet->input_log($log_file) if defined $log_file; $telnet->open; # Log in. print "Waiting for login prompt\n"; $telnet->waitfor('/\rlogin: $/'); print "Logging in\n"; $telnet->print($name); $telnet->print(); # Initialize the last tell time. $last_tell_time = time; # Wait for the prompt. $telnet->waitfor('/\rfics% /'); # Wait a bit for all the initialization text to finish coming. sleep 1; # Run the initialization commands. $telnet->cmd('set open 0'); $telnet->cmd('set seek 0'); $telnet->cmd('set width 240'); $telnet->cmd("set interface $name"); $telnet->cmd('set 1 I am a bot to test a timeout error that occurs ' . 'with blikII.'); my $timeout = 10; # Start the main loop. while (1) { my $period = time - $last_tell_time; # Check if we should do something so that the server doesn't kick # us off. Allow for an extra grace period of $timeout. tell $name, 'ping' if defined $ping_time && $period > $ping_time - $timeout; $_ = $telnet->getline(Timeout => $timeout, Errmode => \&die_unless_timeout); next unless defined; chomp; s/^\r//; if (/^([[:alpha:]]+).*? tells you:\s+(.*)$/) { tell $1, qq/Hi $1. You told me "$2"./; } }
-- <cas> well there ya go. say something stupid in irc and have it immortalised forever in someone's .sig file -------- I prefer to send and receive mail encrypted. Please send me your public key, and if you do not have my public key, please let me know. Thanks. GPG fingerprint: 38F3 975C D173 4037 B397 8095 D4C9 C4FC 5460 8E68
Download signature.asc
application/pgp-signature 227b

Message body not shown because it is not plain text.

Subject: Re: [rt.cpan.org #133371] Connections occasionally time out and cause errors
Date: Tue, 22 Sep 2020 13:27:06 -0400
To: bug-Net-Telnet [...] rt.cpan.org
From: Asher Gordon <AsDaGo [...] posteo.net>
Asher Gordon <AsDaGo@posteo.net> writes: Show quoted text
> I now wrote a minimal example.
In the example, I forgot to close the telnet handle, which causes the bot to remain logged in for an hour if it is killed. Here is a patch to fix that:
--- timeout.pl~ 2020-09-22 12:13:19.131749592 -0400 +++ timeout.pl 2020-09-22 13:23:03.308904610 -0400 @@ -81,6 +81,10 @@ $telnet = new Net::Telnet $telnet->input_log($log_file) if defined $log_file; $telnet->open; +$_ = sub { $telnet->close; exit } + foreach @SIG{qw(INT PWR QUIT TERM)}; +$SIG{__DIE__} = sub { $telnet->close; die @_ }; + # Log in. print "Waiting for login prompt\n"; $telnet->waitfor('/\rlogin: $/');
-- A witty saying proves nothing, but saying something pointless gets people's attention. -------- I prefer to send and receive mail encrypted. Please send me your public key, and if you do not have my public key, please let me know. Thanks. GPG fingerprint: 38F3 975C D173 4037 B397 8095 D4C9 C4FC 5460 8E68 ************************************************************** BONUS!!! BONUS!!! BONUS!!! BONUS!!! BONUS!!! BONUS!!! BONUS!!! ************************************************************** You are the lucky recipient of one of my Flame Signatures(TM)! Only one out of every 100 signatures is a Flame Signature(TM)! A Flame Signature(TM) is a signature in which an auto-generated flame (generated by flame.el) is included. Here is the auto-generated flame for your reading pleasure. WARNING: may be offensive (that's kind of the point of flames). V pbhyqa'g pner yrff nobhg lbhe chax unvephg. Vtabenzhf! Ubj pna lbh or fb yhqvpebhf? Vg gnxrf n juvgr pbjneq yvxr lbh gb fnl gung fhogyrgl vf cerggl sha. V'ir arire urneq nalguvat nf evqvphybhf nf gur vqrn gung nyy penml qhqrf fubhyq trg fubccvat znyyf.
Download signature.asc
application/pgp-signature 227b

Message body not shown because it is not plain text.

Subject: Re: [rt.cpan.org #133371] Connections occasionally time out and cause errors
Date: Tue, 22 Sep 2020 14:06:18 -0400
To: bug-Net-Telnet [...] rt.cpan.org
From: Asher Gordon <AsDaGo [...] posteo.net>
Asher Gordon <AsDaGo@posteo.net> writes: Show quoted text
> In the example, I forgot to close the telnet handle, which causes the > bot to remain logged in for an hour if it is killed. Here is a patch > to fix that:
Ugh, I forgot a couple more important things. I need to make the bot ignore tells from itself and I forgot to update $last_tell_time. Here is a patch to fix those:
--- timeout.pl~ 2020-09-22 13:23:03.308904610 -0400 +++ timeout.pl 2020-09-22 14:01:20.685688625 -0400 @@ -14,7 +14,8 @@ use strict; use warnings; use Net::Telnet; use Getopt::Long; -use subs 'tell'; # Override CORE::tell with Main::tell functions +use v5.16; # For fc +use subs 'tell'; # Override CORE::tell with Main::tell functions my $name = 'TimeoutBot'; my $host = 'freechess.org'; @@ -126,7 +127,9 @@ while (1) { chomp; s/^\r//; - if (/^([[:alpha:]]+).*? tells you:\s+(.*)$/) { + if (/^([[:alpha:]]+).*? tells you:\s+(.*)$/ && + (fc $1) ne (fc $name)) { tell $1, qq/Hi $1. You told me "$2"./; + $last_tell_time = time; } }
If you're too lazy to apply both of my patches, here is the full code again:

Message body is not shown because sender requested not to inline it.

Thanks, Asher -- Yesterday upon the stair I met a man who wasn't there. He wasn't there again today -- I think he's from the CIA. -------- I prefer to send and receive mail encrypted. Please send me your public key, and if you do not have my public key, please let me know. Thanks. GPG fingerprint: 38F3 975C D173 4037 B397 8095 D4C9 C4FC 5460 8E68
Download signature.asc
application/pgp-signature 227b

Message body not shown because it is not plain text.

Subject: Re: [rt.cpan.org #133371] Connections occasionally time out and cause errors
Date: Tue, 22 Sep 2020 20:06:10 -0400
To: bug-Net-Telnet [...] rt.cpan.org
From: Asher Gordon <AsDaGo [...] posteo.net>
So far, the test without my patch did time out, and at about the same time the other test (with my patch) started using up 100% CPU, as if it had gotten stuck in an infinite loop. I actually did notice this with blikII once or twice. Strangely, however, this infinite loop seems to occur much less often (in blikII, at least) than timing out without the patch. I'm not sure, though... What I'm going to do now is modify Net::Telnet to print a message whenever ETIMEDOUT is caught. Then, I can see for sure whether it really is possible to continue using the Net::Telnet handle even after ETIMEDOUT. Thanks, Asher -- On two occasions I have been asked [by members of Parliament!], "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question. -- Charles Babbage -------- I prefer to send and receive mail encrypted. Please send me your public key, and if you do not have my public key, please let me know. Thanks. GPG fingerprint: 38F3 975C D173 4037 B397 8095 D4C9 C4FC 5460 8E68
Download signature.asc
application/pgp-signature 227b

Message body not shown because it is not plain text.

Subject: Re: [rt.cpan.org #133371] Connections occasionally time out and cause errors
Date: Tue, 22 Sep 2020 21:23:04 -0400
To: bug-Net-Telnet [...] rt.cpan.org
From: Jay Rogers <jay [...] rgrs.com>
If you modify Net::Telnet to print a message whenever ETIMEDOUT is caught, also have it indicate whether it is the select() or sysread() that is returning with an error.
CC: gregoa [...] cpan.org
Subject: Re: [rt.cpan.org #133371] Connections occasionally time out and cause errors
Date: Tue, 22 Sep 2020 23:52:07 -0400
To: "Jay Rogers via RT" <bug-Net-Telnet [...] rt.cpan.org>
From: Asher Gordon <AsDaGo [...] posteo.net>
"Jay Rogers via RT" <bug-Net-Telnet@rt.cpan.org> writes: Show quoted text
> If you modify Net::Telnet to print a message whenever ETIMEDOUT is > caught, also have it indicate whether it is the select() or sysread() > that is returning with an error.
Well I did modify it to print a message, although I didn't think to indicate whether it was select() or sysread() that returned the error. It seemed to go into the infinite loop right after it got ETIMEDOUT, but it's a bit hard to tell. Again, both tests (with and without the patch) seemed to receive ETIMEDOUT and around the same time. I'll now modify it to print whether it was select() or sysread() (and I'll use warn() to print the source line as well). I'll also try sending something to the server when I get an ETIMEDOUT error, so it's easier to tell if that's actually possible after ETIMEDOUT. So I think that you're probably right, that it's not a bug. Or that it's a bug somewhere else. But I'm going to run some more tests and see what they indicate. Thanks, Asher -- Hi! I'm a .signature virus! Copy me into your ~/.signature to help me sprea= d! -------- I prefer to send and receive mail encrypted. Please send me your public key, and if you do not have my public key, please let me know. Thanks. GPG fingerprint: 38F3 975C D173 4037 B397 8095 D4C9 C4FC 5460 8E68
Download signature.asc
application/pgp-signature 227b

Message body not shown because it is not plain text.

CC: gregoa [...] cpan.org
Subject: Re: [rt.cpan.org #133371] Connections occasionally time out and cause errors
Date: Wed, 23 Sep 2020 14:47:02 -0400
To: "Jay Rogers via RT" <bug-Net-Telnet [...] rt.cpan.org>
From: Asher Gordon <AsDaGo [...] posteo.net>
Asher Gordon <AsDaGo@posteo.net> writes: Show quoted text
> So I think that you're probably right, that it's not a bug. Or that > it's a bug somewhere else. But I'm going to run some more tests and > see what they indicate.
Well after running the tests overnight, neither of them timed out. I think this could be because I had the test program send something to the server every 10 seconds. So I tried running them without sending every 10 seconds, and it's been a little over three hours and neither of them have timed out yet. So I think it was probably something with my WiFi or the server or something. Not a bug in the library. I think I was just confused because of the similarity of the error messages ("FOO timed-out" vs. "read error: Connection timed out"). Like I said, I don't have much experience with network programming. So I think you can close this bug now. Sorry for leading you on a wild goose chase. :-) Thanks, Asher -- On two occasions I have been asked [by members of Parliament!], "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question. -- Charles Babbage -------- I prefer to send and receive mail encrypted. Please send me your public key, and if you do not have my public key, please let me know. Thanks. GPG fingerprint: 38F3 975C D173 4037 B397 8095 D4C9 C4FC 5460 8E68
Download signature.asc
application/pgp-signature 227b

Message body not shown because it is not plain text.

CC: gregoa [...] cpan.org
Subject: Re: [rt.cpan.org #133371] Connections occasionally time out and cause errors
Date: Wed, 23 Sep 2020 20:16:35 -0400
To: "Jay Rogers via RT" <bug-Net-Telnet [...] rt.cpan.org>
From: Asher Gordon <AsDaGo [...] posteo.net>
The test with my patch timed out a while ago. Then, it said the filehandle was closed when it tried to send something to the server. So again, I'm pretty sure it's either a problem on my end, or on the server's end, not in the middle. I'm not sure how to close this issue, so you'll have to do that. Thanks, Asher -- I hate quotations. -- Ralph Waldo Emerson -------- I prefer to send and receive mail encrypted. Please send me your public key, and if you do not have my public key, please let me know. Thanks. GPG fingerprint: 38F3 975C D173 4037 B397 8095 D4C9 C4FC 5460 8E68
Download signature.asc
application/pgp-signature 227b

Message body not shown because it is not plain text.

Subject: Re: [rt.cpan.org #133371] Connections occasionally time out and cause errors
Date: Wed, 23 Sep 2020 20:28:37 -0400
To: bug-Net-Telnet [...] rt.cpan.org
From: Jay Rogers <jay [...] rgrs.com>
Asher, I would still like to try to debug this. I would like to understand why either select() or sysread() is failing with ETIMEDOUT. That should only happen if SO_KEEPALIVE is enabled on the socket which by default it isn't. I am wondering if something changed in perl 5.30 or Debian 11. If you can get me access to your test code, I can try to figure it out.
CC: gregoa [...] cpan.org
Subject: Re: [rt.cpan.org #133371] Connections occasionally time out and cause errors
Date: Thu, 24 Sep 2020 00:55:37 -0400
To: "Jay Rogers via RT" <bug-Net-Telnet [...] rt.cpan.org>
From: Asher Gordon <AsDaGo [...] posteo.net>
Hi Jay, "Jay Rogers via RT" <bug-Net-Telnet@rt.cpan.org> writes: Show quoted text
> I would still like to try to debug this. I would like to understand > why either select() or sysread() is failing with ETIMEDOUT. That > should only happen if SO_KEEPALIVE is enabled on the socket which by > default it isn't. I am wondering if something changed in perl 5.30 or > Debian 11.
Hmm, that's strange. Although I did modify Net::Telnet to say what function caused the ETIMEDOUT error, I forgot which function it actually was. So I'm running it again now (this time I have it die when it sees ETIMEDOUT). Show quoted text
> If you can get me access to your test code, I can try to figure it > out.
I think I already gave it to you, but here it is again:
#!/usr/bin/env perl ###################################################################### # TimeoutBot is a bot to test a timeout error # # Copyright (C) 2020 Asher Gordon <AsDaGo@posteo.net> # # # # Copying and distribution of this file, with or without # # modification, are permitted in any medium without royalty provided # # the copyright notice and this notice are preserved. This file is # # offered as-is, without any warranty. # ###################################################################### use strict; use warnings; use Net::Telnet; use Getopt::Long; use POSIX 'strftime'; use v5.16; # For fc use subs 'tell'; # Override CORE::tell with Main::tell functions my $name = 'TimeoutBot'; my $host = 'freechess.org'; my $port = 5000; # The time (in seconds) that we need to do something in order to # prevent the server from kicking us off. my $ping_time = 3600; # The time of the last tell. my $last_tell_time; my $usage = <<EOF; Usage: $0 [-n NAME] [-l FILE] [HOST [PORT]] Connect to the chess server at HOST (default $host) through port PORT (default $port) and start running. -n, --name=NAME use NAME to log in rather than $name -l, --log-file=FILE log all I/O to FILE -h, --help print this help and exit EOF GetOptions 'n|name=s' => \$name, 'l|log-file=s' => \my $log_filename, 'h|help' => sub { print $usage; exit } or die $usage; $host = shift if @ARGV; $port = shift if @ARGV; die if @ARGV; die "Invalid handle: $name" unless $name =~ /^[[:alpha:]]+$/; die "Handle too long (must be at most 17 characters): $name" if (length $name) > 17; my $log_file; if (defined $log_filename && ! open $log_file, '>', $log_filename) { warn "Cannot open $log_filename: $!"; undef $log_filename; undef $log_file; } my $telnet; # Die unless the error was a timeout. sub die_unless_timeout { die $_[0] unless $telnet->timed_out; } sub tell { my $user = shift; my $msg = @_ ? "@_" : $_; my $ret = $telnet->cmd("tell $user $msg"); $last_tell_time = time; return $ret; } # Return the time in a human-readable format. sub get_time { strftime '%Y-%m-%d %H:%M:%S', localtime; } # Connect to the server. print "Opening connection to $host on port $port\n"; $telnet = new Net::Telnet Host => $host, Port => $port; $telnet->input_log($log_file) if defined $log_file; $telnet->open; $_ = sub { $telnet->close; exit } foreach @SIG{qw(INT PWR QUIT TERM)}; $SIG{__DIE__} = sub { $telnet->close; die $_[0] }; $SIG{__WARN__} = sub { warn get_time . ": $_[0]" }; # Log in. print "Waiting for login prompt\n"; $telnet->waitfor('/\rlogin: $/'); print "Logging in\n"; $telnet->print($name); $telnet->print(); # Initialize the last tell time. $last_tell_time = time; # Wait for the prompt. $telnet->waitfor('/\rfics% /'); # Wait a bit for all the initialization text to finish coming. sleep 1; # Run the initialization commands. $telnet->cmd('set open 0'); $telnet->cmd('set seek 0'); $telnet->cmd('set width 240'); $telnet->cmd("set interface $name"); $telnet->cmd('set 1 I am a bot to test a timeout error that occurs ' . 'with blikII.'); my $timeout = 10; # Start the main loop. while (1) { my $period = time - $last_tell_time; # Check if we should do something so that the server doesn't kick # us off. Allow for an extra grace period of $timeout. tell $name, 'ping' if defined $ping_time && $period > $ping_time - $timeout; $_ = $telnet->getline(Timeout => $timeout, Errmode => \&die_unless_timeout); next unless defined; chomp; s/^\r//; if (/^([[:alpha:]]+).*? tells you:\s+(.*)$/ && (fc $1) ne (fc $name)) { tell $1, qq/Hi $1. You told me "$2"./; $last_tell_time = time; } }
Let me know if you figure it out. Thanks, Asher P.S. Sorry about the long signature. I usually only add fairly short ones to my collection, but this one was too good to waste (at least I think so). ;-) -- A Plan for the Improvement of English Spelling attributed to Mark Twain For example, in Year 1 that useless letter "c" would be dropped to be replased either by "k" or "s", and likewise "x" would no longer be part of the alphabet. The only kase in which "c" would be retained would be the "ch" formation, which will be dealt with later. Year 2 might reform "w" spelling, so that "which" and "one" would take the same konsonant, wile Year 3 might well abolish "y" replasing it with "i" and Iear 4 might fiks the "g/j" anomali wonse and for all. Jenerally, then, the improvement would kontinue iear bai iear with Iear 5 doing awai with useless double konsonants, and Iears 6-12 or so modifaiing vowlz and the rimeining voist and unvoist konsonants. Bai Iear 15 or sou, it wud fainali bi posibl tu meik ius ov thi ridandant letez "c", "y" and "x" -- bai now jast a memori in the maindz ov ould doderez -- tu riplais "ch", "sh", and "th" rispektivli. Fainali, xen, aafte sam 20 iers ov orxogrefkl riform, wi wud hev a lojikl, kohirnt speling in ius xrewawt xe Ingliy-spiking werld. -------- I prefer to send and receive mail encrypted. Please send me your public key, and if you do not have my public key, please let me know. Thanks. GPG fingerprint: 38F3 975C D173 4037 B397 8095 D4C9 C4FC 5460 8E68
Download signature.asc
application/pgp-signature 227b

Message body not shown because it is not plain text.