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>
27 #include <libcitadel.h>
28 #include "../../citadel_defs.h"
29 #include "../../server.h"
30 #include "../../citserver.h"
31 #include "../../support.h"
32 #include "../../config.h"
33 #include "../../database.h"
34 #include "../../msgbase.h"
35 #include "../../internet_addressing.h"
36 #include "../../ctdl_module.h"
39 // The next sections are enums and keys that drive the serialize/deserialize functions for the inbox rules/state configuration.
41 // Fields to be compared
59 char *field_keys[] = {
77 // Field comparison operators
86 char *fcomp_keys[] = {
104 char *action_keys[] = {
113 // Size comparison operators
118 char *scomp_keys[] = {
128 char *final_keys[] = {
133 // This data structure represents ONE inbox rule within the configuration.
136 int field_compare_op;
137 char compared_value[128];
141 char file_into[ROOMNAMELEN];
142 char redirect_to[1024];
143 char autoreply_message[SIZ];
147 // This data structure represents the entire inbox rules configuration AND current state for a single user.
155 // Destructor for 'struct inboxrules'
156 void free_inbox_rules(struct inboxrules *ibr) {
162 // Constructor for 'struct inboxrules' that deserializes the configuration from text input.
163 struct inboxrules *deserialize_inbox_rules(char *serialized_rules) {
166 if (!serialized_rules) {
170 // Make a copy of the supplied buffer because we're going to shit all over it with strtok_r()
171 char *sr = strdup(serialized_rules);
176 struct inboxrules *ibr = malloc(sizeof(struct inboxrules));
180 memset(ibr, 0, sizeof(struct inboxrules));
184 while ((token = strtok_r(rest, "\n", &rest))) {
186 // For backwards compatibility, "# WEBCIT_RULE" is an alias for "rule".
187 // Prior to version 930, WebCit converted its rules to Sieve scripts, but saved the rules as comments for
188 // later re-editing. Now, the rules hidden in the comments become the real rules.
189 if (!strncasecmp(token, "# WEBCIT_RULE|", 14)) {
190 strcpy(token, "rule|");
191 strcpy(&token[5], &token[14]);
194 // Lines containing actual rules are double-serialized with Base64. It seemed like a good idea at the time :(
195 if (!strncasecmp(token, "rule|", 5)) {
196 remove_token(&token[5], 0, '|');
197 char *decoded_rule = malloc(strlen(token));
198 CtdlDecodeBase64(decoded_rule, &token[5], strlen(&token[5]));
200 ibr->rules = realloc(ibr->rules, (sizeof(struct irule) * ibr->num_rules));
201 struct irule *new_rule = &ibr->rules[ibr->num_rules - 1];
202 memset(new_rule, 0, sizeof(struct irule));
204 // We have a rule , now parse it
206 int nt = num_tokens(decoded_rule, '|');
208 for (t=0; t<nt; ++t) {
209 extract_token(rtoken, decoded_rule, t, '|', sizeof(rtoken));
212 case 1: // field to compare
213 for (i=0; i<=field_all; ++i) {
214 if (!strcasecmp(rtoken, field_keys[i])) {
215 new_rule->compared_field = i;
219 case 2: // field comparison operation
220 for (i=0; i<=fcomp_notmatches; ++i) {
221 if (!strcasecmp(rtoken, fcomp_keys[i])) {
222 new_rule->field_compare_op = i;
226 case 3: // field comparison value
227 safestrncpy(new_rule->compared_value, rtoken, sizeof(new_rule->compared_value));
229 case 4: // size comparison operation
230 for (i=0; i<=scomp_smaller; ++i) {
231 if (!strcasecmp(rtoken, scomp_keys[i])) {
232 new_rule->size_compare_op = i;
236 case 5: // size comparison value
237 new_rule->compared_size = atol(rtoken);
240 for (i=0; i<=action_vacation; ++i) {
241 if (!strcasecmp(rtoken, action_keys[i])) {
242 new_rule->action = i;
246 case 7: // file into (target room)
247 safestrncpy(new_rule->file_into, rtoken, sizeof(new_rule->file_into));
249 case 8: // redirect to (target address)
250 safestrncpy(new_rule->redirect_to, rtoken, sizeof(new_rule->redirect_to));
252 case 9: // autoreply message
253 safestrncpy(new_rule->autoreply_message, rtoken, sizeof(new_rule->autoreply_message));
255 case 10: // final_action;
256 for (i=0; i<=final_stop; ++i) {
257 if (!strcasecmp(rtoken, final_keys[i])) {
258 new_rule->final_action = i;
269 // "lastproc" indicates the newest message number in the inbox that was previously processed by our inbox rules.
270 // This is a legacy location for this value and will only be used if it's the only one present.
271 else if (!strncasecmp(token, "lastproc|", 5)) {
272 ibr->lastproc = atol(&token[9]);
275 // Lines which do not contain a recognizable token must be IGNORED. These lines may be left over
276 // from a previous version and will disappear when we rewrite the config.
280 free(sr); // free our copy of the source buffer that has now been trashed with null bytes...
281 return(ibr); // and return our complex data type to the caller.
285 // Perform the "fileinto" action (save the message in another room)
286 // Returns: 1 or 0 to tell the caller to keep (1) or delete (0) the inbox copy of the message.
288 int inbox_do_fileinto(struct irule *rule, long msgnum) {
289 char *dest_folder = rule->file_into;
290 char original_room_name[ROOMNAMELEN];
291 char foldername[ROOMNAMELEN];
294 // Situations where we want to just keep the message in the inbox:
296 (IsEmptyStr(dest_folder)) // no destination room was specified
297 || (!strcasecmp(dest_folder, "INBOX")) // fileinto inbox is the same as keep
298 || (!strcasecmp(dest_folder, MAILROOM)) // fileinto "Mail" is the same as keep
300 return(1); // don't delete the inbox copy if this failed
303 // Remember what room we came from
304 safestrncpy(original_room_name, CC->room.QRname, sizeof original_room_name);
306 // First try a mailbox name match (check personal mail folders first)
307 strcpy(foldername, original_room_name); // This keeps the user namespace of the inbox
308 snprintf(&foldername[10], sizeof(foldername)-10, ".%s", dest_folder); // And this tacks on the target room name
309 c = CtdlGetRoom(&CC->room, foldername);
311 // Then a regular room name match (public and private rooms)
313 safestrncpy(foldername, dest_folder, sizeof foldername);
314 c = CtdlGetRoom(&CC->room, foldername);
318 syslog(LOG_WARNING, "inboxrules: target <%s> does not exist", dest_folder);
319 return(1); // don't delete the inbox copy if this failed
322 // Yes, we actually have to go there
323 CtdlUserGoto(NULL, 0, 0, NULL, NULL, NULL, NULL);
325 c = CtdlSaveMsgPointersInRoom(NULL, &msgnum, 1, 0, NULL, 0);
327 // Go back to the room we came from
328 if (strcasecmp(original_room_name, CC->room.QRname)) {
329 CtdlUserGoto(original_room_name, 0, 0, NULL, NULL, NULL, NULL);
332 return(0); // delete the inbox copy
336 // Perform the "redirect" action (divert the message to another email address)
337 // Returns: 1 or 0 to tell the caller to keep (1) or delete (0) the inbox copy of the message.
339 int inbox_do_redirect(struct irule *rule, long msgnum) {
340 if (IsEmptyStr(rule->redirect_to)) {
341 syslog(LOG_WARNING, "inboxrules: inbox_do_redirect() invalid recipient <%s>", rule->redirect_to);
342 return(1); // don't delete the inbox copy if this failed
345 struct recptypes *valid = validate_recipients(rule->redirect_to, NULL, 0);
347 syslog(LOG_WARNING, "inboxrules: inbox_do_redirect() invalid recipient <%s>", rule->redirect_to);
348 return(1); // don't delete the inbox copy if this failed
350 if (valid->num_error > 0) {
351 free_recipients(valid);
352 syslog(LOG_WARNING, "inboxrules: inbox_do_redirect() invalid recipient <%s>", rule->redirect_to);
353 return(1); // don't delete the inbox copy if this failed
356 struct CtdlMessage *msg = CtdlFetchMessage(msgnum, 1);
358 free_recipients(valid);
359 syslog(LOG_WARNING, "inboxrules: cannot reload message %ld for forwarding", msgnum);
360 return(1); // don't delete the inbox copy if this failed
363 CtdlSubmitMsg(msg, valid, NULL); // send the message to the new recipient
364 free_recipients(valid);
366 return(0); // delete the inbox copy
370 // Perform the "reject" action (delete the message, and tell the sender we deleted it)
371 void inbox_do_reject(struct irule *rule, struct CtdlMessage *msg) {
372 syslog(LOG_DEBUG, "inbox_do_reject: sender: <%s>, reject", msg->cm_fields[erFc822Addr]);
374 // If we can't determine who sent the message, reject silently.
376 if (!IsEmptyStr(msg->cm_fields[eMessagePath])) {
377 sender = msg->cm_fields[eMessagePath];
379 else if (!IsEmptyStr(msg->cm_fields[erFc822Addr])) {
380 sender = msg->cm_fields[erFc822Addr];
386 // Assemble the reject message.
387 char *reject_text = malloc(strlen(rule->autoreply_message) + 1024);
388 if (reject_text == NULL) {
392 "Content-type: text/plain\n"
394 "The message was refused by the recipient's mail filtering program.\n"
395 "The reason given was as follows:\n"
400 rule->autoreply_message
403 // Deliver the message
406 msg->cm_fields[eenVelopeTo],
411 "Delivery status notification"
417 // Perform the "vacation" action (send an automatic response)
418 void inbox_do_vacation(struct irule *rule, struct CtdlMessage *msg) {
419 syslog(LOG_DEBUG, "inbox_do_vacation: sender: <%s>, vacation", msg->cm_fields[erFc822Addr]);
421 // If we can't determine who sent the message, no auto-reply can be sent.
423 if (!IsEmptyStr(msg->cm_fields[eMessagePath])) {
424 sender = msg->cm_fields[eMessagePath];
426 else if (!IsEmptyStr(msg->cm_fields[erFc822Addr])) {
427 sender = msg->cm_fields[erFc822Addr];
433 // Avoid repeatedly sending auto-replies to the same correspondent over and over again by creating
434 // a hash of the user, correspondent, and reply text, and hitting the S_USETABLE database.
435 StrBuf *u = NewStrBuf();
436 StrBufPrintf(u, "vacation/%x/%x/%x",
437 HashLittle(sender, strlen(sender)),
438 HashLittle(msg->cm_fields[eenVelopeTo], msg->cm_lengths[eenVelopeTo]),
439 HashLittle(rule->autoreply_message, strlen(rule->autoreply_message))
441 int already_seen = CheckIfAlreadySeen(u);
445 // Assemble the auto-reply message.
446 StrBuf *reject_text = NewStrBuf();
447 if (reject_text == NULL) {
451 StrBufPrintf(reject_text,
452 "Content-type: text/plain\n"
457 rule->autoreply_message
460 // Deliver the auto-reply.
463 msg->cm_fields[eenVelopeTo],
468 "Delivery status notification"
470 FreeStrBuf(&reject_text);
475 // Process a single message. We know the room, the user, the rules, the message number, etc.
476 void inbox_do_msg(long msgnum, void *userdata) {
477 struct inboxrules *ii = (struct inboxrules *) userdata;
478 struct CtdlMessage *msg = NULL; // If we are loading a message to process, put it here.
479 int headers_loaded = 0; // Did we load the headers yet? Do it only once.
480 int body_loaded = 0; // Did we load the message body yet? Do it only once.
481 int metadata_loaded = 0; // Did we load the metadata yet? Do it only once.
482 struct MetaData smi; // If we are loading the metadata to compare, put it here.
483 int rule_activated = 0; // On each rule, this is set if the compare succeeds and the rule activates.
484 char compare_me[SIZ]; // On each rule, we will store the field to be compared here.
485 int compare_compound = 0; // Set to 1 when we are comparing both display name and email address
486 int keep_message = 1; // Nonzero to keep the message in the inbox after processing, 0 to delete it.
489 syslog(LOG_DEBUG, "inboxrules: processing message #%ld which is higher than %ld, we are in %s", msgnum, ii->lastproc, CC->room.QRname);
491 if (ii->num_rules <= 0) {
492 syslog(LOG_DEBUG, "inboxrules: rule set is empty");
496 for (i=0; i<ii->num_rules; ++i) {
497 syslog(LOG_DEBUG, "inboxrules: processing rule: %d , field: %s", i, field_keys[ ii->rules[i].compared_field ]);
500 // Before doing a field compare, check to see if we have the correct parts of the message in memory.
502 switch(ii->rules[i].compared_field) {
503 // These fields require loading only the top-level headers
504 case field_from: // From:
505 case field_tocc: // To: or Cc:
506 case field_subject: // Subject:
507 case field_replyto: // Reply-to:
508 case field_listid: // List-ID:
509 case field_envto: // Envelope-to:
510 case field_envfrom: // Return-path:
511 if (!headers_loaded) {
512 syslog(LOG_DEBUG, "inboxrules: loading headers for message %ld", msgnum);
513 msg = CtdlFetchMessage(msgnum, 0);
520 // These fields are not stored as Citadel headers, and therefore require a full message load.
522 case field_resentfrom:
525 case field_xspamflag:
526 case field_xspamstatus:
528 syslog(LOG_DEBUG, "inboxrules: loading all of message %ld", msgnum);
532 msg = CtdlFetchMessage(msgnum, 1);
541 if (!metadata_loaded) {
542 syslog(LOG_DEBUG, "inboxrules: loading metadata for message %ld", msgnum);
543 GetMetaData(&smi, msgnum);
548 syslog(LOG_DEBUG, "inboxrules: this is an always-on rule");
551 syslog(LOG_DEBUG, "inboxrules: unknown rule key");
554 // If the rule involves a field comparison, load the field to be compared.
556 compare_compound = 0;
557 switch(ii->rules[i].compared_field) {
558 case field_from: // From:
559 if ( (!IsEmptyStr(msg->cm_fields[erFc822Addr])) && (!IsEmptyStr(msg->cm_fields[erFc822Addr])) ) {
560 snprintf(compare_me, sizeof compare_me, "%s|%s",
561 msg->cm_fields[eAuthor],
562 msg->cm_fields[erFc822Addr]
564 compare_compound = 1; // there will be two fields to compare "name|address"
566 else if (!IsEmptyStr(msg->cm_fields[erFc822Addr])) {
567 safestrncpy(compare_me, msg->cm_fields[erFc822Addr], sizeof compare_me);
569 else if (!IsEmptyStr(msg->cm_fields[eAuthor])) {
570 safestrncpy(compare_me, msg->cm_fields[eAuthor], sizeof compare_me);
573 case field_tocc: // To: or Cc:
574 if (!IsEmptyStr(msg->cm_fields[eRecipient])) {
575 safestrncpy(compare_me, msg->cm_fields[eRecipient], sizeof compare_me);
577 if (!IsEmptyStr(msg->cm_fields[eCarbonCopY])) {
578 if (!IsEmptyStr(compare_me)) {
579 strcat(compare_me, ",");
581 safestrncpy(&compare_me[strlen(compare_me)], msg->cm_fields[eCarbonCopY], (sizeof compare_me - strlen(compare_me)));
584 case field_subject: // Subject:
585 if (!IsEmptyStr(msg->cm_fields[eMsgSubject])) {
586 safestrncpy(compare_me, msg->cm_fields[eMsgSubject], sizeof compare_me);
589 case field_replyto: // Reply-to:
590 if (!IsEmptyStr(msg->cm_fields[eReplyTo])) {
591 safestrncpy(compare_me, msg->cm_fields[eReplyTo], sizeof compare_me);
594 case field_listid: // List-ID:
595 if (!IsEmptyStr(msg->cm_fields[eListID])) {
596 safestrncpy(compare_me, msg->cm_fields[eListID], sizeof compare_me);
599 case field_envto: // Envelope-to:
600 if (!IsEmptyStr(msg->cm_fields[eenVelopeTo])) {
601 safestrncpy(compare_me, msg->cm_fields[eenVelopeTo], sizeof compare_me);
604 case field_envfrom: // Return-path:
605 if (!IsEmptyStr(msg->cm_fields[eMessagePath])) {
606 safestrncpy(compare_me, msg->cm_fields[eMessagePath], sizeof compare_me);
611 case field_resentfrom:
614 case field_xspamflag:
615 case field_xspamstatus:
621 // Message data to compare is loaded, now do something.
622 switch(ii->rules[i].compared_field) {
623 case field_from: // From:
624 case field_tocc: // To: or Cc:
625 case field_subject: // Subject:
626 case field_replyto: // Reply-to:
627 case field_listid: // List-ID:
628 case field_envto: // Envelope-to:
629 case field_envfrom: // Return-path:
631 case field_resentfrom:
634 case field_xspamflag:
635 case field_xspamstatus:
637 // For all of the above fields, we can compare the field we've loaded into the buffer.
638 syslog(LOG_DEBUG, "inboxrules: rule %d: field \"%s\" is \"%s\", looking for \"%s\"",
640 field_keys[ii->rules[i].compared_field],
642 ii->rules[i].compared_value
644 int substring_match = 0;
649 if (regcomp(®ex, ii->rules[i].compared_value, (REG_EXTENDED | REG_ICASE | REG_NOSUB | REG_NEWLINE))) {
650 syslog(LOG_ERR, "inboxrules: regcomp: %m");
653 if (compare_compound) { // comparing a compound field such as name+address
654 char *sep = strchr(compare_me, '|');
659 (strcasecmp(compare_me, ii->rules[i].compared_value) ? 0 : 1)
660 + (strcasecmp(sep, ii->rules[i].compared_value) ? 0 : 1)
663 (bmstrcasestr(compare_me, ii->rules[i].compared_value) ? 1 : 0)
664 + (bmstrcasestr(sep, ii->rules[i].compared_value) ? 1 : 0)
667 (regexec(®ex, compare_me, 0, 0, 0) ? 0 : 1)
668 + (regexec(®ex, sep, 0, 0, 0) ? 0 : 1)
672 else { // comparing a single field only
673 exact_match = (strcasecmp(compare_me, ii->rules[i].compared_value) ? 0 : 1);
674 substring_match = (bmstrcasestr(compare_me, ii->rules[i].compared_value) ? 1 : 0);
675 regex_match = (regexec(®ex, compare_me, 0, 0, 0) ? 1 : 0);
678 syslog(LOG_DEBUG, "substring match: %d", substring_match);
679 syslog(LOG_DEBUG, " regex match: %d", regex_match);
680 syslog(LOG_DEBUG, " exact match: %d", exact_match);
681 switch(ii->rules[i].field_compare_op) {
683 rule_activated = substring_match;
686 rule_activated = regex_match;
688 case fcomp_notcontains:
689 rule_activated = !substring_match;
691 case fcomp_notmatches:
692 rule_activated = !regex_match;
695 rule_activated = exact_match;
698 rule_activated = !exact_match;
705 switch(ii->rules[i].field_compare_op) {
707 rule_activated = ((smi.meta_rfc822_length > ii->rules[i].compared_size) ? 1 : 0);
710 rule_activated = ((smi.meta_rfc822_length < ii->rules[i].compared_size) ? 1 : 0);
714 case field_all: // The "all messages" rule ALWAYS triggers
717 default: // no matches, fall through and do nothing
718 syslog(LOG_WARNING, "inboxrules: an unknown field comparison was encountered");
723 // If the rule matched, perform the requested action.
724 if (rule_activated) {
725 syslog(LOG_DEBUG, "inboxrules: rule activated");
727 // Perform the requested action
728 switch(ii->rules[i].action) {
736 inbox_do_reject(&ii->rules[i], msg);
739 case action_fileinto:
740 keep_message = inbox_do_fileinto(&ii->rules[i], msgnum);
742 case action_redirect:
743 keep_message = inbox_do_redirect(&ii->rules[i], msgnum);
745 case action_vacation:
746 inbox_do_vacation(&ii->rules[i], msg);
751 // Now perform the "final" action (anything other than "stop" means continue)
752 if (ii->rules[i].final_action == final_stop) {
753 syslog(LOG_DEBUG, "inboxrules: stop processing");
754 i = ii->num_rules + 1; // throw us out of scope to stop
760 syslog(LOG_DEBUG, "inboxrules: rule not activated");
764 if (msg != NULL) { // Delete the copy of the message that is currently in memory. We don't need it anymore.
768 if (!keep_message) { // Delete the copy of the message that is currently in the inbox, if rules dictated that.
769 syslog(LOG_DEBUG, "inboxrules: delete %ld from inbox", msgnum);
770 CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, ""); // we're in the inbox already
773 ii->lastproc = msgnum; // make note of the last message we processed, so we don't scan the whole inbox again
777 // A user account is identified as requring inbox processing. Go ahead and do it.
778 void do_inbox_processing_for_user(long usernum) {
779 struct CtdlMessage *msg;
780 struct inboxrules *ii;
781 char roomname[ROOMNAMELEN];
784 if (CtdlGetUserByNumber(&CC->user, usernum) != 0) { // grab the user record
785 return; // and bail out if we were given an invalid user
788 strcpy(username, CC->user.fullname); // save the user name so we can fetch it later and lock it
790 if (CC->user.msgnum_inboxrules <= 0) {
791 return; // this user has no inbox rules
794 msg = CtdlFetchMessage(CC->user.msgnum_inboxrules, 1);
796 return; // config msgnum is set but that message does not exist
799 ii = deserialize_inbox_rules(msg->cm_fields[eMesageText]);
803 return; // config message exists but body is null
806 if (ii->lastproc > CC->user.lastproc_inboxrules) { // There might be a "last message processed" number left over
807 CC->user.lastproc_inboxrules = ii->lastproc; // in the ruleset from a previous version. Use this if it is
808 } // a higher number.
810 ii->lastproc = CC->user.lastproc_inboxrules;
813 long original_lastproc = ii->lastproc;
814 syslog(LOG_DEBUG, "inboxrules: for %s, messages newer than %ld", CC->user.fullname, original_lastproc);
816 // Go to the user's inbox room and process all new messages
817 snprintf(roomname, sizeof roomname, "%010ld.%s", usernum, MAILROOM);
818 if (CtdlGetRoom(&CC->room, roomname) == 0) {
819 CtdlForEachMessage(MSGS_GT, ii->lastproc, NULL, NULL, NULL, inbox_do_msg, (void *) ii);
822 // Record the number of the last message we processed
823 if (ii->lastproc > original_lastproc) {
824 CtdlGetUserLock(&CC->user, username);
825 CC->user.lastproc_inboxrules = ii->lastproc; // Avoid processing the entire inbox next time
826 CtdlPutUserLock(&CC->user);
829 // And free the memory.
830 free_inbox_rules(ii);
834 // Here is an array of users (by number) who have received messages in their inbox and may require processing.
835 long *users_requiring_inbox_processing = NULL;
837 int num_urip_alloc = 0;
840 // Perform inbox processing for all rooms which require it
841 void perform_inbox_processing(void) {
845 return; // no action required
848 for (i=0; i<num_urip; ++i) {
849 do_inbox_processing_for_user(users_requiring_inbox_processing[i]);
852 free(users_requiring_inbox_processing);
853 users_requiring_inbox_processing = NULL;
859 // 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
860 int serv_inboxrules_roomhook(struct ctdlroom *room) {
863 // Is this someone's inbox?
864 if (!strcasecmp(&room->QRname[11], MAILROOM)) {
865 long usernum = atol(room->QRname);
868 // first check to see if this user is already on the list
870 for (i=0; i<=num_urip; ++i) {
871 if (users_requiring_inbox_processing[i] == usernum) { // already on the list!
877 // make room if we need to
878 if (num_urip_alloc == 0) {
879 num_urip_alloc = 100;
880 users_requiring_inbox_processing = malloc(sizeof(long) * num_urip_alloc);
882 else if (num_urip >= num_urip_alloc) {
883 num_urip_alloc += 100;
884 users_requiring_inbox_processing = realloc(users_requiring_inbox_processing, (sizeof(long) * num_urip_alloc));
887 // now add the user to the list
888 users_requiring_inbox_processing[num_urip++] = usernum;
892 // No errors are possible from this function.
900 // This is a client-facing function which fetches the user's inbox rules -- it omits all lines containing anything other than a rule.
902 // 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);
937 // Rewrite the rule set to disk after it has been modified
938 // Called by cmd_pibr()
939 // Returns the msgnum of the new rules message
940 void rewrite_rules_to_disk(const char *new_config) {
941 long old_msgnum = CC->user.msgnum_inboxrules;
942 char userconfigroomname[ROOMNAMELEN];
943 CtdlMailboxName(userconfigroomname, sizeof userconfigroomname, &CC->user, USERCONFIGROOM);
944 long new_msgnum = quickie_message("Citadel", NULL, NULL, userconfigroomname, new_config, FMT_RFC822, "inbox rules configuration");
945 CtdlGetUserLock(&CC->user, CC->curr_user);
946 CC->user.msgnum_inboxrules = new_msgnum; // Now we know where to get the rules next time
947 CC->user.lastproc_inboxrules = new_msgnum; // Avoid processing the entire inbox next time
948 CtdlPutUserLock(&CC->user);
949 if (old_msgnum > 0) {
950 syslog(LOG_DEBUG, "Deleting old message %ld from %s", old_msgnum, userconfigroomname);
951 CtdlDeleteMessages(userconfigroomname, &old_msgnum, 1, "");
957 // User transmits the new inbox rules for the account. They are inserted into the account, replacing the ones already there.
958 void cmd_pibr(char *argbuf) {
959 if (CtdlAccessCheck(ac_logged_in)) return;
962 cprintf("%d send new rules\n", SEND_LISTING);
963 char *newrules = CtdlReadMessageBody(HKEY("000"), CtdlGetConfigLong("c_maxmsglen"), NULL, 0);
964 StrBuf *NewConfig = NewStrBufPlain("Content-type: application/x-citadel-sieve-config; charset=UTF-8\nContent-transfer-encoding: 8bit\n\n", -1);
967 char *rest = newrules;
968 while ((token = strtok_r(rest, "\n", &rest))) {
969 // Accept only lines containing rules
970 if (!strncasecmp(token, "rule|", 5)) {
971 StrBufAppendBufPlain(NewConfig, token, -1, 0);
972 StrBufAppendBufPlain(NewConfig, HKEY("\n"), 0);
976 rewrite_rules_to_disk(ChrPtr(NewConfig));
977 FreeStrBuf(&NewConfig);
981 // Initialization function, called from modules_init.c
982 char *ctdl_module_init_sieve(void) {
984 CtdlRegisterProtoHook(cmd_gibr, "GIBR", "Get InBox Rules");
985 CtdlRegisterProtoHook(cmd_pibr, "PIBR", "Put InBox Rules");
986 CtdlRegisterRoomHook(serv_inboxrules_roomhook);
987 CtdlRegisterSessionHook(perform_inbox_processing, EVT_HOUSE, PRIO_HOUSE + 10);
990 // return our module name for the log