]> Frank Brehm's Git Trees - pixelpark/pp-admin-tools.git/commitdiff
Adding some more properties to PostfixLogchainInfo
authorFrank Brehm <frank.brehm@pixelpark.com>
Wed, 20 Mar 2024 16:14:08 +0000 (17:14 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Wed, 20 Mar 2024 16:14:08 +0000 (17:14 +0100)
lib/pp_admintools/postfix_chain.py

index da4d41c7f95616b211a166b2771d515c542ec9f6..fdb96eb64ccac1a3d4276bc1fff1ab905b9f76db 100644 (file)
@@ -18,6 +18,8 @@ from collections import OrderedDict
 
 # Third party modules
 from fb_tools.common import pp
+from fb_tools.errors import InvalidMailAddressError
+from fb_tools.mailaddress import MailAddress
 from fb_tools.obj import FbGenericBaseObject
 
 # Own modules
@@ -136,14 +138,19 @@ class PostfixLogchainInfo(FbGenericBaseObject):
         self.client_addr = client_addr
         self.client_host = client_host
         self.commands = commands
+        self.data = data
         self.ehlo = ehlo
         self.end = end
+        self.from_address = from_address
         self.message_id = message_id
+        self.mail = mail
         self.postfix_id = postfix_id
         self.rcpt = rcpt
         self.sent_quit = sent_quit
+        self.smtpd_pid = smtpd_pid
         self.start = start
         self.starttls = starttls
+        self.to_address = to_address
 
     # -------------------------------------------------------------------------
     @classmethod
@@ -250,6 +257,31 @@ class PostfixLogchainInfo(FbGenericBaseObject):
             return
         self._client_host = val
 
+    # -----------------------------------------------------------
+    @property
+    def data(self):
+        """Return statistics about the DATA command in SMTP dialogue."""
+        return self._data
+
+    @data.setter
+    def data(self, value):
+        if value is None:
+            self._data = None
+            return
+
+        if isinstance(value, int):
+            self._data = DataPair(value)
+            return
+        if isinstance(value, DataPair):
+            self._data = copy.copy(value)
+            return
+        val = str(value).strip()
+        if val == '':
+            self._data = None
+            return
+        else:
+            self._data = DataPair.from_str(val)
+
     # -----------------------------------------------------------
     @property
     def commands(self):
@@ -340,6 +372,61 @@ class PostfixLogchainInfo(FbGenericBaseObject):
             return
         self._end = val
 
+    # -----------------------------------------------------------
+    @property
+    def from_address(self):
+        """Return the MAIL FROM address in the SMTP dialogue envelope."""
+        return self._from_address
+
+    @from_address.setter
+    def from_address(self, value):
+        if value is None:
+            self._from_address = None
+            return
+
+        if isinstance(value, MailAddress):
+            self._from_address = copy.copy(value)
+            return
+        val = str(value).strip()
+        if val == '':
+            self._from_address = None
+            return
+
+        try:
+            self._from_address = MailAddress(val)
+        except InvalidMailAddressError as e:
+            msg = _('Could not interprete from address {a!r}: {e}').format(a=val, e=e)
+            if self.warn_on_parse_error:
+                LOG.warn(msg)
+            else:
+                LOG.debug(msg)
+            self._from_address = val
+
+    # -----------------------------------------------------------
+    @property
+    def mail(self):
+        """Return statistics about mails in SMTP dialogue."""
+        return self._mail
+
+    @mail.setter
+    def mail(self, value):
+        if value is None:
+            self._mail = None
+            return
+
+        if isinstance(value, int):
+            self._mail = DataPair(value)
+            return
+        if isinstance(value, DataPair):
+            self._mail = copy.copy(value)
+            return
+        val = str(value).strip()
+        if val == '':
+            self._mail = None
+            return
+        else:
+            self._mail = DataPair.from_str(val)
+
     # -----------------------------------------------------------
     @property
     def message_id(self):
@@ -426,6 +513,36 @@ class PostfixLogchainInfo(FbGenericBaseObject):
         else:
             self._sent_quit = DataPair.from_str(val)
 
+    # -----------------------------------------------------------
+    @property
+    def smtpd_pid(self):
+        """Return statistics about the quit command in SMTP dialogue."""
+        return self._smtpd_pid
+
+    @smtpd_pid.setter
+    def smtpd_pid(self, value):
+        if value is None:
+            self._smtpd_pid = None
+            return
+
+        if isinstance(value, (float, int)):
+            self._smtpd_pid = int(value)
+            return
+        val = str(value).strip()
+        if val == '':
+            self._smtpd_pid = None
+            return
+
+        try:
+            self._smtpd_pid = int(val)
+        except ValueError as e:
+            msg = _('Could not interprete PID of smtpd {a!r}: {e}').format(a=value, e=e)
+            if self.warn_on_parse_error:
+                LOG.warn(msg)
+            else:
+                LOG.debug(msg)
+            self._smtpd_pid = val
+
     # -----------------------------------------------------------
     @property
     def start(self):
@@ -481,6 +598,36 @@ class PostfixLogchainInfo(FbGenericBaseObject):
         else:
             self._starttls = DataPair.from_str(val)
 
+    # -----------------------------------------------------------
+    @property
+    def to_address(self):
+        """Return the RCPT TO address in the SMTP dialogue envelope."""
+        return self._to_address
+
+    @to_address.setter
+    def to_address(self, value):
+        if value is None:
+            self._to_address = None
+            return
+
+        if isinstance(value, MailAddress):
+            self._to_address = copy.copy(value)
+            return
+        val = str(value).strip()
+        if val == '':
+            self._to_address = None
+            return
+
+        try:
+            self._to_address = MailAddress(val)
+        except InvalidMailAddressError as e:
+            msg = _('Could not interprete to address {a!r}: {e}').format(a=val, e=e)
+            if self.warn_on_parse_error:
+                LOG.warn(msg)
+            else:
+                LOG.debug(msg)
+            self._to_address = val
+
     # -------------------------------------------------------------------------
     def __str__(self):
         """Typecast into a string object.
@@ -528,7 +675,8 @@ class PostfixLogchainInfo(FbGenericBaseObject):
 
         atribs = (
             'client_host', 'client_addr', 'start', 'end', 'message_id', 'postfix_id', 'ehlo',
-            'starttls', 'sent_quit', 'auth', 'commands', 'rcpt')
+            'starttls', 'sent_quit', 'auth', 'commands', 'rcpt', 'data', 'mail', 'from_address',
+            'to_address', 'smtpd_pid')
         for attrib in atribs:
             if not hasattr(self, attrib):
                 continue
@@ -542,7 +690,7 @@ class PostfixLogchainInfo(FbGenericBaseObject):
             if isinstance(value, datetime.datetime):
                 res[attrib] = value.isoformat(' ') if exportable else value
                 continue
-            if isinstance(value, DataPair):
+            if isinstance(value, (DataPair, MailAddress)):
                 res[attrib] = str(value) if exportable else value
                 continue