*
* This is the new, exciting, clever version that makes libcurl do all the work :)
*
- * Copyright (c) 1997-2020 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
/*
* Initialize the SMTP outbound queue
*/
-void smtp_init_spoolout(void)
-{
+void smtp_init_spoolout(void) {
struct ctdlroom qrbuf;
/*
* not happen because the delivery instructions message does not
* contain a recipient.
*/
-int smtp_aftersave(struct CtdlMessage *msg, struct 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];
/*
* 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;
* 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)
/*
* 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;
}
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);
* 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;
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,
}
FreeStrBuf(&s.TheMessage);
- if (fromaddr)
+ if (fromaddr) {
free(fromaddr);
+ }
return ((int) response_code);
}
/*
* 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;
int should_try_now = 0;
if (attempted < submitted) { // If no attempts have been made yet, try now
should_try_now = 1;
- } else if ((attempted - submitted) <= 14400) {
+ }
+ else if ((attempted - submitted) <= 14400) {
if ((time(NULL) - attempted) > 1800) { // First four hours, retry every 30 minutes
should_try_now = 1;
}
- } else {
+ }
+ else {
if ((time(NULL) - attempted) > 14400) { // After that, retry once every 4 hours
should_try_now = 1;
}
}
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);
StrBufAppendPrintf(NewInstr, "submitted|%ld\n", submitted);
- if (bounceto)
+ if (bounceto) {
StrBufAppendPrintf(NewInstr, "bounceto|%s\n", bounceto);
- if (envelope_from)
+ }
+ if (envelope_from) {
StrBufAppendPrintf(NewInstr, "envelope_from|%s\n", envelope_from);
-
+ }
for (i = 0; i < num_tokens(instr, '\n'); ++i) {
extract_token(cfgline, instr, i, '\n', sizeof cfgline);
if (!strncasecmp(cfgline, HKEY("remote|"))) {
"smtpclient: recp: <%s> , result: %d (%s)", recp, new_result, server_response);
if ((new_result / 100) == 2) {
++num_success;
- } else {
+ }
+ else {
if ((new_result / 100) == 5) {
++num_fail;
- } else {
+ }
+ else {
++num_delayed;
}
StrBufAppendPrintf
deletes[1] = msgid;
CtdlDeleteMessages(SMTP_SPOOLOUT_ROOM, deletes, 2, "");
FreeStrBuf(&NewInstr); // We have to free NewInstr here, no longer needed
- } else {
+ }
+ else {
// 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
CM_Free(msg);
CtdlDeleteMessages(SMTP_SPOOLOUT_ROOM, &qmsgnum, 1, "");
}
- } else {
+ }
+ else {
syslog(LOG_DEBUG, "smtpclient: %ld retry time not reached", qmsgnum);
}
- if (bounceto != NULL)
+ if (bounceto != NULL) {
free(bounceto);
- if (envelope_from != NULL)
+ }
+ if (envelope_from != NULL) {
free(envelope_from);
+ }
free(instr);
}
/*
* 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;
/*
* Run through the queue sending out messages.
*/
-void smtp_do_queue(void)
-{
+void smtp_do_queue(void) {
int i = 0;
/*
* don't really require extremely fine granularity here, we'll do it
* with a static variable instead.
*/
- if (doing_smtpclient)
+ if (doing_smtpclient) {
return;
+ }
doing_smtpclient = 1;
syslog(LOG_DEBUG, "smtpclient: start queue run");
{
if (!threading) {
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();
}