Index: Lib/site-packages/trac/notification.py =================================================================== --- Lib/site-packages/trac/notification.py (revision 208) +++ Lib/site-packages/trac/notification.py (revision 209) @@ -27,4 +27,21 @@ MAXHEADERLEN = 76 + + +## imports for LDAP name/email lookup +import platform +LDAP_AD_LOOKUP_AVAILABLE = False +if platform.system().lower()=='windows': + try: + import active_directory + LDAP_AD_LOOKUP_AVAILABLE = True + except: + LDAP_AD_LOOKUP_AVAILABLE = False + try: + import threading, pythoncom + LDAP_AD_LOOKUP_AVAILABLE = True + except: + LDAP_AD_LOOKUP_AVAILABLE = False + @@ -247,4 +264,10 @@ if self.config.getbool('notification', 'use_short_addr'): return address + #### BEGIN NEW LDAP CODE #################################################### + if LDAP_AD_LOOKUP_AVAILABLE: + admail = self.lookup_active_directory_email(address) + if ademail: + return admail + #### END NEW LDAP CODE #################################################### domain = self.config.get('notification', 'smtp_default_domain') if domain: @@ -261,4 +284,35 @@ self.env.log.info("Invalid email address: %s" % address) return None + + def lookup_active_directory_email(self, name): + #### BEGIN NEW LDAP CODE #################################################### + if LDAP_AD_LOOKUP_AVAILABLE: + ldap_user = None # so that we don't have to hit the ldap server twice if both features are enabled + get_email_from_ldap_if_empty = self.env.config.getbool('session', 'get_email_from_ldap_if_empty') + get_email_from_ldap_userattribute = self.env.config.get('session','get_email_from_ldap_userattribute','mail') + if get_email_from_ldap_if_empty: + self.env.log.info("Searching for email address via LDAP (using active_directory module) for user '%s'..." % name) + """ + To fix a bizarro error ultimately caused by the + active_directory module's use of pywin32 (and pythoncom + via pywin32) the error will be something like + "com_error: (-2147221008, 'CoInitialize has not been called.', None, None)" + http://mail.python.org/pipermail/python-win32/2006-December/005425.html + """ + if threading.currentThread().getName() <> 'MainThread': + pythoncom.CoInitialize() + if not ldap_user: + ldap_user = active_directory.find_user(str(name)) + if ldap_user: + try: + ldap_address = '' + ldap_address = str(getattr(ldap_user,get_email_from_ldap_userattribute)).strip().lower() + if ldap_address != '': + self.env.log.info("Found email address for user via LDAP: %s -> %s" % (str(name), str(ldap_address))) + return ldap_address + except AttributeError, e: + self.env.log.error("Specified LDAP user attribute does not exist: %s" % get_email_from_ldap_userattribute) + return None + #### END NEW LDAP CODE #################################################### def encode_header(self, key, value):