X-Git-Url: https://code.citadel.org/?p=citadel.git;a=blobdiff_plain;f=citadel%2Fmodules%2Finboxrules%2Fserv_inboxrules.c;h=4979e57150e74f64900535fef0bd7de83e26c0bf;hp=6c176c36ded33b4d4c5028f8014ada1cb3092b5e;hb=0387f48886a9395d89eaca01cd40ab751610426f;hpb=794627693cd53c3be8d84783feaf01b9f1475029 diff --git a/citadel/modules/inboxrules/serv_inboxrules.c b/citadel/modules/inboxrules/serv_inboxrules.c index 6c176c36d..4979e5715 100644 --- a/citadel/modules/inboxrules/serv_inboxrules.c +++ b/citadel/modules/inboxrules/serv_inboxrules.c @@ -48,395 +48,6 @@ #include "ctdl_module.h" -#if 0 - -/* - * Callback function to redirect a message to a different folder - */ -int ctdl_redirect(void) -{ - struct ctdl_sieve *cs = (struct ctdl_sieve *)my; - struct CtdlMessage *msg = NULL; - recptypes *valid = NULL; - char recp[256]; - - safestrncpy(recp, sieve2_getvalue_string(s, "address"), sizeof recp); - - syslog(LOG_DEBUG, "Action is REDIRECT, recipient <%s>", recp); - - valid = validate_recipients(recp, NULL, 0); - if (valid == NULL) { - syslog(LOG_WARNING, "REDIRECT failed: bad recipient <%s>", recp); - return SIEVE2_ERROR_BADARGS; - } - if (valid->num_error > 0) { - syslog(LOG_WARNING, "REDIRECT failed: bad recipient <%s>", recp); - free_recipients(valid); - return SIEVE2_ERROR_BADARGS; - } - - msg = CtdlFetchMessage(cs->msgnum, 1); - if (msg == NULL) { - syslog(LOG_WARNING, "REDIRECT failed: unable to fetch msg %ld", cs->msgnum); - free_recipients(valid); - return SIEVE2_ERROR_BADARGS; - } - - CtdlSubmitMsg(msg, valid, NULL, 0); - cs->cancel_implicit_keep = 1; - free_recipients(valid); - CM_Free(msg); - return SIEVE2_OK; -} - - - - -/* - * Callback function to file a message into a different mailbox - */ -int ctdl_fileinto(sieve2_context_t *s, void *my) -{ - struct ctdl_sieve *cs = (struct ctdl_sieve *)my; - const char *dest_folder = sieve2_getvalue_string(s, "mailbox"); - int c; - char foldername[256]; - char original_room_name[ROOMNAMELEN]; - - syslog(LOG_DEBUG, "Action is FILEINTO, destination is <%s>", dest_folder); - - /* FILEINTO 'INBOX' is the same thing as KEEP */ - if ( (!strcasecmp(dest_folder, "INBOX")) || (!strcasecmp(dest_folder, MAILROOM)) ) { - cs->keep = 1; - cs->cancel_implicit_keep = 1; - return SIEVE2_OK; - } - - /* Remember what room we came from */ - safestrncpy(original_room_name, CC->room.QRname, sizeof original_room_name); - - /* First try a mailbox name match (check personal mail folders first) */ - snprintf(foldername, sizeof foldername, "%010ld.%s", cs->usernum, dest_folder); - c = CtdlGetRoom(&CC->room, foldername); - - /* Then a regular room name match (public and private rooms) */ - if (c != 0) { - safestrncpy(foldername, dest_folder, sizeof foldername); - c = CtdlGetRoom(&CC->room, foldername); - } - - if (c != 0) { - syslog(LOG_WARNING, "FILEINTO failed: target <%s> does not exist", dest_folder); - return SIEVE2_ERROR_BADARGS; - } - - /* Yes, we actually have to go there */ - CtdlUserGoto(NULL, 0, 0, NULL, NULL, NULL, NULL); - - c = CtdlSaveMsgPointersInRoom(NULL, &cs->msgnum, 1, 0, NULL, 0); - - /* Go back to the room we came from */ - if (strcasecmp(original_room_name, CC->room.QRname)) { - CtdlUserGoto(original_room_name, 0, 0, NULL, NULL, NULL, NULL); - } - - if (c == 0) { - cs->cancel_implicit_keep = 1; - return SIEVE2_OK; - } - else { - return SIEVE2_ERROR_BADARGS; - } -} - - -/* - * Callback function to indicate that a message should be discarded. - */ -int ctdl_discard(sieve2_context_t *s, void *my) -{ - struct ctdl_sieve *cs = (struct ctdl_sieve *)my; - - syslog(LOG_DEBUG, "Action is DISCARD"); - - /* Cancel the implicit keep. That's all there is to it. */ - cs->cancel_implicit_keep = 1; - return SIEVE2_OK; -} - - - - - - - - - - -/* - * Callback function to indicate that a vacation message should be generated - */ -int ctdl_vacation(sieve2_context_t *s, void *my) -{ - struct ctdl_sieve *cs = (struct ctdl_sieve *)my; - struct sdm_vacation *vptr; - int days = 1; - const char *message; - char *vacamsg_text = NULL; - char vacamsg_subject[1024]; - - syslog(LOG_DEBUG, "Action is VACATION"); - - message = sieve2_getvalue_string(s, "message"); - if (message == NULL) return SIEVE2_ERROR_BADARGS; - - if (sieve2_getvalue_string(s, "subject") != NULL) { - safestrncpy(vacamsg_subject, sieve2_getvalue_string(s, "subject"), sizeof vacamsg_subject); - } - else { - snprintf(vacamsg_subject, sizeof vacamsg_subject, "Re: %s", cs->subject); - } - - days = sieve2_getvalue_int(s, "days"); - if (days < 1) days = 1; - if (days > MAX_VACATION) days = MAX_VACATION; - - /* Check to see whether we've already alerted this sender that we're on vacation. */ - for (vptr = cs->u->first_vacation; vptr != NULL; vptr = vptr->next) { - if (!strcasecmp(vptr->fromaddr, cs->sender)) { - if ( (time(NULL) - vptr->timestamp) < (days * 86400) ) { - syslog(LOG_DEBUG, "Already alerted <%s> recently.", cs->sender); - return SIEVE2_OK; - } - } - } - - /* Assemble the reject message. */ - vacamsg_text = malloc(strlen(message) + 1024); - if (vacamsg_text == NULL) { - return SIEVE2_ERROR_FAIL; - } - - sprintf(vacamsg_text, - "Content-type: text/plain charset=utf-8\n" - "\n" - "%s\n" - "\n" - , - message - ); - - quickie_message( /* This delivers the message */ - NULL, - cs->envelope_to, - cs->sender, - NULL, - vacamsg_text, - FMT_RFC822, - vacamsg_subject - ); - - free(vacamsg_text); - - /* Now update the list to reflect the fact that we've alerted this sender. - * If they're already in the list, just update the timestamp. - */ - for (vptr = cs->u->first_vacation; vptr != NULL; vptr = vptr->next) { - if (!strcasecmp(vptr->fromaddr, cs->sender)) { - vptr->timestamp = time(NULL); - return SIEVE2_OK; - } - } - - /* If we get to this point, create a new record. - */ - vptr = malloc(sizeof(struct sdm_vacation)); - memset(vptr, 0, sizeof(struct sdm_vacation)); - vptr->timestamp = time(NULL); - safestrncpy(vptr->fromaddr, cs->sender, sizeof vptr->fromaddr); - vptr->next = cs->u->first_vacation; - cs->u->first_vacation = vptr; - - return SIEVE2_OK; -} - - -/* - * Callback function to parse message envelope - */ -int ctdl_getenvelope(sieve2_context_t *s, void *my) -{ - struct ctdl_sieve *cs = (struct ctdl_sieve *)my; - - syslog(LOG_DEBUG, "Action is GETENVELOPE"); - syslog(LOG_DEBUG, "EnvFrom: %s", cs->envelope_from); - syslog(LOG_DEBUG, "EnvTo: %s", cs->envelope_to); - - if (cs->envelope_from != NULL) { - if ((cs->envelope_from[0] != '@')&&(cs->envelope_from[strlen(cs->envelope_from)-1] != '@')) { - sieve2_setvalue_string(s, "from", cs->envelope_from); - } - else { - sieve2_setvalue_string(s, "from", "invalid_envelope_from@example.org"); - } - } - else { - sieve2_setvalue_string(s, "from", "null_envelope_from@example.org"); - } - - - if (cs->envelope_to != NULL) { - if ((cs->envelope_to[0] != '@') && (cs->envelope_to[strlen(cs->envelope_to)-1] != '@')) { - sieve2_setvalue_string(s, "to", cs->envelope_to); - } - else { - sieve2_setvalue_string(s, "to", "invalid_envelope_to@example.org"); - } - } - else { - sieve2_setvalue_string(s, "to", "null_envelope_to@example.org"); - } - - return SIEVE2_OK; -} - - - - -/* - * Given the on-disk representation of our Sieve config, load - * it into an in-memory data structure. - */ -void parse_sieve_config(char *conf, struct sdm_userdata *u) { - char *ptr; - char *c, *vacrec; - char keyword[256]; - struct sdm_script *sptr; - struct sdm_vacation *vptr; - - ptr = conf; - while (c = ptr, ptr = bmstrcasestr(ptr, CTDLSIEVECONFIGSEPARATOR), ptr != NULL) { - *ptr = 0; - ptr += strlen(CTDLSIEVECONFIGSEPARATOR); - - extract_token(keyword, c, 0, '|', sizeof keyword); - - if (!strcasecmp(keyword, "lastproc")) { - u->lastproc = extract_long(c, 1); - } - - else if (!strcasecmp(keyword, "script")) { - sptr = malloc(sizeof(struct sdm_script)); - extract_token(sptr->script_name, c, 1, '|', sizeof sptr->script_name); - sptr->script_active = extract_int(c, 2); - remove_token(c, 0, '|'); - remove_token(c, 0, '|'); - remove_token(c, 0, '|'); - sptr->script_content = strdup(c); - sptr->next = u->first_script; - u->first_script = sptr; - } - - else if (!strcasecmp(keyword, "vacation")) { - - if (c != NULL) while (vacrec=c, c=strchr(c, '\n'), (c != NULL)) { - - *c = 0; - ++c; - - if (strncasecmp(vacrec, "vacation|", 9)) { - vptr = malloc(sizeof(struct sdm_vacation)); - extract_token(vptr->fromaddr, vacrec, 0, '|', sizeof vptr->fromaddr); - vptr->timestamp = extract_long(vacrec, 1); - vptr->next = u->first_vacation; - u->first_vacation = vptr; - } - } - } - - /* ignore unknown keywords */ - } -} - - - - - -/* - * Write our citadel sieve config back to disk - * - * (Set yes_write_to_disk to nonzero to make it actually write the config; - * otherwise it just frees the data structures.) - */ -void rewrite_ctdl_sieve_config(struct sdm_userdata *u, int yes_write_to_disk) { - StrBuf *text; - struct sdm_script *sptr; - struct sdm_vacation *vptr; - - text = NewStrBufPlain(NULL, SIZ); - StrBufPrintf(text, - "Content-type: application/x-citadel-sieve-config\n" - "\n" - CTDLSIEVECONFIGSEPARATOR - "lastproc|%ld" - CTDLSIEVECONFIGSEPARATOR - , - u->lastproc - ); - - while (u->first_script != NULL) { - StrBufAppendPrintf(text, - "script|%s|%d|%s" CTDLSIEVECONFIGSEPARATOR, - u->first_script->script_name, - u->first_script->script_active, - u->first_script->script_content - ); - sptr = u->first_script; - u->first_script = u->first_script->next; - free(sptr->script_content); - free(sptr); - } - - if (u->first_vacation != NULL) { - - StrBufAppendPrintf(text, "vacation|\n"); - while (u->first_vacation != NULL) { - if ( (time(NULL) - u->first_vacation->timestamp) < (MAX_VACATION * 86400)) { - StrBufAppendPrintf(text, "%s|%ld\n", - u->first_vacation->fromaddr, - u->first_vacation->timestamp - ); - } - vptr = u->first_vacation; - u->first_vacation = u->first_vacation->next; - free(vptr); - } - StrBufAppendPrintf(text, CTDLSIEVECONFIGSEPARATOR); - } - - if (yes_write_to_disk) - { - /* Save the config */ - quickie_message("Citadel", NULL, NULL, u->config_roomname, - ChrPtr(text), - 4, - "Sieve configuration" - ); - - /* And delete the old one */ - if (u->config_msgnum > 0) { - CtdlDeleteMessages(u->config_roomname, &u->config_msgnum, 1, ""); - } - } - - FreeStrBuf (&text); - -} - - -#endif - - /* * The next sections are enums and keys that drive the serialize/deserialize functions for the inbox rules/state configuration. */ @@ -611,50 +222,50 @@ struct inboxrules *deserialize_inbox_rules(char *serialized_rules) { extract_token(rtoken, decoded_rule, t, '|', sizeof(rtoken)); striplt(rtoken); switch(t) { - case 1: // field to compare + case 1: // field to compare for (i=0; i<=field_all; ++i) { if (!strcasecmp(rtoken, field_keys[i])) { new_rule->compared_field = i; } } break; - case 2: // field comparison operation + case 2: // field comparison operation for (i=0; i<=fcomp_notmatches; ++i) { if (!strcasecmp(rtoken, fcomp_keys[i])) { new_rule->field_compare_op = i; } } break; - case 3: // field comparison value + case 3: // field comparison value safestrncpy(new_rule->compared_value, rtoken, sizeof(new_rule->compared_value)); break; - case 4: // size comparison operation + case 4: // size comparison operation for (i=0; i<=scomp_smaller; ++i) { if (!strcasecmp(rtoken, scomp_keys[i])) { new_rule->size_compare_op = i; } } break; - case 5: // size comparison value + case 5: // size comparison value new_rule->compared_size = atol(rtoken); break; - case 6: // action + case 6: // action for (i=0; i<=action_vacation; ++i) { if (!strcasecmp(rtoken, action_keys[i])) { new_rule->action = i; } } break; - case 7: // file into (target room) + case 7: // file into (target room) safestrncpy(new_rule->file_into, rtoken, sizeof(new_rule->file_into)); break; - case 8: // redirect to (target address) + case 8: // redirect to (target address) safestrncpy(new_rule->redirect_to, rtoken, sizeof(new_rule->redirect_to)); break; - case 9: // autoreply message + case 9: // autoreply message safestrncpy(new_rule->autoreply_message, rtoken, sizeof(new_rule->autoreply_message)); break; - case 10: // final_action; + case 10: // final_action; for (i=0; i<=final_stop; ++i) { if (!strcasecmp(rtoken, final_keys[i])) { new_rule->final_action = i; @@ -666,29 +277,17 @@ struct inboxrules *deserialize_inbox_rules(char *serialized_rules) { } } free(decoded_rule); - - // if we re-serialized this now, what would it look like? - syslog(LOG_DEBUG, "test reserialize: 0|%s|%s|%s|%s|%ld|%s|%s|%s|%s|%s", - field_keys[new_rule->compared_field], - fcomp_keys[new_rule->field_compare_op], - new_rule->compared_value, - scomp_keys[new_rule->size_compare_op], - new_rule->compared_size, - action_keys[new_rule->action], - new_rule->file_into, - new_rule->redirect_to, - new_rule->autoreply_message, - final_keys[new_rule->final_action] - ); - // delete the above after moving it to a reserialize function - } // "lastproc" indicates the newest message number in the inbox that was previously processed by our inbox rules. + // This is a legacy location for this value and will only be used if it's the only one present. else if (!strncasecmp(token, "lastproc|", 5)) { ibr->lastproc = atol(&token[9]); } + // Lines which do not contain a recognizable token must be IGNORED. These lines may be left over + // from a previous version and will disappear when we rewrite the config. + } free(sr); // free our copy of the source buffer that has now been trashed with null bytes... @@ -696,13 +295,96 @@ struct inboxrules *deserialize_inbox_rules(char *serialized_rules) { } -// Perform the "reject" action +// Perform the "fileinto" action (save the message in another room) +// Returns: 1 or 0 to tell the caller to keep (1) or delete (0) the inbox copy of the message. // -void inbox_do_reject(struct irule *rule, struct CtdlMessage *msg, struct MetaData *smi) { - syslog(LOG_DEBUG, "inbox_do_reject: sender: <%s>, reject message: <%s>", - msg->cm_fields[erFc822Addr], - rule->autoreply_message - ); +int inbox_do_fileinto(struct irule *rule, long msgnum) { + char *dest_folder = rule->file_into; + char original_room_name[ROOMNAMELEN]; + char foldername[ROOMNAMELEN]; + int c; + + // Situations where we want to just keep the message in the inbox: + if ( + (IsEmptyStr(dest_folder)) // no destination room was specified + || (!strcasecmp(dest_folder, "INBOX")) // fileinto inbox is the same as keep + || (!strcasecmp(dest_folder, MAILROOM)) // fileinto "Mail" is the same as keep + ) { + return(1); // don't delete the inbox copy if this failed + } + + // Remember what room we came from + safestrncpy(original_room_name, CC->room.QRname, sizeof original_room_name); + + // First try a mailbox name match (check personal mail folders first) + strcpy(foldername, original_room_name); // This keeps the user namespace of the inbox + snprintf(&foldername[10], sizeof(foldername)-10, ".%s", dest_folder); // And this tacks on the target room name + c = CtdlGetRoom(&CC->room, foldername); + + // Then a regular room name match (public and private rooms) + if (c != 0) { + safestrncpy(foldername, dest_folder, sizeof foldername); + c = CtdlGetRoom(&CC->room, foldername); + } + + if (c != 0) { + syslog(LOG_WARNING, "inboxrules: target <%s> does not exist", dest_folder); + return(1); // don't delete the inbox copy if this failed + } + + // Yes, we actually have to go there + CtdlUserGoto(NULL, 0, 0, NULL, NULL, NULL, NULL); + + c = CtdlSaveMsgPointersInRoom(NULL, &msgnum, 1, 0, NULL, 0); + + // Go back to the room we came from + if (strcasecmp(original_room_name, CC->room.QRname)) { + CtdlUserGoto(original_room_name, 0, 0, NULL, NULL, NULL, NULL); + } + + return(0); // delete the inbox copy +} + + +// Perform the "redirect" action (divert the message to another email address) +// Returns: 1 or 0 to tell the caller to keep (1) or delete (0) the inbox copy of the message. +// +int inbox_do_redirect(struct irule *rule, long msgnum) { + if (IsEmptyStr(rule->redirect_to)) { + syslog(LOG_WARNING, "inboxrules: inbox_do_redirect() invalid recipient <%s>", rule->redirect_to); + return(1); // don't delete the inbox copy if this failed + } + + recptypes *valid = validate_recipients(rule->redirect_to, NULL, 0); + if (valid == NULL) { + syslog(LOG_WARNING, "inboxrules: inbox_do_redirect() invalid recipient <%s>", rule->redirect_to); + return(1); // don't delete the inbox copy if this failed + } + if (valid->num_error > 0) { + free_recipients(valid); + syslog(LOG_WARNING, "inboxrules: inbox_do_redirect() invalid recipient <%s>", rule->redirect_to); + return(1); // don't delete the inbox copy if this failed + } + + struct CtdlMessage *msg = CtdlFetchMessage(msgnum, 1); + if (msg == NULL) { + free_recipients(valid); + syslog(LOG_WARNING, "inboxrules: cannot reload message %ld for forwarding", msgnum); + return(1); // don't delete the inbox copy if this failed + } + + CtdlSubmitMsg(msg, valid, NULL); // send the message to the new recipient + free_recipients(valid); + CM_Free(msg); + return(0); // delete the inbox copy +} + + +/* + * Perform the "reject" action (delete the message, and tell the sender we deleted it) + */ +void inbox_do_reject(struct irule *rule, struct CtdlMessage *msg) { + syslog(LOG_DEBUG, "inbox_do_reject: sender: <%s>, reject", msg->cm_fields[erFc822Addr]); // If we can't determine who sent the message, reject silently. char *sender; @@ -735,10 +417,10 @@ void inbox_do_reject(struct irule *rule, struct CtdlMessage *msg, struct MetaDat // Deliver the message quickie_message( - NULL, + " ", msg->cm_fields[eenVelopeTo], sender, - NULL, + MAILROOM, reject_text, FMT_RFC822, "Delivery status notification" @@ -747,6 +429,66 @@ void inbox_do_reject(struct irule *rule, struct CtdlMessage *msg, struct MetaDat } +/* + * Perform the "vacation" action (send an automatic response) + */ +void inbox_do_vacation(struct irule *rule, struct CtdlMessage *msg) { + syslog(LOG_DEBUG, "inbox_do_vacation: sender: <%s>, vacation", msg->cm_fields[erFc822Addr]); + + // If we can't determine who sent the message, no auto-reply can be sent. + char *sender; + if (!IsEmptyStr(msg->cm_fields[eMessagePath])) { + sender = msg->cm_fields[eMessagePath]; + } + else if (!IsEmptyStr(msg->cm_fields[erFc822Addr])) { + sender = msg->cm_fields[erFc822Addr]; + } + else { + return; + } + + // Avoid repeatedly sending auto-replies to the same correspondent over and over again by creating + // a hash of the user, correspondent, and reply text, and hitting the S_USETABLE database. + StrBuf *u = NewStrBuf(); + StrBufPrintf(u, "vacation/%x/%x/%x", + HashLittle(sender, strlen(sender)), + HashLittle(msg->cm_fields[eenVelopeTo], msg->cm_lengths[eenVelopeTo]), + HashLittle(rule->autoreply_message, strlen(rule->autoreply_message)) + ); + int already_seen = CheckIfAlreadySeen(u); + FreeStrBuf(&u); + + if (!already_seen) { + // Assemble the auto-reply message. + StrBuf *reject_text = NewStrBuf(); + if (reject_text == NULL) { + return; + } + + StrBufPrintf(reject_text, + "Content-type: text/plain\n" + "\n" + "%s\n" + "\n" + , + rule->autoreply_message + ); + + // Deliver the auto-reply. + quickie_message( + "", + msg->cm_fields[eenVelopeTo], + sender, + MAILROOM, + ChrPtr(reject_text), + FMT_RFC822, + "Delivery status notification" + ); + FreeStrBuf(&reject_text); + } +} + + /* * Process a single message. We know the room, the user, the rules, the message number, etc. */ @@ -759,6 +501,7 @@ void inbox_do_msg(long msgnum, void *userdata) { struct MetaData smi; // If we are loading the metadata to compare, put it here. int rule_activated = 0; // On each rule, this is set if the compare succeeds and the rule activates. char compare_me[SIZ]; // On each rule, we will store the field to be compared here. + int compare_compound = 0; // Set to 1 when we are comparing both display name and email address int keep_message = 1; // Nonzero to keep the message in the inbox after processing, 0 to delete it. int i; @@ -829,12 +572,22 @@ void inbox_do_msg(long msgnum, void *userdata) { // If the rule involves a field comparison, load the field to be compared. compare_me[0] = 0; + compare_compound = 0; switch(ii->rules[i].compared_field) { - case field_from: // From: - if (!IsEmptyStr(msg->cm_fields[erFc822Addr])) { + if ( (!IsEmptyStr(msg->cm_fields[erFc822Addr])) && (!IsEmptyStr(msg->cm_fields[erFc822Addr])) ) { + snprintf(compare_me, sizeof compare_me, "%s|%s", + msg->cm_fields[eAuthor], + msg->cm_fields[erFc822Addr] + ); + compare_compound = 1; // there will be two fields to compare "name|address" + } + else if (!IsEmptyStr(msg->cm_fields[erFc822Addr])) { safestrncpy(compare_me, msg->cm_fields[erFc822Addr], sizeof compare_me); } + else if (!IsEmptyStr(msg->cm_fields[eAuthor])) { + safestrncpy(compare_me, msg->cm_fields[eAuthor], sizeof compare_me); + } break; case field_tocc: // To: or Cc: if (!IsEmptyStr(msg->cm_fields[eRecipient])) { @@ -902,39 +655,49 @@ void inbox_do_msg(long msgnum, void *userdata) { // For all of the above fields, we can compare the field we've loaded into the buffer. syslog(LOG_DEBUG, "Value of field to compare is: <%s>", compare_me); + int substring_match = (bmstrcasestr(compare_me, ii->rules[i].compared_value) ? 1 : 0); + int exact_match = 0; + if (compare_compound) { + char *sep = strchr(compare_me, '|'); + if (sep) { + *sep = 0; + exact_match = + (strcasecmp(compare_me, ii->rules[i].compared_value) ? 0 : 1) + + (strcasecmp(++sep, ii->rules[i].compared_value) ? 0 : 1) + ; + } + } + else { + exact_match = (strcasecmp(compare_me, ii->rules[i].compared_value) ? 0 : 1); + } + syslog(LOG_DEBUG, "substring match: %d", substring_match); + syslog(LOG_DEBUG, "exact match: %d", exact_match); switch(ii->rules[i].field_compare_op) { case fcomp_contains: case fcomp_matches: - rule_activated = (bmstrcasestr(compare_me, ii->rules[i].compared_value) ? 1 : 0); - syslog(LOG_DEBUG, "Does %s contain %s? %s", compare_me, ii->rules[i].compared_value, rule_activated?"yes":"no"); + rule_activated = substring_match; break; case fcomp_notcontains: case fcomp_notmatches: - rule_activated = (bmstrcasestr(compare_me, ii->rules[i].compared_value) ? 0 : 1); - syslog(LOG_DEBUG, "Does %s contain %s? %s", compare_me, ii->rules[i].compared_value, rule_activated?"yes":"no"); + rule_activated = !substring_match; break; case fcomp_is: - rule_activated = (strcasecmp(compare_me, ii->rules[i].compared_value) ? 0 : 1); - syslog(LOG_DEBUG, "Does %s equal %s? %s", compare_me, ii->rules[i].compared_value, rule_activated?"yes":"no"); + rule_activated = exact_match; break; case fcomp_isnot: - rule_activated = (strcasecmp(compare_me, ii->rules[i].compared_value) ? 1 : 0); - syslog(LOG_DEBUG, "Does %s equal %s? %s", compare_me, ii->rules[i].compared_value, rule_activated?"yes":"no"); + rule_activated = !exact_match; break; } break; case field_size: rule_activated = 0; - syslog(LOG_DEBUG, "comparing actual message size %ld to rule message size %ld", smi.meta_rfc822_length, ii->rules[i].compared_size); switch(ii->rules[i].field_compare_op) { case scomp_larger: rule_activated = ((smi.meta_rfc822_length > ii->rules[i].compared_size) ? 1 : 0); - 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"); break; case scomp_smaller: rule_activated = ((smi.meta_rfc822_length < ii->rules[i].compared_size) ? 1 : 0); - 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"); break; } break; @@ -942,14 +705,14 @@ void inbox_do_msg(long msgnum, void *userdata) { rule_activated = 1; break; default: // no matches, fall through and do nothing - syslog(LOG_DEBUG, "inboxrules: an unknown field comparison was encountered"); + syslog(LOG_WARNING, "inboxrules: an unknown field comparison was encountered"); rule_activated = 0; break; } // If the rule matched, perform the requested action. if (rule_activated) { - syslog(LOG_DEBUG, "\033[32m\033[7mrule activated\033[0m"); // FIXME remove color + syslog(LOG_DEBUG, "inboxrules: rule activated"); // Perform the requested action switch(ii->rules[i].action) { @@ -960,33 +723,31 @@ void inbox_do_msg(long msgnum, void *userdata) { keep_message = 0; break; case action_reject: - inbox_do_reject(&ii->rules[i], msg, &smi); + inbox_do_reject(&ii->rules[i], msg); keep_message = 0; break; case action_fileinto: - // FIXME put it in the other room - keep_message = 0; + keep_message = inbox_do_fileinto(&ii->rules[i], msgnum); break; case action_redirect: - // FIXME send it to the recipient - keep_message = 0; + keep_message = inbox_do_redirect(&ii->rules[i], msgnum); break; case action_vacation: - // FIXME send the vacation message + inbox_do_vacation(&ii->rules[i], msg); keep_message = 1; break; } // Now perform the "final" action (anything other than "stop" means continue) if (ii->rules[i].final_action == final_stop) { - syslog(LOG_DEBUG, "\033[33m\033[7mSTOP\033[0m"); // FIXME remove color - i = ii->num_rules + 1; // throw us out of scope to stop + syslog(LOG_DEBUG, "inboxrules: stop processing"); + i = ii->num_rules + 1; // throw us out of scope to stop } } else { - syslog(LOG_DEBUG, "\033[31m\033[7mrule not activated\033[0m"); // FIXME remove color + syslog(LOG_DEBUG, "inboxrules: rule not activated"); } } @@ -995,8 +756,8 @@ void inbox_do_msg(long msgnum, void *userdata) { } if (!keep_message) { // Delete the copy of the message that is currently in the inbox, if rules dictated that. - // FIXME delete the message - syslog(LOG_DEBUG, "\033[36m\033[7mDELETE FROM INBOX\033[0m"); + syslog(LOG_DEBUG, "inboxrules: delete %ld from inbox", msgnum); + CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, ""); // we're in the inbox already } ii->lastproc = msgnum; // make note of the last message we processed, so we don't scan the whole inbox again @@ -1011,35 +772,55 @@ void do_inbox_processing_for_user(long usernum) { struct CtdlMessage *msg; struct inboxrules *ii; char roomname[ROOMNAMELEN]; + char username[64]; - if (CtdlGetUserByNumber(&CC->user, usernum) == 0) { - if (CC->user.msgnum_inboxrules <= 0) { - return; // this user has no inbox rules - } + if (CtdlGetUserByNumber(&CC->user, usernum) != 0) { // grab the user record + return; // and bail out if we were given an invalid user + } - msg = CtdlFetchMessage(CC->user.msgnum_inboxrules, 1); - if (msg == NULL) { - return; // config msgnum is set but that message does not exist - } - - ii = deserialize_inbox_rules(msg->cm_fields[eMesageText]); - CM_Free(msg); - - if (ii == NULL) { - return; // config message exists but body is null - } + strcpy(username, CC->user.fullname); // save the user name so we can fetch it later and lock it + + if (CC->user.msgnum_inboxrules <= 0) { + return; // this user has no inbox rules + } - syslog(LOG_DEBUG, "inboxrules: for %s", CC->user.fullname); + msg = CtdlFetchMessage(CC->user.msgnum_inboxrules, 1); + if (msg == NULL) { + return; // config msgnum is set but that message does not exist + } - // Go to the user's inbox room and process all new messages - snprintf(roomname, sizeof roomname, "%010ld.%s", usernum, MAILROOM); - if (CtdlGetRoom(&CC->room, roomname) == 0) { - CtdlForEachMessage(MSGS_GT, ii->lastproc, NULL, NULL, NULL, inbox_do_msg, (void *) ii); - } + ii = deserialize_inbox_rules(msg->cm_fields[eMesageText]); + CM_Free(msg); + + if (ii == NULL) { + return; // config message exists but body is null + } + + if (ii->lastproc > CC->user.lastproc_inboxrules) { // There might be a "last message processed" number left over + CC->user.lastproc_inboxrules = ii->lastproc; // in the ruleset from a previous version. Use this if it is + } // a higher number. + else { + ii->lastproc = CC->user.lastproc_inboxrules; + } - // FIXME reserialize our inbox rules/state and write changes back to the config room - free_inbox_rules(ii); + long original_lastproc = ii->lastproc; + syslog(LOG_DEBUG, "inboxrules: for %s, messages newer than %ld", CC->user.fullname, original_lastproc); + + // Go to the user's inbox room and process all new messages + snprintf(roomname, sizeof roomname, "%010ld.%s", usernum, MAILROOM); + if (CtdlGetRoom(&CC->room, roomname) == 0) { + CtdlForEachMessage(MSGS_GT, ii->lastproc, NULL, NULL, NULL, inbox_do_msg, (void *) ii); + } + + // Record the number of the last message we processed + if (ii->lastproc > original_lastproc) { + CtdlGetUserLock(&CC->user, username); + CC->user.lastproc_inboxrules = ii->lastproc; // Avoid processing the entire inbox next time + CtdlPutUserLock(&CC->user); } + + // And free the memory. + free_inbox_rules(ii); } @@ -1141,6 +922,9 @@ void cmd_gibr(char *argbuf) { if (!strncasecmp(token, "rule|", 5)) { cprintf("%s\n", token); } + else { + cprintf("# invalid rule found : %s\n", token); + } } } CM_Free(msg); @@ -1149,6 +933,27 @@ void cmd_gibr(char *argbuf) { } +/* + * Rewrite the rule set to disk after it has been modified + * Called by cmd_pibr() + * Returns the msgnum of the new rules message + */ +void rewrite_rules_to_disk(const char *new_config) { + long old_msgnum = CC->user.msgnum_inboxrules; + char userconfigroomname[ROOMNAMELEN]; + CtdlMailboxName(userconfigroomname, sizeof userconfigroomname, &CC->user, USERCONFIGROOM); + long new_msgnum = quickie_message("Citadel", NULL, NULL, userconfigroomname, new_config, FMT_RFC822, "inbox rules configuration"); + CtdlGetUserLock(&CC->user, CC->curr_user); + CC->user.msgnum_inboxrules = new_msgnum; // Now we know where to get the rules next time + CC->user.lastproc_inboxrules = new_msgnum; // Avoid processing the entire inbox next time + CtdlPutUserLock(&CC->user); + if (old_msgnum > 0) { + syslog(LOG_DEBUG, "Deleting old message %ld from %s", old_msgnum, userconfigroomname); + CtdlDeleteMessages(userconfigroomname, &old_msgnum, 1, ""); + } +} + + /* * Put InBox Rules * @@ -1172,37 +977,8 @@ void cmd_pibr(char *argbuf) { } } free(newrules); - - // Fetch the existing config so we can merge in anything that is NOT a rule - // (Does not start with "rule|" but has at least one vertical bar) - struct CtdlMessage *msg = CtdlFetchMessage(CC->user.msgnum_inboxrules, 1); - if (msg != NULL) { - if (!CM_IsEmpty(msg, eMesageText)) { - rest = msg->cm_fields[eMesageText]; - while ((token = strtok_r(rest, "\n", &rest))) { - // for backwards compatibility, "# WEBCIT_RULE" is an alias for "rule" - if ((strncasecmp(token, "# WEBCIT_RULE|", 14)) && (strncasecmp(token, "rule|", 5)) && (haschar(token, '|'))) { - StrBufAppendBufPlain(NewConfig, token, -1, 0); - StrBufAppendBufPlain(NewConfig, HKEY("\n"), 0); - } - } - } - CM_Free(msg); - } - - /* we have composed the new configuration , now save it */ - long old_msgnum = CC->user.msgnum_inboxrules; - char userconfigroomname[ROOMNAMELEN]; - CtdlMailboxName(userconfigroomname, sizeof userconfigroomname, &CC->user, USERCONFIGROOM); - long new_msgnum = quickie_message("Citadel", NULL, NULL, userconfigroomname, ChrPtr(NewConfig), FMT_RFC822, "inbox rules configuration"); + rewrite_rules_to_disk(ChrPtr(NewConfig)); FreeStrBuf(&NewConfig); - CtdlGetUserLock(&CC->user, CC->curr_user); - CC->user.msgnum_inboxrules = new_msgnum; - CtdlPutUserLock(&CC->user); - if (old_msgnum > 0) { - syslog(LOG_DEBUG, "Deleting old message %ld from %s", old_msgnum, userconfigroomname); - CtdlDeleteMessages(userconfigroomname, &old_msgnum, 1, ""); - } }