Changeset 633


Ignore:
Timestamp:
08/06/13 16:02:40 (11 years ago)
Author:
bas
Message:

rewrote notification system for email2trac: see #266, #297

I have defined my own TicketNotifyEmail? class: Email2TracNotifyEmail. Now it easy to implemenent:

  • notify_reporter
  • notify_replyto_rewrite

notify_replyto_rewrite can handle 2 formats for email2trac.conf

  • use_mail_domain:email2trac.surfsara.nl
    • the replyto field will be set to <ticket_id>@email2trac.surfsara.nl
  • use_trac_smtp_replyto:+
    • Will use the setting in trac.ini for smtp_replto and use '+' as recipient delimiter eg: smtp_replyto: email2trac@… then reply_to will be: email2trac+<ticket_id>@surfsara.nl
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/email2trac.py.in

    r631 r633  
    7575m = None
    7676
    77 # This is to for the function AlwaysNotifyReporter
    78 sender_email = None
    79 
    80 # This is to for the function set_reply_to_fiedl
    81 REPLY_TO_ADDRESS = None
    82 
    83 
    84 
    8577class SaraDict(UserDict.UserDict):
    8678    def __init__(self, dictin = None):
     
    128120    def __iter__(self):
    129121        return iter(self.data.keys())
     122
     123
    130124
    131125class TicketEmailParser(object):
     
    24162410        self.logger.debug('function notify()')
    24172411
    2418         if self.parameters.notify_reporter:
    2419             self.logger.debug('\t Notify reporter set')
    2420             global sender_email
    2421             sender_email = self.email_addr
    2422  
    2423             self.logger.debug('\t Using Email2TracNotification function AlwaysNotifyReporter')
    2424             import trac.notification as Email2TracNotification
    2425             Email2TracNotification.Notify.notify = AlwaysNotifyReporter
    2426 
     2412
     2413        #if self.parameters.notification_replyto_rewrite:
     2414        #    global REPLY_TO_ADDRESS
     2415        #    dummy = self.smtp_replyto.split('@')
     2416           
     2417        #    if len(dummy) > 1:
     2418        #        REPLY_TO_ADDRESS = '%s+%s@%s' %(dummy[0], self.id, dummy[1])
     2419        #    else:
     2420        #        REPLY_TO_ADDRESS = '%s+%s' %(dummy[0], self.id)
     2421
     2422        #    import trac.notification as Email2TracNotification
     2423        #    Email2TracNotification.Notify.notify = set_reply_to_field
     2424
     2425        class Email2TracNotifyEmail(TicketNotifyEmail):
     2426
     2427            def __init__(self, env):
     2428                TicketNotifyEmail.__init__(self, env)
     2429                self.email2trac_notify_reporter = None
     2430                self.email2trac_replyto = None
     2431
     2432            def send(self, torcpts, ccrcpts):
     2433                #print 'Yes this works'
     2434                dest = self.reporter or 'anonymous'
     2435                hdrs = {}
     2436                hdrs['Message-ID'] = self.get_message_id(dest, self.modtime)
     2437                hdrs['X-Trac-Ticket-ID'] = str(self.ticket.id)
     2438                hdrs['X-Trac-Ticket-URL'] = self.data['ticket']['link']
     2439                if not self.newticket:
     2440                    msgid = self.get_message_id(dest)
     2441                    hdrs['In-Reply-To'] = msgid
     2442                    hdrs['References'] = msgid
     2443
     2444
     2445                if self.email2trac_notify_reporter:
     2446                    if not self.email2trac_notify_reporter in torcpts:
     2447                        torcpts.append(sender_email)
     2448
     2449                if self.email2trac_replyto:
     2450                    # use to rewrite reply to
     2451                    # hdrs does not work, multiple reply addresses
     2452                    #hdrs['Reply-To'] = 'bas.van.der.vlies@gmail.com'
     2453                    self.replyto_email = self.email2trac_replyto
    24272454       
    2428         if self.parameters.notification_replyto_rewrite:
    2429             global REPLY_TO_ADDRESS
    2430             dummy = self.smtp_replyto.split('@')
    2431            
    2432             if len(dummy) > 1:
    2433                 REPLY_TO_ADDRESS = '%s+%s@%s' %(dummy[0], self.id, dummy[1])
    2434             else:
    2435                 REPLY_TO_ADDRESS = '%s+%s' %(dummy[0], self.id)
    2436 
    2437             import trac.notification as Email2TracNotification
    2438             Email2TracNotification.Notify.notify = set_reply_to_field
     2455                NotifyEmail.send(self, torcpts, ccrcpts, hdrs)
    24392456
    24402457        if self.parameters.dry_run  :
     
    24432460        try:
    24442461
    2445             #from trac.ticket.notification import TicketNotificationSystem
    2446             #tn_sys = TicketNotificationSystem(self.env)
    2447             #print tn_sys
    2448             #print tn_sys.__dict__
    2449             #sys.exit(0)
    2450 
    24512462            ## create false {abs_}href properties, to trick Notify()
    24522463            #
     
    24552466                self.env.href = Href(self.get_config('project', 'url'))
    24562467
    2457             tn = TicketNotifyEmail(self.env)
     2468            tn = Email2TracNotifyEmail(self.env)
     2469
     2470            ## additionally append sender (regardeless of settings in trac.ini)
     2471            #
     2472            if self.parameters.notify_reporter:
     2473
     2474                self.logger.debug('\t Notify reporter set')
     2475
     2476                if not self.email_header_acl('notify_reporter_black_list', self.email_addr, False):
     2477                    tn.email2trac_notify_reporter = self.email_addr
     2478
     2479            if self.parameters.notify_replyto_rewrite:
     2480
     2481                self.logger.debug('\t Notify replyto rewrite set')
     2482
     2483                action, value = self.parameters.notify_replyto_rewrite.split(':')
     2484
     2485                if action in ['use_mail_domain']:
     2486                    self.logger.debug('\t\t use_mail_domain:%s'i %value)
     2487                    tn.email2trac_replyto = '%s@%s' %(self.id, value)
     2488
     2489                elif action in ['use_trac_smtp_replyto']:
     2490                    self.logger.debug('\t\t use_trac_smtp_replyto delimiter:%s'i %value)
     2491                    dummy = self.smtp_replyto.split('@')
     2492                    if len(dummy) > 1:
     2493                        tn.email2trac_replyto = '%s%s%s@%s' %(dummy[0], value, self.id, dummy[1])
     2494                    else:
     2495                        tn.email2trac_replyto = '%s%s%s' %(dummy[0], value, self.id)
    24582496
    24592497            if self.parameters.alternate_notify_template:
     
    24812519########## END Class Definition  ########################################################
    24822520
    2483 ########## Global Notificaition Function ################################################
    2484 def AlwaysNotifyReporter(self, resid):
    2485     """
    2486     Copy of def notify() to manipulate recipents to always include reporter for the
    2487     notification.
    2488     """
    2489     #print sender_email, resid
    2490     (torcpts, ccrcpts) = self.get_recipients(resid)
    2491     #print "get_recipients finished"
    2492 
    2493     if not tktparser.email_header_acl('notify_reporter_black_list', sender_email, False):
    2494         ## additionally append sender (regardeless of settings in trac.ini)
    2495         #
    2496         if not sender_email in torcpts:
    2497             torcpts.append(sender_email)
    2498 
    2499     #self.replyto_email = "bas.van.der.vlies@gmail.com"
    2500 
    2501     self.begin_send()
    2502     self.send(torcpts, ccrcpts)
    2503     self.finish_send()
    2504 
    2505 def set_reply_to_field(self, resid):
    2506     """
    2507     Copy of def notify() to manipulate recipents to always include reporter for the
    2508     notification.
    2509     """
    2510     print sender_email, resid, REPLY_TO_ADDRESS
    2511     (torcpts, ccrcpts) = self.get_recipients(resid)
    2512    
    2513     self.replyto_email = '%s' %(REPLY_TO_ADDRESS)
    2514     print self.replyto_email
    2515 
    2516     self.begin_send()
    2517     self.send(torcpts, ccrcpts)
    2518     self.finish_send()
    25192521
    25202522########## Parse Config File  ###########################################################
     
    26532655    return logger
    26542656
     2657########## Own TicketNotifyEmail class ###############################################################
    26552658
    26562659if __name__ == '__main__':
     
    27632766            from trac.core import TracError
    27642767
    2765         elif version in ['0.11', '0.12', '0.13', '1.0']:
     2768        elif version in ['0.11', '0.12', '0.13', '1.0', '1.1']:
    27662769            from trac import attachment
    27672770            from trac import config as trac_config
     
    27952798            # see http://projects.edgewall.com/trac/changeset/2799
    27962799            from trac.ticket.notification import TicketNotifyEmail
     2800            from trac.notification import NotifyEmail
    27972801
    27982802        else:
     
    28332837
    28342838        sys.exit(1)
     2839
    28352840# EOB
Note: See TracChangeset for help on using the changeset viewer.