SMTP-Client: shut down in one chunk
[citadel.git] / citadel / modules / smtp / serv_smtpeventclient.c
1 /*
2  * This module is an SMTP and ESMTP implementation for the Citadel system.
3  * It is compliant with all of the following:
4  *
5  * RFC  821 - Simple Mail Transfer Protocol
6  * RFC  876 - Survey of SMTP Implementations
7  * RFC 1047 - Duplicate messages and SMTP
8  * RFC 1652 - 8 bit MIME
9  * RFC 1869 - Extended Simple Mail Transfer Protocol
10  * RFC 1870 - SMTP Service Extension for Message Size Declaration
11  * RFC 2033 - Local Mail Transfer Protocol
12  * RFC 2197 - SMTP Service Extension for Command Pipelining
13  * RFC 2476 - Message Submission
14  * RFC 2487 - SMTP Service Extension for Secure SMTP over TLS
15  * RFC 2554 - SMTP Service Extension for Authentication
16  * RFC 2821 - Simple Mail Transfer Protocol
17  * RFC 2822 - Internet Message Format
18  * RFC 2920 - SMTP Service Extension for Command Pipelining
19  *
20  * The VRFY and EXPN commands have been removed from this implementation
21  * because nobody uses these commands anymore, except for spammers.
22  *
23  * Copyright (c) 1998-2012 by the citadel.org team
24  *
25  *  This program is open source software; you can redistribute it and/or modify
26  *  it under the terms of the GNU General Public License version 3.
27  *  
28  *  
29  *
30  *  This program is distributed in the hope that it will be useful,
31  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
32  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33  *  GNU General Public License for more details.
34  *
35  *  
36  *  
37  *  
38  */
39
40 #include "sysdep.h"
41 #include <stdlib.h>
42 #include <unistd.h>
43 #include <stdio.h>
44 #include <termios.h>
45 #include <fcntl.h>
46 #include <signal.h>
47 #include <pwd.h>
48 #include <errno.h>
49 #include <sys/types.h>
50 #include <syslog.h>
51
52 #if TIME_WITH_SYS_TIME
53 # include <sys/time.h>
54 # include <time.h>
55 #else
56 # if HAVE_SYS_TIME_H
57 #  include <sys/time.h>
58 # else
59 #  include <time.h>
60 # endif
61 #endif
62 #include <sys/wait.h>
63 #include <ctype.h>
64 #include <string.h>
65 #include <limits.h>
66 #include <sys/socket.h>
67 #include <netinet/in.h>
68 #include <arpa/inet.h>
69 #include <libcitadel.h>
70 #include "citadel.h"
71 #include "server.h"
72 #include "citserver.h"
73 #include "support.h"
74 #include "config.h"
75 #include "control.h"
76 #include "user_ops.h"
77 #include "database.h"
78 #include "msgbase.h"
79 #include "internet_addressing.h"
80 #include "genstamp.h"
81 #include "domain.h"
82 #include "clientsocket.h"
83 #include "locate_host.h"
84 #include "citadel_dirs.h"
85
86 #include "ctdl_module.h"
87
88 #include "smtp_util.h"
89 #include "event_client.h"
90 #include "smtpqueue.h"
91 #include "smtp_clienthandlers.h"
92
93 int SMTPClientDebugEnabled = 0;
94 const unsigned short DefaultMXPort = 25;
95 void DeleteSmtpOutMsg(void *v)
96 {
97         SmtpOutMsg *Msg = v;
98         AsyncIO *IO = &Msg->IO;
99         EVS_syslog(LOG_DEBUG, "%s Exit\n", __FUNCTION__);
100
101         /* these are kept in our own space and free'd below */
102         Msg->IO.ConnectMe = NULL;
103
104         ares_free_data(Msg->AllMX);
105         if (Msg->HostLookup.VParsedDNSReply != NULL)
106                 Msg->HostLookup.DNSReplyFree(Msg->HostLookup.VParsedDNSReply);
107         FreeURL(&Msg->Relay);
108         FreeStrBuf(&Msg->msgtext);
109         FreeAsyncIOContents(&Msg->IO);
110         memset (Msg, 0, sizeof(SmtpOutMsg)); /* just to be shure... */
111         free(Msg);
112 }
113
114 eNextState SMTP_C_Shutdown(AsyncIO *IO);
115 eNextState SMTP_C_Timeout(AsyncIO *IO);
116 eNextState SMTP_C_ConnFail(AsyncIO *IO);
117 eNextState SMTP_C_DispatchReadDone(AsyncIO *IO);
118 eNextState SMTP_C_DispatchWriteDone(AsyncIO *IO);
119 eNextState SMTP_C_DNSFail(AsyncIO *IO);
120 eNextState SMTP_C_Terminate(AsyncIO *IO);
121 eNextState SMTP_C_TerminateDB(AsyncIO *IO);
122 eReadState SMTP_C_ReadServerStatus(AsyncIO *IO);
123
124 eNextState mx_connect_ip(AsyncIO *IO);
125 eNextState get_one_mx_host_ip(AsyncIO *IO);
126
127 /******************************************************************************
128  * So, we're finished with sending (regardless of success or failure)         *
129  * This Message might be referenced by several Queue-Items, if we're the last,*
130  * we need to free the memory and send bounce messages (on terminal failure)  *
131  * else we just free our SMTP-Message struct.                                 *
132  ******************************************************************************/
133 eNextState FinalizeMessageSend_DB(AsyncIO *IO)
134 {
135         const char *Status;
136         SmtpOutMsg *Msg = IO->Data;
137         
138         if (Msg->MyQEntry->Status == 2) 
139                 Status = "Delivery successful.";
140         else if (Msg->MyQEntry->Status == 5) 
141                 Status = "Delivery failed permanently; giving up.";
142         else
143                 Status = "Delivery failed temporarily; will retry later.";
144                         
145         EVS_syslog(LOG_INFO,
146                    "%s Time[%fs] Recipient <%s> @ <%s> (%s) Status message: %s\n",
147                    Status,
148                    Msg->IO.Now - Msg->IO.StartIO,
149                    Msg->user,
150                    Msg->node,
151                    Msg->name,
152                    ChrPtr(Msg->MyQEntry->StatusMessage));
153
154
155         Msg->IDestructQueItem = DecreaseQReference(Msg->MyQItem);
156
157         Msg->nRemain = CountActiveQueueEntries(Msg->MyQItem);
158
159         if (Msg->MyQEntry->Active && 
160             CheckQEntryIsBounce(Msg->MyQEntry))
161         {
162                 /* are we casue for a bounce mail? */
163                 Msg->MyQItem->SendBounceMail |= (1<<Msg->MyQEntry->Status);
164         }
165
166         if ((Msg->nRemain > 0) || Msg->IDestructQueItem)
167                 Msg->QMsgData = SerializeQueueItem(Msg->MyQItem);
168         else
169                 Msg->QMsgData = NULL;
170
171         /*
172          * Uncompleted delivery instructions remain, so delete the old
173          * instructions and replace with the updated ones.
174          */
175         EVS_syslog(LOG_DEBUG, "%ld", Msg->MyQItem->QueMsgID);
176         CtdlDeleteMessages(SMTP_SPOOLOUT_ROOM, &Msg->MyQItem->QueMsgID, 1, "");
177         Msg->MyQItem->QueMsgID = -1;
178
179         if (Msg->IDestructQueItem)
180                 smtpq_do_bounce(Msg->MyQItem, Msg->msgtext);
181
182         if (Msg->nRemain > 0)
183         {
184                 struct CtdlMessage *msg;
185                 msg = malloc(sizeof(struct CtdlMessage));
186                 memset(msg, 0, sizeof(struct CtdlMessage));
187                 msg->cm_magic = CTDLMESSAGE_MAGIC;
188                 msg->cm_anon_type = MES_NORMAL;
189                 msg->cm_format_type = FMT_RFC822;
190                 msg->cm_fields['M'] = SmashStrBuf(&Msg->QMsgData);
191                 msg->cm_fields['U'] = strdup("QMSG");
192                 Msg->MyQItem->QueMsgID =
193                         CtdlSubmitMsg(msg, NULL, SMTP_SPOOLOUT_ROOM, QP_EADDR);
194                 EVS_syslog(LOG_DEBUG, "%ld", Msg->MyQItem->QueMsgID);
195                 CtdlFreeMessage(msg);
196         }
197         else {
198                 CtdlDeleteMessages(SMTP_SPOOLOUT_ROOM,
199                                    &Msg->MyQItem->MessageID,
200                                    1,
201                                    "");
202                 FreeStrBuf(&Msg->QMsgData);
203         }
204
205         RemoveContext(Msg->IO.CitContext);
206         if (Msg->IDestructQueItem)
207                 RemoveQItem(Msg->MyQItem);
208         return eAbort;
209 }
210
211 eNextState Terminate(AsyncIO *IO)
212 {
213         SmtpOutMsg *Msg = IO->Data;
214         DeleteSmtpOutMsg(Msg);
215         return eAbort;
216 }
217 eNextState FinalizeMessageSend(SmtpOutMsg *Msg)
218 {
219         /* hand over to DB Queue */
220         return QueueDBOperation(&Msg->IO, FinalizeMessageSend_DB);
221 }
222
223 eNextState FailOneAttempt(AsyncIO *IO)
224 {
225         SmtpOutMsg *Msg = IO->Data;
226
227         if (Msg->MyQEntry->Status == 2)
228                 return eAbort;
229
230         /*
231          * possible ways here:
232          * - connection timeout
233          * - dns lookup failed
234          */
235         StopClientWatchers(IO);
236
237         if (Msg->pCurrRelay != NULL)
238                 Msg->pCurrRelay = Msg->pCurrRelay->Next;
239
240         if (Msg->pCurrRelay == NULL) {
241                 EVS_syslog(LOG_DEBUG, "%s Aborting\n", __FUNCTION__);
242                 return eAbort;
243         }
244         if (Msg->pCurrRelay->IsIP) {
245                 EVS_syslog(LOG_DEBUG, "%s connecting IP\n", __FUNCTION__);
246                 return mx_connect_ip(IO);
247         }
248         else {
249                 EVS_syslog(LOG_DEBUG,
250                            "%s resolving next MX Record\n",
251                            __FUNCTION__);
252                 return get_one_mx_host_ip(IO);
253         }
254 }
255
256
257 void SetConnectStatus(AsyncIO *IO)
258 {
259         SmtpOutMsg *Msg = IO->Data;
260         char buf[256];
261         void *src;
262
263         buf[0] = '\0';
264
265         if (IO->ConnectMe->IPv6) {
266                 src = &IO->ConnectMe->Addr.sin6_addr;
267         }
268         else {
269                 struct sockaddr_in *addr;
270
271                 addr = (struct sockaddr_in *)&IO->ConnectMe->Addr;
272                 src = &addr->sin_addr.s_addr;
273         }
274
275         inet_ntop((IO->ConnectMe->IPv6)?AF_INET6:AF_INET,
276                   src,
277                   buf,
278                   sizeof(buf));
279
280         if (Msg->mx_host == NULL)
281                 Msg->mx_host = "<no MX-Record>";
282
283         EVS_syslog(LOG_INFO,
284                   "connecting to %s [%s]:%d ...\n",
285                   Msg->mx_host,
286                   buf,
287                   Msg->IO.ConnectMe->Port);
288
289         Msg->MyQEntry->Status = 4;
290         StrBufPrintf(Msg->MyQEntry->StatusMessage,
291                      "Timeout while connecting %s [%s]:%d ",
292                      Msg->mx_host,
293                      buf,
294                      Msg->IO.ConnectMe->Port);
295         Msg->IO.NextState = eConnect;
296 }
297
298 /*****************************************************************************
299  * So we connect our Relay IP here.                                          *
300  *****************************************************************************/
301 eNextState mx_connect_ip(AsyncIO *IO)
302 {
303         SmtpOutMsg *Msg = IO->Data;
304
305         EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
306
307         IO->ConnectMe = Msg->pCurrRelay;
308         Msg->State = eConnectMX;
309
310         SetConnectStatus(IO);
311
312         return EvConnectSock(IO,
313                              SMTP_C_ConnTimeout,
314                              SMTP_C_ReadTimeouts[0],
315                              1);
316 }
317
318 eNextState get_one_mx_host_ip_done(AsyncIO *IO)
319 {
320         SmtpOutMsg *Msg = IO->Data;
321         struct hostent *hostent;
322
323         QueryCbDone(IO);
324         EVS_syslog(LOG_DEBUG, "%s Time[%fs]\n",
325                    __FUNCTION__,
326                    IO->Now - IO->DNS.Start);
327
328         hostent = Msg->HostLookup.VParsedDNSReply;
329         if ((Msg->HostLookup.DNSStatus == ARES_SUCCESS) &&
330             (hostent != NULL) ) {
331                 memset(&Msg->pCurrRelay->Addr, 0, sizeof(struct in6_addr));
332                 if (Msg->pCurrRelay->IPv6) {
333                         memcpy(&Msg->pCurrRelay->Addr.sin6_addr.s6_addr,
334                                &hostent->h_addr_list[0],
335                                sizeof(struct in6_addr));
336
337                         Msg->pCurrRelay->Addr.sin6_family =
338                                 hostent->h_addrtype;
339                         Msg->pCurrRelay->Addr.sin6_port =
340                                 htons(DefaultMXPort);
341                 }
342                 else {
343                         struct sockaddr_in *addr;
344                         /*
345                          * Bypass the ns lookup result like this:
346                          * IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1");
347                          * addr->sin_addr.s_addr =
348                          *   htonl((uint32_t)&hostent->h_addr_list[0]);
349                          */
350
351                         addr = (struct sockaddr_in*) &Msg->pCurrRelay->Addr;
352
353                         memcpy(&addr->sin_addr.s_addr,
354                                hostent->h_addr_list[0],
355                                sizeof(uint32_t));
356
357                         addr->sin_family = hostent->h_addrtype;
358                         addr->sin_port   = htons(DefaultMXPort);
359                 }
360                 Msg->mx_host = Msg->pCurrRelay->Host;
361                 if (Msg->HostLookup.VParsedDNSReply != NULL) {
362                         Msg->HostLookup.DNSReplyFree(Msg->HostLookup.VParsedDNSReply);
363                         Msg->HostLookup.VParsedDNSReply = NULL;
364                 }
365                 return mx_connect_ip(IO);
366         }
367         else {
368                 if (Msg->HostLookup.VParsedDNSReply != NULL) {
369                         Msg->HostLookup.DNSReplyFree(Msg->HostLookup.VParsedDNSReply);
370                         Msg->HostLookup.VParsedDNSReply = NULL;
371                 }
372                 return FailOneAttempt(IO);
373         }
374 }
375
376 eNextState get_one_mx_host_ip(AsyncIO *IO)
377 {
378         SmtpOutMsg * Msg = IO->Data;
379         /*
380          * here we start with the lookup of one host. it might be...
381          * - the relay host *sigh*
382          * - the direct hostname if there was no mx record
383          * - one of the mx'es
384          */
385
386         EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
387
388         EVS_syslog(LOG_DEBUG,
389                   "looking up %s-Record %s : %d ...\n",
390                   (Msg->pCurrRelay->IPv6)? "aaaa": "a",
391                   Msg->pCurrRelay->Host,
392                   Msg->pCurrRelay->Port);
393
394         if (!QueueQuery((Msg->pCurrRelay->IPv6)? ns_t_aaaa : ns_t_a,
395                         Msg->pCurrRelay->Host,
396                         &Msg->IO,
397                         &Msg->HostLookup,
398                         get_one_mx_host_ip_done))
399         {
400                 Msg->MyQEntry->Status = 5;
401                 StrBufPrintf(Msg->MyQEntry->StatusMessage,
402                              "No MX hosts found for <%s>", Msg->node);
403                 Msg->IO.NextState = eTerminateConnection;
404                 return IO->NextState;
405         }
406         IO->NextState = eReadDNSReply;
407         return IO->NextState;
408 }
409
410
411 /*****************************************************************************
412  * here we try to find out about the MX records for our recipients.          *
413  *****************************************************************************/
414 eNextState smtp_resolve_mx_record_done(AsyncIO *IO)
415 {
416         SmtpOutMsg * Msg = IO->Data;
417         ParsedURL **pp;
418
419         QueryCbDone(IO);
420
421         EVS_syslog(LOG_DEBUG, "%s Time[%fs]\n",
422                    __FUNCTION__,
423                    IO->Now - IO->DNS.Start);
424
425         pp = &Msg->Relay;
426         while ((pp != NULL) && (*pp != NULL) && ((*pp)->Next != NULL))
427                 pp = &(*pp)->Next;
428
429         if ((IO->DNS.Query->DNSStatus == ARES_SUCCESS) &&
430             (IO->DNS.Query->VParsedDNSReply != NULL))
431         { /* ok, we found mx records. */
432
433                 Msg->CurrMX
434                         = Msg->AllMX
435                         = IO->DNS.Query->VParsedDNSReply;
436                 while (Msg->CurrMX) {
437                         int i;
438                         for (i = 0; i < 2; i++) {
439                                 ParsedURL *p;
440
441                                 p = (ParsedURL*) malloc(sizeof(ParsedURL));
442                                 memset(p, 0, sizeof(ParsedURL));
443                                 p->Priority = Msg->CurrMX->priority;
444                                 p->IsIP = 0;
445                                 p->Port = DefaultMXPort;
446                                 p->IPv6 = i == 1;
447                                 p->Host = Msg->CurrMX->host;
448                                 if (*pp == NULL)
449                                         *pp = p;
450                                 else {
451                                         ParsedURL *ppp = *pp;
452
453                                         while ((ppp->Next != NULL) &&
454                                                (ppp->Next->Priority <= p->Priority))
455                                                ppp = ppp->Next;
456                                         if ((ppp == *pp) &&
457                                             (ppp->Priority > p->Priority)) {
458                                                 p->Next = *pp;
459                                                 *pp = p;
460                                         }
461                                         else {
462                                                 p->Next = ppp->Next;
463                                                 ppp->Next = p;
464                                         }
465                                 }
466                         }
467                         Msg->CurrMX    = Msg->CurrMX->next;
468                 }
469                 Msg->CXFlags   = Msg->CXFlags & F_HAVE_MX;
470         }
471         else { /* else fall back to the plain hostname */
472                 int i;
473                 for (i = 0; i < 2; i++) {
474                         ParsedURL *p;
475
476                         p = (ParsedURL*) malloc(sizeof(ParsedURL));
477                         memset(p, 0, sizeof(ParsedURL));
478                         p->IsIP = 0;
479                         p->Port = DefaultMXPort;
480                         p->IPv6 = i == 1;
481                         p->Host = Msg->node;
482
483                         *pp = p;
484                         pp = &p->Next;
485                 }
486                 Msg->CXFlags   = Msg->CXFlags & F_DIRECT;
487         }
488         if (Msg->MyQItem->FallBackHost != NULL)
489         {
490                 Msg->MyQItem->FallBackHost->Next = *pp;
491                 *pp = Msg->MyQItem->FallBackHost;
492         }
493         Msg->pCurrRelay = Msg->Relay;
494         return get_one_mx_host_ip(IO);
495 }
496
497 eNextState resolve_mx_records(AsyncIO *IO)
498 {
499         SmtpOutMsg * Msg = IO->Data;
500
501         EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
502         /* start resolving MX records here. */
503         if (!QueueQuery(ns_t_mx,
504                         Msg->node,
505                         &Msg->IO,
506                         &Msg->MxLookup,
507                         smtp_resolve_mx_record_done))
508         {
509                 Msg->MyQEntry->Status = 5;
510                 StrBufPrintf(Msg->MyQEntry->StatusMessage,
511                              "No MX hosts found for <%s>", Msg->node);
512                 return IO->NextState;
513         }
514         Msg->IO.NextState = eReadDNSReply;
515         return IO->NextState;
516 }
517
518
519
520 /******************************************************************************
521  *  so, we're going to start a SMTP delivery.  lets get it on.                *
522  ******************************************************************************/
523
524 SmtpOutMsg *new_smtp_outmsg(OneQueItem *MyQItem,
525                             MailQEntry *MyQEntry,
526                             int MsgCount)
527 {
528         SmtpOutMsg * Msg;
529
530         Msg = (SmtpOutMsg *) malloc(sizeof(SmtpOutMsg));
531         memset(Msg, 0, sizeof(SmtpOutMsg));
532
533         Msg->n                = MsgCount;
534         Msg->MyQEntry         = MyQEntry;
535         Msg->MyQItem          = MyQItem;
536         Msg->pCurrRelay       = MyQItem->URL;
537
538         InitIOStruct(&Msg->IO,
539                      Msg,
540                      eReadMessage,
541                      SMTP_C_ReadServerStatus,
542                      SMTP_C_DNSFail,
543                      SMTP_C_DispatchWriteDone,
544                      SMTP_C_DispatchReadDone,
545                      SMTP_C_Terminate,
546                      SMTP_C_TerminateDB,
547                      SMTP_C_ConnFail,
548                      SMTP_C_Timeout,
549                      SMTP_C_Shutdown);
550
551         Msg->IO.ErrMsg = Msg->MyQEntry->StatusMessage;
552
553         return Msg;
554 }
555
556 void smtp_try_one_queue_entry(OneQueItem *MyQItem,
557                               MailQEntry *MyQEntry,
558                               StrBuf *MsgText,
559                         /*KeepMsgText allows us to use MsgText as ours.*/
560                               int KeepMsgText,
561                               int MsgCount)
562 {
563         SmtpOutMsg *Msg;
564
565         SMTPC_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
566
567         Msg = new_smtp_outmsg(MyQItem, MyQEntry, MsgCount);
568         if (KeepMsgText) Msg->msgtext = MsgText;
569         else             Msg->msgtext = NewStrBufDup(MsgText);
570
571         if (smtp_resolve_recipients(Msg)) {
572
573                 safestrncpy(
574                         ((CitContext *)Msg->IO.CitContext)->cs_host,
575                         Msg->node,
576                         sizeof(((CitContext *)
577                                 Msg->IO.CitContext)->cs_host));
578
579                 SMTPC_syslog(LOG_DEBUG, "Starting: [%ld] <%s> CC <%d> \n",
580                              Msg->MyQItem->MessageID,
581                              ChrPtr(Msg->MyQEntry->Recipient),
582                              ((CitContext*)Msg->IO.CitContext)->cs_pid);
583                 if (Msg->pCurrRelay == NULL)
584                         QueueEventContext(&Msg->IO,
585                                           resolve_mx_records);
586                 else { /* oh... via relay host */
587                         if (Msg->pCurrRelay->IsIP) {
588                                 QueueEventContext(&Msg->IO,
589                                                   mx_connect_ip);
590                         }
591                         else {
592                                 /* uneducated admin has chosen to
593                                    add DNS to the equation... */
594                                 QueueEventContext(&Msg->IO,
595                                                   get_one_mx_host_ip);
596                         }
597                 }
598         }
599         else {
600                 /* No recipients? well fail then. */
601                 if ((Msg==NULL) ||
602                     (Msg->MyQEntry == NULL)) {
603                         Msg->MyQEntry->Status = 5;
604                         StrBufPlain(Msg->MyQEntry->StatusMessage,
605                                     HKEY("Invalid Recipient!"));
606                 }
607                 FinalizeMessageSend_DB(&Msg->IO);
608                 DeleteSmtpOutMsg(&Msg->IO);
609         }
610 }
611
612
613
614
615
616
617 /*****************************************************************************/
618 /*                     SMTP CLIENT DISPATCHER                                */
619 /*****************************************************************************/
620
621 void SMTPSetTimeout(eNextState NextTCPState, SmtpOutMsg *Msg)
622 {
623         double Timeout = 0.0;
624         AsyncIO *IO = &Msg->IO;
625
626         EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
627
628         switch (NextTCPState) {
629         case eSendFile:
630         case eSendReply:
631         case eSendMore:
632                 Timeout = SMTP_C_SendTimeouts[Msg->State];
633                 if (Msg->State == eDATABody) {
634                         /* if we're sending a huge message,
635                          * we need more time.
636                          */
637                         Timeout += StrLength(Msg->msgtext) / 512;
638                 }
639                 break;
640         case eReadMessage:
641                 Timeout = SMTP_C_ReadTimeouts[Msg->State];
642                 if (Msg->State == eDATATerminateBody) {
643                         /*
644                          * some mailservers take a nap before accepting
645                          * the message content inspection and such.
646                          */
647                         Timeout += StrLength(Msg->msgtext) / 512;
648                 }
649                 break;
650         case eSendDNSQuery:
651         case eReadDNSReply:
652         case eDBQuery:
653         case eReadFile:
654         case eReadMore:
655         case eReadPayload:
656         case eConnect:
657         case eTerminateConnection:
658         case eAbort:
659                 return;
660         }
661         SetNextTimeout(&Msg->IO, Timeout);
662 }
663 eNextState SMTP_C_DispatchReadDone(AsyncIO *IO)
664 {
665         EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
666         SmtpOutMsg *Msg = IO->Data;
667         eNextState rc;
668
669         rc = ReadHandlers[Msg->State](Msg);
670         if (rc != eAbort)
671         {
672                 Msg->State++;
673                 SMTPSetTimeout(rc, Msg);
674         }
675         return rc;
676 }
677 eNextState SMTP_C_DispatchWriteDone(AsyncIO *IO)
678 {
679         EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
680         SmtpOutMsg *Msg = IO->Data;
681         eNextState rc;
682
683         rc = SendHandlers[Msg->State](Msg);
684         SMTPSetTimeout(rc, Msg);
685         return rc;
686 }
687
688
689 /*****************************************************************************/
690 /*                     SMTP CLIENT ERROR CATCHERS                            */
691 /*****************************************************************************/
692 eNextState SMTP_C_Terminate(AsyncIO *IO)
693 {
694         SmtpOutMsg *Msg = IO->Data;
695
696         EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
697         return FinalizeMessageSend(Msg);
698 }
699 eNextState SMTP_C_TerminateDB(AsyncIO *IO)
700 {
701         EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
702         return Terminate(IO);
703 }
704 eNextState SMTP_C_Timeout(AsyncIO *IO)
705 {
706         SmtpOutMsg *Msg = IO->Data;
707
708         Msg->MyQEntry->Status = 4;
709         EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
710         StrBufPlain(IO->ErrMsg, CKEY(ReadErrors[Msg->State]));
711         if (Msg->State > eRCPT)
712                 return eAbort;
713         else
714                 return FailOneAttempt(IO);
715 }
716 eNextState SMTP_C_ConnFail(AsyncIO *IO)
717 {
718         SmtpOutMsg *Msg = IO->Data;
719
720         Msg->MyQEntry->Status = 4;
721         EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
722         StrBufPlain(IO->ErrMsg, CKEY(ReadErrors[Msg->State]));
723         return FailOneAttempt(IO);
724 }
725 eNextState SMTP_C_DNSFail(AsyncIO *IO)
726 {
727         SmtpOutMsg *Msg = IO->Data;
728         Msg->MyQEntry->Status = 4;
729         EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
730         return FailOneAttempt(IO);
731 }
732 eNextState SMTP_C_Shutdown(AsyncIO *IO)
733 {
734         EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
735         SmtpOutMsg *Msg = IO->Data;
736
737         Msg->MyQEntry->Status = 3;
738         StrBufPlain(Msg->MyQEntry->StatusMessage,
739                     HKEY("server shutdown during message submit."));
740         return FinalizeMessageSend(Msg);
741 }
742
743
744 /**
745  * @brief lineread Handler;
746  * understands when to read more SMTP lines, and when this is a one-lined reply.
747  */
748 eReadState SMTP_C_ReadServerStatus(AsyncIO *IO)
749 {
750         eReadState Finished = eBufferNotEmpty;
751
752         while (Finished == eBufferNotEmpty) {
753                 Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf);
754
755                 switch (Finished) {
756                 case eMustReadMore: /// read new from socket...
757                         return Finished;
758                         break;
759                 case eBufferNotEmpty: /* shouldn't happen... */
760                 case eReadSuccess: /// done for now...
761                         if (StrLength(IO->IOBuf) < 4)
762                                 continue;
763                         if (ChrPtr(IO->IOBuf)[3] == '-')
764                                 Finished = eBufferNotEmpty;
765                         else
766                                 return Finished;
767                         break;
768                 case eReadFail: /// WHUT?
769                         ///todo: shut down!
770                         break;
771                 }
772         }
773         return Finished;
774 }
775
776 void LogDebugEnableSMTPClient(const int n)
777 {
778         SMTPClientDebugEnabled = n;
779 }
780
781 CTDL_MODULE_INIT(smtp_eventclient)
782 {
783         if (!threading)
784                 CtdlRegisterDebugFlagHook(HKEY("smtpeventclient"), LogDebugEnableSMTPClient, &SMTPClientDebugEnabled);
785         return "smtpeventclient";
786 }