global mime
https://lunarmodules.github.io/luasocket/mime.html
The mime
namespace offers filters that apply and remove common
content transfer encodings, such as Base64 and Quoted-Printable.
It also provides functions to break text into lines and change
the end-of-line convention.
MIME is described mainly in
RFC 2045,
2046,
2047,
2048, and
2049.
All functionality provided by the MIME module follows the ideas presented in
LTN012, Filters sources and sinks.
To obtain the mime
namespace, run:
😱 Types incomplete or incorrect? 🙏 Please contribute!
methods
mime.decode
Returns a filter that decodes data from a given transfer content encoding.
In the Quoted-Printable case, the user can specify whether the data is
textual or binary, by passing the mode
strings "text
" or
"binary
". Mode
defaults to "text
".
mime.encode
Returns a filter that encodes data according to a given transfer content encoding.
In the Quoted-Printable case, the user can specify whether the data is
textual or binary, by passing the mode
strings "text
" or
"binary
". Mode
defaults to "text
".
mime.normalize
Converts most common end-of-line markers to a specific given marker.
Marker
is the new marker. It defaults to CRLF, the canonic
end-of-line marker defined by the MIME standard.
The function returns a filter that performs the conversion.
Note: There is no perfect solution to this problem. Different end-of-line markers are an evil that will probably plague developers forever. This function, however, will work perfectly for text created with any of the most common end-of-line markers, i.e. the Mac OS (CR), the Unix (LF), or the DOS (CRLF) conventions. Even if the data has mixed end-of-line markers, the function will still work well, although it doesn't guarantee that the number of empty lines will be correct.
mime.stuff
Creates and returns a filter that performs stuffing of SMTP messages.
Note: The smtp.send
function
uses this filter automatically. You don't need to chain it with your
source, or apply it to your message body.
😱 Types incomplete or incorrect? 🙏 Please contribute!
mime.wrap
Returns a filter that breaks data into lines.
The "text
" line-wrap filter simply breaks text into lines by
inserting CRLF end-of-line markers at appropriate positions.
Length
defaults 76.
The "base64
" line-wrap filter works just like the default
"text
" line-wrap filter with default length.
The function can also wrap "quoted-printable
" lines, taking care
not to break lines in the middle of an escaped character. In that case, the
line length is fixed at 76.
For example, to create an encoding filter for the Quoted-Printable transfer content encoding of text data, do the following:
qp = ltn12.filter.chain(
mime.normalize(),
mime.encode("quoted-printable"),
mime.wrap("quoted-printable")
)
Note: To break into lines with a different end-of-line convention, apply a normalization filter after the line break filter.
mime.b64
Low-level filter to perform Base64 encoding.
If D
is nil
, A
is padded with
the encoding of the remaining bytes of C
.
Note: The simplest use of this function is to encode a string into it's
Base64 transfer content encoding. Notice the extra parenthesis around the
call to mime.b64
, to discard the second return value.
mime.dot
@param
m
- should tell the same, but for the previous chunk.
Low-level filter to perform SMTP stuffing and enable transmission of messages containing the sequence "CRLF.CRLF".
Note: The message body is defined to begin with
an implicit CRLF. Therefore, to stuff a message correctly, the
first m
should have the value 2.
print((string.gsub(mime.dot(2, ".\r\nStuffing the message.\r\n.\r\n."), "\r\n", "\\n")))
--> ..\nStuffing the message.\n..\n..
Note: The smtp.send
function
uses this filter automatically. You don't need to
apply it again.
mime.eol
@param
C
- C
is the ASCII value of the last character of the previous chunk, if it was a candidate for line break, or 0 otherwise.
@param marker
- Marker
gives the new end-of-line marker and defaults to CRLF.
@return A
- A
is the translated version of D
.
@return B
- B
is the same as C
, but for the current chunk.
😱 Types incomplete or incorrect? 🙏 Please contribute!
A, B = mime.eol(C [, D, marker])
Low-level filter to perform end-of-line marker translation. For each chunk, the function needs to know if the last character of the previous chunk could be part of an end-of-line marker or not. This is the context the function receives besides the chunk. An updated version of the context is returned after each new chunk.
mime.qp
@param
D
- If D
is nil
, A
is padded with the encoding of the remaining bytes of C
.
@param marker
- Throughout encoding, occurrences of CRLF are replaced by the marker
, which itself defaults to CRLF.
@return A
- A
is the encoded version of the largest prefix of C..D
that can be encoded unambiguously.
@return B
- B
has the remaining bytes of C..D
, before decoding.
😱 Types incomplete or incorrect? 🙏 Please contribute!
Low-level filter to perform Quoted-Printable encoding.
Note: The simplest use of this function is to encode a string into it's
Quoted-Printable transfer content encoding.
Notice the extra parenthesis around the call to mime.qp
, to discard the second return value.
mime.qpwrp
@param
n
- n
should tell how many bytes are left for the first line of B
@param length
- broken into lines of at most length
bytes (defaults to 76).
@return A
- A
is a copy of B
, broken into lines of at most length
bytes (defaults to 76).
@return m
- returns the number of bytes left in the last line of A
.
😱 Types incomplete or incorrect? 🙏 Please contribute!
Low-level filter to break Quoted-Printable text into lines.
Note: Besides breaking text into lines, this function makes sure the line
breaks don't fall in the middle of an escaped character combination. Also,
this function only breaks lines that are bigger than length
bytes.
mime.unb64
@return A
- A
is the decoded version of the largest prefix of C..D
that can be decoded unambiguously.
@return B
- B
has the remaining bytes of C..D
, before decoding.
😱 Types incomplete or incorrect? 🙏 Please contribute!
Low-level filter to perform Base64 decoding.
If D
is nil
, A
is the empty string
and B
returns whatever couldn't be decoded.
Note: The simplest use of this function is to decode a string from it's
Base64 transfer content encoding.
Notice the extra parenthesis around the call to mime.unqp
, to discard the second return value.
mime.unqp
@return A
- A
is the decoded version of the largest prefix of C..D
that can be decoded unambiguously. If D
is nil
, A
is augmented with the encoding of the remaining bytes of C
.
@return B
- B
has the remaining bytes of C..D
, before decoding.
😱 Types incomplete or incorrect? 🙏 Please contribute!
Low-level filter to remove the Quoted-Printable transfer content encoding from data.
Note: The simplest use of this function is to decode a string from it's
Quoted-Printable transfer content encoding.
Notice the extra parenthesis around the call to mime.unqp
, to discard the second return value.
mime.wrp
@param
n
- n
should tell how many bytes are left for the first line of B
@param length
- broken into lines of at most length
bytes (defaults to 76).
@return A
- A
is a copy of B
, broken into lines of at most length
bytes (defaults to 76).
@return m
- returns the number of bytes left in the last line of A
.
😱 Types incomplete or incorrect? 🙏 Please contribute!
Low-level filter to break text into lines with CRLF marker.
Text is assumed to be in the normalize
form.
Note: This function only breaks lines that are bigger than
length
bytes. The resulting line length does not include the CRLF
marker.