GMimeParser

GMimeParser — Message and MIME part parser

Functions

Types and Values

Description

A GMimeParser builds headers, MIME entities and messages using a single buffered GMimeReader. Mbox and MMDF streams support successive messages. Optional compliance reporting is enabled with g_mime_parser_set_compliance_logger(), independently of legacy parser warnings. Diagnostic settings never change how input is parsed.

Functions

g_mime_parser_new ()

GMimeParser *
g_mime_parser_new (void);

Creates a new parser object.

Returns

a new parser object.

[transfer full]


g_mime_parser_new_with_stream ()

GMimeParser *
g_mime_parser_new_with_stream (GMimeStream *stream);

Creates a parser at the current position of stream .

Content is retained on seekable sources and copied into memory for nonseekable streams. Use g_mime_parser_new_with_stream_and_storage() to choose a storage mode explicitly.

Parameters

stream

raw message or part stream

 

Returns

a new parser object.

[transfer full]


g_mime_parser_new_with_stream_and_storage ()

GMimeParser *
g_mime_parser_new_with_stream_and_storage
                               (GMimeStream *stream,
                                GMimeFormat format,
                                GMimeContentStorage storage,
                                GError **error);

Creates a parser at the current position of stream with explicit format and content storage. Memory storage copies content; source storage references leaf content through substreams. Source storage requires working tell, seek and substream operations. Seekability is checked with a zero-offset current-position seek before reading. Unsupported seeking reports an error rather than falling back to memory; substream errors are reported when parsing content.

The caller owns stream in both modes. Neither the parser nor returned models close a stream still owned by the caller. With source storage, keep the backing input open and unchanged while parsed content is used; content views retain it after the parser is released or reinitialized.

Parameters

stream

input at the start of the entity, message or mailbox

 

format

the input format

 

storage

storage for parsed MIME part content

 

error

return location for a GError, or NULL

 

Returns

a new parser, or NULL on failure.

[nullable][transfer full]


g_mime_parser_init_with_stream ()

void
g_mime_parser_init_with_stream (GMimeParser *parser,
                                GMimeStream *stream);

Replaces the input at its current position, discarding buffered data, metadata and any previous error. Neither stream is reset or closed. Parser format, persistence, compliance options and the compliance logger are retained.

Do not read, write, seek or reset stream while parsing. Persistent part content may be read between operations: its physical source position is restored before the next operation. Use g_mime_parser_tell() for logical positions; the underlying stream can be ahead due to buffered input. Must not be called from a parsing callback.

Parameters

parser

a GMimeParser context

 

stream

raw message or part stream

 

g_mime_parser_init_with_stream_and_storage ()

gboolean
g_mime_parser_init_with_stream_and_storage
                               (GMimeParser *parser,
                                GMimeStream *stream,
                                GMimeFormat format,
                                GMimeContentStorage storage,
                                GError **error);

Replaces the input, format and storage, discarding buffered data, metadata and any previous parsing error. Compliance options and the compliance logger are retained. Neither stream is reset or closed. Previously returned models retain their content and are unaffected by stream replacement.

Source storage has the capability and lifetime requirements described in g_mime_parser_new_with_stream_and_storage(). Validation occurs before replacing the input: failure leaves the parser's input, buffered data, metadata, format and storage unchanged.

Use g_mime_parser_tell() for logical positions. Do not manipulate stream while parsing; source-backed content may be read between operations. Must not be called from a parsing callback.

Parameters

parser

a GMimeParser context

 

stream

replacement input at its current position

 

format

the input format

 

storage

storage for parsed MIME part content

 

error

return location for a GError, or NULL

 

Returns

TRUE on success, or FALSE on failure.


g_mime_parser_get_format ()

GMimeFormat
g_mime_parser_get_format (GMimeParser *parser);

Parameters

parser

a GMimeParser context

 

Returns

the expected input format.


g_mime_parser_set_format ()

void
g_mime_parser_set_format (GMimeParser *parser,
                          GMimeFormat format);

Changes the input format without discarding buffered input. Must not be called from a parsing callback.

Parameters

parser

a GMimeParser context

 

format

the expected input format

 

g_mime_parser_get_compliance_options ()

GMimeComplianceOptions *
g_mime_parser_get_compliance_options (GMimeParser *parser);

Gets the parser's mutable compliance settings, including before a stream is assigned. Changes apply to the next construction operation, not to an active operation. The settings survive stream replacement.

Parameters

parser

a GMimeParser context

 

Returns

the parser's compliance settings, valid until replaced by g_mime_parser_set_compliance_options() or the parser is released.

[transfer none]


g_mime_parser_set_compliance_options ()

void
g_mime_parser_set_compliance_options (GMimeParser *parser,
                                      GMimeComplianceOptions *options);

Copies options for subsequent header, entity and message construction. Assigning the parser's own settings is safe. Changes made from callbacks apply to the next operation; the active operation retains its snapshot. Settings survive stream replacement and do not enable reporting unless a logger is also assigned. They never change parsing policy.

Parameters

parser

a GMimeParser context

 

options

settings to copy, or NULL for defaults.

[nullable]

g_mime_parser_set_compliance_logger ()

void
g_mime_parser_set_compliance_logger (GMimeParser *parser,
                                     GMimeComplianceIssueFunc report,
                                     gpointer user_data,
                                     GDestroyNotify destroy);

