Index: /trunk/email2trac.py.in
===================================================================
--- /trunk/email2trac.py.in	(revision 340)
+++ /trunk/email2trac.py.in	(revision 341)
@@ -280,5 +280,5 @@
 		self.trac_smtp_from = self.get_config('notification', 'smtp_from')
 
-########## Email Header Field Functions ###########################################################
+########## Email Header Functions ###########################################################
 
 	def spam(self, message):
@@ -350,8 +350,217 @@
 		return False
 
+	def email_header_txt(self, m):
+		"""
+		Display To and CC addresses in description field
+		"""
+		s = ''
+
+		if m['To'] and len(m['To']) > 0:
+			s = "'''To:''' %s\r\n" %(m['To'])
+		if m['Cc'] and len(m['Cc']) > 0:
+			s = "%s'''Cc:''' %s\r\n" % (s, m['Cc'])
+
+		return  self.email_to_unicode(s)
+
+
+	def get_sender_info(self, message):
+		"""
+		Get the default author name and email address from the message
+		""" 
+
+		self.email_to = self.email_to_unicode(message['to']) 
+		self.to_name, self.to_email_addr = email.Utils.parseaddr (self.email_to) 
+
+		self.email_from = self.email_to_unicode(message['from'])
+		self.email_name, self.email_addr  = email.Utils.parseaddr(self.email_from)
+
+		## Trac can not handle author's name that contains spaces
+		#  and forbid the ticket email address as author field
+
+		if self.email_addr == self.trac_smtp_from:
+			self.author = "email2trac"
+		else:
+			self.author = self.email_addr
+
+		if self.IGNORE_TRAC_USER_SETTINGS:
+			return
+
+		# Is this a registered user, use email address as search key:
+		# result:
+		#   u : login name
+		#   n : Name that the user has set in the settings tab
+		#   e : email address that the user has set in the settings tab 
+		#
+		users = [ (u,n,e) for (u, n, e) in self.env.get_known_users(self.db)
+			if e and (e.lower() == self.email_addr.lower()) ]
+
+		if len(users) == 1:
+			self.email_from = users[0][0]
+			self.author = users[0][0]
+
+	def set_reply_fields(self, ticket, message):
+		"""
+		Set all the right fields for a new ticket
+		"""
+		if self.DEBUG: 
+			print 'TD: set_reply_fields'
+
+		## Only use name or email adress
+		#ticket['reporter'] = self.email_from
+		ticket['reporter'] = self.author
+
+
+		# Put all CC-addresses in ticket CC field
+		#
+		if self.REPLY_ALL:
+
+			email_cc = ''
+
+			cc_addrs = email.Utils.getaddresses( message.get_all('cc', []) )
+
+			if not cc_addrs:
+				return
+
+			## Build a list of forbidden CC addresses
+			#
+			#to_addrs = email.Utils.getaddresses( message.get_all('to', []) )
+			#to_list = list()
+			#for n,e in to_addrs:
+			#	to_list.append(e)
+				
+			# Remove reporter email address if notification is 
+			# on
+			#
+			if self.notification:
+				try:
+					cc_addrs.remove((self.author, self.email_addr))
+				except ValueError, detail:
+					pass
+
+			for name,addr in cc_addrs:
+		
+				## Prevent mail loop
+				#
+				#if addr in to_list:
+
+				if addr == self.trac_smtp_from:
+					if self.DEBUG:
+						print "Skipping %s mail address for CC-field" %(addr)
+					continue
+
+				if email_cc:
+					email_cc = '%s, %s' %(email_cc, addr)
+				else:
+					email_cc = addr
+
+			if email_cc:
+				if self.DEBUG:
+					print 'TD: set_reply_fields: %s' %email_cc
+
+				ticket['cc'] = self.email_to_unicode(email_cc)
+
+
+########## DEBUG functions  ###########################################################
+
+	def debug_body(self, message_body, tempfile=False):
+		if tempfile:
+			import tempfile
+			body_file = tempfile.mktemp('.email2trac')
+		else:
+			body_file = os.path.join(self.TMPDIR, 'body.txt')
+
+		if self.DRY_RUN:
+			print 'DRY-RUN: not saving body to %s' %(body_file)
+			return
+
+		print 'TD: writing body to %s' %(body_file)
+		fx = open(body_file, 'wb') 
+		if not message_body:
+				message_body = '(None)'
+
+		message_body = message_body.encode('utf-8')
+		#message_body = unicode(message_body, 'iso-8859-15')
+
+		fx.write(message_body)
+		fx.close()
+		try:
+			os.chmod(body_file,S_IRWXU|S_IRWXG|S_IRWXO)
+		except OSError:
+			pass
+
+	def debug_attachments(self, message_parts):
+		"""
+		"""
+		if self.VERBOSE:
+			print "VB: debug_attachments"
+		
+		n = 0
+		for item in message_parts:
+			# Skip inline text parts
+			if not isinstance(item, tuple):
+				continue
+				
+			(original, filename, part) = item
+
+			n = n + 1
+			print 'TD: part%d: Content-Type: %s' % (n, part.get_content_type())
+			print 'TD: part%d: filename: %s' % (n, part.get_filename())
+
+			part_file = os.path.join(self.TMPDIR, part.get_filename())
+			print 'TD: writing part%d (%s)' % (n,part_file)
+
+			if self.DRY_RUN:
+				print 'DRY_RUN: NOT saving attachments'
+				continue
+
+			fx = open(part_file, 'wb')
+			text = part.get_payload(decode=1)
+
+			if not text:
+				text = '(None)'
+
+			fx.write(text)
+			fx.close()
+
+			try:
+				os.chmod(part_file,S_IRWXU|S_IRWXG|S_IRWXO)
+			except OSError:
+				pass
+
+	def save_email_for_debug(self, message, tempfile=False):
+
+		if tempfile:
+			import tempfile
+			msg_file = tempfile.mktemp('.email2trac')
+		else:
+			#msg_file = '/var/tmp/msg.txt' 
+			msg_file = os.path.join(self.TMPDIR, 'msg.txt')
+
+		if self.DRY_RUN:
+			print 'DRY_RUN: NOT saving email message to %s' %(msg_file)
+		else:
+			print 'TD: saving email to %s' %(msg_file)
+
+			fx = open(msg_file, 'wb')
+			fx.write('%s' % message)
+			fx.close()
+			
+			try:
+				os.chmod(msg_file,S_IRWXU|S_IRWXG|S_IRWXO)
+			except OSError:
+				pass
+
+		message_parts = self.get_message_parts(message)
+		message_parts = self.unique_attachment_names(message_parts)
+		body_text = self.body_text(message_parts)
+		self.debug_body(body_text, True)
+		self.debug_attachments(message_parts)
+
+########## Conversion functions  ###########################################################
+
 	def email_to_unicode(self, message_str):
 		"""
 		Email has 7 bit ASCII code, convert it to unicode with the charset
-        that is encoded in 7-bit ASCII code and encode it as utf-8 so Trac 
+		that is encoded in 7-bit ASCII code and encode it as utf-8 so Trac 
 		understands it.
 		"""
