summaryrefslogtreecommitdiffstats
path: root/src/imap/cmd-notify.c
blob: e043e9726858cca7c85467230d19a2e592a452f6 (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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
/* Copyright (c) 2013-2018 Dovecot authors, see the included COPYING file */

#include "imap-common.h"
#include "str.h"
#include "mailbox-list-iter.h"
#include "imap-quote.h"
#include "imap-commands.h"
#include "imap-fetch.h"
#include "imap-list.h"
#include "imap-status.h"
#include "imap-notify.h"

#define IMAP_NOTIFY_MAX_NAMES_PER_NS 100

static const char *imap_notify_event_names[] = {
	"MessageNew", "MessageExpunge", "FlagChange", "AnnotationChange",
	"MailboxName", "SubscriptionChange", "MailboxMetadataChange",
	"ServerMetadataChange"
};

static int
cmd_notify_parse_event(const struct imap_arg *arg,
		       enum imap_notify_event *event_r)
{
	const char *str;
	unsigned int i;

	if (!imap_arg_get_atom(arg, &str))
		return -1;

	for (i = 0; i < N_ELEMENTS(imap_notify_event_names); i++) {
		if (strcasecmp(str, imap_notify_event_names[i]) == 0) {
			*event_r = (enum imap_notify_event)(1 << i);
			return 0;
		}
	}
	return -1;
}

static int
cmd_notify_parse_fetch(struct imap_notify_context *ctx,
		       const struct imap_arg *list)
{
	if (list->type == IMAP_ARG_EOL)
		return -1; /* at least one attribute must be set */
	return imap_fetch_att_list_parse(ctx->client, ctx->pool, list,
					 &ctx->fetch_ctx, &ctx->error);
}

static int
cmd_notify_set_selected(struct imap_notify_context *ctx,
			const struct imap_arg *events)
{
#define EV_NEW_OR_EXPUNGE \
	(IMAP_NOTIFY_EVENT_MESSAGE_NEW | IMAP_NOTIFY_EVENT_MESSAGE_EXPUNGE)
	const struct imap_arg *list, *fetch_att_list;
	const char *str;
	enum imap_notify_event event;

	if (imap_arg_get_atom(events, &str) &&
	    strcasecmp(str, "NONE") == 0) {
		/* no events for selected mailbox. this is also the default
		   when NOTIFY command doesn't specify it explicitly */
		if (events[1].type != IMAP_ARG_EOL)
			return -1; /* no extra parameters */
		return 0;
	}

	if (!imap_arg_get_list(events, &list))
		return -1;
	if (events[1].type != IMAP_ARG_EOL)
		return -1; /* no extra parameters */
	if (list->type == IMAP_ARG_EOL)
		return -1; /* at least one event */

	for (; list->type != IMAP_ARG_EOL; list++) {
		if (cmd_notify_parse_event(list, &event) < 0)
			return -1;
		ctx->selected_events |= event;
		ctx->global_used_events |= event;

		if (event == IMAP_NOTIFY_EVENT_MESSAGE_NEW &&
		    imap_arg_get_list(&list[1], &fetch_att_list)) {
			/* MessageNew: list of fetch-att */
			if (cmd_notify_parse_fetch(ctx, fetch_att_list) < 0)
				return -1;
			list++;
		}
	}

	/* if MessageNew or MessageExpunge is specified, both of them must */
	if ((ctx->selected_events & EV_NEW_OR_EXPUNGE) != 0 &&
	    (ctx->selected_events & EV_NEW_OR_EXPUNGE) != EV_NEW_OR_EXPUNGE) {
		ctx->error = "MessageNew and MessageExpunge must be together";
		return -1;
	}

	/* if FlagChange or AnnotationChange is specified,
	   MessageNew and MessageExpunge must also be specified */
	if ((ctx->selected_events &
	     (IMAP_NOTIFY_EVENT_FLAG_CHANGE |
	      IMAP_NOTIFY_EVENT_ANNOTATION_CHANGE)) != 0 &&
	    (ctx->selected_events & IMAP_NOTIFY_EVENT_MESSAGE_EXPUNGE) == 0) {
		ctx->error = "FlagChange requires MessageNew and MessageExpunge";
		return -1;
	}
	return 0;
}

static struct imap_notify_namespace *
imap_notify_namespace_get(struct imap_notify_context *ctx,
			  struct mail_namespace *ns)
{
	struct imap_notify_namespace *notify_ns;

	array_foreach_modifiable(&ctx->namespaces, notify_ns) {
		if (notify_ns->ns == ns)
			return notify_ns;
	}
	notify_ns = array_append_space(&ctx->namespaces);
	notify_ns->ctx = ctx;
	notify_ns->ns = ns;
	p_array_init(&notify_ns->mailboxes, ctx->pool, 4);
	return notify_ns;
}

static struct imap_notify_mailboxes *
imap_notify_mailboxes_get(struct imap_notify_namespace *notify_ns,
			  enum imap_notify_type type,
			  enum imap_notify_event events)
{
	struct imap_notify_mailboxes *notify_boxes;

	array_foreach_modifiable(&notify_ns->mailboxes, notify_boxes) {
		if (notify_boxes->type == type &&
		    notify_boxes->events == events)
			return notify_boxes;
	}
	notify_boxes = array_append_space(&notify_ns->mailboxes);
	notify_boxes->type = type;
	notify_boxes->events = events;
	p_array_init(&notify_boxes->names, notify_ns->ctx->pool, 4);
	return notify_boxes;
}

static void
cmd_notify_add_mailbox(struct imap_notify_context *ctx,
		       struct mail_namespace *ns, const char *name,
		       enum imap_notify_type type,
		       enum imap_notify_event events)
{
	struct imap_notify_namespace *notify_ns;
	struct imap_notify_mailboxes *notify_boxes;
	const char *const *names;
	unsigned int i, count;
	size_t cur_len, name_len = strlen(name);
	char ns_sep = mail_namespace_get_sep(ns);

	if (mail_namespace_is_removable(ns)) {
		/* exclude removable namespaces */
		return;
	}

	if ((ns->flags & NAMESPACE_FLAG_INBOX_USER) != 0 &&
	    !str_begins(name, "INBOX") &&
	    strncasecmp(name, "INBOX", 5) == 0 &&
	    (name[5] == '\0' || name[5] == ns_sep)) {
		/* we'll do only case-sensitive comparisons later,
		   so sanitize INBOX to be uppercase */
		name = t_strconcat("INBOX", name + 5, NULL);
	}

	notify_ns = imap_notify_namespace_get(ctx, ns);
	notify_boxes = imap_notify_mailboxes_get(notify_ns, type, events);

	names = array_get(&notify_boxes->names, &count);
	for (i = 0; i < count; ) {
		if (strcmp(names[i], name) == 0) {
			/* exact duplicate, already added */
			return;
		}
		if (type != IMAP_NOTIFY_TYPE_SUBTREE)
			i++;
		else {
			/* see if one is a subtree of the other */
			cur_len = strlen(names[i]);
			if (str_begins(name, names[i]) &&
			    names[i][cur_len] == ns_sep) {
				/* already matched in this subtree */
				return;
			}
			if (strncmp(names[i], name, name_len) == 0 &&
			    names[i][name_len] == ns_sep) {
				/* we're adding a parent, remove the child */
				array_delete(&notify_boxes->names, i, 1);
				names = array_get(&notify_boxes->names, &count);
			} else {
				i++;
			}
		}
	}
	name = p_strdup(ctx->pool, name);
	array_push_back(&notify_boxes->names, &name);

	ctx->global_max_mailbox_names =
		I_MAX(ctx->global_max_mailbox_names,
		      array_count(&notify_boxes->names));
}

static void cmd_notify_add_personal(struct imap_notify_context *ctx,
				    enum imap_notify_event events)
{
	struct mail_namespace *ns;

	for (ns = ctx->client->user->namespaces; ns != NULL; ns = ns->next) {
		if (ns->type == MAIL_NAMESPACE_TYPE_PRIVATE) {
			cmd_notify_add_mailbox(ctx, ns, "",
				IMAP_NOTIFY_TYPE_SUBTREE, events);
		}
	}
}

static int
imap_notify_refresh_subscriptions(struct client_command_context *cmd,
				  struct imap_notify_context *ctx)
{
	struct mailbox_list_iterate_context *iter;
	struct mail_namespace *ns;

	if (!ctx->have_subscriptions)
		return 0;

	/* make sure subscriptions are refreshed at least once */
	for (ns = ctx->client->user->namespaces; ns != NULL; ns = ns->next) {
		iter = mailbox_list_iter_init(ns->list, "*", MAILBOX_LIST_ITER_SELECT_SUBSCRIBED);
		(void)mailbox_list_iter_next(iter);
		if (mailbox_list_iter_deinit(&iter) < 0) {
			client_send_list_error(cmd, ns->list);
			return -1;
		}
	}
	return 0;
}

static void cmd_notify_add_subscribed(struct imap_notify_context *ctx,
				      enum imap_notify_event events)
{
	struct mail_namespace *ns;

	ctx->have_subscriptions = TRUE;
	for (ns = ctx->client->user->namespaces; ns != NULL; ns = ns->next) {
		cmd_notify_add_mailbox(ctx, ns, "",
				       IMAP_NOTIFY_TYPE_SUBSCRIBED, events);
	}
}

static void
cmd_notify_add_mailbox_namespaces(struct imap_notify_context *ctx,
				  const char *name,
				  enum imap_notify_type type,
				  enum imap_notify_event events)
{
	struct mail_namespace *ns;

	ns = mail_namespace_find(ctx->client->user->namespaces, name);
	cmd_notify_add_mailbox(ctx, ns, name, type, events);
}

static int
cmd_notify_add_mailboxes(struct imap_notify_context *ctx,
			 const struct imap_arg *arg,
			 enum imap_notify_type type,
			 enum imap_notify_event events)
{
	const struct imap_arg *list;
	const char *name;

	if (imap_arg_get_astring(arg, &name)) {
		cmd_notify_add_mailbox_namespaces(ctx, name, type, events);
		return 0;
	}
	if (!imap_arg_get_list(arg, &list))
		return -1;

	for (; list->type != IMAP_ARG_EOL; list++) {
		if (!imap_arg_get_astring(list, &name))
			return -1;

		cmd_notify_add_mailbox_namespaces(ctx, name, type, events);
	}
	return 0;
}

static int
cmd_notify_set(struct imap_notify_context *ctx, const struct imap_arg *args)
{
	const struct imap_arg *event_group, *mailboxes, *list;
	const char *str, *filter_mailboxes;
	enum imap_notify_event event, event_mask;

	if (imap_arg_get_atom(args, &str) &&
	    strcasecmp(str, "STATUS") == 0) {
		/* send STATUS replies for all matched mailboxes before
		   NOTIFY's OK reply */
		ctx->send_immediate_status = TRUE;
		args++;
	}
	for (; args->type != IMAP_ARG_EOL; args++) {
		if (!imap_arg_get_list(args, &event_group))
			return -1;

		/* filter-mailboxes */
		if (!imap_arg_get_atom(event_group, &filter_mailboxes))
			return -1;
		event_group++;

		if (strcasecmp(filter_mailboxes, "selected") == 0 ||
		    strcasecmp(filter_mailboxes, "selected-delayed") == 0) {
			/* setting events for selected mailbox.
			   handle specially. */
			if (ctx->selected_set) {
				ctx->error = "Duplicate selected filter";
				return -1;
			}
			ctx->selected_set = TRUE;
			if (strcasecmp(filter_mailboxes, "selected") == 0)
				ctx->selected_immediate_expunges = TRUE;
			if (cmd_notify_set_selected(ctx, event_group) < 0)
				return -1;
			continue;
		}

		if (strcasecmp(filter_mailboxes, "subtree") == 0 ||
		    strcasecmp(filter_mailboxes, "mailboxes") == 0) {
			if (event_group->type == IMAP_ARG_EOL)
				return -1;
			mailboxes = event_group++;
			/* check that the mailboxes parameter is valid */
			if (IMAP_ARG_IS_ASTRING(mailboxes))
				;
			else if (!imap_arg_get_list(mailboxes, &list))
				return -1;
			else if (list->type == IMAP_ARG_EOL) {
				/* should have at least one mailbox */
				return -1;
			}
		} else {
			mailboxes = NULL;
		}

		/* parse events */
		if (imap_arg_get_atom(event_group, &str) &&
		    strcasecmp(str, "NONE") == 0) {
			/* NONE is the default, ignore this */
			continue;
		}
		if (!imap_arg_get_list(event_group, &list) ||
		    list[0].type == IMAP_ARG_EOL)
			return -1;

		event_mask = 0;
		for (; list->type != IMAP_ARG_EOL; list++) {
			if (cmd_notify_parse_event(list, &event) < 0)
				return -1;
			event_mask |= event;
			ctx->global_used_events |= event;
		}

		/* we can't currently know inboxes, so treat it the
		   same as personal */
		if (strcasecmp(filter_mailboxes, "inboxes") == 0 ||
		    strcasecmp(filter_mailboxes, "personal") == 0)
			cmd_notify_add_personal(ctx, event_mask);
		else if (strcasecmp(filter_mailboxes, "subscribed") == 0)
			cmd_notify_add_subscribed(ctx, event_mask);
		else if (strcasecmp(filter_mailboxes, "subtree") == 0) {
			if (cmd_notify_add_mailboxes(ctx, mailboxes,
						     IMAP_NOTIFY_TYPE_SUBTREE,
						     event_mask) < 0)
				return -1;
		} else if (strcasecmp(filter_mailboxes, "mailboxes") == 0) {
			if (cmd_notify_add_mailboxes(ctx, mailboxes,
						     IMAP_NOTIFY_TYPE_MAILBOX,
						     event_mask) < 0)
				return -1;
		} else {
			return -1;
		}
	}
	return 0;
}

static void
imap_notify_box_list_noperm(struct client *client, struct mailbox *box)
{
	string_t *str = t_str_new(128);
	char ns_sep = mail_namespace_get_sep(mailbox_get_namespace(box));
	enum mailbox_info_flags mailbox_flags;

	if (mailbox_list_mailbox(mailbox_get_namespace(box)->list,
				 mailbox_get_name(box), &mailbox_flags) < 0)
		mailbox_flags = 0;

	str_append(str, "* LIST (");
	if (imap_mailbox_flags2str(str, mailbox_flags))
		str_append_c(str, ' ');
	str_append(str, "\\NoAccess) \"");
	if (ns_sep == '\\')
		str_append_c(str, '\\');
	str_append_c(str, ns_sep);
	str_append(str, "\" ");

	imap_append_astring(str, mailbox_get_vname(box));
	client_send_line(client, str_c(str));
}

static void
imap_notify_box_send_status(struct client_command_context *cmd,
			    struct imap_notify_context *ctx,
			    const struct mailbox_info *info)
{
	struct mailbox *box;
	struct imap_status_items items;
	struct imap_status_result result;

	if ((info->flags & (MAILBOX_NONEXISTENT | MAILBOX_NOSELECT)) != 0)
		return;

	/* don't send STATUS to selected mailbox */
	if (cmd->client->mailbox != NULL &&
	    mailbox_equals(cmd->client->mailbox, info->ns, info->vname))
		return;

	i_zero(&items);
	i_zero(&result);

	items.flags = IMAP_STATUS_ITEM_UIDVALIDITY | IMAP_STATUS_ITEM_UIDNEXT |
		IMAP_STATUS_ITEM_MESSAGES | IMAP_STATUS_ITEM_UNSEEN;
	if ((ctx->global_used_events & (IMAP_NOTIFY_EVENT_FLAG_CHANGE |
					IMAP_NOTIFY_EVENT_ANNOTATION_CHANGE)) != 0)
		items.flags |= IMAP_STATUS_ITEM_HIGHESTMODSEQ;

	box = mailbox_alloc(info->ns->list, info->vname, MAILBOX_FLAG_READONLY);
	(void)mailbox_enable(box, client_enabled_mailbox_features(ctx->client));

	if (imap_status_get(cmd, info->ns, info->vname, &items, &result) < 0) {
		if (result.error == MAIL_ERROR_PERM)
			imap_notify_box_list_noperm(ctx->client, box);
		else if (result.error != MAIL_ERROR_NOTFOUND) {
			client_send_line(ctx->client,
				t_strconcat("* ", result.errstr, NULL));
		}
	} else {
		imap_status_send(ctx->client, info->vname, &items, &result);
	}
	mailbox_free(&box);
}

static bool imap_notify_ns_want_status(struct imap_notify_namespace *notify_ns)
{
	const struct imap_notify_mailboxes *notify_boxes;

	array_foreach(&notify_ns->mailboxes, notify_boxes) {
		if ((notify_boxes->events &
		     (IMAP_NOTIFY_EVENT_MESSAGE_NEW |
		      IMAP_NOTIFY_EVENT_MESSAGE_EXPUNGE |
		      IMAP_NOTIFY_EVENT_ANNOTATION_CHANGE |
		      IMAP_NOTIFY_EVENT_FLAG_CHANGE)) != 0)
			return TRUE;
	}
	return FALSE;
}

static void
imap_notify_ns_send_status(struct client_command_context *cmd,
			   struct imap_notify_context *ctx,
			   struct imap_notify_namespace *notify_ns)
{
	struct mailbox_list_iterate_context *iter;
	const struct imap_notify_mailboxes *notify_boxes;
	const struct mailbox_info *info;

	if (!imap_notify_ns_want_status(notify_ns))
		return;

	/* set _RETURN_SUBSCRIBED flag just in case IMAP_NOTIFY_TYPE_SUBSCRIBED
	   is used, which requires refreshing subscriptions */
	iter = mailbox_list_iter_init(notify_ns->ns->list, "*",
				      MAILBOX_LIST_ITER_RETURN_SUBSCRIBED |
				      MAILBOX_LIST_ITER_RETURN_NO_FLAGS);
	while ((info = mailbox_list_iter_next(iter)) != NULL) {
		array_foreach(&notify_ns->mailboxes, notify_boxes) {
			if (imap_notify_match_mailbox(notify_ns, notify_boxes,
						      info->vname)) {
				imap_notify_box_send_status(cmd, ctx, info);
				break;
			}
		}
	}
	if (mailbox_list_iter_deinit(&iter) < 0) {
		client_send_line(notify_ns->ctx->client,
				 "* NO Mailbox listing failed");
	}
}

static void cmd_notify_send_status(struct client_command_context *cmd,
				   struct imap_notify_context *ctx)
{
	struct imap_notify_namespace *notify_ns;

	array_foreach_modifiable(&ctx->namespaces, notify_ns)
		imap_notify_ns_send_status(cmd, ctx, notify_ns);
}

bool cmd_notify(struct client_command_context *cmd)
{
	struct imap_notify_context *ctx;
	const struct imap_arg *args;
	const char *str;
	int ret = 0;
	pool_t pool;

	if (!client_read_args(cmd, 0, 0, &args))
		return FALSE;

	pool = pool_alloconly_create("imap notify context", 1024);
	ctx = p_new(pool, struct imap_notify_context, 1);
	ctx->pool = pool;
	ctx->client = cmd->client;
	p_array_init(&ctx->namespaces, pool, 4);

	if (!imap_arg_get_atom(&args[0], &str))
		ret = -1;
	else if (strcasecmp(str, "NONE") == 0)
		;
	else if (strcasecmp(str, "SET") == 0)
		ret = cmd_notify_set(ctx, args+1);
	else
		ret = -1;

	if (ret < 0) {
		client_send_command_error(cmd, ctx->error != NULL ? ctx->error :
					  "Invalid arguments.");
		pool_unref(&pool);
		return TRUE;
	}

	if ((ctx->global_used_events & UNSUPPORTED_EVENTS) != 0) {
		string_t *client_error = t_str_new(128);
		unsigned int i;

		str_append(client_error, "NO [BADEVENT");
		for (i = 0; i < N_ELEMENTS(imap_notify_event_names); i++) {
			if ((ctx->global_used_events & (1 << i)) != 0 &&
			    ((1 << i) & UNSUPPORTED_EVENTS) != 0) {
				str_append_c(client_error, ' ');
				str_append(client_error, imap_notify_event_names[i]);
			}
		}
		str_append(client_error, "] Unsupported NOTIFY events.");
		client_send_tagline(cmd, str_c(client_error));
		pool_unref(&pool);
		return TRUE;
	}

	if (array_count(&ctx->namespaces) == 0) {
		/* selected mailbox only */
	} else if (ctx->global_max_mailbox_names > IMAP_NOTIFY_MAX_NAMES_PER_NS) {
		client_send_tagline(cmd,
			"NO [NOTIFICATIONOVERFLOW] Too many mailbox names");
		pool_unref(&pool);
		return TRUE;
	} else if (imap_notify_refresh_subscriptions(cmd, ctx) < 0) {
		/* tagline already sent */
		pool_unref(&pool);
		return TRUE;
	} else if (imap_notify_begin(ctx) < 0) {
		client_send_tagline(cmd,
			"NO [NOTIFICATIONOVERFLOW] NOTIFY not supported for these mailboxes.");
		pool_unref(&pool);
		return TRUE;
	}
	if (cmd->client->notify_ctx != NULL)
		imap_notify_deinit(&cmd->client->notify_ctx);

	if (ctx->send_immediate_status)
		cmd_notify_send_status(cmd, ctx);
	cmd->client->notify_immediate_expunges =
		ctx->selected_immediate_expunges;
	cmd->client->notify_count_changes =
		(ctx->selected_events & IMAP_NOTIFY_EVENT_MESSAGE_NEW) != 0;
	cmd->client->notify_flag_changes =
		(ctx->selected_events & IMAP_NOTIFY_EVENT_FLAG_CHANGE) != 0;

	cmd->client->notify_ctx = ctx;
	return cmd_sync(cmd, 0, IMAP_SYNC_FLAG_SAFE, "OK NOTIFY completed.");
}