This is an automated email from the ASF dual-hosted git repository.

sebb pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-ponymail-foal.git


The following commit(s) were added to refs/heads/master by this push:
     new 2d961ad  Anonymise to and cc as well
2d961ad is described below

commit 2d961ad4bc64c044dc428ef930ce03cbaabd1e55
Author: Sebb <[email protected]>
AuthorDate: Thu Nov 11 12:45:50 2021 +0000

    Anonymise to and cc as well
    
    This fixes #128
---
 server/plugins/messages.py | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)

diff --git a/server/plugins/messages.py b/server/plugins/messages.py
index 5ee4717..a76cfbe 100644
--- a/server/plugins/messages.py
+++ b/server/plugins/messages.py
@@ -87,6 +87,11 @@ def extract_name(addr):
     addr = addr.strip("<>")
     return [addr, addr]
 
+# Simple anonymiser; relies on @ appearing only in an email address
+def anonymize_mail_address(emaillist):
+    return re.sub(
+        r"(\S{1,2})\S*@([-a-zA-Z0-9_.]+)", "\\1...@\\2", emaillist
+    )
 
 def anonymize(doc):
     """ Anonymizes an email, hiding author email addresses."""
@@ -100,14 +105,11 @@ def anonymize(doc):
         ptr["md5"] = hashlib.md5(
             bytes(fremail.lower(), encoding="ascii", errors="replace")
         ).hexdigest()
-        if not all(b in ptr["from"] for b in ['<', '>']):  # no <> encasing?
-            ptr["from"] = re.sub(
-                r"(\S{1,2})\S*@([-a-zA-Z0-9_.]+)", "\\1...@\\2", ptr["from"]
-            )
-        else:  # standard <> encasing
-            ptr["from"] = re.sub(
-                r"<(\S{1,2})\S*@([-a-zA-Z0-9_.]+)>", "<\\1...@\\2>", 
ptr["from"]
-            )
+        ptr["from"] = anonymize_mail_address(ptr["from"])
+    if "to" in ptr:
+        ptr["to"] = anonymize_mail_address(ptr["to"])
+    if "cc" in ptr:
+        ptr["cc"] = anonymize_mail_address(ptr["cc"])
     if "body" in ptr and ptr["body"]:
         ptr["body"] = re.sub(
             r"<(\S{1,2})\S*@([-a-zA-Z0-9_.]+)>", "<\\1...@\\2>", ptr["body"]

Reply via email to