#!perl ######################################################################### ######################################################################### # Written by Warren A. Kibbe, 4/3/97 # copyright 1997 Northwestern University # all rights reserved. Permission to use this in any noncommercial # application is granted. # # This demonstrates how to check a log and generate # an email response directly from MacPerl ######################################################################### ######################################################################### # Mail Messages ######################################################################### $replyto = ""; #replyto email address $testemail = ""; #will be sent here if "$test" is true $ccs = ""; #comma delimited list of emails $subj = ""; #subject line $header = ""; #prepended to any messages $footer = ""; #post pended $separator = "\t"; $linesep = "\n"; @allowedCategory = ("Appointment","Meeting"); $notifyhours = 72; # max number of hours ahead of time to mail notification ######################################################################### # Search String ######################################################################### $emailPat = "([\\.\\w]+\\\@[\\w\\.]+)"; ######################################################################### # The Code Starts Here ######################################################################### require "ctime.pl"; require "timelocal.pl"; use Net::SMTP; use Config; if ($Config{'osname'} =~ /^macos/i) { # do mac specific stuff here $wrkdir = "pub:now:"; $doAppend = 1; $datafile = "notify.data"; $logfile = "notify.log"; $srcfile = "Sequencer Reservations"; &MacPerl'Quit(1); #quit if a stand alone app # $inmacintosh = 0; @fileAttributes = ("R*ch", "TEXT"); $nativedirchar=":"; # throw the logs in the working directory $errlogfile ="notifyError.log"; } elsif ($Config{'osname'} =~ /^win/i) { $srcdir = "f:\\pub\\iislogs:"; $destfile = "f:\\pub\\wwwlogs\\ntwww.log"; $nativedirchar="\\"; $logfile = $ENV{'HOME'}.$nativedirchar."merge.log"; $errlogfile = $ENV{'HOME'}.$nativedirchar."mergeError.log"; } else{ $srcdir = "/iislogs:"; $destfile = "/wwwlogs/ntwww.log"; $nativedirchar="/"; $logfile = $ENV{'HOME'}.$nativedirchar."merge.log"; $errlogfile = $ENV{'HOME'}.$nativedirchar."mergeError.log"; } $debug=0; #turn on debugging output $test=0; #email to test address only &INITIALIZE(); &FindEvents(); &TERMINATE(); exit; ######################################################################### # INITIALIZE ######################################################################### sub INITIALIZE { $notificationSent=0; $err=opendir (WORKDIR, $wrkdir); if ($err < 1 ){ die "working directory '$wrkdir' invalid!"; } $err=closedir (WORKDIR); $err=chdir $wrkdir; if ($err < 1 ){ die "Cannot set directory to working directory '$wrkdir'!"; } ######################################################################### # Set up logs ######################################################################### open (LOG,">>".$logfile) || &Die("Unable to open $logfile"); close(STDERR); open(STDERR,">>".$errlogfile) || &Die("Unable to open $errlogfile"); select(STDERR); $| = 1; ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime; $mon++; print LOG "#########################################################################\n"; if($min >= 10) { print LOG "NOTIFIER started at $mon/$mday/$year $hour:$min\n"; } else { print LOG "NOTIFIER started at $mon/$mday/$year $hour:0$min\n"; } ######################################################################### # Read in files ######################################################################### $err=open(EVENTS, $srcfile); if ($err < 1 ){ die "Events file '$srcfile' not found!"; } $fileNum=fileno(EVENTS); if ($fileNum > 0 ){ if (eof(EVENTS)) { die "file '$srcfile' is empty!"; } } else { die "FileNum for supposedly open file ($srcfile) invalid!"; } @eventdata = ; #read in all the file data into an array close EVENTS; $err=open(DATA, $datafile); if ($err < 1 ){ @notifydata = ("RESERVATION DATE AND TIME\n","date\tstart\tfinish\te-mail\tsent\tsent time\n"); goto EXITDATA; } $fileNum=fileno(DATA); if ($fileNum > 0 ){ if (eof(DATA)) { @notifydata = ("RESERVATION DATE AND TIME\n","date\tstart\tfinish\te-mail\tsent\tsent time\n"); goto EXITDATA; } } else { die "FileNum for supposedly open file ($datafile) invalid!"; } @notifydata = ; #read in all the file data into an array EXITDATA: close DATA; } ######################################################################### # TERMINATE ######################################################################### sub TERMINATE { ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime; $mon++; $msg="Notifier ran successfully at $mon/$mday/$year "; $msg.=($min>=10)?"$hour:$min\n":"$hour:0$min\n"; $test?&sendemailmsg($testemail, "", "", $testemail, "Notifier log",$msg):&sendemailmsg($ccs, "", "", $replyto, "Notifier log",$msg); if($min >= 10) { print LOG "NOTIFIER finished at $mon/$mday/$year $hour:$min\n"; } else { print LOG "NOTIFIER finished at $mon/$mday/$year $hour:0$min\n"; } print LOG "#########################################################################\n"; close(LOG); if($notificationSent) { $err=open(DATA, ">$datafile"); if ($err < 1 ){ die "Data file '$datafile' not found!"; } $fileNum=fileno(DATA); if ($fileNum <1 ){ die "FileNum for supposedly open file ($datafile) invalid!"; } print DATA @notifydata; #write out all the file data into an array close DATA; } close(STDERR); if ($Config{'osname'} =~ /^macos/i) { # do mac specific stuff here chdir(); #should go back to the default home directory &MacPerl::SetFileInfo(@fileAttributes, "$logfile"); &MacPerl::SetFileInfo(@fileAttributes, "$errlogfile"); &MacPerl::SetFileInfo(@fileAttributes, "$datafile"); } } sub FindEvents { local($tdiff, $date,$starttime,$endtime,$title,$body,$category,$currentsecs,@fields); local($processevent,$checkemail,$emailfound, $recipient); foreach $line (@eventdata) { ($date,$starttime,$endtime,$title,$body,$category,@fields) = split ( /$separator/, $line); print LOG "\nDate= $date, start=$starttime, end=$endtime, title=$title, body=\"$body\", category=$category\n" if $debug; next if (length($date) <5 || $date =~ /error/i); # skip undated entries and errors $tdiff=&timedifferential("$date $starttime", "$mon/$mday/$year $hour:$min"); $currentsecs = $notifyhours*60*60; #Perl internal times always in seconds next if (&IsAllowedCategory($category)<1); $processevent = ($tdiff>0 && $tdiff-$currentsecs<0)?1:0; print LOG "processevent=$processevent\n" if $debug; $checkemail = ($tdiff>0)?1:0; print LOG "checkemail=$checkemail\n" if $debug; if ($processevent || $checkemail) { $body =~ m/$emailPat/i; $emailfound = (length($1)>7); $recipient=$1; } if ( $emailfound && $processevent) { next if &HasBeenSent($date,$starttime,$endtime,$recipient); local($msg)="According to our records, you requested a reservation on $date at ". "$starttime until $endtime.\n\n"; if ($test?&sendemailmsg($testemail, "", "", $testemail, $subj,$header.$msg.$footer): &sendemailmsg($recipient, $ccs, "", $replyto, $subj,$header.$msg.$footer)) { print LOG "\tNotification sent to $recipient re: reservation on $date from $starttime to $endtime\n"; AddNotifyData($date,$starttime,$endtime,$recipient,"$mon/$mday/$year", ($min >= 10)?"$hour:$min":"$hour:0$min"); } else { print LOG "\tUNABLE to send Notification to $recipient re: reservation on $date\n"; } } if ($checkemail && $emailfound<1) { local($msg)="Could not find valid email in entry on $date at $starttime. \nTitle (reservation name/id): \"". $title."\"\n\nBody of reservation:\n\"".$body. "\"\n\nPlease correct ASAP!\n\n"; $test?&sendemailmsg($testemail, "", "", $testemail, "Invalid SEQUENCER ENTRY!",$msg): &sendemailmsg($replyto, $ccs, "", $replyto, "Invalid SEQUENCER ENTRY!",$msg); print LOG "\tBiotech-lab sent email: no email recipient for reservation on $date\n"; } } } # -------------------------------------------------------------------------- sub IsAllowedCategory { local($cat) = @_; local($found) = 0; if (@allowedCategory == 0) { $found = 1; } else { foreach $focusoncat (@allowedCategory) { if ($cat =~ /$focusoncat/i) { $found = 1; last; } } } $found; } sub HasBeenSent { local($date,$starttime,$endtime,$recipient) = @_; local($notifyrec); local($sent) = 0; if (@notifydata > 0) { foreach $notifyrec (@notifydata) { if ( ($notifyrec =~ /$date/i) && ($notifyrec =~ /$starttime/i) && ($notifyrec =~ /$endtime/i) && ($notifyrec =~ /$recipient/i) ) { $sent = 1; last; } } } $sent; } sub AddNotifyData { local($date,$starttime,$endtime,$recipient,$datesent,$timesent) = @_; local($data) = "$date$separator$starttime$separator$endtime$separator$recipient$separator$datesent$separator$timesent\n"; push (@notifydata, $data); $notificationSent=1; } sub sendemailmsg { local($recipient)=$_[0]; local($cc)=$_[1]; local($bcc)=$_[2]; local($sender)=$_[3]; local($subject)=$_[4]; local($msg)=$_[5]; local($sent)=0; local(@toaddresses)=split(/,[ ]*/,$recipient); local(@ccaddresses)=split(/,[ ]*/,$cc); local(@bccaddresses)=split(/,[ ]*/,$bcc); local($smtp) = Net::SMTP->new('merle.acns.nwu.edu'); # if (smtp->verify($toaddresses[0])) { # print LOG "\tUnable to verify email address $toaddresses[0]\n"; # goto EXITSENDMAIL; # } $smtp->mail($sender); print LOG @toaddresses,@ccaddresses,@bccaddresses, "\n" if $debug; $smtp->to(@toaddresses,@ccaddresses,@bccaddresses); $smtp->data(); $smtp->datasend("To: $recipient\n"); $smtp->datasend("From: $sender\n"); $smtp->datasend("Subject: $subject\n"); if (length($cc)>7) { $smtp->datasend("Cc: $cc\n"); } $smtp->datasend("\n"); $smtp->datasend($msg); $sent=$smtp->dataend(); $smtp->quit; EXITSENDMAIL: $sent; } sub timedifferential { local($time1)=$_[0]; local($time2)=$_[1]; #reposition to the start of the file ?? return -1 if ( length($time1)<5 || length($time2)<5 ) ; local($mon,$mday,$year,$hours,$min)=split(/[:\/\ ]/,$time1); $year=$year-2000 if ($year >2000); $year=$year-1900 if ($year >1900); $mon--; print LOG "timedifferential1: $mon/$mday/$year $hours:$min\n" if $debug; local($ltime1) = &timelocal(0,$min,$hours,$mday,$mon,$year); ($mon,$mday,$year,$hours,$min)=split(/[:\/\ ]/,$time2); $year=$year-2000 if ($year >2000); $year=$year-1900 if ($year >1900); $mon--; print LOG "timedifferential12: $mon/$mday/$year $hours:$min\n" if $debug; local($ltime2) = &timelocal(0,$min,$hours,$mday,$mon,$year); local($timediff)=$ltime1; $timediff-=$ltime2; print LOG "timediff=$timediff, ltime1=$ltime1, ltime2=$ltime2 \n" if $debug; $timediff; } sub gettimezoneadj { local($now)=time; local($nowdiff)=time-localtime; print "gettimezoneadj: time-localtime = ".$nowdiff/(60*60*24)." hours \n" if $debug; local(@nowgm)=gmtime($now); local(@nowlocal)=localtime($now); # (secs mins hours days months-1 years) local($timeadj)=(($nowgm[3]-$nowlocal[3])*24)+($nowgm[2]-$nowlocal[2]); $timeadj }