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"
52 * The next sections are enums and keys that drive the serialize/deserialize functions for the inbox rules/state configuration.
55 // Fields to be compared
73 char *field_keys[] = {
91 // Field comparison operators
100 char *fcomp_keys[] = {
118 char *action_keys[] = {
127 // Size comparison operators
132 char *scomp_keys[] = {
142 char *final_keys[] = {
147 // This data structure represents ONE inbox rule within the configuration.
150 int field_compare_op;
151 char compared_value[128];
155 char file_into[ROOMNAMELEN];
156 char redirect_to[1024];
157 char autoreply_message[SIZ];
161 // This data structure represents the entire inbox rules configuration AND current state for a single user.
169 // Destructor for 'struct inboxrules'
170 void free_inbox_rules(struct inboxrules *ibr) {
176 // Constructor for 'struct inboxrules' that deserializes the configuration from text input.
177 struct inboxrules *deserialize_inbox_rules(char *serialized_rules) {
180 if (!serialized_rules) {
184 /* Make a copy of the supplied buffer because we're going to shit all over it with strtok_r() */
185 char *sr = strdup(serialized_rules);
190 struct inboxrules *ibr = malloc(sizeof(struct inboxrules));
194 memset(ibr, 0, sizeof(struct inboxrules));
198 while ((token = strtok_r(rest, "\n", &rest))) {
200 // For backwards compatibility, "# WEBCIT_RULE" is an alias for "rule".
201 // Prior to version 930, WebCit converted its rules to Sieve scripts, but saved the rules as comments for later re-editing.
202 // Now, the rules hidden in the comments become the real rules.
203 if (!strncasecmp(token, "# WEBCIT_RULE|", 14)) {
204 strcpy(token, "rule|");
205 strcpy(&token[5], &token[14]);
208 // Lines containing actual rules are double-serialized with Base64. It seemed like a good idea at the time :(
209 if (!strncasecmp(token, "rule|", 5)) {
210 remove_token(&token[5], 0, '|');
211 char *decoded_rule = malloc(strlen(token));
212 CtdlDecodeBase64(decoded_rule, &token[5], strlen(&token[5]));
214 ibr->rules = realloc(ibr->rules, (sizeof(struct irule) * ibr->num_rules));
215 struct irule *new_rule = &ibr->rules[ibr->num_rules - 1];
216 memset(new_rule, 0, sizeof(struct irule));
218 // We have a rule , now parse it
220 int nt = num_tokens(decoded_rule, '|');
222 for (t=0; t<nt; ++t) {
223 extract_token(rtoken, decoded_rule, t, '|', sizeof(rtoken));
226 case 1: // field to compare
227 for (i=0; i<=field_all; ++i) {
228 if (!strcasecmp(rtoken, field_keys[i])) {
229 new_rule->compared_field = i;
233 case 2: // field comparison operation
234 for (i=0; i<=fcomp_notmatches; ++i) {
235 if (!strcasecmp(rtoken, fcomp_keys[i])) {
236 new_rule->field_compare_op = i;
240 case 3: // field comparison value
241 safestrncpy(new_rule->compared_value, rtoken, sizeof(new_rule->compared_value));
243 case 4: // size comparison operation
244 for (i=0; i<=scomp_smaller; ++i) {
245 if (!strcasecmp(rtoken, scomp_keys[i])) {
246 new_rule->size_compare_op = i;
250 case 5: // size comparison value
251 new_rule->compared_size = atol(rtoken);
254 for (i=0; i<=action_vacation; ++i) {
255 if (!strcasecmp(rtoken, action_keys[i])) {
256 new_rule->action = i;
260 case 7: // file into (target room)
261 safestrncpy(new_rule->file_into, rtoken, sizeof(new_rule->file_into));
263 case 8: // redirect to (target address)
264 safestrncpy(new_rule->redirect_to, rtoken, sizeof(new_rule->redirect_to));
266 case 9: // autoreply message
267 safestrncpy(new_rule->autoreply_message, rtoken, sizeof(new_rule->autoreply_message));
269 case 10: // final_action;
270 for (i=0; i<=final_stop; ++i) {
271 if (!strcasecmp(rtoken, final_keys[i])) {
272 new_rule->final_action = i;
283 // "lastproc" indicates the newest message number in the inbox that was previously processed by our inbox rules.
284 // This is a legacy location for this value and will only be used if it's the only one present.
285 else if (!strncasecmp(token, "lastproc|", 5)) {
286 ibr->lastproc = atol(&token[9]);
289 // Lines which do not contain a recognizable token must be IGNORED. These lines may be left over
290 // from a previous version and will disappear when we rewrite the config.
294 free(sr); // free our copy of the source buffer that has now been trashed with null bytes...
295 return(ibr); // and return our complex data type to the caller.
299 // Perform the "fileinto" action (save the message in another room)
300 // Returns: 1 or 0 to tell the caller to keep (1) or delete (0) the inbox copy of the message.
302 int inbox_do_fileinto(struct irule *rule, long msgnum) {
303 char *dest_folder = rule->file_into;
304 char original_room_name[ROOMNAMELEN];
305 char foldername[ROOMNAMELEN];
308 // Situations where we want to just keep the message in the inbox:
310 (IsEmptyStr(dest_folder)) // no destination room was specified
311 || (!strcasecmp(dest_folder, "INBOX")) // fileinto inbox is the same as keep
312 || (!strcasecmp(dest_folder, MAILROOM)) // fileinto "Mail" is the same as keep
314 return(1); // don't delete the inbox copy if this failed
317 // Remember what room we came from
318 safestrncpy(original_room_name, CC->room.QRname, sizeof original_room_name);
320 // First try a mailbox name match (check personal mail folders first)
321 strcpy(foldername, original_room_name); // This keeps the user namespace of the inbox
322 snprintf(&foldername[10], sizeof(foldername)-10, ".%s", dest_folder); // And this tacks on the target room name
323 c = CtdlGetRoom(&CC->room, foldername);
325 // Then a regular room name match (public and private rooms)
327 safestrncpy(foldername, dest_folder, sizeof foldername);
328 c = CtdlGetRoom(&CC->room, foldername);
332 syslog(LOG_WARNING, "inboxrules: target <%s> does not exist", dest_folder);
333 return(1); // don't delete the inbox copy if this failed
336 // Yes, we actually have to go there
337 CtdlUserGoto(NULL, 0, 0, NULL, NULL, NULL, NULL);
339 c = CtdlSaveMsgPointersInRoom(NULL, &msgnum, 1, 0, NULL, 0);
341 // Go back to the room we came from
342 if (strcasecmp(original_room_name, CC->room.QRname)) {
343 CtdlUserGoto(original_room_name, 0, 0, NULL, NULL, NULL, NULL);
346 return(0); // delete the inbox copy
350 // Perform the "redirect" action (divert the message to another email address)
351 // Returns: 1 or 0 to tell the caller to keep (1) or delete (0) the inbox copy of the message.
353 int inbox_do_redirect(struct irule *rule, long msgnum) {
354 if (IsEmptyStr(rule->redirect_to)) {
355 syslog(LOG_WARNING, "inboxrules: inbox_do_redirect() invalid recipient <%s>", rule->redirect_to);
356 return(1); // don't delete the inbox copy if this failed
359 recptypes *valid = validate_recipients(rule->redirect_to, NULL, 0);
361 syslog(LOG_WARNING, "inboxrules: inbox_do_redirect() invalid recipient <%s>", rule->redirect_to);
362 return(1); // don't delete the inbox copy if this failed
364 if (valid->num_error > 0) {
365 free_recipients(valid);
366 syslog(LOG_WARNING, "inboxrules: inbox_do_redirect() invalid recipient <%s>", rule->redirect_to);
367 return(1); // don't delete the inbox copy if this failed
370 struct CtdlMessage *msg = CtdlFetchMessage(msgnum, 1);
372 free_recipients(valid);
373 syslog(LOG_WARNING, "inboxrules: cannot reload message %ld for forwarding", msgnum);
374 return(1); // don't delete the inbox copy if this failed
377 CtdlSubmitMsg(msg, valid, NULL); // send the message to the new recipient
378 free_recipients(valid);
380 return(0); // delete the inbox copy
385 * Perform the "reject" action (delete the message, and tell the sender we deleted it)
387 void inbox_do_reject(struct irule *rule, struct CtdlMessage *msg) {
388 syslog(LOG_DEBUG, "inbox_do_reject: sender: <%s>, reject", msg->cm_fields[erFc822Addr]);
390 // If we can't determine who sent the message, reject silently.
392 if (!IsEmptyStr(msg->cm_fields[eMessagePath])) {
393 sender = msg->cm_fields[eMessagePath];
395 else if (!IsEmptyStr(msg->cm_fields[erFc822Addr])) {
396 sender = msg->cm_fields[erFc822Addr];
402 // Assemble the reject message.
403 char *reject_text = malloc(strlen(rule->autoreply_message) + 1024);
404 if (reject_text == NULL) {
408 "Content-type: text/plain\n"
410 "The message was refused by the recipient's mail filtering program.\n"
411 "The reason given was as follows:\n"
416 rule->autoreply_message
419 // Deliver the message
422 msg->cm_fields[eenVelopeTo],
427 "Delivery status notification"
434 * Perform the "vacation" action (send an automatic response)
436 void inbox_do_vacation(struct irule *rule, struct CtdlMessage *msg) {
437 syslog(LOG_DEBUG, "inbox_do_vacation: sender: <%s>, vacation", msg->cm_fields[erFc822Addr]);
439 // If we can't determine who sent the message, no auto-reply can be sent.
441 if (!IsEmptyStr(msg->cm_fields[eMessagePath])) {
442 sender = msg->cm_fields[eMessagePath];
444 else if (!IsEmptyStr(msg->cm_fields[erFc822Addr])) {
445 sender = msg->cm_fields[erFc822Addr];
451 // Avoid repeatedly sending auto-replies to the same correspondent over and over again by creating
452 // a hash of the user, correspondent, and reply text, and hitting the S_USETABLE database.
453 StrBuf *u = NewStrBuf();
454 StrBufPrintf(u, "vacation/%x/%x/%x",
455 HashLittle(sender, strlen(sender)),
456 HashLittle(msg->cm_fields[eenVelopeTo], msg->cm_lengths[eenVelopeTo]),
457 HashLittle(rule->autoreply_message, strlen(rule->autoreply_message))
459 int already_seen = CheckIfAlreadySeen(u);
463 // Assemble the auto-reply message.
464 StrBuf *reject_text = NewStrBuf();
465 if (reject_text == NULL) {
469 StrBufPrintf(reject_text,
470 "Content-type: text/plain\n"
475 rule->autoreply_message
478 // Deliver the auto-reply.
481 msg->cm_fields[eenVelopeTo],
486 "Delivery status notification"
488 FreeStrBuf(&reject_text);
494 * Process a single message. We know the room, the user, the rules, the message number, etc.
496 void inbox_do_msg(long msgnum, void *userdata) {
497 struct inboxrules *ii = (struct inboxrules *) userdata;
498 struct CtdlMessage *msg = NULL; // If we are loading a message to process, put it here.
499 int headers_loaded = 0; // Did we load the headers yet? Do it only once.
500 int body_loaded = 0; // Did we load the message body yet? Do it only once.
501 int metadata_loaded = 0; // Did we load the metadata yet? Do it only once.
502 struct MetaData smi; // If we are loading the metadata to compare, put it here.
503 int rule_activated = 0; // On each rule, this is set if the compare succeeds and the rule activates.
504 char compare_me[SIZ]; // On each rule, we will store the field to be compared here.
505 int compare_compound = 0; // Set to 1 when we are comparing both display name and email address
506 int keep_message = 1; // Nonzero to keep the message in the inbox after processing, 0 to delete it.
509 syslog(LOG_DEBUG, "inboxrules: processing message #%ld which is higher than %ld, we are in %s", msgnum, ii->lastproc, CC->room.QRname);
511 if (ii->num_rules <= 0) {
512 syslog(LOG_DEBUG, "inboxrules: rule set is empty");
516 for (i=0; i<ii->num_rules; ++i) {
517 syslog(LOG_DEBUG, "inboxrules: processing rule %d is %s", i, field_keys[ ii->rules[i].compared_field ]);
520 // Before doing a field compare, check to see if we have the correct parts of the message in memory.
522 switch(ii->rules[i].compared_field) {
523 // These fields require loading only the top-level headers
524 case field_from: // From:
525 case field_tocc: // To: or Cc:
526 case field_subject: // Subject:
527 case field_replyto: // Reply-to:
528 case field_listid: // List-ID:
529 case field_envto: // Envelope-to:
530 case field_envfrom: // Return-path:
531 if (!headers_loaded) {
532 syslog(LOG_DEBUG, "inboxrules: loading headers for message %ld", msgnum);
533 msg = CtdlFetchMessage(msgnum, 0);
540 // These fields are not stored as Citadel headers, and therefore require a full message load.
542 case field_resentfrom:
545 case field_xspamflag:
546 case field_xspamstatus:
548 syslog(LOG_DEBUG, "inboxrules: loading all of message %ld", msgnum);
552 msg = CtdlFetchMessage(msgnum, 1);
561 if (!metadata_loaded) {
562 syslog(LOG_DEBUG, "inboxrules: loading metadata for message %ld", msgnum);
563 GetMetaData(&smi, msgnum);
568 syslog(LOG_DEBUG, "inboxrules: this is an always-on rule");
571 syslog(LOG_DEBUG, "inboxrules: unknown rule key");
574 // If the rule involves a field comparison, load the field to be compared.
576 compare_compound = 0;
577 switch(ii->rules[i].compared_field) {
578 case field_from: // From:
579 if ( (!IsEmptyStr(msg->cm_fields[erFc822Addr])) && (!IsEmptyStr(msg->cm_fields[erFc822Addr])) ) {
580 snprintf(compare_me, sizeof compare_me, "%s|%s",
581 msg->cm_fields[eAuthor],
582 msg->cm_fields[erFc822Addr]
584 compare_compound = 1; // there will be two fields to compare "name|address"
586 else if (!IsEmptyStr(msg->cm_fields[erFc822Addr])) {
587 safestrncpy(compare_me, msg->cm_fields[erFc822Addr], sizeof compare_me);
589 else if (!IsEmptyStr(msg->cm_fields[eAuthor])) {
590 safestrncpy(compare_me, msg->cm_fields[eAuthor], sizeof compare_me);
593 case field_tocc: // To: or Cc:
594 if (!IsEmptyStr(msg->cm_fields[eRecipient])) {
595 safestrncpy(compare_me, msg->cm_fields[eRecipient], sizeof compare_me);
597 if (!IsEmptyStr(msg->cm_fields[eCarbonCopY])) {
598 if (!IsEmptyStr(compare_me)) {
599 strcat(compare_me, ",");
601 safestrncpy(&compare_me[strlen(compare_me)], msg->cm_fields[eCarbonCopY], (sizeof compare_me - strlen(compare_me)));
604 case field_subject: // Subject:
605 if (!IsEmptyStr(msg->cm_fields[eMsgSubject])) {
606 safestrncpy(compare_me, msg->cm_fields[eMsgSubject], sizeof compare_me);
609 case field_replyto: // Reply-to:
610 if (!IsEmptyStr(msg->cm_fields[eReplyTo])) {
611 safestrncpy(compare_me, msg->cm_fields[eReplyTo], sizeof compare_me);
614 case field_listid: // List-ID:
615 if (!IsEmptyStr(msg->cm_fields[eListID])) {
616 safestrncpy(compare_me, msg->cm_fields[eListID], sizeof compare_me);
619 case field_envto: // Envelope-to:
620 if (!IsEmptyStr(msg->cm_fields[eenVelopeTo])) {
621 safestrncpy(compare_me, msg->cm_fields[eenVelopeTo], sizeof compare_me);
624 case field_envfrom: // Return-path:
625 if (!IsEmptyStr(msg->cm_fields[eMessagePath])) {
626 safestrncpy(compare_me, msg->cm_fields[eMessagePath], sizeof compare_me);
631 case field_resentfrom:
634 case field_xspamflag:
635 case field_xspamstatus:
641 // Message data to compare is loaded, now do something.
642 switch(ii->rules[i].compared_field) {
643 case field_from: // From:
644 case field_tocc: // To: or Cc:
645 case field_subject: // Subject:
646 case field_replyto: // Reply-to:
647 case field_listid: // List-ID:
648 case field_envto: // Envelope-to:
649 case field_envfrom: // Return-path:
651 case field_resentfrom:
654 case field_xspamflag:
655 case field_xspamstatus:
657 // For all of the above fields, we can compare the field we've loaded into the buffer.
658 syslog(LOG_DEBUG, "Value of field to compare is: <%s>", compare_me);
659 int substring_match = (bmstrcasestr(compare_me, ii->rules[i].compared_value) ? 1 : 0);
661 if (compare_compound) {
662 char *sep = strchr(compare_me, '|');
666 (strcasecmp(compare_me, ii->rules[i].compared_value) ? 0 : 1)
667 + (strcasecmp(++sep, ii->rules[i].compared_value) ? 0 : 1)
672 exact_match = (strcasecmp(compare_me, ii->rules[i].compared_value) ? 0 : 1);
674 syslog(LOG_DEBUG, "substring match: %d", substring_match);
675 syslog(LOG_DEBUG, "exact match: %d", exact_match);
676 switch(ii->rules[i].field_compare_op) {
679 rule_activated = substring_match;
681 case fcomp_notcontains:
682 case fcomp_notmatches:
683 rule_activated = !substring_match;
686 rule_activated = exact_match;
689 rule_activated = !exact_match;
696 switch(ii->rules[i].field_compare_op) {
698 rule_activated = ((smi.meta_rfc822_length > ii->rules[i].compared_size) ? 1 : 0);
701 rule_activated = ((smi.meta_rfc822_length < ii->rules[i].compared_size) ? 1 : 0);
705 case field_all: // The "all messages" rule ALWAYS triggers
708 default: // no matches, fall through and do nothing
709 syslog(LOG_WARNING, "inboxrules: an unknown field comparison was encountered");
714 // If the rule matched, perform the requested action.
715 if (rule_activated) {
716 syslog(LOG_DEBUG, "inboxrules: rule activated");
718 // Perform the requested action
719 switch(ii->rules[i].action) {
727 inbox_do_reject(&ii->rules[i], msg);
730 case action_fileinto:
731 keep_message = inbox_do_fileinto(&ii->rules[i], msgnum);
733 case action_redirect:
734 keep_message = inbox_do_redirect(&ii->rules[i], msgnum);
736 case action_vacation:
737 inbox_do_vacation(&ii->rules[i], msg);
742 // Now perform the "final" action (anything other than "stop" means continue)
743 if (ii->rules[i].final_action == final_stop) {
744 syslog(LOG_DEBUG, "inboxrules: stop processing");
745 i = ii->num_rules + 1; // throw us out of scope to stop
751 syslog(LOG_DEBUG, "inboxrules: rule not activated");
755 if (msg != NULL) { // Delete the copy of the message that is currently in memory. We don't need it anymore.
759 if (!keep_message) { // Delete the copy of the message that is currently in the inbox, if rules dictated that.
760 syslog(LOG_DEBUG, "inboxrules: delete %ld from inbox", msgnum);
761 CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, ""); // we're in the inbox already
764 ii->lastproc = msgnum; // make note of the last message we processed, so we don't scan the whole inbox again
769 * A user account is identified as requring inbox processing.
772 void do_inbox_processing_for_user(long usernum) {
773 struct CtdlMessage *msg;
774 struct inboxrules *ii;
775 char roomname[ROOMNAMELEN];
778 if (CtdlGetUserByNumber(&CC->user, usernum) != 0) { // grab the user record
779 return; // and bail out if we were given an invalid user
782 strcpy(username, CC->user.fullname); // save the user name so we can fetch it later and lock it
784 if (CC->user.msgnum_inboxrules <= 0) {
785 return; // this user has no inbox rules
788 msg = CtdlFetchMessage(CC->user.msgnum_inboxrules, 1);
790 return; // config msgnum is set but that message does not exist
793 ii = deserialize_inbox_rules(msg->cm_fields[eMesageText]);
797 return; // config message exists but body is null
800 if (ii->lastproc > CC->user.lastproc_inboxrules) { // There might be a "last message processed" number left over
801 CC->user.lastproc_inboxrules = ii->lastproc; // in the ruleset from a previous version. Use this if it is
802 } // a higher number.
804 ii->lastproc = CC->user.lastproc_inboxrules;
807 long original_lastproc = ii->lastproc;
808 syslog(LOG_DEBUG, "inboxrules: for %s, messages newer than %ld", CC->user.fullname, original_lastproc);
810 // Go to the user's inbox room and process all new messages
811 snprintf(roomname, sizeof roomname, "%010ld.%s", usernum, MAILROOM);
812 if (CtdlGetRoom(&CC->room, roomname) == 0) {
813 CtdlForEachMessage(MSGS_GT, ii->lastproc, NULL, NULL, NULL, inbox_do_msg, (void *) ii);
816 // Record the number of the last message we processed
817 if (ii->lastproc > original_lastproc) {
818 CtdlGetUserLock(&CC->user, username);
819 CC->user.lastproc_inboxrules = ii->lastproc; // Avoid processing the entire inbox next time
820 CtdlPutUserLock(&CC->user);
823 // And free the memory.
824 free_inbox_rules(ii);
829 * Here is an array of users (by number) who have received messages in their inbox and may require processing.
831 long *users_requiring_inbox_processing = NULL;
833 int num_urip_alloc = 0;
837 * Perform inbox processing for all rooms which require it
839 void perform_inbox_processing(void) {
841 return; // no action required
844 for (int i=0; i<num_urip; ++i) {
845 do_inbox_processing_for_user(users_requiring_inbox_processing[i]);
848 free(users_requiring_inbox_processing);
849 users_requiring_inbox_processing = NULL;
856 * This function is called after a message is saved to a room.
857 * If it's someone's inbox, we have to check for inbox rules
859 int serv_inboxrules_roomhook(struct ctdlroom *room) {
861 // Is this someone's inbox?
862 if (!strcasecmp(&room->QRname[11], MAILROOM)) {
863 long usernum = atol(room->QRname);
866 // first check to see if this user is already on the list
868 for (int i=0; i<=num_urip; ++i) {
869 if (users_requiring_inbox_processing[i] == usernum) { // already on the list!
875 // make room if we need to
876 if (num_urip_alloc == 0) {
877 num_urip_alloc = 100;
878 users_requiring_inbox_processing = malloc(sizeof(long) * num_urip_alloc);
880 else if (num_urip >= num_urip_alloc) {
881 num_urip_alloc += 100;
882 users_requiring_inbox_processing = realloc(users_requiring_inbox_processing, (sizeof(long) * num_urip_alloc));
885 // now add the user to the list
886 users_requiring_inbox_processing[num_urip++] = usernum;
890 // No errors are possible from this function.
899 * This is a client-facing function which fetches the user's inbox rules -- it omits all lines containing anything other than a rule.
901 * hmmmmm ... should we try to rebuild this in terms of deserialize_inbox_rules() instread?
903 void cmd_gibr(char *argbuf) {
905 if (CtdlAccessCheck(ac_logged_in)) return;
907 cprintf("%d inbox rules for %s\n", LISTING_FOLLOWS, CC->user.fullname);
909 struct CtdlMessage *msg = CtdlFetchMessage(CC->user.msgnum_inboxrules, 1);
911 if (!CM_IsEmpty(msg, eMesageText)) {
913 char *rest = msg->cm_fields[eMesageText];
914 while ((token = strtok_r(rest, "\n", &rest))) {
916 // for backwards compatibility, "# WEBCIT_RULE" is an alias for "rule"
917 if (!strncasecmp(token, "# WEBCIT_RULE|", 14)) {
918 strcpy(token, "rule|");
919 strcpy(&token[5], &token[14]);
922 // Output only lines containing rules.
923 if (!strncasecmp(token, "rule|", 5)) {
924 cprintf("%s\n", token);
927 cprintf("# invalid rule found : %s\n", token);
938 * Rewrite the rule set to disk after it has been modified
939 * Called by cmd_pibr()
940 * Returns the msgnum of the new rules message
942 void rewrite_rules_to_disk(const char *new_config) {
943 long old_msgnum = CC->user.msgnum_inboxrules;
944 char userconfigroomname[ROOMNAMELEN];
945 CtdlMailboxName(userconfigroomname, sizeof userconfigroomname, &CC->user, USERCONFIGROOM);
946 long new_msgnum = quickie_message("Citadel", NULL, NULL, userconfigroomname, new_config, FMT_RFC822, "inbox rules configuration");
947 CtdlGetUserLock(&CC->user, CC->curr_user);
948 CC->user.msgnum_inboxrules = new_msgnum; // Now we know where to get the rules next time
949 CC->user.lastproc_inboxrules = new_msgnum; // Avoid processing the entire inbox next time
950 CtdlPutUserLock(&CC->user);
951 if (old_msgnum > 0) {
952 syslog(LOG_DEBUG, "Deleting old message %ld from %s", old_msgnum, userconfigroomname);
953 CtdlDeleteMessages(userconfigroomname, &old_msgnum, 1, "");
961 * User transmits the new inbox rules for the account. They are inserted into the account, replacing the ones already there.
963 void cmd_pibr(char *argbuf) {
964 if (CtdlAccessCheck(ac_logged_in)) return;
967 cprintf("%d send new rules\n", SEND_LISTING);
968 char *newrules = CtdlReadMessageBody(HKEY("000"), CtdlGetConfigLong("c_maxmsglen"), NULL, 0);
969 StrBuf *NewConfig = NewStrBufPlain("Content-type: application/x-citadel-sieve-config; charset=UTF-8\nContent-transfer-encoding: 8bit\n\n", -1);
972 char *rest = newrules;
973 while ((token = strtok_r(rest, "\n", &rest))) {
974 // Accept only lines containing rules
975 if (!strncasecmp(token, "rule|", 5)) {
976 StrBufAppendBufPlain(NewConfig, token, -1, 0);
977 StrBufAppendBufPlain(NewConfig, HKEY("\n"), 0);
981 rewrite_rules_to_disk(ChrPtr(NewConfig));
982 FreeStrBuf(&NewConfig);
986 CTDL_MODULE_INIT(sieve)
990 CtdlRegisterProtoHook(cmd_gibr, "GIBR", "Get InBox Rules");
991 CtdlRegisterProtoHook(cmd_pibr, "PIBR", "Put InBox Rules");
992 CtdlRegisterRoomHook(serv_inboxrules_roomhook);
993 CtdlRegisterSessionHook(perform_inbox_processing, EVT_HOUSE, PRIO_HOUSE + 10);
996 /* return our module name for the log */