summaryrefslogtreecommitdiffstats
path: root/src/plugins/notify-status/notify-status-plugin.c
blob: 6d0f2a6a75258211f9fc2bb9e1e292b35b577a62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/* Copyright (c) 2017-2018 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "array.h"
#include "json-parser.h"
#include "str.h"
#include "var-expand.h"
#include "mail-user.h"
#include "mail-storage.h"
#include "mail-storage-private.h"
#include "mail-namespace.h"
#include "mail-storage-hooks.h"
#include "imap-match.h"
#include "dict.h"
#include "notify-plugin.h"

#define NOTIFY_STATUS_SETTING_DICT_URI "notify_status_dict"
#define NOTIFY_STATUS_SETTING_MAILBOX_PREFIX "notify_status_mailbox"
#define NOTIFY_STATUS_SETTING_VALUE_TEMPLATE "notify_status_value"
#define NOTIFY_STATUS_SETTING_VALUE_TEMPLATE_DEFAULT "{\"messages\":%{messages},\"unseen\":%{unseen}}"
#define NOTIFY_STATUS_KEY "priv/status/%s"

#define NOTIFY_STATUS_USER_CONTEXT(obj) \
	(struct notify_status_user*)MODULE_CONTEXT(obj, notify_status_user_module)

static MODULE_CONTEXT_DEFINE_INIT(notify_status_user_module,
				  &mail_user_module_register);

void notify_status_plugin_init(struct module *module);
void notify_status_plugin_deinit(void);

const char *notify_status_plugin_version = DOVECOT_ABI_VERSION;
const char *notify_status_plugin_dependencies[] = { "notify", NULL };

ARRAY_DEFINE_TYPE(imap_match_glob, struct imap_match_glob*);

struct notify_status_mail_txn {
	struct mailbox *box;
	bool changed:1;
};

struct notify_status_user {
	union mail_user_module_context module_ctx;

	ARRAY_TYPE(imap_match_glob) patterns;
	struct dict *dict;
	const char *value_template;
	struct notify_context *context;
};

static int notify_status_dict_init(struct mail_user *user, const char *uri,
				   struct dict **dict_r, const char **error_r)
{
	struct dict_settings set = {
		.base_dir = user->set->base_dir,
		.event_parent = user->event,
	};
	if (dict_init(uri, &set, dict_r, error_r) < 0) {
		*error_r = t_strdup_printf("dict_init(%s) failed: %s",
					   uri, *error_r);
		return -1;
	}
	return 0;
}

static void notify_status_mailbox_patterns_init(struct mail_user *user,
						ARRAY_TYPE(imap_match_glob) *patterns)
{
	const char *value;
	unsigned int i;

	p_array_init(patterns, user->pool, 2);

	for(i=1;;i++) {
		struct imap_match_glob **glob;
		const char *key = NOTIFY_STATUS_SETTING_MAILBOX_PREFIX;
		if (i > 1)
			key = t_strdup_printf("%s%u", key, i);
		value = mail_user_plugin_getenv(user, key);
		if (value == NULL)
			return;
		char sep = mail_namespace_get_sep(user->namespaces);
		glob = array_append_space(patterns);
		*glob = imap_match_init(user->pool, value, TRUE, sep);
	}
}

static bool notify_status_mailbox_enabled(struct mailbox *box)
{
	struct mail_user *user = mail_storage_get_user(mailbox_get_storage(box));
	struct notify_status_user *nuser = NOTIFY_STATUS_USER_CONTEXT(user);
	struct imap_match_glob *glob;
	/* not enabled */
	if (nuser == NULL)
		return FALSE;

	/* if no patterns defined, anything goes */
	if (array_count(&nuser->patterns) == 0)
		return TRUE;

	array_foreach_elem(&nuser->patterns, glob) {
		if ((imap_match(glob, mailbox_get_vname(box)) & IMAP_MATCH_YES) != 0)
			return TRUE;
	}
	return FALSE;
}

static void notify_update_callback(const struct dict_commit_result *result,
				   void *context ATTR_UNUSED)
{
	if (result->ret == DICT_COMMIT_RET_OK ||
	    result->ret == DICT_COMMIT_RET_NOTFOUND)
		return;

	i_error("notify-status: dict_transaction_commit failed: %s",
		result->error == NULL ? "" : result->error);
}

#define MAILBOX_STATUS_NOTIFY (STATUS_MESSAGES|STATUS_UNSEEN|\
			       STATUS_RECENT|STATUS_UIDNEXT|\
			       STATUS_UIDVALIDITY|\
			       STATUS_HIGHESTMODSEQ|STATUS_FIRST_RECENT_UID|\
			       STATUS_HIGHESTPVTMODSEQ)
