SMTP: when we did the login and replied, we mustn't try again to process the...
[citadel.git] / citadel / modules / smtp / serv_smtp.c
index be6347466270576c957d1610d8c29e1d3b5a961c..58457b180cdc8ff50cab65801207ee31c4044dca 100644 (file)
@@ -69,6 +69,7 @@
 #include "config.h"
 #include "control.h"
 #include "user_ops.h"
+#include "room_ops.h"
 #include "database.h"
 #include "msgbase.h"
 #include "internet_addressing.h"
@@ -322,12 +323,14 @@ void smtp_webcit_preferences_hack_backend(long msgnum, void *userdata) {
                return;
        }
 
-       if ( (msg->cm_fields['U']) && (!strcasecmp(msg->cm_fields['U'], "__ WebCit Preferences __")) ) {
+       if ( !CM_IsEmpty(msg, 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['M'];
-               msg->cm_fields['M'] = NULL;
+               *webcit_conf = (char *)msg->cm_fields[eMesageText];
+               msg->cm_fields[eMesageText] = NULL;
        }
-       CtdlFreeMessage(msg);
+       CM_Free(msg);
 }
 
 
@@ -619,7 +622,7 @@ void smtp_mail(long offset, long flags) {
         * address so we don't have to contend with the empty string causing
         * other code to fail when it's expecting something there.
         */
-       if (StrLength(sSMTP->from)) {
+       if (StrLength(sSMTP->from) == 0) {
                StrBufPlain(sSMTP->from, HKEY("someone@example.com"));
        }
 
@@ -665,7 +668,7 @@ void smtp_rcpt(long offset, long flags)
 {
        struct CitContext *CCC = CC;
        char message_to_spammer[SIZ];
-       struct recptypes *valid = NULL;
+       recptypes *valid = NULL;
        citsmtp *sSMTP = SMTP;
 
        if (StrLength(sSMTP->from) == 0) {
@@ -764,7 +767,7 @@ void smtp_data(long offset, long flags)
        struct CtdlMessage *msg = NULL;
        long msgnum = (-1L);
        char nowstamp[SIZ];
-       struct recptypes *valid;
+       recptypes *valid;
        int scan_errors;
        int i;
        citsmtp *sSMTP = SMTP;
@@ -830,12 +833,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 (!CM_IsEmpty(msg, 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)))
                        {
@@ -845,52 +848,38 @@ 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['N'] = strdup(config.c_nodename);
-               msg->cm_fields['H'] = strdup(config.c_humannode);
-               msg->cm_fields['O'] = strdup(MAILROOM);
-
-               msg->cm_fields['A'] =
-                       ((sSMTP->preferred_sender_name != NULL)
-                       ? strdup(ChrPtr(sSMTP->preferred_sender_name)) 
-                       : strdup(CCC->user.fullname)
-               );
+               CM_SetField(msg, eNodeName, CFG_KEY(c_nodename));
+               CM_SetField(msg, eHumanNode, CFG_KEY(c_humannode));
+               CM_SetField(msg, eOriginalRoom, HKEY(MAILROOM));
+               if (sSMTP->preferred_sender_name != NULL)
+                       CM_SetField(msg, eAuthor, SKEY(sSMTP->preferred_sender_name));
+               else 
+                       CM_SetField(msg, eAuthor, CCC->user.fullname, strlen(CCC->user.fullname));
 
                if (!validemail) {
-                       if (msg->cm_fields['F'] != NULL) free(msg->cm_fields['F']);
-                       msg->cm_fields['F'] = 
-                               ((sSMTP->preferred_sender_email != NULL)
-                               ? strdup(ChrPtr(sSMTP->preferred_sender_email)) 
-                               : strdup(CCC->cs_inet_email)
-                       );
+                       if (sSMTP->preferred_sender_email != NULL)
+                               CM_SetField(msg, erFc822Addr, SKEY(sSMTP->preferred_sender_email));
+                       else
+                               CM_SetField(msg, erFc822Addr, CCC->cs_inet_email, strlen(CCC->cs_inet_email));
                }
        }
 
        /* Set the "envelope from" address */
-       if (msg->cm_fields['P'] != NULL) {
-               free(msg->cm_fields['P']);
-       }
-       msg->cm_fields['P'] = strdup(ChrPtr(sSMTP->from));
+       CM_SetField(msg, eMessagePath, SKEY(sSMTP->from));
 
        /* Set the "envelope to" address */
-       if (msg->cm_fields['V'] != NULL) {
-               free(msg->cm_fields['V']);
-       }
-       msg->cm_fields['V'] = strdup(ChrPtr(sSMTP->recipients));
+       CM_SetField(msg, eenVelopeTo, SKEY(sSMTP->recipients));
 
        /* Submit the message into the Citadel system. */
        valid = validate_recipients(
@@ -907,16 +896,16 @@ void smtp_data(long offset, long flags)
                scan_errors = 0;
        }
        else {
-               scan_errors = PerformMessageHooks(msg, EVT_SMTPSCAN);
+               scan_errors = PerformMessageHooks(msg, valid, EVT_SMTPSCAN);
        }
 
        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 (CM_IsEmpty(msg, eErrorMsg)) {
+                       CM_SetField(msg, eErrorMsg, HKEY("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. */
@@ -958,7 +947,7 @@ void smtp_data(long offset, long flags)
        );
 
        /* Clean up */
-       CtdlFreeMessage(msg);
+       CM_Free(msg);
        free_recipients(valid);
        smtp_data_clear(0, 0);  /* clear out the buffers now */
 }
@@ -1007,14 +996,17 @@ void smtp_command_loop(void)
 
        if (sSMTP->command_state == smtp_user) {
                smtp_get_user(0);
+               return;
        }
 
        else if (sSMTP->command_state == smtp_password) {
                smtp_get_pass(0, 0);
+               return;
        }
 
        else if (sSMTP->command_state == smtp_plain) {
                smtp_try_plain(0, 0);
+               return;
        }
 
        pchs = pch = ChrPtr(sSMTP->Cmd);