1 // Inbox handling rules
3 // Copyright (c) 1987-2022 by the citadel.org team
5 // This program is open source software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License version 3.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
13 #include "../../sysdep.h"
21 #include <sys/types.h>
26 #include <libcitadel.h>
27 #include "../../citadel.h"
28 #include "../../server.h"
29 #include "../../citserver.h"
30 #include "../../support.h"
31 #include "../../config.h"
32 #include "../../database.h"
33 #include "../../msgbase.h"
34 #include "../../internet_addressing.h"
35 #include "../../ctdl_module.h"
38 // The next sections are enums and keys that drive the serialize/deserialize functions for the inbox rules/state configuration.
40 // Fields to be compared
58 char *field_keys[] = {
76 // Field comparison operators
85 char *fcomp_keys[] = {
103 char *action_keys[] = {
112 // Size comparison operators
117 char *scomp_keys[] = {
127 char *final_keys[] = {
132 // This data structure represents ONE inbox rule within the configuration.
135 int field_compare_op;
136 char compared_value[128];
140 char file_into[ROOMNAMELEN];
141 char redirect_to[1024];
142 char autoreply_message[SIZ];
146 // This data structure represents the entire inbox rules configuration AND current state for a single user.
154 // Destructor for 'struct inboxrules'
155 void free_inbox_rules(struct inboxrules *ibr) {
161 // Constructor for 'struct inboxrules' that deserializes the configuration from text input.
162 struct inboxrules *deserialize_inbox_rules(char *serialized_rules) {
165 if (!serialized_rules) {
169 // Make a copy of the supplied buffer because we're going to shit all over it with strtok_r()
170 char *sr = strdup(serialized_rules);
175 struct inboxrules *ibr = malloc(sizeof(struct inboxrules));
179 memset(ibr, 0, sizeof(struct inboxrules));
183 while ((token = strtok_r(rest, "\n", &rest))) {
185 // For backwards compatibility, "# WEBCIT_RULE" is an alias for "rule".
186 // Prior to version 930, WebCit converted its rules to Sieve scripts, but saved the rules as comments for
187 // later re-editing. Now, the rules hidden in the comments become the real rules.
188 if (!strncasecmp(token, "# WEBCIT_RULE|", 14)) {
189 strcpy(token, "rule|");
190 strcpy(&token[5], &token[14]);
193 // Lines containing actual rules are double-serialized with Base64. It seemed like a good idea at the time :(
194 if (!strncasecmp(token, "rule|", 5)) {
195 remove_token(&token[5], 0, '|');
196 char *decoded_rule = malloc(strlen(token));
197 CtdlDecodeBase64(decoded_rule, &token[5], strlen(&token[5]));
199 ibr->rules = realloc(ibr->rules, (sizeof(struct irule) * ibr->num_rules));
200 struct irule *new_rule = &ibr->rules[ibr->num_rules - 1];
201 memset(new_rule, 0, sizeof(struct irule));
203 // We have a rule , now parse it
205 int nt = num_tokens(decoded_rule, '|');
207 for (t=0; t<nt; ++t) {
208 extract_token(rtoken, decoded_rule, t, '|', sizeof(rtoken));
211 case 1: // field to compare
212 for (i=0; i<=field_all; ++i) {
213 if (!strcasecmp(rtoken, field_keys[i])) {
214 new_rule->compared_field = i;
218 case 2: // field comparison operation
219 for (i=0; i<=fcomp_notmatches; ++i) {
220 if (!strcasecmp(rtoken, fcomp_keys[i])) {
221 new_rule->field_compare_op = i;
225 case 3: // field comparison value
226 safestrncpy(new_rule->compared_value, rtoken, sizeof(new_rule->compared_value));
228 case 4: // size comparison operation
229 for (i=0; i<=scomp_smaller; ++i) {
230 if (!strcasecmp(rtoken, scomp_keys[i])) {
231 new_rule->size_compare_op = i;
235 case 5: // size comparison value
236 new_rule->compared_size = atol(rtoken);
239 for (i=0; i<=action_vacation; ++i) {
240 if (!strcasecmp(rtoken, action_keys[i])) {
241 new_rule->action = i;
245 case 7: // file into (target room)
246 safestrncpy(new_rule->file_into, rtoken, sizeof(new_rule->file_into));
248 case 8: // redirect to (target address)
249 safestrncpy(new_rule->redirect_to, rtoken, sizeof(new_rule->redirect_to));
251 case 9: // autoreply message
252 safestrncpy(new_rule->autoreply_message, rtoken, sizeof(new_rule->autoreply_message));
254 case 10: // final_action;
255 for (i=0; i<=final_stop; ++i) {
256 if (!strcasecmp(rtoken, final_keys[i])) {
257 new_rule->final_action = i;
268 // "lastproc" indicates the newest message number in the inbox that was previously processed by our inbox rules.
269 // This is a legacy location for this value and will only be used if it's the only one present.
270 else if (!strncasecmp(token, "lastproc|", 5)) {
271 ibr->lastproc = atol(&token[9]);
274 // Lines which do not contain a recognizable token must be IGNORED. These lines may be left over
275 // from a previous version and will disappear when we rewrite the config.
279 free(sr); // free our copy of the source buffer that has now been trashed with null bytes...
280 return(ibr); // and return our complex data type to the caller.
284 // Perform the "fileinto" action (save the message in another room)
285 // Returns: 1 or 0 to tell the caller to keep (1) or delete (0) the inbox copy of the message.
287 int inbox_do_fileinto(struct irule *rule, long msgnum) {
288 char *dest_folder = rule->file_into;
289 char original_room_name[ROOMNAMELEN];
290 char foldername[ROOMNAMELEN];
293 // Situations where we want to just keep the message in the inbox:
295 (IsEmptyStr(dest_folder)) // no destination room was specified
296 || (!strcasecmp(dest_folder, "INBOX")) // fileinto inbox is the same as keep
297 || (!strcasecmp(dest_folder, MAILROOM)) // fileinto "Mail" is the same as keep
299 return(1); // don't delete the inbox copy if this failed
302 // Remember what room we came from
303 safestrncpy(original_room_name, CC->room.QRname, sizeof original_room_name);
305 // First try a mailbox name match (check personal mail folders first)
306 strcpy(foldername, original_room_name); // This keeps the user namespace of the inbox
307 snprintf(&foldername[10], sizeof(foldername)-10, ".%s", dest_folder); // And this tacks on the target room name
308 c = CtdlGetRoom(&CC->room, foldername);
310 // Then a regular room name match (public and private rooms)
312 safestrncpy(foldername, dest_folder, sizeof foldername);
313 c = CtdlGetRoom(&CC->room, foldername);
317 syslog(LOG_WARNING, "inboxrules: target <%s> does not exist", dest_folder);
318 return(1); // don't delete the inbox copy if this failed
321 // Yes, we actually have to go there
322 CtdlUserGoto(NULL, 0, 0, NULL, NULL, NULL, NULL);
324 c = CtdlSaveMsgPointersInRoom(NULL, &msgnum, 1, 0, NULL, 0);
326 // Go back to the room we came from
327 if (strcasecmp(original_room_name, CC->room.QRname)) {
328 CtdlUserGoto(original_room_name, 0, 0, NULL, NULL, NULL, NULL);
331 return(0); // delete the inbox copy
335 // Perform the "redirect" action (divert the message to another email address)
336 // Returns: 1 or 0 to tell the caller to keep (1) or delete (0) the inbox copy of the message.
338 int inbox_do_redirect(struct irule *rule, long msgnum) {
339 if (IsEmptyStr(rule->redirect_to)) {
340 syslog(LOG_WARNING, "inboxrules: inbox_do_redirect() invalid recipient <%s>", rule->redirect_to);
341 return(1); // don't delete the inbox copy if this failed
344 struct recptypes *valid = validate_recipients(rule->redirect_to, NULL, 0);
346 syslog(LOG_WARNING, "inboxrules: inbox_do_redirect() invalid recipient <%s>", rule->redirect_to);
347 return(1); // don't delete the inbox copy if this failed
349 if (valid->num_error > 0) {
350 free_recipients(valid);
351 syslog(LOG_WARNING, "inboxrules: inbox_do_redirect() invalid recipient <%s>", rule->redirect_to);
352 return(1); // don't delete the inbox copy if this failed
355 struct CtdlMessage *msg = CtdlFetchMessage(msgnum, 1);
357 free_recipients(valid);
358 syslog(LOG_WARNING, "inboxrules: cannot reload message %ld for forwarding", msgnum);
359 return(1); // don't delete the inbox copy if this failed
362 CtdlSubmitMsg(msg, valid, NULL); // send the message to the new recipient
363 free_recipients(valid);
365 return(0); // delete the inbox copy
369 // Perform the "reject" action (delete the message, and tell the sender we deleted it)
370 void inbox_do_reject(struct irule *rule, struct CtdlMessage *msg) {
371 syslog(LOG_DEBUG, "inbox_do_reject: sender: <%s>, reject", msg->cm_fields[erFc822Addr]);
373 // If we can't determine who sent the message, reject silently.
375 if (!IsEmptyStr(msg->cm_fields[eMessagePath])) {
376 sender = msg->cm_fields[eMessagePath];
378 else if (!IsEmptyStr(msg->cm_fields[erFc822Addr])) {
379 sender = msg->cm_fields[erFc822Addr];
385 // Assemble the reject message.
386 char *reject_text = malloc(strlen(rule->autoreply_message) + 1024);
387 if (reject_text == NULL) {
391 "Content-type: text/plain\n"
393 "The message was refused by the recipient's mail filtering program.\n"
394 "The reason given was as follows:\n"
399 rule->autoreply_message
402 // Deliver the message
405 msg->cm_fields[eenVelopeTo],
410 "Delivery status notification"
416 // Perform the "vacation" action (send an automatic response)
417 void inbox_do_vacation(struct irule *rule, struct CtdlMessage *msg) {
418 syslog(LOG_DEBUG, "inbox_do_vacation: sender: <%s>, vacation", msg->cm_fields[erFc822Addr]);
420 // If we can't determine who sent the message, no auto-reply can be sent.
422 if (!IsEmptyStr(msg->cm_fields[eMessagePath])) {
423 sender = msg->cm_fields[eMessagePath];
425 else if (!IsEmptyStr(msg->cm_fields[erFc822Addr])) {
426 sender = msg->cm_fields[erFc822Addr];
432 // Avoid repeatedly sending auto-replies to the same correspondent over and over again by creating
433 // a hash of the user, correspondent, and reply text, and hitting the S_USETABLE database.
434 StrBuf *u = NewStrBuf();
435 StrBufPrintf(u, "vacation/%x/%x/%x",
436 HashLittle(sender, strlen(sender)),
437 HashLittle(msg->cm_fields[eenVelopeTo], msg->cm_lengths[eenVelopeTo]),
438 HashLittle(rule->autoreply_message, strlen(rule->autoreply_message))
440 int already_seen = CheckIfAlreadySeen(u);
444 // Assemble the auto-reply message.
445 StrBuf *reject_text = NewStrBuf();
446 if (reject_text == NULL) {
450 StrBufPrintf(reject_text,
451 "Content-type: text/plain\n"
456 rule->autoreply_message
459 // Deliver the auto-reply.
462 msg->cm_fields[eenVelopeTo],
467 "Delivery status notification"
469 FreeStrBuf(&reject_text);
475 * Process a single message. We know the room, the user, the rules, the message number, etc.
477 void inbox_do_msg(long msgnum, void *userdata) {
478 struct inboxrules *ii = (struct inboxrules *) userdata;
479 struct CtdlMessage *msg = NULL; // If we are loading a message to process, put it here.
480 int headers_loaded = 0; // Did we load the headers yet? Do it only once.
481 int body_loaded = 0; // Did we load the message body yet? Do it only once.
482 int metadata_loaded = 0; // Did we load the metadata yet? Do it only once.
483 struct MetaData smi; // If we are loading the metadata to compare, put it here.
484 int rule_activated = 0; // On each rule, this is set if the compare succeeds and the rule activates.
485 char compare_me[SIZ]; // On each rule, we will store the field to be compared here.
486 int compare_compound = 0; // Set to 1 when we are comparing both display name and email address
487 int keep_message = 1; // Nonzero to keep the message in the inbox after processing, 0 to delete it.
490 syslog(LOG_DEBUG, "inboxrules: processing message #%ld which is higher than %ld, we are in %s", msgnum, ii->lastproc, CC->room.QRname);
492 if (ii->num_rules <= 0) {
493 syslog(LOG_DEBUG, "inboxrules: rule set is empty");
497 for (i=0; i<ii->num_rules; ++i) {
498 syslog(LOG_DEBUG, "inboxrules: processing rule %d is %s", i, field_keys[ ii->rules[i].compared_field ]);
501 // Before doing a field compare, check to see if we have the correct parts of the message in memory.
503 switch(ii->rules[i].compared_field) {
504 // These fields require loading only the top-level headers
505 case field_from: // From:
506 case field_tocc: // To: or Cc:
507 case field_subject: // Subject:
508 case field_replyto: // Reply-to:
509 case field_listid: // List-ID:
510 case field_envto: // Envelope-to:
511 case field_envfrom: // Return-path:
512 if (!headers_loaded) {
513 syslog(LOG_DEBUG, "inboxrules: loading headers for message %ld", msgnum);
514 msg = CtdlFetchMessage(msgnum, 0);
521 // These fields are not stored as Citadel headers, and therefore require a full message load.
523 case field_resentfrom:
526 case field_xspamflag:
527 case field_xspamstatus:
529 syslog(LOG_DEBUG, "inboxrules: loading all of message %ld", msgnum);
533 msg = CtdlFetchMessage(msgnum, 1);
542 if (!metadata_loaded) {
543 syslog(LOG_DEBUG, "inboxrules: loading metadata for message %ld", msgnum);
544 GetMetaData(&smi, msgnum);
549 syslog(LOG_DEBUG, "inboxrules: this is an always-on rule");
552 syslog(LOG_DEBUG, "inboxrules: unknown rule key");
555 // If the rule involves a field comparison, load the field to be compared.
557 compare_compound = 0;
558 switch(ii->rules[i].compared_field) {
559 case field_from: // From:
560 if ( (!IsEmptyStr(msg->cm_fields[erFc822Addr])) && (!IsEmptyStr(msg->cm_fields[erFc822Addr])) ) {
561 snprintf(compare_me, sizeof compare_me, "%s|%s",
562 msg->cm_fields[eAuthor],
563 msg->cm_fields[erFc822Addr]
565 compare_compound = 1; // there will be two fields to compare "name|address"
567 else if (!IsEmptyStr(msg->cm_fields[erFc822Addr])) {
568 safestrncpy(compare_me, msg->cm_fields[erFc822Addr], sizeof compare_me);
570 else if (!IsEmptyStr(msg->cm_fields[eAuthor])) {
571 safestrncpy(compare_me, msg->cm_fields[eAuthor], sizeof compare_me);
574 case field_tocc: // To: or Cc:
575 if (!IsEmptyStr(msg->cm_fields[eRecipient])) {
576 safestrncpy(compare_me, msg->cm_fields[eRecipient], sizeof compare_me);
578 if (!IsEmptyStr(msg->cm_fields[eCarbonCopY])) {
579 if (!IsEmptyStr(compare_me)) {
580 strcat(compare_me, ",");
582 safestrncpy(&compare_me[strlen(compare_me)], msg->cm_fields[eCarbonCopY], (sizeof compare_me - strlen(compare_me)));
585 case field_subject: // Subject:
586 if (!IsEmptyStr(msg->cm_fields[eMsgSubject])) {
587 safestrncpy(compare_me, msg->cm_fields[eMsgSubject], sizeof compare_me);
590 case field_replyto: // Reply-to:
591 if (!IsEmptyStr(msg->cm_fields[eReplyTo])) {
592 safestrncpy(compare_me, msg->cm_fields[eReplyTo], sizeof compare_me);
595 case field_listid: // List-ID:
596 if (!IsEmptyStr(msg->cm_fields[eListID])) {
597 safestrncpy(compare_me, msg->cm_fields[eListID], sizeof compare_me);
600 case field_envto: // Envelope-to:
601 if (!IsEmptyStr(msg->cm_fields[eenVelopeTo])) {
602 safestrncpy(compare_me, msg->cm_fields[eenVelopeTo], sizeof compare_me);
605 case field_envfrom: // Return-path:
606 if (!IsEmptyStr(msg->cm_fields[eMessagePath])) {
607 safestrncpy(compare_me, msg->cm_fields[eMessagePath], sizeof compare_me);
612 case field_resentfrom:
615 case field_xspamflag:
616 case field_xspamstatus:
622 // Message data to compare is loaded, now do something.
623 switch(ii->rules[i].compared_field) {
624 case field_from: // From:
625 case field_tocc: // To: or Cc:
626 case field_subject: // Subject:
627 case field_replyto: // Reply-to:
628 case field_listid: // List-ID:
629 case field_envto: // Envelope-to:
630 case field_envfrom: // Return-path:
632 case field_resentfrom:
635 case field_xspamflag:
636 case field_xspamstatus:
638 // For all of the above fields, we can compare the field we've loaded into the buffer.
639 syslog(LOG_DEBUG, "Value of field to compare is: <%s>", compare_me);
640 int substring_match = (bmstrcasestr(compare_me, ii->rules[i].compared_value) ? 1 : 0);
642 if (compare_compound) {
643 char *sep = strchr(compare_me, '|');
647 (strcasecmp(compare_me, ii->rules[i].compared_value) ? 0 : 1)
648 + (strcasecmp(++sep, ii->rules[i].compared_value) ? 0 : 1)
653 exact_match = (strcasecmp(compare_me, ii->rules[i].compared_value) ? 0 : 1);
655 syslog(LOG_DEBUG, "substring match: %d", substring_match);
656 syslog(LOG_DEBUG, "exact match: %d", exact_match);
657 switch(ii->rules[i].field_compare_op) {
660 rule_activated = substring_match;
662 case fcomp_notcontains:
663 case fcomp_notmatches:
664 rule_activated = !substring_match;
667 rule_activated = exact_match;
670 rule_activated = !exact_match;
677 switch(ii->rules[i].field_compare_op) {
679 rule_activated = ((smi.meta_rfc822_length > ii->rules[i].compared_size) ? 1 : 0);
682 rule_activated = ((smi.meta_rfc822_length < ii->rules[i].compared_size) ? 1 : 0);
686 case field_all: // The "all messages" rule ALWAYS triggers
689 default: // no matches, fall through and do nothing
690 syslog(LOG_WARNING, "inboxrules: an unknown field comparison was encountered");
695 // If the rule matched, perform the requested action.
696 if (rule_activated) {
697 syslog(LOG_DEBUG, "inboxrules: rule activated");
699 // Perform the requested action
700 switch(ii->rules[i].action) {
708 inbox_do_reject(&ii->rules[i], msg);
711 case action_fileinto:
712 keep_message = inbox_do_fileinto(&ii->rules[i], msgnum);
714 case action_redirect:
715 keep_message = inbox_do_redirect(&ii->rules[i], msgnum);
717 case action_vacation:
718 inbox_do_vacation(&ii->rules[i], msg);
723 // Now perform the "final" action (anything other than "stop" means continue)
724 if (ii->rules[i].final_action == final_stop) {
725 syslog(LOG_DEBUG, "inboxrules: stop processing");
726 i = ii->num_rules + 1; // throw us out of scope to stop
732 syslog(LOG_DEBUG, "inboxrules: rule not activated");
736 if (msg != NULL) { // Delete the copy of the message that is currently in memory. We don't need it anymore.
740 if (!keep_message) { // Delete the copy of the message that is currently in the inbox, if rules dictated that.
741 syslog(LOG_DEBUG, "inboxrules: delete %ld from inbox", msgnum);
742 CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, ""); // we're in the inbox already
745 ii->lastproc = msgnum; // make note of the last message we processed, so we don't scan the whole inbox again
750 * A user account is identified as requring inbox processing.
753 void do_inbox_processing_for_user(long usernum) {
754 struct CtdlMessage *msg;
755 struct inboxrules *ii;
756 char roomname[ROOMNAMELEN];
759 if (CtdlGetUserByNumber(&CC->user, usernum) != 0) { // grab the user record
760 return; // and bail out if we were given an invalid user
763 strcpy(username, CC->user.fullname); // save the user name so we can fetch it later and lock it
765 if (CC->user.msgnum_inboxrules <= 0) {
766 return; // this user has no inbox rules
769 msg = CtdlFetchMessage(CC->user.msgnum_inboxrules, 1);
771 return; // config msgnum is set but that message does not exist
774 ii = deserialize_inbox_rules(msg->cm_fields[eMesageText]);
778 return; // config message exists but body is null
781 if (ii->lastproc > CC->user.lastproc_inboxrules) { // There might be a "last message processed" number left over
782 CC->user.lastproc_inboxrules = ii->lastproc; // in the ruleset from a previous version. Use this if it is
783 } // a higher number.
785 ii->lastproc = CC->user.lastproc_inboxrules;
788 long original_lastproc = ii->lastproc;
789 syslog(LOG_DEBUG, "inboxrules: for %s, messages newer than %ld", CC->user.fullname, original_lastproc);
791 // Go to the user's inbox room and process all new messages
792 snprintf(roomname, sizeof roomname, "%010ld.%s", usernum, MAILROOM);
793 if (CtdlGetRoom(&CC->room, roomname) == 0) {
794 CtdlForEachMessage(MSGS_GT, ii->lastproc, NULL, NULL, NULL, inbox_do_msg, (void *) ii);
797 // Record the number of the last message we processed
798 if (ii->lastproc > original_lastproc) {
799 CtdlGetUserLock(&CC->user, username);
800 CC->user.lastproc_inboxrules = ii->lastproc; // Avoid processing the entire inbox next time
801 CtdlPutUserLock(&CC->user);
804 // And free the memory.
805 free_inbox_rules(ii);
810 * Here is an array of users (by number) who have received messages in their inbox and may require processing.
812 long *users_requiring_inbox_processing = NULL;
814 int num_urip_alloc = 0;
818 * Perform inbox processing for all rooms which require it
820 void perform_inbox_processing(void) {
824 return; // no action required
827 for (i=0; i<num_urip; ++i) {
828 do_inbox_processing_for_user(users_requiring_inbox_processing[i]);
831 free(users_requiring_inbox_processing);
832 users_requiring_inbox_processing = NULL;
839 * This function is called after a message is saved to a room.
840 * If it's someone's inbox, we have to check for inbox rules
842 int serv_inboxrules_roomhook(struct ctdlroom *room) {
845 // Is this someone's inbox?
846 if (!strcasecmp(&room->QRname[11], MAILROOM)) {
847 long usernum = atol(room->QRname);
850 // first check to see if this user is already on the list
852 for (i=0; i<=num_urip; ++i) {
853 if (users_requiring_inbox_processing[i] == usernum) { // already on the list!
859 // make room if we need to
860 if (num_urip_alloc == 0) {
861 num_urip_alloc = 100;
862 users_requiring_inbox_processing = malloc(sizeof(long) * num_urip_alloc);
864 else if (num_urip >= num_urip_alloc) {
865 num_urip_alloc += 100;
866 users_requiring_inbox_processing = realloc(users_requiring_inbox_processing, (sizeof(long) * num_urip_alloc));
869 // now add the user to the list
870 users_requiring_inbox_processing[num_urip++] = usernum;
874 // No errors are possible from this function.
883 * This is a client-facing function which fetches the user's inbox rules -- it omits all lines containing anything other than a rule.
885 * hmmmmm ... should we try to rebuild this in terms of deserialize_inbox_rules() instread?
887 void cmd_gibr(char *argbuf) {
889 if (CtdlAccessCheck(ac_logged_in)) return;
891 cprintf("%d inbox rules for %s\n", LISTING_FOLLOWS, CC->user.fullname);
893 struct CtdlMessage *msg = CtdlFetchMessage(CC->user.msgnum_inboxrules, 1);
895 if (!CM_IsEmpty(msg, eMesageText)) {
897 char *rest = msg->cm_fields[eMesageText];
898 while ((token = strtok_r(rest, "\n", &rest))) {
900 // for backwards compatibility, "# WEBCIT_RULE" is an alias for "rule"
901 if (!strncasecmp(token, "# WEBCIT_RULE|", 14)) {
902 strcpy(token, "rule|");
903 strcpy(&token[5], &token[14]);
906 // Output only lines containing rules.
907 if (!strncasecmp(token, "rule|", 5)) {
908 cprintf("%s\n", token);
911 cprintf("# invalid rule found : %s\n", token);
922 * Rewrite the rule set to disk after it has been modified
923 * Called by cmd_pibr()
924 * Returns the msgnum of the new rules message
926 void rewrite_rules_to_disk(const char *new_config) {
927 long old_msgnum = CC->user.msgnum_inboxrules;
928 char userconfigroomname[ROOMNAMELEN];
929 CtdlMailboxName(userconfigroomname, sizeof userconfigroomname, &CC->user, USERCONFIGROOM);
930 long new_msgnum = quickie_message("Citadel", NULL, NULL, userconfigroomname, new_config, FMT_RFC822, "inbox rules configuration");
931 CtdlGetUserLock(&CC->user, CC->curr_user);
932 CC->user.msgnum_inboxrules = new_msgnum; // Now we know where to get the rules next time
933 CC->user.lastproc_inboxrules = new_msgnum; // Avoid processing the entire inbox next time
934 CtdlPutUserLock(&CC->user);
935 if (old_msgnum > 0) {
936 syslog(LOG_DEBUG, "Deleting old message %ld from %s", old_msgnum, userconfigroomname);
937 CtdlDeleteMessages(userconfigroomname, &old_msgnum, 1, "");
945 * User transmits the new inbox rules for the account. They are inserted into the account, replacing the ones already there.
947 void cmd_pibr(char *argbuf) {
948 if (CtdlAccessCheck(ac_logged_in)) return;
951 cprintf("%d send new rules\n", SEND_LISTING);
952 char *newrules = CtdlReadMessageBody(HKEY("000"), CtdlGetConfigLong("c_maxmsglen"), NULL, 0);
953 StrBuf *NewConfig = NewStrBufPlain("Content-type: application/x-citadel-sieve-config; charset=UTF-8\nContent-transfer-encoding: 8bit\n\n", -1);
956 char *rest = newrules;
957 while ((token = strtok_r(rest, "\n", &rest))) {
958 // Accept only lines containing rules
959 if (!strncasecmp(token, "rule|", 5)) {
960 StrBufAppendBufPlain(NewConfig, token, -1, 0);
961 StrBufAppendBufPlain(NewConfig, HKEY("\n"), 0);
965 rewrite_rules_to_disk(ChrPtr(NewConfig));
966 FreeStrBuf(&NewConfig);
970 // Initialization function, called from modules_init.c
971 char *ctdl_module_init_sieve(void) {
973 CtdlRegisterProtoHook(cmd_gibr, "GIBR", "Get InBox Rules");
974 CtdlRegisterProtoHook(cmd_pibr, "PIBR", "Put InBox Rules");
975 CtdlRegisterRoomHook(serv_inboxrules_roomhook);
976 CtdlRegisterSessionHook(perform_inbox_processing, EVT_HOUSE, PRIO_HOUSE + 10);
979 /* return our module name for the log */