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

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

EmailtoTracScript?:

email2trac.pyt.in, email2trac.conf:

  • Added reply_all option for ticket cc-field
  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 16.1 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"""
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 43 2006-01-26 12:48:25Z 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
138                if parameters.has_key('alternate_notify_template'):
139                        self.notify_template = str(parameters['alternate_notify_template'])
140                else:
141                        self.notify_template = None
142
143                if parameters.has_key('reply_all'):
144                        self.REPLY_ALL = int(parameters['reply_all'])
145                else:
146                        self.REPLY_ALL = 0
147
148
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:
157                                return 'Spam'
158
159                return self.get_config('ticket', 'default_component')
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
239        def set_owner(self, ticket):
240                cursor = self.db.cursor()
241                sql = "SELECT owner FROM component WHERE name='%s'" % ticket['component']
242                cursor.execute(sql)
243                ticket['owner'] = cursor.fetchone()[0]
244
245
246        def set_reply_fields(self, ticket, message):
247                author, email_addr  = email.Utils.parseaddr(message['from'])
248                email_str = self.to_unicode(message['from'])
249
250                ticket['reporter'] = email_str
251                ticket['cc'] = None
252
253                # Put all addresses in ticket CC field
254                #
255                if self.REPLY_ALL:
256                        tos = message.get_all('to', [])
257                        ccs = message.get_all('cc', [])
258
259                        addrs = email.Utils.getaddresses(tos + ccs)
260
261                        # Remove reporter email address if notification is
262                        # on
263                        #
264                        if self.notification:
265                                try:
266                                        addrs.remove((author, email_addr))
267                                except ValueError, detail:
268                                        pass
269
270                        for a,m in addrs:
271                                        if not ticket['cc']:
272                                                ticket['cc'] = m
273                                        else:
274                                                ticket['cc'] = '%s,%s' %(ticket['cc'], m)
275
276                # Put authors email addess in CC field only if notification is off
277                #
278                elif self.CC and not self.notification:
279                        ticket['cc'] = email_str
280
281                return author, email_addr
282
283        def parse(self, fp):
284                msg = email.message_from_file(fp)
285                if not msg:
286                        return
287
288                if self.DEBUG > 1:        # save the entire e-mail message text
289                        msg_file = '/var/tmp/msg.txt'
290                        print 'TD: saving email to %s' % msg_file
291                        fx = open(msg_file, 'wb')
292                        fx.write('%s' % msg)
293                        fx.close()
294                        try:
295                                os.chmod(msg_file,S_IRWXU|S_IRWXG|S_IRWXO)
296                        except OSError:
297                                pass
298
299                self.db = self.env.get_db_cnx()
300                tkt = Ticket(self.env)
301                tkt['status'] = 'new'
302
303                if self.get_config('notification', 'smtp_enabled') in ['true']:
304                        self.notification = 1
305
306                # Some defaults
307                #
308                tkt['milestone'] = self.get_config('ticket', 'default_milestone')
309                tkt['priority'] = self.get_config('ticket', 'default_priority')
310                tkt['severity'] = self.get_config('ticket', 'default_severity')
311                tkt['version'] = self.get_config('ticket', 'default_version')
312
313                if not msg['Subject']:
314                        tkt['summary'] = '(geen subject)'
315                else:
316                        tkt['summary'] = self.to_unicode(msg['Subject'])
317
318
319                if settings.has_key('component'):
320                        tkt['component'] = settings['component']
321                else:
322                        tkt['component'] = self.spam(msg)
323
324                # Must make this an option or so, discard SPAM messages or save then
325                # and delete later
326                #
327                #if self.SPAM_LEVEL and self.spam(msg):
328                #       print 'This message is a SPAM. Automatic ticket insertion refused (SPAM level > %d' % self.SPAM_LEVEL
329                #       sys.exit(1)
330
331                # Set default owner for component
332                #
333                self.set_owner(tkt)
334                author, email_addr = self.set_reply_fields(tkt, msg)
335
336# produce e-mail like header
337                head = ''
338                if self.EMAIL_HEADER > 0:
339                        head = self.email_header_txt(msg)
340
341                if self.DEBUG > 0:
342                        self.debug_attachments(msg)
343
344#
345#       put the message text in the ticket description
346#       message text can be plain text or html or something else
347#
348                has_description = 0
349                for part in msg.walk():
350                        if part.get_content_maintype() == 'multipart':                  # 'multipart/*' is a container for multipart messages
351                                continue
352
353                        if part.get_content_type() == 'text/plain':
354                                body_text = part.get_payload(decode=1)                  # try to decode
355                                if not body_text:                                       # decode failed
356                                        body_text = part.get_payload(decode=0)          # do not decode
357
358                                tkt['description'] = '\n{{{\n\n%s\n}}}\n' % body_text
359
360                        elif part.get_content_type() == 'text/html':
361                                tkt['description'] = '%s\n\n(see attachment for HTML mail message)\n' % head
362                                body_text = tkt['description']
363
364                        else:
365                                tkt['description'] = '%s\n\n(see attachment for message)\n' % head
366                                body_text = tkt['description']
367
368                        has_description = 1
369                        break           # we have the description, so break
370
371                if not has_description:
372                        tkt['description'] = '%s\n\n(no plain text message, see attachments)' % head
373                        has_description = 1
374
375                if self.MAILTO:
376                        mailto = self.html_mailto_link(author, email_addr, self.to_unicode(msg['subject']), body_text)
377                        tkt['description'] = '%s\n%s %s' %(head, mailto, tkt['description'])
378
379                if self.VERSION > 0.8:
380                        tkt['id'] = tkt.insert()
381                else:
382                        tkt['id'] = tkt.insert(self.db)
383
384                #
385                # Just how to show to update description
386                #
387                #tkt['description'] = '\n{{{\n\n Bas is op nieuw bezig\n\n }}}\n'
388                #tkt.save_changes(self.db, author, "Lekker bezig")
389                #
390
391                self.attachments(msg, tkt, author)
392                if self.notification:
393                        self.notify(tkt)
394
395        def notify(self, tkt):
396                try:
397                        # create false {abs_}href properties, to trick Notify()
398                        #
399                        self.env.abs_href = Href(self.get_config('project', 'url'))
400                        self.env.href = Href(self.get_config('project', 'url'))
401
402                        tn = TicketNotifyEmail(self.env)
403                        if self.notify_template:
404                                tn.template_name = self.notify_template;
405
406                        tn.notify(tkt, newticket=True)
407
408                except Exception, e:
409                        print 'TD: Failure sending notification on creation of ticket #%s: %s' \
410                                % (tkt['id'], e)
411
412        def mail_line(self, str):
413                return '%s %s' % (self.comment, str)
414
415
416        def html_mailto_link(self, author, mail_addr, subject, body):
417                if not author:
418                        author = mail_addr
419                else:   
420                        author = self.to_unicode(author)
421
422                # Must find a fix
423                #
424                #arr = string.split(body, '\n')
425                #arr = map(self.mail_line, arr)
426
427                #body = string.join(arr, '\n')
428                #body = '%s wrote:\n%s' %(author, body)
429
430                # Obsolete for reference
431                #
432                #body = self.to_unicode(body)
433                #body = urllib.quote(body)
434                #body = Header.encode(body)
435                #
436
437                # Temporary fix
438                body = '> Type your reply'
439                str = 'mailto:%s?subject=%s&body=%s' % (urllib.quote(mail_addr), urllib.quote('Re: %s' % subject), urllib.quote(body))
440                str = '\n{{{\n#!html\n<a href="%s">Reply to: %s</a>\n}}}\n' %(str, author)
441
442                return str
443
444        def attachments(self, message, ticket, user):
445                '''save any attachments as file in the ticket's directory'''
446
447                count = 0
448                first = 0
449                for part in message.walk():
450                        if part.get_content_maintype() == 'multipart':          # multipart/* is just a container
451                                continue
452
453                        if not first:                                                                           # first content is the message
454                                first = 1
455                                if part.get_content_type() == 'text/plain':             # if first is text, is was already put in the description
456                                        continue
457
458                        filename = part.get_filename()
459                        if not filename:
460                                count = count + 1
461                                filename = 'part%04d' % count
462
463                                ext = mimetypes.guess_extension(part.get_type())
464                                if not ext:
465                                        ext = '.bin'
466
467                                filename = '%s%s' % (filename, ext)
468                        else:
469                                filename = self.to_unicode(filename)
470
471                        if '/' in filename:
472                                filename = os.path.basename(filename)
473
474                        url_filename = urllib.quote(filename)
475
476                        if self.VERSION > 0.8:
477                                tmpfile = '/tmp/email2trac-ticket%sattachment' % str(ticket['id'])
478                        else:
479                                dir = os.path.join(self.env.get_attachments_dir(), 'ticket',
480                                                        urllib.quote(str(ticket['id'])))
481                                if not os.path.exists(dir):
482                                        mkdir_p(dir, 0755)
483
484                                tmpfile = os.path.join(dir, url_filename)
485
486                        f = open(tmpfile, 'wb')
487                        text = part.get_payload(decode=1)
488                        if not text:
489                                text = '(None)'
490                        f.write(text)
491
492                        # get the filesize
493                        #
494                        stats = os.lstat(tmpfile)
495                        filesize = stats[stat.ST_SIZE]
496
497                        # Insert the attachment it differs for the different TRAC versions
498                        #
499                        if self.VERSION > 0.8:
500                                att = attachment.Attachment(self.env,'ticket',ticket['id'])
501                                att.insert(url_filename,f,filesize)
502                                f.close()
503                        else:
504                                cursor = self.db.cursor()
505                                cursor.execute('INSERT INTO attachment VALUES("%s","%s","%s",%d,%d,"%s","%s","%s")'
506                                        %('ticket', urllib.quote(str(ticket['id'])), filename + '?format=raw', filesize,
507                                          int(time.time()),'', user, 'e-mail') )
508                                self.db.commit()
509
510
511def mkdir_p(dir, mode):
512        '''do a mkdir -p'''
513
514        arr = string.split(dir, '/')
515        path = ''
516        for part in arr:
517                path = '%s/%s' % (path, part)
518                try:
519                        stats = os.stat(path)
520                except OSError:
521                        os.mkdir(path, mode)
522
523
524def ReadConfig(file, name):
525        """
526        Parse the config file
527        """
528
529        if not os.path.isfile(file):
530                print 'File %s does not exists' %file
531                sys.exit(1)
532
533        config = ConfigParser.ConfigParser()
534        try:
535                config.read(file)
536        except ConfigParser.MissingSectionHeaderError,detail:
537                print detail
538                sys.exit(1)
539
540
541        # Use given project name else use defaults
542        #
543        if name:
544                if not config.has_section(name):
545                        print "Not an valid project name: %s" %name
546                        print "Valid names: %s" %config.sections()
547                        sys.exit(1)
548
549                project =  dict()
550                for option in  config.options(name):
551                        project[option] = config.get(name, option)
552
553        else:
554                project = config.defaults()
555
556        return project
557
558if __name__ == '__main__':
559        # Default config file
560        #
561        configfile = '@email2trac_conf@'
562        project = ''
563        component = ''
564       
565        try:
566                opts, args = getopt.getopt(sys.argv[1:], 'chf:p:', ['component=','help', 'file=', 'project='])
567        except getopt.error,detail:
568                print __doc__
569                print detail
570                sys.exit(1)
571
572        project_name = None
573        for opt,value in opts:
574                if opt in [ '-h', '--help']:
575                        print __doc__
576                        sys.exit(0)
577                elif opt in ['-c', '--component']:
578                        component = value
579                elif opt in ['-f', '--file']:
580                        configfile = value
581                elif opt in ['-p', '--project']:
582                        project_name = value
583
584        settings = ReadConfig(configfile, project_name)
585        if not settings.has_key('project'):
586                print __doc__
587                print 'No project defined in config file, eg:\n\t project: /data/trac/bas'
588                sys.exit(1)
589
590        if component:
591                settings['component'] = component
592
593        if settings.has_key('trac_version'):
594                version = float(settings['trac_version'])
595        else:
596                version = trac_default_version
597
598        #debug HvB
599        #print settings
600
601        if version > 0.8:
602                from trac import attachment
603                from trac.env import Environment
604                from trac.ticket import Ticket
605                from trac.Notify import TicketNotifyEmail
606                from trac.web.href import Href
607        else:
608                from trac.Environment import Environment
609                from trac.Ticket import Ticket
610                from trac.Notify import TicketNotifyEmail
611                from trac.Href import Href
612
613        env = Environment(settings['project'], create=0)
614        tktparser = TicketEmailParser(env, settings, version)
615        tktparser.parse(sys.stdin)
616
617# EOB
Note: See TracBrowser for help on using the repository browser.