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) |