[22] | 1 | #!@PYTHON@ |
---|
| 2 | # Copyright (C) 2002 |
---|
| 3 | # |
---|
| 4 | # This file is part of the email2trac utils |
---|
| 5 | # |
---|
| 6 | # This program is free software; you can redistribute it and/or modify it |
---|
| 7 | # under the terms of the GNU General Public License as published by the |
---|
| 8 | # Free Software Foundation; either version 2, or (at your option) any |
---|
| 9 | # later version. |
---|
| 10 | # |
---|
| 11 | # This program is distributed in the hope that it will be useful, |
---|
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 14 | # GNU General Public License for more details. |
---|
| 15 | # |
---|
| 16 | # You should have received a copy of the GNU General Public License |
---|
| 17 | # along with this program; if not, write to the Free Software |
---|
| 18 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
---|
| 19 | # |
---|
[80] | 20 | # For vi/emacs or other use tabstop=4 (vi: set ts=4) |
---|
| 21 | # |
---|
[22] | 22 | """ |
---|
[54] | 23 | email2trac.py -- Email tickets to Trac. |
---|
[22] | 24 | |
---|
| 25 | A simple MTA filter to create Trac tickets from inbound emails. |
---|
| 26 | |
---|
| 27 | Copyright 2005, Daniel Lundin <daniel@edgewall.com> |
---|
| 28 | Copyright 2005, Edgewall Software |
---|
| 29 | |
---|
| 30 | Changed By: Bas van der Vlies <basv@sara.nl> |
---|
| 31 | Date : 13 September 2005 |
---|
| 32 | Descr. : Added config file and command line options, spam level |
---|
| 33 | detection, reply address and mailto option. Unicode support |
---|
| 34 | |
---|
| 35 | Changed By: Walter de Jong <walter@sara.nl> |
---|
| 36 | Descr. : multipart-message code and trac attachments |
---|
| 37 | |
---|
| 38 | |
---|
| 39 | The scripts reads emails from stdin and inserts directly into a Trac database. |
---|
| 40 | MIME headers are mapped as follows: |
---|
| 41 | |
---|
| 42 | * From: => Reporter |
---|
| 43 | => CC (Optional via reply_address option) |
---|
| 44 | * Subject: => Summary |
---|
| 45 | * Body => Description |
---|
| 46 | * Component => Can be set to SPAM via spam_level option |
---|
| 47 | |
---|
| 48 | How to use |
---|
| 49 | ---------- |
---|
| 50 | * Create an config file: |
---|
[74] | 51 | [DEFAULT] # REQUIRED |
---|
| 52 | project : /data/trac/test # REQUIRED |
---|
| 53 | debug : 1 # OPTIONAL, if set print some DEBUG info |
---|
| 54 | spam_level : 4 # OPTIONAL, if set check for SPAM mail |
---|
| 55 | reply_address: 1 # OPTIONAL, if set then fill in ticket CC field |
---|
[87] | 56 | umask : 022 # OPTIONAL, if set then use this umask for creation of the attachments |
---|
[74] | 57 | mailto_link : 1 # OPTIONAL, if set then [mailto:<>] in description |
---|
[75] | 58 | mailto_cc : basv@sara.nl # OPTIONAL, use this address as CC in mailto line |
---|
[74] | 59 | ticket_update: 1 # OPTIONAL, if set then check if this is an update for a ticket |
---|
| 60 | trac_version : 0.8 # OPTIONAL, default is 0.9 |
---|
[22] | 61 | |
---|
| 62 | [jouvin] # OPTIONAL project declaration, if set both fields necessary |
---|
| 63 | project : /data/trac/jouvin # use -p|--project jouvin. |
---|
| 64 | |
---|
| 65 | * default config file is : /etc/email2trac.conf |
---|
| 66 | |
---|
| 67 | * Commandline opions: |
---|
| 68 | -h | --help |
---|
| 69 | -c <value> | --component=<value> |
---|
| 70 | -f <config file> | --file=<config file> |
---|
| 71 | -p <project name> | --project=<project name> |
---|
| 72 | |
---|
| 73 | SVN Info: |
---|
| 74 | $Id: email2trac.py.in 138 2006-12-01 14:15:59Z bas $ |
---|
| 75 | """ |
---|
| 76 | import os |
---|
| 77 | import sys |
---|
| 78 | import string |
---|
| 79 | import getopt |
---|
| 80 | import stat |
---|
| 81 | import time |
---|
| 82 | import email |
---|
[136] | 83 | import email.Iterators |
---|
| 84 | import email.Header |
---|
[22] | 85 | import re |
---|
| 86 | import urllib |
---|
| 87 | import unicodedata |
---|
| 88 | import ConfigParser |
---|
| 89 | from stat import * |
---|
| 90 | import mimetypes |
---|
[96] | 91 | import syslog |
---|
| 92 | import traceback |
---|
[22] | 93 | |
---|
[91] | 94 | |
---|
[96] | 95 | # Some global variables |
---|
| 96 | # |
---|
[22] | 97 | trac_default_version = 0.9 |
---|
[96] | 98 | m = None |
---|
[22] | 99 | |
---|
[96] | 100 | |
---|
[22] | 101 | class TicketEmailParser(object): |
---|
| 102 | env = None |
---|
| 103 | comment = '> ' |
---|
| 104 | |
---|
| 105 | def __init__(self, env, parameters, version): |
---|
| 106 | self.env = env |
---|
| 107 | |
---|
| 108 | # Database connection |
---|
| 109 | # |
---|
| 110 | self.db = None |
---|
| 111 | |
---|
[72] | 112 | # Some useful mail constants |
---|
| 113 | # |
---|
| 114 | self.author = None |
---|
| 115 | self.email_addr = None |
---|
| 116 | self.email_field = None |
---|
| 117 | |
---|
[22] | 118 | self.VERSION = version |
---|
[68] | 119 | if self.VERSION == 0.8: |
---|
| 120 | self.get_config = self.env.get_config |
---|
| 121 | else: |
---|
[22] | 122 | self.get_config = self.env.config.get |
---|
| 123 | |
---|
| 124 | if parameters.has_key('umask'): |
---|
| 125 | os.umask(int(parameters['umask'], 8)) |
---|
| 126 | |
---|
| 127 | if parameters.has_key('debug'): |
---|
| 128 | self.DEBUG = int(parameters['debug']) |
---|
| 129 | else: |
---|
| 130 | self.DEBUG = 0 |
---|
| 131 | |
---|
| 132 | if parameters.has_key('mailto_link'): |
---|
| 133 | self.MAILTO = int(parameters['mailto_link']) |
---|
[74] | 134 | if parameters.has_key('mailto_cc'): |
---|
| 135 | self.MAILTO_CC = parameters['mailto_cc'] |
---|
| 136 | else: |
---|
| 137 | self.MAILTO_CC = '' |
---|
[22] | 138 | else: |
---|
| 139 | self.MAILTO = 0 |
---|
| 140 | |
---|
| 141 | if parameters.has_key('spam_level'): |
---|
| 142 | self.SPAM_LEVEL = int(parameters['spam_level']) |
---|
| 143 | else: |
---|
| 144 | self.SPAM_LEVEL = 0 |
---|
| 145 | |
---|
| 146 | if parameters.has_key('email_comment'): |
---|
| 147 | self.comment = str(parameters['email_comment']) |
---|
| 148 | |
---|
| 149 | if parameters.has_key('email_header'): |
---|
| 150 | self.EMAIL_HEADER = int(parameters['email_header']) |
---|
| 151 | else: |
---|
| 152 | self.EMAIL_HEADER = 0 |
---|
| 153 | |
---|
[42] | 154 | if parameters.has_key('alternate_notify_template'): |
---|
| 155 | self.notify_template = str(parameters['alternate_notify_template']) |
---|
| 156 | else: |
---|
| 157 | self.notify_template = None |
---|
[22] | 158 | |
---|
[43] | 159 | if parameters.has_key('reply_all'): |
---|
| 160 | self.REPLY_ALL = int(parameters['reply_all']) |
---|
| 161 | else: |
---|
| 162 | self.REPLY_ALL = 0 |
---|
[42] | 163 | |
---|
[74] | 164 | if parameters.has_key('ticket_update'): |
---|
| 165 | self.TICKET_UPDATE = int(parameters['ticket_update']) |
---|
| 166 | else: |
---|
| 167 | self.TICKET_UPDATE = 0 |
---|
[43] | 168 | |
---|
[118] | 169 | if parameters.has_key('drop_spam'): |
---|
| 170 | self.DROP_SPAM = int(parameters['drop_spam']) |
---|
| 171 | else: |
---|
| 172 | self.DROP_SPAM = 0 |
---|
[74] | 173 | |
---|
[134] | 174 | if parameters.has_key('verbatim_format'): |
---|
| 175 | self.VERBATIM_FORMAT = int(parameters['verbatim_format']) |
---|
| 176 | else: |
---|
| 177 | self.VERBATIM_FORMAT = 1 |
---|
[118] | 178 | |
---|
[136] | 179 | if parameters.has_key('strip_signature'): |
---|
| 180 | self.STRIP_SIGNATURE = int(parameters['strip_signature']) |
---|
| 181 | else: |
---|
| 182 | self.STRIP_SIGNATURE = 0 |
---|
[134] | 183 | |
---|
[136] | 184 | |
---|
[22] | 185 | # X-Spam-Score: *** (3.255) BAYES_50,DNS_FROM_AHBL_RHSBL,HTML_ |
---|
| 186 | # Note if Spam_level then '*' are included |
---|
| 187 | def spam(self, message): |
---|
| 188 | if message.has_key('X-Spam-Score'): |
---|
| 189 | spam_l = string.split(message['X-Spam-Score']) |
---|
| 190 | number = spam_l[0].count('*') |
---|
| 191 | |
---|
| 192 | if number >= self.SPAM_LEVEL: |
---|
[41] | 193 | return 'Spam' |
---|
[22] | 194 | |
---|
[67] | 195 | elif message.has_key('X-Virus-found'): # treat virus mails as spam |
---|
| 196 | return 'Spam' |
---|
| 197 | |
---|
[41] | 198 | return self.get_config('ticket', 'default_component') |
---|
[22] | 199 | |
---|
| 200 | def to_unicode(self, str): |
---|
| 201 | """ |
---|
| 202 | Email has 7 bit ASCII code, convert it to unicode with the charset |
---|
[79] | 203 | that is encoded in 7-bit ASCII code and encode it as utf-8 so Trac |
---|
[22] | 204 | understands it. |
---|
| 205 | """ |
---|
[136] | 206 | results = email.Header.decode_header(str) |
---|
[22] | 207 | str = None |
---|
| 208 | for text,format in results: |
---|
| 209 | if format: |
---|
| 210 | try: |
---|
| 211 | temp = unicode(text, format) |
---|
[87] | 212 | except (UnicodeError,LookupError): |
---|
[22] | 213 | # This always works |
---|
| 214 | # |
---|
| 215 | temp = unicode(text, 'iso-8859-15') |
---|
| 216 | else: |
---|
| 217 | temp = string.strip(text) |
---|
[92] | 218 | temp = unicode(text, 'iso-8859-15') |
---|
[22] | 219 | |
---|
| 220 | if str: |
---|
[100] | 221 | str = '%s %s' %(str, temp) |
---|
[22] | 222 | else: |
---|
[100] | 223 | str = '%s' %temp |
---|
[22] | 224 | |
---|
[96] | 225 | str = str.encode('utf-8') |
---|
[22] | 226 | return str |
---|
| 227 | |
---|
| 228 | def debug_attachments(self, message): |
---|
| 229 | n = 0 |
---|
| 230 | for part in message.walk(): |
---|
| 231 | if part.get_content_maintype() == 'multipart': # multipart/* is just a container |
---|
| 232 | print 'TD: multipart container' |
---|
| 233 | continue |
---|
| 234 | |
---|
| 235 | n = n + 1 |
---|
| 236 | print 'TD: part%d: Content-Type: %s' % (n, part.get_content_type()) |
---|
| 237 | print 'TD: part%d: filename: %s' % (n, part.get_filename()) |
---|
| 238 | |
---|
| 239 | if part.is_multipart(): |
---|
| 240 | print 'TD: this part is multipart' |
---|
| 241 | payload = part.get_payload(decode=1) |
---|
| 242 | print 'TD: payload:', payload |
---|
| 243 | else: |
---|
| 244 | print 'TD: this part is not multipart' |
---|
| 245 | |
---|
| 246 | part_file = '/var/tmp/part%d' % n |
---|
| 247 | print 'TD: writing part%d (%s)' % (n,part_file) |
---|
| 248 | fx = open(part_file, 'wb') |
---|
| 249 | text = part.get_payload(decode=1) |
---|
| 250 | if not text: |
---|
| 251 | text = '(None)' |
---|
| 252 | fx.write(text) |
---|
| 253 | fx.close() |
---|
| 254 | try: |
---|
| 255 | os.chmod(part_file,S_IRWXU|S_IRWXG|S_IRWXO) |
---|
| 256 | except OSError: |
---|
| 257 | pass |
---|
| 258 | |
---|
| 259 | def email_header_txt(self, m): |
---|
[72] | 260 | """ |
---|
| 261 | Display To and CC addresses in description field |
---|
| 262 | """ |
---|
[22] | 263 | str = '' |
---|
| 264 | if m['To'] and len(m['To']) > 0 and m['To'] != 'hic@sara.nl': |
---|
| 265 | str = "'''To:''' %s [[BR]]" %(m['To']) |
---|
| 266 | if m['Cc'] and len(m['Cc']) > 0: |
---|
| 267 | str = "%s'''Cc:''' %s [[BR]]" % (str, m['Cc']) |
---|
| 268 | |
---|
[138] | 269 | #return self.to_unicode(str) |
---|
| 270 | return util.text.to_unicode(str) |
---|
[22] | 271 | |
---|
[138] | 272 | |
---|
[43] | 273 | def set_owner(self, ticket): |
---|
[45] | 274 | """ |
---|
| 275 | Select default owner for ticket component |
---|
| 276 | """ |
---|
[43] | 277 | cursor = self.db.cursor() |
---|
| 278 | sql = "SELECT owner FROM component WHERE name='%s'" % ticket['component'] |
---|
| 279 | cursor.execute(sql) |
---|
[61] | 280 | try: |
---|
| 281 | ticket['owner'] = cursor.fetchone()[0] |
---|
| 282 | except TypeError, detail: |
---|
| 283 | ticket['owner'] = "UNKNOWN" |
---|
[43] | 284 | |
---|
[72] | 285 | def get_author_emailaddrs(self, message): |
---|
[45] | 286 | """ |
---|
[72] | 287 | Get the default author name and email address from the message |
---|
[45] | 288 | """ |
---|
[72] | 289 | self.author, self.email_addr = email.Utils.parseaddr(message['from']) |
---|
[43] | 290 | |
---|
[45] | 291 | # Look for email address in registered trac users |
---|
| 292 | # |
---|
[68] | 293 | if self.VERSION == 0.8: |
---|
| 294 | users = [] |
---|
| 295 | else: |
---|
[45] | 296 | users = [ u for (u, n, e) in self.env.get_known_users(self.db) |
---|
[72] | 297 | if e == self.email_addr ] |
---|
[43] | 298 | |
---|
[45] | 299 | if len(users) == 1: |
---|
[72] | 300 | self.email_field = users[0] |
---|
[45] | 301 | else: |
---|
[138] | 302 | #self.email_field = self.to_unicode(message['from']) |
---|
| 303 | self.email_field = util.text.to_unicode(message['from']) |
---|
[45] | 304 | |
---|
[72] | 305 | def set_reply_fields(self, ticket, message): |
---|
| 306 | """ |
---|
| 307 | Set all the right fields for a new ticket |
---|
| 308 | """ |
---|
| 309 | ticket['reporter'] = self.email_field |
---|
| 310 | |
---|
[45] | 311 | # Put all CC-addresses in ticket CC field |
---|
[43] | 312 | # |
---|
| 313 | if self.REPLY_ALL: |
---|
[45] | 314 | #tos = message.get_all('to', []) |
---|
[43] | 315 | ccs = message.get_all('cc', []) |
---|
| 316 | |
---|
[45] | 317 | addrs = email.Utils.getaddresses(ccs) |
---|
[105] | 318 | if not addrs: |
---|
| 319 | return |
---|
[43] | 320 | |
---|
| 321 | # Remove reporter email address if notification is |
---|
| 322 | # on |
---|
| 323 | # |
---|
| 324 | if self.notification: |
---|
| 325 | try: |
---|
[72] | 326 | addrs.remove((self.author, self.email_addr)) |
---|
[43] | 327 | except ValueError, detail: |
---|
| 328 | pass |
---|
| 329 | |
---|
[45] | 330 | for name,mail in addrs: |
---|
[105] | 331 | try: |
---|
[108] | 332 | mail_list = '%s, %s' %(mail_list, mail) |
---|
[105] | 333 | except: |
---|
| 334 | mail_list = mail |
---|
[43] | 335 | |
---|
[105] | 336 | if mail_list: |
---|
[138] | 337 | #ticket['cc'] = self.to_unicode(mail_list) |
---|
| 338 | ticket['cc'] = util.text.to_unicode(mail_list) |
---|
[96] | 339 | |
---|
| 340 | def save_email_for_debug(self, message, tempfile=False): |
---|
| 341 | if tempfile: |
---|
| 342 | import tempfile |
---|
| 343 | msg_file = tempfile.mktemp('.email2trac') |
---|
| 344 | else: |
---|
| 345 | msg_file = '/var/tmp/msg.txt' |
---|
[44] | 346 | print 'TD: saving email to %s' % msg_file |
---|
| 347 | fx = open(msg_file, 'wb') |
---|
| 348 | fx.write('%s' % message) |
---|
| 349 | fx.close() |
---|
| 350 | try: |
---|
| 351 | os.chmod(msg_file,S_IRWXU|S_IRWXG|S_IRWXO) |
---|
| 352 | except OSError: |
---|
| 353 | pass |
---|
| 354 | |
---|
[71] | 355 | def ticket_update(self, m): |
---|
[78] | 356 | """ |
---|
[79] | 357 | If the current email is a reply to an existing ticket, this function |
---|
| 358 | will append the contents of this email to that ticket, instead of |
---|
| 359 | creating a new one. |
---|
[78] | 360 | """ |
---|
[71] | 361 | if not m['Subject']: |
---|
| 362 | return False |
---|
| 363 | else: |
---|
[138] | 364 | #subject = self.to_unicode(m['Subject']) |
---|
| 365 | subject = util.text.to_unicode(m['Subject']) |
---|
[71] | 366 | |
---|
| 367 | TICKET_RE = re.compile(r""" |
---|
| 368 | (?P<ticketnr>[#][0-9]+:) |
---|
| 369 | """, re.VERBOSE) |
---|
| 370 | |
---|
| 371 | result = TICKET_RE.search(subject) |
---|
| 372 | if not result: |
---|
| 373 | return False |
---|
| 374 | |
---|
[78] | 375 | body_text = self.get_body_text(m) |
---|
| 376 | |
---|
[72] | 377 | # Strip '#' and ':' from ticket_id |
---|
[75] | 378 | # |
---|
[71] | 379 | ticket_id = result.group('ticketnr') |
---|
| 380 | ticket_id = int(ticket_id[1:-1]) |
---|
| 381 | |
---|
[77] | 382 | # Get current time |
---|
| 383 | # |
---|
| 384 | when = int(time.time()) |
---|
| 385 | |
---|
[76] | 386 | if self.VERSION == 0.8: |
---|
[78] | 387 | tkt = Ticket(self.db, ticket_id) |
---|
[77] | 388 | tkt.save_changes(self.db, self.author, body_text, when) |
---|
[76] | 389 | else: |
---|
[126] | 390 | try: |
---|
| 391 | tkt = Ticket(self.env, ticket_id, self.db) |
---|
[131] | 392 | except util.TracError, detail: |
---|
[126] | 393 | return False |
---|
| 394 | |
---|
[77] | 395 | tkt.save_changes(self.author, body_text, when) |
---|
[86] | 396 | tkt['id'] = ticket_id |
---|
[76] | 397 | |
---|
[129] | 398 | if self.VERSION == 0.9: |
---|
| 399 | self.attachments(m, tkt, True) |
---|
| 400 | else: |
---|
| 401 | self.attachments(m, tkt) |
---|
[76] | 402 | |
---|
[72] | 403 | if self.notification: |
---|
[77] | 404 | self.notify(tkt, False, when) |
---|
[72] | 405 | |
---|
[71] | 406 | return True |
---|
| 407 | |
---|
[84] | 408 | def new_ticket(self, msg): |
---|
[77] | 409 | """ |
---|
| 410 | Create a new ticket |
---|
| 411 | """ |
---|
[41] | 412 | tkt = Ticket(self.env) |
---|
[22] | 413 | tkt['status'] = 'new' |
---|
| 414 | |
---|
| 415 | # Some defaults |
---|
| 416 | # |
---|
| 417 | tkt['milestone'] = self.get_config('ticket', 'default_milestone') |
---|
| 418 | tkt['priority'] = self.get_config('ticket', 'default_priority') |
---|
| 419 | tkt['severity'] = self.get_config('ticket', 'default_severity') |
---|
| 420 | tkt['version'] = self.get_config('ticket', 'default_version') |
---|
| 421 | |
---|
| 422 | if not msg['Subject']: |
---|
[96] | 423 | tkt['summary'] = u'(geen subject)' |
---|
[22] | 424 | else: |
---|
[138] | 425 | #tkt['summary'] = self.to_unicode(msg['Subject']) |
---|
| 426 | tkt['summary'] = util.text.to_unicode(msg['Subject']) |
---|
[22] | 427 | |
---|
| 428 | |
---|
[41] | 429 | if settings.has_key('component'): |
---|
[22] | 430 | tkt['component'] = settings['component'] |
---|
| 431 | else: |
---|
[41] | 432 | tkt['component'] = self.spam(msg) |
---|
[22] | 433 | |
---|
[118] | 434 | # Discard SPAM messages. |
---|
[38] | 435 | # |
---|
[122] | 436 | if self.DROP_SPAM and (tkt['component'] == 'Spam'): |
---|
[118] | 437 | # print 'This message is a SPAM. Automatic ticket insertion refused (SPAM level > %d' % self.SPAM_LEVEL |
---|
[124] | 438 | return False |
---|
[38] | 439 | |
---|
[43] | 440 | # Set default owner for component |
---|
[22] | 441 | # |
---|
[43] | 442 | self.set_owner(tkt) |
---|
[72] | 443 | self.set_reply_fields(tkt, msg) |
---|
[22] | 444 | |
---|
[45] | 445 | # produce e-mail like header |
---|
| 446 | # |
---|
[22] | 447 | head = '' |
---|
| 448 | if self.EMAIL_HEADER > 0: |
---|
| 449 | head = self.email_header_txt(msg) |
---|
[92] | 450 | |
---|
[22] | 451 | |
---|
[72] | 452 | body_text = self.get_body_text(msg) |
---|
[45] | 453 | |
---|
[109] | 454 | tkt['description'] = 'email2trac:%s\r\n%s' \ |
---|
[90] | 455 | %(head, body_text) |
---|
| 456 | |
---|
[77] | 457 | when = int(time.time()) |
---|
[68] | 458 | if self.VERSION == 0.8: |
---|
[90] | 459 | ticket_id = tkt.insert(self.db) |
---|
[68] | 460 | else: |
---|
[90] | 461 | ticket_id = tkt.insert() |
---|
[98] | 462 | tkt['id'] = ticket_id |
---|
[45] | 463 | |
---|
[90] | 464 | changed = False |
---|
| 465 | comment = '' |
---|
[77] | 466 | |
---|
[90] | 467 | # Rewrite the description if we have mailto enabled |
---|
[45] | 468 | # |
---|
[72] | 469 | if self.MAILTO: |
---|
[100] | 470 | changed = True |
---|
| 471 | comment = '\nadded mailto line\n' |
---|
[138] | 472 | #mailto = self.html_mailto_link(self.to_unicode(msg['subject']), ticket_id, body_text) |
---|
| 473 | mailto = self.html_mailto_link(util.text.to_unicode(msg['subject']), ticket_id, body_text) |
---|
[109] | 474 | #tkt['description'] = 'email2trac:%s%s\r\n{{{\r\n%s\r\n}}}' %(head, mailto, body_text) |
---|
| 475 | tkt['description'] = 'email2trac:%s%s\r\n%s' %(head, mailto, body_text) |
---|
[45] | 476 | |
---|
[90] | 477 | n = self.attachments(msg, tkt) |
---|
| 478 | if n: |
---|
[100] | 479 | changed = True |
---|
[90] | 480 | comment = '%s\nThis message has %d attachment(s)\n' %(comment, n) |
---|
[77] | 481 | |
---|
[90] | 482 | if changed: |
---|
| 483 | if self.VERSION == 0.8: |
---|
| 484 | tkt.save_changes(self.db, self.author, comment) |
---|
| 485 | else: |
---|
| 486 | tkt.save_changes(self.author, comment) |
---|
| 487 | |
---|
| 488 | #print tkt.get_changelog(self.db, when) |
---|
| 489 | |
---|
[45] | 490 | if self.notification: |
---|
[90] | 491 | self.notify(tkt, True) |
---|
| 492 | #self.notify(tkt, False) |
---|
[45] | 493 | |
---|
[77] | 494 | def parse(self, fp): |
---|
[96] | 495 | global m |
---|
| 496 | |
---|
[77] | 497 | m = email.message_from_file(fp) |
---|
| 498 | if not m: |
---|
| 499 | return |
---|
| 500 | |
---|
| 501 | if self.DEBUG > 1: # save the entire e-mail message text |
---|
| 502 | self.save_email_for_debug(m) |
---|
| 503 | self.debug_attachments(m) |
---|
| 504 | |
---|
| 505 | self.db = self.env.get_db_cnx() |
---|
| 506 | self.get_author_emailaddrs(m) |
---|
| 507 | |
---|
| 508 | if self.get_config('notification', 'smtp_enabled') in ['true']: |
---|
| 509 | self.notification = 1 |
---|
| 510 | else: |
---|
| 511 | self.notification = 0 |
---|
| 512 | |
---|
| 513 | # Must we update existing tickets |
---|
| 514 | # |
---|
| 515 | if self.TICKET_UPDATE > 0: |
---|
| 516 | if self.ticket_update(m): |
---|
| 517 | return True |
---|
| 518 | |
---|
[84] | 519 | self.new_ticket(m) |
---|
[77] | 520 | |
---|
[136] | 521 | def strip_signature(self, text): |
---|
| 522 | """ |
---|
| 523 | Strip signature from message, inspired by Mailman software |
---|
| 524 | """ |
---|
| 525 | body = [] |
---|
| 526 | for line in text.splitlines(): |
---|
| 527 | if line == '-- ': |
---|
| 528 | break |
---|
| 529 | body.append(line) |
---|
| 530 | |
---|
| 531 | return ('\n'.join(body)) |
---|
| 532 | |
---|
[72] | 533 | def get_body_text(self, msg): |
---|
[45] | 534 | """ |
---|
[79] | 535 | put the message text in the ticket description or in the changes field. |
---|
[45] | 536 | message text can be plain text or html or something else |
---|
| 537 | """ |
---|
[22] | 538 | has_description = 0 |
---|
[100] | 539 | encoding = True |
---|
[109] | 540 | ubody_text = u'No plain text message' |
---|
[22] | 541 | for part in msg.walk(): |
---|
[45] | 542 | |
---|
| 543 | # 'multipart/*' is a container for multipart messages |
---|
| 544 | # |
---|
| 545 | if part.get_content_maintype() == 'multipart': |
---|
[22] | 546 | continue |
---|
| 547 | |
---|
| 548 | if part.get_content_type() == 'text/plain': |
---|
[45] | 549 | # Try to decode, if fails then do not decode |
---|
| 550 | # |
---|
[90] | 551 | body_text = part.get_payload(decode=1) |
---|
[45] | 552 | if not body_text: |
---|
[90] | 553 | body_text = part.get_payload(decode=0) |
---|
[136] | 554 | |
---|
| 555 | if self.STRIP_SIGNATURE: |
---|
| 556 | body_text = self.strip_signature(body_text) |
---|
[22] | 557 | |
---|
[45] | 558 | # Get contents charset (iso-8859-15 if not defined in mail headers) |
---|
| 559 | # |
---|
[100] | 560 | charset = part.get_content_charset() |
---|
[102] | 561 | if not charset: |
---|
| 562 | charset = 'iso-8859-15' |
---|
| 563 | |
---|
[89] | 564 | try: |
---|
[96] | 565 | ubody_text = unicode(body_text, charset) |
---|
[100] | 566 | |
---|
| 567 | except UnicodeError, detail: |
---|
[96] | 568 | ubody_text = unicode(body_text, 'iso-8859-15') |
---|
[89] | 569 | |
---|
[100] | 570 | except LookupError, detail: |
---|
| 571 | ubody_text = body_text |
---|
| 572 | encoding = False |
---|
| 573 | |
---|
[22] | 574 | elif part.get_content_type() == 'text/html': |
---|
[109] | 575 | ubody_text = '(see attachment for HTML mail message)' |
---|
[22] | 576 | |
---|
| 577 | else: |
---|
[109] | 578 | ubody_text = '(see attachment for message)' |
---|
[22] | 579 | |
---|
| 580 | has_description = 1 |
---|
| 581 | break # we have the description, so break |
---|
| 582 | |
---|
| 583 | if not has_description: |
---|
[109] | 584 | ubody_text = '(see attachment for message)' |
---|
[22] | 585 | |
---|
[100] | 586 | # A patch so that the web-interface will not update the description |
---|
| 587 | # field of a ticket |
---|
| 588 | # |
---|
| 589 | ubody_text = ('\r\n'.join(ubody_text.splitlines())) |
---|
[22] | 590 | |
---|
[100] | 591 | # If we can unicode it try to encode it for trac |
---|
| 592 | # else we a lot of garbage |
---|
| 593 | # |
---|
| 594 | if encoding: |
---|
| 595 | ubody_text = ubody_text.encode('utf-8') |
---|
| 596 | |
---|
[134] | 597 | if self.VERBATIM_FORMAT: |
---|
| 598 | ubody_text = '{{{\r\n%s\r\n}}}' %ubody_text |
---|
| 599 | else: |
---|
| 600 | ubody_text = '%s' %ubody_text |
---|
| 601 | |
---|
[100] | 602 | return ubody_text |
---|
| 603 | |
---|
[77] | 604 | def notify(self, tkt , new=True, modtime=0): |
---|
[79] | 605 | """ |
---|
| 606 | A wrapper for the TRAC notify function. So we can use templates |
---|
| 607 | """ |
---|
[112] | 608 | if tkt['component'] == 'Spam': |
---|
| 609 | return |
---|
| 610 | |
---|
[41] | 611 | try: |
---|
| 612 | # create false {abs_}href properties, to trick Notify() |
---|
| 613 | # |
---|
| 614 | self.env.abs_href = Href(self.get_config('project', 'url')) |
---|
| 615 | self.env.href = Href(self.get_config('project', 'url')) |
---|
[22] | 616 | |
---|
[41] | 617 | tn = TicketNotifyEmail(self.env) |
---|
[42] | 618 | if self.notify_template: |
---|
| 619 | tn.template_name = self.notify_template; |
---|
| 620 | |
---|
[77] | 621 | tn.notify(tkt, new, modtime) |
---|
[41] | 622 | |
---|
| 623 | except Exception, e: |
---|
[79] | 624 | print 'TD: Failure sending notification on creation of ticket #%s: %s' %(tkt['id'], e) |
---|
[41] | 625 | |
---|
[22] | 626 | def mail_line(self, str): |
---|
| 627 | return '%s %s' % (self.comment, str) |
---|
| 628 | |
---|
| 629 | |
---|
[72] | 630 | def html_mailto_link(self, subject, id, body): |
---|
| 631 | if not self.author: |
---|
[88] | 632 | author = self.email_addr |
---|
[22] | 633 | else: |
---|
[138] | 634 | #author = self.to_unicode(self.author) |
---|
| 635 | author = util.text.to_unicode(self.author) |
---|
[22] | 636 | |
---|
| 637 | # Must find a fix |
---|
| 638 | # |
---|
| 639 | #arr = string.split(body, '\n') |
---|
| 640 | #arr = map(self.mail_line, arr) |
---|
| 641 | #body = string.join(arr, '\n') |
---|
| 642 | #body = '%s wrote:\n%s' %(author, body) |
---|
| 643 | |
---|
| 644 | # Temporary fix |
---|
[74] | 645 | str = 'mailto:%s?Subject=%s&Cc=%s' %( |
---|
| 646 | urllib.quote(self.email_addr), |
---|
| 647 | urllib.quote('Re: #%s: %s' %(id, subject)), |
---|
| 648 | urllib.quote(self.MAILTO_CC) |
---|
| 649 | ) |
---|
| 650 | |
---|
[90] | 651 | str = '\r\n{{{\r\n#!html\r\n<a href="%s">Reply to: %s</a>\r\n}}}\r\n' %(str, author) |
---|
[22] | 652 | |
---|
| 653 | return str |
---|
| 654 | |
---|
[129] | 655 | def attachments(self, message, ticket, update=False): |
---|
[79] | 656 | ''' |
---|
| 657 | save any attachments as files in the ticket's directory |
---|
| 658 | ''' |
---|
[22] | 659 | count = 0 |
---|
[77] | 660 | first = 0 |
---|
| 661 | number = 0 |
---|
[22] | 662 | for part in message.walk(): |
---|
| 663 | if part.get_content_maintype() == 'multipart': # multipart/* is just a container |
---|
| 664 | continue |
---|
| 665 | |
---|
| 666 | if not first: # first content is the message |
---|
| 667 | first = 1 |
---|
| 668 | if part.get_content_type() == 'text/plain': # if first is text, is was already put in the description |
---|
| 669 | continue |
---|
| 670 | |
---|
| 671 | filename = part.get_filename() |
---|
[77] | 672 | count = count + 1 |
---|
[22] | 673 | if not filename: |
---|
[77] | 674 | number = number + 1 |
---|
| 675 | filename = 'part%04d' % number |
---|
[22] | 676 | |
---|
[72] | 677 | ext = mimetypes.guess_extension(part.get_content_type()) |
---|
[22] | 678 | if not ext: |
---|
| 679 | ext = '.bin' |
---|
| 680 | |
---|
| 681 | filename = '%s%s' % (filename, ext) |
---|
| 682 | else: |
---|
[138] | 683 | #filename = self.to_unicode(filename) |
---|
| 684 | filename = util.text.to_unicode(filename) |
---|
[22] | 685 | |
---|
[48] | 686 | # From the trac code |
---|
| 687 | # |
---|
| 688 | filename = filename.replace('\\', '/').replace(':', '/') |
---|
| 689 | filename = os.path.basename(filename) |
---|
[22] | 690 | |
---|
[48] | 691 | # We try to normalize the filename to utf-8 NFC if we can. |
---|
| 692 | # Files uploaded from OS X might be in NFD. |
---|
[92] | 693 | # Check python version and then try it |
---|
[48] | 694 | # |
---|
| 695 | if sys.version_info[0] > 2 or (sys.version_info[0] == 2 and sys.version_info[1] >= 3): |
---|
[92] | 696 | try: |
---|
| 697 | filename = unicodedata.normalize('NFC', unicode(filename, 'utf-8')).encode('utf-8') |
---|
| 698 | except TypeError: |
---|
| 699 | pass |
---|
[48] | 700 | |
---|
[22] | 701 | url_filename = urllib.quote(filename) |
---|
[68] | 702 | if self.VERSION == 0.8: |
---|
[22] | 703 | dir = os.path.join(self.env.get_attachments_dir(), 'ticket', |
---|
| 704 | urllib.quote(str(ticket['id']))) |
---|
| 705 | if not os.path.exists(dir): |
---|
| 706 | mkdir_p(dir, 0755) |
---|
[68] | 707 | else: |
---|
| 708 | dir = '/tmp' |
---|
[22] | 709 | |
---|
[48] | 710 | path, fd = util.create_unique_file(os.path.join(dir, url_filename)) |
---|
[22] | 711 | text = part.get_payload(decode=1) |
---|
| 712 | if not text: |
---|
| 713 | text = '(None)' |
---|
[48] | 714 | fd.write(text) |
---|
| 715 | fd.close() |
---|
[22] | 716 | |
---|
| 717 | # get the filesize |
---|
| 718 | # |
---|
[48] | 719 | stats = os.lstat(path) |
---|
[22] | 720 | filesize = stats[stat.ST_SIZE] |
---|
| 721 | |
---|
| 722 | # Insert the attachment it differs for the different TRAC versions |
---|
[73] | 723 | # |
---|
[68] | 724 | if self.VERSION == 0.8: |
---|
[22] | 725 | cursor = self.db.cursor() |
---|
[73] | 726 | try: |
---|
| 727 | cursor.execute('INSERT INTO attachment VALUES("%s","%s","%s",%d,%d,"%s","%s","%s")' |
---|
| 728 | %('ticket', urllib.quote(str(ticket['id'])), filename + '?format=raw', filesize, |
---|
| 729 | int(time.time()),'', self.author, 'e-mail') ) |
---|
| 730 | |
---|
| 731 | # Attachment is already known |
---|
| 732 | # |
---|
| 733 | except sqlite.IntegrityError: |
---|
[84] | 734 | #self.db.close() |
---|
| 735 | return count |
---|
[73] | 736 | |
---|
[22] | 737 | self.db.commit() |
---|
[73] | 738 | |
---|
[68] | 739 | else: |
---|
| 740 | fd = open(path) |
---|
| 741 | att = attachment.Attachment(self.env, 'ticket', ticket['id']) |
---|
[129] | 742 | |
---|
| 743 | # This will break the ticket_update system, the body_text is vaporized |
---|
| 744 | # ;-( |
---|
| 745 | # |
---|
| 746 | if not update: |
---|
| 747 | att.author = self.author |
---|
[138] | 748 | #att.description = self.to_unicode('Added by email2trac') |
---|
| 749 | att.description = util.text.to_unicode('Added by email2trac') |
---|
[129] | 750 | |
---|
[68] | 751 | att.insert(url_filename, fd, filesize) |
---|
| 752 | fd.close() |
---|
[22] | 753 | |
---|
[103] | 754 | # Remove the created temporary filename |
---|
| 755 | # |
---|
| 756 | os.unlink(path) |
---|
| 757 | |
---|
[77] | 758 | # Return how many attachments |
---|
| 759 | # |
---|
| 760 | return count |
---|
[22] | 761 | |
---|
[77] | 762 | |
---|
[22] | 763 | def mkdir_p(dir, mode): |
---|
| 764 | '''do a mkdir -p''' |
---|
| 765 | |
---|
| 766 | arr = string.split(dir, '/') |
---|
| 767 | path = '' |
---|
| 768 | for part in arr: |
---|
| 769 | path = '%s/%s' % (path, part) |
---|
| 770 | try: |
---|
| 771 | stats = os.stat(path) |
---|
| 772 | except OSError: |
---|
| 773 | os.mkdir(path, mode) |
---|
| 774 | |
---|
| 775 | |
---|
| 776 | def ReadConfig(file, name): |
---|
| 777 | """ |
---|
| 778 | Parse the config file |
---|
| 779 | """ |
---|
| 780 | |
---|
| 781 | if not os.path.isfile(file): |
---|
[79] | 782 | print 'File %s does not exist' %file |
---|
[22] | 783 | sys.exit(1) |
---|
| 784 | |
---|
| 785 | config = ConfigParser.ConfigParser() |
---|
| 786 | try: |
---|
| 787 | config.read(file) |
---|
| 788 | except ConfigParser.MissingSectionHeaderError,detail: |
---|
| 789 | print detail |
---|
| 790 | sys.exit(1) |
---|
| 791 | |
---|
| 792 | |
---|
| 793 | # Use given project name else use defaults |
---|
| 794 | # |
---|
| 795 | if name: |
---|
| 796 | if not config.has_section(name): |
---|
[79] | 797 | print "Not a valid project name: %s" %name |
---|
[22] | 798 | print "Valid names: %s" %config.sections() |
---|
| 799 | sys.exit(1) |
---|
| 800 | |
---|
| 801 | project = dict() |
---|
| 802 | for option in config.options(name): |
---|
| 803 | project[option] = config.get(name, option) |
---|
| 804 | |
---|
| 805 | else: |
---|
| 806 | project = config.defaults() |
---|
| 807 | |
---|
| 808 | return project |
---|
| 809 | |
---|
[87] | 810 | |
---|
[22] | 811 | if __name__ == '__main__': |
---|
| 812 | # Default config file |
---|
| 813 | # |
---|
[24] | 814 | configfile = '@email2trac_conf@' |
---|
[22] | 815 | project = '' |
---|
| 816 | component = '' |
---|
[87] | 817 | ENABLE_SYSLOG = 0 |
---|
| 818 | |
---|
[22] | 819 | try: |
---|
| 820 | opts, args = getopt.getopt(sys.argv[1:], 'chf:p:', ['component=','help', 'file=', 'project=']) |
---|
| 821 | except getopt.error,detail: |
---|
| 822 | print __doc__ |
---|
| 823 | print detail |
---|
| 824 | sys.exit(1) |
---|
[87] | 825 | |
---|
[22] | 826 | project_name = None |
---|
| 827 | for opt,value in opts: |
---|
| 828 | if opt in [ '-h', '--help']: |
---|
| 829 | print __doc__ |
---|
| 830 | sys.exit(0) |
---|
| 831 | elif opt in ['-c', '--component']: |
---|
| 832 | component = value |
---|
| 833 | elif opt in ['-f', '--file']: |
---|
| 834 | configfile = value |
---|
| 835 | elif opt in ['-p', '--project']: |
---|
| 836 | project_name = value |
---|
[87] | 837 | |
---|
[22] | 838 | settings = ReadConfig(configfile, project_name) |
---|
| 839 | if not settings.has_key('project'): |
---|
| 840 | print __doc__ |
---|
[79] | 841 | print 'No Trac project is defined in the email2trac config file.' |
---|
[22] | 842 | sys.exit(1) |
---|
[87] | 843 | |
---|
[22] | 844 | if component: |
---|
| 845 | settings['component'] = component |
---|
[87] | 846 | |
---|
[22] | 847 | if settings.has_key('trac_version'): |
---|
| 848 | version = float(settings['trac_version']) |
---|
| 849 | else: |
---|
| 850 | version = trac_default_version |
---|
| 851 | |
---|
[87] | 852 | if settings.has_key('enable_syslog'): |
---|
| 853 | ENABLE_SYSLOG = float(settings['enable_syslog']) |
---|
| 854 | |
---|
[22] | 855 | #debug HvB |
---|
| 856 | #print settings |
---|
[87] | 857 | |
---|
| 858 | try: |
---|
| 859 | if version == 0.8: |
---|
| 860 | from trac.Environment import Environment |
---|
| 861 | from trac.Ticket import Ticket |
---|
| 862 | from trac.Notify import TicketNotifyEmail |
---|
| 863 | from trac.Href import Href |
---|
| 864 | from trac import util |
---|
| 865 | import sqlite |
---|
| 866 | elif version == 0.9: |
---|
| 867 | from trac import attachment |
---|
| 868 | from trac.env import Environment |
---|
| 869 | from trac.ticket import Ticket |
---|
| 870 | from trac.web.href import Href |
---|
| 871 | from trac import util |
---|
| 872 | from trac.Notify import TicketNotifyEmail |
---|
| 873 | elif version == 0.10: |
---|
| 874 | from trac import attachment |
---|
| 875 | from trac.env import Environment |
---|
| 876 | from trac.ticket import Ticket |
---|
| 877 | from trac.web.href import Href |
---|
| 878 | from trac import util |
---|
| 879 | # see http://projects.edgewall.com/trac/changeset/2799 |
---|
| 880 | from trac.ticket.notification import TicketNotifyEmail |
---|
| 881 | |
---|
| 882 | env = Environment(settings['project'], create=0) |
---|
| 883 | tktparser = TicketEmailParser(env, settings, version) |
---|
| 884 | tktparser.parse(sys.stdin) |
---|
[22] | 885 | |
---|
[87] | 886 | # Catch all errors ans log to SYSLOG if we have enabled this |
---|
| 887 | # else stdout |
---|
| 888 | # |
---|
| 889 | except Exception, error: |
---|
| 890 | if ENABLE_SYSLOG: |
---|
| 891 | syslog.openlog('email2trac', syslog.LOG_NOWAIT) |
---|
| 892 | etype, evalue, etb = sys.exc_info() |
---|
| 893 | for e in traceback.format_exception(etype, evalue, etb): |
---|
| 894 | syslog.syslog(e) |
---|
| 895 | syslog.closelog() |
---|
| 896 | else: |
---|
| 897 | traceback.print_exc() |
---|
[22] | 898 | |
---|
[97] | 899 | if m: |
---|
[98] | 900 | tktparser.save_email_for_debug(m, True) |
---|
[97] | 901 | |
---|
[22] | 902 | # EOB |
---|