1 /*
2 Copyright (c) 2004-2012 NFG Net Facilities Group BV support@nfg.nl
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later
8 version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 /**
21 * \file dm_sievescript.c
22 *
23 */
24
25 #include "dbmail.h"
26 #define THIS_MODULE "sievescript"
27 #define DBPFX db_params.pfx
28
29 extern DBParam_T db_params;
30
dm_sievescript_getbyname(uint64_t user_idnr,char * scriptname,char ** script)31 int dm_sievescript_getbyname(uint64_t user_idnr, char *scriptname, char **script)
32 {
33 Connection_T c; ResultSet_T r; PreparedStatement_T s; volatile int t = FALSE;
34 assert(scriptname);
35
36 c = db_con_get();
37 TRY
38 s = db_stmt_prepare(c, "SELECT script FROM %ssievescripts WHERE owner_idnr = ? AND name = ?", DBPFX);
39 db_stmt_set_u64(s, 1, user_idnr);
40 db_stmt_set_str(s, 2, scriptname);
41
42 r = db_stmt_query(s);
43 if (db_result_next(r))
44 *script = g_strdup(db_result_get(r,0));
45 CATCH(SQLException)
46 LOG_SQLERROR;
47 t = DM_EQUERY;
48 FINALLY
49 db_con_close(c);
50 END_TRY;
51
52 return t;
53 }
54
dm_sievescript_isactive(uint64_t user_idnr)55 int dm_sievescript_isactive(uint64_t user_idnr)
56 {
57 return dm_sievescript_isactive_byname(user_idnr, NULL);
58 }
59
dm_sievescript_isactive_byname(uint64_t user_idnr,const char * scriptname)60 int dm_sievescript_isactive_byname(uint64_t user_idnr, const char *scriptname)
61 {
62 Connection_T c; ResultSet_T r; PreparedStatement_T s; volatile int t = TRUE;
63
64 c = db_con_get();
65 TRY
66 if (scriptname) {
67 s = db_stmt_prepare(c, "SELECT name FROM %ssievescripts WHERE owner_idnr = ? AND active = 1 AND name = ?", DBPFX);
68 db_stmt_set_u64(s, 1, user_idnr);
69 db_stmt_set_str(s, 2, scriptname);
70 } else {
71 s = db_stmt_prepare(c, "SELECT name FROM %ssievescripts WHERE owner_idnr = ? AND active = 1", DBPFX);
72 db_stmt_set_u64(s, 1, user_idnr);
73 }
74
75 r = db_stmt_query(s);
76 if (! db_result_next(r)) t = FALSE;
77
78 CATCH(SQLException)
79 LOG_SQLERROR;
80 t = DM_EQUERY;
81 FINALLY
82 db_con_close(c);
83 END_TRY;
84
85 return t;
86 }
87
dm_sievescript_get(uint64_t user_idnr,char ** scriptname)88 int dm_sievescript_get(uint64_t user_idnr, char **scriptname)
89 {
90 Connection_T c; ResultSet_T r; volatile int t = FALSE;
91 assert(scriptname);
92 *scriptname = NULL;
93
94 c = db_con_get();
95 TRY
96 r = db_query(c, "SELECT name from %ssievescripts where owner_idnr = %" PRIu64 " and active = 1", DBPFX, user_idnr);
97 if (db_result_next(r))
98 *scriptname = g_strdup(db_result_get(r,0));
99
100 CATCH(SQLException)
101 LOG_SQLERROR;
102 t = DM_EQUERY;
103 FINALLY
104 db_con_close(c);
105 END_TRY;
106
107 return t;
108 }
109
dm_sievescript_list(uint64_t user_idnr,GList ** scriptlist)110 int dm_sievescript_list(uint64_t user_idnr, GList **scriptlist)
111 {
112 Connection_T c; ResultSet_T r; volatile int t = FALSE;
113
114 c = db_con_get();
115 TRY
116 r = db_query(c,"SELECT name,active FROM %ssievescripts WHERE owner_idnr = %" PRIu64 "", DBPFX,user_idnr);
117 while (db_result_next(r)) {
118 sievescript_info *info = g_new0(sievescript_info,1);
119 strncpy(info->name, db_result_get(r,0), sizeof(info->name)-1);
120 info->active = db_result_get_int(r,1);
121 *(GList **)scriptlist = g_list_prepend(*(GList **)scriptlist, info);
122 }
123 CATCH(SQLException)
124 LOG_SQLERROR;
125 t = DM_EQUERY;
126 FINALLY
127 db_con_close(c);
128 END_TRY;
129
130 return t;
131 }
132
dm_sievescript_rename(uint64_t user_idnr,char * scriptname,char * newname)133 int dm_sievescript_rename(uint64_t user_idnr, char *scriptname, char *newname)
134 {
135 volatile int active = 0;
136 Connection_T c; ResultSet_T r; PreparedStatement_T s; volatile int t = FALSE;
137 assert(scriptname);
138
139 /*
140 * According to the draft RFC, a script with the same
141 * name as an existing script should *atomically* replace it.
142 */
143 c = db_con_get();
144 TRY
145 db_begin_transaction(c);
146
147 s = db_stmt_prepare(c,"SELECT active FROM %ssievescripts WHERE owner_idnr = ? AND name = ?", DBPFX);
148 db_stmt_set_u64(s,1, user_idnr);
149 db_stmt_set_str(s,2, newname);
150 r = db_stmt_query(s);
151
152 if (db_result_next(r)) {
153 active = db_result_get_int(r,0);
154
155 db_con_clear(c);
156
157 s = db_stmt_prepare(c, "DELETE FROM %ssievescripts WHERE owner_idnr = ? AND name = ?", DBPFX);
158 db_stmt_set_u64(s, 1, user_idnr);
159 db_stmt_set_str(s, 2, newname);
160 db_stmt_exec(s);
161 }
162
163 db_con_clear(c);
164
165 s = db_stmt_prepare(c, "UPDATE %ssievescripts SET name = ?, active = ? WHERE owner_idnr = ? AND name = ?", DBPFX);
166 db_stmt_set_str(s, 1, newname);
167 db_stmt_set_int(s, 2, active);
168 db_stmt_set_u64(s, 3, user_idnr);
169 db_stmt_set_str(s, 4, scriptname);
170 db_stmt_exec(s);
171
172 t = db_commit_transaction(c);
173
174 CATCH(SQLException)
175 LOG_SQLERROR;
176 t = DM_EQUERY;
177 db_rollback_transaction(c);
178 FINALLY
179 db_con_close(c);
180 END_TRY;
181
182 return t;
183 }
184
dm_sievescript_add(uint64_t user_idnr,char * scriptname,char * script)185 int dm_sievescript_add(uint64_t user_idnr, char *scriptname, char *script)
186 {
187 Connection_T c; ResultSet_T r; PreparedStatement_T s; volatile int t = FALSE;
188 assert(scriptname);
189
190 c = db_con_get();
191 TRY
192 db_begin_transaction(c);
193
194 s = db_stmt_prepare(c,"SELECT COUNT(*) FROM %ssievescripts WHERE owner_idnr = ? AND name = ?", DBPFX);
195 db_stmt_set_u64(s, 1, user_idnr);
196 db_stmt_set_str(s, 2, scriptname);
197
198 r = db_stmt_query(s);
199 if (db_result_next(r)) {
200
201 db_con_clear(c);
202
203 s = db_stmt_prepare(c,"DELETE FROM %ssievescripts WHERE owner_idnr = ? AND name = ?", DBPFX);
204 db_stmt_set_u64(s, 1, user_idnr);
205 db_stmt_set_str(s, 2, scriptname);
206
207 db_stmt_exec(s);
208 }
209
210 db_con_clear(c);
211
212 s = db_stmt_prepare(c,"INSERT INTO %ssievescripts (owner_idnr, name, script, active) VALUES (?,?,?,1)", DBPFX);
213 db_stmt_set_u64(s, 1, user_idnr);
214 db_stmt_set_str(s, 2, scriptname);
215 db_stmt_set_blob(s, 3, script, strlen(script));
216 db_stmt_exec(s);
217
218 t = db_commit_transaction(c);
219
220 CATCH(SQLException)
221 LOG_SQLERROR;
222 db_rollback_transaction(c);
223 t = DM_EQUERY;
224 FINALLY
225 db_con_close(c);
226 END_TRY;
227
228 return t;
229 }
230
dm_sievescript_deactivate(uint64_t user_idnr,char * scriptname)231 int dm_sievescript_deactivate(uint64_t user_idnr, char *scriptname)
232 {
233 Connection_T c; PreparedStatement_T s; volatile gboolean t = FALSE;
234 assert(scriptname);
235
236 c = db_con_get();
237 TRY
238 s = db_stmt_prepare(c, "UPDATE %ssievescripts set active = 0 where owner_idnr = ? and name = ?", DBPFX);
239 db_stmt_set_u64(s, 1, user_idnr);
240 db_stmt_set_str(s, 2, scriptname);
241 db_stmt_exec(s);
242 t = TRUE;
243 CATCH(SQLException)
244 LOG_SQLERROR;
245 FINALLY
246 db_con_close(c);
247 END_TRY;
248
249 return t;
250 }
251
dm_sievescript_activate(uint64_t user_idnr,char * scriptname)252 int dm_sievescript_activate(uint64_t user_idnr, char *scriptname)
253 {
254 Connection_T c; PreparedStatement_T s; volatile gboolean t = FALSE;
255 assert(scriptname);
256
257 c = db_con_get();
258 TRY
259 db_begin_transaction(c);
260
261 s = db_stmt_prepare(c,"UPDATE %ssievescripts SET active = 0 WHERE owner_idnr = ? ", DBPFX);
262 db_stmt_set_u64(s, 1, user_idnr);
263 db_stmt_exec(s);
264
265 db_con_clear(c);
266
267 s = db_stmt_prepare(c,"UPDATE %ssievescripts SET active = 1 WHERE owner_idnr = ? AND name = ?", DBPFX);
268 db_stmt_set_u64(s, 1, user_idnr);
269 db_stmt_set_str(s, 2, scriptname);
270 db_stmt_exec(s);
271
272 db_commit_transaction(c);
273 t = TRUE;
274 CATCH(SQLException)
275 LOG_SQLERROR;
276 db_rollback_transaction(c);
277 FINALLY
278 db_con_close(c);
279 END_TRY;
280
281 return t;
282 }
283
dm_sievescript_delete(uint64_t user_idnr,char * scriptname)284 int dm_sievescript_delete(uint64_t user_idnr, char *scriptname)
285 {
286 Connection_T c; PreparedStatement_T s; volatile gboolean t = FALSE;
287 assert(scriptname);
288
289 c = db_con_get();
290 TRY
291 s = db_stmt_prepare(c,"DELETE FROM %ssievescripts WHERE owner_idnr = ? AND name = ?", DBPFX);
292 db_stmt_set_u64(s, 1, user_idnr);
293 db_stmt_set_str(s, 2, scriptname);
294 db_stmt_exec(s);
295 t = TRUE;
296 CATCH(SQLException)
297 LOG_SQLERROR;
298 FINALLY
299 db_con_close(c);
300 END_TRY;
301
302 return t;
303 }
304
dm_sievescript_quota_check(uint64_t user_idnr,uint64_t scriptlen)305 int dm_sievescript_quota_check(uint64_t user_idnr, uint64_t scriptlen)
306 {
307 /* TODO function dm_sievescript_quota_check */
308 TRACE(TRACE_DEBUG, "checking %" PRIu64 " sievescript quota with %" PRIu64 "" , user_idnr, scriptlen);
309 return DM_SUCCESS;
310 }
311
dm_sievescript_quota_set(uint64_t user_idnr,uint64_t quotasize)312 int dm_sievescript_quota_set(uint64_t user_idnr, uint64_t quotasize)
313 {
314 /* TODO function dm_sievescript_quota_set */
315 TRACE(TRACE_DEBUG, "setting %" PRIu64 " sievescript quota with %" PRIu64 "" , user_idnr, quotasize);
316 return DM_SUCCESS;
317 }
318
dm_sievescript_quota_get(uint64_t user_idnr,uint64_t * quotasize)319 int dm_sievescript_quota_get(uint64_t user_idnr, uint64_t * quotasize)
320 {
321 /* TODO function dm_sievescript_quota_get */
322 TRACE(TRACE_DEBUG, "getting sievescript quota for %" PRIu64 "" , user_idnr);
323 *quotasize = 0;
324 return DM_SUCCESS;
325 }
326
327
328