keep track of last_queue_job_submitted
[citadel.git] / citadel / server / modules / smtp / serv_smtpclient.c
1 // Transmit outbound SMTP mail to the big wide world of the Internet
2 //
3 // This is the new, exciting, clever version that makes libcurl do all the work  :)
4 //
5 // Copyright (c) 1997-2023 by the citadel.org team
6 //
7 // This program is open source software.  Use, duplication, or disclosure
8 // is subject to the terms of the GNU General Public License, version 3.
9
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <stdio.h>
13 #include <time.h>
14 #include <ctype.h>
15 #include <string.h>
16 #include <errno.h>
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <libcitadel.h>
20 #include <curl/curl.h>
21 #include "../../sysconfig.h"
22 #include "../../citadel.h"
23 #include "../../server.h"
24 #include "../../citserver.h"
25 #include "../../support.h"
26 #include "../../config.h"
27 #include "../../ctdl_module.h"
28 #include "../../clientsocket.h"
29 #include "../../msgbase.h"
30 #include "../../domain.h"
31 #include "../../internet_addressing.h"
32 #include "../../citadel_dirs.h"
33 #include "../smtp/smtp_util.h"
34
35 long last_queue_job_submitted = 0;
36
37 struct smtpmsgsrc {             // Data passed in and out of libcurl for message upload
38         StrBuf *TheMessage;
39         int bytes_total;
40         int bytes_sent;
41 };
42
43 // Initialize the SMTP outbound queue
44 void smtp_init_spoolout(void) {
45         struct ctdlroom qrbuf;
46
47         // Create the room.  This will silently fail if the room already
48         // exists, and that's perfectly ok, because we want it to exist.
49         CtdlCreateRoom(SMTP_SPOOLOUT_ROOM, 3, "", 0, 1, 0, VIEW_QUEUE);
50
51         // Make sure it's set to be a "system room" so it doesn't show up
52         // in the <K>nown rooms list for administrators.
53         if (CtdlGetRoomLock(&qrbuf, SMTP_SPOOLOUT_ROOM) == 0) {
54                 qrbuf.QRflags2 |= QR2_SYSTEM;
55                 CtdlPutRoomLock(&qrbuf);
56         }
57 }
58
59
60 // For internet mail, generate delivery instructions.
61 // Yes, this is recursive.  Deal with it.  Infinite recursion does
62 // not happen because the delivery instructions message does not
63 // contain a recipient.
64 int smtp_aftersave(struct CtdlMessage *msg, struct recptypes *recps) {
65         if ((recps != NULL) && (recps->num_internet > 0)) {
66                 struct CtdlMessage *imsg = NULL;
67                 char recipient[SIZ];
68                 StrBuf *SpoolMsg = NewStrBuf();
69                 long nTokens;
70                 int i;
71
72                 syslog(LOG_DEBUG, "smtpclient: generating delivery instructions");
73
74                 StrBufPrintf(SpoolMsg,
75                              "Content-type: " SPOOLMIME "\n"
76                              "\n"
77                              "msgid|%s\n"
78                              "submitted|%ld\n" "bounceto|%s\n", msg->cm_fields[eVltMsgNum], (long) time(NULL), recps->bounce_to);
79
80                 if (recps->envelope_from != NULL) {
81                         StrBufAppendBufPlain(SpoolMsg, HKEY("envelope_from|"), 0);
82                         StrBufAppendBufPlain(SpoolMsg, recps->envelope_from, -1, 0);
83                         StrBufAppendBufPlain(SpoolMsg, HKEY("\n"), 0);
84                 }
85                 if (recps->sending_room != NULL) {
86                         StrBufAppendBufPlain(SpoolMsg, HKEY("source_room|"), 0);
87                         StrBufAppendBufPlain(SpoolMsg, recps->sending_room, -1, 0);
88                         StrBufAppendBufPlain(SpoolMsg, HKEY("\n"), 0);
89                 }
90
91                 nTokens = num_tokens(recps->recp_internet, '|');
92                 for (i = 0; i < nTokens; i++) {
93                         long len;
94                         len = extract_token(recipient, recps->recp_internet, i, '|', sizeof recipient);
95                         if (len > 0) {
96                                 StrBufAppendBufPlain(SpoolMsg, HKEY("remote|"), 0);
97                                 StrBufAppendBufPlain(SpoolMsg, recipient, len, 0);
98                                 StrBufAppendBufPlain(SpoolMsg, HKEY("|0||\n"), 0);
99                         }
100                 }
101
102                 imsg = malloc(sizeof(struct CtdlMessage));
103                 memset(imsg, 0, sizeof(struct CtdlMessage));
104                 imsg->cm_magic = CTDLMESSAGE_MAGIC;
105                 imsg->cm_anon_type = MES_NORMAL;
106                 imsg->cm_format_type = FMT_RFC822;
107                 CM_SetField(imsg, eMsgSubject, HKEY("QMSG"));
108                 CM_SetField(imsg, eAuthor, HKEY("Citadel"));
109                 CM_SetField(imsg, eJournal, HKEY("do not journal"));
110                 CM_SetAsFieldSB(imsg, eMesageText, &SpoolMsg);
111                 last_queue_job_submitted = CtdlSubmitMsg(imsg, NULL, SMTP_SPOOLOUT_ROOM);
112                 CM_Free(imsg);
113         }
114         return 0;
115 }
116
117
118 // Callback for smtp_attempt_delivery() to supply libcurl with upload data.
119 static size_t upload_source(void *ptr, size_t size, size_t nmemb, void *userp) {
120         struct smtpmsgsrc *s = (struct smtpmsgsrc *) userp;
121         int sendbytes = 0;
122         const char *send_this = NULL;
123
124         sendbytes = (size * nmemb);
125
126         if (s->bytes_sent >= s->bytes_total) {
127                 return (0);                                     // no data remaining; we are done
128         }
129
130         if (sendbytes > (s->bytes_total - s->bytes_sent)) {
131                 sendbytes = s->bytes_total - s->bytes_sent;     // can't send more than we have
132         }
133
134         send_this = ChrPtr(s->TheMessage);
135         send_this += s->bytes_sent;                             // start where we last left off
136
137         memcpy(ptr, send_this, sendbytes);
138         s->bytes_sent += sendbytes;
139         return(sendbytes);                                      // return the number of bytes _actually_ copied
140 }
141
142
143 // The libcurl API doesn't provide a way to capture the actual SMTP result message returned
144 // by the remote server.  This is an ugly way to extract it, by capturing debug data from
145 // the library and filtering on the lines we want.
146 int ctdl_libcurl_smtp_debug_callback(CURL *handle, curl_infotype type, char *data, size_t size, void *userptr) {
147         if (type != CURLINFO_HEADER_IN)
148                 return 0;
149         if (!userptr)
150                 return 0;
151         char *debugbuf = (char *) userptr;
152
153         int len = strlen(debugbuf);
154         if (len + size > SIZ)
155                 return 0;
156
157         memcpy(&debugbuf[len], data, size);
158         debugbuf[len + size] = 0;
159         return 0;
160 }
161
162
163 // Go through the debug output of an SMTP transaction, and boil it down to just the final success or error response message.
164 void trim_response(long response_code, char *response) {
165         if ((response_code < 100) || (response_code > 999) || (IsEmptyStr(response))) {
166                 return;
167         }
168
169         char *t = malloc(strlen(response));
170         if (!t) {
171                 return;
172         }
173         t[0] = 0;
174
175         char *p;
176         for (p = response; *p != 0; ++p) {
177                 if ( (*p != '\n') && (!isprint(*p)) ) {         // expunge any nonprintables except for newlines
178                         *p = ' ';
179                 }
180         }
181
182         char response_code_str[4];
183         snprintf(response_code_str, sizeof response_code_str, "%ld", response_code);
184         char *respstart = strstr(response, response_code_str);
185         if (respstart == NULL) {                                // If we have a response code but no response text,
186                 strcpy(response, smtpstatus(response_code));    // use one of our canned messages.
187                 return;
188         }
189         strcpy(response, respstart);
190
191         p = strstr(response, "\n");
192         if (p != NULL) {
193                 *p = 0;
194         }
195 }
196
197
198 // Attempt a delivery to one recipient.
199 // Returns a three-digit SMTP status code.
200 int smtp_attempt_delivery(long msgid, char *recp, char *envelope_from, char *source_room, char *response) {
201         struct smtpmsgsrc s;
202         char *fromaddr = NULL;
203         CURL *curl;
204         CURLcode res = CURLE_OK;
205         struct curl_slist *recipients = NULL;
206         long response_code = 421;
207         int num_mx = 0;
208         char mxes[SIZ];
209         char user[1024];
210         char node[1024];
211         char name[1024];
212         char try_this_mx[256];
213         char smtp_url[512];
214         int i;
215
216         syslog(LOG_DEBUG, "smtpclient: smtp_attempt_delivery(%ld, %s)", msgid, recp);
217
218         process_rfc822_addr(recp, user, node, name);    // split recipient address into username, hostname, displayname
219         num_mx = getmx(mxes, node);
220         if (num_mx < 1) {
221                 return (421);
222         }
223
224         CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
225         if (!IsEmptyStr(source_room)) {
226                 // If we have a source room, it's probably a mailing list message; generate an unsubscribe header
227                 char esc_room[ROOMNAMELEN*2];
228                 char esc_email[1024];
229                 urlesc(esc_room, sizeof esc_room, source_room);
230                 urlesc(esc_email, sizeof esc_email, recp);
231                 cprintf("List-Unsubscribe: <http://%s/listsub?cmd=unsubscribe&room=%s&email=%s>\r\n",
232                         CtdlGetConfigStr("c_fqdn"),
233                         esc_room,
234                         esc_email
235                 );
236         }
237         CtdlOutputMsg(msgid, MT_RFC822, HEADERS_ALL, 0, 1, NULL, 0, NULL, &fromaddr, NULL);
238         s.TheMessage = CC->redirect_buffer;
239         s.bytes_total = StrLength(CC->redirect_buffer);
240         s.bytes_sent = 0;
241         CC->redirect_buffer = NULL;
242         response_code = 421;
243                                         // keep trying MXes until one works or we run out
244         for (i = 0; ((i < num_mx) && ((response_code / 100) == 4)); ++i) {
245                 response_code = 421;    // default 421 makes non-protocol errors transient
246                 s.bytes_sent = 0;       // rewind our buffer in case we try multiple MXes
247
248                 curl = curl_easy_init();
249                 if (curl) {
250                         response[0] = 0;
251
252                         if (!IsEmptyStr(envelope_from)) {
253                                 curl_easy_setopt(curl, CURLOPT_MAIL_FROM, envelope_from);
254                         }
255                         else {
256                                 curl_easy_setopt(curl, CURLOPT_MAIL_FROM, fromaddr);
257                         }
258
259                         recipients = curl_slist_append(recipients, recp);
260                         curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
261                         curl_easy_setopt(curl, CURLOPT_READFUNCTION, upload_source);
262                         curl_easy_setopt(curl, CURLOPT_READDATA, &s);
263                         curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);                              // tell libcurl we are uploading
264                         curl_easy_setopt(curl, CURLOPT_TIMEOUT, 20L);                           // Time out after 20 seconds
265                         if (CtdlGetConfigInt("c_smtpclient_disable_starttls") == 0) {
266                                 curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_TRY);        // Attempt STARTTLS if offered
267                         }
268                         curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
269                         curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
270                         curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, ctdl_libcurl_smtp_debug_callback);
271                         curl_easy_setopt(curl, CURLOPT_DEBUGDATA, (void *) response);
272                         curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
273
274                         // Construct an SMTP URL in the form of:
275                         //      smtp[s]://target_host/source_host
276                         // This looks weird but libcurl uses that last part to set our name for EHLO or HELO.
277                         // We check for "smtp://" and "smtps://" because the admin may have put those prefixes in a smart-host entry.
278                         // If there is no prefix we add "smtp://"
279                         extract_token(try_this_mx, mxes, i, '|', (sizeof try_this_mx - 7));
280                         snprintf(smtp_url, sizeof smtp_url,
281                                 "%s%s/%s",
282                                 (((!strncasecmp(try_this_mx, HKEY("smtp://")))
283                                 || (!strncasecmp(try_this_mx, HKEY("smtps://")))) ? "" : "smtp://"),
284                                 try_this_mx, CtdlGetConfigStr("c_fqdn")
285                         );
286                         curl_easy_setopt(curl, CURLOPT_URL, smtp_url);
287                         syslog(LOG_DEBUG, "smtpclient: trying MX %d of %d <%s>", i+1, num_mx, smtp_url);        // send the message
288                         res = curl_easy_perform(curl);
289                         curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
290                         syslog(LOG_DEBUG,
291                                "smtpclient: libcurl returned %d (%s) , SMTP response %ld",
292                                res, curl_easy_strerror(res), response_code
293                         );
294
295                         if ((res != CURLE_OK) && (response_code == 0)) {        // check for errors
296                                 response_code = 421;
297                         }
298
299                         curl_slist_free_all(recipients);
300                         recipients = NULL;                                      // this gets reused; avoid double-free
301                         curl_easy_cleanup(curl);
302                         curl = NULL;                                            // this gets reused; avoid double-free
303
304                         // Trim the error message buffer down to just the actual message
305                         trim_response(response_code, response);
306                 }
307         }
308
309         FreeStrBuf(&s.TheMessage);
310         if (fromaddr) {
311                 free(fromaddr);
312         }
313         return ((int) response_code);
314 }
315
316
317 // Process one outbound message.
318 void smtp_process_one_msg(long qmsgnum) {
319         struct CtdlMessage *msg = NULL;
320         char *instr = NULL;
321         int i;
322         int num_success = 0;
323         int num_fail = 0;
324         int num_delayed = 0;
325         long deletes[2];
326         int delete_this_queue = 0;
327         char server_response[SIZ];
328
329         msg = CtdlFetchMessage(qmsgnum, 1);
330         if (msg == NULL) {
331                 syslog(LOG_WARNING, "smtpclient: %ld does not exist", qmsgnum);
332                 return;
333         }
334
335         instr = msg->cm_fields[eMesageText];
336         msg->cm_fields[eMesageText] = NULL;
337         CM_Free(msg);
338
339         // if the queue message has any CRLF's convert them to LF's
340         char *crlf = NULL;
341         while (crlf = strstr(instr, "\r\n"), crlf != NULL) {
342                 strcpy(crlf, crlf + 1);
343         }
344
345         // Strip out the headers and we are now left with just the instructions.
346         char *soi = strstr(instr, "\n\n");
347         if (soi) {
348                 strcpy(instr, soi + 2);
349         }
350
351         long msgid = 0;
352         time_t submitted = time(NULL);
353         time_t attempted = 0;
354         char *bounceto = NULL;
355         char *envelope_from = NULL;
356         char *source_room = NULL;
357
358         char cfgline[SIZ];
359         for (i = 0; i < num_tokens(instr, '\n'); ++i) {
360                 extract_token(cfgline, instr, i, '\n', sizeof cfgline);
361                 if (!strncasecmp(cfgline, HKEY("msgid|")))
362                         msgid = atol(&cfgline[6]);
363                 if (!strncasecmp(cfgline, HKEY("submitted|")))
364                         submitted = atol(&cfgline[10]);
365                 if (!strncasecmp(cfgline, HKEY("attempted|")))
366                         attempted = atol(&cfgline[10]);
367                 if (!strncasecmp(cfgline, HKEY("bounceto|")))
368                         bounceto = strdup(&cfgline[9]);
369                 if (!strncasecmp(cfgline, HKEY("envelope_from|")))
370                         envelope_from = strdup(&cfgline[14]);
371                 if (!strncasecmp(cfgline, HKEY("source_room|")))
372                         source_room = strdup(&cfgline[12]);
373         }
374
375         int should_try_now = 0;
376         if (attempted < submitted) {                    // If no attempts have been made yet, try now
377                 should_try_now = 1;
378         }
379         else if ((attempted - submitted) <= 14400) {
380                 if ((time(NULL) - attempted) > 1800) {  // First four hours, retry every 30 minutes
381                         should_try_now = 1;
382                 }
383         }
384         else {
385                 if ((time(NULL) - attempted) > 14400) { // After that, retry once every 4 hours
386                         should_try_now = 1;
387                 }
388         }
389
390         if (should_try_now) {
391                 syslog(LOG_DEBUG, "smtpclient: attempting delivery of message <%ld> now", qmsgnum);
392                 if (source_room) {
393                         syslog(LOG_DEBUG, "smtpclient: this message originated in <%s>", source_room);
394                 }
395                 StrBuf *NewInstr = NewStrBuf();
396                 StrBufAppendPrintf(NewInstr, "Content-type: " SPOOLMIME "\n\n");
397                 StrBufAppendPrintf(NewInstr, "msgid|%ld\n", msgid);
398                 StrBufAppendPrintf(NewInstr, "submitted|%ld\n", submitted);
399                 if (bounceto) {
400                         StrBufAppendPrintf(NewInstr, "bounceto|%s\n", bounceto);
401                 }
402                 if (envelope_from) {
403                         StrBufAppendPrintf(NewInstr, "envelope_from|%s\n", envelope_from);
404                 }
405                 for (i = 0; i < num_tokens(instr, '\n'); ++i) {
406                         extract_token(cfgline, instr, i, '\n', sizeof cfgline);
407                         if (!strncasecmp(cfgline, HKEY("remote|"))) {
408                                 char recp[SIZ];
409                                 int previous_result = extract_int(cfgline, 2);
410                                 if ((previous_result == 0) || (previous_result == 4)) {
411                                         int new_result = 421;
412                                         extract_token(recp, cfgline, 1, '|', sizeof recp);
413                                         new_result = smtp_attempt_delivery(msgid, recp, envelope_from, source_room, server_response);
414                                         syslog(LOG_DEBUG, "smtpclient: recp: <%s> , result: %d (%s)", recp, new_result, server_response);
415                                         if ((new_result / 100) == 2) {
416                                                 ++num_success;
417                                         }
418                                         else {
419                                                 if ((new_result / 100) == 5) {
420                                                         ++num_fail;
421                                                 }
422                                                 else {
423                                                         ++num_delayed;
424                                                 }
425                                                 StrBufAppendPrintf(NewInstr, "remote|%s|%ld|%ld (%s)\n", recp, (new_result / 100), new_result, server_response);
426                                         }
427                                 }
428                         }
429                 }
430
431                 StrBufAppendPrintf(NewInstr, "attempted|%ld\n", time(NULL));
432
433                 // All deliveries have now been attempted.  Now determine the disposition of this queue entry.
434
435                 time_t age = time(NULL) - submitted;
436                 syslog(LOG_DEBUG,
437                        "smtpclient: submission age: %ldd%ldh%ldm%lds",
438                        (age / 86400), ((age % 86400) / 3600), ((age % 3600) / 60), (age % 60));
439                 syslog(LOG_DEBUG, "smtpclient: num_success=%d , num_fail=%d , num_delayed=%d", num_success, num_fail, num_delayed);
440
441                 // If there are permanent fails on this attempt, deliver a bounce to the user.
442                 // The 5XX fails will be recorded in the rewritten queue, but they will be removed before the next attempt.
443                 if (num_fail > 0) {
444                         smtp_do_bounce(ChrPtr(NewInstr), SDB_BOUNCE_FATALS);
445                 }
446                 // If all deliveries have either succeeded or failed, we are finished with this queue entry.
447                 if (num_delayed == 0) {
448                         delete_this_queue = 1;
449                 }
450                 // If it's been more than five days, give up and tell the sender that delivery failed
451                 else if ((time(NULL) - submitted) > SMTP_DELIVER_FAIL) {
452                         smtp_do_bounce(ChrPtr(NewInstr), SDB_BOUNCE_ALL);
453                         delete_this_queue = 1;
454                 }
455                 // If it's been more than four hours but less than five days, warn the sender that delivery is delayed
456                 else if (((attempted - submitted) < SMTP_DELIVER_WARN) && ((time(NULL) - submitted) >= SMTP_DELIVER_WARN)) {
457                         smtp_do_bounce(ChrPtr(NewInstr), SDB_WARN);
458                 }
459
460                 if (delete_this_queue) {
461                         syslog(LOG_DEBUG, "smtpclient: %ld deleting", qmsgnum);
462                         deletes[0] = qmsgnum;
463                         deletes[1] = msgid;
464                         CtdlDeleteMessages(SMTP_SPOOLOUT_ROOM, deletes, 2, "");
465                         FreeStrBuf(&NewInstr);  // We have to free NewInstr here, no longer needed
466                 }
467                 else {
468                         // replace the old queue entry with the new one
469                         syslog(LOG_DEBUG, "smtpclient: %ld rewriting", qmsgnum);
470                         msg = convert_internet_message_buf(&NewInstr);  // This function will free NewInstr for us
471                         CtdlSubmitMsg(msg, NULL, SMTP_SPOOLOUT_ROOM);
472                         CM_Free(msg);
473                         CtdlDeleteMessages(SMTP_SPOOLOUT_ROOM, &qmsgnum, 1, "");
474                 }
475         }
476         else {
477                 syslog(LOG_DEBUG, "smtpclient: %ld retry time not reached", qmsgnum);
478         }
479
480         if (bounceto != NULL) {
481                 free(bounceto);
482         }
483         if (envelope_from != NULL) {
484                 free(envelope_from);
485         }
486         if (source_room != NULL) {
487                 free(source_room);
488         }
489         free(instr);
490 }
491
492
493 // Callback for smtp_do_queue()
494 void smtp_add_msg(long msgnum, void *userdata) {
495         Array *smtp_queue = (Array *) userdata;
496         array_append(smtp_queue, &msgnum);
497 }
498
499
500 enum {
501         FULL_QUEUE_RUN,         // try to process the entire queue, including messages that have already been attempted
502         QUICK_QUEUE_RUN         // only process jobs in the queue that have not been tried yet
503 };
504
505
506 // Run through the queue sending out messages.
507 void smtp_do_queue(int type_of_queue_run) {
508         static int doing_smtpclient = 0;
509         static long last_queue_msg_processed = 0;
510         int i = 0;
511
512         // This is a concurrency check to make sure only one smtpclient run is done at a time.
513         begin_critical_section(S_SMTPQUEUE);
514         if (doing_smtpclient) {
515                 end_critical_section(S_SMTPQUEUE);
516                 return;
517         }
518         doing_smtpclient = 1;
519         end_critical_section(S_SMTPQUEUE);
520
521         syslog(LOG_DEBUG, "smtpclient: start queue run , last_queue_msg_processed=%ld , last_queue_job_submitted=%ld", last_queue_msg_processed, last_queue_job_submitted);
522
523         if (CtdlGetRoom(&CC->room, SMTP_SPOOLOUT_ROOM) != 0) {
524                 syslog(LOG_WARNING, "smtpclient: cannot find room <%s>", SMTP_SPOOLOUT_ROOM);
525                 doing_smtpclient = 0;
526                 return;
527         }
528
529         // This array will hold the list of queue instruction messages
530         Array *smtp_queue = array_new(sizeof(long));
531         if (smtp_queue == NULL) {
532                 syslog(LOG_WARNING, "smtpclient: cannot allocate queue array");
533                 doing_smtpclient = 0;
534                 return;
535         }
536
537         // Put the queue in memory so we can close the db cursor
538         CtdlForEachMessage(
539                 (type_of_queue_run == QUICK_QUEUE_RUN ? MSGS_GT : MSGS_ALL),                    // quick = new jobs; full = all jobs
540                 (type_of_queue_run == QUICK_QUEUE_RUN ? last_queue_msg_processed : 0),          // quick = new jobs; full = all jobs
541                 NULL,
542                 SPOOLMIME,              // Searching for Content-type of SPOOLIME will give us only queue instruction messages
543                 NULL,
544                 smtp_add_msg,           // That's our callback function to add a job to the queue
545                 (void *)smtp_queue
546         );
547
548         // We are ready to run through the queue now.
549         for (i = 0; i < array_len(smtp_queue); ++i) {
550                 long m;
551                 memcpy(&m, array_get_element_at(smtp_queue, i), sizeof(long));
552                 smtp_process_one_msg(m);
553                 last_queue_msg_processed = m;
554         }
555
556         array_free(smtp_queue);
557         doing_smtpclient = 0;
558         syslog(LOG_DEBUG, "smtpclient: end queue run , last_queue_msg_processed=%ld , last_queue_job_submitted=%ld", last_queue_msg_processed, last_queue_job_submitted);
559 }
560
561
562 void smtp_do_queue_full(void) {
563         smtp_do_queue(FULL_QUEUE_RUN);
564 }
565
566
567 void smtp_do_queue_quick(void) {
568         smtp_do_queue(QUICK_QUEUE_RUN);
569 }
570
571
572 // Initialization function, called from modules_init.c
573 char *ctdl_module_init_smtpclient(void) {
574         if (!threading) {
575                 CtdlRegisterMessageHook(smtp_aftersave, EVT_AFTERSAVE);
576                 CtdlRegisterSessionHook(smtp_do_queue_full, EVT_TIMER, PRIO_AGGR + 51);
577                 smtp_init_spoolout();
578         }
579
580         // return our module id for the log
581         return "smtpclient";
582 }