-------------------------------------------------------------------------------- -- Title: MIME.lua -- Description: Like a square peg in a round hole -- Author: Raphaël Szwarc http://alt.textdrive.com/lua/ -- Creation Date: February 1, 2005 -- Legal: Copyright (C) 2005 Raphaël Szwarc -------------------------------------------------------------------------------- -- import dependencies local LUObject = require( "LUObject" ) local LUClass = require( "LUClass" ) local LUString = require( "LUString" ) local MIMEHeaders = require( "MIMEHeaders" ) local MIMEBase64 = require( "MIMEBase64" ) local MIMEQuotedPrintable = require( "MIMEQuotedPrintable" ) -- define the class local super = LUObject local self = super() function self:init( aRawContent, aStartIndex, anEndIndex, aParent ) self = super.init( self ) self._rawContent = aRawContent self._startIndex = aStartIndex self._endIndex = anEndIndex self._parent = aParent return self end function self:rawContent() return self._rawContent end function self:startIndex() if self._startIndex == nil then self._startIndex = 1 end return self._startIndex end function self:endIndex() if self._endIndex == nil then self._endIndex = self:rawContent():len() end return self._endIndex end function self:parent() return self._parent end function self:headers() if self._headers == nil then self._headers = MIMEHeaders( self:rawContent(), self:startIndex(), self:endIndex() ) end return self._headers end function self:version() return ( self:headers():getValue( "mime-version" ) or "1.0" ):lower() end function self:type() return ( self:headers():getValue( "content-type" ) or "text/plain" ):lower() end function self:transferEncoding() return ( self:headers():getValue( "content-transfer-encoding" ) or "7bit" ):lower() end function self:id() return self:headers():getValue( "content-id" ) end function self:description() return self:headers():getValue( "content-description" ) end function self:disposition() return self:headers():getValue( "content-disposition" ) end function self:entityWithType( aType ) local aName = "MIME" .. LUString:capitalize( aType ):gsub( "[^%w]", "" ) local aClass = LUClass:classWithName( aName ) if aClass == nil then local anIndex = aType:find( "/", 1, true ) if anIndex ~= nil then aType = aType:sub( 1, anIndex - 1 ) aName = "MIME" .. LUString:capitalize( aType ) aClass = LUClass:classWithName( aName ) end end if aClass ~= nil then return aClass:entity() end return nil end function self:content() if self._content == nil then local aType = self:type() local anEntity = self:entityWithType( aType ) local aRawContent = self:rawContent() local aStart = self:headers():endIndex() local anEnd = self:endIndex() if anEntity == nil then local aContent = aRawContent:sub( aStart, anEnd ) local anEncoding = self:transferEncoding() if anEncoding == "quoted-printable" then aContent = MIMEQuotedPrintable:decode( aContent ) elseif anEncoding == "base64" then aContent = MIMEBase64:decode( aContent ) end self._content = aContent else self._content = anEntity( aRawContent, aStart, anEnd, self ) end end return self._content end return self