Index: trunk/email2trac.py.in
===================================================================
--- trunk/email2trac.py.in	(revision 407)
+++ trunk/email2trac.py.in	(revision 408)
@@ -191,5 +191,4 @@
 		self.setup_parameters()
 
-		self.DRY_RUN = parameters.dry_run
 
 	def setup_log(self):
@@ -222,4 +221,5 @@
 			self.log_handler = logging.handlers.BufferingHandler(0)
 
+
 		if self.parameters.log_format:
 			self.parameters.log_format = self.parameters.log_format.replace('$(', '%(')
@@ -227,5 +227,4 @@
 			self.parameters.log_format = 'Email2trac: %(message)s'
 
-
 		self.logger = logging.getLogger('email2trac')
 
@@ -237,5 +236,5 @@
 			self.parameters.log_level = self.parameters.log_level.upper()
 
-		if self.parameters.log_level in ['DEBUG', 'ALL']:
+		if self.parameters.log_level in ['DEBUG', 'ALL'] and self.parameters.debug > 0:
 			self.logger.setLevel(logging.DEBUG)
 
@@ -265,8 +264,8 @@
 			os.umask(int(self.parameters['umask'], 8))
 
-		if self.parameters.has_key('debug'):
-			self.DEBUG = int(self.parameters['debug'])
-		else:
-			self.DEBUG = 0
+		if self.parameters.debug:
+			self.parameters.debug = int(self.parameters.debug)
+		else:
+			self.parameters.debug = 0
 
 		if self.parameters.has_key('mailto_link'):
@@ -445,6 +444,6 @@
 		#
 		if self.DROP_SPAM and spam:
-			if self.DEBUG > 2 : 
-				print 'This message is a SPAM. Automatic ticket insertion refused (SPAM level > %d' % self.SPAM_LEVEL
+			if self.parameters.debug > 2 : 
+				set.logger.debug('Message is a SPAM. Automatic ticket insertion refused (SPAM level > %d)' % self.SPAM_LEVEL)
 
 			return 'drop'	
@@ -472,6 +471,6 @@
 
 		except KeyError, detail:
-			if self.DEBUG > 2 :
-				print 'TD: %s not defined, all messages are allowed.' %(keyword)
+			if self.parameters.debug > 2 :
+				self.logger.debug('%s not defined, all messages are allowed.' %(keyword))
 
 			return default 
@@ -544,6 +543,5 @@
 		Set all the right fields for a new ticket
 		"""
-		if self.DEBUG: 
-			print 'TD: set_reply_fields'
+		self.logger.info('function set_reply_fields')
 
 		## Only use name or email adress
@@ -584,6 +582,6 @@
 
 				if addr == self.trac_smtp_from:
-					if self.DEBUG:
-						print "Skipping %s mail address for CC-field" %(addr)
+					if self.parameters.debug:
+						self.logger.debug("Skipping %s mail address for CC-field" %(addr))
 					continue
 
@@ -594,6 +592,6 @@
 
 			if email_cc:
-				if self.DEBUG:
-					print 'TD: set_reply_fields: %s' %email_cc
+				if self.parameters.debug:
+						self.logger.debug('set_reply_fields: %s' %email_cc)
 
 				ticket['cc'] = self.email_to_unicode(email_cc)
@@ -775,7 +773,7 @@
 		"""
 		try:
-			print s
+			self.logger.debug(s)
 		except UnicodeEncodeError, detail:
-			print util.text.unicode_quote(s)
+			self.logger.debug(util.text.unicode_quote(s))
 
 ########## TRAC ticket functions  ###########################################################
