use an Enum for the cm_fields vector instead of nameless chars
[citadel.git] / citadel / modules / smtp / serv_smtp.c
index 49169b6f365501a27ed056321392078e67174e35..ad9137ff3961073ae65e030a35f12d7af89c5f4a 100644 (file)
@@ -303,21 +303,60 @@ void smtp_hello(long offset, long which_command)
 }
 
 
+/*
+ * Backend function for smtp_webcit_preferences_hack().
+ * Look at a message and determine if it's the preferences file.
+ */
+void smtp_webcit_preferences_hack_backend(long msgnum, void *userdata) {
+       struct CtdlMessage *msg;
+       char **webcit_conf = (char **) userdata;
+
+       if (*webcit_conf) {
+               return; // already got it
+       }
+
+       msg = CtdlFetchMessage(msgnum, 1);
+       if (msg == NULL) {
+               return;
+       }
+
+       if ( (msg->cm_fields[eMsgSubject]) && (!strcasecmp(msg->cm_fields[eMsgSubject], "__ WebCit Preferences __")) ) {
+               /* This is it!  Change ownership of the message text so it doesn't get freed. */
+               *webcit_conf = (char *)msg->cm_fields[eMesageText];
+               msg->cm_fields[eMesageText] = NULL;
+       }
+       CtdlFreeMessage(msg);
+}
+
+
 /*
  * The configuration item for the user's preferred display name for outgoing email is, unfortunately,
  * stored in the account's WebCit configuration.  We have to fetch it now.
+ */
 void smtp_webcit_preferences_hack(void) {
        char config_roomname[ROOMNAMELEN];
+       char *webcit_conf = NULL;
 
        snprintf(config_roomname, sizeof config_roomname, "%010ld.%s", CC->user.usernum, USERCONFIGROOM);
        if (CtdlGetRoom(&CC->room, config_roomname) != 0) {
                return;
        }
 
-       // FIXME ... finish this
+       /*
+        * Find the WebCit configuration message
+        */
 
+       CtdlForEachMessage(MSGS_ALL, 1, NULL, NULL, NULL, smtp_webcit_preferences_hack_backend, (void *)&webcit_conf);
+
+       if (!webcit_conf) {
+               return;
+       }
+
+       /* FIXME : now do something with this data */
+
+       free(webcit_conf);
+       abort();
 }
- */
 
 
 
