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