summaryrefslogtreecommitdiffstats
path: root/pigeonhole/src/lib-sieve/sieve-storage-sync.c
blob: b1abfccc5ed4c4b2d1d6b0d5d8c76fcd4d092f1f (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
/* Copyright (c) 2002-2018 Pigeonhole authors, see the included COPYING file
 */

#include "lib.h"
#include "array.h"
#include "str-sanitize.h"
#include "home-expand.h"
#include "eacces-error.h"
#include "mkdir-parents.h"
#include "ioloop.h"
#include "mail-storage-private.h"

#include "sieve-common.h"
#include "sieve-settings.h"
#include "sieve-error-private.h"

#include "sieve-script-private.h"
#include "sieve-storage-private.h"

/*
 * Synchronization
 */

int sieve_storage_sync_init
(struct sieve_storage *storage, struct mail_user *user)
{
	enum sieve_storage_flags sflags = storage->flags;

	if ( (sflags & SIEVE_STORAGE_FLAG_SYNCHRONIZING) == 0 &&
		(sflags & SIEVE_STORAGE_FLAG_READWRITE) == 0 )
		return 0;

	if ( !storage->allows_synchronization ) {
		if ( (sflags & SIEVE_STORAGE_FLAG_SYNCHRONIZING) != 0 )
			return -1;
		return 0;
	}

	e_debug(storage->event, "sync: Synchronization active");

	storage->sync_inbox_ns = mail_namespace_find_inbox(user->namespaces);
	return 0;
}

void sieve_storage_sync_deinit
(struct sieve_storage *storage ATTR_UNUSED)
{
	/* nothing */
}

/*
 * Sync attributes
 */

static int sieve_storage_sync_transaction_begin
(struct sieve_storage *storage, struct mailbox_transaction_context **trans_r)
{
	enum mailbox_flags mflags = MAILBOX_FLAG_IGNORE_ACLS;
	struct mail_namespace *ns = storage->sync_inbox_ns;
	struct mailbox *inbox;
	enum mail_error error;

	if (ns == NULL)
		return 0;

	inbox = mailbox_alloc(ns->list, "INBOX", mflags);
	if (mailbox_open(inbox) < 0) {
		e_warning(storage->event, "sync: "
			  "Failed to open user INBOX for attribute modifications: %s",
			  mailbox_get_last_internal_error(inbox, &error));
		mailbox_free(&inbox);
		return -1;
	}

	*trans_r = mailbox_transaction_begin(inbox,
					     MAILBOX_TRANSACTION_FLAG_EXTERNAL,
					     __func__);
	return 1;
}

static int sieve_storage_sync_transaction_finish
(struct sieve_storage *storage, struct mailbox_transaction_context **trans)
{
	struct mailbox *inbox;
	int ret;

	inbox = mailbox_transaction_get_mailbox(*trans);

	if ((ret=mailbox_transaction_commit(trans)) < 0) {
		enum mail_error error;
		
		e_warning(storage->event, "sync: "
			  "Failed to update INBOX attributes: %s",
			   mail_storage_get_last_error(
				mailbox_get_storage(inbox), &error));
	}

	mailbox_free(&inbox);
	return ret;
}

int sieve_storage_sync_script_save
(struct sieve_storage *storage, const char *name)
{
	struct mailbox_transaction_context *trans;
	const char *key;
	int ret;

	if ((ret=sieve_storage_sync_transaction_begin
		(storage, &trans)) <= 0)
		return ret;

	key = t_strconcat
		(MAILBOX_ATTRIBUTE_PREFIX_SIEVE_FILES, name, NULL);

	mail_index_attribute_set(trans->itrans, TRUE, key, ioloop_time, 0);

	return sieve_storage_sync_transaction_finish(storage, &trans);
}

int sieve_storage_sync_script_rename
(struct sieve_storage *storage, const char *oldname,
	const char *newname)
{
	struct mailbox_transaction_context *trans;
	const char *oldkey, *newkey;
	int ret;

	if ((ret=sieve_storage_sync_transaction_begin
		(storage, &trans)) <= 0)
		return ret;

	oldkey = t_strconcat
		(MAILBOX_ATTRIBUTE_PREFIX_SIEVE_FILES, oldname, NULL);
	newkey = t_strconcat
		(MAILBOX_ATTRIBUTE_PREFIX_SIEVE_FILES, newname, NULL);

	mail_index_attribute_unset(trans->itrans, TRUE, oldkey, ioloop_time);
	mail_index_attribute_set(trans->itrans, TRUE, newkey, ioloop_time, 0);

	return sieve_storage_sync_transaction_finish(storage, &trans);
}

int sieve_storage_sync_script_delete
(struct sieve_storage *storage, const char *name)
{
	struct mailbox_transaction_context *trans;
	const char *key;
	int ret;

	if ((ret=sieve_storage_sync_transaction_begin
		(storage, &trans)) <= 0)
		return ret;

	key = t_strconcat
		(MAILBOX_ATTRIBUTE_PREFIX_SIEVE_FILES, name, NULL);

	mail_index_attribute_unset(trans->itrans, TRUE, key, ioloop_time);

	return sieve_storage_sync_transaction_finish(storage, &trans);
}

int sieve_storage_sync_script_activate
(struct sieve_storage *storage)
{
	struct mailbox_transaction_context *trans;
	int ret;

	if ((ret=sieve_storage_sync_transaction_begin
		(storage, &trans)) <= 0)
		return ret;

	mail_index_attribute_set(trans->itrans,
		TRUE, MAILBOX_ATTRIBUTE_SIEVE_DEFAULT, ioloop_time, 0);

	return sieve_storage_sync_transaction_finish(storage, &trans);
}

int sieve_storage_sync_deactivate
(struct sieve_storage *storage)
{
	struct mailbox_transaction_context *trans;
	int ret;

	if ((ret=sieve_storage_sync_transaction_begin
		(storage, &trans)) <= 0)
		return ret;
	
	mail_index_attribute_unset(trans->itrans,
		TRUE, MAILBOX_ATTRIBUTE_SIEVE_DEFAULT, ioloop_time);

	return sieve_storage_sync_transaction_finish(storage, &trans);
}