Add HTTPMsg.pm an async HTTP agent
[spider.git] / cmd / show / contest.pl
index 33b98903b32c49e9f7f2a9eab1f9323aa9971bb8..94d8402ad9b83249fbdc9f31652b4db2a74c71f8 100644 (file)
@@ -7,80 +7,59 @@
 #
 #
 
-my ($self, $line) = @_;
+sub handle
+{
+       my ($self, $line) = @_;
 
-#return (1, "usage: sh/contest [<month>] [<year>], e g sh/contest sep 2012") unless $line;
+       return (1, $self->msg('e24')) unless $Internet::allow;
 
-my @out;
+       my @out;
 
-my $mon;;
+       #$DB::single = 1;
 
-#$DB::single = 1;
 
+       # trying to make the syntax abit more user friendly...
+       # and yes, I have been here and it *is* all my fault (dirk)
+       $line = lc $line;
+       my ($m,$y);
+       ($y) = $line =~ /(\d+)/;
+       ($m) = $line =~ /([a-z]{3})/;
 
-# trying to make the syntax abit more user friendly...
-# and yes, I have been here and it *is* all my fault (dirk)
-$line = lc $line;
-my ($m,$y);
-($y) = $line =~ /(\d+)/;
-($m) = $line =~ /([a-z]{3})/;
-
-unless ($y) {
-       ($y) = (gmtime)[5];
-       $y += 1900;
-}
-unless ($m) {
-       ($m) = (gmtime)[4];
-       $m = lc $DXUtil::month[$m];
-}
-$y += 2000 if $y <= 50;
-$y += 1900 if $y > 50 && $y <= 99;
-$m = substr $m, 0, 3 if length $m > 3;
-$m = 'oct' if $m eq 'okt';
-$m = 'may' if $m eq 'mai' || $m eq 'maj';
-$mon = "$y$m";
-
-dbg("sh/contest: month=$mon") if isdbg('contest');
-
-my $filename = "c" . $mon . ".txt";
-my $host = $Internet::contest_host || 'www.sk3bg.se';
-my $port = 80;
+       unless ($y) {
+               ($y) = (gmtime)[5];
+               $y += 1900;
+       }
+       unless ($m) {
+               ($m) = (gmtime)[4];
+               $m = lc $DXUtil::month[$m];
+       }
+       $y += 2000 if $y <= 50;
+       $y += 1900 if $y > 50 && $y <= 99;
+       $m = substr $m, 0, 3 if length $m > 3;
+       $m = 'oct' if $m eq 'okt';
+       $m = 'may' if $m eq 'mai' || $m eq 'maj';
+       my $mon = "$y$m";
 
-dbg("sh/contest: host=$host:$port") if isdbg('contest');
+       dbg("sh/contest: month=$mon") if isdbg('contest');
 
-my $url = $Internet::contest_url || "/contest/text";
-$url .= "/$filename";
+       my $filename = "c" . $mon . ".txt";
+       my $host = $Internet::contest_host || 'www.sk3bg.se';
+       my $port = 80;
 
-dbg("sh/contest: url=$url") if isdbg("contest");
+       dbg("sh/contest: host=$host:$port") if isdbg('contest');
 
-my $t = new Net::Telnet (Telnetmode => 0);
-eval { $t->open(Host => $host, Port => $port, Timeout => 15); };
+       my $url = $Internet::contest_url || "/contest/text";
+       $url .= "/$filename";
 
-if (!$t || $@) {
-    push @out, $self->msg('e18','sk3bg.se');
-} else {
-    my $s = "GET $url HTTP/1.0";
-       dbg("sh/contest: get='$s'") if isdbg('contest');
-       
-    $t->print($s);
-       $t->print("Host: $host\n");
-       $t->print("\n\n");
+       dbg("sh/contest: url=$url") if isdbg("contest");
 
-    my $notfound = $t->getline(Timeout => 10);
-    if (!$notfound || $notfound =~ /404 Object Not Found/) {
-           push @out, "there is no contest info for $mon at $host/$url";
-               return (1, @out);
-       else {
-           push @out, $notfound;
+       my $r = HTTPMsg->get($self->call, $host, $port, $url);
+       if ($r) {
+               push @out, $self->msg('m21', "show/contest");
+       }
+       else {
+               push @out, $self->msg('e18','sk3bg.se');
        }
-    while (!$t->eof) {
-               eval { push @out, $t->getline(Timeout => 10); };
-               if ($@) {
-                       push @out, $self->msg('e18', 'sk3bg.se');
-                       last;
-               }
-    }
-}
-$t->close;
 
-return (1, @out);
+       return (1, @out);
+}