source: emailtotracscript/0.9/email2trac.py @ 5

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

EmailtoTracScript?:

First release and import to track_hacks

File size: 14.6 KB
RevLine 
[5]1#!/usr/bin/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
55                                                                                                creation of the attachments
56        mailto_link: 1               # OPTIONAL, if set then [mailto:<CC>] in
57                                                                                                description
58       
59 * Configure script as a mail (pipe) filter with your MTA
60    typically, this involves adding a line like this to /etc/aliases:
61       somename: | /usr/sara/sbin/run_email2trac
62
63    Check your MTA's documentation for specifics.
64
65 * default config file is : /etc/email2trac.conf
66
67SVN Info:
68        $Id: email2trac.py 1523 2005-11-01 09:27:51Z bas $
69"""
70
71import os
72import sys
73import string
74import getopt
75import stat
76import time
77import email
78import re
79import urllib
80import unicodedata
81import ConfigParser
82from email import Header
83from stat import *
84import mimetypes
85
86trac_default_version = 0.9
87
88class TicketEmailParser(object):
89        env = None
90        comment = '> '
91   
92        def __init__(self, env, parameters, version):
93                self.env = env
94
95                # Database connection
96                #
97                self.db = None
98
99                self.VERSION = version
100                if self.VERSION > 0.8:
101                        self.new_ticket = ticket.Ticket
102                        self.get_config = self.env.config.get
103                else:
104                        self.new_ticket = Ticket.Ticket
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
139        # X-Spam-Score: *** (3.255) BAYES_50,DNS_FROM_AHBL_RHSBL,HTML_
140        # Note if Spam_level then '*' are included
141        def spam(self, message):
142                if message.has_key('X-Spam-Score'):
143                        spam_l = string.split(message['X-Spam-Score'])
144                        number = spam_l[0].count('*')
145
146                        if number >= self.SPAM_LEVEL:
147                                return number
148
149                return 0
150
151        def to_unicode(self, str):
152                """
153                Email has 7 bit ASCII code, convert it to unicode with the charset
154                that is encoded in 7-bit ASCII code and encode it as utf-8 so TRAC
155                understands it.
156                """
157                results =  Header.decode_header(str)
158                str = None
159                for text,format in results:
160                        if format:
161                                try:
162                                        temp = unicode(text, format)
163                                except UnicodeError:
164                                        # This always works
165                                        #
166                                        temp = unicode(text, 'iso-8859-15')
167                                temp =  temp.encode('utf-8')
168                        else:
169                                temp = string.strip(text)
170
171                        if str:
172                                str = '%s %s' %(str, temp)
173                        else:
174                                str = temp
175
176                return str
177
178        def debug_attachments(self, message):
179                n = 0
180                for part in message.walk():
181                        if part.get_content_maintype() == 'multipart':      # multipart/* is just a container
182                                print 'TD: multipart container'
183                                continue
184
185                        n = n + 1
186                        print 'TD: part%d: Content-Type: %s' % (n, part.get_content_type())
187                        print 'TD: part%d: filename: %s' % (n, part.get_filename())
188
189                        if part.is_multipart():
190                                print 'TD: this part is multipart'
191                                payload = part.get_payload(decode=1)
192                                print 'TD: payload:', payload
193                        else:
194                                print 'TD: this part is not multipart'
195
196                        part_file = '/var/tmp/part%d' % n
197                        print 'TD: writing part%d (%s)' % (n,part_file)
198                        fx = open(part_file, 'wb')
199                        text = part.get_payload(decode=1)
200                        if not text:
201                                text = '(None)'
202                        fx.write(text)
203                        fx.close()
204                        try:
205                                os.chmod(part_file,S_IRWXU|S_IRWXG|S_IRWXO)
206                        except OSError:
207                                pass
208
209        def email_header_txt(self, m):
210#               if not m['Subject']:
211#                       subject = '(geen subject)'
212#               else:
213#                       subject = self.to_unicode(m['Subject'])
214#
215#               head = "'''Subject:''' %s [[BR]]" % subject
216#               if m['From'] and len(m['From']) > 0:
217#                       head = "%s'''From:''' %s [[BR]]" % (head, m['From'])
218#               if m['Date'] and len(m['Date']) > 0:
219#                       head = "%s'''Date:''' %s [[BR]]" %(head, m['Date'])
220
221                str = ''
222                if m['To'] and len(m['To']) > 0 and m['To'] != 'hic@sara.nl':
223                        str = "'''To:''' %s [[BR]]" %(m['To'])
224                if m['Cc'] and len(m['Cc']) > 0:
225                        str = "%s'''Cc:''' %s [[BR]]" % (str, m['Cc'])
226
227                return str
228
229        def parse(self, fp):
230                msg = email.message_from_file(fp)
231                if not msg:
232                        return
233
234                if self.DEBUG > 1:        # save the entire e-mail message text
235                        msg_file = '/var/tmp/msg.txt' 
236                        print 'TD: saving email to %s' % msg_file
237                        fx = open(msg_file, 'wb')
238                        fx.write('%s' % msg)
239                        fx.close()
240                        try:
241                                os.chmod(msg_file,S_IRWXU|S_IRWXG|S_IRWXO)
242                        except OSError:
243                                pass
244
245                self.db = self.env.get_db_cnx()
246                tkt = self.new_ticket(self.env)
247                tkt['status'] = 'new'
248
249                # Some defaults
250                #
251                tkt['milestone'] = self.get_config('ticket', 'default_milestone')
252                tkt['priority'] = self.get_config('ticket', 'default_priority')
253                tkt['severity'] = self.get_config('ticket', 'default_severity')
254                tkt['version'] = self.get_config('ticket', 'default_version')
255
256                if not msg['Subject']:
257                        tkt['summary'] = '(geen subject)'
258                else:
259                        tkt['summary'] = self.to_unicode(msg['Subject'])
260
261                if self.SPAM_LEVEL and self.spam(msg):
262                        print 'This message is a SPAM. Automatic ticket insertion refused (SPAM level > %d' % self.SPAM_LEVEL
263                        sys.exit(1)
264
265                if settings.has_key('component'):
266                        tkt['component'] = settings['component']
267                else:
268                        tkt['component'] = self.get_config('ticket', 'default_component')
269
270                # Get default owner for component
271                #
272                cursor = self.db.cursor()
273                sql = 'SELECT owner FROM component WHERE name=\'%s\'' % tkt['component']
274                cursor.execute(sql)
275                tkt['owner'] = cursor.fetchone()[0]
276
277                from_str = self.to_unicode(msg['from'])
278                tkt['reporter'] = from_str
279                if self.CC:
280                        tkt['cc'] = from_str
281
282# produce e-mail like header
283                head = ''
284                if self.EMAIL_HEADER > 0:
285                        head = self.email_header_txt(msg)
286
287                if self.DEBUG > 0:
288                        self.debug_attachments(msg)
289
290#
291#       put the message text in the ticket description
292#       message text can be plain text or html or something else
293#
294                has_description = 0
295                for part in msg.walk():
296                        if part.get_content_maintype() == 'multipart':                  # 'multipart/*' is a container for multipart messages
297                                continue
298
299                        if part.get_content_type() == 'text/plain':
300                                body_text = part.get_payload(decode=1)                  # try to decode
301                                if not body_text:                                       # decode failed
302                                        body_text = part.get_payload(decode=0)          # do not decode
303
304                                tkt['description'] = '\n{{{\n\n%s\n}}}\n' % body_text
305
306                        elif part.get_content_type() == 'text/html':
307                                tkt['description'] = '%s\n\n(see attachment for HTML mail message)\n' % head
308                                body_text = tkt['description']
309
310                        else:
311                                tkt['description'] = '%s\n\n(see attachment for message)\n' % head
312                                body_text = tkt['description']
313
314                        has_description = 1
315                        break           # we have the description, so break
316
317                if not has_description:
318                        tkt['description'] = '%s\n\n(no plain text message, see attachments)' % head
319                        has_description = 1
320
321                author, email_addr  = email.Utils.parseaddr(msg['from'])
322                if self.MAILTO:
323                        mailto = self.html_mailto_link(author, email_addr, self.to_unicode(msg['subject']), body_text)
324                        tkt['description'] = '%s\n%s %s' %(head, mailto, tkt['description'])
325
326                if self.VERSION > 0.8:
327                        tkt['id'] = tkt.insert()
328                else:
329                        tkt['id'] = tkt.insert(self.db)
330
331                #
332                # Just how to show to update description
333                #
334                #tkt['description'] = '\n{{{\n\n Bas is op nieuw bezig\n\n }}}\n'
335                #tkt.save_changes(self.db, author, "Lekker bezig")
336                #
337                self.attachments(msg, tkt, author)
338
339
340        def mail_line(self, str):
341                return '%s %s' % (self.comment, str)
342
343
344        def html_mailto_link(self, author, mail_addr, subject, body):
345                if not author:
346                        author = mail_addr
347                else:   
348                        author = self.to_unicode(author)
349
350                # Must find a fix
351                #
352                #arr = string.split(body, '\n')
353                #arr = map(self.mail_line, arr)
354
355                #body = string.join(arr, '\n')
356                #body = '%s wrote:\n%s' %(author, body)
357
358                # Obsolete for reference
359                #
360                #body = self.to_unicode(body)
361                #body = urllib.quote(body)
362                #body = Header.encode(body)
363                #
364
365                # Temporary fix
366                body = '> Type your reply'
367                str = 'mailto:%s?subject=%s&body=%s' % (urllib.quote(mail_addr), urllib.quote('Re: %s' % subject), urllib.quote(body))
368                str = '\n{{{\n#!html\n<a href="%s">Reply to: %s</a>\n}}}\n' %(str, author)
369
370                return str
371
372        def attachments(self, message, ticket, user):
373                '''save any attachments as file in the ticket's directory'''
374
375                count = 0
376                first = 0
377                for part in message.walk():
378                        if part.get_content_maintype() == 'multipart':          # multipart/* is just a container
379                                continue
380
381                        if not first:                                                                           # first content is the message
382                                first = 1
383                                if part.get_content_type() == 'text/plain':             # if first is text, is was already put in the description
384                                        continue
385
386                        filename = part.get_filename() 
387                        if not filename:
388                                count = count + 1
389                                filename = 'part%04d' % count
390
391                                ext = mimetypes.guess_extension(part.get_type())
392                                if not ext:
393                                        ext = '.bin'
394
395                                filename = '%s%s' % (filename, ext)
396                        else:
397                                filename = self.to_unicode(filename)
398
399                        if '/' in filename:
400                                filename = os.path.basename(filename)
401
402                        url_filename = urllib.quote(filename)
403
404                        if self.VERSION > 0.8:
405                                tmpfile = '/tmp/email2trac-ticket%sattachment' % str(ticket['id'])
406                        else:
407                                dir = os.path.join(self.env.get_attachments_dir(), 'ticket', 
408                                                        urllib.quote(str(ticket['id'])))
409                                if not os.path.exists(dir):
410                                        mkdir_p(dir, 0755)
411
412                                tmpfile = os.path.join(dir, url_filename)
413
414                        f = open(tmpfile, 'wb')
415                        text = part.get_payload(decode=1)
416                        if not text:
417                                text = '(None)'
418                        f.write(text)
419
420                        # get the filesize
421                        #
422                        stats = os.lstat(tmpfile) 
423                        filesize = stats[stat.ST_SIZE]
424
425                        # Insert the attachment it differs for the different TRAC versions
426                        #
427                        if self.VERSION > 0.8:
428                                att = attachment.Attachment(self.env,'ticket',ticket['id'])
429                                att.insert(url_filename,f,filesize)
430                                f.close()
431                        else:
432                                cursor = self.db.cursor()
433                                cursor.execute('INSERT INTO attachment VALUES("%s","%s","%s",%d,%d,"%s","%s","%s")' 
434                                        %('ticket', urllib.quote(str(ticket['id'])), filename + '?format=raw', filesize, 
435                                          int(time.time()),'', user, 'e-mail') ) 
436                                self.db.commit()
437
438
439def mkdir_p(dir, mode):
440        '''do a mkdir -p'''
441
442        arr = string.split(dir, '/')
443        path = ''
444        for part in arr:
445                path = '%s/%s' % (path, part)
446                try:
447                        stats = os.stat(path)
448                except OSError:
449                        os.mkdir(path, mode)
450
451
452def ReadConfig(file):
453        """
454        Parse the config file
455        """
456
457        if not os.path.isfile(file):
458                print 'File %s does not exists' %file
459                sys.exit(1)
460
461        config = ConfigParser.ConfigParser()
462
463        try:
464          config.read(file)
465        except ConfigParser.MissingSectionHeaderError,detail:
466          print detail
467          sys.exit(1)
468
469        defaults = config.defaults()
470        if not defaults.has_key('project') and  not defaults.has_key('project_root'):
471                print 'You have to define the location of your trac project'
472                print 'or the root of your projects, eg:'
473                print '\t project: /var/trac/<projectname>'
474                print '\t project_root: /var/trac'
475                sys.exit(1)
476         
477        return defaults
478
479
480if __name__ == '__main__':
481        # Default config file
482        #
483        configfile = '/etc/email2trac.conf'
484        project = ''
485        component = ''
486       
487        try:
488                opts, args = getopt.getopt(sys.argv[1:], 'chf:p:', ['component=','help', 'file=', 'project='])
489        except getopt.error,detail:
490                print __doc__
491                print detail
492                sys.exit(1)
493
494        for opt,value in opts:
495                if opt in [ '-h', '--help']:
496                        print __doc__
497                        sys.exit(0)
498                elif opt in ['-c', '--component']:
499                        component = value
500                elif opt in ['-f', '--file']:
501                        configfile = value
502                elif opt in ['-p', '--project']:
503                        project = value
504
505        settings = ReadConfig(configfile)
506
507        if not settings.has_key('project') and not project:
508                print 'You must specify a project either in configuration file'
509                print 'or with option --project'
510                sys.exit(1)
511               
512        if project:
513                if settings.has_key('project_root') :
514                        settings['project'] = os.path.join(settings['project_root'], project)
515                else:
516                        print 'You must specify a project root in configuration file'
517                        print 'in order to use option --project'
518                        sys.exit(1)
519        else:
520                if not re.search("/",settings['project']):
521                        if settings.has_key('project_root') :
522                                settings['project'] = os.path.join(settings['project_root'], settings['project'])
523                        else:
524                                print 'You must specify a project root in configuration file'
525                                print 'or specify the project full path'
526                                sys.exit(1)
527
528        if component:
529                settings['component'] = component
530
531        if settings.has_key('trac_version'):
532                version = float(settings['trac_version'])
533        else:
534                version = trac_default_version
535
536        if version > 0.8:
537                from trac import attachment, config, env, ticket
538                env = env.Environment(settings['project'], create=0)
539                ticket_mod = ticket
540        else:
541                from trac import Environment, Ticket
542                env = Environment.Environment(settings['project'], create=0)
543                ticket_mod = Ticket
544               
545        tktparser = TicketEmailParser(env, settings, version)
546        tktparser.parse(sys.stdin)
547
548# EOB
Note: See TracBrowser for help on using the repository browser.