@@ -386,214 +595,4 @@
 		#s = s.encode('utf-8')
 		return s
-
-
-	def email_header_txt(self, m):
-		"""
-		Display To and CC addresses in description field
-		"""
-		s = ''
-
-		if m['To'] and len(m['To']) > 0:
-			s = "'''To:''' %s\r\n" %(m['To'])
-		if m['Cc'] and len(m['Cc']) > 0:
-			s = "%s'''Cc:''' %s\r\n" % (s, m['Cc'])
-
-		return  self.email_to_unicode(s)
-
-
-	def get_sender_info(self, message):
-		"""
-		Get the default author name and email address from the message
-		""" 
-
-		self.email_to = self.email_to_unicode(message['to']) 
-		self.to_name, self.to_email_addr = email.Utils.parseaddr (self.email_to) 
-
-		self.email_from = self.email_to_unicode(message['from'])
-		self.email_name, self.email_addr  = email.Utils.parseaddr(self.email_from)
-
-		## Trac can not handle author's name that contains spaces
-		#  and forbid the ticket email address as author field
-
-		if self.email_addr == self.trac_smtp_from:
-			self.author = "email2trac"
-		else:
-			self.author = self.email_addr
-
-		if self.IGNORE_TRAC_USER_SETTINGS:
-			return
-
-		# Is this a registered user, use email address as search key:
-		# result:
-		#   u : login name
-		#   n : Name that the user has set in the settings tab
-		#   e : email address that the user has set in the settings tab 
-		#
-		users = [ (u,n,e) for (u, n, e) in self.env.get_known_users(self.db)
-			if e and (e.lower() == self.email_addr.lower()) ]
-
-		if len(users) == 1:
-			self.email_from = users[0][0]
-			self.author = users[0][0]
-
-	def set_reply_fields(self, ticket, message):
-		"""
-		Set all the right fields for a new ticket
-		"""
-		if self.DEBUG: 
-			print 'TD: set_reply_fields'
-
-		## Only use name or email adress
-		#ticket['reporter'] = self.email_from
-		ticket['reporter'] = self.author
-
-
-		# Put all CC-addresses in ticket CC field
-		#
-		if self.REPLY_ALL:
-
-			email_cc = ''
-
-			cc_addrs = email.Utils.getaddresses( message.get_all('cc', []) )
-
-			if not cc_addrs:
-				return
-
-			## Build a list of forbidden CC addresses
-			#
-			#to_addrs = email.Utils.getaddresses( message.get_all('to', []) )
-			#to_list = list()
-			#for n,e in to_addrs:
-			#	to_list.append(e)
-				
-			# Remove reporter email address if notification is 
-			# on
-			#
-			if self.notification:
-				try:
-					cc_addrs.remove((self.author, self.email_addr))
-				except ValueError, detail:
-					pass
-
-			for name,addr in cc_addrs:
-		
-				## Prevent mail loop
-				#
-				#if addr in to_list:
-
-				if addr == self.trac_smtp_from:
-					if self.DEBUG:
-						print "Skipping %s mail address for CC-field" %(addr)
-					continue
-
-				if email_cc:
-					email_cc = '%s, %s' %(email_cc, addr)
-				else:
-					email_cc = addr
-
-			if email_cc:
-				if self.DEBUG:
-					print 'TD: set_reply_fields: %s' %email_cc
-
-				ticket['cc'] = self.email_to_unicode(email_cc)
-
-
-########## DEBUG functions  ###########################################################
-
-	def debug_body(self, message_body, tempfile=False):
-		if tempfile:
-			import tempfile
-			body_file = tempfile.mktemp('.email2trac')
-		else:
-			body_file = os.path.join(self.TMPDIR, 'body.txt')
-
-		if self.DRY_RUN:
-			print 'DRY-RUN: not saving body to %s' %(body_file)
-			return
-
-		print 'TD: writing body to %s' %(body_file)
-		fx = open(body_file, 'wb') 
-		if not message_body:
-				message_body = '(None)'
-
-		message_body = message_body.encode('utf-8')
-		#message_body = unicode(message_body, 'iso-8859-15')
-
-		fx.write(message_body)
-		fx.close()
-		try:
-			os.chmod(body_file,S_IRWXU|S_IRWXG|S_IRWXO)
-		except OSError:
-			pass
-
-	def debug_attachments(self, message_parts):
-		"""
-		"""
-		if self.VERBOSE:
-			print "VB: debug_attachments"
-		
-		n = 0
-		for item in message_parts:
-			# Skip inline text parts
-			if not isinstance(item, tuple):
-				continue
-				
-			(original, filename, part) = item
-
-			n = n + 1
-			print 'TD: part%d: Content-Type: %s' % (n, part.get_content_type())
-			print 'TD: part%d: filename: %s' % (n, part.get_filename())
-
-			part_file = os.path.join(self.TMPDIR, part.get_filename())
-			print 'TD: writing part%d (%s)' % (n,part_file)
-
-			if self.DRY_RUN:
-				print 'DRY_RUN: NOT saving attachments'
-				continue
-
-			fx = open(part_file, 'wb')
-			text = part.get_payload(decode=1)
-
-			if not text:
-				text = '(None)'
-
-			fx.write(text)
-			fx.close()
-
-			try:
-				os.chmod(part_file,S_IRWXU|S_IRWXG|S_IRWXO)
-			except OSError:
-				pass
-
-	def save_email_for_debug(self, message, tempfile=False):
-
-		if tempfile:
-			import tempfile
-			msg_file = tempfile.mktemp('.email2trac')
-		else:
-			#msg_file = '/var/tmp/msg.txt' 
-			msg_file = os.path.join(self.TMPDIR, 'msg.txt')
-
-		if self.DRY_RUN:
-			print 'DRY_RUN: NOT saving email message to %s' %(msg_file)
-		else:
-			print 'TD: saving email to %s' %(msg_file)
-
-			fx = open(msg_file, 'wb')
-			fx.write('%s' % message)
-			fx.close()
-			
-			try:
-				os.chmod(msg_file,S_IRWXU|S_IRWXG|S_IRWXO)
-			except OSError:
-				pass
-
-		message_parts = self.get_message_parts(message)
-		message_parts = self.unique_attachment_names(message_parts)
-		body_text = self.body_text(message_parts)
-		self.debug_body(body_text, True)
-		self.debug_attachments(message_parts)
-
-########## Conversion functions  ###########################################################
 
 	def str_to_dict(self, s):
