aboutsummaryrefslogtreecommitdiffstats
path: root/lualib/lua_content
diff options
context:
space:
mode:
Diffstat (limited to 'lualib/lua_content')
-rw-r--r--lualib/lua_content/ical.lua9
-rw-r--r--lualib/lua_content/init.lua73
2 files changed, 77 insertions, 5 deletions
diff --git a/lualib/lua_content/ical.lua b/lualib/lua_content/ical.lua
index 4f6b61919..c19723614 100644
--- a/lualib/lua_content/ical.lua
+++ b/lualib/lua_content/ical.lua
@@ -15,6 +15,7 @@ limitations under the License.
]]--
local l = require 'lpeg'
+local rspamd_text = require "rspamd_text"
local wsp = l.P" "
local crlf = l.P"\r"^-1 * l.P"\n"
@@ -25,7 +26,7 @@ local elt = name * ":" * wsp^0 * value * eol
local exports = {}
-local function ical_txt_values(input)
+local function process_ical(input, _, _)
local control={n='\n', r='\r'}
local escaper = l.Ct((elt / function(_,b) return (b:gsub("\\(.)", control)) end)^1)
@@ -35,13 +36,13 @@ local function ical_txt_values(input)
return nil
end
- return table.concat(values, "\n")
+ return rspamd_text.fromtable(values, "\n")
end
--[[[
--- @function lua_ical.ical_txt_values(input)
+-- @function lua_ical.process(input)
-- Returns all values from ical as a plain text. Names are completely ignored.
--]]
-exports.ical_txt_values = ical_txt_values
+exports.process = process_ical
return exports \ No newline at end of file
diff --git a/lualib/lua_content/init.lua b/lualib/lua_content/init.lua
index a96852139..994d613f4 100644
--- a/lualib/lua_content/init.lua
+++ b/lualib/lua_content/init.lua
@@ -17,4 +17,75 @@ limitations under the License.
--[[[
-- @module lua_content
-- This module contains content processing logic
---]] \ No newline at end of file
+--]]
+
+
+local exports = {}
+local N = "lua_content"
+local lua_util = require "lua_util"
+
+local content_modules = {
+ ical = {
+ mime_type = "text/calendar",
+ module = require "lua_content/ical",
+ extensions = {'ical'},
+ output = "text"
+ },
+}
+
+local modules_by_mime_type
+local modules_by_extension
+
+local function init()
+ modules_by_mime_type = {}
+ modules_by_extension = {}
+ for k,v in pairs(content_modules) do
+ if v.mime_type then
+ modules_by_mime_type[v.mime_type] = {k, v}
+ end
+ if v.extensions then
+ for _,ext in ipairs(v.extensions) do
+ modules_by_extension[ext] = {k, v}
+ end
+ end
+ end
+end
+
+exports.maybe_process_mime_part = function(part, log_obj)
+ if not modules_by_mime_type then
+ init()
+ end
+
+ local ctype, csubtype = part:get_type()
+ local mt = string.format("%s/%s", ctype or 'application',
+ csubtype or 'octet-stream')
+ local pair = modules_by_mime_type[mt]
+
+ if not pair then
+ local ext = part:get_detected_ext()
+
+ if ext then
+ pair = modules_by_extension[ext]
+ end
+ end
+
+ if pair then
+ lua_util.debugm(N, log_obj, "found known content of type %s: %s",
+ mt, pair[1])
+
+ local data = pair[2].module.process(part:get_content(), part, log_obj)
+
+ if data then
+ lua_util.debugm(N, log_obj, "extracted content from %s: %s type",
+ pair[1], type(data))
+ part:set_specific(data)
+ else
+ lua_util.debugm(N, log_obj, "failed to extract anything from %s",
+ pair[1])
+ end
+ end
+
+end
+
+
+return exports \ No newline at end of file