message-parser.c revision 5e63deb8afbcbf466abb84acc3fb28a1e33ab17a
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen/* Copyright (C) 2002 Timo Sirainen */
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainenmessage_parse_part_header(struct message_parser_ctx *parser_ctx);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainenmessage_parse_part_body(struct message_parser_ctx *parser_ctx);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainenmessage_parse_body(struct message_parser_ctx *parser_ctx,
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen struct message_size *msg_size, int *has_nuls);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainenmessage_skip_boundary(struct message_parser_ctx *parser_ctx,
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen struct message_size *boundary_size, int *has_nuls);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainenstatic void message_size_add_part(struct message_size *dest,
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen dest->lines += part->header_size.lines + part->body_size.lines;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainenmessage_part_append(pool_t pool, struct message_part *parent)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen /* set child position */
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainenstatic void parse_content_type(const unsigned char *value, size_t value_len,
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen struct message_parser_ctx *parser_ctx = context;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen const char *str;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen if (parser_ctx->last_content_type != NULL || value_len == 0)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen p_strndup(parser_ctx->parser_pool, value, value_len);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen parser_ctx->part->flags |= MESSAGE_PART_FLAG_MESSAGE_RFC822;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen parser_ctx->part->flags |= MESSAGE_PART_FLAG_TEXT;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen else if (strncasecmp(str, "multipart/", 10) == 0) {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen parser_ctx->part->flags |= MESSAGE_PART_FLAG_MULTIPART;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainenparse_content_type_param(const unsigned char *name, size_t name_len,
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen struct message_parser_ctx *parser_ctx = context;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen if ((parser_ctx->part->flags & MESSAGE_PART_FLAG_MULTIPART) == 0 ||
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen name_len != 8 || memcasecmp(name, "boundary", 8) != 0)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen p_strndup(parser_ctx->parser_pool, value, value_len);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainenmessage_parse_multipart(struct message_parser_ctx *parser_ctx)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen struct message_part *parent_part, *next_part, *part;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen /* multipart message. add new boundary */
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen b = p_new(parser_ctx->parser_pool, struct message_boundary, 1);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen /* reset fields */
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen /* skip the data before the first boundary */
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen next_part = message_skip_boundary(parser_ctx, parser_ctx->boundaries,
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen parent_part->flags |= MESSAGE_PART_FLAG_HAS_NULS;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen /* now, parse the parts */
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen /* new child */
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen part = message_part_append(parser_ctx->part_pool, parent_part);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen if ((parent_part->flags & MESSAGE_PART_FLAG_IS_MIME) != 0)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen next_part = message_parse_part_body(parser_ctx);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen if ((part->flags & MESSAGE_PART_FLAG_HAS_NULS) != 0) {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen /* it also belongs to parent */
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen parent_part->flags |= MESSAGE_PART_FLAG_HAS_NULS;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen /* update our size */
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen message_size_add_part(&parent_part->body_size, part);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen /* skip the boundary */
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen parent_part->flags |= MESSAGE_PART_FLAG_HAS_NULS;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen /* remove boundary */
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen (MESSAGE_PART_FLAG_MESSAGE_RFC822 | MESSAGE_PART_FLAG_MULTIPART)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainenstatic void message_parse_part_header(struct message_parser_ctx *parser_ctx)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen hdr_ctx = message_parse_header_init(parser_ctx->input,
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen while ((hdr = message_parse_header_next(hdr_ctx)) != NULL) {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen /* call the user-defined header parser */
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen parser_ctx->callback(part, hdr, parser_ctx->context);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen if (!hdr->eoh && strcasecmp(hdr->name, "Mime-Version") == 0) {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen /* it's MIME. Content-* headers are valid */
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen if (!hdr->eoh && strcasecmp(hdr->name, "Content-Type") == 0) {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen /* we need to know the boundary */
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen if ((part->flags & MESSAGE_PART_FLAG_IS_MIME) == 0) {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen /* It's not MIME. Reset everything we found from
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen Content-Type. */
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen parser_ctx->callback(part, NULL, parser_ctx->context);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen i_assert((part->flags & MUTEX_FLAGS) != MUTEX_FLAGS);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainenmessage_parse_part_body(struct message_parser_ctx *parser_ctx)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen /* when there's no content-type specified and we're
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen below multipart/digest, the assume message/rfc822
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen content-type */
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen part->flags |= MESSAGE_PART_FLAG_MESSAGE_RFC822;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen /* otherwise we default to text/plain */
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen if (part->flags & MESSAGE_PART_FLAG_MESSAGE_RFC822) {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen /* message/rfc822 part - the message body begins with
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen headers again, this works pretty much the same as
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen a single multipart/mixed item */
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen part = message_part_append(parser_ctx->part_pool, part);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen next_part = message_parse_part_body(parser_ctx);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen /* our body size is the size of header+body in message/rfc822 */
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen message_size_add_part(&part->parent->body_size, part);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen /* normal message, read until the next boundary */
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen if ((part->flags & MESSAGE_PART_FLAG_HAS_NULS) != 0 &&
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen /* it also belongs to parent */
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen part->parent->flags |= MESSAGE_PART_FLAG_HAS_NULS;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainenstatic void message_skip_line(struct istream *input,
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen const unsigned char *msg;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen while (i_stream_read_data(input, &msg, &size, startpos) > 0) {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen /* leave the last character, it may be \r */
static struct message_boundary *
return boundaries;
return NULL;
static struct message_boundary *
int *has_nuls)
const unsigned char *msg;
i = line_start;
line_start -= i;
if (skip_over) {
line_start--;
line_start--;
return boundary;
static struct message_part *
static struct message_part *
const unsigned char *msg;
int end_boundary;
return NULL;
if (end_boundary) {
struct message_parser_ctx *
return ctx;
return parts;
void *context)
void *context)
void *context)
void *context)
struct message_header_parser_ctx *
return ctx;
struct message_header_line *
const unsigned char *msg;
int ret;
return NULL;
colon_pos = 0;
if (ret != 0) {
return NULL;
size = 0;
size = i;
colon_pos = i;
if (i < parse_size) {
size = i;
startpos = i;
if (size == 0) {
colon_pos--;
return line;