Ticket #188: email2trac-by-subject.2.patch

File email2trac-by-subject.2.patch, 2.3 KB (added by Konstantin Ryabitsev <icon@…>, 14 years ago)

Slightly improved version of the patch.

  • (a) email2trac.py.in vs. (b) /home/kryabi/email2trac-modified

    a b  
    197197                else:
    198198                        self.TICKET_UPDATE = 0
    199199
     200                if parameters.has_key('ticket_update_by_subject'):
     201                        self.TICKET_UPDATE_BY_SUBJECT = int(parameters['ticket_update_by_subject'])
     202                else:
     203                        self.TICKET_UPDATE_BY_SUBJECT = 0
     204
    200205                if parameters.has_key('drop_spam'):
    201206                        self.DROP_SPAM = int(parameters['drop_spam'])
    202207                else:
     
    12111217                        elif result.group('new_fields'):
    12121218                                self.new_ticket(m, subject[:result.start('new_fields')], spam_msg, result.group('new_fields'))
    12131219
    1214                 # Create ticket
    1215                 #
    12161220                else:
    1217                         self.new_ticket(m, subject, spam_msg)
     1221                        if self.TICKET_UPDATE and self.TICKET_UPDATE_BY_SUBJECT:
     1222                                if self.DEBUG:
     1223                                        print 'TD: Will try to figure out ticket by subject'
     1224
     1225                                # This list of Re: prefixes is probably incomplete. Taken from
     1226                                # wikipedia.
     1227                                re_regex = re.compile(r'^(RE|AW|VS|SV):\s*(.*)', re.IGNORECASE)
     1228                                result = re_regex.search(subject)
     1229
     1230                                if result:
     1231                                        # This is a reply
     1232                                        orig_subject = result.group(2)
     1233                                        cursor = self.db.cursor()
     1234
     1235                                        summaries = [orig_subject, '%%: %s' % orig_subject]
     1236
     1237                                        # hard-code the time to 30 days for now. Can easily be
     1238                                        # made configurable
     1239                                        lookback = int(time.mktime(time.gmtime())) - 2592000
     1240
     1241                                        matched_id = 0
     1242                                        for summary in summaries:
     1243                                                if self.DEBUG:
     1244                                                        print 'TD: Looking for summary matching: "%s"' % summary
     1245                                                sql = """SELECT id FROM ticket
     1246                                                                                  WHERE changetime >= %s
     1247                                                                                        AND summary LIKE %s
     1248                                                                                        ORDER BY changetime DESC"""
     1249                                                cursor.execute(sql, [lookback, summary.strip()])
     1250
     1251                                                for row in cursor:
     1252                                                        (matched_id,) = row
     1253                                                        if self.DEBUG:
     1254                                                                print 'TD: Found matching ticket id: %d' % matched_id
     1255                                                        break
     1256
     1257                                                if matched_id:
     1258                                                        break
     1259
     1260                                        if matched_id:
     1261                                                # Change the id to string, as expected by ticket_update
     1262                                                matched_id = '#%d:' % matched_id
     1263                                                self.ticket_update(m, matched_id, spam_msg)
     1264                                                return
     1265
     1266                                # Not a reply or no matches found, create a new ticket
     1267                                self.new_ticket(m, subject, spam_msg)
     1268
     1269                        else:
     1270                                # No update by subject, so just create a new ticket
     1271                                self.new_ticket(m, subject, spam_msg)
    12181272
    12191273########## BODY TEXT functions  ###########################################################
    12201274