| 1 |
--[[ |
| 2 |
Copyright (c) 2022, Vsevolod Stakhov <[email protected]> |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
]]-- |
| 16 |
|
| 17 |
local l = require 'lpeg' |
| 18 |
local lua_util = require "lua_util" |
| 19 |
local N = "lua_content" |
| 20 |
|
| 21 |
local ical_grammar |
| 22 |
|
| 23 |
local function gen_grammar() |
| 24 |
if not ical_grammar then |
| 25 |
local wsp = l.S(" \t\v\f") |
| 26 |
local crlf = (l.P "\r" ^ -1 * l.P "\n") + l.P "\r" |
| 27 |
local eol = (crlf * #crlf) + (crlf - (crlf ^ -1 * wsp)) |
| 28 |
local name = l.C((l.P(1) - (l.P ":")) ^ 1) / function(v) |
| 29 |
return (v:gsub("[\n\r]+%s", "")) |
| 30 |
end |
| 31 |
local value = l.C((l.P(1) - eol) ^ 0) / function(v) |
| 32 |
return (v:gsub("[\n\r]+%s", "")) |
| 33 |
end |
| 34 |
ical_grammar = name * ":" * wsp ^ 0 * value * eol ^ -1 |
| 35 |
end |
| 36 |
|
| 37 |
return ical_grammar |
| 38 |
end |
| 39 |
|
| 40 |
local exports = {} |
| 41 |
|
| 42 |
local function extract_text_data(specific) |
| 43 |
local fun = require "fun" |
| 44 |
|
| 45 |
local tbl = fun.totable(fun.map(function(e) |
| 46 |
return e[2]:lower() |
| 47 |
end, specific.elts)) |
| 48 |
return table.concat(tbl, '\n') |
| 49 |
end |
| 50 |
|
| 51 |
|
| 52 |
-- Keys that can have visible urls |
| 53 |
local url_keys = lua_util.list_to_hash { |
| 54 |
'description', |
| 55 |
'location', |
| 56 |
'summary', |
| 57 |
'organizer', |
| 58 |
'organiser', |
| 59 |
'attendee', |
| 60 |
'url' |
| 61 |
} |
| 62 |
|
| 63 |
local function process_ical(input, mpart, task) |
| 64 |
local control = { n = '\n', r = '' } |
| 65 |
local rspamd_url = require "rspamd_url" |
| 66 |
local escaper = l.Ct((gen_grammar() / function(key, value) |
| 67 |
value = value:gsub("\\(.)", control) |
| 68 |
key = key:lower():match('^([^;]+)') |
| 69 |
|
| 70 |
if key and url_keys[key] then |
| 71 |
local local_urls = rspamd_url.all(task:get_mempool(), value) |
| 72 |
|
| 73 |
if local_urls and #local_urls > 0 then |
| 74 |
for _, u in ipairs(local_urls) do |
| 75 |
lua_util.debugm(N, task, 'ical: found URL in ical key "%s": %s', |
| 76 |
key, tostring(u)) |
| 77 |
task:inject_url(u, mpart) |
| 78 |
end |
| 79 |
end |
| 80 |
end |
| 81 |
lua_util.debugm(N, task, 'ical: ical key %s = "%s"', |
| 82 |
key, value) |
| 83 |
return { key, value } |
| 84 |
end) ^ 1) |
| 85 |
|
| 86 |
local elts = escaper:match(input) |
| 87 |
|
| 88 |
if not elts then |
| 89 |
return nil |
| 90 |
end |
| 91 |
|
| 92 |
return { |
| 93 |
tag = 'ical', |
| 94 |
extract_text = extract_text_data, |
| 95 |
elts = elts |
| 96 |
} |
| 97 |
end |
| 98 |
|
| 99 |
--[[[ |
| 100 |
-- @function lua_ical.process(input) |
| 101 |
-- Returns all values from ical as a plain text. Names are completely ignored. |
| 102 |
--]] |
| 103 |
exports.process = process_ical |
| 104 |
|
| 105 |
return exports |