diff --git a/src/lib-sieve/util/edit-mail.c b/src/lib-sieve/util/edit-mail.c index 4040d83052a2d2887e92b042cb7119550f04f24c..3382b7a71eaa2266bae6fef36c5c5849cd285821 100644 --- a/src/lib-sieve/util/edit-mail.c +++ b/src/lib-sieve/util/edit-mail.c @@ -35,8 +35,8 @@ static struct mail_vfuncs edit_mail_vfuncs; struct edit_mail_istream; struct istream *edit_mail_istream_create(struct edit_mail *edmail); -static struct _header_index *edit_mail_header_clone - (struct edit_mail *edmail, struct _header *header); +static struct _header_index * +edit_mail_header_clone(struct edit_mail *edmail, struct _header *header); /* * Raw storage @@ -47,10 +47,12 @@ static unsigned int edit_mail_refcount = 0; static struct mail_user *edit_mail_raw_storage_get(struct mail_user *mail_user) { - if ( edit_mail_user == NULL ) { - void **sets = master_service_settings_get_others(master_service); + if (edit_mail_user == NULL) { + void **sets = + master_service_settings_get_others(master_service); - edit_mail_user = raw_storage_create_from_set(mail_user->set_info, sets[0]); + edit_mail_user = raw_storage_create_from_set( + mail_user->set_info, sets[0]); } edit_mail_refcount++; @@ -62,7 +64,7 @@ static void edit_mail_raw_storage_drop(void) { i_assert(edit_mail_refcount > 0); - if ( --edit_mail_refcount != 0) + if (--edit_mail_refcount != 0) return; mail_user_unref(&edit_mail_user); @@ -130,8 +132,8 @@ static inline void _header_ref(struct _header *header) static inline void _header_unref(struct _header *header) { - i_assert( header->refcount > 0 ); - if ( --header->refcount != 0 ) + i_assert(header->refcount > 0); + if (--header->refcount != 0) return; i_free(header->name); @@ -145,7 +147,7 @@ static inline struct _header_field *_header_field_create(struct _header *header) hfield = i_new(struct _header_field, 1); hfield->refcount = 1; hfield->header = header; - if ( header != NULL ) + if (header != NULL) _header_ref(header); return hfield; @@ -158,16 +160,16 @@ static inline void _header_field_ref(struct _header_field *hfield) static inline void _header_field_unref(struct _header_field *hfield) { - i_assert( hfield->refcount > 0 ); - if ( --hfield->refcount != 0 ) + i_assert(hfield->refcount > 0); + if (--hfield->refcount != 0) return; - if ( hfield->header != NULL ) + if (hfield->header != NULL) _header_unref(hfield->header); - if ( hfield->data != NULL ) + if (hfield->data != NULL) i_free(hfield->data); - if ( hfield->utf8_value != NULL ) + if (hfield->utf8_value != NULL) i_free(hfield->utf8_value); i_free(hfield); } @@ -215,16 +217,15 @@ struct edit_mail *edit_mail_wrap(struct mail *mail) uoff_t size_diff; pool_t pool; - if ( mail_get_stream(mail, &hdr_size, &body_size, &wrapped_stream) < 0 ) { + if (mail_get_stream(mail, &hdr_size, &body_size, &wrapped_stream) < 0) return NULL; - } /* Create dummy raw mailbox for our wrapper */ raw_mail_user = edit_mail_raw_storage_get(mail->box->storage->user); - if ( raw_mailbox_alloc_stream(raw_mail_user, wrapped_stream, (time_t)-1, - "editor@example.com", &raw_box) < 0 ) { + if (raw_mailbox_alloc_stream(raw_mail_user, wrapped_stream, (time_t)-1, + "editor@example.com", &raw_box) < 0) { i_error("edit-mail: failed to open raw box: %s", mailbox_get_last_internal_error(raw_box, NULL)); mailbox_free(&raw_box); @@ -248,10 +249,11 @@ struct edit_mail *edit_mail_wrap(struct mail *mail) edmail->wrapped_stream = wrapped_stream; i_stream_ref(edmail->wrapped_stream); - /* Determine whether we should use CRLF or LF for the physical message */ - size_diff = (hdr_size.virtual_size + body_size.virtual_size) - - (hdr_size.physical_size + body_size.physical_size); - if ( size_diff == 0 || size_diff <= (hdr_size.lines + body_size.lines)/2 ) + /* Determine whether we should use CRLF or LF for the physical message + */ + size_diff = ((hdr_size.virtual_size + body_size.virtual_size) - + (hdr_size.physical_size + body_size.physical_size)); + if (size_diff == 0 || size_diff <= (hdr_size.lines + body_size.lines)/2) edmail->crlf = edmail->eoh_crlf = TRUE; array_create(&edmail->mail.module_contexts, pool, sizeof(void *), 5); @@ -272,9 +274,8 @@ struct edit_mail *edit_mail_snapshot(struct edit_mail *edmail) struct edit_mail *edmail_new; pool_t pool; - if ( !edmail->snapshot_modified ) { + if (!edmail->snapshot_modified) return edmail; - } pool = pool_alloconly_create("edit_mail", 1024); edmail_new = p_new(pool, struct edit_mail, 1); @@ -294,7 +295,8 @@ struct edit_mail *edit_mail_snapshot(struct edit_mail *edmail) edmail_new->crlf = edmail->crlf; edmail_new->eoh_crlf = edmail->eoh_crlf; - array_create(&edmail_new->mail.module_contexts, pool, sizeof(void *), 5); + array_create(&edmail_new->mail.module_contexts, pool, + sizeof(void *), 5); edmail_new->mail.v = edit_mail_vfuncs; edmail_new->mail.mail.seq = 1; @@ -305,31 +307,33 @@ struct edit_mail *edit_mail_snapshot(struct edit_mail *edmail) edmail_new->stream = NULL; - if ( edmail->modified ) { + if (edmail->modified) { field_idx = edmail->header_fields_head; - while ( field_idx != NULL ) { + while (field_idx != NULL) { struct _header_field_index *next = field_idx->next; field_idx_new = i_new(struct _header_field_index, 1); - field_idx_new->header = - edit_mail_header_clone(edmail_new, field_idx->header->header); + field_idx_new->header = edit_mail_header_clone( + edmail_new, field_idx->header->header); field_idx_new->field = field_idx->field; _header_field_ref(field_idx_new->field); - DLLIST2_APPEND - (&edmail_new->header_fields_head, &edmail_new->header_fields_tail, - field_idx_new); + DLLIST2_APPEND(&edmail_new->header_fields_head, + &edmail_new->header_fields_tail, + field_idx_new); field_idx_new->header->count++; - if ( field_idx->header->first == field_idx ) + if (field_idx->header->first == field_idx) field_idx_new->header->first = field_idx_new; - if ( field_idx->header->last == field_idx ) + if (field_idx->header->last == field_idx) field_idx_new->header->last = field_idx_new; - if ( field_idx == edmail->header_fields_appended ) - edmail_new->header_fields_appended = field_idx_new; + if (field_idx == edmail->header_fields_appended) { + edmail_new->header_fields_appended = + field_idx_new; + } field_idx = next; } @@ -338,9 +342,7 @@ struct edit_mail *edit_mail_snapshot(struct edit_mail *edmail) } edmail_new->headers_parsed = edmail->headers_parsed; - edmail_new->parent = edmail; - //edmail->refcount++; return edmail_new; } @@ -353,7 +355,7 @@ void edit_mail_reset(struct edit_mail *edmail) i_stream_unref(&edmail->stream); field_idx = edmail->header_fields_head; - while ( field_idx != NULL ) { + while (field_idx != NULL) { struct _header_field_index *next = field_idx->next; _header_field_unref(field_idx->field); @@ -363,7 +365,7 @@ void edit_mail_reset(struct edit_mail *edmail) } header_idx = edmail->headers_head; - while ( header_idx != NULL ) { + while (header_idx != NULL) { struct _header_index *next = header_idx->next; _header_unref(header_idx->header); @@ -379,8 +381,8 @@ void edit_mail_unwrap(struct edit_mail **edmail) { struct edit_mail *parent; - i_assert( (*edmail)->refcount > 0 ); - if ( --(*edmail)->refcount != 0 ) + i_assert((*edmail)->refcount > 0); + if (--(*edmail)->refcount != 0) return; edit_mail_reset(*edmail); @@ -388,7 +390,7 @@ void edit_mail_unwrap(struct edit_mail **edmail) parent = (*edmail)->parent; - if ( parent == NULL ) { + if (parent == NULL) { mailbox_transaction_rollback(&(*edmail)->mail.mail.transaction); mailbox_free(&(*edmail)->mail.mail.box); edit_mail_raw_storage_drop(); @@ -397,14 +399,14 @@ void edit_mail_unwrap(struct edit_mail **edmail) pool_unref(&(*edmail)->mail.pool); *edmail = NULL; - if ( parent != NULL ) + if (parent != NULL) edit_mail_unwrap(&parent); } struct mail *edit_mail_get_mail(struct edit_mail *edmail) { /* Return wrapped mail when nothing is modified yet */ - if ( !edmail->modified ) + if (!edmail->modified) return &edmail->wrapped->mail; return &edmail->mail.mail; @@ -423,29 +425,27 @@ static inline void edit_mail_modify(struct edit_mail *edmail) /* Header modification */ -static inline char *_header_value_unfold -(const char *value) +static inline char *_header_value_unfold(const char *value) { string_t *out; unsigned int i; - for ( i = 0; value[i] != '\0'; i++ ) { + for (i = 0; value[i] != '\0'; i++) { if (value[i] == '\r' || value[i] == '\n') break; } - if ( value[i] == '\0' ) { + if (value[i] == '\0') return i_strdup(value); - } out = t_str_new(i + strlen(value+i) + 10); str_append_data(out, value, i); - for ( ; value[i] != '\0'; i++ ) { + for (; value[i] != '\0'; i++) { if (value[i] == '\n') { i++; if (value[i] == '\0') break; - switch ( value[i] ) { + switch (value[i]) { case ' ': str_append_c(out, ' '); break; @@ -462,14 +462,14 @@ static inline char *_header_value_unfold return i_strndup(str_c(out), str_len(out)); } -static struct _header_index *edit_mail_header_find -(struct edit_mail *edmail, const char *field_name) +static struct _header_index * +edit_mail_header_find(struct edit_mail *edmail, const char *field_name) { struct _header_index *header_idx; header_idx = edmail->headers_head; - while ( header_idx != NULL ) { - if ( strcasecmp(header_idx->header->name, field_name) == 0 ) + while (header_idx != NULL) { + if (strcasecmp(header_idx->header->name, field_name) == 0) return header_idx; header_idx = header_idx->next; @@ -478,29 +478,31 @@ static struct _header_index *edit_mail_header_find return NULL; } -static struct _header_index *edit_mail_header_create -(struct edit_mail *edmail, const char *field_name) +static struct _header_index * +edit_mail_header_create(struct edit_mail *edmail, const char *field_name) { struct _header_index *header_idx; - if ( (header_idx=edit_mail_header_find(edmail, field_name)) == NULL ) { + header_idx = edit_mail_header_find(edmail, field_name); + if (header_idx == NULL) { header_idx = i_new(struct _header_index, 1); header_idx->header = _header_create(field_name); - DLLIST2_APPEND(&edmail->headers_head, &edmail->headers_tail, header_idx); + DLLIST2_APPEND(&edmail->headers_head, &edmail->headers_tail, + header_idx); } return header_idx; } -static struct _header_index *edit_mail_header_clone -(struct edit_mail *edmail, struct _header *header) +static struct _header_index * +edit_mail_header_clone(struct edit_mail *edmail, struct _header *header) { struct _header_index *header_idx; header_idx = edmail->headers_head; - while ( header_idx != NULL ) { - if ( header_idx->header == header ) + while (header_idx != NULL) { + if (header_idx->header == header) return header_idx; header_idx = header_idx->next; @@ -509,14 +511,15 @@ static struct _header_index *edit_mail_header_clone header_idx = i_new(struct _header_index, 1); header_idx->header = header; _header_ref(header); - DLLIST2_APPEND(&edmail->headers_head, &edmail->headers_tail, header_idx); + DLLIST2_APPEND(&edmail->headers_head, &edmail->headers_tail, + header_idx); return header_idx; } static struct _header_field_index * -edit_mail_header_field_create -(struct edit_mail *edmail, const char *field_name, const char *value) +edit_mail_header_field_create(struct edit_mail *edmail, const char *field_name, + const char *value) { struct _header_index *header_idx; struct _header *header; @@ -542,13 +545,15 @@ edit_mail_header_field_create message_header_encode(value, enc_value); - lines = rfc2822_header_append - (data, field_name, str_c(enc_value), edmail->crlf, &field->body_offset); + lines = rfc2822_header_append(data, field_name, + str_c(enc_value), edmail->crlf, + &field->body_offset); /* Copy to new field */ field->data = i_strndup(str_data(data), str_len(data)); field->size = str_len(data); - field->virtual_size = ( edmail->crlf ? field->size : field->size + lines ); + field->virtual_size = (edmail->crlf ? + field->size : field->size + lines); field->lines = lines; } T_END; @@ -558,70 +563,73 @@ edit_mail_header_field_create return field_idx; } -static void edit_mail_header_field_delete -(struct edit_mail *edmail, struct _header_field_index *field_idx, - bool update_index) +static void +edit_mail_header_field_delete(struct edit_mail *edmail, + struct _header_field_index *field_idx, + bool update_index) { struct _header_index *header_idx = field_idx->header; struct _header_field *field = field_idx->field; - i_assert( header_idx != NULL ); + i_assert(header_idx != NULL); edmail->hdr_size.physical_size -= field->size; edmail->hdr_size.virtual_size -= field->virtual_size; edmail->hdr_size.lines -= field->lines; header_idx->count--; - if ( update_index ) { - if ( header_idx->count == 0 ) { - DLLIST2_REMOVE(&edmail->headers_head, &edmail->headers_tail, header_idx); + if (update_index) { + if (header_idx->count == 0) { + DLLIST2_REMOVE(&edmail->headers_head, + &edmail->headers_tail, header_idx); _header_unref(header_idx->header); i_free(header_idx); - } else if ( header_idx->first == field_idx ) { - struct _header_field_index *hfield = header_idx->first->next; + } else if (header_idx->first == field_idx) { + struct _header_field_index *hfield = + header_idx->first->next; - while ( hfield != NULL && hfield->header != header_idx ) { + while (hfield != NULL && hfield->header != header_idx) hfield = hfield->next; - } - i_assert( hfield != NULL ); + i_assert(hfield != NULL); header_idx->first = hfield; - } else if ( header_idx->last == field_idx ) { - struct _header_field_index *hfield = header_idx->last->prev; + } else if (header_idx->last == field_idx) { + struct _header_field_index *hfield = + header_idx->last->prev; - while ( hfield != NULL && hfield->header != header_idx ) { + while (hfield != NULL && hfield->header != header_idx) hfield = hfield->prev; - } - i_assert( hfield != NULL ); + i_assert(hfield != NULL); header_idx->last = hfield; } } - DLLIST2_REMOVE - (&edmail->header_fields_head, &edmail->header_fields_tail, field_idx); + DLLIST2_REMOVE(&edmail->header_fields_head, &edmail->header_fields_tail, + field_idx); _header_field_unref(field_idx->field); i_free(field_idx); } static struct _header_field_index * -edit_mail_header_field_replace -(struct edit_mail *edmail, struct _header_field_index *field_idx, - const char *newname, const char *newvalue, bool update_index) +edit_mail_header_field_replace(struct edit_mail *edmail, + struct _header_field_index *field_idx, + const char *newname, const char *newvalue, + bool update_index) { struct _header_field_index *field_idx_new; struct _header_index *header_idx = field_idx->header, *header_idx_new; struct _header_field *field = field_idx->field, *field_new; - i_assert( header_idx != NULL ); - i_assert( newname != NULL || newvalue != NULL ); + i_assert(header_idx != NULL); + i_assert(newname != NULL || newvalue != NULL); - if ( newname == NULL ) + if (newname == NULL) newname = header_idx->header->name; - if ( newvalue == NULL ) + if (newvalue == NULL) newvalue = field_idx->field->utf8_value; - field_idx_new = edit_mail_header_field_create - (edmail, newname, newvalue); + field_idx_new = edit_mail_header_field_create( + edmail, newname, newvalue); field_new = field_idx_new->field; header_idx_new = field_idx_new->header; @@ -636,16 +644,16 @@ edit_mail_header_field_replace /* Replace header field index */ field_idx_new->prev = field_idx->prev; field_idx_new->next = field_idx->next; - if ( field_idx->prev != NULL ) + if (field_idx->prev != NULL) field_idx->prev->next = field_idx_new; - if ( field_idx->next != NULL ) + if (field_idx->next != NULL) field_idx->next->prev = field_idx_new; if (edmail->header_fields_head == field_idx) edmail->header_fields_head = field_idx_new; if (edmail->header_fields_tail == field_idx) edmail->header_fields_tail = field_idx_new; - if ( header_idx_new == header_idx ) { + if (header_idx_new == header_idx) { if (header_idx->first == field_idx) header_idx->first = field_idx_new; if (header_idx->last == field_idx) @@ -654,47 +662,51 @@ edit_mail_header_field_replace header_idx->count--; header_idx_new->count++; - if ( update_index ) { - if ( header_idx->count == 0 ) { - DLLIST2_REMOVE(&edmail->headers_head, &edmail->headers_tail, header_idx); + if (update_index) { + if (header_idx->count == 0) { + DLLIST2_REMOVE(&edmail->headers_head, + &edmail->headers_tail, + header_idx); _header_unref(header_idx->header); i_free(header_idx); - } else if ( header_idx->first == field_idx ) { - struct _header_field_index *hfield = header_idx->first->next; + } else if (header_idx->first == field_idx) { + struct _header_field_index *hfield = + header_idx->first->next; - while ( hfield != NULL && hfield->header != header_idx ) { + while (hfield != NULL && + hfield->header != header_idx) hfield = hfield->next; - } - i_assert( hfield != NULL ); + i_assert(hfield != NULL); header_idx->first = hfield; - } else if ( header_idx->last == field_idx ) { - struct _header_field_index *hfield = header_idx->last->prev; + } else if (header_idx->last == field_idx) { + struct _header_field_index *hfield = + header_idx->last->prev; - while ( hfield != NULL && hfield->header != header_idx ) { + while (hfield != NULL && + hfield->header != header_idx) hfield = hfield->prev; - } - i_assert( hfield != NULL ); + i_assert(hfield != NULL); header_idx->last = hfield; } - if ( header_idx_new->count > 0 ) { + if (header_idx_new->count > 0) { struct _header_field_index *hfield; hfield = edmail->header_fields_head; - while ( hfield != NULL && hfield->header != header_idx_new ) { + while (hfield != NULL && + hfield->header != header_idx_new) hfield = hfield->next; - } - i_assert( hfield != NULL ); + i_assert(hfield != NULL); header_idx_new->first = hfield; hfield = edmail->header_fields_tail; - while ( hfield != NULL && hfield->header != header_idx_new ) { + while (hfield != NULL && + hfield->header != header_idx_new) hfield = hfield->prev; - } - i_assert( hfield != NULL ); + i_assert(hfield != NULL); header_idx_new->last = hfield; } } @@ -705,21 +717,20 @@ edit_mail_header_field_replace return field_idx_new; } -static inline char *_header_decode -(const unsigned char *hdr_data, size_t hdr_data_len) +static inline char * +_header_decode(const unsigned char *hdr_data, size_t hdr_data_len) { string_t *str = t_str_new(512); /* hdr_data is already unfolded */ /* Decode MIME encoded-words. */ - message_header_decode_utf8 - ((const unsigned char *)hdr_data, hdr_data_len, str, NULL); + message_header_decode_utf8((const unsigned char *)hdr_data, + hdr_data_len, str, NULL); return i_strdup(str_c(str)); } -static int edit_mail_headers_parse -(struct edit_mail *edmail) +static int edit_mail_headers_parse(struct edit_mail *edmail) { struct message_header_parser_ctx *hparser; enum message_header_parser_flags hparser_flags = @@ -733,50 +744,57 @@ static int edit_mail_headers_parse unsigned int lines = 0; int ret; - if ( edmail->headers_parsed ) return 1; + if (edmail->headers_parsed) + return 1; i_stream_seek(edmail->wrapped_stream, 0); - hparser = message_parse_header_init - (edmail->wrapped_stream, NULL, hparser_flags); + hparser = message_parse_header_init(edmail->wrapped_stream, NULL, + hparser_flags); T_BEGIN { hdr_data = t_str_new(1024); - while ( (ret=message_parse_header_next(hparser, &hdr)) > 0 ) { + while ((ret = message_parse_header_next(hparser, &hdr)) > 0) { struct _header_field_index *field_idx_new; struct _header_field *field; - if ( hdr->eoh ) { + if (hdr->eoh) { /* Record whether header ends in CRLF or LF */ edmail->eoh_crlf = hdr->crlf_newline; } - if ( hdr == NULL || hdr->eoh ) break; + if (hdr == NULL || hdr->eoh) + break; - /* We deny the existence of any `Content-Length:' header. This header is - * non-standard and it can wreak havok when the message is modified. + /* We deny the existence of any `Content-Length:' + header. This header is non-standard and it can wreak + havok when the message is modified. */ - if ( strcasecmp(hdr->name, "Content-Length" ) == 0 ) + if (strcasecmp(hdr->name, "Content-Length" ) == 0) continue; - if ( hdr->continued ) { + if (hdr->continued) { /* Continued line of folded header */ - buffer_append(hdr_data, hdr->value, hdr->value_len); + buffer_append(hdr_data, hdr->value, + hdr->value_len); } else { /* First line of header */ offset = hdr->name_offset; body_offset = hdr->name_len + hdr->middle_len; str_truncate(hdr_data, 0); - buffer_append(hdr_data, hdr->name, hdr->name_len); - buffer_append(hdr_data, hdr->middle, hdr->middle_len); - buffer_append(hdr_data, hdr->value, hdr->value_len); + buffer_append(hdr_data, hdr->name, + hdr->name_len); + buffer_append(hdr_data, hdr->middle, + hdr->middle_len); + buffer_append(hdr_data, hdr->value, + hdr->value_len); lines = 0; vsize_diff = 0; } - if ( !hdr->no_newline ) { + if (!hdr->no_newline) { lines++; - if ( hdr->crlf_newline ) { + if (hdr->crlf_newline) { buffer_append(hdr_data, "\r\n", 2); } else { buffer_append(hdr_data, "\n", 1); @@ -784,7 +802,7 @@ static int edit_mail_headers_parse } } - if ( hdr->continues ) { + if (hdr->continues) { hdr->use_full_value = TRUE; continue; } @@ -796,16 +814,19 @@ static int edit_mail_headers_parse header_idx = edit_mail_header_create(edmail, hdr->name); header_idx->count++; field_idx_new->header = header_idx; - field_idx_new->field = field = _header_field_create(header_idx->header); + field_idx_new->field = field = + _header_field_create(header_idx->header); - i_assert( body_offset > 0 ); + i_assert(body_offset > 0); field->body_offset = body_offset; - field->utf8_value = _header_decode(hdr->full_value, hdr->full_value_len); + field->utf8_value = _header_decode(hdr->full_value, + hdr->full_value_len); field->size = str_len(hdr_data); field->virtual_size = field->size + vsize_diff; - field->data = i_strndup(str_data(hdr_data), field->size); + field->data = i_strndup(str_data(hdr_data), + field->size); field->offset = offset; field->lines = lines; @@ -819,16 +840,16 @@ static int edit_mail_headers_parse message_parse_header_deinit(&hparser); - /* blocking i/o required */ - i_assert( ret != 0 ); + /* Blocking i/o required */ + i_assert(ret != 0); - if ( ret < 0 && edmail->wrapped_stream->stream_errno != 0 ) { + if (ret < 0 && edmail->wrapped_stream->stream_errno != 0) { /* Error; clean up */ i_error("read(%s) failed: %s", i_stream_get_name(edmail->wrapped_stream), i_stream_get_error(edmail->wrapped_stream)); current = head; - while ( current != NULL ) { + while (current != NULL) { struct _header_field_index *next = current->next; _header_field_unref(current->field); @@ -841,9 +862,10 @@ static int edit_mail_headers_parse } /* Insert header field index items in main list */ - if ( head != NULL && tail != NULL ) { - if ( edmail->header_fields_appended != NULL ) { - if ( edmail->header_fields_head != edmail->header_fields_appended ) { + if (head != NULL && tail != NULL) { + if (edmail->header_fields_appended != NULL) { + if (edmail->header_fields_head != + edmail->header_fields_appended) { edmail->header_fields_appended->prev->next = head; head->prev = edmail->header_fields_appended->prev; } else { @@ -852,7 +874,7 @@ static int edit_mail_headers_parse tail->next = edmail->header_fields_appended; edmail->header_fields_appended->prev = tail; - } else if ( edmail->header_fields_tail != NULL ) { + } else if (edmail->header_fields_tail != NULL) { edmail->header_fields_tail->next = head; head->prev = edmail->header_fields_tail; edmail->header_fields_tail = tail; @@ -864,8 +886,8 @@ static int edit_mail_headers_parse /* Rebuild header index */ current = edmail->header_fields_head; - while ( current != NULL ) { - if ( current->header->first == NULL ) + while (current != NULL) { + if (current->header->first == NULL) current->header->first = current; current->header->last = current; @@ -884,9 +906,8 @@ static int edit_mail_headers_parse return 1; } -void edit_mail_header_add -(struct edit_mail *edmail, const char *field_name, const char *value, - bool last) +void edit_mail_header_add(struct edit_mail *edmail, const char *field_name, + const char *value, bool last) { struct _header_index *header_idx; struct _header_field_index *field_idx; @@ -899,16 +920,16 @@ void edit_mail_header_add field = field_idx->field; /* Add it to the header field index */ - if ( last ) { - DLLIST2_APPEND - (&edmail->header_fields_head, &edmail->header_fields_tail, field_idx); + if (last) { + DLLIST2_APPEND(&edmail->header_fields_head, + &edmail->header_fields_tail, field_idx); header_idx->last = field_idx; - if ( header_idx->first == NULL ) + if (header_idx->first == NULL) header_idx->first = field_idx; - if ( !edmail->headers_parsed ) { - if ( edmail->header_fields_appended == NULL ) { + if (!edmail->headers_parsed) { + if (edmail->header_fields_appended == NULL) { /* Record beginning of appended headers */ edmail->header_fields_appended = field_idx; } @@ -918,11 +939,11 @@ void edit_mail_header_add edmail->appended_hdr_size.lines += field->lines; } } else { - DLLIST2_PREPEND - (&edmail->header_fields_head, &edmail->header_fields_tail, field_idx); + DLLIST2_PREPEND(&edmail->header_fields_head, + &edmail->header_fields_tail, field_idx); header_idx->first = field_idx; - if ( header_idx->last == NULL ) + if (header_idx->last == NULL) header_idx->last = field_idx; } @@ -933,8 +954,8 @@ void edit_mail_header_add edmail->hdr_size.lines += field->lines; } -int edit_mail_header_delete -(struct edit_mail *edmail, const char *field_name, int index) +int edit_mail_header_delete(struct edit_mail *edmail, const char *field_name, + int index) { struct _header_index *header_idx; struct _header_field_index *field_idx; @@ -942,11 +963,12 @@ int edit_mail_header_delete int ret = 0; /* Make sure headers are parsed */ - if ( edit_mail_headers_parse(edmail) <= 0 ) + if (edit_mail_headers_parse(edmail) <= 0) return -1; /* Find the header entry */ - if ( (header_idx=edit_mail_header_find(edmail, field_name)) == NULL ) { + header_idx = edit_mail_header_find(edmail, field_name); + if (header_idx == NULL) { /* Not found */ return 0; } @@ -955,46 +977,51 @@ int edit_mail_header_delete edit_mail_modify(edmail); /* Iterate through all header fields and remove those that match */ - field_idx = ( index >= 0 ? header_idx->first : header_idx->last ); - while ( field_idx != NULL ) { + field_idx = (index >= 0 ? header_idx->first : header_idx->last); + while (field_idx != NULL) { struct _header_field_index *next = - ( index >= 0 ? field_idx->next : field_idx->prev ); + (index >= 0 ? field_idx->next : field_idx->prev); - if ( field_idx->field->header == header_idx->header ) { + if (field_idx->field->header == header_idx->header) { bool final; - if ( index >= 0 ) { + if (index >= 0) { pos++; - final = ( header_idx->last == field_idx ); + final = (header_idx->last == field_idx); } else { pos--; - final = ( header_idx->first == field_idx ); + final = (header_idx->first == field_idx); } - if ( index == 0 || index == pos ) { - if ( header_idx->first == field_idx ) header_idx->first = NULL; - if ( header_idx->last == field_idx ) header_idx->last = NULL; - edit_mail_header_field_delete(edmail, field_idx, FALSE); + if (index == 0 || index == pos) { + if (header_idx->first == field_idx) + header_idx->first = NULL; + if (header_idx->last == field_idx) + header_idx->last = NULL; + edit_mail_header_field_delete( + edmail, field_idx, FALSE); ret++; } - if ( final || (index != 0 && index == pos) ) + if (final || (index != 0 && index == pos)) break; } field_idx = next; } - if ( index == 0 || header_idx->count == 0 ) { - DLLIST2_REMOVE(&edmail->headers_head, &edmail->headers_tail, header_idx); + if (index == 0 || header_idx->count == 0) { + DLLIST2_REMOVE(&edmail->headers_head, + &edmail->headers_tail, header_idx); _header_unref(header_idx->header); i_free(header_idx); - } else if ( header_idx->first == NULL || header_idx->last == NULL ) { - struct _header_field_index *current = edmail->header_fields_head; + } else if (header_idx->first == NULL || header_idx->last == NULL) { + struct _header_field_index *current = + edmail->header_fields_head; - while ( current != NULL ) { - if ( current->header == header_idx ) { - if ( header_idx->first == NULL ) + while (current != NULL) { + if (current->header == header_idx) { + if (header_idx->first == NULL) header_idx->first = current; header_idx->last = current; } @@ -1005,9 +1032,9 @@ int edit_mail_header_delete return ret; } -int edit_mail_header_replace -(struct edit_mail *edmail, const char *field_name, int index, - const char *newname, const char *newvalue) +int edit_mail_header_replace(struct edit_mail *edmail, + const char *field_name, int index, + const char *newname, const char *newvalue) { struct _header_index *header_idx, *header_idx_new; struct _header_field_index *field_idx, *field_idx_new; @@ -1015,11 +1042,12 @@ int edit_mail_header_replace int ret = 0; /* Make sure headers are parsed */ - if ( edit_mail_headers_parse(edmail) <= 0 ) + if (edit_mail_headers_parse(edmail) <= 0) return -1; /* Find the header entry */ - if ( (header_idx=edit_mail_header_find(edmail, field_name)) == NULL ) { + header_idx = edit_mail_header_find(edmail, field_name); + if (header_idx == NULL) { /* Not found */ return 0; } @@ -1028,32 +1056,35 @@ int edit_mail_header_replace edit_mail_modify(edmail); /* Iterate through all header fields and replace those that match */ - field_idx = ( index >= 0 ? header_idx->first : header_idx->last ); + field_idx = (index >= 0 ? header_idx->first : header_idx->last); field_idx_new = NULL; - while ( field_idx != NULL ) { + while (field_idx != NULL) { struct _header_field_index *next = - ( index >= 0 ? field_idx->next : field_idx->prev ); + (index >= 0 ? field_idx->next : field_idx->prev); - if ( field_idx->field->header == header_idx->header ) { + if (field_idx->field->header == header_idx->header) { bool final; - if ( index >= 0 ) { + if (index >= 0) { pos++; - final = ( header_idx->last == field_idx ); + final = (header_idx->last == field_idx); } else { pos--; - final = ( header_idx->first == field_idx ); + final = (header_idx->first == field_idx); } - if ( index == 0 || index == pos ) { - if ( header_idx->first == field_idx ) header_idx->first = NULL; - if ( header_idx->last == field_idx ) header_idx->last = NULL; - field_idx_new = edit_mail_header_field_replace - (edmail, field_idx, newname, newvalue, FALSE); + if (index == 0 || index == pos) { + if (header_idx->first == field_idx) + header_idx->first = NULL; + if (header_idx->last == field_idx) + header_idx->last = NULL; + field_idx_new = edit_mail_header_field_replace( + edmail, field_idx, newname, newvalue, + FALSE); ret++; } - if ( final || (index != 0 && index == pos) ) + if (final || (index != 0 && index == pos)) break; } @@ -1061,16 +1092,18 @@ int edit_mail_header_replace } /* Update old header index */ - if ( header_idx->count == 0 ) { - DLLIST2_REMOVE(&edmail->headers_head, &edmail->headers_tail, header_idx); + if (header_idx->count == 0) { + DLLIST2_REMOVE(&edmail->headers_head, &edmail->headers_tail, + header_idx); _header_unref(header_idx->header); i_free(header_idx); - } else if ( header_idx->first == NULL || header_idx->last == NULL ) { - struct _header_field_index *current = edmail->header_fields_head; + } else if (header_idx->first == NULL || header_idx->last == NULL) { + struct _header_field_index *current = + edmail->header_fields_head; - while ( current != NULL ) { - if ( current->header == header_idx ) { - if ( header_idx->first == NULL ) + while (current != NULL) { + if (current->header == header_idx) { + if (header_idx->first == NULL) header_idx->first = current; header_idx->last = current; } @@ -1078,14 +1111,15 @@ int edit_mail_header_replace } } - /* Update new header index */ - if ( field_idx_new != NULL ) { - struct _header_field_index *current = edmail->header_fields_head; - - header_idx_new = field_idx_new->header; - while ( current != NULL ) { - if ( current->header == header_idx_new ) { - if ( header_idx_new->first == NULL ) + /* Update new header index */ + if (field_idx_new != NULL) { + struct _header_field_index *current = + edmail->header_fields_head; + + header_idx_new = field_idx_new->header; + while (current != NULL) { + if (current->header == header_idx_new) { + if (header_idx_new->first == NULL) header_idx_new->first = current; header_idx_new->last = current; } @@ -1105,36 +1139,36 @@ struct edit_mail_header_iter bool reverse:1; }; -int edit_mail_headers_iterate_init -(struct edit_mail *edmail, const char *field_name, bool reverse, - struct edit_mail_header_iter **edhiter_r) +int edit_mail_headers_iterate_init(struct edit_mail *edmail, + const char *field_name, bool reverse, + struct edit_mail_header_iter **edhiter_r) { struct edit_mail_header_iter *edhiter; struct _header_index *header_idx = NULL; struct _header_field_index *current = NULL; /* Make sure headers are parsed */ - if ( edit_mail_headers_parse(edmail) <= 0 ) { + if (edit_mail_headers_parse(edmail) <= 0) { /* Failure */ return -1; } header_idx = edit_mail_header_find(edmail, field_name); - if ( field_name != NULL && header_idx == NULL ) { + if (field_name != NULL && header_idx == NULL) { current = NULL; - } else if ( !reverse ) { - current = - ( header_idx != NULL ? header_idx->first : edmail->header_fields_head ); + } else if (!reverse) { + current = (header_idx != NULL ? + header_idx->first : edmail->header_fields_head); } else { - current = - ( header_idx != NULL ? header_idx->last : edmail->header_fields_tail ); - if ( current->header == NULL ) + current = (header_idx != NULL ? + header_idx->last : edmail->header_fields_tail); + if (current->header == NULL) current = current->prev; } - if ( current == NULL ) - return 0; + if (current == NULL) + return 0; edhiter = i_new(struct edit_mail_header_iter, 1); edhiter->mail = edmail; @@ -1146,51 +1180,51 @@ int edit_mail_headers_iterate_init return 1; } -void edit_mail_headers_iterate_deinit -(struct edit_mail_header_iter **edhiter) +void edit_mail_headers_iterate_deinit(struct edit_mail_header_iter **edhiter) { i_free(*edhiter); *edhiter = NULL; } -void edit_mail_headers_iterate_get -(struct edit_mail_header_iter *edhiter, const char **value_r) +void edit_mail_headers_iterate_get(struct edit_mail_header_iter *edhiter, + const char **value_r) { const char *raw; int i; - i_assert( edhiter->current != NULL && edhiter->current->header != NULL); + i_assert(edhiter->current != NULL && edhiter->current->header != NULL); raw = edhiter->current->field->utf8_value; - for ( i = strlen(raw)-1; i >= 0; i-- ) { - if ( raw[i] != ' ' && raw[i] != '\t' ) break; + for (i = strlen(raw)-1; i >= 0; i--) { + if (raw[i] != ' ' && raw[i] != '\t') + break; } *value_r = t_strndup(raw, i+1); } -bool edit_mail_headers_iterate_next -(struct edit_mail_header_iter *edhiter) +bool edit_mail_headers_iterate_next(struct edit_mail_header_iter *edhiter) { - if ( edhiter->current == NULL ) + if (edhiter->current == NULL) return FALSE; do { - edhiter->current = - ( !edhiter->reverse ? edhiter->current->next : edhiter->current->prev ); - } while ( edhiter->current != NULL && edhiter->current->header != NULL && - edhiter->header != NULL && edhiter->current->header != edhiter->header ); + edhiter->current = (!edhiter->reverse ? + edhiter->current->next : + edhiter->current->prev ); + } while (edhiter->current != NULL && edhiter->current->header != NULL && + edhiter->header != NULL && + edhiter->current->header != edhiter->header); - return ( edhiter->current != NULL && edhiter->current->header != NULL); + return (edhiter->current != NULL && edhiter->current->header != NULL); } -bool edit_mail_headers_iterate_remove -(struct edit_mail_header_iter *edhiter) +bool edit_mail_headers_iterate_remove(struct edit_mail_header_iter *edhiter) { struct _header_field_index *field_idx; bool next; - i_assert( edhiter->current != NULL && edhiter->current->header != NULL); + i_assert(edhiter->current != NULL && edhiter->current->header != NULL); edit_mail_modify(edhiter->mail); @@ -1201,21 +1235,21 @@ bool edit_mail_headers_iterate_remove return next; } -bool edit_mail_headers_iterate_replace -(struct edit_mail_header_iter *edhiter, - const char *newname, const char *newvalue) +bool edit_mail_headers_iterate_replace(struct edit_mail_header_iter *edhiter, + const char *newname, + const char *newvalue) { struct _header_field_index *field_idx; bool next; - i_assert( edhiter->current != NULL && edhiter->current->header != NULL); + i_assert(edhiter->current != NULL && edhiter->current->header != NULL); edit_mail_modify(edhiter->mail); field_idx = edhiter->current; next = edit_mail_headers_iterate_next(edhiter); - edit_mail_header_field_replace - (edhiter->mail, field_idx, newname, newvalue, TRUE); + edit_mail_header_field_replace(edhiter->mail, field_idx, + newname, newvalue, TRUE); return next; } @@ -1244,15 +1278,15 @@ static void edit_mail_free(struct mail *mail) edit_mail_unwrap(&edmail); } -static void edit_mail_set_seq -(struct mail *mail ATTR_UNUSED, uint32_t seq ATTR_UNUSED, - bool saving ATTR_UNUSED) +static void +edit_mail_set_seq(struct mail *mail ATTR_UNUSED, uint32_t seq ATTR_UNUSED, + bool saving ATTR_UNUSED) { i_panic("edit_mail_set_seq() not implemented"); } -static bool ATTR_NORETURN edit_mail_set_uid -(struct mail *mail ATTR_UNUSED, uint32_t uid ATTR_UNUSED) +static bool ATTR_NORETURN +edit_mail_set_uid(struct mail *mail ATTR_UNUSED, uint32_t uid ATTR_UNUSED) { i_panic("edit_mail_set_uid() not implemented"); } @@ -1264,8 +1298,9 @@ static void edit_mail_set_uid_cache_updates(struct mail *mail, bool set) edmail->wrapped->v.set_uid_cache_updates(&edmail->wrapped->mail, set); } -static void edit_mail_add_temp_wanted_fields -(struct mail *mail ATTR_UNUSED, enum mail_fetch_field fields ATTR_UNUSED, +static void +edit_mail_add_temp_wanted_fields( + struct mail *mail ATTR_UNUSED, enum mail_fetch_field fields ATTR_UNUSED, struct mailbox_header_lookup_ctx *headers ATTR_UNUSED) { /* Nothing */ @@ -1285,8 +1320,8 @@ static const char *const *edit_mail_get_keywords(struct mail *mail) return edmail->wrapped->v.get_keywords(&edmail->wrapped->mail); } -static const ARRAY_TYPE(keyword_indexes) *edit_mail_get_keyword_indexes -(struct mail *mail) +static const ARRAY_TYPE(keyword_indexes) * +edit_mail_get_keyword_indexes(struct mail *mail) { struct edit_mail *edmail = (struct edit_mail *)mail; @@ -1307,27 +1342,28 @@ static uint64_t edit_mail_get_pvt_modseq(struct mail *mail) return edmail->wrapped->v.get_pvt_modseq(&edmail->wrapped->mail); } -static int edit_mail_get_parts -(struct mail *mail, struct message_part **parts_r) +static int edit_mail_get_parts(struct mail *mail, struct message_part **parts_r) { struct edit_mail *edmail = (struct edit_mail *)mail; return edmail->wrapped->v.get_parts(&edmail->wrapped->mail, parts_r); } -static int edit_mail_get_date -(struct mail *mail, time_t *date_r, int *timezone_r) +static int +edit_mail_get_date(struct mail *mail, time_t *date_r, int *timezone_r) { struct edit_mail *edmail = (struct edit_mail *)mail; - return edmail->wrapped->v.get_date(&edmail->wrapped->mail, date_r, timezone_r); + return edmail->wrapped->v.get_date(&edmail->wrapped->mail, + date_r, timezone_r); } static int edit_mail_get_received_date(struct mail *mail, time_t *date_r) { struct edit_mail *edmail = (struct edit_mail *)mail; - return edmail->wrapped->v.get_received_date(&edmail->wrapped->mail, date_r); + return edmail->wrapped->v.get_received_date(&edmail->wrapped->mail, + date_r); } static int edit_mail_get_save_date(struct mail *mail, time_t *date_r) @@ -1341,17 +1377,18 @@ static int edit_mail_get_virtual_size(struct mail *mail, uoff_t *size_r) { struct edit_mail *edmail = (struct edit_mail *)mail; - if ( !edmail->headers_parsed ) { - *size_r = edmail->wrapped_hdr_size.virtual_size + - edmail->wrapped_body_size.virtual_size; + if (!edmail->headers_parsed) { + *size_r = (edmail->wrapped_hdr_size.virtual_size + + edmail->wrapped_body_size.virtual_size); - if ( !edmail->modified ) + if (!edmail->modified) return 0; } else { *size_r = edmail->wrapped_body_size.virtual_size + 2; } - *size_r += edmail->hdr_size.virtual_size + edmail->body_size.virtual_size; + *size_r += (edmail->hdr_size.virtual_size + + edmail->body_size.virtual_size); return 0; } @@ -1360,24 +1397,25 @@ static int edit_mail_get_physical_size(struct mail *mail, uoff_t *size_r) struct edit_mail *edmail = (struct edit_mail *)mail; *size_r = 0; - if ( !edmail->headers_parsed ) { - *size_r = edmail->wrapped_hdr_size.physical_size + - edmail->wrapped_body_size.physical_size; + if (!edmail->headers_parsed) { + *size_r = (edmail->wrapped_hdr_size.physical_size + + edmail->wrapped_body_size.physical_size); - if ( !edmail->modified ) + if (!edmail->modified) return 0; } else { - *size_r = edmail->wrapped_body_size.physical_size + - ( edmail->eoh_crlf ? 2 : 1 ); + *size_r = (edmail->wrapped_body_size.physical_size + + (edmail->eoh_crlf ? 2 : 1)); } - *size_r += edmail->hdr_size.physical_size + edmail->body_size.physical_size; + *size_r += (edmail->hdr_size.physical_size + + edmail->body_size.physical_size); return 0; } -static int edit_mail_get_first_header -(struct mail *mail, const char *field_name, bool decode_to_utf8, - const char **value_r) +static int +edit_mail_get_first_header(struct mail *mail, const char *field_name, + bool decode_to_utf8, const char **value_r) { struct edit_mail *edmail = (struct edit_mail *)mail; struct _header_index *header_idx; @@ -1385,20 +1423,21 @@ static int edit_mail_get_first_header int ret; /* Check whether mail headers were modified at all */ - if ( !edmail->modified || edmail->headers_head == NULL ) { + if (!edmail->modified || edmail->headers_head == NULL) { /* Unmodified */ - return edmail->wrapped->v.get_first_header - (&edmail->wrapped->mail, field_name, decode_to_utf8, value_r); + return edmail->wrapped->v.get_first_header( + &edmail->wrapped->mail, field_name, decode_to_utf8, + value_r); } /* Try to find modified header */ - if ( (header_idx=edit_mail_header_find(edmail, field_name)) == NULL || - header_idx->count == 0 ) { - - if ( !edmail->headers_parsed ) { + header_idx = edit_mail_header_find(edmail, field_name); + if (header_idx == NULL || header_idx->count == 0 ) { + if (!edmail->headers_parsed) { /* No new header */ - return edmail->wrapped->v.get_first_header - (&edmail->wrapped->mail, field_name, decode_to_utf8, value_r); + return edmail->wrapped->v.get_first_header( + &edmail->wrapped->mail, field_name, + decode_to_utf8, value_r); } *value_r = NULL; @@ -1406,7 +1445,7 @@ static int edit_mail_get_first_header } /* Get the first occurrence */ - if ( edmail->header_fields_appended == NULL ) { + if (edmail->header_fields_appended == NULL) { /* There are no appended headers, so first is found directly */ field = header_idx->first->field; } else { @@ -1414,21 +1453,23 @@ static int edit_mail_get_first_header /* Scan prepended headers */ field_idx = edmail->header_fields_head; - while ( field_idx != NULL ) { - if ( field_idx->header == header_idx ) + while (field_idx != NULL) { + if (field_idx->header == header_idx) break; - if ( field_idx == edmail->header_fields_appended ) { + if (field_idx == edmail->header_fields_appended) { field_idx = NULL; break; } field_idx = field_idx->next; } - if ( field_idx == NULL ) { + if (field_idx == NULL) { /* Check original message */ - if ( (ret=edmail->wrapped->v.get_first_header - (&edmail->wrapped->mail, field_name, decode_to_utf8, value_r)) != 0 ) + ret = edmail->wrapped->v.get_first_header( + &edmail->wrapped->mail, field_name, + decode_to_utf8, value_r); + if (ret != 0) return ret; /* Use first (apparently appended) header */ @@ -1438,16 +1479,16 @@ static int edit_mail_get_first_header } } - if ( decode_to_utf8 ) + if (decode_to_utf8) *value_r = field->utf8_value; else - *value_r = (const char *) (field->data + field->body_offset); + *value_r = (const char *)(field->data + field->body_offset); return 1; } -static int edit_mail_get_headers -(struct mail *mail, const char *field_name, bool decode_to_utf8, - const char *const **value_r) +static int +edit_mail_get_headers(struct mail *mail, const char *field_name, + bool decode_to_utf8, const char *const **value_r) { struct edit_mail *edmail = (struct edit_mail *)mail; struct _header_index *header_idx; @@ -1455,18 +1496,20 @@ static int edit_mail_get_headers const char *const *headers; ARRAY(const char *) header_values; - if ( !edmail->modified || edmail->headers_head == NULL ) { + if (!edmail->modified || edmail->headers_head == NULL) { /* Unmodified */ - return edmail->wrapped->v.get_headers - (&edmail->wrapped->mail, field_name, decode_to_utf8, value_r); + return edmail->wrapped->v.get_headers( + &edmail->wrapped->mail, field_name, decode_to_utf8, + value_r); } - if ( (header_idx=edit_mail_header_find(edmail, field_name)) == NULL || - header_idx->count == 0 ) { - if ( !edmail->headers_parsed ) { + header_idx = edit_mail_header_find(edmail, field_name); + if (header_idx == NULL || header_idx->count == 0 ) { + if (!edmail->headers_parsed) { /* No new header */ - return edmail->wrapped->v.get_headers - (&edmail->wrapped->mail, field_name, decode_to_utf8, value_r); + return edmail->wrapped->v.get_headers( + &edmail->wrapped->mail, field_name, + decode_to_utf8, value_r); } p_array_init(&header_values, edmail->mail.pool, 1); @@ -1479,40 +1522,41 @@ static int edit_mail_get_headers /* Read original headers too if message headers are not parsed */ headers = NULL; - if ( !edmail->headers_parsed && edmail->wrapped->v.get_headers - (&edmail->wrapped->mail, field_name, decode_to_utf8, &headers) < 0 ) { + if (!edmail->headers_parsed && + edmail->wrapped->v.get_headers(&edmail->wrapped->mail, field_name, + decode_to_utf8, &headers) < 0) return -1; - } /* Fill result array */ p_array_init(&header_values, edmail->mail.pool, 32); field_idx = header_idx->first; - while ( field_idx != NULL ) { - - /* If current field is the first appended one, we need to add original - * headers first. + while (field_idx != NULL) { + /* If current field is the first appended one, we need to add + original headers first. */ - if ( field_idx == edmail->header_fields_appended && headers != NULL ) { - while ( *headers != NULL ) { + if (field_idx == edmail->header_fields_appended && + headers != NULL) { + while (*headers != NULL) { array_append(&header_values, headers, 1); - headers++; } } /* Add modified header to the list */ - if ( field_idx->field->header == header_idx->header ) { + if (field_idx->field->header == header_idx->header) { struct _header_field *field = field_idx->field; const char *value; - if ( decode_to_utf8 ) + if (decode_to_utf8) value = field->utf8_value; - else - value = (const char *)(field->data + field->body_offset); + else { + value = (const char *)(field->data + + field->body_offset); + } array_append(&header_values, &value, 1); - if ( field_idx == header_idx->last ) + if (field_idx == header_idx->last) break; } @@ -1520,10 +1564,9 @@ static int edit_mail_get_headers } /* Add original headers if necessary */ - if ( headers != NULL ) { - while ( *headers != NULL ) { + if (headers != NULL) { + while (*headers != NULL) { array_append(&header_values, headers, 1); - headers++; } } @@ -1533,8 +1576,9 @@ static int edit_mail_get_headers return 1; } -static int ATTR_NORETURN edit_mail_get_header_stream -(struct mail *mail ATTR_UNUSED, +static int ATTR_NORETURN +edit_mail_get_header_stream( + struct mail *mail ATTR_UNUSED, struct mailbox_header_lookup_ctx *headers ATTR_UNUSED, struct istream **stream_r ATTR_UNUSED) { @@ -1542,26 +1586,25 @@ static int ATTR_NORETURN edit_mail_get_header_stream i_panic("edit_mail_get_header_stream() not implemented"); } -static int edit_mail_get_stream -(struct mail *mail, bool get_body ATTR_UNUSED, struct message_size *hdr_size, - struct message_size *body_size, struct istream **stream_r) +static int +edit_mail_get_stream(struct mail *mail, bool get_body ATTR_UNUSED, + struct message_size *hdr_size, + struct message_size *body_size, struct istream **stream_r) { struct edit_mail *edmail = (struct edit_mail *)mail; - if ( edmail->stream == NULL ) { + if (edmail->stream == NULL) edmail->stream = edit_mail_istream_create(edmail); - } - if ( hdr_size != NULL ) { + if (hdr_size != NULL) { *hdr_size = edmail->wrapped_hdr_size; hdr_size->physical_size += edmail->hdr_size.physical_size; hdr_size->virtual_size += edmail->hdr_size.virtual_size; hdr_size->lines += edmail->hdr_size.lines; } - if ( body_size != NULL ) { + if (body_size != NULL) *body_size = edmail->wrapped_body_size; - } *stream_r = edmail->stream; i_stream_seek(edmail->stream, 0); @@ -1569,12 +1612,13 @@ static int edit_mail_get_stream return 0; } -static int edit_mail_get_special -(struct mail *mail, enum mail_fetch_field field, const char **value_r) +static int +edit_mail_get_special(struct mail *mail, enum mail_fetch_field field, + const char **value_r) { struct edit_mail *edmail = (struct edit_mail *)mail; - if ( edmail->modified ) { + if (edmail->modified) { /* Block certain fields when modified */ switch (field) { @@ -1591,7 +1635,8 @@ static int edit_mail_get_special } } - return edmail->wrapped->v.get_special(&edmail->wrapped->mail, field, value_r); + return edmail->wrapped->v.get_special(&edmail->wrapped->mail, + field, value_r); } static int @@ -1603,22 +1648,24 @@ edit_mail_get_backend_mail(struct mail *mail, struct mail **real_mail_r) return 0; } -static void edit_mail_update_flags -(struct mail *mail, enum modify_type modify_type, enum mail_flags flags) +static void +edit_mail_update_flags(struct mail *mail, enum modify_type modify_type, + enum mail_flags flags) { struct edit_mail *edmail = (struct edit_mail *)mail; - edmail->wrapped->v.update_flags(&edmail->wrapped->mail, modify_type, flags); + edmail->wrapped->v.update_flags(&edmail->wrapped->mail, + modify_type, flags); } -static void edit_mail_update_keywords -(struct mail *mail, enum modify_type modify_type, - struct mail_keywords *keywords) +static void +edit_mail_update_keywords(struct mail *mail, enum modify_type modify_type, + struct mail_keywords *keywords) { struct edit_mail *edmail = (struct edit_mail *)mail; - edmail->wrapped->v.update_keywords - (&edmail->wrapped->mail, modify_type, keywords); + edmail->wrapped->v.update_keywords(&edmail->wrapped->mail, + modify_type, keywords); } static void edit_mail_update_modseq(struct mail *mail, uint64_t min_modseq) @@ -1628,19 +1675,23 @@ static void edit_mail_update_modseq(struct mail *mail, uint64_t min_modseq) edmail->wrapped->v.update_modseq(&edmail->wrapped->mail, min_modseq); } -static void edit_mail_update_pvt_modseq(struct mail *mail, uint64_t min_pvt_modseq) +static void +edit_mail_update_pvt_modseq(struct mail *mail, uint64_t min_pvt_modseq) { struct edit_mail *edmail = (struct edit_mail *)mail; - edmail->wrapped->v.update_pvt_modseq(&edmail->wrapped->mail, min_pvt_modseq); + edmail->wrapped->v.update_pvt_modseq(&edmail->wrapped->mail, + min_pvt_modseq); } static void edit_mail_update_pop3_uidl(struct mail *mail, const char *uidl) { struct edit_mail *edmail = (struct edit_mail *)mail; - if ( edmail->wrapped->v.update_pop3_uidl != NULL ) - edmail->wrapped->v.update_pop3_uidl(&edmail->wrapped->mail, uidl); + if (edmail->wrapped->v.update_pop3_uidl != NULL) { + edmail->wrapped->v.update_pop3_uidl( + &edmail->wrapped->mail, uidl); + } } static void edit_mail_expunge(struct mail *mail ATTR_UNUSED) @@ -1648,14 +1699,14 @@ static void edit_mail_expunge(struct mail *mail ATTR_UNUSED) /* NOOP */ } -static void edit_mail_set_cache_corrupted -(struct mail *mail, enum mail_fetch_field field, - const char *reason) +static void +edit_mail_set_cache_corrupted(struct mail *mail, enum mail_fetch_field field, + const char *reason) { struct edit_mail *edmail = (struct edit_mail *)mail; - edmail->wrapped->v.set_cache_corrupted - (&edmail->wrapped->mail, field, reason); + edmail->wrapped->v.set_cache_corrupted(&edmail->wrapped->mail, + field, reason); } static struct mail_vfuncs edit_mail_vfuncs = { @@ -1723,9 +1774,9 @@ static void edit_mail_istream_destroy(struct iostream_private *stream) pool_unref(&edstream->pool); } -static ssize_t merge_from_parent -(struct edit_mail_istream *edstream, uoff_t parent_v_offset, - uoff_t parent_end_v_offset, uoff_t copy_v_offset) +static ssize_t +merge_from_parent(struct edit_mail_istream *edstream, uoff_t parent_v_offset, + uoff_t parent_end_v_offset, uoff_t copy_v_offset) { struct istream_private *stream = &edstream->istream; uoff_t v_offset, append_v_offset; @@ -1767,29 +1818,30 @@ static ssize_t merge_from_parent if (pos > cur_pos) ret = 0; else do { - /* Use normal read here, since parent data can be returned directly - to caller. */ + /* Use normal read here, since parent data can be returned + directly to caller. */ ret = i_stream_read(stream->parent); stream->istream.stream_errno = stream->parent->stream_errno; stream->istream.eof = stream->parent->eof; edstream->eof = stream->parent->eof; data = i_stream_get_data(stream->parent, &pos); - /* check again, in case the parent stream had been seeked + /* Check again, in case the parent stream had been seeked backwards and the previous read() didn't get us far enough. */ } while (pos <= cur_pos && ret > 0); /* Don't read beyond parent end offset */ if (parent_end_v_offset != (uoff_t)-1) { - parent_bytes_left = (size_t)(parent_end_v_offset - parent_v_offset); + parent_bytes_left = (size_t)(parent_end_v_offset - + parent_v_offset); if (pos >= parent_bytes_left) { pos = parent_bytes_left; } } if (v_offset < copy_v_offset || ret == -2 || - (parent_buffer && (append_v_offset + 1) >= parent_end_v_offset)) { + (parent_buffer && (append_v_offset + 1) >= parent_end_v_offset)) { /* Merging with our local buffer; copying data from parent */ if (pos > 0) { size_t avail; @@ -1815,8 +1867,8 @@ static ssize_t merge_from_parent } } else { /* Just passing buffers from parent; no copying */ - ret = (pos > cur_pos ? (ssize_t)(pos - cur_pos) : - (ret == 0 ? 0 : -1)); + ret = (pos > cur_pos ? + (ssize_t)(pos - cur_pos) : (ret == 0 ? 0 : -1)); stream->buffer = data; stream->pos = pos; stream->skip = 0; @@ -1846,14 +1898,15 @@ static ssize_t merge_modified_headers(struct edit_mail_istream *edstream) /* Add modified headers to buffer */ written = 0; - while ( edstream->cur_header != NULL) { + while (edstream->cur_header != NULL) { size_t wsize; /* Determine what part of the header was already buffered */ append_v_offset = v_offset + (stream->pos - stream->skip); i_assert(append_v_offset >= edstream->cur_header_v_offset); if (append_v_offset >= edstream->cur_header_v_offset) - appended = (size_t)(append_v_offset - edstream->cur_header_v_offset); + appended = (size_t)(append_v_offset - + edstream->cur_header_v_offset); else appended = 0; i_assert(appended <= edstream->cur_header->field->size); @@ -1868,7 +1921,8 @@ static ssize_t merge_modified_headers(struct edit_mail_istream *edstream) /* Write (part of) the header to buffer */ memcpy(stream->w_buffer + stream->pos, - edstream->cur_header->field->data + appended, wsize); + edstream->cur_header->field->data + appended, + wsize); stream->pos += wsize; stream->buffer = stream->w_buffer; written += wsize; @@ -1884,9 +1938,10 @@ static ssize_t merge_modified_headers(struct edit_mail_istream *edstream) edstream->cur_header->field->size; edstream->cur_header = edstream->cur_header->next; - /* Stop at end of prepended headers if original header is left unparsed */ - if ( !edmail->headers_parsed - && edstream->cur_header == edmail->header_fields_appended ) + /* Stop at end of prepended headers if original header is left + unparsed */ + if (!edmail->headers_parsed && + edstream->cur_header == edmail->header_fields_appended) edstream->cur_header = NULL; } @@ -1922,53 +1977,58 @@ static ssize_t edit_mail_istream_read(struct istream_private *stream) /* Merge prepended headers */ if (!edstream->parent_buffer) { - if ( (ret=merge_modified_headers(edstream)) != 0 ) + ret = merge_modified_headers(edstream); + if (ret != 0) return ret; } v_offset = stream->istream.v_offset; append_v_offset = v_offset + (stream->pos - stream->skip); - if ( !edmail->headers_parsed && - edmail->header_fields_appended != NULL && - !edstream->header_read) { + if (!edmail->headers_parsed && edmail->header_fields_appended != NULL && + !edstream->header_read) { /* Output headers from original stream */ /* Size of the prepended header */ i_assert(edmail->hdr_size.physical_size >= - edmail->appended_hdr_size.physical_size); - prep_hdr_size = edmail->hdr_size.physical_size - - edmail->appended_hdr_size.physical_size; + edmail->appended_hdr_size.physical_size); + prep_hdr_size = (edmail->hdr_size.physical_size - + edmail->appended_hdr_size.physical_size); - /* Offset of header end or appended header - * Any final CR is dealt with later + /* Calculate offset of header end or appended header. Any final + CR is dealt with later. */ - hdr_size = prep_hdr_size + edmail->wrapped_hdr_size.physical_size; + hdr_size = (prep_hdr_size + + edmail->wrapped_hdr_size.physical_size); i_assert(hdr_size > 0); - if ( append_v_offset <= hdr_size - 1 && - edmail->wrapped_hdr_size.physical_size > 0) { - + if (append_v_offset <= (hdr_size - 1) && + edmail->wrapped_hdr_size.physical_size > 0) { parent_v_offset = stream->parent_start_offset; - parent_end_v_offset = stream->parent_start_offset + - edmail->wrapped_hdr_size.physical_size - 1; + parent_end_v_offset = + (stream->parent_start_offset + + edmail->wrapped_hdr_size.physical_size - 1); copy_v_offset = prep_hdr_size; - if ( (ret=merge_from_parent(edstream, parent_v_offset, - parent_end_v_offset, copy_v_offset)) < 0 ) + ret = merge_from_parent(edstream, parent_v_offset, + parent_end_v_offset, + copy_v_offset); + if (ret < 0) return ret; - append_v_offset = v_offset + (stream->pos - stream->skip); + append_v_offset = (v_offset + + (stream->pos - stream->skip)); i_assert(append_v_offset <= hdr_size - 1); - if ( append_v_offset == hdr_size - 1 ) { + if (append_v_offset == hdr_size - 1) { /* Strip final CR too when it is present */ - if ( stream->buffer != NULL && - stream->buffer[stream->pos-1] == '\r' ) { + if (stream->buffer != NULL && + stream->buffer[stream->pos-1] == '\r') { stream->pos--; append_v_offset--; ret--; } i_assert(ret >= 0); - edstream->cur_header = edmail->header_fields_appended; + edstream->cur_header = + edmail->header_fields_appended; edstream->cur_header_v_offset = append_v_offset; if (!edstream->parent_buffer) edstream->header_read = TRUE; @@ -1981,34 +2041,35 @@ static ssize_t edit_mail_istream_read(struct istream_private *stream) } /* Merge appended headers */ - if ( (ret=merge_modified_headers(edstream)) != 0 ) + ret = merge_modified_headers(edstream); + if (ret != 0) return ret; } /* Header does not come from original mail at all */ - if ( edmail->headers_parsed ) { - parent_v_offset = stream->parent_start_offset + - edmail->wrapped_hdr_size.physical_size - ( edmail->eoh_crlf ? 2 : 1); + if (edmail->headers_parsed) { + parent_v_offset = (stream->parent_start_offset + + edmail->wrapped_hdr_size.physical_size - + (edmail->eoh_crlf ? 2 : 1)); copy_v_offset = edmail->hdr_size.physical_size; - - /* Header comes partially from original mail and headers are added between - header and body. - */ + /* Header comes partially from original mail and headers are added + between header and body. */ } else if (edmail->header_fields_appended != NULL) { - parent_v_offset = stream->parent_start_offset + - edmail->wrapped_hdr_size.physical_size - ( edmail->eoh_crlf ? 2 : 1); - copy_v_offset = edmail->hdr_size.physical_size + - edmail->wrapped_hdr_size.physical_size - ( edmail->eoh_crlf ? 2 : 1); - - /* Header comes partially from original mail, but headers are only prepended. - */ + parent_v_offset = (stream->parent_start_offset + + edmail->wrapped_hdr_size.physical_size - + (edmail->eoh_crlf ? 2 : 1)); + copy_v_offset = (edmail->hdr_size.physical_size + + edmail->wrapped_hdr_size.physical_size - + (edmail->eoh_crlf ? 2 : 1)); + /* Header comes partially from original mail, but headers are only + prepended. */ } else { parent_v_offset = stream->parent_start_offset; copy_v_offset = edmail->hdr_size.physical_size; } - return merge_from_parent(edstream, - parent_v_offset, (uoff_t)-1, copy_v_offset); + return merge_from_parent(edstream, parent_v_offset, (uoff_t)-1, + copy_v_offset); } static void @@ -2023,8 +2084,9 @@ stream_reset_to(struct edit_mail_istream *edstream, uoff_t v_offset) i_stream_seek(edstream->istream.parent, 0); } -static void edit_mail_istream_seek -(struct istream_private *stream, uoff_t v_offset, bool mark ATTR_UNUSED) +static void +edit_mail_istream_seek(struct istream_private *stream, uoff_t v_offset, + bool mark ATTR_UNUSED) { struct edit_mail_istream *edstream = (struct edit_mail_istream *)stream; @@ -2037,35 +2099,36 @@ static void edit_mail_istream_seek edstream->cur_header_v_offset = 0; /* The beginning */ - if ( v_offset == 0 ) { + if (v_offset == 0) { stream_reset_to(edstream, 0); - if ( edmail->header_fields_head != edmail->header_fields_appended ) + if (edmail->header_fields_head != + edmail->header_fields_appended) edstream->cur_header = edmail->header_fields_head; return; } /* Inside (prepended) headers */ - if ( edmail->headers_parsed ) { + if (edmail->headers_parsed) { offset = edmail->hdr_size.physical_size; } else { - offset = edmail->hdr_size.physical_size - - edmail->appended_hdr_size.physical_size; + offset = (edmail->hdr_size.physical_size - + edmail->appended_hdr_size.physical_size); } - if ( v_offset < offset ) { + if (v_offset < offset) { stream_reset_to(edstream, v_offset); /* Find the header */ cur_header = edmail->header_fields_head; - i_assert( cur_header != NULL && - cur_header != edmail->header_fields_appended ); + i_assert(cur_header != NULL && + cur_header != edmail->header_fields_appended); edstream->cur_header_v_offset = 0; offset = cur_header->field->size; - while ( v_offset > offset ) { + while (v_offset > offset) { cur_header = cur_header->next; - i_assert( cur_header != NULL && - cur_header != edmail->header_fields_appended ); + i_assert(cur_header != NULL && + cur_header != edmail->header_fields_appended); edstream->cur_header_v_offset = offset; offset += cur_header->field->size; @@ -2075,12 +2138,12 @@ static void edit_mail_istream_seek return; } - if ( !edmail->headers_parsed ) { + if (!edmail->headers_parsed) { /* Inside original header */ - offset = edmail->hdr_size.physical_size - - edmail->appended_hdr_size.physical_size + - edmail->wrapped_hdr_size.physical_size; - if ( v_offset < offset ) { + offset = (edmail->hdr_size.physical_size - + edmail->appended_hdr_size.physical_size + + edmail->wrapped_hdr_size.physical_size); + if (v_offset < offset) { stream_reset_to(edstream, v_offset); return; } @@ -2088,21 +2151,21 @@ static void edit_mail_istream_seek edstream->header_read = TRUE; /* Inside appended header */ - offset = edmail->hdr_size.physical_size + - edmail->wrapped_hdr_size.physical_size; - if ( v_offset < offset ) { + offset = (edmail->hdr_size.physical_size + + edmail->wrapped_hdr_size.physical_size); + if (v_offset < offset) { stream_reset_to(edstream, v_offset); offset -= edmail->appended_hdr_size.physical_size; cur_header = edmail->header_fields_appended; - i_assert( cur_header != NULL ); + i_assert(cur_header != NULL); edstream->cur_header_v_offset = offset; offset += cur_header->field->size; - while ( v_offset > offset ) { + while (v_offset > offset) { cur_header = cur_header->next; - i_assert( cur_header != NULL ); + i_assert(cur_header != NULL); edstream->cur_header_v_offset = offset; offset += cur_header->field->size; @@ -2139,31 +2202,32 @@ edit_mail_istream_stat(struct istream_private *stream, bool exact) if (st->st_size == -1 || !exact) return 0; - if ( !edmail->headers_parsed ) { - if ( !edmail->modified ) + if (!edmail->headers_parsed) { + if (!edmail->modified) return 0; } else { - stream->statbuf.st_size = edmail->wrapped_body_size.physical_size + - ( edmail->eoh_crlf ? 2 : 1 ); + stream->statbuf.st_size = + (edmail->wrapped_body_size.physical_size + + (edmail->eoh_crlf ? 2 : 1)); } - stream->statbuf.st_size += edmail->hdr_size.physical_size + - edmail->body_size.physical_size; + stream->statbuf.st_size += (edmail->hdr_size.physical_size + + edmail->body_size.physical_size); return 0; } -struct istream *edit_mail_istream_create -(struct edit_mail *edmail) +struct istream *edit_mail_istream_create(struct edit_mail *edmail) { struct edit_mail_istream *edstream; struct istream *wrapped = edmail->wrapped_stream; edstream = i_new(struct edit_mail_istream, 1); edstream->pool = pool_alloconly_create(MEMPOOL_GROWING - "edit mail stream", 4096); + "edit mail stream", 4096); edstream->mail = edmail; - edstream->istream.max_buffer_size = wrapped->real_stream->max_buffer_size; + edstream->istream.max_buffer_size = + wrapped->real_stream->max_buffer_size; edstream->istream.iostream.destroy = edit_mail_istream_destroy; edstream->istream.read = edit_mail_istream_read; @@ -2175,11 +2239,10 @@ struct istream *edit_mail_istream_create edstream->istream.istream.blocking = wrapped->blocking; edstream->istream.istream.seekable = wrapped->seekable; - if ( edmail->header_fields_head != edmail->header_fields_appended ) + if (edmail->header_fields_head != edmail->header_fields_appended) edstream->cur_header = edmail->header_fields_head; i_stream_seek(wrapped, 0); return i_stream_create(&edstream->istream, wrapped, -1, 0); } -