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.

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