Zhang Huangbin avatar Zhang Huangbin committed 40fe3c3

Code cleanup.

Comments (0)

Files changed (5)

 iRedAPD-1.3.7:
     * Supports PostgreSQL.
-    * Don't query mail user in plugin 'sql_alias_access_policy.py'.
+    * Excluding mail user in SQL query (plugin: sql_alias_access_policy).
 
 iRedAPD-1.3.6:
     * Use MySQLdb directly for SQL related operations. Avoid 'too many

src/plugins/block_amavisd_blacklisted_senders.py

 # Author: Zhang Huangbin <zhb@iredmail.org>
 
-import os
+# Priority: whitelist first, then blacklist.
 
-PLUGIN_NAME = os.path.basename(__file__)
+PLUGIN_NAME = 'block_amavisd_blacklisted_senders'
 
 def restriction(smtpSessionData, ldapRecipientLdif, logger, **kargs):
     # Get sender address.
     sender = smtpSessionData.get('sender').lower()
-    splited_sender_domain = str(sender.split('@')[-1]).split('.')
 
-    # Get correct domain name and sub-domain name.
-    # Sample sender domain: sub2.sub1.com.cn
-    #   -> sub2.sub1.com.cn
-    #   -> .sub2.sub1.com.cn
-    #   -> .sub1.com.cn
-    #   -> .com.cn
-    #   -> .cn
-    list_senders = [sender, '@'+sender.split('@')[-1],]
+    # Get valid Amavisd sender, sender domain and sub-domain(s).
+    # - Sample user: user@sub2.sub1.com.cn
+    # - Valid Amavisd senders:
+    #   -> user@sub2.sub1.com.cn
+    #   -> @sub2.sub1.com.cn
+    #   -> @.sub2.sub1.com.cn
+    #   -> @.sub1.com.cn
+    #   -> @.com.cn
+    #   -> @.cn
+    splited_sender_domain = str(sender.split('@', 1)[-1]).split('.')
+
+    # Default senders (user@domain.ltd): ['user@domain.ltd', @domain.ltd']
+    valid_amavisd_senders = [sender, '@'+sender.split('@', 1)[-1],]
     for counter in range(len(splited_sender_domain)):
         # Append domain and sub-domain.
-        list_senders += ['@.' + '.'.join(splited_sender_domain)]
+        valid_amavisd_senders += ['@.' + '.'.join(splited_sender_domain)]
         splited_sender_domain.pop(0)
 
+    valid_amavisd_senders = set(valid_amavisd_senders)
+
     # Get list of amavisBlacklistedSender.
-    blSenders = [v.lower() for v in ldapRecipientLdif.get('amavisBlacklistSender', [])]
+    blSenders = set([v.lower() for v in ldapRecipientLdif.get('amavisBlacklistSender', [])])
 
     # Get list of amavisWhitelistSender.
-    wlSenders = [v.lower() for v in ldapRecipientLdif.get('amavisWhitelistSender', [])]
+    wlSenders = set([v.lower() for v in ldapRecipientLdif.get('amavisWhitelistSender', [])])
 
     logger.debug('(%s) Sender: %s' % (PLUGIN_NAME, sender))
-    logger.debug('(%s) Blacklisted senders: %s' % (PLUGIN_NAME, ', '.join(blSenders)))
-    logger.debug('(%s) Whitelisted senders: %s' % (PLUGIN_NAME, ', '.join(wlSenders)))
-
-    #
-    # Process whitelisted senders first.
-    #
+    logger.debug('(%s) Whitelisted senders: %s' % (PLUGIN_NAME, str(wlSenders)))
+    logger.debug('(%s) Blacklisted senders: %s' % (PLUGIN_NAME, str(blSenders)))
 
     # Bypass whitelisted senders.
-    if len(set(list_senders) & set(wlSenders)) > 0:
+    if len(valid_amavisd_senders & wlSenders) > 0:
         return 'DUNNO Whitelisted'
 
     # Reject blacklisted senders.
-    if len(set(list_senders) & set(blSenders)) > 0:
+    if len(valid_amavisd_senders & blSenders) > 0:
         return 'REJECT Blacklisted'
 
     # Neither blacklisted nor whitelisted.

src/plugins/ldap_domain_wblist.py

 # This plugin is used for per-domain white-/blacklist.
 # ----------------------------------------------------------------------------
 
-import os
-
-PLUGIN_NAME = os.path.basename(__file__)
+PLUGIN_NAME = 'ldap_domain_wblist'
 
 def restriction(ldapConn, ldapBaseDn, smtpSessionData, logger, **kargs):
     sender = smtpSessionData['sender'].lower()

src/plugins/ldap_maillist_access_policy.py

 
 # ----------------------------------------------------------------------------
 
-import os
 from libs import SMTP_ACTIONS
 
-PLUGIN_NAME = os.path.basename(__file__)
+PLUGIN_NAME = 'ldap_maillist_access_policy'
 
 def __get_allowed_senders(ldapConn, ldapBaseDn, listDn, sender, recipient, policy, logger, *kw, **kargs):
     """return search_result_list_based_on_access_policy"""

src/plugins/sql_alias_access_policy.py

 # Author: Zhang Huangbin <zhb@iredmail.org>
 
 # Date: 2010-03-12
-# Purpose: Apply access policy on sender while recipient is an alias.
+# Purpose: Apply access policy on sender while recipient is an mail alias.
 
-# -------- ALTER MYSQL TABLE BEFORE ENABLE THIS PLUGIN -----------
-#   mysql> USE vmail;
-#   mysql> ALTER TABLE alias ADD COLUMN accesspolicy VARCHAR(30) NOT NULL DEFAULT '';
-#   mysql> ALTER TABLE alias ADD COLUMN moderators TEXT NOT NULL DEFAULT '';
-# --------
-
-# Handled policies:
+# Available access policies:
 #   - public:   Unrestricted
 #   - domain:   Only users under same domain are allowed.
 #   - subdomain:    Only users under same domain and sub domains are allowed.
 #   - membersOnly:  Only members are allowed.
 #   - moderatorsOnly:   Only moderators are allowed.
 #   - membersAndModeratorsOnly: Only members and moderators are allowed.
-import os
 from web import sqlquote
 from libs import SMTP_ACTIONS
 
-PLUGIN_NAME = os.path.basename(__file__)
+PLUGIN_NAME = 'sql_alias_access_policy'
 
 # Policies. MUST be defined in lower case.
 POLICY_PUBLIC = 'public'
 
     # Recipient account doesn't exist.
     if sqlRecord is None:
-        return 'DUNNO Alias account does not exist.'
+        return 'DUNNO Not an alias account.'
 
     policy = str(sqlRecord[0]).lower()
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.