Index: trunk/email2trac.py.in
===================================================================
--- trunk/email2trac.py.in	(revision 249)
+++ trunk/email2trac.py.in	(revision 250)
@@ -327,5 +327,5 @@
 		except KeyError, detail:
 			if self.DEBUG > 2 :
-				print '%s not defined, all messages are allowed.' %(keyword)
+				print 'TD: %s not defined, all messages are allowed.' %(keyword)
 
 			return default 
@@ -434,17 +434,4 @@
 
 
-	def set_owner(self, ticket):
-		"""
-		Select default owner for ticket component
-		"""
-		#### return self.get_config('ticket', 'default_component') 
-		cursor = self.db.cursor()
-		sql = "SELECT owner FROM component WHERE name='%s'" % ticket['component']
-		cursor.execute(sql)
-		try:
-			ticket['owner'] = cursor.fetchone()[0]
-		except TypeError, detail:
-			ticket['owner'] = None
-
 	def get_sender_info(self, message):
 		"""
@@ -471,10 +458,10 @@
 		#   e : email address that the user has set in the settings tab 
 		#
-	    tmp_email_addrs = self.email_addr.lower()
 		users = [ (u,n,e) for (u, n, e) in self.env.get_known_users(self.db)
-				if e == tmp_email_addr ]
+			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):
@@ -614,4 +601,6 @@
 		creating a new one.
 		"""
+		if self.DEBUG:
+			print "TD: ticket_update"
 
 		if not m['Subject']:
@@ -677,5 +666,5 @@
 
 		message_parts = self.get_message_parts(m)
-		message_parts = self.unique_attachment_names(message_parts, tkt)
+		message_parts = self.unique_attachment_names(message_parts, ticket_id)
 
 		if self.EMAIL_HEADER:
@@ -685,15 +674,17 @@
 
 		if body_text.strip() or update_fields:
-			tkt.save_changes(self.author, body_text, when)
-
-		tkt['id'] = ticket_id
+			if self.DRY_RUN:
+				print 'DRY_RUN: tkt.save_changes(self.author, comment) ', self.author
+			else:
+				tkt.save_changes(self.author, body_text, when)
+
 
 		if self.VERSION  == 0.9:
-			str = self.attachments(message_parts, tkt, True)
-		else:
-			str = self.attachments(message_parts, tkt)
+			str = self.attachments(message_parts, ticket_id, True)
+		else:
+			str = self.attachments(message_parts, ticket_id)
 
 		if self.notification and not spam:
-			self.notify(tkt, False, when)
+			self.notify(tkt, ticket_id, False, when)
 
 		return True
@@ -755,16 +746,9 @@
 		Create a new ticket
 		"""
+		if self.DEBUG:
+			print "TD: new_ticket"
+
 		tkt = Ticket(self.env)
-
 		self.set_ticket_fields(tkt)
-
-		# Some defaults
-		#
-		#tkt['status'] = 'new'
-		#tkt['milestone'] = self.get_config('ticket', 'default_milestone')
-		#tkt['priority'] = self.get_config('ticket', 'default_priority')
-		#tkt['severity'] = self.get_config('ticket', 'default_severity')
-		#tkt['version'] = self.get_config('ticket', 'default_version')
-		#tkt['type'] = self.get_config('ticket', 'default_type')
 
 		# Old style setting for component, will be removed
@@ -780,5 +764,4 @@
 		else:
 			tkt['summary'] = self.email_to_unicode(msg['Subject'])
-
 
 		self.set_reply_fields(tkt, msg)
@@ -809,7 +792,5 @@
 		else:
 			ticket_id = tkt.insert()
-			
-		tkt['id'] = ticket_id
-
+		
 		changed = False
 		comment = ''
@@ -825,5 +806,5 @@
 				%(head, mailto, body_text)
 
-		str =  self.attachments(message_parts, tkt)
+		str =  self.attachments(message_parts, ticket_id)
 		if str:
 			changed = True
@@ -832,16 +813,12 @@
 		if changed:
 			if self.DRY_RUN:
-				print 'DRY_RUN: tkt.save_changes(self.author, comment)'
+				print 'DRY_RUN: tkt.save_changes(self.author, comment) ', self.author
 			else:
 				tkt.save_changes(self.author, comment)
 				#print tkt.get_changelog(self.db, when)
 
-		if self.notification:
-			if self.DRY_RUN:
-				print 'DRY_RUN: self.notify(tkt, True)'
-			else:
-				if not spam:
-					self.notify(tkt, True)
-				#self.notify(tkt, False)
+		if self.notification and not spam:
+			self.notify(tkt, ticket_id, True)
+			#self.notify(ticket_id, False)
 
 	def parse(self, fp):
@@ -852,5 +829,5 @@
 		if not m:
 			if self.DEBUG:
-				print "This is not a valid email message format"
+				print "TD: This is not a valid email message format"
 			return
 			
@@ -1103,5 +1080,5 @@
 		return message_parts
 		
-	def unique_attachment_names(self, message_parts, tkt = None):
+	def unique_attachment_names(self, message_parts, tkt_id = None):
 		renamed_parts = []
 		attachment_names = set()
@@ -1153,5 +1130,5 @@
 			filename, ext = os.path.splitext(filename)
 
-			while unique_filename in attachment_names or self.attachment_exists(tkt, unique_filename):
+			while unique_filename in attachment_names or self.attachment_exists(tkt_id, unique_filename):
 				num += 1
 				unique_filename = "%s-%s%s" % (filename, num, ext)
@@ -1170,12 +1147,18 @@
 		
 			
-	def attachment_exists(self, tkt, filename):
-		if tkt is None:
+	def attachment_exists(self, tkt_id, filename):
+
+		if self.DEBUG:
+			print "TD: attachment_exists: Ticket number : %s, Filename : %s" %(tkt_id, filename)
+
+		# We have no valid ticket id
+		#
+		if not tkt_id:
 			return False
-			
+
 		try:
-			Attachment(self.env, 'ticket', tkt['id'], filename)
+			att = attachment.Attachment(self.env, 'ticket', tkt_id, filename)
 			return True
-		except ResourceNotFound:
+		except attachment.ResourceNotFound:
 			return False
 			
@@ -1203,8 +1186,11 @@
 		return body_text
 
-	def notify(self, tkt , new=True, modtime=0):
+	def notify(self, tkt, id, new=True, modtime=0):
 		"""
 		A wrapper for the TRAC notify function. So we can use templates
 		"""
+		if self.DRY_RUN:
+				print 'DRY_RUN: self.notify(tkt, True) ', self.author
+				return
 		try:
 			# create false {abs_}href properties, to trick Notify()
@@ -1236,5 +1222,5 @@
 
 		except Exception, e:
-			print 'TD: Failure sending notification on creation of ticket #%s: %s' %(tkt['id'], e)
+			print 'TD: Failure sending notification on creation of ticket #%s: %s' %(id, e)
 
 	def html_mailto_link(self, subject, id, body):
@@ -1262,9 +1248,10 @@
 		return str
 
-	def attachments(self, message_parts, ticket, update=False):
+	def attachments(self, message_parts, tkt_id, update=False):
 		'''
 		save any attachments as files in the ticket's directory
 		'''
 		if self.DRY_RUN:
+			print "DRY_RUN: no attachments saved"
 			return ''
 
@@ -1313,5 +1300,5 @@
 			# 
 			fd = open(path, 'rb')
-			att = attachment.Attachment(self.env, 'ticket', ticket['id'])
+			att = attachment.Attachment(self.env, 'ticket', tkt_id)
 
 			# This will break the ticket_update system, the body_text is vaporized