@@ -785,15 +783,19 @@
 		Check if the mailer is allowed to update the ticket
 		"""
+		self.logger.info('function check_permission_participants')
 
 		if tkt['reporter'].lower() in [self.author, self.email_addr]:
-			if self.DEBUG:
-				print 'ALLOW, %s is the ticket reporter' %(self.email_addr)
+			if self.parameters.debug:
+				self.logger.debug('ALLOW, %s is the ticket reporter' %(self.email_addr))
+
 			return True
 
 		perm = PermissionSystem(self.env)
 		if perm.check_permission('TICKET_MODIFY', self.author):
-			if self.DEBUG:
-				print 'ALLOW, %s has trac permission to update the ticket' %(self.author)
+			if self.parameters.debug:
+				self.logger.debug('ALLOW, %s has trac permission to update the ticket' %(self.author))
+
 			return True
+
 		else:
 			return False
@@ -806,6 +808,6 @@
 				if self.email_addr.lower() in cc.strip():
 
-					if self.DEBUG:
-						print 'ALLOW, %s is in the CC' %(self.email_addr)
+					if self.parameters.debug:
+						self.logger.debug('ALLOW, %s is in the CC' %(self.email_addr))
 
 					return True
@@ -875,5 +877,5 @@
 		#
 		for field,value in user_dict.items():
-			if self.DEBUG >= 10: 
+			if self.parameters.debug:
 				s = 'TD: user_field\t %s = %s' %(field,value) 
 				self.print_unicode(s)
@@ -886,6 +888,7 @@
 
 				if self.trac_smtp_from in cc_list:
-					if self.DEBUG > 10:
-						print 'TD: MAIL LOOP: %s is not allowed as CC address' %(self.trac_smtp_from)
+					if self.parameters.debug:
+						self.logger.debug('TD: MAIL LOOP: %s is not allowed as CC address' %(self.trac_smtp_from))
+
 					cc_list.remove(self.trac_smtp_from)
 
@@ -915,5 +918,5 @@
 					ticket[field] = value
 
-				if self.DEBUG >= 10: 
+				if self.parameters.debug:
 					s = 'ticket_field\t %s = %s' %(field,  ticket[field]) 
 					self.print_unicode(s)
@@ -1004,8 +1007,11 @@
 				fields = controller.get_ticket_changes(req, tkt, self.WORKFLOW)
 
-				if self.DEBUG:
-					print 'TD: Workflow ticket update fields: ', fields
+				if self.parameters.debug:
+					self.logger.debug('Workflow ticket update fields: ')
 
 				for key in fields.keys():
+					if self.parameters.debug:
+						self.logger.debug('\t %s : %s' %(key, fields[key]))
+
 					tkt[key] = fields[key]
 
@@ -1074,5 +1080,5 @@
 					 value = options[int(value)]
 	
-			if self.DEBUG > 10:
+			if self.parameters.debug:
 				s = 'TD: trac.ini name %s = %s' %(name, value)
 				self.print_unicode(s)
@@ -1083,5 +1089,5 @@
 			try:
 				value = self.parameters['%s_%s' %(prefix, name)]
-				if self.DEBUG > 10:
+				if self.parameters.debug > 10:
 					s = 'TD: email2trac.conf %s = %s ' %(name, value)
 					self.print_unicode(s)
@@ -1090,5 +1096,5 @@
 				pass
 		
-			if self.DEBUG:
+			if self.parameters.debug:
 				s = 'TD: user_dict[%s] = %s' %(name, value)
 				self.print_unicode(s)
@@ -1124,6 +1130,6 @@
 				orig_subject = result.group(3)
 
-				if self.DEBUG:
-					print 'TD: subject search string: %s' %(orig_subject)
+				if self.parameters.debug:
+					self.logger.debug('subject search string: %s' %(orig_subject))
 
 				cursor = self.db.cursor()
@@ -1137,6 +1143,7 @@
 
 				for summary in summaries:
-					if self.DEBUG:
-						print 'TD: Looking for summary matching: "%s"' % summary
+					if self.parameters.debug:
+						self.logger.debug('Looking for summary matching: "%s"' % summary)
+
 					sql = """SELECT id FROM ticket
 						 	WHERE changetime >= %s AND summary LIKE %s
@@ -1146,6 +1153,8 @@
 					for row in cursor:
 						(matched_id,) = row
-						if self.DEBUG:
-							print 'TD: Found matching ticket id: %d' % matched_id
+
+						if self.parameters.debug:
+							self.logger.debug('Found matching ticket id: %d' % matched_id)
+
 						break
 
@@ -1173,5 +1182,5 @@
 		if self.TICKET_PERMISSION_SYSTEM:
 			if not self.check_permission(tkt, 'TICKET_CREATE'):
