]> Frank Brehm's Git Trees - pixelpark/pp-admin-tools.git/commitdiff
Fixing and extending lib/pp_admintools/postfix_chain.py
authorFrank Brehm <frank.brehm@pixelpark.com>
Tue, 19 Mar 2024 09:17:57 +0000 (10:17 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Tue, 19 Mar 2024 09:17:57 +0000 (10:17 +0100)
lib/pp_admintools/postfix_chain.py

index e394ec5f6a232388d106e604964b9b02cfdeba77..a066fa44adeb3e3a75f5ce568b9644188ccde2c2 100644 (file)
@@ -10,17 +10,24 @@ from __future__ import absolute_import
 
 # Standard modules
 import datetime
+import ipaddress
 import logging
 import re
 
 # Third party modules
 from fb_tools.obj import FbGenericBaseObject
 
+# Own modules
+from .xlate import XLATOR
+
+_ = XLATOR.gettext
+ngettext = XLATOR.ngettext
+
 __version__ = '0.1.0'
 
 LOG = logging.getLogger(__name__)
 
-UTC = utc = datetime.timezone(0, 'UTC')
+UTC = utc = datetime.timezone(datetime.timedelta(), 'UTC')
 
 
 # =============================================================================
@@ -35,6 +42,8 @@ class PostfixLogchainInfo(FbGenericBaseObject):
     re_isodatetime = re.compile(
         pattern_isodate + r'[T\s]' + pattern_isotime + pattern_isotimezone)
 
+    warn_on_parse_error = True
+
     # -------------------------------------------------------------------------
     def __init__(
             self, client_host=None, client_addr=None, start=None, end=None, message_id=None,
@@ -59,6 +68,8 @@ class PostfixLogchainInfo(FbGenericBaseObject):
         self._to_address = None
 
         self.auth = auth
+        self.client_addr = client_addr
+        self.client_host = client_host
 
     # -----------------------------------------------------------
     @property
@@ -78,6 +89,51 @@ class PostfixLogchainInfo(FbGenericBaseObject):
         else:
             self._auth = val
 
+    # -----------------------------------------------------------
+    @property
+    def client_addr(self):
+        """Return the IP address of the SMTP client."""
+        return self._client_addr
+
+    @client_addr.setter
+    def client_addr(self, value):
+        if value is None:
+            self._client_addr = None
+            return
+
+        val = str(value).strip()
+        if val == '':
+            self._client_addr = None
+            return
+        try:
+            addr = ipaddress.ip_address(val)
+            self._client_addr = addr
+        except ValueError as e:
+            msg = _('Could not interprete client address {a!r}: {e}').format(a=val, e=e)
+            if self.warn_on_parse_error:
+                LOG.warn(msg)
+            else:
+                LOG.debug(msg)
+            self._client_addr = val
+
+    # -----------------------------------------------------------
+    @property
+    def client_host(self):
+        """Return the hostname of the SMTP client."""
+        return self._client_host
+
+    @client_host.setter
+    def client_host(self, value):
+        if value is None:
+            self._client_host = None
+            return
+
+        val = str(value).strip()
+        if val == '':
+            self._client_host = None
+            return
+        self._client_host = val
+
     # -------------------------------------------------------------------------
     def __repr__(self):
         """Typecast into a string for reproduction."""
@@ -87,8 +143,15 @@ class PostfixLogchainInfo(FbGenericBaseObject):
 
         if self.auth is not None:
             fields.append('auth={!r}'.format(self.auth))
+        if self.client_addr is not None:
+            fields.append('client_addr={!r}'.format(str(self.client_addr)))
+        if self.client_host is not None:
+            fields.append('client_host={!r}'.format(str(self.client_host)))
+
+        if fields:
+            out += ', '.join(fields)
 
-        out += ', '.join(fields) + ')>'
+        out += ')>'
         return out
 
     # -------------------------------------------------------------------------
@@ -105,6 +168,8 @@ class PostfixLogchainInfo(FbGenericBaseObject):
         res = super(PostfixLogchainInfo, self).as_dict(short=short)
 
         res['auth'] = self.auth
+        res['client_addr'] = self.client_addr
+        res['client_host'] = self.client_host
 
         return res