#!/usr/bin/perl -w # Script to automaticly get emails from pop3 servers using getmail # Author: Felipe Ferreira Date: 14/11/08, UP 20/11/08 # Version : 3.0 # 1. Open .txt file and read parameters, User,Pass,MailServer # 2. test if mail server and credentials, OK #### SHOULD COUNT, IF 0 DONT START DOWNLOAD (avoid creating empty folders!) # 3. Download mailbox using getmail, OK #### SHOUOLD BE IN QUITE MODE # 3.a Create the config file and the folders # 4. Search Strings in Mailbox #### WHEN FINDS MATCHS, CP EMAILS TO INTERSTING FOLDER #use strict; use warnings; use Regexp::Trie; use Net::POP3; my $fileSkel = "new($mailserver,Timeout => 5) or ($testpop = 1); if ($testpop ne 1) { $tot_msg = $pop3->login($user,$pass) or ($testpop = 1); } if ($tot_msg gt 0) { # printf("\n->There are $tot_msg messages\n\n"); #Log to Text file open FILEOUT, "+>>", "/scripts/popOK.txt" or die $!; print FILEOUT $user . ";" . $pass .";". $mailserver . "\n"; close FILEOUT; $countfound++; $testpop = 0; } else { $testpop = 1; } $tot_msg = 0; } #creates folders, and download emails sub downloadmx() { my @lines; my $line; #Must get only left part of email, to create folders. if ($user =~ /@/) { $user =~ s/@/\@/; (split "@", $user); $userP = $_[0]; } else { $userP = $user; } $homeMXfolder = "/home/Mailbox/".$userP; #Create Folder structure where to Download system("mkdir $homeMXfolder"); system("mkdir $homeMXfolder/cur"); system("mkdir $homeMXfolder/tmp"); system("mkdir $homeMXfolder/new"); my $fileout = $homeMXfolder."/config.txt"; ##Open skeleton file in Read mode and parse it open FILE, $fileSkel or die $!; @lines = ; #assigns lines to array close FILE; open FILEOUT, "+>".$fileout or die $!; foreach $line (@lines) #go through each line in file { #regex to substitute var1,var2,var3 to server,user,pass $line =~ s/var1/$mailserver/g; $line =~ s/var2/$user/g; $line =~ s/var3/$pass/g; $line =~ s/var4/$userP/g; print FILEOUT $line; } close FILEOUT; print "\n saved to: ". $fileout."\n"; $homeMXfolder = $homeMXfolder."/"; print "Downloading mails to: " . $homeMXfolder ."\n"; #set folder perimssions system("chown downmx -R /home/Mailbox/"); #download using downmx user account, since from root its not allowed system("su -c 'getmail --rcfile $fileout --getmaildir $homeMXfolder' downmx"); } #Search for strings inside \new DIR sub search() { my $Mcounter = 0; # Matches Found counter my $Fcounter = 0; # Files Searched counter my $REGEX2 = "\b(?:\\d[ -]*){13,16}\b"; #my $homeMXfolder = "/home/Mailbox/ogigant81/new/"; my @allfiles; my $file; my $filename; my @Slines; my $Sline; $homeMXfolder = $homeMXfolder."new/"; #should do a for loop in entire directory @allfiles = `ls $homeMXfolder`; foreach $file (@allfiles) { $filename = "<". $homeMXfolder . $file; #print "Searching in: ".$filename ."\n"; open SEARCHFILE, $filename or die $!; @Slines = ; #assigns lines to array foreach $Sline (@Slines) { if (($Sline =~ qr(\b$REGEX\b)) || ($Sline =~ /$REGEX2/)) { print "Match in: " .$filename. "\n"; print $Sline ."\n"; print "_______________________________________________________________________________\n"; $Mcounter++; #SHOULD CP EMAILS TO ANOTHER FOLDER CALLED INTERESTING } # each regex MATCHES found } # each LINE loop close SEARCHFILE; $Fcounter++; } # loop thru each file in DIR print "\n Found " . $Mcounter . " matches in ". $Fcounter. " files. \n"; #CLEAN UP #$homeMXfolder = ""; } #Get words from txt file and make list of words to search sub buildregex() { my $rt = Regexp::Trie->new; open(IN, "/scripts/phrases.txt"); my @list = ; for (@list){ chomp; $rt->add($_); } $REGEX = $rt->regexp; print "$REGEX\n"; } sub getinfo() { my @vals; print "staring...\n Opening " .$fileInfo."\n"; #open the file in Read mode and parse it open FILEINFO, $fileInfo or die $!; while () { #Parse using Regex to get values and print out to file if (/,/) { print "Line:" . $_; @vals = split /,/; #better coding would be assign to a var #print "Number of values splited: " . $#_ . "\n"; # if (scalar @vals eq 2) { # print "First Value: " . $vals[0], "\n"; # print "Second Value: " . $vals[1], "\n"; #print "Third Value: " . $vals[2], "\n"; #Set variables $user = $vals[0]; $pass = $vals[1]; $mailserver = $vals[2]; # remove leading whitespace, and remove trailing whitespace $user =~ s/^\s+//; $user =~ s/\s+$//; $pass =~ s/^\s+//; $pass =~ s/\s+$//; $mailserver =~ s/^\s+//; $mailserver =~ s/\s+$//; $testpop = -1; poptest(); if ($testpop eq 0) { print "---Login OK---\n"; #START DOWNLOADING main(); } else { print "LOGIN ERROR\n"; } # test if POP server/pass is OK } } # close while close FILEINFO; print "Found: ". $countfound . " good pop3 logins\n"; } #CALLS ALL OTHER SUBS sub main() { downloadmx(); buildregex(); search(); } getinfo();