fix sh/contest and add CTY2308 prefixes
[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 [<month>] [<year>], e g sh/contest sep 2012") unless $line;
13
14 my @out;
15
16 my $mon;;
17
18 #$DB::single = 1;
19
20
21 # trying to make the syntax abit more user friendly...
22 # and yes, I have been here and it *is* all my fault (dirk)
23 $line = lc $line;
24 my ($m,$y);
25 ($y) = $line =~ /(\d+)/;
26 ($m) = $line =~ /([a-z]{3})/;
27
28 unless ($y) {
29         ($y) = (gmtime)[5];
30         $y += 1900;
31 }
32 unless ($m) {
33         ($m) = (gmtime)[4];
34         $m = lc $DXUtil::month[$m];
35 }
36 $y += 2000 if $y <= 50;
37 $y += 1900 if $y > 50 && $y <= 99;
38 $m = substr $m, 0, 3 if length $m > 3;
39 $m = 'oct' if $m eq 'okt';
40 $m = 'may' if $m eq 'mai' || $m eq 'maj';
41 $mon = "$y$m";
42
43 dbg("sh/contest: month=$mon") if isdbg('contest');
44
45 my $filename = "c" . $mon . ".txt";
46 my $host = $Internet::contest_host || 'www.sk3bg.se';
47 my $port = 80;
48
49 dbg("sh/contest: host=$host:$port") if isdbg('contest');
50
51 my $url = $Internet::contest_url || "/contest/text";
52 $url .= "/$filename";
53
54 dbg("sh/contest: url=$url") if isdbg("contest");
55
56 my $t = new Net::Telnet (Telnetmode => 0);
57 eval { $t->open(Host => $host, Port => $port, Timeout => 15); };
58
59 if (!$t || $@) {
60     push @out, $self->msg('e18','sk3bg.se');
61 } else {
62     my $s = "GET $url HTTP/1.0";
63         dbg("sh/contest: get='$s'") if isdbg('contest');
64         
65     $t->print($s);
66         $t->print("Host: $host\n");
67         $t->print("\n\n");
68
69     my $notfound = $t->getline(Timeout => 10);
70     if (!$notfound || $notfound =~ /404 Object Not Found/) {
71             push @out, "there is no contest info for $mon at $host/$url";
72                 return (1, @out);
73         } else {
74             push @out, $notfound;
75         }
76     while (!$t->eof) {
77                 eval { push @out, $t->getline(Timeout => 10); };
78                 if ($@) {
79                         push @out, $self->msg('e18', 'sk3bg.se');
80                         last;
81                 }
82     }
83 }
84 $t->close;
85
86 return (1, @out);