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"
59 * Callback function to indicate that a vacation message should be generated
61 int ctdl_vacation(sieve2_context_t *s, void *my)
63 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
64 struct sdm_vacation *vptr;
67 char *vacamsg_text = NULL;
68 char vacamsg_subject[1024];
70 syslog(LOG_DEBUG, "Action is VACATION");
72 message = sieve2_getvalue_string(s, "message");
73 if (message == NULL) return SIEVE2_ERROR_BADARGS;
75 if (sieve2_getvalue_string(s, "subject") != NULL) {
76 safestrncpy(vacamsg_subject, sieve2_getvalue_string(s, "subject"), sizeof vacamsg_subject);
79 snprintf(vacamsg_subject, sizeof vacamsg_subject, "Re: %s", cs->subject);
82 days = sieve2_getvalue_int(s, "days");
83 if (days < 1) days = 1;
84 if (days > MAX_VACATION) days = MAX_VACATION;
86 /* Check to see whether we've already alerted this sender that we're on vacation. */
87 for (vptr = cs->u->first_vacation; vptr != NULL; vptr = vptr->next) {
88 if (!strcasecmp(vptr->fromaddr, cs->sender)) {
89 if ( (time(NULL) - vptr->timestamp) < (days * 86400) ) {
90 syslog(LOG_DEBUG, "Already alerted <%s> recently.", cs->sender);
96 /* Assemble the reject message. */
97 vacamsg_text = malloc(strlen(message) + 1024);
98 if (vacamsg_text == NULL) {
99 return SIEVE2_ERROR_FAIL;
102 sprintf(vacamsg_text,
103 "Content-type: text/plain charset=utf-8\n"
111 quickie_message( /* This delivers the message */
123 /* Now update the list to reflect the fact that we've alerted this sender.
124 * If they're already in the list, just update the timestamp.
126 for (vptr = cs->u->first_vacation; vptr != NULL; vptr = vptr->next) {
127 if (!strcasecmp(vptr->fromaddr, cs->sender)) {
128 vptr->timestamp = time(NULL);
133 /* If we get to this point, create a new record.
135 vptr = malloc(sizeof(struct sdm_vacation));
136 memset(vptr, 0, sizeof(struct sdm_vacation));
137 vptr->timestamp = time(NULL);
138 safestrncpy(vptr->fromaddr, cs->sender, sizeof vptr->fromaddr);
139 vptr->next = cs->u->first_vacation;
140 cs->u->first_vacation = vptr;
147 * Given the on-disk representation of our Sieve config, load
148 * it into an in-memory data structure.
150 void parse_sieve_config(char *conf, struct sdm_userdata *u) {
154 struct sdm_script *sptr;
155 struct sdm_vacation *vptr;
158 while (c = ptr, ptr = bmstrcasestr(ptr, CTDLSIEVECONFIGSEPARATOR), ptr != NULL) {
160 ptr += strlen(CTDLSIEVECONFIGSEPARATOR);
162 extract_token(keyword, c, 0, '|', sizeof keyword);
164 if (!strcasecmp(keyword, "lastproc")) {
165 u->lastproc = extract_long(c, 1);
168 else if (!strcasecmp(keyword, "script")) {
169 sptr = malloc(sizeof(struct sdm_script));
170 extract_token(sptr->script_name, c, 1, '|', sizeof sptr->script_name);
171 sptr->script_active = extract_int(c, 2);
172 remove_token(c, 0, '|');
173 remove_token(c, 0, '|');
174 remove_token(c, 0, '|');
175 sptr->script_content = strdup(c);
176 sptr->next = u->first_script;
177 u->first_script = sptr;
180 else if (!strcasecmp(keyword, "vacation")) {
182 if (c != NULL) while (vacrec=c, c=strchr(c, '\n'), (c != NULL)) {
187 if (strncasecmp(vacrec, "vacation|", 9)) {
188 vptr = malloc(sizeof(struct sdm_vacation));
189 extract_token(vptr->fromaddr, vacrec, 0, '|', sizeof vptr->fromaddr);
190 vptr->timestamp = extract_long(vacrec, 1);
191 vptr->next = u->first_vacation;
192 u->first_vacation = vptr;
197 /* ignore unknown keywords */
206 * Write our citadel sieve config back to disk
208 * (Set yes_write_to_disk to nonzero to make it actually write the config;
209 * otherwise it just frees the data structures.)
211 void rewrite_ctdl_sieve_config(struct sdm_userdata *u, int yes_write_to_disk) {
213 struct sdm_script *sptr;
214 struct sdm_vacation *vptr;
216 text = NewStrBufPlain(NULL, SIZ);
218 "Content-type: application/x-citadel-sieve-config\n"
220 CTDLSIEVECONFIGSEPARATOR
222 CTDLSIEVECONFIGSEPARATOR
227 while (u->first_script != NULL) {
228 StrBufAppendPrintf(text,
229 "script|%s|%d|%s" CTDLSIEVECONFIGSEPARATOR,
230 u->first_script->script_name,
231 u->first_script->script_active,
232 u->first_script->script_content
234 sptr = u->first_script;
235 u->first_script = u->first_script->next;
236 free(sptr->script_content);
240 if (u->first_vacation != NULL) {
242 StrBufAppendPrintf(text, "vacation|\n");
243 while (u->first_vacation != NULL) {
244 if ( (time(NULL) - u->first_vacation->timestamp) < (MAX_VACATION * 86400)) {
245 StrBufAppendPrintf(text, "%s|%ld\n",
246 u->first_vacation->fromaddr,
247 u->first_vacation->timestamp
250 vptr = u->first_vacation;
251 u->first_vacation = u->first_vacation->next;
254 StrBufAppendPrintf(text, CTDLSIEVECONFIGSEPARATOR);
257 if (yes_write_to_disk)
259 /* Save the config */
260 quickie_message("Citadel", NULL, NULL, u->config_roomname,
263 "Sieve configuration"
266 /* And delete the old one */
267 if (u->config_msgnum > 0) {
268 CtdlDeleteMessages(u->config_roomname, &u->config_msgnum, 1, "");
281 * The next sections are enums and keys that drive the serialize/deserialize functions for the inbox rules/state configuration.
284 // Fields to be compared
302 char *field_keys[] = {
320 // Field comparison operators
329 char *fcomp_keys[] = {
347 char *action_keys[] = {
356 // Size comparison operators
361 char *scomp_keys[] = {
371 char *final_keys[] = {
376 // This data structure represents ONE inbox rule within the configuration.
379 int field_compare_op;
380 char compared_value[128];
384 char file_into[ROOMNAMELEN];
385 char redirect_to[1024];
386 char autoreply_message[SIZ];
390 // This data structure represents the entire inbox rules configuration AND current state for a single user.
398 // Destructor for 'struct inboxrules'
399 void free_inbox_rules(struct inboxrules *ibr) {
405 // Constructor for 'struct inboxrules' that deserializes the configuration from text input.
406 struct inboxrules *deserialize_inbox_rules(char *serialized_rules) {
409 if (!serialized_rules) {
413 /* Make a copy of the supplied buffer because we're going to shit all over it with strtok_r() */
414 char *sr = strdup(serialized_rules);
419 struct inboxrules *ibr = malloc(sizeof(struct inboxrules));
423 memset(ibr, 0, sizeof(struct inboxrules));
427 while ((token = strtok_r(rest, "\n", &rest))) {
429 // For backwards compatibility, "# WEBCIT_RULE" is an alias for "rule".
430 // Prior to version 930, WebCit converted its rules to Sieve scripts, but saved the rules as comments for later re-editing.
431 // Now, the rules hidden in the comments become the real rules.
432 if (!strncasecmp(token, "# WEBCIT_RULE|", 14)) {
433 strcpy(token, "rule|");
434 strcpy(&token[5], &token[14]);
437 // Lines containing actual rules are double-serialized with Base64. It seemed like a good idea at the time :(
438 if (!strncasecmp(token, "rule|", 5)) {
439 remove_token(&token[5], 0, '|');
440 char *decoded_rule = malloc(strlen(token));
441 CtdlDecodeBase64(decoded_rule, &token[5], strlen(&token[5]));
443 ibr->rules = realloc(ibr->rules, (sizeof(struct irule) * ibr->num_rules));
444 struct irule *new_rule = &ibr->rules[ibr->num_rules - 1];
445 memset(new_rule, 0, sizeof(struct irule));
447 // We have a rule , now parse it
449 int nt = num_tokens(decoded_rule, '|');
450 for (int t=0; t<nt; ++t) {
451 extract_token(rtoken, decoded_rule, t, '|', sizeof(rtoken));
454 case 1: // field to compare
455 for (i=0; i<=field_all; ++i) {
456 if (!strcasecmp(rtoken, field_keys[i])) {
457 new_rule->compared_field = i;
461 case 2: // field comparison operation
462 for (i=0; i<=fcomp_notmatches; ++i) {
463 if (!strcasecmp(rtoken, fcomp_keys[i])) {
464 new_rule->field_compare_op = i;
468 case 3: // field comparison value
469 safestrncpy(new_rule->compared_value, rtoken, sizeof(new_rule->compared_value));
471 case 4: // size comparison operation
472 for (i=0; i<=scomp_smaller; ++i) {
473 if (!strcasecmp(rtoken, scomp_keys[i])) {
474 new_rule->size_compare_op = i;
478 case 5: // size comparison value
479 new_rule->compared_size = atol(rtoken);
482 for (i=0; i<=action_vacation; ++i) {
483 if (!strcasecmp(rtoken, action_keys[i])) {
484 new_rule->action = i;
488 case 7: // file into (target room)
489 safestrncpy(new_rule->file_into, rtoken, sizeof(new_rule->file_into));
491 case 8: // redirect to (target address)
492 safestrncpy(new_rule->redirect_to, rtoken, sizeof(new_rule->redirect_to));
494 case 9: // autoreply message
495 safestrncpy(new_rule->autoreply_message, rtoken, sizeof(new_rule->autoreply_message));
497 case 10: // final_action;
498 for (i=0; i<=final_stop; ++i) {
499 if (!strcasecmp(rtoken, final_keys[i])) {
500 new_rule->final_action = i;
510 // if we re-serialized this now, what would it look like?
511 syslog(LOG_DEBUG, "test reserialize: 0|%s|%s|%s|%s|%ld|%s|%s|%s|%s|%s",
512 field_keys[new_rule->compared_field],
513 fcomp_keys[new_rule->field_compare_op],
514 new_rule->compared_value,
515 scomp_keys[new_rule->size_compare_op],
516 new_rule->compared_size,
517 action_keys[new_rule->action],
519 new_rule->redirect_to,
520 new_rule->autoreply_message,
521 final_keys[new_rule->final_action]
523 // delete the above after moving it to a reserialize function
527 // "lastproc" indicates the newest message number in the inbox that was previously processed by our inbox rules.
528 else if (!strncasecmp(token, "lastproc|", 5)) {
529 ibr->lastproc = atol(&token[9]);
534 free(sr); // free our copy of the source buffer that has now been trashed with null bytes...
535 return(ibr); // and return our complex data type to the caller.
539 // Perform the "fileinto" action (save the message in another room)
540 // Returns: 1 or 0 to tell the caller to keep (1) or delete (0) the inbox copy of the message.
542 int inbox_do_fileinto(struct irule *rule, long msgnum) {
543 char *dest_folder = rule->file_into;
544 char original_room_name[ROOMNAMELEN];
545 char foldername[ROOMNAMELEN];
548 // Situations where we want to just keep the message in the inbox:
550 (IsEmptyStr(dest_folder)) // no destination room was specified
551 || (!strcasecmp(dest_folder, "INBOX")) // fileinto inbox is the same as keep
552 || (!strcasecmp(dest_folder, MAILROOM)) // fileinto "Mail" is the same as keep
554 return(1); // don't delete the inbox copy if this failed
557 // Remember what room we came from
558 safestrncpy(original_room_name, CC->room.QRname, sizeof original_room_name);
560 // First try a mailbox name match (check personal mail folders first)
561 strcpy(foldername, original_room_name); // This keeps the user namespace of the inbox
562 snprintf(&foldername[10], sizeof(foldername)-10, ".%s", dest_folder); // And this tacks on the target room name
563 c = CtdlGetRoom(&CC->room, foldername);
565 // Then a regular room name match (public and private rooms)
567 safestrncpy(foldername, dest_folder, sizeof foldername);
568 c = CtdlGetRoom(&CC->room, foldername);
572 syslog(LOG_WARNING, "inboxrules: target <%s> does not exist", dest_folder);
573 return(1); // don't delete the inbox copy if this failed
576 // Yes, we actually have to go there
577 CtdlUserGoto(NULL, 0, 0, NULL, NULL, NULL, NULL);
579 c = CtdlSaveMsgPointersInRoom(NULL, &msgnum, 1, 0, NULL, 0);
581 // Go back to the room we came from
582 if (strcasecmp(original_room_name, CC->room.QRname)) {
583 CtdlUserGoto(original_room_name, 0, 0, NULL, NULL, NULL, NULL);
586 return(0); // delete the inbox copy
590 // Perform the "redirect" action (divert the message to another email address)
591 // Returns: 1 or 0 to tell the caller to keep (1) or delete (0) the inbox copy of the message.
593 int inbox_do_redirect(struct irule *rule, long msgnum) {
594 if (IsEmptyStr(rule->redirect_to)) {
595 syslog(LOG_WARNING, "inboxrules: inbox_do_redirect() invalid recipient <%s>", rule->redirect_to);
596 return(1); // don't delete the inbox copy if this failed
599 recptypes *valid = validate_recipients(rule->redirect_to, NULL, 0);
601 syslog(LOG_WARNING, "inboxrules: inbox_do_redirect() invalid recipient <%s>", rule->redirect_to);
602 return(1); // don't delete the inbox copy if this failed
604 if (valid->num_error > 0) {
605 free_recipients(valid);
606 syslog(LOG_WARNING, "inboxrules: inbox_do_redirect() invalid recipient <%s>", rule->redirect_to);
607 return(1); // don't delete the inbox copy if this failed
610 struct CtdlMessage *msg = CtdlFetchMessage(msgnum, 1);
612 free_recipients(valid);
613 syslog(LOG_WARNING, "inboxrules: cannot reload message %ld for forwarding", msgnum);
614 return(1); // don't delete the inbox copy if this failed
617 CtdlSubmitMsg(msg, valid, NULL, 0); // send the message to the new recipient
618 free_recipients(valid);
620 return(0); // delete the inbox copy
624 // Perform the "reject" action (delete the message, and tell the sender we deleted it)
626 void inbox_do_reject(struct irule *rule, struct CtdlMessage *msg) {
627 syslog(LOG_DEBUG, "inbox_do_reject: sender: <%s>, reject message: <%s>",
628 msg->cm_fields[erFc822Addr],
629 rule->autoreply_message
632 // If we can't determine who sent the message, reject silently.
634 if (!IsEmptyStr(msg->cm_fields[eMessagePath])) {
635 sender = msg->cm_fields[eMessagePath];
637 else if (!IsEmptyStr(msg->cm_fields[erFc822Addr])) {
638 sender = msg->cm_fields[erFc822Addr];
644 // Assemble the reject message.
645 char *reject_text = malloc(strlen(rule->autoreply_message) + 1024);
646 if (reject_text == NULL) {
650 "Content-type: text/plain\n"
652 "The message was refused by the recipient's mail filtering program.\n"
653 "The reason given was as follows:\n"
658 rule->autoreply_message
661 // Deliver the message
664 msg->cm_fields[eenVelopeTo],
669 "Delivery status notification"
676 * Process a single message. We know the room, the user, the rules, the message number, etc.
678 void inbox_do_msg(long msgnum, void *userdata) {
679 struct inboxrules *ii = (struct inboxrules *) userdata;
680 struct CtdlMessage *msg = NULL; // If we are loading a message to process, put it here.
681 int headers_loaded = 0; // Did we load the headers yet? Do it only once.
682 int body_loaded = 0; // Did we load the message body yet? Do it only once.
683 int metadata_loaded = 0; // Did we load the metadata yet? Do it only once.
684 struct MetaData smi; // If we are loading the metadata to compare, put it here.
685 int rule_activated = 0; // On each rule, this is set if the compare succeeds and the rule activates.
686 char compare_me[SIZ]; // On each rule, we will store the field to be compared here.
687 int keep_message = 1; // Nonzero to keep the message in the inbox after processing, 0 to delete it.
690 syslog(LOG_DEBUG, "inboxrules: processing message #%ld which is higher than %ld, we are in %s", msgnum, ii->lastproc, CC->room.QRname);
692 if (ii->num_rules <= 0) {
693 syslog(LOG_DEBUG, "inboxrules: rule set is empty");
697 for (i=0; i<ii->num_rules; ++i) {
698 syslog(LOG_DEBUG, "inboxrules: processing rule %d is %s", i, field_keys[ ii->rules[i].compared_field ]);
701 // Before doing a field compare, check to see if we have the correct parts of the message in memory.
703 switch(ii->rules[i].compared_field) {
704 // These fields require loading only the top-level headers
705 case field_from: // From:
706 case field_tocc: // To: or Cc:
707 case field_subject: // Subject:
708 case field_replyto: // Reply-to:
709 case field_listid: // List-ID:
710 case field_envto: // Envelope-to:
711 case field_envfrom: // Return-path:
712 if (!headers_loaded) {
713 syslog(LOG_DEBUG, "inboxrules: loading headers for message %ld", msgnum);
714 msg = CtdlFetchMessage(msgnum, 0);
721 // These fields are not stored as Citadel headers, and therefore require a full message load.
723 case field_resentfrom:
726 case field_xspamflag:
727 case field_xspamstatus:
729 syslog(LOG_DEBUG, "inboxrules: loading all of message %ld", msgnum);
733 msg = CtdlFetchMessage(msgnum, 1);
742 if (!metadata_loaded) {
743 syslog(LOG_DEBUG, "inboxrules: loading metadata for message %ld", msgnum);
744 GetMetaData(&smi, msgnum);
749 syslog(LOG_DEBUG, "inboxrules: this is an always-on rule");
752 syslog(LOG_DEBUG, "inboxrules: unknown rule key");
755 // If the rule involves a field comparison, load the field to be compared.
757 switch(ii->rules[i].compared_field) {
759 case field_from: // From:
760 if (!IsEmptyStr(msg->cm_fields[erFc822Addr])) {
761 safestrncpy(compare_me, msg->cm_fields[erFc822Addr], sizeof compare_me);
764 case field_tocc: // To: or Cc:
765 if (!IsEmptyStr(msg->cm_fields[eRecipient])) {
766 safestrncpy(compare_me, msg->cm_fields[eRecipient], sizeof compare_me);
768 if (!IsEmptyStr(msg->cm_fields[eCarbonCopY])) {
769 if (!IsEmptyStr(compare_me)) {
770 strcat(compare_me, ",");
772 safestrncpy(&compare_me[strlen(compare_me)], msg->cm_fields[eCarbonCopY], (sizeof compare_me - strlen(compare_me)));
775 case field_subject: // Subject:
776 if (!IsEmptyStr(msg->cm_fields[eMsgSubject])) {
777 safestrncpy(compare_me, msg->cm_fields[eMsgSubject], sizeof compare_me);
780 case field_replyto: // Reply-to:
781 if (!IsEmptyStr(msg->cm_fields[eReplyTo])) {
782 safestrncpy(compare_me, msg->cm_fields[eReplyTo], sizeof compare_me);
785 case field_listid: // List-ID:
786 if (!IsEmptyStr(msg->cm_fields[eListID])) {
787 safestrncpy(compare_me, msg->cm_fields[eListID], sizeof compare_me);
790 case field_envto: // Envelope-to:
791 if (!IsEmptyStr(msg->cm_fields[eenVelopeTo])) {
792 safestrncpy(compare_me, msg->cm_fields[eenVelopeTo], sizeof compare_me);
795 case field_envfrom: // Return-path:
796 if (!IsEmptyStr(msg->cm_fields[eMessagePath])) {
797 safestrncpy(compare_me, msg->cm_fields[eMessagePath], sizeof compare_me);
802 case field_resentfrom:
805 case field_xspamflag:
806 case field_xspamstatus:
812 // Message data to compare is loaded, now do something.
813 switch(ii->rules[i].compared_field) {
814 case field_from: // From:
815 case field_tocc: // To: or Cc:
816 case field_subject: // Subject:
817 case field_replyto: // Reply-to:
818 case field_listid: // List-ID:
819 case field_envto: // Envelope-to:
820 case field_envfrom: // Return-path:
822 case field_resentfrom:
825 case field_xspamflag:
826 case field_xspamstatus:
828 // For all of the above fields, we can compare the field we've loaded into the buffer.
829 syslog(LOG_DEBUG, "Value of field to compare is: <%s>", compare_me);
830 switch(ii->rules[i].field_compare_op) {
833 rule_activated = (bmstrcasestr(compare_me, ii->rules[i].compared_value) ? 1 : 0);
834 syslog(LOG_DEBUG, "Does %s contain %s? %s", compare_me, ii->rules[i].compared_value, rule_activated?"yes":"no");
836 case fcomp_notcontains:
837 case fcomp_notmatches:
838 rule_activated = (bmstrcasestr(compare_me, ii->rules[i].compared_value) ? 0 : 1);
839 syslog(LOG_DEBUG, "Does %s contain %s? %s", compare_me, ii->rules[i].compared_value, rule_activated?"yes":"no");
842 rule_activated = (strcasecmp(compare_me, ii->rules[i].compared_value) ? 0 : 1);
843 syslog(LOG_DEBUG, "Does %s equal %s? %s", compare_me, ii->rules[i].compared_value, rule_activated?"yes":"no");
846 rule_activated = (strcasecmp(compare_me, ii->rules[i].compared_value) ? 1 : 0);
847 syslog(LOG_DEBUG, "Does %s equal %s? %s", compare_me, ii->rules[i].compared_value, rule_activated?"yes":"no");
854 syslog(LOG_DEBUG, "comparing actual message size %ld to rule message size %ld", smi.meta_rfc822_length, ii->rules[i].compared_size);
855 switch(ii->rules[i].field_compare_op) {
857 rule_activated = ((smi.meta_rfc822_length > ii->rules[i].compared_size) ? 1 : 0);
858 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");
861 rule_activated = ((smi.meta_rfc822_length < ii->rules[i].compared_size) ? 1 : 0);
862 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");
866 case field_all: // The "all messages" rule ALWAYS triggers
869 default: // no matches, fall through and do nothing
870 syslog(LOG_DEBUG, "inboxrules: an unknown field comparison was encountered");
875 // If the rule matched, perform the requested action.
876 if (rule_activated) {
877 syslog(LOG_DEBUG, "inboxrules: rule activated");
879 // Perform the requested action
880 switch(ii->rules[i].action) {
888 inbox_do_reject(&ii->rules[i], msg);
891 case action_fileinto:
892 keep_message = inbox_do_fileinto(&ii->rules[i], msgnum);
894 case action_redirect:
895 keep_message = inbox_do_redirect(&ii->rules[i], msgnum);
897 case action_vacation:
898 // inbox_do_vacation(&ii->rules[i], msg);
903 // Now perform the "final" action (anything other than "stop" means continue)
904 if (ii->rules[i].final_action == final_stop) {
905 syslog(LOG_DEBUG, "inboxrules: stop processing");
906 i = ii->num_rules + 1; // throw us out of scope to stop
912 syslog(LOG_DEBUG, "inboxrules: rule not activated");
916 if (msg != NULL) { // Delete the copy of the message that is currently in memory. We don't need it anymore.
920 if (!keep_message) { // Delete the copy of the message that is currently in the inbox, if rules dictated that.
921 syslog(LOG_DEBUG, "inboxrules: delete %ld from inbox", msgnum);
922 CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, ""); // we're in the inbox already
925 ii->lastproc = msgnum; // make note of the last message we processed, so we don't scan the whole inbox again
930 * A user account is identified as requring inbox processing.
933 void do_inbox_processing_for_user(long usernum) {
934 struct CtdlMessage *msg;
935 struct inboxrules *ii;
936 char roomname[ROOMNAMELEN];
938 if (CtdlGetUserByNumber(&CC->user, usernum) == 0) {
939 if (CC->user.msgnum_inboxrules <= 0) {
940 return; // this user has no inbox rules
943 msg = CtdlFetchMessage(CC->user.msgnum_inboxrules, 1);
945 return; // config msgnum is set but that message does not exist
948 ii = deserialize_inbox_rules(msg->cm_fields[eMesageText]);
952 return; // config message exists but body is null
955 syslog(LOG_DEBUG, "inboxrules: for %s", CC->user.fullname);
957 // Go to the user's inbox room and process all new messages
958 snprintf(roomname, sizeof roomname, "%010ld.%s", usernum, MAILROOM);
959 if (CtdlGetRoom(&CC->room, roomname) == 0) {
960 CtdlForEachMessage(MSGS_GT, ii->lastproc, NULL, NULL, NULL, inbox_do_msg, (void *) ii);
963 // FIXME reserialize our inbox rules/state and write changes back to the config room
964 free_inbox_rules(ii);
970 * Here is an array of users (by number) who have received messages in their inbox and may require processing.
972 long *users_requiring_inbox_processing = NULL;
974 int num_urip_alloc = 0;
978 * Perform inbox processing for all rooms which require it
980 void perform_inbox_processing(void) {
982 return; // no action required
985 for (int i=0; i<num_urip; ++i) {
986 do_inbox_processing_for_user(users_requiring_inbox_processing[i]);
989 free(users_requiring_inbox_processing);
990 users_requiring_inbox_processing = NULL;
997 * This function is called after a message is saved to a room.
998 * If it's someone's inbox, we have to check for inbox rules
1000 int serv_inboxrules_roomhook(struct ctdlroom *room) {
1002 // Is this someone's inbox?
1003 if (!strcasecmp(&room->QRname[11], MAILROOM)) {
1004 long usernum = atol(room->QRname);
1007 // first check to see if this user is already on the list
1009 for (int i=0; i<=num_urip; ++i) {
1010 if (users_requiring_inbox_processing[i] == usernum) { // already on the list!
1016 // make room if we need to
1017 if (num_urip_alloc == 0) {
1018 num_urip_alloc = 100;
1019 users_requiring_inbox_processing = malloc(sizeof(long) * num_urip_alloc);
1021 else if (num_urip >= num_urip_alloc) {
1022 num_urip_alloc += 100;
1023 users_requiring_inbox_processing = realloc(users_requiring_inbox_processing, (sizeof(long) * num_urip_alloc));
1026 // now add the user to the list
1027 users_requiring_inbox_processing[num_urip++] = usernum;
1031 // No errors are possible from this function.
1040 * This is a client-facing function which fetches the user's inbox rules -- it omits all lines containing anything other than a rule.
1042 * hmmmmm ... should we try to rebuild this in terms of deserialize_inbox_rules() instread?
1044 void cmd_gibr(char *argbuf) {
1046 if (CtdlAccessCheck(ac_logged_in)) return;
1048 cprintf("%d inbox rules for %s\n", LISTING_FOLLOWS, CC->user.fullname);
1050 struct CtdlMessage *msg = CtdlFetchMessage(CC->user.msgnum_inboxrules, 1);
1052 if (!CM_IsEmpty(msg, eMesageText)) {
1054 char *rest = msg->cm_fields[eMesageText];
1055 while ((token = strtok_r(rest, "\n", &rest))) {
1057 // for backwards compatibility, "# WEBCIT_RULE" is an alias for "rule"
1058 if (!strncasecmp(token, "# WEBCIT_RULE|", 14)) {
1059 strcpy(token, "rule|");
1060 strcpy(&token[5], &token[14]);
1063 // Output only lines containing rules.
1064 if (!strncasecmp(token, "rule|", 5)) {
1065 cprintf("%s\n", token);
1078 * User transmits the new inbox rules for the account. They are inserted into the account, replacing the ones already there.
1080 void cmd_pibr(char *argbuf) {
1081 if (CtdlAccessCheck(ac_logged_in)) return;
1084 cprintf("%d send new rules\n", SEND_LISTING);
1085 char *newrules = CtdlReadMessageBody(HKEY("000"), CtdlGetConfigLong("c_maxmsglen"), NULL, 0);
1086 StrBuf *NewConfig = NewStrBufPlain("Content-type: application/x-citadel-sieve-config; charset=UTF-8\nContent-transfer-encoding: 8bit\n\n", -1);
1089 char *rest = newrules;
1090 while ((token = strtok_r(rest, "\n", &rest))) {
1091 // Accept only lines containing rules
1092 if (!strncasecmp(token, "rule|", 5)) {
1093 StrBufAppendBufPlain(NewConfig, token, -1, 0);
1094 StrBufAppendBufPlain(NewConfig, HKEY("\n"), 0);
1099 // Fetch the existing config so we can merge in anything that is NOT a rule
1100 // (Does not start with "rule|" but has at least one vertical bar)
1101 struct CtdlMessage *msg = CtdlFetchMessage(CC->user.msgnum_inboxrules, 1);
1103 if (!CM_IsEmpty(msg, eMesageText)) {
1104 rest = msg->cm_fields[eMesageText];
1105 while ((token = strtok_r(rest, "\n", &rest))) {
1106 // for backwards compatibility, "# WEBCIT_RULE" is an alias for "rule"
1107 if ((strncasecmp(token, "# WEBCIT_RULE|", 14)) && (strncasecmp(token, "rule|", 5)) && (haschar(token, '|'))) {
1108 StrBufAppendBufPlain(NewConfig, token, -1, 0);
1109 StrBufAppendBufPlain(NewConfig, HKEY("\n"), 0);
1116 /* we have composed the new configuration , now save it */
1117 long old_msgnum = CC->user.msgnum_inboxrules;
1118 char userconfigroomname[ROOMNAMELEN];
1119 CtdlMailboxName(userconfigroomname, sizeof userconfigroomname, &CC->user, USERCONFIGROOM);
1120 long new_msgnum = quickie_message("Citadel", NULL, NULL, userconfigroomname, ChrPtr(NewConfig), FMT_RFC822, "inbox rules configuration");
1121 FreeStrBuf(&NewConfig);
1122 CtdlGetUserLock(&CC->user, CC->curr_user);
1123 CC->user.msgnum_inboxrules = new_msgnum;
1124 CtdlPutUserLock(&CC->user);
1125 if (old_msgnum > 0) {
1126 syslog(LOG_DEBUG, "Deleting old message %ld from %s", old_msgnum, userconfigroomname);
1127 CtdlDeleteMessages(userconfigroomname, &old_msgnum, 1, "");
1132 CTDL_MODULE_INIT(sieve)
1136 CtdlRegisterProtoHook(cmd_gibr, "GIBR", "Get InBox Rules");
1137 CtdlRegisterProtoHook(cmd_pibr, "PIBR", "Put InBox Rules");
1138 CtdlRegisterRoomHook(serv_inboxrules_roomhook);
1139 CtdlRegisterSessionHook(perform_inbox_processing, EVT_HOUSE, PRIO_HOUSE + 10);
1142 /* return our module name for the log */
1143 return "inboxrules";