您不能選擇超過 %s 個話題 話題必須以字母或數字為開頭,可包含連接號 ('-') 且最長為 35 個字

88 行
2.4 KiB

  1. -----------------------------------------------------------------------------
  2. -- MIME support for the Lua language.
  3. -- Author: Diego Nehab
  4. -- Conforming to RFCs 2045-2049
  5. -- RCS ID: $Id: mime.lua,v 1.29 2007/06/11 23:44:54 diego Exp $
  6. -----------------------------------------------------------------------------
  7. -----------------------------------------------------------------------------
  8. -- Declare module and import dependencies
  9. -----------------------------------------------------------------------------
  10. local base = _G
  11. local ltn12 = require("preload.tools.ltn12")
  12. local mime = require("mime.core")
  13. local io = require("io")
  14. local string = require("string")
  15. module("mime")
  16. -- encode, decode and wrap algorithm tables
  17. encodet = {}
  18. decodet = {}
  19. wrapt = {}
  20. -- creates a function that chooses a filter by name from a given table
  21. local function choose(table)
  22. return function(name, opt1, opt2)
  23. if base.type(name) ~= "string" then
  24. name, opt1, opt2 = "default", name, opt1
  25. end
  26. local f = table[name or "nil"]
  27. if not f then
  28. base.error("unknown key (" .. base.tostring(name) .. ")", 3)
  29. else return f(opt1, opt2) end
  30. end
  31. end
  32. -- define the encoding filters
  33. encodet['base64'] = function()
  34. return ltn12.filter.cycle(b64, "")
  35. end
  36. encodet['quoted-printable'] = function(mode)
  37. return ltn12.filter.cycle(qp, "",
  38. (mode == "binary") and "=0D=0A" or "\r\n")
  39. end
  40. -- define the decoding filters
  41. decodet['base64'] = function()
  42. return ltn12.filter.cycle(unb64, "")
  43. end
  44. decodet['quoted-printable'] = function()
  45. return ltn12.filter.cycle(unqp, "")
  46. end
  47. local function format(chunk)
  48. if chunk then
  49. if chunk == "" then return "''"
  50. else return string.len(chunk) end
  51. else return "nil" end
  52. end
  53. -- define the line-wrap filters
  54. wrapt['text'] = function(length)
  55. length = length or 76
  56. return ltn12.filter.cycle(wrp, length, length)
  57. end
  58. wrapt['base64'] = wrapt['text']
  59. wrapt['default'] = wrapt['text']
  60. wrapt['quoted-printable'] = function()
  61. return ltn12.filter.cycle(qpwrp, 76, 76)
  62. end
  63. -- function that choose the encoding, decoding or wrap algorithm
  64. encode = choose(encodet)
  65. decode = choose(decodet)
  66. wrap = choose(wrapt)
  67. -- define the end-of-line normalization filter
  68. function normalize(marker)
  69. return ltn12.filter.cycle(eol, 0, marker)
  70. end
  71. -- high level stuffing filter
  72. function stuff()
  73. return ltn12.filter.cycle(dot, 2)
  74. end