#!/usr/bin/python
import email, sys, os, re, glob
from email import Message
from email import Header

pagesdir = "/var/www/wiki/data/pages/"
prefix = "Email(2f)"

def handle_message(mail):
	subject = ""
	text = ""
	attachments = []
	if mail.has_key("Subject"):
		[(s,e)] = Header.decode_header(mail["Subject"])
		if e:
			subject = unicode(s, e).encode("utf8")
		else:
			subject = s
	if mail.has_key("Date"):
		[(d,e)] = Header.decode_header(mail["Date"])
		if e:
			text = text + "## Date: %s\n" % unicode(d, e).encode("utf8")
		else:
			text = text + "## Date: %s\n" % d
	for part in mail.walk():
		# skip multipart parts...
		if part.is_multipart():
			continue
		# get content and filename if present...
		ct = part.get_content_type()
		fn = part.get_filename()
		# handle plaintext content
		if ct == "text/plain":
			# get and decode
			t = part.get_payload(decode=True)
			if part.get_charset():
				t = unicode(t, part.get_charset()).encode("utf8")
			else:
				t = unicode(t, "latin1").encode("utf8")
			# remove quotation signs
			t = re.compile("^( *> *)+", re.M).sub("", t)
			# strip proper signatures...
			t = re.compile("\r?\n-- \r?\n.*$", re.S).sub("", t)
			# repeat linewraps
			t = re.compile("\r?\n").sub("\n\n", t)
			text = text + t
		# ignore HTML content
		elif ct == "text/html":
			pass
		# handle proper attachments
		elif fn:
			print "Attachment of type %s" % (part.get_content_type())
			print "Filename is %s" % fn
			attachments.append( (fn, part.get_payload(decode=True)) )
		else:
			print "Unknown mimetype '%s', no filename" % (mail.get_content_type())
	return subject, text, attachments

# get last ID
nextid = 1
fn = os.path.join(pagesdir, prefix)
r = re.compile( re.escape(fn) + "([0-9]+)(?![0-9])" )
for f in glob.glob( fn + "*" ):
	nums = r.search( f )
	num = int(nums.group(1))
	if nextid <= num:
		nextid = num + 1

# Load email
mail = email.message_from_file(sys.stdin)

# this is quoteWikinameFS from moinmoin
# with some modifications...
STRIP  = re.compile(r'[\?\\\/\.]+')
UNSAFE = re.compile(r'[^a-zA-Z0-9_]+')
def quoteWikinameFS(wikiname, charset='utf-8'):
	""" Return file system representation of a Unicode WikiName.

	Warning: will raise UnicodeError if wikiname can not be encoded using
	charset. The default value of config.charset, 'utf-8' can encode any
	character.

	@param wikiname: Unicode string possibly containing non-ascii characters
	@param charset: charset to encode string
	@rtype: string
	@return: quoted name, safe for any file system
	"""
	filename = wikiname.replace(' ', '_') # " " -> "_"
	filename = STRIP.sub('', filename)

	quoted = []
	location = 0
	for needle in UNSAFE.finditer(filename):
		# append leading safe stuff
		quoted.append(filename[location:needle.start()])
		location = needle.end()
		# Quote and append unsafe stuff
		quoted.append('(')
		for character in needle.group():
			quoted.append('%02x' % ord(character))
		quoted.append(')')

	# append rest of string
	quoted.append(filename[location:])
	return ''.join(quoted)

if mail.__class__ is Message.Message:
	(s, t, a) = handle_message(mail)
	#fs = re.compile("[^A-Za-z0-9]").sub("", s)
	fs = prefix + quoteWikinameFS("%06d_%s" % (nextid,s))

	# make the output directory
	os.mkdir(os.path.join(pagesdir, fs ))
	os.mkdir(os.path.join(pagesdir, fs, "revisions" ))
	if a:
		os.mkdir(os.path.join(pagesdir, fs, "attachments" ))
	
	t = ("= %s =\n" % s) + t
	for pair in a:
		f = file(os.path.join(pagesdir, fs, "attachments", pair[0]), "w")
		f.write(pair[1])
		f.close()
		print pair[0]
		#t = t + "\n\n" + "attachment:%s" % pair[0]
	if a:
		t = t + "\n\n" + "[[AttachList]]"
	
	# write revision
	file(os.path.join(pagesdir, fs, "current"), "w").write("00000001")
	file(os.path.join(pagesdir, fs, "revisions", "00000001"), "w").write(t)
else:
	raise "Unknown class returned by parser: %s" % (mail.__class__)
