You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ponymail.apache.org by se...@apache.org on 2016/12/18 01:55:08 UTC

incubator-ponymail git commit: Move common anonymizing code to utils

Repository: incubator-ponymail
Updated Branches:
  refs/heads/master 60b8bac89 -> b78032b5c


Move common anonymizing code to utils 

This fixes #308

Project: http://git-wip-us.apache.org/repos/asf/incubator-ponymail/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-ponymail/commit/b78032b5
Tree: http://git-wip-us.apache.org/repos/asf/incubator-ponymail/tree/b78032b5
Diff: http://git-wip-us.apache.org/repos/asf/incubator-ponymail/diff/b78032b5

Branch: refs/heads/master
Commit: b78032b5c021ab0321ff46b35f86ff1819edf662
Parents: 60b8bac
Author: Sebb <se...@apache.org>
Authored: Sun Dec 18 01:54:53 2016 +0000
Committer: Sebb <se...@apache.org>
Committed: Sun Dec 18 01:54:53 2016 +0000

----------------------------------------------------------------------
 CHANGELOG.md           |  1 +
 site/api/email.lua     | 21 ++-------------------
 site/api/lib/utils.lua | 44 ++++++++++++++++++++++++++++++++++++++++++++
 site/api/thread.lua    | 18 ++----------------
 4 files changed, 49 insertions(+), 35 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ponymail/blob/b78032b5/CHANGELOG.md
