getmx() now uses our array class
[citadel.git] / citadel / modules / smtp / serv_smtpclient.c
index 9e1226ac87be67f8ed24139b161a0cad1629d4c8..8e87ce4c2f4da626569a4842c602db51aaf7af51 100644 (file)
@@ -3,7 +3,7 @@
  *
  * This is the new, exciting, clever version that makes libcurl do all the work  :)
  *
- * Copyright (c) 1997-2018 by the citadel.org team
+ * Copyright (c) 1997-2021 by the citadel.org team
  *
  * This program is open source software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License as published
 #include <unistd.h>
 #include <stdio.h>
 #include <sysconfig.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 <ctype.h>
 #include <string.h>
 #include <errno.h>
@@ -58,7 +47,6 @@ struct smtpmsgsrc {           // Data passed in and out of libcurl for message upload
        int bytes_sent;
 };
 
-struct CitContext smtp_client_CC;
 static int doing_smtpclient = 0;
 long *smtpq = NULL;            // array of msgnums containing queue instructions
 int smtpq_count = 0;           // number of queue messages in smtpq
@@ -68,8 +56,7 @@ int smtpq_alloc = 0;          // current allocation size for smtpq
 /*
  * Initialize the SMTP outbound queue
  */
-void smtp_init_spoolout(void)
-{
+void smtp_init_spoolout(void) {
        struct ctdlroom qrbuf;
 
        /*
@@ -94,8 +81,7 @@ void smtp_init_spoolout(void)
  * not happen because the delivery instructions message does not
  * contain a recipient.
  */
-int smtp_aftersave(struct CtdlMessage *msg, recptypes * recps)
-{
+int smtp_aftersave(struct CtdlMessage *msg, struct recptypes *recps) {
        if ((recps != NULL) && (recps->num_internet > 0)) {
                struct CtdlMessage *imsg = NULL;
                char recipient[SIZ];
@@ -142,7 +128,7 @@ int smtp_aftersave(struct CtdlMessage *msg, recptypes * recps)
                CM_SetField(imsg, eAuthor, HKEY("Citadel"));
                CM_SetField(imsg, eJournal, HKEY("do not journal"));
                CM_SetAsFieldSB(imsg, eMesageText, &SpoolMsg);
-               CtdlSubmitMsg(imsg, NULL, SMTP_SPOOLOUT_ROOM, QP_EADDR);
+               CtdlSubmitMsg(imsg, NULL, SMTP_SPOOLOUT_ROOM);
                CM_Free(imsg);
        }
        return 0;
@@ -152,8 +138,7 @@ int smtp_aftersave(struct CtdlMessage *msg, recptypes * recps)
 /*
  * Callback for smtp_attempt_delivery() to supply libcurl with upload data.
  */
-static size_t upload_source(void *ptr, size_t size, size_t nmemb, void *userp)
-{
+static size_t upload_source(void *ptr, size_t size, size_t nmemb, void *userp) {
        struct smtpmsgsrc *s = (struct smtpmsgsrc *) userp;
        int sendbytes = 0;
        const char *send_this = NULL;
@@ -182,8 +167,7 @@ static size_t upload_source(void *ptr, size_t size, size_t nmemb, void *userp)
  * by the remote server.  This is an ugly way to extract it, by capturing debug data from
  * the library and filtering on the lines we want.
  */
-int ctdl_libcurl_smtp_debug_callback(CURL *handle, curl_infotype type, char *data, size_t size, void *userptr)
-{
+int ctdl_libcurl_smtp_debug_callback(CURL *handle, curl_infotype type, char *data, size_t size, void *userptr) {
        if (type != CURLINFO_HEADER_IN)
                return 0;
        if (!userptr)
@@ -203,8 +187,7 @@ int ctdl_libcurl_smtp_debug_callback(CURL *handle, curl_infotype type, char *dat
 /*
  * Go through the debug output of an SMTP transaction, and boil it down to just the final success or error response message.
  */
-void trim_response(long response_code, char *response)
-{
+void trim_response(long response_code, char *response) {
        if ((response_code < 100) || (response_code > 999) || (IsEmptyStr(response))) {
                return;
        }
@@ -225,8 +208,8 @@ void trim_response(long response_code, char *response)
        char response_code_str[4];
        snprintf(response_code_str, sizeof response_code_str, "%ld", response_code);
        char *respstart = strstr(response, response_code_str);
-       if (respstart == NULL) {
-               strcpy(response, smtpstatus(response_code));
+       if (respstart == NULL) {                                // If we have a response code but no response text,
+               strcpy(response, smtpstatus(response_code));    // use one of our canned messages.
                return;
        }
        strcpy(response, respstart);
@@ -242,8 +225,7 @@ void trim_response(long response_code, char *response)
  * Attempt a delivery to one recipient.
  * Returns a three-digit SMTP status code.
  */
-int smtp_attempt_delivery(long msgid, char *recp, char *envelope_from, char *response)
-{
+int smtp_attempt_delivery(long msgid, char *recp, char *envelope_from, char *response) {
        struct smtpmsgsrc s;
        char *fromaddr = NULL;
        CURL *curl;
@@ -318,7 +300,7 @@ int smtp_attempt_delivery(long msgid, char *recp, char *envelope_from, char *res
                                 try_this_mx, CtdlGetConfigStr("c_fqdn")
                            );
                        curl_easy_setopt(curl, CURLOPT_URL, smtp_url);
-                       syslog(LOG_DEBUG, "smtpclient: trying %s", smtp_url);   // send the message
+                       syslog(LOG_DEBUG, "smtpclient: trying MX %d of %d <%s>", i+1, num_mx, smtp_url);        // send the message
                        res = curl_easy_perform(curl);
                        curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
                        syslog(LOG_DEBUG,
@@ -349,8 +331,7 @@ int smtp_attempt_delivery(long msgid, char *recp, char *envelope_from, char *res
 /*
  * Process one outbound message.
  */
-void smtp_process_one_msg(long qmsgnum)
-{
+void smtp_process_one_msg(long qmsgnum) {
        struct CtdlMessage *msg = NULL;
        char *instr = NULL;
        int i;
@@ -361,7 +342,7 @@ void smtp_process_one_msg(long qmsgnum)
        int delete_this_queue = 0;
        char server_response[SIZ];
 
-       msg = CtdlFetchMessage(qmsgnum, 1, 1);
+       msg = CtdlFetchMessage(qmsgnum, 1);
        if (msg == NULL) {
                syslog(LOG_WARNING, "smtpclient: %ld does not exist", qmsgnum);
                return;
@@ -418,7 +399,7 @@ void smtp_process_one_msg(long qmsgnum)
        }
 
        if (should_try_now) {
-               syslog(LOG_DEBUG, "smtpclient: %ld attempting delivery now", qmsgnum);
+               syslog(LOG_DEBUG, "smtpclient: attempting delivery of message <%ld> now", qmsgnum);
                StrBuf *NewInstr = NewStrBuf();
                StrBufAppendPrintf(NewInstr, "Content-type: " SPOOLMIME "\n\n");
                StrBufAppendPrintf(NewInstr, "msgid|%ld\n", msgid);
@@ -500,7 +481,7 @@ void smtp_process_one_msg(long qmsgnum)
                        // replace the old queue entry with the new one
                        syslog(LOG_DEBUG, "smtpclient: %ld rewriting", qmsgnum);
                        msg = convert_internet_message_buf(&NewInstr);  // This function will free NewInstr for us
-                       CtdlSubmitMsg(msg, NULL, SMTP_SPOOLOUT_ROOM, 0);
+                       CtdlSubmitMsg(msg, NULL, SMTP_SPOOLOUT_ROOM);
                        CM_Free(msg);
                        CtdlDeleteMessages(SMTP_SPOOLOUT_ROOM, &qmsgnum, 1, "");
                }
@@ -519,8 +500,7 @@ void smtp_process_one_msg(long qmsgnum)
 /*
  * Callback for smtp_do_queue()
  */
-void smtp_add_msg(long msgnum, void *userdata)
-{
+void smtp_add_msg(long msgnum, void *userdata) {
 
        if (smtpq == NULL) {
                smtpq_count = 0;
@@ -540,8 +520,7 @@ void smtp_add_msg(long msgnum, void *userdata)
 /*
  * Run through the queue sending out messages.
  */
-void smtp_do_queue(void)
-{
+void smtp_do_queue(void) {
        int i = 0;
 
        /*
@@ -555,7 +534,6 @@ void smtp_do_queue(void)
        doing_smtpclient = 1;
 
        syslog(LOG_DEBUG, "smtpclient: start queue run");
-       pthread_setspecific(MyConKey, (void *) &smtp_client_CC);
 
        if (CtdlGetRoom(&CC->room, SMTP_SPOOLOUT_ROOM) != 0) {
                syslog(LOG_WARNING, "Cannot find room <%s>", SMTP_SPOOLOUT_ROOM);
@@ -582,9 +560,8 @@ void smtp_do_queue(void)
 CTDL_MODULE_INIT(smtpclient)
 {
        if (!threading) {
-               CtdlFillSystemContext(&smtp_client_CC, "SMTP_Send");
                CtdlRegisterMessageHook(smtp_aftersave, EVT_AFTERSAVE);
-               CtdlRegisterSessionHook(smtp_do_queue, EVT_TIMER, PRIO_AGGR + 50);
+               CtdlRegisterSessionHook(smtp_do_queue, EVT_TIMER, PRIO_AGGR + 51);
                smtp_init_spoolout();
        }