[22] | 1 | #!@PYTHON@ |
---|
| 2 | # |
---|
| 3 | # This file is part of the email2trac utils |
---|
| 4 | # |
---|
[595] | 5 | # Copyright 2002 SARA |
---|
[22] | 6 | # |
---|
[595] | 7 | # Licensed under the Apache License, Version 2.0 (the "License"); |
---|
| 8 | # you may not use this file except in compliance with the License. |
---|
| 9 | # You may obtain a copy of the License at |
---|
[22] | 10 | # |
---|
[595] | 11 | # http://www.apache.org/licenses/LICENSE-2.0 |
---|
[22] | 12 | # |
---|
[595] | 13 | # Unless required by applicable law or agreed to in writing, software |
---|
| 14 | # distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 15 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 16 | # See the License for the specific language governing permissions and |
---|
| 17 | # limitations under the License. |
---|
| 18 | # |
---|
[80] | 19 | # For vi/emacs or other use tabstop=4 (vi: set ts=4) |
---|
| 20 | # |
---|
[22] | 21 | """ |
---|
[547] | 22 | email2trac.py -- Email -> TRAC tickets |
---|
[22] | 23 | |
---|
[547] | 24 | A MTA filter to create Trac tickets from inbound emails. |
---|
[22] | 25 | |
---|
[547] | 26 | first proof of concept from: |
---|
| 27 | Copyright 2005, Daniel Lundin <daniel@edgewall.com> |
---|
| 28 | Copyright 2005, Edgewall Software |
---|
[22] | 29 | |
---|
[282] | 30 | Authors: |
---|
| 31 | Bas van der Vlies <basv@sara.nl> |
---|
| 32 | Walter de Jong <walter@sara.nl> |
---|
[22] | 33 | |
---|
| 34 | How to use |
---|
| 35 | ---------- |
---|
[282] | 36 | * See https://subtrac.sara.nl/oss/email2trac/ |
---|
| 37 | |
---|
[22] | 38 | * Commandline opions: |
---|
[205] | 39 | -h,--help |
---|
[507] | 40 | -d, --debug |
---|
[205] | 41 | -f,--file <configuration file> |
---|
[410] | 42 | -n,--dry-run |
---|
[205] | 43 | -p, --project <project name> |
---|
[415] | 44 | -t, --ticket_prefix <name> |
---|
[22] | 45 | |
---|
| 46 | SVN Info: |
---|
| 47 | $Id: email2trac.py.in 604 2012-08-30 11:55:54Z bas $ |
---|
| 48 | """ |
---|
| 49 | import os |
---|
| 50 | import sys |
---|
| 51 | import string |
---|
| 52 | import getopt |
---|
| 53 | import time |
---|
| 54 | import email |
---|
[136] | 55 | import email.Iterators |
---|
| 56 | import email.Header |
---|
[22] | 57 | import re |
---|
| 58 | import urllib |
---|
| 59 | import unicodedata |
---|
| 60 | import mimetypes |
---|
[96] | 61 | import traceback |
---|
[403] | 62 | import logging |
---|
| 63 | import logging.handlers |
---|
[404] | 64 | import UserDict |
---|
[498] | 65 | import tempfile |
---|
[433] | 66 | |
---|
[520] | 67 | from datetime import timedelta, datetime |
---|
[433] | 68 | from stat import * |
---|
[22] | 69 | |
---|
[593] | 70 | try: |
---|
| 71 | from trac import __version__ as trac_version |
---|
| 72 | from trac import config as trac_config |
---|
[403] | 73 | |
---|
[593] | 74 | except ImportError, detail: |
---|
| 75 | print "Can not find a a valid trac installation, try setting PYTHONPATH" |
---|
| 76 | sys.exit(1) |
---|
[91] | 77 | |
---|
[508] | 78 | ## Some global variables |
---|
[96] | 79 | # |
---|
| 80 | m = None |
---|
[22] | 81 | |
---|
[508] | 82 | # This is to for the function AlwaysNotifyReporter |
---|
| 83 | sender_email = None |
---|
| 84 | |
---|
[404] | 85 | class SaraDict(UserDict.UserDict): |
---|
[507] | 86 | def __init__(self, dictin = None): |
---|
| 87 | UserDict.UserDict.__init__(self) |
---|
| 88 | self.name = None |
---|
| 89 | |
---|
| 90 | if dictin: |
---|
| 91 | if dictin.has_key('name'): |
---|
| 92 | self.name = dictin['name'] |
---|
| 93 | del dictin['name'] |
---|
| 94 | self.data = dictin |
---|
| 95 | |
---|
| 96 | def get_value(self, name): |
---|
| 97 | if self.has_key(name): |
---|
| 98 | return self[name] |
---|
| 99 | else: |
---|
| 100 | return None |
---|
| 101 | |
---|
| 102 | def __repr__(self): |
---|
| 103 | return repr(self.data) |
---|
[182] | 104 | |
---|
[507] | 105 | def __str__(self): |
---|
| 106 | return str(self.data) |
---|
| 107 | |
---|
| 108 | def __getattr__(self, name): |
---|
| 109 | """ |
---|
| 110 | override the class attribute get method. Return the value |
---|
| 111 | from the dictionary |
---|
| 112 | """ |
---|
| 113 | if self.data.has_key(name): |
---|
| 114 | return self.data[name] |
---|
| 115 | else: |
---|
| 116 | return None |
---|
| 117 | |
---|
| 118 | def __setattr__(self, name, value): |
---|
| 119 | """ |
---|
| 120 | override the class attribute set method only when the UserDict |
---|
| 121 | has set its class attribute |
---|
| 122 | """ |
---|
| 123 | if self.__dict__.has_key('data'): |
---|
| 124 | self.data[name] = value |
---|
| 125 | else: |
---|
| 126 | self.__dict__[name] = value |
---|
[404] | 127 | |
---|
[507] | 128 | def __iter__(self): |
---|
| 129 | return iter(self.data.keys()) |
---|
[404] | 130 | |
---|
[22] | 131 | class TicketEmailParser(object): |
---|
[507] | 132 | env = None |
---|
| 133 | comment = '> ' |
---|
[359] | 134 | |
---|
[507] | 135 | def __init__(self, env, parameters, logger, version): |
---|
| 136 | self.env = env |
---|
[22] | 137 | |
---|
[507] | 138 | # Database connection |
---|
| 139 | # |
---|
| 140 | self.db = None |
---|
[22] | 141 | |
---|
[507] | 142 | # Save parameters |
---|
| 143 | # |
---|
| 144 | self.parameters = parameters |
---|
| 145 | self.logger = logger |
---|
[206] | 146 | |
---|
[507] | 147 | # Some useful mail constants |
---|
| 148 | # |
---|
| 149 | self.email_name = None |
---|
| 150 | self.email_addr = None |
---|
| 151 | self.email_from = None |
---|
| 152 | self.author = None |
---|
| 153 | self.id = None |
---|
[587] | 154 | |
---|
| 155 | ## Will be set to True if the user has ben registered |
---|
| 156 | # |
---|
| 157 | self.allow_registered_user = False |
---|
[507] | 158 | |
---|
| 159 | self.STRIP_CONTENT_TYPES = list() |
---|
[72] | 160 | |
---|
[507] | 161 | ## fields properties via body_text |
---|
| 162 | # |
---|
| 163 | self.properties = dict() |
---|
[452] | 164 | |
---|
[507] | 165 | self.VERSION = version |
---|
[402] | 166 | |
---|
[507] | 167 | self.get_config = self.env.config.get |
---|
[404] | 168 | |
---|
[507] | 169 | ## init function ## |
---|
| 170 | # |
---|
| 171 | self.setup_parameters() |
---|
[402] | 172 | |
---|
[507] | 173 | def setup_parameters(self): |
---|
| 174 | if self.parameters.umask: |
---|
| 175 | os.umask(self.parameters.umask) |
---|
[402] | 176 | |
---|
[507] | 177 | if not self.parameters.spam_level: |
---|
| 178 | self.parameters.spam_level = 0 |
---|
[22] | 179 | |
---|
[507] | 180 | if not self.parameters.spam_header: |
---|
| 181 | self.parameters.spam_header = 'X-Spam-Score' |
---|
[207] | 182 | |
---|
[507] | 183 | if not self.parameters.email_quote: |
---|
| 184 | self.parameters.email_quote = '> ' |
---|
[22] | 185 | |
---|
[507] | 186 | if not self.parameters.ticket_update_by_subject_lookback: |
---|
| 187 | self.parameters.ticket_update_by_subject_lookback = 30 |
---|
[360] | 188 | |
---|
[507] | 189 | if self.parameters.verbatim_format == None: |
---|
| 190 | self.parameters.verbatim_format = 1 |
---|
[118] | 191 | |
---|
[507] | 192 | if self.parameters.reflow == None: |
---|
| 193 | self.parameters.reflow = 1 |
---|
[231] | 194 | |
---|
[507] | 195 | if self.parameters.binhex: |
---|
| 196 | self.STRIP_CONTENT_TYPES.append('application/mac-binhex40') |
---|
[238] | 197 | |
---|
[507] | 198 | if self.parameters.applesingle: |
---|
| 199 | self.STRIP_CONTENT_TYPES.append('application/applefile') |
---|
[238] | 200 | |
---|
[507] | 201 | if self.parameters.appledouble: |
---|
| 202 | self.STRIP_CONTENT_TYPES.append('application/applefile') |
---|
[238] | 203 | |
---|
[507] | 204 | if self.parameters.strip_content_types: |
---|
| 205 | items = self.parameters.strip_content_types.split(',') |
---|
| 206 | for item in items: |
---|
| 207 | self.STRIP_CONTENT_TYPES.append(item.strip()) |
---|
[294] | 208 | |
---|
[507] | 209 | if self.parameters.tmpdir: |
---|
| 210 | self.parameters.tmpdir = os.path.normcase(str(self.parameters['tmpdir'])) |
---|
| 211 | else: |
---|
| 212 | self.parameters.tmpdir = os.path.normcase('/tmp') |
---|
[257] | 213 | |
---|
[507] | 214 | if self.parameters.email_triggers_workflow == None: |
---|
| 215 | self.parameters.email_triggers_workflow = 1 |
---|
[191] | 216 | |
---|
[507] | 217 | if not self.parameters.subject_field_separator: |
---|
| 218 | self.parameters.subject_field_separator = '&' |
---|
| 219 | else: |
---|
| 220 | self.parameters.subject_field_separator = self.parameters.subject_field_separator.strip() |
---|
[297] | 221 | |
---|
[604] | 222 | #if self.parameters.only_strip_on_update == None: |
---|
| 223 | # self.parameters.only_strip_on_update == False: |
---|
| 224 | #else: |
---|
| 225 | # self.parameters.only_strip_on_update == True: |
---|
| 226 | |
---|
[507] | 227 | self.trac_smtp_from = self.get_config('notification', 'smtp_from') |
---|
| 228 | self.smtp_default_domain = self.get_config('notification', 'smtp_default_domain') |
---|
[305] | 229 | |
---|
[484] | 230 | |
---|
[507] | 231 | self.system = None |
---|
[359] | 232 | |
---|
[341] | 233 | ########## Email Header Functions ########################################################### |
---|
[339] | 234 | |
---|
[507] | 235 | def spam(self, message): |
---|
| 236 | """ |
---|
| 237 | # X-Spam-Score: *** (3.255) BAYES_50,DNS_FROM_AHBL_RHSBL,HTML_ |
---|
| 238 | # Note if Spam_level then '*' are included |
---|
| 239 | """ |
---|
[580] | 240 | self.logger.debug('function spam') |
---|
| 241 | |
---|
[507] | 242 | spam = False |
---|
| 243 | if message.has_key(self.parameters.spam_header): |
---|
| 244 | spam_l = string.split(message[self.parameters.spam_header]) |
---|
[578] | 245 | #self.logger.info('Spam header: %s' %(message[self.parameters.spam_header])) |
---|
[22] | 246 | |
---|
[507] | 247 | try: |
---|
| 248 | number = spam_l[0].count('*') |
---|
| 249 | except IndexError, detail: |
---|
| 250 | number = 0 |
---|
| 251 | |
---|
| 252 | if number >= self.parameters.spam_level: |
---|
| 253 | spam = True |
---|
| 254 | |
---|
[580] | 255 | ## treat virus mails as spam |
---|
[507] | 256 | # |
---|
| 257 | elif message.has_key('X-Virus-found'): |
---|
| 258 | spam = True |
---|
[194] | 259 | |
---|
[580] | 260 | ## Astaro firewall spam handling |
---|
[507] | 261 | # |
---|
[580] | 262 | elif message.get('X-Spam-Flag') == "YES" and message.get('X-Spam-Result') == "Spam": |
---|
| 263 | spam = True |
---|
| 264 | |
---|
| 265 | ## How to handle SPAM messages |
---|
| 266 | # |
---|
[507] | 267 | if self.parameters.drop_spam and spam: |
---|
[194] | 268 | |
---|
[507] | 269 | self.logger.info('Message is a SPAM. Automatic ticket insertion refused (SPAM level > %d)' %self.parameters.spam_level) |
---|
| 270 | return 'drop' |
---|
[194] | 271 | |
---|
[507] | 272 | elif spam: |
---|
[194] | 273 | |
---|
[507] | 274 | return 'Spam' |
---|
| 275 | else: |
---|
[22] | 276 | |
---|
[507] | 277 | return False |
---|
[191] | 278 | |
---|
[572] | 279 | def email_header_acl(self, keyword, message_field, default): |
---|
[507] | 280 | """ |
---|
| 281 | This function wil check if the email address is allowed or denied |
---|
| 282 | to send mail to the ticket list |
---|
| 283 | """ |
---|
| 284 | self.logger.debug('function email_header_acl: %s' %keyword) |
---|
[409] | 285 | |
---|
[507] | 286 | try: |
---|
| 287 | mail_addresses = self.parameters[keyword] |
---|
[221] | 288 | |
---|
[507] | 289 | # Check if we have an empty string |
---|
| 290 | # |
---|
| 291 | if not mail_addresses: |
---|
| 292 | return default |
---|
[221] | 293 | |
---|
[507] | 294 | except KeyError, detail: |
---|
[572] | 295 | self.logger.debug('\t %s not defined, all messages are allowed.' %(keyword)) |
---|
[206] | 296 | |
---|
[507] | 297 | return default |
---|
[206] | 298 | |
---|
[507] | 299 | mail_addresses = string.split(mail_addresses, ',') |
---|
[572] | 300 | message_addresses = string.split(message_field, ',') |
---|
[221] | 301 | |
---|
[507] | 302 | for entry in mail_addresses: |
---|
| 303 | entry = entry.strip() |
---|
| 304 | TO_RE = re.compile(entry, re.VERBOSE|re.IGNORECASE) |
---|
[149] | 305 | |
---|
[572] | 306 | for addr in message_addresses: |
---|
| 307 | addr = addr.strip() |
---|
| 308 | |
---|
| 309 | if self.parameters.compare_function_list in [ 'matches', 'match']: |
---|
| 310 | s = '\t%s matches %s' %(addr, entry) |
---|
| 311 | result = TO_RE.match(addr) |
---|
| 312 | else: |
---|
| 313 | s = '\t%s contains %s' %(addr, entry) |
---|
| 314 | result = TO_RE.search(addr) |
---|
| 315 | |
---|
| 316 | if result: |
---|
| 317 | self.logger.debug(s) |
---|
| 318 | return True |
---|
| 319 | |
---|
[507] | 320 | return False |
---|
[208] | 321 | |
---|
[507] | 322 | def email_header_txt(self, m): |
---|
| 323 | """ |
---|
| 324 | Display To and CC addresses in description field |
---|
| 325 | """ |
---|
| 326 | s = '' |
---|
[334] | 327 | |
---|
[507] | 328 | if m['To'] and len(m['To']) > 0: |
---|
| 329 | s = "'''To:''' %s\r\n" %(m['To']) |
---|
| 330 | if m['Cc'] and len(m['Cc']) > 0: |
---|
| 331 | s = "%s'''Cc:''' %s\r\n" % (s, m['Cc']) |
---|
[22] | 332 | |
---|
[507] | 333 | return self.email_to_unicode(s) |
---|
[22] | 334 | |
---|
[138] | 335 | |
---|
[507] | 336 | def get_sender_info(self, message): |
---|
| 337 | """ |
---|
| 338 | Get the default author name and email address from the message |
---|
| 339 | """ |
---|
[576] | 340 | self.logger.debug('function get_sender_info') |
---|
[43] | 341 | |
---|
[570] | 342 | to_addrs = email.Utils.getaddresses( message.get_all('to', []) ) |
---|
| 343 | self.email_to_addrs = list() |
---|
[226] | 344 | |
---|
[570] | 345 | for n,e in to_addrs: |
---|
| 346 | self.email_to_addrs.append(e) |
---|
| 347 | |
---|
| 348 | self.email_to_addrs = ','.join(self.email_to_addrs) |
---|
| 349 | |
---|
| 350 | |
---|
[576] | 351 | self.email_name, self.email_addr = email.Utils.parseaddr(message['from']) |
---|
[142] | 352 | |
---|
[576] | 353 | ## decode email name can contain charset |
---|
| 354 | # |
---|
| 355 | self.email_name = self.email_to_unicode(self.email_name) |
---|
| 356 | |
---|
| 357 | dstr = '\t email name: %s, email address: %s' %(self.email_name, self.email_addr) |
---|
| 358 | self.logger.debug(dstr) |
---|
| 359 | |
---|
[507] | 360 | ## Trac can not handle author's name that contains spaces |
---|
| 361 | # |
---|
[518] | 362 | if self.email_addr.lower() == self.trac_smtp_from.lower(): |
---|
[507] | 363 | if self.email_name: |
---|
| 364 | self.author = self.email_name |
---|
| 365 | else: |
---|
| 366 | self.author = "email2trac" |
---|
| 367 | else: |
---|
| 368 | self.author = self.email_addr |
---|
[304] | 369 | |
---|
[507] | 370 | if self.parameters.ignore_trac_user_settings: |
---|
| 371 | return |
---|
[194] | 372 | |
---|
[507] | 373 | # Is this a registered user, use email address as search key: |
---|
| 374 | # result: |
---|
| 375 | # u : login name |
---|
| 376 | # n : Name that the user has set in the settings tab |
---|
| 377 | # e : email address that the user has set in the settings tab |
---|
| 378 | # |
---|
| 379 | users = [ (u,n,e) for (u, n, e) in self.env.get_known_users(self.db) |
---|
| 380 | if ( |
---|
| 381 | (e and (e.lower() == self.email_addr.lower())) or |
---|
[518] | 382 | (u.lower() + '@' + self.smtp_default_domain.lower() == self.email_addr.lower()) |
---|
[507] | 383 | ) |
---|
| 384 | ] |
---|
[43] | 385 | |
---|
[507] | 386 | if len(users) >= 1: |
---|
[587] | 387 | self.email_from = users[0][2] |
---|
[507] | 388 | self.author = users[0][0] |
---|
[45] | 389 | |
---|
[590] | 390 | self.logger.debug('\tget_sender_info: found registered user: %s' %(self.author) ) |
---|
| 391 | |
---|
[587] | 392 | if self.parameters.white_list_registered_users: |
---|
[590] | 393 | self.logger.debug('\tget_sender_info: added registered user to white list') |
---|
[587] | 394 | self.allow_registered_user = True |
---|
| 395 | |
---|
| 396 | |
---|
[597] | 397 | def set_cc_fields(self, ticket, message, update=False): |
---|
[507] | 398 | """ |
---|
| 399 | Set all the right fields for a new ticket |
---|
| 400 | """ |
---|
[597] | 401 | self.logger.debug('function set_cc_fields') |
---|
[72] | 402 | |
---|
[270] | 403 | |
---|
[507] | 404 | # Put all CC-addresses in ticket CC field |
---|
| 405 | # |
---|
| 406 | if self.parameters.reply_all: |
---|
[43] | 407 | |
---|
[507] | 408 | email_cc = '' |
---|
[299] | 409 | |
---|
[507] | 410 | cc_addrs = email.Utils.getaddresses( message.get_all('cc', []) ) |
---|
[299] | 411 | |
---|
[507] | 412 | if not cc_addrs: |
---|
| 413 | return |
---|
[43] | 414 | |
---|
[507] | 415 | ## Build a list of forbidden CC addresses |
---|
| 416 | # |
---|
| 417 | #to_addrs = email.Utils.getaddresses( message.get_all('to', []) ) |
---|
| 418 | #to_list = list() |
---|
| 419 | #for n,e in to_addrs: |
---|
| 420 | # to_list.append(e) |
---|
[43] | 421 | |
---|
[597] | 422 | if update: |
---|
| 423 | self.logger.debug("\tupdate cc-fields") |
---|
| 424 | |
---|
[507] | 425 | for name,addr in cc_addrs: |
---|
| 426 | |
---|
| 427 | ## Prevent mail loop |
---|
| 428 | # |
---|
| 429 | if addr == self.trac_smtp_from: |
---|
[597] | 430 | self.logger.debug("\tSkipping %s email address for CC-field, same as smtp_from address in trac.ini " %(addr)) |
---|
[507] | 431 | continue |
---|
[43] | 432 | |
---|
[597] | 433 | ## Alwyas remove reporter email address |
---|
| 434 | # |
---|
| 435 | elif addr == self.email_addr: |
---|
| 436 | self.logger.debug("\tSkipping reporter email address for CC-field") |
---|
| 437 | continue |
---|
| 438 | |
---|
[507] | 439 | else: |
---|
[597] | 440 | if email_cc: |
---|
| 441 | email_cc = '%s, %s' %(email_cc, addr) |
---|
| 442 | else: |
---|
| 443 | email_cc = addr |
---|
[96] | 444 | |
---|
[507] | 445 | if email_cc: |
---|
[597] | 446 | self.logger.debug('\tCC fields: %s' %email_cc) |
---|
[507] | 447 | ticket['cc'] = self.email_to_unicode(email_cc) |
---|
[299] | 448 | |
---|
[339] | 449 | |
---|
[588] | 450 | def acl_list_from_file(self, f, keyword): |
---|
| 451 | """ |
---|
| 452 | Read the email address from a file |
---|
| 453 | """ |
---|
| 454 | self.logger.debug('function acl_list_from_file %s : %s' %(f, keyword)) |
---|
| 455 | |
---|
| 456 | if not os.path.isfile(f): |
---|
| 457 | self.logger.error('%s_file: %s does not exists' %(keyword, f) ) |
---|
| 458 | else: |
---|
| 459 | ## read whole file and replace '\n' with '' |
---|
| 460 | # |
---|
| 461 | addr_l = open(f, 'r').readlines() |
---|
| 462 | s = ','.join(addr_l).replace('\n','') |
---|
| 463 | |
---|
| 464 | try: |
---|
| 465 | self.parameters[keyword] = "%s,%s" %(self.parameters[keyword], s) |
---|
| 466 | except KeyError, detail: |
---|
| 467 | self.parameters[keyword] = s |
---|
| 468 | |
---|
[339] | 469 | ########## DEBUG functions ########################################################### |
---|
| 470 | |
---|
[555] | 471 | def debug_body(self, message_body, temp_file): |
---|
| 472 | """ |
---|
| 473 | """ |
---|
| 474 | self.logger.debug('function debug_body:') |
---|
[310] | 475 | |
---|
[555] | 476 | body_file = "%s.body" %(temp_file) |
---|
| 477 | fx = open(body_file, 'wb') |
---|
| 478 | |
---|
[507] | 479 | if self.parameters.dry_run: |
---|
[561] | 480 | self.logger.info('DRY-RUN: not saving body to %s' %(body_file)) |
---|
[507] | 481 | return |
---|
[331] | 482 | |
---|
[555] | 483 | self.logger.debug('writing body to %s' %(body_file)) |
---|
[507] | 484 | if not message_body: |
---|
| 485 | message_body = '(None)' |
---|
[310] | 486 | |
---|
[507] | 487 | message_body = message_body.encode('utf-8') |
---|
[310] | 488 | |
---|
[507] | 489 | fx.write(message_body) |
---|
| 490 | fx.close() |
---|
| 491 | try: |
---|
| 492 | os.chmod(body_file,S_IRWXU|S_IRWXG|S_IRWXO) |
---|
| 493 | except OSError: |
---|
| 494 | pass |
---|
[310] | 495 | |
---|
[555] | 496 | def debug_attachments(self, message_parts, temp_file): |
---|
[507] | 497 | """ |
---|
| 498 | """ |
---|
| 499 | self.logger.debug('function debug_attachments') |
---|
| 500 | |
---|
| 501 | n = 0 |
---|
| 502 | for item in message_parts: |
---|
[553] | 503 | |
---|
| 504 | ## Skip inline text parts |
---|
| 505 | # |
---|
[507] | 506 | if not isinstance(item, tuple): |
---|
| 507 | continue |
---|
| 508 | |
---|
| 509 | (original, filename, part) = item |
---|
[555] | 510 | self.logger.debug('\t part%d: Content-Type: %s' % (n, part.get_content_type()) ) |
---|
[310] | 511 | |
---|
[555] | 512 | dummy_filename, ext = os.path.splitext(filename) |
---|
| 513 | |
---|
[507] | 514 | n = n + 1 |
---|
[555] | 515 | part_name = 'part%d%s' %(n, ext) |
---|
| 516 | part_file = "%s.%s" %(temp_file, part_name) |
---|
| 517 | |
---|
| 518 | s = 'writing %s: filename: %s' %(part_file, filename) |
---|
[507] | 519 | self.print_unicode(s) |
---|
[555] | 520 | |
---|
| 521 | ## Forbidden chars, just write part files instead of names |
---|
[507] | 522 | # |
---|
[555] | 523 | #filename = filename.replace('\\', '_') |
---|
| 524 | #filename = filename.replace('/', '_') |
---|
| 525 | #filename = filename + '.att_email2trac' |
---|
| 526 | # part_file = os.path.join(self.parameters.tmpdir, filename) |
---|
| 527 | #part_file = util.text.unicode_quote(part_file) |
---|
| 528 | #self.print_unicode(part_file) |
---|
[331] | 529 | |
---|
[507] | 530 | if self.parameters.dry_run: |
---|
[561] | 531 | self.logger.info('DRY_RUN: NOT saving attachments') |
---|
[507] | 532 | continue |
---|
[331] | 533 | |
---|
[507] | 534 | fx = open(part_file, 'wb') |
---|
| 535 | text = part.get_payload(decode=1) |
---|
[331] | 536 | |
---|
[507] | 537 | if not text: |
---|
| 538 | text = '(None)' |
---|
[331] | 539 | |
---|
[507] | 540 | fx.write(text) |
---|
| 541 | fx.close() |
---|
[331] | 542 | |
---|
[507] | 543 | try: |
---|
| 544 | os.chmod(part_file,S_IRWXU|S_IRWXG|S_IRWXO) |
---|
| 545 | except OSError: |
---|
| 546 | pass |
---|
[310] | 547 | |
---|
[528] | 548 | def save_email_for_debug(self, message, project_name, create_tempfile=False): |
---|
[309] | 549 | |
---|
[528] | 550 | if self.parameters.dry_run: |
---|
[555] | 551 | self.logger.debug('DRY_RUN: NOT saving email message') |
---|
[528] | 552 | return |
---|
| 553 | |
---|
[507] | 554 | if create_tempfile: |
---|
[555] | 555 | (fd, tmp_file) = tempfile.mkstemp('.%s.email2trac' %project_name) |
---|
[514] | 556 | fx = os.fdopen(fd, 'wb') |
---|
[507] | 557 | else: |
---|
[555] | 558 | tmp_file = os.path.join(self.parameters.tmpdir, 'msg.%s.email2trac' %project_name) |
---|
| 559 | fx = open(tmp_file, 'wb') |
---|
[173] | 560 | |
---|
[44] | 561 | |
---|
[555] | 562 | self.logger.debug('saving email to %s' %(tmp_file)) |
---|
[528] | 563 | fx.write('%s' % message) |
---|
| 564 | fx.close() |
---|
| 565 | |
---|
| 566 | try: |
---|
[555] | 567 | os.chmod(tmp_file, S_IRWXU|S_IRWXG|S_IRWXO) |
---|
[528] | 568 | except OSError: |
---|
| 569 | pass |
---|
[331] | 570 | |
---|
[507] | 571 | message_parts = self.get_message_parts(message) |
---|
| 572 | message_parts = self.unique_attachment_names(message_parts) |
---|
| 573 | body_text = self.get_body_text(message_parts) |
---|
[555] | 574 | self.debug_body(body_text, tmp_file) |
---|
| 575 | self.debug_attachments(message_parts, tmp_file) |
---|
[309] | 576 | |
---|
[339] | 577 | ########## Conversion functions ########################################################### |
---|
| 578 | |
---|
[507] | 579 | def email_to_unicode(self, message_str): |
---|
| 580 | """ |
---|
| 581 | Email has 7 bit ASCII code, convert it to unicode with the charset |
---|
| 582 | that is encoded in 7-bit ASCII code and encode it as utf-8 so Trac |
---|
| 583 | understands it. |
---|
| 584 | """ |
---|
| 585 | self.logger.debug("function email_to_unicode") |
---|
[341] | 586 | |
---|
[507] | 587 | results = email.Header.decode_header(message_str) |
---|
[341] | 588 | |
---|
[507] | 589 | s = None |
---|
| 590 | for text,format in results: |
---|
| 591 | if format: |
---|
| 592 | try: |
---|
| 593 | temp = unicode(text, format) |
---|
| 594 | except UnicodeError, detail: |
---|
| 595 | # This always works |
---|
| 596 | # |
---|
[525] | 597 | msg = 'ERROR: Could not find charset: %s, please install' %format |
---|
| 598 | self.logger.error(msg) |
---|
[507] | 599 | temp = unicode(text, 'iso-8859-15') |
---|
| 600 | except LookupError, detail: |
---|
[514] | 601 | msg = 'ERROR: Could not find charset: %s, please install' %format |
---|
[525] | 602 | self.logger.error(msg) |
---|
[507] | 603 | #temp = unicode(text, 'iso-8859-15') |
---|
| 604 | temp = message_str |
---|
| 605 | |
---|
| 606 | else: |
---|
| 607 | temp = string.strip(text) |
---|
| 608 | temp = unicode(text, 'iso-8859-15') |
---|
[341] | 609 | |
---|
[507] | 610 | if s: |
---|
| 611 | s = '%s %s' %(s, temp) |
---|
| 612 | else: |
---|
| 613 | s = '%s' %temp |
---|
[341] | 614 | |
---|
[507] | 615 | return s |
---|
[341] | 616 | |
---|
[507] | 617 | def str_to_dict(self, s): |
---|
| 618 | """ |
---|
| 619 | Transfrom a string of the form [<key>=<value>]+ to dict[<key>] = <value> |
---|
| 620 | """ |
---|
| 621 | self.logger.debug("function str_to_dict") |
---|
[164] | 622 | |
---|
[507] | 623 | fields = string.split(s, self.parameters.subject_field_separator) |
---|
[262] | 624 | |
---|
[507] | 625 | result = dict() |
---|
| 626 | for field in fields: |
---|
| 627 | try: |
---|
| 628 | index, value = string.split(field, '=') |
---|
[169] | 629 | |
---|
[507] | 630 | # We can not change the description of a ticket via the subject |
---|
| 631 | # line. The description is the body of the email |
---|
| 632 | # |
---|
| 633 | if index.lower() in ['description']: |
---|
| 634 | continue |
---|
[169] | 635 | |
---|
[507] | 636 | if value: |
---|
| 637 | result[index.lower()] = value |
---|
[169] | 638 | |
---|
[507] | 639 | except ValueError: |
---|
| 640 | pass |
---|
| 641 | return result |
---|
[167] | 642 | |
---|
[507] | 643 | def print_unicode(self,s): |
---|
| 644 | """ |
---|
| 645 | This function prints unicode strings if possible else it will quote it |
---|
| 646 | """ |
---|
| 647 | try: |
---|
| 648 | self.logger.debug(s) |
---|
| 649 | except UnicodeEncodeError, detail: |
---|
| 650 | self.logger.debug(util.text.unicode_quote(s)) |
---|
[379] | 651 | |
---|
[503] | 652 | |
---|
[507] | 653 | def html_2_txt(self, data): |
---|
| 654 | """ |
---|
| 655 | Various routines to convert html syntax to valid trac wiki syntax |
---|
| 656 | """ |
---|
| 657 | self.logger.debug('function html_2_txt') |
---|
[503] | 658 | |
---|
[507] | 659 | ## This routine make an safe html that can be include |
---|
| 660 | # in trac, but no further text processing can be done |
---|
| 661 | # |
---|
| 662 | # try: |
---|
| 663 | # from lxml.html.clean import Cleaner |
---|
| 664 | # tags_rm = list() |
---|
| 665 | # tags_rm.append('body') |
---|
[503] | 666 | # |
---|
[507] | 667 | # cleaner = Cleaner(remove_tags=tags_rm ) |
---|
| 668 | # parsed_data = cleaner.clean_html(data) |
---|
| 669 | # parsed_data = '\n{{{\n#!html\n' + parsed_data + '\n}}}\n' |
---|
[503] | 670 | # |
---|
[507] | 671 | # return parsed_data |
---|
| 672 | # |
---|
| 673 | # except ImportError:: |
---|
| 674 | # pass |
---|
[503] | 675 | |
---|
[507] | 676 | parsed_data = None |
---|
| 677 | if self.parameters.html2text_cmd: |
---|
[514] | 678 | (fd, tmp_file) = tempfile.mkstemp('email2trac.html') |
---|
| 679 | f = os.fdopen(fd, 'w') |
---|
| 680 | |
---|
[507] | 681 | cmd = '%s %s' %(self.parameters.html2text_cmd, tmp_file) |
---|
| 682 | self.logger.debug('\t html2text conversion %s'%(cmd)) |
---|
| 683 | |
---|
| 684 | if self.parameters.dry_run: |
---|
[561] | 685 | self.logger.info('DRY_RUN: html2text conversion command: %s\n' %(cmd)) |
---|
[503] | 686 | |
---|
[507] | 687 | else: |
---|
| 688 | f.write(data) |
---|
| 689 | f.close() |
---|
[503] | 690 | |
---|
[507] | 691 | lines = os.popen(cmd).readlines() |
---|
| 692 | parsed_data = ''.join(lines) |
---|
[503] | 693 | |
---|
[507] | 694 | os.unlink(tmp_file) |
---|
[503] | 695 | |
---|
[507] | 696 | else: |
---|
| 697 | self.logger.debug('\t No html2text conversion tool specified in email2trac.conf') |
---|
[503] | 698 | |
---|
[507] | 699 | return parsed_data |
---|
[503] | 700 | |
---|
[549] | 701 | def check_filename_length(self, name): |
---|
| 702 | """ |
---|
[551] | 703 | To bypass a bug in Trac |
---|
| 704 | check if the filename lenght is not larger then OS limit. |
---|
[549] | 705 | yes : return truncated filename |
---|
| 706 | no : return unmodified filename |
---|
| 707 | """ |
---|
| 708 | self.logger.debug('function check_filename_length: ') |
---|
[550] | 709 | |
---|
| 710 | if not name: |
---|
| 711 | return 'None' |
---|
[549] | 712 | |
---|
| 713 | dummy_filename, ext = os.path.splitext(name) |
---|
| 714 | |
---|
| 715 | ## Trac uses this format |
---|
| 716 | # |
---|
| 717 | quote_format = util.text.unicode_quote(dummy_filename) |
---|
| 718 | |
---|
| 719 | ## Determine max filename lenght |
---|
| 720 | # |
---|
[603] | 721 | try: |
---|
| 722 | filemax_length = os.pathconf('/', 'PC_NAME_MAX') |
---|
| 723 | except AttributeError, detail: |
---|
| 724 | filemax_lenght = 240 |
---|
[549] | 725 | |
---|
| 726 | if len(quote_format) <= filemax_length: |
---|
| 727 | return name |
---|
| 728 | |
---|
| 729 | else: |
---|
| 730 | ## Truncate file to filemax_length and reserve room for extension |
---|
| 731 | # We must break on a boundry |
---|
| 732 | # |
---|
| 733 | length = filemax_length - 6 |
---|
| 734 | |
---|
| 735 | for i in range(0,10): |
---|
| 736 | |
---|
| 737 | truncated = quote_format[ : (length - i)] |
---|
| 738 | |
---|
| 739 | try: |
---|
| 740 | unqoute_truncated = util.text.unicode_unquote(truncated) |
---|
| 741 | unqoute_truncated = unqoute_truncated + ext |
---|
| 742 | |
---|
| 743 | self.print_unicode('\t ' + unqoute_truncated) |
---|
| 744 | |
---|
| 745 | break |
---|
| 746 | |
---|
| 747 | except UnicodeDecodeError, detail: |
---|
| 748 | continue |
---|
| 749 | |
---|
| 750 | |
---|
| 751 | return unqoute_truncated |
---|
| 752 | |
---|
[339] | 753 | ########## TRAC ticket functions ########################################################### |
---|
| 754 | |
---|
[546] | 755 | def mail_workflow(self, tkt): |
---|
[545] | 756 | """ |
---|
| 757 | """ |
---|
[546] | 758 | self.logger.debug('function mail_workflow: ') |
---|
[545] | 759 | |
---|
| 760 | req = Mock(authname=self.author, perm=MockPerm(), args={}) |
---|
| 761 | ticket_system = TicketSystem(self.env) |
---|
| 762 | |
---|
| 763 | try: |
---|
| 764 | workflow = self.parameters['workflow_%s' %tkt['status'].lower()] |
---|
| 765 | |
---|
| 766 | except KeyError: |
---|
[553] | 767 | ## fallback for compability (Will be deprecated) |
---|
| 768 | # workflow can be none. |
---|
[545] | 769 | # |
---|
| 770 | workflow = None |
---|
| 771 | if tkt['status'] in ['closed']: |
---|
| 772 | workflow = self.parameters.workflow |
---|
| 773 | |
---|
| 774 | if workflow: |
---|
[556] | 775 | |
---|
[557] | 776 | ## process all workflow implementations |
---|
| 777 | # |
---|
[556] | 778 | tkt_module = TicketModule(self.env) |
---|
| 779 | field_changes, problems = tkt_module.get_ticket_changes(req, tkt, workflow) |
---|
| 780 | |
---|
| 781 | for field in field_changes.keys(): |
---|
| 782 | |
---|
| 783 | ## We have already processed these fields |
---|
| 784 | # |
---|
[558] | 785 | if not field in ['summary', 'description']: |
---|
[556] | 786 | s = 'workflow : %s, field %s : %s, by %s' \ |
---|
| 787 | %(workflow, field, field_changes[field]['new'],field_changes[field]['by'] ) |
---|
| 788 | self.logger.debug(s) |
---|
| 789 | |
---|
| 790 | tkt[field] = field_changes[field]['new'] |
---|
| 791 | |
---|
[545] | 792 | return True |
---|
| 793 | |
---|
| 794 | else: |
---|
| 795 | return False |
---|
| 796 | |
---|
[507] | 797 | def check_permission_participants(self, tkt, action): |
---|
| 798 | """ |
---|
| 799 | Check if the mailer is allowed to update the ticket |
---|
| 800 | """ |
---|
[538] | 801 | self.logger.debug('function check_permission_participants %s') |
---|
[398] | 802 | |
---|
[518] | 803 | if tkt['reporter'].lower() in [self.author.lower(), self.email_addr.lower()]: |
---|
[507] | 804 | self.logger.debug('ALLOW, %s is the ticket reporter' %(self.email_addr)) |
---|
[408] | 805 | |
---|
[507] | 806 | return True |
---|
[398] | 807 | |
---|
[507] | 808 | perm = PermissionSystem(self.env) |
---|
| 809 | if perm.check_permission(action, self.author): |
---|
| 810 | self.logger.debug('ALLOW, %s has trac permission to update the ticket' %(self.author)) |
---|
[408] | 811 | |
---|
[507] | 812 | return True |
---|
| 813 | |
---|
| 814 | # Is the updater in the CC? |
---|
| 815 | try: |
---|
| 816 | cc_list = tkt['cc'].split(',') |
---|
| 817 | for cc in cc_list: |
---|
[518] | 818 | if self.email_addr.lower() in cc.lower.strip(): |
---|
[507] | 819 | self.logger.debug('ALLOW, %s is in the CC' %(self.email_addr)) |
---|
[398] | 820 | |
---|
[507] | 821 | return True |
---|
[398] | 822 | |
---|
[507] | 823 | except KeyError: |
---|
| 824 | pass |
---|
[398] | 825 | |
---|
[507] | 826 | return False |
---|
[474] | 827 | |
---|
[507] | 828 | def check_permission(self, tkt, action): |
---|
| 829 | """ |
---|
| 830 | check if the reporter has the right permission for the action: |
---|
[397] | 831 | - TICKET_CREATE |
---|
| 832 | - TICKET_MODIFY |
---|
[474] | 833 | - TICKET_APPEND |
---|
| 834 | - TICKET_CHGPROP |
---|
[397] | 835 | |
---|
[507] | 836 | There are three models: |
---|
| 837 | - None : no checking at all |
---|
| 838 | - trac : check the permission via trac permission model |
---|
| 839 | - email2trac: .... |
---|
| 840 | """ |
---|
[538] | 841 | self.logger.debug("function check_permission: %s" %(action)) |
---|
[397] | 842 | |
---|
[507] | 843 | if self.parameters.ticket_permission_system in ['trac']: |
---|
[398] | 844 | |
---|
[539] | 845 | perm = PermissionCache(self.env, self.author) |
---|
| 846 | if perm.has_permission(action): |
---|
[507] | 847 | return True |
---|
| 848 | else: |
---|
| 849 | return False |
---|
[398] | 850 | |
---|
[507] | 851 | elif self.parameters.ticket_permission_system in ['update_restricted_to_participants']: |
---|
| 852 | return (self.check_permission_participants(tkt, action)) |
---|
[398] | 853 | |
---|
[507] | 854 | ## Default is to allow everybody ticket updates and ticket creation |
---|
| 855 | # |
---|
| 856 | else: |
---|
| 857 | return True |
---|
[397] | 858 | |
---|
| 859 | |
---|
[507] | 860 | def update_ticket_fields(self, ticket, user_dict, new=None): |
---|
| 861 | """ |
---|
| 862 | This will update the ticket fields. It will check if the |
---|
| 863 | given fields are known and if the right values are specified |
---|
| 864 | It will only update the ticket field value: |
---|
| 865 | - If the field is known |
---|
| 866 | - If the value supplied is valid for the ticket field. |
---|
| 867 | If not then there are two options: |
---|
| 868 | 1) Skip the value (new=None) |
---|
| 869 | 2) Set default value for field (new=1) |
---|
| 870 | """ |
---|
| 871 | self.logger.debug("function update_ticket_fields") |
---|
[169] | 872 | |
---|
[507] | 873 | ## Check only permission model on ticket updates |
---|
| 874 | # |
---|
| 875 | if not new: |
---|
| 876 | if self.parameters.ticket_permission_system: |
---|
| 877 | if not self.check_permission(ticket, 'TICKET_CHGPROP'): |
---|
| 878 | self.logger.info('Reporter: %s has no permission to change ticket properties' %self.author) |
---|
| 879 | return False |
---|
[474] | 880 | |
---|
[507] | 881 | ## Build a system dictionary from the ticket fields |
---|
| 882 | # with field as index and option as value |
---|
| 883 | # |
---|
| 884 | sys_dict = dict() |
---|
[530] | 885 | |
---|
[507] | 886 | for field in ticket.fields: |
---|
[530] | 887 | |
---|
[507] | 888 | try: |
---|
| 889 | sys_dict[field['name']] = field['options'] |
---|
[169] | 890 | |
---|
[507] | 891 | except KeyError: |
---|
[531] | 892 | sys_dict[field['name']] = None |
---|
[507] | 893 | pass |
---|
[169] | 894 | |
---|
[507] | 895 | ## Check user supplied fields an compare them with the |
---|
| 896 | # system one's |
---|
| 897 | # |
---|
[549] | 898 | for field,value in user_dict.items(): |
---|
| 899 | |
---|
| 900 | s = 'user_field\t %s = %s' %(field,value) |
---|
| 901 | self.print_unicode(s) |
---|
[524] | 902 | |
---|
[531] | 903 | if not field in sys_dict.keys(): |
---|
[524] | 904 | self.logger.debug('%s is not a valid field for tickets' %(field)) |
---|
| 905 | continue |
---|
| 906 | |
---|
[507] | 907 | ## To prevent mail loop |
---|
| 908 | # |
---|
| 909 | if field == 'cc': |
---|
[301] | 910 | |
---|
[507] | 911 | cc_list = user_dict['cc'].split(',') |
---|
[301] | 912 | |
---|
[507] | 913 | if self.trac_smtp_from in cc_list: |
---|
| 914 | self.logger.debug('MAIL LOOP: %s is not allowed as CC address' %(self.trac_smtp_from)) |
---|
[408] | 915 | |
---|
[507] | 916 | cc_list.remove(self.trac_smtp_from) |
---|
[301] | 917 | |
---|
[507] | 918 | value = ','.join(cc_list) |
---|
| 919 | |
---|
[301] | 920 | |
---|
[507] | 921 | ## Check if every value is allowed for this field |
---|
| 922 | # |
---|
[531] | 923 | if sys_dict[field]: |
---|
[169] | 924 | |
---|
[507] | 925 | if value in sys_dict[field]: |
---|
| 926 | ticket[field] = value |
---|
| 927 | else: |
---|
| 928 | ## Must we set a default if value is not allowed |
---|
| 929 | # |
---|
| 930 | if new: |
---|
| 931 | value = self.get_config('ticket', 'default_%s' %(field) ) |
---|
[169] | 932 | |
---|
[507] | 933 | else: |
---|
[553] | 934 | |
---|
[507] | 935 | ticket[field] = value |
---|
[202] | 936 | |
---|
[549] | 937 | s = 'ticket_field\t %s = %s' %(field, ticket[field]) |
---|
| 938 | self.print_unicode(s) |
---|
[345] | 939 | |
---|
[507] | 940 | def ticket_update(self, m, id, spam): |
---|
| 941 | """ |
---|
| 942 | If the current email is a reply to an existing ticket, this function |
---|
| 943 | will append the contents of this email to that ticket, instead of |
---|
| 944 | creating a new one. |
---|
| 945 | """ |
---|
| 946 | self.logger.debug("function ticket_update") |
---|
[202] | 947 | |
---|
[507] | 948 | if not self.parameters.ticket_update: |
---|
| 949 | self.logger.debug("ticket_update disabled") |
---|
| 950 | return False |
---|
[456] | 951 | |
---|
[507] | 952 | ## Must we update ticket fields |
---|
| 953 | # |
---|
| 954 | update_fields = dict() |
---|
| 955 | try: |
---|
| 956 | id, keywords = string.split(id, '?') |
---|
[262] | 957 | |
---|
[507] | 958 | update_fields = self.str_to_dict(keywords) |
---|
[165] | 959 | |
---|
[507] | 960 | ## Strip '#' |
---|
| 961 | # |
---|
| 962 | self.id = int(id[1:]) |
---|
[165] | 963 | |
---|
[507] | 964 | except ValueError: |
---|
[390] | 965 | |
---|
[507] | 966 | ## Strip '#' |
---|
| 967 | # |
---|
| 968 | self.id = int(id[1:]) |
---|
[164] | 969 | |
---|
[597] | 970 | self.logger.debug("\tticket id: %s" %id) |
---|
[71] | 971 | |
---|
[507] | 972 | ## When is the change committed |
---|
| 973 | # |
---|
| 974 | if self.VERSION < 0.11: |
---|
| 975 | when = int(time.time()) |
---|
| 976 | else: |
---|
| 977 | when = datetime.now(util.datefmt.utc) |
---|
[77] | 978 | |
---|
[507] | 979 | try: |
---|
| 980 | tkt = Ticket(self.env, self.id, self.db) |
---|
[390] | 981 | |
---|
[507] | 982 | except util.TracError, detail: |
---|
[390] | 983 | |
---|
[507] | 984 | ## Not a valid ticket |
---|
| 985 | # |
---|
[604] | 986 | self.logger.info("\tCreating a new ticket, ticket id: %s does not exists" %id) |
---|
[507] | 987 | self.id = None |
---|
| 988 | return False |
---|
[126] | 989 | |
---|
[507] | 990 | ## Check the permission of the reporter |
---|
| 991 | # |
---|
| 992 | if self.parameters.ticket_permission_system: |
---|
| 993 | if not self.check_permission(tkt, 'TICKET_APPEND'): |
---|
| 994 | self.logger.info('Reporter: %s has no permission to add comments or attachments to tickets' %self.author) |
---|
| 995 | return False |
---|
[398] | 996 | |
---|
[507] | 997 | ## How many changes has this ticket |
---|
| 998 | # |
---|
| 999 | grouped = TicketModule(self.env).grouped_changelog_entries(tkt, self.db) |
---|
| 1000 | cnum = sum(1 for e in grouped) + 1 |
---|
[288] | 1001 | |
---|
[486] | 1002 | |
---|
[507] | 1003 | ## reopen the ticket if it is was closed |
---|
| 1004 | # We must use the ticket workflow framework |
---|
| 1005 | # |
---|
| 1006 | if self.parameters.email_triggers_workflow and (self.VERSION >= 0.11): |
---|
[257] | 1007 | |
---|
[546] | 1008 | if not self.mail_workflow(tkt): |
---|
[538] | 1009 | |
---|
[507] | 1010 | if tkt['status'] in ['closed']: |
---|
[526] | 1011 | tkt['status'] = 'reopened' |
---|
| 1012 | tkt['resolution'] = '' |
---|
| 1013 | |
---|
[507] | 1014 | ## Old pre 0.11 situation |
---|
| 1015 | # |
---|
| 1016 | elif self.parameters.email_triggers_workflow: |
---|
[470] | 1017 | |
---|
[597] | 1018 | self.logger.debug('\temail triggers workflow pre trac 0.11') |
---|
[470] | 1019 | |
---|
[507] | 1020 | if tkt['status'] in ['closed']: |
---|
| 1021 | tkt['status'] = 'reopened' |
---|
| 1022 | tkt['resolution'] = '' |
---|
[257] | 1023 | |
---|
[507] | 1024 | else: |
---|
[597] | 1025 | self.logger.debug('\temail triggers workflow disabled') |
---|
[469] | 1026 | |
---|
[507] | 1027 | ## Must we update some ticket fields properties via subject line |
---|
| 1028 | # |
---|
| 1029 | if update_fields: |
---|
| 1030 | self.update_ticket_fields(tkt, update_fields) |
---|
[166] | 1031 | |
---|
[597] | 1032 | |
---|
[507] | 1033 | message_parts = self.get_message_parts(m) |
---|
| 1034 | message_parts = self.unique_attachment_names(message_parts) |
---|
[210] | 1035 | |
---|
[597] | 1036 | ## Must we update some ticket fields properties via inline comments |
---|
| 1037 | # in body_text |
---|
[507] | 1038 | # |
---|
| 1039 | if self.properties: |
---|
| 1040 | self.update_ticket_fields(tkt, self.properties) |
---|
[309] | 1041 | |
---|
[597] | 1042 | ## Must we update the CC ticket field |
---|
| 1043 | # |
---|
| 1044 | self.set_cc_fields(tkt, m, update=True) |
---|
| 1045 | |
---|
[507] | 1046 | if self.parameters.email_header: |
---|
| 1047 | message_parts.insert(0, self.email_header_txt(m)) |
---|
[76] | 1048 | |
---|
[507] | 1049 | body_text = self.get_body_text(message_parts) |
---|
[236] | 1050 | |
---|
[507] | 1051 | error_with_attachments = self.attach_attachments(message_parts) |
---|
[348] | 1052 | |
---|
[507] | 1053 | if body_text.strip() or update_fields or self.properties: |
---|
[543] | 1054 | |
---|
[507] | 1055 | if self.parameters.dry_run: |
---|
[561] | 1056 | s = 'DRY_RUN: tkt.save_changes(self.author, body_text, ticket_change_number) %s %s' %(self.author, cnum) |
---|
| 1057 | self.logger.info(s) |
---|
[543] | 1058 | |
---|
[507] | 1059 | else: |
---|
| 1060 | if error_with_attachments: |
---|
| 1061 | body_text = '%s\\%s' %(error_with_attachments, body_text) |
---|
[543] | 1062 | |
---|
[597] | 1063 | self.logger.debug('\ttkt.save_changes(%s, %d)' %(self.author, cnum)) |
---|
[507] | 1064 | tkt.save_changes(self.author, body_text, when, None, str(cnum)) |
---|
| 1065 | |
---|
| 1066 | if not spam: |
---|
| 1067 | self.notify(tkt, False, when) |
---|
[72] | 1068 | |
---|
[507] | 1069 | return True |
---|
[71] | 1070 | |
---|
[507] | 1071 | def set_ticket_fields(self, ticket): |
---|
| 1072 | """ |
---|
| 1073 | set the ticket fields to value specified |
---|
| 1074 | - /etc/email2trac.conf with <prefix>_<field> |
---|
| 1075 | - trac default values, trac.ini |
---|
| 1076 | """ |
---|
| 1077 | self.logger.debug('function set_ticket_fields') |
---|
[410] | 1078 | |
---|
[507] | 1079 | user_dict = dict() |
---|
[202] | 1080 | |
---|
[507] | 1081 | for field in ticket.fields: |
---|
[202] | 1082 | |
---|
[507] | 1083 | name = field['name'] |
---|
[202] | 1084 | |
---|
[507] | 1085 | ## default trac value |
---|
| 1086 | # |
---|
| 1087 | if not field.get('custom'): |
---|
| 1088 | value = self.get_config('ticket', 'default_%s' %(name) ) |
---|
[599] | 1089 | |
---|
| 1090 | ## skip this field can only be set by email2trac.conf |
---|
| 1091 | # |
---|
| 1092 | if name in ['resolution']: |
---|
[507] | 1093 | value = None |
---|
[599] | 1094 | |
---|
[507] | 1095 | else: |
---|
| 1096 | ## Else get the default value for reporter |
---|
| 1097 | # |
---|
| 1098 | value = field.get('value') |
---|
| 1099 | options = field.get('options') |
---|
[345] | 1100 | |
---|
[507] | 1101 | if value and options and (value not in options): |
---|
| 1102 | value = options[int(value)] |
---|
| 1103 | |
---|
[549] | 1104 | s = 'trac[%s] = %s' %(name, value) |
---|
| 1105 | self.print_unicode(s) |
---|
[202] | 1106 | |
---|
[507] | 1107 | ## email2trac.conf settings |
---|
| 1108 | # |
---|
| 1109 | prefix = self.parameters.ticket_prefix |
---|
| 1110 | try: |
---|
| 1111 | value = self.parameters['%s_%s' %(prefix, name)] |
---|
[202] | 1112 | |
---|
[549] | 1113 | s = 'email2trac[%s] = %s ' %(name, value) |
---|
| 1114 | self.print_unicode(s) |
---|
| 1115 | |
---|
[507] | 1116 | except KeyError, detail: |
---|
| 1117 | pass |
---|
| 1118 | |
---|
| 1119 | if value: |
---|
| 1120 | user_dict[name] = value |
---|
[202] | 1121 | |
---|
[550] | 1122 | s = 'used %s = %s' %(name, value) |
---|
| 1123 | self.print_unicode(s) |
---|
| 1124 | |
---|
[507] | 1125 | self.update_ticket_fields(ticket, user_dict, new=1) |
---|
[202] | 1126 | |
---|
[507] | 1127 | if 'status' not in user_dict.keys(): |
---|
| 1128 | ticket['status'] = 'new' |
---|
[202] | 1129 | |
---|
| 1130 | |
---|
[507] | 1131 | def ticket_update_by_subject(self, subject): |
---|
| 1132 | """ |
---|
| 1133 | This list of Re: prefixes is probably incomplete. Taken from |
---|
| 1134 | wikipedia. Here is how the subject is matched |
---|
| 1135 | - Re: <subject> |
---|
| 1136 | - Re: (<Mail list label>:)+ <subject> |
---|
[202] | 1137 | |
---|
[507] | 1138 | So we must have the last column |
---|
| 1139 | """ |
---|
| 1140 | self.logger.debug('function ticket_update_by_subject') |
---|
[356] | 1141 | |
---|
[533] | 1142 | found_id = None |
---|
[507] | 1143 | if self.parameters.ticket_update and self.parameters.ticket_update_by_subject: |
---|
| 1144 | |
---|
| 1145 | SUBJECT_RE = re.compile(r'^(?:(?:RE|AW|VS|SV|FW|FWD):\s*)+(.*)', re.IGNORECASE) |
---|
| 1146 | result = SUBJECT_RE.search(subject) |
---|
[356] | 1147 | |
---|
[507] | 1148 | if result: |
---|
| 1149 | ## This is a reply |
---|
| 1150 | # |
---|
| 1151 | orig_subject = result.group(1) |
---|
[356] | 1152 | |
---|
[507] | 1153 | self.logger.debug('subject search string: %s' %(orig_subject)) |
---|
[356] | 1154 | |
---|
[507] | 1155 | cursor = self.db.cursor() |
---|
| 1156 | summaries = [orig_subject, '%%: %s' % orig_subject] |
---|
[356] | 1157 | |
---|
[520] | 1158 | ## Time resolution is in micoseconds |
---|
[507] | 1159 | # |
---|
[520] | 1160 | search_date = datetime.now(util.datefmt.utc) - timedelta(days=self.parameters.ticket_update_by_subject_lookback) |
---|
[356] | 1161 | |
---|
[529] | 1162 | if self.VERSION < 0.12: |
---|
| 1163 | lookback = util.datefmt.to_timestamp(search_date) |
---|
| 1164 | else: |
---|
| 1165 | lookback = util.datefmt.to_utimestamp(search_date) |
---|
| 1166 | |
---|
[507] | 1167 | for summary in summaries: |
---|
| 1168 | self.logger.debug('Looking for summary matching: "%s"' % summary) |
---|
[408] | 1169 | |
---|
[533] | 1170 | sql = """SELECT id, reporter FROM ticket |
---|
[507] | 1171 | WHERE changetime >= %s AND summary LIKE %s |
---|
| 1172 | ORDER BY changetime DESC""" |
---|
[533] | 1173 | |
---|
[507] | 1174 | cursor.execute(sql, [lookback, summary.strip()]) |
---|
[356] | 1175 | |
---|
[507] | 1176 | for row in cursor: |
---|
[408] | 1177 | |
---|
[533] | 1178 | (matched_id, sql_reporter) = row |
---|
[408] | 1179 | |
---|
[533] | 1180 | ## Save first entry. |
---|
| 1181 | # |
---|
| 1182 | if not found_id: |
---|
| 1183 | found_id = matched_id |
---|
| 1184 | |
---|
| 1185 | ## If subject and reporter are the same. The we certainly have found the right ticket |
---|
| 1186 | # |
---|
| 1187 | if sql_reporter == self.author: |
---|
| 1188 | self.logger.debug('Found matching reporter: %s with ticket id: %d' %(sql_reporter, matched_id)) |
---|
| 1189 | found_id = matched_id |
---|
| 1190 | break |
---|
[356] | 1191 | |
---|
[533] | 1192 | if found_id: |
---|
| 1193 | self.logger.debug('Found matching ticket id: %d' % found_id) |
---|
| 1194 | found_id = '#%d' % found_id |
---|
| 1195 | return (found_id, orig_subject) |
---|
| 1196 | |
---|
[553] | 1197 | # obsolete !?? 12 Aug 2011 |
---|
[533] | 1198 | #subject = orig_subject |
---|
[356] | 1199 | |
---|
[533] | 1200 | return (found_id, subject) |
---|
[356] | 1201 | |
---|
| 1202 | |
---|
[507] | 1203 | def new_ticket(self, msg, subject, spam, set_fields = None): |
---|
| 1204 | """ |
---|
| 1205 | Create a new ticket |
---|
| 1206 | """ |
---|
| 1207 | self.logger.debug('function new_ticket') |
---|
[250] | 1208 | |
---|
[507] | 1209 | tkt = Ticket(self.env) |
---|
[326] | 1210 | |
---|
[597] | 1211 | ## self.author can be email address of an username |
---|
| 1212 | # |
---|
| 1213 | tkt['reporter'] = self.author |
---|
[326] | 1214 | |
---|
[597] | 1215 | self.set_cc_fields(tkt, msg) |
---|
| 1216 | |
---|
[507] | 1217 | self.set_ticket_fields(tkt) |
---|
[202] | 1218 | |
---|
[507] | 1219 | ## Check the permission of the reporter |
---|
| 1220 | # |
---|
| 1221 | if self.parameters.ticket_permission_system: |
---|
| 1222 | if not self.check_permission(tkt, 'TICKET_CREATE'): |
---|
| 1223 | self.logger.info('Reporter: %s has no permission to create tickets' %self.author) |
---|
| 1224 | return False |
---|
[397] | 1225 | |
---|
[507] | 1226 | ## Old style setting for component, will be removed |
---|
| 1227 | # |
---|
| 1228 | if spam: |
---|
| 1229 | tkt['component'] = 'Spam' |
---|
[204] | 1230 | |
---|
[507] | 1231 | elif self.parameters.has_key('component'): |
---|
| 1232 | tkt['component'] = self.parameters['component'] |
---|
[201] | 1233 | |
---|
[507] | 1234 | if not msg['Subject']: |
---|
| 1235 | tkt['summary'] = u'(No subject)' |
---|
| 1236 | else: |
---|
| 1237 | tkt['summary'] = subject |
---|
[22] | 1238 | |
---|
| 1239 | |
---|
[507] | 1240 | if set_fields: |
---|
| 1241 | rest, keywords = string.split(set_fields, '?') |
---|
[262] | 1242 | |
---|
[507] | 1243 | if keywords: |
---|
| 1244 | update_fields = self.str_to_dict(keywords) |
---|
| 1245 | self.update_ticket_fields(tkt, update_fields) |
---|
[262] | 1246 | |
---|
[296] | 1247 | |
---|
[604] | 1248 | message_parts = self.get_message_parts(msg, True) |
---|
[309] | 1249 | |
---|
[507] | 1250 | ## Must we update some ticket fields properties via body_text |
---|
| 1251 | # |
---|
| 1252 | if self.properties: |
---|
| 1253 | self.update_ticket_fields(tkt, self.properties) |
---|
[309] | 1254 | |
---|
[507] | 1255 | message_parts = self.unique_attachment_names(message_parts) |
---|
| 1256 | |
---|
| 1257 | ## produce e-mail like header |
---|
| 1258 | # |
---|
| 1259 | head = '' |
---|
| 1260 | if self.parameters.email_header: |
---|
| 1261 | head = self.email_header_txt(msg) |
---|
| 1262 | message_parts.insert(0, head) |
---|
| 1263 | |
---|
| 1264 | body_text = self.get_body_text(message_parts) |
---|
[45] | 1265 | |
---|
[507] | 1266 | tkt['description'] = body_text |
---|
[90] | 1267 | |
---|
[507] | 1268 | ## When is the change committed |
---|
| 1269 | # |
---|
| 1270 | if self.VERSION < 0.11: |
---|
| 1271 | when = int(time.time()) |
---|
| 1272 | else: |
---|
| 1273 | when = datetime.now(util.datefmt.utc) |
---|
[45] | 1274 | |
---|
[507] | 1275 | if self.parameters.dry_run: |
---|
[561] | 1276 | self.logger.info('DRY_RUN: tkt.insert()') |
---|
[507] | 1277 | else: |
---|
| 1278 | self.id = tkt.insert() |
---|
| 1279 | |
---|
| 1280 | changed = False |
---|
| 1281 | comment = '' |
---|
[77] | 1282 | |
---|
[507] | 1283 | ## some routines in trac are dependend on ticket id |
---|
| 1284 | # like alternate notify template |
---|
| 1285 | # |
---|
| 1286 | if self.parameters.alternate_notify_template: |
---|
| 1287 | tkt['id'] = self.id |
---|
| 1288 | changed = True |
---|
[273] | 1289 | |
---|
[507] | 1290 | ## Rewrite the description if we have mailto enabled |
---|
| 1291 | # |
---|
| 1292 | if self.parameters.mailto_link: |
---|
| 1293 | changed = True |
---|
| 1294 | comment = u'\nadded mailto line\n' |
---|
[592] | 1295 | |
---|
[581] | 1296 | #mailto = self.html_mailto_link( m['Subject']) |
---|
| 1297 | mailto = self.html_mailto_link(subject) |
---|
[253] | 1298 | |
---|
[507] | 1299 | tkt['description'] = u'%s\r\n%s%s\r\n' \ |
---|
| 1300 | %(head, mailto, body_text) |
---|
| 1301 | |
---|
| 1302 | ## Save the attachments to the ticket |
---|
| 1303 | # |
---|
| 1304 | error_with_attachments = self.attach_attachments(message_parts) |
---|
[295] | 1305 | |
---|
[507] | 1306 | if error_with_attachments: |
---|
| 1307 | changed = True |
---|
| 1308 | comment = '%s\n%s\n' %(comment, error_with_attachments) |
---|
[45] | 1309 | |
---|
[545] | 1310 | if self.parameters.email_triggers_workflow and (self.VERSION >= 0.11): |
---|
[546] | 1311 | if self.mail_workflow(tkt): |
---|
[545] | 1312 | changed = True |
---|
| 1313 | |
---|
[507] | 1314 | if changed: |
---|
| 1315 | if self.parameters.dry_run: |
---|
[561] | 1316 | s = 'DRY_RUN: tkt.save_changes(%s, comment) real reporter = %s' %( tkt['reporter'], self.author) |
---|
| 1317 | self.logger.info(s) |
---|
| 1318 | |
---|
[507] | 1319 | else: |
---|
| 1320 | tkt.save_changes(tkt['reporter'], comment) |
---|
[90] | 1321 | |
---|
[507] | 1322 | if not spam: |
---|
[545] | 1323 | self.notify(tkt, True) |
---|
[45] | 1324 | |
---|
[260] | 1325 | |
---|
[507] | 1326 | def attach_attachments(self, message_parts, update=False): |
---|
| 1327 | ''' |
---|
| 1328 | save any attachments as files in the ticket's directory |
---|
| 1329 | ''' |
---|
| 1330 | self.logger.debug('function attach_attachments()') |
---|
[342] | 1331 | |
---|
[507] | 1332 | if self.parameters.dry_run: |
---|
[555] | 1333 | self.logger.debug("DRY_RUN: no attachments attached to tickets") |
---|
[507] | 1334 | return '' |
---|
[342] | 1335 | |
---|
[507] | 1336 | count = 0 |
---|
[342] | 1337 | |
---|
[507] | 1338 | ## Get Maxium attachment size |
---|
| 1339 | # |
---|
| 1340 | max_size = int(self.get_config('attachment', 'max_size')) |
---|
| 1341 | status = None |
---|
| 1342 | |
---|
| 1343 | for item in message_parts: |
---|
| 1344 | ## Skip body parts |
---|
| 1345 | # |
---|
| 1346 | if not isinstance(item, tuple): |
---|
| 1347 | continue |
---|
| 1348 | |
---|
| 1349 | (original, filename, part) = item |
---|
[477] | 1350 | |
---|
[548] | 1351 | ## We have to determine the size so we use this temporary solution. |
---|
[507] | 1352 | # |
---|
[548] | 1353 | path, fd = util.create_unique_file(os.path.join(self.parameters.tmpdir, 'email2trac_tmp.att')) |
---|
[507] | 1354 | text = part.get_payload(decode=1) |
---|
| 1355 | if not text: |
---|
| 1356 | text = '(None)' |
---|
| 1357 | fd.write(text) |
---|
| 1358 | fd.close() |
---|
[342] | 1359 | |
---|
[507] | 1360 | ## get the file_size |
---|
| 1361 | # |
---|
| 1362 | stats = os.lstat(path) |
---|
| 1363 | file_size = stats[ST_SIZE] |
---|
[342] | 1364 | |
---|
[507] | 1365 | ## Check if the attachment size is allowed |
---|
| 1366 | # |
---|
| 1367 | if (max_size != -1) and (file_size > max_size): |
---|
| 1368 | status = '%s\nFile %s is larger then allowed attachment size (%d > %d)\n\n' \ |
---|
| 1369 | %(status, original, file_size, max_size) |
---|
[342] | 1370 | |
---|
[507] | 1371 | os.unlink(path) |
---|
| 1372 | continue |
---|
| 1373 | else: |
---|
| 1374 | count = count + 1 |
---|
| 1375 | |
---|
| 1376 | ## Insert the attachment |
---|
| 1377 | # |
---|
| 1378 | fd = open(path, 'rb') |
---|
| 1379 | if self.system == 'discussion': |
---|
[553] | 1380 | att = attachment.Attachment(self.env, 'discussion', 'topic/%s' % (self.id,)) |
---|
[585] | 1381 | elif self.system == 'blog': |
---|
| 1382 | att = attachment.Attachment(self.env, 'blog', '%s' % (self.id,)) |
---|
[507] | 1383 | else: |
---|
[548] | 1384 | s = 'Attach %s to ticket %d' %(filename, self.id) |
---|
| 1385 | self.print_unicode(s) |
---|
[507] | 1386 | att = attachment.Attachment(self.env, 'ticket', self.id) |
---|
[359] | 1387 | |
---|
[507] | 1388 | ## This will break the ticket_update system, the body_text is vaporized |
---|
| 1389 | # ;-( |
---|
| 1390 | # |
---|
| 1391 | if not update: |
---|
| 1392 | att.author = self.author |
---|
| 1393 | att.description = self.email_to_unicode('Added by email2trac') |
---|
[342] | 1394 | |
---|
[553] | 1395 | try: |
---|
[549] | 1396 | |
---|
[507] | 1397 | self.logger.debug('Insert atachment') |
---|
| 1398 | att.insert(filename, fd, file_size) |
---|
[553] | 1399 | |
---|
[507] | 1400 | except OSError, detail: |
---|
[553] | 1401 | |
---|
[507] | 1402 | self.logger.info('%s\nFilename %s could not be saved, problem: %s' %(status, filename, detail)) |
---|
| 1403 | status = '%s\nFilename %s could not be saved, problem: %s' %(status, filename, detail) |
---|
[342] | 1404 | |
---|
[507] | 1405 | ## Remove the created temporary filename |
---|
| 1406 | # |
---|
| 1407 | fd.close() |
---|
| 1408 | os.unlink(path) |
---|
[342] | 1409 | |
---|
[507] | 1410 | ## return error |
---|
| 1411 | # |
---|
| 1412 | return status |
---|
[342] | 1413 | |
---|
[359] | 1414 | ########## Fullblog functions ################################################# |
---|
[339] | 1415 | |
---|
[585] | 1416 | def blog(self, msg, subject, id, params): |
---|
[507] | 1417 | """ |
---|
| 1418 | The blog create/update function |
---|
| 1419 | """ |
---|
| 1420 | ## import the modules |
---|
| 1421 | # |
---|
| 1422 | from tracfullblog.core import FullBlogCore |
---|
| 1423 | from tracfullblog.model import BlogPost, BlogComment |
---|
[260] | 1424 | |
---|
[507] | 1425 | ## instantiate blog core |
---|
| 1426 | # |
---|
| 1427 | blog = FullBlogCore(self.env) |
---|
| 1428 | req = Mock(authname='anonymous', perm=MockPerm(), args={}) |
---|
[312] | 1429 | |
---|
[585] | 1430 | ## parameters from the subject |
---|
| 1431 | # |
---|
| 1432 | params = self.str_to_dict((params or '').lstrip('?')) |
---|
[260] | 1433 | |
---|
[585] | 1434 | ## preferably get the time from the email date header, or else |
---|
| 1435 | # use current date and time |
---|
| 1436 | date = email.Utils.parsedate_tz(msg.get('date')) |
---|
| 1437 | if date: |
---|
| 1438 | dt = util.datefmt.to_datetime(email.Utils.mktime_tz(date), util.datefmt.utc) |
---|
| 1439 | else: |
---|
| 1440 | self.logger.warn("No valid date header found") |
---|
| 1441 | dt = util.datefmt.to_datetime(None, util.datefmt.utc) |
---|
| 1442 | |
---|
| 1443 | ## blog entry affected |
---|
| 1444 | # |
---|
| 1445 | self.id = id or util.datefmt.format_datetime(dt, "%Y%m%d%H%M%S", util.datefmt.utc) |
---|
| 1446 | |
---|
| 1447 | ## check wether a blog post exists |
---|
| 1448 | # |
---|
| 1449 | post = BlogPost(self.env, self.id) |
---|
| 1450 | force_update = self.properties.get('update', params.get('update')) |
---|
| 1451 | |
---|
| 1452 | ## message parts |
---|
| 1453 | # |
---|
| 1454 | message_parts = self.get_message_parts(msg) |
---|
| 1455 | message_parts = self.unique_attachment_names(message_parts) |
---|
| 1456 | |
---|
| 1457 | if post.get_versions() and not force_update: |
---|
| 1458 | |
---|
| 1459 | ## add comment to blog entry |
---|
[507] | 1460 | # |
---|
[585] | 1461 | comment = BlogComment(self.env, self.id) |
---|
| 1462 | comment.author = self.properties.get('author', params.get('author', self.author)) |
---|
[507] | 1463 | comment.comment = self.get_body_text(message_parts) |
---|
[585] | 1464 | comment.time = dt |
---|
[312] | 1465 | |
---|
[578] | 1466 | if self.parameters.dry_run: |
---|
| 1467 | self.logger.info('DRY-RUN: not adding comment for blog entry "%s"' % id) |
---|
| 1468 | return |
---|
[585] | 1469 | warnings = blog.create_comment(req, comment) |
---|
[260] | 1470 | |
---|
[507] | 1471 | else: |
---|
[585] | 1472 | ## create or update blog entry |
---|
[507] | 1473 | # |
---|
[585] | 1474 | post.author = self.properties.get('author', params.get('author', self.author)) |
---|
| 1475 | post.categories = self.properties.get('categories', params.get('categories', '')) |
---|
[578] | 1476 | post.title = subject.strip() |
---|
[585] | 1477 | post.publish_time = dt |
---|
[507] | 1478 | post.body = self.get_body_text(message_parts) |
---|
| 1479 | |
---|
[578] | 1480 | if self.parameters.dry_run: |
---|
| 1481 | self.logger.info('DRY-RUN: not creating blog entry "%s"' % post.title) |
---|
| 1482 | return |
---|
[585] | 1483 | warnings = blog.create_post(req, post, self.author, u'Created by email2trac', False) |
---|
[260] | 1484 | |
---|
[585] | 1485 | ## check for problems |
---|
| 1486 | # |
---|
| 1487 | if warnings: |
---|
| 1488 | raise TracError(', '.join('blog:%s:%s' % (w[0], w[1]) for w in warnings)) |
---|
| 1489 | |
---|
| 1490 | ## all seems well, attach attachments |
---|
| 1491 | # |
---|
| 1492 | self.attach_attachments(message_parts) |
---|
[260] | 1493 | |
---|
[585] | 1494 | |
---|
[359] | 1495 | ########## Discussion functions ############################################## |
---|
[342] | 1496 | |
---|
[507] | 1497 | def discussion_topic(self, content, subject): |
---|
[342] | 1498 | |
---|
[507] | 1499 | ## Import modules. |
---|
| 1500 | # |
---|
| 1501 | from tracdiscussion.api import DiscussionApi |
---|
| 1502 | from trac.util.datefmt import to_timestamp, utc |
---|
[359] | 1503 | |
---|
[507] | 1504 | self.logger.debug('Creating a new topic in forum:', self.id) |
---|
[359] | 1505 | |
---|
[507] | 1506 | ## Get dissussion API component. |
---|
| 1507 | # |
---|
| 1508 | api = self.env[DiscussionApi] |
---|
| 1509 | context = self._create_context(content, subject) |
---|
[359] | 1510 | |
---|
[507] | 1511 | ## Get forum for new topic. |
---|
| 1512 | # |
---|
| 1513 | forum = api.get_forum(context, self.id) |
---|
[359] | 1514 | |
---|
[507] | 1515 | if not forum: |
---|
| 1516 | self.logger.error("ERROR: Replied forum doesn't exist") |
---|
[359] | 1517 | |
---|
[507] | 1518 | ## Prepare topic. |
---|
| 1519 | # |
---|
| 1520 | topic = {'forum' : forum['id'], |
---|
| 1521 | 'subject' : context.subject, |
---|
| 1522 | 'time': to_timestamp(datetime.now(utc)), |
---|
| 1523 | 'author' : self.author, |
---|
| 1524 | 'subscribers' : [self.email_addr], |
---|
| 1525 | 'body' : self.get_body_text(context.content_parts)} |
---|
[359] | 1526 | |
---|
[507] | 1527 | ## Add topic to DB and commit it. |
---|
| 1528 | # |
---|
| 1529 | self._add_topic(api, context, topic) |
---|
| 1530 | self.db.commit() |
---|
[359] | 1531 | |
---|
[507] | 1532 | def discussion_topic_reply(self, content, subject): |
---|
[359] | 1533 | |
---|
[507] | 1534 | ## Import modules. |
---|
| 1535 | # |
---|
| 1536 | from tracdiscussion.api import DiscussionApi |
---|
| 1537 | from trac.util.datefmt import to_timestamp, utc |
---|
[359] | 1538 | |
---|
[507] | 1539 | self.logger.debug('Replying to discussion topic', self.id) |
---|
[359] | 1540 | |
---|
[507] | 1541 | ## Get dissussion API component. |
---|
| 1542 | # |
---|
| 1543 | api = self.env[DiscussionApi] |
---|
| 1544 | context = self._create_context(content, subject) |
---|
[359] | 1545 | |
---|
[507] | 1546 | ## Get replied topic. |
---|
| 1547 | # |
---|
| 1548 | topic = api.get_topic(context, self.id) |
---|
[359] | 1549 | |
---|
[507] | 1550 | if not topic: |
---|
| 1551 | self.logger.error("ERROR: Replied topic doesn't exist") |
---|
[359] | 1552 | |
---|
[507] | 1553 | ## Prepare message. |
---|
| 1554 | # |
---|
| 1555 | message = {'forum' : topic['forum'], |
---|
| 1556 | 'topic' : topic['id'], |
---|
| 1557 | 'replyto' : -1, |
---|
| 1558 | 'time' : to_timestamp(datetime.now(utc)), |
---|
| 1559 | 'author' : self.author, |
---|
| 1560 | 'body' : self.get_body_text(context.content_parts)} |
---|
[359] | 1561 | |
---|
[507] | 1562 | ## Add message to DB and commit it. |
---|
| 1563 | # |
---|
| 1564 | self._add_message(api, context, message) |
---|
| 1565 | self.db.commit() |
---|
[359] | 1566 | |
---|
[507] | 1567 | def discussion_message_reply(self, content, subject): |
---|
[359] | 1568 | |
---|
[507] | 1569 | ## Import modules. |
---|
| 1570 | # |
---|
| 1571 | from tracdiscussion.api import DiscussionApi |
---|
| 1572 | from trac.util.datefmt import to_timestamp, utc |
---|
[359] | 1573 | |
---|
[507] | 1574 | self.logger.debug('Replying to discussion message', self.id) |
---|
[359] | 1575 | |
---|
[507] | 1576 | ## Get dissussion API component. |
---|
| 1577 | # |
---|
| 1578 | api = self.env[DiscussionApi] |
---|
| 1579 | context = self._create_context(content, subject) |
---|
[359] | 1580 | |
---|
[507] | 1581 | ## Get replied message. |
---|
| 1582 | # |
---|
| 1583 | message = api.get_message(context, self.id) |
---|
[359] | 1584 | |
---|
[507] | 1585 | if not message: |
---|
| 1586 | self.logger.error("ERROR: Replied message doesn't exist") |
---|
[359] | 1587 | |
---|
[507] | 1588 | ## Prepare message. |
---|
| 1589 | # |
---|
| 1590 | message = {'forum' : message['forum'], |
---|
| 1591 | 'topic' : message['topic'], |
---|
| 1592 | 'replyto' : message['id'], |
---|
| 1593 | 'time' : to_timestamp(datetime.now(utc)), |
---|
| 1594 | 'author' : self.author, |
---|
| 1595 | 'body' : self.get_body_text(context.content_parts)} |
---|
[359] | 1596 | |
---|
[507] | 1597 | ## Add message to DB and commit it. |
---|
| 1598 | # |
---|
| 1599 | self._add_message(api, context, message) |
---|
| 1600 | self.db.commit() |
---|
[359] | 1601 | |
---|
[507] | 1602 | def _create_context(self, content, subject): |
---|
[359] | 1603 | |
---|
[507] | 1604 | ## Import modules. |
---|
| 1605 | # |
---|
| 1606 | from trac.mimeview import Context |
---|
| 1607 | from trac.web.api import Request |
---|
| 1608 | from trac.perm import PermissionCache |
---|
[359] | 1609 | |
---|
[507] | 1610 | ## TODO: Read server base URL from config. |
---|
| 1611 | # Create request object to mockup context creation. |
---|
| 1612 | # |
---|
| 1613 | environ = {'SERVER_PORT' : 80, |
---|
| 1614 | 'SERVER_NAME' : 'test', |
---|
| 1615 | 'REQUEST_METHOD' : 'POST', |
---|
| 1616 | 'wsgi.url_scheme' : 'http', |
---|
| 1617 | 'wsgi.input' : sys.stdin} |
---|
| 1618 | chrome = {'links': {}, |
---|
| 1619 | 'scripts': [], |
---|
| 1620 | 'ctxtnav': [], |
---|
| 1621 | 'warnings': [], |
---|
| 1622 | 'notices': []} |
---|
[359] | 1623 | |
---|
[507] | 1624 | if self.env.base_url_for_redirect: |
---|
| 1625 | environ['trac.base_url'] = self.env.base_url |
---|
[359] | 1626 | |
---|
[507] | 1627 | req = Request(environ, None) |
---|
| 1628 | req.chrome = chrome |
---|
| 1629 | req.tz = 'missing' |
---|
| 1630 | req.authname = self.author |
---|
| 1631 | req.perm = PermissionCache(self.env, self.author) |
---|
| 1632 | req.locale = None |
---|
[359] | 1633 | |
---|
[507] | 1634 | ## Create and return context. |
---|
| 1635 | # |
---|
| 1636 | context = Context.from_request(req) |
---|
| 1637 | context.realm = 'discussion-email2trac' |
---|
| 1638 | context.cursor = self.db.cursor() |
---|
| 1639 | context.content = content |
---|
| 1640 | context.subject = subject |
---|
[359] | 1641 | |
---|
[507] | 1642 | ## Read content parts from content. |
---|
| 1643 | # |
---|
| 1644 | context.content_parts = self.get_message_parts(content) |
---|
| 1645 | context.content_parts = self.unique_attachment_names( |
---|
| 1646 | context.content_parts) |
---|
[359] | 1647 | |
---|
[507] | 1648 | return context |
---|
[359] | 1649 | |
---|
[507] | 1650 | def _add_topic(self, api, context, topic): |
---|
| 1651 | context.req.perm.assert_permission('DISCUSSION_APPEND') |
---|
[359] | 1652 | |
---|
[507] | 1653 | ## Filter topic. |
---|
| 1654 | # |
---|
| 1655 | for discussion_filter in api.discussion_filters: |
---|
| 1656 | accept, topic_or_error = discussion_filter.filter_topic( |
---|
| 1657 | context, topic) |
---|
| 1658 | if accept: |
---|
| 1659 | topic = topic_or_error |
---|
| 1660 | else: |
---|
| 1661 | raise TracError(topic_or_error) |
---|
[359] | 1662 | |
---|
[507] | 1663 | ## Add a new topic. |
---|
| 1664 | # |
---|
| 1665 | api.add_topic(context, topic) |
---|
[359] | 1666 | |
---|
[507] | 1667 | ## Get inserted topic with new ID. |
---|
| 1668 | # |
---|
| 1669 | topic = api.get_topic_by_time(context, topic['time']) |
---|
[359] | 1670 | |
---|
[507] | 1671 | ## Attach attachments. |
---|
| 1672 | # |
---|
| 1673 | self.id = topic['id'] |
---|
| 1674 | self.attach_attachments(context.content_parts, True) |
---|
[359] | 1675 | |
---|
[507] | 1676 | ## Notify change listeners. |
---|
| 1677 | # |
---|
| 1678 | for listener in api.topic_change_listeners: |
---|
| 1679 | listener.topic_created(context, topic) |
---|
[359] | 1680 | |
---|
[507] | 1681 | def _add_message(self, api, context, message): |
---|
| 1682 | context.req.perm.assert_permission('DISCUSSION_APPEND') |
---|
[359] | 1683 | |
---|
[507] | 1684 | ## Filter message. |
---|
| 1685 | # |
---|
| 1686 | for discussion_filter in api.discussion_filters: |
---|
| 1687 | accept, message_or_error = discussion_filter.filter_message( |
---|
| 1688 | context, message) |
---|
| 1689 | if accept: |
---|
| 1690 | message = message_or_error |
---|
| 1691 | else: |
---|
| 1692 | raise TracError(message_or_error) |
---|
[359] | 1693 | |
---|
[507] | 1694 | ## Add message. |
---|
| 1695 | # |
---|
| 1696 | api.add_message(context, message) |
---|
[359] | 1697 | |
---|
[507] | 1698 | ## Get inserted message with new ID. |
---|
| 1699 | # |
---|
| 1700 | message = api.get_message_by_time(context, message['time']) |
---|
[359] | 1701 | |
---|
[507] | 1702 | ## Attach attachments. |
---|
| 1703 | # |
---|
[549] | 1704 | |
---|
[507] | 1705 | self.attach_attachments(context.content_parts, True) |
---|
[359] | 1706 | |
---|
[507] | 1707 | ## Notify change listeners. |
---|
| 1708 | # |
---|
| 1709 | for listener in api.message_change_listeners: |
---|
| 1710 | listener.message_created(context, message) |
---|
[359] | 1711 | |
---|
| 1712 | ########## MAIN function ###################################################### |
---|
| 1713 | |
---|
[507] | 1714 | def parse(self, fp): |
---|
| 1715 | """ |
---|
| 1716 | """ |
---|
| 1717 | self.logger.debug('Main function parse') |
---|
| 1718 | global m |
---|
[96] | 1719 | |
---|
[507] | 1720 | m = email.message_from_file(fp) |
---|
| 1721 | |
---|
| 1722 | if not m: |
---|
| 1723 | self.logger.debug('This is not a valid email message format') |
---|
| 1724 | return |
---|
| 1725 | |
---|
| 1726 | ## Work around lack of header folding in Python; see http://bugs.python.org/issue4696 |
---|
| 1727 | # |
---|
| 1728 | try: |
---|
| 1729 | m.replace_header('Subject', m['Subject'].replace('\r', '').replace('\n', '')) |
---|
| 1730 | except AttributeError, detail: |
---|
| 1731 | pass |
---|
[239] | 1732 | |
---|
[507] | 1733 | if self.parameters.debug: # save the entire e-mail message text |
---|
[528] | 1734 | self.save_email_for_debug(m, self.parameters.project_name, True) |
---|
[77] | 1735 | |
---|
[507] | 1736 | self.db = self.env.get_db_cnx() |
---|
| 1737 | self.get_sender_info(m) |
---|
[152] | 1738 | |
---|
[587] | 1739 | |
---|
[588] | 1740 | if self.parameters.white_list_file: |
---|
| 1741 | self.acl_list_from_file(self.parameters.white_list_file, 'white_list') |
---|
| 1742 | |
---|
[587] | 1743 | if not ( self.email_header_acl('white_list', self.email_addr, True) or self.allow_registered_user ) : |
---|
| 1744 | |
---|
[507] | 1745 | self.logger.info('Message rejected : %s not in white list' %(self.email_addr)) |
---|
| 1746 | return False |
---|
[77] | 1747 | |
---|
[507] | 1748 | if self.email_header_acl('black_list', self.email_addr, False): |
---|
| 1749 | self.logger.info('Message rejected : %s in black list' %(self.email_addr)) |
---|
| 1750 | return False |
---|
[221] | 1751 | |
---|
[570] | 1752 | if not self.email_header_acl('recipient_list', self.email_to_addrs, True): |
---|
| 1753 | self.logger.info('Message rejected : %s not in recipient list' %(self.email_to_addrs)) |
---|
[507] | 1754 | return False |
---|
[226] | 1755 | |
---|
[507] | 1756 | ## If spam drop the message |
---|
| 1757 | # |
---|
| 1758 | if self.spam(m) == 'drop': |
---|
| 1759 | return False |
---|
[194] | 1760 | |
---|
[507] | 1761 | elif self.spam(m) == 'spam': |
---|
| 1762 | spam_msg = True |
---|
| 1763 | else: |
---|
| 1764 | spam_msg = False |
---|
[204] | 1765 | |
---|
[507] | 1766 | if not m['Subject']: |
---|
| 1767 | subject = 'No Subject' |
---|
| 1768 | else: |
---|
[564] | 1769 | subject = self.email_to_unicode(m['Subject']) |
---|
[304] | 1770 | |
---|
[564] | 1771 | ## Bug in python logging module <2.6 |
---|
| 1772 | # |
---|
| 1773 | self.logger.info('subject: %s' %repr(subject)) |
---|
[359] | 1774 | |
---|
[507] | 1775 | ## [hic] #1529: Re: LRZ |
---|
| 1776 | # [hic] #1529?owner=bas,priority=medium: Re: LRZ |
---|
| 1777 | # |
---|
| 1778 | ticket_regex = r''' |
---|
| 1779 | (?P<new_fields>[#][?].*) |
---|
| 1780 | |(?P<reply>(?P<id>[#][\d]+)(?P<fields>\?.*)?:) |
---|
| 1781 | ''' |
---|
| 1782 | ## Check if FullBlogPlugin is installed |
---|
| 1783 | # |
---|
| 1784 | blog_enabled = None |
---|
| 1785 | blog_regex = '' |
---|
| 1786 | if self.get_config('components', 'tracfullblog.*') in ['enabled']: |
---|
| 1787 | self.logger.debug('Trac BLOG support enabled') |
---|
| 1788 | blog_enabled = True |
---|
[585] | 1789 | blog_regex = '''|(?P<blog>blog(?P<blog_params>[?][^:]*)?:(?P<blog_id>\S*))''' |
---|
[329] | 1790 | |
---|
[77] | 1791 | |
---|
[507] | 1792 | ## Check if DiscussionPlugin is installed |
---|
| 1793 | # |
---|
| 1794 | discussion_enabled = None |
---|
| 1795 | discussion_regex = '' |
---|
| 1796 | if self.get_config('components', 'tracdiscussion.api.discussionapi') in ['enabled']: |
---|
| 1797 | self.logger.debug('Trac Discussion support enabled') |
---|
| 1798 | discussion_enabled = True |
---|
| 1799 | discussion_regex = r''' |
---|
| 1800 | |(?P<forum>Forum[ ][#](?P<forum_id>\d+)[ ]-[ ]?) |
---|
| 1801 | |(?P<topic>Topic[ ][#](?P<topic_id>\d+)[ ]-[ ]?) |
---|
| 1802 | |(?P<message>Message[ ][#](?P<message_id>\d+)[ ]-[ ]?) |
---|
| 1803 | ''' |
---|
[77] | 1804 | |
---|
[359] | 1805 | |
---|
[507] | 1806 | regex_str = ticket_regex + blog_regex + discussion_regex |
---|
| 1807 | SYSTEM_RE = re.compile(regex_str, re.VERBOSE) |
---|
[359] | 1808 | |
---|
[507] | 1809 | ## Find out if this is a ticket, a blog or a discussion |
---|
| 1810 | # |
---|
| 1811 | result = SYSTEM_RE.search(subject) |
---|
[390] | 1812 | |
---|
[507] | 1813 | if result: |
---|
| 1814 | ## update ticket + fields |
---|
| 1815 | # |
---|
| 1816 | if result.group('reply'): |
---|
| 1817 | self.system = 'ticket' |
---|
[260] | 1818 | |
---|
[507] | 1819 | ## Skip the last ':' character |
---|
| 1820 | # |
---|
| 1821 | if not self.ticket_update(m, result.group('reply')[:-1], spam_msg): |
---|
| 1822 | self.new_ticket(m, subject, spam_msg) |
---|
[390] | 1823 | |
---|
[507] | 1824 | ## New ticket + fields |
---|
| 1825 | # |
---|
| 1826 | elif result.group('new_fields'): |
---|
| 1827 | self.system = 'ticket' |
---|
| 1828 | self.new_ticket(m, subject[:result.start('new_fields')], spam_msg, result.group('new_fields')) |
---|
[262] | 1829 | |
---|
[507] | 1830 | if blog_enabled: |
---|
| 1831 | if result.group('blog'): |
---|
| 1832 | self.system = 'blog' |
---|
[585] | 1833 | self.blog(m, subject[result.end('blog'):], result.group('blog_id'), result.group('blog_params')) |
---|
[359] | 1834 | |
---|
[507] | 1835 | if discussion_enabled: |
---|
| 1836 | ## New topic. |
---|
| 1837 | # |
---|
| 1838 | if result.group('forum'): |
---|
| 1839 | self.system = 'discussion' |
---|
| 1840 | self.id = int(result.group('forum_id')) |
---|
| 1841 | self.discussion_topic(m, subject[result.end('forum'):]) |
---|
[359] | 1842 | |
---|
[507] | 1843 | ## Reply to topic. |
---|
| 1844 | # |
---|
| 1845 | elif result.group('topic'): |
---|
| 1846 | self.system = 'discussion' |
---|
| 1847 | self.id = int(result.group('topic_id')) |
---|
| 1848 | self.discussion_topic_reply(m, subject[result.end('topic'):]) |
---|
[359] | 1849 | |
---|
[507] | 1850 | ## Reply to topic message. |
---|
| 1851 | # |
---|
| 1852 | elif result.group('message'): |
---|
| 1853 | self.system = 'discussion' |
---|
| 1854 | self.id = int(result.group('message_id')) |
---|
| 1855 | self.discussion_message_reply(m, subject[result.end('message'):]) |
---|
[359] | 1856 | |
---|
[507] | 1857 | else: |
---|
[553] | 1858 | |
---|
[507] | 1859 | self.system = 'ticket' |
---|
| 1860 | (matched_id, subject) = self.ticket_update_by_subject(subject) |
---|
[553] | 1861 | |
---|
[507] | 1862 | if matched_id: |
---|
[553] | 1863 | |
---|
[507] | 1864 | if not self.ticket_update(m, matched_id, spam_msg): |
---|
| 1865 | self.new_ticket(m, subject, spam_msg) |
---|
[553] | 1866 | |
---|
[507] | 1867 | else: |
---|
| 1868 | ## No update by subject, so just create a new ticket |
---|
| 1869 | # |
---|
| 1870 | self.new_ticket(m, subject, spam_msg) |
---|
[353] | 1871 | |
---|
[356] | 1872 | |
---|
[343] | 1873 | ########## BODY TEXT functions ########################################################### |
---|
| 1874 | |
---|
[507] | 1875 | def strip_signature(self, text): |
---|
| 1876 | """ |
---|
| 1877 | Strip signature from message, inspired by Mailman software |
---|
| 1878 | """ |
---|
| 1879 | self.logger.debug('function strip_signature') |
---|
[462] | 1880 | |
---|
[507] | 1881 | body = [] |
---|
| 1882 | for line in text.splitlines(): |
---|
| 1883 | if line == '-- ': |
---|
| 1884 | break |
---|
| 1885 | body.append(line) |
---|
[136] | 1886 | |
---|
[507] | 1887 | return ('\n'.join(body)) |
---|
[136] | 1888 | |
---|
[507] | 1889 | def reflow(self, text, delsp = 0): |
---|
| 1890 | """ |
---|
| 1891 | Reflow the message based on the format="flowed" specification (RFC 3676) |
---|
| 1892 | """ |
---|
| 1893 | flowedlines = [] |
---|
| 1894 | quotelevel = 0 |
---|
| 1895 | prevflowed = 0 |
---|
[231] | 1896 | |
---|
[507] | 1897 | for line in text.splitlines(): |
---|
| 1898 | from re import match |
---|
| 1899 | |
---|
| 1900 | ## Figure out the quote level and the content of the current line |
---|
| 1901 | # |
---|
| 1902 | m = match('(>*)( ?)(.*)', line) |
---|
| 1903 | linequotelevel = len(m.group(1)) |
---|
| 1904 | line = m.group(3) |
---|
[231] | 1905 | |
---|
[507] | 1906 | ## Determine whether this line is flowed |
---|
| 1907 | # |
---|
| 1908 | if line and line != '-- ' and line[-1] == ' ': |
---|
| 1909 | flowed = 1 |
---|
| 1910 | else: |
---|
| 1911 | flowed = 0 |
---|
[231] | 1912 | |
---|
[507] | 1913 | if flowed and delsp and line and line[-1] == ' ': |
---|
| 1914 | line = line[:-1] |
---|
[231] | 1915 | |
---|
[507] | 1916 | ## If the previous line is flowed, append this line to it |
---|
| 1917 | # |
---|
| 1918 | if prevflowed and line != '-- ' and linequotelevel == quotelevel: |
---|
| 1919 | flowedlines[-1] += line |
---|
[477] | 1920 | |
---|
[507] | 1921 | ## Otherwise, start a new line |
---|
| 1922 | # |
---|
| 1923 | else: |
---|
| 1924 | flowedlines.append('>' * linequotelevel + line) |
---|
[231] | 1925 | |
---|
[507] | 1926 | prevflowed = flowed |
---|
| 1927 | |
---|
[231] | 1928 | |
---|
[507] | 1929 | return '\n'.join(flowedlines) |
---|
[231] | 1930 | |
---|
[507] | 1931 | def strip_quotes(self, text): |
---|
| 1932 | """ |
---|
| 1933 | Strip quotes from message by Nicolas Mendoza |
---|
| 1934 | """ |
---|
| 1935 | self.logger.debug('function strip_quotes') |
---|
[462] | 1936 | |
---|
[507] | 1937 | body = [] |
---|
| 1938 | for line in text.splitlines(): |
---|
| 1939 | try: |
---|
[462] | 1940 | |
---|
[507] | 1941 | if line.startswith(self.parameters.email_quote): |
---|
| 1942 | continue |
---|
[462] | 1943 | |
---|
[507] | 1944 | except UnicodeDecodeError: |
---|
[462] | 1945 | |
---|
[507] | 1946 | tmp_line = self.email_to_unicode(line) |
---|
| 1947 | if tmp_line.startswith(self.parameters.email_quote): |
---|
| 1948 | continue |
---|
| 1949 | |
---|
| 1950 | body.append(line) |
---|
[151] | 1951 | |
---|
[507] | 1952 | return ('\n'.join(body)) |
---|
[191] | 1953 | |
---|
[507] | 1954 | def inline_properties(self, text): |
---|
| 1955 | """ |
---|
| 1956 | Parse text if we use inline keywords to set ticket fields |
---|
| 1957 | """ |
---|
| 1958 | self.logger.debug('function inline_properties') |
---|
[309] | 1959 | |
---|
[507] | 1960 | properties = dict() |
---|
| 1961 | body = list() |
---|
[309] | 1962 | |
---|
[507] | 1963 | INLINE_EXP = re.compile('\s*[@]\s*(\w+)\s*:(.*)$') |
---|
[309] | 1964 | |
---|
[507] | 1965 | for line in text.splitlines(): |
---|
| 1966 | match = INLINE_EXP.match(line) |
---|
| 1967 | if match: |
---|
| 1968 | keyword, value = match.groups() |
---|
[509] | 1969 | |
---|
| 1970 | if self.parameters.inline_properties_first_wins: |
---|
| 1971 | if keyword in self.properties.keys(): |
---|
| 1972 | continue |
---|
| 1973 | |
---|
[507] | 1974 | self.properties[keyword] = value.strip() |
---|
[509] | 1975 | self.logger.debug('\tinline properties: %s : %s' %(keyword,value)) |
---|
[408] | 1976 | |
---|
[507] | 1977 | else: |
---|
| 1978 | body.append(line) |
---|
| 1979 | |
---|
| 1980 | return '\n'.join(body) |
---|
[309] | 1981 | |
---|
| 1982 | |
---|
[507] | 1983 | def wrap_text(self, text, replace_whitespace = False): |
---|
| 1984 | """ |
---|
| 1985 | Will break a lines longer then given length into several small |
---|
| 1986 | lines of size given length |
---|
| 1987 | """ |
---|
| 1988 | import textwrap |
---|
[154] | 1989 | |
---|
[507] | 1990 | LINESEPARATOR = '\n' |
---|
| 1991 | reformat = '' |
---|
[151] | 1992 | |
---|
[507] | 1993 | for s in text.split(LINESEPARATOR): |
---|
| 1994 | tmp = textwrap.fill(s, self.parameters.use_textwrap) |
---|
| 1995 | if tmp: |
---|
| 1996 | reformat = '%s\n%s' %(reformat,tmp) |
---|
| 1997 | else: |
---|
| 1998 | reformat = '%s\n' %reformat |
---|
[153] | 1999 | |
---|
[507] | 2000 | return reformat |
---|
[153] | 2001 | |
---|
[507] | 2002 | # Python2.4 and higher |
---|
| 2003 | # |
---|
| 2004 | #return LINESEPARATOR.join(textwrap.fill(s,width) for s in str.split(LINESEPARATOR)) |
---|
| 2005 | # |
---|
[154] | 2006 | |
---|
[343] | 2007 | ########## EMAIL attachements functions ########################################################### |
---|
| 2008 | |
---|
[507] | 2009 | def inline_part(self, part): |
---|
| 2010 | """ |
---|
| 2011 | """ |
---|
| 2012 | self.logger.debug('function inline_part()') |
---|
[154] | 2013 | |
---|
[507] | 2014 | return part.get_param('inline', None, 'Content-Disposition') == '' or not part.has_key('Content-Disposition') |
---|
[340] | 2015 | |
---|
[604] | 2016 | def get_message_parts(self, msg, new_email=False): |
---|
[507] | 2017 | """ |
---|
| 2018 | parses the email message and returns a list of body parts and attachments |
---|
| 2019 | body parts are returned as strings, attachments are returned as tuples of (filename, Message object) |
---|
| 2020 | """ |
---|
| 2021 | self.logger.debug('function get_message_parts()') |
---|
[317] | 2022 | |
---|
[507] | 2023 | message_parts = list() |
---|
| 2024 | |
---|
| 2025 | ALTERNATIVE_MULTIPART = False |
---|
[278] | 2026 | |
---|
[507] | 2027 | for part in msg.walk(): |
---|
| 2028 | content_maintype = part.get_content_maintype() |
---|
| 2029 | content_type = part.get_content_type() |
---|
[278] | 2030 | |
---|
[507] | 2031 | self.logger.debug('\t Message part: Main-Type: %s' % content_maintype) |
---|
| 2032 | self.logger.debug('\t Message part: Content-Type: %s' % content_type) |
---|
[498] | 2033 | |
---|
[507] | 2034 | ## Check content type |
---|
| 2035 | # |
---|
| 2036 | if content_type in self.STRIP_CONTENT_TYPES: |
---|
| 2037 | self.logger.debug("\t A %s attachment named '%s' was skipped" %(content_type, part.get_filename())) |
---|
| 2038 | continue |
---|
[238] | 2039 | |
---|
[507] | 2040 | ## Catch some mulitpart execptions |
---|
| 2041 | # |
---|
| 2042 | if content_type == 'multipart/alternative': |
---|
| 2043 | ALTERNATIVE_MULTIPART = True |
---|
| 2044 | continue |
---|
[278] | 2045 | |
---|
[507] | 2046 | ## Skip multipart containers |
---|
| 2047 | # |
---|
| 2048 | if content_maintype == 'multipart': |
---|
| 2049 | self.logger.debug("\t Skipping multipart container") |
---|
| 2050 | continue |
---|
| 2051 | |
---|
[569] | 2052 | ## Check if this is an inline part. It's inline if there is co Cont-Disp header, |
---|
| 2053 | # or if there is one and it says "inline" |
---|
[507] | 2054 | # |
---|
| 2055 | inline = self.inline_part(part) |
---|
[236] | 2056 | |
---|
[507] | 2057 | ## Drop HTML message |
---|
| 2058 | # |
---|
| 2059 | if ALTERNATIVE_MULTIPART and self.parameters.drop_alternative_html_version: |
---|
[553] | 2060 | |
---|
[507] | 2061 | if content_type == 'text/html': |
---|
| 2062 | self.logger.debug('\t Skipping alternative HTML message') |
---|
| 2063 | ALTERNATIVE_MULTIPART = False |
---|
| 2064 | continue |
---|
[278] | 2065 | |
---|
[569] | 2066 | filename = part.get_filename() |
---|
| 2067 | s = '\t unicode filename: %s' %(filename) |
---|
| 2068 | self.print_unicode(s) |
---|
| 2069 | self.logger.debug('\t raw filename: %s' %repr(filename)) |
---|
[498] | 2070 | |
---|
[569] | 2071 | filename = self.check_filename_length(filename) |
---|
| 2072 | |
---|
[507] | 2073 | ## Save all non plain text message as attachment |
---|
| 2074 | # |
---|
| 2075 | if not content_type in ['text/plain']: |
---|
[498] | 2076 | |
---|
[569] | 2077 | message_parts.append( (filename, part) ) |
---|
[498] | 2078 | |
---|
[569] | 2079 | ## We only convert html messages |
---|
[507] | 2080 | # |
---|
| 2081 | if not content_type == 'text/html': |
---|
[569] | 2082 | self.logger.debug('\t Appending %s (%s)' %(repr(filename), content_type)) |
---|
[507] | 2083 | continue |
---|
[500] | 2084 | |
---|
[569] | 2085 | |
---|
| 2086 | ## We have an text or html message |
---|
| 2087 | # |
---|
[507] | 2088 | if not inline: |
---|
[569] | 2089 | self.logger.debug('\t Appending %s (%s), not an inline messsage part' %(repr(filename), content_type)) |
---|
| 2090 | message_parts.append( (filename, part) ) |
---|
[507] | 2091 | continue |
---|
| 2092 | |
---|
| 2093 | ## Try to decode message part. We have a html or plain text messafe |
---|
| 2094 | # |
---|
| 2095 | body_text = part.get_payload(decode=1) |
---|
| 2096 | if not body_text: |
---|
| 2097 | body_text = part.get_payload(decode=0) |
---|
[236] | 2098 | |
---|
[507] | 2099 | ## Try to convert html message |
---|
| 2100 | # |
---|
| 2101 | if content_type == 'text/html': |
---|
[502] | 2102 | |
---|
[507] | 2103 | body_text = self.html_2_txt(body_text) |
---|
| 2104 | if not body_text: |
---|
| 2105 | continue |
---|
[498] | 2106 | |
---|
[507] | 2107 | format = email.Utils.collapse_rfc2231_value(part.get_param('Format', 'fixed')).lower() |
---|
| 2108 | delsp = email.Utils.collapse_rfc2231_value(part.get_param('DelSp', 'no')).lower() |
---|
[231] | 2109 | |
---|
[507] | 2110 | if self.parameters.reflow and not self.parameters.verbatim_format and format == 'flowed': |
---|
| 2111 | body_text = self.reflow(body_text, delsp == 'yes') |
---|
[22] | 2112 | |
---|
[604] | 2113 | if new_email and self.parameters.only_strip_on_update: |
---|
| 2114 | self.logger.debug('Skip signature/quote stripping for new messages') |
---|
| 2115 | else: |
---|
| 2116 | if self.parameters.strip_signature: |
---|
| 2117 | body_text = self.strip_signature(body_text) |
---|
[191] | 2118 | |
---|
[604] | 2119 | if self.parameters.strip_quotes: |
---|
| 2120 | body_text = self.strip_quotes(body_text) |
---|
| 2121 | |
---|
[507] | 2122 | if self.parameters.inline_properties: |
---|
| 2123 | body_text = self.inline_properties(body_text) |
---|
[309] | 2124 | |
---|
[507] | 2125 | if self.parameters.use_textwrap: |
---|
| 2126 | body_text = self.wrap_text(body_text) |
---|
[148] | 2127 | |
---|
[507] | 2128 | ## Get contents charset (iso-8859-15 if not defined in mail headers) |
---|
| 2129 | # |
---|
| 2130 | charset = part.get_content_charset() |
---|
| 2131 | if not charset: |
---|
| 2132 | charset = 'iso-8859-15' |
---|
[102] | 2133 | |
---|
[507] | 2134 | try: |
---|
| 2135 | ubody_text = unicode(body_text, charset) |
---|
[100] | 2136 | |
---|
[507] | 2137 | except UnicodeError, detail: |
---|
| 2138 | ubody_text = unicode(body_text, 'iso-8859-15') |
---|
[89] | 2139 | |
---|
[507] | 2140 | except LookupError, detail: |
---|
| 2141 | ubody_text = 'ERROR: Could not find charset: %s, please install' %(charset) |
---|
[100] | 2142 | |
---|
[507] | 2143 | if self.parameters.verbatim_format: |
---|
| 2144 | message_parts.append('{{{\r\n%s\r\n}}}' %ubody_text) |
---|
| 2145 | else: |
---|
| 2146 | message_parts.append('%s' %ubody_text) |
---|
[22] | 2147 | |
---|
[507] | 2148 | return message_parts |
---|
| 2149 | |
---|
| 2150 | def unique_attachment_names(self, message_parts): |
---|
| 2151 | """ |
---|
| 2152 | Make sure we have unique names attachments: |
---|
| 2153 | - check if it contains illegal characters |
---|
| 2154 | - Rename "None" filenames to "untitled-part" |
---|
| 2155 | """ |
---|
| 2156 | self.logger.debug('function unique_attachment_names()') |
---|
| 2157 | renamed_parts = [] |
---|
| 2158 | attachment_names = set() |
---|
[296] | 2159 | |
---|
[507] | 2160 | for item in message_parts: |
---|
| 2161 | |
---|
| 2162 | ## If not an attachment, leave it alone |
---|
| 2163 | # |
---|
| 2164 | if not isinstance(item, tuple): |
---|
| 2165 | renamed_parts.append(item) |
---|
| 2166 | continue |
---|
| 2167 | |
---|
| 2168 | (filename, part) = item |
---|
[295] | 2169 | |
---|
[507] | 2170 | ## If filename = None, use a default one |
---|
| 2171 | # |
---|
| 2172 | if filename in [ 'None']: |
---|
| 2173 | filename = 'untitled-part' |
---|
[550] | 2174 | self.logger.info('\t Rename filename "None" to: %s' %filename) |
---|
[22] | 2175 | |
---|
[507] | 2176 | ## Guess the extension from the content type, use non strict mode |
---|
| 2177 | # some additional non-standard but commonly used MIME types |
---|
| 2178 | # are also recognized |
---|
| 2179 | # |
---|
| 2180 | ext = mimetypes.guess_extension(part.get_content_type(), False) |
---|
| 2181 | if not ext: |
---|
| 2182 | ext = '.bin' |
---|
[22] | 2183 | |
---|
[507] | 2184 | filename = '%s%s' % (filename, ext) |
---|
[22] | 2185 | |
---|
[507] | 2186 | ## Discard relative paths for windows/unix in attachment names |
---|
| 2187 | # |
---|
| 2188 | filename = filename.replace('\\', '_') |
---|
| 2189 | filename = filename.replace('/', '_') |
---|
[347] | 2190 | |
---|
[507] | 2191 | ## remove linefeed char |
---|
| 2192 | # |
---|
| 2193 | for forbidden_char in ['\r', '\n']: |
---|
| 2194 | filename = filename.replace(forbidden_char,'') |
---|
[465] | 2195 | |
---|
[507] | 2196 | ## We try to normalize the filename to utf-8 NFC if we can. |
---|
| 2197 | # Files uploaded from OS X might be in NFD. |
---|
| 2198 | # Check python version and then try it |
---|
| 2199 | # |
---|
| 2200 | #if sys.version_info[0] > 2 or (sys.version_info[0] == 2 and sys.version_info[1] >= 3): |
---|
| 2201 | # try: |
---|
| 2202 | # filename = unicodedata.normalize('NFC', unicode(filename, 'utf-8')).encode('utf-8') |
---|
| 2203 | # except TypeError: |
---|
| 2204 | # pass |
---|
[100] | 2205 | |
---|
[507] | 2206 | ## Make the filename unique for this ticket |
---|
| 2207 | # |
---|
| 2208 | num = 0 |
---|
| 2209 | unique_filename = filename |
---|
| 2210 | dummy_filename, ext = os.path.splitext(filename) |
---|
[134] | 2211 | |
---|
[507] | 2212 | while (unique_filename in attachment_names) or self.attachment_exists(unique_filename): |
---|
| 2213 | num += 1 |
---|
| 2214 | unique_filename = "%s-%s%s" % (dummy_filename, num, ext) |
---|
| 2215 | |
---|
[550] | 2216 | s = '\t Attachment with filename %s will be saved as %s' % (filename, unique_filename) |
---|
[549] | 2217 | self.print_unicode(s) |
---|
[100] | 2218 | |
---|
[507] | 2219 | attachment_names.add(unique_filename) |
---|
[236] | 2220 | |
---|
[507] | 2221 | renamed_parts.append((filename, unique_filename, part)) |
---|
| 2222 | |
---|
| 2223 | return renamed_parts |
---|
| 2224 | |
---|
| 2225 | |
---|
| 2226 | def attachment_exists(self, filename): |
---|
[250] | 2227 | |
---|
[549] | 2228 | self.logger.debug("function attachment_exists") |
---|
[250] | 2229 | |
---|
[549] | 2230 | s = '\t check if attachment already exists: Id : %s, Filename : %s' %(self.id, filename) |
---|
| 2231 | self.print_unicode(s) |
---|
| 2232 | |
---|
[553] | 2233 | ## Do we have a valid ticket id |
---|
[507] | 2234 | # |
---|
| 2235 | if not self.id: |
---|
| 2236 | return False |
---|
[250] | 2237 | |
---|
[507] | 2238 | try: |
---|
| 2239 | if self.system == 'discussion': |
---|
[553] | 2240 | |
---|
| 2241 | att = attachment.Attachment(self.env, 'discussion', 'ticket/%s' % (self.id,), filename) |
---|
| 2242 | |
---|
[585] | 2243 | elif self.system == 'blog': |
---|
| 2244 | |
---|
| 2245 | att = attachment.Attachment(self.env, 'blog', '%s' % (self.id,), filename) |
---|
| 2246 | |
---|
[507] | 2247 | else: |
---|
[553] | 2248 | |
---|
| 2249 | att = attachment.Attachment(self.env, 'ticket', self.id, filename) |
---|
| 2250 | |
---|
[507] | 2251 | return True |
---|
[553] | 2252 | |
---|
[507] | 2253 | except attachment.ResourceNotFound: |
---|
[553] | 2254 | |
---|
[507] | 2255 | return False |
---|
[343] | 2256 | |
---|
| 2257 | ########## TRAC Ticket Text ########################################################### |
---|
[507] | 2258 | |
---|
| 2259 | def get_body_text(self, message_parts): |
---|
| 2260 | """ |
---|
| 2261 | """ |
---|
| 2262 | self.logger.debug('function get_body_text()') |
---|
[498] | 2263 | |
---|
[507] | 2264 | body_text = [] |
---|
| 2265 | |
---|
| 2266 | for part in message_parts: |
---|
| 2267 | |
---|
| 2268 | ## Plain text part, append it |
---|
| 2269 | # |
---|
| 2270 | if not isinstance(part, tuple): |
---|
| 2271 | body_text.extend(part.strip().splitlines()) |
---|
| 2272 | body_text.append("") |
---|
| 2273 | continue |
---|
[490] | 2274 | |
---|
[507] | 2275 | (original, filename, part) = part |
---|
| 2276 | inline = self.inline_part(part) |
---|
[491] | 2277 | |
---|
[507] | 2278 | ## Skip generation of attachment link if html is converted to text |
---|
| 2279 | # |
---|
| 2280 | if part.get_content_type() == 'text/html' and self.parameters.html2text_cmd and inline: |
---|
| 2281 | s = 'Skipping attachment link for html part: %s' %(filename) |
---|
| 2282 | self.print_unicode(s) |
---|
| 2283 | continue |
---|
| 2284 | |
---|
| 2285 | if part.get_content_maintype() == 'image' and inline: |
---|
[553] | 2286 | |
---|
[507] | 2287 | if self.system != 'discussion': |
---|
| 2288 | s = 'wiki image link for: %s' %(filename) |
---|
| 2289 | self.print_unicode(s) |
---|
| 2290 | body_text.append('[[Image(%s)]]' % filename) |
---|
[553] | 2291 | |
---|
[507] | 2292 | body_text.append("") |
---|
[553] | 2293 | |
---|
[507] | 2294 | else: |
---|
[553] | 2295 | |
---|
[507] | 2296 | if self.system != 'discussion': |
---|
[553] | 2297 | |
---|
[507] | 2298 | s = 'wiki attachment link for: %s' %(filename) |
---|
| 2299 | self.print_unicode(s) |
---|
| 2300 | body_text.append('[attachment:"%s"]' % filename) |
---|
[553] | 2301 | |
---|
[507] | 2302 | body_text.append("") |
---|
[490] | 2303 | |
---|
[507] | 2304 | ## Convert list body_texts to string |
---|
| 2305 | # |
---|
| 2306 | body_text = '\r\n'.join(body_text) |
---|
| 2307 | return body_text |
---|
[236] | 2308 | |
---|
[507] | 2309 | def html_mailto_link(self, subject): |
---|
| 2310 | """ |
---|
| 2311 | This function returns a HTML mailto tag with the ticket id and author email address |
---|
| 2312 | """ |
---|
| 2313 | self.logger.debug("function html_mailto_link") |
---|
[576] | 2314 | |
---|
[507] | 2315 | if not self.author: |
---|
| 2316 | author = self.email_addr |
---|
| 2317 | else: |
---|
| 2318 | author = self.author |
---|
[343] | 2319 | |
---|
[507] | 2320 | if not self.parameters.mailto_cc: |
---|
| 2321 | self.parameters.mailto_cc = '' |
---|
[592] | 2322 | |
---|
| 2323 | ## Bug in urllib.quote function |
---|
| 2324 | # |
---|
| 2325 | if isinstance(subject, unicode): |
---|
| 2326 | subject = subject.encode('utf-8') |
---|
[434] | 2327 | |
---|
[507] | 2328 | ## use urllib to escape the chars |
---|
| 2329 | # |
---|
[517] | 2330 | s = '%s?Subject=%s&cc=%s' %( |
---|
[507] | 2331 | urllib.quote(self.email_addr), |
---|
| 2332 | urllib.quote('Re: #%s: %s' %(self.id, subject)), |
---|
| 2333 | urllib.quote(self.parameters.mailto_cc) |
---|
| 2334 | ) |
---|
[343] | 2335 | |
---|
[507] | 2336 | if self.VERSION in [ 0.10 ]: |
---|
| 2337 | s = '\r\n{{{\r\n#!html\r\n<a\r\n href="mailto:%s">Reply to: %s\r\n</a>\r\n}}}\r\n' %(s, author) |
---|
| 2338 | else: |
---|
| 2339 | s = '[mailto:"%s" Reply to: %s]' %(s, author) |
---|
[506] | 2340 | |
---|
[507] | 2341 | self.logger.debug("\tmailto link %s" %s) |
---|
| 2342 | return s |
---|
[343] | 2343 | |
---|
| 2344 | ########## TRAC notify section ########################################################### |
---|
| 2345 | |
---|
[507] | 2346 | def notify(self, tkt, new=True, modtime=0): |
---|
| 2347 | """ |
---|
| 2348 | A wrapper for the TRAC notify function. So we can use templates |
---|
| 2349 | """ |
---|
| 2350 | self.logger.debug('function notify()') |
---|
[392] | 2351 | |
---|
[509] | 2352 | if self.parameters.notify_reporter: |
---|
| 2353 | self.logger.debug('\t Notify reporter set') |
---|
[508] | 2354 | global sender_email |
---|
| 2355 | sender_email = self.email_addr |
---|
| 2356 | |
---|
| 2357 | self.logger.debug('\t Using Email2TracNotification function AlwaysNotifyReporter') |
---|
| 2358 | import trac.notification as Email2TracNotification |
---|
| 2359 | Email2TracNotification.Notify.notify = AlwaysNotifyReporter |
---|
| 2360 | |
---|
| 2361 | if self.parameters.dry_run : |
---|
[561] | 2362 | self.logger.info('DRY_RUN: self.notify(tkt, True) reporter = %s' %tkt['reporter']) |
---|
[507] | 2363 | return |
---|
| 2364 | try: |
---|
[392] | 2365 | |
---|
[507] | 2366 | #from trac.ticket.notification import TicketNotificationSystem |
---|
| 2367 | #tn_sys = TicketNotificationSystem(self.env) |
---|
| 2368 | #print tn_sys |
---|
| 2369 | #print tn_sys.__dict__ |
---|
| 2370 | #sys.exit(0) |
---|
[392] | 2371 | |
---|
[507] | 2372 | ## create false {abs_}href properties, to trick Notify() |
---|
| 2373 | # |
---|
[540] | 2374 | if self.VERSION in [0.10]: |
---|
[507] | 2375 | self.env.abs_href = Href(self.get_config('project', 'url')) |
---|
| 2376 | self.env.href = Href(self.get_config('project', 'url')) |
---|
[22] | 2377 | |
---|
[507] | 2378 | tn = TicketNotifyEmail(self.env) |
---|
[213] | 2379 | |
---|
[507] | 2380 | if self.parameters.alternate_notify_template: |
---|
[222] | 2381 | |
---|
[507] | 2382 | if self.VERSION >= 0.11: |
---|
[222] | 2383 | |
---|
[507] | 2384 | from trac.web.chrome import Chrome |
---|
[222] | 2385 | |
---|
[507] | 2386 | if self.parameters.alternate_notify_template_update and not new: |
---|
| 2387 | tn.template_name = self.parameters.alternate_notify_template_update |
---|
| 2388 | else: |
---|
| 2389 | tn.template_name = self.parameters.alternate_notify_template |
---|
[222] | 2390 | |
---|
[507] | 2391 | tn.template = Chrome(tn.env).load_template(tn.template_name, method='text') |
---|
| 2392 | |
---|
| 2393 | else: |
---|
[222] | 2394 | |
---|
[507] | 2395 | tn.template_name = self.parameters.alternate_notify_template |
---|
[42] | 2396 | |
---|
[507] | 2397 | tn.notify(tkt, new, modtime) |
---|
[41] | 2398 | |
---|
[507] | 2399 | except Exception, e: |
---|
| 2400 | self.logger.error('Failure sending notification on creation of ticket #%s: %s' %(self.id, e)) |
---|
[41] | 2401 | |
---|
[508] | 2402 | ########## END Class Definition ######################################################## |
---|
[22] | 2403 | |
---|
[508] | 2404 | ########## Global Notificaition Function ################################################ |
---|
| 2405 | def AlwaysNotifyReporter(self, resid): |
---|
| 2406 | """ |
---|
| 2407 | Copy of def notify() to manipulate recipents to always include reporter for the |
---|
| 2408 | notification. |
---|
| 2409 | """ |
---|
[569] | 2410 | #print sender_email, resid |
---|
[508] | 2411 | (torcpts, ccrcpts) = self.get_recipients(resid) |
---|
[569] | 2412 | #print "get_recipients finished" |
---|
[74] | 2413 | |
---|
[508] | 2414 | if not tktparser.email_header_acl('notify_reporter_black_list', sender_email, False): |
---|
| 2415 | ## additionally append sender (regardeless of settings in trac.ini) |
---|
| 2416 | # |
---|
| 2417 | if not sender_email in torcpts: |
---|
| 2418 | torcpts.append(sender_email) |
---|
| 2419 | |
---|
| 2420 | self.begin_send() |
---|
| 2421 | self.send(torcpts, ccrcpts) |
---|
| 2422 | self.finish_send() |
---|
| 2423 | |
---|
[343] | 2424 | ########## Parse Config File ########################################################### |
---|
[22] | 2425 | |
---|
| 2426 | def ReadConfig(file, name): |
---|
[507] | 2427 | """ |
---|
| 2428 | Parse the config file |
---|
| 2429 | """ |
---|
| 2430 | if not os.path.isfile(file): |
---|
| 2431 | print 'File %s does not exist' %file |
---|
| 2432 | sys.exit(1) |
---|
[22] | 2433 | |
---|
[507] | 2434 | config = trac_config.Configuration(file) |
---|
| 2435 | |
---|
| 2436 | parentdir = config.get('DEFAULT', 'parentdir') |
---|
| 2437 | sections = config.sections() |
---|
[22] | 2438 | |
---|
[507] | 2439 | ## use some trac internals to get the defaults |
---|
| 2440 | # |
---|
| 2441 | tmp = config.parser.defaults() |
---|
| 2442 | project = SaraDict() |
---|
[485] | 2443 | |
---|
[507] | 2444 | for option, value in tmp.items(): |
---|
| 2445 | try: |
---|
| 2446 | project[option] = int(value) |
---|
| 2447 | except ValueError: |
---|
| 2448 | project[option] = value |
---|
[485] | 2449 | |
---|
[507] | 2450 | if name: |
---|
| 2451 | if name in sections: |
---|
| 2452 | project = SaraDict() |
---|
| 2453 | for option, value in config.options(name): |
---|
| 2454 | try: |
---|
| 2455 | project[option] = int(value) |
---|
| 2456 | except ValueError: |
---|
| 2457 | project[option] = value |
---|
[485] | 2458 | |
---|
[507] | 2459 | elif not parentdir: |
---|
| 2460 | print "Not a valid project name: %s, valid names are: %s" %(name, sections) |
---|
| 2461 | print "or set parentdir in the [DEFAULT] section" |
---|
| 2462 | sys.exit(1) |
---|
[22] | 2463 | |
---|
[507] | 2464 | ## If parentdir then set project dir to parentdir + name |
---|
| 2465 | # |
---|
| 2466 | if not project.has_key('project'): |
---|
| 2467 | if not parentdir: |
---|
| 2468 | print "You must set project or parentdir in your configuration file" |
---|
| 2469 | sys.exit(1) |
---|
| 2470 | elif not name: |
---|
| 2471 | print "You must configure a project section in your configuration file" |
---|
| 2472 | else: |
---|
| 2473 | project['project'] = os.path.join(parentdir, name) |
---|
[22] | 2474 | |
---|
[528] | 2475 | ## |
---|
| 2476 | # Save the project name |
---|
| 2477 | # |
---|
| 2478 | project['project_name'] = os.path.basename(project['project']) |
---|
| 2479 | |
---|
[507] | 2480 | return project |
---|
[22] | 2481 | |
---|
[410] | 2482 | ########## Setup Logging ############################################################### |
---|
[87] | 2483 | |
---|
[419] | 2484 | def setup_log(parameters, project_name, interactive=None): |
---|
[507] | 2485 | """ |
---|
[516] | 2486 | Setup logging |
---|
[410] | 2487 | |
---|
[507] | 2488 | Note for log format the usage of `$(...)s` instead of `%(...)s` as the latter form |
---|
[420] | 2489 | would be interpreted by the ConfigParser itself. |
---|
[507] | 2490 | """ |
---|
| 2491 | logger = logging.getLogger('email2trac %s' %project_name) |
---|
[410] | 2492 | |
---|
[507] | 2493 | if interactive: |
---|
| 2494 | parameters.log_type = 'stderr' |
---|
[414] | 2495 | |
---|
[507] | 2496 | if not parameters.log_type: |
---|
| 2497 | if sys.platform in ['win32', 'cygwin']: |
---|
[521] | 2498 | parameters.log_type = 'eventlog' |
---|
[507] | 2499 | else: |
---|
| 2500 | parameters.log_type = 'syslog' |
---|
[410] | 2501 | |
---|
[507] | 2502 | if parameters.log_type == 'file': |
---|
[415] | 2503 | |
---|
[507] | 2504 | if not parameters.log_file: |
---|
| 2505 | parameters.log_file = 'email2trac.log' |
---|
[415] | 2506 | |
---|
[507] | 2507 | if not os.path.isabs(parameters.log_file): |
---|
| 2508 | parameters.log_file = os.path.join(tempfile.gettempdir(), parameters.log_file) |
---|
[410] | 2509 | |
---|
[507] | 2510 | log_handler = logging.FileHandler(parameters.log_file) |
---|
[415] | 2511 | |
---|
[507] | 2512 | elif parameters.log_type in ('winlog', 'eventlog', 'nteventlog'): |
---|
| 2513 | ## Requires win32 extensions |
---|
| 2514 | # |
---|
[532] | 2515 | logid = "email2trac" |
---|
[507] | 2516 | log_handler = logging.handlers.NTEventLogHandler(logid, logtype='Application') |
---|
[410] | 2517 | |
---|
[507] | 2518 | elif parameters.log_type in ('syslog', 'unix'): |
---|
| 2519 | log_handler = logging.handlers.SysLogHandler('/dev/log') |
---|
[410] | 2520 | |
---|
[507] | 2521 | elif parameters.log_type in ('stderr'): |
---|
| 2522 | log_handler = logging.StreamHandler(sys.stderr) |
---|
[410] | 2523 | |
---|
[507] | 2524 | else: |
---|
| 2525 | log_handler = logging.handlers.BufferingHandler(0) |
---|
[410] | 2526 | |
---|
[507] | 2527 | if parameters.log_format: |
---|
| 2528 | parameters.log_format = parameters.log_format.replace('$(', '%(') |
---|
| 2529 | else: |
---|
| 2530 | parameters.log_format = '%(name)s: %(message)s' |
---|
[516] | 2531 | if parameters.log_type in ('file', 'stderr'): |
---|
| 2532 | parameters.log_format = '%(asctime)s ' + parameters.log_format |
---|
[410] | 2533 | |
---|
[507] | 2534 | log_formatter = logging.Formatter(parameters.log_format) |
---|
| 2535 | log_handler.setFormatter(log_formatter) |
---|
| 2536 | logger.addHandler(log_handler) |
---|
[410] | 2537 | |
---|
[507] | 2538 | if (parameters.log_level in ['DEBUG', 'ALL']) or (parameters.debug > 0): |
---|
| 2539 | logger.setLevel(logging.DEBUG) |
---|
| 2540 | parameters.debug = 1 |
---|
[410] | 2541 | |
---|
[507] | 2542 | elif parameters.log_level in ['INFO'] or parameters.verbose: |
---|
| 2543 | logger.setLevel(logging.INFO) |
---|
[410] | 2544 | |
---|
[507] | 2545 | elif parameters.log_level in ['WARNING']: |
---|
| 2546 | logger.setLevel(logging.WARNING) |
---|
[410] | 2547 | |
---|
[507] | 2548 | elif parameters.log_level in ['ERROR']: |
---|
| 2549 | logger.setLevel(logging.ERROR) |
---|
[410] | 2550 | |
---|
[507] | 2551 | elif parameters.log_level in ['CRITICAL']: |
---|
| 2552 | logger.setLevel(logging.CRITICAL) |
---|
[410] | 2553 | |
---|
[507] | 2554 | else: |
---|
| 2555 | logger.setLevel(logging.INFO) |
---|
[410] | 2556 | |
---|
[507] | 2557 | return logger |
---|
[410] | 2558 | |
---|
| 2559 | |
---|
[22] | 2560 | if __name__ == '__main__': |
---|
[507] | 2561 | ## Default config file |
---|
| 2562 | # |
---|
| 2563 | configfile = '@email2trac_conf@' |
---|
| 2564 | project = '' |
---|
| 2565 | component = '' |
---|
| 2566 | ticket_prefix = 'default' |
---|
| 2567 | dry_run = None |
---|
| 2568 | verbose = None |
---|
| 2569 | debug_interactive = None |
---|
[202] | 2570 | |
---|
[507] | 2571 | SHORT_OPT = 'cdhf:np:t:v' |
---|
| 2572 | LONG_OPT = ['component=', 'debug', 'dry-run', 'help', 'file=', 'project=', 'ticket_prefix=', 'verbose'] |
---|
[201] | 2573 | |
---|
[507] | 2574 | try: |
---|
| 2575 | opts, args = getopt.getopt(sys.argv[1:], SHORT_OPT, LONG_OPT) |
---|
| 2576 | except getopt.error,detail: |
---|
| 2577 | print __doc__ |
---|
| 2578 | print detail |
---|
| 2579 | sys.exit(1) |
---|
| 2580 | |
---|
| 2581 | project_name = None |
---|
| 2582 | for opt,value in opts: |
---|
| 2583 | if opt in [ '-h', '--help']: |
---|
| 2584 | print __doc__ |
---|
| 2585 | sys.exit(0) |
---|
| 2586 | elif opt in ['-c', '--component']: |
---|
| 2587 | component = value |
---|
| 2588 | elif opt in ['-d', '--debug']: |
---|
| 2589 | debug_interactive = 1 |
---|
| 2590 | elif opt in ['-f', '--file']: |
---|
| 2591 | configfile = value |
---|
| 2592 | elif opt in ['-n', '--dry-run']: |
---|
| 2593 | dry_run = True |
---|
| 2594 | elif opt in ['-p', '--project']: |
---|
| 2595 | project_name = value |
---|
| 2596 | elif opt in ['-t', '--ticket_prefix']: |
---|
| 2597 | ticket_prefix = value |
---|
| 2598 | elif opt in ['-v', '--verbose']: |
---|
| 2599 | verbose = True |
---|
| 2600 | |
---|
| 2601 | settings = ReadConfig(configfile, project_name) |
---|
[410] | 2602 | |
---|
[507] | 2603 | ## The default prefix for ticket values in email2trac.conf |
---|
| 2604 | # |
---|
| 2605 | settings.ticket_prefix = ticket_prefix |
---|
| 2606 | settings.dry_run = dry_run |
---|
| 2607 | settings.verbose = verbose |
---|
[410] | 2608 | |
---|
[507] | 2609 | if not settings.debug and debug_interactive: |
---|
| 2610 | settings.debug = debug_interactive |
---|
[412] | 2611 | |
---|
[507] | 2612 | if not settings.project: |
---|
| 2613 | print __doc__ |
---|
| 2614 | print 'No Trac project is defined in the email2trac config file.' |
---|
| 2615 | sys.exit(1) |
---|
[419] | 2616 | |
---|
[507] | 2617 | logger = setup_log(settings, os.path.basename(settings.project), debug_interactive) |
---|
| 2618 | |
---|
| 2619 | if component: |
---|
| 2620 | settings['component'] = component |
---|
[202] | 2621 | |
---|
[507] | 2622 | ## Determine major trac version used to be in email2trac.conf |
---|
| 2623 | # Quick hack for 0.12 |
---|
| 2624 | # |
---|
| 2625 | version = '0.%s' %(trac_version.split('.')[1]) |
---|
| 2626 | if version.startswith('0.12'): |
---|
| 2627 | version = '0.12' |
---|
[541] | 2628 | elif version.startswith('0.13'): |
---|
| 2629 | version = '0.13' |
---|
[373] | 2630 | |
---|
[507] | 2631 | logger.debug("Found trac version: %s" %(version)) |
---|
| 2632 | |
---|
| 2633 | try: |
---|
| 2634 | if version == '0.10': |
---|
| 2635 | from trac import attachment |
---|
| 2636 | from trac.env import Environment |
---|
| 2637 | from trac.ticket import Ticket |
---|
| 2638 | from trac.web.href import Href |
---|
| 2639 | from trac import util |
---|
| 2640 | from trac.ticket.web_ui import TicketModule |
---|
[486] | 2641 | |
---|
[507] | 2642 | # |
---|
| 2643 | # return util.text.to_unicode(str) |
---|
| 2644 | # |
---|
| 2645 | # see http://projects.edgewall.com/trac/changeset/2799 |
---|
| 2646 | from trac.ticket.notification import TicketNotifyEmail |
---|
| 2647 | from trac import config as trac_config |
---|
| 2648 | from trac.core import TracError |
---|
[359] | 2649 | |
---|
[554] | 2650 | elif version in ['0.11', '0.12', '0.13']: |
---|
[507] | 2651 | from trac import attachment |
---|
| 2652 | from trac import config as trac_config |
---|
| 2653 | from trac import util |
---|
| 2654 | from trac.core import TracError |
---|
[554] | 2655 | from trac.env import Environment |
---|
[507] | 2656 | from trac.perm import PermissionSystem |
---|
[539] | 2657 | from trac.perm import PermissionCache |
---|
[538] | 2658 | from trac.test import Mock, MockPerm |
---|
[507] | 2659 | from trac.ticket import Ticket |
---|
[554] | 2660 | from trac.ticket.api import TicketSystem |
---|
[507] | 2661 | from trac.ticket.web_ui import TicketModule |
---|
[541] | 2662 | from trac.web.href import Href |
---|
[368] | 2663 | |
---|
[541] | 2664 | # |
---|
| 2665 | # return util.text.to_unicode(str) |
---|
| 2666 | # |
---|
| 2667 | # see http://projects.edgewall.com/trac/changeset/2799 |
---|
| 2668 | from trac.ticket.notification import TicketNotifyEmail |
---|
| 2669 | |
---|
[507] | 2670 | else: |
---|
| 2671 | logger.error('TRAC version %s is not supported' %version) |
---|
[519] | 2672 | sys.exit(0) |
---|
[182] | 2673 | |
---|
[507] | 2674 | ## Must be set before environment is created |
---|
| 2675 | # |
---|
| 2676 | if settings.has_key('python_egg_cache'): |
---|
| 2677 | python_egg_cache = str(settings['python_egg_cache']) |
---|
| 2678 | os.environ['PYTHON_EGG_CACHE'] = python_egg_cache |
---|
[291] | 2679 | |
---|
[507] | 2680 | if settings.debug > 0: |
---|
| 2681 | logger.debug('Loading environment %s', settings.project) |
---|
[359] | 2682 | |
---|
[507] | 2683 | try: |
---|
| 2684 | env = Environment(settings['project'], create=0) |
---|
| 2685 | except IOError, detail: |
---|
[534] | 2686 | logger.error("trac error: %s" %detail) |
---|
[519] | 2687 | sys.exit(0) |
---|
[333] | 2688 | |
---|
[507] | 2689 | tktparser = TicketEmailParser(env, settings, logger, float(version)) |
---|
| 2690 | tktparser.parse(sys.stdin) |
---|
[22] | 2691 | |
---|
[507] | 2692 | ## Catch all errors and use the logging module |
---|
| 2693 | # |
---|
| 2694 | except Exception, error: |
---|
[187] | 2695 | |
---|
[507] | 2696 | etype, evalue, etb = sys.exc_info() |
---|
| 2697 | for e in traceback.format_exception(etype, evalue, etb): |
---|
| 2698 | logger.critical(e) |
---|
[187] | 2699 | |
---|
[507] | 2700 | if m: |
---|
[528] | 2701 | tktparser.save_email_for_debug(m, settings.project_name, True) |
---|
[97] | 2702 | |
---|
[507] | 2703 | sys.exit(1) |
---|
[22] | 2704 | # EOB |
---|