source: emailtotracscript/trunk/email2trac.py.in @ 45

Last change on this file since 45 was 45, checked in by bas, 18 years ago

EmailtoTracScript?:

email2trac.py.in:

  • Added patches for UTF-8 encoding, Kilian Cavalotti
  • Added patches for known trac users (only trac version 0.9), Kilian Cavalotti
  • Rearranged some code
  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 16.6 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#
20"""
21emailfilter.py -- Email tickets to Trac.
22
23A simple MTA filter to create Trac tickets from inbound emails.
24
25Copyright 2005, Daniel Lundin <daniel@edgewall.com>
26Copyright 2005, Edgewall Software
27
28Changed By: Bas van der Vlies <basv@sara.nl>
29Date      : 13 September 2005
30Descr.    : Added config file and command line options, spam level
31            detection, reply address and mailto option. Unicode support
32
33Changed By: Walter de Jong <walter@sara.nl>
34Descr.    : multipart-message code and trac attachments
35
36
37The scripts reads emails from stdin and inserts directly into a Trac database.
38MIME headers are mapped as follows:
39
40        * From:      => Reporter
41                     => CC (Optional via reply_address option)
42        * Subject:   => Summary
43        * Body       => Description
44        * Component  => Can be set to SPAM via spam_level option
45
46How to use
47----------
48 * Create an config file:
49        [DEFAULT]                        # REQUIRED
50        project      : /data/trac/test   # REQUIRED
51        debug        : 1                 # OPTIONAL, if set print some DEBUG info
52        spam_level   : 4                 # OPTIONAL, if set check for SPAM mail
53        reply_address: 1                 # OPTIONAL, if set then fill in ticket CC field
54        umask        : 022               # OPTIONAL, if set then use this umask for creation of the attachments
55        mailto_link  : 1                 # OPTIONAL, if set then [mailto:<CC>] in description
56        trac_version : 0.8               # OPTIONAL, default is 0.9
57
58        [jouvin]                         # OPTIONAL project declaration, if set both fields necessary
59        project      : /data/trac/jouvin # use -p|--project jouvin. 
60       
61 * default config file is : /etc/email2trac.conf
62
63 * Commandline opions:
64                -h | --help
65                -c <value> | --component=<value>
66                -f <config file> | --file=<config file>
67                -p <project name> | --project=<project name>
68
69SVN Info:
70        $Id: email2trac.py.in 45 2006-01-30 18:16:27Z bas $
71"""
72
73import os
74import sys
75import string
76import getopt
77import stat
78import time
79import email
80import re
81import urllib
82import unicodedata
83import ConfigParser
84from email import Header
85from stat import *
86import mimetypes
87
88trac_default_version = 0.9
89
90class TicketEmailParser(object):
91        env = None
92        comment = '> '
93   
94        def __init__(self, env, parameters, version):
95                self.env = env
96
97                # Database connection
98                #
99                self.db = None
100
101                self.VERSION = version
102                if self.VERSION > 0.8:
103                        self.get_config = self.env.config.get
104                else:
105                        self.get_config = self.env.get_config
106
107                if parameters.has_key('umask'):
108                        os.umask(int(parameters['umask'], 8))
109
110                if parameters.has_key('debug'):
111                        self.DEBUG = int(parameters['debug'])
112                else:
113                        self.DEBUG = 0
114
115                if parameters.has_key('reply_address'):
116                        self.CC = int(parameters['reply_address'])
117                else:
118                        self.CC = 0
119
120                if parameters.has_key('mailto_link'):
121                        self.MAILTO = int(parameters['mailto_link'])
122                else:
123                        self.MAILTO = 0
124
125                if parameters.has_key('spam_level'):
126                        self.SPAM_LEVEL = int(parameters['spam_level'])
127                else:
128                        self.SPAM_LEVEL = 0
129
130                if parameters.has_key('email_comment'):
131                        self.comment = str(parameters['email_comment'])
132
133                if parameters.has_key('email_header'):
134                        self.EMAIL_HEADER = int(parameters['email_header'])
135                else:
136                        self.EMAIL_HEADER = 0
137
[42]138                if parameters.has_key('alternate_notify_template'):
139                        self.notify_template = str(parameters['alternate_notify_template'])
140                else:
141                        self.notify_template = None
[22]142
[43]143                if parameters.has_key('reply_all'):
144                        self.REPLY_ALL = int(parameters['reply_all'])
145                else:
146                        self.REPLY_ALL = 0
[42]147
[43]148
[22]149        # X-Spam-Score: *** (3.255) BAYES_50,DNS_FROM_AHBL_RHSBL,HTML_
150        # Note if Spam_level then '*' are included
151        def spam(self, message):
152                if message.has_key('X-Spam-Score'):
153                        spam_l = string.split(message['X-Spam-Score'])
154                        number = spam_l[0].count('*')
155
156                        if number >= self.SPAM_LEVEL:
[41]157                                return 'Spam'
[22]158
[41]159                return self.get_config('ticket', 'default_component')
[22]160
161        def to_unicode(self, str):
162                """
163                Email has 7 bit ASCII code, convert it to unicode with the charset
164                that is encoded in 7-bit ASCII code and encode it as utf-8 so TRAC
165                understands it.
166                """
167                results =  Header.decode_header(str)
168                str = None
169                for text,format in results:
170                        if format:
171                                try:
172                                        temp = unicode(text, format)
173                                except UnicodeError:
174                                        # This always works
175                                        #
176                                        temp = unicode(text, 'iso-8859-15')
177                                temp =  temp.encode('utf-8')
178                        else:
179                                temp = string.strip(text)
180
181                        if str:
182                                str = '%s %s' %(str, temp)
183                        else:
184                                str = temp
185
186                return str
187
188        def debug_attachments(self, message):
189                n = 0
190                for part in message.walk():
191                        if part.get_content_maintype() == 'multipart':      # multipart/* is just a container
192                                print 'TD: multipart container'
193                                continue
194
195                        n = n + 1
196                        print 'TD: part%d: Content-Type: %s' % (n, part.get_content_type())
197                        print 'TD: part%d: filename: %s' % (n, part.get_filename())
198
199                        if part.is_multipart():
200                                print 'TD: this part is multipart'
201                                payload = part.get_payload(decode=1)
202                                print 'TD: payload:', payload
203                        else:
204                                print 'TD: this part is not multipart'
205
206                        part_file = '/var/tmp/part%d' % n
207                        print 'TD: writing part%d (%s)' % (n,part_file)
208                        fx = open(part_file, 'wb')
209                        text = part.get_payload(decode=1)
210                        if not text:
211                                text = '(None)'
212                        fx.write(text)
213                        fx.close()
214                        try:
215                                os.chmod(part_file,S_IRWXU|S_IRWXG|S_IRWXO)
216                        except OSError:
217                                pass
218
219        def email_header_txt(self, m):
220#               if not m['Subject']:
221#                       subject = '(geen subject)'
222#               else:
223#                       subject = self.to_unicode(m['Subject'])
224#
225#               head = "'''Subject:''' %s [[BR]]" % subject
226#               if m['From'] and len(m['From']) > 0:
227#                       head = "%s'''From:''' %s [[BR]]" % (head, m['From'])
228#               if m['Date'] and len(m['Date']) > 0:
229#                       head = "%s'''Date:''' %s [[BR]]" %(head, m['Date'])
230
231                str = ''
232                if m['To'] and len(m['To']) > 0 and m['To'] != 'hic@sara.nl':
233                        str = "'''To:''' %s [[BR]]" %(m['To'])
234                if m['Cc'] and len(m['Cc']) > 0:
235                        str = "%s'''Cc:''' %s [[BR]]" % (str, m['Cc'])
236
237                return str
238
[43]239        def set_owner(self, ticket):
[45]240                """
241                Select default owner for ticket component
242                """
[43]243                cursor = self.db.cursor()
244                sql = "SELECT owner FROM component WHERE name='%s'" % ticket['component']
245                cursor.execute(sql)
246                ticket['owner'] = cursor.fetchone()[0]
247
248
249        def set_reply_fields(self, ticket, message):
[45]250                """
251                Bla Bla
252                """
[43]253                author, email_addr  = email.Utils.parseaddr(message['from'])
254                email_str = self.to_unicode(message['from'])
255
[45]256                # Look for email address in registered trac users
257                #
258                if self.VERSION > 0.8:
259                        users = [ u for (u, n, e) in self.env.get_known_users(self.db)
260                                if e == email_addr ]
261                else:
262                        users = []
[43]263
[45]264                if len(users) == 1:
265                        ticket['reporter'] = users[0]
266                else:
267                        ticket['reporter'] = email_str
268
269                # Put all CC-addresses in ticket CC field
[43]270                #
271                if self.REPLY_ALL:
[45]272                        #tos = message.get_all('to', [])
[43]273                        ccs = message.get_all('cc', [])
274
[45]275                        addrs = email.Utils.getaddresses(ccs)
[43]276
277                        # Remove reporter email address if notification is
278                        # on
279                        #
280                        if self.notification:
281                                try:
282                                        addrs.remove((author, email_addr))
283                                except ValueError, detail:
284                                        pass
285
[45]286                        for name,mail in addrs:
287                                        try:
288                                                ticket['cc'] = '%s,%s' %(ticket['cc'], mail)
289                                        except:
290                                                ticket['cc'] = mail
[43]291                return author, email_addr
292
[44]293        def save_email_for_debug(self, message):
294
295                msg_file = '/var/tmp/msg.txt'
296                print 'TD: saving email to %s' % msg_file
297                fx = open(msg_file, 'wb')
298                fx.write('%s' % message)
299                fx.close()
300                try:
301                        os.chmod(msg_file,S_IRWXU|S_IRWXG|S_IRWXO)
302                except OSError:
303                        pass
304
[22]305        def parse(self, fp):
306                msg = email.message_from_file(fp)
307                if not msg:
308                        return
309
310                if self.DEBUG > 1:        # save the entire e-mail message text
[44]311                        self.save_email_for_debug(msg)
[22]312
313                self.db = self.env.get_db_cnx()
[41]314                tkt = Ticket(self.env)
[22]315                tkt['status'] = 'new'
316
[43]317                if self.get_config('notification', 'smtp_enabled') in ['true']:
318                        self.notification = 1
319
[22]320                # Some defaults
321                #
322                tkt['milestone'] = self.get_config('ticket', 'default_milestone')
323                tkt['priority'] = self.get_config('ticket', 'default_priority')
324                tkt['severity'] = self.get_config('ticket', 'default_severity')
325                tkt['version'] = self.get_config('ticket', 'default_version')
326
327                if not msg['Subject']:
328                        tkt['summary'] = '(geen subject)'
329                else:
330                        tkt['summary'] = self.to_unicode(msg['Subject'])
331
332
[41]333                if settings.has_key('component'):
[22]334                        tkt['component'] = settings['component']
335                else:
[41]336                        tkt['component'] = self.spam(msg)
[22]337
[38]338                # Must make this an option or so, discard SPAM messages or save then
339                # and delete later
340                #
341                #if self.SPAM_LEVEL and self.spam(msg):
342                #       print 'This message is a SPAM. Automatic ticket insertion refused (SPAM level > %d' % self.SPAM_LEVEL
343                #       sys.exit(1)
344
[43]345                # Set default owner for component
[22]346                #
[43]347                self.set_owner(tkt)
348                author, email_addr = self.set_reply_fields(tkt, msg)
[22]349
[45]350                # produce e-mail like header
351                #
[22]352                head = ''
353                if self.EMAIL_HEADER > 0:
354                        head = self.email_header_txt(msg)
355
356                if self.DEBUG > 0:
357                        self.debug_attachments(msg)
358
[45]359                self.description(msg,tkt, head, author, email_addr)
360
361                # Insert ticket in database
362                #
363                if self.VERSION > 0.8:
364                        tkt['id'] = tkt.insert()
365                else:
366                        tkt['id'] = tkt.insert(self.db)
367
368                #
369                # Just how to show to update description
370                #
371                #tkt['description'] = '\n{{{\n\n Bas is op nieuw bezig\n\n }}}\n'
372                #tkt.save_changes(self.db, author, "Lekker bezig")
373                #
374
375                self.attachments(msg, tkt, author)
376                if self.notification:
377                        self.notify(tkt)
378
379
380        def description(self, msg, tkt, head, author, email):
381                """
382                put the message text in the ticket description
383                message text can be plain text or html or something else
384                """
[22]385                has_description = 0
386                for part in msg.walk():
[45]387
388                        # 'multipart/*' is a container for multipart messages
389                        #
390                        if part.get_content_maintype() == 'multipart':
[22]391                                continue
392
393                        if part.get_content_type() == 'text/plain':
[45]394                                # Try to decode, if fails then do not decode
395                                #
396                                body_text = part.get_payload(decode=1)         
397                                if not body_text:                       
398                                        body_text = part.get_payload(decode=0) 
[22]399
[45]400                                # Get contents charset (iso-8859-15 if not defined in mail headers)
401                                # UTF-8 encode body_text
402                                #
403                                charset = msg.get_content_charset('iso-8859-15')
404                                ubody_text = unicode(body_text, charset).encode('utf-8')
[22]405
[45]406                                tkt['description'] = '\n{{{\n\n%s\n}}}\n' %(ubody_text)
407
[22]408                        elif part.get_content_type() == 'text/html':
[45]409                                tkt['description'] = '%s\n\n(see attachment for HTML mail message)\n' \
410                                        %(head)
[22]411                                body_text = tkt['description']
412
413                        else:
[45]414                                tkt['description'] = '%s\n\n(see attachment for message)\n' %(head)
[22]415                                body_text = tkt['description']
416
417                        has_description = 1
418                        break           # we have the description, so break
419
420                if not has_description:
421                        tkt['description'] = '%s\n\n(no plain text message, see attachments)' % head
422                        has_description = 1
423
424                if self.MAILTO:
[45]425                        mailto = self.html_mailto_link(author, email, self.to_unicode(msg['subject']), ubody_text)
[22]426                        tkt['description'] = '%s\n%s %s' %(head, mailto, tkt['description'])
427
428
[41]429        def notify(self, tkt):
430                try:
431                        # create false {abs_}href properties, to trick Notify()
432                        #
433                        self.env.abs_href = Href(self.get_config('project', 'url'))
434                        self.env.href = Href(self.get_config('project', 'url'))
[22]435
[41]436                        tn = TicketNotifyEmail(self.env)
[42]437                        if self.notify_template:
438                                tn.template_name = self.notify_template;
439
[41]440                        tn.notify(tkt, newticket=True)
441
442                except Exception, e:
[42]443                        print 'TD: Failure sending notification on creation of ticket #%s: %s' \
444                                % (tkt['id'], e)
[41]445
[22]446        def mail_line(self, str):
447                return '%s %s' % (self.comment, str)
448
449
450        def html_mailto_link(self, author, mail_addr, subject, body):
451                if not author:
452                        author = mail_addr
453                else:   
454                        author = self.to_unicode(author)
455
456                # Must find a fix
457                #
458                #arr = string.split(body, '\n')
459                #arr = map(self.mail_line, arr)
460                #body = string.join(arr, '\n')
461                #body = '%s wrote:\n%s' %(author, body)
462
463                # Temporary fix
464                body = '> Type your reply'
[45]465                str = 'mailto:%s?subject=%s&body=%s' %(urllib.quote(mail_addr), urllib.quote('Re: %s' % subject), urllib.quote(body))
[22]466                str = '\n{{{\n#!html\n<a href="%s">Reply to: %s</a>\n}}}\n' %(str, author)
467
468                return str
469
470        def attachments(self, message, ticket, user):
471                '''save any attachments as file in the ticket's directory'''
472
473                count = 0
474                first = 0
475                for part in message.walk():
476                        if part.get_content_maintype() == 'multipart':          # multipart/* is just a container
477                                continue
478
479                        if not first:                                                                           # first content is the message
480                                first = 1
481                                if part.get_content_type() == 'text/plain':             # if first is text, is was already put in the description
482                                        continue
483
484                        filename = part.get_filename()
485                        if not filename:
486                                count = count + 1
487                                filename = 'part%04d' % count
488
489                                ext = mimetypes.guess_extension(part.get_type())
490                                if not ext:
491                                        ext = '.bin'
492
493                                filename = '%s%s' % (filename, ext)
494                        else:
495                                filename = self.to_unicode(filename)
496
497                        if '/' in filename:
498                                filename = os.path.basename(filename)
499
500                        url_filename = urllib.quote(filename)
501
502                        if self.VERSION > 0.8:
503                                tmpfile = '/tmp/email2trac-ticket%sattachment' % str(ticket['id'])
504                        else:
505                                dir = os.path.join(self.env.get_attachments_dir(), 'ticket',
506                                                        urllib.quote(str(ticket['id'])))
507                                if not os.path.exists(dir):
508                                        mkdir_p(dir, 0755)
509
510                                tmpfile = os.path.join(dir, url_filename)
511
512                        f = open(tmpfile, 'wb')
513                        text = part.get_payload(decode=1)
514                        if not text:
515                                text = '(None)'
516                        f.write(text)
517
518                        # get the filesize
519                        #
520                        stats = os.lstat(tmpfile)
521                        filesize = stats[stat.ST_SIZE]
522
523                        # Insert the attachment it differs for the different TRAC versions
524                        #
525                        if self.VERSION > 0.8:
526                                att = attachment.Attachment(self.env,'ticket',ticket['id'])
527                                att.insert(url_filename,f,filesize)
528                                f.close()
529                        else:
530                                cursor = self.db.cursor()
531                                cursor.execute('INSERT INTO attachment VALUES("%s","%s","%s",%d,%d,"%s","%s","%s")'
532                                        %('ticket', urllib.quote(str(ticket['id'])), filename + '?format=raw', filesize,
533                                          int(time.time()),'', user, 'e-mail') )
534                                self.db.commit()
535
536
537def mkdir_p(dir, mode):
538        '''do a mkdir -p'''
539
540        arr = string.split(dir, '/')
541        path = ''
542        for part in arr:
543                path = '%s/%s' % (path, part)
544                try:
545                        stats = os.stat(path)
546                except OSError:
547                        os.mkdir(path, mode)
548
549
550def ReadConfig(file, name):
551        """
552        Parse the config file
553        """
554
555        if not os.path.isfile(file):
556                print 'File %s does not exists' %file
557                sys.exit(1)
558
559        config = ConfigParser.ConfigParser()
560        try:
561                config.read(file)
562        except ConfigParser.MissingSectionHeaderError,detail:
563                print detail
564                sys.exit(1)
565
566
567        # Use given project name else use defaults
568        #
569        if name:
570                if not config.has_section(name):
571                        print "Not an valid project name: %s" %name
572                        print "Valid names: %s" %config.sections()
573                        sys.exit(1)
574
575                project =  dict()
576                for option in  config.options(name):
577                        project[option] = config.get(name, option)
578
579        else:
580                project = config.defaults()
581
582        return project
583
584if __name__ == '__main__':
585        # Default config file
586        #
[24]587        configfile = '@email2trac_conf@'
[22]588        project = ''
589        component = ''
590       
591        try:
592                opts, args = getopt.getopt(sys.argv[1:], 'chf:p:', ['component=','help', 'file=', 'project='])
593        except getopt.error,detail:
594                print __doc__
595                print detail
596                sys.exit(1)
597
598        project_name = None
599        for opt,value in opts:
600                if opt in [ '-h', '--help']:
601                        print __doc__
602                        sys.exit(0)
603                elif opt in ['-c', '--component']:
604                        component = value
605                elif opt in ['-f', '--file']:
606                        configfile = value
607                elif opt in ['-p', '--project']:
608                        project_name = value
609
610        settings = ReadConfig(configfile, project_name)
611        if not settings.has_key('project'):
612                print __doc__
613                print 'No project defined in config file, eg:\n\t project: /data/trac/bas'
614                sys.exit(1)
615
616        if component:
617                settings['component'] = component
618
619        if settings.has_key('trac_version'):
620                version = float(settings['trac_version'])
621        else:
622                version = trac_default_version
623
624        #debug HvB
625        #print settings
626
627        if version > 0.8:
[41]628                from trac import attachment
629                from trac.env import Environment
630                from trac.ticket import Ticket
631                from trac.Notify import TicketNotifyEmail
632                from trac.web.href import Href
[22]633        else:
[41]634                from trac.Environment import Environment
635                from trac.Ticket import Ticket
636                from trac.Notify import TicketNotifyEmail
637                from trac.Href import Href
638
639        env = Environment(settings['project'], create=0)
[22]640        tktparser = TicketEmailParser(env, settings, version)
641        tktparser.parse(sys.stdin)
642
643# EOB
Note: See TracBrowser for help on using the repository browser.