-				print 'Reporter: %s has no permission to create tickets' %self.author
+				self.logger.error('Reporter: %s has no permission to create tickets' %self.author)
 				return False
 
@@ -1210,12 +1219,5 @@
 				self.update_ticket_fields(tkt, self.properties)
 
-		if self.DEBUG:
-			print 'TD: self.get_message_parts ',
-			print message_parts
-
 		message_parts = self.unique_attachment_names(message_parts)
-		if self.DEBUG:
-			print 'TD: self.unique_attachment_names',
-			print message_parts
 		
 		if self.EMAIL_HEADER > 0:
@@ -1231,5 +1233,7 @@
 		when = datetime.now(utc)
 
-		if not self.parameters.dry_run:
+		if self.parameters.dry_run:
+			print 'DRY_RUN: tkt.insert()'
+		else:
 			self.id = tkt.insert()
 	
@@ -1263,5 +1267,5 @@
 
 		if changed:
-			if self.parameters.dry_run
+			if self.parameters.dry_run:
 				print 'DRY_RUN: tkt.save_changes(%s, comment) real reporter = %s' %( tkt['reporter'], self.author)
 			else:
@@ -1406,6 +1410,6 @@
 		from trac.util.datefmt import to_timestamp, utc
 
-		if self.DEBUG:
-			print 'TD: Creating a new topic in forum:', self.id
+		if self.parameters.debug:
+			self.logger.debug('Creating a new topic in forum:', self.id)
 
 		# Get dissussion API component.
@@ -1416,6 +1420,6 @@
 		forum = api.get_forum(context, self.id)
 
-		if not forum and self.DEBUG:
-			print 'ERROR: Replied forum doesn\'t exist'
+		if not forum and self.parameters.debug:
+			self.logger.debug("ERROR: Replied forum doesn't exist")
 
 		# Prepare topic.
@@ -1437,6 +1441,6 @@
 		from trac.util.datefmt import to_timestamp, utc
 
-		if self.DEBUG:
-			print 'TD: Replying to discussion topic', self.id
+		if self.parameters.debug:
+			self.logger.debug('Replying to discussion topic', self.id)
 
 		# Get dissussion API component.
@@ -1447,6 +1451,6 @@
 		topic = api.get_topic(context, self.id)
 
-		if not topic and self.DEBUG:
-			print 'ERROR: Replied topic doesn\'t exist'
+		if not topic and self.parameters.debug:
+			self.logger.debug("ERROR: Replied topic doesn't exist")
 
 		# Prepare message.
@@ -1468,6 +1472,6 @@
 		from trac.util.datefmt import to_timestamp, utc
 
-		if self.DEBUG:
-			print 'TD: Replying to discussion message', self.id
+		if self.parameters.debug:
+			self.loggger.debug('TD: Replying to discussion message', self.id)
 
 		# Get dissussion API component.
@@ -1478,6 +1482,6 @@
 		message = api.get_message(context, self.id)
 
-		if not message and self.DEBUG:
-			print 'ERROR: Replied message doesn\'t exist'
+		if not message and self.parameters.debug:
+			self.logger.debug("ERROR: Replied message doesn't exist")
 
 		# Prepare message.
@@ -1600,6 +1604,7 @@
 		
 		if not m:
-			if self.DEBUG:
-				print "TD: This is not a valid email message format"
+			if self.parameters.debug:
+				self.logger.debug('This is not a valid email message format')
+
 			return
 			
@@ -1610,5 +1615,5 @@
 			pass
 
-		if self.DEBUG > 1:	  # save the entire e-mail message text
+		if self.parameters.debug:	  # save the entire e-mail message text
 			self.save_email_for_debug(m, True)
 
@@ -1617,16 +1622,19 @@
 
 		if not self.email_header_acl('white_list', self.email_addr, True):
-			if self.DEBUG > 1 :
- 				print 'Message rejected : %s not in white list' %(self.email_addr)
+			if self.parameters.debug:
+				self.logger.debug('Message rejected : %s not in white list' %(self.email_addr))
+
 			return False
 
 		if self.email_header_acl('black_list', self.email_addr, False):
-			if self.DEBUG > 1 :
- 				print 'Message rejected : %s in black list' %(self.email_addr)
+			if self.parameters.debug:
+				self.logger.debug('Message rejected : %s in black list' %(self.email_addr))
+
 			return False
 
 		if not self.email_header_acl('recipient_list', self.to_email_addr, True):
