#Charles Oppenheimer 5/6/2008 - Perl Artistic License # blackquery use strict; use Net::POP3; use Mail::Internet; use Yahoo::Search; use Log::Logger; use Mail::Sender; use Error qw(:try); ########################################################### #config options my $lh = new Log::Logger "blackquery.log"; # global log file my $AppId = 'YahooDemo'; #pop3 mailserver settings my $mailserver = 'mail.yourserver.com'; my $user = 'query@yourserver.com'; my $password = 'query'; my $smtpauth = 'usesr'; my $smtppass = 'password'; my $smtpfrom = 'user@yoursmtpserver.com'; my $smtpserver = 'smtp.yourserver.com'; my $smtpport = 587; #yahoo port - many ISP block port 25 my $sender; # object for sending SMTP $lh->log_print("Initializing SMTP mailserver"); &initsmtp; ### main loop while (1) { print localtime() . " connecting to POP $mailserver\n"; my $pop; my $poplogin; $pop = Net::POP3->new($mailserver, Timeout => 60); try { $poplogin = $pop->login($user, $password ); } catch Error with { my $ex = shift; # Get hold of the exception object $lh->log_print("ERROR! $ex In main while loop " . localtime()); } finally { }; # <-- Remember the semicolon if ($poplogin > 0) { #greater than 0 if there is mail $lh->log_print("sucessfull login to POP3 server, has mail"); my $msgnums = $pop->list; # hashref of msgnum => size foreach my $msgnum (keys %$msgnums) { my $header = $pop->top($msgnum, 0); my ($subject, $from, $status) = analyze_header($header); #print "$subject, $from, $status\n"; $lh->log_print("retrived message,$from, $subject, doing query."); try { #consider farming the query and reply to a different process? my $results = blackquery ($subject, $from); #reply sendreplysmtp ($subject, $from, $results); $pop->delete($msgnum); } catch Error with { my $ex = shift; # Get hold of the exception object $lh->log_print("ERROR! $ex In main while loop " . localtime()); } finally { }; } #end foreach } try { $pop->quit; } catch Error with { my $ex = shift; # Get hold of the exception object $lh->log_print("ERROR! $ex In main while loop " . localtime()); } finally { }; sleep(1); } ############################ sub blackquery { my $subject = shift; my $from = shift; my @body = @_; #using Yahoo #take subject, execute query # config parameters my $AppId = 'YahooDemo'; my $count = 3; my $Mode = 'all'; my $Type = 'any'; #benchmark my @Results = Yahoo::Search->Results(Doc => $subject, AppId => $AppId, # The following args are optional. # (Values shown are package defaults). Mode => $Mode , # all words Start => 0, Count => $count, Type => 'any', # all types AllowAdult => 0, # no porn, please AllowSimilar => 0, # no dups, please Language => undef, ); warn $@ if $@; # report any errors $lh->log_print("finished query $subject"); #format code # create a string, with formats... # allow options to have only URLs, titiles, and summaries, or 1st page lucky parse? my $reply; for my $Result (@Results) { $reply = $reply . "Title: " .$Result->Title . " " . $Result->Summary . "\n"; $reply = $reply . $Result->Url . "\n"; $reply = $reply . "\n"; } return $reply; #print "my reply = $reply\n"; } sub initsmtp { my $smtp_authentication = "TRUE"; my %conhash; #hard coded because we are using yahoo as smtp $conhash{from} = $smtpfrom; $conhash{smtp} = $smtpserver; $conhash{replyto} = $user; $conhash{fake_from} = $user; if ($smtp_authentication =~ "TRUE") { $conhash{auth} = "LOGIN"; #hard coded! eek. but hey, mail and login based auth should be enough eh? $conhash{authid} = $smtpauth ; $conhash{authpwd} = $smtppass; } $conhash{Port} = $smtpport; ref ($sender = new Mail::Sender({%conhash})) or $lh->log_print("Mail ERROR = $Mail::Sender::Error"); $lh->log_print("Conneced to smtp server $smtpserver "); } sub sendreplysmtp { my $subject = shift; my $to = shift; #the address it came $from is now the $to my $body = shift; $lh->log_print("Sending message..."); ref ($sender->MailMsg({to => $to, subject => $subject, msg => $body})); #$lh->log_print("Mail sent to $from subject = $subject"); $lh->log_print("Sent message! $subject, $body"); } ########################## sub analyze_header { my $header_array_ref = shift; my $header = join "", @$header_array_ref; my ($subject) = $header =~ /Subject: (.*)/m; my ($from ) = $header =~ /From: (.*)/m; my ($status ) = $header =~ /Status: (.*)/m; if (defined $status) { $status = "Unread" if $status eq 'O'; $status = "Read" if $status eq 'R'; $status = "Read" if $status eq 'RO'; $status = "Ne $status = "-";w" if $status eq 'NEW'; $status = "New" if $status eq 'U'; } else { $status = "-"; } return ($subject, $from, $status); }