# This script is for weather, based on zip code.
#
# To use, try the following commands:
#
#    !weather zipcode
#
#    This returns current weather as reported on wutherground.com
#
#    !temp zipcode
#
#    This returns current temperature as reported on wutherground.com
#
# This script was originally written as an addon for another bot, that uses !
# as it's trigger.
#
#
# Revision 10.10.03 - Now requires LWP::UserAgent to run. Now uses
# wutherground.com instead of weather.com for weather reports, no longer
# supports "forecast"
#
# Revision 08.26.03 - No longer requires Geo::Weather Module to run
#
# Originally Created On 08.04.03
#
# Thanks to b2s (bored2sleep@hotmail.com) for telling me about
# wutherground.com's raw format weather pages and sending me his xchat weather
# script, this one has many similarities
#  

use Irssi;
use LWP::UserAgent;

$VERSION = "2.0";
%IRSSI = (
    author => 'PrincessLeia2',
    contact => 'lyz\@princessleia.com ',
    name => 'weatherbot',
    description => 'a 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 (\d\d\d\d\d)/i )

        {

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

                        my $ua = new LWP::UserAgent;
                        $ua->timeout(10);
                        my $results = $ua->get("http://www.wunderground.com/auto/raw/$zip");
                        my @badarray = split(/\n/, $results->content);
                        if ( ! $results->is_success ) {
                                $server->command ( "msg $target Sorry, no weather available for your location, please try !weather zipcode");
                        }
                        elsif ( $badarray[1] =~ /<center><b>Search not found:/ ) {
                                $server->command ( "msg $target Sorry, no weather available for your location, please try !weather zipcode");
                        }
                        else {
                        my @goodarray = split(/[|]\s*/, $results->content);
                                $server->command ( "msg $target  \002Current Conditions for $goodarray[18], $goodarray[19] at $goodarray[0]:\002 $goodarray[8] \002Temp:\002 $goodarray[1] F \002Humidity:\002 $goodarray[4] \002Barometer:\002 $goodarray[7] \002Wind:\002 $goodarray[6] mph");
                        }

        }


elsif ( $text =~ /^!weather (.*)/i )

        {
                $server->command ( "msg $target Please type !weather zipcode");
        }
elsif ( $text =~ /^!temp (\d\d\d\d\d)/i )

        {

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

                        my $ua = new LWP::UserAgent;
                        $ua->timeout(10);
                        my $results = $ua->get("http://www.wunderground.com/auto/raw/$zip");
                        my @badarray = split(/\n/, $results->content);
                        if ( ! $results->is_success ) {
                                $server->command ( "msg $target Sorry, no temperature available for your location, please try !temp zipcode");
                        }
                        elsif ( $badarray[1] =~ /<center><b>Search not found:/ ) {
                                $server->command ( "msg $target Sorry, no temperature available for your location, please try !temp zipcode");
                        }
                        else {
                        my @goodarray = split(/[|]\s*/, $results->content);
                                $server->command ( "msg $target  \002Current Temperature for $goodarray[18], $goodarray[19]:\002 $goodarray[1] F");
                        }

        }


elsif ( $text =~ /^!temp (.*)/i )

        {
                $server->command ( "msg $target Please type !temp zipcode");
        }
}




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