mail-log-plugin.c revision 901d0f036826476cf75799a0fdda5777e51301e4
5a580c3a38ced62d4bcc95b8ac7c4f2935b5d294Timo Sirainen/* Copyright (c) 2007-2010 Dovecot authors, see the included COPYING file */
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen MODULE_CONTEXT(obj, mail_log_mailbox_list_module)
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen (MAIL_LOG_EVENT_DELETE | MAIL_LOG_EVENT_UNDELETE | \
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen MAIL_LOG_EVENT_EXPUNGE | MAIL_LOG_EVENT_SAVE | \
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen MAIL_LOG_EVENT_MAILBOX_DELETE | MAIL_LOG_EVENT_MAILBOX_RENAME)
16cb5d65265dd0b216542803fd80c4b999ae118eTimo Sirainenstatic const char *field_names[] = {
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainenstatic const char *event_names[] = {
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen "mailbox_delete",
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen "mailbox_rename",
16cb5d65265dd0b216542803fd80c4b999ae118eTimo Sirainen "flag_change",
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen struct mail_log_message *messages, *messages_tail;
56aa97d74071f3a2987140c2ff1cfd5a59cb35aaTimo Sirainenstatic enum mail_log_field mail_log_field_find(const char *name)
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen unsigned int i;
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen return 1 << i;
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainenstatic enum mail_log_event mail_log_event_find(const char *name)
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen unsigned int i;
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen return 1 << i;
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainenstatic enum mail_log_field mail_log_parse_fields(const char *str)
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen const char *const *tmp;
8c909e451d14075c05d90382cf8eebc4e354f569Timo Sirainen for (tmp = t_strsplit_spaces(str, ", "); *tmp != NULL; tmp++) {
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen i_fatal("Unknown field in mail_log_fields: '%s'", *tmp);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainenstatic enum mail_log_event mail_log_parse_events(const char *str)
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen const char *const *tmp;
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen for (tmp = t_strsplit_spaces(str, ", "); *tmp != NULL; tmp++) {
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen i_fatal("Unknown event in mail_log_events: '%s'", *tmp);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainenstatic void mail_log_read_settings(struct mail_log_settings *set)
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen const char *str;
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen set->fields = str == NULL ? MAIL_LOG_DEFAULT_FIELDS :
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen set->events = str == NULL ? MAIL_LOG_DEFAULT_EVENTS :
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainenstatic void mail_log_append_mailbox_name(string_t *str, struct mail *mail)
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen str_sanitize(mailbox_str, MAILBOX_NAME_LOG_LEN));
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainenmail_log_append_mail_header(string_t *str, struct mail *mail,
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen if (mail_get_first_header(mail, header, &value) <= 0)
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen str_printfa(str, "%s=%s", name, str_sanitize(value, HEADER_LOG_LEN));
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainenmail_log_append_uid(struct mail_log_mail_txn_context *ctx,
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen struct mail_log_message *msg, string_t *str, uint32_t uid)
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen /* we don't know the uid yet, assign it later */
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen msg->pretext = p_strdup(ctx->pool, str_c(str));
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainenmail_log_append_mail_message_real(struct mail_log_mail_txn_context *ctx,
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen msg = p_new(ctx->pool, struct mail_log_message, 1);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen if ((mail_log_set.fields & MAIL_LOG_FIELD_BOX) != 0) {
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen if ((mail_log_set.fields & MAIL_LOG_FIELD_UID) != 0) {
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen mail_log_append_uid(ctx, msg, text, mail->uid);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen if ((mail_log_set.fields & MAIL_LOG_FIELD_MSGID) != 0) {
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen mail_log_append_mail_header(text, mail, "msgid", "Message-ID");
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen if ((mail_log_set.fields & MAIL_LOG_FIELD_PSIZE) != 0) {
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen if ((mail_log_set.fields & MAIL_LOG_FIELD_VSIZE) != 0) {
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen if ((mail_log_set.fields & MAIL_LOG_FIELD_FROM) != 0) {
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen mail_log_append_mail_header(text, mail, "from", "From");
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen if ((mail_log_set.fields & MAIL_LOG_FIELD_SUBJECT) != 0) {
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen mail_log_append_mail_header(text, mail, "subject", "Subject");
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen if ((mail_log_set.fields & MAIL_LOG_FIELD_FLAGS) != 0) {
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainenmail_log_append_mail_message(struct mail_log_mail_txn_context *ctx,
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen mail_log_append_mail_message_real(ctx, mail, event, desc);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainenmail_log_mail_transaction_begin(struct mailbox_transaction_context *t ATTR_UNUSED)
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen pool = pool_alloconly_create("mail-log", 2048);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen ctx = p_new(pool, struct mail_log_mail_txn_context, 1);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainenstatic void mail_log_mail_save(void *txn, struct mail *mail)
const char *desc;
bool ret;
void mail_log_plugin_deinit(void)