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

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

EmailtoTracScript?:

email2trac.py.in, email2trac.conf:

  • Added alternate_notify_template


  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 15.2 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 42 2006-01-25 17:03:57Z 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
144        # X-Spam-Score: *** (3.255) BAYES_50,DNS_FROM_AHBL_RHSBL,HTML_
145        # Note if Spam_level then '*' are included
146        def spam(self, message):
147                if message.has_key('X-Spam-Score'):
148                        spam_l = string.split(message['X-Spam-Score'])
149                        number = spam_l[0].count('*')
150
151                        if number >= self.SPAM_LEVEL:
152                                return 'Spam'
153
154                return self.get_config('ticket', 'default_component')
155
156        def to_unicode(self, str):
157                """
158                Email has 7 bit ASCII code, convert it to unicode with the charset
159                that is encoded in 7-bit ASCII code and encode it as utf-8 so TRAC
160                understands it.
161                """
162                results =  Header.decode_header(str)
163                str = None
164                for text,format in results:
165                        if format:
166                                try:
167                                        temp = unicode(text, format)
168                                except UnicodeError:
169                                        # This always works
170                                        #
171                                        temp = unicode(text, 'iso-8859-15')
172                                temp =  temp.encode('utf-8')
173                        else:
174                                temp = string.strip(text)
175
176                        if str:
177                                str = '%s %s' %(str, temp)
178                        else:
179                                str = temp
180
181                return str
182
183        def debug_attachments(self, message):
184                n = 0
185                for part in message.walk():
186                        if part.get_content_maintype() == 'multipart':      # multipart/* is just a container
187                                print 'TD: multipart container'
188                                continue
189
190                        n = n + 1
191                        print 'TD: part%d: Content-Type: %s' % (n, part.get_content_type())
192                        print 'TD: part%d: filename: %s' % (n, part.get_filename())
193
194                        if part.is_multipart():
195                                print 'TD: this part is multipart'
196                                payload = part.get_payload(decode=1)
197                                print 'TD: payload:', payload
198                        else:
199                                print 'TD: this part is not multipart'
200
201                        part_file = '/var/tmp/part%d' % n
202                        print 'TD: writing part%d (%s)' % (n,part_file)
203                        fx = open(part_file, 'wb')
204                        text = part.get_payload(decode=1)
205                        if not text:
206                                text = '(None)'
207                        fx.write(text)
208                        fx.close()
209                        try:
210                                os.chmod(part_file,S_IRWXU|S_IRWXG|S_IRWXO)
211                        except OSError:
212                                pass
213
214        def email_header_txt(self, m):
215#               if not m['Subject']:
216#                       subject = '(geen subject)'
217#               else:
218#                       subject = self.to_unicode(m['Subject'])
219#
220#               head = "'''Subject:''' %s [[BR]]" % subject
221#               if m['From'] and len(m['From']) > 0:
222#                       head = "%s'''From:''' %s [[BR]]" % (head, m['From'])
223#               if m['Date'] and len(m['Date']) > 0:
224#                       head = "%s'''Date:''' %s [[BR]]" %(head, m['Date'])
225
226                str = ''
227                if m['To'] and len(m['To']) > 0 and m['To'] != 'hic@sara.nl':
228                        str = "'''To:''' %s [[BR]]" %(m['To'])
229                if m['Cc'] and len(m['Cc']) > 0:
230                        str = "%s'''Cc:''' %s [[BR]]" % (str, m['Cc'])
231
232                return str
233
234        def parse(self, fp):
235                msg = email.message_from_file(fp)
236                if not msg:
237                        return
238
239                if self.DEBUG > 1:        # save the entire e-mail message text
240                        msg_file = '/var/tmp/msg.txt'
241                        print 'TD: saving email to %s' % msg_file
242                        fx = open(msg_file, 'wb')
243                        fx.write('%s' % msg)
244                        fx.close()
245                        try:
246                                os.chmod(msg_file,S_IRWXU|S_IRWXG|S_IRWXO)
247                        except OSError:
248                                pass
249
250                self.db = self.env.get_db_cnx()
251                tkt = Ticket(self.env)
252                tkt['status'] = 'new'
253
254                # Some defaults
255                #
256                tkt['milestone'] = self.get_config('ticket', 'default_milestone')
257                tkt['priority'] = self.get_config('ticket', 'default_priority')
258                tkt['severity'] = self.get_config('ticket', 'default_severity')
259                tkt['version'] = self.get_config('ticket', 'default_version')
260
261                if not msg['Subject']:
262                        tkt['summary'] = '(geen subject)'
263                else:
264                        tkt['summary'] = self.to_unicode(msg['Subject'])
265
266
267                if settings.has_key('component'):
268                        tkt['component'] = settings['component']
269                else:
270                        tkt['component'] = self.spam(msg)
271
272                # Must make this an option or so, discard SPAM messages or save then
273                # and delete later
274                #
275                #if self.SPAM_LEVEL and self.spam(msg):
276                #       print 'This message is a SPAM. Automatic ticket insertion refused (SPAM level > %d' % self.SPAM_LEVEL
277                #       sys.exit(1)
278
279                # Get default owner for component
280                #
281                cursor = self.db.cursor()
282                sql = "SELECT owner FROM component WHERE name='%s'" % tkt['component']
283                cursor.execute(sql)
284                tkt['owner'] = cursor.fetchone()[0]
285
286                author, email_addr  = email.Utils.parseaddr(msg['from'])
287                email_str = self.to_unicode(msg['from'])
288                if author:
289                        tkt['reporter'] = email_str
290                else:
291                        tkt['reporter'] = email_str
292
293                if self.CC:
294                        tkt['cc'] = email_str
295
296# produce e-mail like header
297                head = ''
298                if self.EMAIL_HEADER > 0:
299                        head = self.email_header_txt(msg)
300
301                if self.DEBUG > 0:
302                        self.debug_attachments(msg)
303
304#
305#       put the message text in the ticket description
306#       message text can be plain text or html or something else
307#
308                has_description = 0
309                for part in msg.walk():
310                        if part.get_content_maintype() == 'multipart':                  # 'multipart/*' is a container for multipart messages
311                                continue
312
313                        if part.get_content_type() == 'text/plain':
314                                body_text = part.get_payload(decode=1)                  # try to decode
315                                if not body_text:                                       # decode failed
316                                        body_text = part.get_payload(decode=0)          # do not decode
317
318                                tkt['description'] = '\n{{{\n\n%s\n}}}\n' % body_text
319
320                        elif part.get_content_type() == 'text/html':
321                                tkt['description'] = '%s\n\n(see attachment for HTML mail message)\n' % head
322                                body_text = tkt['description']
323
324                        else:
325                                tkt['description'] = '%s\n\n(see attachment for message)\n' % head
326                                body_text = tkt['description']
327
328                        has_description = 1
329                        break           # we have the description, so break
330
331                if not has_description:
332                        tkt['description'] = '%s\n\n(no plain text message, see attachments)' % head
333                        has_description = 1
334
335                if self.MAILTO:
336                        mailto = self.html_mailto_link(author, email_addr, self.to_unicode(msg['subject']), body_text)
337                        tkt['description'] = '%s\n%s %s' %(head, mailto, tkt['description'])
338
339                if self.VERSION > 0.8:
340                        tkt['id'] = tkt.insert()
341                else:
342                        tkt['id'] = tkt.insert(self.db)
343
344                #
345                # Just how to show to update description
346                #
347                #tkt['description'] = '\n{{{\n\n Bas is op nieuw bezig\n\n }}}\n'
348                #tkt.save_changes(self.db, author, "Lekker bezig")
349                #
350
351                self.attachments(msg, tkt, author)
352                if self.get_config('notification', 'smtp_enabled') in ['true']:
353                        self.notify(tkt)
354
355        def notify(self, tkt):
356                try:
357                        # create false {abs_}href properties, to trick Notify()
358                        #
359                        self.env.abs_href = Href(self.get_config('project', 'url'))
360                        self.env.href = Href(self.get_config('project', 'url'))
361
362                        tn = TicketNotifyEmail(self.env)
363                        if self.notify_template:
364                                tn.template_name = self.notify_template;
365
366                        tn.notify(tkt, newticket=True)
367
368                except Exception, e:
369                        print 'TD: Failure sending notification on creation of ticket #%s: %s' \
370                                % (tkt['id'], e)
371
372        def mail_line(self, str):
373                return '%s %s' % (self.comment, str)
374
375
376        def html_mailto_link(self, author, mail_addr, subject, body):
377                if not author:
378                        author = mail_addr
379                else:   
380                        author = self.to_unicode(author)
381
382                # Must find a fix
383                #
384                #arr = string.split(body, '\n')
385                #arr = map(self.mail_line, arr)
386
387                #body = string.join(arr, '\n')
388                #body = '%s wrote:\n%s' %(author, body)
389
390                # Obsolete for reference
391                #
392                #body = self.to_unicode(body)
393                #body = urllib.quote(body)
394                #body = Header.encode(body)
395                #
396
397                # Temporary fix
398                body = '> Type your reply'
399                str = 'mailto:%s?subject=%s&body=%s' % (urllib.quote(mail_addr), urllib.quote('Re: %s' % subject), urllib.quote(body))
400                str = '\n{{{\n#!html\n<a href="%s">Reply to: %s</a>\n}}}\n' %(str, author)
401
402                return str
403
404        def attachments(self, message, ticket, user):
405                '''save any attachments as file in the ticket's directory'''
406
407                count = 0
408                first = 0
409                for part in message.walk():
410                        if part.get_content_maintype() == 'multipart':          # multipart/* is just a container
411                                continue
412
413                        if not first:                                                                           # first content is the message
414                                first = 1
415                                if part.get_content_type() == 'text/plain':             # if first is text, is was already put in the description
416                                        continue
417
418                        filename = part.get_filename()
419                        if not filename:
420                                count = count + 1
421                                filename = 'part%04d' % count
422
423                                ext = mimetypes.guess_extension(part.get_type())
424                                if not ext:
425                                        ext = '.bin'
426
427                                filename = '%s%s' % (filename, ext)
428                        else:
429                                filename = self.to_unicode(filename)
430
431                        if '/' in filename:
432                                filename = os.path.basename(filename)
433
434                        url_filename = urllib.quote(filename)
435
436                        if self.VERSION > 0.8:
437                                tmpfile = '/tmp/email2trac-ticket%sattachment' % str(ticket['id'])
438                        else:
439                                dir = os.path.join(self.env.get_attachments_dir(), 'ticket',
440                                                        urllib.quote(str(ticket['id'])))
441                                if not os.path.exists(dir):
442                                        mkdir_p(dir, 0755)
443
444                                tmpfile = os.path.join(dir, url_filename)
445
446                        f = open(tmpfile, 'wb')
447                        text = part.get_payload(decode=1)
448                        if not text:
449                                text = '(None)'
450                        f.write(text)
451
452                        # get the filesize
453                        #
454                        stats = os.lstat(tmpfile)
455                        filesize = stats[stat.ST_SIZE]
456
457                        # Insert the attachment it differs for the different TRAC versions
458                        #
459                        if self.VERSION > 0.8:
460                                att = attachment.Attachment(self.env,'ticket',ticket['id'])
461                                att.insert(url_filename,f,filesize)
462                                f.close()
463                        else:
464                                cursor = self.db.cursor()
465                                cursor.execute('INSERT INTO attachment VALUES("%s","%s","%s",%d,%d,"%s","%s","%s")'
466                                        %('ticket', urllib.quote(str(ticket['id'])), filename + '?format=raw', filesize,
467                                          int(time.time()),'', user, 'e-mail') )
468                                self.db.commit()
469
470
471def mkdir_p(dir, mode):
472        '''do a mkdir -p'''
473
474        arr = string.split(dir, '/')
475        path = ''
476        for part in arr:
477                path = '%s/%s' % (path, part)
478                try:
479                        stats = os.stat(path)
480                except OSError:
481                        os.mkdir(path, mode)
482
483
484def ReadConfig(file, name):
485        """
486        Parse the config file
487        """
488
489        if not os.path.isfile(file):
490                print 'File %s does not exists' %file
491                sys.exit(1)
492
493        config = ConfigParser.ConfigParser()
494        try:
495                config.read(file)
496        except ConfigParser.MissingSectionHeaderError,detail:
497                print detail
498                sys.exit(1)
499
500
501        # Use given project name else use defaults
502        #
503        if name:
504                if not config.has_section(name):
505                        print "Not an valid project name: %s" %name
506                        print "Valid names: %s" %config.sections()
507                        sys.exit(1)
508
509                project =  dict()
510                for option in  config.options(name):
511                        project[option] = config.get(name, option)
512
513        else:
514                project = config.defaults()
515
516        return project
517
518if __name__ == '__main__':
519        # Default config file
520        #
521        configfile = '@email2trac_conf@'
522        project = ''
523        component = ''
524       
525        try:
526                opts, args = getopt.getopt(sys.argv[1:], 'chf:p:', ['component=','help', 'file=', 'project='])
527        except getopt.error,detail:
528                print __doc__
529                print detail
530                sys.exit(1)
531
532        project_name = None
533        for opt,value in opts:
534                if opt in [ '-h', '--help']:
535                        print __doc__
536                        sys.exit(0)
537                elif opt in ['-c', '--component']:
538                        component = value
539                elif opt in ['-f', '--file']:
540                        configfile = value
541                elif opt in ['-p', '--project']:
542                        project_name = value
543
544        settings = ReadConfig(configfile, project_name)
545        if not settings.has_key('project'):
546                print __doc__
547                print 'No project defined in config file, eg:\n\t project: /data/trac/bas'
548                sys.exit(1)
549
550        if component:
551                settings['component'] = component
552
553        if settings.has_key('trac_version'):
554                version = float(settings['trac_version'])
555        else:
556                version = trac_default_version
557
558        #debug HvB
559        #print settings
560
561        if version > 0.8:
562                from trac import attachment
563                from trac.env import Environment
564                from trac.ticket import Ticket
565                from trac.Notify import TicketNotifyEmail
566                from trac.web.href import Href
567        else:
568                from trac.Environment import Environment
569                from trac.Ticket import Ticket
570                from trac.Notify import TicketNotifyEmail
571                from trac.Href import Href
572
573        env = Environment(settings['project'], create=0)
574        tktparser = TicketEmailParser(env, settings, version)
575        tktparser.parse(sys.stdin)
576
577# EOB
Note: See TracBrowser for help on using the repository browser.