[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 |
---|
[152] | 43 | => CC (Optional via reply_all option) |
---|
[22] | 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 |
---|
[152] | 55 | reply_all : 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 |
---|
[172] | 60 | trac_version : 0.9 # OPTIONAL, default is 0.10 |
---|
[22] | 61 | |
---|
[148] | 62 | [jouvin] # OPTIONAL project declaration, if set both fields necessary |
---|
[22] | 63 | project : /data/trac/jouvin # use -p|--project jouvin. |
---|
| 64 | |
---|
| 65 | * default config file is : /etc/email2trac.conf |
---|
| 66 | |
---|
| 67 | * Commandline opions: |
---|
[205] | 68 | -h,--help |
---|
| 69 | -f,--file <configuration file> |
---|
| 70 | -n,--dry-run |
---|
| 71 | -p, --project <project name> |
---|
| 72 | -t, --ticket_prefix <name> |
---|
[22] | 73 | |
---|
| 74 | SVN Info: |
---|
| 75 | $Id: email2trac.py.in 237 2008-12-05 05:10:15Z bromine $ |
---|
| 76 | """ |
---|
| 77 | import os |
---|
| 78 | import sys |
---|
| 79 | import string |
---|
| 80 | import getopt |
---|
| 81 | import stat |
---|
| 82 | import time |
---|
| 83 | import email |
---|
[136] | 84 | import email.Iterators |
---|
| 85 | import email.Header |
---|
[22] | 86 | import re |
---|
| 87 | import urllib |
---|
| 88 | import unicodedata |
---|
| 89 | from stat import * |
---|
| 90 | import mimetypes |
---|
[96] | 91 | import traceback |
---|
[22] | 92 | |
---|
[190] | 93 | |
---|
| 94 | # Will fail where unavailable, e.g. Windows |
---|
| 95 | # |
---|
| 96 | try: |
---|
| 97 | import syslog |
---|
| 98 | SYSLOG_AVAILABLE = True |
---|
| 99 | except ImportError: |
---|
| 100 | SYSLOG_AVAILABLE = False |
---|
| 101 | |
---|
[182] | 102 | from datetime import tzinfo, timedelta, datetime |
---|
[199] | 103 | from trac import config as trac_config |
---|
[91] | 104 | |
---|
[96] | 105 | # Some global variables |
---|
| 106 | # |
---|
[189] | 107 | trac_default_version = '0.10' |
---|
[96] | 108 | m = None |
---|
[22] | 109 | |
---|
[182] | 110 | # A UTC class needed for trac version 0.11, added by |
---|
| 111 | # tbaschak at ktc dot mb dot ca |
---|
| 112 | # |
---|
| 113 | class UTC(tzinfo): |
---|
| 114 | """UTC""" |
---|
| 115 | ZERO = timedelta(0) |
---|
| 116 | HOUR = timedelta(hours=1) |
---|
| 117 | |
---|
| 118 | def utcoffset(self, dt): |
---|
| 119 | return self.ZERO |
---|
| 120 | |
---|
| 121 | def tzname(self, dt): |
---|
| 122 | return "UTC" |
---|
| 123 | |
---|
| 124 | def dst(self, dt): |
---|
| 125 | return self.ZERO |
---|
| 126 | |
---|
| 127 | |
---|
[22] | 128 | class TicketEmailParser(object): |
---|
| 129 | env = None |
---|
| 130 | comment = '> ' |
---|
| 131 | |
---|
[206] | 132 | def __init__(self, env, parameters, version): |
---|
[22] | 133 | self.env = env |
---|
| 134 | |
---|
| 135 | # Database connection |
---|
| 136 | # |
---|
| 137 | self.db = None |
---|
| 138 | |
---|
[206] | 139 | # Save parameters |
---|
| 140 | # |
---|
| 141 | self.parameters = parameters |
---|
| 142 | |
---|
[72] | 143 | # Some useful mail constants |
---|
| 144 | # |
---|
| 145 | self.author = None |
---|
| 146 | self.email_addr = None |
---|
[183] | 147 | self.email_from = None |
---|
[72] | 148 | |
---|
[22] | 149 | self.VERSION = version |
---|
[206] | 150 | self.DRY_RUN = parameters['dry_run'] |
---|
[204] | 151 | |
---|
[172] | 152 | self.get_config = self.env.config.get |
---|
[22] | 153 | |
---|
| 154 | if parameters.has_key('umask'): |
---|
| 155 | os.umask(int(parameters['umask'], 8)) |
---|
| 156 | |
---|
[236] | 157 | if parameters.has_key('quote_attachment_filenames'): |
---|
| 158 | self.QUOTE_ATTACHMENT_FILENAMES = int(parameters['quote_attachment_filenames']) |
---|
| 159 | else: |
---|
| 160 | self.QUOTE_ATTACHMENT_FILENAMES = 1 |
---|
| 161 | |
---|
[22] | 162 | if parameters.has_key('debug'): |
---|
| 163 | self.DEBUG = int(parameters['debug']) |
---|
| 164 | else: |
---|
| 165 | self.DEBUG = 0 |
---|
| 166 | |
---|
| 167 | if parameters.has_key('mailto_link'): |
---|
| 168 | self.MAILTO = int(parameters['mailto_link']) |
---|
[74] | 169 | if parameters.has_key('mailto_cc'): |
---|
| 170 | self.MAILTO_CC = parameters['mailto_cc'] |
---|
| 171 | else: |
---|
| 172 | self.MAILTO_CC = '' |
---|
[22] | 173 | else: |
---|
| 174 | self.MAILTO = 0 |
---|
| 175 | |
---|
| 176 | if parameters.has_key('spam_level'): |
---|
| 177 | self.SPAM_LEVEL = int(parameters['spam_level']) |
---|
| 178 | else: |
---|
| 179 | self.SPAM_LEVEL = 0 |
---|
| 180 | |
---|
[207] | 181 | if parameters.has_key('spam_header'): |
---|
| 182 | self.SPAM_HEADER = parameters['spam_header'] |
---|
| 183 | else: |
---|
| 184 | self.SPAM_HEADER = 'X-Spam-Score' |
---|
| 185 | |
---|
[191] | 186 | if parameters.has_key('email_quote'): |
---|
| 187 | self.EMAIL_QUOTE = str(parameters['email_quote']) |
---|
| 188 | else: |
---|
| 189 | self.EMAIL_QUOTE = '> ' |
---|
[22] | 190 | |
---|
| 191 | if parameters.has_key('email_header'): |
---|
| 192 | self.EMAIL_HEADER = int(parameters['email_header']) |
---|
| 193 | else: |
---|
| 194 | self.EMAIL_HEADER = 0 |
---|
| 195 | |
---|
[42] | 196 | if parameters.has_key('alternate_notify_template'): |
---|
| 197 | self.notify_template = str(parameters['alternate_notify_template']) |
---|
| 198 | else: |
---|
| 199 | self.notify_template = None |
---|
[22] | 200 | |
---|
[222] | 201 | if parameters.has_key('alternate_notify_template_update'): |
---|
| 202 | self.notify_template_update = str(parameters['alternate_notify_template_update']) |
---|
| 203 | else: |
---|
| 204 | self.notify_template_update = None |
---|
| 205 | |
---|
[43] | 206 | if parameters.has_key('reply_all'): |
---|
| 207 | self.REPLY_ALL = int(parameters['reply_all']) |
---|
| 208 | else: |
---|
| 209 | self.REPLY_ALL = 0 |
---|
[42] | 210 | |
---|
[74] | 211 | if parameters.has_key('ticket_update'): |
---|
| 212 | self.TICKET_UPDATE = int(parameters['ticket_update']) |
---|
| 213 | else: |
---|
| 214 | self.TICKET_UPDATE = 0 |
---|
[43] | 215 | |
---|
[118] | 216 | if parameters.has_key('drop_spam'): |
---|
| 217 | self.DROP_SPAM = int(parameters['drop_spam']) |
---|
| 218 | else: |
---|
| 219 | self.DROP_SPAM = 0 |
---|
[74] | 220 | |
---|
[134] | 221 | if parameters.has_key('verbatim_format'): |
---|
| 222 | self.VERBATIM_FORMAT = int(parameters['verbatim_format']) |
---|
| 223 | else: |
---|
| 224 | self.VERBATIM_FORMAT = 1 |
---|
[118] | 225 | |
---|
[231] | 226 | if parameters.has_key('reflow'): |
---|
| 227 | self.REFLOW = int(parameter['reflow']) |
---|
| 228 | else: |
---|
| 229 | self.REFLOW = 1 |
---|
| 230 | |
---|
[136] | 231 | if parameters.has_key('strip_signature'): |
---|
| 232 | self.STRIP_SIGNATURE = int(parameters['strip_signature']) |
---|
| 233 | else: |
---|
| 234 | self.STRIP_SIGNATURE = 0 |
---|
[134] | 235 | |
---|
[191] | 236 | if parameters.has_key('strip_quotes'): |
---|
| 237 | self.STRIP_QUOTES = int(parameters['strip_quotes']) |
---|
| 238 | else: |
---|
| 239 | self.STRIP_QUOTES = 0 |
---|
| 240 | |
---|
[148] | 241 | if parameters.has_key('use_textwrap'): |
---|
| 242 | self.USE_TEXTWRAP = int(parameters['use_textwrap']) |
---|
| 243 | else: |
---|
| 244 | self.USE_TEXTWRAP = 0 |
---|
| 245 | |
---|
[163] | 246 | if parameters.has_key('python_egg_cache'): |
---|
| 247 | self.python_egg_cache = str(parameters['python_egg_cache']) |
---|
| 248 | os.environ['PYTHON_EGG_CACHE'] = self.python_egg_cache |
---|
| 249 | |
---|
[173] | 250 | # Use OS independend functions |
---|
| 251 | # |
---|
| 252 | self.TMPDIR = os.path.normcase('/tmp') |
---|
| 253 | if parameters.has_key('tmpdir'): |
---|
| 254 | self.TMPDIR = os.path.normcase(str(parameters['tmpdir'])) |
---|
| 255 | |
---|
[194] | 256 | if parameters.has_key('ignore_trac_user_settings'): |
---|
| 257 | self.IGNORE_TRAC_USER_SETTINGS = int(parameters['ignore_trac_user_settings']) |
---|
| 258 | else: |
---|
| 259 | self.IGNORE_TRAC_USER_SETTINGS = 0 |
---|
[191] | 260 | |
---|
[22] | 261 | def spam(self, message): |
---|
[191] | 262 | """ |
---|
| 263 | # X-Spam-Score: *** (3.255) BAYES_50,DNS_FROM_AHBL_RHSBL,HTML_ |
---|
| 264 | # Note if Spam_level then '*' are included |
---|
| 265 | """ |
---|
[194] | 266 | spam = False |
---|
[207] | 267 | if message.has_key(self.SPAM_HEADER): |
---|
| 268 | spam_l = string.split(message[self.SPAM_HEADER]) |
---|
[22] | 269 | |
---|
[207] | 270 | try: |
---|
| 271 | number = spam_l[0].count('*') |
---|
| 272 | except IndexError, detail: |
---|
| 273 | number = 0 |
---|
| 274 | |
---|
[22] | 275 | if number >= self.SPAM_LEVEL: |
---|
[194] | 276 | spam = True |
---|
| 277 | |
---|
[191] | 278 | # treat virus mails as spam |
---|
| 279 | # |
---|
| 280 | elif message.has_key('X-Virus-found'): |
---|
[194] | 281 | spam = True |
---|
| 282 | |
---|
| 283 | # How to handle SPAM messages |
---|
| 284 | # |
---|
| 285 | if self.DROP_SPAM and spam: |
---|
| 286 | if self.DEBUG > 2 : |
---|
| 287 | print 'This message is a SPAM. Automatic ticket insertion refused (SPAM level > %d' % self.SPAM_LEVEL |
---|
| 288 | |
---|
[204] | 289 | return 'drop' |
---|
[194] | 290 | |
---|
| 291 | elif spam: |
---|
| 292 | |
---|
[204] | 293 | return 'Spam' |
---|
[67] | 294 | |
---|
[194] | 295 | else: |
---|
[22] | 296 | |
---|
[204] | 297 | return False |
---|
[191] | 298 | |
---|
[221] | 299 | def email_header_acl(self, keyword, header_field, default): |
---|
[206] | 300 | """ |
---|
[221] | 301 | This function wil check if the email address is allowed or denied |
---|
| 302 | to send mail to the ticket list |
---|
| 303 | """ |
---|
[206] | 304 | try: |
---|
[221] | 305 | mail_addresses = self.parameters[keyword] |
---|
| 306 | |
---|
| 307 | # Check if we have an empty string |
---|
| 308 | # |
---|
| 309 | if not mail_addresses: |
---|
| 310 | return default |
---|
| 311 | |
---|
[206] | 312 | except KeyError, detail: |
---|
[221] | 313 | if self.DEBUG > 2 : |
---|
| 314 | print '%s not defined, all messages are allowed.' %(keyword) |
---|
[206] | 315 | |
---|
[221] | 316 | return default |
---|
[206] | 317 | |
---|
[221] | 318 | mail_addresses = string.split(mail_addresses, ',') |
---|
| 319 | |
---|
| 320 | for entry in mail_addresses: |
---|
[209] | 321 | entry = entry.strip() |
---|
[221] | 322 | TO_RE = re.compile(entry, re.VERBOSE|re.IGNORECASE) |
---|
| 323 | result = TO_RE.search(header_field) |
---|
[208] | 324 | if result: |
---|
| 325 | return True |
---|
[149] | 326 | |
---|
[208] | 327 | return False |
---|
| 328 | |
---|
[139] | 329 | def email_to_unicode(self, message_str): |
---|
[22] | 330 | """ |
---|
| 331 | Email has 7 bit ASCII code, convert it to unicode with the charset |
---|
[79] | 332 | that is encoded in 7-bit ASCII code and encode it as utf-8 so Trac |
---|
[22] | 333 | understands it. |
---|
| 334 | """ |
---|
[139] | 335 | results = email.Header.decode_header(message_str) |
---|
[22] | 336 | str = None |
---|
| 337 | for text,format in results: |
---|
| 338 | if format: |
---|
| 339 | try: |
---|
| 340 | temp = unicode(text, format) |
---|
[139] | 341 | except UnicodeError, detail: |
---|
[22] | 342 | # This always works |
---|
| 343 | # |
---|
| 344 | temp = unicode(text, 'iso-8859-15') |
---|
[139] | 345 | except LookupError, detail: |
---|
| 346 | #text = 'ERROR: Could not find charset: %s, please install' %format |
---|
| 347 | #temp = unicode(text, 'iso-8859-15') |
---|
| 348 | temp = message_str |
---|
| 349 | |
---|
[22] | 350 | else: |
---|
| 351 | temp = string.strip(text) |
---|
[92] | 352 | temp = unicode(text, 'iso-8859-15') |
---|
[22] | 353 | |
---|
| 354 | if str: |
---|
[100] | 355 | str = '%s %s' %(str, temp) |
---|
[22] | 356 | else: |
---|
[100] | 357 | str = '%s' %temp |
---|
[22] | 358 | |
---|
[139] | 359 | #str = str.encode('utf-8') |
---|
[22] | 360 | return str |
---|
| 361 | |
---|
[236] | 362 | def debug_body(self, message_body, tempfile=False): |
---|
| 363 | if tempfile: |
---|
| 364 | import tempfile |
---|
| 365 | body_file = tempfile.mktemp('.email2trac') |
---|
| 366 | else: |
---|
| 367 | body_file = os.path.join(self.TMPDIR, 'body.txt') |
---|
| 368 | |
---|
| 369 | print 'TD: writing body (%s)' % body_file |
---|
| 370 | fx = open(body_file, 'wb') |
---|
| 371 | if not message_body: |
---|
| 372 | message_body = '(None)' |
---|
| 373 | fx.write(message_body) |
---|
| 374 | fx.close() |
---|
| 375 | try: |
---|
| 376 | os.chmod(body_file,S_IRWXU|S_IRWXG|S_IRWXO) |
---|
| 377 | except OSError: |
---|
| 378 | pass |
---|
| 379 | |
---|
| 380 | def debug_attachments(self, message_parts): |
---|
[22] | 381 | n = 0 |
---|
[236] | 382 | for part in message_parts: |
---|
| 383 | # Skip inline text parts |
---|
| 384 | if not isinstance(part, tuple): |
---|
[22] | 385 | continue |
---|
[236] | 386 | |
---|
[237] | 387 | (original, filename, part) = part |
---|
[22] | 388 | |
---|
| 389 | n = n + 1 |
---|
| 390 | print 'TD: part%d: Content-Type: %s' % (n, part.get_content_type()) |
---|
| 391 | print 'TD: part%d: filename: %s' % (n, part.get_filename()) |
---|
| 392 | |
---|
[236] | 393 | part_file = os.path.join(self.TMPDIR, filename) |
---|
[173] | 394 | #part_file = '/var/tmp/part%d' % n |
---|
[22] | 395 | print 'TD: writing part%d (%s)' % (n,part_file) |
---|
| 396 | fx = open(part_file, 'wb') |
---|
| 397 | text = part.get_payload(decode=1) |
---|
| 398 | if not text: |
---|
| 399 | text = '(None)' |
---|
| 400 | fx.write(text) |
---|
| 401 | fx.close() |
---|
| 402 | try: |
---|
| 403 | os.chmod(part_file,S_IRWXU|S_IRWXG|S_IRWXO) |
---|
| 404 | except OSError: |
---|
| 405 | pass |
---|
| 406 | |
---|
| 407 | def email_header_txt(self, m): |
---|
[72] | 408 | """ |
---|
| 409 | Display To and CC addresses in description field |
---|
| 410 | """ |
---|
[22] | 411 | str = '' |
---|
[213] | 412 | #if m['To'] and len(m['To']) > 0 and m['To'] != 'hic@sara.nl': |
---|
| 413 | if m['To'] and len(m['To']) > 0: |
---|
| 414 | str = "'''To:''' %s\r\n" %(m['To']) |
---|
[22] | 415 | if m['Cc'] and len(m['Cc']) > 0: |
---|
[213] | 416 | str = "%s'''Cc:''' %s\r\n" % (str, m['Cc']) |
---|
[22] | 417 | |
---|
[139] | 418 | return self.email_to_unicode(str) |
---|
[22] | 419 | |
---|
[138] | 420 | |
---|
[43] | 421 | def set_owner(self, ticket): |
---|
[45] | 422 | """ |
---|
| 423 | Select default owner for ticket component |
---|
| 424 | """ |
---|
[176] | 425 | #### return self.get_config('ticket', 'default_component') |
---|
[43] | 426 | cursor = self.db.cursor() |
---|
| 427 | sql = "SELECT owner FROM component WHERE name='%s'" % ticket['component'] |
---|
| 428 | cursor.execute(sql) |
---|
[61] | 429 | try: |
---|
| 430 | ticket['owner'] = cursor.fetchone()[0] |
---|
| 431 | except TypeError, detail: |
---|
[176] | 432 | ticket['owner'] = None |
---|
[43] | 433 | |
---|
[194] | 434 | def get_sender_info(self, message): |
---|
[45] | 435 | """ |
---|
[72] | 436 | Get the default author name and email address from the message |
---|
[226] | 437 | """ |
---|
[43] | 438 | |
---|
[226] | 439 | self.email_to = self.email_to_unicode(message['to']) |
---|
| 440 | self.to_name, self.to_email_addr = email.Utils.parseaddr (self.email_to) |
---|
| 441 | |
---|
[194] | 442 | self.email_from = self.email_to_unicode(message['from']) |
---|
[223] | 443 | self.author, self.email_addr = email.Utils.parseaddr(self.email_from) |
---|
[142] | 444 | |
---|
[194] | 445 | # Maybe for later user |
---|
| 446 | #self.email_from = self.email_to_unicode(self.email_addr) |
---|
| 447 | |
---|
| 448 | |
---|
| 449 | if self.IGNORE_TRAC_USER_SETTINGS: |
---|
| 450 | return |
---|
| 451 | |
---|
| 452 | # Is this a registered user, use email address as search key: |
---|
| 453 | # result: |
---|
| 454 | # u : login name |
---|
| 455 | # n : Name that the user has set in the settings tab |
---|
| 456 | # e : email address that the user has set in the settings tab |
---|
[45] | 457 | # |
---|
[194] | 458 | users = [ (u,n,e) for (u, n, e) in self.env.get_known_users(self.db) |
---|
[72] | 459 | if e == self.email_addr ] |
---|
[43] | 460 | |
---|
[45] | 461 | if len(users) == 1: |
---|
[194] | 462 | self.email_from = users[0][0] |
---|
[45] | 463 | |
---|
[72] | 464 | def set_reply_fields(self, ticket, message): |
---|
| 465 | """ |
---|
| 466 | Set all the right fields for a new ticket |
---|
| 467 | """ |
---|
[183] | 468 | ticket['reporter'] = self.email_from |
---|
[72] | 469 | |
---|
[45] | 470 | # Put all CC-addresses in ticket CC field |
---|
[43] | 471 | # |
---|
| 472 | if self.REPLY_ALL: |
---|
[45] | 473 | #tos = message.get_all('to', []) |
---|
[43] | 474 | ccs = message.get_all('cc', []) |
---|
| 475 | |
---|
[45] | 476 | addrs = email.Utils.getaddresses(ccs) |
---|
[105] | 477 | if not addrs: |
---|
| 478 | return |
---|
[43] | 479 | |
---|
| 480 | # Remove reporter email address if notification is |
---|
| 481 | # on |
---|
| 482 | # |
---|
| 483 | if self.notification: |
---|
| 484 | try: |
---|
[72] | 485 | addrs.remove((self.author, self.email_addr)) |
---|
[43] | 486 | except ValueError, detail: |
---|
| 487 | pass |
---|
| 488 | |
---|
[45] | 489 | for name,mail in addrs: |
---|
[105] | 490 | try: |
---|
[108] | 491 | mail_list = '%s, %s' %(mail_list, mail) |
---|
[230] | 492 | except UnboundLocalError, detail: |
---|
[105] | 493 | mail_list = mail |
---|
[43] | 494 | |
---|
[105] | 495 | if mail_list: |
---|
[139] | 496 | ticket['cc'] = self.email_to_unicode(mail_list) |
---|
[96] | 497 | |
---|
| 498 | def save_email_for_debug(self, message, tempfile=False): |
---|
| 499 | if tempfile: |
---|
| 500 | import tempfile |
---|
| 501 | msg_file = tempfile.mktemp('.email2trac') |
---|
| 502 | else: |
---|
[173] | 503 | #msg_file = '/var/tmp/msg.txt' |
---|
| 504 | msg_file = os.path.join(self.TMPDIR, 'msg.txt') |
---|
| 505 | |
---|
[44] | 506 | print 'TD: saving email to %s' % msg_file |
---|
| 507 | fx = open(msg_file, 'wb') |
---|
| 508 | fx.write('%s' % message) |
---|
| 509 | fx.close() |
---|
| 510 | try: |
---|
| 511 | os.chmod(msg_file,S_IRWXU|S_IRWXG|S_IRWXO) |
---|
| 512 | except OSError: |
---|
| 513 | pass |
---|
| 514 | |
---|
[167] | 515 | def str_to_dict(self, str): |
---|
[164] | 516 | """ |
---|
| 517 | Transfrom a str of the form [<key>=<value>]+ to dict[<key>] = <value> |
---|
| 518 | """ |
---|
| 519 | # Skip the last ':' character |
---|
| 520 | # |
---|
| 521 | fields = string.split(str[:-1], ',') |
---|
| 522 | |
---|
| 523 | result = dict() |
---|
| 524 | for field in fields: |
---|
| 525 | try: |
---|
| 526 | index, value = string.split(field,'=') |
---|
[169] | 527 | |
---|
| 528 | # We can not change the description of a ticket via the subject |
---|
| 529 | # line. The description is the body of the email |
---|
| 530 | # |
---|
| 531 | if index.lower() in ['description']: |
---|
| 532 | continue |
---|
| 533 | |
---|
[164] | 534 | if value: |
---|
[165] | 535 | result[index.lower()] = value |
---|
[169] | 536 | |
---|
[164] | 537 | except ValueError: |
---|
| 538 | pass |
---|
| 539 | |
---|
[165] | 540 | return result |
---|
[167] | 541 | |
---|
[202] | 542 | def update_ticket_fields(self, ticket, user_dict, use_default=None): |
---|
| 543 | """ |
---|
| 544 | This will update the ticket fields. It will check if the |
---|
| 545 | given fields are known and if the right values are specified |
---|
| 546 | It will only update the ticket field value: |
---|
[169] | 547 | - If the field is known |
---|
[202] | 548 | - If the value supplied is valid for the ticket field. |
---|
| 549 | If not then there are two options: |
---|
| 550 | 1) Skip the value (use_default=None) |
---|
| 551 | 2) Set default value for field (use_default=1) |
---|
[169] | 552 | """ |
---|
| 553 | |
---|
| 554 | # Build a system dictionary from the ticket fields |
---|
| 555 | # with field as index and option as value |
---|
| 556 | # |
---|
| 557 | sys_dict = dict() |
---|
| 558 | for field in ticket.fields: |
---|
[167] | 559 | try: |
---|
[169] | 560 | sys_dict[field['name']] = field['options'] |
---|
| 561 | |
---|
[167] | 562 | except KeyError: |
---|
[169] | 563 | sys_dict[field['name']] = None |
---|
[167] | 564 | pass |
---|
[169] | 565 | |
---|
| 566 | # Check user supplied fields an compare them with the |
---|
| 567 | # system one's |
---|
| 568 | # |
---|
| 569 | for field,value in user_dict.items(): |
---|
[202] | 570 | if self.DEBUG >= 10: |
---|
| 571 | print 'user_field\t %s = %s' %(field,value) |
---|
[169] | 572 | |
---|
| 573 | if sys_dict.has_key(field): |
---|
| 574 | |
---|
| 575 | # Check if value is an allowed system option, if TypeError then |
---|
| 576 | # every value is allowed |
---|
| 577 | # |
---|
| 578 | try: |
---|
| 579 | if value in sys_dict[field]: |
---|
| 580 | ticket[field] = value |
---|
[202] | 581 | else: |
---|
| 582 | # Must we set a default if value is not allowed |
---|
| 583 | # |
---|
| 584 | if use_default: |
---|
| 585 | value = self.get_config('ticket', 'default_%s' %(field) ) |
---|
| 586 | ticket[field] = value |
---|
[169] | 587 | |
---|
| 588 | except TypeError: |
---|
| 589 | ticket[field] = value |
---|
[202] | 590 | |
---|
| 591 | if self.DEBUG >= 10: |
---|
| 592 | print 'ticket_field\t %s = %s' %(field, ticket[field]) |
---|
[169] | 593 | |
---|
[204] | 594 | def ticket_update(self, m, spam): |
---|
[78] | 595 | """ |
---|
[79] | 596 | If the current email is a reply to an existing ticket, this function |
---|
| 597 | will append the contents of this email to that ticket, instead of |
---|
| 598 | creating a new one. |
---|
[78] | 599 | """ |
---|
[202] | 600 | |
---|
[71] | 601 | if not m['Subject']: |
---|
| 602 | return False |
---|
| 603 | else: |
---|
[139] | 604 | subject = self.email_to_unicode(m['Subject']) |
---|
[71] | 605 | |
---|
[182] | 606 | # [hic] #1529: Re: LRZ |
---|
| 607 | # [hic] #1529?owner=bas,priority=medium: Re: LRZ |
---|
| 608 | # |
---|
[71] | 609 | TICKET_RE = re.compile(r""" |
---|
| 610 | (?P<ticketnr>[#][0-9]+:) |
---|
[194] | 611 | |(?P<ticketnr_fields>[#][\d]+\?.*?:) |
---|
[71] | 612 | """, re.VERBOSE) |
---|
| 613 | |
---|
| 614 | result = TICKET_RE.search(subject) |
---|
| 615 | if not result: |
---|
| 616 | return False |
---|
| 617 | |
---|
[164] | 618 | # Must we update ticket fields |
---|
| 619 | # |
---|
[220] | 620 | update_fields = dict() |
---|
[165] | 621 | try: |
---|
[164] | 622 | nr, keywords = string.split(result.group('ticketnr_fields'), '?') |
---|
[220] | 623 | update_fields = self.str_to_dict(keywords) |
---|
[165] | 624 | |
---|
| 625 | # Strip '#' |
---|
| 626 | # |
---|
| 627 | ticket_id = int(nr[1:]) |
---|
| 628 | |
---|
| 629 | except AttributeError: |
---|
| 630 | # Strip '#' and ':' |
---|
| 631 | # |
---|
[167] | 632 | nr = result.group('ticketnr') |
---|
[165] | 633 | ticket_id = int(nr[1:-1]) |
---|
[164] | 634 | |
---|
[71] | 635 | |
---|
[194] | 636 | # When is the change committed |
---|
| 637 | # |
---|
[77] | 638 | # |
---|
[194] | 639 | if self.VERSION == 0.11: |
---|
| 640 | utc = UTC() |
---|
| 641 | when = datetime.now(utc) |
---|
| 642 | else: |
---|
| 643 | when = int(time.time()) |
---|
[77] | 644 | |
---|
[172] | 645 | try: |
---|
| 646 | tkt = Ticket(self.env, ticket_id, self.db) |
---|
| 647 | except util.TracError, detail: |
---|
| 648 | return False |
---|
[126] | 649 | |
---|
[220] | 650 | # reopen the ticket if it is was closed |
---|
| 651 | # We must use the ticket workflow framework |
---|
| 652 | # |
---|
| 653 | if tkt['status'] in ['closed']: |
---|
| 654 | tkt['status'] = 'reopened' |
---|
| 655 | tkt['resolution'] = '' |
---|
| 656 | |
---|
[172] | 657 | # Must we update some ticket fields properties |
---|
| 658 | # |
---|
[220] | 659 | if update_fields: |
---|
| 660 | self.update_ticket_fields(tkt, update_fields) |
---|
[166] | 661 | |
---|
[236] | 662 | message_parts = self.get_message_parts(m) |
---|
| 663 | message_parts = self.unique_attachment_names(message_parts, tkt) |
---|
[210] | 664 | |
---|
[177] | 665 | if self.EMAIL_HEADER: |
---|
[236] | 666 | message_parts.insert(0, self.email_header_txt(m)) |
---|
[76] | 667 | |
---|
[236] | 668 | body_text = self.body_text(message_parts) |
---|
| 669 | |
---|
[219] | 670 | if body_text.strip(): |
---|
| 671 | tkt.save_changes(self.author, body_text, when) |
---|
| 672 | |
---|
[172] | 673 | tkt['id'] = ticket_id |
---|
| 674 | |
---|
[129] | 675 | if self.VERSION == 0.9: |
---|
[236] | 676 | str = self.attachments(message_parts, tkt, True) |
---|
[129] | 677 | else: |
---|
[236] | 678 | str = self.attachments(message_parts, tkt) |
---|
[76] | 679 | |
---|
[204] | 680 | if self.notification and not spam: |
---|
[77] | 681 | self.notify(tkt, False, when) |
---|
[72] | 682 | |
---|
[71] | 683 | return True |
---|
| 684 | |
---|
[202] | 685 | def set_ticket_fields(self, ticket): |
---|
[77] | 686 | """ |
---|
[202] | 687 | set the ticket fields to value specified |
---|
| 688 | - /etc/email2trac.conf with <prefix>_<field> |
---|
| 689 | - trac default values, trac.ini |
---|
| 690 | """ |
---|
| 691 | user_dict = dict() |
---|
| 692 | |
---|
| 693 | for field in ticket.fields: |
---|
| 694 | |
---|
| 695 | name = field['name'] |
---|
| 696 | |
---|
[215] | 697 | # skip some fields like resolution |
---|
| 698 | # |
---|
| 699 | if name in [ 'resolution' ]: |
---|
| 700 | continue |
---|
| 701 | |
---|
[202] | 702 | # default trac value |
---|
| 703 | # |
---|
[233] | 704 | if not field.get('custom'): |
---|
| 705 | value = self.get_config('ticket', 'default_%s' %(name) ) |
---|
| 706 | else: |
---|
| 707 | value = field.get('value') |
---|
| 708 | options = field.get('options') |
---|
[234] | 709 | if value and options and value not in options: |
---|
[233] | 710 | value = options[int(value)] |
---|
| 711 | |
---|
[202] | 712 | if self.DEBUG > 10: |
---|
| 713 | print 'trac.ini name %s = %s' %(name, value) |
---|
| 714 | |
---|
[206] | 715 | prefix = self.parameters['ticket_prefix'] |
---|
[202] | 716 | try: |
---|
[206] | 717 | value = self.parameters['%s_%s' %(prefix, name)] |
---|
[202] | 718 | if self.DEBUG > 10: |
---|
| 719 | print 'email2trac.conf %s = %s ' %(name, value) |
---|
| 720 | |
---|
| 721 | except KeyError, detail: |
---|
| 722 | pass |
---|
| 723 | |
---|
| 724 | if self.DEBUG: |
---|
| 725 | print 'user_dict[%s] = %s' %(name, value) |
---|
| 726 | |
---|
| 727 | user_dict[name] = value |
---|
| 728 | |
---|
| 729 | self.update_ticket_fields(ticket, user_dict, use_default=1) |
---|
| 730 | |
---|
| 731 | # Set status ticket |
---|
| 732 | #` |
---|
| 733 | ticket['status'] = 'new' |
---|
| 734 | |
---|
| 735 | |
---|
| 736 | |
---|
[204] | 737 | def new_ticket(self, msg, spam): |
---|
[202] | 738 | """ |
---|
[77] | 739 | Create a new ticket |
---|
| 740 | """ |
---|
[41] | 741 | tkt = Ticket(self.env) |
---|
[22] | 742 | |
---|
[202] | 743 | self.set_ticket_fields(tkt) |
---|
| 744 | |
---|
[22] | 745 | # Some defaults |
---|
| 746 | # |
---|
[202] | 747 | #tkt['status'] = 'new' |
---|
| 748 | #tkt['milestone'] = self.get_config('ticket', 'default_milestone') |
---|
| 749 | #tkt['priority'] = self.get_config('ticket', 'default_priority') |
---|
| 750 | #tkt['severity'] = self.get_config('ticket', 'default_severity') |
---|
| 751 | #tkt['version'] = self.get_config('ticket', 'default_version') |
---|
| 752 | #tkt['type'] = self.get_config('ticket', 'default_type') |
---|
[22] | 753 | |
---|
[202] | 754 | # Old style setting for component, will be removed |
---|
| 755 | # |
---|
[204] | 756 | if spam: |
---|
| 757 | tkt['component'] = 'Spam' |
---|
| 758 | |
---|
[206] | 759 | elif self.parameters.has_key('component'): |
---|
| 760 | tkt['component'] = self.parameters['component'] |
---|
[201] | 761 | |
---|
[22] | 762 | if not msg['Subject']: |
---|
[151] | 763 | tkt['summary'] = u'(No subject)' |
---|
[22] | 764 | else: |
---|
[139] | 765 | tkt['summary'] = self.email_to_unicode(msg['Subject']) |
---|
[22] | 766 | |
---|
| 767 | |
---|
[72] | 768 | self.set_reply_fields(tkt, msg) |
---|
[22] | 769 | |
---|
[45] | 770 | # produce e-mail like header |
---|
| 771 | # |
---|
[22] | 772 | head = '' |
---|
| 773 | if self.EMAIL_HEADER > 0: |
---|
| 774 | head = self.email_header_txt(msg) |
---|
[92] | 775 | |
---|
[236] | 776 | message_parts = self.get_message_parts(msg) |
---|
| 777 | message_parts = self.unique_attachment_names(message_parts) |
---|
| 778 | |
---|
| 779 | if self.EMAIL_HEADER > 0: |
---|
| 780 | message_parts.insert(0, self.email_header_txt(msg)) |
---|
| 781 | |
---|
| 782 | body_text = self.body_text(message_parts) |
---|
[45] | 783 | |
---|
[236] | 784 | tkt['description'] = body_text |
---|
[90] | 785 | |
---|
[182] | 786 | #when = int(time.time()) |
---|
[192] | 787 | # |
---|
[182] | 788 | utc = UTC() |
---|
| 789 | when = datetime.now(utc) |
---|
[45] | 790 | |
---|
[204] | 791 | if self.DRY_RUN: |
---|
[201] | 792 | ticket_id = 'DRY_RUN' |
---|
| 793 | else: |
---|
| 794 | ticket_id = tkt.insert() |
---|
[187] | 795 | |
---|
[172] | 796 | tkt['id'] = ticket_id |
---|
| 797 | |
---|
[90] | 798 | changed = False |
---|
| 799 | comment = '' |
---|
[77] | 800 | |
---|
[90] | 801 | # Rewrite the description if we have mailto enabled |
---|
[45] | 802 | # |
---|
[72] | 803 | if self.MAILTO: |
---|
[100] | 804 | changed = True |
---|
[142] | 805 | comment = u'\nadded mailto line\n' |
---|
[210] | 806 | #mailto = self.html_mailto_link(tkt['summary'], ticket_id, body_text) |
---|
| 807 | mailto = self.html_mailto_link( m['Subject'], ticket_id, body_text) |
---|
[213] | 808 | tkt['description'] = u'%s\r\n%s%s\r\n' \ |
---|
[142] | 809 | %(head, mailto, body_text) |
---|
[45] | 810 | |
---|
[236] | 811 | str = self.attachments(message_parts, tkt) |
---|
[152] | 812 | if str: |
---|
[100] | 813 | changed = True |
---|
[152] | 814 | comment = '%s\n%s\n' %(comment, str) |
---|
[77] | 815 | |
---|
[90] | 816 | if changed: |
---|
[204] | 817 | if self.DRY_RUN: |
---|
[201] | 818 | print 'DRY_RUN: tkt.save_changes(self.author, comment)' |
---|
| 819 | else: |
---|
| 820 | tkt.save_changes(self.author, comment) |
---|
| 821 | #print tkt.get_changelog(self.db, when) |
---|
[90] | 822 | |
---|
[45] | 823 | if self.notification: |
---|
[204] | 824 | if self.DRY_RUN: |
---|
[202] | 825 | print 'DRY_RUN: self.notify(tkt, True)' |
---|
| 826 | else: |
---|
[204] | 827 | if not spam: |
---|
| 828 | self.notify(tkt, True) |
---|
[202] | 829 | #self.notify(tkt, False) |
---|
[45] | 830 | |
---|
[77] | 831 | def parse(self, fp): |
---|
[96] | 832 | global m |
---|
| 833 | |
---|
[77] | 834 | m = email.message_from_file(fp) |
---|
| 835 | if not m: |
---|
[221] | 836 | if self.DEBUG: |
---|
| 837 | print "This is not a valid email message format" |
---|
[77] | 838 | return |
---|
| 839 | |
---|
| 840 | if self.DEBUG > 1: # save the entire e-mail message text |
---|
[236] | 841 | message_parts = self.get_message_parts(m) |
---|
| 842 | message_parts = self.unique_attachment_names(message_parts) |
---|
[219] | 843 | self.save_email_for_debug(m, True) |
---|
[236] | 844 | body_text = self.body_text(message_parts) |
---|
| 845 | self.debug_body(body_text, True) |
---|
| 846 | self.debug_attachments(message_parts) |
---|
[77] | 847 | |
---|
| 848 | self.db = self.env.get_db_cnx() |
---|
[194] | 849 | self.get_sender_info(m) |
---|
[152] | 850 | |
---|
[221] | 851 | if not self.email_header_acl('white_list', self.email_addr, True): |
---|
| 852 | if self.DEBUG > 1 : |
---|
| 853 | print 'Message rejected : %s not in white list' %(self.email_addr) |
---|
| 854 | return False |
---|
[77] | 855 | |
---|
[221] | 856 | if self.email_header_acl('black_list', self.email_addr, False): |
---|
| 857 | if self.DEBUG > 1 : |
---|
| 858 | print 'Message rejected : %s in black list' %(self.email_addr) |
---|
| 859 | return False |
---|
| 860 | |
---|
[227] | 861 | if not self.email_header_acl('recipient_list', self.to_email_addr, True): |
---|
[226] | 862 | if self.DEBUG > 1 : |
---|
| 863 | print 'Message rejected : %s not in recipient list' %(self.to_email_addr) |
---|
| 864 | return False |
---|
| 865 | |
---|
[204] | 866 | # If drop the message |
---|
[194] | 867 | # |
---|
[204] | 868 | if self.spam(m) == 'drop': |
---|
[194] | 869 | return False |
---|
| 870 | |
---|
[204] | 871 | elif self.spam(m) == 'spam': |
---|
| 872 | spam_msg = True |
---|
[194] | 873 | |
---|
[204] | 874 | else: |
---|
| 875 | spam_msg = False |
---|
| 876 | |
---|
[77] | 877 | if self.get_config('notification', 'smtp_enabled') in ['true']: |
---|
| 878 | self.notification = 1 |
---|
| 879 | else: |
---|
| 880 | self.notification = 0 |
---|
| 881 | |
---|
| 882 | # Must we update existing tickets |
---|
| 883 | # |
---|
| 884 | if self.TICKET_UPDATE > 0: |
---|
[204] | 885 | if self.ticket_update(m, spam_msg): |
---|
[77] | 886 | return True |
---|
| 887 | |
---|
[204] | 888 | self.new_ticket(m, spam_msg) |
---|
[77] | 889 | |
---|
[136] | 890 | def strip_signature(self, text): |
---|
| 891 | """ |
---|
| 892 | Strip signature from message, inspired by Mailman software |
---|
| 893 | """ |
---|
| 894 | body = [] |
---|
| 895 | for line in text.splitlines(): |
---|
| 896 | if line == '-- ': |
---|
| 897 | break |
---|
| 898 | body.append(line) |
---|
| 899 | |
---|
| 900 | return ('\n'.join(body)) |
---|
| 901 | |
---|
[231] | 902 | def reflow(self, text, delsp = 0): |
---|
| 903 | """ |
---|
| 904 | Reflow the message based on the format="flowed" specification (RFC 3676) |
---|
| 905 | """ |
---|
| 906 | flowedlines = [] |
---|
| 907 | quotelevel = 0 |
---|
| 908 | prevflowed = 0 |
---|
| 909 | |
---|
| 910 | for line in text.splitlines(): |
---|
| 911 | from re import match |
---|
| 912 | |
---|
| 913 | # Figure out the quote level and the content of the current line |
---|
| 914 | m = match('(>*)( ?)(.*)', line) |
---|
| 915 | linequotelevel = len(m.group(1)) |
---|
| 916 | line = m.group(3) |
---|
| 917 | |
---|
| 918 | # Determine whether this line is flowed |
---|
| 919 | if line and line != '-- ' and line[-1] == ' ': |
---|
| 920 | flowed = 1 |
---|
| 921 | else: |
---|
| 922 | flowed = 0 |
---|
| 923 | |
---|
| 924 | if flowed and delsp and line and line[-1] == ' ': |
---|
| 925 | line = line[:-1] |
---|
| 926 | |
---|
| 927 | # If the previous line is flowed, append this line to it |
---|
| 928 | if prevflowed and line != '-- ' and linequotelevel == quotelevel: |
---|
| 929 | flowedlines[-1] += line |
---|
| 930 | # Otherwise, start a new line |
---|
| 931 | else: |
---|
| 932 | flowedlines.append('>' * linequotelevel + line) |
---|
| 933 | |
---|
| 934 | prevflowed = flowed |
---|
| 935 | |
---|
| 936 | |
---|
| 937 | return '\n'.join(flowedlines) |
---|
| 938 | |
---|
[191] | 939 | def strip_quotes(self, text): |
---|
[193] | 940 | """ |
---|
| 941 | Strip quotes from message by Nicolas Mendoza |
---|
| 942 | """ |
---|
| 943 | body = [] |
---|
| 944 | for line in text.splitlines(): |
---|
| 945 | if line.startswith(self.EMAIL_QUOTE): |
---|
| 946 | continue |
---|
| 947 | body.append(line) |
---|
[151] | 948 | |
---|
[193] | 949 | return ('\n'.join(body)) |
---|
[191] | 950 | |
---|
[154] | 951 | def wrap_text(self, text, replace_whitespace = False): |
---|
[151] | 952 | """ |
---|
[191] | 953 | Will break a lines longer then given length into several small |
---|
| 954 | lines of size given length |
---|
[151] | 955 | """ |
---|
| 956 | import textwrap |
---|
[154] | 957 | |
---|
[151] | 958 | LINESEPARATOR = '\n' |
---|
[153] | 959 | reformat = '' |
---|
[151] | 960 | |
---|
[154] | 961 | for s in text.split(LINESEPARATOR): |
---|
| 962 | tmp = textwrap.fill(s,self.USE_TEXTWRAP) |
---|
| 963 | if tmp: |
---|
| 964 | reformat = '%s\n%s' %(reformat,tmp) |
---|
| 965 | else: |
---|
| 966 | reformat = '%s\n' %reformat |
---|
[153] | 967 | |
---|
| 968 | return reformat |
---|
| 969 | |
---|
[154] | 970 | # Python2.4 and higher |
---|
| 971 | # |
---|
| 972 | #return LINESEPARATOR.join(textwrap.fill(s,width) for s in str.split(LINESEPARATOR)) |
---|
| 973 | # |
---|
| 974 | |
---|
| 975 | |
---|
[236] | 976 | def get_message_parts(self, msg): |
---|
[45] | 977 | """ |
---|
[236] | 978 | parses the email message and returns a list of body parts and attachments |
---|
| 979 | body parts are returned as strings, attachments are returned as tuples of (filename, Message object) |
---|
[45] | 980 | """ |
---|
[236] | 981 | message_parts = [] |
---|
| 982 | |
---|
[22] | 983 | for part in msg.walk(): |
---|
[45] | 984 | # 'multipart/*' is a container for multipart messages |
---|
| 985 | # |
---|
[236] | 986 | if self.DEBUG: |
---|
| 987 | print 'TD: Message part: Content-Type: %s' % part.get_content_type() |
---|
| 988 | |
---|
[45] | 989 | if part.get_content_maintype() == 'multipart': |
---|
[22] | 990 | continue |
---|
| 991 | |
---|
[236] | 992 | # Check if this is an inline part. It's inline if there is co Cont-Disp header, or if there is one and it says "inline" |
---|
| 993 | inline = self.inline_part(part) |
---|
| 994 | |
---|
| 995 | # Inline text parts are where the body is |
---|
| 996 | if part.get_content_type() == 'text/plain' and inline: |
---|
| 997 | if self.DEBUG: |
---|
| 998 | print 'TD: Inline body part' |
---|
| 999 | |
---|
[45] | 1000 | # Try to decode, if fails then do not decode |
---|
| 1001 | # |
---|
[90] | 1002 | body_text = part.get_payload(decode=1) |
---|
[45] | 1003 | if not body_text: |
---|
[90] | 1004 | body_text = part.get_payload(decode=0) |
---|
[231] | 1005 | |
---|
[232] | 1006 | format = email.Utils.collapse_rfc2231_value(part.get_param('Format', 'fixed')).lower() |
---|
| 1007 | delsp = email.Utils.collapse_rfc2231_value(part.get_param('DelSp', 'no')).lower() |
---|
[231] | 1008 | |
---|
| 1009 | if self.REFLOW and not self.VERBATIM_FORMAT and format == 'flowed': |
---|
| 1010 | body_text = self.reflow(body_text, delsp == 'yes') |
---|
[154] | 1011 | |
---|
[136] | 1012 | if self.STRIP_SIGNATURE: |
---|
| 1013 | body_text = self.strip_signature(body_text) |
---|
[22] | 1014 | |
---|
[191] | 1015 | if self.STRIP_QUOTES: |
---|
| 1016 | body_text = self.strip_quotes(body_text) |
---|
| 1017 | |
---|
[148] | 1018 | if self.USE_TEXTWRAP: |
---|
[151] | 1019 | body_text = self.wrap_text(body_text) |
---|
[148] | 1020 | |
---|
[45] | 1021 | # Get contents charset (iso-8859-15 if not defined in mail headers) |
---|
| 1022 | # |
---|
[100] | 1023 | charset = part.get_content_charset() |
---|
[102] | 1024 | if not charset: |
---|
| 1025 | charset = 'iso-8859-15' |
---|
| 1026 | |
---|
[89] | 1027 | try: |
---|
[96] | 1028 | ubody_text = unicode(body_text, charset) |
---|
[100] | 1029 | |
---|
| 1030 | except UnicodeError, detail: |
---|
[96] | 1031 | ubody_text = unicode(body_text, 'iso-8859-15') |
---|
[89] | 1032 | |
---|
[100] | 1033 | except LookupError, detail: |
---|
[139] | 1034 | ubody_text = 'ERROR: Could not find charset: %s, please install' %(charset) |
---|
[100] | 1035 | |
---|
[236] | 1036 | if self.VERBATIM_FORMAT: |
---|
| 1037 | message_parts.append('{{{\r\n%s\r\n}}}' %ubody_text) |
---|
| 1038 | else: |
---|
| 1039 | message_parts.append('%s' %ubody_text) |
---|
| 1040 | else: |
---|
| 1041 | if self.DEBUG: |
---|
| 1042 | print 'TD: Filename: %s' % part.get_filename() |
---|
[22] | 1043 | |
---|
[236] | 1044 | message_parts.append((part.get_filename(), part)) |
---|
| 1045 | |
---|
| 1046 | return message_parts |
---|
| 1047 | |
---|
| 1048 | def unique_attachment_names(self, message_parts, tkt = None): |
---|
| 1049 | renamed_parts = [] |
---|
| 1050 | attachment_names = set() |
---|
| 1051 | for part in message_parts: |
---|
| 1052 | |
---|
| 1053 | # If not an attachment, leave it alone |
---|
| 1054 | if not isinstance(part, tuple): |
---|
| 1055 | renamed_parts.append(part) |
---|
| 1056 | continue |
---|
| 1057 | |
---|
| 1058 | (filename, part) = part |
---|
| 1059 | # Decode the filename |
---|
| 1060 | if filename: |
---|
| 1061 | filename = self.email_to_unicode(filename) |
---|
| 1062 | # If no name, use a default one |
---|
[22] | 1063 | else: |
---|
[236] | 1064 | filename = 'untitled-part' |
---|
[22] | 1065 | |
---|
[236] | 1066 | # Guess the extension from the content type |
---|
| 1067 | ext = mimetypes.guess_extension(part.get_content_type()) |
---|
| 1068 | if not ext: |
---|
| 1069 | ext = '.bin' |
---|
[22] | 1070 | |
---|
[236] | 1071 | filename = '%s%s' % (filename, ext) |
---|
[22] | 1072 | |
---|
[236] | 1073 | # Discard relative paths in attachment names |
---|
| 1074 | filename = filename.replace('\\', '/').replace(':', '/') |
---|
| 1075 | filename = os.path.basename(filename) |
---|
[22] | 1076 | |
---|
[236] | 1077 | # We try to normalize the filename to utf-8 NFC if we can. |
---|
| 1078 | # Files uploaded from OS X might be in NFD. |
---|
| 1079 | # Check python version and then try it |
---|
| 1080 | # |
---|
| 1081 | if sys.version_info[0] > 2 or (sys.version_info[0] == 2 and sys.version_info[1] >= 3): |
---|
| 1082 | try: |
---|
| 1083 | filename = unicodedata.normalize('NFC', unicode(filename, 'utf-8')).encode('utf-8') |
---|
| 1084 | except TypeError: |
---|
| 1085 | pass |
---|
| 1086 | |
---|
| 1087 | if self.QUOTE_ATTACHMENT_FILENAMES: |
---|
| 1088 | filename = urllib.quote(filename) |
---|
[100] | 1089 | |
---|
[236] | 1090 | # Make the filename unique for this ticket |
---|
| 1091 | num = 0 |
---|
| 1092 | unique_filename = filename |
---|
| 1093 | filename, ext = os.path.splitext(filename) |
---|
[134] | 1094 | |
---|
[236] | 1095 | while unique_filename in attachment_names or self.attachment_exists(tkt, unique_filename): |
---|
| 1096 | num += 1 |
---|
| 1097 | unique_filename = "%s-%s%s" % (filename, num, ext) |
---|
| 1098 | |
---|
| 1099 | if self.DEBUG: |
---|
| 1100 | print 'TD: Attachment with filename %s will be saved as %s' % (filename, unique_filename) |
---|
[100] | 1101 | |
---|
[236] | 1102 | attachment_names.add(unique_filename) |
---|
| 1103 | |
---|
| 1104 | renamed_parts.append((filename, unique_filename, part)) |
---|
| 1105 | |
---|
| 1106 | return renamed_parts |
---|
| 1107 | |
---|
| 1108 | def inline_part(self, part): |
---|
| 1109 | return part.get_param('inline', None, 'Content-Disposition') == '' or not part.has_key('Content-Disposition') |
---|
| 1110 | |
---|
| 1111 | |
---|
| 1112 | def attachment_exists(self, tkt, filename): |
---|
| 1113 | if tkt is None: |
---|
| 1114 | return False |
---|
| 1115 | |
---|
| 1116 | try: |
---|
| 1117 | Attachment(self.env, 'ticket', tkt['id'], filename) |
---|
| 1118 | return True |
---|
| 1119 | except ResourceNotFound: |
---|
| 1120 | return False |
---|
| 1121 | |
---|
| 1122 | def body_text(self, message_parts): |
---|
| 1123 | body_text = [] |
---|
| 1124 | |
---|
| 1125 | for part in message_parts: |
---|
| 1126 | # Plain text part, append it |
---|
| 1127 | if not isinstance(part, tuple): |
---|
| 1128 | body_text.extend(part.strip().splitlines()) |
---|
| 1129 | body_text.append("") |
---|
| 1130 | continue |
---|
| 1131 | |
---|
| 1132 | (original, filename, part) = part |
---|
| 1133 | inline = self.inline_part(part) |
---|
| 1134 | |
---|
| 1135 | if part.get_content_maintype() == 'image' and inline: |
---|
| 1136 | body_text.append('[[Image(%s)]]' % filename) |
---|
| 1137 | body_text.append("") |
---|
| 1138 | else: |
---|
| 1139 | body_text.append('[attachment:"%s"]' % filename) |
---|
| 1140 | body_text.append("") |
---|
| 1141 | |
---|
| 1142 | body_text = '\r\n'.join(body_text) |
---|
| 1143 | return body_text |
---|
| 1144 | |
---|
[77] | 1145 | def notify(self, tkt , new=True, modtime=0): |
---|
[79] | 1146 | """ |
---|
| 1147 | A wrapper for the TRAC notify function. So we can use templates |
---|
| 1148 | """ |
---|
[41] | 1149 | try: |
---|
| 1150 | # create false {abs_}href properties, to trick Notify() |
---|
| 1151 | # |
---|
[193] | 1152 | if not self.VERSION == 0.11: |
---|
[192] | 1153 | self.env.abs_href = Href(self.get_config('project', 'url')) |
---|
| 1154 | self.env.href = Href(self.get_config('project', 'url')) |
---|
[22] | 1155 | |
---|
[41] | 1156 | tn = TicketNotifyEmail(self.env) |
---|
[213] | 1157 | |
---|
[42] | 1158 | if self.notify_template: |
---|
[222] | 1159 | |
---|
[221] | 1160 | if self.VERSION == 0.11: |
---|
[222] | 1161 | |
---|
[221] | 1162 | from trac.web.chrome import Chrome |
---|
[222] | 1163 | |
---|
| 1164 | if self.notify_template_update and not new: |
---|
| 1165 | tn.template_name = self.notify_template_update |
---|
| 1166 | else: |
---|
| 1167 | tn.template_name = self.notify_template |
---|
| 1168 | |
---|
[221] | 1169 | tn.template = Chrome(tn.env).load_template(tn.template_name, method='text') |
---|
| 1170 | |
---|
| 1171 | else: |
---|
[222] | 1172 | |
---|
[221] | 1173 | tn.template_name = self.notify_template; |
---|
[42] | 1174 | |
---|
[77] | 1175 | tn.notify(tkt, new, modtime) |
---|
[41] | 1176 | |
---|
| 1177 | except Exception, e: |
---|
[79] | 1178 | print 'TD: Failure sending notification on creation of ticket #%s: %s' %(tkt['id'], e) |
---|
[41] | 1179 | |
---|
[72] | 1180 | def html_mailto_link(self, subject, id, body): |
---|
| 1181 | if not self.author: |
---|
[143] | 1182 | author = self.email_addr |
---|
[22] | 1183 | else: |
---|
[142] | 1184 | author = self.author |
---|
[22] | 1185 | |
---|
| 1186 | # Must find a fix |
---|
| 1187 | # |
---|
| 1188 | #arr = string.split(body, '\n') |
---|
| 1189 | #arr = map(self.mail_line, arr) |
---|
| 1190 | #body = string.join(arr, '\n') |
---|
| 1191 | #body = '%s wrote:\n%s' %(author, body) |
---|
| 1192 | |
---|
| 1193 | # Temporary fix |
---|
[142] | 1194 | # |
---|
[74] | 1195 | str = 'mailto:%s?Subject=%s&Cc=%s' %( |
---|
| 1196 | urllib.quote(self.email_addr), |
---|
| 1197 | urllib.quote('Re: #%s: %s' %(id, subject)), |
---|
| 1198 | urllib.quote(self.MAILTO_CC) |
---|
| 1199 | ) |
---|
| 1200 | |
---|
[213] | 1201 | str = '\r\n{{{\r\n#!html\r\n<a\r\n href="%s">Reply to: %s\r\n</a>\r\n}}}\r\n' %(str, author) |
---|
[22] | 1202 | return str |
---|
| 1203 | |
---|
[236] | 1204 | def attachments(self, message_parts, ticket, update=False): |
---|
[79] | 1205 | ''' |
---|
| 1206 | save any attachments as files in the ticket's directory |
---|
| 1207 | ''' |
---|
[237] | 1208 | if self.DRY_RUN: |
---|
| 1209 | return '' |
---|
| 1210 | |
---|
[22] | 1211 | count = 0 |
---|
[77] | 1212 | first = 0 |
---|
| 1213 | number = 0 |
---|
[152] | 1214 | |
---|
| 1215 | # Get Maxium attachment size |
---|
| 1216 | # |
---|
| 1217 | max_size = int(self.get_config('attachment', 'max_size')) |
---|
[153] | 1218 | status = '' |
---|
[236] | 1219 | |
---|
| 1220 | for part in message_parts: |
---|
| 1221 | # Skip body parts |
---|
| 1222 | if not isinstance(part, tuple): |
---|
[22] | 1223 | continue |
---|
[236] | 1224 | |
---|
| 1225 | (original, filename, part) = part |
---|
[48] | 1226 | # |
---|
[172] | 1227 | # Must be tuneables HvB |
---|
| 1228 | # |
---|
[236] | 1229 | path, fd = util.create_unique_file(os.path.join(self.TMPDIR, filename)) |
---|
[22] | 1230 | text = part.get_payload(decode=1) |
---|
| 1231 | if not text: |
---|
| 1232 | text = '(None)' |
---|
[48] | 1233 | fd.write(text) |
---|
| 1234 | fd.close() |
---|
[22] | 1235 | |
---|
[153] | 1236 | # get the file_size |
---|
[22] | 1237 | # |
---|
[48] | 1238 | stats = os.lstat(path) |
---|
[153] | 1239 | file_size = stats[stat.ST_SIZE] |
---|
[22] | 1240 | |
---|
[152] | 1241 | # Check if the attachment size is allowed |
---|
| 1242 | # |
---|
[153] | 1243 | if (max_size != -1) and (file_size > max_size): |
---|
| 1244 | status = '%s\nFile %s is larger then allowed attachment size (%d > %d)\n\n' \ |
---|
[236] | 1245 | %(status, original, file_size, max_size) |
---|
[152] | 1246 | |
---|
| 1247 | os.unlink(path) |
---|
| 1248 | continue |
---|
| 1249 | else: |
---|
| 1250 | count = count + 1 |
---|
| 1251 | |
---|
[172] | 1252 | # Insert the attachment |
---|
[73] | 1253 | # |
---|
[172] | 1254 | fd = open(path) |
---|
| 1255 | att = attachment.Attachment(self.env, 'ticket', ticket['id']) |
---|
[73] | 1256 | |
---|
[172] | 1257 | # This will break the ticket_update system, the body_text is vaporized |
---|
| 1258 | # ;-( |
---|
| 1259 | # |
---|
| 1260 | if not update: |
---|
| 1261 | att.author = self.author |
---|
| 1262 | att.description = self.email_to_unicode('Added by email2trac') |
---|
[73] | 1263 | |
---|
[236] | 1264 | att.insert(filename, fd, file_size) |
---|
[172] | 1265 | #except util.TracError, detail: |
---|
| 1266 | # print detail |
---|
[73] | 1267 | |
---|
[103] | 1268 | # Remove the created temporary filename |
---|
| 1269 | # |
---|
[172] | 1270 | fd.close() |
---|
[103] | 1271 | os.unlink(path) |
---|
| 1272 | |
---|
[77] | 1273 | # Return how many attachments |
---|
| 1274 | # |
---|
[153] | 1275 | status = 'This message has %d attachment(s)\n%s' %(count, status) |
---|
| 1276 | return status |
---|
[22] | 1277 | |
---|
[77] | 1278 | |
---|
[22] | 1279 | def mkdir_p(dir, mode): |
---|
| 1280 | '''do a mkdir -p''' |
---|
| 1281 | |
---|
| 1282 | arr = string.split(dir, '/') |
---|
| 1283 | path = '' |
---|
| 1284 | for part in arr: |
---|
| 1285 | path = '%s/%s' % (path, part) |
---|
| 1286 | try: |
---|
| 1287 | stats = os.stat(path) |
---|
| 1288 | except OSError: |
---|
| 1289 | os.mkdir(path, mode) |
---|
| 1290 | |
---|
| 1291 | def ReadConfig(file, name): |
---|
| 1292 | """ |
---|
| 1293 | Parse the config file |
---|
| 1294 | """ |
---|
| 1295 | if not os.path.isfile(file): |
---|
[79] | 1296 | print 'File %s does not exist' %file |
---|
[22] | 1297 | sys.exit(1) |
---|
| 1298 | |
---|
[199] | 1299 | config = trac_config.Configuration(file) |
---|
[22] | 1300 | |
---|
| 1301 | # Use given project name else use defaults |
---|
| 1302 | # |
---|
| 1303 | if name: |
---|
[199] | 1304 | sections = config.sections() |
---|
| 1305 | if not name in sections: |
---|
[79] | 1306 | print "Not a valid project name: %s" %name |
---|
[199] | 1307 | print "Valid names: %s" %sections |
---|
[22] | 1308 | sys.exit(1) |
---|
| 1309 | |
---|
| 1310 | project = dict() |
---|
[199] | 1311 | for option, value in config.options(name): |
---|
| 1312 | project[option] = value |
---|
[22] | 1313 | |
---|
| 1314 | else: |
---|
[217] | 1315 | # use some trac internales to get the defaults |
---|
| 1316 | # |
---|
| 1317 | project = config.parser.defaults() |
---|
[22] | 1318 | |
---|
| 1319 | return project |
---|
| 1320 | |
---|
[87] | 1321 | |
---|
[22] | 1322 | if __name__ == '__main__': |
---|
| 1323 | # Default config file |
---|
| 1324 | # |
---|
[24] | 1325 | configfile = '@email2trac_conf@' |
---|
[22] | 1326 | project = '' |
---|
| 1327 | component = '' |
---|
[202] | 1328 | ticket_prefix = 'default' |
---|
[204] | 1329 | dry_run = None |
---|
[202] | 1330 | |
---|
[87] | 1331 | ENABLE_SYSLOG = 0 |
---|
[201] | 1332 | |
---|
[204] | 1333 | |
---|
[202] | 1334 | SHORT_OPT = 'chf:np:t:' |
---|
| 1335 | LONG_OPT = ['component=', 'dry-run', 'help', 'file=', 'project=', 'ticket_prefix='] |
---|
[201] | 1336 | |
---|
[22] | 1337 | try: |
---|
[201] | 1338 | opts, args = getopt.getopt(sys.argv[1:], SHORT_OPT, LONG_OPT) |
---|
[22] | 1339 | except getopt.error,detail: |
---|
| 1340 | print __doc__ |
---|
| 1341 | print detail |
---|
| 1342 | sys.exit(1) |
---|
[87] | 1343 | |
---|
[22] | 1344 | project_name = None |
---|
| 1345 | for opt,value in opts: |
---|
| 1346 | if opt in [ '-h', '--help']: |
---|
| 1347 | print __doc__ |
---|
| 1348 | sys.exit(0) |
---|
| 1349 | elif opt in ['-c', '--component']: |
---|
| 1350 | component = value |
---|
| 1351 | elif opt in ['-f', '--file']: |
---|
| 1352 | configfile = value |
---|
[201] | 1353 | elif opt in ['-n', '--dry-run']: |
---|
[204] | 1354 | dry_run = True |
---|
[22] | 1355 | elif opt in ['-p', '--project']: |
---|
| 1356 | project_name = value |
---|
[202] | 1357 | elif opt in ['-t', '--ticket_prefix']: |
---|
| 1358 | ticket_prefix = value |
---|
[87] | 1359 | |
---|
[22] | 1360 | settings = ReadConfig(configfile, project_name) |
---|
| 1361 | if not settings.has_key('project'): |
---|
| 1362 | print __doc__ |
---|
[79] | 1363 | print 'No Trac project is defined in the email2trac config file.' |
---|
[22] | 1364 | sys.exit(1) |
---|
[87] | 1365 | |
---|
[22] | 1366 | if component: |
---|
| 1367 | settings['component'] = component |
---|
[202] | 1368 | |
---|
| 1369 | # The default prefix for ticket values in email2trac.conf |
---|
| 1370 | # |
---|
| 1371 | settings['ticket_prefix'] = ticket_prefix |
---|
[206] | 1372 | settings['dry_run'] = dry_run |
---|
[87] | 1373 | |
---|
[22] | 1374 | if settings.has_key('trac_version'): |
---|
[189] | 1375 | version = settings['trac_version'] |
---|
[22] | 1376 | else: |
---|
| 1377 | version = trac_default_version |
---|
| 1378 | |
---|
[189] | 1379 | |
---|
[22] | 1380 | #debug HvB |
---|
| 1381 | #print settings |
---|
[189] | 1382 | |
---|
[87] | 1383 | try: |
---|
[189] | 1384 | if version == '0.9': |
---|
[87] | 1385 | from trac import attachment |
---|
| 1386 | from trac.env import Environment |
---|
| 1387 | from trac.ticket import Ticket |
---|
| 1388 | from trac.web.href import Href |
---|
| 1389 | from trac import util |
---|
| 1390 | from trac.Notify import TicketNotifyEmail |
---|
[189] | 1391 | elif version == '0.10': |
---|
[87] | 1392 | from trac import attachment |
---|
| 1393 | from trac.env import Environment |
---|
| 1394 | from trac.ticket import Ticket |
---|
| 1395 | from trac.web.href import Href |
---|
| 1396 | from trac import util |
---|
[139] | 1397 | # |
---|
| 1398 | # return util.text.to_unicode(str) |
---|
| 1399 | # |
---|
[87] | 1400 | # see http://projects.edgewall.com/trac/changeset/2799 |
---|
| 1401 | from trac.ticket.notification import TicketNotifyEmail |
---|
[199] | 1402 | from trac import config as trac_config |
---|
[189] | 1403 | elif version == '0.11': |
---|
[182] | 1404 | from trac import attachment |
---|
| 1405 | from trac.env import Environment |
---|
| 1406 | from trac.ticket import Ticket |
---|
| 1407 | from trac.web.href import Href |
---|
[199] | 1408 | from trac import config as trac_config |
---|
[182] | 1409 | from trac import util |
---|
| 1410 | # |
---|
| 1411 | # return util.text.to_unicode(str) |
---|
| 1412 | # |
---|
| 1413 | # see http://projects.edgewall.com/trac/changeset/2799 |
---|
| 1414 | from trac.ticket.notification import TicketNotifyEmail |
---|
[189] | 1415 | else: |
---|
| 1416 | print 'TRAC version %s is not supported' %version |
---|
| 1417 | sys.exit(1) |
---|
| 1418 | |
---|
| 1419 | if settings.has_key('enable_syslog'): |
---|
[190] | 1420 | if SYSLOG_AVAILABLE: |
---|
| 1421 | ENABLE_SYSLOG = float(settings['enable_syslog']) |
---|
[182] | 1422 | |
---|
[87] | 1423 | env = Environment(settings['project'], create=0) |
---|
[206] | 1424 | tktparser = TicketEmailParser(env, settings, float(version)) |
---|
[87] | 1425 | tktparser.parse(sys.stdin) |
---|
[22] | 1426 | |
---|
[87] | 1427 | # Catch all errors ans log to SYSLOG if we have enabled this |
---|
| 1428 | # else stdout |
---|
| 1429 | # |
---|
| 1430 | except Exception, error: |
---|
| 1431 | if ENABLE_SYSLOG: |
---|
| 1432 | syslog.openlog('email2trac', syslog.LOG_NOWAIT) |
---|
[187] | 1433 | |
---|
[87] | 1434 | etype, evalue, etb = sys.exc_info() |
---|
| 1435 | for e in traceback.format_exception(etype, evalue, etb): |
---|
| 1436 | syslog.syslog(e) |
---|
[187] | 1437 | |
---|
[87] | 1438 | syslog.closelog() |
---|
| 1439 | else: |
---|
| 1440 | traceback.print_exc() |
---|
[22] | 1441 | |
---|
[97] | 1442 | if m: |
---|
[98] | 1443 | tktparser.save_email_for_debug(m, True) |
---|
[97] | 1444 | |
---|
[22] | 1445 | # EOB |
---|