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;
293 char vacamsg_subject[1024];
295 lprintf(CTDL_DEBUG, "Action is VACATION\n");
297 message = sieve2_getvalue_string(s, "message");
298 if (message == NULL) return SIEVE2_ERROR_BADARGS;
300 if (sieve2_getvalue_string(s, "subject") != NULL) {
301 safestrncpy(vacamsg_subject, sieve2_getvalue_string(s, "subject"), sizeof vacamsg_subject);
304 snprintf(vacamsg_subject, sizeof vacamsg_subject, "Re: %s", cs->subject);
307 days = sieve2_getvalue_int(s, "days");
308 if (days < 1) days = 1;
309 if (days > MAX_VACATION) days = MAX_VACATION;
311 /* Check to see whether we've already alerted this sender that we're on vacation. */
312 for (vptr = cs->u->first_vacation; vptr != NULL; vptr = vptr->next) {
313 if (!strcasecmp(vptr->fromaddr, cs->sender)) {
314 if ( (time(NULL) - vptr->timestamp) < (days * 86400) ) {
315 lprintf(CTDL_DEBUG, "Already alerted <%s> recently.\n", cs->sender);
321 /* Assemble the reject message. */
322 vacamsg_text = malloc(strlen(message) + 1024);
323 if (vacamsg_text == NULL) {
324 return SIEVE2_ERROR_FAIL;
327 sprintf(vacamsg_text,
328 "Content-type: text/plain\n"
336 quickie_message( /* This delivers the message */
337 "Citadel", /* FIXME make it myself */
347 /* Now update the list to reflect the fact that we've alerted this sender.
348 * If they're already in the list, just update the timestamp.
350 for (vptr = cs->u->first_vacation; vptr != NULL; vptr = vptr->next) {
351 if (!strcasecmp(vptr->fromaddr, cs->sender)) {
352 vptr->timestamp = time(NULL);
357 /* If we get to this point, create a new record.
359 vptr = malloc(sizeof(struct sdm_vacation));
360 vptr->timestamp = time(NULL);
361 safestrncpy(vptr->fromaddr, cs->sender, sizeof vptr->fromaddr);
362 vptr->next = cs->u->first_vacation;
363 cs->u->first_vacation = vptr;
370 * Callback function to parse addresses per local system convention
371 * It is disabled because we don't support subaddresses.
374 int ctdl_getsubaddress(sieve2_context_t *s, void *my)
376 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
378 /* libSieve does not take ownership of the memory used here. But, since we
379 * are just pointing to locations inside a struct which we are going to free
382 sieve2_setvalue_string(s, "user", cs->recp_user);
383 sieve2_setvalue_string(s, "detail", "");
384 sieve2_setvalue_string(s, "localpart", cs->recp_user);
385 sieve2_setvalue_string(s, "domain", cs->recp_node);
392 * Callback function to parse message envelope
394 int ctdl_getenvelope(sieve2_context_t *s, void *my)
396 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
398 lprintf(CTDL_DEBUG, "Action is GETENVELOPE\n");
399 sieve2_setvalue_string(s, "to", cs->envelope_to);
400 sieve2_setvalue_string(s, "from", cs->envelope_from);
406 * Callback function to fetch message body
407 * FIXME implement this
410 int ctdl_getbody(sieve2_context_t *s, void *my)
412 return SIEVE2_ERROR_UNSUPPORTED;
418 * Callback function to fetch message size
420 int ctdl_getsize(sieve2_context_t *s, void *my)
422 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
425 GetMetaData(&smi, cs->msgnum);
427 if (smi.meta_rfc822_length > 0L) {
428 sieve2_setvalue_int(s, "size", (int)smi.meta_rfc822_length);
432 return SIEVE2_ERROR_UNSUPPORTED;
437 * Callback function to retrieve the sieve script
439 int ctdl_getscript(sieve2_context_t *s, void *my) {
440 struct sdm_script *sptr;
441 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
443 for (sptr=cs->u->first_script; sptr!=NULL; sptr=sptr->next) {
444 if (sptr->script_active > 0) {
445 lprintf(CTDL_DEBUG, "ctdl_getscript() is using script '%s'\n", sptr->script_name);
446 sieve2_setvalue_string(s, "script", sptr->script_content);
451 lprintf(CTDL_DEBUG, "ctdl_getscript() found no active script\n");
452 return SIEVE2_ERROR_GETSCRIPT;
456 * Callback function to retrieve message headers
458 int ctdl_getheaders(sieve2_context_t *s, void *my) {
460 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
462 lprintf(CTDL_DEBUG, "ctdl_getheaders() was called\n");
464 sieve2_setvalue_string(s, "allheaders", cs->rfc822headers);
471 * Add a room to the list of those rooms which potentially require sieve processing
473 void sieve_queue_room(struct ctdlroom *which_room) {
474 struct RoomProcList *ptr;
476 ptr = (struct RoomProcList *) malloc(sizeof (struct RoomProcList));
477 if (ptr == NULL) return;
479 safestrncpy(ptr->name, which_room->QRname, sizeof ptr->name);
480 begin_critical_section(S_SIEVELIST);
481 ptr->next = sieve_list;
483 end_critical_section(S_SIEVELIST);
489 * Perform sieve processing for one message (called by sieve_do_room() for each message)
491 void sieve_do_msg(long msgnum, void *userdata) {
492 struct sdm_userdata *u = (struct sdm_userdata *) userdata;
493 sieve2_context_t *sieve2_context = u->sieve2_context;
494 struct ctdl_sieve my;
496 struct CtdlMessage *msg;
498 lprintf(CTDL_DEBUG, "Performing sieve processing on msg <%ld>\n", msgnum);
500 msg = CtdlFetchMessage(msgnum, 0);
501 if (msg == NULL) return;
503 CC->redirect_buffer = malloc(SIZ);
504 CC->redirect_len = 0;
505 CC->redirect_alloc = SIZ;
506 CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ONLY, 0, 1);
507 my.rfc822headers = CC->redirect_buffer;
508 CC->redirect_buffer = NULL;
509 CC->redirect_len = 0;
510 CC->redirect_alloc = 0;
512 my.keep = 0; /* Set to 1 to declare an *explicit* keep */
513 my.cancel_implicit_keep = 0; /* Some actions will cancel the implicit keep */
514 my.usernum = atol(CC->room.QRname); /* Keep track of the owner of the room's namespace */
515 my.msgnum = msgnum; /* Keep track of the message number in our local store */
516 my.u = u; /* Hand off a pointer to the rest of this info */
518 /* Keep track of the recipient so we can do handling based on it later */
519 process_rfc822_addr(msg->cm_fields['R'], my.recp_user, my.recp_node, my.recp_name);
521 /* Keep track of the sender so we can use it for REJECT and VACATION responses */
522 if (msg->cm_fields['F'] != NULL) {
523 safestrncpy(my.sender, msg->cm_fields['F'], sizeof my.sender);
525 else if ( (msg->cm_fields['A'] != NULL) && (msg->cm_fields['N'] != NULL) ) {
526 snprintf(my.sender, sizeof my.sender, "%s@%s", msg->cm_fields['A'], msg->cm_fields['N']);
528 else if (msg->cm_fields['A'] != NULL) {
529 safestrncpy(my.sender, msg->cm_fields['A'], sizeof my.sender);
532 strcpy(my.sender, "");
535 /* Keep track of the subject so we can use it for VACATION responses */
536 if (msg->cm_fields['U'] != NULL) {
537 safestrncpy(my.subject, msg->cm_fields['U'], sizeof my.subject);
540 strcpy(my.subject, "");
543 /* Keep track of the envelope-from address (use body-from if not found) */
544 if (msg->cm_fields['P'] != NULL) {
545 safestrncpy(my.envelope_from, msg->cm_fields['P'], sizeof my.envelope_from);
547 else if (msg->cm_fields['F'] != NULL) {
548 safestrncpy(my.envelope_from, msg->cm_fields['F'], sizeof my.envelope_from);
551 strcpy(my.envelope_from, "");
554 /* Keep track of the envelope-to address */
555 if (msg->cm_fields['V'] != NULL) {
556 safestrncpy(my.envelope_to, msg->cm_fields['V'], sizeof my.envelope_to);
559 strcpy(my.envelope_to, "");
564 sieve2_setvalue_string(sieve2_context, "allheaders", my.rfc822headers);
566 lprintf(CTDL_DEBUG, "Calling sieve2_execute()\n");
567 res = sieve2_execute(sieve2_context, &my);
568 if (res != SIEVE2_OK) {
569 lprintf(CTDL_CRIT, "sieve2_execute() returned %d: %s\n", res, sieve2_errstr(res));
572 free(my.rfc822headers);
573 my.rfc822headers = NULL;
576 * Delete the message from the inbox unless either we were told not to, or
577 * if no other action was successfully taken.
579 if ( (!my.keep) && (my.cancel_implicit_keep) ) {
580 lprintf(CTDL_DEBUG, "keep is 0 -- deleting message from inbox\n");
581 CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, "", 0);
584 lprintf(CTDL_DEBUG, "Completed sieve processing on msg <%ld>\n", msgnum);
585 u->lastproc = msgnum;
593 * Given the on-disk representation of our Sieve config, load
594 * it into an in-memory data structure.
596 void parse_sieve_config(char *conf, struct sdm_userdata *u) {
600 struct sdm_script *sptr;
601 struct sdm_vacation *vptr;
604 while (c = ptr, ptr = bmstrcasestr(ptr, CTDLSIEVECONFIGSEPARATOR), ptr != NULL) {
606 ptr += strlen(CTDLSIEVECONFIGSEPARATOR);
608 extract_token(keyword, c, 0, '|', sizeof keyword);
610 if (!strcasecmp(keyword, "lastproc")) {
611 u->lastproc = extract_long(c, 1);
614 else if (!strcasecmp(keyword, "script")) {
615 sptr = malloc(sizeof(struct sdm_script));
616 extract_token(sptr->script_name, c, 1, '|', sizeof sptr->script_name);
617 sptr->script_active = extract_int(c, 2);
618 remove_token(c, 0, '|');
619 remove_token(c, 0, '|');
620 remove_token(c, 0, '|');
621 sptr->script_content = strdup(c);
622 sptr->next = u->first_script;
623 u->first_script = sptr;
626 else if (!strcasecmp(keyword, "vacation")) {
628 if (c != NULL) while (vacrec=c, c=strchr(c, '\n'), (c != NULL)) {
633 if (strncasecmp(vacrec, "vacation|", 9)) {
634 vptr = malloc(sizeof(struct sdm_vacation));
635 extract_token(vptr->fromaddr, vacrec, 0, '|', sizeof vptr->fromaddr);
636 vptr->timestamp = extract_long(vacrec, 1);
637 vptr->next = u->first_vacation;
638 u->first_vacation = vptr;
643 /* ignore unknown keywords */
648 * We found the Sieve configuration for this user.
649 * Now do something with it.
651 void get_sieve_config_backend(long msgnum, void *userdata) {
652 struct sdm_userdata *u = (struct sdm_userdata *) userdata;
653 struct CtdlMessage *msg;
656 u->config_msgnum = msgnum;
657 msg = CtdlFetchMessage(msgnum, 1);
659 u->config_msgnum = (-1) ;
663 conf = msg->cm_fields['M'];
664 msg->cm_fields['M'] = NULL;
665 CtdlFreeMessage(msg);
668 parse_sieve_config(conf, u);
676 * Write our citadel sieve config back to disk
678 void rewrite_ctdl_sieve_config(struct sdm_userdata *u) {
680 struct sdm_script *sptr;
681 struct sdm_vacation *vptr;
686 "Content-type: application/x-citadel-sieve-config\n"
688 CTDLSIEVECONFIGSEPARATOR
690 CTDLSIEVECONFIGSEPARATOR
695 while (u->first_script != NULL) {
696 text = realloc(text, strlen(text) + strlen(u->first_script->script_content) + 256);
697 sprintf(&text[strlen(text)], "script|%s|%d|%s" CTDLSIEVECONFIGSEPARATOR,
698 u->first_script->script_name,
699 u->first_script->script_active,
700 u->first_script->script_content
702 sptr = u->first_script;
703 u->first_script = u->first_script->next;
704 free(sptr->script_content);
708 if (u->first_vacation != NULL) {
710 size_t realloc_len = strlen(text) + 256;
711 for (vptr = u->first_vacation; vptr != NULL; vptr = vptr->next) {
712 realloc_len += strlen(vptr->fromaddr + 32);
714 text = realloc(text, realloc_len);
716 sprintf(&text[strlen(text)], "vacation|\n");
717 while (u->first_vacation != NULL) {
718 if ( (time(NULL) - u->first_vacation->timestamp) < (MAX_VACATION * 86400)) {
719 sprintf(&text[strlen(text)], "%s|%ld\n",
720 u->first_vacation->fromaddr,
721 u->first_vacation->timestamp
724 vptr = u->first_vacation;
725 u->first_vacation = u->first_vacation->next;
728 sprintf(&text[strlen(text)], CTDLSIEVECONFIGSEPARATOR);
731 /* Save the config */
732 quickie_message("Citadel", NULL, u->config_roomname,
735 "Sieve configuration"
738 /* And delete the old one */
739 if (u->config_msgnum > 0) {
740 CtdlDeleteMessages(u->config_roomname, &u->config_msgnum, 1, "", 0);
747 * This is our callback registration table for libSieve.
749 sieve2_callback_t ctdl_sieve_callbacks[] = {
750 { SIEVE2_ACTION_REJECT, ctdl_reject },
751 { SIEVE2_ACTION_VACATION, ctdl_vacation },
752 { SIEVE2_ERRCALL_PARSE, ctdl_errparse },
753 { SIEVE2_ERRCALL_RUNTIME, ctdl_errexec },
754 { SIEVE2_ACTION_FILEINTO, ctdl_fileinto },
755 { SIEVE2_ACTION_REDIRECT, ctdl_redirect },
756 { SIEVE2_ACTION_DISCARD, ctdl_discard },
757 { SIEVE2_ACTION_KEEP, ctdl_keep },
758 { SIEVE2_SCRIPT_GETSCRIPT, ctdl_getscript },
759 { SIEVE2_DEBUG_TRACE, ctdl_debug },
760 { SIEVE2_MESSAGE_GETALLHEADERS, ctdl_getheaders },
761 { SIEVE2_MESSAGE_GETSIZE, ctdl_getsize },
762 { SIEVE2_MESSAGE_GETENVELOPE, ctdl_getenvelope },
764 * These actions are unsupported by Citadel so we don't declare them.
766 { SIEVE2_ACTION_NOTIFY, ctdl_notify },
767 { SIEVE2_MESSAGE_GETSUBADDRESS, ctdl_getsubaddress },
768 { SIEVE2_MESSAGE_GETBODY, ctdl_getbody },
776 * Perform sieve processing for a single room
778 void sieve_do_room(char *roomname) {
780 struct sdm_userdata u;
781 sieve2_context_t *sieve2_context = NULL; /* Context for sieve parser */
782 int res; /* Return code from libsieve calls */
783 long orig_lastproc = 0;
785 memset(&u, 0, sizeof u);
787 /* See if the user who owns this 'mailbox' has any Sieve scripts that
790 snprintf(u.config_roomname, sizeof u.config_roomname, "%010ld.%s", atol(roomname), USERCONFIGROOM);
791 if (getroom(&CC->room, u.config_roomname) != 0) {
792 lprintf(CTDL_DEBUG, "<%s> does not exist. No processing is required.\n", u.config_roomname);
797 * Find the sieve scripts and control record and do something
799 u.config_msgnum = (-1);
800 CtdlForEachMessage(MSGS_LAST, 1, NULL, SIEVECONFIG, NULL,
801 get_sieve_config_backend, (void *)&u );
803 if (u.config_msgnum < 0) {
804 lprintf(CTDL_DEBUG, "No Sieve rules exist. No processing is required.\n");
808 lprintf(CTDL_DEBUG, "Rules found. Performing Sieve processing for <%s>\n", roomname);
810 if (getroom(&CC->room, roomname) != 0) {
811 lprintf(CTDL_CRIT, "ERROR: cannot load <%s>\n", roomname);
815 /* Initialize the Sieve parser */
817 res = sieve2_alloc(&sieve2_context);
818 if (res != SIEVE2_OK) {
819 lprintf(CTDL_CRIT, "sieve2_alloc() returned %d: %s\n", res, sieve2_errstr(res));
823 res = sieve2_callbacks(sieve2_context, ctdl_sieve_callbacks);
824 if (res != SIEVE2_OK) {
825 lprintf(CTDL_CRIT, "sieve2_callbacks() returned %d: %s\n", res, sieve2_errstr(res));
829 /* Validate the script */
831 struct ctdl_sieve my; /* dummy ctdl_sieve struct just to pass "u" slong */
832 memset(&my, 0, sizeof my);
834 res = sieve2_validate(sieve2_context, &my);
835 if (res != SIEVE2_OK) {
836 lprintf(CTDL_CRIT, "sieve2_validate() returned %d: %s\n", res, sieve2_errstr(res));
840 /* Do something useful */
841 u.sieve2_context = sieve2_context;
842 orig_lastproc = u.lastproc;
843 CtdlForEachMessage(MSGS_GT, u.lastproc, NULL, NULL, NULL,
849 res = sieve2_free(&sieve2_context);
850 if (res != SIEVE2_OK) {
851 lprintf(CTDL_CRIT, "sieve2_free() returned %d: %s\n", res, sieve2_errstr(res));
854 /* Rewrite the config if we have to */
855 if (u.lastproc > orig_lastproc) {
856 rewrite_ctdl_sieve_config(&u);
862 * Perform sieve processing for all rooms which require it
864 void perform_sieve_processing(void) {
865 struct RoomProcList *ptr = NULL;
867 if (sieve_list != NULL) {
868 lprintf(CTDL_DEBUG, "Begin Sieve processing\n");
869 while (sieve_list != NULL) {
870 char spoolroomname[ROOMNAMELEN];
871 safestrncpy(spoolroomname, sieve_list->name, sizeof spoolroomname);
872 begin_critical_section(S_SIEVELIST);
874 /* pop this record off the list */
876 sieve_list = sieve_list->next;
879 /* invalidate any duplicate entries to prevent double processing */
880 for (ptr=sieve_list; ptr!=NULL; ptr=ptr->next) {
881 if (!strcasecmp(ptr->name, spoolroomname)) {
886 end_critical_section(S_SIEVELIST);
887 if (spoolroomname[0] != 0) {
888 sieve_do_room(spoolroomname);
895 void msiv_load(struct sdm_userdata *u) {
896 char hold_rm[ROOMNAMELEN];
898 strcpy(hold_rm, CC->room.QRname); /* save current room */
900 /* Take a spin through the user's personal address book */
901 if (getroom(&CC->room, USERCONFIGROOM) == 0) {
903 u->config_msgnum = (-1);
904 strcpy(u->config_roomname, CC->room.QRname);
905 CtdlForEachMessage(MSGS_LAST, 1, NULL, SIEVECONFIG, NULL,
906 get_sieve_config_backend, (void *)u );
910 if (strcmp(CC->room.QRname, hold_rm)) {
911 getroom(&CC->room, hold_rm); /* return to saved room */
915 void msiv_store(struct sdm_userdata *u) {
916 rewrite_ctdl_sieve_config(u);
921 * Select the active script.
922 * (Set script_name to an empty string to disable all scripts)
924 * Returns 0 on success or nonzero for error.
926 int msiv_setactive(struct sdm_userdata *u, char *script_name) {
928 struct sdm_script *s;
930 /* First see if the supplied value is ok */
932 if (strlen(script_name) == 0) {
936 for (s=u->first_script; s!=NULL; s=s->next) {
937 if (!strcasecmp(s->script_name, script_name)) {
945 /* Now set the active script */
946 for (s=u->first_script; s!=NULL; s=s->next) {
947 if (!strcasecmp(s->script_name, script_name)) {
948 s->script_active = 1;
951 s->script_active = 0;
960 * Fetch a script by name.
962 * Returns NULL if the named script was not found, or a pointer to the script
963 * if it was found. NOTE: the caller does *not* own the memory returned by
964 * this function. Copy it if you need to keep it.
966 char *msiv_getscript(struct sdm_userdata *u, char *script_name) {
967 struct sdm_script *s;
969 for (s=u->first_script; s!=NULL; s=s->next) {
970 if (!strcasecmp(s->script_name, script_name)) {
971 if (s->script_content != NULL) {
972 return (s->script_content);
982 * Delete a script by name.
984 * Returns 0 if the script was deleted.
985 * 1 if the script was not found.
986 * 2 if the script cannot be deleted because it is active.
988 int msiv_deletescript(struct sdm_userdata *u, char *script_name) {
989 struct sdm_script *s = NULL;
990 struct sdm_script *script_to_delete = NULL;
992 for (s=u->first_script; s!=NULL; s=s->next) {
993 if (!strcasecmp(s->script_name, script_name)) {
994 script_to_delete = s;
995 if (s->script_active) {
1001 if (script_to_delete == NULL) return(1);
1003 if (u->first_script == script_to_delete) {
1004 u->first_script = u->first_script->next;
1006 else for (s=u->first_script; s!=NULL; s=s->next) {
1007 if (s->next == script_to_delete) {
1008 s->next = s->next->next;
1012 free(script_to_delete->script_content);
1013 free(script_to_delete);
1019 * Add or replace a new script.
1020 * NOTE: after this function returns, "u" owns the memory that "script_content"
1023 void msiv_putscript(struct sdm_userdata *u, char *script_name, char *script_content) {
1025 struct sdm_script *s, *sptr;
1027 for (s=u->first_script; s!=NULL; s=s->next) {
1028 if (!strcasecmp(s->script_name, script_name)) {
1029 if (s->script_content != NULL) {
1030 free(s->script_content);
1032 s->script_content = script_content;
1037 if (replaced == 0) {
1038 sptr = malloc(sizeof(struct sdm_script));
1039 safestrncpy(sptr->script_name, script_name, sizeof sptr->script_name);
1040 sptr->script_content = script_content;
1041 sptr->script_active = 0;
1042 sptr->next = u->first_script;
1043 u->first_script = sptr;
1050 * Citadel protocol to manage sieve scripts.
1051 * This is basically a simplified (read: doesn't resemble IMAP) version
1052 * of the 'managesieve' protocol.
1054 void cmd_msiv(char *argbuf) {
1056 struct sdm_userdata u;
1057 char script_name[256];
1058 char *script_content = NULL;
1059 struct sdm_script *s;
1062 memset(&u, 0, sizeof(struct sdm_userdata));
1064 if (CtdlAccessCheck(ac_logged_in)) return;
1065 extract_token(subcmd, argbuf, 0, '|', sizeof subcmd);
1068 if (!strcasecmp(subcmd, "putscript")) {
1069 extract_token(script_name, argbuf, 1, '|', sizeof script_name);
1070 if (strlen(script_name) > 0) {
1071 cprintf("%d Transmit script now\n", SEND_LISTING);
1072 script_content = CtdlReadMessageBody("000", config.c_maxmsglen, NULL, 0);
1073 msiv_putscript(&u, script_name, script_content);
1076 cprintf("%d Invalid script name.\n", ERROR + ILLEGAL_VALUE);
1080 else if (!strcasecmp(subcmd, "listscripts")) {
1081 cprintf("%d Scripts:\n", LISTING_FOLLOWS);
1082 for (s=u.first_script; s!=NULL; s=s->next) {
1083 if (s->script_content != NULL) {
1084 cprintf("%s|%d|\n", s->script_name, s->script_active);
1090 else if (!strcasecmp(subcmd, "setactive")) {
1091 extract_token(script_name, argbuf, 1, '|', sizeof script_name);
1092 if (msiv_setactive(&u, script_name) == 0) {
1093 cprintf("%d ok\n", CIT_OK);
1096 cprintf("%d Script '%s' does not exist.\n",
1097 ERROR + ILLEGAL_VALUE,
1103 else if (!strcasecmp(subcmd, "getscript")) {
1104 extract_token(script_name, argbuf, 1, '|', sizeof script_name);
1105 script_content = msiv_getscript(&u, script_name);
1106 if (script_content != NULL) {
1107 cprintf("%d Script:\n", LISTING_FOLLOWS);
1108 cprintf("%s000\n", script_content);
1111 cprintf("%d Invalid script name.\n", ERROR + ILLEGAL_VALUE);
1115 else if (!strcasecmp(subcmd, "deletescript")) {
1116 extract_token(script_name, argbuf, 1, '|', sizeof script_name);
1117 i = msiv_deletescript(&u, script_name);
1119 cprintf("%d ok\n", CIT_OK);
1122 cprintf("%d Script '%s' does not exist.\n",
1123 ERROR + ILLEGAL_VALUE,
1128 cprintf("%d Script '%s' is active and cannot be deleted.\n",
1129 ERROR + ILLEGAL_VALUE,
1134 cprintf("%d unknown error\n", ERROR);
1139 cprintf("%d Invalid subcommand\n", ERROR + CMD_NOT_SUPPORTED);
1147 void ctdl_sieve_init(void) {
1149 sieve2_context_t *sieve2_context = NULL;
1153 * We don't really care about dumping the entire credits to the log
1154 * every time the server is initialized. The documentation will suffice
1155 * for that purpose. We are making a call to sieve2_credits() in order
1156 * to demonstrate that we have successfully linked in to libsieve.
1158 cred = strdup(sieve2_credits());
1159 if (cred == NULL) return;
1161 if (strlen(cred) > 60) {
1162 strcpy(&cred[55], "...");
1165 lprintf(CTDL_INFO, "%s\n",cred);
1168 /* Briefly initialize a Sieve parser instance just so we can list the
1169 * extensions that are available.
1171 res = sieve2_alloc(&sieve2_context);
1172 if (res != SIEVE2_OK) {
1173 lprintf(CTDL_CRIT, "sieve2_alloc() returned %d: %s\n", res, sieve2_errstr(res));
1177 res = sieve2_callbacks(sieve2_context, ctdl_sieve_callbacks);
1178 if (res != SIEVE2_OK) {
1179 lprintf(CTDL_CRIT, "sieve2_callbacks() returned %d: %s\n", res, sieve2_errstr(res));
1183 msiv_extensions = strdup(sieve2_listextensions(sieve2_context));
1184 lprintf(CTDL_INFO, "Extensions: %s\n", msiv_extensions);
1186 BAIL: res = sieve2_free(&sieve2_context);
1187 if (res != SIEVE2_OK) {
1188 lprintf(CTDL_CRIT, "sieve2_free() returned %d: %s\n", res, sieve2_errstr(res));
1195 char *serv_sieve_init(void)
1198 CtdlRegisterProtoHook(cmd_msiv, "MSIV", "Manage Sieve scripts");
1199 return "$Id: serv_sieve.c 3850 2005-09-13 14:00:24Z ajc $";
1202 #else /* HAVE_LIBSIEVE */
1204 char *serv_sieve_init(void)
1206 lprintf(CTDL_INFO, "This server is missing libsieve. Mailbox filtering will be disabled.\n");
1207 return "$Id: serv_sieve.c 3850 2005-09-13 14:00:24Z ajc $";
1210 #endif /* HAVE_LIBSIEVE */