Enables structured MIME compliance reporting during header, entity and message construction, or disables it when report is NULL. When disabling, user_data and destroy must also be NULL. Without a logger, compliance validation is skipped; legacy parser warning callbacks are unaffected.

The logger may be configured before assigning input and survives stream replacement. Replacing or disabling it from a parsing callback takes effect at the next operation. The previous data is released once no active operation uses it, or when the parser is released if the logger remains installed. Diagnostic budgets reset for each construction operation/message.

Callbacks receive borrowed issues and may inspect the parser, change its compliance settings or release their parser reference. They must not start another parse, replace input, or manipulate the underlying stream. Concurrent use of a parser is not supported.

Parameters

parser

a GMimeParser context

 

report

diagnostic callback.

[nullable][scope notified][closure user_data][destroy destroy]

user_data

application data.

[nullable]

destroy

releases user_data .

[nullable]

g_mime_parser_tell ()

gint64
g_mime_parser_tell (GMimeParser *parser);

Gets the logical input position, excluding unread buffered bytes. Nonseekable streams use offset 0 as their initial origin.

Parameters

parser

a GMimeParser context

 

Returns

the logical offset, or -1 if no stream has been assigned.


g_mime_parser_eos ()

gboolean
g_mime_parser_eos (GMimeParser *parser);

Parameters

parser

a GMimeParser context

 

Returns

TRUE if parsing has reached EOF, or no stream is assigned.


g_mime_parser_construct_headers ()

GMimeHeaderList *
g_mime_parser_construct_headers (GMimeParser *parser,
                                 GMimeParserOptions *options,
                                 GError **error);

Reads a header block directly at the current logical position, without searching for a mailbox marker or stripping a byte-order mark. Retains message and content headers, original field bytes and offsets. A blank separator is consumed but is not included in the list.

Empty input returns an empty list. Fatal syntax, header limits and I/O errors discard partial results. Errors remain sticky until stream replacement. Options are copied at the start of each operation. Reentrant or concurrent operations are not supported.

Parameters

parser

a GMimeParser context

 

options

parser options, or NULL for defaults.

[nullable]

error

return location for a GError, or NULL

 

Returns

parsed headers, or NULL on failure.

[nullable][transfer full]


g_mime_parser_construct_part ()

GMimeEntity *
g_mime_parser_construct_part (GMimeParser *parser,
                              GMimeParserOptions *options,
                              GError **error);

Constructs a MIME entity directly at the current logical position, without mailbox marker search or byte-order-mark stripping. All input operations share the same reader and buffered bytes.

Fatal syntax, header limits and I/O errors discard partial results and remain sticky until stream replacement. Options are copied per operation. Reentrant or concurrent operations are not supported.

Parameters

parser

a GMimeParser context

 

options

parser options, or NULL for defaults.

[nullable]

error

return location for a GError, or NULL

 

Returns

an entity, or NULL on failure or end of input. End of input does not set error .

[nullable][transfer full]


g_mime_parser_construct_message ()

GMimeMessage *
g_mime_parser_construct_message (GMimeParser *parser,
                                 GMimeParserOptions *options,
                                 GError **error);

Constructs a message through EOF or the next mailbox delimiter. The first message operation on a newly assigned stream strips an optional UTF-8 byte-order mark. Mbox and MMDF inputs search for an opening marker. Persistent content may be read between successive message operations.

Fatal syntax, header limits and I/O errors discard partial results and remain sticky until stream replacement. Options are copied per operation. Reentrant or concurrent operations are not supported.

Parameters

parser

a GMimeParser context

 

options

parser options, or NULL for defaults.

[nullable]

error

return location for a GError, or NULL

 

Returns

a message, or NULL on failure or end of input. End of input does not set error .

[nullable][transfer full]


g_mime_parser_get_mbox_marker ()

char *
g_mime_parser_get_mbox_marker (GMimeParser *parser);

Gets the opening From-line, excluding CRLF, of the last successfully constructed mbox message. The next construction operation clears it.

Parameters

parser

a GMimeParser context

 

Returns

the From-line, or NULL if unavailable.

[nullable][transfer full]


g_mime_parser_get_mbox_marker_offset ()

gint64
g_mime_parser_get_mbox_marker_offset (GMimeParser *parser);

Parameters

parser

a GMimeParser context

 

Returns

the last successfully constructed mbox message's opening From-line offset, or -1 if unavailable.


g_mime_parser_get_headers_begin ()

gint64
g_mime_parser_get_headers_begin (GMimeParser *parser);

Parameters

parser

a GMimeParser context

 

Returns

the most recent top-level header block's starting offset, or -1 if unavailable.


g_mime_parser_get_headers_end ()

gint64
g_mime_parser_get_headers_end (GMimeParser *parser);

Parameters

parser

a GMimeParser context

 

Returns

the start of the most recent top-level header block's blank separator, or -1 if no separator was encountered.

Types and Values

GMimeParser

typedef struct _GMimeParser GMimeParser;

A MIME parser context.


enum GMimeFormat

The input format used by a reader or parser.

Members

GMIME_FORMAT_MESSAGE

The stream contains a single message.

 

GMIME_FORMAT_MBOX

The stream is in the UNIX mbox format.

 

GMIME_FORMAT_MMDF

The stream is in the MMDF format.