1217 | | self.new_ticket(m, subject, spam_msg) |
| 1227 | if self.TICKET_UPDATE and self.TICKET_UPDATE_BY_SUBJECT: |
| 1228 | if self.DEBUG: |
| 1229 | print 'TD: Will try to figure out ticket by subject' |
| 1230 | |
| 1231 | # This list of Re: prefixes is probably incomplete. Taken from |
| 1232 | # wikipedia. |
| 1233 | re_regex = re.compile(r'^(RE|AW|VS|SV):\s*(.*)', re.IGNORECASE) |
| 1234 | result = re_regex.search(subject) |
| 1235 | |
| 1236 | if result: |
| 1237 | # This is a reply |
| 1238 | orig_subject = result.group(2) |
| 1239 | # query for any open tickets that match the subject |
| 1240 | cursor = self.db.cursor() |
| 1241 | |
| 1242 | summaries = [orig_subject, subject] |
| 1243 | |
| 1244 | # hard-code the time to 30 days for now. Can easily be |
| 1245 | # made configurable |
| 1246 | lookback = int(time.mktime(time.gmtime())) - 2592000 |
| 1247 | |
| 1248 | matched_id = 0 |
| 1249 | for summary in summaries: |
| 1250 | if self.DEBUG: |
| 1251 | print 'TD: Looking for summary matching: "%s"' % summary |
| 1252 | sql = """SELECT id FROM ticket |
| 1253 | WHERE changetime >= %s |
| 1254 | AND summary = %s |
| 1255 | ORDER BY changetime DESC""" |
| 1256 | cursor.execute(sql, [lookback, summary.strip()]) |
| 1257 | |
| 1258 | for row in cursor: |
| 1259 | (matched_id,) = row |
| 1260 | if self.DEBUG: |
| 1261 | print 'TD: Found matching ticket id: %d' % matched_id |
| 1262 | break |
| 1263 | |
| 1264 | if matched_id: |
| 1265 | break |
| 1266 | |
| 1267 | if matched_id: |
| 1268 | # Change the id to string, as expected by ticket_update |
| 1269 | matched_id = '#%d:' % matched_id |
| 1270 | self.ticket_update(m, matched_id, spam_msg) |
| 1271 | return |
| 1272 | |
| 1273 | # Not a reply or no matches found, create a new ticket |
| 1274 | self.new_ticket(m, subject, spam_msg) |
| 1275 | |
| 1276 | else: |
| 1277 | # No update by subject, so just create a new ticket |
| 1278 | self.new_ticket(m, subject, spam_msg) |