Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. --[[[
  14. -- @module lua_content
  15. -- This module contains content processing logic
  16. --]]
  17. local exports = {}
  18. local N = "lua_content"
  19. local lua_util = require "lua_util"
  20. local content_modules = {
  21. ical = {
  22. mime_type = {"text/calendar", "application/calendar"},
  23. module = require "lua_content/ical",
  24. extensions = {'ics'},
  25. output = "text"
  26. },
  27. vcf = {
  28. mime_type = {"text/vcard", "application/vcard"},
  29. module = require "lua_content/vcard",
  30. extensions = {'vcf'},
  31. output = "text"
  32. },
  33. pdf = {
  34. mime_type = "application/pdf",
  35. module = require "lua_content/pdf",
  36. extensions = {'pdf'},
  37. output = "table"
  38. },
  39. }
  40. local modules_by_mime_type
  41. local modules_by_extension
  42. local function init()
  43. modules_by_mime_type = {}
  44. modules_by_extension = {}
  45. for k,v in pairs(content_modules) do
  46. if v.mime_type then
  47. if type(v.mime_type) == 'table' then
  48. for _,mt in ipairs(v.mime_type) do
  49. modules_by_mime_type[mt] = {k, v}
  50. end
  51. else
  52. modules_by_mime_type[v.mime_type] = {k, v}
  53. end
  54. end
  55. if v.extensions then
  56. for _,ext in ipairs(v.extensions) do
  57. modules_by_extension[ext] = {k, v}
  58. end
  59. end
  60. end
  61. end
  62. exports.maybe_process_mime_part = function(part, task)
  63. if not modules_by_mime_type then
  64. init()
  65. end
  66. local ctype, csubtype = part:get_type()
  67. local mt = string.format("%s/%s", ctype or 'application',
  68. csubtype or 'octet-stream')
  69. local pair = modules_by_mime_type[mt]
  70. if not pair then
  71. local ext = part:get_detected_ext()
  72. if ext then
  73. pair = modules_by_extension[ext]
  74. end
  75. end
  76. if pair then
  77. lua_util.debugm(N, task, "found known content of type %s: %s",
  78. mt, pair[1])
  79. local data = pair[2].module.process(part:get_content(), part, task)
  80. if data then
  81. lua_util.debugm(N, task, "extracted content from %s: %s type",
  82. pair[1], type(data))
  83. part:set_specific(data)
  84. else
  85. lua_util.debugm(N, task, "failed to extract anything from %s",
  86. pair[1])
  87. end
  88. end
  89. end
  90. return exports