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 "citserver.h"
43 #include "internet_addressing.h"
47 #include "ctdl_module.h"
52 #include "serv_sieve.h"
54 struct RoomProcList *sieve_list = NULL;
55 char *msiv_extensions = NULL;
59 * Callback function to send libSieve trace messages to Citadel log facility
61 int ctdl_debug(sieve2_context_t *s, void *my)
63 lprintf(CTDL_DEBUG, "Sieve: %s\n", sieve2_getvalue_string(s, "message"));
69 * Callback function to log script parsing errors
71 int ctdl_errparse(sieve2_context_t *s, void *my)
73 lprintf(CTDL_WARNING, "Error in script, line %d: %s\n",
74 sieve2_getvalue_int(s, "lineno"),
75 sieve2_getvalue_string(s, "message")
82 * Callback function to log script execution errors
84 int ctdl_errexec(sieve2_context_t *s, void *my)
86 lprintf(CTDL_WARNING, "Error executing script: %s\n",
87 sieve2_getvalue_string(s, "message")
94 * Callback function to redirect a message to a different folder
96 int ctdl_redirect(sieve2_context_t *s, void *my)
98 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
99 struct CtdlMessage *msg = NULL;
100 struct recptypes *valid = NULL;
103 safestrncpy(recp, sieve2_getvalue_string(s, "address"), sizeof recp);
105 lprintf(CTDL_DEBUG, "Action is REDIRECT, recipient <%s>\n", recp);
107 valid = validate_recipients(recp);
109 lprintf(CTDL_WARNING, "REDIRECT failed: bad recipient <%s>\n", recp);
110 return SIEVE2_ERROR_BADARGS;
112 if (valid->num_error > 0) {
113 lprintf(CTDL_WARNING, "REDIRECT failed: bad recipient <%s>\n", recp);
114 free_recipients(valid);
115 return SIEVE2_ERROR_BADARGS;
118 msg = CtdlFetchMessage(cs->msgnum, 1);
120 lprintf(CTDL_WARNING, "REDIRECT failed: unable to fetch msg %ld\n", cs->msgnum);
121 free_recipients(valid);
122 return SIEVE2_ERROR_BADARGS;
125 CtdlSubmitMsg(msg, valid, NULL);
126 cs->cancel_implicit_keep = 1;
127 free_recipients(valid);
128 CtdlFreeMessage(msg);
134 * Callback function to indicate that a message *will* be kept in the inbox
136 int ctdl_keep(sieve2_context_t *s, void *my)
138 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
140 lprintf(CTDL_DEBUG, "Action is KEEP\n");
143 cs->cancel_implicit_keep = 1;
149 * Callback function to file a message into a different mailbox
151 int ctdl_fileinto(sieve2_context_t *s, void *my)
153 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
154 const char *dest_folder = sieve2_getvalue_string(s, "mailbox");
156 char foldername[256];
157 char original_room_name[ROOMNAMELEN];
159 lprintf(CTDL_DEBUG, "Action is FILEINTO, destination is <%s>\n", dest_folder);
161 /* FILEINTO 'INBOX' is the same thing as KEEP */
162 if ( (!strcasecmp(dest_folder, "INBOX")) || (!strcasecmp(dest_folder, MAILROOM)) ) {
164 cs->cancel_implicit_keep = 1;
168 /* Remember what room we came from */
169 safestrncpy(original_room_name, CC->room.QRname, sizeof original_room_name);
171 /* First try a mailbox name match (check personal mail folders first) */
172 snprintf(foldername, sizeof foldername, "%010ld.%s", cs->usernum, dest_folder);
173 c = getroom(&CC->room, foldername);
175 /* Then a regular room name match (public and private rooms) */
177 safestrncpy(foldername, dest_folder, sizeof foldername);
178 c = getroom(&CC->room, foldername);
182 lprintf(CTDL_WARNING, "FILEINTO failed: target <%s> does not exist\n", dest_folder);
183 return SIEVE2_ERROR_BADARGS;
186 /* Yes, we actually have to go there */
187 usergoto(NULL, 0, 0, NULL, NULL);
189 c = CtdlSaveMsgPointersInRoom(NULL, &cs->msgnum, 1, 0, NULL);
191 /* Go back to the room we came from */
192 if (strcasecmp(original_room_name, CC->room.QRname)) {
193 usergoto(original_room_name, 0, 0, NULL, NULL);
197 cs->cancel_implicit_keep = 1;
201 return SIEVE2_ERROR_BADARGS;
207 * Callback function to indicate that a message should be discarded.
209 int ctdl_discard(sieve2_context_t *s, void *my)
211 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
213 lprintf(CTDL_DEBUG, "Action is DISCARD\n");
215 /* Cancel the implicit keep. That's all there is to it. */
216 cs->cancel_implicit_keep = 1;
223 * Callback function to indicate that a message should be rejected
225 int ctdl_reject(sieve2_context_t *s, void *my)
227 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
228 char *reject_text = NULL;
230 lprintf(CTDL_DEBUG, "Action is REJECT\n");
232 /* If we don't know who sent the message, do a DISCARD instead. */
233 if (IsEmptyStr(cs->sender)) {
234 lprintf(CTDL_INFO, "Unknown sender. Doing DISCARD instead of REJECT.\n");
235 return ctdl_discard(s, my);
238 /* Assemble the reject message. */
239 reject_text = malloc(strlen(sieve2_getvalue_string(s, "message")) + 1024);
240 if (reject_text == NULL) {
241 return SIEVE2_ERROR_FAIL;
245 "Content-type: text/plain\n"
247 "The message was refused by the recipient's mail filtering program.\n"
248 "The reason given was as follows:\n"
253 sieve2_getvalue_string(s, "message")
256 quickie_message( /* This delivers the message */
263 "Delivery status notification"
267 cs->cancel_implicit_keep = 1;
274 * Callback function to indicate that a vacation message should be generated
276 int ctdl_vacation(sieve2_context_t *s, void *my)
278 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
279 struct sdm_vacation *vptr;
282 char *vacamsg_text = NULL;
283 char vacamsg_subject[1024];
285 lprintf(CTDL_DEBUG, "Action is VACATION\n");
287 message = sieve2_getvalue_string(s, "message");
288 if (message == NULL) return SIEVE2_ERROR_BADARGS;
290 if (sieve2_getvalue_string(s, "subject") != NULL) {
291 safestrncpy(vacamsg_subject, sieve2_getvalue_string(s, "subject"), sizeof vacamsg_subject);
294 snprintf(vacamsg_subject, sizeof vacamsg_subject, "Re: %s", cs->subject);
297 days = sieve2_getvalue_int(s, "days");
298 if (days < 1) days = 1;
299 if (days > MAX_VACATION) days = MAX_VACATION;
301 /* Check to see whether we've already alerted this sender that we're on vacation. */
302 for (vptr = cs->u->first_vacation; vptr != NULL; vptr = vptr->next) {
303 if (!strcasecmp(vptr->fromaddr, cs->sender)) {
304 if ( (time(NULL) - vptr->timestamp) < (days * 86400) ) {
305 lprintf(CTDL_DEBUG, "Already alerted <%s> recently.\n", cs->sender);
311 /* Assemble the reject message. */
312 vacamsg_text = malloc(strlen(message) + 1024);
313 if (vacamsg_text == NULL) {
314 return SIEVE2_ERROR_FAIL;
317 sprintf(vacamsg_text,
318 "Content-type: text/plain\n"
326 quickie_message( /* This delivers the message */
338 /* Now update the list to reflect the fact that we've alerted this sender.
339 * If they're already in the list, just update the timestamp.
341 for (vptr = cs->u->first_vacation; vptr != NULL; vptr = vptr->next) {
342 if (!strcasecmp(vptr->fromaddr, cs->sender)) {
343 vptr->timestamp = time(NULL);
348 /* If we get to this point, create a new record.
350 vptr = malloc(sizeof(struct sdm_vacation));
351 vptr->timestamp = time(NULL);
352 safestrncpy(vptr->fromaddr, cs->sender, sizeof vptr->fromaddr);
353 vptr->next = cs->u->first_vacation;
354 cs->u->first_vacation = vptr;
361 * Callback function to parse addresses per local system convention
362 * It is disabled because we don't support subaddresses.
365 int ctdl_getsubaddress(sieve2_context_t *s, void *my)
367 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
369 /* libSieve does not take ownership of the memory used here. But, since we
370 * are just pointing to locations inside a struct which we are going to free
373 sieve2_setvalue_string(s, "user", cs->recp_user);
374 sieve2_setvalue_string(s, "detail", "");
375 sieve2_setvalue_string(s, "localpart", cs->recp_user);
376 sieve2_setvalue_string(s, "domain", cs->recp_node);
383 * Callback function to parse message envelope
385 int ctdl_getenvelope(sieve2_context_t *s, void *my)
387 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
389 lprintf(CTDL_DEBUG, "Action is GETENVELOPE\nEnvFrom: %s\n EnvTo: %s\n",
390 cs->envelope_from, cs->envelope_to);
392 if (cs->envelope_from != NULL) {
393 if ((cs->envelope_from[0] != '@')&&(cs->envelope_from[strlen(cs->envelope_from)-1] != '@')) {
394 sieve2_setvalue_string(s, "from", cs->envelope_from);
397 sieve2_setvalue_string(s, "from", "invalid_envelope_from@example.org");
401 sieve2_setvalue_string(s, "from", "null_envelope_from@example.org");
405 if (cs->envelope_to != NULL) {
406 if ((cs->envelope_to[0] != '@') && (cs->envelope_to[strlen(cs->envelope_to)-1] != '@')) {
407 sieve2_setvalue_string(s, "to", cs->envelope_to);
410 sieve2_setvalue_string(s, "to", "invalid_envelope_to@example.org");
414 sieve2_setvalue_string(s, "to", "null_envelope_to@example.org");
422 * Callback function to fetch message body
423 * (Uncomment the code if we implement this extension)
425 int ctdl_getbody(sieve2_context_t *s, void *my)
427 return SIEVE2_ERROR_UNSUPPORTED;
434 * Callback function to fetch message size
436 int ctdl_getsize(sieve2_context_t *s, void *my)
438 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
441 GetMetaData(&smi, cs->msgnum);
443 if (smi.meta_rfc822_length > 0L) {
444 sieve2_setvalue_int(s, "size", (int)smi.meta_rfc822_length);
448 return SIEVE2_ERROR_UNSUPPORTED;
453 * Callback function to retrieve the sieve script
455 int ctdl_getscript(sieve2_context_t *s, void *my) {
456 struct sdm_script *sptr;
457 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
459 for (sptr=cs->u->first_script; sptr!=NULL; sptr=sptr->next) {
460 if (sptr->script_active > 0) {
461 lprintf(CTDL_DEBUG, "ctdl_getscript() is using script '%s'\n", sptr->script_name);
462 sieve2_setvalue_string(s, "script", sptr->script_content);
467 lprintf(CTDL_DEBUG, "ctdl_getscript() found no active script\n");
468 return SIEVE2_ERROR_GETSCRIPT;
472 * Callback function to retrieve message headers
474 int ctdl_getheaders(sieve2_context_t *s, void *my) {
476 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
478 lprintf(CTDL_DEBUG, "ctdl_getheaders() was called\n");
479 sieve2_setvalue_string(s, "allheaders", cs->rfc822headers);
486 * Add a room to the list of those rooms which potentially require sieve processing
488 void sieve_queue_room(struct ctdlroom *which_room) {
489 struct RoomProcList *ptr;
491 ptr = (struct RoomProcList *) malloc(sizeof (struct RoomProcList));
492 if (ptr == NULL) return;
494 safestrncpy(ptr->name, which_room->QRname, sizeof ptr->name);
495 begin_critical_section(S_SIEVELIST);
496 ptr->next = sieve_list;
498 end_critical_section(S_SIEVELIST);
499 lprintf(CTDL_DEBUG, "<%s> queued for Sieve processing\n", which_room->QRname);
505 * Perform sieve processing for one message (called by sieve_do_room() for each message)
507 void sieve_do_msg(long msgnum, void *userdata) {
508 struct sdm_userdata *u = (struct sdm_userdata *) userdata;
509 sieve2_context_t *sieve2_context = u->sieve2_context;
510 struct ctdl_sieve my;
512 struct CtdlMessage *msg;
514 size_t headers_len = 0;
517 lprintf(CTDL_DEBUG, "Performing sieve processing on msg <%ld>\n", msgnum);
519 msg = CtdlFetchMessage(msgnum, 0);
520 if (msg == NULL) return;
523 * Grab the message headers so we can feed them to libSieve.
525 CC->redirect_buffer = malloc(SIZ);
526 CC->redirect_len = 0;
527 CC->redirect_alloc = SIZ;
528 CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ONLY, 0, 1);
529 my.rfc822headers = CC->redirect_buffer;
530 headers_len = CC->redirect_len;
531 CC->redirect_buffer = NULL;
532 CC->redirect_len = 0;
533 CC->redirect_alloc = 0;
536 * libSieve clobbers the stack if it encounters badly formed
537 * headers. Sanitize our headers by stripping nonprintable
540 for (i=0; i<headers_len; ++i) {
541 if (!isascii(my.rfc822headers[i])) {
542 my.rfc822headers[i] = '_';
546 my.keep = 0; /* Set to 1 to declare an *explicit* keep */
547 my.cancel_implicit_keep = 0; /* Some actions will cancel the implicit keep */
548 my.usernum = atol(CC->room.QRname); /* Keep track of the owner of the room's namespace */
549 my.msgnum = msgnum; /* Keep track of the message number in our local store */
550 my.u = u; /* Hand off a pointer to the rest of this info */
552 /* Keep track of the recipient so we can do handling based on it later */
553 process_rfc822_addr(msg->cm_fields['R'], my.recp_user, my.recp_node, my.recp_name);
555 /* Keep track of the sender so we can use it for REJECT and VACATION responses */
556 if (msg->cm_fields['F'] != NULL) {
557 safestrncpy(my.sender, msg->cm_fields['F'], sizeof my.sender);
559 else if ( (msg->cm_fields['A'] != NULL) && (msg->cm_fields['N'] != NULL) ) {
560 snprintf(my.sender, sizeof my.sender, "%s@%s", msg->cm_fields['A'], msg->cm_fields['N']);
562 else if (msg->cm_fields['A'] != NULL) {
563 safestrncpy(my.sender, msg->cm_fields['A'], sizeof my.sender);
566 strcpy(my.sender, "");
569 /* Keep track of the subject so we can use it for VACATION responses */
570 if (msg->cm_fields['U'] != NULL) {
571 safestrncpy(my.subject, msg->cm_fields['U'], sizeof my.subject);
574 strcpy(my.subject, "");
577 /* Keep track of the envelope-from address (use body-from if not found) */
578 if (msg->cm_fields['P'] != NULL) {
579 safestrncpy(my.envelope_from, msg->cm_fields['P'], sizeof my.envelope_from);
580 stripallbut(my.envelope_from, '<', '>');
582 else if (msg->cm_fields['F'] != NULL) {
583 safestrncpy(my.envelope_from, msg->cm_fields['F'], sizeof my.envelope_from);
584 stripallbut(my.envelope_from, '<', '>');
587 strcpy(my.envelope_from, "");
590 len = strlen(my.envelope_from);
591 for (i=0; i<len; ++i) {
592 if (isspace(my.envelope_from[i])) my.envelope_from[i] = '_';
594 if (haschar(my.envelope_from, '@') == 0) {
595 strcat(my.envelope_from, "@");
596 strcat(my.envelope_from, config.c_fqdn);
599 /* Keep track of the envelope-to address (use body-to if not found) */
600 if (msg->cm_fields['V'] != NULL) {
601 safestrncpy(my.envelope_to, msg->cm_fields['V'], sizeof my.envelope_to);
602 stripallbut(my.envelope_to, '<', '>');
604 else if (msg->cm_fields['R'] != NULL) {
605 safestrncpy(my.envelope_to, msg->cm_fields['R'], sizeof my.envelope_to);
606 if (msg->cm_fields['D'] != NULL) {
607 strcat(my.envelope_to, "@");
608 strcat(my.envelope_to, msg->cm_fields['D']);
610 stripallbut(my.envelope_to, '<', '>');
613 strcpy(my.envelope_to, "");
616 len = strlen(my.envelope_to);
617 for (i=0; i<len; ++i) {
618 if (isspace(my.envelope_to[i])) my.envelope_to[i] = '_';
620 if (haschar(my.envelope_to, '@') == 0) {
621 strcat(my.envelope_to, "@");
622 strcat(my.envelope_to, config.c_fqdn);
625 CtdlFreeMessage(msg);
627 sieve2_setvalue_string(sieve2_context, "allheaders", my.rfc822headers);
629 lprintf(CTDL_DEBUG, "Calling sieve2_execute()\n");
630 res = sieve2_execute(sieve2_context, &my);
631 if (res != SIEVE2_OK) {
632 lprintf(CTDL_CRIT, "sieve2_execute() returned %d: %s\n", res, sieve2_errstr(res));
635 free(my.rfc822headers);
636 my.rfc822headers = NULL;
639 * Delete the message from the inbox unless either we were told not to, or
640 * if no other action was successfully taken.
642 if ( (!my.keep) && (my.cancel_implicit_keep) ) {
643 lprintf(CTDL_DEBUG, "keep is 0 -- deleting message from inbox\n");
644 CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, "");
647 lprintf(CTDL_DEBUG, "Completed sieve processing on msg <%ld>\n", msgnum);
648 u->lastproc = msgnum;
656 * Given the on-disk representation of our Sieve config, load
657 * it into an in-memory data structure.
659 void parse_sieve_config(char *conf, struct sdm_userdata *u) {
663 struct sdm_script *sptr;
664 struct sdm_vacation *vptr;
667 while (c = ptr, ptr = bmstrcasestr(ptr, CTDLSIEVECONFIGSEPARATOR), ptr != NULL) {
669 ptr += strlen(CTDLSIEVECONFIGSEPARATOR);
671 extract_token(keyword, c, 0, '|', sizeof keyword);
673 if (!strcasecmp(keyword, "lastproc")) {
674 u->lastproc = extract_long(c, 1);
677 else if (!strcasecmp(keyword, "script")) {
678 sptr = malloc(sizeof(struct sdm_script));
679 extract_token(sptr->script_name, c, 1, '|', sizeof sptr->script_name);
680 sptr->script_active = extract_int(c, 2);
681 remove_token(c, 0, '|');
682 remove_token(c, 0, '|');
683 remove_token(c, 0, '|');
684 sptr->script_content = strdup(c);
685 sptr->next = u->first_script;
686 u->first_script = sptr;
689 else if (!strcasecmp(keyword, "vacation")) {
691 if (c != NULL) while (vacrec=c, c=strchr(c, '\n'), (c != NULL)) {
696 if (strncasecmp(vacrec, "vacation|", 9)) {
697 vptr = malloc(sizeof(struct sdm_vacation));
698 extract_token(vptr->fromaddr, vacrec, 0, '|', sizeof vptr->fromaddr);
699 vptr->timestamp = extract_long(vacrec, 1);
700 vptr->next = u->first_vacation;
701 u->first_vacation = vptr;
706 /* ignore unknown keywords */
711 * We found the Sieve configuration for this user.
712 * Now do something with it.
714 void get_sieve_config_backend(long msgnum, void *userdata) {
715 struct sdm_userdata *u = (struct sdm_userdata *) userdata;
716 struct CtdlMessage *msg;
719 u->config_msgnum = msgnum;
720 msg = CtdlFetchMessage(msgnum, 1);
722 u->config_msgnum = (-1) ;
726 conf = msg->cm_fields['M'];
727 msg->cm_fields['M'] = NULL;
728 CtdlFreeMessage(msg);
731 parse_sieve_config(conf, u);
739 * Write our citadel sieve config back to disk
741 * (Set yes_write_to_disk to nonzero to make it actually write the config;
742 * otherwise it just frees the data structures.)
744 void rewrite_ctdl_sieve_config(struct sdm_userdata *u, int yes_write_to_disk) {
746 struct sdm_script *sptr;
747 struct sdm_vacation *vptr;
753 "Content-type: application/x-citadel-sieve-config\n"
755 CTDLSIEVECONFIGSEPARATOR
757 CTDLSIEVECONFIGSEPARATOR
762 while (u->first_script != NULL) {
765 tsize = tlen + strlen(u->first_script->script_content) +256;
766 text = realloc(text, tsize);
767 sprintf(&text[strlen(text)], "script|%s|%d|%s" CTDLSIEVECONFIGSEPARATOR,
768 u->first_script->script_name,
769 u->first_script->script_active,
770 u->first_script->script_content
772 sptr = u->first_script;
773 u->first_script = u->first_script->next;
774 free(sptr->script_content);
778 if (u->first_vacation != NULL) {
780 tsize = strlen(text) + 256;
781 for (vptr = u->first_vacation; vptr != NULL; vptr = vptr->next) {
782 tsize += strlen(vptr->fromaddr + 32);
784 text = realloc(text, tsize);
786 sprintf(&text[strlen(text)], "vacation|\n");
787 while (u->first_vacation != NULL) {
788 if ( (time(NULL) - u->first_vacation->timestamp) < (MAX_VACATION * 86400)) {
789 sprintf(&text[strlen(text)], "%s|%ld\n",
790 u->first_vacation->fromaddr,
791 u->first_vacation->timestamp
794 vptr = u->first_vacation;
795 u->first_vacation = u->first_vacation->next;
798 sprintf(&text[strlen(text)], CTDLSIEVECONFIGSEPARATOR);
801 if (yes_write_to_disk)
803 /* Save the config */
804 quickie_message("Citadel", NULL, NULL, u->config_roomname,
807 "Sieve configuration"
810 /* And delete the old one */
811 if (u->config_msgnum > 0) {
812 CtdlDeleteMessages(u->config_roomname, &u->config_msgnum, 1, "");
822 * This is our callback registration table for libSieve.
824 sieve2_callback_t ctdl_sieve_callbacks[] = {
825 { SIEVE2_ACTION_REJECT, ctdl_reject },
826 { SIEVE2_ACTION_VACATION, ctdl_vacation },
827 { SIEVE2_ERRCALL_PARSE, ctdl_errparse },
828 { SIEVE2_ERRCALL_RUNTIME, ctdl_errexec },
829 { SIEVE2_ACTION_FILEINTO, ctdl_fileinto },
830 { SIEVE2_ACTION_REDIRECT, ctdl_redirect },
831 { SIEVE2_ACTION_DISCARD, ctdl_discard },
832 { SIEVE2_ACTION_KEEP, ctdl_keep },
833 { SIEVE2_SCRIPT_GETSCRIPT, ctdl_getscript },
834 { SIEVE2_DEBUG_TRACE, ctdl_debug },
835 { SIEVE2_MESSAGE_GETALLHEADERS, ctdl_getheaders },
836 { SIEVE2_MESSAGE_GETSIZE, ctdl_getsize },
837 { SIEVE2_MESSAGE_GETENVELOPE, ctdl_getenvelope },
839 * These actions are unsupported by Citadel so we don't declare them.
841 { SIEVE2_ACTION_NOTIFY, ctdl_notify },
842 { SIEVE2_MESSAGE_GETSUBADDRESS, ctdl_getsubaddress },
843 { SIEVE2_MESSAGE_GETBODY, ctdl_getbody },
851 * Perform sieve processing for a single room
853 void sieve_do_room(char *roomname) {
855 struct sdm_userdata u;
856 sieve2_context_t *sieve2_context = NULL; /* Context for sieve parser */
857 int res; /* Return code from libsieve calls */
858 long orig_lastproc = 0;
860 memset(&u, 0, sizeof u);
862 /* See if the user who owns this 'mailbox' has any Sieve scripts that
865 snprintf(u.config_roomname, sizeof u.config_roomname, "%010ld.%s", atol(roomname), USERCONFIGROOM);
866 if (getroom(&CC->room, u.config_roomname) != 0) {
867 lprintf(CTDL_DEBUG, "<%s> does not exist. No processing is required.\n", u.config_roomname);
872 * Find the sieve scripts and control record and do something
874 u.config_msgnum = (-1);
875 CtdlForEachMessage(MSGS_LAST, 1, NULL, SIEVECONFIG, NULL,
876 get_sieve_config_backend, (void *)&u );
878 if (u.config_msgnum < 0) {
879 lprintf(CTDL_DEBUG, "No Sieve rules exist. No processing is required.\n");
883 lprintf(CTDL_DEBUG, "Rules found. Performing Sieve processing for <%s>\n", roomname);
885 if (getroom(&CC->room, roomname) != 0) {
886 lprintf(CTDL_CRIT, "ERROR: cannot load <%s>\n", roomname);
890 /* Initialize the Sieve parser */
892 res = sieve2_alloc(&sieve2_context);
893 if (res != SIEVE2_OK) {
894 lprintf(CTDL_CRIT, "sieve2_alloc() returned %d: %s\n", res, sieve2_errstr(res));
898 res = sieve2_callbacks(sieve2_context, ctdl_sieve_callbacks);
899 if (res != SIEVE2_OK) {
900 lprintf(CTDL_CRIT, "sieve2_callbacks() returned %d: %s\n", res, sieve2_errstr(res));
904 /* Validate the script */
906 struct ctdl_sieve my; /* dummy ctdl_sieve struct just to pass "u" slong */
907 memset(&my, 0, sizeof my);
909 res = sieve2_validate(sieve2_context, &my);
910 if (res != SIEVE2_OK) {
911 lprintf(CTDL_CRIT, "sieve2_validate() returned %d: %s\n", res, sieve2_errstr(res));
915 /* Do something useful */
916 u.sieve2_context = sieve2_context;
917 orig_lastproc = u.lastproc;
918 CtdlForEachMessage(MSGS_GT, u.lastproc, NULL, NULL, NULL,
924 res = sieve2_free(&sieve2_context);
925 if (res != SIEVE2_OK) {
926 lprintf(CTDL_CRIT, "sieve2_free() returned %d: %s\n", res, sieve2_errstr(res));
929 /* Rewrite the config if we have to */
930 rewrite_ctdl_sieve_config(&u, (u.lastproc > orig_lastproc) ) ;
935 * Perform sieve processing for all rooms which require it
937 void perform_sieve_processing(void) {
938 struct RoomProcList *ptr = NULL;
940 if (sieve_list != NULL) {
941 lprintf(CTDL_DEBUG, "Begin Sieve processing\n");
942 while (sieve_list != NULL) {
943 char spoolroomname[ROOMNAMELEN];
944 safestrncpy(spoolroomname, sieve_list->name, sizeof spoolroomname);
945 begin_critical_section(S_SIEVELIST);
947 /* pop this record off the list */
949 sieve_list = sieve_list->next;
952 /* invalidate any duplicate entries to prevent double processing */
953 for (ptr=sieve_list; ptr!=NULL; ptr=ptr->next) {
954 if (!strcasecmp(ptr->name, spoolroomname)) {
959 end_critical_section(S_SIEVELIST);
960 if (spoolroomname[0] != 0) {
961 sieve_do_room(spoolroomname);
968 void msiv_load(struct sdm_userdata *u) {
969 char hold_rm[ROOMNAMELEN];
971 strcpy(hold_rm, CC->room.QRname); /* save current room */
973 /* Take a spin through the user's personal address book */
974 if (getroom(&CC->room, USERCONFIGROOM) == 0) {
976 u->config_msgnum = (-1);
977 strcpy(u->config_roomname, CC->room.QRname);
978 CtdlForEachMessage(MSGS_LAST, 1, NULL, SIEVECONFIG, NULL,
979 get_sieve_config_backend, (void *)u );
983 if (strcmp(CC->room.QRname, hold_rm)) {
984 getroom(&CC->room, hold_rm); /* return to saved room */
988 void msiv_store(struct sdm_userdata *u, int yes_write_to_disk) {
989 rewrite_ctdl_sieve_config(u, yes_write_to_disk);
994 * Select the active script.
995 * (Set script_name to an empty string to disable all scripts)
997 * Returns 0 on success or nonzero for error.
999 int msiv_setactive(struct sdm_userdata *u, char *script_name) {
1001 struct sdm_script *s;
1003 /* First see if the supplied value is ok */
1005 if (IsEmptyStr(script_name)) {
1009 for (s=u->first_script; s!=NULL; s=s->next) {
1010 if (!strcasecmp(s->script_name, script_name)) {
1016 if (!ok) return(-1);
1018 /* Now set the active script */
1019 for (s=u->first_script; s!=NULL; s=s->next) {
1020 if (!strcasecmp(s->script_name, script_name)) {
1021 s->script_active = 1;
1024 s->script_active = 0;
1033 * Fetch a script by name.
1035 * Returns NULL if the named script was not found, or a pointer to the script
1036 * if it was found. NOTE: the caller does *not* own the memory returned by
1037 * this function. Copy it if you need to keep it.
1039 char *msiv_getscript(struct sdm_userdata *u, char *script_name) {
1040 struct sdm_script *s;
1042 for (s=u->first_script; s!=NULL; s=s->next) {
1043 if (!strcasecmp(s->script_name, script_name)) {
1044 if (s->script_content != NULL) {
1045 return (s->script_content);
1055 * Delete a script by name.
1057 * Returns 0 if the script was deleted.
1058 * 1 if the script was not found.
1059 * 2 if the script cannot be deleted because it is active.
1061 int msiv_deletescript(struct sdm_userdata *u, char *script_name) {
1062 struct sdm_script *s = NULL;
1063 struct sdm_script *script_to_delete = NULL;
1065 for (s=u->first_script; s!=NULL; s=s->next) {
1066 if (!strcasecmp(s->script_name, script_name)) {
1067 script_to_delete = s;
1068 if (s->script_active) {
1074 if (script_to_delete == NULL) return(1);
1076 if (u->first_script == script_to_delete) {
1077 u->first_script = u->first_script->next;
1079 else for (s=u->first_script; s!=NULL; s=s->next) {
1080 if (s->next == script_to_delete) {
1081 s->next = s->next->next;
1085 free(script_to_delete->script_content);
1086 free(script_to_delete);
1092 * Add or replace a new script.
1093 * NOTE: after this function returns, "u" owns the memory that "script_content"
1096 void msiv_putscript(struct sdm_userdata *u, char *script_name, char *script_content) {
1098 struct sdm_script *s, *sptr;
1100 for (s=u->first_script; s!=NULL; s=s->next) {
1101 if (!strcasecmp(s->script_name, script_name)) {
1102 if (s->script_content != NULL) {
1103 free(s->script_content);
1105 s->script_content = script_content;
1110 if (replaced == 0) {
1111 sptr = malloc(sizeof(struct sdm_script));
1112 safestrncpy(sptr->script_name, script_name, sizeof sptr->script_name);
1113 sptr->script_content = script_content;
1114 sptr->script_active = 0;
1115 sptr->next = u->first_script;
1116 u->first_script = sptr;
1123 * Citadel protocol to manage sieve scripts.
1124 * This is basically a simplified (read: doesn't resemble IMAP) version
1125 * of the 'managesieve' protocol.
1127 void cmd_msiv(char *argbuf) {
1129 struct sdm_userdata u;
1130 char script_name[256];
1131 char *script_content = NULL;
1132 struct sdm_script *s;
1134 int changes_made = 0;
1136 memset(&u, 0, sizeof(struct sdm_userdata));
1138 if (CtdlAccessCheck(ac_logged_in)) return;
1139 extract_token(subcmd, argbuf, 0, '|', sizeof subcmd);
1142 if (!strcasecmp(subcmd, "putscript")) {
1143 extract_token(script_name, argbuf, 1, '|', sizeof script_name);
1144 if (!IsEmptyStr(script_name)) {
1145 cprintf("%d Transmit script now\n", SEND_LISTING);
1146 script_content = CtdlReadMessageBody("000", config.c_maxmsglen, NULL, 0, 0);
1147 msiv_putscript(&u, script_name, script_content);
1151 cprintf("%d Invalid script name.\n", ERROR + ILLEGAL_VALUE);
1155 else if (!strcasecmp(subcmd, "listscripts")) {
1156 cprintf("%d Scripts:\n", LISTING_FOLLOWS);
1157 for (s=u.first_script; s!=NULL; s=s->next) {
1158 if (s->script_content != NULL) {
1159 cprintf("%s|%d|\n", s->script_name, s->script_active);
1165 else if (!strcasecmp(subcmd, "setactive")) {
1166 extract_token(script_name, argbuf, 1, '|', sizeof script_name);
1167 if (msiv_setactive(&u, script_name) == 0) {
1168 cprintf("%d ok\n", CIT_OK);
1172 cprintf("%d Script '%s' does not exist.\n",
1173 ERROR + ILLEGAL_VALUE,
1179 else if (!strcasecmp(subcmd, "getscript")) {
1180 extract_token(script_name, argbuf, 1, '|', sizeof script_name);
1181 script_content = msiv_getscript(&u, script_name);
1182 if (script_content != NULL) {
1185 cprintf("%d Script:\n", LISTING_FOLLOWS);
1186 script_len = strlen(script_content);
1187 client_write(script_content, script_len);
1188 if (script_content[script_len-1] != '\n') {
1194 cprintf("%d Invalid script name.\n", ERROR + ILLEGAL_VALUE);
1198 else if (!strcasecmp(subcmd, "deletescript")) {
1199 extract_token(script_name, argbuf, 1, '|', sizeof script_name);
1200 i = msiv_deletescript(&u, script_name);
1202 cprintf("%d ok\n", CIT_OK);
1206 cprintf("%d Script '%s' does not exist.\n",
1207 ERROR + ILLEGAL_VALUE,
1212 cprintf("%d Script '%s' is active and cannot be deleted.\n",
1213 ERROR + ILLEGAL_VALUE,
1218 cprintf("%d unknown error\n", ERROR);
1223 cprintf("%d Invalid subcommand\n", ERROR + CMD_NOT_SUPPORTED);
1226 msiv_store(&u, changes_made);
1231 void ctdl_sieve_init(void) {
1233 sieve2_context_t *sieve2_context = NULL;
1237 * We don't really care about dumping the entire credits to the log
1238 * every time the server is initialized. The documentation will suffice
1239 * for that purpose. We are making a call to sieve2_credits() in order
1240 * to demonstrate that we have successfully linked in to libsieve.
1242 cred = strdup(sieve2_credits());
1243 if (cred == NULL) return;
1245 if (strlen(cred) > 60) {
1246 strcpy(&cred[55], "...");
1249 lprintf(CTDL_INFO, "%s\n",cred);
1252 /* Briefly initialize a Sieve parser instance just so we can list the
1253 * extensions that are available.
1255 res = sieve2_alloc(&sieve2_context);
1256 if (res != SIEVE2_OK) {
1257 lprintf(CTDL_CRIT, "sieve2_alloc() returned %d: %s\n", res, sieve2_errstr(res));
1261 res = sieve2_callbacks(sieve2_context, ctdl_sieve_callbacks);
1262 if (res != SIEVE2_OK) {
1263 lprintf(CTDL_CRIT, "sieve2_callbacks() returned %d: %s\n", res, sieve2_errstr(res));
1267 msiv_extensions = strdup(sieve2_listextensions(sieve2_context));
1268 lprintf(CTDL_INFO, "Extensions: %s\n", msiv_extensions);
1270 BAIL: res = sieve2_free(&sieve2_context);
1271 if (res != SIEVE2_OK) {
1272 lprintf(CTDL_CRIT, "sieve2_free() returned %d: %s\n", res, sieve2_errstr(res));
1277 int serv_sieve_room(struct ctdlroom *room)
1279 if (!strcasecmp(&room->QRname[11], MAILROOM)) {
1280 sieve_queue_room(room);
1285 #endif /* HAVE_LIBSIEVE */
1287 CTDL_MODULE_INIT(sieve)
1290 #ifdef HAVE_LIBSIEVE
1293 CtdlRegisterProtoHook(cmd_msiv, "MSIV", "Manage Sieve scripts");
1295 CtdlRegisterRoomHook(serv_sieve_room);
1297 CtdlRegisterSessionHook(perform_sieve_processing, EVT_HOUSE);
1299 #else /* HAVE_LIBSIEVE */
1301 lprintf(CTDL_INFO, "This server is missing libsieve. Mailbox filtering will be disabled.\n");
1303 #endif /* HAVE_LIBSIEVE */
1305 /* return our Subversion id for the Log */