Ignore:
Timestamp:
05/22/06 11:44:59 (18 years ago)
Author:
bas
Message:

EmailtoTracScript?:

email2trac.py.in:

  • First working implementation of ticket merging :-)
  • Now implement it as option

msg.txt:

  • Changed back to orginal version
File:
1 edited

Legend:

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

    r71 r72  
    9999                self.db = None
    100100
     101                # Some useful mail constants
     102                #
     103                self.author = None
     104                self.email_addr = None
     105                self.email_field = None
     106
    101107                self.VERSION = version
    102108                if self.VERSION == 0.8:
     
    216222
    217223        def email_header_txt(self, m):
    218 #               if not m['Subject']:
    219 #                       subject = '(geen subject)'
    220 #               else:
    221 #                       subject = self.to_unicode(m['Subject'])
    222 #
    223 #               head = "'''Subject:''' %s [[BR]]" % subject
    224 #               if m['From'] and len(m['From']) > 0:
    225 #                       head = "%s'''From:''' %s [[BR]]" % (head, m['From'])
    226 #               if m['Date'] and len(m['Date']) > 0:
    227 #                       head = "%s'''Date:''' %s [[BR]]" %(head, m['Date'])
    228 
     224                """
     225                Display To and CC addresses in description field
     226                """
    229227                str = ''
    230228                if m['To'] and len(m['To']) > 0 and m['To'] != 'hic@sara.nl':
     
    247245                        ticket['owner'] = "UNKNOWN"
    248246
    249         def set_reply_fields(self, ticket, message):
    250                 """
    251                 Bla Bla
    252                 """
    253                 author, email_addr  = email.Utils.parseaddr(message['from'])
    254                 email_str = self.to_unicode(message['from'])
     247        def get_author_emailaddrs(self, message):
     248                """
     249                Get the default author name and email address from the message
     250                """
     251                self.author, self.email_addr  = email.Utils.parseaddr(message['from'])
    255252
    256253                # Look for email address in registered trac users
     
    260257                else:
    261258                        users = [ u for (u, n, e) in self.env.get_known_users(self.db)
    262                                 if e == email_addr ]
     259                                if e == self.email_addr ]
    263260
    264261                if len(users) == 1:
    265                         ticket['reporter'] = users[0]
    266                 else:
    267                         ticket['reporter'] = email_str
     262                        self.email_field = users[0]
     263                else:
     264                        self.email_field =  self.to_unicode(message['from'])
     265
     266        def set_reply_fields(self, ticket, message):
     267                """
     268                Set all the right fields for a new ticket
     269                """
     270                ticket['reporter'] = self.email_field
    268271
    269272                # Put all CC-addresses in ticket CC field
     
    280283                        if self.notification:
    281284                                try:
    282                                         addrs.remove((author, email_addr))
     285                                        addrs.remove((self.author, self.email_addr))
    283286                                except ValueError, detail:
    284287                                        pass
     
    289292                                        except:
    290293                                                ticket['cc'] = mail
    291                 return author, email_addr
    292294
    293295        def save_email_for_debug(self, message):
     
    319321                        return False
    320322
    321                 # Strip # and : frim ticket_id
     323                # Strip '#' and ':' from ticket_id
    322324                ticket_id = result.group('ticketnr')
    323325                ticket_id = int(ticket_id[1:-1])
    324                 print ticket_id
    325326
    326327                tkt = Ticket(self.db, ticket_id)
    327                 print tkt
     328
     329                body_text = self.get_body_text(m)
     330                body_text = '{{{\n%s\n}}}' %body_text
     331                tkt.save_changes(self.db, self.author, body_text)
     332
     333                self.attachments(m, tkt)
     334                if self.notification:
     335                        self.notify(tkt, False)
     336
    328337                return True
    329338
     
    336345                if self.DEBUG > 1:        # save the entire e-mail message text
    337346                        self.save_email_for_debug(msg)
     347                        self.debug_attachments(msg)
    338348
    339349                self.db = self.env.get_db_cnx()
     350                self.get_author_emailaddrs(msg)
     351
     352                if self.get_config('notification', 'smtp_enabled') in ['true']:
     353                        self.notification = 1
     354                else:
     355                        self.notification = 0
     356
     357                # Is this a previous ticket
     358                #
    340359                if self.ticket_update(msg):
    341                         print 'Ticket has been updated now new one'
    342                         sys.exit(0)
    343                 else:
    344                         print 'New ticket'
    345 
    346                 sys.exit(0)
     360                        return True
    347361               
    348362                tkt = Ticket(self.env)
    349363                tkt['status'] = 'new'
    350 
    351                 if self.get_config('notification', 'smtp_enabled') in ['true']:
    352                         self.notification = 1
    353                 else:
    354                         self.notification = 0
    355364
    356365                # Some defaults
     
    382391                #
    383392                self.set_owner(tkt)
    384                 author, email_addr = self.set_reply_fields(tkt, msg)
     393                self.set_reply_fields(tkt, msg)
    385394
    386395                # produce e-mail like header
     
    390399                        head = self.email_header_txt(msg)
    391400
    392                 if self.DEBUG > 0:
    393                         self.debug_attachments(msg)
    394 
    395                 self.description(msg, tkt, head, author, email_addr)
     401
     402                body_text = self.get_body_text(msg)
     403                tkt['description'] = '\n{{{\n%s\n}}}\n' %(body_text)
    396404
    397405                # Insert ticket in database
     
    402410                        tkt['id'] = tkt.insert()
    403411
    404                 #
    405                 # Just how to show to update description
    406                 #
    407                 #tkt['summary'] = '#%s: %s' %(tkt['id'], tkt['summary'])
    408                 #tkt.save_changes(self.db, author, "")
    409 
    410                 self.attachments(msg, tkt, author)
     412                # Else we do no have a ticket id for the subject line
     413                #
     414                mailto = ''
     415                if self.MAILTO:
     416                        mailto = self.html_mailto_link(self.to_unicode(msg['subject']), tkt['id'], body_text)
     417                        tkt['description'] = '%s%s\n{{{\n%s\n}}}\n' %(head, mailto, body_text)
     418                        tkt.save_changes(self.db, self.author, "")
     419
     420                self.attachments(msg, tkt)
    411421                if self.notification:
    412422                        self.notify(tkt)
    413423
    414 
    415         def description(self, msg, tkt, head, author, email):
    416                 """
    417                 put the message text in the ticket description
     424        def get_body_text(self, msg):
     425                """
     426                put the message text in the ticket description or in the changes field
    418427                message text can be plain text or html or something else
    419428                """
     
    440449                                ubody_text = unicode(body_text, charset).encode('utf-8')
    441450
    442                                 tkt['description'] = '\n{{{\n\n%s\n}}}\n' %(ubody_text)
    443 
    444451                        elif part.get_content_type() == 'text/html':
    445                                 tkt['description'] = '%s\n\n(see attachment for HTML mail message)\n' \
    446                                         %(head)
    447                                 body_text = tkt['description']
     452                                ubody_text = '\n\n(see attachment for HTML mail message)\n'
    448453
    449454                        else:
    450                                 tkt['description'] = '%s\n\n(see attachment for message)\n' %(head)
    451                                 body_text = tkt['description']
     455                                ubody_text = '\n\n(see attachment for message)\n'
    452456
    453457                        has_description = 1
     
    455459
    456460                if not has_description:
    457                         tkt['description'] = '%s\n\n(no plain text message, see attachments)' % head
    458                         has_description = 1
    459 
    460                 if self.MAILTO:
    461                         mailto = self.html_mailto_link(author, email, self.to_unicode(msg['subject']), ubody_text)
    462                         tkt['description'] = '%s\n%s %s' %(head, mailto, tkt['description'])
    463 
    464 
    465         def notify(self, tkt):
     461                        ubody_text = '\n\n(see attachment for message)\n'
     462
     463                return ubody_text
     464
     465        def notify(self, tkt , new=True):
    466466                try:
    467467                        # create false {abs_}href properties, to trick Notify()
     
    474474                                tn.template_name = self.notify_template;
    475475
    476                         tn.notify(tkt, newticket=True)
     476                        tn.notify(tkt, new)
    477477
    478478                except Exception, e:
     
    484484
    485485
    486         def html_mailto_link(self, author, mail_addr, subject, body):
    487                 if not author:
    488                         author = mail_addr
     486        def html_mailto_link(self, subject, id, body):
     487                if not self.author:
     488                        author = self.mail_addr
    489489                else:   
    490                         author = self.to_unicode(author)
     490                        author = self.to_unicode(self.author)
    491491
    492492                # Must find a fix
     
    498498
    499499                # Temporary fix
    500                 str = 'mailto:%s?subject=%s' %(urllib.quote(mail_addr), urllib.quote('Re: %s' % subject))
     500                str = 'mailto:%s?subject=%s' %(urllib.quote(self.email_addr), urllib.quote('Re: #%s: %s' %(id, subject)))
    501501                str = '\n{{{\n#!html\n<a href="%s">Reply to: %s</a>\n}}}\n' %(str, author)
    502502
    503503                return str
    504504
    505         def attachments(self, message, ticket, user):
     505        def attachments(self, message, ticket):
    506506                '''save any attachments as file in the ticket's directory'''
    507507
     
    522522                                filename = 'part%04d' % count
    523523
    524                                 ext = mimetypes.guess_extension(part.get_type())
     524                                ext = mimetypes.guess_extension(part.get_content_type())
    525525                                if not ext:
    526526                                        ext = '.bin'
     
    568568                                cursor.execute('INSERT INTO attachment VALUES("%s","%s","%s",%d,%d,"%s","%s","%s")'
    569569                                        %('ticket', urllib.quote(str(ticket['id'])), filename + '?format=raw', filesize,
    570                                           int(time.time()),'', user, 'e-mail') )
     570                                          int(time.time()),'', self.author, 'e-mail') )
    571571                                self.db.commit()
    572572                        else:
Note: See TracChangeset for help on using the changeset viewer.