1 // Inbox handling rules
3 // Copyright (c) 1987-2023 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_defs.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);
474 // Process a single message. We know the room, the user, the rules, the message number, etc.
475 void inbox_do_msg(long msgnum, void *userdata) {
476 struct inboxrules *ii = (struct inboxrules *) userdata;
477 struct CtdlMessage *msg = NULL; // If we are loading a message to process, put it here.
478 int headers_loaded = 0; // Did we load the headers yet? Do it only once.
479 int body_loaded = 0; // Did we load the message body yet? Do it only once.
480 int metadata_loaded = 0; // Did we load the metadata yet? Do it only once.
481 struct MetaData smi; // If we are loading the metadata to compare, put it here.
482 int rule_activated = 0; // On each rule, this is set if the compare succeeds and the rule activates.
483 char compare_me[SIZ]; // On each rule, we will store the field to be compared here.
484 int compare_compound = 0; // Set to 1 when we are comparing both display name and email address
485 int keep_message = 1; // Nonzero to keep the message in the inbox after processing, 0 to delete it.
488 syslog(LOG_DEBUG, "inboxrules: processing message #%ld which is higher than %ld, we are in %s", msgnum, ii->lastproc, CC->room.QRname);
490 if (ii->num_rules <= 0) {
491 syslog(LOG_DEBUG, "inboxrules: rule set is empty");
495 for (i=0; i<ii->num_rules; ++i) {
496 syslog(LOG_DEBUG, "inboxrules: processing rule %d is %s", i, field_keys[ ii->rules[i].compared_field ]);
499 // Before doing a field compare, check to see if we have the correct parts of the message in memory.
501 switch(ii->rules[i].compared_field) {
502 // These fields require loading only the top-level headers
503 case field_from: // From:
504 case field_tocc: // To: or Cc:
505 case field_subject: // Subject:
506 case field_replyto: // Reply-to:
507 case field_listid: // List-ID:
508 case field_envto: // Envelope-to:
509 case field_envfrom: // Return-path:
510 if (!headers_loaded) {
511 syslog(LOG_DEBUG, "inboxrules: loading headers for message %ld", msgnum);
512 msg = CtdlFetchMessage(msgnum, 0);
519 // These fields are not stored as Citadel headers, and therefore require a full message load.
521 case field_resentfrom:
524 case field_xspamflag:
525 case field_xspamstatus:
527 syslog(LOG_DEBUG, "inboxrules: loading all of message %ld", msgnum);
531 msg = CtdlFetchMessage(msgnum, 1);
540 if (!metadata_loaded) {
541 syslog(LOG_DEBUG, "inboxrules: loading metadata for message %ld", msgnum);
542 GetMetaData(&smi, msgnum);
547 syslog(LOG_DEBUG, "inboxrules: this is an always-on rule");
550 syslog(LOG_DEBUG, "inboxrules: unknown rule key");
553 // If the rule involves a field comparison, load the field to be compared.
555 compare_compound = 0;
556 switch(ii->rules[i].compared_field) {
557 case field_from: // From:
558 if ( (!IsEmptyStr(msg->cm_fields[erFc822Addr])) && (!IsEmptyStr(msg->cm_fields[erFc822Addr])) ) {
559 snprintf(compare_me, sizeof compare_me, "%s|%s",
560 msg->cm_fields[eAuthor],
561 msg->cm_fields[erFc822Addr]
563 compare_compound = 1; // there will be two fields to compare "name|address"
565 else if (!IsEmptyStr(msg->cm_fields[erFc822Addr])) {
566 safestrncpy(compare_me, msg->cm_fields[erFc822Addr], sizeof compare_me);
568 else if (!IsEmptyStr(msg->cm_fields[eAuthor])) {
569 safestrncpy(compare_me, msg->cm_fields[eAuthor], sizeof compare_me);
572 case field_tocc: // To: or Cc:
573 if (!IsEmptyStr(msg->cm_fields[eRecipient])) {
574 safestrncpy(compare_me, msg->cm_fields[eRecipient], sizeof compare_me);
576 if (!IsEmptyStr(msg->cm_fields[eCarbonCopY])) {
577 if (!IsEmptyStr(compare_me)) {
578 strcat(compare_me, ",");
580 safestrncpy(&compare_me[strlen(compare_me)], msg->cm_fields[eCarbonCopY], (sizeof compare_me - strlen(compare_me)));
583 case field_subject: // Subject:
584 if (!IsEmptyStr(msg->cm_fields[eMsgSubject])) {
585 safestrncpy(compare_me, msg->cm_fields[eMsgSubject], sizeof compare_me);
588 case field_replyto: // Reply-to:
589 if (!IsEmptyStr(msg->cm_fields[eReplyTo])) {
590 safestrncpy(compare_me, msg->cm_fields[eReplyTo], sizeof compare_me);
593 case field_listid: // List-ID:
594 if (!IsEmptyStr(msg->cm_fields[eListID])) {
595 safestrncpy(compare_me, msg->cm_fields[eListID], sizeof compare_me);
598 case field_envto: // Envelope-to:
599 if (!IsEmptyStr(msg->cm_fields[eenVelopeTo])) {
600 safestrncpy(compare_me, msg->cm_fields[eenVelopeTo], sizeof compare_me);
603 case field_envfrom: // Return-path:
604 if (!IsEmptyStr(msg->cm_fields[eMessagePath])) {
605 safestrncpy(compare_me, msg->cm_fields[eMessagePath], sizeof compare_me);
610 case field_resentfrom:
613 case field_xspamflag:
614 case field_xspamstatus:
620 // Message data to compare is loaded, now do something.
621 switch(ii->rules[i].compared_field) {
622 case field_from: // From:
623 case field_tocc: // To: or Cc:
624 case field_subject: // Subject:
625 case field_replyto: // Reply-to:
626 case field_listid: // List-ID:
627 case field_envto: // Envelope-to:
628 case field_envfrom: // Return-path:
630 case field_resentfrom:
633 case field_xspamflag:
634 case field_xspamstatus:
636 // For all of the above fields, we can compare the field we've loaded into the buffer.
637 syslog(LOG_DEBUG, "Value of field to compare is: <%s>", compare_me);
638 int substring_match = (bmstrcasestr(compare_me, ii->rules[i].compared_value) ? 1 : 0);
640 if (compare_compound) {
641 char *sep = strchr(compare_me, '|');
645 (strcasecmp(compare_me, ii->rules[i].compared_value) ? 0 : 1)
646 + (strcasecmp(++sep, ii->rules[i].compared_value) ? 0 : 1)
651 exact_match = (strcasecmp(compare_me, ii->rules[i].compared_value) ? 0 : 1);
653 syslog(LOG_DEBUG, "substring match: %d", substring_match);
654 syslog(LOG_DEBUG, "exact match: %d", exact_match);
655 switch(ii->rules[i].field_compare_op) {
658 rule_activated = substring_match;
660 case fcomp_notcontains:
661 case fcomp_notmatches:
662 rule_activated = !substring_match;
665 rule_activated = exact_match;
668 rule_activated = !exact_match;
675 switch(ii->rules[i].field_compare_op) {
677 rule_activated = ((smi.meta_rfc822_length > ii->rules[i].compared_size) ? 1 : 0);
680 rule_activated = ((smi.meta_rfc822_length < ii->rules[i].compared_size) ? 1 : 0);
684 case field_all: // The "all messages" rule ALWAYS triggers
687 default: // no matches, fall through and do nothing
688 syslog(LOG_WARNING, "inboxrules: an unknown field comparison was encountered");
693 // If the rule matched, perform the requested action.
694 if (rule_activated) {
695 syslog(LOG_DEBUG, "inboxrules: rule activated");
697 // Perform the requested action
698 switch(ii->rules[i].action) {
706 inbox_do_reject(&ii->rules[i], msg);
709 case action_fileinto:
710 keep_message = inbox_do_fileinto(&ii->rules[i], msgnum);
712 case action_redirect:
713 keep_message = inbox_do_redirect(&ii->rules[i], msgnum);
715 case action_vacation:
716 inbox_do_vacation(&ii->rules[i], msg);
721 // Now perform the "final" action (anything other than "stop" means continue)
722 if (ii->rules[i].final_action == final_stop) {
723 syslog(LOG_DEBUG, "inboxrules: stop processing");
724 i = ii->num_rules + 1; // throw us out of scope to stop
730 syslog(LOG_DEBUG, "inboxrules: rule not activated");
734 if (msg != NULL) { // Delete the copy of the message that is currently in memory. We don't need it anymore.
738 if (!keep_message) { // Delete the copy of the message that is currently in the inbox, if rules dictated that.
739 syslog(LOG_DEBUG, "inboxrules: delete %ld from inbox", msgnum);
740 CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, ""); // we're in the inbox already
743 ii->lastproc = msgnum; // make note of the last message we processed, so we don't scan the whole inbox again
747 // A user account is identified as requring inbox processing. Go ahead and do it.
748 void do_inbox_processing_for_user(long usernum) {
749 struct CtdlMessage *msg;
750 struct inboxrules *ii;
751 char roomname[ROOMNAMELEN];
754 if (CtdlGetUserByNumber(&CC->user, usernum) != 0) { // grab the user record
755 return; // and bail out if we were given an invalid user
758 strcpy(username, CC->user.fullname); // save the user name so we can fetch it later and lock it
760 if (CC->user.msgnum_inboxrules <= 0) {
761 return; // this user has no inbox rules
764 msg = CtdlFetchMessage(CC->user.msgnum_inboxrules, 1);
766 return; // config msgnum is set but that message does not exist
769 ii = deserialize_inbox_rules(msg->cm_fields[eMesageText]);
773 return; // config message exists but body is null
776 if (ii->lastproc > CC->user.lastproc_inboxrules) { // There might be a "last message processed" number left over
777 CC->user.lastproc_inboxrules = ii->lastproc; // in the ruleset from a previous version. Use this if it is
778 } // a higher number.
780 ii->lastproc = CC->user.lastproc_inboxrules;
783 long original_lastproc = ii->lastproc;
784 syslog(LOG_DEBUG, "inboxrules: for %s, messages newer than %ld", CC->user.fullname, original_lastproc);
786 // Go to the user's inbox room and process all new messages
787 snprintf(roomname, sizeof roomname, "%010ld.%s", usernum, MAILROOM);
788 if (CtdlGetRoom(&CC->room, roomname) == 0) {
789 CtdlForEachMessage(MSGS_GT, ii->lastproc, NULL, NULL, NULL, inbox_do_msg, (void *) ii);
792 // Record the number of the last message we processed
793 if (ii->lastproc > original_lastproc) {
794 CtdlGetUserLock(&CC->user, username);
795 CC->user.lastproc_inboxrules = ii->lastproc; // Avoid processing the entire inbox next time
796 CtdlPutUserLock(&CC->user);
799 // And free the memory.
800 free_inbox_rules(ii);
804 // Here is an array of users (by number) who have received messages in their inbox and may require processing.
805 long *users_requiring_inbox_processing = NULL;
807 int num_urip_alloc = 0;
810 // Perform inbox processing for all rooms which require it
811 void perform_inbox_processing(void) {
815 return; // no action required
818 for (i=0; i<num_urip; ++i) {
819 do_inbox_processing_for_user(users_requiring_inbox_processing[i]);
822 free(users_requiring_inbox_processing);
823 users_requiring_inbox_processing = NULL;
829 // This function is called after a message is saved to a room. If it's someone's inbox, we have to check for inbox rules
830 int serv_inboxrules_roomhook(struct ctdlroom *room) {
833 // Is this someone's inbox?
834 if (!strcasecmp(&room->QRname[11], MAILROOM)) {
835 long usernum = atol(room->QRname);
838 // first check to see if this user is already on the list
840 for (i=0; i<=num_urip; ++i) {
841 if (users_requiring_inbox_processing[i] == usernum) { // already on the list!
847 // make room if we need to
848 if (num_urip_alloc == 0) {
849 num_urip_alloc = 100;
850 users_requiring_inbox_processing = malloc(sizeof(long) * num_urip_alloc);
852 else if (num_urip >= num_urip_alloc) {
853 num_urip_alloc += 100;
854 users_requiring_inbox_processing = realloc(users_requiring_inbox_processing, (sizeof(long) * num_urip_alloc));
857 // now add the user to the list
858 users_requiring_inbox_processing[num_urip++] = usernum;
862 // No errors are possible from this function.
870 // This is a client-facing function which fetches the user's inbox rules -- it omits all lines containing anything other than a rule.
872 // hmmmmm ... should we try to rebuild this in terms of deserialize_inbox_rules() instread?
873 void cmd_gibr(char *argbuf) {
875 if (CtdlAccessCheck(ac_logged_in)) return;
877 cprintf("%d inbox rules for %s\n", LISTING_FOLLOWS, CC->user.fullname);
879 struct CtdlMessage *msg = CtdlFetchMessage(CC->user.msgnum_inboxrules, 1);
881 if (!CM_IsEmpty(msg, eMesageText)) {
883 char *rest = msg->cm_fields[eMesageText];
884 while ((token = strtok_r(rest, "\n", &rest))) {
886 // for backwards compatibility, "# WEBCIT_RULE" is an alias for "rule"
887 if (!strncasecmp(token, "# WEBCIT_RULE|", 14)) {
888 strcpy(token, "rule|");
889 strcpy(&token[5], &token[14]);
892 // Output only lines containing rules.
893 if (!strncasecmp(token, "rule|", 5)) {
894 cprintf("%s\n", token);
897 cprintf("# invalid rule found : %s\n", token);
907 // Rewrite the rule set to disk after it has been modified
908 // Called by cmd_pibr()
909 // Returns the msgnum of the new rules message
910 void rewrite_rules_to_disk(const char *new_config) {
911 long old_msgnum = CC->user.msgnum_inboxrules;
912 char userconfigroomname[ROOMNAMELEN];
913 CtdlMailboxName(userconfigroomname, sizeof userconfigroomname, &CC->user, USERCONFIGROOM);
914 long new_msgnum = quickie_message("Citadel", NULL, NULL, userconfigroomname, new_config, FMT_RFC822, "inbox rules configuration");
915 CtdlGetUserLock(&CC->user, CC->curr_user);
916 CC->user.msgnum_inboxrules = new_msgnum; // Now we know where to get the rules next time
917 CC->user.lastproc_inboxrules = new_msgnum; // Avoid processing the entire inbox next time
918 CtdlPutUserLock(&CC->user);
919 if (old_msgnum > 0) {
920 syslog(LOG_DEBUG, "Deleting old message %ld from %s", old_msgnum, userconfigroomname);
921 CtdlDeleteMessages(userconfigroomname, &old_msgnum, 1, "");
927 // User transmits the new inbox rules for the account. They are inserted into the account, replacing the ones already there.
928 void cmd_pibr(char *argbuf) {
929 if (CtdlAccessCheck(ac_logged_in)) return;
932 cprintf("%d send new rules\n", SEND_LISTING);
933 char *newrules = CtdlReadMessageBody(HKEY("000"), CtdlGetConfigLong("c_maxmsglen"), NULL, 0);
934 StrBuf *NewConfig = NewStrBufPlain("Content-type: application/x-citadel-sieve-config; charset=UTF-8\nContent-transfer-encoding: 8bit\n\n", -1);
937 char *rest = newrules;
938 while ((token = strtok_r(rest, "\n", &rest))) {
939 // Accept only lines containing rules
940 if (!strncasecmp(token, "rule|", 5)) {
941 StrBufAppendBufPlain(NewConfig, token, -1, 0);
942 StrBufAppendBufPlain(NewConfig, HKEY("\n"), 0);
946 rewrite_rules_to_disk(ChrPtr(NewConfig));
947 FreeStrBuf(&NewConfig);
951 // Initialization function, called from modules_init.c
952 char *ctdl_module_init_sieve(void) {
954 CtdlRegisterProtoHook(cmd_gibr, "GIBR", "Get InBox Rules");
955 CtdlRegisterProtoHook(cmd_pibr, "PIBR", "Put InBox Rules");
956 CtdlRegisterRoomHook(serv_inboxrules_roomhook);
957 CtdlRegisterSessionHook(perform_inbox_processing, EVT_HOUSE, PRIO_HOUSE + 10);
960 // return our module name for the log