source: trunk/email2trac.py.in @ 262

Last change on this file since 262 was 262, checked in by bas, 15 years ago

closes #130

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