| 1 | #!/usr/bin/python |
|---|
| 2 | TRAC_ENV_PATH = '/var/lib/trac/ops' |
|---|
| 3 | |
|---|
| 4 | import time |
|---|
| 5 | import email |
|---|
| 6 | import sys |
|---|
| 7 | import re |
|---|
| 8 | import trac.util |
|---|
| 9 | from trac.env import Environment |
|---|
| 10 | from trac.ticket import Ticket |
|---|
| 11 | |
|---|
| 12 | env=Environment(TRAC_ENV_PATH) |
|---|
| 13 | |
|---|
| 14 | ################################ |
|---|
| 15 | ## Stolen from emailfilter.py |
|---|
| 16 | class TicketEmailParser(object): |
|---|
| 17 | env = None |
|---|
| 18 | def __init__(self, env): |
|---|
| 19 | self.env = env |
|---|
| 20 | def parse(self, msg): |
|---|
| 21 | tkt = Ticket(self.env) |
|---|
| 22 | tkt['status'] = 'new' |
|---|
| 23 | tkt['reporter'] = msg['from'] |
|---|
| 24 | tkt['summary'] = msg['subject'] |
|---|
| 25 | for part in msg.walk(): |
|---|
| 26 | if part.get_content_type() == 'text/plain': |
|---|
| 27 | tkt['description'] = part.get_payload(decode=1).strip() |
|---|
| 28 | tkt.insert() |
|---|
| 29 | |
|---|
| 30 | ################################ |
|---|
| 31 | ## Parse the message |
|---|
| 32 | msg = email.message_from_file(sys.stdin) |
|---|
| 33 | msg_sub_is_reply = re.compile('^[rR][eE]:') |
|---|
| 34 | msg_sub_find_ticket_num = re.compile('#[0-9]+') |
|---|
| 35 | m1 = msg_sub_is_reply.match(msg['subject']) |
|---|
| 36 | m2 = msg_sub_find_ticket_num.search(msg['subject']) |
|---|
| 37 | tNum=None |
|---|
| 38 | dbMatch=2 |
|---|
| 39 | if m1: |
|---|
| 40 | if m2: |
|---|
| 41 | tNum=m2.group() |
|---|
| 42 | tNum=tNum[1:8] |
|---|
| 43 | if tNum: |
|---|
| 44 | db = env.get_db_cnx() |
|---|
| 45 | cursor = db.cursor() |
|---|
| 46 | cursor.execute("SELECT id FROM ticket WHERE id=" + tNum) |
|---|
| 47 | for id in cursor: |
|---|
| 48 | if id[0] == int(tNum): |
|---|
| 49 | dbMatch = 1 |
|---|
| 50 | |
|---|
| 51 | if dbMatch and tNum: |
|---|
| 52 | ### We want to add this e-mail message as a COMMENT |
|---|
| 53 | tkt = Ticket(env, int(tNum)) |
|---|
| 54 | msgSender=msg['From'] |
|---|
| 55 | msgBody="" |
|---|
| 56 | msgPart=0 |
|---|
| 57 | for part in msg.walk(): |
|---|
| 58 | if part.get_content_type() == 'text/plain': |
|---|
| 59 | msgPart=msgPart+1 |
|---|
| 60 | if msgPart == 1: |
|---|
| 61 | msgBody=part.get_payload(decode=1).strip() |
|---|
| 62 | else: |
|---|
| 63 | msgBody=msgBody + "\r\n" + part.get_payload(decode=1).strip() |
|---|
| 64 | cursor.execute('INSERT INTO ticket_change (\'ticket\',\'time\',\'author\',\'field\',\'oldvalue\',\'newvalue\') VALUES (\'%s\',\'%s\',\'%s\',\'comment\',\'\',\'%s\')' % (tNum, time.time(), msgSender, trac.util.sql_escape(msgBody))) |
|---|
| 65 | db.commit() |
|---|
| 66 | else: |
|---|
| 67 | ### we want to add this e-mail message as a NEW TICKET |
|---|
| 68 | tktparser = TicketEmailParser(env) |
|---|
| 69 | tktparser.parse(msg) |
|---|