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"
45 #include "internet_addressing.h"
50 #include "serv_sieve.h"
52 struct RoomProcList *sieve_list = NULL;
53 char *msiv_extensions = NULL;
57 * Callback function to send libSieve trace messages to Citadel log facility
58 * Set ctdl_libsieve_debug=1 to see extremely verbose libSieve trace
60 int ctdl_debug(sieve2_context_t *s, void *my)
62 static int ctdl_libsieve_debug = 0;
64 if (ctdl_libsieve_debug) {
66 lprintf(CTDL_DEBUG, "Sieve: level [%d] module [%s] file [%s] function [%s]\n",
67 sieve2_getvalue_int(s, "level"),
68 sieve2_getvalue_string(s, "module"),
69 sieve2_getvalue_string(s, "file"),
70 sieve2_getvalue_string(s, "function"));
72 lprintf(CTDL_DEBUG, "Sieve: %s\n",
73 sieve2_getvalue_string(s, "message"));
80 * Callback function to log script parsing errors
82 int ctdl_errparse(sieve2_context_t *s, void *my)
84 lprintf(CTDL_WARNING, "Error in script, line %d: %s\n",
85 sieve2_getvalue_int(s, "lineno"),
86 sieve2_getvalue_string(s, "message")
93 * Callback function to log script execution errors
95 int ctdl_errexec(sieve2_context_t *s, void *my)
97 lprintf(CTDL_WARNING, "Error executing script: %s\n",
98 sieve2_getvalue_string(s, "message")
105 * Callback function to redirect a message to a different folder
107 int ctdl_redirect(sieve2_context_t *s, void *my)
109 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
110 struct CtdlMessage *msg = NULL;
111 struct recptypes *valid = NULL;
114 safestrncpy(recp, sieve2_getvalue_string(s, "address"), sizeof recp);
116 lprintf(CTDL_DEBUG, "Action is REDIRECT, recipient <%s>\n", recp);
118 valid = validate_recipients(recp);
120 lprintf(CTDL_WARNING, "REDIRECT failed: bad recipient <%s>\n", recp);
121 return SIEVE2_ERROR_BADARGS;
123 if (valid->num_error > 0) {
124 lprintf(CTDL_WARNING, "REDIRECT failed: bad recipient <%s>\n", recp);
126 return SIEVE2_ERROR_BADARGS;
129 msg = CtdlFetchMessage(cs->msgnum, 1);
131 lprintf(CTDL_WARNING, "REDIRECT failed: unable to fetch msg %ld\n", cs->msgnum);
133 return SIEVE2_ERROR_BADARGS;
136 CtdlSubmitMsg(msg, valid, NULL);
137 cs->cancel_implicit_keep = 1;
139 CtdlFreeMessage(msg);
145 * Callback function to indicate that a message *will* be kept in the inbox
147 int ctdl_keep(sieve2_context_t *s, void *my)
149 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
151 lprintf(CTDL_DEBUG, "Action is KEEP\n");
154 cs->cancel_implicit_keep = 1;
160 * Callback function to file a message into a different mailbox
162 int ctdl_fileinto(sieve2_context_t *s, void *my)
164 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
165 const char *dest_folder = sieve2_getvalue_string(s, "mailbox");
167 char foldername[256];
168 char original_room_name[ROOMNAMELEN];
170 lprintf(CTDL_DEBUG, "Action is FILEINTO, destination is <%s>\n", dest_folder);
172 /* FILEINTO 'INBOX' is the same thing as KEEP */
173 if ( (!strcasecmp(dest_folder, "INBOX")) || (!strcasecmp(dest_folder, MAILROOM)) ) {
175 cs->cancel_implicit_keep = 1;
179 /* Remember what room we came from */
180 safestrncpy(original_room_name, CC->room.QRname, sizeof original_room_name);
182 /* First try a mailbox name match (check personal mail folders first) */
183 snprintf(foldername, sizeof foldername, "%010ld.%s", cs->usernum, dest_folder);
184 c = getroom(&CC->room, foldername);
186 /* Then a regular room name match (public and private rooms) */
188 safestrncpy(foldername, dest_folder, sizeof foldername);
189 c = getroom(&CC->room, foldername);
193 lprintf(CTDL_WARNING, "FILEINTO failed: target <%s> does not exist\n", dest_folder);
194 return SIEVE2_ERROR_BADARGS;
197 /* Yes, we actually have to go there */
198 usergoto(NULL, 0, 0, NULL, NULL);
200 c = CtdlSaveMsgPointersInRoom(NULL, &cs->msgnum, 1, 0, NULL);
202 /* Go back to the room we came from */
203 if (strcasecmp(original_room_name, CC->room.QRname)) {
204 usergoto(original_room_name, 0, 0, NULL, NULL);
208 cs->cancel_implicit_keep = 1;
212 return SIEVE2_ERROR_BADARGS;
218 * Callback function to indicate that a message should be discarded.
220 int ctdl_discard(sieve2_context_t *s, void *my)
222 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
224 lprintf(CTDL_DEBUG, "Action is DISCARD\n");
226 /* Cancel the implicit keep. That's all there is to it. */
227 cs->cancel_implicit_keep = 1;
234 * Callback function to indicate that a message should be rejected
236 int ctdl_reject(sieve2_context_t *s, void *my)
238 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
239 char *reject_text = NULL;
241 lprintf(CTDL_DEBUG, "Action is REJECT\n");
243 /* If we don't know who sent the message, do a DISCARD instead. */
244 if (strlen(cs->sender) == 0) {
245 lprintf(CTDL_INFO, "Unknown sender. Doing DISCARD instead of REJECT.\n");
246 return ctdl_discard(s, my);
249 /* Assemble the reject message. */
250 reject_text = malloc(strlen(sieve2_getvalue_string(s, "message")) + 1024);
251 if (reject_text == NULL) {
252 return SIEVE2_ERROR_FAIL;
256 "Content-type: text/plain\n"
258 "The message was refused by the recipient's mail filtering program.\n"
259 "The reason given was as follows:\n"
264 sieve2_getvalue_string(s, "message")
267 quickie_message( /* This delivers the message */
268 "Citadel", /* FIXME make it myself */
273 "Delivery status notification"
277 cs->cancel_implicit_keep = 1;
284 * Callback function to indicate that a vacation message should be generated
286 int ctdl_vacation(sieve2_context_t *s, void *my)
288 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
289 struct sdm_vacation *vptr;
292 char *vacamsg_text = NULL;
294 lprintf(CTDL_DEBUG, "Action is VACATION\n");
296 message = sieve2_getvalue_string(s, "message");
297 if (message == NULL) return SIEVE2_ERROR_BADARGS;
299 days = sieve2_getvalue_int(s, "days");
300 if (days < 1) days = 1;
301 if (days > MAX_VACATION) days = MAX_VACATION;
303 /* Check to see whether we've already alerted this sender that we're on vacation. */
304 for (vptr = cs->u->first_vacation; vptr != NULL; vptr = vptr->next) {
305 if (!strcasecmp(vptr->fromaddr, cs->sender)) {
306 if ( (time(NULL) - vptr->timestamp) < (days * 86400) ) {
307 lprintf(CTDL_DEBUG, "Already alerted <%s> recently.\n", cs->sender);
313 /* Assemble the reject message. */
314 vacamsg_text = malloc(strlen(message) + 1024);
315 if (vacamsg_text == NULL) {
316 return SIEVE2_ERROR_FAIL;
319 sprintf(vacamsg_text,
320 "Content-type: text/plain\n"
328 quickie_message( /* This delivers the message */
329 "Citadel", /* FIXME make it myself */
334 "Delivery status notification"
339 /* Now update the list to reflect the fact that we've alerted this sender.
340 * If they're already in the list, just update the timestamp.
342 for (vptr = cs->u->first_vacation; vptr != NULL; vptr = vptr->next) {
343 if (!strcasecmp(vptr->fromaddr, cs->sender)) {
344 vptr->timestamp = time(NULL);
349 /* If we get to this point, create a new record.
351 vptr = malloc(sizeof(struct sdm_vacation));
352 vptr->timestamp = time(NULL);
353 safestrncpy(vptr->fromaddr, cs->sender, sizeof vptr->fromaddr);
354 vptr->next = cs->u->first_vacation;
355 cs->u->first_vacation = vptr;
362 * Callback function to parse addresses per local system convention
363 * It is disabled because we don't support subaddresses.
366 int ctdl_getsubaddress(sieve2_context_t *s, void *my)
368 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
370 /* libSieve does not take ownership of the memory used here. But, since we
371 * are just pointing to locations inside a struct which we are going to free
374 sieve2_setvalue_string(s, "user", cs->recp_user);
375 sieve2_setvalue_string(s, "detail", "");
376 sieve2_setvalue_string(s, "localpart", cs->recp_user);
377 sieve2_setvalue_string(s, "domain", cs->recp_node);
384 * Callback function to parse message envelope
386 int ctdl_getenvelope(sieve2_context_t *s, void *my)
388 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
390 lprintf(CTDL_DEBUG, "Action is GETENVELOPE\n");
391 sieve2_setvalue_string(s, "to", cs->envelope_to);
392 sieve2_setvalue_string(s, "from", cs->envelope_from);
398 * Callback function to fetch message body
399 * FIXME implement this
402 int ctdl_getbody(sieve2_context_t *s, void *my)
404 return SIEVE2_ERROR_UNSUPPORTED;
410 * Callback function to fetch message size
412 int ctdl_getsize(sieve2_context_t *s, void *my)
414 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
417 GetMetaData(&smi, cs->msgnum);
419 if (smi.meta_rfc822_length > 0L) {
420 sieve2_setvalue_int(s, "size", (int)smi.meta_rfc822_length);
424 return SIEVE2_ERROR_UNSUPPORTED;
429 * Callback function to retrieve the sieve script
431 int ctdl_getscript(sieve2_context_t *s, void *my) {
432 struct sdm_script *sptr;
433 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
435 for (sptr=cs->u->first_script; sptr!=NULL; sptr=sptr->next) {
436 if (sptr->script_active > 0) {
437 lprintf(CTDL_DEBUG, "ctdl_getscript() is using script '%s'\n", sptr->script_name);
438 sieve2_setvalue_string(s, "script", sptr->script_content);
443 lprintf(CTDL_DEBUG, "ctdl_getscript() found no active script\n");
444 return SIEVE2_ERROR_GETSCRIPT;
448 * Callback function to retrieve message headers
450 int ctdl_getheaders(sieve2_context_t *s, void *my) {
452 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
454 lprintf(CTDL_DEBUG, "ctdl_getheaders() was called\n");
456 sieve2_setvalue_string(s, "allheaders", cs->rfc822headers);
463 * Add a room to the list of those rooms which potentially require sieve processing
465 void sieve_queue_room(struct ctdlroom *which_room) {
466 struct RoomProcList *ptr;
468 ptr = (struct RoomProcList *) malloc(sizeof (struct RoomProcList));
469 if (ptr == NULL) return;
471 safestrncpy(ptr->name, which_room->QRname, sizeof ptr->name);
472 begin_critical_section(S_SIEVELIST);
473 ptr->next = sieve_list;
475 end_critical_section(S_SIEVELIST);
481 * Perform sieve processing for one message (called by sieve_do_room() for each message)
483 void sieve_do_msg(long msgnum, void *userdata) {
484 struct sdm_userdata *u = (struct sdm_userdata *) userdata;
485 sieve2_context_t *sieve2_context = u->sieve2_context;
486 struct ctdl_sieve my;
488 struct CtdlMessage *msg;
490 lprintf(CTDL_DEBUG, "Performing sieve processing on msg <%ld>\n", msgnum);
492 msg = CtdlFetchMessage(msgnum, 0);
493 if (msg == NULL) return;
495 CC->redirect_buffer = malloc(SIZ);
496 CC->redirect_len = 0;
497 CC->redirect_alloc = SIZ;
498 CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ONLY, 0, 1);
499 my.rfc822headers = CC->redirect_buffer;
500 CC->redirect_buffer = NULL;
501 CC->redirect_len = 0;
502 CC->redirect_alloc = 0;
504 my.keep = 0; /* Set to 1 to declare an *explicit* keep */
505 my.cancel_implicit_keep = 0; /* Some actions will cancel the implicit keep */
506 my.usernum = atol(CC->room.QRname); /* Keep track of the owner of the room's namespace */
507 my.msgnum = msgnum; /* Keep track of the message number in our local store */
508 my.u = u; /* Hand off a pointer to the rest of this info */
510 /* Keep track of the recipient so we can do handling based on it later */
511 process_rfc822_addr(msg->cm_fields['R'], my.recp_user, my.recp_node, my.recp_name);
513 /* Keep track of the sender so we can use it for REJECT and VACATION responses */
514 if (msg->cm_fields['F'] != NULL) {
515 safestrncpy(my.sender, msg->cm_fields['F'], sizeof my.sender);
517 else if ( (msg->cm_fields['A'] != NULL) && (msg->cm_fields['N'] != NULL) ) {
518 snprintf(my.sender, sizeof my.sender, "%s@%s", msg->cm_fields['A'], msg->cm_fields['N']);
520 else if (msg->cm_fields['A'] != NULL) {
521 safestrncpy(my.sender, msg->cm_fields['A'], sizeof my.sender);
524 strcpy(my.sender, "");
527 /* Keep track of the subject so we can use it for VACATION responses */
528 if (msg->cm_fields['U'] != NULL) {
529 safestrncpy(my.subject, msg->cm_fields['U'], sizeof my.subject);
532 strcpy(my.subject, "");
535 /* Keep track of the envelope-from address (use body-from if not found) */
536 if (msg->cm_fields['P'] != NULL) {
537 safestrncpy(my.envelope_from, msg->cm_fields['P'], sizeof my.envelope_from);
539 else if (msg->cm_fields['F'] != NULL) {
540 safestrncpy(my.envelope_from, msg->cm_fields['F'], sizeof my.envelope_from);
543 strcpy(my.envelope_from, "");
546 /* Keep track of the envelope-to address */
547 if (msg->cm_fields['V'] != NULL) {
548 safestrncpy(my.envelope_to, msg->cm_fields['V'], sizeof my.envelope_to);
551 strcpy(my.envelope_to, "");
556 sieve2_setvalue_string(sieve2_context, "allheaders", my.rfc822headers);
558 lprintf(CTDL_DEBUG, "Calling sieve2_execute()\n");
559 res = sieve2_execute(sieve2_context, &my);
560 if (res != SIEVE2_OK) {
561 lprintf(CTDL_CRIT, "sieve2_execute() returned %d: %s\n", res, sieve2_errstr(res));
564 free(my.rfc822headers);
565 my.rfc822headers = NULL;
568 * Delete the message from the inbox unless either we were told not to, or
569 * if no other action was successfully taken.
571 if ( (!my.keep) && (my.cancel_implicit_keep) ) {
572 lprintf(CTDL_DEBUG, "keep is 0 -- deleting message from inbox\n");
573 CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, "", 0);
576 lprintf(CTDL_DEBUG, "Completed sieve processing on msg <%ld>\n", msgnum);
577 u->lastproc = msgnum;
585 * Given the on-disk representation of our Sieve config, load
586 * it into an in-memory data structure.
588 void parse_sieve_config(char *conf, struct sdm_userdata *u) {
592 struct sdm_script *sptr;
593 struct sdm_vacation *vptr;
596 while (c = ptr, ptr = bmstrcasestr(ptr, CTDLSIEVECONFIGSEPARATOR), ptr != NULL) {
598 ptr += strlen(CTDLSIEVECONFIGSEPARATOR);
600 extract_token(keyword, c, 0, '|', sizeof keyword);
602 if (!strcasecmp(keyword, "lastproc")) {
603 u->lastproc = extract_long(c, 1);
606 else if (!strcasecmp(keyword, "script")) {
607 sptr = malloc(sizeof(struct sdm_script));
608 extract_token(sptr->script_name, c, 1, '|', sizeof sptr->script_name);
609 sptr->script_active = extract_int(c, 2);
610 remove_token(c, 0, '|');
611 remove_token(c, 0, '|');
612 remove_token(c, 0, '|');
613 sptr->script_content = strdup(c);
614 sptr->next = u->first_script;
615 u->first_script = sptr;
618 else if (!strcasecmp(keyword, "vacation")) {
619 vptr = malloc(sizeof(struct sdm_vacation));
620 vptr->timestamp = extract_long(c, 1);
621 extract_token(vptr->fromaddr, c, 2, '|', sizeof vptr->fromaddr);
622 vptr->next = u->first_vacation;
623 u->first_vacation = vptr;
626 /* ignore unknown keywords */
631 * We found the Sieve configuration for this user.
632 * Now do something with it.
634 void get_sieve_config_backend(long msgnum, void *userdata) {
635 struct sdm_userdata *u = (struct sdm_userdata *) userdata;
636 struct CtdlMessage *msg;
639 u->config_msgnum = msgnum;
640 msg = CtdlFetchMessage(msgnum, 1);
642 u->config_msgnum = (-1) ;
646 conf = msg->cm_fields['M'];
647 msg->cm_fields['M'] = NULL;
648 CtdlFreeMessage(msg);
651 parse_sieve_config(conf, u);
659 * Write our citadel sieve config back to disk
661 void rewrite_ctdl_sieve_config(struct sdm_userdata *u) {
663 struct sdm_script *sptr;
664 struct sdm_vacation *vptr;
669 "Content-type: application/x-citadel-sieve-config\n"
671 CTDLSIEVECONFIGSEPARATOR
673 CTDLSIEVECONFIGSEPARATOR
678 while (u->first_script != NULL) {
679 text = realloc(text, strlen(text) + strlen(u->first_script->script_content) + 256);
680 sprintf(&text[strlen(text)], "script|%s|%d|%s" CTDLSIEVECONFIGSEPARATOR,
681 u->first_script->script_name,
682 u->first_script->script_active,
683 u->first_script->script_content
685 sptr = u->first_script;
686 u->first_script = u->first_script->next;
687 free(sptr->script_content);
691 while (u->first_vacation != NULL) {
692 if ( (time(NULL) - u->first_vacation->timestamp) < MAX_VACATION) {
693 text = realloc(text, strlen(text) + strlen(u->first_vacation->fromaddr) + 256);
694 sprintf(&text[strlen(text)], "vacation|%ld|%s" CTDLSIEVECONFIGSEPARATOR,
695 u->first_vacation->timestamp,
696 u->first_vacation->fromaddr
699 vptr = u->first_vacation;
700 u->first_vacation = u->first_vacation->next;
704 /* Save the config */
705 quickie_message("Citadel", NULL, u->config_roomname,
708 "Sieve configuration"
711 /* And delete the old one */
712 if (u->config_msgnum > 0) {
713 CtdlDeleteMessages(u->config_roomname, &u->config_msgnum, 1, "", 0);
720 * This is our callback registration table for libSieve.
722 sieve2_callback_t ctdl_sieve_callbacks[] = {
723 { SIEVE2_ACTION_REJECT, ctdl_reject },
724 { SIEVE2_ACTION_VACATION, ctdl_vacation },
725 { SIEVE2_ERRCALL_PARSE, ctdl_errparse },
726 { SIEVE2_ERRCALL_RUNTIME, ctdl_errexec },
727 { SIEVE2_ACTION_FILEINTO, ctdl_fileinto },
728 { SIEVE2_ACTION_REDIRECT, ctdl_redirect },
729 { SIEVE2_ACTION_DISCARD, ctdl_discard },
730 { SIEVE2_ACTION_KEEP, ctdl_keep },
731 { SIEVE2_SCRIPT_GETSCRIPT, ctdl_getscript },
732 { SIEVE2_DEBUG_TRACE, ctdl_debug },
733 { SIEVE2_MESSAGE_GETALLHEADERS, ctdl_getheaders },
734 { SIEVE2_MESSAGE_GETSIZE, ctdl_getsize },
735 { SIEVE2_MESSAGE_GETENVELOPE, ctdl_getenvelope },
737 * These actions are unsupported by Citadel so we don't declare them.
739 { SIEVE2_ACTION_NOTIFY, ctdl_notify },
740 { SIEVE2_MESSAGE_GETSUBADDRESS, ctdl_getsubaddress },
741 { SIEVE2_MESSAGE_GETBODY, ctdl_getbody },
749 * Perform sieve processing for a single room
751 void sieve_do_room(char *roomname) {
753 struct sdm_userdata u;
754 sieve2_context_t *sieve2_context = NULL; /* Context for sieve parser */
755 int res; /* Return code from libsieve calls */
756 long orig_lastproc = 0;
758 memset(&u, 0, sizeof u);
760 /* See if the user who owns this 'mailbox' has any Sieve scripts that
763 snprintf(u.config_roomname, sizeof u.config_roomname, "%010ld.%s", atol(roomname), SIEVERULES);
764 if (getroom(&CC->room, u.config_roomname) != 0) {
765 lprintf(CTDL_DEBUG, "<%s> does not exist. No processing is required.\n", u.config_roomname);
770 * Find the sieve scripts and control record and do something
772 u.config_msgnum = (-1);
773 CtdlForEachMessage(MSGS_LAST, 1, NULL, SIEVECONFIG, NULL,
774 get_sieve_config_backend, (void *)&u );
776 if (u.config_msgnum < 0) {
777 lprintf(CTDL_DEBUG, "No Sieve rules exist. No processing is required.\n");
781 lprintf(CTDL_DEBUG, "Rules found. Performing Sieve processing for <%s>\n", roomname);
783 if (getroom(&CC->room, roomname) != 0) {
784 lprintf(CTDL_CRIT, "ERROR: cannot load <%s>\n", roomname);
788 /* Initialize the Sieve parser */
790 res = sieve2_alloc(&sieve2_context);
791 if (res != SIEVE2_OK) {
792 lprintf(CTDL_CRIT, "sieve2_alloc() returned %d: %s\n", res, sieve2_errstr(res));
796 res = sieve2_callbacks(sieve2_context, ctdl_sieve_callbacks);
797 if (res != SIEVE2_OK) {
798 lprintf(CTDL_CRIT, "sieve2_callbacks() returned %d: %s\n", res, sieve2_errstr(res));
802 /* Validate the script */
804 struct ctdl_sieve my; /* dummy ctdl_sieve struct just to pass "u" slong */
805 memset(&my, 0, sizeof my);
807 res = sieve2_validate(sieve2_context, &my);
808 if (res != SIEVE2_OK) {
809 lprintf(CTDL_CRIT, "sieve2_validate() returned %d: %s\n", res, sieve2_errstr(res));
813 /* Do something useful */
814 u.sieve2_context = sieve2_context;
815 orig_lastproc = u.lastproc;
816 CtdlForEachMessage(MSGS_GT, u.lastproc, NULL, NULL, NULL,
822 res = sieve2_free(&sieve2_context);
823 if (res != SIEVE2_OK) {
824 lprintf(CTDL_CRIT, "sieve2_free() returned %d: %s\n", res, sieve2_errstr(res));
827 /* Rewrite the config if we have to */
828 if (u.lastproc > orig_lastproc) {
829 rewrite_ctdl_sieve_config(&u);
835 * Perform sieve processing for all rooms which require it
837 void perform_sieve_processing(void) {
838 struct RoomProcList *ptr = NULL;
840 if (sieve_list != NULL) {
841 lprintf(CTDL_DEBUG, "Begin Sieve processing\n");
842 while (sieve_list != NULL) {
843 char spoolroomname[ROOMNAMELEN];
844 safestrncpy(spoolroomname, sieve_list->name, sizeof spoolroomname);
845 begin_critical_section(S_SIEVELIST);
847 /* pop this record off the list */
849 sieve_list = sieve_list->next;
852 /* invalidate any duplicate entries to prevent double processing */
853 for (ptr=sieve_list; ptr!=NULL; ptr=ptr->next) {
854 if (!strcasecmp(ptr->name, spoolroomname)) {
859 end_critical_section(S_SIEVELIST);
860 if (spoolroomname[0] != 0) {
861 sieve_do_room(spoolroomname);
868 void msiv_load(struct sdm_userdata *u) {
869 char hold_rm[ROOMNAMELEN];
871 strcpy(hold_rm, CC->room.QRname); /* save current room */
873 /* Take a spin through the user's personal address book */
874 if (getroom(&CC->room, SIEVERULES) == 0) {
876 u->config_msgnum = (-1);
877 strcpy(u->config_roomname, CC->room.QRname);
878 CtdlForEachMessage(MSGS_LAST, 1, NULL, SIEVECONFIG, NULL,
879 get_sieve_config_backend, (void *)u );
883 if (strcmp(CC->room.QRname, hold_rm)) {
884 getroom(&CC->room, hold_rm); /* return to saved room */
888 void msiv_store(struct sdm_userdata *u) {
889 rewrite_ctdl_sieve_config(u);
894 * Select the active script.
895 * (Set script_name to an empty string to disable all scripts)
897 * Returns 0 on success or nonzero for error.
899 int msiv_setactive(struct sdm_userdata *u, char *script_name) {
901 struct sdm_script *s;
903 /* First see if the supplied value is ok */
905 if (strlen(script_name) == 0) {
909 for (s=u->first_script; s!=NULL; s=s->next) {
910 if (!strcasecmp(s->script_name, script_name)) {
918 /* Now set the active script */
919 for (s=u->first_script; s!=NULL; s=s->next) {
920 if (!strcasecmp(s->script_name, script_name)) {
921 s->script_active = 1;
924 s->script_active = 0;
933 * Fetch a script by name.
935 * Returns NULL if the named script was not found, or a pointer to the script
936 * if it was found. NOTE: the caller does *not* own the memory returned by
937 * this function. Copy it if you need to keep it.
939 char *msiv_getscript(struct sdm_userdata *u, char *script_name) {
940 struct sdm_script *s;
942 for (s=u->first_script; s!=NULL; s=s->next) {
943 if (!strcasecmp(s->script_name, script_name)) {
944 if (s->script_content != NULL) {
945 return (s->script_content);
955 * Delete a script by name.
957 * Returns 0 if the script was deleted.
958 * 1 if the script was not found.
959 * 2 if the script cannot be deleted because it is active.
961 int msiv_deletescript(struct sdm_userdata *u, char *script_name) {
962 struct sdm_script *s = NULL;
963 struct sdm_script *script_to_delete = NULL;
965 for (s=u->first_script; s!=NULL; s=s->next) {
966 if (!strcasecmp(s->script_name, script_name)) {
967 script_to_delete = s;
968 if (s->script_active) {
974 if (script_to_delete == NULL) return(1);
976 if (u->first_script == script_to_delete) {
977 u->first_script = u->first_script->next;
979 else for (s=u->first_script; s!=NULL; s=s->next) {
980 if (s->next == script_to_delete) {
981 s->next = s->next->next;
985 free(script_to_delete->script_content);
986 free(script_to_delete);
992 * Add or replace a new script.
993 * NOTE: after this function returns, "u" owns the memory that "script_content"
996 void msiv_putscript(struct sdm_userdata *u, char *script_name, char *script_content) {
998 struct sdm_script *s, *sptr;
1000 for (s=u->first_script; s!=NULL; s=s->next) {
1001 if (!strcasecmp(s->script_name, script_name)) {
1002 if (s->script_content != NULL) {
1003 free(s->script_content);
1005 s->script_content = script_content;
1010 if (replaced == 0) {
1011 sptr = malloc(sizeof(struct sdm_script));
1012 safestrncpy(sptr->script_name, script_name, sizeof sptr->script_name);
1013 sptr->script_content = script_content;
1014 sptr->script_active = 0;
1015 sptr->next = u->first_script;
1016 u->first_script = sptr;
1023 * Citadel protocol to manage sieve scripts.
1024 * This is basically a simplified (read: doesn't resemble IMAP) version
1025 * of the 'managesieve' protocol.
1027 void cmd_msiv(char *argbuf) {
1029 struct sdm_userdata u;
1030 char script_name[256];
1031 char *script_content = NULL;
1032 struct sdm_script *s;
1035 memset(&u, 0, sizeof(struct sdm_userdata));
1037 if (CtdlAccessCheck(ac_logged_in)) return;
1038 extract_token(subcmd, argbuf, 0, '|', sizeof subcmd);
1041 if (!strcasecmp(subcmd, "putscript")) {
1042 extract_token(script_name, argbuf, 1, '|', sizeof script_name);
1043 if (strlen(script_name) > 0) {
1044 cprintf("%d Transmit script now\n", SEND_LISTING);
1045 script_content = CtdlReadMessageBody("000", config.c_maxmsglen, NULL, 0);
1046 msiv_putscript(&u, script_name, script_content);
1049 cprintf("%d Invalid script name.\n", ERROR + ILLEGAL_VALUE);
1053 else if (!strcasecmp(subcmd, "listscripts")) {
1054 cprintf("%d Scripts:\n", LISTING_FOLLOWS);
1055 for (s=u.first_script; s!=NULL; s=s->next) {
1056 if (s->script_content != NULL) {
1057 cprintf("%s|%d|\n", s->script_name, s->script_active);
1063 else if (!strcasecmp(subcmd, "setactive")) {
1064 extract_token(script_name, argbuf, 1, '|', sizeof script_name);
1065 if (msiv_setactive(&u, script_name) == 0) {
1066 cprintf("%d ok\n", CIT_OK);
1069 cprintf("%d Script '%s' does not exist.\n",
1070 ERROR + ILLEGAL_VALUE,
1076 else if (!strcasecmp(subcmd, "getscript")) {
1077 extract_token(script_name, argbuf, 1, '|', sizeof script_name);
1078 script_content = msiv_getscript(&u, script_name);
1079 if (script_content != NULL) {
1080 cprintf("%d Script:\n", SEND_LISTING);
1081 cprintf("%s000\n", script_content);
1084 cprintf("%d Invalid script name.\n", ERROR + ILLEGAL_VALUE);
1088 else if (!strcasecmp(subcmd, "deletescript")) {
1089 extract_token(script_name, argbuf, 1, '|', sizeof script_name);
1090 i = msiv_deletescript(&u, script_name);
1092 cprintf("%d ok\n", CIT_OK);
1095 cprintf("%d Script '%s' does not exist.\n",
1096 ERROR + ILLEGAL_VALUE,
1101 cprintf("%d Script '%s' is active and cannot be deleted.\n",
1102 ERROR + ILLEGAL_VALUE,
1107 cprintf("%d unknown error\n", ERROR);
1112 cprintf("%d Invalid subcommand\n", ERROR + CMD_NOT_SUPPORTED);
1120 void ctdl_sieve_init(void) {
1122 sieve2_context_t *sieve2_context = NULL;
1126 * We don't really care about dumping the entire credits to the log
1127 * every time the server is initialized. The documentation will suffice
1128 * for that purpose. We are making a call to sieve2_credits() in order
1129 * to demonstrate that we have successfully linked in to libsieve.
1131 cred = strdup(sieve2_credits());
1132 if (cred == NULL) return;
1134 if (strlen(cred) > 60) {
1135 strcpy(&cred[55], "...");
1138 lprintf(CTDL_INFO, "%s\n",cred);
1141 /* Briefly initialize a Sieve parser instance just so we can list the
1142 * extensions that are available.
1144 res = sieve2_alloc(&sieve2_context);
1145 if (res != SIEVE2_OK) {
1146 lprintf(CTDL_CRIT, "sieve2_alloc() returned %d: %s\n", res, sieve2_errstr(res));
1150 res = sieve2_callbacks(sieve2_context, ctdl_sieve_callbacks);
1151 if (res != SIEVE2_OK) {
1152 lprintf(CTDL_CRIT, "sieve2_callbacks() returned %d: %s\n", res, sieve2_errstr(res));
1156 msiv_extensions = strdup(sieve2_listextensions(sieve2_context));
1157 lprintf(CTDL_INFO, "Extensions: %s\n", msiv_extensions);
1159 BAIL: res = sieve2_free(&sieve2_context);
1160 if (res != SIEVE2_OK) {
1161 lprintf(CTDL_CRIT, "sieve2_free() returned %d: %s\n", res, sieve2_errstr(res));
1168 char *serv_sieve_init(void)
1171 CtdlRegisterProtoHook(cmd_msiv, "MSIV", "Manage Sieve scripts");
1172 return "$Id: serv_sieve.c 3850 2005-09-13 14:00:24Z ajc $";
1175 #else /* HAVE_LIBSIEVE */
1177 char *serv_sieve_init(void)
1179 lprintf(CTDL_INFO, "This server is missing libsieve. Mailbox filtering will be disabled.\n");
1180 return "$Id: serv_sieve.c 3850 2005-09-13 14:00:24Z ajc $";
1183 #endif /* HAVE_LIBSIEVE */