-			if self.DEBUG > 1 :
- 				print 'Message rejected : %s not in recipient list' %(self.to_email_addr)
+			if self.parameters.debug:
+				self.logger.debug('Message rejected : %s not in recipient list' %(self.to_email_addr))
+
 			return False
 
@@ -1646,6 +1654,6 @@
 			subject  = self.email_to_unicode(m['Subject'])
 
-		if self.DEBUG:
-			 print "TD:", subject
+		if self.parameters.debug:
+			self.logger.debug('subject: %s' %subject)
 
 		#
@@ -1808,6 +1816,6 @@
 		Parse text if we use inline keywords to set ticket fields
 		"""
-		if self.DEBUG:
-			print 'TD: inline_properties function'
+		if self.parameters.debug:
+			self.logger.info('function inline_properties')
 
 		properties = dict()
@@ -1821,6 +1829,7 @@
 				keyword, value = match.groups()
 				self.properties[keyword] = value.strip()
-				if self.DEBUG:
-					print "TD: inline properties: %s : %s" %(keyword,value)
+
+				if self.parameters.debug:
+					self.logger.debug('inline properties: %s : %s' %(keyword,value))
 			else:
 				body.append(line)
@@ -1874,7 +1883,7 @@
 
 		for part in msg.walk():
-			if self.DEBUG:
-				print 'TD: Message part: Main-Type: %s' % part.get_content_maintype()
-				print 'TD: Message part: Content-Type: %s' % part.get_content_type()
+			if self.parameters.debug:
+				self.logger.debug('Message part: Main-Type: %s' % part.get_content_maintype())
+				self.logger.debug('Message part: Content-Type: %s' % part.get_content_type())
 
 			## Check content type
@@ -1882,6 +1891,6 @@
 			if part.get_content_type() in self.STRIP_CONTENT_TYPES:
 
-				if self.DEBUG:
-					print "TD: A %s attachment named '%s' was skipped" %(part.get_content_type(), part.get_filename())
+				if self.parameters.debug:
+					self.logger.debug("A %s attachment named '%s' was skipped" %(part.get_content_type(), part.get_filename()))
 
 				continue
@@ -1896,6 +1905,6 @@
 			#
 			if part.get_content_maintype() == 'multipart':
-				if self.DEBUG:
-					print "TD: Skipping multipart container"
+				if self.parameters.debug:
+					self.logger.debug("Skipping multipart container")
 				continue
 			
@@ -1908,6 +1917,6 @@
 			if ALTERNATIVE_MULTIPART and self.DROP_ALTERNATIVE_HTML_VERSION:
 				if part.get_content_type() == 'text/html':
-					if self.DEBUG:
-						print "TD: Skipping alternative HTML message" 
+					if self.parameters.debug:
+						self.logger.debug('Skipping alternative HTML message')
 
 			        	ALTERNATIVE_MULTIPART = False
@@ -1917,6 +1926,6 @@
 			#
 			if part.get_content_type() == 'text/plain' and inline:
-				if self.DEBUG:
-					print 'TD:               Inline body part'
+				if self.parameters.debug:
+					self.logger.debug('               Inline body part')
 
 				# Try to decode, if fails then do not decode
@@ -1964,5 +1973,5 @@
 					message_parts.append('%s' %ubody_text)
 			else:
-				if self.DEBUG:
+				if self.parameters.debug:
 					s = 'TD:               Filename: %s' % part.get_filename()
 					self.print_unicode(s)
@@ -2037,5 +2046,5 @@
 				unique_filename = "%s-%s%s" % (dummy_filename, num, ext)
 				
-			if self.DEBUG:
+			if self.parameters.debug:
 				s = 'TD: Attachment with filename %s will be saved as %s' % (filename, unique_filename)
 				self.print_unicode(s)
@@ -2050,5 +2059,5 @@
 	def attachment_exists(self, filename):
 
-		if self.DEBUG:
+		if self.parameters.debug:
 			s = 'TD: attachment already exists: Id : %s, Filename : %s' %(self.id, filename)
 			self.print_unicode(s)
