Debian Codegraph @

lualib/lua_content/vcard.lua

raw
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 vcard_grammar
22
23 -- XXX: Currently it is a copy of ical grammar
24 local function gen_grammar()
25 if not vcard_grammar then
26 local wsp = l.S(" \t\v\f")
27 local crlf = (l.P "\r" ^ -1 * l.P "\n") + l.P "\r"
28 local eol = (crlf * #crlf) + (crlf - (crlf ^ -1 * wsp))
29 local name = l.C((l.P(1) - (l.P ":")) ^ 1) / function(v)
30 return (v:gsub("[\n\r]+%s", ""))
31 end
32 local value = l.C((l.P(1) - eol) ^ 0) / function(v)
33 return (v:gsub("[\n\r]+%s", ""))
34 end
35 vcard_grammar = name * ":" * wsp ^ 0 * value * eol ^ -1
36 end
37
38 return vcard_grammar
39 end
40
41 local exports = {}
42
43 local function process_vcard(input, mpart, task)
44 local control = { n = '\n', r = '' }
45 local rspamd_url = require "rspamd_url"
46 local escaper = l.Ct((gen_grammar() / function(key, value)
47 value = value:gsub("\\(.)", control)
48 key = key:lower()
49 local local_urls = rspamd_url.all(task:get_mempool(), value)
50
51 if local_urls and #local_urls > 0 then
52 for _, u in ipairs(local_urls) do
53 lua_util.debugm(N, task, 'vcard: found URL in vcard %s',
54 tostring(u))
55 task:inject_url(u, mpart)
56 end
57 end
58 lua_util.debugm(N, task, 'vcard: vcard key %s = "%s"',
59 key, value)
60 return { key, value }
61 end) ^ 1)
62
63 local elts = escaper:match(input)
64
65 if not elts then
66 return nil
67 end
68
69 return {
70 tag = 'vcard',
71 extract_text = function()
72 return nil
73 end, -- NYI
74 elts = elts
75 }
76 end
77
78 --[[[
79 -- @function vcard.process(input)
80 -- Returns all values from vcard as a plain text. Names are completely ignored.
81 --]]
82 exports.process = process_vcard
83
84 return exports