Index: /emailtotracscript/trunk/email2trac.py.in
===================================================================
--- /emailtotracscript/trunk/email2trac.py.in	(revision 71)
+++ /emailtotracscript/trunk/email2trac.py.in	(revision 72)
@@ -99,4 +99,10 @@
 		self.db = None
 
+		# Some useful mail constants
+		#
+		self.author = None
+		self.email_addr = None
+		self.email_field = None
+
 		self.VERSION = version
 		if self.VERSION == 0.8:
@@ -216,15 +222,7 @@
 
 	def email_header_txt(self, m):
-#		if not m['Subject']:
-#			subject = '(geen subject)'
-#		else:
-#			subject = self.to_unicode(m['Subject'])
-#
-#		head = "'''Subject:''' %s [[BR]]" % subject
-#		if m['From'] and len(m['From']) > 0:
-#			head = "%s'''From:''' %s [[BR]]" % (head, m['From'])
-#		if m['Date'] and len(m['Date']) > 0:
-#			head = "%s'''Date:''' %s [[BR]]" %(head, m['Date'])
-
+		"""
+		Display To and CC addresses in description field
+		"""
 		str = ''
 		if m['To'] and len(m['To']) > 0 and m['To'] != 'hic@sara.nl':
@@ -247,10 +245,9 @@
 			ticket['owner'] = "UNKNOWN"
 
-	def set_reply_fields(self, ticket, message):
-		"""
-		Bla Bla
-		"""
-		author, email_addr  = email.Utils.parseaddr(message['from'])
-		email_str = self.to_unicode(message['from'])
+	def get_author_emailaddrs(self, message):
+		"""
+		Get the default author name and email address from the message
+		"""
+		self.author, self.email_addr  = email.Utils.parseaddr(message['from'])
 
 		# Look for email address in registered trac users
@@ -260,10 +257,16 @@
 		else:
 			users = [ u for (u, n, e) in self.env.get_known_users(self.db)
-				if e == email_addr ]
+				if e == self.email_addr ]
 
 		if len(users) == 1:
-			ticket['reporter'] = users[0]
-		else:
-			ticket['reporter'] = email_str
+			self.email_field = users[0]
+		else:
+			self.email_field =  self.to_unicode(message['from'])
+
+	def set_reply_fields(self, ticket, message):
+		"""
+		Set all the right fields for a new ticket
+		"""
+		ticket['reporter'] = self.email_field
 
 		# Put all CC-addresses in ticket CC field
@@ -280,5 +283,5 @@
 			if self.notification:
 				try:
-					addrs.remove((author, email_addr))
+					addrs.remove((self.author, self.email_addr))
 				except ValueError, detail:
 					pass
@@ -289,5 +292,4 @@
 					except:
 						ticket['cc'] = mail
-		return author, email_addr
 
 	def save_email_for_debug(self, message):
@@ -319,11 +321,18 @@
 			return False
 
-		# Strip # and : frim ticket_id
+		# Strip '#' and ':' from ticket_id
 		ticket_id = result.group('ticketnr')
 		ticket_id = int(ticket_id[1:-1])
-		print ticket_id
 
 		tkt = Ticket(self.db, ticket_id)
-		print tkt
+
+		body_text = self.get_body_text(m)
+		body_text = '{{{\n%s\n}}}' %body_text
+		tkt.save_changes(self.db, self.author, body_text)
+
+		self.attachments(m, tkt)
+		if self.notification:
+			self.notify(tkt, False)
+
 		return True
 
@@ -336,21 +345,21 @@
 		if self.DEBUG > 1:	  # save the entire e-mail message text
 			self.save_email_for_debug(msg)
+			self.debug_attachments(msg)
 
 		self.db = self.env.get_db_cnx()
+ 		self.get_author_emailaddrs(msg) 
+
+		if self.get_config('notification', 'smtp_enabled') in ['true']:
+			self.notification = 1
+		else:
+			self.notification = 0
+
+		# Is this a previous ticket
+		#
 		if self.ticket_update(msg):
-			print 'Ticket has been updated now new one'
-			sys.exit(0)
-		else:
-			print 'New ticket'
-
-		sys.exit(0)
+			return True
 		
 		tkt = Ticket(self.env)
 		tkt['status'] = 'new'
-
-		if self.get_config('notification', 'smtp_enabled') in ['true']:
-			self.notification = 1
-		else:
-			self.notification = 0
 
 		# Some defaults
@@ -382,5 +391,5 @@
 		#
 		self.set_owner(tkt)
-		author, email_addr = self.set_reply_fields(tkt, msg)
+		self.set_reply_fields(tkt, msg)
 
 		# produce e-mail like header
@@ -390,8 +399,7 @@
 			head = self.email_header_txt(msg)
 
-		if self.DEBUG > 0:
-			self.debug_attachments(msg)
-
-		self.description(msg, tkt, head, author, email_addr)
+
+		body_text = self.get_body_text(msg)
+		tkt['description'] = '\n{{{\n%s\n}}}\n' %(body_text)
 
 		# Insert ticket in database
