You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

vcard.lua 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. --[[
  2. Copyright (c) 2022, Vsevolod Stakhov <vsevolod@rspamd.com>
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ]]--
  13. local l = require 'lpeg'
  14. local lua_util = require "lua_util"
  15. local N = "lua_content"
  16. local vcard_grammar
  17. -- XXX: Currently it is a copy of ical grammar
  18. local function gen_grammar()
  19. if not vcard_grammar then
  20. local wsp = l.S(" \t\v\f")
  21. local crlf = (l.P"\r"^-1 * l.P"\n") + l.P"\r"
  22. local eol = (crlf * #crlf) + (crlf - (crlf^-1 * wsp))
  23. local name = l.C((l.P(1) - (l.P":"))^1) / function(v) return (v:gsub("[\n\r]+%s","")) end
  24. local value = l.C((l.P(1) - eol)^0) / function(v) return (v:gsub("[\n\r]+%s","")) end
  25. vcard_grammar = name * ":" * wsp^0 * value * eol^-1
  26. end
  27. return vcard_grammar
  28. end
  29. local exports = {}
  30. local function process_vcard(input, mpart, task)
  31. local control={n='\n', r=''}
  32. local rspamd_url = require "rspamd_url"
  33. local escaper = l.Ct((gen_grammar() / function(key, value)
  34. value = value:gsub("\\(.)", control)
  35. key = key:lower()
  36. local local_urls = rspamd_url.all(task:get_mempool(), value)
  37. if local_urls and #local_urls > 0 then
  38. for _,u in ipairs(local_urls) do
  39. lua_util.debugm(N, task, 'vcard: found URL in vcard %s',
  40. tostring(u))
  41. task:inject_url(u, mpart)
  42. end
  43. end
  44. lua_util.debugm(N, task, 'vcard: vcard key %s = "%s"',
  45. key, value)
  46. return {key, value}
  47. end)^1)
  48. local elts = escaper:match(input)
  49. if not elts then
  50. return nil
  51. end
  52. return {
  53. tag = 'vcard',
  54. extract_text = function() return nil end, -- NYI
  55. elts = elts
  56. }
  57. end
  58. --[[[
  59. -- @function vcard.process(input)
  60. -- Returns all values from vcard as a plain text. Names are completely ignored.
  61. --]]
  62. exports.process = process_vcard
  63. return exports