dovecot-2.0: Reimplemented mail-log plugin using notify plugin f...
dovecot at dovecot.org
dovecot at dovecot.org
Mon Aug 24 23:59:02 EEST 2009
details: http://hg.dovecot.org/dovecot-2.0/rev/d1cdc927ea3c
changeset: 9813:d1cdc927ea3c
user: Timo Sirainen <tss at iki.fi>
date: Mon Aug 24 16:58:53 2009 -0400
description:
Reimplemented mail-log plugin using notify plugin framework.
Patch by Mark Washenberger / Rackspace.
diffstat:
2 files changed, 313 insertions(+), 605 deletions(-)
src/plugins/mail-log/Makefile.am | 3
src/plugins/mail-log/mail-log-plugin.c | 915 ++++++++++----------------------
diffs (truncated from 1002 to 300 lines):
diff -r f751c23561f9 -r d1cdc927ea3c src/plugins/mail-log/Makefile.am
--- a/src/plugins/mail-log/Makefile.am Mon Aug 24 16:57:50 2009 -0400
+++ b/src/plugins/mail-log/Makefile.am Mon Aug 24 16:58:53 2009 -0400
@@ -5,7 +5,8 @@ AM_CPPFLAGS = \
-I$(top_srcdir)/src/lib-index \
-I$(top_srcdir)/src/lib-storage \
-I$(top_srcdir)/src/lib-storage/index \
- -I$(top_srcdir)/src/lib-storage/index/maildir
+ -I$(top_srcdir)/src/lib-storage/index/maildir \
+ -I$(top_srcdir)/src/plugins/notify
lib20_mail_log_plugin_la_LDFLAGS = -module -avoid-version
diff -r f751c23561f9 -r d1cdc927ea3c src/plugins/mail-log/mail-log-plugin.c
--- a/src/plugins/mail-log/mail-log-plugin.c Mon Aug 24 16:57:50 2009 -0400
+++ b/src/plugins/mail-log/mail-log-plugin.c Mon Aug 24 16:58:53 2009 -0400
@@ -7,7 +7,7 @@
#include "imap-util.h"
#include "mail-storage-private.h"
#include "mailbox-list-private.h"
-#include "mail-user.h"
+#include "notify-plugin.h"
#include "mail-log-plugin.h"
#include <stdlib.h>
@@ -21,8 +21,6 @@
MODULE_CONTEXT(obj, mail_log_mail_module)
#define MAIL_LOG_LIST_CONTEXT(obj) \
MODULE_CONTEXT(obj, mail_log_mailbox_list_module)
-#define MAIL_LOG_USER_CONTEXT(obj) \
- MODULE_CONTEXT(obj, mail_log_mail_user_module)
enum mail_log_field {
MAIL_LOG_FIELD_UID = 0x01,
@@ -42,15 +40,14 @@ enum mail_log_event {
MAIL_LOG_EVENT_DELETE = 0x01,
MAIL_LOG_EVENT_UNDELETE = 0x02,
MAIL_LOG_EVENT_EXPUNGE = 0x04,
- MAIL_LOG_EVENT_COPY = 0x08,
+ MAIL_LOG_EVENT_SAVE = 0x08,
MAIL_LOG_EVENT_MAILBOX_DELETE = 0x10,
MAIL_LOG_EVENT_MAILBOX_RENAME = 0x20,
- MAIL_LOG_EVENT_FLAG_CHANGE = 0x40,
- MAIL_LOG_EVENT_APPEND = 0x80
+ MAIL_LOG_EVENT_FLAG_CHANGE = 0x40
};
#define MAIL_LOG_DEFAULT_EVENTS \
(MAIL_LOG_EVENT_DELETE | MAIL_LOG_EVENT_UNDELETE | \
- MAIL_LOG_EVENT_EXPUNGE | MAIL_LOG_EVENT_COPY | \
+ MAIL_LOG_EVENT_EXPUNGE | MAIL_LOG_EVENT_SAVE | \
MAIL_LOG_EVENT_MAILBOX_DELETE | MAIL_LOG_EVENT_MAILBOX_RENAME)
static const char *field_names[] = {
@@ -69,56 +66,30 @@ static const char *event_names[] = {
"delete",
"undelete",
"expunge",
- "copy",
+ "save",
"mailbox_delete",
"mailbox_rename",
"flag_change",
- "append",
NULL
};
-struct mail_log_user {
- union mail_user_module_context module_ctx;
-
+struct mail_log_settings {
enum mail_log_field fields;
enum mail_log_event events;
-
- unsigned int group_events:1;
-};
-
-struct mail_log_group_changes {
- enum mail_log_event event;
- const char *data;
-
- ARRAY_TYPE(seq_range) uids;
- uoff_t psize_total, vsize_total;
-};
-
-struct mail_log_transaction_context {
- union mailbox_transaction_module_context module_ctx;
+};
+
+
+struct mail_log_message {
+ struct mail_log_message *prev, *next;
+ const char *pretext, *text;
+};
+
+struct mail_log_mail_txn_context {
pool_t pool;
- struct mail *tmp_mail;
-
- ARRAY_DEFINE(group_changes, struct mail_log_group_changes);
-
- unsigned int changes;
-};
-
-const char *mail_log_plugin_version = PACKAGE_VERSION;
-
-static void (*mail_log_next_hook_mail_storage_created)
- (struct mail_storage *storage);
-static void (*mail_log_next_hook_mailbox_list_created)
- (struct mailbox_list *list);
-static void (*mail_log_next_hook_mail_user_created)(struct mail_user *user);
-
-static MODULE_CONTEXT_DEFINE_INIT(mail_log_storage_module,
- &mail_storage_module_register);
-static MODULE_CONTEXT_DEFINE_INIT(mail_log_mail_module, &mail_module_register);
-static MODULE_CONTEXT_DEFINE_INIT(mail_log_mailbox_list_module,
- &mailbox_list_module_register);
-static MODULE_CONTEXT_DEFINE_INIT(mail_log_mail_user_module,
- &mail_user_module_register);
+ struct mail_log_message *messages, *messages_tail;
+};
+
+static struct mail_log_settings mail_log_set;
static enum mail_log_field mail_log_field_find(const char *name)
{
@@ -142,584 +113,320 @@ static enum mail_log_event mail_log_even
return 0;
}
-static const char *mail_log_event_get_name(enum mail_log_event event)
-{
- unsigned int i;
-
- for (i = 0; event_names[i] != NULL; i++) {
- if ((unsigned)event == (unsigned)(1 << i))
- return event_names[i];
- }
- i_unreached();
- return NULL;
-}
-
-static struct mail_log_group_changes *
-mail_log_action_get_group(struct mail_log_transaction_context *lt,
- enum mail_log_event event, const char *data)
-{
- struct mail_log_group_changes *group;
- unsigned int i, count;
-
- if (!array_is_created(<->group_changes))
- p_array_init(<->group_changes, lt->pool, 8);
-
- group = array_get_modifiable(<->group_changes, &count);
- for (i = 0; i < count; i++) {
- if (group[i].event == event &&
- null_strcmp(data, group[i].data) == 0)
- return &group[i];
- }
-
- group = array_append_space(<->group_changes);
- group->event = event;
- group->data = p_strdup(lt->pool, data);
- return group;
-}
-
-static void
-mail_log_action_add_group(struct mail_log_transaction_context *lt,
- struct mail *mail, enum mail_log_event event,
- const char *data)
-{
- struct mail_log_user *muser =
- MAIL_LOG_USER_CONTEXT(mail->box->storage->user);
- struct mail_log_group_changes *group;
- uoff_t size;
-
- group = mail_log_action_get_group(lt, event, data);
-
- if ((muser->fields & MAIL_LOG_FIELD_UID) != 0) {
- if (!array_is_created(&group->uids))
- p_array_init(&group->uids, lt->pool, 32);
- seq_range_array_add(&group->uids, 0, mail->uid);
- }
-
- if ((muser->fields & MAIL_LOG_FIELD_PSIZE) != 0 &&
- (event & (MAIL_LOG_EVENT_EXPUNGE | MAIL_LOG_EVENT_COPY)) != 0) {
- if (mail_get_physical_size(mail, &size) == 0)
- group->psize_total += size;
- }
-
- if ((muser->fields & MAIL_LOG_FIELD_VSIZE) != 0 &&
- (event & (MAIL_LOG_EVENT_EXPUNGE | MAIL_LOG_EVENT_COPY)) != 0) {
- if (mail_get_virtual_size(mail, &size) == 0)
- group->vsize_total += size;
- }
-}
-
-static void mail_log_append_mailbox_name(string_t *str, struct mailbox *box)
+static enum mail_log_field mail_log_parse_fields(const char *str)
+{
+ const char *const *tmp;
+ static enum mail_log_field field, fields = 0;
+
+ for (tmp = t_strsplit_spaces(str, ", "); *tmp != NULL; tmp++) {
+ field = mail_log_field_find(*tmp);
+ if (field == 0)
+ i_fatal("Unknown field in mail_log_fields: '%s'", *tmp);
+ fields |= field;
+ }
+ return fields;
+}
+
+static enum mail_log_event mail_log_parse_events(const char *str)
+{
+ const char *const *tmp;
+ static enum mail_log_event event, events = 0;
+
+ for (tmp = t_strsplit_spaces(str, ", "); *tmp != NULL; tmp++) {
+ event = mail_log_event_find(*tmp);
+ if (event == 0)
+ i_fatal("Unknown event in mail_log_events: '%s'", *tmp);
+ events |= event;
+ }
+ return events;
+}
+
+static void mail_log_read_settings(struct mail_log_settings *set)
+{
+ const char *str;
+
+ memset(set, 0, sizeof(*set));
+
+ str = getenv("MAIL_LOG_FIELDS");
+ set->fields = str == NULL ? MAIL_LOG_DEFAULT_FIELDS :
+ mail_log_parse_fields(str);
+
+ str = getenv("MAIL_LOG_EVENTS");
+ set->events = str == NULL ? MAIL_LOG_DEFAULT_EVENTS :
+ mail_log_parse_events(str);
+}
+
+static void mail_log_append_mailbox_name(string_t *str, struct mail *mail)
{
const char *mailbox_str;
- /* most operations are for INBOX, and POP3 has only INBOX,
- so don't add it. */
- mailbox_str = mailbox_get_vname(box);
- if (strcmp(mailbox_str, "INBOX") != 0) {
- str_printfa(str, "box=%s, ",
- str_sanitize(mailbox_str, MAILBOX_NAME_LOG_LEN));
- }
-}
-
-static void
-mail_log_group(struct mailbox *box, const struct mail_log_group_changes *group)
-{
- struct mail_log_user *muser =
- MAIL_LOG_USER_CONTEXT(box->storage->user);
- const struct seq_range *range;
- unsigned int i, count;
- string_t *str;
-
- str = t_str_new(128);
- str_printfa(str, "%s: ", mail_log_event_get_name(group->event));
-
- if ((muser->fields & MAIL_LOG_FIELD_UID) != 0 &&
- array_is_created(&group->uids)) {
- str_append(str, "uids=");
-
- range = array_get(&group->uids, &count);
- for (i = 0; i < count; i++) {
- if (i != 0)
- str_append_c(str, ',');
-
- str_printfa(str, "%u", range[i].seq1);
- if (range[i].seq1 != range[i].seq2)
- str_printfa(str, "-%u", range[i].seq2);
- }
- str_append(str, ", ");
- }
-
- if ((muser->fields & MAIL_LOG_FIELD_BOX) != 0)
- mail_log_append_mailbox_name(str, box);
-
- if (group->event == MAIL_LOG_EVENT_COPY)
- str_printfa(str, "dest=%s, ", group->data);
-
- if (group->psize_total != 0)
- str_printfa(str, "size=%"PRIuUOFF_T", ", group->psize_total);
- if (group->vsize_total != 0)
- str_printfa(str, "size=%"PRIuUOFF_T", ", group->vsize_total);
- str_truncate(str, str_len(str)-2);
-
- i_info("%s", str_c(str));
-}
-
-static void
-mail_log_group_changes(struct mailbox *box,
- struct mail_log_transaction_context *lt)
-{
- const struct mail_log_group_changes *group;
More information about the dovecot-cvs
mailing list