]> Frank Brehm's Git Trees - pixelpark/pp-admin-tools.git/commitdiff
Cleaning up method eval_postfix_entry() of class PostfixLogfileParser.
authorFrank Brehm <frank.brehm@pixelpark.com>
Wed, 10 Apr 2024 15:34:49 +0000 (17:34 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Wed, 10 Apr 2024 15:34:49 +0000 (17:34 +0200)
lib/pp_admintools/handler/pflogparse.py

index b1d63c91a2d274c8e9b83533a877a45adf5c8861..051770fec332c39ca03843fe38ac6cd6ea9f7ac4 100644 (file)
@@ -289,7 +289,7 @@ class PostfixLogfileParser(HandlingObject):
             self.chain[postfix_id].mail = m[1]
 
     # -------------------------------------------------------------------------
-    def eval_postfix_entry(                                                         # noqa: C901
+    def eval_postfix_entry(
             self, postfix_id, timestamp=None, host=None, command=None, pid=None,
             message=None, smtpd_done=False):
         """Evaluate a log line with a given Postfix-Id."""
@@ -310,12 +310,7 @@ class PostfixLogfileParser(HandlingObject):
             self.chain[postfix_id].finished = True
             return
 
-        m = self.re_from_addr.match(message)
-        if m:
-            if postfix_id in self.chain and self.chain[postfix_id]:
-                self.chain[postfix_id].from_address = m['from']
-                self.chain[postfix_id].size = m['size']
-                self.chain[postfix_id].nr_rcpt = m['nrcpt']
+        if self._eval_fom_address(postfix_id, message):
             return
 
         if command in self.deliver_commands and pid:
@@ -325,77 +320,23 @@ class PostfixLogfileParser(HandlingObject):
                 pid=pid, message=message)
             return
 
-        if postfix_id in self.chain:
-            m = self.re_message_id.search(message)
-            if m:
-                self.chain[postfix_id].message_id = m[1]
-                return
-
-        if command == 'postfix/cleanup':
-            m = self.re_message_id.search(message)
-            if m:
-                if postfix_id in self.chain:
-                    self.chain[postfix_id].message_id = m[1]
-                else:
-                    msg = _('Did not found Postfix ID {pfid!r} for Message Id {mid!r}.').format(
-                        pfid=postfix_id, mid=m[1])
-                    if self.warn_on_parse_error:
-                        LOG.warn(msg)
-                    else:
-                        LOG.debug(msg)
-                return
+        if self._eval_cleanup(postfix_id, message):
+            return
 
         if command == 'postfix/pickup':
-            m = self.re_pickup.search(message)
-            if m:
-                if postfix_id in self.chain:
-                    self.chain[postfix_id].pickup_from = m['from']
-                    self.chain[postfix_id].pickup_uid = m['uid']
-                else:
-                    chain = PostfixLogchainInfo(
-                        start=timestamp,
-                        postfix_id=postfix_id,
-                        smtpd_pid=pid,
-                        pickup_from=m['from'],
-                        pickup_uid=m['uid'],
-                    )
-                    self.chain[postfix_id] = chain
+            if self._eval_pickup(postfix_id, message, timestamp, pid):
                 return
 
         if command == 'opendkim':
-            m = self.re_dkim.search(message)
-            if m:
-                if postfix_id in self.chain:
-                    self.chain[postfix_id].dkim_selector = m['selector']
-                    self.chain[postfix_id].dkim_domain = m['domain']
-                else:
-                    msg = _('Did not found Postfix ID {pfid!r} for OpenDKIM log entry.').format(
-                        pfid=postfix_id)
-                    if self.warn_on_parse_error:
-                        LOG.warn(msg)
-                    else:
-                        LOG.debug(msg)
+            if self._eval_dkim(self, postfix_id, message):
                 return
 
         if smtpd_done:
             return
 
         if command == 'postfix/bounce':
-            m = self.re_bounce_id.match(message)
-            if m:
-                bid = m['bounce_id'].strip()
-                if bid:
-                    if postfix_id in self.chain:
-                        self.chain[postfix_id].add_bounce_id(bid)
-                    else:
-                        msg = _(
-                            'Did not found Postfix ID {pfid!r} for Bounce ID {bid!r}.').format(
-                            pfid=postfix_id, bid=bid)
-                        if self.warn_on_parse_error:
-                            LOG.warn(msg)
-                        else:
-                            LOG.debug(msg)
-                    return
+            if self._eval_bounce(self, postfix_id, message):
+                return
 
         if self.verbose > 1:
             msg = f'Evaluating further entry of {command!r} for Postfix Id {postfix_id!r}: '
@@ -420,6 +361,105 @@ class PostfixLogfileParser(HandlingObject):
 
         self.chain[postfix_id].add_deliver_action(action)
 
+    # -------------------------------------------------------------------------
+    def _eval_fom_address(self, postfix_id, message):
+
+        m = self.re_from_addr.match(message)
+        if m:
+            if postfix_id in self.chain and self.chain[postfix_id]:
+                self.chain[postfix_id].from_address = m['from']
+                self.chain[postfix_id].size = m['size']
+                self.chain[postfix_id].nr_rcpt = m['nrcpt']
+            return True
+        return False
+
+    # -------------------------------------------------------------------------
+    def _eval_cleanup(self, postfix_id, message):
+
+        m = self.re_message_id.search(message)
+        if not m:
+            return False
+
+        if postfix_id in self.chain:
+            self.chain[postfix_id].message_id = m[1]
+            return True
+
+        msg = _('Did not found Postfix ID {pfid!r} for Message Id {mid!r}.').format(
+            pfid=postfix_id, mid=m[1])
+        if self.warn_on_parse_error:
+            LOG.warn(msg)
+        else:
+            LOG.debug(msg)
+
+        return True
+
+    # -------------------------------------------------------------------------
+    def _eval_pickup(self, postfix_id, message, timestamp, pid):
+
+        m = self.re_pickup.search(message)
+        if not m:
+            return False
+
+        if postfix_id in self.chain:
+            self.chain[postfix_id].pickup_from = m['from']
+            self.chain[postfix_id].pickup_uid = m['uid']
+        else:
+            chain = PostfixLogchainInfo(
+                start=timestamp,
+                postfix_id=postfix_id,
+                smtpd_pid=pid,
+                pickup_from=m['from'],
+                pickup_uid=m['uid'],
+            )
+            self.chain[postfix_id] = chain
+
+        return True
+
+    # -------------------------------------------------------------------------
+    def _eval_dkim(self, postfix_id, message):
+
+        m = self.re_dkim.search(message)
+        if not m:
+            return False
+
+        if postfix_id in self.chain:
+            self.chain[postfix_id].dkim_selector = m['selector']
+            self.chain[postfix_id].dkim_domain = m['domain']
+            return True
+
+        msg = _('Did not found Postfix ID {pfid!r} for OpenDKIM log entry.').format(
+            pfid=postfix_id)
+        if self.warn_on_parse_error:
+            LOG.warn(msg)
+        else:
+            LOG.debug(msg)
+
+        return True
+
+    # -------------------------------------------------------------------------
+    def _eval_bounce(self, postfix_id, message):
+
+        m = self.re_bounce_id.match(message)
+        if not m:
+            return False
+
+        bid = m['bounce_id'].strip()
+        if not bid:
+            return False
+
+        if postfix_id in self.chain:
+            self.chain[postfix_id].add_bounce_id(bid)
+            return True
+
+        msg = _('Did not found Postfix ID {pfid!r} for Bounce ID {bid!r}.').format(
+            pfid=postfix_id, bid=bid)
+        if self.warn_on_parse_error:
+            LOG.warn(msg)
+        else:
+            LOG.debug(msg)
+
+        return True
+
 
 # =========================================================================
 if __name__ == '__main__':