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) {
65 lprintf(CTDL_DEBUG, "Sieve: level [%d] module [%s] file [%s] function [%s]\n",
66 sieve2_getvalue_int(s, "level"),
67 sieve2_getvalue_string(s, "module"),
68 sieve2_getvalue_string(s, "file"),
69 sieve2_getvalue_string(s, "function"));
70 lprintf(CTDL_DEBUG, " message [%s]\n",
71 sieve2_getvalue_string(s, "message"));
78 * Callback function to log script parsing errors
80 int ctdl_errparse(sieve2_context_t *s, void *my)
82 lprintf(CTDL_WARNING, "Error in script, line %d: %s\n",
83 sieve2_getvalue_int(s, "lineno"),
84 sieve2_getvalue_string(s, "message")
91 * Callback function to log script execution errors
93 int ctdl_errexec(sieve2_context_t *s, void *my)
95 lprintf(CTDL_WARNING, "Error executing script: %s\n",
96 sieve2_getvalue_string(s, "message")
103 * Callback function to redirect a message to a different folder
105 int ctdl_redirect(sieve2_context_t *s, void *my)
107 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
108 struct CtdlMessage *msg = NULL;
109 struct recptypes *valid = NULL;
112 safestrncpy(recp, sieve2_getvalue_string(s, "address"), sizeof recp);
114 lprintf(CTDL_DEBUG, "Action is REDIRECT, recipient <%s>\n", recp);
116 valid = validate_recipients(recp);
118 lprintf(CTDL_WARNING, "REDIRECT failed: bad recipient <%s>\n", recp);
119 return SIEVE2_ERROR_BADARGS;
121 if (valid->num_error > 0) {
122 lprintf(CTDL_WARNING, "REDIRECT failed: bad recipient <%s>\n", recp);
124 return SIEVE2_ERROR_BADARGS;
127 msg = CtdlFetchMessage(cs->msgnum, 1);
129 lprintf(CTDL_WARNING, "REDIRECT failed: unable to fetch msg %ld\n", cs->msgnum);
131 return SIEVE2_ERROR_BADARGS;
134 CtdlSubmitMsg(msg, valid, NULL);
135 cs->cancel_implicit_keep = 1;
137 CtdlFreeMessage(msg);
143 * Callback function to indicate that a message *will* be kept in the inbox
145 int ctdl_keep(sieve2_context_t *s, void *my)
147 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
149 lprintf(CTDL_DEBUG, "Action is KEEP\n");
152 cs->cancel_implicit_keep = 1;
158 * Callback function to file a message into a different mailbox
160 int ctdl_fileinto(sieve2_context_t *s, void *my)
162 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
163 const char *dest_folder = sieve2_getvalue_string(s, "mailbox");
165 char foldername[256];
166 char original_room_name[ROOMNAMELEN];
168 lprintf(CTDL_DEBUG, "Action is FILEINTO, destination is <%s>\n", dest_folder);
170 /* FILEINTO 'INBOX' is the same thing as KEEP */
171 if ( (!strcasecmp(dest_folder, "INBOX")) || (!strcasecmp(dest_folder, MAILROOM)) ) {
173 cs->cancel_implicit_keep = 1;
177 /* Remember what room we came from */
178 safestrncpy(original_room_name, CC->room.QRname, sizeof original_room_name);
180 /* First try a mailbox name match (check personal mail folders first) */
181 snprintf(foldername, sizeof foldername, "%010ld.%s", cs->usernum, dest_folder);
182 c = getroom(&CC->room, foldername);
184 /* Then a regular room name match (public and private rooms) */
186 safestrncpy(foldername, dest_folder, sizeof foldername);
187 c = getroom(&CC->room, foldername);
191 lprintf(CTDL_WARNING, "FILEINTO failed: target <%s> does not exist\n", dest_folder);
192 return SIEVE2_ERROR_BADARGS;
195 /* Yes, we actually have to go there */
196 usergoto(NULL, 0, 0, NULL, NULL);
198 c = CtdlSaveMsgPointersInRoom(NULL, &cs->msgnum, 1, 0, NULL);
200 /* Go back to the room we came from */
201 if (strcasecmp(original_room_name, CC->room.QRname)) {
202 usergoto(original_room_name, 0, 0, NULL, NULL);
206 cs->cancel_implicit_keep = 1;
210 return SIEVE2_ERROR_BADARGS;
216 * Callback function to indicate that a message should be discarded.
218 int ctdl_discard(sieve2_context_t *s, void *my)
220 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
222 lprintf(CTDL_DEBUG, "Action is DISCARD\n");
224 /* Yes, this is really all there is to it. Since we are not setting "keep" to 1,
225 * the message will be discarded because "some other action" was successfully taken.
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 */
273 "Delivery status notification"
277 cs->cancel_implicit_keep = 1;
284 * Callback function to indicate that a vacation message should be generated
285 * FIXME implement this
287 int ctdl_vacation(sieve2_context_t *s, void *my)
289 lprintf(CTDL_DEBUG, "Action is VACATION\n");
290 return SIEVE2_ERROR_UNSUPPORTED;
295 * Callback function to parse addresses per local system convention
296 * It is disabled because we don't support subaddresses.
299 int ctdl_getsubaddress(sieve2_context_t *s, void *my)
301 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
303 /* libSieve does not take ownership of the memory used here. But, since we
304 * are just pointing to locations inside a struct which we are going to free
307 sieve2_setvalue_string(s, "user", cs->recp_user);
308 sieve2_setvalue_string(s, "detail", "");
309 sieve2_setvalue_string(s, "localpart", cs->recp_user);
310 sieve2_setvalue_string(s, "domain", cs->recp_node);
317 * Callback function to parse message envelope
318 * FIXME implement this
320 int ctdl_getenvelope(sieve2_context_t *s, void *my)
322 return SIEVE2_ERROR_UNSUPPORTED;
327 * Callback function to fetch message body
328 * FIXME implement this
330 int ctdl_getbody(sieve2_context_t *s, void *my)
332 return SIEVE2_ERROR_UNSUPPORTED;
337 * Callback function to fetch message size
339 int ctdl_getsize(sieve2_context_t *s, void *my)
341 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
344 GetMetaData(&smi, cs->msgnum);
346 if (smi.meta_rfc822_length > 0L) {
347 sieve2_setvalue_int(s, "size", (int)smi.meta_rfc822_length);
351 return SIEVE2_ERROR_UNSUPPORTED;
356 * Callback function to retrieve the sieve script
358 int ctdl_getscript(sieve2_context_t *s, void *my) {
359 struct sdm_script *sptr;
360 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
362 for (sptr=cs->u->first_script; sptr!=NULL; sptr=sptr->next) {
363 if (sptr->script_active > 0) {
364 lprintf(CTDL_DEBUG, "ctdl_getscript() is using script '%s'\n", sptr->script_name);
365 sieve2_setvalue_string(s, "script", sptr->script_content);
370 lprintf(CTDL_DEBUG, "ctdl_getscript() found no active script\n");
371 return SIEVE2_ERROR_GETSCRIPT;
375 * Callback function to retrieve message headers
377 int ctdl_getheaders(sieve2_context_t *s, void *my) {
379 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
381 lprintf(CTDL_DEBUG, "ctdl_getheaders() was called\n");
383 sieve2_setvalue_string(s, "allheaders", cs->rfc822headers);
390 * Add a room to the list of those rooms which potentially require sieve processing
392 void sieve_queue_room(struct ctdlroom *which_room) {
393 struct RoomProcList *ptr;
395 ptr = (struct RoomProcList *) malloc(sizeof (struct RoomProcList));
396 if (ptr == NULL) return;
398 safestrncpy(ptr->name, which_room->QRname, sizeof ptr->name);
399 begin_critical_section(S_SIEVELIST);
400 ptr->next = sieve_list;
402 end_critical_section(S_SIEVELIST);
408 * Perform sieve processing for one message (called by sieve_do_room() for each message)
410 void sieve_do_msg(long msgnum, void *userdata) {
411 struct sdm_userdata *u = (struct sdm_userdata *) userdata;
412 sieve2_context_t *sieve2_context = u->sieve2_context;
413 struct ctdl_sieve my;
415 struct CtdlMessage *msg;
417 lprintf(CTDL_DEBUG, "Performing sieve processing on msg <%ld>\n", msgnum);
419 msg = CtdlFetchMessage(msgnum, 0);
420 if (msg == NULL) return;
422 CC->redirect_buffer = malloc(SIZ);
423 CC->redirect_len = 0;
424 CC->redirect_alloc = SIZ;
425 CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ONLY, 0, 1);
426 my.rfc822headers = CC->redirect_buffer;
427 CC->redirect_buffer = NULL;
428 CC->redirect_len = 0;
429 CC->redirect_alloc = 0;
431 my.keep = 0; /* Set to 1 to declare an *explicit* keep */
432 my.cancel_implicit_keep = 0; /* Some actions will cancel the implicit keep */
433 my.usernum = atol(CC->room.QRname); /* Keep track of the owner of the room's namespace */
434 my.msgnum = msgnum; /* Keep track of the message number in our local store */
435 my.u = u; /* Hand off a pointer to the rest of this info */
437 /* Keep track of the recipient so we can do handling based on it later */
438 process_rfc822_addr(msg->cm_fields['R'], my.recp_user, my.recp_node, my.recp_name);
440 /* Keep track of the sender so we can use it for REJECT and VACATION responses */
441 if (msg->cm_fields['F'] != NULL) {
442 safestrncpy(my.sender, msg->cm_fields['F'], sizeof my.sender);
444 else if ( (msg->cm_fields['A'] != NULL) && (msg->cm_fields['N'] != NULL) ) {
445 snprintf(my.sender, sizeof my.sender, "%s@%s", msg->cm_fields['A'], msg->cm_fields['N']);
447 else if (msg->cm_fields['A'] != NULL) {
448 safestrncpy(my.sender, msg->cm_fields['A'], sizeof my.sender);
451 strcpy(my.sender, "");
456 sieve2_setvalue_string(sieve2_context, "allheaders", my.rfc822headers);
458 lprintf(CTDL_DEBUG, "Calling sieve2_execute()\n");
459 res = sieve2_execute(sieve2_context, &my);
460 if (res != SIEVE2_OK) {
461 lprintf(CTDL_CRIT, "sieve2_execute() returned %d: %s\n", res, sieve2_errstr(res));
464 free(my.rfc822headers);
465 my.rfc822headers = NULL;
468 * Delete the message from the inbox unless either we were told not to, or
469 * if no other action was successfully taken.
471 if ( (!my.keep) && (my.cancel_implicit_keep) ) {
472 lprintf(CTDL_DEBUG, "keep is 0 -- deleting message from inbox\n");
473 CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, "", 0);
476 lprintf(CTDL_DEBUG, "Completed sieve processing on msg <%ld>\n", msgnum);
477 u->lastproc = msgnum;
485 * Given the on-disk representation of our Sieve config, load
486 * it into an in-memory data structure.
488 void parse_sieve_config(char *conf, struct sdm_userdata *u) {
492 struct sdm_script *sptr;
495 while (c = ptr, ptr = bmstrcasestr(ptr, CTDLSIEVECONFIGSEPARATOR), ptr != NULL) {
497 ptr += strlen(CTDLSIEVECONFIGSEPARATOR);
499 extract_token(keyword, c, 0, '|', sizeof keyword);
501 if (!strcasecmp(keyword, "lastproc")) {
502 u->lastproc = extract_long(c, 1);
505 else if (!strcasecmp(keyword, "script")) {
506 sptr = malloc(sizeof(struct sdm_script));
507 extract_token(sptr->script_name, c, 1, '|', sizeof sptr->script_name);
508 sptr->script_active = extract_int(c, 2);
509 remove_token(c, 0, '|');
510 remove_token(c, 0, '|');
511 remove_token(c, 0, '|');
512 sptr->script_content = strdup(c);
513 sptr->next = u->first_script;
514 u->first_script = sptr;
517 /* ignore unknown keywords */
522 * We found the Sieve configuration for this user.
523 * Now do something with it.
525 void get_sieve_config_backend(long msgnum, void *userdata) {
526 struct sdm_userdata *u = (struct sdm_userdata *) userdata;
527 struct CtdlMessage *msg;
530 u->config_msgnum = msgnum;
531 msg = CtdlFetchMessage(msgnum, 1);
533 u->config_msgnum = (-1) ;
537 conf = msg->cm_fields['M'];
538 msg->cm_fields['M'] = NULL;
539 CtdlFreeMessage(msg);
542 parse_sieve_config(conf, u);
550 * Write our citadel sieve config back to disk
552 void rewrite_ctdl_sieve_config(struct sdm_userdata *u) {
554 struct sdm_script *sptr;
559 "Content-type: application/x-citadel-sieve-config\n"
561 CTDLSIEVECONFIGSEPARATOR
563 CTDLSIEVECONFIGSEPARATOR
568 while (u->first_script != NULL) {
569 text = realloc(text, strlen(text) + strlen(u->first_script->script_content) + 256);
570 sprintf(&text[strlen(text)], "script|%s|%d|%s" CTDLSIEVECONFIGSEPARATOR,
571 u->first_script->script_name,
572 u->first_script->script_active,
573 u->first_script->script_content
575 sptr = u->first_script;
576 u->first_script = u->first_script->next;
577 free(sptr->script_content);
581 /* Save the config */
582 quickie_message("Citadel", NULL, u->config_roomname,
585 "Sieve configuration"
588 /* And delete the old one */
589 if (u->config_msgnum > 0) {
590 CtdlDeleteMessages(u->config_roomname, &u->config_msgnum, 1, "", 0);
597 * This is our callback registration table for libSieve.
599 sieve2_callback_t ctdl_sieve_callbacks[] = {
600 { SIEVE2_ACTION_REJECT, ctdl_reject },
601 { SIEVE2_ACTION_NOTIFY, NULL /* ctdl_notify */ },
602 { SIEVE2_ACTION_VACATION, ctdl_vacation },
603 { SIEVE2_ERRCALL_PARSE, ctdl_errparse },
604 { SIEVE2_ERRCALL_RUNTIME, ctdl_errexec },
605 { SIEVE2_ACTION_FILEINTO, ctdl_fileinto },
606 { SIEVE2_ACTION_REDIRECT, ctdl_redirect },
607 { SIEVE2_ACTION_DISCARD, ctdl_discard },
608 { SIEVE2_ACTION_KEEP, ctdl_keep },
609 { SIEVE2_SCRIPT_GETSCRIPT, ctdl_getscript },
610 { SIEVE2_DEBUG_TRACE, ctdl_debug },
611 { SIEVE2_MESSAGE_GETALLHEADERS, ctdl_getheaders },
612 { SIEVE2_MESSAGE_GETSUBADDRESS, NULL /* ctdl_getsubaddress */ },
613 { SIEVE2_MESSAGE_GETENVELOPE, ctdl_getenvelope },
614 { SIEVE2_MESSAGE_GETBODY, ctdl_getbody },
615 { SIEVE2_MESSAGE_GETSIZE, ctdl_getsize },
621 * Perform sieve processing for a single room
623 void sieve_do_room(char *roomname) {
625 struct sdm_userdata u;
626 sieve2_context_t *sieve2_context = NULL; /* Context for sieve parser */
627 int res; /* Return code from libsieve calls */
628 long orig_lastproc = 0;
630 memset(&u, 0, sizeof u);
632 /* See if the user who owns this 'mailbox' has any Sieve scripts that
635 snprintf(u.config_roomname, sizeof u.config_roomname, "%010ld.%s", atol(roomname), SIEVERULES);
636 if (getroom(&CC->room, u.config_roomname) != 0) {
637 lprintf(CTDL_DEBUG, "<%s> does not exist. No processing is required.\n", u.config_roomname);
642 * Find the sieve scripts and control record and do something
644 u.config_msgnum = (-1);
645 CtdlForEachMessage(MSGS_LAST, 1, NULL, SIEVECONFIG, NULL,
646 get_sieve_config_backend, (void *)&u );
648 if (u.config_msgnum < 0) {
649 lprintf(CTDL_DEBUG, "No Sieve rules exist. No processing is required.\n");
653 lprintf(CTDL_DEBUG, "Rules found. Performing Sieve processing for <%s>\n", roomname);
655 if (getroom(&CC->room, roomname) != 0) {
656 lprintf(CTDL_CRIT, "ERROR: cannot load <%s>\n", roomname);
660 /* Initialize the Sieve parser */
662 res = sieve2_alloc(&sieve2_context);
663 if (res != SIEVE2_OK) {
664 lprintf(CTDL_CRIT, "sieve2_alloc() returned %d: %s\n", res, sieve2_errstr(res));
668 res = sieve2_callbacks(sieve2_context, ctdl_sieve_callbacks);
669 if (res != SIEVE2_OK) {
670 lprintf(CTDL_CRIT, "sieve2_callbacks() returned %d: %s\n", res, sieve2_errstr(res));
674 /* Validate the script */
676 struct ctdl_sieve my; /* dummy ctdl_sieve struct just to pass "u" slong */
677 memset(&my, 0, sizeof my);
679 res = sieve2_validate(sieve2_context, &my);
680 if (res != SIEVE2_OK) {
681 lprintf(CTDL_CRIT, "sieve2_validate() returned %d: %s\n", res, sieve2_errstr(res));
685 /* Do something useful */
686 u.sieve2_context = sieve2_context;
687 orig_lastproc = u.lastproc;
688 CtdlForEachMessage(MSGS_GT, u.lastproc, NULL, NULL, NULL,
694 res = sieve2_free(&sieve2_context);
695 if (res != SIEVE2_OK) {
696 lprintf(CTDL_CRIT, "sieve2_free() returned %d: %s\n", res, sieve2_errstr(res));
699 /* Rewrite the config if we have to */
700 if (u.lastproc > orig_lastproc) {
701 rewrite_ctdl_sieve_config(&u);
707 * Perform sieve processing for all rooms which require it
709 void perform_sieve_processing(void) {
710 struct RoomProcList *ptr = NULL;
712 if (sieve_list != NULL) {
713 lprintf(CTDL_DEBUG, "Begin Sieve processing\n");
714 while (sieve_list != NULL) {
715 char spoolroomname[ROOMNAMELEN];
716 safestrncpy(spoolroomname, sieve_list->name, sizeof spoolroomname);
717 begin_critical_section(S_SIEVELIST);
719 /* pop this record off the list */
721 sieve_list = sieve_list->next;
724 /* invalidate any duplicate entries to prevent double processing */
725 for (ptr=sieve_list; ptr!=NULL; ptr=ptr->next) {
726 if (!strcasecmp(ptr->name, spoolroomname)) {
731 end_critical_section(S_SIEVELIST);
732 if (spoolroomname[0] != 0) {
733 sieve_do_room(spoolroomname);
740 void msiv_load(struct sdm_userdata *u) {
741 char hold_rm[ROOMNAMELEN];
743 strcpy(hold_rm, CC->room.QRname); /* save current room */
745 /* Take a spin through the user's personal address book */
746 if (getroom(&CC->room, SIEVERULES) == 0) {
748 u->config_msgnum = (-1);
749 strcpy(u->config_roomname, CC->room.QRname);
750 CtdlForEachMessage(MSGS_LAST, 1, NULL, SIEVECONFIG, NULL,
751 get_sieve_config_backend, (void *)u );
755 if (strcmp(CC->room.QRname, hold_rm)) {
756 getroom(&CC->room, hold_rm); /* return to saved room */
760 void msiv_store(struct sdm_userdata *u) {
761 rewrite_ctdl_sieve_config(u);
766 * Select the active script.
767 * (Set script_name to an empty string to disable all scripts)
769 * Returns 0 on success or nonzero for error.
771 int msiv_setactive(struct sdm_userdata *u, char *script_name) {
773 struct sdm_script *s;
775 /* First see if the supplied value is ok */
777 if (strlen(script_name) == 0) {
781 for (s=u->first_script; s!=NULL; s=s->next) {
782 if (!strcasecmp(s->script_name, script_name)) {
790 /* Now set the active script */
791 for (s=u->first_script; s!=NULL; s=s->next) {
792 if (!strcasecmp(s->script_name, script_name)) {
793 s->script_active = 1;
796 s->script_active = 0;
805 * Fetch a script by name.
807 * Returns NULL if the named script was not found, or a pointer to the script
808 * if it was found. NOTE: the caller does *not* own the memory returned by
809 * this function. Copy it if you need to keep it.
811 char *msiv_getscript(struct sdm_userdata *u, char *script_name) {
812 struct sdm_script *s;
814 for (s=u->first_script; s!=NULL; s=s->next) {
815 if (!strcasecmp(s->script_name, script_name)) {
816 if (s->script_content != NULL) {
817 return (s->script_content);
827 * Delete a script by name.
829 * Returns 0 if the script was deleted.
830 * 1 if the script was not found.
831 * 2 if the script cannot be deleted because it is active.
833 int msiv_deletescript(struct sdm_userdata *u, char *script_name) {
834 struct sdm_script *s = NULL;
835 struct sdm_script *script_to_delete = NULL;
837 for (s=u->first_script; s!=NULL; s=s->next) {
838 if (!strcasecmp(s->script_name, script_name)) {
839 script_to_delete = s;
840 if (s->script_active) {
846 if (script_to_delete == NULL) return(1);
848 if (u->first_script == script_to_delete) {
849 u->first_script = u->first_script->next;
851 else for (s=u->first_script; s!=NULL; s=s->next) {
852 if (s->next == script_to_delete) {
853 s->next = s->next->next;
857 free(script_to_delete->script_content);
858 free(script_to_delete);
864 * Add or replace a new script.
865 * NOTE: after this function returns, "u" owns the memory that "script_content"
868 void msiv_putscript(struct sdm_userdata *u, char *script_name, char *script_content) {
870 struct sdm_script *s, *sptr;
872 for (s=u->first_script; s!=NULL; s=s->next) {
873 if (!strcasecmp(s->script_name, script_name)) {
874 if (s->script_content != NULL) {
875 free(s->script_content);
877 s->script_content = script_content;
883 sptr = malloc(sizeof(struct sdm_script));
884 safestrncpy(sptr->script_name, script_name, sizeof sptr->script_name);
885 sptr->script_content = script_content;
886 sptr->script_active = 0;
887 sptr->next = u->first_script;
888 u->first_script = sptr;
895 * Citadel protocol to manage sieve scripts.
896 * This is basically a simplified (read: doesn't resemble IMAP) version
897 * of the 'managesieve' protocol.
899 void cmd_msiv(char *argbuf) {
901 struct sdm_userdata u;
902 char script_name[256];
903 char *script_content = NULL;
904 struct sdm_script *s;
907 memset(&u, 0, sizeof(struct sdm_userdata));
909 if (CtdlAccessCheck(ac_logged_in)) return;
910 extract_token(subcmd, argbuf, 0, '|', sizeof subcmd);
913 if (!strcasecmp(subcmd, "putscript")) {
914 extract_token(script_name, argbuf, 1, '|', sizeof script_name);
915 if (strlen(script_name) > 0) {
916 cprintf("%d Transmit script now\n", SEND_LISTING);
917 script_content = CtdlReadMessageBody("000", config.c_maxmsglen, NULL, 0);
918 msiv_putscript(&u, script_name, script_content);
921 cprintf("%d Invalid script name.\n", ERROR + ILLEGAL_VALUE);
925 else if (!strcasecmp(subcmd, "listscripts")) {
926 cprintf("%d Scripts:\n", LISTING_FOLLOWS);
927 for (s=u.first_script; s!=NULL; s=s->next) {
928 if (s->script_content != NULL) {
929 cprintf("%s|%d|\n", s->script_name, s->script_active);
935 else if (!strcasecmp(subcmd, "setactive")) {
936 extract_token(script_name, argbuf, 1, '|', sizeof script_name);
937 if (msiv_setactive(&u, script_name) == 0) {
938 cprintf("%d ok\n", CIT_OK);
941 cprintf("%d Script '%s' does not exist.\n",
942 ERROR + ILLEGAL_VALUE,
948 else if (!strcasecmp(subcmd, "getscript")) {
949 extract_token(script_name, argbuf, 1, '|', sizeof script_name);
950 script_content = msiv_getscript(&u, script_name);
951 if (script_content != NULL) {
952 cprintf("%d Script:\n", SEND_LISTING);
953 cprintf("%s000\n", script_content);
956 cprintf("%d Invalid script name.\n", ERROR + ILLEGAL_VALUE);
960 else if (!strcasecmp(subcmd, "deletescript")) {
961 extract_token(script_name, argbuf, 1, '|', sizeof script_name);
962 i = msiv_deletescript(&u, script_name);
964 cprintf("%d ok\n", CIT_OK);
967 cprintf("%d Script '%s' does not exist.\n",
968 ERROR + ILLEGAL_VALUE,
973 cprintf("%d Script '%s' is active and cannot be deleted.\n",
974 ERROR + ILLEGAL_VALUE,
979 cprintf("%d unknown error\n", ERROR);
984 cprintf("%d Invalid subcommand\n", ERROR + CMD_NOT_SUPPORTED);
992 void ctdl_sieve_init(void) {
994 sieve2_context_t *sieve2_context = NULL;
998 * We don't really care about dumping the entire credits to the log
999 * every time the server is initialized. The documentation will suffice
1000 * for that purpose. We are making a call to sieve2_credits() in order
1001 * to demonstrate that we have successfully linked in to libsieve.
1003 cred = strdup(sieve2_credits());
1004 if (cred == NULL) return;
1006 if (strlen(cred) > 60) {
1007 strcpy(&cred[55], "...");
1010 lprintf(CTDL_INFO, "%s\n",cred);
1013 /* Briefly initialize a Sieve parser instance just so we can list the
1014 * extensions that are available.
1016 res = sieve2_alloc(&sieve2_context);
1017 if (res != SIEVE2_OK) {
1018 lprintf(CTDL_CRIT, "sieve2_alloc() returned %d: %s\n", res, sieve2_errstr(res));
1022 res = sieve2_callbacks(sieve2_context, ctdl_sieve_callbacks);
1023 if (res != SIEVE2_OK) {
1024 lprintf(CTDL_CRIT, "sieve2_callbacks() returned %d: %s\n", res, sieve2_errstr(res));
1028 msiv_extensions = strdup(sieve2_listextensions(sieve2_context));
1029 lprintf(CTDL_INFO, "Extensions: %s\n", msiv_extensions);
1031 BAIL: res = sieve2_free(&sieve2_context);
1032 if (res != SIEVE2_OK) {
1033 lprintf(CTDL_CRIT, "sieve2_free() returned %d: %s\n", res, sieve2_errstr(res));
1040 char *serv_sieve_init(void)
1043 CtdlRegisterProtoHook(cmd_msiv, "MSIV", "Manage Sieve scripts");
1044 return "$Id: serv_sieve.c 3850 2005-09-13 14:00:24Z ajc $";
1047 #else /* HAVE_LIBSIEVE */
1049 char *serv_sieve_init(void)
1051 lprintf(CTDL_INFO, "This server is missing libsieve. Mailbox filtering will be disabled.\n");
1052 return "$Id: serv_sieve.c 3850 2005-09-13 14:00:24Z ajc $";
1055 #endif /* HAVE_LIBSIEVE */