@@ -402,18 +410,19 @@
 			tkt['id'] = tkt.insert()
 
-		#
-		# Just how to show to update description
-		#
-		#tkt['summary'] = '#%s: %s' %(tkt['id'], tkt['summary'])
-		#tkt.save_changes(self.db, author, "")
-
-		self.attachments(msg, tkt, author)
+		# Else we do no have a ticket id for the subject line
+		#
+		mailto = ''
+		if self.MAILTO:
+			mailto = self.html_mailto_link(self.to_unicode(msg['subject']), tkt['id'], body_text)
+			tkt['description'] = '%s%s\n{{{\n%s\n}}}\n' %(head, mailto, body_text)
+			tkt.save_changes(self.db, self.author, "")
+
+		self.attachments(msg, tkt)
 		if self.notification:
 			self.notify(tkt)
 
-
-	def description(self, msg, tkt, head, author, email):
-		"""
-		put the message text in the ticket description
+	def get_body_text(self, msg):
+		"""
+		put the message text in the ticket description or in the changes field 
 		message text can be plain text or html or something else
 		"""
@@ -440,14 +449,9 @@
 				ubody_text = unicode(body_text, charset).encode('utf-8')
 
-				tkt['description'] = '\n{{{\n\n%s\n}}}\n' %(ubody_text)
-
 			elif part.get_content_type() == 'text/html':
-				tkt['description'] = '%s\n\n(see attachment for HTML mail message)\n' \
-					%(head)
-				body_text = tkt['description']
+				ubody_text = '\n\n(see attachment for HTML mail message)\n'
 
 			else:
-				tkt['description'] = '%s\n\n(see attachment for message)\n' %(head)
-				body_text = tkt['description']
+				ubody_text = '\n\n(see attachment for message)\n'
 
 			has_description = 1
@@ -455,13 +459,9 @@
 
 		if not has_description:
-			tkt['description'] = '%s\n\n(no plain text message, see attachments)' % head
-			has_description = 1
-
-		if self.MAILTO:
-			mailto = self.html_mailto_link(author, email, self.to_unicode(msg['subject']), ubody_text)
-			tkt['description'] = '%s\n%s %s' %(head, mailto, tkt['description'])
-
-
-	def notify(self, tkt):
+			ubody_text = '\n\n(see attachment for message)\n'
+
+		return ubody_text
+
+	def notify(self, tkt , new=True):
 		try:
 			# create false {abs_}href properties, to trick Notify()
@@ -474,5 +474,5 @@
 				tn.template_name = self.notify_template;
 
-			tn.notify(tkt, newticket=True) 
+			tn.notify(tkt, new) 
 
 		except Exception, e:
@@ -484,9 +484,9 @@
 
 
-	def html_mailto_link(self, author, mail_addr, subject, body):
-		if not author:
-			author = mail_addr
+	def html_mailto_link(self, subject, id, body):
+		if not self.author:
+			author = self.mail_addr
 		else:	
-			author = self.to_unicode(author)
+			author = self.to_unicode(self.author)
 
 		# Must find a fix
@@ -498,10 +498,10 @@
 
 		# Temporary fix
-		str = 'mailto:%s?subject=%s' %(urllib.quote(mail_addr), urllib.quote('Re: %s' % subject))
+		str = 'mailto:%s?subject=%s' %(urllib.quote(self.email_addr), urllib.quote('Re: #%s: %s' %(id, subject)))
 		str = '\n{{{\n#!html\n<a href="%s">Reply to: %s</a>\n}}}\n' %(str, author)
 
 		return str
 
-	def attachments(self, message, ticket, user):
+	def attachments(self, message, ticket):
 		'''save any attachments as file in the ticket's directory'''
 
@@ -522,5 +522,5 @@
 				filename = 'part%04d' % count
 
-				ext = mimetypes.guess_extension(part.get_type())
+				ext = mimetypes.guess_extension(part.get_content_type())
 				if not ext:
 					ext = '.bin'
@@ -568,5 +568,5 @@
 				cursor.execute('INSERT INTO attachment VALUES("%s","%s","%s",%d,%d,"%s","%s","%s")' 
 					%('ticket', urllib.quote(str(ticket['id'])), filename + '?format=raw', filesize, 
-					  int(time.time()),'', user, 'e-mail') ) 
+					  int(time.time()),'', self.author, 'e-mail') ) 
 				self.db.commit()
 			else:
Index: /emailtotracscript/trunk/msg.txt
===================================================================
--- /emailtotracscript/trunk/msg.txt	(revision 71)
+++ /emailtotracscript/trunk/msg.txt	(revision 72)
@@ -15,5 +15,5 @@
 MIME-Version: 1.0
 To: test@subtrac.sara.nl
-Subject: Test123 #153:
+Subject: Test123
 Content-Type: multipart/mixed; boundary="------------070800080005060203060809"
 X-OriginalArrivalTime: 08 Mar 2006 14:29:17.0140 (UTC)
