summaryrefslogtreecommitdiffstats
path: root/src/lib-storage/list/mailbox-list-notify-tree.c
blob: 6152dbb98b82704e59cf9456de43b43aef9ea8ae (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
/* Copyright (c) 2013-2018 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "str.h"
#include "mail-index.h"
#include "mail-storage.h"
#include "mailbox-list-private.h"
#include "mailbox-list-index.h"
#include "mailbox-list-notify-tree.h"

struct mailbox_list_notify_tree {
	struct mailbox_list *list;
	struct mailbox_tree_context *mailboxes;

	struct mail_index_view *view;
	bool failed;
};

static void
mailbox_list_notify_node_get_status(struct mailbox_list_notify_tree *tree,
				    struct mailbox_notify_node *nnode)
{
	struct mailbox_status status;
	const char *reason;
	uint32_t seq;

	if (!mail_index_lookup_seq(tree->view, nnode->index_uid, &seq))
		return;

	i_zero(&status);
	(void)mailbox_list_index_status(tree->list, tree->view, seq,
		STATUS_UIDVALIDITY | STATUS_UIDNEXT | STATUS_MESSAGES |
		STATUS_UNSEEN | STATUS_HIGHESTMODSEQ, &status, nnode->guid,
		NULL, &reason);
	nnode->uidvalidity = status.uidvalidity;
	nnode->uidnext = status.uidnext;
	nnode->messages = status.messages;
	nnode->unseen = status.unseen;
	nnode->highest_modseq = status.highest_modseq;
}

static void
mailbox_list_notify_node_build(struct mailbox_list_notify_tree *tree,
			       struct mailbox_list_index_node *index_node,
			       string_t *path)
{
	struct mailbox_node *node;
	struct mailbox_notify_node *nnode;
	size_t prefix_len;
	bool created;

	str_append(path, index_node->raw_name);

	node = mailbox_tree_get(tree->mailboxes, str_c(path), &created);
	nnode = (struct mailbox_notify_node *)node;
	nnode->index_uid = index_node->uid;

	if ((index_node->flags & MAILBOX_LIST_INDEX_FLAG_NONEXISTENT) != 0)
		node->flags = MAILBOX_NONEXISTENT;
	else if ((index_node->flags & MAILBOX_LIST_INDEX_FLAG_NOSELECT) != 0)
		node->flags = MAILBOX_NOSELECT;
	else {
		node->flags = 0;
		mailbox_list_notify_node_get_status(tree, nnode);
	}
	if ((index_node->flags & MAILBOX_LIST_INDEX_FLAG_NOINFERIORS) != 0)
		node->flags |= MAILBOX_NOINFERIORS;

	if (index_node->children != NULL) {
		str_append_c(path, mailbox_list_get_hierarchy_sep(tree->list));
		prefix_len = str_len(path);
		index_node = index_node->children;
		for (; index_node != NULL; index_node = index_node->next) {
			str_truncate(path, prefix_len);
			mailbox_list_notify_node_build(tree, index_node, path);
		}
	}
}

static void
mailbox_list_notify_tree_build(struct mailbox_list_notify_tree *tree)
{
	struct mailbox_list_index *ilist = INDEX_LIST_CONTEXT_REQUIRE(tree->list);
	struct mailbox_list_index_node *index_node;
	string_t *path = t_str_new(128);

	if (mailbox_list_index_refresh(tree->list) < 0)
		tree->failed = TRUE;

	tree->view = mail_index_view_open(ilist->index);
	index_node = ilist->mailbox_tree;
	for (; index_node != NULL; index_node = index_node->next) {
		str_truncate(path, 0);
		mailbox_list_notify_node_build(tree, index_node, path);
	}
	mail_index_view_close(&tree->view);
}

struct mailbox_list_notify_tree *
mailbox_list_notify_tree_init(struct mailbox_list *list)
{
	struct mailbox_list_notify_tree *tree;

	tree = i_new(struct mailbox_list_notify_tree, 1);
	tree->list = list;
	tree->mailboxes =
		mailbox_tree_init_size(mailbox_list_get_hierarchy_sep(list),
				       sizeof(struct mailbox_notify_node));
	mailbox_list_notify_tree_build(tree);
	return tree;
}

void mailbox_list_notify_tree_deinit(struct mailbox_list_notify_tree **_tree)
{
	struct mailbox_list_notify_tree *tree = *_tree;

	*_tree = NULL;

	mailbox_tree_deinit(&tree->mailboxes);
	i_free(tree);
}

struct mailbox_notify_node *
mailbox_list_notify_tree_lookup(struct mailbox_list_notify_tree *tree,
				const char *storage_name)
{
	struct mailbox_node *node;

	node = mailbox_tree_lookup(tree->mailboxes, storage_name);
	return (struct mailbox_notify_node *)node;
}