| 1 |
-------------------------------------------------------------------------------- |
|---|
| 2 |
-- Title: MIME.lua |
|---|
| 3 |
-- Description: Like a square peg in a round hole |
|---|
| 4 |
-- Author: Raphaël Szwarc http://alt.textdrive.com/lua/ |
|---|
| 5 |
-- Creation Date: February 1, 2005 |
|---|
| 6 |
-- Legal: Copyright (C) 2005 Raphaël Szwarc |
|---|
| 7 |
-------------------------------------------------------------------------------- |
|---|
| 8 |
|
|---|
| 9 |
-- import dependencies |
|---|
| 10 |
local LUObject = require( "LUObject" ) |
|---|
| 11 |
local LUClass = require( "LUClass" ) |
|---|
| 12 |
local LUString = require( "LUString" ) |
|---|
| 13 |
local MIMEHeaders = require( "MIMEHeaders" ) |
|---|
| 14 |
local MIMEBase64 = require( "MIMEBase64" ) |
|---|
| 15 |
local MIMEQuotedPrintable = require( "MIMEQuotedPrintable" ) |
|---|
| 16 |
|
|---|
| 17 |
-- define the class |
|---|
| 18 |
local super = LUObject |
|---|
| 19 |
local self = super() |
|---|
| 20 |
|
|---|
| 21 |
function self:init( aRawContent, aStartIndex, anEndIndex, aParent ) |
|---|
| 22 |
self = super.init( self ) |
|---|
| 23 |
|
|---|
| 24 |
self._rawContent = aRawContent |
|---|
| 25 |
self._startIndex = aStartIndex |
|---|
| 26 |
self._endIndex = anEndIndex |
|---|
| 27 |
self._parent = aParent |
|---|
| 28 |
|
|---|
| 29 |
return self |
|---|
| 30 |
end |
|---|
| 31 |
|
|---|
| 32 |
function self:rawContent() |
|---|
| 33 |
return self._rawContent |
|---|
| 34 |
end |
|---|
| 35 |
|
|---|
| 36 |
function self:startIndex() |
|---|
| 37 |
if self._startIndex == nil then |
|---|
| 38 |
self._startIndex = 1 |
|---|
| 39 |
end |
|---|
| 40 |
|
|---|
| 41 |
return self._startIndex |
|---|
| 42 |
end |
|---|
| 43 |
|
|---|
| 44 |
function self:endIndex() |
|---|
| 45 |
if self._endIndex == nil then |
|---|
| 46 |
self._endIndex = self:rawContent():len() |
|---|
| 47 |
end |
|---|
| 48 |
|
|---|
| 49 |
return self._endIndex |
|---|
| 50 |
end |
|---|
| 51 |
|
|---|
| 52 |
function self:parent() |
|---|
| 53 |
return self._parent |
|---|
| 54 |
end |
|---|
| 55 |
|
|---|
| 56 |
function self:headers() |
|---|
| 57 |
if self._headers == nil then |
|---|
| 58 |
self._headers = MIMEHeaders( self:rawContent(), self:startIndex(), self:endIndex() ) |
|---|
| 59 |
end |
|---|
| 60 |
|
|---|
| 61 |
return self._headers |
|---|
| 62 |
end |
|---|
| 63 |
|
|---|
| 64 |
function self:version() |
|---|
| 65 |
return ( self:headers():getValue( "mime-version" ) or "1.0" ):lower() |
|---|
| 66 |
end |
|---|
| 67 |
|
|---|
| 68 |
function self:type() |
|---|
| 69 |
return ( self:headers():getValue( "content-type" ) or "text/plain" ):lower() |
|---|
| 70 |
end |
|---|
| 71 |
|
|---|
| 72 |
function self:transferEncoding() |
|---|
| 73 |
return ( self:headers():getValue( "content-transfer-encoding" ) or "7bit" ):lower() |
|---|
| 74 |
end |
|---|
| 75 |
|
|---|
| 76 |
function self:id() |
|---|
| 77 |
return self:headers():getValue( "content-id" ) |
|---|
| 78 |
end |
|---|
| 79 |
|
|---|
| 80 |
function self:description() |
|---|
| 81 |
return self:headers():getValue( "content-description" ) |
|---|
| 82 |
end |
|---|
| 83 |
|
|---|
| 84 |
function self:disposition() |
|---|
| 85 |
return self:headers():getValue( "content-disposition" ) |
|---|
| 86 |
end |
|---|
| 87 |
|
|---|
| 88 |
function self:entityWithType( aType ) |
|---|
| 89 |
local aName = "MIME" .. LUString:capitalize( aType ):gsub( "[^%w]", "" ) |
|---|
| 90 |
local aClass = LUClass:classWithName( aName ) |
|---|
| 91 |
|
|---|
| 92 |
if aClass == nil then |
|---|
| 93 |
local anIndex = aType:find( "/", 1, true ) |
|---|
| 94 |
|
|---|
| 95 |
if anIndex ~= nil then |
|---|
| 96 |
aType = aType:sub( 1, anIndex - 1 ) |
|---|
| 97 |
aName = "MIME" .. LUString:capitalize( aType ) |
|---|
| 98 |
aClass = LUClass:classWithName( aName ) |
|---|
| 99 |
end |
|---|
| 100 |
end |
|---|
| 101 |
|
|---|
| 102 |
if aClass ~= nil then |
|---|
| 103 |
return aClass:entity() |
|---|
| 104 |
end |
|---|
| 105 |
|
|---|
| 106 |
return nil |
|---|
| 107 |
end |
|---|
| 108 |
|
|---|
| 109 |
function self:content() |
|---|
| 110 |
if self._content == nil then |
|---|
| 111 |
local aType = self:type() |
|---|
| 112 |
local anEntity = self:entityWithType( aType ) |
|---|
| 113 |
local aRawContent = self:rawContent() |
|---|
| 114 |
local aStart = self:headers():endIndex() |
|---|
| 115 |
local anEnd = self:endIndex() |
|---|
| 116 |
|
|---|
| 117 |
if anEntity == nil then |
|---|
| 118 |
local aContent = aRawContent:sub( aStart, anEnd ) |
|---|
| 119 |
local anEncoding = self:transferEncoding() |
|---|
| 120 |
|
|---|
| 121 |
if anEncoding == "quoted-printable" then |
|---|
| 122 |
aContent = MIMEQuotedPrintable:decode( aContent ) |
|---|
| 123 |
elseif anEncoding == "base64" then |
|---|
| 124 |
aContent = MIMEBase64:decode( aContent ) |
|---|
| 125 |
end |
|---|
| 126 |
|
|---|
| 127 |
self._content = aContent |
|---|
| 128 |
else |
|---|
| 129 |
self._content = anEntity( aRawContent, aStart, anEnd, self ) |
|---|
| 130 |
end |
|---|
| 131 |
end |
|---|
| 132 |
|
|---|
| 133 |
return self._content |
|---|
| 134 |
end |
|---|
| 135 |
|
|---|
| 136 |
return self |
|---|