static void notify_update_mailbox_status(struct mailbox *box)
{
	struct mail_user *user = mail_storage_get_user(mailbox_get_storage(box));
	struct notify_status_user *nuser = NOTIFY_STATUS_USER_CONTEXT(user);
	i_assert(nuser != NULL);
	struct dict_transaction_context *t;
	struct mailbox_status status;

	e_debug(box->event, "notify-status: Updating mailbox status");

	box = mailbox_alloc(mailbox_get_namespace(box)->list,
			   mailbox_get_vname(box), MAILBOX_FLAG_READONLY);

	if (mailbox_open(box) < 0) {
		i_error("notify-status: mailbox_open(%s) failed: %s",
			  mailbox_get_vname(box),
			  mailbox_get_last_error(box, NULL));
	} else if (mailbox_sync(box, MAILBOX_SYNC_FLAG_FULL_READ) < 0) {
		i_error("notify-status: mailbox_sync(%s) failed: %s",
			  mailbox_get_vname(box),
			  mailbox_get_last_error(box, NULL));
	} else if (mailbox_get_status(box, MAILBOX_STATUS_NOTIFY,
				      &status) < 0) {
		i_error("notify-status: mailbox_get_status(%s) failed: %s",
			  mailbox_get_vname(box),
			  mailbox_get_last_error(box, NULL));
	} else {
		string_t *username = t_str_new(strlen(user->username));
		string_t *mboxname = t_str_new(64);

		json_append_escaped(username, user->username);
		json_append_escaped(mboxname, mailbox_get_vname(box));

		const struct var_expand_table values[] = {
			{ '\0', str_c(username), "username" },
			{ '\0', str_c(mboxname), "mailbox" },
			{ '\0', dec2str(status.messages), "messages" },
			{ '\0', dec2str(status.unseen), "unseen" },
			{ '\0', dec2str(status.recent), "recent" },
			{ '\0', dec2str(status.uidvalidity), "uidvalidity" },
			{ '\0', dec2str(status.uidnext), "uidnext" },
			{ '\0', dec2str(status.first_recent_uid), "first_recent_uid" },
			{ '\0', dec2str(status.highest_modseq), "highest_modseq" },
			{ '\0', dec2str(status.highest_pvt_modseq), "highest_pvt_modseq" },
			{ '\0', NULL, NULL }
		};
		const char *error;
		const char *key =
			t_strdup_printf(NOTIFY_STATUS_KEY, mailbox_get_vname(box));
		string_t *dest = t_str_new(64);
		if (var_expand(dest, nuser->value_template, values, &error)<0) {
			i_error("notify-status: var_expand(%s) failed: %s",
				nuser->value_template, error);
		} else {
			const struct dict_op_settings *set = mail_user_get_dict_op_settings(user);
			t = dict_transaction_begin(nuser->dict, set);
			dict_set(t, key, str_c(dest));
			dict_transaction_commit_async(&t, notify_update_callback, NULL) ;
		}
	}

	mailbox_free(&box);
}

static void notify_remove_mailbox_status(struct mailbox *box)
{
	struct mail_user *user = mail_storage_get_user(mailbox_get_storage(box));
	struct notify_status_user *nuser = NOTIFY_STATUS_USER_CONTEXT(user);
	i_assert(nuser != NULL);
	struct dict_transaction_context *t;

	e_debug(box->event, "notify-status: Removing mailbox status");

	const char *key =
		t_strdup_printf(NOTIFY_STATUS_KEY, mailbox_get_vname(box));

	const struct dict_op_settings *set = mail_user_get_dict_op_settings(user);
	t = dict_transaction_begin(nuser->dict, set);
	dict_unset(t, key);
	dict_transaction_commit_async(&t, notify_update_callback, NULL) ;
}

static void *notify_status_mail_transaction_begin(struct mailbox_transaction_context *t)
{
	struct notify_status_mail_txn *txn = i_new(struct notify_status_mail_txn, 1);
	txn->box = mailbox_transaction_get_mailbox(t);
	return txn;
}

static void
notify_status_mail_transaction_commit(void *t,
				      struct mail_transaction_commit_changes *changes ATTR_UNUSED)
{
	struct notify_status_mail_txn *txn = (struct notify_status_mail_txn *)t;
	if (txn->changed && notify_status_mailbox_enabled(txn->box))
		notify_update_mailbox_status(txn->box);
	i_free(txn);
}

static void notify_status_mail_transaction_rollback(void *t)
{
	i_free(t);
}

static void notify_status_mailbox_create(struct mailbox *box)
{
	if (notify_status_mailbox_enabled(box))
		notify_update_mailbox_status(box);
}

static void notify_status_mailbox_delete_commit(void *txn ATTR_UNUSED,
						struct mailbox *box)
{
	if (notify_status_mailbox_enabled(box))
		notify_remove_mailbox_status(box);
}

