Index: /trunk/email2trac.py.in
===================================================================
--- /trunk/email2trac.py.in	(revision 252)
+++ /trunk/email2trac.py.in	(revision 253)
@@ -146,4 +146,5 @@
 		self.email_addr = None
 		self.email_from = None
+		self.id = None
 
 		self.VERSION = version
@@ -630,5 +631,5 @@
 			# Strip '#' 
 			#
-			ticket_id = int(nr[1:])
+			self.id = int(nr[1:])
 
 		except AttributeError:
@@ -636,5 +637,5 @@
 			#
 			nr = result.group('ticketnr') 
-			ticket_id = int(nr[1:-1])
+			self.id = int(nr[1:-1])
 
 
@@ -649,6 +650,8 @@
 
 		try:
-			tkt = Ticket(self.env, ticket_id, self.db)
+			tkt = Ticket(self.env, self.id, self.db)
 		except util.TracError, detail:
+			# Not a valid ticket
+			self.id = None
 			return False
 
@@ -666,5 +669,5 @@
 
 		message_parts = self.get_message_parts(m)
-		message_parts = self.unique_attachment_names(message_parts, ticket_id)
+		message_parts = self.unique_attachment_names(message_parts)
 
 		if self.EMAIL_HEADER:
@@ -681,10 +684,10 @@
 
 		if self.VERSION  == 0.9:
-			str = self.attachments(message_parts, ticket_id, True)
-		else:
-			str = self.attachments(message_parts, ticket_id)
+			str = self.attachments(message_parts, True)
+		else:
+			str = self.attachments(message_parts)
 
 		if self.notification and not spam:
-			self.notify(tkt, ticket_id, False, when)
+			self.notify(tkt, False, when)
 
 		return True
@@ -788,8 +791,6 @@
 		when = datetime.now(utc)
 
-		if self.DRY_RUN:
-			ticket_id = 'DRY_RUN'
-		else:
-			ticket_id = tkt.insert()
+		if not self.DRY_RUN:
+			self.id = tkt.insert()
 		
 		changed = False
@@ -801,10 +802,10 @@
 			changed = True
 			comment = u'\nadded mailto line\n'
-			#mailto = self.html_mailto_link(tkt['summary'], ticket_id, body_text)
-			mailto = self.html_mailto_link( m['Subject'], ticket_id, body_text)
+			mailto = self.html_mailto_link( m['Subject'], body_text)
+
 			tkt['description'] = u'%s\r\n%s%s\r\n' \
 				%(head, mailto, body_text)
 
-		str =  self.attachments(message_parts, ticket_id)
+		str =  self.attachments(message_parts)
 		if str:
 			changed = True
@@ -819,6 +820,5 @@
 
 		if self.notification and not spam:
-			self.notify(tkt, ticket_id, True)
-			#self.notify(ticket_id, False)
+			self.notify(tkt, True)
 
 	def parse(self, fp):
@@ -1080,5 +1080,5 @@
 		return message_parts
 		
-	def unique_attachment_names(self, message_parts, tkt_id = None):
+	def unique_attachment_names(self, message_parts):
 		renamed_parts = []
 		attachment_names = set()
@@ -1130,5 +1130,5 @@
 			filename, ext = os.path.splitext(filename)
 
-			while unique_filename in attachment_names or self.attachment_exists(tkt_id, unique_filename):
+			while unique_filename in attachment_names or self.attachment_exists(unique_filename):
 				num += 1
 				unique_filename = "%s-%s%s" % (filename, num, ext)
@@ -1147,16 +1147,16 @@
 		
 			
-	def attachment_exists(self, tkt_id, filename):
+	def attachment_exists(self, filename):
 
 		if self.DEBUG:
-			print "TD: attachment_exists: Ticket number : %s, Filename : %s" %(tkt_id, filename)
+			print "TD: attachment_exists: Ticket number : %s, Filename : %s" %(self.id, filename)
 
 		# We have no valid ticket id
 		#
-		if not tkt_id:
+		if not self.id:
 			return False
 
 		try:
-			att = attachment.Attachment(self.env, 'ticket', tkt_id, filename)
+			att = attachment.Attachment(self.env, 'ticket', self.id, filename)
 			return True
 		except attachment.ResourceNotFound:
@@ -1186,5 +1186,5 @@
 		return body_text
 
-	def notify(self, tkt, id, new=True, modtime=0):
+	def notify(self, tkt, new=True, modtime=0):
 		"""
 		A wrapper for the TRAC notify function. So we can use templates
@@ -1222,7 +1222,10 @@
 
 		except Exception, e:
-			print 'TD: Failure sending notification on creation of ticket #%s: %s' %(id, e)
-
-	def html_mailto_link(self, subject, id, body):
+			print 'TD: Failure sending notification on creation of ticket #%s: %s' %(self.id, e)
+
+	def html_mailto_link(self, subject, body):
+		"""
+		This function returns a HTML mailto tag with the ticket id and author email address
+		"""
 		if not self.author:
 			author = self.email_addr
@@ -1230,16 +1233,9 @@
 			author = self.author
 
-		# Must find a fix
-		#
-		#arr = string.split(body, '\n')
-		#arr = map(self.mail_line, arr)
-		#body = string.join(arr, '\n')
-		#body = '%s wrote:\n%s' %(author, body)
-
-		# Temporary fix
+		# use urllib to escape the chars
 		#
 		str = 'mailto:%s?Subject=%s&Cc=%s' %( 
 		       urllib.quote(self.email_addr), 
-			   urllib.quote('Re: #%s: %s' %(id, subject)),
+			   urllib.quote('Re: #%s: %s' %(self.id, subject)),
 			   urllib.quote(self.MAILTO_CC)
 			   )
@@ -1248,5 +1244,5 @@
 		return str
 
-	def attachments(self, message_parts, tkt_id, update=False):
+	def attachments(self, message_parts, update=False):
 		'''
 		save any attachments as files in the ticket's directory
@@ -1257,6 +1253,4 @@
 
 		count = 0
-		first = 0 
-		number = 0 
 
 		# Get Maxium attachment size
@@ -1300,5 +1294,5 @@
 			# 
 			fd = open(path, 'rb')
-			att = attachment.Attachment(self.env, 'ticket', tkt_id)
+			att = attachment.Attachment(self.env, 'ticket', self.id)
 
 			# This will break the ticket_update system, the body_text is vaporized
