# This script is for weather, based on zip code or city, country to use try 
# the following commands:
#
#    !weather zipcode
#    OR
#    !weather city, state
#    OR
#    !weather city, country
#
#    Same commands for !temp
#
# This script was originally written as an addon for another bot, that uses !
# as it's trigger.
#
# Revision 03.15.04 - Now does international weather! Also has !temp option. 
# Includes temps in C.
#
# 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 b2m (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 = "3.1";
%IRSSI = (
    author => 'pleia2',
    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 (.*)/i )

        {

                my ($location) = $text =~ /!weather (.*)/;
		$location =~ tr/ /_/;
		my $ua = new LWP::UserAgent;
                        $ua->timeout(10);
                        my $results = $ua->get("http://38.102.136.104/auto/raw/$location");
                        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/city,country");
                        }
                        elsif ( $badarray[1] =~ /<center><b>Search not found:|<p align=center><b><font size*/ ) {
                                $server->command ( "msg $target Sorry, no weather available for your location, please try !weather zipcode/city,country");
                        }
                        else {
                        my @goodarray = split(/[|]\s*/, $results->content);
			if ($goodarray[1] != "") { $ctemp = sprintf( "%4.1f", ($goodarray[1] - 32) * (5 / 9) );}
                                $server->command ( "msg $target  \002Conditions for $goodarray[18], $goodarray[19] at $goodarray[0]:\002 $goodarray[8] \002Temp:\002 $goodarray[1] F/$ctemp C \002Humidity:\002 $goodarray[4] \002Barometer:\002 $goodarray[7] \002Wind:\002 $goodarray[6] mph");
                        }
	}
elsif ( $text =~ /^!temp (.*)/i )

        {
                my ($location) = $text =~ /!temp (.*)/;
		 $location =~ tr/ /_/;

                        my $ua = new LWP::UserAgent;
                        $ua->timeout(10);
                        my $results = $ua->get("http://38.102.136.104/auto/raw/$location");
                        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/city,country");
                        }
                        elsif ( $badarray[1] =~ /<center><b>Search not found:|<p align=center><b><font size*/ ) {
                                $server->command ( "msg $target Sorry, no temperature available for your location, please try !temp zipcode/city,country");
                        }
                        else {
                        my @goodarray = split(/[|]\s*/, $results->content);
			$ctemp = sprintf( "%4.1f", ($goodarray[1] - 32) * (5 / 9) );
                                $server->command ( "msg $target  \002Current Temperature for $goodarray[18], $goodarray[19]:\002 $goodarray[1] F/$ctemp C");
                        }
        }
}




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