Network support for the Lua language

home · download · installation · introduction · reference

## MIME

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:

    -- loads the MIME module and everything it requires
    local mime = require("mime")

### High-level filters

mime.normalize([marker])

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.decode("base64")
mime.decode("quoted-printable")

Returns a filter that decodes data from a given transfer content encoding.

mime.encode("base64")
mime.encode("quoted-printable" [, mode])

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`".

Although both transfer content encodings specify a limit for the line length, the encoding filters do not break text into lines (for added flexibility). Below is a filter that converts binary data to the Base64 transfer content encoding and breaks it into lines of the correct size.

    base64 = ltn12.filter.chain(
      mime.encode("base64"),
      mime.wrap("base64")
    )

Note: Text data has to be converted to canonic form before being encoded.

    base64 = ltn12.filter.chain(
      mime.normalize(),
      mime.encode("base64"),
      mime.wrap("base64")
    )

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.

mime.wrap("text" [, length])
mime.wrap("base64")
mime.wrap("quoted-printable")

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.

### Low-level filters

A, B = mime.b64(C [, D])

Low-level filter to perform Base64 encoding.

`A` is the encoded version of the largest prefix of `C..D` that can be encoded unambiguously. `B` has the remaining bytes of `C..D`, before 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.

    print((mime.b64("diego:password")))
    --> ZGllZ286cGFzc3dvcmQ=

A, n = mime.dot(m [, B])

Low-level filter to perform SMTP stuffing and enable transmission of messages containing the sequence "CRLF.CRLF".

`A` is the stuffed version of `B`. '`n`' gives the number of characters from the sequence CRLF seen in the end of `B`. '`m`' should tell the same, but for the previous chunk.

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.

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.

`A` is the translated version of `D`. `C` is the ASCII value of the last character of the previous chunk, if it was a candidate for line break, or 0 otherwise. `B` is the same as `C`, but for the current chunk. `Marker` gives the new end-of-line marker and defaults to CRLF.

    -- translates the end-of-line marker to UNIX
    unix = mime.eol(0, dos, "\n")

A, B = mime.qp(C [, D, marker])

Low-level filter to perform Quoted-Printable encoding.

`A` is the encoded version of the largest prefix of `C..D` that can be encoded unambiguously. `B` has the remaining bytes of `C..D`, before encoding. If `D` is `nil`, `A` is padded with the encoding of the remaining bytes of `C`. Throughout encoding, occurrences of CRLF are replaced by the `marker`, which itself defaults to CRLF.

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.

    print((mime.qp("maçã")))
    --> ma=E7=E3=

A, m = mime.qpwrp(n [, B, length])

Low-level filter to break Quoted-Printable text into lines.

`A` is a copy of `B`, broken into lines of at most `length` bytes (defaults to 76). '`n`' should tell how many bytes are left for the first line of `B` and '`m`' returns the number of bytes left in the last line of `A`.

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.

A, B = mime.unb64(C [, D])

Low-level filter to perform Base64 decoding.

`A` is the decoded version of the largest prefix of `C..D` that can be decoded unambiguously. `B` has the remaining bytes of `C..D`, before 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.

    print((mime.unb64("ZGllZ286cGFzc3dvcmQ=")))
    --> diego:password

A, B = mime.unqp(C [, D])

Low-level filter to remove the Quoted-Printable transfer content encoding from data.

`A` is the decoded version of the largest prefix of `C..D` that can be decoded unambiguously. `B` has the remaining bytes of `C..D`, before decoding. If `D` is `nil`, `A` is augmented with the encoding of the remaining bytes of `C`.

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.

    print((mime.qp("ma=E7=E3=")))
    --> maçã

A, m = mime.wrp(n [, B, length])

Low-level filter to break text into lines with CRLF marker. Text is assumed to be in the `normalize` form.

`A` is a copy of `B`, broken into lines of at most `length` bytes (defaults to 76). '`n`' should tell how many bytes are left for the first line of `B` and '`m`' returns the number of bytes left in the last line of `A`.

Note: This function only breaks lines that are bigger than `length` bytes. The resulting line length does not include the CRLF marker.