Index: /emailtotracscript/trunk/email2trac.py.in
===================================================================
--- /emailtotracscript/trunk/email2trac.py.in	(revision 44)
+++ /emailtotracscript/trunk/email2trac.py.in	(revision 45)
@@ -238,4 +238,7 @@
 
 	def set_owner(self, ticket):
+		"""
+		Select default owner for ticket component
+		"""
 		cursor = self.db.cursor()
 		sql = "SELECT owner FROM component WHERE name='%s'" % ticket['component']
@@ -245,18 +248,30 @@
 
 	def set_reply_fields(self, ticket, message):
-
+		"""
+		Bla Bla
+		"""
 		author, email_addr  = email.Utils.parseaddr(message['from'])
 		email_str = self.to_unicode(message['from'])
 
-		ticket['reporter'] = email_str
-		ticket['cc'] = None
-
-		# Put all addresses in ticket CC field
+		# Look for email address in registered trac users
+		#
+		if self.VERSION > 0.8:
+			users = [ u for (u, n, e) in self.env.get_known_users(self.db)
+				if e == email_addr ]
+		else:
+			users = []
+
+		if len(users) == 1:
+			ticket['reporter'] = users[0]
+		else:
+			ticket['reporter'] = email_str
+
+		# Put all CC-addresses in ticket CC field
 		#
 		if self.REPLY_ALL:
-			tos = message.get_all('to', [])
+			#tos = message.get_all('to', [])
 			ccs = message.get_all('cc', [])
 
-			addrs = email.Utils.getaddresses(tos + ccs)
+			addrs = email.Utils.getaddresses(ccs)
 
 			# Remove reporter email address if notification is 
@@ -269,15 +284,9 @@
 					pass
 
-			for a,m in addrs:
-					if not ticket['cc']:
-						ticket['cc'] = m
-					else:
-						ticket['cc'] = '%s,%s' %(ticket['cc'], m)
-
-		# Put authors email addess in CC field only if notification is off
-		#
-		elif self.CC and not self.notification:
-			ticket['cc'] = email_str
-
+			for name,mail in addrs:
+					try:
+						ticket['cc'] = '%s,%s' %(ticket['cc'], mail)
+					except:
+						ticket['cc'] = mail
 		return author, email_addr
 
@@ -339,5 +348,6 @@
 		author, email_addr = self.set_reply_fields(tkt, msg)
 
-# produce e-mail like header
+		# produce e-mail like header
+		#
 		head = ''
 		if self.EMAIL_HEADER > 0:
@@ -347,26 +357,60 @@
 			self.debug_attachments(msg)
 
-#
-#	put the message text in the ticket description
-#	message text can be plain text or html or something else
-#
+		self.description(msg,tkt, head, author, email_addr)
+
+		# Insert ticket in database
+		#
+		if self.VERSION > 0.8:
+			tkt['id'] = tkt.insert()
+		else:
+			tkt['id'] = tkt.insert(self.db)
+
+		#
+		# Just how to show to update description
+		#
+		#tkt['description'] = '\n{{{\n\n Bas is op nieuw bezig\n\n }}}\n' 
+		#tkt.save_changes(self.db, author, "Lekker bezig")
+		#
+
+		self.attachments(msg, tkt, author)
+		if self.notification:
+			self.notify(tkt)
+
+
+	def description(self, msg, tkt, head, author, email):
+		"""
+		put the message text in the ticket description
+		message text can be plain text or html or something else
+		"""
 		has_description = 0
 		for part in msg.walk():
-			if part.get_content_maintype() == 'multipart':			# 'multipart/*' is a container for multipart messages
+
+			# 'multipart/*' is a container for multipart messages
+			#
+			if part.get_content_maintype() == 'multipart':
 				continue
 
 			if part.get_content_type() == 'text/plain':
-				body_text = part.get_payload(decode=1)			# try to decode
-				if not body_text:					# decode failed
-					body_text = part.get_payload(decode=0)		# do not decode
-
-				tkt['description'] = '\n{{{\n\n%s\n}}}\n' % body_text
+				# Try to decode, if fails then do not decode
+				#
+				body_text = part.get_payload(decode=1)		
+				if not body_text:			
+					body_text = part.get_payload(decode=0)	
+
+				# Get contents charset (iso-8859-15 if not defined in mail headers)
+				# UTF-8 encode body_text
+				#
+				charset = msg.get_content_charset('iso-8859-15')
+				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
+				tkt['description'] = '%s\n\n(see attachment for HTML mail message)\n' \
+					%(head)
 				body_text = tkt['description']
 
 			else:
-				tkt['description'] = '%s\n\n(see attachment for message)\n' % head
+				tkt['description'] = '%s\n\n(see attachment for message)\n' %(head)
 				body_text = tkt['description']
 
@@ -379,22 +423,7 @@
 
 		if self.MAILTO:
-			mailto = self.html_mailto_link(author, email_addr, self.to_unicode(msg['subject']), body_text)
+			mailto = self.html_mailto_link(author, email, self.to_unicode(msg['subject']), ubody_text)
 			tkt['description'] = '%s\n%s %s' %(head, mailto, tkt['description'])
 
-		if self.VERSION > 0.8:
-			tkt['id'] = tkt.insert()
-		else:
-			tkt['id'] = tkt.insert(self.db)
-
-		#
-		# Just how to show to update description
-		#
-		#tkt['description'] = '\n{{{\n\n Bas is op nieuw bezig\n\n }}}\n' 
-		#tkt.save_changes(self.db, author, "Lekker bezig")
-		#
-
-		self.attachments(msg, tkt, author)
-		if self.notification:
-			self.notify(tkt)
 
 	def notify(self, tkt):
@@ -429,18 +458,10 @@
 		#arr = string.split(body, '\n')
 		#arr = map(self.mail_line, arr)
-
 		#body = string.join(arr, '\n')
 		#body = '%s wrote:\n%s' %(author, body)
 
-		# Obsolete for reference
-		#
-		#body = self.to_unicode(body)
-		#body = urllib.quote(body)
-		#body = Header.encode(body)
-		#
-
 		# Temporary fix
 		body = '> Type your reply'
-		str = 'mailto:%s?subject=%s&body=%s' % (urllib.quote(mail_addr), urllib.quote('Re: %s' % subject), urllib.quote(body))
+		str = 'mailto:%s?subject=%s&body=%s' %(urllib.quote(mail_addr), urllib.quote('Re: %s' % subject), urllib.quote(body))
 		str = '\n{{{\n#!html\n<a href="%s">Reply to: %s</a>\n}}}\n' %(str, author)
 
