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 */
274 "Delivery status notification"
278 cs->cancel_implicit_keep = 1;
285 * Callback function to indicate that a vacation message should be generated
287 int ctdl_vacation(sieve2_context_t *s, void *my)
289 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
290 struct sdm_vacation *vptr;
293 char *vacamsg_text = NULL;
294 char vacamsg_subject[1024];
296 lprintf(CTDL_DEBUG, "Action is VACATION\n");
298 message = sieve2_getvalue_string(s, "message");
299 if (message == NULL) return SIEVE2_ERROR_BADARGS;
301 if (sieve2_getvalue_string(s, "subject") != NULL) {
302 safestrncpy(vacamsg_subject, sieve2_getvalue_string(s, "subject"), sizeof vacamsg_subject);
305 snprintf(vacamsg_subject, sizeof vacamsg_subject, "Re: %s", cs->subject);
308 days = sieve2_getvalue_int(s, "days");
309 if (days < 1) days = 1;
310 if (days > MAX_VACATION) days = MAX_VACATION;
312 /* Check to see whether we've already alerted this sender that we're on vacation. */
313 for (vptr = cs->u->first_vacation; vptr != NULL; vptr = vptr->next) {
314 if (!strcasecmp(vptr->fromaddr, cs->sender)) {
315 if ( (time(NULL) - vptr->timestamp) < (days * 86400) ) {
316 lprintf(CTDL_DEBUG, "Already alerted <%s> recently.\n", cs->sender);
322 /* Assemble the reject message. */
323 vacamsg_text = malloc(strlen(message) + 1024);
324 if (vacamsg_text == NULL) {
325 return SIEVE2_ERROR_FAIL;
328 sprintf(vacamsg_text,
329 "Content-type: text/plain\n"
337 quickie_message( /* This delivers the message */
349 /* Now update the list to reflect the fact that we've alerted this sender.
350 * If they're already in the list, just update the timestamp.
352 for (vptr = cs->u->first_vacation; vptr != NULL; vptr = vptr->next) {
353 if (!strcasecmp(vptr->fromaddr, cs->sender)) {
354 vptr->timestamp = time(NULL);
359 /* If we get to this point, create a new record.
361 vptr = malloc(sizeof(struct sdm_vacation));
362 vptr->timestamp = time(NULL);
363 safestrncpy(vptr->fromaddr, cs->sender, sizeof vptr->fromaddr);
364 vptr->next = cs->u->first_vacation;
365 cs->u->first_vacation = vptr;
372 * Callback function to parse addresses per local system convention
373 * It is disabled because we don't support subaddresses.
376 int ctdl_getsubaddress(sieve2_context_t *s, void *my)
378 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
380 /* libSieve does not take ownership of the memory used here. But, since we
381 * are just pointing to locations inside a struct which we are going to free
384 sieve2_setvalue_string(s, "user", cs->recp_user);
385 sieve2_setvalue_string(s, "detail", "");
386 sieve2_setvalue_string(s, "localpart", cs->recp_user);
387 sieve2_setvalue_string(s, "domain", cs->recp_node);
394 * Callback function to parse message envelope
396 int ctdl_getenvelope(sieve2_context_t *s, void *my)
398 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
400 lprintf(CTDL_DEBUG, "Action is GETENVELOPE\n");
401 sieve2_setvalue_string(s, "to", cs->envelope_to);
402 sieve2_setvalue_string(s, "from", cs->envelope_from);
408 * Callback function to fetch message body
409 * (Uncomment the code if we implement this extension)
411 int ctdl_getbody(sieve2_context_t *s, void *my)
413 return SIEVE2_ERROR_UNSUPPORTED;
420 * Callback function to fetch message size
422 int ctdl_getsize(sieve2_context_t *s, void *my)
424 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
427 GetMetaData(&smi, cs->msgnum);
429 if (smi.meta_rfc822_length > 0L) {
430 sieve2_setvalue_int(s, "size", (int)smi.meta_rfc822_length);
434 return SIEVE2_ERROR_UNSUPPORTED;
439 * Callback function to retrieve the sieve script
441 int ctdl_getscript(sieve2_context_t *s, void *my) {
442 struct sdm_script *sptr;
443 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
445 for (sptr=cs->u->first_script; sptr!=NULL; sptr=sptr->next) {
446 if (sptr->script_active > 0) {
447 lprintf(CTDL_DEBUG, "ctdl_getscript() is using script '%s'\n", sptr->script_name);
448 sieve2_setvalue_string(s, "script", sptr->script_content);
453 lprintf(CTDL_DEBUG, "ctdl_getscript() found no active script\n");
454 return SIEVE2_ERROR_GETSCRIPT;
458 * Callback function to retrieve message headers
460 int ctdl_getheaders(sieve2_context_t *s, void *my) {
462 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
464 lprintf(CTDL_DEBUG, "ctdl_getheaders() was called\n");
466 sieve2_setvalue_string(s, "allheaders", cs->rfc822headers);
473 * Add a room to the list of those rooms which potentially require sieve processing
475 void sieve_queue_room(struct ctdlroom *which_room) {
476 struct RoomProcList *ptr;
478 ptr = (struct RoomProcList *) malloc(sizeof (struct RoomProcList));
479 if (ptr == NULL) return;
481 safestrncpy(ptr->name, which_room->QRname, sizeof ptr->name);
482 begin_critical_section(S_SIEVELIST);
483 ptr->next = sieve_list;
485 end_critical_section(S_SIEVELIST);
491 * Perform sieve processing for one message (called by sieve_do_room() for each message)
493 void sieve_do_msg(long msgnum, void *userdata) {
494 struct sdm_userdata *u = (struct sdm_userdata *) userdata;
495 sieve2_context_t *sieve2_context = u->sieve2_context;
496 struct ctdl_sieve my;
498 struct CtdlMessage *msg;
500 lprintf(CTDL_DEBUG, "Performing sieve processing on msg <%ld>\n", msgnum);
502 msg = CtdlFetchMessage(msgnum, 0);
503 if (msg == NULL) return;
505 CC->redirect_buffer = malloc(SIZ);
506 CC->redirect_len = 0;
507 CC->redirect_alloc = SIZ;
508 CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ONLY, 0, 1);
509 my.rfc822headers = CC->redirect_buffer;
510 CC->redirect_buffer = NULL;
511 CC->redirect_len = 0;
512 CC->redirect_alloc = 0;
514 my.keep = 0; /* Set to 1 to declare an *explicit* keep */
515 my.cancel_implicit_keep = 0; /* Some actions will cancel the implicit keep */
516 my.usernum = atol(CC->room.QRname); /* Keep track of the owner of the room's namespace */
517 my.msgnum = msgnum; /* Keep track of the message number in our local store */
518 my.u = u; /* Hand off a pointer to the rest of this info */
520 /* Keep track of the recipient so we can do handling based on it later */
521 process_rfc822_addr(msg->cm_fields['R'], my.recp_user, my.recp_node, my.recp_name);
523 /* Keep track of the sender so we can use it for REJECT and VACATION responses */
524 if (msg->cm_fields['F'] != NULL) {
525 safestrncpy(my.sender, msg->cm_fields['F'], sizeof my.sender);
527 else if ( (msg->cm_fields['A'] != NULL) && (msg->cm_fields['N'] != NULL) ) {
528 snprintf(my.sender, sizeof my.sender, "%s@%s", msg->cm_fields['A'], msg->cm_fields['N']);
530 else if (msg->cm_fields['A'] != NULL) {
531 safestrncpy(my.sender, msg->cm_fields['A'], sizeof my.sender);
534 strcpy(my.sender, "");
537 /* Keep track of the subject so we can use it for VACATION responses */
538 if (msg->cm_fields['U'] != NULL) {
539 safestrncpy(my.subject, msg->cm_fields['U'], sizeof my.subject);
542 strcpy(my.subject, "");
545 /* Keep track of the envelope-from address (use body-from if not found) */
546 if (msg->cm_fields['P'] != NULL) {
547 safestrncpy(my.envelope_from, msg->cm_fields['P'], sizeof my.envelope_from);
549 else if (msg->cm_fields['F'] != NULL) {
550 safestrncpy(my.envelope_from, msg->cm_fields['F'], sizeof my.envelope_from);
553 strcpy(my.envelope_from, "");
556 /* Keep track of the envelope-to address */
557 if (msg->cm_fields['V'] != NULL) {
558 safestrncpy(my.envelope_to, msg->cm_fields['V'], sizeof my.envelope_to);
561 strcpy(my.envelope_to, "");
566 sieve2_setvalue_string(sieve2_context, "allheaders", my.rfc822headers);
568 lprintf(CTDL_DEBUG, "Calling sieve2_execute()\n");
569 res = sieve2_execute(sieve2_context, &my);
570 if (res != SIEVE2_OK) {
571 lprintf(CTDL_CRIT, "sieve2_execute() returned %d: %s\n", res, sieve2_errstr(res));
574 free(my.rfc822headers);
575 my.rfc822headers = NULL;
578 * Delete the message from the inbox unless either we were told not to, or
579 * if no other action was successfully taken.
581 if ( (!my.keep) && (my.cancel_implicit_keep) ) {
582 lprintf(CTDL_DEBUG, "keep is 0 -- deleting message from inbox\n");
583 CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, "", 0);
586 lprintf(CTDL_DEBUG, "Completed sieve processing on msg <%ld>\n", msgnum);
587 u->lastproc = msgnum;
595 * Given the on-disk representation of our Sieve config, load
596 * it into an in-memory data structure.
598 void parse_sieve_config(char *conf, struct sdm_userdata *u) {
602 struct sdm_script *sptr;
603 struct sdm_vacation *vptr;
606 while (c = ptr, ptr = bmstrcasestr(ptr, CTDLSIEVECONFIGSEPARATOR), ptr != NULL) {
608 ptr += strlen(CTDLSIEVECONFIGSEPARATOR);
610 extract_token(keyword, c, 0, '|', sizeof keyword);
612 if (!strcasecmp(keyword, "lastproc")) {
613 u->lastproc = extract_long(c, 1);
616 else if (!strcasecmp(keyword, "script")) {
617 sptr = malloc(sizeof(struct sdm_script));
618 extract_token(sptr->script_name, c, 1, '|', sizeof sptr->script_name);
619 sptr->script_active = extract_int(c, 2);
620 remove_token(c, 0, '|');
621 remove_token(c, 0, '|');
622 remove_token(c, 0, '|');
623 sptr->script_content = strdup(c);
624 sptr->next = u->first_script;
625 u->first_script = sptr;
628 else if (!strcasecmp(keyword, "vacation")) {
630 if (c != NULL) while (vacrec=c, c=strchr(c, '\n'), (c != NULL)) {
635 if (strncasecmp(vacrec, "vacation|", 9)) {
636 vptr = malloc(sizeof(struct sdm_vacation));
637 extract_token(vptr->fromaddr, vacrec, 0, '|', sizeof vptr->fromaddr);
638 vptr->timestamp = extract_long(vacrec, 1);
639 vptr->next = u->first_vacation;
640 u->first_vacation = vptr;
645 /* ignore unknown keywords */
650 * We found the Sieve configuration for this user.
651 * Now do something with it.
653 void get_sieve_config_backend(long msgnum, void *userdata) {
654 struct sdm_userdata *u = (struct sdm_userdata *) userdata;
655 struct CtdlMessage *msg;
658 u->config_msgnum = msgnum;
659 msg = CtdlFetchMessage(msgnum, 1);
661 u->config_msgnum = (-1) ;
665 conf = msg->cm_fields['M'];
666 msg->cm_fields['M'] = NULL;
667 CtdlFreeMessage(msg);
670 parse_sieve_config(conf, u);
678 * Write our citadel sieve config back to disk
680 * (Set yes_write_to_disk to nonzero to make it actually write the config;
681 * otherwise it just frees the data structures.)
683 void rewrite_ctdl_sieve_config(struct sdm_userdata *u, int yes_write_to_disk) {
685 struct sdm_script *sptr;
686 struct sdm_vacation *vptr;
691 "Content-type: application/x-citadel-sieve-config\n"
693 CTDLSIEVECONFIGSEPARATOR
695 CTDLSIEVECONFIGSEPARATOR
700 while (u->first_script != NULL) {
701 text = realloc(text, strlen(text) + strlen(u->first_script->script_content) + 256);
702 sprintf(&text[strlen(text)], "script|%s|%d|%s" CTDLSIEVECONFIGSEPARATOR,
703 u->first_script->script_name,
704 u->first_script->script_active,
705 u->first_script->script_content
707 sptr = u->first_script;
708 u->first_script = u->first_script->next;
709 free(sptr->script_content);
713 if (u->first_vacation != NULL) {
715 size_t realloc_len = strlen(text) + 256;
716 for (vptr = u->first_vacation; vptr != NULL; vptr = vptr->next) {
717 realloc_len += strlen(vptr->fromaddr + 32);
719 text = realloc(text, realloc_len);
721 sprintf(&text[strlen(text)], "vacation|\n");
722 while (u->first_vacation != NULL) {
723 if ( (time(NULL) - u->first_vacation->timestamp) < (MAX_VACATION * 86400)) {
724 sprintf(&text[strlen(text)], "%s|%ld\n",
725 u->first_vacation->fromaddr,
726 u->first_vacation->timestamp
729 vptr = u->first_vacation;
730 u->first_vacation = u->first_vacation->next;
733 sprintf(&text[strlen(text)], CTDLSIEVECONFIGSEPARATOR);
736 /* Save the config */
737 quickie_message("Citadel", NULL, NULL, u->config_roomname,
740 "Sieve configuration"
743 /* And delete the old one */
744 if (u->config_msgnum > 0) {
745 CtdlDeleteMessages(u->config_roomname, &u->config_msgnum, 1, "", 0);
752 * This is our callback registration table for libSieve.
754 sieve2_callback_t ctdl_sieve_callbacks[] = {
755 { SIEVE2_ACTION_REJECT, ctdl_reject },
756 { SIEVE2_ACTION_VACATION, ctdl_vacation },
757 { SIEVE2_ERRCALL_PARSE, ctdl_errparse },
758 { SIEVE2_ERRCALL_RUNTIME, ctdl_errexec },
759 { SIEVE2_ACTION_FILEINTO, ctdl_fileinto },
760 { SIEVE2_ACTION_REDIRECT, ctdl_redirect },
761 { SIEVE2_ACTION_DISCARD, ctdl_discard },
762 { SIEVE2_ACTION_KEEP, ctdl_keep },
763 { SIEVE2_SCRIPT_GETSCRIPT, ctdl_getscript },
764 { SIEVE2_DEBUG_TRACE, ctdl_debug },
765 { SIEVE2_MESSAGE_GETALLHEADERS, ctdl_getheaders },
766 { SIEVE2_MESSAGE_GETSIZE, ctdl_getsize },
767 { SIEVE2_MESSAGE_GETENVELOPE, ctdl_getenvelope },
769 * These actions are unsupported by Citadel so we don't declare them.
771 { SIEVE2_ACTION_NOTIFY, ctdl_notify },
772 { SIEVE2_MESSAGE_GETSUBADDRESS, ctdl_getsubaddress },
773 { SIEVE2_MESSAGE_GETBODY, ctdl_getbody },
781 * Perform sieve processing for a single room
783 void sieve_do_room(char *roomname) {
785 struct sdm_userdata u;
786 sieve2_context_t *sieve2_context = NULL; /* Context for sieve parser */
787 int res; /* Return code from libsieve calls */
788 long orig_lastproc = 0;
790 memset(&u, 0, sizeof u);
792 /* See if the user who owns this 'mailbox' has any Sieve scripts that
795 snprintf(u.config_roomname, sizeof u.config_roomname, "%010ld.%s", atol(roomname), USERCONFIGROOM);
796 if (getroom(&CC->room, u.config_roomname) != 0) {
797 lprintf(CTDL_DEBUG, "<%s> does not exist. No processing is required.\n", u.config_roomname);
802 * Find the sieve scripts and control record and do something
804 u.config_msgnum = (-1);
805 CtdlForEachMessage(MSGS_LAST, 1, NULL, SIEVECONFIG, NULL,
806 get_sieve_config_backend, (void *)&u );
808 if (u.config_msgnum < 0) {
809 lprintf(CTDL_DEBUG, "No Sieve rules exist. No processing is required.\n");
813 lprintf(CTDL_DEBUG, "Rules found. Performing Sieve processing for <%s>\n", roomname);
815 if (getroom(&CC->room, roomname) != 0) {
816 lprintf(CTDL_CRIT, "ERROR: cannot load <%s>\n", roomname);
820 /* Initialize the Sieve parser */
822 res = sieve2_alloc(&sieve2_context);
823 if (res != SIEVE2_OK) {
824 lprintf(CTDL_CRIT, "sieve2_alloc() returned %d: %s\n", res, sieve2_errstr(res));
828 res = sieve2_callbacks(sieve2_context, ctdl_sieve_callbacks);
829 if (res != SIEVE2_OK) {
830 lprintf(CTDL_CRIT, "sieve2_callbacks() returned %d: %s\n", res, sieve2_errstr(res));
834 /* Validate the script */
836 struct ctdl_sieve my; /* dummy ctdl_sieve struct just to pass "u" slong */
837 memset(&my, 0, sizeof my);
839 res = sieve2_validate(sieve2_context, &my);
840 if (res != SIEVE2_OK) {
841 lprintf(CTDL_CRIT, "sieve2_validate() returned %d: %s\n", res, sieve2_errstr(res));
845 /* Do something useful */
846 u.sieve2_context = sieve2_context;
847 orig_lastproc = u.lastproc;
848 CtdlForEachMessage(MSGS_GT, u.lastproc, NULL, NULL, NULL,
854 res = sieve2_free(&sieve2_context);
855 if (res != SIEVE2_OK) {
856 lprintf(CTDL_CRIT, "sieve2_free() returned %d: %s\n", res, sieve2_errstr(res));
859 /* Rewrite the config if we have to */
860 rewrite_ctdl_sieve_config(&u, (u.lastproc > orig_lastproc) ) ;
865 * Perform sieve processing for all rooms which require it
867 void perform_sieve_processing(void) {
868 struct RoomProcList *ptr = NULL;
870 if (sieve_list != NULL) {
871 lprintf(CTDL_DEBUG, "Begin Sieve processing\n");
872 while (sieve_list != NULL) {
873 char spoolroomname[ROOMNAMELEN];
874 safestrncpy(spoolroomname, sieve_list->name, sizeof spoolroomname);
875 begin_critical_section(S_SIEVELIST);
877 /* pop this record off the list */
879 sieve_list = sieve_list->next;
882 /* invalidate any duplicate entries to prevent double processing */
883 for (ptr=sieve_list; ptr!=NULL; ptr=ptr->next) {
884 if (!strcasecmp(ptr->name, spoolroomname)) {
889 end_critical_section(S_SIEVELIST);
890 if (spoolroomname[0] != 0) {
891 sieve_do_room(spoolroomname);
898 void msiv_load(struct sdm_userdata *u) {
899 char hold_rm[ROOMNAMELEN];
901 strcpy(hold_rm, CC->room.QRname); /* save current room */
903 /* Take a spin through the user's personal address book */
904 if (getroom(&CC->room, USERCONFIGROOM) == 0) {
906 u->config_msgnum = (-1);
907 strcpy(u->config_roomname, CC->room.QRname);
908 CtdlForEachMessage(MSGS_LAST, 1, NULL, SIEVECONFIG, NULL,
909 get_sieve_config_backend, (void *)u );
913 if (strcmp(CC->room.QRname, hold_rm)) {
914 getroom(&CC->room, hold_rm); /* return to saved room */
918 void msiv_store(struct sdm_userdata *u, int yes_write_to_disk) {
919 rewrite_ctdl_sieve_config(u, yes_write_to_disk);
924 * Select the active script.
925 * (Set script_name to an empty string to disable all scripts)
927 * Returns 0 on success or nonzero for error.
929 int msiv_setactive(struct sdm_userdata *u, char *script_name) {
931 struct sdm_script *s;
933 /* First see if the supplied value is ok */
935 if (strlen(script_name) == 0) {
939 for (s=u->first_script; s!=NULL; s=s->next) {
940 if (!strcasecmp(s->script_name, script_name)) {
948 /* Now set the active script */
949 for (s=u->first_script; s!=NULL; s=s->next) {
950 if (!strcasecmp(s->script_name, script_name)) {
951 s->script_active = 1;
954 s->script_active = 0;
963 * Fetch a script by name.
965 * Returns NULL if the named script was not found, or a pointer to the script
966 * if it was found. NOTE: the caller does *not* own the memory returned by
967 * this function. Copy it if you need to keep it.
969 char *msiv_getscript(struct sdm_userdata *u, char *script_name) {
970 struct sdm_script *s;
972 for (s=u->first_script; s!=NULL; s=s->next) {
973 if (!strcasecmp(s->script_name, script_name)) {
974 if (s->script_content != NULL) {
975 return (s->script_content);
985 * Delete a script by name.
987 * Returns 0 if the script was deleted.
988 * 1 if the script was not found.
989 * 2 if the script cannot be deleted because it is active.
991 int msiv_deletescript(struct sdm_userdata *u, char *script_name) {
992 struct sdm_script *s = NULL;
993 struct sdm_script *script_to_delete = NULL;
995 for (s=u->first_script; s!=NULL; s=s->next) {
996 if (!strcasecmp(s->script_name, script_name)) {
997 script_to_delete = s;
998 if (s->script_active) {
1004 if (script_to_delete == NULL) return(1);
1006 if (u->first_script == script_to_delete) {
1007 u->first_script = u->first_script->next;
1009 else for (s=u->first_script; s!=NULL; s=s->next) {
1010 if (s->next == script_to_delete) {
1011 s->next = s->next->next;
1015 free(script_to_delete->script_content);
1016 free(script_to_delete);
1022 * Add or replace a new script.
1023 * NOTE: after this function returns, "u" owns the memory that "script_content"
1026 void msiv_putscript(struct sdm_userdata *u, char *script_name, char *script_content) {
1028 struct sdm_script *s, *sptr;
1030 for (s=u->first_script; s!=NULL; s=s->next) {
1031 if (!strcasecmp(s->script_name, script_name)) {
1032 if (s->script_content != NULL) {
1033 free(s->script_content);
1035 s->script_content = script_content;
1040 if (replaced == 0) {
1041 sptr = malloc(sizeof(struct sdm_script));
1042 safestrncpy(sptr->script_name, script_name, sizeof sptr->script_name);
1043 sptr->script_content = script_content;
1044 sptr->script_active = 0;
1045 sptr->next = u->first_script;
1046 u->first_script = sptr;
1053 * Citadel protocol to manage sieve scripts.
1054 * This is basically a simplified (read: doesn't resemble IMAP) version
1055 * of the 'managesieve' protocol.
1057 void cmd_msiv(char *argbuf) {
1059 struct sdm_userdata u;
1060 char script_name[256];
1061 char *script_content = NULL;
1062 struct sdm_script *s;
1064 int changes_made = 0;
1066 memset(&u, 0, sizeof(struct sdm_userdata));
1068 if (CtdlAccessCheck(ac_logged_in)) return;
1069 extract_token(subcmd, argbuf, 0, '|', sizeof subcmd);
1072 if (!strcasecmp(subcmd, "putscript")) {
1073 extract_token(script_name, argbuf, 1, '|', sizeof script_name);
1074 if (strlen(script_name) > 0) {
1075 cprintf("%d Transmit script now\n", SEND_LISTING);
1076 script_content = CtdlReadMessageBody("000", config.c_maxmsglen, NULL, 0);
1077 msiv_putscript(&u, script_name, script_content);
1081 cprintf("%d Invalid script name.\n", ERROR + ILLEGAL_VALUE);
1085 else if (!strcasecmp(subcmd, "listscripts")) {
1086 cprintf("%d Scripts:\n", LISTING_FOLLOWS);
1087 for (s=u.first_script; s!=NULL; s=s->next) {
1088 if (s->script_content != NULL) {
1089 cprintf("%s|%d|\n", s->script_name, s->script_active);
1095 else if (!strcasecmp(subcmd, "setactive")) {
1096 extract_token(script_name, argbuf, 1, '|', sizeof script_name);
1097 if (msiv_setactive(&u, script_name) == 0) {
1098 cprintf("%d ok\n", CIT_OK);
1102 cprintf("%d Script '%s' does not exist.\n",
1103 ERROR + ILLEGAL_VALUE,
1109 else if (!strcasecmp(subcmd, "getscript")) {
1110 extract_token(script_name, argbuf, 1, '|', sizeof script_name);
1111 script_content = msiv_getscript(&u, script_name);
1112 if (script_content != NULL) {
1113 cprintf("%d Script:\n", LISTING_FOLLOWS);
1114 cprintf("%s000\n", script_content);
1117 cprintf("%d Invalid script name.\n", ERROR + ILLEGAL_VALUE);
1121 else if (!strcasecmp(subcmd, "deletescript")) {
1122 extract_token(script_name, argbuf, 1, '|', sizeof script_name);
1123 i = msiv_deletescript(&u, script_name);
1125 cprintf("%d ok\n", CIT_OK);
1129 cprintf("%d Script '%s' does not exist.\n",
1130 ERROR + ILLEGAL_VALUE,
1135 cprintf("%d Script '%s' is active and cannot be deleted.\n",
1136 ERROR + ILLEGAL_VALUE,
1141 cprintf("%d unknown error\n", ERROR);
1146 cprintf("%d Invalid subcommand\n", ERROR + CMD_NOT_SUPPORTED);
1149 msiv_store(&u, changes_made);
1154 void ctdl_sieve_init(void) {
1156 sieve2_context_t *sieve2_context = NULL;
1160 * We don't really care about dumping the entire credits to the log
1161 * every time the server is initialized. The documentation will suffice
1162 * for that purpose. We are making a call to sieve2_credits() in order
1163 * to demonstrate that we have successfully linked in to libsieve.
1165 cred = strdup(sieve2_credits());
1166 if (cred == NULL) return;
1168 if (strlen(cred) > 60) {
1169 strcpy(&cred[55], "...");
1172 lprintf(CTDL_INFO, "%s\n",cred);
1175 /* Briefly initialize a Sieve parser instance just so we can list the
1176 * extensions that are available.
1178 res = sieve2_alloc(&sieve2_context);
1179 if (res != SIEVE2_OK) {
1180 lprintf(CTDL_CRIT, "sieve2_alloc() returned %d: %s\n", res, sieve2_errstr(res));
1184 res = sieve2_callbacks(sieve2_context, ctdl_sieve_callbacks);
1185 if (res != SIEVE2_OK) {
1186 lprintf(CTDL_CRIT, "sieve2_callbacks() returned %d: %s\n", res, sieve2_errstr(res));
1190 msiv_extensions = strdup(sieve2_listextensions(sieve2_context));
1191 lprintf(CTDL_INFO, "Extensions: %s\n", msiv_extensions);
1193 BAIL: res = sieve2_free(&sieve2_context);
1194 if (res != SIEVE2_OK) {
1195 lprintf(CTDL_CRIT, "sieve2_free() returned %d: %s\n", res, sieve2_errstr(res));
1202 char *serv_sieve_init(void)
1205 CtdlRegisterProtoHook(cmd_msiv, "MSIV", "Manage Sieve scripts");
1206 return "$Id: serv_sieve.c 3850 2005-09-13 14:00:24Z ajc $";
1209 #else /* HAVE_LIBSIEVE */
1211 char *serv_sieve_init(void)
1213 lprintf(CTDL_INFO, "This server is missing libsieve. Mailbox filtering will be disabled.\n");
1214 return "$Id: serv_sieve.c 3850 2005-09-13 14:00:24Z ajc $";
1217 #endif /* HAVE_LIBSIEVE */