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, "Value of field to compare is: <%s>", compare_me);
639 int substring_match = 0;
642 if (compare_compound) {
643 char *sep = strchr(compare_me, '|');
648 (strcasecmp(compare_me, ii->rules[i].compared_value) ? 0 : 1)
649 + (strcasecmp(sep, ii->rules[i].compared_value) ? 0 : 1)
652 (bmstrcasestr(compare_me, ii->rules[i].compared_value) ? 1 : 0)
653 + (bmstrcasestr(sep, ii->rules[i].compared_value) ? 1 : 0)
658 exact_match = (strcasecmp(compare_me, ii->rules[i].compared_value) ? 0 : 1);
659 substring_match = (bmstrcasestr(compare_me, ii->rules[i].compared_value) ? 1 : 0);
661 syslog(LOG_DEBUG, "substring match: %d", substring_match);
662 syslog(LOG_DEBUG, " regex match: %d", regex_match);
663 syslog(LOG_DEBUG, " exact match: %d", exact_match);
664 switch(ii->rules[i].field_compare_op) {
666 rule_activated = substring_match;
669 rule_activated = regex_match;
671 case fcomp_notcontains:
672 rule_activated = !substring_match;
674 case fcomp_notmatches:
675 rule_activated = !regex_match;
678 rule_activated = exact_match;
681 rule_activated = !exact_match;
688 switch(ii->rules[i].field_compare_op) {
690 rule_activated = ((smi.meta_rfc822_length > ii->rules[i].compared_size) ? 1 : 0);
693 rule_activated = ((smi.meta_rfc822_length < ii->rules[i].compared_size) ? 1 : 0);
697 case field_all: // The "all messages" rule ALWAYS triggers
700 default: // no matches, fall through and do nothing
701 syslog(LOG_WARNING, "inboxrules: an unknown field comparison was encountered");
706 // If the rule matched, perform the requested action.
707 if (rule_activated) {
708 syslog(LOG_DEBUG, "inboxrules: rule activated");
710 // Perform the requested action
711 switch(ii->rules[i].action) {
719 inbox_do_reject(&ii->rules[i], msg);
722 case action_fileinto:
723 keep_message = inbox_do_fileinto(&ii->rules[i], msgnum);
725 case action_redirect:
726 keep_message = inbox_do_redirect(&ii->rules[i], msgnum);
728 case action_vacation:
729 inbox_do_vacation(&ii->rules[i], msg);
734 // Now perform the "final" action (anything other than "stop" means continue)
735 if (ii->rules[i].final_action == final_stop) {
736 syslog(LOG_DEBUG, "inboxrules: stop processing");
737 i = ii->num_rules + 1; // throw us out of scope to stop
743 syslog(LOG_DEBUG, "inboxrules: rule not activated");
747 if (msg != NULL) { // Delete the copy of the message that is currently in memory. We don't need it anymore.
751 if (!keep_message) { // Delete the copy of the message that is currently in the inbox, if rules dictated that.
752 syslog(LOG_DEBUG, "inboxrules: delete %ld from inbox", msgnum);
753 CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, ""); // we're in the inbox already
756 ii->lastproc = msgnum; // make note of the last message we processed, so we don't scan the whole inbox again
760 // A user account is identified as requring inbox processing. Go ahead and do it.
761 void do_inbox_processing_for_user(long usernum) {
762 struct CtdlMessage *msg;
763 struct inboxrules *ii;
764 char roomname[ROOMNAMELEN];
767 if (CtdlGetUserByNumber(&CC->user, usernum) != 0) { // grab the user record
768 return; // and bail out if we were given an invalid user
771 strcpy(username, CC->user.fullname); // save the user name so we can fetch it later and lock it
773 if (CC->user.msgnum_inboxrules <= 0) {
774 return; // this user has no inbox rules
777 msg = CtdlFetchMessage(CC->user.msgnum_inboxrules, 1);
779 return; // config msgnum is set but that message does not exist
782 ii = deserialize_inbox_rules(msg->cm_fields[eMesageText]);
786 return; // config message exists but body is null
789 if (ii->lastproc > CC->user.lastproc_inboxrules) { // There might be a "last message processed" number left over
790 CC->user.lastproc_inboxrules = ii->lastproc; // in the ruleset from a previous version. Use this if it is
791 } // a higher number.
793 ii->lastproc = CC->user.lastproc_inboxrules;
796 long original_lastproc = ii->lastproc;
797 syslog(LOG_DEBUG, "inboxrules: for %s, messages newer than %ld", CC->user.fullname, original_lastproc);
799 // Go to the user's inbox room and process all new messages
800 snprintf(roomname, sizeof roomname, "%010ld.%s", usernum, MAILROOM);
801 if (CtdlGetRoom(&CC->room, roomname) == 0) {
802 CtdlForEachMessage(MSGS_GT, ii->lastproc, NULL, NULL, NULL, inbox_do_msg, (void *) ii);
805 // Record the number of the last message we processed
806 if (ii->lastproc > original_lastproc) {
807 CtdlGetUserLock(&CC->user, username);
808 CC->user.lastproc_inboxrules = ii->lastproc; // Avoid processing the entire inbox next time
809 CtdlPutUserLock(&CC->user);
812 // And free the memory.
813 free_inbox_rules(ii);
817 // Here is an array of users (by number) who have received messages in their inbox and may require processing.
818 long *users_requiring_inbox_processing = NULL;
820 int num_urip_alloc = 0;
823 // Perform inbox processing for all rooms which require it
824 void perform_inbox_processing(void) {
828 return; // no action required
831 for (i=0; i<num_urip; ++i) {
832 do_inbox_processing_for_user(users_requiring_inbox_processing[i]);
835 free(users_requiring_inbox_processing);
836 users_requiring_inbox_processing = NULL;
842 // 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
843 int serv_inboxrules_roomhook(struct ctdlroom *room) {
846 // Is this someone's inbox?
847 if (!strcasecmp(&room->QRname[11], MAILROOM)) {
848 long usernum = atol(room->QRname);
851 // first check to see if this user is already on the list
853 for (i=0; i<=num_urip; ++i) {
854 if (users_requiring_inbox_processing[i] == usernum) { // already on the list!
860 // make room if we need to
861 if (num_urip_alloc == 0) {
862 num_urip_alloc = 100;
863 users_requiring_inbox_processing = malloc(sizeof(long) * num_urip_alloc);
865 else if (num_urip >= num_urip_alloc) {
866 num_urip_alloc += 100;
867 users_requiring_inbox_processing = realloc(users_requiring_inbox_processing, (sizeof(long) * num_urip_alloc));
870 // now add the user to the list
871 users_requiring_inbox_processing[num_urip++] = usernum;
875 // 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?
886 void cmd_gibr(char *argbuf) {
888 if (CtdlAccessCheck(ac_logged_in)) return;
890 cprintf("%d inbox rules for %s\n", LISTING_FOLLOWS, CC->user.fullname);
892 struct CtdlMessage *msg = CtdlFetchMessage(CC->user.msgnum_inboxrules, 1);
894 if (!CM_IsEmpty(msg, eMesageText)) {
896 char *rest = msg->cm_fields[eMesageText];
897 while ((token = strtok_r(rest, "\n", &rest))) {
899 // for backwards compatibility, "# WEBCIT_RULE" is an alias for "rule"
900 if (!strncasecmp(token, "# WEBCIT_RULE|", 14)) {
901 strcpy(token, "rule|");
902 strcpy(&token[5], &token[14]);
905 // Output only lines containing rules.
906 if (!strncasecmp(token, "rule|", 5)) {
907 cprintf("%s\n", token);
910 cprintf("# invalid rule found : %s\n", token);
920 // Rewrite the rule set to disk after it has been modified
921 // Called by cmd_pibr()
922 // Returns the msgnum of the new rules message
923 void rewrite_rules_to_disk(const char *new_config) {
924 long old_msgnum = CC->user.msgnum_inboxrules;
925 char userconfigroomname[ROOMNAMELEN];
926 CtdlMailboxName(userconfigroomname, sizeof userconfigroomname, &CC->user, USERCONFIGROOM);
927 long new_msgnum = quickie_message("Citadel", NULL, NULL, userconfigroomname, new_config, FMT_RFC822, "inbox rules configuration");
928 CtdlGetUserLock(&CC->user, CC->curr_user);
929 CC->user.msgnum_inboxrules = new_msgnum; // Now we know where to get the rules next time
930 CC->user.lastproc_inboxrules = new_msgnum; // Avoid processing the entire inbox next time
931 CtdlPutUserLock(&CC->user);
932 if (old_msgnum > 0) {
933 syslog(LOG_DEBUG, "Deleting old message %ld from %s", old_msgnum, userconfigroomname);
934 CtdlDeleteMessages(userconfigroomname, &old_msgnum, 1, "");
940 // User transmits the new inbox rules for the account. They are inserted into the account, replacing the ones already there.
941 void cmd_pibr(char *argbuf) {
942 if (CtdlAccessCheck(ac_logged_in)) return;
945 cprintf("%d send new rules\n", SEND_LISTING);
946 char *newrules = CtdlReadMessageBody(HKEY("000"), CtdlGetConfigLong("c_maxmsglen"), NULL, 0);
947 StrBuf *NewConfig = NewStrBufPlain("Content-type: application/x-citadel-sieve-config; charset=UTF-8\nContent-transfer-encoding: 8bit\n\n", -1);
950 char *rest = newrules;
951 while ((token = strtok_r(rest, "\n", &rest))) {
952 // Accept only lines containing rules
953 if (!strncasecmp(token, "rule|", 5)) {
954 StrBufAppendBufPlain(NewConfig, token, -1, 0);
955 StrBufAppendBufPlain(NewConfig, HKEY("\n"), 0);
959 rewrite_rules_to_disk(ChrPtr(NewConfig));
960 FreeStrBuf(&NewConfig);
964 // Initialization function, called from modules_init.c
965 char *ctdl_module_init_sieve(void) {
967 CtdlRegisterProtoHook(cmd_gibr, "GIBR", "Get InBox Rules");
968 CtdlRegisterProtoHook(cmd_pibr, "PIBR", "Put InBox Rules");
969 CtdlRegisterRoomHook(serv_inboxrules_roomhook);
970 CtdlRegisterSessionHook(perform_inbox_processing, EVT_HOUSE, PRIO_HOUSE + 10);
973 // return our module name for the log