static void notify_status_mailbox_rename(struct mailbox *src, struct mailbox *dest)
{
	if (notify_status_mailbox_enabled(src))
		notify_remove_mailbox_status(src);
	if (notify_status_mailbox_enabled(dest))
		notify_update_mailbox_status(dest);
}

static void notify_status_mail_save(void *t, struct mail *mail ATTR_UNUSED)
{
	struct notify_status_mail_txn *txn = (struct notify_status_mail_txn *)t;
	txn->changed = TRUE;
}

static void notify_status_mail_copy(void *t, struct mail *src ATTR_UNUSED,
				    struct mail *dst ATTR_UNUSED)
{
	struct notify_status_mail_txn *txn = (struct notify_status_mail_txn *)t;
	txn->changed = TRUE;
}

static void notify_status_mail_expunge(void *t, struct mail *mail ATTR_UNUSED)
{
	struct notify_status_mail_txn *txn = (struct notify_status_mail_txn *)t;
	txn->changed = TRUE;
}
static void notify_status_mail_update_flags(void *t, struct mail *mail,
					    enum mail_flags old_flags)
{
	struct notify_status_mail_txn *txn = (struct notify_status_mail_txn *)t;
	if ((old_flags & MAIL_SEEN) != (mail_get_flags(mail) & MAIL_SEEN))
		txn->changed = TRUE;
}

static const struct notify_vfuncs notify_vfuncs =
{
	.mail_transaction_begin = notify_status_mail_transaction_begin,
	.mail_save = notify_status_mail_save,
	.mail_copy = notify_status_mail_copy,
	.mail_expunge = notify_status_mail_expunge,
	.mail_update_flags = notify_status_mail_update_flags,
	.mail_transaction_commit = notify_status_mail_transaction_commit,
	.mail_transaction_rollback = notify_status_mail_transaction_rollback,
	.mailbox_create = notify_status_mailbox_create,
	.mailbox_delete_commit = notify_status_mailbox_delete_commit,
	.mailbox_rename = notify_status_mailbox_rename,
};

static void notify_status_mail_user_deinit(struct mail_user *user)
{
	struct notify_status_user *nuser = NOTIFY_STATUS_USER_CONTEXT(user);
	i_assert(nuser != NULL);

	dict_wait(nuser->dict);
	dict_deinit(&nuser->dict);
	if (nuser->context != NULL)
		notify_unregister(nuser->context);
	nuser->module_ctx.super.deinit(user);
}

static void notify_status_mail_user_created(struct mail_user *user)
{
	struct mail_user_vfuncs *v = user->vlast;
	struct notify_status_user *nuser;
	struct dict *dict;
	const char *error;
	const char *template = mail_user_plugin_getenv(user, NOTIFY_STATUS_SETTING_VALUE_TEMPLATE);
	const char *uri = mail_user_plugin_getenv(user, NOTIFY_STATUS_SETTING_DICT_URI);

	if (user->autocreated)
		return;

	if (uri == NULL || *uri == '\0') {
		e_debug(user->event, "notify-status: Disabled - Missing plugin/"
			NOTIFY_STATUS_SETTING_DICT_URI" setting");
		return;
	}

	if (template == NULL || *template == '\0')
		template = NOTIFY_STATUS_SETTING_VALUE_TEMPLATE_DEFAULT;

	if (notify_status_dict_init(user, uri, &dict, &error) < 0) {
		i_error("notify-status: %s", error);
		return;
	}

	nuser = p_new(user->pool, struct notify_status_user, 1);
	nuser->module_ctx.super = *v;
	nuser->dict = dict;
	user->vlast = &nuser->module_ctx.super;
	v->deinit = notify_status_mail_user_deinit;
	/* either static value or lifetime is user object's lifetime */
	nuser->value_template = template;

	MODULE_CONTEXT_SET(user, notify_status_user_module, nuser);
}

static void
notify_status_mail_namespaces_created(struct mail_namespace *namespaces)
{
	struct mail_user *user = namespaces->user;
	struct notify_status_user *nuser = NOTIFY_STATUS_USER_CONTEXT(user);
	if (nuser == NULL)
		return;
        notify_status_mailbox_patterns_init(user, &nuser->patterns);
	nuser->context = notify_register(&notify_vfuncs);
}

static const struct mail_storage_hooks notify_storage_hooks =
{
	.mail_user_created = notify_status_mail_user_created,
	.mail_namespaces_created = notify_status_mail_namespaces_created,
};

void notify_status_plugin_init(struct module *module)
{
	mail_storage_hooks_add(module, &notify_storage_hooks);
}

void notify_status_plugin_deinit(void)
{
	mail_storage_hooks_remove(&notify_storage_hooks);
}