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
Line 
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#
20# For vi/emacs or other use tabstop=4 (vi: set ts=4)
21#
22"""
23email2trac.py -- Email tickets to Trac.
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
30Authors:
31  Bas van der Vlies <basv@sara.nl>
32  Walter de Jong <walter@sara.nl>
33
34The scripts reads emails from stdin and inserts directly into a Trac database.
35
36How to use
37----------
38 * See https://subtrac.sara.nl/oss/email2trac/
39
40 * Create an config file:
41    [DEFAULT]                        # REQUIRED
42    project      : /data/trac/test   # REQUIRED
43    debug        : 1                 # OPTIONAL, if set print some DEBUG info
44
45    [jouvin]                         # OPTIONAL project declaration, if set both fields necessary
46    project      : /data/trac/jouvin # use -p|--project jouvin. 
47       
48 * default config file is : /etc/email2trac.conf
49
50 * Commandline opions:
51                -h,--help
52                -d, --debug
53                -f,--file  <configuration file>
54                -n,--dry-run
55                -p, --project <project name>
56                -t, --ticket_prefix <name>
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
67import email.Iterators
68import email.Header
69import re
70import urllib
71import unicodedata
72import mimetypes
73import traceback
74import logging
75import logging.handlers
76import UserDict
77import tempfile
78
79from datetime import tzinfo, timedelta, datetime
80from stat import *
81
82
83from trac import __version__ as trac_version
84from trac import config as trac_config
85
86## Some global variables
87#
88m = None
89
90# This is to for the function AlwaysNotifyReporter
91sender_email = None
92
93class SaraDict(UserDict.UserDict):
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)
112
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
135
136    def __iter__(self):
137        return iter(self.data.keys())
138
139class TicketEmailParser(object):
140    env = None
141    comment = '> '
142
143    def __init__(self, env, parameters, logger, version):
144        self.env = env
145
146        # Database connection
147        #
148        self.db = None
149
150        # Save parameters
151        #
152        self.parameters = parameters
153        self.logger = logger
154
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()
164
165        ## fields properties via body_text
166        #
167        self.properties = dict()
168
169        self.VERSION = version
170
171        self.get_config = self.env.config.get
172
173        ## init function ##
174        #
175        self.setup_parameters()
176
177    def setup_parameters(self):
178        if self.parameters.umask:
179            os.umask(self.parameters.umask)
180
181        if not self.parameters.spam_level:
182            self.parameters.spam_level = 0
183
184        if not self.parameters.spam_header:
185            self.parameters.spam_header = 'X-Spam-Score'
186
187        if not self.parameters.email_quote:
188            self.parameters.email_quote = '> '
189
190        if not self.parameters.ticket_update_by_subject_lookback:
191            self.parameters.ticket_update_by_subject_lookback = 30
192
193        if self.parameters.verbatim_format == None:
194            self.parameters.verbatim_format = 1
195
196        if self.parameters.reflow == None:
197            self.parameters.reflow = 1
198
199        if self.parameters.binhex:
200            self.STRIP_CONTENT_TYPES.append('application/mac-binhex40')
201
202        if self.parameters.applesingle:
203            self.STRIP_CONTENT_TYPES.append('application/applefile')
204
205        if self.parameters.appledouble:
206            self.STRIP_CONTENT_TYPES.append('application/applefile')
207
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())
212
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')
217
218        if self.parameters.email_triggers_workflow == None:
219            self.parameters.email_triggers_workflow = 1
220
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()
225
226        self.trac_smtp_from = self.get_config('notification', 'smtp_from')
227        self.smtp_default_domain = self.get_config('notification', 'smtp_default_domain')
228
229
230        self.system = None
231
232########## Email Header Functions ###########################################################
233
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])
242
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
255
256        # How to handle SPAM messages
257        #
258        if self.parameters.drop_spam and spam:
259
260            self.logger.info('Message is a SPAM. Automatic ticket insertion refused (SPAM level > %d)' %self.parameters.spam_level)
261            return 'drop'   
262
263        elif spam:
264
265            return 'Spam'   
266        else:
267
268            return False
269
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)
276
277        try:
278            mail_addresses = self.parameters[keyword]
279
280            # Check if we have an empty string
281            #
282            if not mail_addresses:
283                return default
284
285        except KeyError, detail:
286            self.logger.debug('%s not defined, all messages are allowed.' %(keyword))
287
288            return default
289
290        mail_addresses = string.split(mail_addresses, ',')
291
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
298
299        return False
300
301    def email_header_txt(self, m):
302        """
303        Display To and CC addresses in description field
304        """
305        s = ''
306
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'])
311
312        return  self.email_to_unicode(s)
313
314
315    def get_sender_info(self, message):
316        """
317        Get the default author name and email address from the message
318        """
319
320        self.email_to = self.email_to_unicode(message['to'])
321        self.to_name, self.to_email_addr = email.Utils.parseaddr (self.email_to)
322
323        self.email_from = self.email_to_unicode(message['from'])
324        self.email_name, self.email_addr  = email.Utils.parseaddr(self.email_from)
325
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
335
336        if self.parameters.ignore_trac_user_settings:
337            return
338
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            ]
351
352        if len(users) >= 1:
353            self.email_from = users[0][0]
354            self.author = users[0][0]
355
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')
361
362        ## Only use name or email adress
363        #ticket['reporter'] = self.email_from
364        ticket['reporter'] = self.author
365
366
367        # Put all CC-addresses in ticket CC field
368        #
369        if self.parameters.reply_all:
370
371            email_cc = ''
372
373            cc_addrs = email.Utils.getaddresses( message.get_all('cc', []) )
374
375            if not cc_addrs:
376                return
377
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
391
392            for name,addr in cc_addrs:
393       
394                ## Prevent mail loop
395                #
396                #if addr in to_list:
397
398                if addr == self.trac_smtp_from:
399                    self.logger.debug("Skipping %s mail address for CC-field" %(addr))
400                    continue
401
402                if email_cc:
403                    email_cc = '%s, %s' %(email_cc, addr)
404                else:
405                    email_cc = addr
406
407            if email_cc:
408                self.logger.debug('set_reply_fields: %s' %email_cc)
409
410                ticket['cc'] = self.email_to_unicode(email_cc)
411
412
413########## DEBUG functions  ###########################################################
414
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')
420
421        if self.parameters.dry_run:
422            print 'DRY-RUN: not saving body to %s' %(body_file)
423            return
424
425        print 'writing body to %s' %(body_file)
426        fx = open(body_file, 'wb')
427        if not message_body:
428                message_body = '(None)'
429
430        message_body = message_body.encode('utf-8')
431        #message_body = unicode(message_body, 'iso-8859-15')
432
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
439
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
452
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   
464
465            part_file = os.path.join(self.parameters.tmpdir, filename)
466            s = 'writing part%d (%s)' % (n,part_file)
467            self.print_unicode(s)
468
469            if self.parameters.dry_run:
470                print 'DRY_RUN: NOT saving attachments'
471                continue
472
473            part_file = util.text.unicode_quote(part_file)
474
475            fx = open(part_file, 'wb')
476            text = part.get_payload(decode=1)
477
478            if not text:
479                text = '(None)'
480
481            fx.write(text)
482            fx.close()
483
484            try:
485                os.chmod(part_file,S_IRWXU|S_IRWXG|S_IRWXO)
486            except OSError:
487                pass
488
489    def save_email_for_debug(self, message, create_tempfile=False):
490
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')
496
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)
501
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
510
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)
516
517########## Conversion functions  ###########################################################
518
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")
526
527        results =  email.Header.decode_header(message_str)
528
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')
546
547            if s:
548                s = '%s %s' %(s, temp)
549            else:
550                s = '%s' %temp
551
552        #s = s.encode('utf-8')
553        return s
554
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")
560
561        fields = string.split(s, self.parameters.subject_field_separator)
562
563        result = dict()
564        for field in fields:
565            try:
566                index, value = string.split(field, '=')
567
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
573
574                if value:
575                    result[index.lower()] = value
576
577            except ValueError:
578                pass
579        return result
580
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))
589
590
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')
596
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')
604#
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'
608#
609#           return parsed_data
610#           
611#       except ImportError::
612#           pass
613
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)
622
623            else:
624                f = open(tmp_file, "w+")
625                f.write(data)
626                f.close()
627
628                lines = os.popen(cmd).readlines()
629                parsed_data =  ''.join(lines)
630
631                os.unlink(tmp_file)
632
633        else:
634            self.logger.debug('\t No html2text conversion tool specified in email2trac.conf')
635
636        return parsed_data
637
638########## TRAC ticket functions  ###########################################################
639
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')
645
646        if tkt['reporter'].lower() in [self.author, self.email_addr]:
647            self.logger.debug('ALLOW, %s is the ticket reporter' %(self.email_addr))
648
649            return True
650
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))
654
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))
663
664                    return True
665
666        except KeyError:
667            pass
668
669        return False
670
671    def check_permission(self, tkt, action):
672        """
673        check if the reporter has the right permission for the action:
674          - TICKET_CREATE
675          - TICKET_MODIFY
676          - TICKET_APPEND
677          - TICKET_CHGPROP
678
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")
685
686        if self.parameters.ticket_permission_system in ['trac']:
687
688            perm = PermissionSystem(self.env)
689            if perm.check_permission(action, self.author):
690                return True
691            else:
692                return False
693
694        elif self.parameters.ticket_permission_system in ['update_restricted_to_participants']:
695            return (self.check_permission_participants(tkt, action))   
696
697        ## Default is to allow everybody ticket updates and ticket creation
698        #
699        else:
700                return True
701
702
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")
715
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
723
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']
731
732            except KeyError:
733                #sys_dict[field['name']] = None
734                pass
735
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)
743
744            ## To prevent mail loop
745            #
746            if field == 'cc':
747
748                cc_list = user_dict['cc'].split(',')
749
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))
752
753                    cc_list.remove(self.trac_smtp_from)
754
755                value = ','.join(cc_list)
756               
757
758            ## Check if every value is allowed for this field
759            #
760            if sys_dict.has_key(field):
761
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) )
769
770            else:
771                ## Only set if we have a value
772                #
773                #if value:
774                #   ticket[field] = value
775                ticket[field] = value
776
777            if self.parameters.debug:
778                s = 'ticket_field\t %s = %s' %(field,  ticket[field])
779                self.print_unicode(s)
780
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")
788
789        if not self.parameters.ticket_update:
790            self.logger.debug("ticket_update disabled")
791            return False
792
793        ## Must we update ticket fields
794        #
795        update_fields = dict()
796        try:
797            id, keywords = string.split(id, '?')
798
799            update_fields = self.str_to_dict(keywords)
800
801            ## Strip '#'
802            #
803            self.id = int(id[1:])
804
805        except ValueError:
806
807            ## Strip '#'
808            #
809            self.id = int(id[1:])
810
811        self.logger.debug("ticket_update id %s" %id)
812
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)
819
820        try:
821            tkt = Ticket(self.env, self.id, self.db)
822
823        except util.TracError, detail:
824
825            ## Not a valid ticket
826            #
827            self.id = None
828            return False
829
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
836
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
842
843
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):
848
849            self.logger.debug('Workflow ticket update fields: ')
850
851            from trac.ticket.default_workflow import ConfigurableTicketWorkflow
852            from trac.test import Mock, MockPerm
853
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
864
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
871
872            if workflow:
873
874                if self.parameters.ticket_permission_system:
875
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)
881
882                else:
883                    fields = controller.get_ticket_changes(req, tkt, workflow)
884
885                for key in fields.keys():
886                    self.logger.debug('\t %s : %s' %(key, fields[key]))
887                    tkt[key] = fields[key]
888
889        ## Old pre 0.11 situation
890        #
891        elif self.parameters.email_triggers_workflow:
892
893            self.logger.debug('email triggers workflow pre trac 0.11')
894
895            if tkt['status'] in ['closed']:
896                tkt['status'] = 'reopened'
897                tkt['resolution'] = ''
898
899        else:
900            self.logger.debug('email triggers workflow disabled')
901
902        ## Must we update some ticket fields properties via subject line
903        #
904        if update_fields:
905            self.update_ticket_fields(tkt, update_fields)
906
907        message_parts = self.get_message_parts(m)
908        message_parts = self.unique_attachment_names(message_parts)
909
910        ## Must we update some ticket fields properties via body_text
911        #
912        if self.properties:
913                self.update_ticket_fields(tkt, self.properties)
914
915        if self.parameters.email_header:
916            message_parts.insert(0, self.email_header_txt(m))
917
918        body_text = self.get_body_text(message_parts)
919
920        error_with_attachments = self.attach_attachments(message_parts)
921
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           
931
932        if not spam:
933            self.notify(tkt, False, when)
934
935        return True
936
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')
944
945        user_dict = dict()
946
947        for field in ticket.fields:
948
949            name = field['name']
950
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')
962
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)
969
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)
978
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)
987
988        self.update_ticket_fields(ticket, user_dict, new=1)
989
990        if 'status' not in user_dict.keys():
991            ticket['status'] = 'new'
992
993
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>
1000
1001        So we must have the last column
1002        """
1003        self.logger.debug('function ticket_update_by_subject')
1004
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)
1010
1011            if result:
1012                ## This is a reply
1013                #
1014                orig_subject = result.group(1)
1015
1016                self.logger.debug('subject search string: %s' %(orig_subject))
1017
1018                cursor = self.db.cursor()
1019                summaries = [orig_subject, '%%: %s' % orig_subject]
1020
1021                ## Convert days to seconds
1022                #
1023                lookback = int(time.mktime(time.gmtime())) - \
1024                        self.parameters.ticket_update_by_subject_lookback * 24 * 3600
1025
1026
1027                for summary in summaries:
1028                    self.logger.debug('Looking for summary matching: "%s"' % summary)
1029
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()])
1034
1035                    for row in cursor:
1036                        (matched_id,) = row
1037
1038                        self.logger.debug('Found matching ticket id: %d' % matched_id)
1039
1040                        break
1041
1042                    if matched_id:
1043                        matched_id = '#%d' % matched_id
1044                        return (matched_id, orig_subject)
1045                   
1046                    subject = orig_subject
1047
1048        return (matched_id, subject)
1049
1050
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')
1056
1057        tkt = Ticket(self.env)
1058
1059        self.set_reply_fields(tkt, msg)
1060
1061        self.set_ticket_fields(tkt)
1062
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
1069
1070        ## Old style setting for component, will be removed
1071        #
1072        if spam:
1073            tkt['component'] = 'Spam'
1074
1075        elif self.parameters.has_key('component'):
1076            tkt['component'] = self.parameters['component']
1077
1078        if not msg['Subject']:
1079            tkt['summary'] = u'(No subject)'
1080        else:
1081            tkt['summary'] = subject
1082
1083
1084        if set_fields:
1085            rest, keywords = string.split(set_fields, '?')
1086
1087            if keywords:
1088                update_fields = self.str_to_dict(keywords)
1089                self.update_ticket_fields(tkt, update_fields)
1090
1091
1092        message_parts = self.get_message_parts(msg)
1093
1094        ## Must we update some ticket fields properties via body_text
1095        #
1096        if self.properties:
1097                self.update_ticket_fields(tkt, self.properties)
1098
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)
1109
1110        tkt['description'] = body_text
1111
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)
1118
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 = ''
1126
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
1133
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'])
1140
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)
1147
1148        if error_with_attachments:
1149            changed = True
1150            comment = '%s\n%s\n' %(comment, error_with_attachments)
1151
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)
1158
1159        if not spam:
1160            self.notify(tkt, True)
1161
1162
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()')
1168
1169        if self.parameters.dry_run:
1170            print "DRY_RUN: no attachments attached to tickets"
1171            return ''
1172
1173        count = 0
1174
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
1187
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()
1197
1198            ## get the file_size
1199            #
1200            stats = os.lstat(path)
1201            file_size = stats[ST_SIZE]
1202
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)
1208
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)
1223 
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')
1230
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)
1237
1238            ## Remove the created temporary filename
1239            #
1240            fd.close()
1241            os.unlink(path)
1242
1243        ## return error
1244        #
1245        return status
1246
1247########## Fullblog functions  #################################################
1248
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
1258
1259        ## instantiate blog core
1260        #
1261        blog = FullBlogCore(self.env)
1262        req = Mock(authname='anonymous', perm=MockPerm(), args={})
1263
1264        if id:
1265
1266            ## update blog
1267            #
1268            comment = BlogComment(self.env, id)
1269            comment.author = self.author
1270
1271            message_parts = self.get_message_parts(m)
1272            comment.comment = self.get_body_text(message_parts)
1273
1274            blog.create_comment(req, comment)
1275
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()))
1281
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'])
1286
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)
1291
1292
1293########## Discussion functions  ##############################################
1294
1295    def discussion_topic(self, content, subject):
1296
1297        ## Import modules.
1298        #
1299        from tracdiscussion.api import DiscussionApi
1300        from trac.util.datefmt import to_timestamp, utc
1301
1302        self.logger.debug('Creating a new topic in forum:', self.id)
1303
1304        ## Get dissussion API component.
1305        #
1306        api = self.env[DiscussionApi]
1307        context = self._create_context(content, subject)
1308
1309        ## Get forum for new topic.
1310        #
1311        forum = api.get_forum(context, self.id)
1312
1313        if not forum:
1314            self.logger.error("ERROR: Replied forum doesn't exist")
1315
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)}
1324
1325        ## Add topic to DB and commit it.
1326        #
1327        self._add_topic(api, context, topic)
1328        self.db.commit()
1329
1330    def discussion_topic_reply(self, content, subject):
1331
1332        ## Import modules.
1333        #
1334        from tracdiscussion.api import DiscussionApi
1335        from trac.util.datefmt import to_timestamp, utc
1336
1337        self.logger.debug('Replying to discussion topic', self.id)
1338
1339        ## Get dissussion API component.
1340        #
1341        api = self.env[DiscussionApi]
1342        context = self._create_context(content, subject)
1343
1344        ## Get replied topic.
1345        #
1346        topic = api.get_topic(context, self.id)
1347
1348        if not topic:
1349            self.logger.error("ERROR: Replied topic doesn't exist")
1350
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)}
1359
1360        ## Add message to DB and commit it.
1361        #
1362        self._add_message(api, context, message)
1363        self.db.commit()
1364
1365    def discussion_message_reply(self, content, subject):
1366
1367        ## Import modules.
1368        #
1369        from tracdiscussion.api import DiscussionApi
1370        from trac.util.datefmt import to_timestamp, utc
1371
1372        self.logger.debug('Replying to discussion message', self.id)
1373
1374        ## Get dissussion API component.
1375        #
1376        api = self.env[DiscussionApi]
1377        context = self._create_context(content, subject)
1378
1379        ## Get replied message.
1380        #
1381        message = api.get_message(context, self.id)
1382
1383        if not message:
1384            self.logger.error("ERROR: Replied message doesn't exist")
1385
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)}
1394
1395        ## Add message to DB and commit it.
1396        #
1397        self._add_message(api, context, message)
1398        self.db.commit()
1399
1400    def _create_context(self, content, subject):
1401
1402        ## Import modules.
1403        #
1404        from trac.mimeview import Context
1405        from trac.web.api import Request
1406        from trac.perm import PermissionCache
1407
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': []}
1421
1422        if self.env.base_url_for_redirect:
1423            environ['trac.base_url'] = self.env.base_url
1424
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
1431
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
1439
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)
1445
1446        return context
1447
1448    def _add_topic(self, api, context, topic):
1449        context.req.perm.assert_permission('DISCUSSION_APPEND')
1450
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)
1460
1461        ## Add a new topic.
1462        #
1463        api.add_topic(context, topic)
1464
1465        ## Get inserted topic with new ID.
1466        #
1467        topic = api.get_topic_by_time(context, topic['time'])
1468
1469        ## Attach attachments.
1470        #
1471        self.id = topic['id']
1472        self.attach_attachments(context.content_parts, True)
1473
1474        ## Notify change listeners.
1475        #
1476        for listener in api.topic_change_listeners:
1477            listener.topic_created(context, topic)
1478
1479    def _add_message(self, api, context, message):
1480        context.req.perm.assert_permission('DISCUSSION_APPEND')
1481
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)
1491
1492        ## Add message.
1493        #
1494        api.add_message(context, message)
1495
1496        ## Get inserted message with new ID.
1497        #
1498        message = api.get_message_by_time(context, message['time'])
1499
1500        ## Attach attachments.
1501        #
1502        self.id = message['topic']
1503        self.attach_attachments(context.content_parts, True)
1504
1505        ## Notify change listeners.
1506        #
1507        for listener in api.message_change_listeners:
1508            listener.message_created(context, message)
1509
1510########## MAIN function  ######################################################
1511
1512    def parse(self, fp):
1513        """
1514        """
1515        self.logger.debug('Main function parse')
1516        global m
1517
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
1530
1531        if self.parameters.debug:     # save the entire e-mail message text
1532            self.save_email_for_debug(m, True)
1533
1534        self.db = self.env.get_db_cnx()
1535        self.get_sender_info(m)
1536
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
1540
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
1544
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
1548
1549        ## If spam drop the message
1550        #
1551        if self.spam(m) == 'drop':
1552            return False
1553
1554        elif self.spam(m) == 'spam':
1555            spam_msg = True
1556        else:
1557            spam_msg = False
1558
1559        if not m['Subject']:
1560            subject  = 'No Subject'
1561        else:
1562            subject  = self.email_to_unicode(m['Subject'])
1563
1564        self.logger.debug('subject: %s' %subject)
1565
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*))'''
1581
1582
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            '''
1595
1596
1597        regex_str = ticket_regex + blog_regex + discussion_regex
1598        SYSTEM_RE = re.compile(regex_str, re.VERBOSE)
1599
1600        ## Find out if this is a ticket, a blog or a discussion
1601        #
1602        result =  SYSTEM_RE.search(subject)
1603
1604        if result:
1605            ## update ticket + fields
1606            #
1607            if result.group('reply'):
1608                self.system = 'ticket'
1609
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)
1614
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'))
1620
1621            if blog_enabled:
1622                if result.group('blog'):
1623                    self.system = 'blog'
1624                    self.blog(result.group('blog_id'))
1625
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'):])
1633
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'):])
1640
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'):])
1647
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)
1658
1659
1660########## BODY TEXT functions  ###########################################################
1661
1662    def strip_signature(self, text):
1663        """
1664        Strip signature from message, inspired by Mailman software
1665        """
1666        self.logger.debug('function strip_signature')
1667
1668        body = []
1669        for line in text.splitlines():
1670            if line == '-- ':
1671                break
1672            body.append(line)
1673
1674        return ('\n'.join(body))
1675
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
1683
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)
1692
1693            ## Determine whether this line is flowed
1694            #
1695            if line and line != '-- ' and line[-1] == ' ':
1696                flowed = 1
1697            else:
1698                flowed = 0
1699
1700            if flowed and delsp and line and line[-1] == ' ':
1701                line = line[:-1]
1702
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
1707
1708            ## Otherwise, start a new line
1709            #
1710            else:
1711                flowedlines.append('>' * linequotelevel + line)
1712
1713            prevflowed = flowed
1714           
1715
1716        return '\n'.join(flowedlines)
1717
1718    def strip_quotes(self, text):
1719        """
1720        Strip quotes from message by Nicolas Mendoza
1721        """
1722        self.logger.debug('function strip_quotes')
1723
1724        body = []
1725        for line in text.splitlines():
1726            try:
1727
1728                if line.startswith(self.parameters.email_quote):
1729                    continue
1730
1731            except UnicodeDecodeError:
1732
1733                tmp_line = self.email_to_unicode(line)
1734                if tmp_line.startswith(self.parameters.email_quote):
1735                    continue
1736               
1737            body.append(line)
1738
1739        return ('\n'.join(body))
1740
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')
1746
1747        properties = dict()
1748        body = list()
1749
1750        INLINE_EXP = re.compile('\s*[@]\s*(\w+)\s*:(.*)$')
1751
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()
1757
1758                self.logger.debug('inline properties: %s : %s' %(keyword,value))
1759
1760            else:
1761                body.append(line)
1762               
1763        return '\n'.join(body)
1764
1765
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
1772
1773        LINESEPARATOR = '\n'
1774        reformat = ''
1775
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
1782
1783        return reformat
1784
1785        # Python2.4 and higher
1786        #
1787        #return LINESEPARATOR.join(textwrap.fill(s,width) for s in str.split(LINESEPARATOR))
1788        #
1789
1790########## EMAIL attachements functions ###########################################################
1791
1792    def inline_part(self, part):
1793        """
1794        """
1795        self.logger.debug('function inline_part()')
1796
1797        return part.get_param('inline', None, 'Content-Disposition') == '' or not part.has_key('Content-Disposition')
1798
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()')
1805
1806        message_parts = list()
1807   
1808        ALTERNATIVE_MULTIPART = False
1809
1810        for part in msg.walk():
1811            content_maintype = part.get_content_maintype()
1812            content_type =  part.get_content_type()
1813
1814            self.logger.debug('\t Message part: Main-Type: %s' % content_maintype)
1815            self.logger.debug('\t Message part: Content-Type: %s' % content_type)
1816
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
1822
1823            ## Catch some mulitpart execptions
1824            #
1825            if content_type == 'multipart/alternative':
1826                ALTERNATIVE_MULTIPART = True
1827                continue
1828
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)
1838
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
1846
1847
1848            ## Save all non plain text message as attachment
1849            #
1850            if not content_type in ['text/plain']:
1851
1852                if self.parameters.debug:
1853                    s = '\t Filename: %s' % part.get_filename()
1854                    self.print_unicode(s)
1855
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()
1863
1864                message_parts.append((filename, part))
1865
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
1871
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)
1881
1882            ## Try to convert html message
1883            #
1884            if content_type == 'text/html':
1885
1886                body_text = self.html_2_txt(body_text)
1887                if not body_text:
1888                    continue
1889
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()
1892
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)
1898
1899            if self.parameters.strip_quotes:
1900                body_text = self.strip_quotes(body_text)
1901
1902            if self.parameters.inline_properties:
1903                body_text = self.inline_properties(body_text)
1904
1905            if self.parameters.use_textwrap:
1906                body_text = self.wrap_text(body_text)
1907
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'
1913
1914            try:
1915                ubody_text = unicode(body_text, charset)
1916
1917            except UnicodeError, detail:
1918                ubody_text = unicode(body_text, 'iso-8859-15')
1919
1920            except LookupError, detail:
1921                ubody_text = 'ERROR: Could not find charset: %s, please install' %(charset)
1922
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)
1927
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()
1939
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
1949
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)
1955
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'
1963
1964                filename = '%s%s' % (filename, ext)
1965
1966            ## Discard relative paths for windows/unix in attachment names
1967            #
1968            #filename = filename.replace('\\', '/').replace(':', '/')
1969            filename = filename.replace('\\', '_')
1970            filename = filename.replace('/', '_')
1971
1972            ## remove linefeed char
1973            #
1974            for forbidden_char in ['\r', '\n']:
1975                filename = filename.replace(forbidden_char,'')
1976
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
1986
1987            ## Make the filename unique for this ticket
1988            #
1989            num = 0
1990            unique_filename = filename
1991            dummy_filename, ext = os.path.splitext(filename)
1992
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)
2000
2001            attachment_names.add(unique_filename)
2002
2003            renamed_parts.append((filename, unique_filename, part))
2004   
2005        return renamed_parts
2006           
2007           
2008    def attachment_exists(self, filename):
2009
2010        if self.parameters.debug:
2011            s = 'attachment already exists: Id : %s, Filename : %s' %(self.id, filename)
2012            self.print_unicode(s)
2013
2014        ## We have no valid ticket id
2015        #
2016        if not self.id:
2017            return False
2018
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
2029
2030########## TRAC Ticket Text ###########################################################
2031           
2032    def get_body_text(self, message_parts):
2033        """
2034        """
2035        self.logger.debug('function get_body_text()')
2036
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
2047
2048            (original, filename, part) = part
2049            inline = self.inline_part(part)
2050
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("")
2070
2071        ## Convert list body_texts to string
2072        #
2073        body_text = '\r\n'.join(body_text)
2074        return body_text
2075
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
2085
2086        if not self.parameters.mailto_cc:
2087            self.parameters.mailto_cc = ''
2088
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               )
2096
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)
2101
2102        self.logger.debug("\tmailto link %s" %s)
2103        return s
2104
2105########## TRAC notify section ###########################################################
2106
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()')
2112
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  :
2122                print 'DRY_RUN: self.notify(tkt, True) reporter = %s' %tkt['reporter']
2123                return
2124        try:
2125
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)
2135
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'))
2141
2142            tn = TicketNotifyEmail(self.env)
2143
2144            if self.parameters.alternate_notify_template:
2145
2146                if self.VERSION >= 0.11:
2147
2148                    from trac.web.chrome import Chrome
2149
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
2154
2155                    tn.template = Chrome(tn.env).load_template(tn.template_name, method='text')
2156                       
2157                else:
2158
2159                    tn.template_name = self.parameters.alternate_notify_template
2160
2161            tn.notify(tkt, new, modtime)
2162
2163        except Exception, e:
2164            self.logger.error('Failure sending notification on creation of ticket #%s: %s' %(self.id, e))
2165
2166########## END Class Definition  ########################################################
2167
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)
2175
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
2186########## Parse Config File  ###########################################################
2187
2188def ReadConfig(file, name):
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)
2195
2196    config = trac_config.Configuration(file)
2197   
2198    parentdir = config.get('DEFAULT', 'parentdir')
2199    sections = config.sections()
2200
2201    ## use some trac internals to get the defaults
2202    #
2203    tmp = config.parser.defaults()
2204    project =  SaraDict()
2205
2206    for option, value in tmp.items():
2207        try:
2208            project[option] = int(value)
2209        except ValueError:
2210            project[option] = value
2211
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
2220
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)
2225
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)
2236
2237    return project
2238
2239########## Setup Logging ###############################################################
2240
2241def setup_log(parameters, project_name, interactive=None):
2242    """
2243    Setup loging
2244
2245    Note for log format the usage of `$(...)s` instead of `%(...)s` as the latter form
2246    would be interpreted by the ConfigParser itself.
2247    """
2248    logger = logging.getLogger('email2trac %s' %project_name)
2249
2250    if interactive:
2251        parameters.log_type = 'stderr'
2252
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'
2258
2259    if parameters.log_type == 'file':
2260
2261        if not parameters.log_file:
2262            parameters.log_file = 'email2trac.log'
2263
2264        if not os.path.isabs(parameters.log_file):
2265            parameters.log_file = os.path.join(tempfile.gettempdir(), parameters.log_file)
2266
2267        log_handler = logging.FileHandler(parameters.log_file)
2268
2269    elif parameters.log_type in ('winlog', 'eventlog', 'nteventlog'):
2270        ## Requires win32 extensions
2271        #
2272        log_handler = logging.handlers.NTEventLogHandler(logid, logtype='Application')
2273
2274    elif parameters.log_type in ('syslog', 'unix'):
2275        log_handler = logging.handlers.SysLogHandler('/dev/log')
2276
2277    elif parameters.log_type in ('stderr'):
2278        log_handler = logging.StreamHandler(sys.stderr)
2279
2280    else:
2281        log_handler = logging.handlers.BufferingHandler(0)
2282
2283    if parameters.log_format:
2284        parameters.log_format = parameters.log_format.replace('$(', '%(')
2285    else:
2286        parameters.log_format = '%(name)s: %(message)s'
2287
2288    log_formatter = logging.Formatter(parameters.log_format)
2289    log_handler.setFormatter(log_formatter)
2290    logger.addHandler(log_handler)
2291
2292    if (parameters.log_level in ['DEBUG', 'ALL']) or (parameters.debug > 0):
2293        logger.setLevel(logging.DEBUG)
2294        parameters.debug = 1
2295
2296    elif parameters.log_level in ['INFO'] or parameters.verbose:
2297        logger.setLevel(logging.INFO)
2298
2299    elif parameters.log_level in ['WARNING']:
2300        logger.setLevel(logging.WARNING)
2301
2302    elif parameters.log_level in ['ERROR']:
2303        logger.setLevel(logging.ERROR)
2304
2305    elif parameters.log_level in ['CRITICAL']:
2306        logger.setLevel(logging.CRITICAL)
2307
2308    else:
2309        logger.setLevel(logging.INFO)
2310
2311    return logger
2312
2313
2314if __name__ == '__main__':
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
2324
2325    SHORT_OPT = 'cdhf:np:t:v'
2326    LONG_OPT  =  ['component=', 'debug', 'dry-run', 'help', 'file=', 'project=', 'ticket_prefix=', 'verbose']
2327
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)
2356
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
2362
2363    if not settings.debug and debug_interactive:
2364        settings.debug = debug_interactive
2365
2366    if not settings.project:
2367        print __doc__
2368        print 'No Trac project is defined in the email2trac config file.'
2369        sys.exit(1)
2370
2371    logger = setup_log(settings, os.path.basename(settings.project), debug_interactive)
2372   
2373    if component:
2374        settings['component'] = component
2375
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'
2382
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
2393
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
2401
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
2412
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
2418
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
2429
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
2435
2436
2437        else:
2438            logger.error('TRAC version %s is not supported' %version)
2439            sys.exit(1)
2440
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
2446
2447        if settings.debug > 0:
2448            logger.debug('Loading environment %s', settings.project)
2449
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)
2455
2456        tktparser = TicketEmailParser(env, settings, logger, float(version))
2457        tktparser.parse(sys.stdin)
2458
2459    ## Catch all errors and use the logging module
2460    #
2461    except Exception, error:
2462
2463        etype, evalue, etb = sys.exc_info()
2464        for e in traceback.format_exception(etype, evalue, etb):
2465            logger.critical(e)
2466
2467        if m:
2468            tktparser.save_email_for_debug(m, True)
2469
2470        sys.exit(1)
2471# EOB
Note: See TracBrowser for help on using the repository browser.