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 */
59 long usernum; /* Owner of the mailbox we're processing */
60 long msgnum; /* Message base ID of the message being processed */
65 * Callback function to send libSieve trace messages to Citadel log facility
66 * Set ctdl_libsieve_debug=1 to see extremely verbose libSieve trace
68 int ctdl_debug(sieve2_context_t *s, void *my)
70 static int ctdl_libsieve_debug = 0;
72 if (ctdl_libsieve_debug) {
73 lprintf(CTDL_DEBUG, "Sieve: level [%d] module [%s] file [%s] function [%s]\n",
74 sieve2_getvalue_int(s, "level"),
75 sieve2_getvalue_string(s, "module"),
76 sieve2_getvalue_string(s, "file"),
77 sieve2_getvalue_string(s, "function"));
78 lprintf(CTDL_DEBUG, " message [%s]\n",
79 sieve2_getvalue_string(s, "message"));
86 * Callback function to log script parsing errors
88 int ctdl_errparse(sieve2_context_t *s, void *my)
90 lprintf(CTDL_WARNING, "Error in script, line %d: %s\n",
91 sieve2_getvalue_int(s, "lineno"),
92 sieve2_getvalue_string(s, "message")
99 * Callback function to log script execution errors
101 int ctdl_errexec(sieve2_context_t *s, void *my)
103 lprintf(CTDL_WARNING, "Error executing script: %s\n",
104 sieve2_getvalue_string(s, "message")
111 * Callback function to redirect a message to a different folder
113 int ctdl_redirect(sieve2_context_t *s, void *my)
115 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
117 lprintf(CTDL_DEBUG, "REDIRECT to <%s> (FIXME complete this)\n",
118 sieve2_getvalue_string(s, "address"));
125 * Callback function to indicate that a message *will* be kept in the inbox
127 int ctdl_keep(sieve2_context_t *s, void *my)
129 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
131 lprintf(CTDL_DEBUG, "Action is KEEP\n");
140 * Callback function to file a message into a different mailbox
142 int ctdl_fileinto(sieve2_context_t *s, void *my)
144 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
145 const char *dest_folder = sieve2_getvalue_string(s, "mailbox");
147 char foldername[256];
148 char original_room_name[ROOMNAMELEN];
150 lprintf(CTDL_DEBUG, "Action is FILEINTO, destination is <%s>\n", dest_folder);
152 /* FILEINTO 'INBOX' is the same thing as KEEP */
153 if ( (!strcasecmp(dest_folder, "INBOX")) || (!strcasecmp(dest_folder, MAILROOM)) ) {
159 /* Remember what room we came from */
160 safestrncpy(original_room_name, CC->room.QRname, sizeof original_room_name);
162 /* First try a mailbox name match (check personal mail folders first) */
163 snprintf(foldername, sizeof foldername, "%010ld.%s", cs->usernum, dest_folder);
164 c = getroom(&CC->room, foldername);
166 /* Then a regular room name match (public and private rooms) */
168 safestrncpy(foldername, dest_folder, sizeof foldername);
169 c = getroom(&CC->room, foldername);
173 lprintf(CTDL_WARNING, "FILEINTO failed: target <%s> does not exist\n", dest_folder);
174 return SIEVE2_ERROR_BADARGS;
177 /* Yes, we actually have to go there */
178 usergoto(NULL, 0, 0, NULL, NULL);
180 c = CtdlSaveMsgPointersInRoom(NULL, &cs->msgnum, 1, 0, NULL);
182 /* Go back to the room we came from */
183 if (strcasecmp(original_room_name, CC->room.QRname)) {
184 usergoto(original_room_name, 0, 0, NULL, NULL);
192 return SIEVE2_ERROR_BADARGS;
198 * Callback function to indicate that a message should be discarded.
200 int ctdl_discard(sieve2_context_t *s, void *my)
202 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
204 lprintf(CTDL_DEBUG, "Action is DISCARD\n");
206 /* Yes, this is really all there is to it. Since we are not setting "keep" to 1,
207 * the message will be discarded because "some other action" was successfully taken.
216 * Callback function to retrieve the sieve script
218 int ctdl_getscript(sieve2_context_t *s, void *my) {
220 lprintf(CTDL_DEBUG, "ctdl_getscript() was called\n");
222 sieve2_setvalue_string(s, "script",
224 "require \"fileinto\"; \n"
225 " if header :contains [\"From\"] [\"coyote\"] { \n"
226 " redirect \"acm@frobnitzm.edu\"; \n"
227 " } elsif header :contains \"Subject\" \"JIHAD\" { \n"
228 " fileinto \"jihad\"; \n"
239 * Callback function to retrieve message headers
241 int ctdl_getheaders(sieve2_context_t *s, void *my) {
243 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
245 lprintf(CTDL_DEBUG, "ctdl_getheaders() was called\n");
247 sieve2_setvalue_string(s, "allheaders", cs->rfc822headers);
254 * Add a room to the list of those rooms which potentially require sieve processing
256 void sieve_queue_room(struct ctdlroom *which_room) {
257 struct RoomProcList *ptr;
259 ptr = (struct RoomProcList *) malloc(sizeof (struct RoomProcList));
260 if (ptr == NULL) return;
262 safestrncpy(ptr->name, which_room->QRname, sizeof ptr->name);
263 begin_critical_section(S_SIEVELIST);
264 ptr->next = sieve_list;
266 end_critical_section(S_SIEVELIST);
272 * Perform sieve processing for one message (called by sieve_do_room() for each message)
274 void sieve_do_msg(long msgnum, void *userdata) {
275 sieve2_context_t *sieve2_context = (sieve2_context_t *) userdata;
276 struct ctdl_sieve my;
279 lprintf(CTDL_DEBUG, "Performing sieve processing on msg <%ld>\n", msgnum);
281 CC->redirect_buffer = malloc(SIZ);
282 CC->redirect_len = 0;
283 CC->redirect_alloc = SIZ;
284 CtdlOutputMsg(msgnum, MT_RFC822, HEADERS_ONLY, 0, 1, NULL);
285 my.rfc822headers = CC->redirect_buffer;
286 CC->redirect_buffer = NULL;
287 CC->redirect_len = 0;
288 CC->redirect_alloc = 0;
290 my.keep = 0; /* Don't keep a copy in the inbox unless a callback tells us to do so */
291 my.actiontaken = 0; /* Keep track of whether any actions were successfully taken */
292 my.usernum = atol(CC->room.QRname); /* Keep track of the owner of the room's namespace */
293 my.msgnum = msgnum; /* Keep track of the message number in our local store */
295 sieve2_setvalue_string(sieve2_context, "allheaders", my.rfc822headers);
297 lprintf(CTDL_DEBUG, "Calling sieve2_execute()\n");
298 res = sieve2_execute(sieve2_context, &my);
299 if (res != SIEVE2_OK) {
300 lprintf(CTDL_CRIT, "sieve2_execute() returned %d: %s\n", res, sieve2_errstr(res));
303 free(my.rfc822headers);
304 my.rfc822headers = NULL;
307 * Delete the message from the inbox unless either we were told not to, or
308 * if no other action was successfully taken.
310 if ( (!my.keep) && (my.actiontaken) ) {
311 lprintf(CTDL_DEBUG, "keep is 0 -- deleting message from inbox\n");
312 CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, "", 0);
315 lprintf(CTDL_DEBUG, "Completed sieve processing on msg <%ld>\n", msgnum);
322 * Perform sieve processing for a single room
323 * FIXME ... actually do this instead of just talking about it
325 void sieve_do_room(char *roomname) {
327 sieve2_context_t *sieve2_context = NULL; /* Context for sieve parser */
328 int res; /* Return code from libsieve calls */
331 * This is our callback registration table for libSieve.
333 sieve2_callback_t ctdl_sieve_callbacks[] = {
335 { SIEVE2_ACTION_REJECT, my_reject }, * not yet implemented but we definitely need them */
336 { SIEVE2_ACTION_NOTIFY, my_notify },
337 { SIEVE2_ACTION_VACATION, my_vacation },
339 { SIEVE2_ERRCALL_PARSE, ctdl_errparse },
340 { SIEVE2_ERRCALL_RUNTIME, ctdl_errexec },
341 { SIEVE2_ACTION_FILEINTO, ctdl_fileinto },
342 { SIEVE2_ACTION_REDIRECT, ctdl_redirect },
343 { SIEVE2_ACTION_DISCARD, ctdl_discard },
344 { SIEVE2_ACTION_KEEP, ctdl_keep },
345 { SIEVE2_SCRIPT_GETSCRIPT, ctdl_getscript },
346 { SIEVE2_DEBUG_TRACE, ctdl_debug },
347 { SIEVE2_MESSAGE_GETALLHEADERS, ctdl_getheaders }, /* libSieve can parse the headers itself */
349 { SIEVE2_MESSAGE_GETSUBADDRESS, my_getsubaddress }, * hopefully we won't need any of these *
350 { SIEVE2_MESSAGE_GETENVELOPE, my_getenvelope },
351 { SIEVE2_MESSAGE_GETBODY, my_getbody },
352 { SIEVE2_MESSAGE_GETSIZE, my_getsize },
357 /* FIXME check to see if this room has any sieve scripts to run */
359 lprintf(CTDL_DEBUG, "Performing Sieve processing for <%s>\n", roomname);
361 if (getroom(&CC->room, roomname) != 0) {
362 lprintf(CTDL_CRIT, "ERROR: cannot load <%s>\n", roomname);
366 /* Initialize the Sieve parser */
368 res = sieve2_alloc(&sieve2_context);
369 if (res != SIEVE2_OK) {
370 lprintf(CTDL_CRIT, "sieve2_alloc() returned %d: %s\n", res, sieve2_errstr(res));
374 res = sieve2_callbacks(sieve2_context, ctdl_sieve_callbacks);
375 if (res != SIEVE2_OK) {
376 lprintf(CTDL_CRIT, "sieve2_callbacks() returned %d: %s\n", res, sieve2_errstr(res));
380 /* Validate the script */
382 res = sieve2_validate(sieve2_context, NULL);
383 if (res != SIEVE2_OK) {
384 lprintf(CTDL_CRIT, "sieve2_validate() returned %d: %s\n", res, sieve2_errstr(res));
388 /* Do something useful */
389 /* CtdlForEachMessage(MSGS_GT, sc.lastsent, NULL, NULL, NULL, */
390 /* FIXME figure out which messages haven't yet been processed by sieve */
391 CtdlForEachMessage(MSGS_LAST, 1, NULL, NULL, NULL,
393 (void *) sieve2_context
397 res = sieve2_free(&sieve2_context);
398 if (res != SIEVE2_OK) {
399 lprintf(CTDL_CRIT, "sieve2_free() returned %d: %s\n", res, sieve2_errstr(res));
405 * Perform sieve processing for all rooms which require it
407 void perform_sieve_processing(void) {
408 struct RoomProcList *ptr = NULL;
410 if (sieve_list != NULL) {
411 lprintf(CTDL_DEBUG, "Begin Sieve processing\n");
412 while (sieve_list != NULL) {
413 char spoolroomname[ROOMNAMELEN];
414 safestrncpy(spoolroomname, sieve_list->name, sizeof spoolroomname);
415 begin_critical_section(S_SIEVELIST);
417 /* pop this record off the list */
419 sieve_list = sieve_list->next;
422 /* invalidate any duplicate entries to prevent double processing */
423 for (ptr=sieve_list; ptr!=NULL; ptr=ptr->next) {
424 if (!strcasecmp(ptr->name, spoolroomname)) {
429 end_critical_section(S_SIEVELIST);
430 if (spoolroomname[0] != 0) {
431 sieve_do_room(spoolroomname);
440 * We don't really care about dumping the entire credits to the log
441 * every time the server is initialized. The documentation will suffice
442 * for that purpose. We are making a call to sieve2_credits() in order
443 * to demonstrate that we have successfully linked in to libsieve.
445 void log_the_sieve2_credits(void) {
448 cred = strdup(sieve2_credits());
449 if (cred == NULL) return;
451 if (strlen(cred) > 60) {
452 strcpy(&cred[55], "...");
455 lprintf(CTDL_INFO, "%s\n",cred);
460 char *serv_sieve_init(void)
462 log_the_sieve2_credits();
463 return "$Id: serv_sieve.c 3850 2005-09-13 14:00:24Z ajc $";
466 #else /* HAVE_LIBSIEVE */
468 char *serv_sieve_init(void)
470 lprintf(CTDL_INFO, "This server is missing libsieve. Mailbox filtering will be disabled.\n");
471 return "$Id: serv_sieve.c 3850 2005-09-13 14:00:24Z ajc $";
474 #endif /* HAVE_LIBSIEVE */