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;
151 * The next sections are enums and keys that drive the serialize/deserialize functions for the inbox rules/state configuration.
154 // Fields to be compared
172 char *field_keys[] = {
190 // Field comparison operators
199 char *fcomp_keys[] = {
217 char *action_keys[] = {
226 // Size comparison operators
231 char *scomp_keys[] = {
241 char *final_keys[] = {
246 // This data structure represents ONE inbox rule within the configuration.
249 int field_compare_op;
250 char compared_value[128];
254 char file_into[ROOMNAMELEN];
255 char redirect_to[1024];
256 char autoreply_message[SIZ];
260 // This data structure represents the entire inbox rules configuration AND current state for a single user.
268 // Destructor for 'struct inboxrules'
269 void free_inbox_rules(struct inboxrules *ibr) {
275 // Constructor for 'struct inboxrules' that deserializes the configuration from text input.
276 struct inboxrules *deserialize_inbox_rules(char *serialized_rules) {
279 if (!serialized_rules) {
283 /* Make a copy of the supplied buffer because we're going to shit all over it with strtok_r() */
284 char *sr = strdup(serialized_rules);
289 struct inboxrules *ibr = malloc(sizeof(struct inboxrules));
293 memset(ibr, 0, sizeof(struct inboxrules));
297 while ((token = strtok_r(rest, "\n", &rest))) {
299 // For backwards compatibility, "# WEBCIT_RULE" is an alias for "rule".
300 // Prior to version 930, WebCit converted its rules to Sieve scripts, but saved the rules as comments for later re-editing.
301 // Now, the rules hidden in the comments become the real rules.
302 if (!strncasecmp(token, "# WEBCIT_RULE|", 14)) {
303 strcpy(token, "rule|");
304 strcpy(&token[5], &token[14]);
307 // Lines containing actual rules are double-serialized with Base64. It seemed like a good idea at the time :(
308 if (!strncasecmp(token, "rule|", 5)) {
309 remove_token(&token[5], 0, '|');
310 char *decoded_rule = malloc(strlen(token));
311 CtdlDecodeBase64(decoded_rule, &token[5], strlen(&token[5]));
313 ibr->rules = realloc(ibr->rules, (sizeof(struct irule) * ibr->num_rules));
314 struct irule *new_rule = &ibr->rules[ibr->num_rules - 1];
315 memset(new_rule, 0, sizeof(struct irule));
317 // We have a rule , now parse it
319 int nt = num_tokens(decoded_rule, '|');
320 for (int t=0; t<nt; ++t) {
321 extract_token(rtoken, decoded_rule, t, '|', sizeof(rtoken));
324 case 1: // field to compare
325 for (i=0; i<=field_all; ++i) {
326 if (!strcasecmp(rtoken, field_keys[i])) {
327 new_rule->compared_field = i;
331 case 2: // field comparison operation
332 for (i=0; i<=fcomp_notmatches; ++i) {
333 if (!strcasecmp(rtoken, fcomp_keys[i])) {
334 new_rule->field_compare_op = i;
338 case 3: // field comparison value
339 safestrncpy(new_rule->compared_value, rtoken, sizeof(new_rule->compared_value));
341 case 4: // size comparison operation
342 for (i=0; i<=scomp_smaller; ++i) {
343 if (!strcasecmp(rtoken, scomp_keys[i])) {
344 new_rule->size_compare_op = i;
348 case 5: // size comparison value
349 new_rule->compared_size = atol(rtoken);
352 for (i=0; i<=action_vacation; ++i) {
353 if (!strcasecmp(rtoken, action_keys[i])) {
354 new_rule->action = i;
358 case 7: // file into (target room)
359 safestrncpy(new_rule->file_into, rtoken, sizeof(new_rule->file_into));
361 case 8: // redirect to (target address)
362 safestrncpy(new_rule->redirect_to, rtoken, sizeof(new_rule->redirect_to));
364 case 9: // autoreply message
365 safestrncpy(new_rule->autoreply_message, rtoken, sizeof(new_rule->autoreply_message));
367 case 10: // final_action;
368 for (i=0; i<=final_stop; ++i) {
369 if (!strcasecmp(rtoken, final_keys[i])) {
370 new_rule->final_action = i;
381 // "lastproc" indicates the newest message number in the inbox that was previously processed by our inbox rules.
382 // This is a legacy location for this value and will only be used if it's the only one present.
383 else if (!strncasecmp(token, "lastproc|", 5)) {
384 ibr->lastproc = atol(&token[9]);
387 // Lines which do not contain a recognizable token must be IGNORED. These lines may be left over
388 // from a previous version and will disappear when we rewrite the config.
392 free(sr); // free our copy of the source buffer that has now been trashed with null bytes...
393 return(ibr); // and return our complex data type to the caller.
397 // Perform the "fileinto" action (save the message in another room)
398 // Returns: 1 or 0 to tell the caller to keep (1) or delete (0) the inbox copy of the message.
400 int inbox_do_fileinto(struct irule *rule, long msgnum) {
401 char *dest_folder = rule->file_into;
402 char original_room_name[ROOMNAMELEN];
403 char foldername[ROOMNAMELEN];
406 // Situations where we want to just keep the message in the inbox:
408 (IsEmptyStr(dest_folder)) // no destination room was specified
409 || (!strcasecmp(dest_folder, "INBOX")) // fileinto inbox is the same as keep
410 || (!strcasecmp(dest_folder, MAILROOM)) // fileinto "Mail" is the same as keep
412 return(1); // don't delete the inbox copy if this failed
415 // Remember what room we came from
416 safestrncpy(original_room_name, CC->room.QRname, sizeof original_room_name);
418 // First try a mailbox name match (check personal mail folders first)
419 strcpy(foldername, original_room_name); // This keeps the user namespace of the inbox
420 snprintf(&foldername[10], sizeof(foldername)-10, ".%s", dest_folder); // And this tacks on the target room name
421 c = CtdlGetRoom(&CC->room, foldername);
423 // Then a regular room name match (public and private rooms)
425 safestrncpy(foldername, dest_folder, sizeof foldername);
426 c = CtdlGetRoom(&CC->room, foldername);
430 syslog(LOG_WARNING, "inboxrules: target <%s> does not exist", dest_folder);
431 return(1); // don't delete the inbox copy if this failed
434 // Yes, we actually have to go there
435 CtdlUserGoto(NULL, 0, 0, NULL, NULL, NULL, NULL);
437 c = CtdlSaveMsgPointersInRoom(NULL, &msgnum, 1, 0, NULL, 0);
439 // Go back to the room we came from
440 if (strcasecmp(original_room_name, CC->room.QRname)) {
441 CtdlUserGoto(original_room_name, 0, 0, NULL, NULL, NULL, NULL);
444 return(0); // delete the inbox copy
448 // Perform the "redirect" action (divert the message to another email address)
449 // Returns: 1 or 0 to tell the caller to keep (1) or delete (0) the inbox copy of the message.
451 int inbox_do_redirect(struct irule *rule, long msgnum) {
452 if (IsEmptyStr(rule->redirect_to)) {
453 syslog(LOG_WARNING, "inboxrules: inbox_do_redirect() invalid recipient <%s>", rule->redirect_to);
454 return(1); // don't delete the inbox copy if this failed
457 recptypes *valid = validate_recipients(rule->redirect_to, NULL, 0);
459 syslog(LOG_WARNING, "inboxrules: inbox_do_redirect() invalid recipient <%s>", rule->redirect_to);
460 return(1); // don't delete the inbox copy if this failed
462 if (valid->num_error > 0) {
463 free_recipients(valid);
464 syslog(LOG_WARNING, "inboxrules: inbox_do_redirect() invalid recipient <%s>", rule->redirect_to);
465 return(1); // don't delete the inbox copy if this failed
468 struct CtdlMessage *msg = CtdlFetchMessage(msgnum, 1);
470 free_recipients(valid);
471 syslog(LOG_WARNING, "inboxrules: cannot reload message %ld for forwarding", msgnum);
472 return(1); // don't delete the inbox copy if this failed
475 CtdlSubmitMsg(msg, valid, NULL, 0); // send the message to the new recipient
476 free_recipients(valid);
478 return(0); // delete the inbox copy
482 // Perform the "reject" action (delete the message, and tell the sender we deleted it)
484 void inbox_do_reject(struct irule *rule, struct CtdlMessage *msg) {
485 syslog(LOG_DEBUG, "inbox_do_reject: sender: <%s>, reject message: <%s>",
486 msg->cm_fields[erFc822Addr],
487 rule->autoreply_message
490 // If we can't determine who sent the message, reject silently.
492 if (!IsEmptyStr(msg->cm_fields[eMessagePath])) {
493 sender = msg->cm_fields[eMessagePath];
495 else if (!IsEmptyStr(msg->cm_fields[erFc822Addr])) {
496 sender = msg->cm_fields[erFc822Addr];
502 // Assemble the reject message.
503 char *reject_text = malloc(strlen(rule->autoreply_message) + 1024);
504 if (reject_text == NULL) {
508 "Content-type: text/plain\n"
510 "The message was refused by the recipient's mail filtering program.\n"
511 "The reason given was as follows:\n"
516 rule->autoreply_message
519 // Deliver the message
522 msg->cm_fields[eenVelopeTo],
527 "Delivery status notification"
534 * Process a single message. We know the room, the user, the rules, the message number, etc.
536 void inbox_do_msg(long msgnum, void *userdata) {
537 struct inboxrules *ii = (struct inboxrules *) userdata;
538 struct CtdlMessage *msg = NULL; // If we are loading a message to process, put it here.
539 int headers_loaded = 0; // Did we load the headers yet? Do it only once.
540 int body_loaded = 0; // Did we load the message body yet? Do it only once.
541 int metadata_loaded = 0; // Did we load the metadata yet? Do it only once.
542 struct MetaData smi; // If we are loading the metadata to compare, put it here.
543 int rule_activated = 0; // On each rule, this is set if the compare succeeds and the rule activates.
544 char compare_me[SIZ]; // On each rule, we will store the field to be compared here.
545 int keep_message = 1; // Nonzero to keep the message in the inbox after processing, 0 to delete it.
548 syslog(LOG_DEBUG, "inboxrules: processing message #%ld which is higher than %ld, we are in %s", msgnum, ii->lastproc, CC->room.QRname);
550 if (ii->num_rules <= 0) {
551 syslog(LOG_DEBUG, "inboxrules: rule set is empty");
555 for (i=0; i<ii->num_rules; ++i) {
556 syslog(LOG_DEBUG, "inboxrules: processing rule %d is %s", i, field_keys[ ii->rules[i].compared_field ]);
559 // Before doing a field compare, check to see if we have the correct parts of the message in memory.
561 switch(ii->rules[i].compared_field) {
562 // These fields require loading only the top-level headers
563 case field_from: // From:
564 case field_tocc: // To: or Cc:
565 case field_subject: // Subject:
566 case field_replyto: // Reply-to:
567 case field_listid: // List-ID:
568 case field_envto: // Envelope-to:
569 case field_envfrom: // Return-path:
570 if (!headers_loaded) {
571 syslog(LOG_DEBUG, "inboxrules: loading headers for message %ld", msgnum);
572 msg = CtdlFetchMessage(msgnum, 0);
579 // These fields are not stored as Citadel headers, and therefore require a full message load.
581 case field_resentfrom:
584 case field_xspamflag:
585 case field_xspamstatus:
587 syslog(LOG_DEBUG, "inboxrules: loading all of message %ld", msgnum);
591 msg = CtdlFetchMessage(msgnum, 1);
600 if (!metadata_loaded) {
601 syslog(LOG_DEBUG, "inboxrules: loading metadata for message %ld", msgnum);
602 GetMetaData(&smi, msgnum);
607 syslog(LOG_DEBUG, "inboxrules: this is an always-on rule");
610 syslog(LOG_DEBUG, "inboxrules: unknown rule key");
613 // If the rule involves a field comparison, load the field to be compared.
615 switch(ii->rules[i].compared_field) {
617 case field_from: // From:
618 if (!IsEmptyStr(msg->cm_fields[erFc822Addr])) {
619 safestrncpy(compare_me, msg->cm_fields[erFc822Addr], sizeof compare_me);
622 case field_tocc: // To: or Cc:
623 if (!IsEmptyStr(msg->cm_fields[eRecipient])) {
624 safestrncpy(compare_me, msg->cm_fields[eRecipient], sizeof compare_me);
626 if (!IsEmptyStr(msg->cm_fields[eCarbonCopY])) {
627 if (!IsEmptyStr(compare_me)) {
628 strcat(compare_me, ",");
630 safestrncpy(&compare_me[strlen(compare_me)], msg->cm_fields[eCarbonCopY], (sizeof compare_me - strlen(compare_me)));
633 case field_subject: // Subject:
634 if (!IsEmptyStr(msg->cm_fields[eMsgSubject])) {
635 safestrncpy(compare_me, msg->cm_fields[eMsgSubject], sizeof compare_me);
638 case field_replyto: // Reply-to:
639 if (!IsEmptyStr(msg->cm_fields[eReplyTo])) {
640 safestrncpy(compare_me, msg->cm_fields[eReplyTo], sizeof compare_me);
643 case field_listid: // List-ID:
644 if (!IsEmptyStr(msg->cm_fields[eListID])) {
645 safestrncpy(compare_me, msg->cm_fields[eListID], sizeof compare_me);
648 case field_envto: // Envelope-to:
649 if (!IsEmptyStr(msg->cm_fields[eenVelopeTo])) {
650 safestrncpy(compare_me, msg->cm_fields[eenVelopeTo], sizeof compare_me);
653 case field_envfrom: // Return-path:
654 if (!IsEmptyStr(msg->cm_fields[eMessagePath])) {
655 safestrncpy(compare_me, msg->cm_fields[eMessagePath], sizeof compare_me);
660 case field_resentfrom:
663 case field_xspamflag:
664 case field_xspamstatus:
670 // Message data to compare is loaded, now do something.
671 switch(ii->rules[i].compared_field) {
672 case field_from: // From:
673 case field_tocc: // To: or Cc:
674 case field_subject: // Subject:
675 case field_replyto: // Reply-to:
676 case field_listid: // List-ID:
677 case field_envto: // Envelope-to:
678 case field_envfrom: // Return-path:
680 case field_resentfrom:
683 case field_xspamflag:
684 case field_xspamstatus:
686 // For all of the above fields, we can compare the field we've loaded into the buffer.
687 syslog(LOG_DEBUG, "Value of field to compare is: <%s>", compare_me);
688 switch(ii->rules[i].field_compare_op) {
691 rule_activated = (bmstrcasestr(compare_me, ii->rules[i].compared_value) ? 1 : 0);
692 syslog(LOG_DEBUG, "Does %s contain %s? %s", compare_me, ii->rules[i].compared_value, rule_activated?"yes":"no");
694 case fcomp_notcontains:
695 case fcomp_notmatches:
696 rule_activated = (bmstrcasestr(compare_me, ii->rules[i].compared_value) ? 0 : 1);
697 syslog(LOG_DEBUG, "Does %s contain %s? %s", compare_me, ii->rules[i].compared_value, rule_activated?"yes":"no");
700 rule_activated = (strcasecmp(compare_me, ii->rules[i].compared_value) ? 0 : 1);
701 syslog(LOG_DEBUG, "Does %s equal %s? %s", compare_me, ii->rules[i].compared_value, rule_activated?"yes":"no");
704 rule_activated = (strcasecmp(compare_me, ii->rules[i].compared_value) ? 1 : 0);
705 syslog(LOG_DEBUG, "Does %s equal %s? %s", compare_me, ii->rules[i].compared_value, rule_activated?"yes":"no");
712 syslog(LOG_DEBUG, "comparing actual message size %ld to rule message size %ld", smi.meta_rfc822_length, ii->rules[i].compared_size);
713 switch(ii->rules[i].field_compare_op) {
715 rule_activated = ((smi.meta_rfc822_length > ii->rules[i].compared_size) ? 1 : 0);
716 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");
719 rule_activated = ((smi.meta_rfc822_length < ii->rules[i].compared_size) ? 1 : 0);
720 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");
724 case field_all: // The "all messages" rule ALWAYS triggers
727 default: // no matches, fall through and do nothing
728 syslog(LOG_DEBUG, "inboxrules: an unknown field comparison was encountered");
733 // If the rule matched, perform the requested action.
734 if (rule_activated) {
735 syslog(LOG_DEBUG, "inboxrules: rule activated");
737 // Perform the requested action
738 switch(ii->rules[i].action) {
746 inbox_do_reject(&ii->rules[i], msg);
749 case action_fileinto:
750 keep_message = inbox_do_fileinto(&ii->rules[i], msgnum);
752 case action_redirect:
753 keep_message = inbox_do_redirect(&ii->rules[i], msgnum);
755 case action_vacation:
756 // inbox_do_vacation(&ii->rules[i], msg);
761 // Now perform the "final" action (anything other than "stop" means continue)
762 if (ii->rules[i].final_action == final_stop) {
763 syslog(LOG_DEBUG, "inboxrules: stop processing");
764 i = ii->num_rules + 1; // throw us out of scope to stop
770 syslog(LOG_DEBUG, "inboxrules: rule not activated");
774 if (msg != NULL) { // Delete the copy of the message that is currently in memory. We don't need it anymore.
778 if (!keep_message) { // Delete the copy of the message that is currently in the inbox, if rules dictated that.
779 syslog(LOG_DEBUG, "inboxrules: delete %ld from inbox", msgnum);
780 CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, ""); // we're in the inbox already
783 ii->lastproc = msgnum; // make note of the last message we processed, so we don't scan the whole inbox again
788 * A user account is identified as requring inbox processing.
791 void do_inbox_processing_for_user(long usernum) {
792 struct CtdlMessage *msg;
793 struct inboxrules *ii;
794 char roomname[ROOMNAMELEN];
797 if (CtdlGetUserByNumber(&CC->user, usernum) != 0) { // grab the user record
798 return; // and bail out if we were given an invalid user
801 strcpy(username, CC->user.fullname); // save the user name so we can fetch it later and lock it
803 if (CC->user.msgnum_inboxrules <= 0) {
804 return; // this user has no inbox rules
807 msg = CtdlFetchMessage(CC->user.msgnum_inboxrules, 1);
809 return; // config msgnum is set but that message does not exist
812 ii = deserialize_inbox_rules(msg->cm_fields[eMesageText]);
816 return; // config message exists but body is null
820 syslog(LOG_DEBUG, "ii->lastproc %ld", ii->lastproc);
821 syslog(LOG_DEBUG, "CC->user.lastproc_inboxrules %ld", CC->user.lastproc_inboxrules);
823 if (ii->lastproc > CC->user.lastproc_inboxrules) { // There might be a "last message processed" number left over
824 CC->user.lastproc_inboxrules = ii->lastproc; // in the ruleset from a previous version. Use this if it is
825 } // a higher number.
827 ii->lastproc = CC->user.lastproc_inboxrules;
830 long original_lastproc = ii->lastproc;
831 syslog(LOG_DEBUG, "inboxrules: for %s, messages newer than %ld", CC->user.fullname, original_lastproc);
833 // Go to the user's inbox room and process all new messages
834 snprintf(roomname, sizeof roomname, "%010ld.%s", usernum, MAILROOM);
835 if (CtdlGetRoom(&CC->room, roomname) == 0) {
836 CtdlForEachMessage(MSGS_GT, ii->lastproc, NULL, NULL, NULL, inbox_do_msg, (void *) ii);
839 // Record the number of the last message we processed
840 if (ii->lastproc > original_lastproc) {
841 CtdlGetUserLock(&CC->user, username);
842 CC->user.lastproc_inboxrules = ii->lastproc; // Avoid processing the entire inbox next time
843 CtdlPutUserLock(&CC->user);
846 // And free the memory.
847 free_inbox_rules(ii);
852 * Here is an array of users (by number) who have received messages in their inbox and may require processing.
854 long *users_requiring_inbox_processing = NULL;
856 int num_urip_alloc = 0;
860 * Perform inbox processing for all rooms which require it
862 void perform_inbox_processing(void) {
864 return; // no action required
867 for (int i=0; i<num_urip; ++i) {
868 do_inbox_processing_for_user(users_requiring_inbox_processing[i]);
871 free(users_requiring_inbox_processing);
872 users_requiring_inbox_processing = NULL;
879 * This function is called after a message is saved to a room.
880 * If it's someone's inbox, we have to check for inbox rules
882 int serv_inboxrules_roomhook(struct ctdlroom *room) {
884 // Is this someone's inbox?
885 if (!strcasecmp(&room->QRname[11], MAILROOM)) {
886 long usernum = atol(room->QRname);
889 // first check to see if this user is already on the list
891 for (int i=0; i<=num_urip; ++i) {
892 if (users_requiring_inbox_processing[i] == usernum) { // already on the list!
898 // make room if we need to
899 if (num_urip_alloc == 0) {
900 num_urip_alloc = 100;
901 users_requiring_inbox_processing = malloc(sizeof(long) * num_urip_alloc);
903 else if (num_urip >= num_urip_alloc) {
904 num_urip_alloc += 100;
905 users_requiring_inbox_processing = realloc(users_requiring_inbox_processing, (sizeof(long) * num_urip_alloc));
908 // now add the user to the list
909 users_requiring_inbox_processing[num_urip++] = usernum;
913 // No errors are possible from this function.
922 * This is a client-facing function which fetches the user's inbox rules -- it omits all lines containing anything other than a rule.
924 * hmmmmm ... should we try to rebuild this in terms of deserialize_inbox_rules() instread?
926 void cmd_gibr(char *argbuf) {
928 if (CtdlAccessCheck(ac_logged_in)) return;
930 cprintf("%d inbox rules for %s\n", LISTING_FOLLOWS, CC->user.fullname);
932 struct CtdlMessage *msg = CtdlFetchMessage(CC->user.msgnum_inboxrules, 1);
934 if (!CM_IsEmpty(msg, eMesageText)) {
936 char *rest = msg->cm_fields[eMesageText];
937 while ((token = strtok_r(rest, "\n", &rest))) {
939 // for backwards compatibility, "# WEBCIT_RULE" is an alias for "rule"
940 if (!strncasecmp(token, "# WEBCIT_RULE|", 14)) {
941 strcpy(token, "rule|");
942 strcpy(&token[5], &token[14]);
945 // Output only lines containing rules.
946 if (!strncasecmp(token, "rule|", 5)) {
947 cprintf("%s\n", token);
950 cprintf("# invalid rule found : %s\n", token);
961 * Rewrite the rule set to disk after it has been modified
962 * Called by cmd_pibr()
963 * Returns the msgnum of the new rules message
965 void rewrite_rules_to_disk(const char *new_config) {
966 long old_msgnum = CC->user.msgnum_inboxrules;
967 char userconfigroomname[ROOMNAMELEN];
968 CtdlMailboxName(userconfigroomname, sizeof userconfigroomname, &CC->user, USERCONFIGROOM);
969 long new_msgnum = quickie_message("Citadel", NULL, NULL, userconfigroomname, new_config, FMT_RFC822, "inbox rules configuration");
970 CtdlGetUserLock(&CC->user, CC->curr_user);
971 CC->user.msgnum_inboxrules = new_msgnum; // Now we know where to get the rules next time
972 CC->user.lastproc_inboxrules = new_msgnum; // Avoid processing the entire inbox next time
973 CtdlPutUserLock(&CC->user);
974 if (old_msgnum > 0) {
975 syslog(LOG_DEBUG, "Deleting old message %ld from %s", old_msgnum, userconfigroomname);
976 CtdlDeleteMessages(userconfigroomname, &old_msgnum, 1, "");
984 * User transmits the new inbox rules for the account. They are inserted into the account, replacing the ones already there.
986 void cmd_pibr(char *argbuf) {
987 if (CtdlAccessCheck(ac_logged_in)) return;
990 cprintf("%d send new rules\n", SEND_LISTING);
991 char *newrules = CtdlReadMessageBody(HKEY("000"), CtdlGetConfigLong("c_maxmsglen"), NULL, 0);
992 StrBuf *NewConfig = NewStrBufPlain("Content-type: application/x-citadel-sieve-config; charset=UTF-8\nContent-transfer-encoding: 8bit\n\n", -1);
995 char *rest = newrules;
996 while ((token = strtok_r(rest, "\n", &rest))) {
997 // Accept only lines containing rules
998 if (!strncasecmp(token, "rule|", 5)) {
999 StrBufAppendBufPlain(NewConfig, token, -1, 0);
1000 StrBufAppendBufPlain(NewConfig, HKEY("\n"), 0);
1004 rewrite_rules_to_disk(ChrPtr(NewConfig));
1005 FreeStrBuf(&NewConfig);
1009 CTDL_MODULE_INIT(sieve)
1013 CtdlRegisterProtoHook(cmd_gibr, "GIBR", "Get InBox Rules");
1014 CtdlRegisterProtoHook(cmd_pibr, "PIBR", "Put InBox Rules");
1015 CtdlRegisterRoomHook(serv_inboxrules_roomhook);
1016 CtdlRegisterSessionHook(perform_inbox_processing, EVT_HOUSE, PRIO_HOUSE + 10);
1019 /* return our module name for the log */
1020 return "inboxrules";