4 * Copyright (c) 1987-2020 by the citadel.org team
6 * This program is open source software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 3.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
23 #include <sys/types.h>
25 #if TIME_WITH_SYS_TIME
26 # include <sys/time.h>
30 # include <sys/time.h>
39 #include <libcitadel.h>
42 #include "citserver.h"
47 #include "internet_addressing.h"
48 #include "ctdl_module.h"
54 * Callback function to redirect a message to a different folder
56 int ctdl_redirect(void)
58 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
59 struct CtdlMessage *msg = NULL;
60 recptypes *valid = NULL;
63 safestrncpy(recp, sieve2_getvalue_string(s, "address"), sizeof recp);
65 syslog(LOG_DEBUG, "Action is REDIRECT, recipient <%s>", recp);
67 valid = validate_recipients(recp, NULL, 0);
69 syslog(LOG_WARNING, "REDIRECT failed: bad recipient <%s>", recp);
70 return SIEVE2_ERROR_BADARGS;
72 if (valid->num_error > 0) {
73 syslog(LOG_WARNING, "REDIRECT failed: bad recipient <%s>", recp);
74 free_recipients(valid);
75 return SIEVE2_ERROR_BADARGS;
78 msg = CtdlFetchMessage(cs->msgnum, 1, 1);
80 syslog(LOG_WARNING, "REDIRECT failed: unable to fetch msg %ld", cs->msgnum);
81 free_recipients(valid);
82 return SIEVE2_ERROR_BADARGS;
85 CtdlSubmitMsg(msg, valid, NULL, 0);
86 cs->cancel_implicit_keep = 1;
87 free_recipients(valid);
94 * Callback function to indicate that a message *will* be kept in the inbox
96 int ctdl_keep(sieve2_context_t *s, void *my)
98 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
100 syslog(LOG_DEBUG, "Action is KEEP");
103 cs->cancel_implicit_keep = 1;
109 * Callback function to file a message into a different mailbox
111 int ctdl_fileinto(sieve2_context_t *s, void *my)
113 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
114 const char *dest_folder = sieve2_getvalue_string(s, "mailbox");
116 char foldername[256];
117 char original_room_name[ROOMNAMELEN];
119 syslog(LOG_DEBUG, "Action is FILEINTO, destination is <%s>", dest_folder);
121 /* FILEINTO 'INBOX' is the same thing as KEEP */
122 if ( (!strcasecmp(dest_folder, "INBOX")) || (!strcasecmp(dest_folder, MAILROOM)) ) {
124 cs->cancel_implicit_keep = 1;
128 /* Remember what room we came from */
129 safestrncpy(original_room_name, CC->room.QRname, sizeof original_room_name);
131 /* First try a mailbox name match (check personal mail folders first) */
132 snprintf(foldername, sizeof foldername, "%010ld.%s", cs->usernum, dest_folder);
133 c = CtdlGetRoom(&CC->room, foldername);
135 /* Then a regular room name match (public and private rooms) */
137 safestrncpy(foldername, dest_folder, sizeof foldername);
138 c = CtdlGetRoom(&CC->room, foldername);
142 syslog(LOG_WARNING, "FILEINTO failed: target <%s> does not exist", dest_folder);
143 return SIEVE2_ERROR_BADARGS;
146 /* Yes, we actually have to go there */
147 CtdlUserGoto(NULL, 0, 0, NULL, NULL, NULL, NULL);
149 c = CtdlSaveMsgPointersInRoom(NULL, &cs->msgnum, 1, 0, NULL, 0);
151 /* Go back to the room we came from */
152 if (strcasecmp(original_room_name, CC->room.QRname)) {
153 CtdlUserGoto(original_room_name, 0, 0, NULL, NULL, NULL, NULL);
157 cs->cancel_implicit_keep = 1;
161 return SIEVE2_ERROR_BADARGS;
167 * Callback function to indicate that a message should be discarded.
169 int ctdl_discard(sieve2_context_t *s, void *my)
171 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
173 syslog(LOG_DEBUG, "Action is DISCARD");
175 /* Cancel the implicit keep. That's all there is to it. */
176 cs->cancel_implicit_keep = 1;
182 * Callback function to indicate that a message should be rejected
184 int ctdl_reject(sieve2_context_t *s, void *my)
186 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
187 char *reject_text = NULL;
189 syslog(LOG_DEBUG, "Action is REJECT");
191 /* If we don't know who sent the message, do a DISCARD instead. */
192 if (IsEmptyStr(cs->sender)) {
193 syslog(LOG_INFO, "Unknown sender. Doing DISCARD instead of REJECT.");
194 return ctdl_discard(s, my);
197 /* Assemble the reject message. */
198 reject_text = malloc(strlen(sieve2_getvalue_string(s, "message")) + 1024);
199 if (reject_text == NULL) {
200 return SIEVE2_ERROR_FAIL;
204 "Content-type: text/plain\n"
206 "The message was refused by the recipient's mail filtering program.\n"
207 "The reason given was as follows:\n"
212 sieve2_getvalue_string(s, "message")
215 quickie_message( /* This delivers the message */
222 "Delivery status notification"
226 cs->cancel_implicit_keep = 1;
232 * Callback function to indicate that a vacation message should be generated
234 int ctdl_vacation(sieve2_context_t *s, void *my)
236 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
237 struct sdm_vacation *vptr;
240 char *vacamsg_text = NULL;
241 char vacamsg_subject[1024];
243 syslog(LOG_DEBUG, "Action is VACATION");
245 message = sieve2_getvalue_string(s, "message");
246 if (message == NULL) return SIEVE2_ERROR_BADARGS;
248 if (sieve2_getvalue_string(s, "subject") != NULL) {
249 safestrncpy(vacamsg_subject, sieve2_getvalue_string(s, "subject"), sizeof vacamsg_subject);
252 snprintf(vacamsg_subject, sizeof vacamsg_subject, "Re: %s", cs->subject);
255 days = sieve2_getvalue_int(s, "days");
256 if (days < 1) days = 1;
257 if (days > MAX_VACATION) days = MAX_VACATION;
259 /* Check to see whether we've already alerted this sender that we're on vacation. */
260 for (vptr = cs->u->first_vacation; vptr != NULL; vptr = vptr->next) {
261 if (!strcasecmp(vptr->fromaddr, cs->sender)) {
262 if ( (time(NULL) - vptr->timestamp) < (days * 86400) ) {
263 syslog(LOG_DEBUG, "Already alerted <%s> recently.", cs->sender);
269 /* Assemble the reject message. */
270 vacamsg_text = malloc(strlen(message) + 1024);
271 if (vacamsg_text == NULL) {
272 return SIEVE2_ERROR_FAIL;
275 sprintf(vacamsg_text,
276 "Content-type: text/plain charset=utf-8\n"
284 quickie_message( /* This delivers the message */
296 /* Now update the list to reflect the fact that we've alerted this sender.
297 * If they're already in the list, just update the timestamp.
299 for (vptr = cs->u->first_vacation; vptr != NULL; vptr = vptr->next) {
300 if (!strcasecmp(vptr->fromaddr, cs->sender)) {
301 vptr->timestamp = time(NULL);
306 /* If we get to this point, create a new record.
308 vptr = malloc(sizeof(struct sdm_vacation));
309 memset(vptr, 0, sizeof(struct sdm_vacation));
310 vptr->timestamp = time(NULL);
311 safestrncpy(vptr->fromaddr, cs->sender, sizeof vptr->fromaddr);
312 vptr->next = cs->u->first_vacation;
313 cs->u->first_vacation = vptr;
320 * Callback function to parse message envelope
322 int ctdl_getenvelope(sieve2_context_t *s, void *my)
324 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
326 syslog(LOG_DEBUG, "Action is GETENVELOPE");
327 syslog(LOG_DEBUG, "EnvFrom: %s", cs->envelope_from);
328 syslog(LOG_DEBUG, "EnvTo: %s", cs->envelope_to);
330 if (cs->envelope_from != NULL) {
331 if ((cs->envelope_from[0] != '@')&&(cs->envelope_from[strlen(cs->envelope_from)-1] != '@')) {
332 sieve2_setvalue_string(s, "from", cs->envelope_from);
335 sieve2_setvalue_string(s, "from", "invalid_envelope_from@example.org");
339 sieve2_setvalue_string(s, "from", "null_envelope_from@example.org");
343 if (cs->envelope_to != NULL) {
344 if ((cs->envelope_to[0] != '@') && (cs->envelope_to[strlen(cs->envelope_to)-1] != '@')) {
345 sieve2_setvalue_string(s, "to", cs->envelope_to);
348 sieve2_setvalue_string(s, "to", "invalid_envelope_to@example.org");
352 sieve2_setvalue_string(s, "to", "null_envelope_to@example.org");
361 * Callback function to fetch message size
363 int ctdl_getsize(sieve2_context_t *s, void *my)
365 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
368 GetMetaData(&smi, cs->msgnum);
370 if (smi.meta_rfc822_length > 0L) {
371 sieve2_setvalue_int(s, "size", (int)smi.meta_rfc822_length);
375 return SIEVE2_ERROR_UNSUPPORTED;
380 * Return a pointer to the active Sieve script.
381 * (Caller does NOT own the memory and should not free the returned pointer.)
383 char *get_active_script(struct sdm_userdata *u) {
384 struct sdm_script *sptr;
386 for (sptr=u->first_script; sptr!=NULL; sptr=sptr->next) {
387 if (sptr->script_active > 0) {
388 syslog(LOG_DEBUG, "get_active_script() is using script '%s'", sptr->script_name);
389 return(sptr->script_content);
393 syslog(LOG_DEBUG, "get_active_script() found no active script");
399 * Callback function to retrieve the sieve script
401 int ctdl_getscript(sieve2_context_t *s, void *my) {
402 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
404 char *active_script = get_active_script(cs->u);
405 if (active_script != NULL) {
406 sieve2_setvalue_string(s, "script", active_script);
410 return SIEVE2_ERROR_GETSCRIPT;
415 * Callback function to retrieve message headers
417 int ctdl_getheaders(sieve2_context_t *s, void *my) {
419 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
421 syslog(LOG_DEBUG, "ctdl_getheaders() was called");
422 sieve2_setvalue_string(s, "allheaders", cs->rfc822headers);
429 * Perform sieve processing for one message (called by sieve_do_room() for each message)
431 void sieve_do_msg(long msgnum, void *userdata) {
432 struct sdm_userdata *u = (struct sdm_userdata *) userdata;
433 sieve2_context_t *sieve2_context;
434 struct ctdl_sieve my;
436 struct CtdlMessage *msg;
438 size_t headers_len = 0;
442 syslog(LOG_ERR, "Can't process message <%ld> without userdata!", msgnum);
446 sieve2_context = u->sieve2_context;
448 syslog(LOG_DEBUG, "Performing sieve processing on msg <%ld>", msgnum);
451 * Make sure you include message body so you can get those second-level headers ;)
453 msg = CtdlFetchMessage(msgnum, 1, 1);
454 if (msg == NULL) return;
457 * Grab the message headers so we can feed them to libSieve.
458 * Use HEADERS_ONLY rather than HEADERS_FAST in order to include second-level headers.
460 CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
461 CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ONLY, 0, 1, 0);
462 headers_len = StrLength(CC->redirect_buffer);
463 my.rfc822headers = SmashStrBuf(&CC->redirect_buffer);
466 * libSieve clobbers the stack if it encounters badly formed
467 * headers. Sanitize our headers by stripping nonprintable
470 for (i=0; i<headers_len; ++i) {
471 if (!isascii(my.rfc822headers[i])) {
472 my.rfc822headers[i] = '_';
476 my.keep = 0; /* Set to 1 to declare an *explicit* keep */
477 my.cancel_implicit_keep = 0; /* Some actions will cancel the implicit keep */
478 my.usernum = atol(CC->room.QRname); /* Keep track of the owner of the room's namespace */
479 my.msgnum = msgnum; /* Keep track of the message number in our local store */
480 my.u = u; /* Hand off a pointer to the rest of this info */
482 /* Keep track of the recipient so we can do handling based on it later */
483 process_rfc822_addr(msg->cm_fields[eRecipient], my.recp_user, my.recp_node, my.recp_name);
485 /* Keep track of the sender so we can use it for REJECT and VACATION responses */
486 if (!CM_IsEmpty(msg, erFc822Addr)) {
487 safestrncpy(my.sender, msg->cm_fields[erFc822Addr], sizeof my.sender);
489 else if (!CM_IsEmpty(msg, eAuthor)) {
490 safestrncpy(my.sender, msg->cm_fields[eAuthor], sizeof my.sender);
493 strcpy(my.sender, "");
496 /* Keep track of the subject so we can use it for VACATION responses */
497 if (!CM_IsEmpty(msg, eMsgSubject)) {
498 safestrncpy(my.subject, msg->cm_fields[eMsgSubject], sizeof my.subject);
501 strcpy(my.subject, "");
504 /* Keep track of the envelope-from address (use body-from if not found) */
505 if (!CM_IsEmpty(msg, eMessagePath)) {
506 safestrncpy(my.envelope_from, msg->cm_fields[eMessagePath], sizeof my.envelope_from);
507 stripallbut(my.envelope_from, '<', '>');
509 else if (!CM_IsEmpty(msg, erFc822Addr)) {
510 safestrncpy(my.envelope_from, msg->cm_fields[erFc822Addr], sizeof my.envelope_from);
511 stripallbut(my.envelope_from, '<', '>');
514 strcpy(my.envelope_from, "");
517 len = strlen(my.envelope_from);
518 for (i=0; i<len; ++i) {
519 if (isspace(my.envelope_from[i])) my.envelope_from[i] = '_';
521 if (haschar(my.envelope_from, '@') == 0) {
522 strcat(my.envelope_from, "@");
523 strcat(my.envelope_from, CtdlGetConfigStr("c_fqdn"));
526 /* Keep track of the envelope-to address (use body-to if not found) */
527 if (!CM_IsEmpty(msg, eenVelopeTo)) {
528 safestrncpy(my.envelope_to, msg->cm_fields[eenVelopeTo], sizeof my.envelope_to);
529 stripallbut(my.envelope_to, '<', '>');
531 else if (!CM_IsEmpty(msg, eRecipient)) {
532 safestrncpy(my.envelope_to, msg->cm_fields[eRecipient], sizeof my.envelope_to);
533 stripallbut(my.envelope_to, '<', '>');
536 strcpy(my.envelope_to, "");
539 len = strlen(my.envelope_to);
540 for (i=0; i<len; ++i) {
541 if (isspace(my.envelope_to[i])) my.envelope_to[i] = '_';
543 if (haschar(my.envelope_to, '@') == 0) {
544 strcat(my.envelope_to, "@");
545 strcat(my.envelope_to, CtdlGetConfigStr("c_fqdn"));
550 syslog(LOG_DEBUG, "Calling sieve2_execute()");
551 res = sieve2_execute(sieve2_context, &my);
552 if (res != SIEVE2_OK) {
553 syslog(LOG_ERR, "sieve2_execute() returned %d: %s", res, sieve2_errstr(res));
556 free(my.rfc822headers);
557 my.rfc822headers = NULL;
560 * Delete the message from the inbox unless either we were told not to, or
561 * if no other action was successfully taken.
563 if ( (!my.keep) && (my.cancel_implicit_keep) ) {
564 syslog(LOG_DEBUG, "keep is 0 -- deleting message from inbox");
565 CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, "");
568 syslog(LOG_DEBUG, "Completed sieve processing on msg <%ld>", msgnum);
569 u->lastproc = msgnum;
577 * Given the on-disk representation of our Sieve config, load
578 * it into an in-memory data structure.
580 void parse_sieve_config(char *conf, struct sdm_userdata *u) {
584 struct sdm_script *sptr;
585 struct sdm_vacation *vptr;
588 while (c = ptr, ptr = bmstrcasestr(ptr, CTDLSIEVECONFIGSEPARATOR), ptr != NULL) {
590 ptr += strlen(CTDLSIEVECONFIGSEPARATOR);
592 extract_token(keyword, c, 0, '|', sizeof keyword);
594 if (!strcasecmp(keyword, "lastproc")) {
595 u->lastproc = extract_long(c, 1);
598 else if (!strcasecmp(keyword, "script")) {
599 sptr = malloc(sizeof(struct sdm_script));
600 extract_token(sptr->script_name, c, 1, '|', sizeof sptr->script_name);
601 sptr->script_active = extract_int(c, 2);
602 remove_token(c, 0, '|');
603 remove_token(c, 0, '|');
604 remove_token(c, 0, '|');
605 sptr->script_content = strdup(c);
606 sptr->next = u->first_script;
607 u->first_script = sptr;
610 else if (!strcasecmp(keyword, "vacation")) {
612 if (c != NULL) while (vacrec=c, c=strchr(c, '\n'), (c != NULL)) {
617 if (strncasecmp(vacrec, "vacation|", 9)) {
618 vptr = malloc(sizeof(struct sdm_vacation));
619 extract_token(vptr->fromaddr, vacrec, 0, '|', sizeof vptr->fromaddr);
620 vptr->timestamp = extract_long(vacrec, 1);
621 vptr->next = u->first_vacation;
622 u->first_vacation = vptr;
627 /* ignore unknown keywords */
632 * We found the Sieve configuration for this user.
633 * Now do something with it.
635 void get_sieve_config_backend(long msgnum, void *userdata) {
636 struct sdm_userdata *u = (struct sdm_userdata *) userdata;
637 struct CtdlMessage *msg;
641 u->config_msgnum = msgnum;
642 msg = CtdlFetchMessage(msgnum, 1, 1);
644 u->config_msgnum = (-1) ;
648 CM_GetAsField(msg, eMesageText, &conf, &conflen);
653 parse_sieve_config(conf, u);
661 * Write our citadel sieve config back to disk
663 * (Set yes_write_to_disk to nonzero to make it actually write the config;
664 * otherwise it just frees the data structures.)
666 void rewrite_ctdl_sieve_config(struct sdm_userdata *u, int yes_write_to_disk) {
668 struct sdm_script *sptr;
669 struct sdm_vacation *vptr;
671 text = NewStrBufPlain(NULL, SIZ);
673 "Content-type: application/x-citadel-sieve-config\n"
675 CTDLSIEVECONFIGSEPARATOR
677 CTDLSIEVECONFIGSEPARATOR
682 while (u->first_script != NULL) {
683 StrBufAppendPrintf(text,
684 "script|%s|%d|%s" CTDLSIEVECONFIGSEPARATOR,
685 u->first_script->script_name,
686 u->first_script->script_active,
687 u->first_script->script_content
689 sptr = u->first_script;
690 u->first_script = u->first_script->next;
691 free(sptr->script_content);
695 if (u->first_vacation != NULL) {
697 StrBufAppendPrintf(text, "vacation|\n");
698 while (u->first_vacation != NULL) {
699 if ( (time(NULL) - u->first_vacation->timestamp) < (MAX_VACATION * 86400)) {
700 StrBufAppendPrintf(text, "%s|%ld\n",
701 u->first_vacation->fromaddr,
702 u->first_vacation->timestamp
705 vptr = u->first_vacation;
706 u->first_vacation = u->first_vacation->next;
709 StrBufAppendPrintf(text, CTDLSIEVECONFIGSEPARATOR);
712 if (yes_write_to_disk)
714 /* Save the config */
715 quickie_message("Citadel", NULL, NULL, u->config_roomname,
718 "Sieve configuration"
721 /* And delete the old one */
722 if (u->config_msgnum > 0) {
723 CtdlDeleteMessages(u->config_roomname, &u->config_msgnum, 1, "");
733 * This is our callback registration table for libSieve.
735 sieve2_callback_t ctdl_sieve_callbacks[] = {
736 { SIEVE2_ACTION_REJECT, ctdl_reject },
737 { SIEVE2_ACTION_VACATION, ctdl_vacation },
738 { SIEVE2_ERRCALL_PARSE, ctdl_errparse },
739 { SIEVE2_ERRCALL_RUNTIME, ctdl_errexec },
740 { SIEVE2_ACTION_FILEINTO, ctdl_fileinto },
741 { SIEVE2_ACTION_REDIRECT, ctdl_redirect },
742 { SIEVE2_ACTION_DISCARD, ctdl_discard },
743 { SIEVE2_ACTION_KEEP, ctdl_keep },
744 { SIEVE2_SCRIPT_GETSCRIPT, ctdl_getscript },
745 { SIEVE2_DEBUG_TRACE, ctdl_debug },
746 { SIEVE2_MESSAGE_GETALLHEADERS, ctdl_getheaders },
747 { SIEVE2_MESSAGE_GETSIZE, ctdl_getsize },
748 { SIEVE2_MESSAGE_GETENVELOPE, ctdl_getenvelope },
754 * Perform sieve processing for a single room
756 void sieve_do_room(char *roomname) {
758 struct sdm_userdata u;
759 sieve2_context_t *sieve2_context = NULL; /* Context for sieve parser */
760 int res; /* Return code from libsieve calls */
761 long orig_lastproc = 0;
763 memset(&u, 0, sizeof u);
765 /* See if the user who owns this 'mailbox' has any Sieve scripts that
768 snprintf(u.config_roomname, sizeof u.config_roomname, "%010ld.%s", atol(roomname), USERCONFIGROOM);
769 if (CtdlGetRoom(&CC->room, u.config_roomname) != 0) {
770 syslog(LOG_DEBUG, "<%s> does not exist. No processing is required.", u.config_roomname);
775 * Find the sieve scripts and control record and do something
777 u.config_msgnum = (-1);
778 CtdlForEachMessage(MSGS_LAST, 1, NULL, SIEVECONFIG, NULL, get_sieve_config_backend, (void *)&u );
780 if (u.config_msgnum < 0) {
781 syslog(LOG_DEBUG, "No Sieve rules exist. No processing is required.");
786 * Check to see whether the script is empty and should not be processed.
787 * A script is considered non-empty if it contains at least one semicolon.
790 (get_active_script(&u) == NULL)
791 || (strchr(get_active_script(&u), ';') == NULL)
793 syslog(LOG_DEBUG, "Sieve script is empty. No processing is required.");
797 syslog(LOG_DEBUG, "Rules found. Performing Sieve processing for <%s>", roomname);
799 if (CtdlGetRoom(&CC->room, roomname) != 0) {
800 syslog(LOG_ERR, "ERROR: cannot load <%s>", roomname);
804 /* Initialize the Sieve parser */
806 res = sieve2_alloc(&sieve2_context);
807 if (res != SIEVE2_OK) {
808 syslog(LOG_ERR, "sieve2_alloc() returned %d: %s", res, sieve2_errstr(res));
812 res = sieve2_callbacks(sieve2_context, ctdl_sieve_callbacks);
813 if (res != SIEVE2_OK) {
814 syslog(LOG_ERR, "sieve2_callbacks() returned %d: %s", res, sieve2_errstr(res));
818 /* Validate the script */
820 struct ctdl_sieve my; /* dummy ctdl_sieve struct just to pass "u" along */
821 memset(&my, 0, sizeof my);
823 res = sieve2_validate(sieve2_context, &my);
824 if (res != SIEVE2_OK) {
825 syslog(LOG_ERR, "sieve2_validate() returned %d: %s", res, sieve2_errstr(res));
829 /* Do something useful */
830 u.sieve2_context = sieve2_context;
831 orig_lastproc = u.lastproc;
832 CtdlForEachMessage(MSGS_GT, u.lastproc, NULL, NULL, NULL, sieve_do_msg, (void *) &u);
835 res = sieve2_free(&sieve2_context);
836 if (res != SIEVE2_OK) {
837 syslog(LOG_ERR, "sieve2_free() returned %d: %s", res, sieve2_errstr(res));
840 /* Rewrite the config if we have to (we're not the user right now) */
841 rewrite_ctdl_sieve_config(&u, (u.lastproc > orig_lastproc) ) ;
855 * This is a client-facing function which fetches the user's inbox rules -- it omits all lines containing anything other than a rule.
857 void cmd_gibr(char *argbuf) {
859 if (CtdlAccessCheck(ac_logged_in)) return;
861 cprintf("%d inbox rules for %s\n", LISTING_FOLLOWS, CC->user.fullname);
863 struct CtdlMessage *msg = CtdlFetchMessage(CC->user.msgnum_inboxrules, 1, 1);
865 if (!CM_IsEmpty(msg, eMesageText)) {
867 char *rest = msg->cm_fields[eMesageText];
868 while ((token = strtok_r(rest, "\n", &rest))) {
870 // for backwards compatibility, "# WEBCIT_RULE" is an alias for "rule"
871 if (!strncasecmp(token, "# WEBCIT_RULE|", 14)) {
872 strcpy(token, "rule|");
873 strcpy(&token[5], &token[14]);
876 // Output only lines containing rules.
877 if (!strncasecmp(token, "rule|", 5)) {
878 cprintf("%s\n", token);
891 * User transmits the new inbox rules for the account. They are inserted into the account, replacing the ones already there.
893 void cmd_pibr(char *argbuf) {
894 if (CtdlAccessCheck(ac_logged_in)) return;
897 cprintf("%d send new rules\n", SEND_LISTING);
898 char *newrules = CtdlReadMessageBody(HKEY("000"), CtdlGetConfigLong("c_maxmsglen"), NULL, 0);
899 StrBuf *NewConfig = NewStrBufPlain("Content-type: application/x-citadel-sieve-config; charset=UTF-8\nContent-transfer-encoding: 8bit\n\n", -1);
902 char *rest = newrules;
903 while ((token = strtok_r(rest, "\n", &rest))) {
904 // Accept only lines containing rules
905 if (!strncasecmp(token, "rule|", 5)) {
906 StrBufAppendBufPlain(NewConfig, token, -1, 0);
907 StrBufAppendBufPlain(NewConfig, HKEY("\n"), 0);
912 // Fetch the existing config so we can merge in anything that is NOT a rule
913 // (Does not start with "rule|" but has at least one vertical bar)
914 struct CtdlMessage *msg = CtdlFetchMessage(CC->user.msgnum_inboxrules, 1, 1);
916 if (!CM_IsEmpty(msg, eMesageText)) {
917 rest = msg->cm_fields[eMesageText];
918 while ((token = strtok_r(rest, "\n", &rest))) {
919 // for backwards compatibility, "# WEBCIT_RULE" is an alias for "rule"
920 if ((strncasecmp(token, "# WEBCIT_RULE|", 14)) && (strncasecmp(token, "rule|", 5)) && (haschar(token, '|'))) {
921 StrBufAppendBufPlain(NewConfig, token, -1, 0);
922 StrBufAppendBufPlain(NewConfig, HKEY("\n"), 0);
929 /* we have composed the new configuration , now save it */
930 long old_msgnum = CC->user.msgnum_inboxrules;
931 char userconfigroomname[ROOMNAMELEN];
932 CtdlMailboxName(userconfigroomname, sizeof userconfigroomname, &CC->user, USERCONFIGROOM);
933 long new_msgnum = quickie_message("Citadel", NULL, NULL, userconfigroomname, ChrPtr(NewConfig), FMT_RFC822, "inbox rules configuration");
934 FreeStrBuf(&NewConfig);
935 CtdlGetUserLock(&CC->user, CC->curr_user);
936 CC->user.msgnum_inboxrules = new_msgnum;
937 CtdlPutUserLock(&CC->user);
938 if (old_msgnum > 0) {
939 syslog(LOG_DEBUG, "Deleting old message %ld from %s", old_msgnum, userconfigroomname);
940 CtdlDeleteMessages(userconfigroomname, &old_msgnum, 1, "");
945 CTDL_MODULE_INIT(sieve)
949 // ctdl_sieve_init();
950 CtdlRegisterProtoHook(cmd_gibr, "GIBR", "Get InBox Rules");
951 CtdlRegisterProtoHook(cmd_pibr, "PIBR", "Put InBox Rules");
952 // CtdlRegisterSessionHook(perform_sieve_processing, EVT_HOUSE, PRIO_HOUSE + 10);
953 // CtdlRegisterCleanupHook(cleanup_sieve);
956 /* return our module name for the log */