----------------------------------------------------------------------
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 56ee724..ecf2502 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -84,6 +84,7 @@
 - preferences.lua should not update the user account if the mail is not sent OK (#306)
 - alts.js does not check for errors when calling preferences.lua (#304)
 - An unauthorised private mail should be treated like a non-existent mail (#295)
+- Move common anonymizing code to utils (#308)
 
 ## CHANGES in 0.9b:
 

http://git-wip-us.apache.org/repos/asf/incubator-ponymail/blob/b78032b5/site/api/email.lua
----------------------------------------------------------------------
diff --git a/site/api/email.lua b/site/api/email.lua
index 8654224..1139f3c 100644
--- a/site/api/email.lua
+++ b/site/api/email.lua
@@ -25,20 +25,6 @@ local cross = require 'lib/cross'
 local config = require 'lib/config'
 local utils = require 'lib/utils'
 
--- anonymizer func
-local function anonymize(doc)
-    if doc.from and doc.from ~= JSON.null and #doc.from > 0 then
-        doc.from = doc.from:gsub("(%S+)@(%S+)", function(a,b) return a:sub(1,2) .. "..." .. "@" .. b end)
-    end
-    if doc.cc and doc.cc ~= JSON.null and #doc.cc > 0 then
-        doc.cc = doc.cc:gsub("(%S+)@(%S+)", function(a,b) return a:sub(1,2) .. "..." .. "@" .. b end)
-    end
-    if doc.to and doc.to ~= JSON.null and #doc.to > 0 then
-        doc.to = doc.to:gsub("(%S+)@(%S+)", function(a,b) return a:sub(1,2) .. "..." .. "@" .. b end)
-    end
-    return doc
-end
-
 function handle(r)
     cross.contentType(r, "application/json")
     local get = r:parseargs()
@@ -106,15 +92,12 @@ function handle(r)
                     eml = "unknown"
                 end
                 if not account then -- anonymize email address if not logged in
-                    doc = anonymize(doc)
-                    if doc.from_raw then
-                        doc.from_raw = doc.from_raw:gsub("(%S+)@(%S+)", function(a,b) return a:sub(1,2) .. "..." .. "@" .. b end)
-                    end
+                    doc = utils.anonymizeHdrs(doc, true)
                 end
                 
                 -- Anonymize any email address mentioned in the email if not logged in
                 if not account and config.antispam then
-                    doc.body = doc.body:gsub("<(%S+)@([-a-zA-Z0-9_.]+)>", function(a,b) return "<" .. a:sub(1,2) .. "..." .. "@" .. b .. ">" end)
+                    doc.body = utils.anonymizeBody(doc.body)
                 end
                 
                 

http://git-wip-us.apache.org/repos/asf/incubator-ponymail/blob/b78032b5/site/api/lib/utils.lua
----------------------------------------------------------------------
diff --git a/site/api/lib/utils.lua b/site/api/lib/utils.lua
index 1b27645..13e77c0 100644
--- a/site/api/lib/utils.lua
+++ b/site/api/lib/utils.lua
@@ -17,6 +17,8 @@
 
 -- This is lib/utils.lua - utility methods
 
+local JSON = require 'cjson' -- for JSON.null
+
 -- find the original topic starter
 local function findParent(r, doc, elastic)
     local step = 0
@@ -42,6 +44,48 @@ local function findParent(r, doc, elastic)
 end
 
 
+
+--[[
+    Anonymize the document body
+]]
+local function anonymizeBody(body)
+    return body:gsub("<(%S+)@([-a-zA-Z0-9_.]+)>", function(a,b) return "<" .. a:sub(1,2) .. "..." .. "@" .. b .. ">" end)
+end
+
+--[[
+    Anonymize an email address
+]]
+local function anonymizeEmail(email)
+    return email:gsub("(%S+)@(%S+)", function(a,b) return a:sub(1,2) .. "..." .. "@" .. b end)
+end
+
+--[[
+    Anonymize document headers:
+    - from
+    - cc
+    - to
+    Also processes from_raw if specified
+]]
+local function anonymizeHdrs(doc, from_raw)
+    if doc.from and doc.from ~= JSON.null and #doc.from > 0 then
+        doc.from = anonymizeEmail(doc.from)
+    end
+    if doc.cc and doc.cc ~= JSON.null and #doc.cc > 0 then
+        doc.cc = anonymizeEmail(doc.cc)
+    end
+    if doc.to and doc.to ~= JSON.null and #doc.to > 0 then
+        doc.to = anonymizeEmail(doc.to)
+    end
+    if from_raw and doc.from_raw then
+        doc.from_raw = anonymizeEmail(doc.from_raw)
+    end
+    return doc
+end
+
+
 return {
+    anonymizeHdrs = anonymizeHdrs,
+    anonymizeBody = anonymizeBody,
+    anonymizeEmail = anonymizeEmail,
     findParent = findParent
 }

http://git-wip-us.apache.org/repos/asf/incubator-ponymail/blob/b78032b5/site/api/thread.lua
----------------------------------------------------------------------
diff --git a/site/api/thread.lua b/site/api/thread.lua
index 62039f7..7be9c47 100644
--- a/site/api/thread.lua
+++ b/site/api/thread.lua
@@ -28,20 +28,6 @@ local utils = require 'lib/utils'
 
 local emls_thrd
 
--- anonymizer func
-local function anonymize(doc)
-    if doc.from and doc.from ~= JSON.null and #doc.from > 0 then
-        doc.from = doc.from:gsub("(%S+)@(%S+)", function(a,b) return a:sub(1,2) .. "..." .. "@" .. b end)
-    end
-    if doc.cc and doc.cc ~= JSON.null and #doc.cc > 0 then
-        doc.cc = doc.cc:gsub("(%S+)@(%S+)", function(a,b) return a:sub(1,2) .. "..." .. "@" .. b end)
-    end
-    if doc.to and doc.to ~= JSON.null and #doc.to > 0 then
-        doc.to = doc.to:gsub("(%S+)@(%S+)", function(a,b) return a:sub(1,2) .. "..." .. "@" .. b end)
-    end
-    return doc
-end
-
 -- func that fetches all children of an original topic email thingy
 local function fetchChildren(r, pdoc, c, biglist, account)
     c = (c or 0) + 1
@@ -60,7 +46,7 @@ local function fetchChildren(r, pdoc, c, biglist, account)
             biglist[doc['message-id']] = true
             local mykids = fetchChildren(r, doc, c, biglist, account)
             if not account and config.antispam then
-                doc = anonymize(doc)
+                doc = utils.anonymizeHdrs(doc)
             end
             local dc = {
                 tid = doc.mid,
@@ -112,7 +98,7 @@ function handle(r)
         local account = user.get(r)
         if doc and doc.mid and aaa.canAccessDoc(r, doc, account) then
             if not account and config.antispam then
-                doc = anonymize(doc)
+                doc = utils.anonymizeHdrs(doc)
             end
             table.insert(emls_thrd, doc)
             doc.children = fetchChildren(r, doc, 1, nil, account)