#!/usr/bin/perl -w # WeatherPrim # by: Andrew Burton - tuglyraisin@aol.com ##### ##### # modules use HTTP::Response; use LWP::UserAgent; use Net::POP3; use strict; use XML::Parser; # mail variables my $id = 'identity'; my $pw = 'password'; my $servername = 'mail.domain.com'; # Second Life variable my $slXmlGateway = 'http://xmlrpc.secondlife.com/cgi-bin/xmlrpc.cgi'; my @data = ; # Weather Bug XML parsing globals my $xmlp = XML::Parser->new(Handlers => {Start => \&xml_start, End => \&xml_end, Char => \&xml_char}); my $thisday = ''; my $checkx = 0; my $tag = ''; my $wday = ''; my $wdes = ''; my @weathers = (); # miscellaneous variables my $checkm = 0; my $messtext = ''; my %entry = (); # main loop while (1) { # Creates the connection my $pop = &make_pop(); # Logs in # This will just die if the id and pwd are wrong my $logcheck = $pop->login($id, $pw); unless ($logcheck) { die "Die could not login.\n"; } my $msg = $pop->list(); unless ($msg =~ m/0E0/i) { foreach my $msgid (keys(%$msg)) { my $msg_sender = $pop->get($msgid); foreach my $mline (@$msg_sender) { if ($checkm == 1) { unless ($mline =~ m/^\n/) { $messtext .= $mline; } } if ($mline =~ m/^Object-Name: WeatherPrim/i) { $checkm = 1; } } my @emlines = split(/\n/, $messtext); my ($xmlkey, $wxbkey, $zipcode, $theday) = split(/\,/, $emlines[2]); if ($theday eq 'today') { $theday = 'tonight'; } my $wreport = &getweather($wxbkey, $zipcode, $theday); if ($wreport ne "no connection") { &sendweather($xmlkey, "100", $wreport); } else { &sendweather($xmlkey, "400", "Error"); } # Deletes the message $pop->delete($msgid); # Resets some global variables; $wreport = ''; $checkm = 0; $messtext = ''; $thisday = ''; $checkx = 0; $tag = ''; $wday = ''; $wdes = ''; # Cleans out @weathers while($weathers[0]) { pop(@weathers); } } } # closes the connection $pop->quit(); # Waits a minute before posting sleep(30); } ##### sub sendweather { my ($slkey, $code, $weather) = @_; my $envelope = ''; my @dtemp = @data; # Loads XML-RPC request into memory foreach my $line (@dtemp) { if ($line) { print "$line"; $line =~ s/\$slkey/$slkey/; $line =~ s/\$code/$code/; $line =~ s/\$weather/$weather/; $envelope .= $line; print "$line"; } } # Builds the HTTP request my $slua = LWP::UserAgent->new(); my $request = HTTP::Request->new(POST => $slXmlGateway); $request->content_type('text/xml; charset=utf-8'); $request->content($envelope); # Fires off the HTTP Request my $result = $slua->request($request); if ($result->is_success) { if ($result->content =~ m/Recieved/) { print "101: Connection made.\n"; } else { print "\n\n" . $result->content . "\n\n"; } } else { print "401: Weather Bug not found.\n"; } } ##### sub getweather { my ($bugcode, $zipcode, $theday) = @_; my $ua = LWP::UserAgent->new(); my $url = 'http://' . $bugcode . '.api.wxbug.net/getFullForecast.aspx?' . 'acode=' . $bugcode . '&zipcode=' . $zipcode; my $result = $ua->get($url); if ($result->is_success) { $thisday = $theday; my $xml = $result->content; $xmlp->parsestring($xml); my $retval = join('|', @weathers); return($retval); } else { return("no connection"); } } # XML Handlers # Open tag event sub xml_start { my ($expat, $el, %attr) = @_; $tag = $el; } # Character tag event sub xml_char { my ($expat, $el) = @_; if ($el =~ m/$thisday/i) { $checkx = 1; } if ($tag eq 'aws:title' && $checkx == 1) { $wday = $el; } elsif ($tag eq 'aws:short-title' && $checkx == 1) { $wdes = $el; } } # Close tag event sub xml_end { my ($expat, $el) = @_; if ($el eq "aws:forecast" && $checkx == 1) { if (($wday) && ($wdes)) { my $temp = $wday . ',' . $wdes; push(@weathers, $temp); } $checkx = 0; } } # Creates the POP3 connection sub make_pop { my $pop = Net::POP3->new($servername, Default => 1); unless ($pop) { print "Cannot make POP3 connection.\n"; sleep(5); $pop = &make_pop(); } return($pop); } __DATA__ llRemoteData Channel $slkey IntValue $code StringValue $weather