Changeset 408
- Timestamp:
- 07/19/10 17:23:08 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/email2trac.py.in
r407 r408 191 191 self.setup_parameters() 192 192 193 self.DRY_RUN = parameters.dry_run194 193 195 194 def setup_log(self): … … 222 221 self.log_handler = logging.handlers.BufferingHandler(0) 223 222 223 224 224 if self.parameters.log_format: 225 225 self.parameters.log_format = self.parameters.log_format.replace('$(', '%(') … … 227 227 self.parameters.log_format = 'Email2trac: %(message)s' 228 228 229 230 229 self.logger = logging.getLogger('email2trac') 231 230 … … 237 236 self.parameters.log_level = self.parameters.log_level.upper() 238 237 239 if self.parameters.log_level in ['DEBUG', 'ALL'] :238 if self.parameters.log_level in ['DEBUG', 'ALL'] and self.parameters.debug > 0: 240 239 self.logger.setLevel(logging.DEBUG) 241 240 … … 265 264 os.umask(int(self.parameters['umask'], 8)) 266 265 267 if self.parameters. has_key('debug'):268 self. DEBUG = int(self.parameters['debug'])269 else: 270 self. DEBUG= 0266 if self.parameters.debug: 267 self.parameters.debug = int(self.parameters.debug) 268 else: 269 self.parameters.debug = 0 271 270 272 271 if self.parameters.has_key('mailto_link'): … … 445 444 # 446 445 if self.DROP_SPAM and spam: 447 if self. DEBUG> 2 :448 print 'This message is a SPAM. Automatic ticket insertion refused (SPAM level > %d' % self.SPAM_LEVEL446 if self.parameters.debug > 2 : 447 set.logger.debug('Message is a SPAM. Automatic ticket insertion refused (SPAM level > %d)' % self.SPAM_LEVEL) 449 448 450 449 return 'drop' … … 472 471 473 472 except KeyError, detail: 474 if self. DEBUG> 2 :475 print 'TD: %s not defined, all messages are allowed.' %(keyword)473 if self.parameters.debug > 2 : 474 self.logger.debug('%s not defined, all messages are allowed.' %(keyword)) 476 475 477 476 return default … … 544 543 Set all the right fields for a new ticket 545 544 """ 546 if self.DEBUG: 547 print 'TD: set_reply_fields' 545 self.logger.info('function set_reply_fields') 548 546 549 547 ## Only use name or email adress … … 584 582 585 583 if addr == self.trac_smtp_from: 586 if self. DEBUG:587 print "Skipping %s mail address for CC-field" %(addr)584 if self.parameters.debug: 585 self.logger.debug("Skipping %s mail address for CC-field" %(addr)) 588 586 continue 589 587 … … 594 592 595 593 if email_cc: 596 if self. DEBUG:597 print 'TD: set_reply_fields: %s' %email_cc594 if self.parameters.debug: 595 self.logger.debug('set_reply_fields: %s' %email_cc) 598 596 599 597 ticket['cc'] = self.email_to_unicode(email_cc) … … 775 773 """ 776 774 try: 777 print s775 self.logger.debug(s) 778 776 except UnicodeEncodeError, detail: 779 print util.text.unicode_quote(s)777 self.logger.debug(util.text.unicode_quote(s)) 780 778 781 779 ########## TRAC ticket functions ########################################################### … … 785 783 Check if the mailer is allowed to update the ticket 786 784 """ 785 self.logger.info('function check_permission_participants') 787 786 788 787 if tkt['reporter'].lower() in [self.author, self.email_addr]: 789 if self.DEBUG: 790 print 'ALLOW, %s is the ticket reporter' %(self.email_addr) 788 if self.parameters.debug: 789 self.logger.debug('ALLOW, %s is the ticket reporter' %(self.email_addr)) 790 791 791 return True 792 792 793 793 perm = PermissionSystem(self.env) 794 794 if perm.check_permission('TICKET_MODIFY', self.author): 795 if self.DEBUG: 796 print 'ALLOW, %s has trac permission to update the ticket' %(self.author) 795 if self.parameters.debug: 796 self.logger.debug('ALLOW, %s has trac permission to update the ticket' %(self.author)) 797 797 798 return True 799 798 800 else: 799 801 return False … … 806 808 if self.email_addr.lower() in cc.strip(): 807 809 808 if self. DEBUG:809 print 'ALLOW, %s is in the CC' %(self.email_addr)810 if self.parameters.debug: 811 self.logger.debug('ALLOW, %s is in the CC' %(self.email_addr)) 810 812 811 813 return True … … 875 877 # 876 878 for field,value in user_dict.items(): 877 if self. DEBUG >= 10:879 if self.parameters.debug: 878 880 s = 'TD: user_field\t %s = %s' %(field,value) 879 881 self.print_unicode(s) … … 886 888 887 889 if self.trac_smtp_from in cc_list: 888 if self.DEBUG > 10: 889 print 'TD: MAIL LOOP: %s is not allowed as CC address' %(self.trac_smtp_from) 890 if self.parameters.debug: 891 self.logger.debug('TD: MAIL LOOP: %s is not allowed as CC address' %(self.trac_smtp_from)) 892 890 893 cc_list.remove(self.trac_smtp_from) 891 894 … … 915 918 ticket[field] = value 916 919 917 if self. DEBUG >= 10:920 if self.parameters.debug: 918 921 s = 'ticket_field\t %s = %s' %(field, ticket[field]) 919 922 self.print_unicode(s) … … 1004 1007 fields = controller.get_ticket_changes(req, tkt, self.WORKFLOW) 1005 1008 1006 if self. DEBUG:1007 print 'TD: Workflow ticket update fields: ', fields1009 if self.parameters.debug: 1010 self.logger.debug('Workflow ticket update fields: ') 1008 1011 1009 1012 for key in fields.keys(): 1013 if self.parameters.debug: 1014 self.logger.debug('\t %s : %s' %(key, fields[key])) 1015 1010 1016 tkt[key] = fields[key] 1011 1017 … … 1074 1080 value = options[int(value)] 1075 1081 1076 if self. DEBUG > 10:1082 if self.parameters.debug: 1077 1083 s = 'TD: trac.ini name %s = %s' %(name, value) 1078 1084 self.print_unicode(s) … … 1083 1089 try: 1084 1090 value = self.parameters['%s_%s' %(prefix, name)] 1085 if self. DEBUG> 10:1091 if self.parameters.debug > 10: 1086 1092 s = 'TD: email2trac.conf %s = %s ' %(name, value) 1087 1093 self.print_unicode(s) … … 1090 1096 pass 1091 1097 1092 if self. DEBUG:1098 if self.parameters.debug: 1093 1099 s = 'TD: user_dict[%s] = %s' %(name, value) 1094 1100 self.print_unicode(s) … … 1124 1130 orig_subject = result.group(3) 1125 1131 1126 if self. DEBUG:1127 print 'TD: subject search string: %s' %(orig_subject)1132 if self.parameters.debug: 1133 self.logger.debug('subject search string: %s' %(orig_subject)) 1128 1134 1129 1135 cursor = self.db.cursor() … … 1137 1143 1138 1144 for summary in summaries: 1139 if self.DEBUG: 1140 print 'TD: Looking for summary matching: "%s"' % summary 1145 if self.parameters.debug: 1146 self.logger.debug('Looking for summary matching: "%s"' % summary) 1147 1141 1148 sql = """SELECT id FROM ticket 1142 1149 WHERE changetime >= %s AND summary LIKE %s … … 1146 1153 for row in cursor: 1147 1154 (matched_id,) = row 1148 if self.DEBUG: 1149 print 'TD: Found matching ticket id: %d' % matched_id 1155 1156 if self.parameters.debug: 1157 self.logger.debug('Found matching ticket id: %d' % matched_id) 1158 1150 1159 break 1151 1160 … … 1173 1182 if self.TICKET_PERMISSION_SYSTEM: 1174 1183 if not self.check_permission(tkt, 'TICKET_CREATE'): 1175 print 'Reporter: %s has no permission to create tickets' %self.author1184 self.logger.error('Reporter: %s has no permission to create tickets' %self.author) 1176 1185 return False 1177 1186 … … 1210 1219 self.update_ticket_fields(tkt, self.properties) 1211 1220 1212 if self.DEBUG:1213 print 'TD: self.get_message_parts ',1214 print message_parts1215 1216 1221 message_parts = self.unique_attachment_names(message_parts) 1217 if self.DEBUG:1218 print 'TD: self.unique_attachment_names',1219 print message_parts1220 1222 1221 1223 if self.EMAIL_HEADER > 0: … … 1231 1233 when = datetime.now(utc) 1232 1234 1233 if not self.parameters.dry_run: 1235 if self.parameters.dry_run: 1236 print 'DRY_RUN: tkt.insert()' 1237 else: 1234 1238 self.id = tkt.insert() 1235 1239 … … 1263 1267 1264 1268 if changed: 1265 if self.parameters.dry_run 1269 if self.parameters.dry_run: 1266 1270 print 'DRY_RUN: tkt.save_changes(%s, comment) real reporter = %s' %( tkt['reporter'], self.author) 1267 1271 else: … … 1406 1410 from trac.util.datefmt import to_timestamp, utc 1407 1411 1408 if self. DEBUG:1409 print 'TD: Creating a new topic in forum:', self.id1412 if self.parameters.debug: 1413 self.logger.debug('Creating a new topic in forum:', self.id) 1410 1414 1411 1415 # Get dissussion API component. … … 1416 1420 forum = api.get_forum(context, self.id) 1417 1421 1418 if not forum and self. DEBUG:1419 print 'ERROR: Replied forum doesn\'t exist'1422 if not forum and self.parameters.debug: 1423 self.logger.debug("ERROR: Replied forum doesn't exist") 1420 1424 1421 1425 # Prepare topic. … … 1437 1441 from trac.util.datefmt import to_timestamp, utc 1438 1442 1439 if self. DEBUG:1440 print 'TD: Replying to discussion topic', self.id1443 if self.parameters.debug: 1444 self.logger.debug('Replying to discussion topic', self.id) 1441 1445 1442 1446 # Get dissussion API component. … … 1447 1451 topic = api.get_topic(context, self.id) 1448 1452 1449 if not topic and self. DEBUG:1450 print 'ERROR: Replied topic doesn\'t exist'1453 if not topic and self.parameters.debug: 1454 self.logger.debug("ERROR: Replied topic doesn't exist") 1451 1455 1452 1456 # Prepare message. … … 1468 1472 from trac.util.datefmt import to_timestamp, utc 1469 1473 1470 if self. DEBUG:1471 print 'TD: Replying to discussion message', self.id1474 if self.parameters.debug: 1475 self.loggger.debug('TD: Replying to discussion message', self.id) 1472 1476 1473 1477 # Get dissussion API component. … … 1478 1482 message = api.get_message(context, self.id) 1479 1483 1480 if not message and self. DEBUG:1481 print 'ERROR: Replied message doesn\'t exist'1484 if not message and self.parameters.debug: 1485 self.logger.debug("ERROR: Replied message doesn't exist") 1482 1486 1483 1487 # Prepare message. … … 1600 1604 1601 1605 if not m: 1602 if self.DEBUG: 1603 print "TD: This is not a valid email message format" 1606 if self.parameters.debug: 1607 self.logger.debug('This is not a valid email message format') 1608 1604 1609 return 1605 1610 … … 1610 1615 pass 1611 1616 1612 if self. DEBUG > 1: # save the entire e-mail message text1617 if self.parameters.debug: # save the entire e-mail message text 1613 1618 self.save_email_for_debug(m, True) 1614 1619 … … 1617 1622 1618 1623 if not self.email_header_acl('white_list', self.email_addr, True): 1619 if self.DEBUG > 1 : 1620 print 'Message rejected : %s not in white list' %(self.email_addr) 1624 if self.parameters.debug: 1625 self.logger.debug('Message rejected : %s not in white list' %(self.email_addr)) 1626 1621 1627 return False 1622 1628 1623 1629 if self.email_header_acl('black_list', self.email_addr, False): 1624 if self.DEBUG > 1 : 1625 print 'Message rejected : %s in black list' %(self.email_addr) 1630 if self.parameters.debug: 1631 self.logger.debug('Message rejected : %s in black list' %(self.email_addr)) 1632 1626 1633 return False 1627 1634 1628 1635 if not self.email_header_acl('recipient_list', self.to_email_addr, True): 1629 if self.DEBUG > 1 : 1630 print 'Message rejected : %s not in recipient list' %(self.to_email_addr) 1636 if self.parameters.debug: 1637 self.logger.debug('Message rejected : %s not in recipient list' %(self.to_email_addr)) 1638 1631 1639 return False 1632 1640 … … 1646 1654 subject = self.email_to_unicode(m['Subject']) 1647 1655 1648 if self. DEBUG:1649 print "TD:", subject1656 if self.parameters.debug: 1657 self.logger.debug('subject: %s' %subject) 1650 1658 1651 1659 # … … 1808 1816 Parse text if we use inline keywords to set ticket fields 1809 1817 """ 1810 if self. DEBUG:1811 print 'TD: inline_properties function'1818 if self.parameters.debug: 1819 self.logger.info('function inline_properties') 1812 1820 1813 1821 properties = dict() … … 1821 1829 keyword, value = match.groups() 1822 1830 self.properties[keyword] = value.strip() 1823 if self.DEBUG: 1824 print "TD: inline properties: %s : %s" %(keyword,value) 1831 1832 if self.parameters.debug: 1833 self.logger.debug('inline properties: %s : %s' %(keyword,value)) 1825 1834 else: 1826 1835 body.append(line) … … 1874 1883 1875 1884 for part in msg.walk(): 1876 if self. DEBUG:1877 print 'TD: Message part: Main-Type: %s' % part.get_content_maintype()1878 print 'TD: Message part: Content-Type: %s' % part.get_content_type()1885 if self.parameters.debug: 1886 self.logger.debug('Message part: Main-Type: %s' % part.get_content_maintype()) 1887 self.logger.debug('Message part: Content-Type: %s' % part.get_content_type()) 1879 1888 1880 1889 ## Check content type … … 1882 1891 if part.get_content_type() in self.STRIP_CONTENT_TYPES: 1883 1892 1884 if self. DEBUG:1885 print "TD: A %s attachment named '%s' was skipped" %(part.get_content_type(), part.get_filename())1893 if self.parameters.debug: 1894 self.logger.debug("A %s attachment named '%s' was skipped" %(part.get_content_type(), part.get_filename())) 1886 1895 1887 1896 continue … … 1896 1905 # 1897 1906 if part.get_content_maintype() == 'multipart': 1898 if self. DEBUG:1899 print "TD: Skipping multipart container"1907 if self.parameters.debug: 1908 self.logger.debug("Skipping multipart container") 1900 1909 continue 1901 1910 … … 1908 1917 if ALTERNATIVE_MULTIPART and self.DROP_ALTERNATIVE_HTML_VERSION: 1909 1918 if part.get_content_type() == 'text/html': 1910 if self. DEBUG:1911 print "TD: Skipping alternative HTML message"1919 if self.parameters.debug: 1920 self.logger.debug('Skipping alternative HTML message') 1912 1921 1913 1922 ALTERNATIVE_MULTIPART = False … … 1917 1926 # 1918 1927 if part.get_content_type() == 'text/plain' and inline: 1919 if self. DEBUG:1920 print 'TD: Inline body part'1928 if self.parameters.debug: 1929 self.logger.debug(' Inline body part') 1921 1930 1922 1931 # Try to decode, if fails then do not decode … … 1964 1973 message_parts.append('%s' %ubody_text) 1965 1974 else: 1966 if self. DEBUG:1975 if self.parameters.debug: 1967 1976 s = 'TD: Filename: %s' % part.get_filename() 1968 1977 self.print_unicode(s) … … 2037 2046 unique_filename = "%s-%s%s" % (dummy_filename, num, ext) 2038 2047 2039 if self. DEBUG:2048 if self.parameters.debug: 2040 2049 s = 'TD: Attachment with filename %s will be saved as %s' % (filename, unique_filename) 2041 2050 self.print_unicode(s) … … 2050 2059 def attachment_exists(self, filename): 2051 2060 2052 if self. DEBUG:2061 if self.parameters.debug: 2053 2062 s = 'TD: attachment already exists: Id : %s, Filename : %s' %(self.id, filename) 2054 2063 self.print_unicode(s)
Note: See TracChangeset
for help on using the changeset viewer.