remove typedef from struct recptypes
[citadel.git] / citadel / modules / inboxrules / serv_inboxrules.c
index 17ea9713a76f8751e936a0f62b08855abf036ef3..b7e01278f4d63cdb3975809c9bfd612ab9b1b4cb 100644 (file)
 #include <pwd.h>
 #include <errno.h>
 #include <sys/types.h>
-
-#if TIME_WITH_SYS_TIME
-# include <sys/time.h>
-# include <time.h>
-#else
-# if HAVE_SYS_TIME_H
-#  include <sys/time.h>
-# else
-#  include <time.h>
-# endif
-#endif
-
+#include <time.h>
 #include <sys/wait.h>
 #include <string.h>
 #include <limits.h>
@@ -218,7 +207,8 @@ struct inboxrules *deserialize_inbox_rules(char *serialized_rules) {
                        // We have a rule , now parse it
                        char rtoken[SIZ];
                        int nt = num_tokens(decoded_rule, '|');
-                       for (int t=0; t<nt; ++t) {
+                       int t = 0;
+                       for (t=0; t<nt; ++t) {
                                extract_token(rtoken, decoded_rule, t, '|', sizeof(rtoken));
                                striplt(rtoken);
                                switch(t) {
@@ -355,7 +345,7 @@ int inbox_do_redirect(struct irule *rule, long msgnum) {
                return(1);                                      // don't delete the inbox copy if this failed
        }
 
-       recptypes *valid = validate_recipients(rule->redirect_to, NULL, 0);
+       struct 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
@@ -373,7 +363,7 @@ int inbox_do_redirect(struct irule *rule, long msgnum) {
                return(1);                                      // don't delete the inbox copy if this failed
        }
 
-       CtdlSubmitMsg(msg, valid, NULL, 0);                     // send the message to the new recipient
+       CtdlSubmitMsg(msg, valid, NULL);                        // send the message to the new recipient
        free_recipients(valid);
        CM_Free(msg);
        return(0);                                              // delete the inbox copy
@@ -384,10 +374,7 @@ int inbox_do_redirect(struct irule *rule, long msgnum) {
  * 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 message: <%s>",
-               msg->cm_fields[erFc822Addr],
-               rule->autoreply_message
-       );
+       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;
@@ -420,10 +407,10 @@ void inbox_do_reject(struct irule *rule, struct CtdlMessage *msg) {
 
        // Deliver the message
        quickie_message(
-               NULL,
+               " ",
                msg->cm_fields[eenVelopeTo],
                sender,
-               NULL,
+               MAILROOM,
                reject_text,
                FMT_RFC822,
                "Delivery status notification"
@@ -436,12 +423,9 @@ void inbox_do_reject(struct irule *rule, struct CtdlMessage *msg) {
  * 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 message: <%s>",
-               msg->cm_fields[erFc822Addr],
-               rule->autoreply_message
-       );
+       syslog(LOG_DEBUG, "inbox_do_vacation: sender: <%s>, vacation", msg->cm_fields[erFc822Addr]);
 
-       // If we can't determine who sent the message, reject silently.
+       // 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];
@@ -453,38 +437,45 @@ void inbox_do_vacation(struct irule *rule, struct CtdlMessage *msg) {
                return;
        }
 
-
-
-       // FIXME use the S_USETABLE to avoid sending the same correspondent a vacation message repeatedly.
-
-
-
-
-       // Assemble the reject message.
-       char *reject_text = malloc(strlen(rule->autoreply_message) + 1024);
-       if (reject_text == NULL) {
-               return;
-       }
-       sprintf(reject_text, 
-               "Content-type: text/plain\n"
-               "\n"
-               "%s\n"
-               "\n"
-       ,
-               rule->autoreply_message
+       // 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;
+               }
 
-       // Deliver the message
-       quickie_message(
-               NULL,
-               msg->cm_fields[eenVelopeTo],
-               sender,
-               NULL,
-               reject_text,
-               FMT_RFC822,
-               "Delivery status notification"
-       );
-       free(reject_text);
+               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);
+       }
 }
 
 
@@ -500,6 +491,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;
 
@@ -570,12 +562,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])) {
@@ -643,39 +645,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;
@@ -683,7 +695,7 @@ 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;
                }
@@ -774,10 +786,6 @@ void do_inbox_processing_for_user(long usernum) {
                return;                                         // config message exists but body is null
        }
 
-
-       syslog(LOG_DEBUG, "ii->lastproc                 %ld", ii->lastproc);
-       syslog(LOG_DEBUG, "CC->user.lastproc_inboxrules %ld", CC->user.lastproc_inboxrules);
-
        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.
@@ -818,11 +826,13 @@ int num_urip_alloc = 0;
  * Perform inbox processing for all rooms which require it
  */
 void perform_inbox_processing(void) {
+       int i = 0;
+
        if (num_urip == 0) {
                return;                                                                                 // no action required
        }
 
-       for (int i=0; i<num_urip; ++i) {
+       for (i=0; i<num_urip; ++i) {
                do_inbox_processing_for_user(users_requiring_inbox_processing[i]);
        }
 
@@ -838,6 +848,7 @@ void perform_inbox_processing(void) {
  * If it's someone's inbox, we have to check for inbox rules
  */
 int serv_inboxrules_roomhook(struct ctdlroom *room) {
+       int i = 0;
 
        // Is this someone's inbox?
        if (!strcasecmp(&room->QRname[11], MAILROOM)) {
@@ -846,7 +857,7 @@ int serv_inboxrules_roomhook(struct ctdlroom *room) {
 
                        // first check to see if this user is already on the list
                        if (num_urip > 0) {
-                               for (int i=0; i<=num_urip; ++i) {
+                               for (i=0; i<=num_urip; ++i) {
                                        if (users_requiring_inbox_processing[i] == usernum) {           // already on the list!
                                                return(0);
                                        }