forked from dbmail/dbmail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdm_sievescript.c
327 lines (263 loc) · 8.14 KB
/
dm_sievescript.c
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
/*
Copyright (c) 2004-2012 NFG Net Facilities Group BV support@nfg.nl
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/**
* \file dm_sievescript.c
*
*/
#include "dbmail.h"
#define THIS_MODULE "sievescript"
#define DBPFX db_params.pfx
extern DBParam_T db_params;
int dm_sievescript_getbyname(uint64_t user_idnr, char *scriptname, char **script)
{
Connection_T c; ResultSet_T r; PreparedStatement_T s; volatile int t = FALSE;
assert(scriptname);
c = db_con_get();
TRY
s = db_stmt_prepare(c, "SELECT script FROM %ssievescripts WHERE owner_idnr = ? AND name = ?", DBPFX);
db_stmt_set_u64(s, 1, user_idnr);
db_stmt_set_str(s, 2, scriptname);
r = db_stmt_query(s);
if (db_result_next(r))
*script = g_strdup(db_result_get(r,0));
CATCH(SQLException)
LOG_SQLERROR;
t = DM_EQUERY;
FINALLY
db_con_close(c);
END_TRY;
return t;
}
int dm_sievescript_isactive(uint64_t user_idnr)
{
return dm_sievescript_isactive_byname(user_idnr, NULL);
}
int dm_sievescript_isactive_byname(uint64_t user_idnr, const char *scriptname)
{
Connection_T c; ResultSet_T r; PreparedStatement_T s; volatile int t = TRUE;
c = db_con_get();
TRY
if (scriptname) {
s = db_stmt_prepare(c, "SELECT name FROM %ssievescripts WHERE owner_idnr = ? AND active = 1 AND name = ?", DBPFX);
db_stmt_set_u64(s, 1, user_idnr);
db_stmt_set_str(s, 2, scriptname);
} else {
s = db_stmt_prepare(c, "SELECT name FROM %ssievescripts WHERE owner_idnr = ? AND active = 1", DBPFX);
db_stmt_set_u64(s, 1, user_idnr);
}
r = db_stmt_query(s);
if (! db_result_next(r)) t = FALSE;
CATCH(SQLException)
LOG_SQLERROR;
t = DM_EQUERY;
FINALLY
db_con_close(c);
END_TRY;
return t;
}
int dm_sievescript_get(uint64_t user_idnr, char **scriptname)
{
Connection_T c; ResultSet_T r; volatile int t = FALSE;
assert(scriptname);
*scriptname = NULL;
c = db_con_get();
TRY
r = db_query(c, "SELECT name from %ssievescripts where owner_idnr = %" PRIu64 " and active = 1", DBPFX, user_idnr);
if (db_result_next(r))
*scriptname = g_strdup(db_result_get(r,0));
CATCH(SQLException)
LOG_SQLERROR;
t = DM_EQUERY;
FINALLY
db_con_close(c);
END_TRY;
return t;
}
int dm_sievescript_list(uint64_t user_idnr, GList **scriptlist)
{
Connection_T c; ResultSet_T r; volatile int t = FALSE;
c = db_con_get();
TRY
r = db_query(c,"SELECT name,active FROM %ssievescripts WHERE owner_idnr = %" PRIu64 "", DBPFX,user_idnr);
while (db_result_next(r)) {
sievescript_info *info = g_new0(sievescript_info,1);
strncpy(info->name, db_result_get(r,0), sizeof(info->name));
info->active = db_result_get_int(r,1);
*(GList **)scriptlist = g_list_prepend(*(GList **)scriptlist, info);
}
CATCH(SQLException)
LOG_SQLERROR;
t = DM_EQUERY;
FINALLY
db_con_close(c);
END_TRY;
return t;
}
int dm_sievescript_rename(uint64_t user_idnr, char *scriptname, char *newname)
{
int active = 0;
Connection_T c; ResultSet_T r; PreparedStatement_T s; volatile int t = FALSE;
assert(scriptname);
/*
* According to the draft RFC, a script with the same
* name as an existing script should *atomically* replace it.
*/
c = db_con_get();
TRY
db_begin_transaction(c);
s = db_stmt_prepare(c,"SELECT active FROM %ssievescripts WHERE owner_idnr = ? AND name = ?", DBPFX);
db_stmt_set_u64(s,1, user_idnr);
db_stmt_set_str(s,2, newname);
r = db_stmt_query(s);
if (db_result_next(r)) {
active = db_result_get_int(r,0);
db_con_clear(c);
s = db_stmt_prepare(c, "DELETE FROM %ssievescripts WHERE owner_idnr = ? AND name = ?", DBPFX);
db_stmt_set_u64(s, 1, user_idnr);
db_stmt_set_str(s, 2, newname);
db_stmt_exec(s);
}
db_con_clear(c);
s = db_stmt_prepare(c, "UPDATE %ssievescripts SET name = ?, active = ? WHERE owner_idnr = ? AND name = ?", DBPFX);
db_stmt_set_str(s, 1, newname);
db_stmt_set_int(s, 2, active);
db_stmt_set_u64(s, 3, user_idnr);
db_stmt_set_str(s, 4, scriptname);
db_stmt_exec(s);
t = db_commit_transaction(c);
CATCH(SQLException)
LOG_SQLERROR;
t = DM_EQUERY;
db_rollback_transaction(c);
FINALLY
db_con_close(c);
END_TRY;
return t;
}
int dm_sievescript_add(uint64_t user_idnr, char *scriptname, char *script)
{
Connection_T c; ResultSet_T r; PreparedStatement_T s; volatile int t = FALSE;
assert(scriptname);
c = db_con_get();
TRY
db_begin_transaction(c);
s = db_stmt_prepare(c,"SELECT COUNT(*) FROM %ssievescripts WHERE owner_idnr = ? AND name = ?", DBPFX);
db_stmt_set_u64(s, 1, user_idnr);
db_stmt_set_str(s, 2, scriptname);
r = db_stmt_query(s);
if (db_result_next(r)) {
db_con_clear(c);
s = db_stmt_prepare(c,"DELETE FROM %ssievescripts WHERE owner_idnr = ? AND name = ?", DBPFX);
db_stmt_set_u64(s, 1, user_idnr);
db_stmt_set_str(s, 2, scriptname);
db_stmt_exec(s);
}
db_con_clear(c);
s = db_stmt_prepare(c,"INSERT INTO %ssievescripts (owner_idnr, name, script, active) VALUES (?,?,?,1)", DBPFX);
db_stmt_set_u64(s, 1, user_idnr);
db_stmt_set_str(s, 2, scriptname);
db_stmt_set_blob(s, 3, script, strlen(script));
db_stmt_exec(s);
t = db_commit_transaction(c);
CATCH(SQLException)
LOG_SQLERROR;
db_rollback_transaction(c);
t = DM_EQUERY;
FINALLY
db_con_close(c);
END_TRY;
return t;
}
int dm_sievescript_deactivate(uint64_t user_idnr, char *scriptname)
{
Connection_T c; PreparedStatement_T s; volatile gboolean t = FALSE;
assert(scriptname);
c = db_con_get();
TRY
s = db_stmt_prepare(c, "UPDATE %ssievescripts set active = 0 where owner_idnr = ? and name = ?", DBPFX);
db_stmt_set_u64(s, 1, user_idnr);
db_stmt_set_str(s, 2, scriptname);
db_stmt_exec(s);
t = TRUE;
CATCH(SQLException)
LOG_SQLERROR;
FINALLY
db_con_close(c);
END_TRY;
return t;
}
int dm_sievescript_activate(uint64_t user_idnr, char *scriptname)
{
Connection_T c; PreparedStatement_T s; volatile gboolean t = FALSE;
assert(scriptname);
c = db_con_get();
TRY
db_begin_transaction(c);
s = db_stmt_prepare(c,"UPDATE %ssievescripts SET active = 0 WHERE owner_idnr = ? ", DBPFX);
db_stmt_set_u64(s, 1, user_idnr);
db_stmt_exec(s);
db_con_clear(c);
s = db_stmt_prepare(c,"UPDATE %ssievescripts SET active = 1 WHERE owner_idnr = ? AND name = ?", DBPFX);
db_stmt_set_u64(s, 1, user_idnr);
db_stmt_set_str(s, 2, scriptname);
db_stmt_exec(s);
db_commit_transaction(c);
t = TRUE;
CATCH(SQLException)
LOG_SQLERROR;
db_rollback_transaction(c);
FINALLY
db_con_close(c);
END_TRY;
return t;
}
int dm_sievescript_delete(uint64_t user_idnr, char *scriptname)
{
Connection_T c; PreparedStatement_T s; volatile gboolean t = FALSE;
assert(scriptname);
c = db_con_get();
TRY
s = db_stmt_prepare(c,"DELETE FROM %ssievescripts WHERE owner_idnr = ? AND name = ?", DBPFX);
db_stmt_set_u64(s, 1, user_idnr);
db_stmt_set_str(s, 2, scriptname);
db_stmt_exec(s);
t = TRUE;
CATCH(SQLException)
LOG_SQLERROR;
FINALLY
db_con_close(c);
END_TRY;
return t;
}
int dm_sievescript_quota_check(uint64_t user_idnr, uint64_t scriptlen)
{
/* TODO function dm_sievescript_quota_check */
TRACE(TRACE_DEBUG, "checking %" PRIu64 " sievescript quota with %" PRIu64 "" , user_idnr, scriptlen);
return DM_SUCCESS;
}
int dm_sievescript_quota_set(uint64_t user_idnr, uint64_t quotasize)
{
/* TODO function dm_sievescript_quota_set */
TRACE(TRACE_DEBUG, "setting %" PRIu64 " sievescript quota with %" PRIu64 "" , user_idnr, quotasize);
return DM_SUCCESS;
}
int dm_sievescript_quota_get(uint64_t user_idnr, uint64_t * quotasize)
{
/* TODO function dm_sievescript_quota_get */
TRACE(TRACE_DEBUG, "getting sievescript quota for %" PRIu64 "" , user_idnr);
*quotasize = 0;
return DM_SUCCESS;
}