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.
488 const char *ptr,*ptr1; // Temporary variables to get X-Spam-Status
491 syslog(LOG_DEBUG, "inboxrules: processing message #%ld which is higher than %ld, we are in %s", msgnum, ii->lastproc, CC->room.QRname);
493 if (ii->num_rules <= 0) {
494 syslog(LOG_DEBUG, "inboxrules: rule set is empty");
498 for (i=0; i<ii->num_rules; ++i) {
499 syslog(LOG_DEBUG, "inboxrules: processing rule: %d , field: %s", i, field_keys[ ii->rules[i].compared_field ]);
502 // Before doing a field compare, check to see if we have the correct parts of the message in memory.
504 switch(ii->rules[i].compared_field) {
505 // These fields require loading only the top-level headers
506 case field_from: // From:
507 case field_tocc: // To: or Cc:
508 case field_subject: // Subject:
509 case field_replyto: // Reply-to:
510 case field_listid: // List-ID:
511 case field_envto: // Envelope-to:
512 case field_envfrom: // Return-path:
513 if (!headers_loaded) {
514 syslog(LOG_DEBUG, "inboxrules: loading headers for message %ld", msgnum);
515 msg = CtdlFetchMessage(msgnum, 0);
522 // These fields are not stored as Citadel headers, and therefore require a full message load.
524 case field_resentfrom:
527 case field_xspamflag:
528 case field_xspamstatus:
530 syslog(LOG_DEBUG, "inboxrules: loading all of message %ld", msgnum);
534 msg = CtdlFetchMessage(msgnum, 1);
543 if (!metadata_loaded) {
544 syslog(LOG_DEBUG, "inboxrules: loading metadata for message %ld", msgnum);
545 GetMetaData(&smi, msgnum);
550 syslog(LOG_DEBUG, "inboxrules: this is an always-on rule");
553 syslog(LOG_DEBUG, "inboxrules: unknown rule key");
556 // If the rule involves a field comparison, load the field to be compared.
558 compare_compound = 0;
559 switch(ii->rules[i].compared_field) {
560 case field_from: // From:
561 if ( (!IsEmptyStr(msg->cm_fields[erFc822Addr])) && (!IsEmptyStr(msg->cm_fields[erFc822Addr])) ) {
562 snprintf(compare_me, sizeof compare_me, "%s|%s",
563 msg->cm_fields[eAuthor],
564 msg->cm_fields[erFc822Addr]
566 compare_compound = 1; // there will be two fields to compare "name|address"
568 else if (!IsEmptyStr(msg->cm_fields[erFc822Addr])) {
569 safestrncpy(compare_me, msg->cm_fields[erFc822Addr], sizeof compare_me);
571 else if (!IsEmptyStr(msg->cm_fields[eAuthor])) {
572 safestrncpy(compare_me, msg->cm_fields[eAuthor], sizeof compare_me);
575 case field_tocc: // To: or Cc:
576 if (!IsEmptyStr(msg->cm_fields[eRecipient])) {
577 safestrncpy(compare_me, msg->cm_fields[eRecipient], sizeof compare_me);
579 if (!IsEmptyStr(msg->cm_fields[eCarbonCopY])) {
580 if (!IsEmptyStr(compare_me)) {
581 strcat(compare_me, ",");
583 safestrncpy(&compare_me[strlen(compare_me)], msg->cm_fields[eCarbonCopY], (sizeof compare_me - strlen(compare_me)));
586 case field_subject: // Subject:
587 if (!IsEmptyStr(msg->cm_fields[eMsgSubject])) {
588 safestrncpy(compare_me, msg->cm_fields[eMsgSubject], sizeof compare_me);
591 case field_replyto: // Reply-to:
592 if (!IsEmptyStr(msg->cm_fields[eReplyTo])) {
593 safestrncpy(compare_me, msg->cm_fields[eReplyTo], sizeof compare_me);
596 case field_listid: // List-ID:
597 if (!IsEmptyStr(msg->cm_fields[eListID])) {
598 safestrncpy(compare_me, msg->cm_fields[eListID], sizeof compare_me);
601 case field_envto: // Envelope-to:
602 if (!IsEmptyStr(msg->cm_fields[eenVelopeTo])) {
603 safestrncpy(compare_me, msg->cm_fields[eenVelopeTo], sizeof compare_me);
606 case field_envfrom: // Return-path:
607 if (!IsEmptyStr(msg->cm_fields[eMessagePath])) {
608 safestrncpy(compare_me, msg->cm_fields[eMessagePath], sizeof compare_me);
612 case field_xspamstatus:
613 if (!IsEmptyStr(msg->cm_fields[eMesageText])) {
614 if ((ptr=strstr(msg->cm_fields[eMesageText],"X-Spam-Status"))!=NULL) {
617 while (*ptr1 && (*ptr1!='\r') && (*ptr1!='\n')) {
621 if (len && (len<SIZ)) {
622 memcpy(compare_me, ptr, len);
623 compare_me[len]='\0';
630 case field_resentfrom:
633 case field_xspamflag:
639 // Message data to compare is loaded, now do something.
640 switch(ii->rules[i].compared_field) {
641 case field_from: // From:
642 case field_tocc: // To: or Cc:
643 case field_subject: // Subject:
644 case field_replyto: // Reply-to:
645 case field_listid: // List-ID:
646 case field_envto: // Envelope-to:
647 case field_envfrom: // Return-path:
649 case field_resentfrom:
652 case field_xspamflag:
653 case field_xspamstatus:
655 // For all of the above fields, we can compare the field we've loaded into the buffer.
656 syslog(LOG_DEBUG, "inboxrules: rule %d: field \"%s\" is \"%s\", looking for \"%s\"",
658 field_keys[ii->rules[i].compared_field],
660 ii->rules[i].compared_value
662 int substring_match = 0;
669 rc = regcomp(®ex, ii->rules[i].compared_value, (REG_EXTENDED | REG_ICASE | REG_NOSUB | REG_NEWLINE));
671 syslog(LOG_ERR, "inboxrules: regcomp: error %d trying to compile \"%s\"", rc, ii->rules[i].compared_value);
674 if (compare_compound) { // comparing a compound field such as name+address
675 char *sep = strchr(compare_me, '|');
680 (strcasecmp(compare_me, ii->rules[i].compared_value) ? 0 : 1)
681 + (strcasecmp(sep, ii->rules[i].compared_value) ? 0 : 1)
684 (bmstrcasestr(compare_me, ii->rules[i].compared_value) ? 1 : 0)
685 + (bmstrcasestr(sep, ii->rules[i].compared_value) ? 1 : 0)
687 if (!rc) regex_match =
688 (regexec(®ex, compare_me, 0, 0, 0) ? 0 : 1)
689 + (regexec(®ex, sep, 0, 0, 0) ? 0 : 1)
693 else { // comparing a single field only
694 exact_match = (strcasecmp(compare_me, ii->rules[i].compared_value) ? 0 : 1);
695 substring_match = (bmstrcasestr(compare_me, ii->rules[i].compared_value) ? 1 : 0);
696 if (!rc) regex_match = (regexec(®ex, compare_me, 0, 0, 0) ? 0 : 1);
700 syslog(LOG_DEBUG, "inboxrules: substring match: %d", substring_match);
701 syslog(LOG_DEBUG, "inboxrules: regex match: %d", regex_match);
702 syslog(LOG_DEBUG, "inboxrules: exact match: %d", exact_match);
703 switch(ii->rules[i].field_compare_op) {
705 rule_activated = substring_match;
708 rule_activated = regex_match;
710 case fcomp_notcontains:
711 rule_activated = !substring_match;
713 case fcomp_notmatches:
714 rule_activated = !regex_match;
717 rule_activated = exact_match;
720 rule_activated = !exact_match;
727 switch(ii->rules[i].field_compare_op) {
729 rule_activated = ((smi.meta_rfc822_length > ii->rules[i].compared_size) ? 1 : 0);
732 rule_activated = ((smi.meta_rfc822_length < ii->rules[i].compared_size) ? 1 : 0);
736 case field_all: // The "all messages" rule ALWAYS triggers
739 default: // no matches, fall through and do nothing
740 syslog(LOG_WARNING, "inboxrules: an unknown field comparison was encountered");
745 // If the rule matched, perform the requested action.
746 if (rule_activated) {
747 syslog(LOG_DEBUG, "inboxrules: rule activated");
749 // Perform the requested action
750 switch(ii->rules[i].action) {
758 inbox_do_reject(&ii->rules[i], msg);
761 case action_fileinto:
762 keep_message = inbox_do_fileinto(&ii->rules[i], msgnum);
764 case action_redirect:
765 keep_message = inbox_do_redirect(&ii->rules[i], msgnum);
767 case action_vacation:
768 inbox_do_vacation(&ii->rules[i], msg);
773 // Now perform the "final" action (anything other than "stop" means continue)
774 if (ii->rules[i].final_action == final_stop) {
775 syslog(LOG_DEBUG, "inboxrules: stop processing");
776 i = ii->num_rules + 1; // throw us out of scope to stop
782 syslog(LOG_DEBUG, "inboxrules: rule not activated");
786 if (msg != NULL) { // Delete the copy of the message that is currently in memory. We don't need it anymore.
790 if (!keep_message) { // Delete the copy of the message that is currently in the inbox, if rules dictated that.
791 syslog(LOG_DEBUG, "inboxrules: delete %ld from inbox", msgnum);
792 CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, ""); // we're in the inbox already
795 ii->lastproc = msgnum; // make note of the last message we processed, so we don't scan the whole inbox again
799 // A user account is identified as requring inbox processing. Go ahead and do it.
800 void do_inbox_processing_for_user(long usernum) {
801 struct CtdlMessage *msg;
802 struct inboxrules *ii;
803 char roomname[ROOMNAMELEN];
806 if (CtdlGetUserByNumber(&CC->user, usernum) != 0) { // grab the user record
807 return; // and bail out if we were given an invalid user
810 strcpy(username, CC->user.fullname); // save the user name so we can fetch it later and lock it
812 if (CC->user.msgnum_inboxrules <= 0) {
813 return; // this user has no inbox rules
816 msg = CtdlFetchMessage(CC->user.msgnum_inboxrules, 1);
818 return; // config msgnum is set but that message does not exist
821 ii = deserialize_inbox_rules(msg->cm_fields[eMesageText]);
825 return; // config message exists but body is null
828 if (ii->lastproc > CC->user.lastproc_inboxrules) { // There might be a "last message processed" number left over
829 CC->user.lastproc_inboxrules = ii->lastproc; // in the ruleset from a previous version. Use this if it is
830 } // a higher number.
832 ii->lastproc = CC->user.lastproc_inboxrules;
835 long original_lastproc = ii->lastproc;
836 syslog(LOG_DEBUG, "inboxrules: for %s, messages newer than %ld", CC->user.fullname, original_lastproc);
838 // Go to the user's inbox room and process all new messages
839 snprintf(roomname, sizeof roomname, "%010ld.%s", usernum, MAILROOM);
840 if (CtdlGetRoom(&CC->room, roomname) == 0) {
841 CtdlForEachMessage(MSGS_GT, ii->lastproc, NULL, NULL, NULL, inbox_do_msg, (void *) ii);
844 // Record the number of the last message we processed
845 if (ii->lastproc > original_lastproc) {
846 CtdlGetUserLock(&CC->user, username);
847 CC->user.lastproc_inboxrules = ii->lastproc; // Avoid processing the entire inbox next time
848 CtdlPutUserLock(&CC->user);
851 // And free the memory.
852 free_inbox_rules(ii);
856 // Here is an array of users (by number) who have received messages in their inbox and may require processing.
857 long *users_requiring_inbox_processing = NULL;
859 int num_urip_alloc = 0;
862 // Perform inbox processing for all rooms which require it
863 void perform_inbox_processing(void) {
867 return; // no action required
870 for (i=0; i<num_urip; ++i) {
871 do_inbox_processing_for_user(users_requiring_inbox_processing[i]);
874 free(users_requiring_inbox_processing);
875 users_requiring_inbox_processing = NULL;
881 // 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
882 int serv_inboxrules_roomhook(struct ctdlroom *room) {
885 // Is this someone's inbox?
886 if (!strcasecmp(&room->QRname[11], MAILROOM)) {
887 long usernum = atol(room->QRname);
890 // first check to see if this user is already on the list
892 for (i=0; i<=num_urip; ++i) {
893 if (users_requiring_inbox_processing[i] == usernum) { // already on the list!
899 // make room if we need to
900 if (num_urip_alloc == 0) {
901 num_urip_alloc = 100;
902 users_requiring_inbox_processing = malloc(sizeof(long) * num_urip_alloc);
904 else if (num_urip >= num_urip_alloc) {
905 num_urip_alloc += 100;
906 users_requiring_inbox_processing = realloc(users_requiring_inbox_processing, (sizeof(long) * num_urip_alloc));
909 // now add the user to the list
910 users_requiring_inbox_processing[num_urip++] = usernum;
914 // No errors are possible from this function.
922 // This is a client-facing function which fetches the user's inbox rules -- it omits all lines containing anything other than a rule.
924 // hmmmmm ... should we try to rebuild this in terms of deserialize_inbox_rules() instread?
925 void cmd_gibr(char *argbuf) {
927 if (CtdlAccessCheck(ac_logged_in)) return;
929 cprintf("%d inbox rules for %s\n", LISTING_FOLLOWS, CC->user.fullname);
931 struct CtdlMessage *msg = CtdlFetchMessage(CC->user.msgnum_inboxrules, 1);
933 if (!CM_IsEmpty(msg, eMesageText)) {
935 char *rest = msg->cm_fields[eMesageText];
936 while ((token = strtok_r(rest, "\n", &rest))) {
938 // for backwards compatibility, "# WEBCIT_RULE" is an alias for "rule"
939 if (!strncasecmp(token, "# WEBCIT_RULE|", 14)) {
940 strcpy(token, "rule|");
941 strcpy(&token[5], &token[14]);
944 // Output only lines containing rules.
945 if (!strncasecmp(token, "rule|", 5)) {
946 cprintf("%s\n", token);
949 cprintf("# invalid rule found : %s\n", token);
959 // Rewrite the rule set to disk after it has been modified
960 // Called by cmd_pibr()
961 // Returns the msgnum of the new rules message
962 void rewrite_rules_to_disk(const char *new_config) {
963 long old_msgnum = CC->user.msgnum_inboxrules;
964 char userconfigroomname[ROOMNAMELEN];
965 CtdlMailboxName(userconfigroomname, sizeof userconfigroomname, &CC->user, USERCONFIGROOM);
966 long new_msgnum = quickie_message("Citadel", NULL, NULL, userconfigroomname, new_config, FMT_RFC822, "inbox rules configuration");
967 CtdlGetUserLock(&CC->user, CC->curr_user);
968 CC->user.msgnum_inboxrules = new_msgnum; // Now we know where to get the rules next time
969 CC->user.lastproc_inboxrules = new_msgnum; // Avoid processing the entire inbox next time
970 CtdlPutUserLock(&CC->user);
971 if (old_msgnum > 0) {
972 syslog(LOG_DEBUG, "Deleting old message %ld from %s", old_msgnum, userconfigroomname);
973 CtdlDeleteMessages(userconfigroomname, &old_msgnum, 1, "");
979 // User transmits the new inbox rules for the account. They are inserted into the account, replacing the ones already there.
980 void cmd_pibr(char *argbuf) {
981 if (CtdlAccessCheck(ac_logged_in)) return;
984 cprintf("%d send new rules\n", SEND_LISTING);
985 char *newrules = CtdlReadMessageBody(HKEY("000"), CtdlGetConfigLong("c_maxmsglen"), NULL, 0);
986 StrBuf *NewConfig = NewStrBufPlain("Content-type: application/x-citadel-sieve-config; charset=UTF-8\nContent-transfer-encoding: 8bit\n\n", -1);
989 char *rest = newrules;
990 while ((token = strtok_r(rest, "\n", &rest))) {
991 // Accept only lines containing rules
992 if (!strncasecmp(token, "rule|", 5)) {
993 StrBufAppendBufPlain(NewConfig, token, -1, 0);
994 StrBufAppendBufPlain(NewConfig, HKEY("\n"), 0);
998 rewrite_rules_to_disk(ChrPtr(NewConfig));
999 FreeStrBuf(&NewConfig);
1003 // Initialization function, called from modules_init.c
1004 char *ctdl_module_init_sieve(void) {
1006 CtdlRegisterProtoHook(cmd_gibr, "GIBR", "Get InBox Rules");
1007 CtdlRegisterProtoHook(cmd_pibr, "PIBR", "Put InBox Rules");
1008 CtdlRegisterRoomHook(serv_inboxrules_roomhook);
1009 CtdlRegisterSessionHook(perform_inbox_processing, EVT_HOUSE, PRIO_HOUSE + 10);
1012 // return our module name for the log
1013 return "inboxrules";