Stream FiltersStream Filters — How to use Stream Filters |
Stream filters are an efficient way of converting data from one format to another. To use a stream filter, you must first construct a GMimeStreamFilter stream and then add the desired filters to it.
GMime comes equipped with some basic filters such as GMimeFilterBasic, GMimeFilterCharset, GMimeFilterDos2Unix, GMimeFilterUnix2Dos and GMimeFilterFrom.
The GMimeFilterBasic filter is actually a collection of filters for common transfer encodings used by MIME. So far, it is able to handle encoding and decoding of base64, quoted-printable and (x-)uuencode.
GMimeFilterBasic's create_encoder() and create_decoder() factories accept a content encoding enum or name and return a GMimeFilterPassThrough for unhandled encodings. Encoder factories use CRLF by default; the explicit newline variants also accept a maximum line length. The existing new() constructor retains LF defaults and copying pass-through codecs.
GMimeFilterPassThrough is a zero-copy identity filter. It returns the caller's input buffer, length and prespace unchanged, including on completion. Unlike a GMimeFilterBasic using a pass-through codec, it does not allocate an output buffer. Use it when an optional transformation should leave the stream untouched.
GMimeFilterCharset converts complete input characters immediately with the native iconv backend, growing its output buffer as necessary. Incomplete input is retained until another call; completion discards an incomplete final character and emits any final target shift sequence. Reset discards both pending input and shift state. Copying creates a fresh converter with the same original source/target charset names, available through get_from_charset() and get_to_charset(). Native invalid/unrepresentable-byte skipping and iconv byte-order-mark policy are retained, rather than managed Encoding replacement fallbacks.
GMimeFilterAnonymize replaces every byte except ASCII space, tab, CR and LF with lowercase ASCII 'x'. It preserves byte counts and newline spellings, including when input is split across calls. Input is never modified: output uses filter-owned storage. Completion adds no bytes and the filter can be reused without resetting it. This byte transformation alone does not anonymize message headers or MIME structure; original lengths and whitespace patterns remain.
For internationalization support, GMime also includes a filter for converting between any 2 charsets, GMimeFilterCharset. With this filter, mail user agents can allow the user to read the message content in his or her own charset.
A common conversion needed for text is CRLF -> LF and LF -> CRLF which is needed for most (all?) internet protocols. Luckily, GMime comes equipped with a set of filters to handle this for you: GMimeFilterDos2Unix and GMimeFilterUnix2Dos.
On Unix systems, mail often resides in spools in mbox format. Mbox uses a line that starts with "From " to delimit messages which means that message bodies are forbidden to contain lines starting with "From ". The common way to escape these from lines in message bodies is to prepend the line with a greater-than character ('>') producing ">From ". You'll find GMimeFilterFrom handy when dealing with this.
To convert plain or RFC 3676 flowed text into HTML, use GMimeTextToHtml or GMimeFlowedToHtml. These are standalone GMimeTextConverter objects, not stream filters. They support HTML tag callbacks for customizing links and nested blockquotes.