# This script is only for United States weather, based on zip code to use try
# the following commands:
#
#    !weather zipcode
#
#    This returns current weather as reported on weather.com
#
#    !forecast zipcode
#
#    This retures the current extended forecast for 6 days (including current)
#    as reported on weather.com
#    
#
# This script was originally written as an addon for another bot, that uses !
# as it's trigger.
#
#
# Revision 08.26.03 - No longer requires Geo::Weather Module to run
#
# Originally Created On 08.04.03
# 

use Irssi;

$VERSION = "1.1";
%IRSSI = (
    author => 'PrincessLeia2',
    contact => 'lyz\@princessleia.com ',
    name => 'weatherbot',
    description => 'a US weatherbot that provides weather and forecast based on zip code',
    license => 'GNU GPL v2 or later',
    url => 'http://www.princessleia.com'
);

sub event_privmsg {
        my ($server, $data) =@_;
        my ($target, $text) = $data =~ /^(\S*)\s:(.*)/;
        return if ( $text !~ /^!/i );



if ( $text =~ /^!weather /i )

        {

                my ($zip) = $text =~ /!weather (.*)/;

                        `wget -q http://www.w3.weather.com/outlook/driving/local/$zip`;

                        $pagehere = `grep "Sorry, the page you requested was not found on weather.com." $zip`;

                        $error = "<FONT CLASS=\"errorText\">Sorry, the page you requested was not found on weather.com.</FONT><BR>";

                        if ($pagehere =~ /.*$error/){

                                $server->command ( "msg $target Sorry, no weather available for your location, please type !weather zipcode");
                        }

                        else {

                        $onelocation = `grep '<TR><TD WIDTH="100%" CLASS="titleBar"><B>' $zip`;
                        $onecondition = `grep obsInfo2 $zip`;

                        my @location = split(/\n/, $onelocation);

                        my @condtions = split(/<\/TD>\n/, $onecondition);

                        my ($space, $place) = $location[3] =~ /(.*)<TR><TD WIDTH="100%" CLASS="titleBar"><B>Your (.*) ...<\/B><\/TD><\/TR>/;

                        my ($temperature) = $condtions[0] =~ /<TD VALIGN=MIDDLE ALIGN=CENTER CLASS=obsInfo2 WIDTH=50%><B CLASS=obsTempTextA>(.*)&deg;F<\/B>/;
                        my ($condition) = $condtions[1] =~ /<TD VALIGN=TOP ALIGN=CENTER CLASS=obsInfo2><B CLASS=obsTextA>(.*)<\/B>/;
                        my ($humidity) = $condtions[5] =~ /<TD ALIGN=LEFT VALIGN=TOP CLASS=obsInfo2>(.*)/;
                        my ($barometer) = $condtions[7] =~ / <TD ALIGN=LEFT VALIGN=TOP CLASS=obsInfo2>(.*)/;
                        my ($teat) = $condtions[8] =~ /(.*)/;

                                $server->command ( "msg $target  \002Current Conditions for $place :\002 $condition \002Temp:\002 $temperature F \002Humidity:\002 $humidity \002Barometer:\002 $barometer");
                        }

                `rm $zip`

        }

elsif ($text =~ /^!forcast/i ) {
        $server->command ( "msg $target Learn to SPELL!!! use !forecast zipcode" );
}

elsif ( $text =~ /^!forecast/i )
        {

                my ($zip) = $text =~ /!forecast (.*)/;

                        `wget -q http://www.w3.weather.com/outlook/driving/local/$zip`;

                        $pagehere = `grep "Sorry, the page you requested was not found on weather.com." $zip`;

                        $error = "<FONT CLASS=\"errorText\">Sorry, the page you requested was not found on weather.com.</FONT><BR>";

                        if ($pagehere =~ /.*$error/){

                                $server->command ( "msg $target Sorry, no forecast available for your location, please type !forecast zipcode");
                        }

                        else {

                        `wget -q http://www.w3.weather.com/outlook/driving/local/$zip`;

                        $oneday = `grep inDentWhite10DayA11 $zip`;

                        $twoday = `grep inDentBlue10DayB11 $zip`;

                        $onefore = `grep inDentWhite10DayA5 $zip`;

                        $twofore = `grep inDentBlue10DayB5 $zip`;

                        $onelocation = `grep '<TR><TD WIDTH="100%" CLASS="titleBar"><B>' $zip`;

                        my @day1 = split(/<\/TD>\n/, $oneday);

                        my @day2 = split(/<\/TD>\n/, $twoday);

                        my @fore1 = split(/<\/TD>\n/, $onefore);

                        my @fore2 = split(/<\/TD>\n/, $twofore);

                        my @location = split(/\n/, $onelocation);

                        my ($next0, $date0) = $day1[0] =~ /<TD CLASS=inDentWhite10DayA11><A HREF=\/outlook\/driving\/detail\/$zip>(.*)<\/A><BR> (.*)/;
                        my ($next1, $date1) = $day2[0] =~ /<TD CLASS=inDentBlue10DayB11><A HREF=\/outlook\/driving\/detail\/$zip\?dayNum=1>(.*)<\/A><BR> (.*)/;
                        my ($next2, $date2) = $day1[1] =~ /<TD CLASS=inDentWhite10DayA11><A HREF=\/outlook\/driving\/detail\/$zip\?dayNum=2>(.*)<\/A><BR> (.*)/;
                        my ($next3, $date3) = $day2[1] =~ /<TD CLASS=inDentBlue10DayB11><A HREF=\/outlook\/driving\/detail\/$zip\?dayNum=3>(.*)<\/A><BR> (.*)/;
                        my ($next4, $date4) = $day1[2] =~ /<TD CLASS=inDentWhite10DayA11><A HREF=\/outlook\/driving\/detail\/$zip\?dayNum=4>(.*)<\/A><BR> (.*)/;
                        my ($next5, $date5) = $day2[2] =~ /<TD CLASS=inDentBlue10DayB11><A HREF=\/outlook\/driving\/detail\/$zip\?dayNum=5>(.*)<\/A><BR> (.*)/;

                        my ($nextfore0) = $fore1[1] =~ /<TD CLASS=inDentWhite10DayA5>(.*)/;
                        my ($nextfore1) = $fore2[1] =~ /<TD CLASS=inDentBlue10DayB5>(.*)/;
                        my ($nextfore2) = $fore1[5] =~ /<TD CLASS=inDentWhite10DayA5>(.*)/;
                        my ($nextfore3) = $fore2[5] =~ /<TD CLASS=inDentBlue10DayB5>(.*)/;
                        my ($nextfore4) = $fore1[9] =~ /<TD CLASS=inDentWhite10DayA5>(.*)/;
                        my ($nextfore5) = $fore2[9] =~ /<TD CLASS=inDentBlue10DayB5>(.*)/;


                        my ($temp) = $fore1[2] =~ /<TD CLASS=inDentWhite10DayA5><DIV ALIGN=CENTER><B>(.*)&deg;<\/B><\/DIV>/;
                                if ($temp =~ /.*&deg;\/.*/) {
                                        my ($high, $low) = $temp =~ /(.*)&deg;\/(.*)/;
                                        $temp = ("$high/$low");
                                }

                        my ($nexthigh1, $nextlow1) = $fore2[2] =~ /<TD CLASS=inDentBlue10DayB5><DIV ALIGN=CENTER><B>(.*)&deg;\/(.*)&deg;<\/B><\/DIV>/;
                        my ($nexthigh2, $nextlow2) = $fore1[6] =~ /<TD CLASS=inDentWhite10DayA5><DIV ALIGN=CENTER><B>(.*)&deg;\/(.*)&deg;<\/B><\/DIV>/;
                        my ($nexthigh3, $nextlow3) = $fore2[6] =~ /<TD CLASS=inDentBlue10DayB5><DIV ALIGN=CENTER><B>(.*)&deg;\/(.*)&deg;<\/B><\/DIV>/;
                        my ($nexthigh4, $nextlow4) = $fore1[10] =~ /<TD CLASS=inDentWhite10DayA5><DIV ALIGN=CENTER><B>(.*)&deg;\/(.*)&deg;<\/B><\/DIV>/;
                        my ($nexthigh5, $nextlow5) = $fore2[10] =~ /<TD CLASS=inDentBlue10DayB5><DIV ALIGN=CENTER><B>(.*)&deg;\/(.*)&deg;<\/B><\/DIV>/;

                        my ($nextpop0) = $fore1[3] =~ /<TD CLASS=inDentWhite10DayA5><DIV ALIGN=CENTER>(.*)<\/DIV>/;
                        my ($nextpop1) = $fore2[3] =~ /<TD CLASS=inDentBlue10DayB5><DIV ALIGN=CENTER>(.*)<\/DIV>/;
                        my ($nextpop2) = $fore1[7] =~ /<TD CLASS=inDentWhite10DayA5><DIV ALIGN=CENTER>(.*)<\/DIV>/;
                        my ($nextpop3) = $fore2[7] =~ /<TD CLASS=inDentBlue10DayB5><DIV ALIGN=CENTER>(.*)<\/DIV>/;
                        my ($nextpop4) = $fore1[11] =~ /<TD CLASS=inDentWhite10DayA5><DIV ALIGN=CENTER>(.*)<\/DIV>/;
                        my ($nextpop5) = $fore2[11] =~ /<TD CLASS=inDentBlue10DayB5><DIV ALIGN=CENTER>(.*)<\/DIV>/;

                        my ($space, $place) = $location[3] =~ /(.*)<TR><TD WIDTH="100%" CLASS="titleBar"><B>Your (.*) ...<\/B><\/TD><\/TR>/;

                                $return = ("\002Forecast for $place : $next0 $date0\002 $nextfore0 Temp: $temp POP: $nextpop0 \002$next1 $date1\002 $nextfore1 Temp: $nexthigh1/$nextlow1 POP: $nextpop1 \002$next2 $date2\002 $nextfore2 Temp: $nexthigh2/$nextlow2 POP: $nextpop2 \002$next3 $date3\002 $nextfore3 Temp: $nexthigh3/$nextlow3 POP: $nextpop3 \002$next4 $date4\002 $nextfore4 Temp: $nexthigh4/$nextlow4 POP: $nextpop4 \002$next5 $date5\002 $nextfore5 Temp: $nexthigh5/$nextlow5 POP: $nextpop5");

                                        $server->command ( "msg $target $return");

                        `rm $zip`;
                }
        }

}

Irssi::signal_add('event privmsg', 'event_privmsg');