Ignore:
Timestamp:
01/30/06 19:16:27 (18 years ago)
Author:
bas
Message:

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
File:
1 edited

Legend:

Unmodified
Added
Removed
  • emailtotracscript/trunk/email2trac.py.in

    r44 r45  
    238238
    239239        def set_owner(self, ticket):
     240                """
     241                Select default owner for ticket component
     242                """
    240243                cursor = self.db.cursor()
    241244                sql = "SELECT owner FROM component WHERE name='%s'" % ticket['component']
     
    245248
    246249        def set_reply_fields(self, ticket, message):
    247 
     250                """
     251                Bla Bla
     252                """
    248253                author, email_addr  = email.Utils.parseaddr(message['from'])
    249254                email_str = self.to_unicode(message['from'])
    250255
    251                 ticket['reporter'] = email_str
    252                 ticket['cc'] = None
    253 
    254                 # Put all addresses in ticket CC field
     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 = []
     263
     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
    255270                #
    256271                if self.REPLY_ALL:
    257                         tos = message.get_all('to', [])
     272                        #tos = message.get_all('to', [])
    258273                        ccs = message.get_all('cc', [])
    259274
    260                         addrs = email.Utils.getaddresses(tos + ccs)
     275                        addrs = email.Utils.getaddresses(ccs)
    261276
    262277                        # Remove reporter email address if notification is
     
    269284                                        pass
    270285
    271                         for a,m in addrs:
    272                                         if not ticket['cc']:
    273                                                 ticket['cc'] = m
    274                                         else:
    275                                                 ticket['cc'] = '%s,%s' %(ticket['cc'], m)
    276 
    277                 # Put authors email addess in CC field only if notification is off
    278                 #
    279                 elif self.CC and not self.notification:
    280                         ticket['cc'] = email_str
    281 
     286                        for name,mail in addrs:
     287                                        try:
     288                                                ticket['cc'] = '%s,%s' %(ticket['cc'], mail)
     289                                        except:
     290                                                ticket['cc'] = mail
    282291                return author, email_addr
    283292
     
    339348                author, email_addr = self.set_reply_fields(tkt, msg)
    340349
    341 # produce e-mail like header
     350                # produce e-mail like header
     351                #
    342352                head = ''
    343353                if self.EMAIL_HEADER > 0:
     
    347357                        self.debug_attachments(msg)
    348358
    349 #
    350 #       put the message text in the ticket description
    351 #       message text can be plain text or html or something else
    352 #
     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                """
    353385                has_description = 0
    354386                for part in msg.walk():
    355                         if part.get_content_maintype() == 'multipart':                  # 'multipart/*' is a container for multipart messages
     387
     388                        # 'multipart/*' is a container for multipart messages
     389                        #
     390                        if part.get_content_maintype() == 'multipart':
    356391                                continue
    357392
    358393                        if part.get_content_type() == 'text/plain':
    359                                 body_text = part.get_payload(decode=1)                  # try to decode
    360                                 if not body_text:                                       # decode failed
    361                                         body_text = part.get_payload(decode=0)          # do not decode
    362 
    363                                 tkt['description'] = '\n{{{\n\n%s\n}}}\n' % body_text
     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) 
     399
     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')
     405
     406                                tkt['description'] = '\n{{{\n\n%s\n}}}\n' %(ubody_text)
    364407
    365408                        elif part.get_content_type() == 'text/html':
    366                                 tkt['description'] = '%s\n\n(see attachment for HTML mail message)\n' % head
     409                                tkt['description'] = '%s\n\n(see attachment for HTML mail message)\n' \
     410                                        %(head)
    367411                                body_text = tkt['description']
    368412
    369413                        else:
    370                                 tkt['description'] = '%s\n\n(see attachment for message)\n' % head
     414                                tkt['description'] = '%s\n\n(see attachment for message)\n' %(head)
    371415                                body_text = tkt['description']
    372416
     
    379423
    380424                if self.MAILTO:
    381                         mailto = self.html_mailto_link(author, email_addr, self.to_unicode(msg['subject']), body_text)
     425                        mailto = self.html_mailto_link(author, email, self.to_unicode(msg['subject']), ubody_text)
    382426                        tkt['description'] = '%s\n%s %s' %(head, mailto, tkt['description'])
    383427
    384                 if self.VERSION > 0.8:
    385                         tkt['id'] = tkt.insert()
    386                 else:
    387                         tkt['id'] = tkt.insert(self.db)
    388 
    389                 #
    390                 # Just how to show to update description
    391                 #
    392                 #tkt['description'] = '\n{{{\n\n Bas is op nieuw bezig\n\n }}}\n'
    393                 #tkt.save_changes(self.db, author, "Lekker bezig")
    394                 #
    395 
    396                 self.attachments(msg, tkt, author)
    397                 if self.notification:
    398                         self.notify(tkt)
    399428
    400429        def notify(self, tkt):
     
    429458                #arr = string.split(body, '\n')
    430459                #arr = map(self.mail_line, arr)
    431 
    432460                #body = string.join(arr, '\n')
    433461                #body = '%s wrote:\n%s' %(author, body)
    434462
    435                 # Obsolete for reference
    436                 #
    437                 #body = self.to_unicode(body)
    438                 #body = urllib.quote(body)
    439                 #body = Header.encode(body)
    440                 #
    441 
    442463                # Temporary fix
    443464                body = '> Type your reply'
    444                 str = 'mailto:%s?subject=%s&body=%s' % (urllib.quote(mail_addr), urllib.quote('Re: %s' % subject), urllib.quote(body))
     465                str = 'mailto:%s?subject=%s&body=%s' %(urllib.quote(mail_addr), urllib.quote('Re: %s' % subject), urllib.quote(body))
    445466                str = '\n{{{\n#!html\n<a href="%s">Reply to: %s</a>\n}}}\n' %(str, author)
    446467
Note: See TracChangeset for help on using the changeset viewer.