#!/usr/local/bin/perl 'di'; 'ig00'; # # A perl script to connect to the 'weather server' at # Michigan and get the forcast for whatever city # you want (3 letter code -- columbus 'cmh' by default). # # Alternatively, you can get the current information for # a state if you enter a 2 letter code. # # Thanks to J Greely for the original network code, and # Tom Fine for assistance and harassment. # # Copyright 1991 Frank Adelstein. All Rights Reserved. # # Permission to use, copy, modify, and distribute this # software is hereby granted without fee, provided that # the copyright notice and permission notice are not removed. # # --FNA 6/28/91 # # Hacked by George Ferguson (ferguson@cs.rochester.edu) to include # Canadian forecasts by zone number. # # Modified by gf for new weather service menus, 26 Jun 1992. # I ripped lots of stuff out of this version, some of it may have to be # put back, but it works fine for me. # It's much easier to navigate the system now (for US forecasts, anyway), # so the interaction loop is considerably simplified. The Canadian stuff # is relatively straightforward also. # # Modified by rouilj@cs.umb.edu October 9, 1992. Changed pattern to # match almost all "type return to continue" type lines. Hopefully the # entire report will be presented to the user now. Also added flags # that allow selection of the info to get from the weather server. In # addition added code that makes use of the menu bypass ability, and # allows supression of the 24 line scroll restriction. Added the -q # flag that can be used to list (query) the three letter abbreviations # for all weather reporting sites within a state. In addition changed # the default city to be Boston ;-). Also crafted (crufted??) together # a rudimentary man page, and encapsulated it into the weather script. # # Modified by gregor@kafka.saic.com October 26, 1992. Added the -h # flag for help. Formats the script's man page. Also changed the # default city to be San Diego. # # Modified by metcalf@akala.ifa.hawaii.edu to extract the hurricane advisories, # 94-08-02 use Socket; @SERVER = ("madlab.sprl.umich.edu", "thunder.met.fsu.edu", "downwind.sprl.umich.edu", "OOPS"); $PORT = "3000"; # # Connect to the server # local($sockaddr,$here,$there,$response,$tries) = ("Snc4x8"); SLOOP: foreach $s (@SERVER) { if ($s eq "OOPS") { die "$0 connect: $!\n";} $there = pack($sockaddr,2,$PORT,&getaddress($s)); $proto = getprotobyname('tcp'); die "$0 socket: $!\n" if (!socket(SOCK,PF_INET,SOCK_STREAM,$proto)); # die "$0 socket: $!\n" if (!socket(SOCK,2,1,6)); if (!connect(SOCK,$there)) { shutdown(SOCK,2); } else { last SLOOP; } } select(SOCK); $| = 1; select(STDOUT); $| = 1; # make unbuffered # # Initialize # $SHOWIT = 0; # Should we print? $hurr_found = 0; $hurr_num = 8; $atl_done = 0; $atl_num = 1; # Default menu numbers. Actual values derived below $nep_done = 0; $nep_num = 2; $cnp_done = 0; $cnp_num = 3; $nwp_done = 0; $nwp_num = 4; $ind_done = 0; $ind_num = 5; # # Interact... # while (read(SOCK,$c,1)) { # Get a character if ($c eq "\n") { # Newline -> maybe print, start new line if ($SHOWIT == 1) { $curline =~ s/\cC/NNNN/; # Control-C marks the end-of-message print $curline, "\n"; } $curline = ""; next; } if ($c eq "\r") { next; } # Return -> ignore $curline .= $c; # Else add char to current line # # Now test the current line so far to see what action to take, if any. # if ($curline =~ /Press Return for menu, or enter 3 letter forecast city code:/) { # At first prompt... printf SOCK "\n"; # go to main menu $curline = ""; } elsif ($curline =~ / (\d+)\).*Hurricane advisories/ && !$hurr_found) { # Get menu number $hurr_num = $1; } elsif ($curline =~ / (\d+)\).*Atlantic Ocean/ && $hurr_found) { # Get menu number $atl_num = $1; } elsif ($curline =~ / (\d+)\).*East Pacific Ocean/ && $hurr_found) { # Get menu number $nep_num = $1; } elsif ($curline =~ / (\d+)\).*Central Pacific Ocean/ && $hurr_found) { # Get menu number $cnp_num = $1; } elsif ($curline =~ / (\d+)\).*West Pacific Ocean/ && $hurr_found) { # Get menu number $nwp_num = $1; } elsif ($curline =~ / (\d+)\).*Indian Ocean/ && $hurr_found) { # Get menu number $ind_num = $1; } elsif ($curline =~ / Selection:/) { # We're at a menu prompt if ($hurr_found) {&showiton("Selection");} if (!$hurr_found) { printf SOCK "$hurr_num\n"; $curline = ""; # Go to Hurricanes from main menu } elsif (!$atl_done) { printf SOCK "$atl_num\n"; $atl_done=1; $curline = ""; print "NNNN\n"; } elsif (!$nep_done) { printf SOCK "$nep_num\n"; $nep_done=1; $curline = ""; print "NNNN\n"; } elsif (!$cnp_done) { printf SOCK "$cnp_num\n"; $cnp_done=1; $curline = ""; print "NNNN\n"; } elsif (!$nwp_done) { printf SOCK "$nwp_num\n"; $nwp_done=1; $curline = ""; print "NNNN\n"; } elsif (!$ind_done) { printf SOCK "$ind_num\n"; $ind_done=1; $curline = ""; print "NNNN\n"; } else { printf SOCK "x\n"; $curline = ""; &showitoff("Exit"); print "NNNN\n"; } } elsif ($curline =~ /HURRICANE ADVISORIES/) { $hurr_found++; &showitoff("HURRICANE ADVISORIES") } elsif ($curline =~ / Press Return (to continue|for menu).*: /) { printf SOCK "\n"; $curline = ""; &showiton("Return to continue"); } } # # Clean up and done # close(SOCK); exit(0); ##################################################### sub getaddress { local($host) = @_; local(@ary); @ary = gethostbyname($host); return(unpack("C4",$ary[4])); } sub showitoff { local($txt) = @_; &maybeprint ("showit off ($txt)\n"); $SHOWIT = 0; } sub showiton { local($txt) = @_; &maybeprint ("showit on ($txt)\n"); $SHOWIT = 1; } sub maybeprint { # print @_; } ###############################################################