]> code.citadel.org Git - citadel.git/blobdiff - citadel/modules/smtp/serv_smtp.c
CtdlThreadSchedule() considered harmful. Eliminate. Replaced all
[citadel.git] / citadel / modules / smtp / serv_smtp.c
index 5e5707d2fec31539903dd5045a240ad41f2b1aaf..ab01bbd7db9bbc1fb10f4c2049d8be55c49f10a5 100644 (file)
@@ -1,6 +1,4 @@
 /*
- * $Id$
- *
  * This module is an SMTP and ESMTP implementation for the Citadel system.
  * It is compliant with all of the following:
  *
@@ -76,7 +74,6 @@
 #include "config.h"
 #include "control.h"
 #include "user_ops.h"
-#include "policy.h"
 #include "database.h"
 #include "msgbase.h"
 #include "internet_addressing.h"
@@ -122,8 +119,6 @@ enum {                              /* Command states for login authentication */
 
 int run_queue_now = 0; /* Set to 1 to ignore SMTP send retry times */
 
-citthread_mutex_t smtp_send_lock;
-
 
 /*****************************************************************************/
 /*                      SMTP SERVER (INBOUND) STUFF                          */
@@ -340,11 +335,12 @@ void smtp_get_user(char *argbuf) {
  */
 void smtp_get_pass(char *argbuf) {
        char password[SIZ];
+       long len;
 
        memset(password, 0, sizeof(password));  
-       CtdlDecodeBase64(password, argbuf, SIZ);
+       len = CtdlDecodeBase64(password, argbuf, SIZ);
        /* CtdlLogPrintf(CTDL_DEBUG, "Trying <%s>\n", password); */
-       if (CtdlTryPassword(password) == pass_ok) {
+       if (CtdlTryPassword(password, len) == pass_ok) {
                smtp_auth_greeting();
        }
        else {
@@ -363,11 +359,14 @@ void smtp_try_plain(char *encoded_authstring) {
        char user[256];
        char pass[256];
        int result;
+       long len;
 
        CtdlDecodeBase64(decoded_authstring, encoded_authstring, strlen(encoded_authstring) );
        safestrncpy(ident, decoded_authstring, sizeof ident);
        safestrncpy(user, &decoded_authstring[strlen(ident) + 1], sizeof user);
-       safestrncpy(pass, &decoded_authstring[strlen(ident) + strlen(user) + 2], sizeof pass);
+       len = safestrncpy(pass, &decoded_authstring[strlen(ident) + strlen(user) + 2], sizeof pass);
+       if (len == -1)
+               len = sizeof(pass) - 1;
 
        SMTP->command_state = smtp_command;
 
@@ -379,7 +378,7 @@ void smtp_try_plain(char *encoded_authstring) {
        }
 
        if (result == login_ok) {
-               if (CtdlTryPassword(pass) == pass_ok) {
+               if (CtdlTryPassword(pass, len) == pass_ok) {
                        smtp_auth_greeting();
                        return;
                }
@@ -989,7 +988,7 @@ void smtp_try(const char *key, const char *addr, int *status,
 
        /* Load the message out of the database */
        CCC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
-       CtdlOutputMsg(msgnum, MT_RFC822, HEADERS_ALL, 0, 1, NULL, ESC_DOT);
+       CtdlOutputMsg(msgnum, MT_RFC822, HEADERS_ALL, 0, 1, NULL, (ESC_DOT|SUPPRESS_ENV_TO) );
        msg_size = StrLength(CC->redirect_buffer);
        msgtext = SmashStrBuf(&CC->redirect_buffer);
 
@@ -999,7 +998,7 @@ void smtp_try(const char *key, const char *addr, int *status,
                scan_done = 0;
                ptr = msgtext;
                do {
-                       if (ptr = memreadline(ptr, buf, sizeof buf), *ptr == 0) {
+                       if (ptr = cmemreadline(ptr, buf, sizeof buf), *ptr == 0) {
                                scan_done = 1;
                        }
                        if (!strncasecmp(buf, "From:", 5)) {
@@ -1080,7 +1079,7 @@ void smtp_try(const char *key, const char *addr, int *status,
                        strcpy(mx_port, "25");
                }
                CtdlLogPrintf(CTDL_DEBUG, "SMTP client: connecting to %s : %s ...\n", mx_host, mx_port);
-               sock = sock_connect(mx_host, mx_port, "tcp");
+               sock = sock_connect(mx_host, mx_port);
                snprintf(dsn, SIZ, "Could not connect: %s", strerror(errno));
                if (sock >= 0) CtdlLogPrintf(CTDL_DEBUG, "SMTP client: connected!\n");
                if (sock < 0) {
@@ -1731,61 +1730,34 @@ void smtp_do_procmsg(long msgnum, void *userdata) {
 
 
 /*
- * smtp_do_queue()
+ * smtp_queue_thread()
  * 
  * Run through the queue sending out messages.
  */
-void *smtp_do_queue(void *arg) {
+void *smtp_queue_thread(void *arg) {
        int num_processed = 0;
        struct CitContext smtp_queue_CC;
 
-       CtdlLogPrintf(CTDL_INFO, "SMTP client: processing outbound queue\n");
-
        CtdlFillSystemContext(&smtp_queue_CC, "SMTP Send");
-       citthread_setspecific(MyConKey, (void *)&smtp_queue_CC );
-
-       if (CtdlGetRoom(&CC->room, SMTP_SPOOLOUT_ROOM) != 0) {
-               CtdlLogPrintf(CTDL_ERR, "Cannot find room <%s>\n", SMTP_SPOOLOUT_ROOM);
-       }
-       else {
-               num_processed = CtdlForEachMessage(MSGS_ALL, 0L, NULL, SPOOLMIME, NULL, smtp_do_procmsg, NULL);
-       }
-
-       citthread_mutex_unlock (&smtp_send_lock);
-       CtdlLogPrintf(CTDL_INFO, "SMTP client: queue run completed; %d messages processed\n", num_processed);
-       return(NULL);
-}
-
+       citthread_setspecific(MyConKey, (void *)&smtp_queue_CC);
+       CtdlLogPrintf(CTDL_DEBUG, "smtp_queue_thread() initializing\n");
 
+       while (!CtdlThreadCheckStop()) {
+               
+               CtdlLogPrintf(CTDL_INFO, "SMTP client: processing outbound queue\n");
 
-/*
- * smtp_queue_thread
- *
- * Create a thread to run the SMTP queue
- *
- * This was created as a response to a situation seen on Uncensored where a bad remote was holding
- * up SMTP sending for long times.
- * Converting to a thread does not fix the problem caused by the bad remote but it does prevent
- * the SMTP sending from stopping housekeeping and the EVT_TIMER event system which in turn prevented
- * other things from happening.
- */
-void smtp_queue_thread (void)
-{
-       if (citthread_mutex_trylock (&smtp_send_lock)) {
-               CtdlLogPrintf(CTDL_DEBUG, "SMTP queue run already in progress\n");
-       }
-       else {
-               CtdlThreadCreate("SMTP Send", CTDLTHREAD_BIGSTACK, smtp_do_queue, NULL);
+               if (CtdlGetRoom(&CC->room, SMTP_SPOOLOUT_ROOM) != 0) {
+                       CtdlLogPrintf(CTDL_ERR, "Cannot find room <%s>\n", SMTP_SPOOLOUT_ROOM);
+               }
+               else {
+                       num_processed = CtdlForEachMessage(MSGS_ALL, 0L, NULL, SPOOLMIME, NULL, smtp_do_procmsg, NULL);
+               }
+               CtdlLogPrintf(CTDL_INFO, "SMTP client: queue run completed; %d messages processed\n", num_processed);
+               CtdlThreadSleep(60);
        }
-}
-
-
 
-void smtp_server_going_down (void)
-{
-       CtdlLogPrintf(CTDL_DEBUG, "SMTP module clean up for shutdown.\n");
-
-       citthread_mutex_destroy (&smtp_send_lock);
+       CtdlClearSystemContext();
+       return(NULL);
 }
 
 
@@ -1922,13 +1894,11 @@ CTDL_MODULE_INIT(smtp)
                                        CitadelServiceSMTP_LMTP_UNF);
 
                smtp_init_spoolout();
-               CtdlRegisterSessionHook(smtp_queue_thread, EVT_TIMER);
                CtdlRegisterSessionHook(smtp_cleanup_function, EVT_STOP);
                CtdlRegisterProtoHook(cmd_smtp, "SMTP", "SMTP utility commands");
-               CtdlRegisterCleanupHook (smtp_server_going_down);
-               citthread_mutex_init (&smtp_send_lock, NULL);
+               CtdlThreadCreate("SMTP Send", CTDLTHREAD_BIGSTACK, smtp_queue_thread, NULL);
        }
        
        /* return our Subversion id for the Log */
-       return "$Id$";
+       return "smtp";
 }