head     1.8;
branch   ;
access   ;
symbols  ;
locks    ; strict;
comment  @# @;


1.8
date     95.05.17.23.18.27;  author wwwmaint;  state Exp;
branches ;
next     1.7;

1.7
date     95.03.07.14.03.13;  author yonah;  state Exp;
branches ;
next     1.6;

1.6
date     95.02.08.21.21.20;  author probe;  state Exp;
branches ;
next     1.5;

1.5
date     95.02.04.00.00.53;  author probe;  state Exp;
branches ;
next     1.4;

1.4
date     95.01.28.11.16.45;  author probe;  state Exp;
branches ;
next     1.3;

1.3
date     95.01.28.11.09.54;  author probe;  state Exp;
branches ;
next     1.2;

1.2
date     94.11.15.03.13.10;  author nocturne;  state Exp;
branches ;
next     1.1;

1.1
date     94.11.15.03.06.35;  author nocturne;  state Exp;
branches ;
next     ;


desc
@matthew's magic weather script
@


1.8
log
@nocturne's modifications.
Changed a lot of links to have a preceding "/"; hopefully this will fix
some relative-URL problems.
@
text
@#!/usr/athena/bin/perl
# mkgray.pl -- gateway to weather
#
# $Id: weather,v 1.7 95/03/07 14:03:13 yonah Exp Locker: wwwmaint $
#
# Matthew Gray (mkgray@@athena.mit.edu)
#

require 'our-chat2.pl';
require 'citycode.pl';
require 'ourfork.pl';

$| = 1;
open(STDERR,">&STDOUT");

$query = join(' ', @@ARGV) if $ARGV[0];

&ourfork'safefork(15, "do_weather", $query);

# package weather;

sub do_weather {
    local($station) = @@_;

    print("Content-Type: text/html\n\n");

    $cmd = $ENV{'PATH_INFO'};
    $cmd =~ s%^/%%;

    if ($cmd eq "usmap") {
	&do_usmap($station);
    }
    elsif ($station) {
	print "<isindex>".&get_weather($station)."</pre>\n";
    }
    else{
	print <<"EndOfInfo";
<TITLE>Weather</TITLE>
<h1>Weather</H1>
This is the weather gateway.  If you find any bugs,
send mail to <a href="/webmasters.html"><em>webmaster@@mit.edu</em></a>
or enter your <a href="/comment">comments here</a>.<p>
Lately,
<a href="telnet://downwind.sprl.umich.edu:3000/">
the server from which we get our weather data</a>
has been kind of flaky. If your weather queries time out, 
this is most likely the reason why.<p>

If you know the <a href="/city-codes.html">city code</a> of the forecast
you want, enter it in the search field.<p>
<H2><A HREF="/usa.html">Click here for current weather map and forecasts
across the nation</A></H2><P>
<H2><A HREF="/weather?bos">Click here for Boston forecast</A></H2><P>
<ISINDEX>
EndOfInfo
    }
}

sub do_usmap {
    local($args) = @@_;
    local($x, $y);

    if ($args !~ /,/) {
	print "<isindex>" . &get_weather($args) . "</pre>\n";
    }

    ($x, $y) = split(',', $args);

    @@name = ('BOS','LAX','MCO','TOP','NYC','SLC','BWI','FTW','CHI','NEW',
	     'DSM','DEN','MEM','PHX','SEA','ATL');
    @@x = (750,100,675,440,700,225,700,400,550,500,475,300,600,200,150,613);
    @@y = (175,325,475,320,200,250,250,450,250,475,260,300,350,375,100,382);

    $min = 1000000;
    foreach $n (0..$#x){
	$locale = $name[$n];
	if(($dist=&distance($x, $y, $x[$n], $y[$n])) < $min){
	    $min = $dist;
	    $whereami = $locale;
	}
    }

    print("<title>Nearest Weather Station is $whereami</title><isindex>\n");
    print &get_weather($whereami)."</pre>\n";
}

sub distance {
    local($x1, $y1, $x2, $y2) = @@_;
    local($xd) = (($x1-$x2)*($x1-$x2));
    local($sqd)=$xd + (($y1-$y2)*($y1-$y2));
    $sqd;
}

