#! /usr/bin/python
"""
#+
# NAME:
#	sync_ips_email
# PURPOSE:
#	To determine if there is new IPS data by checking for new email
# OUTPUT:
#	Returns a 1 if an IPS email was found, 0 if not
# RESTRICTIONS:
#	To be able to replace /var/spool/mail/username, the account running
#	this script needs to have write acces to the file. The mailbox is already
#	owned by 'username' but is in group 'mail'. The easiest way to provide
#	write access seems to be to make 'username' a member of group 'mail', in
#	addition to the group 'users'.
# CALLS:
#	time2time, tiny.run_cmd
# PROCEDURE:
#	Incoming email is put in /var/spool/mail/username
#	Pine moves email from /var/spool/mail/username to $HOME/mbox
#	We check both mailboxes for IPS email from Nagoya.
#
#	Both mailboxes are scanned for email with "stelab.nagoya-u.ac.jp" in the
#	message ID and "VLIST_UCSD_" in the body. All such emails are deleted,
#	and a status of 1 is returned.
#
#	Because PortableUnixMailbox library does not seem to have a function for
#	deleting we have to explicitly rewrite the mailbox to remove the IPS emails
#	once they have been detected.
#
#	If no IPS email is found in a mailbox then the mailbox remains unmodified.
#	If one or more IPS emails are found, then all non-IPS emails are accumulated
#	in a temporary mailbox file. The temporary file (which does not contain
#	the IPS emails) replaces the old mailbox file after completion.
#
#	The only kludge used is the addition of a phony From_ line to the start of
#	each email written to the temporary mail box file. There does not seem to be
#	a way to extract this line from the mailbox file itself, so we had to fake
#	one (if it is not there, pine will not recognize it as a valid mailbox).
# MODIFICATION HISTORY:
#	SEP-2003, Austin Duncan
#	OCT-2003, Paul Hick (UCSD/CASS)
#		Added check to make sure the mailbox file exists.
#		Added processing of $HOME/mbox in addition to /var/spool/mail/username
#	NOV-2003, Paul Hick (UCSD/CASS; pphick@ucsd.edu)
#		Added the email time converted from JST to UT to output log.
#-
"""
import mailbox, os, sys
from tiny import run_cmd

import tempfile
tempfile.tempdir = os.environ['TUB']

def sync_ips_email( matchFlag, mailFile, logFile ):

	if not os.path.exists(mailFile):
		print "sync_ips_email, "+mailFile+" does not exist"
		return matchFlag

	# Defined variables and initializations

	bodySearch		= "VLIST_UCSD_"
	messageSearch	= "stelab.nagoya-u.ac.jp"

	# Open mailbox file. Check for presence of IPS email.

	mailHandle = open(mailFile,"rb")
	mbox = mailbox.PortableUnixMailbox(mailHandle)

	email_found = 0

	while not email_found:

		msg = mbox.next()		# Grab next message

		if msg is None:			# No more messages: break out of the while loop
			break

		messageID = msg.getheader("Message-Id"	, "").strip()
		text = msg.fp.read()

		# Testing for: (match in body) && (match in Message-Id)

		email_found = (text.find(bodySearch) != -1) and (messageID.find(messageSearch) != -1)

	mailHandle.close()       	# Close the mailbox file

	if not email_found:
		return matchFlag

	# The mailbox does contain an IPS email. Process the mailbox again.
	# Open temporary mailbox file.

	tempFile = tempfile.mktemp('.ips_email')
	tempHandle = open(tempFile,"a")

	# Open mailbox file again.

	mailHandle = open(mailFile,"rb")
	mbox = mailbox.PortableUnixMailbox(mailHandle)

	while 1:

		msg = mbox.next()		# Grab next message

		if msg is None:			# No more messages: break out of the while loop
			break

		# Read the fields into variables

		subject   = msg.getheader("Subject"		, "").strip()
		messageID = msg.getheader("Message-Id"	, "").strip()
		sender    = msg.getheader("From"		, "").strip()
		dateSent  = msg.getheader("Date"		, "").strip()

		text = msg.fp.read()

		# Testing for:  (newMail) && (match in body) && (match in Message-Id)

		if (text.find(bodySearch) != -1) and (messageID.find(messageSearch) != -1):

			# It is an IPS email
			# Update the logfile. Do NOT write the email to the temporary mailbox file

			matchFlag = matchFlag+1			# Count IPS emails

			# Strip the following off of the body:  "VLIST_UCSD_" and "\n"
			# The log format will be:  fileNumber \t jst_date  ut_date\n

			newLine = text.replace(" is created.", "").replace("\n", "") + "\t" + dateSent+	\
				"  "+run_cmd( 'echo "'+dateSent+'" | time2time -email', 0 )

			logHandle = open(logFile,"a")	# Append to log file
			logHandle.write(newLine)
			logHandle.close()

		else:

			# Not an IPS email. Save it in the temporary mailbox

			# Insert hony From_ (unix envelope) so pine is happy

			tempHandle.write("From cass@ucsd.edu Tue Sep 23 15:57:01 2003\n" )

			tempHandle.write( str(msg) )	# Convert msg instance to string
			tempHandle.write("\n")    
			tempHandle.write(text)

	# Close the mailboxes and overwrite the mailbox file.

	tempHandle.close()						# Closes the temporary mailbox file
	mailHandle.close()       				# Close the mailbox file

	#os.rename does not necessarily work, since it requires write access to the
	#directory containg mailFile. The read-write combination only requires write
	#access to mailFile itself.
	#os.rename(tempFile,mailFile)

	(open(mailFile,'w')).write( (open(tempFile,'r')).read() )
	os.remove(tempFile)						# Replace mailbox file with temporary file

	return matchFlag						# Return number of IPS emails found


if __name__ == '__main__':

	logFile = os.path.join( os.environ["DAT"],"nagoya","daily","logmail.ips" )

	mailFiles = [os.path.join( "/var","spool","mail",os.environ["USER"] ),	\
				 os.path.join( os.environ["HOME"], "mbox" ) ]

	matchFlag = 0
	for mailFile in mailFiles:
		matchFlag = sync_ips_email( matchFlag, mailFile, logFile )
		print "Processed", mailFile, matchFlag

	sys.exit (matchFlag > 0)
