index-mail.h revision 3af0a1a2c2ef22a14c6b3c5cc4f780a2ea4df438
e8a59a1671127f87e2d22f42e84c572f28299d81Timo Sirainen#ifndef __INDEX_MAIL_H
e8a59a1671127f87e2d22f42e84c572f28299d81Timo Sirainen#define __INDEX_MAIL_H
e8a59a1671127f87e2d22f42e84c572f28299d81Timo Sirainen
dbb1fb1c51727e2050792f8c333b212e22a36d69Timo Sirainen#include "message-size.h"
6789ed17e7ca4021713507baf0dcf6979bb42e0cTimo Sirainen#include "mail-cache.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "mail-storage-private.h"
dbb1fb1c51727e2050792f8c333b212e22a36d69Timo Sirainen
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainenenum index_cache_field {
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen /* fixed size fields */
e667602217af55105d44d8d9b75f09a8a9ac2f14Timo Sirainen MAIL_CACHE_FLAGS = 0,
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen MAIL_CACHE_SENT_DATE,
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen MAIL_CACHE_RECEIVED_DATE,
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen MAIL_CACHE_VIRTUAL_FULL_SIZE,
d67fde1a8ebc1d85704c5986d8f93aae97eccef3Timo Sirainen MAIL_CACHE_PHYSICAL_FULL_SIZE,
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen /* variable sized field */
55773f17bccf6361d6599ffcbe072d7c9fe205bfTimo Sirainen MAIL_CACHE_IMAP_BODY,
55773f17bccf6361d6599ffcbe072d7c9fe205bfTimo Sirainen MAIL_CACHE_IMAP_BODYSTRUCTURE,
55773f17bccf6361d6599ffcbe072d7c9fe205bfTimo Sirainen MAIL_CACHE_IMAP_ENVELOPE,
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen MAIL_CACHE_MESSAGEPART,
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen MAIL_CACHE_FIELD_COUNT
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen};
bcd8a160ce32e5afe3566793b5a144028063b2fbTimo Sirainenextern struct mail_cache_field global_cache_fields[MAIL_CACHE_FIELD_COUNT];
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen
1b4441e3e6f9e78ebeae8218de971959cd55bf60Timo Sirainen#define IMAP_BODY_PLAIN_7BIT_ASCII \
1b4441e3e6f9e78ebeae8218de971959cd55bf60Timo Sirainen "\"text\" \"plain\" (\"charset\" \"us-ascii\") NIL NIL \"7bit\""
1b4441e3e6f9e78ebeae8218de971959cd55bf60Timo Sirainen
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainenenum mail_cache_record_flag {
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen /* If binary flags are set, it's not checked whether mail is
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen missing CRs. So this flag may be set as an optimization for
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen regular non-binary mails as well if it's known that it contains
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen valid CR+LF line breaks. */
e667602217af55105d44d8d9b75f09a8a9ac2f14Timo Sirainen MAIL_CACHE_FLAG_BINARY_HEADER = 0x0001,
e667602217af55105d44d8d9b75f09a8a9ac2f14Timo Sirainen MAIL_CACHE_FLAG_BINARY_BODY = 0x0002,
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen /* Mail header or body is known to contain NUL characters. */
e667602217af55105d44d8d9b75f09a8a9ac2f14Timo Sirainen MAIL_CACHE_FLAG_HAS_NULS = 0x0004,
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen /* Mail header or body is known to not contain NUL characters. */
1b4441e3e6f9e78ebeae8218de971959cd55bf60Timo Sirainen MAIL_CACHE_FLAG_HAS_NO_NULS = 0x0008,
1b4441e3e6f9e78ebeae8218de971959cd55bf60Timo Sirainen
1b4441e3e6f9e78ebeae8218de971959cd55bf60Timo Sirainen /* BODY is IMAP_BODY_PLAIN_7BIT_ASCII and rest of BODYSTRUCTURE
1b4441e3e6f9e78ebeae8218de971959cd55bf60Timo Sirainen fields are NIL */
1b4441e3e6f9e78ebeae8218de971959cd55bf60Timo Sirainen MAIL_CACHE_FLAG_TEXT_PLAIN_7BIT_ASCII = 0x0010
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen};
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen
55773f17bccf6361d6599ffcbe072d7c9fe205bfTimo Sirainenenum index_mail_access_part {
55773f17bccf6361d6599ffcbe072d7c9fe205bfTimo Sirainen READ_HDR = 0x01,
55773f17bccf6361d6599ffcbe072d7c9fe205bfTimo Sirainen READ_BODY = 0x02,
55773f17bccf6361d6599ffcbe072d7c9fe205bfTimo Sirainen PARSE_HDR = 0x04,
55773f17bccf6361d6599ffcbe072d7c9fe205bfTimo Sirainen PARSE_BODY = 0x08
55773f17bccf6361d6599ffcbe072d7c9fe205bfTimo Sirainen};
55773f17bccf6361d6599ffcbe072d7c9fe205bfTimo Sirainen
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainenstruct mail_sent_date {
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen time_t time;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen int32_t timezone;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen};
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainenstruct index_mail_line {
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen unsigned int field_idx;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen uint32_t start_pos, end_pos;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen uint32_t line_num;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen unsigned int cache:1;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen};
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen
dbb1fb1c51727e2050792f8c333b212e22a36d69Timo Sirainenstruct message_header_line;
dbb1fb1c51727e2050792f8c333b212e22a36d69Timo Sirainen
e8a59a1671127f87e2d22f42e84c572f28299d81Timo Sirainenstruct index_mail_data {
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen enum mail_flags flags;
6789ed17e7ca4021713507baf0dcf6979bb42e0cTimo Sirainen time_t date, received_date;
d67fde1a8ebc1d85704c5986d8f93aae97eccef3Timo Sirainen uoff_t virtual_size, physical_size;
e8a59a1671127f87e2d22f42e84c572f28299d81Timo Sirainen
6789ed17e7ca4021713507baf0dcf6979bb42e0cTimo Sirainen struct mail_sent_date sent_date;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen struct index_mail_line parse_line;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen uint32_t parse_line_num;
6789ed17e7ca4021713507baf0dcf6979bb42e0cTimo Sirainen
e8a59a1671127f87e2d22f42e84c572f28299d81Timo Sirainen struct message_part *parts;
33d63688ed8b26dc333e3c2edbfb2fe6e412604dTimo Sirainen const char *envelope, *body, *bodystructure, *uid_string;
645f258ea29afaf09b673fc65d1bd788dfec8db8Timo Sirainen struct message_part_envelope_data *envelope_data;
e8a59a1671127f87e2d22f42e84c572f28299d81Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen uint32_t seq;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen const struct mail_index_record *rec;
e667602217af55105d44d8d9b75f09a8a9ac2f14Timo Sirainen uint32_t cache_flags;
55773f17bccf6361d6599ffcbe072d7c9fe205bfTimo Sirainen enum index_mail_access_part access_part;
6389aeec8c26b585e583c364b48ad12adf741898Timo Sirainen
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen struct istream *stream, *filter_stream;
96541d31299bb40b5a6efdbf9b4cb3d4f4b4a069Timo Sirainen struct message_size hdr_size, body_size;
96541d31299bb40b5a6efdbf9b4cb3d4f4b4a069Timo Sirainen struct message_parser_ctx *parser_ctx;
644268f7848a7c4221146d0b11feb8ed5bbed233Timo Sirainen int parsing_count;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen array_t ARRAY_DEFINE(keywords, const char *);
e8a59a1671127f87e2d22f42e84c572f28299d81Timo Sirainen
3af0a1a2c2ef22a14c6b3c5cc4f780a2ea4df438Timo Sirainen const struct mail_cache_field *all_cache_fields;
3af0a1a2c2ef22a14c6b3c5cc4f780a2ea4df438Timo Sirainen unsigned int all_cache_fields_count;
3af0a1a2c2ef22a14c6b3c5cc4f780a2ea4df438Timo Sirainen
1bdda5c0c30463160c47151537e6bb2c6c994841Timo Sirainen unsigned int save_sent_date:1;
55773f17bccf6361d6599ffcbe072d7c9fe205bfTimo Sirainen unsigned int save_envelope:1;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen unsigned int save_bodystructure_header:1;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen unsigned int save_bodystructure_body:1;
a58e4aec412a30352d9d45e63726cac044aa6aa5Timo Sirainen unsigned int parsed_bodystructure:1;
e8a59a1671127f87e2d22f42e84c572f28299d81Timo Sirainen unsigned int hdr_size_set:1;
e8a59a1671127f87e2d22f42e84c572f28299d81Timo Sirainen unsigned int body_size_set:1;
1b4441e3e6f9e78ebeae8218de971959cd55bf60Timo Sirainen unsigned int messageparts_saved_to_cache:1;
e8a59a1671127f87e2d22f42e84c572f28299d81Timo Sirainen};
e8a59a1671127f87e2d22f42e84c572f28299d81Timo Sirainen
e8a59a1671127f87e2d22f42e84c572f28299d81Timo Sirainenstruct index_mail {
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen struct mail_private mail;
e8a59a1671127f87e2d22f42e84c572f28299d81Timo Sirainen struct index_mail_data data;
e8a59a1671127f87e2d22f42e84c572f28299d81Timo Sirainen
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen pool_t data_pool;
e8a59a1671127f87e2d22f42e84c572f28299d81Timo Sirainen struct index_mailbox *ibox;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen struct index_transaction_context *trans;
33d63688ed8b26dc333e3c2edbfb2fe6e412604dTimo Sirainen uint32_t uid_validity;
e8a59a1671127f87e2d22f42e84c572f28299d81Timo Sirainen
e8a59a1671127f87e2d22f42e84c572f28299d81Timo Sirainen enum mail_fetch_field wanted_fields;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen struct index_header_lookup_ctx *wanted_headers;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen /* per-mail variables, here for performance reasons: */
98dd8e6e81f11f1e6040ca72f4916242d246c863Timo Sirainen uint32_t header_seq;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen string_t *header_data;
cf52b37d807553e91a2d6fb7cb2c8b4c34589e1dTimo Sirainen array_t ARRAY_DEFINE(header_lines, struct index_mail_line);
cf52b37d807553e91a2d6fb7cb2c8b4c34589e1dTimo Sirainen array_t ARRAY_DEFINE(header_match, uint8_t);
87460b08cb97b31cde640d4975a6aa2c1d0e7226Timo Sirainen array_t ARRAY_DEFINE(header_match_lines, unsigned int);
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen uint8_t header_match_value;
e8a59a1671127f87e2d22f42e84c572f28299d81Timo Sirainen};
e8a59a1671127f87e2d22f42e84c572f28299d81Timo Sirainen
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainenstruct mail *
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainenindex_mail_alloc(struct mailbox_transaction_context *t,
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen enum mail_fetch_field wanted_fields,
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen struct mailbox_header_lookup_ctx *wanted_headers);
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainenint index_mail_set_seq(struct mail *mail, uint32_t seq);
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainenvoid index_mail_free(struct mail *mail);
e8a59a1671127f87e2d22f42e84c572f28299d81Timo Sirainen
6789ed17e7ca4021713507baf0dcf6979bb42e0cTimo Sirainenvoid index_mail_parse_header_init(struct index_mail *mail,
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen struct mailbox_header_lookup_ctx *headers);
d2b94d25f842cd1b7acaf4dd7de858f7c6a821c9Timo Sirainenvoid index_mail_parse_header(struct message_part *part,
d2b94d25f842cd1b7acaf4dd7de858f7c6a821c9Timo Sirainen struct message_header_line *hdr,
d2b94d25f842cd1b7acaf4dd7de858f7c6a821c9Timo Sirainen struct index_mail *mail);
2aecf7be5834e7f6520f8deaad683a6fa1de4d61Timo Sirainenint index_mail_parse_headers(struct index_mail *mail,
2aecf7be5834e7f6520f8deaad683a6fa1de4d61Timo Sirainen struct mailbox_header_lookup_ctx *headers);
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainenvoid index_mail_headers_get_envelope(struct index_mail *mail);
1bdda5c0c30463160c47151537e6bb2c6c994841Timo Sirainen
87460b08cb97b31cde640d4975a6aa2c1d0e7226Timo Sirainenconst char *index_mail_get_first_header(struct mail *_mail, const char *field);
87460b08cb97b31cde640d4975a6aa2c1d0e7226Timo Sirainenconst char *const *
87460b08cb97b31cde640d4975a6aa2c1d0e7226Timo Sirainenindex_mail_get_headers(struct mail *_mail, const char *field);
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainenstruct istream *
87460b08cb97b31cde640d4975a6aa2c1d0e7226Timo Sirainenindex_mail_get_header_stream(struct mail *_mail,
87460b08cb97b31cde640d4975a6aa2c1d0e7226Timo Sirainen struct mailbox_header_lookup_ctx *headers);
1bdda5c0c30463160c47151537e6bb2c6c994841Timo Sirainen
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainenenum mail_flags index_mail_get_flags(struct mail *_mail);
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainenconst char *const *index_mail_get_keywords(struct mail *_mail);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenconst struct message_part *index_mail_get_parts(struct mail *_mail);
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainentime_t index_mail_get_received_date(struct mail *_mail);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainentime_t index_mail_get_date(struct mail *_mail, int *timezone);
d67fde1a8ebc1d85704c5986d8f93aae97eccef3Timo Sirainenuoff_t index_mail_get_virtual_size(struct mail *mail);
d67fde1a8ebc1d85704c5986d8f93aae97eccef3Timo Sirainenuoff_t index_mail_get_physical_size(struct mail *mail);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstruct istream *index_mail_init_stream(struct index_mail *mail,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen struct message_size *hdr_size,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen struct message_size *body_size);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenconst char *index_mail_get_special(struct mail *_mail,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen enum mail_fetch_field field);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainenint index_mail_update_flags(struct mail *mail, enum modify_type modify_type,
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen enum mail_flags flags);
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainenint index_mail_update_keywords(struct mail *mail, enum modify_type modify_type,
41e1c7380edda701719d8ce1fb4d465d2ec4c84dTimo Sirainen struct mail_keywords *keywords);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenint index_mail_expunge(struct mail *mail);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
1171f0abf442638bac1827bb24a0b6b8eb682a82Timo Sirainenconst char *index_mail_get_cached_string(struct index_mail *mail,
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen enum index_cache_field field);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenuoff_t index_mail_get_cached_uoff_t(struct index_mail *mail,
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen enum index_cache_field field);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenuoff_t index_mail_get_cached_virtual_size(struct index_mail *mail);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainentime_t index_mail_get_cached_received_date(struct index_mail *mail);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
e8a59a1671127f87e2d22f42e84c572f28299d81Timo Sirainen#endif