]> Frank Brehm's Git Trees - pixelpark/pp-admin-tools.git/commitdiff
Implementing collecting results in bin/get-intern-used-from-addresses
authorFrank Brehm <frank.brehm@pixelpark.com>
Fri, 12 Apr 2024 12:09:25 +0000 (14:09 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Fri, 12 Apr 2024 12:09:25 +0000 (14:09 +0200)
lib/pp_admintools/app/get_from_addr.py

index ef1fdd5ecd8222f791216b25fd5bd45e6282894e..91b4512d4d9a5cc637d903ae501868ce344ea2b0 100644 (file)
@@ -23,7 +23,7 @@ from . import BaseDPXApplication
 from ..handler.pflogparse import PostfixLogfileParser
 from ..xlate import XLATOR
 
-__version__ = '0.4.1'
+__version__ = '0.5.0'
 LOG = logging.getLogger(__name__)
 
 _ = XLATOR.gettext
@@ -197,7 +197,7 @@ class GetFromAddressesApp(BaseDPXApplication):
     def _collect_results(self):
 
         self.empty_line()
-        self.debug(_('Collecting results ...'))
+        LOG.debug(_('Collecting results ...'))
 
         for postfix_id in self.pflogparser.chain:
 
@@ -222,28 +222,75 @@ class GetFromAddressesApp(BaseDPXApplication):
                 continue
 
             # Client address is not an ipaddress
-            if isinstance(chain.client_addr, ipaddress._BaseAddress):
+            if chain.client_addr:
+                if not isinstance(chain.client_addr, ipaddress._BaseAddress):
+                    continue
+
+            if not self._is_local_client(chain):
+                # Not from our network originating
                 continue
 
-            local_client = False
-            if chain.client_addr:
-                for net in self.local_networks:
-                    if chain.client_addr in net:
-                        local_client = True
-                        break
-
-                if local_client:
-                    for except_src in self.except_sources:
-                        if chain.client_addr == except_src:
-                            local_client = False
-            else:
-                # from pickup ...
-                local_client = True
+            if not self._has_extern_rcpt(chain):
+                # remaining in our network
+                continue
+
+            loghost = chain.host
+
+            if loghost not in self.per_domain:
+                self.per_domain[loghost] = {}
+
+            if domain not in self.per_domain[loghost]:
+                self.per_domain[loghost][domain] = 0
+
+            self.per_domain[loghost][domain] += 1
+
+    # -------------------------------------------------------------------------
+    def _is_local_client(self, chain):
+
+        if not chain.client_addr:
+            # from pickup ...
+            return True
+
+        is_local_client = False
+        for net in self.local_networks:
+            if chain.client_addr in net:
+                is_local_client = True
 
-            # Not from our network originating
-            if not local_client:
+        if not is_local_client:
+            return False
+
+        for except_src in self.except_sources:
+            if chain.client_addr == except_src:
+                is_local_client = False
+                break
+
+        return is_local_client
+
+    # -------------------------------------------------------------------------
+    def _has_extern_rcpt(self, chain):
+
+        has_extern_rcpt = False
+
+        for action in chain.deliver_actions:
+            if action.command != 'smtp':
                 continue
 
+            relay_address = action.relay_address
+            if not relay_address:
+                continue
+
+            local_rcpt = False
+            for net in self.local_networks:
+                if relay_address in net:
+                    local_rcpt = True
+                    break
+
+            if not local_rcpt:
+                has_extern_rcpt = True
+                break
+
+        return has_extern_rcpt
+
     # -------------------------------------------------------------------------
     def _run(self):
 
@@ -263,6 +310,10 @@ class GetFromAddressesApp(BaseDPXApplication):
 
         self.pflogparser.evaluate_logfiles(self.logfiles)
 
+        self._collect_results()
+        if self.verbose > 1:
+            LOG.debug(_('Results per domain:') + '\n' + pp(self.per_domain))
+
 
 # =============================================================================
 if __name__ == '__main__':