source: trunk/email2trac.py.in @ 508

Last change on this file since 508 was 508, checked in by bas, 13 years ago

We now can override the notify function with AlwaysNotifyReport?, see #178,#229

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