08f555ee9d8022b060c092fb8760dc6829229150
[spider.git] / cmd / show / contest.pl
1 # contest.pl - contest calendar from www.sk3bg.se/contest
2 # used with 1 argument: sh/contest <yearandmonth>
3 # e g sh/contest 2002sep
4 # Tommy Johansson (SM3OSM) 2002-07-23
5 # New version using Net::Telnet  2003-03-09
6 #
7 #
8 #
9
10 my ($self, $line) = @_;
11
12 #return (1, "usage: sh/contest  <year_and_month>, e g sh/contest 2002sep ") unless $line;
13
14 my @out;
15
16 my $mon;;
17
18 # trying to make the syntax abit more user friendly...
19 # and yes, I have been here and it *is* all my fault (dirk)
20 $line = lc $line;
21 my ($m,$y) = $line =~ /^([a-z]{3})\w*\s*(\d+)/;
22 ($y,$m) = $line =~ /^(\d+)\s*([a-z]{3})/ unless $y && $m;
23 unless ($y && $m) {
24         ($m,$y) = (gmtime)[4,5];
25         $m = qw(jan feb mar apr may jun jul aug sep oct nov dec)[$m];
26         $y += 1900;
27 }
28 $y += 2000 if $y <= 50;
29 $y += 1900 if $y > 50 && $y <= 99;
30 $m = 'oct' if $m eq 'okt';
31 $m = 'may' if $m eq 'mai' || $m eq 'maj';
32 $mon = "$y$m";
33
34 dbg($mon) if isdbg('contest');
35
36 my $filename = "c" . $mon . ".txt";
37 my $host = $Internet::contest_host || 'www.sk3bg.se';
38 my $port = 80;
39 my $url = $Internet::contest_url || "http://www.sk3bg.se/contest/text";
40 $url .= "/$filename";
41
42 my $t = new Net::Telnet (Telnetmode => 0);
43 eval {
44     $t->open(Host => $host, Port => $port, Timeout => 15);
45     };
46
47 if (!$t || $@) {
48     push @out, $self->msg('e18','sk3bg.se');
49 } else {
50     my $s = "GET $url";
51     $t->print($s);
52     my $notfound = $t->getline(Timeout => 10);
53     if ($notfound =~ /404 Object Not Found/) {
54             return (1, "there is no contest info for $mon")
55         } else {
56             push @out, $notfound;
57         }
58     while (!$t->eof) {
59         eval { 
60             push @out, $t->getline(Timeout => 10);
61         };
62         if ($@) {
63             push @out, $self->msg('e18', 'sk3bg.se');
64             last;    
65         }
66     }
67 }
68 $t->close;
69
70 return (1, @out);