#!/usr/bin/perl ########################################################### # BiffSocko # email-parse.pl # # This program parses an mbox formatted email file and # distributes the email to various users on the system. # # NLS uses this program as part of it's email redundancy # set up. Most NLS users get their email put into a "bucket" # at the remote site. We download the bucket, and run this # program against the bucket to distribute the email to its # rightful owner # # RETURN CODES # ============================================== # 0 Success # 1 can't open $INFILE to read # 2 can't open $TMPFILE # 3 can't open user file # 4 cant reopen TMPFILE ########################################################### use File::Copy; $TMPFILE="mail.tmp"; $INFILE="nls"; #$INFILE="mbox"; ############################ # open the mbox file ############################ if(!(open(IN, $INFILE))){ print "cant open $INFILE .. exiting\n"; exit(1); } $FLAG=0; $counter=0; ############################ # start parsing the mbox ############################ while(){ $line=$_; ##################################### # found the top of next email ##################################### if($_ =~ m/(^From[^:])(.*)(\@)(.*)/i){ $EmailFrom=$4; $counter++; ################################### # if we already parsing an email # in the mbox file ################################### if($FLAG eq 2){ close(TMP); move($TMPFILE, $TMPFILE.$counter); if(!(open(usr,">>$user"))){ print "cant open user file\n"; exit(3); } if(!(open(TMPNEW, $TMPFILE.$counter))){ print "cant open $TMPFILE.$counter\n"; close(usr); exit(4); } while(){ print usr "$_"; } close(TMPNEW); close(usr); unlink($TMPFILE.$counter); } $FLAG=1; &opentmp(); } ##################################### # get the RCPT. ##################################### if($_ =~ m/(^To:)(.*)(<)(.*)(>)/ig){ $EmailTo=$4; print "$EmailTo\n"; @tmp=split(/\@/,$EmailTo); $user=$tmp[0]; print "$user\n"; $FLAG=2; } ##################################### # print out to the email file ##################################### if(($FLAG eq 1)||($FLAG eq 2)){ print TMP "$line"; } } print "$counter\n"; exit(0); &email_head(); ################################################################# # sub oopentmp() # opens a temp file ################################################################# sub opentmp(){ if(!(open(TMP, ">$TMPFILE"))){ print "cant open $TMPFILE\n"; exit(2); } } ################################################################# # sub email_head() # prints out the top of an email spool file ################################################################# sub email_head(){ $OUTFILE="nls_out"; $HEADER="From MAILER-DAEMON Mon Jan 22 14:44:36 2007 Date: 22 Jan 2007 14:44:36 -0500 From: Mail System Internal Data Subject: DON'T DELETE THIS MESSAGE -- FOLDER INTERNAL DATA Message-ID: <1169495076@nassaulibrary.org> X-IMAP: 1111078350 0000013607 Status: RO This text is part of the internal format of your mail folder, and is not a real message. It is created automatically by the mail system software. If deleted, important folder data will be lost, and it will be re-created with the data reset to initial values. "; if(!(open(OUT,">$OUTFILE"))){ print "can not open $OUTFILE to write\n"; exit(1); } print OUT "$HEADER"; close(OUT); }