Changeset 341


Ignore:
Timestamp:
03/24/10 15:33:02 (14 years ago)
Author:
bas
Message:

moved email_to_unicode to conversion section

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/email2trac.py.in

    r340 r341  
    280280                self.trac_smtp_from = self.get_config('notification', 'smtp_from')
    281281
    282 ########## Email Header Field Functions ###########################################################
     282########## Email Header Functions ###########################################################
    283283
    284284        def spam(self, message):
     
    350350                return False
    351351
     352        def email_header_txt(self, m):
     353                """
     354                Display To and CC addresses in description field
     355                """
     356                s = ''
     357
     358                if m['To'] and len(m['To']) > 0:
     359                        s = "'''To:''' %s\r\n" %(m['To'])
     360                if m['Cc'] and len(m['Cc']) > 0:
     361                        s = "%s'''Cc:''' %s\r\n" % (s, m['Cc'])
     362
     363                return  self.email_to_unicode(s)
     364
     365
     366        def get_sender_info(self, message):
     367                """
     368                Get the default author name and email address from the message
     369                """
     370
     371                self.email_to = self.email_to_unicode(message['to'])
     372                self.to_name, self.to_email_addr = email.Utils.parseaddr (self.email_to)
     373
     374                self.email_from = self.email_to_unicode(message['from'])
     375                self.email_name, self.email_addr  = email.Utils.parseaddr(self.email_from)
     376
     377                ## Trac can not handle author's name that contains spaces
     378                #  and forbid the ticket email address as author field
     379
     380                if self.email_addr == self.trac_smtp_from:
     381                        self.author = "email2trac"
     382                else:
     383                        self.author = self.email_addr
     384
     385                if self.IGNORE_TRAC_USER_SETTINGS:
     386                        return
     387
     388                # Is this a registered user, use email address as search key:
     389                # result:
     390                #   u : login name
     391                #   n : Name that the user has set in the settings tab
     392                #   e : email address that the user has set in the settings tab
     393                #
     394                users = [ (u,n,e) for (u, n, e) in self.env.get_known_users(self.db)
     395                        if e and (e.lower() == self.email_addr.lower()) ]
     396
     397                if len(users) == 1:
     398                        self.email_from = users[0][0]
     399                        self.author = users[0][0]
     400
     401        def set_reply_fields(self, ticket, message):
     402                """
     403                Set all the right fields for a new ticket
     404                """
     405                if self.DEBUG:
     406                        print 'TD: set_reply_fields'
     407
     408                ## Only use name or email adress
     409                #ticket['reporter'] = self.email_from
     410                ticket['reporter'] = self.author
     411
     412
     413                # Put all CC-addresses in ticket CC field
     414                #
     415                if self.REPLY_ALL:
     416
     417                        email_cc = ''
     418
     419                        cc_addrs = email.Utils.getaddresses( message.get_all('cc', []) )
     420
     421                        if not cc_addrs:
     422                                return
     423
     424                        ## Build a list of forbidden CC addresses
     425                        #
     426                        #to_addrs = email.Utils.getaddresses( message.get_all('to', []) )
     427                        #to_list = list()
     428                        #for n,e in to_addrs:
     429                        #       to_list.append(e)
     430                               
     431                        # Remove reporter email address if notification is
     432                        # on
     433                        #
     434                        if self.notification:
     435                                try:
     436                                        cc_addrs.remove((self.author, self.email_addr))
     437                                except ValueError, detail:
     438                                        pass
     439
     440                        for name,addr in cc_addrs:
     441               
     442                                ## Prevent mail loop
     443                                #
     444                                #if addr in to_list:
     445
     446                                if addr == self.trac_smtp_from:
     447                                        if self.DEBUG:
     448                                                print "Skipping %s mail address for CC-field" %(addr)
     449                                        continue
     450
     451                                if email_cc:
     452                                        email_cc = '%s, %s' %(email_cc, addr)
     453                                else:
     454                                        email_cc = addr
     455
     456                        if email_cc:
     457                                if self.DEBUG:
     458                                        print 'TD: set_reply_fields: %s' %email_cc
     459
     460                                ticket['cc'] = self.email_to_unicode(email_cc)
     461
     462
     463########## DEBUG functions  ###########################################################
     464
     465        def debug_body(self, message_body, tempfile=False):
     466                if tempfile:
     467                        import tempfile
     468                        body_file = tempfile.mktemp('.email2trac')
     469                else:
     470                        body_file = os.path.join(self.TMPDIR, 'body.txt')
     471
     472                if self.DRY_RUN:
     473                        print 'DRY-RUN: not saving body to %s' %(body_file)
     474                        return
     475
     476                print 'TD: writing body to %s' %(body_file)
     477                fx = open(body_file, 'wb')
     478                if not message_body:
     479                                message_body = '(None)'
     480
     481                message_body = message_body.encode('utf-8')
     482                #message_body = unicode(message_body, 'iso-8859-15')
     483
     484                fx.write(message_body)
     485                fx.close()
     486                try:
     487                        os.chmod(body_file,S_IRWXU|S_IRWXG|S_IRWXO)
     488                except OSError:
     489                        pass
     490
     491        def debug_attachments(self, message_parts):
     492                """
     493                """
     494                if self.VERBOSE:
     495                        print "VB: debug_attachments"
     496               
     497                n = 0
     498                for item in message_parts:
     499                        # Skip inline text parts
     500                        if not isinstance(item, tuple):
     501                                continue
     502                               
     503                        (original, filename, part) = item
     504
     505                        n = n + 1
     506                        print 'TD: part%d: Content-Type: %s' % (n, part.get_content_type())
     507                        print 'TD: part%d: filename: %s' % (n, part.get_filename())
     508
     509                        part_file = os.path.join(self.TMPDIR, part.get_filename())
     510                        print 'TD: writing part%d (%s)' % (n,part_file)
     511
     512                        if self.DRY_RUN:
     513                                print 'DRY_RUN: NOT saving attachments'
     514                                continue
     515
     516                        fx = open(part_file, 'wb')
     517                        text = part.get_payload(decode=1)
     518
     519                        if not text:
     520                                text = '(None)'
     521
     522                        fx.write(text)
     523                        fx.close()
     524
     525                        try:
     526                                os.chmod(part_file,S_IRWXU|S_IRWXG|S_IRWXO)
     527                        except OSError:
     528                                pass
     529
     530        def save_email_for_debug(self, message, tempfile=False):
     531
     532                if tempfile:
     533                        import tempfile
     534                        msg_file = tempfile.mktemp('.email2trac')
     535                else:
     536                        #msg_file = '/var/tmp/msg.txt'
     537                        msg_file = os.path.join(self.TMPDIR, 'msg.txt')
     538
     539                if self.DRY_RUN:
     540                        print 'DRY_RUN: NOT saving email message to %s' %(msg_file)
     541                else:
     542                        print 'TD: saving email to %s' %(msg_file)
     543
     544                        fx = open(msg_file, 'wb')
     545                        fx.write('%s' % message)
     546                        fx.close()
     547                       
     548                        try:
     549                                os.chmod(msg_file,S_IRWXU|S_IRWXG|S_IRWXO)
     550                        except OSError:
     551                                pass
     552
     553                message_parts = self.get_message_parts(message)
     554                message_parts = self.unique_attachment_names(message_parts)
     555                body_text = self.body_text(message_parts)
     556                self.debug_body(body_text, True)
     557                self.debug_attachments(message_parts)
     558
     559########## Conversion functions  ###########################################################
     560
    352561        def email_to_unicode(self, message_str):
    353562                """
    354563                Email has 7 bit ASCII code, convert it to unicode with the charset
    355         that is encoded in 7-bit ASCII code and encode it as utf-8 so Trac
     564                that is encoded in 7-bit ASCII code and encode it as utf-8 so Trac
    356565                understands it.
    357566                """
     
    386595                #s = s.encode('utf-8')
    387596                return s
    388 
    389 
    390         def email_header_txt(self, m):
    391                 """
    392                 Display To and CC addresses in description field
    393                 """
    394                 s = ''
    395 
    396                 if m['To'] and len(m['To']) > 0:
    397                         s = "'''To:''' %s\r\n" %(m['To'])
    398                 if m['Cc'] and len(m['Cc']) > 0:
    399                         s = "%s'''Cc:''' %s\r\n" % (s, m['Cc'])
    400 
    401                 return  self.email_to_unicode(s)
    402 
    403 
    404         def get_sender_info(self, message):
    405                 """
    406                 Get the default author name and email address from the message
    407                 """
    408 
    409                 self.email_to = self.email_to_unicode(message['to'])
    410                 self.to_name, self.to_email_addr = email.Utils.parseaddr (self.email_to)
    411 
    412                 self.email_from = self.email_to_unicode(message['from'])
    413                 self.email_name, self.email_addr  = email.Utils.parseaddr(self.email_from)
    414 
    415                 ## Trac can not handle author's name that contains spaces
    416                 #  and forbid the ticket email address as author field
    417 
    418                 if self.email_addr == self.trac_smtp_from:
    419                         self.author = "email2trac"
    420                 else:
    421                         self.author = self.email_addr
    422 
    423                 if self.IGNORE_TRAC_USER_SETTINGS:
    424                         return
    425 
    426                 # Is this a registered user, use email address as search key:
    427                 # result:
    428                 #   u : login name
    429                 #   n : Name that the user has set in the settings tab
    430                 #   e : email address that the user has set in the settings tab
    431                 #
    432                 users = [ (u,n,e) for (u, n, e) in self.env.get_known_users(self.db)
    433                         if e and (e.lower() == self.email_addr.lower()) ]
    434 
    435                 if len(users) == 1:
    436                         self.email_from = users[0][0]
    437                         self.author = users[0][0]
    438 
    439         def set_reply_fields(self, ticket, message):
    440                 """
    441                 Set all the right fields for a new ticket
    442                 """
    443                 if self.DEBUG:
    444                         print 'TD: set_reply_fields'
    445 
    446                 ## Only use name or email adress
    447                 #ticket['reporter'] = self.email_from
    448                 ticket['reporter'] = self.author
    449 
    450 
    451                 # Put all CC-addresses in ticket CC field
    452                 #
    453                 if self.REPLY_ALL:
    454 
    455                         email_cc = ''
    456 
    457                         cc_addrs = email.Utils.getaddresses( message.get_all('cc', []) )
    458 
    459                         if not cc_addrs:
    460                                 return
    461 
    462                         ## Build a list of forbidden CC addresses
    463                         #
    464                         #to_addrs = email.Utils.getaddresses( message.get_all('to', []) )
    465                         #to_list = list()
    466                         #for n,e in to_addrs:
    467                         #       to_list.append(e)
    468                                
    469                         # Remove reporter email address if notification is
    470                         # on
    471                         #
    472                         if self.notification:
    473                                 try:
    474                                         cc_addrs.remove((self.author, self.email_addr))
    475                                 except ValueError, detail:
    476                                         pass
    477 
    478                         for name,addr in cc_addrs:
    479                
    480                                 ## Prevent mail loop
    481                                 #
    482                                 #if addr in to_list:
    483 
    484                                 if addr == self.trac_smtp_from:
    485                                         if self.DEBUG:
    486                                                 print "Skipping %s mail address for CC-field" %(addr)
    487                                         continue
    488 
    489                                 if email_cc:
    490                                         email_cc = '%s, %s' %(email_cc, addr)
    491                                 else:
    492                                         email_cc = addr
    493 
    494                         if email_cc:
    495                                 if self.DEBUG:
    496                                         print 'TD: set_reply_fields: %s' %email_cc
    497 
    498                                 ticket['cc'] = self.email_to_unicode(email_cc)
    499 
    500 
    501 ########## DEBUG functions  ###########################################################
    502 
    503         def debug_body(self, message_body, tempfile=False):
    504                 if tempfile:
    505                         import tempfile
    506                         body_file = tempfile.mktemp('.email2trac')
    507                 else:
    508                         body_file = os.path.join(self.TMPDIR, 'body.txt')
    509 
    510                 if self.DRY_RUN:
    511                         print 'DRY-RUN: not saving body to %s' %(body_file)
    512                         return
    513 
    514                 print 'TD: writing body to %s' %(body_file)
    515                 fx = open(body_file, 'wb')
    516                 if not message_body:
    517                                 message_body = '(None)'
    518 
    519                 message_body = message_body.encode('utf-8')
    520                 #message_body = unicode(message_body, 'iso-8859-15')
    521 
    522                 fx.write(message_body)
    523                 fx.close()
    524                 try:
    525                         os.chmod(body_file,S_IRWXU|S_IRWXG|S_IRWXO)
    526                 except OSError:
    527                         pass
    528 
    529         def debug_attachments(self, message_parts):
    530                 """
    531                 """
    532                 if self.VERBOSE:
    533                         print "VB: debug_attachments"
    534                
    535                 n = 0
    536                 for item in message_parts:
    537                         # Skip inline text parts
    538                         if not isinstance(item, tuple):
    539                                 continue
    540                                
    541                         (original, filename, part) = item
    542 
    543                         n = n + 1
    544                         print 'TD: part%d: Content-Type: %s' % (n, part.get_content_type())
    545                         print 'TD: part%d: filename: %s' % (n, part.get_filename())
    546 
    547                         part_file = os.path.join(self.TMPDIR, part.get_filename())
    548                         print 'TD: writing part%d (%s)' % (n,part_file)
    549 
    550                         if self.DRY_RUN:
    551                                 print 'DRY_RUN: NOT saving attachments'
    552                                 continue
    553 
    554                         fx = open(part_file, 'wb')
    555                         text = part.get_payload(decode=1)
    556 
    557                         if not text:
    558                                 text = '(None)'
    559 
    560                         fx.write(text)
    561                         fx.close()
    562 
    563                         try:
    564                                 os.chmod(part_file,S_IRWXU|S_IRWXG|S_IRWXO)
    565                         except OSError:
    566                                 pass
    567 
    568         def save_email_for_debug(self, message, tempfile=False):
    569 
    570                 if tempfile:
    571                         import tempfile
    572                         msg_file = tempfile.mktemp('.email2trac')
    573                 else:
    574                         #msg_file = '/var/tmp/msg.txt'
    575                         msg_file = os.path.join(self.TMPDIR, 'msg.txt')
    576 
    577                 if self.DRY_RUN:
    578                         print 'DRY_RUN: NOT saving email message to %s' %(msg_file)
    579                 else:
    580                         print 'TD: saving email to %s' %(msg_file)
    581 
    582                         fx = open(msg_file, 'wb')
    583                         fx.write('%s' % message)
    584                         fx.close()
    585                        
    586                         try:
    587                                 os.chmod(msg_file,S_IRWXU|S_IRWXG|S_IRWXO)
    588                         except OSError:
    589                                 pass
    590 
    591                 message_parts = self.get_message_parts(message)
    592                 message_parts = self.unique_attachment_names(message_parts)
    593                 body_text = self.body_text(message_parts)
    594                 self.debug_body(body_text, True)
    595                 self.debug_attachments(message_parts)
    596 
    597 ########## Conversion functions  ###########################################################
    598597
    599598        def str_to_dict(self, s):
Note: See TracChangeset for help on using the changeset viewer.