#!@PYTHON@ # # This file is part of the email2trac utils # # Copyright 2002 SURFsara # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # # For vi/emacs or other use tabstop=4 (vi: set ts=4) # """ email2trac.py -- Email -> TRAC tickets A MTA filter to create Trac tickets from inbound emails. first proof of concept from: Copyright 2005, Daniel Lundin Copyright 2005, Edgewall Software Authors: Bas van der Vlies Walter de Jong How to use ---------- * See https://oss.trac.surfsara.nl/email2trac/ * Commandline opions: -h,--help -A, --agilo -B, --bh_product -d, --debug -E, --virtualenv -f,--file -n,--dry-run -p, --project -t, --ticket_prefix -v, --verbose SVN Info: $Id: email2trac.py.in 672 2015-10-19 10:42:25Z bas $ """ import os import sys import string import getopt import time import email import email.Iterators import email.Header import re import urllib import unicodedata import mimetypes import traceback import logging import logging.handlers import UserDict import tempfile from datetime import timedelta, datetime from stat import * ## Some global variables # m = None class SaraDict(UserDict.UserDict): def __init__(self, dictin = None): UserDict.UserDict.__init__(self) self.name = None if dictin: if dictin.has_key('name'): self.name = dictin['name'] del dictin['name'] self.data = dictin def get_value(self, name): if self.has_key(name): return self[name] else: return None def __repr__(self): return repr(self.data) def __str__(self): return str(self.data) def __getattr__(self, name): """ override the class attribute get method. Return the value from the dictionary """ if self.data.has_key(name): return self.data[name] else: return None def __setattr__(self, name, value): """ override the class attribute set method only when the UserDict has set its class attribute """ if self.__dict__.has_key('data'): self.data[name] = value else: self.__dict__[name] = value def __iter__(self): return iter(self.data.keys()) class TicketEmailParser(object): env = None comment = '> ' def __init__(self, env, parameters, logger, version): self.env = env # Database connection # self.db = None # Save parameters # self.parameters = parameters self.logger = logger # Some useful mail constants # self.email_name = None self.email_addr = None self.email_from = None self.author = None self.id = None ## Will be set to True if the user has ben registered # self.allow_registered_user = False self.STRIP_CONTENT_TYPES = list() ## fields properties via body_text # self.properties = dict() self.VERSION = version self.get_config = self.env.config.get ## init function ## # self.setup_parameters() def setup_parameters(self): if self.parameters.umask: os.umask(self.parameters.umask) if not self.parameters.spam_level: self.parameters.spam_level = 0 if not self.parameters.spam_header: self.parameters.spam_header = 'X-Spam-Score' if not self.parameters.email_quote: self.parameters.email_quote = '^> .*' self.parameters.skip_line_regex = '^> .*' if not self.parameters.ticket_update_by_subject_lookback: self.parameters.ticket_update_by_subject_lookback = 30 if self.parameters.verbatim_format == None: self.parameters.verbatim_format = 1 if self.parameters.reflow == None: self.parameters.reflow = 1 if self.parameters.binhex: self.STRIP_CONTENT_TYPES.append('application/mac-binhex40') if self.parameters.applesingle: self.STRIP_CONTENT_TYPES.append('application/applefile') if self.parameters.appledouble: self.STRIP_CONTENT_TYPES.append('application/applefile') if self.parameters.strip_content_types: items = self.parameters.strip_content_types.split(',') for item in items: self.STRIP_CONTENT_TYPES.append(item.strip()) if self.parameters.tmpdir: self.parameters.tmpdir = os.path.normcase(str(self.parameters['tmpdir'])) else: self.parameters.tmpdir = os.path.normcase('/tmp') if self.parameters.email_triggers_workflow == None: self.parameters.email_triggers_workflow = 1 if not self.parameters.subject_field_separator: self.parameters.subject_field_separator = '&' else: self.parameters.subject_field_separator = self.parameters.subject_field_separator.strip() if not self.parameters.strip_signature_regex: self.parameters.strip_signature_regex = '^-- $' self.parameters.cut_line_regex = '^-- $' self.trac_smtp_from = self.get_config('notification', 'smtp_from') self.smtp_default_domain = self.get_config('notification', 'smtp_default_domain') self.smtp_replyto = self.get_config('notification', 'smtp_replyto') self.trac_smtp_always_cc = self.get_config('notification', 'smtp_always_cc') self.trac_smtp_always_bcc = self.get_config('notification', 'smtp_always_bcc') self.system = None ########## Email Header Functions ########################################################### def spam(self, message): """ # X-Spam-Score: *** (3.255) BAYES_50,DNS_FROM_AHBL_RHSBL,HTML_ # Note if Spam_level then '*' are included """ self.logger.debug('function spam') spam = False if message.has_key(self.parameters.spam_header): spam_l = string.split(message[self.parameters.spam_header]) #self.logger.info('Spam header: %s' %(message[self.parameters.spam_header])) try: number = spam_l[0].count('*') except IndexError, detail: number = 0 if number >= self.parameters.spam_level: spam = True ## treat virus mails as spam # elif message.has_key('X-Virus-found'): spam = True ## Astaro firewall spam handling # elif message.get('X-Spam-Flag') == "YES" and message.get('X-Spam-Result') == "Spam": spam = True ## How to handle SPAM messages # if self.parameters.drop_spam and spam: self.logger.info('Message is a SPAM. Automatic ticket insertion refused (SPAM level > %d)' %self.parameters.spam_level) return 'drop' elif spam: return 'Spam' else: return False def email_header_acl(self, keyword, message_field, default): """ This function wil check if the email address is allowed or denied to send mail to the ticket list """ self.logger.debug('function email_header_acl: %s' %keyword) try: mail_addresses = self.parameters[keyword] # Check if we have an empty string # if not mail_addresses: return default except KeyError, detail: self.logger.debug('\t %s not defined, all messages are allowed.' %(keyword)) return default mail_addresses = string.split(mail_addresses, ',') message_addresses = string.split(message_field, ',') for entry in mail_addresses: entry = entry.strip() TO_RE = re.compile(entry, re.VERBOSE|re.IGNORECASE) for addr in message_addresses: addr = addr.strip() if self.parameters.compare_function_list in [ 'matches', 'match']: s = '\t%s matches %s' %(addr, entry) result = TO_RE.match(addr) else: s = '\t%s contains %s' %(addr, entry) result = TO_RE.search(addr) if result: self.logger.debug(s) return True return False def email_header_txt(self, m): """ Display To and CC addresses in description field """ s = '' if m['To'] and len(m['To']) > 0: s = "'''To:''' %s\r\n" %(m['To']) if m['Cc'] and len(m['Cc']) > 0: s = "%s'''Cc:''' %s\r\n" % (s, m['Cc']) return self.email_to_unicode(s) def get_sender_info(self, message): """ Get the default author name and email address from the message """ self.logger.debug('function get_sender_info') to_addrs = email.Utils.getaddresses( message.get_all('to', []) ) self.email_to_addrs = list() for n,e in to_addrs: self.email_to_addrs.append(e) self.email_to_addrs = ','.join(self.email_to_addrs) self.email_name, self.email_addr = email.Utils.parseaddr(message['from']) ## decode email name can contain charset # self.email_name = self.email_to_unicode(self.email_name) dstr = '\t email name: %s, email address: %s' %(self.email_name, self.email_addr) self.logger.debug(dstr) ## Trac can not handle author's name that contains spaces # if self.email_addr.lower() == self.trac_smtp_from.lower(): if self.email_name: self.author = self.email_name else: self.author = "email2trac" else: self.author = self.email_addr if self.parameters.ignore_trac_user_settings: return # Is this a registered user, use email address as search key: # result: # u : login name # n : Name that the user has set in the settings tab # e : email address that the user has set in the settings tab # #users = [ (u,n,e) for (u, n, e) in self.env.get_known_users(self.db) users = [ (u,n,e) for (u, n, e) in self.env.get_known_users() if ( (e and (e.lower() == self.email_addr.lower())) or (u and (u.lower() + '@' + self.smtp_default_domain.lower() == self.email_addr.lower())) ) ] if len(users) >= 1: self.email_from = users[0][2] self.author = users[0][0] self.logger.debug('\tget_sender_info: found registered user: %s' %(self.author) ) if self.parameters.white_list_registered_users: self.logger.debug('\tget_sender_info: added registered user to white list') self.allow_registered_user = True def set_cc_fields(self, ticket, message, update=False): """ Set all the right fields for a new ticket """ self.logger.debug('function set_cc_fields') # Put all CC-addresses in ticket CC field # if self.parameters.reply_all: ticket_cc = '' msg_cc_addrs = email.Utils.getaddresses( message.get_all('cc', []) ) if not msg_cc_addrs: return if update: self.logger.debug("\tupdate ticket cc-field") ticket_cc = ticket['cc'] ticket_cc_list = ticket_cc.split(',') for name,addr in msg_cc_addrs: ## Prevent mail loop # if addr == self.trac_smtp_from: self.logger.debug("\tSkipping %s email address for CC-field, same as smtp_from address in trac.ini " %(addr)) continue ## Alwyas remove reporter email address # elif addr == self.email_addr: self.logger.debug("\tSkipping reporter email address for CC-field") continue ## Always remove the always_cc address # elif addr == self.trac_smtp_always_cc: self.logger.debug("\tSkipping smtp_always_cc email address for CC-field") continue ## Always remove the always_bcc address # elif addr == self.trac_smtp_always_bcc: self.logger.debug("\tSkipping smtp_always_bcc email address for CC-field") continue ## Skip also addr in cc_black_list (email2trac.conf) # if self.email_header_acl('cc_black_list', addr, False): self.logger.debug("\tSkipping address for CC-field due to cc_black_list") continue else: ## On update, prevent adding duplicates # if update: if addr in ticket_cc_list: continue if ticket_cc: ticket_cc = '%s, %s' %(ticket_cc, addr) else: ticket_cc = addr if ticket_cc: self.logger.debug('\tCC fields: %s' %ticket_cc) ticket['cc'] = self.email_to_unicode(ticket_cc) def acl_list_from_file(self, f, keyword): """ Read the email address from a file """ self.logger.debug('function acl_list_from_file %s : %s' %(f, keyword)) if not os.path.isfile(f): self.logger.error('%s_file: %s does not exists' %(keyword, f) ) else: ## read whole file and replace '\n' with '' # addr_l = open(f, 'r').readlines() s = ','.join(addr_l).replace('\n','') try: self.parameters[keyword] = "%s,%s" %(self.parameters[keyword], s) except KeyError, detail: self.parameters[keyword] = s ########## DEBUG functions ########################################################### def debug_body(self, message_body, temp_file): """ """ self.logger.debug('function debug_body:') body_file = "%s.body" %(temp_file) fx = open(body_file, 'wb') if self.parameters.dry_run: self.logger.info('DRY-RUN: not saving body to %s' %(body_file)) return self.logger.debug('writing body to %s' %(body_file)) if not message_body: message_body = '(None)' message_body = message_body.encode('utf-8') fx.write(message_body) fx.close() try: os.chmod(body_file,S_IRWXU|S_IRWXG|S_IRWXO) except OSError: pass def debug_attachments(self, message_parts, temp_file): """ """ self.logger.debug('function debug_attachments') n = 0 for item in message_parts: ## Skip inline text parts # if not isinstance(item, tuple): continue (original, filename, part) = item self.logger.debug('\t part%d: Content-Type: %s' % (n, part.get_content_type()) ) dummy_filename, ext = os.path.splitext(filename) n = n + 1 part_name = 'part%d%s' %(n, ext) part_file = "%s.%s" %(temp_file, part_name) s = 'writing %s: filename: %s' %(part_file, filename) self.print_unicode(s) ## Forbidden chars, just write part files instead of names # #filename = filename.replace('\\', '_') #filename = filename.replace('/', '_') #filename = filename + '.att_email2trac' # part_file = os.path.join(self.parameters.tmpdir, filename) #part_file = util.text.unicode_quote(part_file) #self.print_unicode(part_file) if self.parameters.dry_run: self.logger.info('DRY_RUN: NOT saving attachments') continue fx = open(part_file, 'wb') text = part.get_payload(decode=1) if not text: text = '(None)' fx.write(text) fx.close() try: os.chmod(part_file,S_IRWXU|S_IRWXG|S_IRWXO) except OSError: pass def save_email_for_debug(self, message, project_name, save_only_raw_message=False): if self.parameters.dry_run: self.logger.debug('DRY_RUN: NOT saving email message') return (fd, tmp_file) = tempfile.mkstemp('.email2trac', project_name, self.parameters.tmpdir) fx = os.fdopen(fd, 'wb') self.logger.debug('saving email to %s' %(tmp_file)) fx.write('%s' % message) fx.close() try: os.chmod(tmp_file, S_IRWXU|S_IRWXG|S_IRWXO) except OSError: pass if not save_only_raw_message: message_parts = self.get_message_parts(message) message_parts = self.unique_attachment_names(message_parts) body_text = self.get_body_text(message_parts) self.debug_body(body_text, tmp_file) self.debug_attachments(message_parts, tmp_file) ########## Conversion functions ########################################################### def email_to_unicode(self, message_str): """ Email has 7 bit ASCII code, convert it to unicode with the charset that is encoded in 7-bit ASCII code and encode it as utf-8 so Trac understands it. """ self.logger.debug("function email_to_unicode") self.logger.debug("\t repr:%s type:%s" %(repr(message_str), type(message_str))) ## Skip unicode strings, there are already converted # if type(message_str) is unicode: return message_str results = email.Header.decode_header(message_str) s = None for text,format in results: if format: try: temp = unicode(text, format) except UnicodeError, detail: # This always works # msg = 'ERROR: Could not find charset: %s, please install' %format self.logger.error(msg) temp = unicode(text, 'iso-8859-15') except LookupError, detail: msg = 'ERROR: Could not find charset: %s, please install' %format self.logger.error(msg) #temp = unicode(text, 'iso-8859-15') temp = message_str else: temp = string.strip(text) temp = unicode(text, 'iso-8859-15') if s: s = '%s %s' %(s, temp) else: s = '%s' %temp return s def str_to_dict(self, s): """ Transfrom a string of the form [=]+ to dict[] = """ self.logger.debug("function str_to_dict") fields = string.split(s, self.parameters.subject_field_separator) result = dict() for field in fields: try: index, value = string.split(field, '=') # We can not change the description of a ticket via the subject # line. The description is the body of the email # if index.lower() in ['description']: continue if value: result[index.lower()] = value except ValueError: pass return result def print_unicode(self,s): """ This function prints unicode strings if possible else it will quote it """ try: self.logger.debug(s) except UnicodeEncodeError, detail: self.logger.debug(util.text.unicode_quote(s)) def html_2_txt(self, data): """ Various routines to convert html syntax to valid trac wiki syntax """ self.logger.debug('function html_2_txt') ## This routine make an safe html that can be include # in trac, but no further text processing can be done # # try: # from lxml.html.clean import Cleaner # tags_rm = list() # tags_rm.append('body') # # cleaner = Cleaner(remove_tags=tags_rm ) # parsed_data = cleaner.clean_html(data) # parsed_data = '\n{{{\n#!html\n' + parsed_data + '\n}}}\n' # # return parsed_data # # except ImportError:: # pass parsed_data = None if self.parameters.html2text_cmd: (fd, tmp_file) = tempfile.mkstemp('email2trac.html') f = os.fdopen(fd, 'w') cmd = '%s %s' %(self.parameters.html2text_cmd, tmp_file) self.logger.debug('\t html2text conversion %s'%(cmd)) if self.parameters.dry_run: self.logger.info('DRY_RUN: html2text conversion command: %s\n' %(cmd)) else: f.write(data) f.close() lines = os.popen(cmd).readlines() parsed_data = ''.join(lines) os.unlink(tmp_file) else: self.logger.debug('\t No html2text conversion tool specified in email2trac.conf') return parsed_data def check_filename_length(self, name): """ To bypass a bug in Trac check if the filename length is not larger then OS limit. yes : return truncated filename no : return unmodified filename """ self.logger.debug('function check_filename_length: ') if not name: return 'None' dummy_filename, ext = os.path.splitext(name) ## Trac uses this format # try: quote_format = util.text.unicode_quote(dummy_filename) except UnicodeDecodeError, detail: ## last resort convert to unicode # dummy_filename = util.text.to_unicode(dummy_filename) quote_format = util.text.unicode_quote(dummy_filename) ## Determine max filename length # try: filemax_length = os.pathconf('/', 'PC_NAME_MAX') except AttributeError, detail: filemax_length = 240 if len(quote_format) <= filemax_length: return name else: ## Truncate file to filemax_length and reserve room for extension # We must break on a boundry # length = filemax_length - 6 for i in range(0,10): truncated = quote_format[ : (length - i)] try: unqoute_truncated = util.text.unicode_unquote(truncated) unqoute_truncated = unqoute_truncated + ext self.print_unicode('\t ' + unqoute_truncated) break except UnicodeDecodeError, detail: continue return unqoute_truncated ########## TRAC ticket functions ########################################################### def mail_workflow(self, tkt): """ """ self.logger.debug('function mail_workflow: ') req = Mock(authname=self.author, perm=MockPerm(), args={}) ticket_system = TicketSystem(self.env) try: workflow = self.parameters['workflow_%s' %tkt['status'].lower()] except KeyError: ## fallback for compability (Will be deprecated) # workflow can be none. # workflow = None if tkt['status'] in ['closed']: workflow = self.parameters.workflow if workflow: ## process all workflow implementations # tkt_module = TicketModule(self.env) field_changes, problems = tkt_module.get_ticket_changes(req, tkt, workflow) for field in field_changes.keys(): ## We have already processed these fields # if not field in ['summary', 'description']: s = 'workflow : %s, field %s : %s, by %s' \ %(workflow, field, field_changes[field]['new'],field_changes[field]['by'] ) self.logger.debug(s) tkt[field] = field_changes[field]['new'] return True else: return False def check_permission_participants(self, tkt, action): """ Check if the mailer is allowed to update the ticket """ self.logger.debug('function check_permission_participants %s') if tkt['reporter'].lower() in [self.author.lower(), self.email_addr.lower()]: self.logger.debug('ALLOW, %s is the ticket reporter' %(self.email_addr)) return True perm = PermissionSystem(self.env) if perm.check_permission(action, self.author): self.logger.debug('ALLOW, %s has trac permission to update the ticket' %(self.author)) return True # Is the updater in the CC? try: cc_list = tkt['cc'].split(',') for cc in cc_list: if self.email_addr.lower() in cc.lower().strip(): self.logger.debug('ALLOW, %s is in the CC' %(self.email_addr)) return True except KeyError: pass return False def check_permission(self, tkt, action): """ check if the reporter has the right permission for the action: - TICKET_CREATE - TICKET_MODIFY - TICKET_APPEND - TICKET_CHGPROP There are three models: - None : no checking at all - trac : check the permission via trac permission model - email2trac: .... """ self.logger.debug("function check_permission: %s" %(action)) if self.parameters.ticket_permission_system in ['trac']: perm = PermissionCache(self.env, self.author) if perm.has_permission(action): return True else: return False elif self.parameters.ticket_permission_system in ['update_restricted_to_participants']: return (self.check_permission_participants(tkt, action)) ## Default is to allow everybody ticket updates and ticket creation # else: return True def update_ticket_fields(self, ticket, user_dict, new=None): """ This will update the ticket fields. It will check if the given fields are known and if the right values are specified It will only update the ticket field value: - If the field is known - If the value supplied is valid for the ticket field. If not then there are two options: 1) Skip the value (new=None) 2) Set default value for field (new=1) """ self.logger.debug("function update_ticket_fields") if self.parameters.bh_product: if 'product' in user_dict: if user_dict['product'] != ticket.env.product.prefix: self.logging.warning("bloodhound products cannot be changed " "- ignoring") user_dict.pop('product') ## Check only permission model on ticket updates # if not new: if self.parameters.ticket_permission_system: if not self.check_permission(ticket, 'TICKET_CHGPROP'): self.logger.info('Reporter: %s has no permission to change ticket properties' %self.author) return False ## Build a system dictionary from the ticket fields # with field as index and option as value # sys_dict = dict() for field in ticket.fields: try: sys_dict[field['name']] = field['options'] except KeyError: sys_dict[field['name']] = None pass ## Check user supplied fields an compare them with the # system one's # for field,value in user_dict.items(): s = 'user_field\t %s = %s' %(field,value) self.print_unicode(s) if not field in sys_dict.keys(): self.logger.debug('%s is not a valid field for tickets' %(field)) continue ## To prevent mail loop # if field == 'cc': cc_list = user_dict['cc'].split(',') if self.trac_smtp_from in cc_list: self.logger.debug('MAIL LOOP: %s is not allowed as CC address' %(self.trac_smtp_from)) cc_list.remove(self.trac_smtp_from) value = ','.join(cc_list) ## Check if every value is allowed for this field # if sys_dict[field]: if value in sys_dict[field]: ticket[field] = value else: ## Must we set a default if value is not allowed # if new: value = self.get_config('ticket', 'default_%s' %(field) ) else: ticket[field] = value s = 'ticket_field\t %s = %s' %(field, ticket[field]) self.print_unicode(s) def ticket_update(self, m, id, spam): """ If the current email is a reply to an existing ticket, this function will append the contents of this email to that ticket, instead of creating a new one. """ self.logger.debug("function ticket_update") if not self.parameters.ticket_update: self.logger.debug("ticket_update disabled") return False ## Must we update ticket fields # update_fields = dict() try: id, keywords = string.split(id, '?') update_fields = self.str_to_dict(keywords) ## Strip '#' # self.id = int(id[1:]) except ValueError: ## Strip '#' # self.id = int(id[1:]) self.logger.debug("\tticket id: %s" %id) ## When is the change committed # when = datetime.now(util.datefmt.utc) try: tkt = Ticket(self.env, self.id, self.db) except util.TracError, detail: ## Not a valid ticket # self.logger.info("\tCreating a new ticket, ticket id: %s does not exists" %id) self.id = None return False ## Check the permission of the reporter # if self.parameters.ticket_permission_system: if not self.check_permission(tkt, 'TICKET_APPEND'): self.logger.info('Reporter: %s has no permission to add comments or attachments to tickets' %self.author) return False ## How many changes has this ticket # #grouped = TicketModule(self.env).grouped_changelog_entries(tkt, self.db) grouped = TicketModule(self.env).grouped_changelog_entries(tkt) cnum = sum(1 for e in grouped) + 1 ## reopen the ticket if it is was closed # We must use the ticket workflow framework # if self.parameters.email_triggers_workflow: if not self.mail_workflow(tkt): if tkt['status'] in ['closed']: tkt['status'] = 'reopened' tkt['resolution'] = '' else: self.logger.debug('\temail triggers workflow disabled') ## Must we update some ticket fields properties via subject line # if update_fields: self.update_ticket_fields(tkt, update_fields) message_parts = self.get_message_parts(m) message_parts = self.unique_attachment_names(message_parts) ## Must we update some ticket fields properties via inline comments # in body_text # if self.properties: self.update_ticket_fields(tkt, self.properties) ## Must we update the CC ticket field # self.set_cc_fields(tkt, m, update=True) if self.parameters.email_header: message_parts.insert(0, self.email_header_txt(m)) body_text = self.get_body_text(message_parts) error_with_attachments = self.attach_attachments(message_parts) if body_text.strip() or update_fields or self.properties: if self.parameters.dry_run: s = 'DRY_RUN: tkt.save_changes(self.author, body_text, ticket_change_number) %s %s' %(self.author, cnum) self.logger.info(s) else: if error_with_attachments: body_text = '%s\\%s' %(error_with_attachments, body_text) self.logger.debug('\ttkt.save_changes(%s, %d)' %(self.author, cnum)) tkt.save_changes(self.author, body_text, when, None, str(cnum)) if not spam: self.notify(tkt, False, when) return True def set_ticket_fields(self, ticket): """ set the ticket fields to value specified - /etc/email2trac.conf with _ - trac default values, trac.ini """ self.logger.debug('function set_ticket_fields') user_dict = dict() for field in ticket.fields: name = field['name'] ## default trac value # CUSTOM_FIELD = False if not field.get('custom'): value = self.get_config('ticket', 'default_%s' %(name) ) ## skip this field can only be set by email2trac.conf # if name in ['resolution']: value = None else: ## Else get the default value for custom fields # CUSTOM_FIELD = True value = field.get('value') options = field.get('options') if value and options and (value not in options): value = options[int(value)] s = 'trac[%s] = %s' %(name, value) self.print_unicode(s) ## email2trac.conf settings # prefix = self.parameters.ticket_prefix try: value = self.parameters['%s_%s' %(prefix, name)] s = 'email2trac[%s] = %s ' %(name, value) self.print_unicode(s) except KeyError, detail: pass if value: user_dict[name] = value s = 'used %s = %s' %(name, value) self.print_unicode(s) else: ## custom fields need some initialisation # if CUSTOM_FIELD: user_dict[name] = '' self.update_ticket_fields(ticket, user_dict, new=1) if 'status' not in user_dict.keys(): ticket['status'] = 'new' def ticket_update_by_subject(self, subject): """ This list of Re: prefixes is probably incomplete. Taken from wikipedia. Here is how the subject is matched - Re: - Re: (:)+ So we must have the last column """ self.logger.debug('function ticket_update_by_subject') found_id = None if self.parameters.ticket_update and self.parameters.ticket_update_by_subject: SUBJECT_RE = re.compile(r'^(?:(?:RE|AW|VS|SV|FW|FWD):\s*)+(.*)', re.IGNORECASE) result = SUBJECT_RE.search(subject) if result: ## This is a reply # orig_subject = result.group(1) self.logger.debug('subject search string: %s' %(orig_subject)) cursor = self.db.cursor() summaries = [orig_subject, '%%: %s' % orig_subject] ## Time resolution is in micoseconds # search_date = datetime.now(util.datefmt.utc) - timedelta(days=self.parameters.ticket_update_by_subject_lookback) if self.VERSION < 0.12: lookback = util.datefmt.to_timestamp(search_date) else: lookback = util.datefmt.to_utimestamp(search_date) for summary in summaries: self.logger.debug('Looking for summary matching: "%s"' % summary) sql = """SELECT id, reporter FROM ticket WHERE changetime >= %s AND summary LIKE %s ORDER BY changetime DESC""" cursor.execute(sql, [lookback, summary.strip()]) for row in cursor: (matched_id, sql_reporter) = row ## Save first entry. # if not found_id: found_id = matched_id ## If subject and reporter are the same. The we certainly have found the right ticket # if sql_reporter == self.author: self.logger.debug('Found matching reporter: %s with ticket id: %d' %(sql_reporter, matched_id)) found_id = matched_id break if found_id: self.logger.debug('Found matching ticket id: %d' % found_id) found_id = '#%d' % found_id return (found_id, orig_subject) return (found_id, subject) def new_ticket(self, msg, subject, spam, set_fields = None): """ Create a new ticket """ self.logger.debug('function new_ticket') tkt = Ticket(self.env) ## self.author can be email address of an username # tkt['reporter'] = self.author self.set_cc_fields(tkt, msg) self.set_ticket_fields(tkt) ## Check the permission of the reporter # if self.parameters.ticket_permission_system: if not self.check_permission(tkt, 'TICKET_CREATE'): self.logger.info('Reporter: %s has no permission to create tickets' %self.author) return False ## Old style setting for component, will be removed # if spam: tkt['component'] = 'Spam' elif self.parameters.has_key('component'): tkt['component'] = self.parameters['component'] if not msg['Subject']: tkt['summary'] = u'(No subject)' else: tkt['summary'] = subject if set_fields: rest, keywords = string.split(set_fields, '?') if keywords: update_fields = self.str_to_dict(keywords) self.update_ticket_fields(tkt, update_fields) message_parts = self.get_message_parts(msg, True) ## Must we update some ticket fields properties via body_text # if self.properties: self.update_ticket_fields(tkt, self.properties) message_parts = self.unique_attachment_names(message_parts) ## produce e-mail like header # head = '' if self.parameters.email_header: head = self.email_header_txt(msg) message_parts.insert(0, head) body_text = self.get_body_text(message_parts) tkt['description'] = body_text ## When is the change committed # when = datetime.now(util.datefmt.utc) if self.parameters.dry_run: self.logger.info('DRY_RUN: tkt.insert()') else: self.id = tkt.insert() changed = False comment = '' ## some routines in trac are dependend on ticket id # like alternate notify template # if self.parameters.alternate_notify_template: tkt['id'] = self.id changed = True ## Rewrite the description if we have mailto enabled # if self.parameters.mailto_link: changed = True comment = u'\nadded mailto line\n' #mailto = self.html_mailto_link( m['Subject']) mailto = self.html_mailto_link(subject) tkt['description'] = u'%s\r\n%s%s\r\n' \ %(head, mailto, body_text) ## Save the attachments to the ticket # error_with_attachments = self.attach_attachments(message_parts) if error_with_attachments: changed = True comment = '%s\n%s\n' %(comment, error_with_attachments) if self.parameters.email_triggers_workflow: if self.mail_workflow(tkt): changed = True if changed: if self.parameters.dry_run: s = 'DRY_RUN: tkt.save_changes(%s, comment) real reporter = %s' %( tkt['reporter'], self.author) self.logger.info(s) else: tkt.save_changes(tkt['reporter'], comment) if not spam: self.notify(tkt, True) def attach_attachments(self, message_parts, update=False): ''' save any attachments as files in the ticket's directory ''' self.logger.debug('function attach_attachments()') if self.parameters.dry_run: self.logger.debug("DRY_RUN: no attachments attached to tickets") return '' count = 0 ## Get Maxium attachment size # max_size = int(self.get_config('attachment', 'max_size')) status = None for item in message_parts: ## Skip body parts # if not isinstance(item, tuple): continue (original, filename, part) = item ## We have to determine the size so we use this temporary solution. # path, fd = util.create_unique_file(os.path.join(self.parameters.tmpdir, 'email2trac_tmp.att')) text = part.get_payload(decode=1) if not text: text = '(None)' fd.write(text) fd.close() ## get the file_size # stats = os.lstat(path) file_size = stats[ST_SIZE] ## Check if the attachment size is allowed # if (max_size != -1) and (file_size > max_size): status = '%s\nFile %s is larger then allowed attachment size (%d > %d)\n\n' \ %(status, original, file_size, max_size) os.unlink(path) continue else: count = count + 1 ## Insert the attachment # fd = open(path, 'rb') if self.system == 'discussion': att = attachment.Attachment(self.env, 'discussion', 'topic/%s' % (self.id,)) elif self.system == 'blog': att = attachment.Attachment(self.env, 'blog', '%s' % (self.id,)) else: s = 'Attach %s to ticket %d' %(filename, self.id) self.print_unicode(s) att = attachment.Attachment(self.env, 'ticket', self.id) ## This will break the ticket_update system, the body_text is vaporized # ;-( # if not update: att.author = self.author att.description = self.email_to_unicode('Added by email2trac') try: self.logger.debug('Insert atachment') att.insert(filename, fd, file_size) except OSError, detail: self.logger.info('%s\nFilename %s could not be saved, problem: %s' %(status, filename, detail)) status = '%s\nFilename %s could not be saved, problem: %s' %(status, filename, detail) ## Remove the created temporary filename # fd.close() os.unlink(path) ## return error # return status ########## Fullblog functions ################################################# def blog(self, msg, subject, id, params): """ The blog create/update function """ ## import the modules # from tracfullblog.core import FullBlogCore from tracfullblog.model import BlogPost, BlogComment ## instantiate blog core # blog = FullBlogCore(self.env) req = Mock(authname='anonymous', perm=MockPerm(), args={}) ## parameters from the subject # params = self.str_to_dict((params or '').lstrip('?')) ## preferably get the time from the email date header, or else # use current date and time date = email.Utils.parsedate_tz(msg.get('date')) if date: dt = util.datefmt.to_datetime(email.Utils.mktime_tz(date), util.datefmt.utc) else: self.logger.warn("No valid date header found") dt = util.datefmt.to_datetime(None, util.datefmt.utc) ## blog entry affected # self.id = id or util.datefmt.format_datetime(dt, "%Y%m%d%H%M%S", util.datefmt.utc) ## check wether a blog post exists # post = BlogPost(self.env, self.id) force_update = self.properties.get('update', params.get('update')) ## message parts # message_parts = self.get_message_parts(msg) message_parts = self.unique_attachment_names(message_parts) if post.get_versions() and not force_update: ## add comment to blog entry # comment = BlogComment(self.env, self.id) comment.author = self.properties.get('author', params.get('author', self.author)) comment.comment = self.get_body_text(message_parts) comment.time = dt if self.parameters.dry_run: self.logger.info('DRY-RUN: not adding comment for blog entry "%s"' % id) return warnings = blog.create_comment(req, comment) else: ## create or update blog entry # post.author = self.properties.get('author', params.get('author', self.author)) post.categories = self.properties.get('categories', params.get('categories', '')) post.title = subject.strip() post.publish_time = dt post.body = self.get_body_text(message_parts) if self.parameters.dry_run: self.logger.info('DRY-RUN: not creating blog entry "%s"' % post.title) return warnings = blog.create_post(req, post, self.author, u'Created by email2trac', False) ## check for problems # if warnings: raise TracError(', '.join('blog:%s:%s' % (w[0], w[1]) for w in warnings)) ## all seems well, attach attachments # self.attach_attachments(message_parts) ########## Discussion functions ############################################## def discussion_topic(self, content, subject): ## Import modules. # from tracdiscussion.api import DiscussionApi from trac.util.datefmt import to_timestamp, utc self.logger.debug('Creating a new topic in forum:', self.id) ## Get dissussion API component. # api = self.env[DiscussionApi] args = {'forum' : self.id} context = self._create_context(api, args, content, subject) ## Get forum for new topic. # forum = context.forum if not forum: self.logger.error("ERROR: Replied forum doesn't exist") ## Prepare topic. # topic = {'forum' : forum['id'], 'subject' : context.subject, 'time': to_timestamp(datetime.now(utc)), 'author' : self.author, 'subscribers' : [self.email_addr], 'body' : self.get_body_text(context.content_parts)} ## Add topic to DB and commit it. # self._add_topic(api, context, topic) def discussion_topic_reply(self, content, subject): ## Import modules. # from tracdiscussion.api import DiscussionApi from trac.util.datefmt import to_timestamp, utc self.logger.debug('Replying to discussion topic', self.id) ## Get dissussion API component. # api = self.env[DiscussionApi] args = {'topic' : self.id} context = self._create_context(api, args, content, subject) ## Get replied topic. # topic = context.topic if not topic: self.logger.error("ERROR: Replied topic doesn't exist") ## Prepare message. # message = {'forum' : topic['forum'], 'topic' : topic['id'], 'replyto' : -1, 'time' : to_timestamp(datetime.now(utc)), 'author' : self.author, 'body' : self.get_body_text(context.content_parts)} ## Add message to DB and commit it. # self._add_message(api, context, message) def discussion_message_reply(self, content, subject): ## Import modules. # from tracdiscussion.api import DiscussionApi from trac.util.datefmt import to_timestamp, utc self.logger.debug('Replying to discussion message', self.id) ## Get dissussion API component. # api = self.env[DiscussionApi] args = {'message' : self.id} context = self._create_context(api, args, content, subject) ## Get replied message. # message = context.message if not message: self.logger.error("ERROR: Replied message doesn't exist") ## Prepare message. # message = {'forum' : message['forum'], 'topic' : message['topic'], 'replyto' : message['id'], 'time' : to_timestamp(datetime.now(utc)), 'author' : self.author, 'body' : self.get_body_text(context.content_parts)} ## Add message to DB and commit it. # self._add_message(api, context, message) def _create_context(self, api, args, content, subject): ## Import modules. # from trac.mimeview import Context from trac.web.api import Request from trac.web.session import Session from trac.perm import PermissionCache ## TODO: Read server base URL from config. # Create request object to mockup context creation. # environ = {'SERVER_PORT' : 80, 'SERVER_NAME' : 'test', 'REQUEST_METHOD' : 'POST', 'wsgi.url_scheme' : 'http', 'wsgi.input' : sys.stdin} chrome = {'links': {}, 'scripts': [], 'ctxtnav': [], 'warnings': [], 'notices': []} if self.env.base_url_for_redirect: environ['trac.base_url'] = self.env.base_url req = Request(environ, None) req.chrome = chrome req.tz = 'missing' req.authname = self.author req.perm = PermissionCache(self.env, self.author) req.locale = None req.args = args req.session = Session(env, req) ## Create and return context. # context = Context.from_request(req) context.realm = 'discussion-email2trac' context.db = self.env.get_db_cnx() context.content = content context.subject = subject ## Read content parts from content. # context.content_parts = self.get_message_parts(content) context.content_parts = self.unique_attachment_names( context.content_parts) api._prepare_context(context) return context def _add_topic(self, api, context, topic): context.req.perm.assert_permission('DISCUSSION_APPEND') ## Filter topic. # for discussion_filter in api.discussion_filters: accept, topic_or_error = discussion_filter.filter_topic( context, topic) if accept: topic = topic_or_error else: raise TracError(topic_or_error) ## Add a new topic. # new_topic_id = api.add_topic(context, topic) ## Get inserted topic with new ID. # topic = api.get_topic(context, new_topic_id) ## Attach attachments. # self.id = topic['id'] self.attach_attachments(context.content_parts, True) ## Notify change listeners. # for listener in api.topic_change_listeners: listener.topic_created(context, topic) def _add_message(self, api, context, message): context.req.perm.assert_permission('DISCUSSION_APPEND') ## Filter message. # for discussion_filter in api.discussion_filters: accept, message_or_error = discussion_filter.filter_message( context, message) if accept: message = message_or_error else: raise TracError(message_or_error) ## Add message. # new_msg_id = api.add_message(context, message) ## Get inserted message with new ID. # message = api.get_message(context, new_msg_id) ## Attach attachments. # self.attach_attachments(context.content_parts, True) ## Notify change listeners. # for listener in api.message_change_listeners: listener.message_created(context, message) ########## MAIN function ###################################################### def parse(self, fp): """ """ self.logger.debug('Main function parse') global m m = email.message_from_file(fp) if not m: self.logger.debug('This is not a valid email message format') return ## Work around lack of header folding in Python; see http://bugs.python.org/issue4696 # try: m.replace_header('Subject', m['Subject'].replace('\r', '').replace('\n', '')) except AttributeError, detail: pass if self.parameters.debug: # save email + try to decode message part self.save_email_for_debug(m, self.parameters.project_name) elif self.parameters.save_raw_message: # save only the raw e-mail message text self.save_email_for_debug(m, self.parameters.project_name, True) self.db = self.env.get_read_db() self.get_sender_info(m) if self.parameters.white_list_file: self.acl_list_from_file(self.parameters.white_list_file, 'white_list') if not ( self.email_header_acl('white_list', self.email_addr, True) or self.allow_registered_user ) : self.logger.info('Message rejected : %s not in white list' %(self.email_addr)) return False if self.email_header_acl('black_list', self.email_addr, False): self.logger.info('Message rejected : %s in black list' %(self.email_addr)) return False if not self.email_header_acl('recipient_list', self.email_to_addrs, True): self.logger.info('Message rejected : %s not in recipient list' %(self.email_to_addrs)) return False ## If spam drop the message # if self.spam(m) == 'drop': return False elif self.spam(m) == 'spam': spam_msg = True else: spam_msg = False if not m['Subject']: subject = 'No Subject' else: subject = self.email_to_unicode(m['Subject']) ## Bug in python logging module <2.6 # self.logger.info('subject: %s' %repr(subject)) ## First try the new method and then fall back to old method # if not self.parse_delivered_to_field(m, subject, spam_msg): self.parse_subject_field(m, subject, spam_msg) def parse_delivered_to_field(self, m, subject, spam_msg): """ See if we have replied to an existing ticket """ self.logger.debug('function parse_delivered_to_field') ## Ticket id is in Delivered-To Field. Trac notify must send this # eg: is+390@surfsara.nl ## Only run if this parameter is se # if not self.parameters.recipient_delimiter: return False try: #print self.parameters.project_name self.logger.debug('Delivered To: %s' %m['Delivered-To']) id = m['Delivered-To'] id = id.split(self.parameters.recipient_delimiter)[1] id = id.split('@')[0] self.logger.debug('\t Found ticket id: %s' %id) ## The ticket_update expects a # in front of the ticket id # id = "#%s" %(id) ## true if ticket update # return self.ticket_update(m, id, spam_msg) except KeyError, detail: pass except IndexError, detail: pass return False def parse_subject_field(self, m, subject, spam_msg): """ """ self.logger.debug('function parse_subject_header') ## [hic] #1529: Re: LRZ # [hic] #1529?owner=bas,priority=medium: Re: LRZ # ticket_regex = r''' (?P[#][?].*) |(?P(?P[#][\d]+)(?P\?[^:]*)?:) ''' ## Check if FullBlogPlugin is installed # blog_enabled = None blog_regex = '' if self.get_config('components', 'tracfullblog.*') in ['enabled']: self.logger.debug('Trac BLOG support enabled') blog_enabled = True blog_regex = '''|(?Pblog(?P[?][^:]*)?:(?P\S*))''' ## Check if DiscussionPlugin is installed # discussion_enabled = None discussion_regex = '' if self.get_config('components', 'tracdiscussion.api.discussionapi') in ['enabled']: self.logger.debug('Trac Discussion support enabled') discussion_enabled = True discussion_regex = r''' |(?PForum[ ][#](?P\d+)[ ]-[ ]?) |(?PTopic[ ][#](?P\d+)[ ]-[ ]?) |(?PMessage[ ][#](?P\d+)[ ]-[ ]?) ''' regex_str = ticket_regex + blog_regex + discussion_regex SYSTEM_RE = re.compile(regex_str, re.VERBOSE) ## Find out if this is a ticket, a blog or a discussion # result = SYSTEM_RE.search(subject) if result: ## update ticket + fields # if result.group('reply'): self.system = 'ticket' ## Skip the last ':' character # if not self.ticket_update(m, result.group('reply')[:-1], spam_msg): self.new_ticket(m, subject, spam_msg) ## New ticket + fields # elif result.group('new_fields'): self.system = 'ticket' self.new_ticket(m, subject[:result.start('new_fields')], spam_msg, result.group('new_fields')) if blog_enabled: if result.group('blog'): self.system = 'blog' self.blog(m, subject[result.end('blog'):], result.group('blog_id'), result.group('blog_params')) if discussion_enabled: ## New topic. # if result.group('forum'): self.system = 'discussion' self.id = int(result.group('forum_id')) self.discussion_topic(m, subject[result.end('forum'):]) ## Reply to topic. # elif result.group('topic'): self.system = 'discussion' self.id = int(result.group('topic_id')) self.discussion_topic_reply(m, subject[result.end('topic'):]) ## Reply to topic message. # elif result.group('message'): self.system = 'discussion' self.id = int(result.group('message_id')) self.discussion_message_reply(m, subject[result.end('message'):]) else: self.system = 'ticket' (matched_id, subject) = self.ticket_update_by_subject(subject) if matched_id: if not self.ticket_update(m, matched_id, spam_msg): self.new_ticket(m, subject, spam_msg) else: ## No update by subject, so just create a new ticket # self.new_ticket(m, subject, spam_msg) ########## BODY TEXT functions ########################################################### def strip_signature(self, text): """ Strip signature from message, inspired by Mailman software """ self.logger.debug('function strip_signature: %s' %self.parameters.strip_signature_regex) body = [] STRIP_RE = re.compile( self.parameters.strip_signature_regex ) for line in text.splitlines(): match = STRIP_RE.match(line) if match: self.logger.debug('\t"%s " matched, skiping rest of message' %line) break body.append(line) return ('\n'.join(body)) def reflow(self, text, delsp = 0): """ Reflow the message based on the format="flowed" specification (RFC 3676) """ flowedlines = [] quotelevel = 0 prevflowed = 0 for line in text.splitlines(): from re import match ## Figure out the quote level and the content of the current line # m = match('(>*)( ?)(.*)', line) linequotelevel = len(m.group(1)) line = m.group(3) ## Determine whether this line is flowed # if line and line != '-- ' and line[-1] == ' ': flowed = 1 else: flowed = 0 if flowed and delsp and line and line[-1] == ' ': line = line[:-1] ## If the previous line is flowed, append this line to it # if prevflowed and line != '-- ' and linequotelevel == quotelevel: flowedlines[-1] += line ## Otherwise, start a new line # else: flowedlines.append('>' * linequotelevel + line) prevflowed = flowed return '\n'.join(flowedlines) def strip_quotes(self, text): """ Strip quotes from message by Nicolas Mendoza """ self.logger.debug('function strip_quotes: %s' %self.parameters.email_quote) body = [] STRIP_RE = re.compile( self.parameters.email_quote ) for line in text.splitlines(): try: match = STRIP_RE.match(line) if match: self.logger.debug('\t"%s " matched, skipping rest of message' %line) continue except UnicodeDecodeError: tmp_line = self.email_to_unicode(line) match = STRIP_RE.match(tmp_line) if match: self.logger.debug('\t"%s " matched, skipping rest of message' %line) continue body.append(line) return ('\n'.join(body)) def inline_properties(self, text): """ Parse text if we use inline keywords to set ticket fields """ self.logger.debug('function inline_properties') properties = dict() body = list() INLINE_EXP = re.compile('\s*[@]\s*(\w+)\s*:(.*)$') for line in text.splitlines(): match = INLINE_EXP.match(line) if match: keyword, value = match.groups() if self.parameters.inline_properties_first_wins: if keyword in self.properties.keys(): continue self.properties[keyword] = value.strip() self.logger.debug('\tinline properties: %s : %s' %(keyword,value)) else: body.append(line) return '\n'.join(body) def wrap_text(self, text, replace_whitespace = False): """ Will break a lines longer then given length into several small lines of size given length """ import textwrap LINESEPARATOR = '\n' reformat = '' for s in text.split(LINESEPARATOR): tmp = textwrap.fill(s, self.parameters.use_textwrap) if tmp: reformat = '%s\n%s' %(reformat,tmp) else: reformat = '%s\n' %reformat return reformat # Python2.4 and higher # #return LINESEPARATOR.join(textwrap.fill(s,width) for s in str.split(LINESEPARATOR)) # ########## EMAIL attachements functions ########################################################### def inline_part(self, part): """ """ self.logger.debug('function inline_part()') return part.get_param('inline', None, 'Content-Disposition') == '' or not part.has_key('Content-Disposition') def get_message_parts(self, msg, new_email=False): """ parses the email message and returns a list of body parts and attachments body parts are returned as strings, attachments are returned as tuples of (filename, Message object) """ self.logger.debug('function get_message_parts()') message_parts = list() ALTERNATIVE_MULTIPART = False for part in msg.walk(): content_maintype = part.get_content_maintype() content_type = part.get_content_type() self.logger.debug('\t Message part: Main-Type: %s' % content_maintype) self.logger.debug('\t Message part: Content-Type: %s' % content_type) ## Check content type # if content_type in self.STRIP_CONTENT_TYPES: self.logger.debug("\t A %s attachment named '%s' was skipped" %(content_type, part.get_filename())) continue ## Catch some mulitpart execptions # if content_type == 'multipart/alternative': ALTERNATIVE_MULTIPART = True continue ## Skip multipart containers # if content_maintype == 'multipart': self.logger.debug("\t Skipping multipart container") continue ## Check if this is an inline part. It's inline if there is co Cont-Disp header, # or if there is one and it says "inline" # inline = self.inline_part(part) ## Drop HTML message # if ALTERNATIVE_MULTIPART and self.parameters.drop_alternative_html_version: if content_type == 'text/html': self.logger.debug('\t Skipping alternative HTML message') ALTERNATIVE_MULTIPART = False continue #if self.VERSION < 1.0: # filename = part.get_filename() ## convert 7 bit filename to 8 bit unicode # raw_filename = part.get_filename() filename = self.email_to_unicode(raw_filename); s = '\t unicode filename: %s' %(filename) self.print_unicode(s) self.logger.debug('\t raw filename: %s' %repr(raw_filename)) if self.VERSION < 1.0: filename = self.check_filename_length(filename) ## Save all non plain text message as attachment # if not content_type in ['text/plain']: message_parts.append( (filename, part) ) ## We only convert html messages # if not content_type == 'text/html': self.logger.debug('\t Appending %s (%s)' %(repr(filename), content_type)) continue ## We have an text or html message # if not inline: self.logger.debug('\t Appending %s (%s), not an inline messsage part' %(repr(filename), content_type)) message_parts.append( (filename, part) ) continue ## Try to decode message part. We have a html or plain text messafe # body_text = part.get_payload(decode=1) if not body_text: body_text = part.get_payload(decode=0) ## Try to convert html message # if content_type == 'text/html': body_text = self.html_2_txt(body_text) if not body_text: continue format = email.Utils.collapse_rfc2231_value(part.get_param('Format', 'fixed')).lower() delsp = email.Utils.collapse_rfc2231_value(part.get_param('DelSp', 'no')).lower() if self.parameters.reflow and not self.parameters.verbatim_format and format == 'flowed': body_text = self.reflow(body_text, delsp == 'yes') if new_email and self.parameters.only_strip_on_update: self.logger.debug('Skip signature/quote stripping for new messages') else: if self.parameters.strip_signature: body_text = self.strip_signature(body_text) if self.parameters.strip_quotes: body_text = self.strip_quotes(body_text) if self.parameters.inline_properties: body_text = self.inline_properties(body_text) if self.parameters.use_textwrap: body_text = self.wrap_text(body_text) ## Get contents charset (iso-8859-15 if not defined in mail headers) # charset = part.get_content_charset() if not charset: charset = 'iso-8859-15' try: ubody_text = unicode(body_text, charset) except UnicodeError, detail: ubody_text = unicode(body_text, 'iso-8859-15') except LookupError, detail: ubody_text = 'ERROR: Could not find charset: %s, please install' %(charset) if self.parameters.verbatim_format: message_parts.append('{{{\r\n%s\r\n}}}' %ubody_text) else: message_parts.append('%s' %ubody_text) return message_parts def unique_attachment_names(self, message_parts): """ Make sure we have unique names attachments: - check if it contains illegal characters - Rename "None" filenames to "untitled-part" """ self.logger.debug('function unique_attachment_names()') renamed_parts = [] attachment_names = set() for item in message_parts: ## If not an attachment, leave it alone # if not isinstance(item, tuple): renamed_parts.append(item) continue (filename, part) = item ## If filename = None, use a default one # if filename in [ 'None']: filename = 'untitled-part' self.logger.info('\t Rename filename "None" to: %s' %filename) ## Guess the extension from the content type, use non strict mode # some additional non-standard but commonly used MIME types # are also recognized # ext = mimetypes.guess_extension(part.get_content_type(), False) if not ext: ext = '.bin' filename = '%s%s' % (filename, ext) ## Discard relative paths for windows/unix in attachment names # filename = filename.replace('\\', '_') filename = filename.replace('/', '_') ## remove linefeed char # for forbidden_char in ['\r', '\n']: filename = filename.replace(forbidden_char,'') ## We try to normalize the filename to utf-8 NFC if we can. # Files uploaded from OS X might be in NFD. # Check python version and then try it # #if sys.version_info[0] > 2 or (sys.version_info[0] == 2 and sys.version_info[1] >= 3): # try: # filename = unicodedata.normalize('NFC', unicode(filename, 'utf-8')).encode('utf-8') # except TypeError: # pass ## Make the filename unique for this ticket # num = 0 unique_filename = filename dummy_filename, ext = os.path.splitext(filename) while (unique_filename in attachment_names) or self.attachment_exists(unique_filename): num += 1 unique_filename = "%s-%s%s" % (dummy_filename, num, ext) s = '\t Attachment with filename %s will be saved as %s' % (filename, unique_filename) self.print_unicode(s) attachment_names.add(unique_filename) renamed_parts.append((filename, unique_filename, part)) return renamed_parts def attachment_exists(self, filename): self.logger.debug("function attachment_exists") s = '\t check if attachment already exists: Id : %s, Filename : %s' %(self.id, filename) self.print_unicode(s) ## Do we have a valid ticket id # if not self.id: return False try: if self.system == 'discussion': att = attachment.Attachment(self.env, 'discussion', 'ticket/%s' % (self.id,), filename) elif self.system == 'blog': att = attachment.Attachment(self.env, 'blog', '%s' % (self.id,), filename) else: att = attachment.Attachment(self.env, 'ticket', self.id, filename) return True except attachment.ResourceNotFound: return False ########## TRAC Ticket Text ########################################################### def get_body_text(self, message_parts): """ """ self.logger.debug('function get_body_text()') body_text = [] for part in message_parts: ## Plain text part, append it # if not isinstance(part, tuple): body_text.extend(part.strip().splitlines()) body_text.append("") continue (original, filename, part) = part inline = self.inline_part(part) ## Skip generation of attachment link if html is converted to text # if part.get_content_type() == 'text/html' and self.parameters.html2text_cmd and inline: s = 'Skipping attachment link for html part: %s' %(filename) self.print_unicode(s) continue if part.get_content_maintype() == 'image' and inline: if self.system != 'discussion': s = 'wiki image link for: %s' %(filename) self.print_unicode(s) body_text.append('[[Image(%s)]]' % filename) body_text.append("") else: if self.system != 'discussion': s = 'wiki attachment link for: %s' %(filename) self.print_unicode(s) body_text.append('[attachment:"%s"]' % filename) body_text.append("") ## Convert list body_texts to string # body_text = '\r\n'.join(body_text) return body_text def html_mailto_link(self, subject): """ This function returns a HTML mailto tag with the ticket id and author email address """ self.logger.debug("function html_mailto_link") if not self.author: author = self.email_addr else: author = self.author if not self.parameters.mailto_cc: self.parameters.mailto_cc = '' ## Bug in urllib.quote function # if isinstance(subject, unicode): subject = subject.encode('utf-8') ## use urllib to escape the chars # s = '%s?Subject=%s&cc=%s' %( urllib.quote(self.email_addr), urllib.quote('Re: #%s: %s' %(self.id, subject)), urllib.quote(self.parameters.mailto_cc) ) s = '[mailto:"%s" Reply to: %s]' %(s, author) self.logger.debug("\tmailto link %s" %s) return s ########## TRAC notify section ########################################################### def notify(self, tkt, new=True, modtime=0): """ A wrapper for the TRAC notify function. So we can use templates """ self.logger.debug('function notify()') class Email2TracNotifyEmail(TicketNotifyEmail): def __init__(self, env): TicketNotifyEmail.__init__(self, env) self.email2trac_notify_reporter = None self.email2trac_replyto = None def send(self, torcpts, ccrcpts): #print 'Yes this works' dest = self.reporter or 'anonymous' hdrs = {} hdrs['Message-ID'] = self.get_message_id(dest, self.modtime) hdrs['X-Trac-Ticket-ID'] = str(self.ticket.id) hdrs['X-Trac-Ticket-URL'] = self.data['ticket']['link'] if not self.newticket: msgid = self.get_message_id(dest) hdrs['In-Reply-To'] = msgid hdrs['References'] = msgid if self.email2trac_notify_reporter: if not self.email2trac_notify_reporter in torcpts: torcpts.append(self.email2trac_notify_reporter) if self.email2trac_replyto: # use to rewrite reply to # hdrs does not work, multiple reply addresses #hdrs['Reply-To'] = 'bas.van.der.vlies@gmail.com' self.replyto_email = self.email2trac_replyto NotifyEmail.send(self, torcpts, ccrcpts, hdrs) if self.parameters.dry_run : self.logger.info('DRY_RUN: self.notify(tkt, True) reporter = %s' %tkt['reporter']) return try: tn = Email2TracNotifyEmail(self.env) ## additionally append sender (regardeless of settings in trac.ini) # if self.parameters.notify_reporter: self.logger.debug('\t Notify reporter set') if not self.email_header_acl('notify_reporter_black_list', self.email_addr, False): tn.email2trac_notify_reporter = self.email_addr if self.parameters.notify_replyto_rewrite: self.logger.debug('\t Notify replyto rewrite set to:%s' %self.parameters.notify_replyto_rewrite) if self.parameters.notify_replyto_rewrite in ['use_mail_domain']: self.logger.debug('\t\t use_mail_domain:%s' %self.smtp_default_domain) tn.email2trac_replyto = '%s@%s' %(self.id, self.smtp_default_domain ) elif self.parameters.notify_replyto_rewrite in ['use_trac_smtp_replyto']: self.logger.debug('\t\t use_trac_smtp_replyto delimiter:%s' %self.parameters.recipient_delimiter) ## handle addres with @ and without # dummy = self.smtp_replyto.split('@') if len(dummy) > 1: tn.email2trac_replyto = '%s%s%s@%s' %(dummy[0], self.parameters.recipient_delimiter, self.id, dummy[1]) else: tn.email2trac_replyto = '%s%s%s' %(dummy[0], self.parameters.recipient_delimiter, self.id) if self.parameters.alternate_notify_template: if self.VERSION >= 0.12: from trac.web.chrome import Chrome if self.parameters.alternate_notify_template_update and not new: tn.template_name = self.parameters.alternate_notify_template_update else: tn.template_name = self.parameters.alternate_notify_template tn.template = Chrome(tn.env).load_template(tn.template_name, method='text') else: tn.template_name = self.parameters.alternate_notify_template tn.notify(tkt, new, modtime) except Exception, e: self.logger.error('Failure sending notification on creation of ticket #%s: %s' %(self.id, e)) ########## END Class Definition ######################################################## ########## Parse Config File ########################################################### def ReadConfig(file, name): """ Parse the config file """ if not os.path.isfile(file): print 'File %s does not exist' %file sys.exit(1) config = trac_config.Configuration(file) parentdir = config.get('DEFAULT', 'parentdir') sections = config.sections() ## use some trac internals to get the defaults # tmp = config.parser.defaults() project = SaraDict() for option, value in tmp.items(): try: project[option] = int(value) except ValueError: project[option] = value if name: if name in sections: project = SaraDict() for option, value in config.options(name): try: project[option] = int(value) except ValueError: project[option] = value elif not parentdir: print "Not a valid project name: %s, valid names are: %s" %(name, sections) print "or set parentdir in the [DEFAULT] section" sys.exit(1) ## If parentdir then set project dir to parentdir + name # if not project.has_key('project'): if not parentdir: print "You must set project or parentdir in your configuration file" sys.exit(1) elif not name: print "You must configure a project section in your configuration file" else: project['project'] = os.path.join(parentdir, name) ## # Save the project name # project['project_name'] = os.path.basename(project['project']) return project ########## Setup Logging ############################################################### def setup_log(parameters, project_name, interactive=None): """ Setup logging Note for log format the usage of `$(...)s` instead of `%(...)s` as the latter form would be interpreted by the ConfigParser itself. """ logger = logging.getLogger('email2trac %s' %project_name) if interactive: parameters.log_type = 'stderr' if not parameters.log_type: if sys.platform in ['win32', 'cygwin']: parameters.log_type = 'eventlog' else: parameters.log_type = 'syslog' if parameters.log_type == 'file': if not parameters.log_file: parameters.log_file = 'email2trac.log' if not os.path.isabs(parameters.log_file): parameters.log_file = os.path.join(tempfile.gettempdir(), parameters.log_file) log_handler = logging.FileHandler(parameters.log_file) elif parameters.log_type in ('winlog', 'eventlog', 'nteventlog'): ## Requires win32 extensions # logid = "email2trac" log_handler = logging.handlers.NTEventLogHandler(logid, logtype='Application') elif parameters.log_type in ('syslog', 'unix'): log_handler = logging.handlers.SysLogHandler('/dev/log') elif parameters.log_type in ('stderr'): log_handler = logging.StreamHandler(sys.stderr) else: log_handler = logging.handlers.BufferingHandler(0) if parameters.log_format: parameters.log_format = parameters.log_format.replace('$(', '%(') else: parameters.log_format = '%(name)s: %(message)s' if parameters.log_type in ('file', 'stderr'): parameters.log_format = '%(asctime)s ' + parameters.log_format log_formatter = logging.Formatter(parameters.log_format) log_handler.setFormatter(log_formatter) logger.addHandler(log_handler) if (parameters.log_level in ['DEBUG', 'ALL']) or (parameters.debug > 0): logger.setLevel(logging.DEBUG) parameters.debug = 1 elif parameters.log_level in ['INFO'] or parameters.verbose: logger.setLevel(logging.INFO) elif parameters.log_level in ['WARNING']: logger.setLevel(logging.WARNING) elif parameters.log_level in ['ERROR']: logger.setLevel(logging.ERROR) elif parameters.log_level in ['CRITICAL']: logger.setLevel(logging.CRITICAL) else: logger.setLevel(logging.INFO) return logger ########## Own TicketNotifyEmail class ############################################################### if __name__ == '__main__': ## Default config file # agilo = False bh_product = None configfile = '@email2trac_conf@' project = '' component = '' ticket_prefix = 'default' dry_run = None verbose = None debug_interactive = None virtualenv = '@virtualenv@' SHORT_OPT = 'AB:cdE:hf:np:t:v' LONG_OPT = ['agilo', 'bh_product=', 'component=', 'debug', 'dry-run', 'help', 'file=', 'project=', 'ticket_prefix=', 'virtualenv=', 'verbose'] try: opts, args = getopt.getopt(sys.argv[1:], SHORT_OPT, LONG_OPT) except getopt.error,detail: print __doc__ print detail sys.exit(1) project_name = None for opt,value in opts: if opt in [ '-h', '--help']: print __doc__ sys.exit(0) elif opt in ['-A', '--agilo']: agilo = True elif opt in ['-B', '--bh_product']: bh_product = value elif opt in ['-c', '--component']: component = value elif opt in ['-d', '--debug']: debug_interactive = 1 elif opt in ['-E', '--virtualenv']: virtualenv = value elif opt in ['-f', '--file']: configfile = value elif opt in ['-n', '--dry-run']: dry_run = True elif opt in ['-p', '--project']: project_name = value elif opt in ['-t', '--ticket_prefix']: ticket_prefix = value elif opt in ['-v', '--verbose']: verbose = True if virtualenv and os.path.exists(virtualenv): activate_this = os.path.join(virtualenv, 'bin/activate_this.py') if os.path.exists(activate_this): execfile(activate_this, dict(__file__=activate_this)) try: from trac import __version__ as trac_version from trac import config as trac_config except ImportError, detail: print detail print "Can not find a a valid trac installation, solutions could be:" print "\tset PYTHONPATH" print "\tuse the --virtualenv option" sys.exit(1) settings = ReadConfig(configfile, project_name) ## The default prefix for ticket values in email2trac.conf # settings.ticket_prefix = ticket_prefix settings.dry_run = dry_run settings.verbose = verbose if bh_product: settings.bh_product = bh_product if not settings.debug and debug_interactive: settings.debug = debug_interactive if not settings.project: print __doc__ print 'No Trac project is defined in the email2trac config file.' sys.exit(1) logger = setup_log(settings, os.path.basename(settings.project), debug_interactive) if component: settings['component'] = component ## We are only interested in the major versions # 0.12.3 --> 0.12 # 1.0.2 --> 1.0 # l = trac_version.split('.') version = '.'.join(l[0:2]) logger.debug("Found trac version: %s" %(version)) try: if version in ['0.12', '0.13', '1.0', '1.1']: from trac import attachment from trac import config as trac_config from trac import util from trac.core import TracError from trac.env import Environment from trac.perm import PermissionSystem from trac.perm import PermissionCache from trac.test import Mock, MockPerm from trac.ticket.api import TicketSystem from trac.ticket.web_ui import TicketModule from trac.web.href import Href try: import pkg_resources pkg = pkg_resources.get_distribution('BloodhoundMultiProduct') bloodhound = pkg.version.split()[:2] except pkg_resources.DistributionNotFound: # assume no bloodhound bloodhound = None if bloodhound: from multiproduct.env import Environment, ProductEnvironment from multiproduct.ticket.web_ui import (ProductTicketModule as TicketModule) logger.debug("Found Bloodhound Distribution") if not settings.bh_product: print __doc__ print 'No Bloodhound project defined (bh_project) in section:%s email2trac config file.' %(settings.project) sys.exit(1) if agilo: try: from agilo.ticket.model import AgiloTicket as Ticket except ImportError, detail: try: from agilo.ticket.model import Ticket except ImportError, detail: logger.error('Could not find Trac Agilo environemnt') sys.exit(0) else: from trac.ticket import Ticket # # return util.text.to_unicode(str) # # see http://projects.edgewall.com/trac/changeset/2799 from trac.ticket.notification import TicketNotifyEmail from trac.notification import NotifyEmail else: logger.error('TRAC version %s is not supported' %version) sys.exit(0) ## Must be set before environment is created # if settings.has_key('python_egg_cache'): python_egg_cache = str(settings['python_egg_cache']) os.environ['PYTHON_EGG_CACHE'] = python_egg_cache if settings.debug > 0: logger.debug('Loading environment %s', settings.project) try: env = Environment(settings['project'], create=0) if bloodhound: ## possibly overkill testing if the multiproduct schema is a # new enough version # from multiproduct.env import MultiProductSystem mps = MultiProductSystem(env) if mps.get_version() > 4: try: env = ProductEnvironment(env, settings.bh_product, create=0) except LookupError: logger.error('%s is not a valid Bloodhound' %settings.bh_product) sys.exit(0) except IOError, detail: logger.error("trac error: %s" %detail) sys.exit(0) except TracError, detail: logger.error("trac error: %s" %detail) sys.exit(0) tktparser = TicketEmailParser(env, settings, logger, float(version)) tktparser.parse(sys.stdin) ## Catch all errors and use the logging module # except Exception, error: etype, evalue, etb = sys.exc_info() for e in traceback.format_exception(etype, evalue, etb): logger.critical(e) if m: tktparser.save_email_for_debug(m, settings.project_name) sys.exit(1) # EOB