sub get_weather {
    local($station, $more_than_one_match) = @@_;

    $rawstation = $station;

    $station =~ tr/[A-Z]/[a-z]/;
    $station =~ tr/[_\-\.\+]/ /;
    $station =~ s/%20/ /g;
	print <<"EndOfInfo2";
Lately,
<a href="telnet://downwind.sprl.umich.edu:3000/">the server from which we get
our weather data</a> has been kind of flaky. If your weather queries time
out, this is most likely the reason why. The weather server should be
working again, eventually.<p>
EndOfInfo2

    print"<title>Weather Conditions for $rawstation</title>";

    if (((length($station) > 3) || ($station eq "rio") || ($station eq "key")) && ($station ne "boston")) {

        %citycodes = &citycode'citycode_assoc_array; #'
	print"<h1>Weather Conditions for $rawstation</h1>";

	if ($city_code = $citycodes{$station}) {
	    @@matches = split('\:', $city_code);

	    $more_than_one_match = "yup" if ($#matches != 0);
	 
	    print"<hr>\nThis gateway is designed to accept three-letter ";
	    print"<a href=\"/city-codes.html\">city codes</a>. <p>\n";
	    print"Your query was not a valid city code. However, it\n";
	    print"matched one ", $more_than_one_match?"or more ":"";
	    print"of the entries we have in our\n";
	    print"local table of cities.\n<p>";
	    print"For your convenience, you may select ";
	    print $more_than_one_match?"one of these links.":"this link.","\n";
	    print"However, in order to minimize the load on our server, we\n";
	    print"ask that, if you wish to repeat this query in the future,\n";
	    print"you use ";
	    print $more_than_one_match?"one of these three-letter codes":"this three-letter code";
	    print" rather than the\n";
	    print"query you just made (\"$rawstation\").<p>\n";
	    print"<ul>\n";
	    for (@@matches) {
		print "<li><a href=\"/weather?";
		print($_."\">".$_." (".$rawstation,")</a>\n");
	    }
	    return;
	}
	else {
	    print"Your query was not a three-letter ";
	    print"<a href=\"/city-codes.html\">city code</a>,\n";
	    print"and it did not match any of the city-to-city-code\n";
	    print"mappings we have on our server. If you have a city\n";
	    print"code for the query you just submitted, please please\n";
	    print"let us know about it so that we can add it to our\n";
	    print"database. You can either send mail to\n";
	    print"<a href=\"/webmasters.html\">webmaster@@mit.edu</a> or\n";
	    print"use the forms-based ";
	    print"<a href=\"/comment\">comment gateway</a>\n";
	    print"on our server.\n<p>";
	    return;
	}
    }

    $closeme = &chat'open_port('downwind.sprl.umich.edu', 3000);
    &listen(2);
    &chat'print($station."\n");
    while($forecast!~/CITY FORECAST MENU/){
	$forecast .= &listen(1);
	&chat'print("\n");
    }
    &chat'print("x\n");
    &chat'close($closeme);
    $forecast=~s/Press Return to continue, M to return to menu, X to exit://g;
    $forecast=~s/CITY FORECAST MENU[\w\W]+//;
    $forecast=~s/\n\W\W([^\.\n]+)\.\.\./\n<h2>\1:<\/h2>/g;
    $forecast=~s/(Weather Conditions at .+)/<h1>\1<\/h1><pre>/;
    $forecast=~s/\n(.+)FORECAST\n/\n<h2>\1FORECAST<\/h2>\n/;
    $forecast=~s/Press Return for menu://;
    return $forecast;
}
sub listen {
        local($secs) = @@_;
        local($return,$tmp) = "";
        while (length($tmp = &chat'expect($secs, '(.|\n)+', '$&'))) {
                print $tmp if $trace;
                $return .= $tmp;
        }
        $return;
}

1;
@


1.7
log
@*** empty log message ***
@
text
@d4 1
a4 1
# $Id: weather,v 1.6 95/02/08 21:21:20 probe Exp Locker: yonah $
d41 2
a42 2
send mail to <a href="webmasters.html"><em>webmaster@@mit.edu</em></a>
or enter your <a href="comment">comments here</a>.<p>
d49 1
a49 1
If you know the <a href="city-codes.html">city code</a> of the forecast
d51 1
a51 1
<H2><A HREF="usa.html">Click here for current weather map and forecasts
d53 1
a53 1
<H2><A HREF="weather?bos">Click here for Boston forecast</A></H2><P>
d123 1
a123 1
	    print"<a href=\"city-codes.html\">city codes</a>. <p>\n";
d145 1
a145 1
	    print"<a href=\"city-codes.html\">city code</a>,\n";
d151 1
a151 1
	    print"<a href=\"webmasters.html\">webmaster@@mit.edu</a> or\n";
d153 1
a153 1
	    print"<a href=\"comment\">comment gateway</a>\n";
@


1.6
log
@You can now use the index after using the US map.
@
text
@d1 1
a1 1
#!/afs/athena/contrib/perl/p
d4 1
a4 1
# $Id: weather,v 1.5 1995/02/04 00:00:53 probe Exp $
@


1.5
log
@Make all the server references consistent (downwind.sprl.umich.edu:3000)
@
text
@d4 1
a4 1
# $Id: weather,v 1.4 1995/01/28 11:16:45 probe Exp probe $
a35 3



d44 4
a47 1
<a href="telnet://downwind.sprl.umich.edu:3000/">the server from which we get our weather data</a> has been kind of flaky. If your weather queries time out, this is most likely the reason why.<p>
d61 7
a67 1
    local($x, $y) = split(',', $args);
@


1.4
log
@Force STDERR to the same handle as STDOUT (to avoid confusing parent process)
@
text
@d4 1
a4 1
# $Id: weather,v 1.3 95/01/28 11:09:54 probe Exp Locker: probe $
d47 1
a47 1
<a href="telnet://madlab.sprl.umich.edu:3000/">the server from which we get our weather data</a> has been kind of flaky. If your weather queries time out, this is most likely the reason why.<p>
d98 1
a98 1
<a href="telnet://madlab.sprl.umich.edu:3000/">the server from which we get
@


1.3
log
@Append newline after </pre>
Force output to be line-buffered, as opposed to stream buffered.
@
text
@d4 1
a4 1
# $Id: weather,v 1.2 94/11/15 03:13:10 nocturne Exp Locker: probe $
d14 1
@


1.2
log
@no significant changes
@
text
@d4 1
a4 1
# $Id: weather,v 1.1 1994/11/15 03:06:35 nocturne Exp nocturne $
d13 2
d33 1
a33 1
	print "<isindex>".&get_weather($station)."</pre>";
d77 1
a77 1
    print &get_weather($whereami)."</pre>";
@


1.1
log
@Initial revision
@
text
@d4 1
a4 1
# $Id: weather.pl,v 1.12 1994/05/24 04:36:19 nocturne Exp $
d9 1
a9 1
require 'chat2.pl';
@
