Advertisement
Guest User

mime

a guest
Dec 18th, 2014
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  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. [Module]
  8. Name=Mime
  9. By=Diego Nehab(2007/06/11 23:44:54)
  10. Version=1.29
  11. ]]--
  12. -----------------------------------------------------------------------------
  13.  
  14. -----------------------------------------------------------------------------
  15. -- Declare module and import dependencies
  16. -----------------------------------------------------------------------------
  17. local base = _G
  18. local ltn12 = require("ltn12")
  19. local mime = require("mime.core")
  20. local io = require("io")
  21. local string = require("string")
  22. module("mime")
  23.  
  24. -- encode, decode and wrap algorithm tables
  25. encodet = {}
  26. decodet = {}
  27. wrapt = {}
  28.  
  29. -- creates a function that chooses a filter by name from a given table
  30. local function choose(table)
  31. return function(name, opt1, opt2)
  32. if base.type(name) ~= "string" then
  33. name, opt1, opt2 = "default", name, opt1
  34. end
  35. local f = table[name or "nil"]
  36. if not f then
  37. base.error("unknown key (" .. base.tostring(name) .. ")", 3)
  38. else return f(opt1, opt2) end
  39. end
  40. end
  41.  
  42. -- define the encoding filters
  43. encodet['base64'] = function()
  44. return ltn12.filter.cycle(b64, "")
  45. end
  46.  
  47. encodet['quoted-printable'] = function(mode)
  48. return ltn12.filter.cycle(qp, "",
  49. (mode == "binary") and "=0D=0A" or "\r\n")
  50. end
  51.  
  52. -- define the decoding filters
  53. decodet['base64'] = function()
  54. return ltn12.filter.cycle(unb64, "")
  55. end
  56.  
  57. decodet['quoted-printable'] = function()
  58. return ltn12.filter.cycle(unqp, "")
  59. end
  60.  
  61. local function format(chunk)
  62. if chunk then
  63. if chunk == "" then return "''"
  64. else return string.len(chunk) end
  65. else return "nil" end
  66. end
  67.  
  68. -- define the line-wrap filters
  69. wrapt['text'] = function(length)
  70. length = length or 76
  71. return ltn12.filter.cycle(wrp, length, length)
  72. end
  73. wrapt['base64'] = wrapt['text']
  74. wrapt['default'] = wrapt['text']
  75.  
  76. wrapt['quoted-printable'] = function()
  77. return ltn12.filter.cycle(qpwrp, 76, 76)
  78. end
  79.  
  80. -- function that choose the encoding, decoding or wrap algorithm
  81. encode = choose(encodet)
  82. decode = choose(decodet)
  83. wrap = choose(wrapt)
  84.  
  85. -- define the end-of-line normalization filter
  86. function normalize(marker)
  87. return ltn12.filter.cycle(eol, 0, marker)
  88. end
  89.  
  90. -- high level stuffing filter
  91. function stuff()
  92. return ltn12.filter.cycle(dot, 2)
  93. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement