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);
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);
98 * Callback function to indicate that a message should be discarded.
100 int ctdl_discard(sieve2_context_t *s, void *my)
102 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
104 syslog(LOG_DEBUG, "Action is DISCARD");
106 /* Cancel the implicit keep. That's all there is to it. */
107 cs->cancel_implicit_keep = 1;
121 * Callback function to indicate that a vacation message should be generated
123 int ctdl_vacation(sieve2_context_t *s, void *my)
125 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
126 struct sdm_vacation *vptr;
129 char *vacamsg_text = NULL;
130 char vacamsg_subject[1024];
132 syslog(LOG_DEBUG, "Action is VACATION");
134 message = sieve2_getvalue_string(s, "message");
135 if (message == NULL) return SIEVE2_ERROR_BADARGS;
137 if (sieve2_getvalue_string(s, "subject") != NULL) {
138 safestrncpy(vacamsg_subject, sieve2_getvalue_string(s, "subject"), sizeof vacamsg_subject);
141 snprintf(vacamsg_subject, sizeof vacamsg_subject, "Re: %s", cs->subject);
144 days = sieve2_getvalue_int(s, "days");
145 if (days < 1) days = 1;
146 if (days > MAX_VACATION) days = MAX_VACATION;
148 /* Check to see whether we've already alerted this sender that we're on vacation. */
149 for (vptr = cs->u->first_vacation; vptr != NULL; vptr = vptr->next) {
150 if (!strcasecmp(vptr->fromaddr, cs->sender)) {
151 if ( (time(NULL) - vptr->timestamp) < (days * 86400) ) {
152 syslog(LOG_DEBUG, "Already alerted <%s> recently.", cs->sender);
158 /* Assemble the reject message. */
159 vacamsg_text = malloc(strlen(message) + 1024);
160 if (vacamsg_text == NULL) {
161 return SIEVE2_ERROR_FAIL;
164 sprintf(vacamsg_text,
165 "Content-type: text/plain charset=utf-8\n"
173 quickie_message( /* This delivers the message */
185 /* Now update the list to reflect the fact that we've alerted this sender.
186 * If they're already in the list, just update the timestamp.
188 for (vptr = cs->u->first_vacation; vptr != NULL; vptr = vptr->next) {
189 if (!strcasecmp(vptr->fromaddr, cs->sender)) {
190 vptr->timestamp = time(NULL);
195 /* If we get to this point, create a new record.
197 vptr = malloc(sizeof(struct sdm_vacation));
198 memset(vptr, 0, sizeof(struct sdm_vacation));
199 vptr->timestamp = time(NULL);
200 safestrncpy(vptr->fromaddr, cs->sender, sizeof vptr->fromaddr);
201 vptr->next = cs->u->first_vacation;
202 cs->u->first_vacation = vptr;
209 * Given the on-disk representation of our Sieve config, load
210 * it into an in-memory data structure.
212 void parse_sieve_config(char *conf, struct sdm_userdata *u) {
216 struct sdm_script *sptr;
217 struct sdm_vacation *vptr;
220 while (c = ptr, ptr = bmstrcasestr(ptr, CTDLSIEVECONFIGSEPARATOR), ptr != NULL) {
222 ptr += strlen(CTDLSIEVECONFIGSEPARATOR);
224 extract_token(keyword, c, 0, '|', sizeof keyword);
226 if (!strcasecmp(keyword, "lastproc")) {
227 u->lastproc = extract_long(c, 1);
230 else if (!strcasecmp(keyword, "script")) {
231 sptr = malloc(sizeof(struct sdm_script));
232 extract_token(sptr->script_name, c, 1, '|', sizeof sptr->script_name);
233 sptr->script_active = extract_int(c, 2);
234 remove_token(c, 0, '|');
235 remove_token(c, 0, '|');
236 remove_token(c, 0, '|');
237 sptr->script_content = strdup(c);
238 sptr->next = u->first_script;
239 u->first_script = sptr;
242 else if (!strcasecmp(keyword, "vacation")) {
244 if (c != NULL) while (vacrec=c, c=strchr(c, '\n'), (c != NULL)) {
249 if (strncasecmp(vacrec, "vacation|", 9)) {
250 vptr = malloc(sizeof(struct sdm_vacation));
251 extract_token(vptr->fromaddr, vacrec, 0, '|', sizeof vptr->fromaddr);
252 vptr->timestamp = extract_long(vacrec, 1);
253 vptr->next = u->first_vacation;
254 u->first_vacation = vptr;
259 /* ignore unknown keywords */
268 * Write our citadel sieve config back to disk
270 * (Set yes_write_to_disk to nonzero to make it actually write the config;
271 * otherwise it just frees the data structures.)
273 void rewrite_ctdl_sieve_config(struct sdm_userdata *u, int yes_write_to_disk) {
275 struct sdm_script *sptr;
276 struct sdm_vacation *vptr;
278 text = NewStrBufPlain(NULL, SIZ);
280 "Content-type: application/x-citadel-sieve-config\n"
282 CTDLSIEVECONFIGSEPARATOR
284 CTDLSIEVECONFIGSEPARATOR
289 while (u->first_script != NULL) {
290 StrBufAppendPrintf(text,
291 "script|%s|%d|%s" CTDLSIEVECONFIGSEPARATOR,
292 u->first_script->script_name,
293 u->first_script->script_active,
294 u->first_script->script_content
296 sptr = u->first_script;
297 u->first_script = u->first_script->next;
298 free(sptr->script_content);
302 if (u->first_vacation != NULL) {
304 StrBufAppendPrintf(text, "vacation|\n");
305 while (u->first_vacation != NULL) {
306 if ( (time(NULL) - u->first_vacation->timestamp) < (MAX_VACATION * 86400)) {
307 StrBufAppendPrintf(text, "%s|%ld\n",
308 u->first_vacation->fromaddr,
309 u->first_vacation->timestamp
312 vptr = u->first_vacation;
313 u->first_vacation = u->first_vacation->next;
316 StrBufAppendPrintf(text, CTDLSIEVECONFIGSEPARATOR);
319 if (yes_write_to_disk)
321 /* Save the config */
322 quickie_message("Citadel", NULL, NULL, u->config_roomname,
325 "Sieve configuration"
328 /* And delete the old one */
329 if (u->config_msgnum > 0) {
330 CtdlDeleteMessages(u->config_roomname, &u->config_msgnum, 1, "");
343 * The next sections are enums and keys that drive the serialize/deserialize functions for the inbox rules/state configuration.
346 // Fields to be compared
364 char *field_keys[] = {
382 // Field comparison operators
391 char *fcomp_keys[] = {
409 char *action_keys[] = {
418 // Size comparison operators
423 char *scomp_keys[] = {
433 char *final_keys[] = {
438 // This data structure represents ONE inbox rule within the configuration.
441 int field_compare_op;
442 char compared_value[128];
446 char file_into[ROOMNAMELEN];
447 char redirect_to[1024];
448 char autoreply_message[SIZ];
452 // This data structure represents the entire inbox rules configuration AND current state for a single user.
460 // Destructor for 'struct inboxrules'
461 void free_inbox_rules(struct inboxrules *ibr) {
467 // Constructor for 'struct inboxrules' that deserializes the configuration from text input.
468 struct inboxrules *deserialize_inbox_rules(char *serialized_rules) {
471 if (!serialized_rules) {
475 /* Make a copy of the supplied buffer because we're going to shit all over it with strtok_r() */
476 char *sr = strdup(serialized_rules);
481 struct inboxrules *ibr = malloc(sizeof(struct inboxrules));
485 memset(ibr, 0, sizeof(struct inboxrules));
489 while ((token = strtok_r(rest, "\n", &rest))) {
491 // For backwards compatibility, "# WEBCIT_RULE" is an alias for "rule".
492 // Prior to version 930, WebCit converted its rules to Sieve scripts, but saved the rules as comments for later re-editing.
493 // Now, the rules hidden in the comments become the real rules.
494 if (!strncasecmp(token, "# WEBCIT_RULE|", 14)) {
495 strcpy(token, "rule|");
496 strcpy(&token[5], &token[14]);
499 // Lines containing actual rules are double-serialized with Base64. It seemed like a good idea at the time :(
500 if (!strncasecmp(token, "rule|", 5)) {
501 remove_token(&token[5], 0, '|');
502 char *decoded_rule = malloc(strlen(token));
503 CtdlDecodeBase64(decoded_rule, &token[5], strlen(&token[5]));
505 ibr->rules = realloc(ibr->rules, (sizeof(struct irule) * ibr->num_rules));
506 struct irule *new_rule = &ibr->rules[ibr->num_rules - 1];
507 memset(new_rule, 0, sizeof(struct irule));
509 // We have a rule , now parse it
511 int nt = num_tokens(decoded_rule, '|');
512 for (int t=0; t<nt; ++t) {
513 extract_token(rtoken, decoded_rule, t, '|', sizeof(rtoken));
516 case 1: // field to compare
517 for (i=0; i<=field_all; ++i) {
518 if (!strcasecmp(rtoken, field_keys[i])) {
519 new_rule->compared_field = i;
523 case 2: // field comparison operation
524 for (i=0; i<=fcomp_notmatches; ++i) {
525 if (!strcasecmp(rtoken, fcomp_keys[i])) {
526 new_rule->field_compare_op = i;
530 case 3: // field comparison value
531 safestrncpy(new_rule->compared_value, rtoken, sizeof(new_rule->compared_value));
533 case 4: // size comparison operation
534 for (i=0; i<=scomp_smaller; ++i) {
535 if (!strcasecmp(rtoken, scomp_keys[i])) {
536 new_rule->size_compare_op = i;
540 case 5: // size comparison value
541 new_rule->compared_size = atol(rtoken);
544 for (i=0; i<=action_vacation; ++i) {
545 if (!strcasecmp(rtoken, action_keys[i])) {
546 new_rule->action = i;
550 case 7: // file into (target room)
551 safestrncpy(new_rule->file_into, rtoken, sizeof(new_rule->file_into));
553 case 8: // redirect to (target address)
554 safestrncpy(new_rule->redirect_to, rtoken, sizeof(new_rule->redirect_to));
556 case 9: // autoreply message
557 safestrncpy(new_rule->autoreply_message, rtoken, sizeof(new_rule->autoreply_message));
559 case 10: // final_action;
560 for (i=0; i<=final_stop; ++i) {
561 if (!strcasecmp(rtoken, final_keys[i])) {
562 new_rule->final_action = i;
572 // if we re-serialized this now, what would it look like?
573 syslog(LOG_DEBUG, "test reserialize: 0|%s|%s|%s|%s|%ld|%s|%s|%s|%s|%s",
574 field_keys[new_rule->compared_field],
575 fcomp_keys[new_rule->field_compare_op],
576 new_rule->compared_value,
577 scomp_keys[new_rule->size_compare_op],
578 new_rule->compared_size,
579 action_keys[new_rule->action],
581 new_rule->redirect_to,
582 new_rule->autoreply_message,
583 final_keys[new_rule->final_action]
585 // delete the above after moving it to a reserialize function
589 // "lastproc" indicates the newest message number in the inbox that was previously processed by our inbox rules.
590 else if (!strncasecmp(token, "lastproc|", 5)) {
591 ibr->lastproc = atol(&token[9]);
596 free(sr); // free our copy of the source buffer that has now been trashed with null bytes...
597 return(ibr); // and return our complex data type to the caller.
601 // Perform the "fileinto" action (save the message in another room)
602 // Returns: 1 or 0 to tell the caller to keep (1) or delete (0) the inbox copy of the message.
604 int inbox_do_fileinto(struct irule *rule, long msgnum) {
605 char *dest_folder = rule->file_into;
606 char original_room_name[ROOMNAMELEN];
607 char foldername[ROOMNAMELEN];
610 // Situations where we want to just keep the message in the inbox:
612 (IsEmptyStr(dest_folder)) // no destination room was specified
613 || (!strcasecmp(dest_folder, "INBOX")) // fileinto inbox is the same as keep
614 || (!strcasecmp(dest_folder, MAILROOM)) // fileinto inbox is the same as keep
619 /* Remember what room we came from */
620 safestrncpy(original_room_name, CC->room.QRname, sizeof original_room_name);
622 /* First try a mailbox name match (check personal mail folders first) */
623 strcpy(foldername, original_room_name); // This keeps the user namespace of the inbox
624 snprintf(&foldername[10], sizeof(foldername)-10, ".%s", dest_folder); // And this tacks on the target room name
625 c = CtdlGetRoom(&CC->room, foldername);
627 /* Then a regular room name match (public and private rooms) */
629 safestrncpy(foldername, dest_folder, sizeof foldername);
630 c = CtdlGetRoom(&CC->room, foldername);
634 syslog(LOG_WARNING, "inboxrules: target <%s> does not exist", dest_folder);
638 /* Yes, we actually have to go there */
639 CtdlUserGoto(NULL, 0, 0, NULL, NULL, NULL, NULL);
641 c = CtdlSaveMsgPointersInRoom(NULL, &msgnum, 1, 0, NULL, 0);
643 /* Go back to the room we came from */
644 if (strcasecmp(original_room_name, CC->room.QRname)) {
645 CtdlUserGoto(original_room_name, 0, 0, NULL, NULL, NULL, NULL);
648 return(0); // delete the inbox copy
652 // Perform the "reject" action (delete the message, and tell the sender we deleted it)
653 // Returns: 1 or 0 to tell the caller to keep (1) or delete (0) the inbox copy of the message.
655 int inbox_do_reject(struct irule *rule, struct CtdlMessage *msg) {
656 syslog(LOG_DEBUG, "inbox_do_reject: sender: <%s>, reject message: <%s>",
657 msg->cm_fields[erFc822Addr],
658 rule->autoreply_message
661 // If we can't determine who sent the message, reject silently.
663 if (!IsEmptyStr(msg->cm_fields[eMessagePath])) {
664 sender = msg->cm_fields[eMessagePath];
666 else if (!IsEmptyStr(msg->cm_fields[erFc822Addr])) {
667 sender = msg->cm_fields[erFc822Addr];
673 // Assemble the reject message.
674 char *reject_text = malloc(strlen(rule->autoreply_message) + 1024);
675 if (reject_text == NULL) {
679 "Content-type: text/plain\n"
681 "The message was refused by the recipient's mail filtering program.\n"
682 "The reason given was as follows:\n"
687 rule->autoreply_message
690 // Deliver the message
693 msg->cm_fields[eenVelopeTo],
698 "Delivery status notification"
706 * Process a single message. We know the room, the user, the rules, the message number, etc.
708 void inbox_do_msg(long msgnum, void *userdata) {
709 struct inboxrules *ii = (struct inboxrules *) userdata;
710 struct CtdlMessage *msg = NULL; // If we are loading a message to process, put it here.
711 int headers_loaded = 0; // Did we load the headers yet? Do it only once.
712 int body_loaded = 0; // Did we load the message body yet? Do it only once.
713 int metadata_loaded = 0; // Did we load the metadata yet? Do it only once.
714 struct MetaData smi; // If we are loading the metadata to compare, put it here.
715 int rule_activated = 0; // On each rule, this is set if the compare succeeds and the rule activates.
716 char compare_me[SIZ]; // On each rule, we will store the field to be compared here.
717 int keep_message = 1; // Nonzero to keep the message in the inbox after processing, 0 to delete it.
720 syslog(LOG_DEBUG, "inboxrules: processing message #%ld which is higher than %ld, we are in %s", msgnum, ii->lastproc, CC->room.QRname);
722 if (ii->num_rules <= 0) {
723 syslog(LOG_DEBUG, "inboxrules: rule set is empty");
727 for (i=0; i<ii->num_rules; ++i) {
728 syslog(LOG_DEBUG, "inboxrules: processing rule %d is %s", i, field_keys[ ii->rules[i].compared_field ]);
731 // Before doing a field compare, check to see if we have the correct parts of the message in memory.
733 switch(ii->rules[i].compared_field) {
734 // These fields require loading only the top-level headers
735 case field_from: // From:
736 case field_tocc: // To: or Cc:
737 case field_subject: // Subject:
738 case field_replyto: // Reply-to:
739 case field_listid: // List-ID:
740 case field_envto: // Envelope-to:
741 case field_envfrom: // Return-path:
742 if (!headers_loaded) {
743 syslog(LOG_DEBUG, "inboxrules: loading headers for message %ld", msgnum);
744 msg = CtdlFetchMessage(msgnum, 0);
751 // These fields are not stored as Citadel headers, and therefore require a full message load.
753 case field_resentfrom:
756 case field_xspamflag:
757 case field_xspamstatus:
759 syslog(LOG_DEBUG, "inboxrules: loading all of message %ld", msgnum);
763 msg = CtdlFetchMessage(msgnum, 1);
772 if (!metadata_loaded) {
773 syslog(LOG_DEBUG, "inboxrules: loading metadata for message %ld", msgnum);
774 GetMetaData(&smi, msgnum);
779 syslog(LOG_DEBUG, "inboxrules: this is an always-on rule");
782 syslog(LOG_DEBUG, "inboxrules: unknown rule key");
785 // If the rule involves a field comparison, load the field to be compared.
787 switch(ii->rules[i].compared_field) {
789 case field_from: // From:
790 if (!IsEmptyStr(msg->cm_fields[erFc822Addr])) {
791 safestrncpy(compare_me, msg->cm_fields[erFc822Addr], sizeof compare_me);
794 case field_tocc: // To: or Cc:
795 if (!IsEmptyStr(msg->cm_fields[eRecipient])) {
796 safestrncpy(compare_me, msg->cm_fields[eRecipient], sizeof compare_me);
798 if (!IsEmptyStr(msg->cm_fields[eCarbonCopY])) {
799 if (!IsEmptyStr(compare_me)) {
800 strcat(compare_me, ",");
802 safestrncpy(&compare_me[strlen(compare_me)], msg->cm_fields[eCarbonCopY], (sizeof compare_me - strlen(compare_me)));
805 case field_subject: // Subject:
806 if (!IsEmptyStr(msg->cm_fields[eMsgSubject])) {
807 safestrncpy(compare_me, msg->cm_fields[eMsgSubject], sizeof compare_me);
810 case field_replyto: // Reply-to:
811 if (!IsEmptyStr(msg->cm_fields[eReplyTo])) {
812 safestrncpy(compare_me, msg->cm_fields[eReplyTo], sizeof compare_me);
815 case field_listid: // List-ID:
816 if (!IsEmptyStr(msg->cm_fields[eListID])) {
817 safestrncpy(compare_me, msg->cm_fields[eListID], sizeof compare_me);
820 case field_envto: // Envelope-to:
821 if (!IsEmptyStr(msg->cm_fields[eenVelopeTo])) {
822 safestrncpy(compare_me, msg->cm_fields[eenVelopeTo], sizeof compare_me);
825 case field_envfrom: // Return-path:
826 if (!IsEmptyStr(msg->cm_fields[eMessagePath])) {
827 safestrncpy(compare_me, msg->cm_fields[eMessagePath], sizeof compare_me);
832 case field_resentfrom:
835 case field_xspamflag:
836 case field_xspamstatus:
842 // Message data to compare is loaded, now do something.
843 switch(ii->rules[i].compared_field) {
844 case field_from: // From:
845 case field_tocc: // To: or Cc:
846 case field_subject: // Subject:
847 case field_replyto: // Reply-to:
848 case field_listid: // List-ID:
849 case field_envto: // Envelope-to:
850 case field_envfrom: // Return-path:
852 case field_resentfrom:
855 case field_xspamflag:
856 case field_xspamstatus:
858 // For all of the above fields, we can compare the field we've loaded into the buffer.
859 syslog(LOG_DEBUG, "Value of field to compare is: <%s>", compare_me);
860 switch(ii->rules[i].field_compare_op) {
863 rule_activated = (bmstrcasestr(compare_me, ii->rules[i].compared_value) ? 1 : 0);
864 syslog(LOG_DEBUG, "Does %s contain %s? %s", compare_me, ii->rules[i].compared_value, rule_activated?"yes":"no");
866 case fcomp_notcontains:
867 case fcomp_notmatches:
868 rule_activated = (bmstrcasestr(compare_me, ii->rules[i].compared_value) ? 0 : 1);
869 syslog(LOG_DEBUG, "Does %s contain %s? %s", compare_me, ii->rules[i].compared_value, rule_activated?"yes":"no");
872 rule_activated = (strcasecmp(compare_me, ii->rules[i].compared_value) ? 0 : 1);
873 syslog(LOG_DEBUG, "Does %s equal %s? %s", compare_me, ii->rules[i].compared_value, rule_activated?"yes":"no");
876 rule_activated = (strcasecmp(compare_me, ii->rules[i].compared_value) ? 1 : 0);
877 syslog(LOG_DEBUG, "Does %s equal %s? %s", compare_me, ii->rules[i].compared_value, rule_activated?"yes":"no");
884 syslog(LOG_DEBUG, "comparing actual message size %ld to rule message size %ld", smi.meta_rfc822_length, ii->rules[i].compared_size);
885 switch(ii->rules[i].field_compare_op) {
887 rule_activated = ((smi.meta_rfc822_length > ii->rules[i].compared_size) ? 1 : 0);
888 syslog(LOG_DEBUG, "Is %ld larger than %ld? %s", smi.meta_rfc822_length, ii->rules[i].compared_size, (smi.meta_rfc822_length > ii->rules[i].compared_size) ? "yes":"no");
891 rule_activated = ((smi.meta_rfc822_length < ii->rules[i].compared_size) ? 1 : 0);
892 syslog(LOG_DEBUG, "Is %ld smaller than %ld? %s", smi.meta_rfc822_length, ii->rules[i].compared_size, (smi.meta_rfc822_length < ii->rules[i].compared_size) ? "yes":"no");
896 case field_all: // The "all messages" rule ALWAYS triggers
899 default: // no matches, fall through and do nothing
900 syslog(LOG_DEBUG, "inboxrules: an unknown field comparison was encountered");
905 // If the rule matched, perform the requested action.
906 if (rule_activated) {
907 syslog(LOG_DEBUG, "inboxrules: rule activated");
909 // Perform the requested action
910 switch(ii->rules[i].action) {
918 keep_message = inbox_do_reject(&ii->rules[i], msg);
920 case action_fileinto:
921 keep_message = inbox_do_fileinto(&ii->rules[i], msgnum);
923 case action_redirect:
924 // FIXME send it to the recipient
927 case action_vacation:
928 // FIXME send the vacation message
933 // Now perform the "final" action (anything other than "stop" means continue)
934 if (ii->rules[i].final_action == final_stop) {
935 syslog(LOG_DEBUG, "inboxrules: stop processing");
936 i = ii->num_rules + 1; // throw us out of scope to stop
942 syslog(LOG_DEBUG, "inboxrules: rule not activated");
946 if (msg != NULL) { // Delete the copy of the message that is currently in memory. We don't need it anymore.
950 if (!keep_message) { // Delete the copy of the message that is currently in the inbox, if rules dictated that.
951 syslog(LOG_DEBUG, "inboxrules: delete %ld from inbox", msgnum);
952 CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, ""); // we're in the inbox already
955 ii->lastproc = msgnum; // make note of the last message we processed, so we don't scan the whole inbox again
960 * A user account is identified as requring inbox processing.
963 void do_inbox_processing_for_user(long usernum) {
964 struct CtdlMessage *msg;
965 struct inboxrules *ii;
966 char roomname[ROOMNAMELEN];
968 if (CtdlGetUserByNumber(&CC->user, usernum) == 0) {
969 if (CC->user.msgnum_inboxrules <= 0) {
970 return; // this user has no inbox rules
973 msg = CtdlFetchMessage(CC->user.msgnum_inboxrules, 1);
975 return; // config msgnum is set but that message does not exist
978 ii = deserialize_inbox_rules(msg->cm_fields[eMesageText]);
982 return; // config message exists but body is null
985 syslog(LOG_DEBUG, "inboxrules: for %s", CC->user.fullname);
987 // Go to the user's inbox room and process all new messages
988 snprintf(roomname, sizeof roomname, "%010ld.%s", usernum, MAILROOM);
989 if (CtdlGetRoom(&CC->room, roomname) == 0) {
990 CtdlForEachMessage(MSGS_GT, ii->lastproc, NULL, NULL, NULL, inbox_do_msg, (void *) ii);
993 // FIXME reserialize our inbox rules/state and write changes back to the config room
994 free_inbox_rules(ii);
1000 * Here is an array of users (by number) who have received messages in their inbox and may require processing.
1002 long *users_requiring_inbox_processing = NULL;
1004 int num_urip_alloc = 0;
1008 * Perform inbox processing for all rooms which require it
1010 void perform_inbox_processing(void) {
1011 if (num_urip == 0) {
1012 return; // no action required
1015 for (int i=0; i<num_urip; ++i) {
1016 do_inbox_processing_for_user(users_requiring_inbox_processing[i]);
1019 free(users_requiring_inbox_processing);
1020 users_requiring_inbox_processing = NULL;
1027 * This function is called after a message is saved to a room.
1028 * If it's someone's inbox, we have to check for inbox rules
1030 int serv_inboxrules_roomhook(struct ctdlroom *room) {
1032 // Is this someone's inbox?
1033 if (!strcasecmp(&room->QRname[11], MAILROOM)) {
1034 long usernum = atol(room->QRname);
1037 // first check to see if this user is already on the list
1039 for (int i=0; i<=num_urip; ++i) {
1040 if (users_requiring_inbox_processing[i] == usernum) { // already on the list!
1046 // make room if we need to
1047 if (num_urip_alloc == 0) {
1048 num_urip_alloc = 100;
1049 users_requiring_inbox_processing = malloc(sizeof(long) * num_urip_alloc);
1051 else if (num_urip >= num_urip_alloc) {
1052 num_urip_alloc += 100;
1053 users_requiring_inbox_processing = realloc(users_requiring_inbox_processing, (sizeof(long) * num_urip_alloc));
1056 // now add the user to the list
1057 users_requiring_inbox_processing[num_urip++] = usernum;
1061 // No errors are possible from this function.
1070 * This is a client-facing function which fetches the user's inbox rules -- it omits all lines containing anything other than a rule.
1072 * hmmmmm ... should we try to rebuild this in terms of deserialize_inbox_rules() instread?
1074 void cmd_gibr(char *argbuf) {
1076 if (CtdlAccessCheck(ac_logged_in)) return;
1078 cprintf("%d inbox rules for %s\n", LISTING_FOLLOWS, CC->user.fullname);
1080 struct CtdlMessage *msg = CtdlFetchMessage(CC->user.msgnum_inboxrules, 1);
1082 if (!CM_IsEmpty(msg, eMesageText)) {
1084 char *rest = msg->cm_fields[eMesageText];
1085 while ((token = strtok_r(rest, "\n", &rest))) {
1087 // for backwards compatibility, "# WEBCIT_RULE" is an alias for "rule"
1088 if (!strncasecmp(token, "# WEBCIT_RULE|", 14)) {
1089 strcpy(token, "rule|");
1090 strcpy(&token[5], &token[14]);
1093 // Output only lines containing rules.
1094 if (!strncasecmp(token, "rule|", 5)) {
1095 cprintf("%s\n", token);
1108 * User transmits the new inbox rules for the account. They are inserted into the account, replacing the ones already there.
1110 void cmd_pibr(char *argbuf) {
1111 if (CtdlAccessCheck(ac_logged_in)) return;
1114 cprintf("%d send new rules\n", SEND_LISTING);
1115 char *newrules = CtdlReadMessageBody(HKEY("000"), CtdlGetConfigLong("c_maxmsglen"), NULL, 0);
1116 StrBuf *NewConfig = NewStrBufPlain("Content-type: application/x-citadel-sieve-config; charset=UTF-8\nContent-transfer-encoding: 8bit\n\n", -1);
1119 char *rest = newrules;
1120 while ((token = strtok_r(rest, "\n", &rest))) {
1121 // Accept only lines containing rules
1122 if (!strncasecmp(token, "rule|", 5)) {
1123 StrBufAppendBufPlain(NewConfig, token, -1, 0);
1124 StrBufAppendBufPlain(NewConfig, HKEY("\n"), 0);
1129 // Fetch the existing config so we can merge in anything that is NOT a rule
1130 // (Does not start with "rule|" but has at least one vertical bar)
1131 struct CtdlMessage *msg = CtdlFetchMessage(CC->user.msgnum_inboxrules, 1);
1133 if (!CM_IsEmpty(msg, eMesageText)) {
1134 rest = msg->cm_fields[eMesageText];
1135 while ((token = strtok_r(rest, "\n", &rest))) {
1136 // for backwards compatibility, "# WEBCIT_RULE" is an alias for "rule"
1137 if ((strncasecmp(token, "# WEBCIT_RULE|", 14)) && (strncasecmp(token, "rule|", 5)) && (haschar(token, '|'))) {
1138 StrBufAppendBufPlain(NewConfig, token, -1, 0);
1139 StrBufAppendBufPlain(NewConfig, HKEY("\n"), 0);
1146 /* we have composed the new configuration , now save it */
1147 long old_msgnum = CC->user.msgnum_inboxrules;
1148 char userconfigroomname[ROOMNAMELEN];
1149 CtdlMailboxName(userconfigroomname, sizeof userconfigroomname, &CC->user, USERCONFIGROOM);
1150 long new_msgnum = quickie_message("Citadel", NULL, NULL, userconfigroomname, ChrPtr(NewConfig), FMT_RFC822, "inbox rules configuration");
1151 FreeStrBuf(&NewConfig);
1152 CtdlGetUserLock(&CC->user, CC->curr_user);
1153 CC->user.msgnum_inboxrules = new_msgnum;
1154 CtdlPutUserLock(&CC->user);
1155 if (old_msgnum > 0) {
1156 syslog(LOG_DEBUG, "Deleting old message %ld from %s", old_msgnum, userconfigroomname);
1157 CtdlDeleteMessages(userconfigroomname, &old_msgnum, 1, "");
1162 CTDL_MODULE_INIT(sieve)
1166 CtdlRegisterProtoHook(cmd_gibr, "GIBR", "Get InBox Rules");
1167 CtdlRegisterProtoHook(cmd_pibr, "PIBR", "Put InBox Rules");
1168 CtdlRegisterRoomHook(serv_inboxrules_roomhook);
1169 CtdlRegisterSessionHook(perform_inbox_processing, EVT_HOUSE, PRIO_HOUSE + 10);
1172 /* return our module name for the log */
1173 return "inboxrules";