2 * $Id: serv_sieve.c 3850 2005-09-13 14:00:24Z ajc $
4 * This module glues libSieve to the Citadel server in order to implement
5 * the Sieve mailbox filtering language (RFC 3028).
7 * This code is released under the terms of the GNU General Public License.
18 #include <sys/types.h>
20 #if TIME_WITH_SYS_TIME
21 # include <sys/time.h>
25 # include <sys/time.h>
36 #include "sysdep_decls.h"
37 #include "citserver.h"
40 #include "serv_extensions.h"
50 #include <sieve2_error.h>
51 #include "serv_sieve.h"
53 struct RoomProcList *sieve_list = NULL;
57 int actiontaken; /* Set to 1 if the message was successfully acted upon */
58 int keep; /* Set to 1 to suppress message deletion from the inbox */
63 * Callback function to send libSieve trace messages to Citadel log facility
64 * Set ctdl_libsieve_debug=1 to see extremely verbose libSieve trace
66 int ctdl_debug(sieve2_context_t *s, void *my)
68 static int ctdl_libsieve_debug = 0;
70 if (ctdl_libsieve_debug) {
71 lprintf(CTDL_DEBUG, "Sieve: level [%d] module [%s] file [%s] function [%s]\n",
72 sieve2_getvalue_int(s, "level"),
73 sieve2_getvalue_string(s, "module"),
74 sieve2_getvalue_string(s, "file"),
75 sieve2_getvalue_string(s, "function"));
76 lprintf(CTDL_DEBUG, " message [%s]\n",
77 sieve2_getvalue_string(s, "message"));
84 * Callback function to log script parsing errors
86 int ctdl_errparse(sieve2_context_t *s, void *my)
88 lprintf(CTDL_WARNING, "Error in script, line %d: %s\n",
89 sieve2_getvalue_int(s, "lineno"),
90 sieve2_getvalue_string(s, "message")
97 * Callback function to log script execution errors
99 int ctdl_errexec(sieve2_context_t *s, void *my)
101 lprintf(CTDL_WARNING, "Error executing script: %s\n",
102 sieve2_getvalue_string(s, "message")
109 * Callback function to redirect a message to a different folder
111 int ctdl_redirect(sieve2_context_t *s, void *my)
113 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
115 lprintf(CTDL_DEBUG, "REDIRECT to <%s> (FIXME complete this)\n",
116 sieve2_getvalue_string(s, "address"));
123 * Callback function to indicate that a message *will* be kept in the inbox
125 int ctdl_keep(sieve2_context_t *s, void *my)
127 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
129 lprintf(CTDL_DEBUG, "Action is KEEP\n");
138 * Callback function to file a message into a different mailbox
140 int ctdl_fileinto(sieve2_context_t *s, void *my)
142 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
143 const char *dest_folder = sieve2_getvalue_string(s, "mailbox");
145 lprintf(CTDL_DEBUG, "Action is FILEINTO, destination is <%s>\n", dest_folder);
148 /* FILEINTO 'INBOX' is the same thing as KEEP */
149 if ( (!strcasecmp(dest_folder, "INBOX")) || (!strcasecmp(dest_folder, MAILROOM)) ) {
155 /* FIXME finish this to file it in another arbitrary room */
156 return SIEVE2_ERROR_BADARGS;
161 * Callback function to indicate that a message should be discarded.
163 int ctdl_discard(sieve2_context_t *s, void *my)
165 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
167 lprintf(CTDL_DEBUG, "Action is DISCARD\n");
169 /* Yes, this is really all there is to it. Since we are not setting "keep" to 1,
170 * the message will be discarded because "some other action" was successfully taken.
179 * Callback function to retrieve the sieve script
181 int ctdl_getscript(sieve2_context_t *s, void *my) {
183 lprintf(CTDL_DEBUG, "ctdl_getscript() was called\n");
185 sieve2_setvalue_string(s, "script",
187 "require \"fileinto\"; \n"
188 " if header :contains [\"From\"] [\"coyote\"] { \n"
189 " redirect \"acm@frobnitzm.edu\"; \n"
190 " } elsif header :contains \"Subject\" \"JIHAD\" { \n"
191 " fileinto \"jihad\"; \n"
202 * Callback function to retrieve message headers
204 int ctdl_getheaders(sieve2_context_t *s, void *my) {
206 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
208 lprintf(CTDL_DEBUG, "ctdl_getheaders() was called\n");
210 sieve2_setvalue_string(s, "allheaders", cs->rfc822headers);
217 * Add a room to the list of those rooms which potentially require sieve processing
219 void sieve_queue_room(struct ctdlroom *which_room) {
220 struct RoomProcList *ptr;
222 ptr = (struct RoomProcList *) malloc(sizeof (struct RoomProcList));
223 if (ptr == NULL) return;
225 safestrncpy(ptr->name, which_room->QRname, sizeof ptr->name);
226 begin_critical_section(S_SIEVELIST);
227 ptr->next = sieve_list;
229 end_critical_section(S_SIEVELIST);
235 * Perform sieve processing for one message (called by sieve_do_room() for each message)
237 void sieve_do_msg(long msgnum, void *userdata) {
238 sieve2_context_t *sieve2_context = (sieve2_context_t *) userdata;
239 struct ctdl_sieve my;
242 lprintf(CTDL_DEBUG, "Performing sieve processing on msg <%ld>\n", msgnum);
244 CC->redirect_buffer = malloc(SIZ);
245 CC->redirect_len = 0;
246 CC->redirect_alloc = SIZ;
247 CtdlOutputMsg(msgnum, MT_RFC822, HEADERS_ONLY, 0, 1, NULL);
248 my.rfc822headers = CC->redirect_buffer;
249 CC->redirect_buffer = NULL;
250 CC->redirect_len = 0;
251 CC->redirect_alloc = 0;
253 my.keep = 0; /* Don't keep a copy in the inbox unless a callback tells us to do so */
254 my.actiontaken = 0; /* Keep track of whether any actions were successfully taken */
256 sieve2_setvalue_string(sieve2_context, "allheaders", my.rfc822headers);
258 lprintf(CTDL_DEBUG, "Calling sieve2_execute()\n");
259 res = sieve2_execute(sieve2_context, &my);
260 if (res != SIEVE2_OK) {
261 lprintf(CTDL_CRIT, "sieve2_execute() returned %d: %s\n", res, sieve2_errstr(res));
264 free(my.rfc822headers);
265 my.rfc822headers = NULL;
268 * Delete the message from the inbox unless either we were told not to, or
269 * if no other action was successfully taken.
271 if ( (!my.keep) && (my.actiontaken) ) {
272 lprintf(CTDL_DEBUG, "keep is 0 -- deleting message from inbox\n");
273 CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, "", 0);
276 lprintf(CTDL_DEBUG, "Completed sieve processing on msg <%ld>\n", msgnum);
283 * Perform sieve processing for a single room
284 * FIXME ... actually do this instead of just talking about it
286 void sieve_do_room(char *roomname) {
288 sieve2_context_t *sieve2_context = NULL; /* Context for sieve parser */
289 int res; /* Return code from libsieve calls */
292 * CALLBACK REGISTRATION TABLE
294 sieve2_callback_t ctdl_sieve_callbacks[] = {
295 { SIEVE2_DEBUG_TRACE, ctdl_debug },
297 { SIEVE2_ACTION_REJECT, my_reject },
298 { SIEVE2_ACTION_NOTIFY, my_notify },
299 { SIEVE2_ACTION_VACATION, my_vacation },
301 { SIEVE2_ERRCALL_PARSE, ctdl_errparse },
302 { SIEVE2_ERRCALL_RUNTIME, ctdl_errexec },
303 { SIEVE2_ACTION_FILEINTO, ctdl_fileinto },
304 { SIEVE2_ACTION_REDIRECT, ctdl_redirect },
305 { SIEVE2_ACTION_DISCARD, ctdl_discard },
306 { SIEVE2_ACTION_KEEP, ctdl_keep },
307 { SIEVE2_SCRIPT_GETSCRIPT, ctdl_getscript },
308 { SIEVE2_MESSAGE_GETHEADER, NULL }, /* We don't support one header at a time. */
309 { SIEVE2_MESSAGE_GETALLHEADERS, ctdl_getheaders }, /* libSieve can parse headers itself, so we'll use that. */
311 { SIEVE2_MESSAGE_GETSUBADDRESS, my_getsubaddress },
312 { SIEVE2_MESSAGE_GETENVELOPE, my_getenvelope },
313 { SIEVE2_MESSAGE_GETBODY, my_getbody },
314 { SIEVE2_MESSAGE_GETSIZE, my_getsize },
319 /* FIXME check to see if this room has any sieve scripts to run */
321 lprintf(CTDL_DEBUG, "Performing Sieve processing for <%s>\n", roomname);
323 if (getroom(&CC->room, roomname) != 0) {
324 lprintf(CTDL_CRIT, "ERROR: cannot load <%s>\n", roomname);
328 /* Initialize the Sieve parser */
330 res = sieve2_alloc(&sieve2_context);
331 if (res != SIEVE2_OK) {
332 lprintf(CTDL_CRIT, "sieve2_alloc() returned %d: %s\n", res, sieve2_errstr(res));
336 res = sieve2_callbacks(sieve2_context, ctdl_sieve_callbacks);
337 if (res != SIEVE2_OK) {
338 lprintf(CTDL_CRIT, "sieve2_callbacks() returned %d: %s\n", res, sieve2_errstr(res));
342 /* Validate the script */
344 res = sieve2_validate(sieve2_context, NULL);
345 if (res != SIEVE2_OK) {
346 lprintf(CTDL_CRIT, "sieve2_validate() returned %d: %s\n", res, sieve2_errstr(res));
350 /* Do something useful */
351 /* CtdlForEachMessage(MSGS_GT, sc.lastsent, NULL, NULL, NULL, */
352 /* FIXME figure out which messages haven't yet been processed by sieve */
353 CtdlForEachMessage(MSGS_LAST, 1, NULL, NULL, NULL,
355 (void *) sieve2_context
359 res = sieve2_free(&sieve2_context);
360 if (res != SIEVE2_OK) {
361 lprintf(CTDL_CRIT, "sieve2_free() returned %d: %s\n", res, sieve2_errstr(res));
367 * Perform sieve processing for all rooms which require it
369 void perform_sieve_processing(void) {
370 struct RoomProcList *ptr = NULL;
372 if (sieve_list != NULL) {
373 lprintf(CTDL_DEBUG, "Begin Sieve processing\n");
374 while (sieve_list != NULL) {
375 char spoolroomname[ROOMNAMELEN];
376 safestrncpy(spoolroomname, sieve_list->name, sizeof spoolroomname);
377 begin_critical_section(S_SIEVELIST);
379 /* pop this record off the list */
381 sieve_list = sieve_list->next;
384 /* invalidate any duplicate entries to prevent double processing */
385 for (ptr=sieve_list; ptr!=NULL; ptr=ptr->next) {
386 if (!strcasecmp(ptr->name, spoolroomname)) {
391 end_critical_section(S_SIEVELIST);
392 if (spoolroomname[0] != 0) {
393 sieve_do_room(spoolroomname);
402 * We don't really care about dumping the entire credits to the log
403 * every time the server is initialized. The documentation will suffice
404 * for that purpose. We are making a call to sieve2_credits() in order
405 * to demonstrate that we have successfully linked in to libsieve.
407 void log_the_sieve2_credits(void) {
410 cred = strdup(sieve2_credits());
411 if (cred == NULL) return;
413 if (strlen(cred) > 60) {
414 strcpy(&cred[55], "...");
417 lprintf(CTDL_INFO, "%s\n",cred);
422 char *serv_sieve_init(void)
424 log_the_sieve2_credits();
425 return "$Id: serv_sieve.c 3850 2005-09-13 14:00:24Z ajc $";
428 #else /* HAVE_LIBSIEVE */
430 char *serv_sieve_init(void)
432 lprintf(CTDL_INFO, "This server is missing libsieve. Mailbox filtering will be disabled.\n");
433 return "$Id: serv_sieve.c 3850 2005-09-13 14:00:24Z ajc $";
436 #endif /* HAVE_LIBSIEVE */