@@ -405,7 +444,7 @@ void smtp_try_plain(long offset, long Flags)
 
        if (result == login_ok) {
                if (CtdlTryPassword(pass, len) == pass_ok) {
-                       /* smtp_webcit_preferences_hack(); */
+////                   smtp_webcit_preferences_hack();
                        smtp_auth_greeting(offset, Flags);
                        return;
                }
@@ -477,8 +516,6 @@ void smtp_auth(long offset, long Flags)
  * Set do_response to nonzero to output the SMTP RSET response code.
  */
 void smtp_rset(long offset, long do_response) {
-       int is_lmtp;
-       int is_unfiltered;
        citsmtp *sSMTP = SMTP;
 
        /*
@@ -486,10 +523,21 @@ void smtp_rset(long offset, long do_response) {
         * but we need to preserve this one little piece of information, so
         * we save it for later.
         */
-       is_lmtp = sSMTP->is_lmtp;
-       is_unfiltered = sSMTP->is_unfiltered;
 
-       memset(sSMTP, 0, sizeof(citsmtp));
+       FlushStrBuf(sSMTP->Cmd);
+       FlushStrBuf(sSMTP->helo_node);
+       FlushStrBuf(sSMTP->from);
+       FlushStrBuf(sSMTP->recipients);
+       FlushStrBuf(sSMTP->OneRcpt);
+
+       sSMTP->command_state = 0;
+       sSMTP->number_of_recipients = 0;
+       sSMTP->delivery_mode = 0;
+       sSMTP->message_originated_locally = 0;
+       sSMTP->is_msa = 0;
+       /*
+        * we must remember is_lmtp & is_unfiltered.
+        */
 
        /*
         * It is somewhat ambiguous whether we want to log out when a RSET
@@ -502,12 +550,6 @@ void smtp_rset(long offset, long do_response) {
         * }
         */
 
-       /*
-        * Reinstate this little piece of information we saved (see above).
-        */
-       sSMTP->is_lmtp = is_lmtp;
-       sSMTP->is_unfiltered = is_unfiltered;
-
        if (do_response) {
                cprintf("250 Zap!\r\n");
        }
@@ -583,9 +625,13 @@ void smtp_mail(long offset, long flags) {
         */
        else if (config.c_allow_spoofing == 0) {
                process_rfc822_addr(ChrPtr(sSMTP->from), user, node, name);
+               syslog(LOG_DEBUG, "Claimed envelope sender is '%s' == '%s' @ '%s' ('%s')",
+                       ChrPtr(sSMTP->from), user, node, name
+               );
                if (CtdlHostAlias(node) != hostalias_nomatch) {
                        cprintf("550 You must log in to send mail from %s\r\n", node);
                        FlushStrBuf(sSMTP->from);
+                       syslog(LOG_DEBUG, "Rejecting unauthenticated mail from %s", node);
                        return;
                }
        }
@@ -767,12 +813,12 @@ void smtp_data(long offset, long flags)
        if ( (CCC->logged_in) && (config.c_rfc822_strict_from != CFG_SMTP_FROM_NOFILTER) ) {
                int validemail = 0;
                
-               if (!IsEmptyStr(msg->cm_fields['F'])       &&
+               if (!IsEmptyStr(msg->cm_fields[erFc822Addr])       &&
                    ((config.c_rfc822_strict_from == CFG_SMTP_FROM_CORRECT) || 
                     (config.c_rfc822_strict_from == CFG_SMTP_FROM_REJECT)    )  )
                {
                        if (!IsEmptyStr(CCC->cs_inet_email))
-                               validemail = strcmp(CCC->cs_inet_email, msg->cm_fields['F']) == 0;
+                               validemail = strcmp(CCC->cs_inet_email, msg->cm_fields[erFc822Addr]) == 0;
                        if ((!validemail) && 
                            (!IsEmptyStr(CCC->cs_inet_other_emails)))
                        {
@@ -782,43 +828,43 @@ void smtp_data(long offset, long flags)
                                for (i=0; i < num_secondary_emails && !validemail; ++i) {
                                        char buf[256];
                                        extract_token(buf, CCC->cs_inet_other_emails,i,'|',sizeof CCC->cs_inet_other_emails);
-                                       validemail = strcmp(buf, msg->cm_fields['F']) == 0;
+                                       validemail = strcmp(buf, msg->cm_fields[erFc822Addr]) == 0;
                                }
                        }
                }
 
                if (!validemail && (config.c_rfc822_strict_from == CFG_SMTP_FROM_REJECT)) {
-                       syslog(LOG_ERR, "invalid sender '%s' - rejecting this message", msg->cm_fields['F']);
-                       cprintf("550 Invalid sender '%s' - rejecting this message.\r\n", msg->cm_fields['F']);
+                       syslog(LOG_ERR, "invalid sender '%s' - rejecting this message", msg->cm_fields[erFc822Addr]);
+                       cprintf("550 Invalid sender '%s' - rejecting this message.\r\n", msg->cm_fields[erFc822Addr]);
                        return;
                }
 
-               if (msg->cm_fields['A'] != NULL) free(msg->cm_fields['A']);
-               if (msg->cm_fields['N'] != NULL) free(msg->cm_fields['N']);
-               if (msg->cm_fields['H'] != NULL) free(msg->cm_fields['H']);
-               if (msg->cm_fields['O'] != NULL) free(msg->cm_fields['O']);
-               msg->cm_fields['A'] = strdup(CCC->user.fullname);
-               msg->cm_fields['N'] = strdup(config.c_nodename);
-               msg->cm_fields['H'] = strdup(config.c_humannode);
-               msg->cm_fields['O'] = strdup(MAILROOM);
+               if (msg->cm_fields[eAuthor] != NULL) free(msg->cm_fields[eAuthor]);
+               if (msg->cm_fields[eNodeName] != NULL) free(msg->cm_fields[eNodeName]);
+               if (msg->cm_fields[eHumanNode] != NULL) free(msg->cm_fields[eHumanNode]);
+               if (msg->cm_fields[eOriginalRoom] != NULL) free(msg->cm_fields[eOriginalRoom]);
+               msg->cm_fields[eAuthor] = strdup(CCC->user.fullname);
+               msg->cm_fields[eNodeName] = strdup(config.c_nodename);
+               msg->cm_fields[eHumanNode] = strdup(config.c_humannode);
+               msg->cm_fields[eOriginalRoom] = strdup(MAILROOM);
 
                if (!validemail) {
-                       if (msg->cm_fields['F'] != NULL) free(msg->cm_fields['F']);
-                       msg->cm_fields['F'] = strdup(CCC->cs_inet_email);
+                       if (msg->cm_fields[erFc822Addr] != NULL) free(msg->cm_fields[erFc822Addr]);
+                       msg->cm_fields[erFc822Addr] = strdup(CCC->cs_inet_email);
                }
        }
 
        /* Set the "envelope from" address */
-       if (msg->cm_fields['P'] != NULL) {
-               free(msg->cm_fields['P']);
+       if (msg->cm_fields[eMessagePath] != NULL) {
+               free(msg->cm_fields[eMessagePath]);
        }
-       msg->cm_fields['P'] = strdup(ChrPtr(sSMTP->from));
+       msg->cm_fields[eMessagePath] = strdup(ChrPtr(sSMTP->from));
 
        /* Set the "envelope to" address */
-       if (msg->cm_fields['V'] != NULL) {
-               free(msg->cm_fields['V']);
+       if (msg->cm_fields[eenVelopeTo] != NULL) {
+               free(msg->cm_fields[eenVelopeTo]);
        }
-       msg->cm_fields['V'] = strdup(ChrPtr(sSMTP->recipients));
+       msg->cm_fields[eenVelopeTo] = strdup(ChrPtr(sSMTP->recipients));
 
        /* Submit the message into the Citadel system. */
        valid = validate_recipients(
@@ -840,11 +886,11 @@ void smtp_data(long offset, long flags)
 
        if (scan_errors > 0) {  /* We don't want this message! */
 
-               if (msg->cm_fields['0'] == NULL) {
-                       msg->cm_fields['0'] = strdup("Message rejected by filter");
+               if (msg->cm_fields[eErrorMsg] == NULL) {
+                       msg->cm_fields[eErrorMsg] = strdup("Message rejected by filter");
                }
 
-               StrBufPrintf(sSMTP->OneRcpt, "550 %s\r\n", msg->cm_fields['0']);
+               StrBufPrintf(sSMTP->OneRcpt, "550 %s\r\n", msg->cm_fields[eErrorMsg]);
        }
        
        else {                  /* Ok, we'll accept this message. */