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