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, '|');
221 for (int t=0; t<nt; ++t) {
222 extract_token(rtoken, decoded_rule, t, '|', sizeof(rtoken));
225 case 1: // field to compare
226 for (i=0; i<=field_all; ++i) {
227 if (!strcasecmp(rtoken, field_keys[i])) {
228 new_rule->compared_field = i;
232 case 2: // field comparison operation
233 for (i=0; i<=fcomp_notmatches; ++i) {
234 if (!strcasecmp(rtoken, fcomp_keys[i])) {
235 new_rule->field_compare_op = i;
239 case 3: // field comparison value
240 safestrncpy(new_rule->compared_value, rtoken, sizeof(new_rule->compared_value));
242 case 4: // size comparison operation
243 for (i=0; i<=scomp_smaller; ++i) {
244 if (!strcasecmp(rtoken, scomp_keys[i])) {
245 new_rule->size_compare_op = i;
249 case 5: // size comparison value
250 new_rule->compared_size = atol(rtoken);
253 for (i=0; i<=action_vacation; ++i) {
254 if (!strcasecmp(rtoken, action_keys[i])) {
255 new_rule->action = i;
259 case 7: // file into (target room)
260 safestrncpy(new_rule->file_into, rtoken, sizeof(new_rule->file_into));
262 case 8: // redirect to (target address)
263 safestrncpy(new_rule->redirect_to, rtoken, sizeof(new_rule->redirect_to));
265 case 9: // autoreply message
266 safestrncpy(new_rule->autoreply_message, rtoken, sizeof(new_rule->autoreply_message));
268 case 10: // final_action;
269 for (i=0; i<=final_stop; ++i) {
270 if (!strcasecmp(rtoken, final_keys[i])) {
271 new_rule->final_action = i;
282 // "lastproc" indicates the newest message number in the inbox that was previously processed by our inbox rules.
283 // This is a legacy location for this value and will only be used if it's the only one present.
284 else if (!strncasecmp(token, "lastproc|", 5)) {
285 ibr->lastproc = atol(&token[9]);
288 // Lines which do not contain a recognizable token must be IGNORED. These lines may be left over
289 // from a previous version and will disappear when we rewrite the config.
293 free(sr); // free our copy of the source buffer that has now been trashed with null bytes...
294 return(ibr); // and return our complex data type to the caller.
298 // Perform the "fileinto" action (save the message in another room)
299 // Returns: 1 or 0 to tell the caller to keep (1) or delete (0) the inbox copy of the message.
301 int inbox_do_fileinto(struct irule *rule, long msgnum) {
302 char *dest_folder = rule->file_into;
303 char original_room_name[ROOMNAMELEN];
304 char foldername[ROOMNAMELEN];
307 // Situations where we want to just keep the message in the inbox:
309 (IsEmptyStr(dest_folder)) // no destination room was specified
310 || (!strcasecmp(dest_folder, "INBOX")) // fileinto inbox is the same as keep
311 || (!strcasecmp(dest_folder, MAILROOM)) // fileinto "Mail" is the same as keep
313 return(1); // don't delete the inbox copy if this failed
316 // Remember what room we came from
317 safestrncpy(original_room_name, CC->room.QRname, sizeof original_room_name);
319 // First try a mailbox name match (check personal mail folders first)
320 strcpy(foldername, original_room_name); // This keeps the user namespace of the inbox
321 snprintf(&foldername[10], sizeof(foldername)-10, ".%s", dest_folder); // And this tacks on the target room name
322 c = CtdlGetRoom(&CC->room, foldername);
324 // Then a regular room name match (public and private rooms)
326 safestrncpy(foldername, dest_folder, sizeof foldername);
327 c = CtdlGetRoom(&CC->room, foldername);
331 syslog(LOG_WARNING, "inboxrules: target <%s> does not exist", dest_folder);
332 return(1); // don't delete the inbox copy if this failed
335 // Yes, we actually have to go there
336 CtdlUserGoto(NULL, 0, 0, NULL, NULL, NULL, NULL);
338 c = CtdlSaveMsgPointersInRoom(NULL, &msgnum, 1, 0, NULL, 0);
340 // Go back to the room we came from
341 if (strcasecmp(original_room_name, CC->room.QRname)) {
342 CtdlUserGoto(original_room_name, 0, 0, NULL, NULL, NULL, NULL);
345 return(0); // delete the inbox copy
349 // Perform the "redirect" action (divert the message to another email address)
350 // Returns: 1 or 0 to tell the caller to keep (1) or delete (0) the inbox copy of the message.
352 int inbox_do_redirect(struct irule *rule, long msgnum) {
353 if (IsEmptyStr(rule->redirect_to)) {
354 syslog(LOG_WARNING, "inboxrules: inbox_do_redirect() invalid recipient <%s>", rule->redirect_to);
355 return(1); // don't delete the inbox copy if this failed
358 recptypes *valid = validate_recipients(rule->redirect_to, NULL, 0);
360 syslog(LOG_WARNING, "inboxrules: inbox_do_redirect() invalid recipient <%s>", rule->redirect_to);
361 return(1); // don't delete the inbox copy if this failed
363 if (valid->num_error > 0) {
364 free_recipients(valid);
365 syslog(LOG_WARNING, "inboxrules: inbox_do_redirect() invalid recipient <%s>", rule->redirect_to);
366 return(1); // don't delete the inbox copy if this failed
369 struct CtdlMessage *msg = CtdlFetchMessage(msgnum, 1);
371 free_recipients(valid);
372 syslog(LOG_WARNING, "inboxrules: cannot reload message %ld for forwarding", msgnum);
373 return(1); // don't delete the inbox copy if this failed
376 CtdlSubmitMsg(msg, valid, NULL, 0); // send the message to the new recipient
377 free_recipients(valid);
379 return(0); // delete the inbox copy
384 * Perform the "reject" action (delete the message, and tell the sender we deleted it)
386 void inbox_do_reject(struct irule *rule, struct CtdlMessage *msg) {
387 syslog(LOG_DEBUG, "inbox_do_reject: sender: <%s>, reject message: <%s>",
388 msg->cm_fields[erFc822Addr],
389 rule->autoreply_message
392 // If we can't determine who sent the message, reject silently.
394 if (!IsEmptyStr(msg->cm_fields[eMessagePath])) {
395 sender = msg->cm_fields[eMessagePath];
397 else if (!IsEmptyStr(msg->cm_fields[erFc822Addr])) {
398 sender = msg->cm_fields[erFc822Addr];
404 // Assemble the reject message.
405 char *reject_text = malloc(strlen(rule->autoreply_message) + 1024);
406 if (reject_text == NULL) {
410 "Content-type: text/plain\n"
412 "The message was refused by the recipient's mail filtering program.\n"
413 "The reason given was as follows:\n"
418 rule->autoreply_message
421 // Deliver the message
424 msg->cm_fields[eenVelopeTo],
429 "Delivery status notification"
436 * Perform the "vacation" action (send an automatic response)
438 void inbox_do_vacation(struct irule *rule, struct CtdlMessage *msg) {
439 syslog(LOG_DEBUG, "inbox_do_vacation: sender: <%s>, vacation message: <%s>",
440 msg->cm_fields[erFc822Addr],
441 rule->autoreply_message
444 // If we can't determine who sent the message, no auto-reply can be sent.
446 if (!IsEmptyStr(msg->cm_fields[eMessagePath])) {
447 sender = msg->cm_fields[eMessagePath];
449 else if (!IsEmptyStr(msg->cm_fields[erFc822Addr])) {
450 sender = msg->cm_fields[erFc822Addr];
456 // Avoid repeatedly sending auto-replies to the same correspondent over and over again by creating
457 // a hash of the user, correspondent, and reply text, and hitting the S_USETABLE database.
458 StrBuf *u = NewStrBuf();
459 StrBufPrintf(u, "vacation/%x/%x/%x",
460 HashLittle(sender, strlen(sender)),
461 HashLittle(msg->cm_fields[eenVelopeTo], msg->cm_lengths[eenVelopeTo]),
462 HashLittle(rule->autoreply_message, strlen(rule->autoreply_message))
464 int already_seen = CheckIfAlreadySeen(u);
468 // Assemble the auto-reply message.
469 StrBuf *reject_text = NewStrBuf();
470 if (reject_text == NULL) {
474 StrBufPrintf(reject_text,
475 "Content-type: text/plain\n"
480 rule->autoreply_message
483 // Deliver the auto-reply.
486 msg->cm_fields[eenVelopeTo],
491 "Delivery status notification"
493 FreeStrBuf(&reject_text);
499 * Process a single message. We know the room, the user, the rules, the message number, etc.
501 void inbox_do_msg(long msgnum, void *userdata) {
502 struct inboxrules *ii = (struct inboxrules *) userdata;
503 struct CtdlMessage *msg = NULL; // If we are loading a message to process, put it here.
504 int headers_loaded = 0; // Did we load the headers yet? Do it only once.
505 int body_loaded = 0; // Did we load the message body yet? Do it only once.
506 int metadata_loaded = 0; // Did we load the metadata yet? Do it only once.
507 struct MetaData smi; // If we are loading the metadata to compare, put it here.
508 int rule_activated = 0; // On each rule, this is set if the compare succeeds and the rule activates.
509 char compare_me[SIZ]; // On each rule, we will store the field to be compared here.
510 int keep_message = 1; // Nonzero to keep the message in the inbox after processing, 0 to delete it.
513 syslog(LOG_DEBUG, "inboxrules: processing message #%ld which is higher than %ld, we are in %s", msgnum, ii->lastproc, CC->room.QRname);
515 if (ii->num_rules <= 0) {
516 syslog(LOG_DEBUG, "inboxrules: rule set is empty");
520 for (i=0; i<ii->num_rules; ++i) {
521 syslog(LOG_DEBUG, "inboxrules: processing rule %d is %s", i, field_keys[ ii->rules[i].compared_field ]);
524 // Before doing a field compare, check to see if we have the correct parts of the message in memory.
526 switch(ii->rules[i].compared_field) {
527 // These fields require loading only the top-level headers
528 case field_from: // From:
529 case field_tocc: // To: or Cc:
530 case field_subject: // Subject:
531 case field_replyto: // Reply-to:
532 case field_listid: // List-ID:
533 case field_envto: // Envelope-to:
534 case field_envfrom: // Return-path:
535 if (!headers_loaded) {
536 syslog(LOG_DEBUG, "inboxrules: loading headers for message %ld", msgnum);
537 msg = CtdlFetchMessage(msgnum, 0);
544 // These fields are not stored as Citadel headers, and therefore require a full message load.
546 case field_resentfrom:
549 case field_xspamflag:
550 case field_xspamstatus:
552 syslog(LOG_DEBUG, "inboxrules: loading all of message %ld", msgnum);
556 msg = CtdlFetchMessage(msgnum, 1);
565 if (!metadata_loaded) {
566 syslog(LOG_DEBUG, "inboxrules: loading metadata for message %ld", msgnum);
567 GetMetaData(&smi, msgnum);
572 syslog(LOG_DEBUG, "inboxrules: this is an always-on rule");
575 syslog(LOG_DEBUG, "inboxrules: unknown rule key");
578 // If the rule involves a field comparison, load the field to be compared.
580 switch(ii->rules[i].compared_field) {
582 case field_from: // From:
583 if (!IsEmptyStr(msg->cm_fields[erFc822Addr])) {
584 safestrncpy(compare_me, msg->cm_fields[erFc822Addr], sizeof compare_me);
587 case field_tocc: // To: or Cc:
588 if (!IsEmptyStr(msg->cm_fields[eRecipient])) {
589 safestrncpy(compare_me, msg->cm_fields[eRecipient], sizeof compare_me);
591 if (!IsEmptyStr(msg->cm_fields[eCarbonCopY])) {
592 if (!IsEmptyStr(compare_me)) {
593 strcat(compare_me, ",");
595 safestrncpy(&compare_me[strlen(compare_me)], msg->cm_fields[eCarbonCopY], (sizeof compare_me - strlen(compare_me)));
598 case field_subject: // Subject:
599 if (!IsEmptyStr(msg->cm_fields[eMsgSubject])) {
600 safestrncpy(compare_me, msg->cm_fields[eMsgSubject], sizeof compare_me);
603 case field_replyto: // Reply-to:
604 if (!IsEmptyStr(msg->cm_fields[eReplyTo])) {
605 safestrncpy(compare_me, msg->cm_fields[eReplyTo], sizeof compare_me);
608 case field_listid: // List-ID:
609 if (!IsEmptyStr(msg->cm_fields[eListID])) {
610 safestrncpy(compare_me, msg->cm_fields[eListID], sizeof compare_me);
613 case field_envto: // Envelope-to:
614 if (!IsEmptyStr(msg->cm_fields[eenVelopeTo])) {
615 safestrncpy(compare_me, msg->cm_fields[eenVelopeTo], sizeof compare_me);
618 case field_envfrom: // Return-path:
619 if (!IsEmptyStr(msg->cm_fields[eMessagePath])) {
620 safestrncpy(compare_me, msg->cm_fields[eMessagePath], sizeof compare_me);
625 case field_resentfrom:
628 case field_xspamflag:
629 case field_xspamstatus:
635 // Message data to compare is loaded, now do something.
636 switch(ii->rules[i].compared_field) {
637 case field_from: // From:
638 case field_tocc: // To: or Cc:
639 case field_subject: // Subject:
640 case field_replyto: // Reply-to:
641 case field_listid: // List-ID:
642 case field_envto: // Envelope-to:
643 case field_envfrom: // Return-path:
645 case field_resentfrom:
648 case field_xspamflag:
649 case field_xspamstatus:
651 // For all of the above fields, we can compare the field we've loaded into the buffer.
652 syslog(LOG_DEBUG, "Value of field to compare is: <%s>", compare_me);
653 switch(ii->rules[i].field_compare_op) {
656 rule_activated = (bmstrcasestr(compare_me, ii->rules[i].compared_value) ? 1 : 0);
657 syslog(LOG_DEBUG, "Does %s contain %s? %s", compare_me, ii->rules[i].compared_value, rule_activated?"yes":"no");
659 case fcomp_notcontains:
660 case fcomp_notmatches:
661 rule_activated = (bmstrcasestr(compare_me, ii->rules[i].compared_value) ? 0 : 1);
662 syslog(LOG_DEBUG, "Does %s contain %s? %s", compare_me, ii->rules[i].compared_value, rule_activated?"yes":"no");
665 rule_activated = (strcasecmp(compare_me, ii->rules[i].compared_value) ? 0 : 1);
666 syslog(LOG_DEBUG, "Does %s equal %s? %s", compare_me, ii->rules[i].compared_value, rule_activated?"yes":"no");
669 rule_activated = (strcasecmp(compare_me, ii->rules[i].compared_value) ? 1 : 0);
670 syslog(LOG_DEBUG, "Does %s equal %s? %s", compare_me, ii->rules[i].compared_value, rule_activated?"yes":"no");
677 syslog(LOG_DEBUG, "comparing actual message size %ld to rule message size %ld", smi.meta_rfc822_length, ii->rules[i].compared_size);
678 switch(ii->rules[i].field_compare_op) {
680 rule_activated = ((smi.meta_rfc822_length > ii->rules[i].compared_size) ? 1 : 0);
681 syslog(LOG_DEBUG, "Is %ld larger than %ld? %s", smi.meta_rfc822_length, ii->rules[i].compared_size, (smi.meta_rfc822_length > ii->rules[i].compared_size) ? "yes":"no");
684 rule_activated = ((smi.meta_rfc822_length < ii->rules[i].compared_size) ? 1 : 0);
685 syslog(LOG_DEBUG, "Is %ld smaller than %ld? %s", smi.meta_rfc822_length, ii->rules[i].compared_size, (smi.meta_rfc822_length < ii->rules[i].compared_size) ? "yes":"no");
689 case field_all: // The "all messages" rule ALWAYS triggers
692 default: // no matches, fall through and do nothing
693 syslog(LOG_DEBUG, "inboxrules: an unknown field comparison was encountered");
698 // If the rule matched, perform the requested action.
699 if (rule_activated) {
700 syslog(LOG_DEBUG, "inboxrules: rule activated");
702 // Perform the requested action
703 switch(ii->rules[i].action) {
711 inbox_do_reject(&ii->rules[i], msg);
714 case action_fileinto:
715 keep_message = inbox_do_fileinto(&ii->rules[i], msgnum);
717 case action_redirect:
718 keep_message = inbox_do_redirect(&ii->rules[i], msgnum);
720 case action_vacation:
721 inbox_do_vacation(&ii->rules[i], msg);
726 // Now perform the "final" action (anything other than "stop" means continue)
727 if (ii->rules[i].final_action == final_stop) {
728 syslog(LOG_DEBUG, "inboxrules: stop processing");
729 i = ii->num_rules + 1; // throw us out of scope to stop
735 syslog(LOG_DEBUG, "inboxrules: rule not activated");
739 if (msg != NULL) { // Delete the copy of the message that is currently in memory. We don't need it anymore.
743 if (!keep_message) { // Delete the copy of the message that is currently in the inbox, if rules dictated that.
744 syslog(LOG_DEBUG, "inboxrules: delete %ld from inbox", msgnum);
745 CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, ""); // we're in the inbox already
748 ii->lastproc = msgnum; // make note of the last message we processed, so we don't scan the whole inbox again
753 * A user account is identified as requring inbox processing.
756 void do_inbox_processing_for_user(long usernum) {
757 struct CtdlMessage *msg;
758 struct inboxrules *ii;
759 char roomname[ROOMNAMELEN];
762 if (CtdlGetUserByNumber(&CC->user, usernum) != 0) { // grab the user record
763 return; // and bail out if we were given an invalid user
766 strcpy(username, CC->user.fullname); // save the user name so we can fetch it later and lock it
768 if (CC->user.msgnum_inboxrules <= 0) {
769 return; // this user has no inbox rules
772 msg = CtdlFetchMessage(CC->user.msgnum_inboxrules, 1);
774 return; // config msgnum is set but that message does not exist
777 ii = deserialize_inbox_rules(msg->cm_fields[eMesageText]);
781 return; // config message exists but body is null
785 syslog(LOG_DEBUG, "ii->lastproc %ld", ii->lastproc);
786 syslog(LOG_DEBUG, "CC->user.lastproc_inboxrules %ld", CC->user.lastproc_inboxrules);
788 if (ii->lastproc > CC->user.lastproc_inboxrules) { // There might be a "last message processed" number left over
789 CC->user.lastproc_inboxrules = ii->lastproc; // in the ruleset from a previous version. Use this if it is
790 } // a higher number.
792 ii->lastproc = CC->user.lastproc_inboxrules;
795 long original_lastproc = ii->lastproc;
796 syslog(LOG_DEBUG, "inboxrules: for %s, messages newer than %ld", CC->user.fullname, original_lastproc);
798 // Go to the user's inbox room and process all new messages
799 snprintf(roomname, sizeof roomname, "%010ld.%s", usernum, MAILROOM);
800 if (CtdlGetRoom(&CC->room, roomname) == 0) {
801 CtdlForEachMessage(MSGS_GT, ii->lastproc, NULL, NULL, NULL, inbox_do_msg, (void *) ii);
804 // Record the number of the last message we processed
805 if (ii->lastproc > original_lastproc) {
806 CtdlGetUserLock(&CC->user, username);
807 CC->user.lastproc_inboxrules = ii->lastproc; // Avoid processing the entire inbox next time
808 CtdlPutUserLock(&CC->user);
811 // And free the memory.
812 free_inbox_rules(ii);
817 * Here is an array of users (by number) who have received messages in their inbox and may require processing.
819 long *users_requiring_inbox_processing = NULL;
821 int num_urip_alloc = 0;
825 * Perform inbox processing for all rooms which require it
827 void perform_inbox_processing(void) {
829 return; // no action required
832 for (int i=0; i<num_urip; ++i) {
833 do_inbox_processing_for_user(users_requiring_inbox_processing[i]);
836 free(users_requiring_inbox_processing);
837 users_requiring_inbox_processing = NULL;
844 * This function is called after a message is saved to a room.
845 * If it's someone's inbox, we have to check for inbox rules
847 int serv_inboxrules_roomhook(struct ctdlroom *room) {
849 // Is this someone's inbox?
850 if (!strcasecmp(&room->QRname[11], MAILROOM)) {
851 long usernum = atol(room->QRname);
854 // first check to see if this user is already on the list
856 for (int i=0; i<=num_urip; ++i) {
857 if (users_requiring_inbox_processing[i] == usernum) { // already on the list!
863 // make room if we need to
864 if (num_urip_alloc == 0) {
865 num_urip_alloc = 100;
866 users_requiring_inbox_processing = malloc(sizeof(long) * num_urip_alloc);
868 else if (num_urip >= num_urip_alloc) {
869 num_urip_alloc += 100;
870 users_requiring_inbox_processing = realloc(users_requiring_inbox_processing, (sizeof(long) * num_urip_alloc));
873 // now add the user to the list
874 users_requiring_inbox_processing[num_urip++] = usernum;
878 // No errors are possible from this function.
887 * This is a client-facing function which fetches the user's inbox rules -- it omits all lines containing anything other than a rule.
889 * hmmmmm ... should we try to rebuild this in terms of deserialize_inbox_rules() instread?
891 void cmd_gibr(char *argbuf) {
893 if (CtdlAccessCheck(ac_logged_in)) return;
895 cprintf("%d inbox rules for %s\n", LISTING_FOLLOWS, CC->user.fullname);
897 struct CtdlMessage *msg = CtdlFetchMessage(CC->user.msgnum_inboxrules, 1);
899 if (!CM_IsEmpty(msg, eMesageText)) {
901 char *rest = msg->cm_fields[eMesageText];
902 while ((token = strtok_r(rest, "\n", &rest))) {
904 // for backwards compatibility, "# WEBCIT_RULE" is an alias for "rule"
905 if (!strncasecmp(token, "# WEBCIT_RULE|", 14)) {
906 strcpy(token, "rule|");
907 strcpy(&token[5], &token[14]);
910 // Output only lines containing rules.
911 if (!strncasecmp(token, "rule|", 5)) {
912 cprintf("%s\n", token);
915 cprintf("# invalid rule found : %s\n", token);
926 * Rewrite the rule set to disk after it has been modified
927 * Called by cmd_pibr()
928 * Returns the msgnum of the new rules message
930 void rewrite_rules_to_disk(const char *new_config) {
931 long old_msgnum = CC->user.msgnum_inboxrules;
932 char userconfigroomname[ROOMNAMELEN];
933 CtdlMailboxName(userconfigroomname, sizeof userconfigroomname, &CC->user, USERCONFIGROOM);
934 long new_msgnum = quickie_message("Citadel", NULL, NULL, userconfigroomname, new_config, FMT_RFC822, "inbox rules configuration");
935 CtdlGetUserLock(&CC->user, CC->curr_user);
936 CC->user.msgnum_inboxrules = new_msgnum; // Now we know where to get the rules next time
937 CC->user.lastproc_inboxrules = new_msgnum; // Avoid processing the entire inbox next time
938 CtdlPutUserLock(&CC->user);
939 if (old_msgnum > 0) {
940 syslog(LOG_DEBUG, "Deleting old message %ld from %s", old_msgnum, userconfigroomname);
941 CtdlDeleteMessages(userconfigroomname, &old_msgnum, 1, "");
949 * User transmits the new inbox rules for the account. They are inserted into the account, replacing the ones already there.
951 void cmd_pibr(char *argbuf) {
952 if (CtdlAccessCheck(ac_logged_in)) return;
955 cprintf("%d send new rules\n", SEND_LISTING);
956 char *newrules = CtdlReadMessageBody(HKEY("000"), CtdlGetConfigLong("c_maxmsglen"), NULL, 0);
957 StrBuf *NewConfig = NewStrBufPlain("Content-type: application/x-citadel-sieve-config; charset=UTF-8\nContent-transfer-encoding: 8bit\n\n", -1);
960 char *rest = newrules;
961 while ((token = strtok_r(rest, "\n", &rest))) {
962 // Accept only lines containing rules
963 if (!strncasecmp(token, "rule|", 5)) {
964 StrBufAppendBufPlain(NewConfig, token, -1, 0);
965 StrBufAppendBufPlain(NewConfig, HKEY("\n"), 0);
969 rewrite_rules_to_disk(ChrPtr(NewConfig));
970 FreeStrBuf(&NewConfig);
974 CTDL_MODULE_INIT(sieve)
978 CtdlRegisterProtoHook(cmd_gibr, "GIBR", "Get InBox Rules");
979 CtdlRegisterProtoHook(cmd_pibr, "PIBR", "Put InBox Rules");
980 CtdlRegisterRoomHook(serv_inboxrules_roomhook);
981 CtdlRegisterSessionHook(perform_inbox_processing, EVT_HOUSE, PRIO_HOUSE + 10);
984 /* return our module name for the log */