mail-log-plugin.c revision 23fdad6c7e2581921f511e24cd9371c9eaebcef9
c865b0e9c65fd77f7b2ab6f8616d3def5501ecb3Timo Sirainen/* Copyright (c) 2007-2009 Dovecot authors, see the included COPYING file */
19ed8f08b23d6ed204e6b27e5d1c0c6fe6bb11ddPhil Carmody MODULE_CONTEXT(obj, mail_log_mailbox_list_module)
62fc0b4f07eb6f18a3bff4b1fccb636e6fae3cf4Timo Sirainen (MAIL_LOG_EVENT_DELETE | MAIL_LOG_EVENT_UNDELETE | \
62fc0b4f07eb6f18a3bff4b1fccb636e6fae3cf4Timo Sirainen MAIL_LOG_EVENT_EXPUNGE | MAIL_LOG_EVENT_SAVE | \
62fc0b4f07eb6f18a3bff4b1fccb636e6fae3cf4Timo Sirainen MAIL_LOG_EVENT_MAILBOX_DELETE | MAIL_LOG_EVENT_MAILBOX_RENAME)
62fc0b4f07eb6f18a3bff4b1fccb636e6fae3cf4Timo Sirainenstatic const char *field_names[] = {
f5c0d5cada4da23a167c38426d0c481a3e1d5583Timo Sirainenstatic const char *event_names[] = {
62fc0b4f07eb6f18a3bff4b1fccb636e6fae3cf4Timo Sirainen "mailbox_delete",
62fc0b4f07eb6f18a3bff4b1fccb636e6fae3cf4Timo Sirainen "mailbox_rename",
568fec5b1e629f25d288b48007485b9aa4a018b1Timo Sirainen "flag_change",
568fec5b1e629f25d288b48007485b9aa4a018b1Timo Sirainen struct mail_log_message *messages, *messages_tail;
568fec5b1e629f25d288b48007485b9aa4a018b1Timo Sirainenstatic enum mail_log_field mail_log_field_find(const char *name)
568fec5b1e629f25d288b48007485b9aa4a018b1Timo Sirainen unsigned int i;
568fec5b1e629f25d288b48007485b9aa4a018b1Timo Sirainen return 1 << i;
62fc0b4f07eb6f18a3bff4b1fccb636e6fae3cf4Timo Sirainenstatic enum mail_log_event mail_log_event_find(const char *name)
568fec5b1e629f25d288b48007485b9aa4a018b1Timo Sirainen unsigned int i;
568fec5b1e629f25d288b48007485b9aa4a018b1Timo Sirainen return 1 << i;
568fec5b1e629f25d288b48007485b9aa4a018b1Timo Sirainenstatic enum mail_log_field mail_log_parse_fields(const char *str)
568fec5b1e629f25d288b48007485b9aa4a018b1Timo Sirainen const char *const *tmp;
62fc0b4f07eb6f18a3bff4b1fccb636e6fae3cf4Timo Sirainen for (tmp = t_strsplit_spaces(str, ", "); *tmp != NULL; tmp++) {
62fc0b4f07eb6f18a3bff4b1fccb636e6fae3cf4Timo Sirainen i_fatal("Unknown field in mail_log_fields: '%s'", *tmp);
62fc0b4f07eb6f18a3bff4b1fccb636e6fae3cf4Timo Sirainenstatic enum mail_log_event mail_log_parse_events(const char *str)
62fc0b4f07eb6f18a3bff4b1fccb636e6fae3cf4Timo Sirainen const char *const *tmp;
568fec5b1e629f25d288b48007485b9aa4a018b1Timo Sirainen for (tmp = t_strsplit_spaces(str, ", "); *tmp != NULL; tmp++) {
c865b0e9c65fd77f7b2ab6f8616d3def5501ecb3Timo Sirainen i_fatal("Unknown event in mail_log_events: '%s'", *tmp);
19ed8f08b23d6ed204e6b27e5d1c0c6fe6bb11ddPhil Carmodystatic void mail_log_read_settings(struct mail_log_settings *set)
568fec5b1e629f25d288b48007485b9aa4a018b1Timo Sirainen const char *str;
b04e76cbc807707d299055be79500f8ff131da43Timo Sirainen set->fields = str == NULL ? MAIL_LOG_DEFAULT_FIELDS :
19ed8f08b23d6ed204e6b27e5d1c0c6fe6bb11ddPhil Carmody set->events = str == NULL ? MAIL_LOG_DEFAULT_EVENTS :
b6b06530d654f0436bfbaefc1e988d53fff0cbeeTimo Sirainenstatic void mail_log_append_mailbox_name(string_t *str, struct mail *mail)
83172e28d4ac684dfed83f7c9db933493d7c5922Timo Sirainen str_sanitize(mailbox_str, MAILBOX_NAME_LOG_LEN));
83172e28d4ac684dfed83f7c9db933493d7c5922Timo Sirainenmail_log_append_mail_header(string_t *str, struct mail *mail,
62fc0b4f07eb6f18a3bff4b1fccb636e6fae3cf4Timo Sirainen if (mail_get_first_header(mail, header, &value) <= 0)
3a54211bd6c4dc3f8687c16020770551cf83a548Teemu Huovila str_printfa(str, "%s=%s", name, str_sanitize(value, HEADER_LOG_LEN));
c865b0e9c65fd77f7b2ab6f8616d3def5501ecb3Timo Sirainenmail_log_append_uid(struct mail_log_mail_txn_context *ctx,
568fec5b1e629f25d288b48007485b9aa4a018b1Timo Sirainen struct mail_log_message *msg, string_t *str, uint32_t uid)
62fc0b4f07eb6f18a3bff4b1fccb636e6fae3cf4Timo Sirainen /* we don't know the uid yet, assign it later */
c865b0e9c65fd77f7b2ab6f8616d3def5501ecb3Timo Sirainen msg->pretext = p_strdup(ctx->pool, str_c(str));
62fc0b4f07eb6f18a3bff4b1fccb636e6fae3cf4Timo Sirainenmail_log_append_mail_message_real(struct mail_log_mail_txn_context *ctx,
fdf70410de49eadfbb77997bb60ebba19aee4752Teemu Huovila msg = p_new(ctx->pool, struct mail_log_message, 1);
fdf70410de49eadfbb77997bb60ebba19aee4752Teemu Huovila if ((mail_log_set.fields & MAIL_LOG_FIELD_BOX) != 0) {
0c5854b6891c59c1c3f443569bc823d7db571582Teemu Huovila if ((mail_log_set.fields & MAIL_LOG_FIELD_UID) != 0) {
b6b06530d654f0436bfbaefc1e988d53fff0cbeeTimo Sirainen mail_log_append_uid(ctx, msg, text, mail->uid);
b6b06530d654f0436bfbaefc1e988d53fff0cbeeTimo Sirainen if ((mail_log_set.fields & MAIL_LOG_FIELD_MSGID) != 0) {
b6b06530d654f0436bfbaefc1e988d53fff0cbeeTimo Sirainen mail_log_append_mail_header(text, mail, "msgid", "Message-ID");
b6b06530d654f0436bfbaefc1e988d53fff0cbeeTimo Sirainen if ((mail_log_set.fields & MAIL_LOG_FIELD_PSIZE) != 0) {
c865b0e9c65fd77f7b2ab6f8616d3def5501ecb3Timo Sirainen if ((mail_log_set.fields & MAIL_LOG_FIELD_VSIZE) != 0) {
62fc0b4f07eb6f18a3bff4b1fccb636e6fae3cf4Timo Sirainen if ((mail_log_set.fields & MAIL_LOG_FIELD_FROM) != 0) {
c865b0e9c65fd77f7b2ab6f8616d3def5501ecb3Timo Sirainen mail_log_append_mail_header(text, mail, "from", "From");
c865b0e9c65fd77f7b2ab6f8616d3def5501ecb3Timo Sirainen if ((mail_log_set.fields & MAIL_LOG_FIELD_SUBJECT) != 0) {
62fc0b4f07eb6f18a3bff4b1fccb636e6fae3cf4Timo Sirainen mail_log_append_mail_header(text, mail, "subject", "Subject");
c865b0e9c65fd77f7b2ab6f8616d3def5501ecb3Timo Sirainen if ((mail_log_set.fields & MAIL_LOG_FIELD_FLAGS) != 0) {
3a54211bd6c4dc3f8687c16020770551cf83a548Teemu Huovilamail_log_append_mail_message(struct mail_log_mail_txn_context *ctx,
19ed8f08b23d6ed204e6b27e5d1c0c6fe6bb11ddPhil Carmody mail_log_append_mail_message_real(ctx, mail, event, desc);
3a54211bd6c4dc3f8687c16020770551cf83a548Teemu Huovilamail_log_mail_transaction_begin(struct mailbox_transaction_context *t ATTR_UNUSED)
3a54211bd6c4dc3f8687c16020770551cf83a548Teemu Huovila pool = pool_alloconly_create("mail-log", 1024);
3a54211bd6c4dc3f8687c16020770551cf83a548Teemu Huovila ctx = p_new(pool, struct mail_log_mail_txn_context, 1);
3a54211bd6c4dc3f8687c16020770551cf83a548Teemu Huovilastatic void mail_log_mail_save(void *txn, struct mail *mail)
3a54211bd6c4dc3f8687c16020770551cf83a548Teemu Huovila mail_log_append_mail_message(ctx, mail, MAIL_LOG_EVENT_SAVE, "save");
3a54211bd6c4dc3f8687c16020770551cf83a548Teemu Huovilastatic void mail_log_mail_copy(void *txn, struct mail *src, struct mail *dst)
3a54211bd6c4dc3f8687c16020770551cf83a548Teemu Huovila mail_log_append_mail_message(ctx, dst, MAIL_LOG_EVENT_SAVE, desc);
c865b0e9c65fd77f7b2ab6f8616d3def5501ecb3Timo Sirainenstatic void mail_log_mail_expunge(void *txn, struct mail *mail)
c865b0e9c65fd77f7b2ab6f8616d3def5501ecb3Timo Sirainen mail_log_append_mail_message(ctx, mail, MAIL_LOG_EVENT_EXPUNGE,
568fec5b1e629f25d288b48007485b9aa4a018b1Timo Sirainenstatic void mail_log_mail_update_flags(void *txn, struct mail *mail,
c865b0e9c65fd77f7b2ab6f8616d3def5501ecb3Timo Sirainen enum mail_flags new_flags = mail_get_flags(mail);
fdf70410de49eadfbb77997bb60ebba19aee4752Teemu Huovila if (((old_flags ^ new_flags) & MAIL_DELETED) == 0) {
c865b0e9c65fd77f7b2ab6f8616d3def5501ecb3Timo Sirainen "flag_change");
568fec5b1e629f25d288b48007485b9aa4a018b1Timo Sirainen mail_log_append_mail_message(ctx, mail, MAIL_LOG_EVENT_DELETE,
c865b0e9c65fd77f7b2ab6f8616d3def5501ecb3Timo Sirainen mail_log_append_mail_message(ctx, mail, MAIL_LOG_EVENT_UNDELETE,
c865b0e9c65fd77f7b2ab6f8616d3def5501ecb3Timo Sirainenmail_log_mail_update_keywords(void *txn, struct mail *mail,
c865b0e9c65fd77f7b2ab6f8616d3def5501ecb3Timo Sirainen mail_log_append_mail_message(ctx, mail, MAIL_LOG_EVENT_FLAG_CHANGE,
fdf70410de49eadfbb77997bb60ebba19aee4752Teemu Huovila "flag_change");
fdf70410de49eadfbb77997bb60ebba19aee4752Teemu Huovila struct mail_transaction_commit_changes *changes)
e4bf76afb82ea28ec9d06823fa7deed5f8277183Timo Sirainen unsigned int n = 0;
568fec5b1e629f25d288b48007485b9aa4a018b1Timo Sirainen seq_range_array_iter_init(&iter, &changes->saved_uids);
568fec5b1e629f25d288b48007485b9aa4a018b1Timo Sirainen for (msg = ctx->messages; msg != NULL; msg = msg->next) {
568fec5b1e629f25d288b48007485b9aa4a018b1Timo Sirainen ret = seq_range_array_iter_nth(&iter, n++, &uid);
e4bf76afb82ea28ec9d06823fa7deed5f8277183Timo Sirainen i_info("%s%u%s", msg->pretext, uid, msg->text);
e4bf76afb82ea28ec9d06823fa7deed5f8277183Timo Sirainen i_assert(!seq_range_array_iter_nth(&iter, n, &uid));
8b1a9a4d63b0abccdf7cb1acb8359d5396dd657bTimo Sirainenstatic void mail_log_mail_transaction_rollback(void *txn)
c4b772bfbdafe68ac1a0076eab26cd681f8e5046Timo Sirainenmail_log_mailbox_delete_commit(void *txn ATTR_UNUSED,
8b1a9a4d63b0abccdf7cb1acb8359d5396dd657bTimo Sirainen if ((mail_log_set.events & MAIL_LOG_EVENT_MAILBOX_DELETE) == 0)
8b1a9a4d63b0abccdf7cb1acb8359d5396dd657bTimo Sirainen i_info("Mailbox deleted: %s", str_sanitize(name, MAILBOX_NAME_LOG_LEN));
2730605833442b5ddcb261f90b8375fc98201e35Timo Sirainenmail_log_mailbox_rename(struct mailbox_list *oldlist ATTR_UNUSED,
e4bf76afb82ea28ec9d06823fa7deed5f8277183Timo Sirainen const char *newname, bool rename_children ATTR_UNUSED)
e4bf76afb82ea28ec9d06823fa7deed5f8277183Timo Sirainen if ((mail_log_set.events & MAIL_LOG_EVENT_MAILBOX_RENAME) == 0)
c865b0e9c65fd77f7b2ab6f8616d3def5501ecb3Timo Sirainenstatic const struct notify_vfuncs mail_log_vfuncs = {
3a54211bd6c4dc3f8687c16020770551cf83a548Teemu Huovila /* mail_transaction_begin */ mail_log_mail_transaction_begin,
e4bf76afb82ea28ec9d06823fa7deed5f8277183Timo Sirainen /* mail_update_flags */ mail_log_mail_update_flags,
c865b0e9c65fd77f7b2ab6f8616d3def5501ecb3Timo Sirainen /* mail_update_keywords */ mail_log_mail_update_keywords,
c865b0e9c65fd77f7b2ab6f8616d3def5501ecb3Timo Sirainen /* mail_transaction_commit */ mail_log_mail_transaction_commit,
b1965419f329eb7cf78ee39e7c5942462eabb256Timo Sirainen /* mail_transaction_rollback */ mail_log_mail_transaction_rollback,
c865b0e9c65fd77f7b2ab6f8616d3def5501ecb3Timo Sirainen /* mailbox_delete_begin */ notify_noop_mailbox_delete_begin,
b1965419f329eb7cf78ee39e7c5942462eabb256Timo Sirainen /* mailbox_delete_commit */ mail_log_mailbox_delete_commit,
b1965419f329eb7cf78ee39e7c5942462eabb256Timo Sirainen /* mailbox_delete_rollback */ notify_noop_mailbox_delete_rollback,
void mail_log_plugin_init(void)
void mail_log_plugin_deinit(void)