Re-Sort Cleanup of event io on shutdown
[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 const unsigned short DefaultMXPort = 25;
94 void DeleteSmtpOutMsg(void *v)
95 {
96         SmtpOutMsg *Msg = v;
97         AsyncIO *IO = &Msg->IO;
98         EV_syslog(LOG_DEBUG, "SMTP: %s Aborting\n", __FUNCTION__);
99
100         /* these are kept in our own space and free'd below */
101         Msg->IO.ConnectMe = NULL;
102
103         ares_free_data(Msg->AllMX);
104         if (Msg->HostLookup.VParsedDNSReply != NULL)
105                 Msg->HostLookup.DNSReplyFree(Msg->HostLookup.VParsedDNSReply);
106         FreeURL(&Msg->Relay);
107         FreeStrBuf(&Msg->msgtext);
108         FreeAsyncIOContents(&Msg->IO);
109         memset (Msg, 0, sizeof(SmtpOutMsg)); /* just to be shure... */
110         free(Msg);
111 }
112
113 eNextState SMTP_C_Shutdown(AsyncIO *IO);
114 eNextState SMTP_C_Timeout(AsyncIO *IO);
115 eNextState SMTP_C_ConnFail(AsyncIO *IO);
116 eNextState SMTP_C_DispatchReadDone(AsyncIO *IO);
117 eNextState SMTP_C_DispatchWriteDone(AsyncIO *IO);
118 eNextState SMTP_C_DNSFail(AsyncIO *IO);
119 eNextState SMTP_C_Terminate(AsyncIO *IO);
120 eReadState SMTP_C_ReadServerStatus(AsyncIO *IO);
121
122 eNextState mx_connect_ip(AsyncIO *IO);
123 eNextState get_one_mx_host_ip(AsyncIO *IO);
124
125 /******************************************************************************
126  * So, we're finished with sending (regardless of success or failure)         *
127  * This Message might be referenced by several Queue-Items, if we're the last,*
128  * we need to free the memory and send bounce messages (on terminal failure)  *
129  * else we just free our SMTP-Message struct.                                 *
130  ******************************************************************************/
131 void FinalizeMessageSend(SmtpOutMsg *Msg)
132 {
133         int IDestructQueItem;
134         int nRemain;
135         StrBuf *MsgData;
136         AsyncIO *IO = &Msg->IO;
137
138         IDestructQueItem = DecreaseQReference(Msg->MyQItem);
139
140         nRemain = CountActiveQueueEntries(Msg->MyQItem);
141
142         if ((nRemain > 0) || IDestructQueItem)
143                 MsgData = SerializeQueueItem(Msg->MyQItem);
144         else
145                 MsgData = NULL;
146
147         /*
148          * Uncompleted delivery instructions remain, so delete the old
149          * instructions and replace with the updated ones.
150          */
151         EVS_syslog(LOG_DEBUG, "SMTPQD: %ld", Msg->MyQItem->QueMsgID);
152         CtdlDeleteMessages(SMTP_SPOOLOUT_ROOM, &Msg->MyQItem->QueMsgID, 1, "");
153
154         if (IDestructQueItem)
155                 smtpq_do_bounce(Msg->MyQItem, Msg->msgtext);
156
157         if (nRemain > 0)
158         {
159                 struct CtdlMessage *msg;
160                 msg = malloc(sizeof(struct CtdlMessage));
161                 memset(msg, 0, sizeof(struct CtdlMessage));
162                 msg->cm_magic = CTDLMESSAGE_MAGIC;
163                 msg->cm_anon_type = MES_NORMAL;
164                 msg->cm_format_type = FMT_RFC822;
165                 msg->cm_fields['M'] = SmashStrBuf(&MsgData);
166                 msg->cm_fields['U'] = strdup("QMSG");
167                 Msg->MyQItem->QueMsgID =
168                         CtdlSubmitMsg(msg, NULL, SMTP_SPOOLOUT_ROOM, QP_EADDR);
169                 EVS_syslog(LOG_DEBUG, "SMTPQ: %ld", Msg->MyQItem->QueMsgID);
170                 CtdlFreeMessage(msg);
171         }
172         else {
173                 CtdlDeleteMessages(SMTP_SPOOLOUT_ROOM,
174                                    &Msg->MyQItem->MessageID,
175                                    1,
176                                    "");
177                 FreeStrBuf(&MsgData);
178         }
179         RemoveContext(Msg->IO.CitContext);
180         if (IDestructQueItem)
181                 RemoveQItem(Msg->MyQItem);
182         DeleteSmtpOutMsg(Msg);
183 }
184
185 eNextState FailOneAttempt(AsyncIO *IO)
186 {
187         SmtpOutMsg *Msg = IO->Data;
188
189         if (Msg->MyQEntry->Status == 2)
190                 return eAbort;
191
192         /*
193          * possible ways here:
194          * - connection timeout
195          * - dns lookup failed
196          */
197         StopClientWatchers(IO);
198
199         if (Msg->pCurrRelay != NULL)
200                 Msg->pCurrRelay = Msg->pCurrRelay->Next;
201
202         if (Msg->pCurrRelay == NULL) {
203                 EVS_syslog(LOG_DEBUG, "SMTP: %s Aborting\n", __FUNCTION__);
204                 return eAbort;
205         }
206         if (Msg->pCurrRelay->IsIP) {
207                 EVS_syslog(LOG_DEBUG, "SMTP: %s connecting IP\n", __FUNCTION__);
208                 return mx_connect_ip(IO);
209         }
210         else {
211                 EVS_syslog(LOG_DEBUG,
212                            "SMTP: %s resolving next MX Record\n",
213                            __FUNCTION__);
214                 return get_one_mx_host_ip(IO);
215         }
216 }
217
218
219 void SetConnectStatus(AsyncIO *IO)
220 {
221         SmtpOutMsg *Msg = IO->Data;
222         char buf[256];
223         void *src;
224
225         buf[0] = '\0';
226
227         if (IO->ConnectMe->IPv6) {
228                 src = &IO->ConnectMe->Addr.sin6_addr;
229         }
230         else {
231                 struct sockaddr_in *addr;
232
233                 addr = (struct sockaddr_in *)&IO->ConnectMe->Addr;
234                 src = &addr->sin_addr.s_addr;
235         }
236
237         inet_ntop((IO->ConnectMe->IPv6)?AF_INET6:AF_INET,
238                   src,
239                   buf,
240                   sizeof(buf));
241
242         if (Msg->mx_host == NULL)
243                 Msg->mx_host = "<no MX-Record>";
244
245         EVS_syslog(LOG_DEBUG,
246                   "SMTP client[%ld]: connecting to %s [%s]:%d ...\n",
247                   Msg->n,
248                   Msg->mx_host,
249                   buf,
250                   Msg->IO.ConnectMe->Port);
251
252         Msg->MyQEntry->Status = 5;
253         StrBufPrintf(Msg->MyQEntry->StatusMessage,
254                      "Timeout while connecting %s [%s]:%d ",
255                      Msg->mx_host,
256                      buf,
257                      Msg->IO.ConnectMe->Port);
258         Msg->IO.NextState = eConnect;
259 }
260
261 /*****************************************************************************
262  * So we connect our Relay IP here.                                          *
263  *****************************************************************************/
264 eNextState mx_connect_ip(AsyncIO *IO)
265 {
266         SmtpOutMsg *Msg = IO->Data;
267
268         EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
269
270         IO->ConnectMe = Msg->pCurrRelay;
271         Msg->State = eConnectMX;
272
273         SetConnectStatus(IO);
274
275         return EvConnectSock(IO,
276                              SMTP_C_ConnTimeout,
277                              SMTP_C_ReadTimeouts[0],
278                              1);
279 }
280
281 eNextState get_one_mx_host_ip_done(AsyncIO *IO)
282 {
283         SmtpOutMsg *Msg = IO->Data;
284         struct hostent *hostent;
285
286         QueryCbDone(IO);
287
288         hostent = Msg->HostLookup.VParsedDNSReply;
289         if ((Msg->HostLookup.DNSStatus == ARES_SUCCESS) &&
290             (hostent != NULL) ) {
291                 memset(&Msg->pCurrRelay->Addr, 0, sizeof(struct in6_addr));
292                 if (Msg->pCurrRelay->IPv6) {
293                         memcpy(&Msg->pCurrRelay->Addr.sin6_addr.s6_addr,
294                                &hostent->h_addr_list[0],
295                                sizeof(struct in6_addr));
296
297                         Msg->pCurrRelay->Addr.sin6_family =
298                                 hostent->h_addrtype;
299                         Msg->pCurrRelay->Addr.sin6_port =
300                                 htons(DefaultMXPort);
301                 }
302                 else {
303                         struct sockaddr_in *addr;
304                         /*
305                          * Bypass the ns lookup result like this:
306                          * IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1");
307                          * addr->sin_addr.s_addr =
308                          *   htonl((uint32_t)&hostent->h_addr_list[0]);
309                          */
310
311                         addr = (struct sockaddr_in*) &Msg->pCurrRelay->Addr;
312
313                         memcpy(&addr->sin_addr.s_addr,
314                                hostent->h_addr_list[0],
315                                sizeof(uint32_t));
316
317                         addr->sin_family = hostent->h_addrtype;
318                         addr->sin_port   = htons(DefaultMXPort);
319                 }
320                 Msg->mx_host = Msg->pCurrRelay->Host;
321                 return mx_connect_ip(IO);
322         }
323         else
324                 return FailOneAttempt(IO);
325 }
326
327 eNextState get_one_mx_host_ip(AsyncIO *IO)
328 {
329         SmtpOutMsg * Msg = IO->Data;
330         /*
331          * here we start with the lookup of one host. it might be...
332          * - the relay host *sigh*
333          * - the direct hostname if there was no mx record
334          * - one of the mx'es
335          */
336
337         InitC_ares_dns(IO);
338
339         EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
340
341         EVS_syslog(LOG_DEBUG,
342                   "SMTP client[%ld]: looking up %s-Record %s : %d ...\n",
343                   Msg->n,
344                   (Msg->pCurrRelay->IPv6)? "aaaa": "a",
345                   Msg->pCurrRelay->Host,
346                   Msg->pCurrRelay->Port);
347
348         if (!QueueQuery((Msg->pCurrRelay->IPv6)? ns_t_aaaa : ns_t_a,
349                         Msg->pCurrRelay->Host,
350                         &Msg->IO,
351                         &Msg->HostLookup,
352                         get_one_mx_host_ip_done))
353         {
354                 Msg->MyQEntry->Status = 5;
355                 StrBufPrintf(Msg->MyQEntry->StatusMessage,
356                              "No MX hosts found for <%s>", Msg->node);
357                 Msg->IO.NextState = eTerminateConnection;
358                 return IO->NextState;
359         }
360         IO->NextState = eReadDNSReply;
361         return IO->NextState;
362 }
363
364
365 /*****************************************************************************
366  * here we try to find out about the MX records for our recipients.          *
367  *****************************************************************************/
368 eNextState smtp_resolve_mx_record_done(AsyncIO *IO)
369 {
370         SmtpOutMsg * Msg = IO->Data;
371         ParsedURL **pp;
372
373         QueryCbDone(IO);
374
375         EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
376
377         pp = &Msg->Relay;
378         while ((pp != NULL) && (*pp != NULL) && ((*pp)->Next != NULL))
379                 pp = &(*pp)->Next;
380
381         if ((IO->DNS.Query->DNSStatus == ARES_SUCCESS) &&
382             (IO->DNS.Query->VParsedDNSReply != NULL))
383         { /* ok, we found mx records. */
384
385                 Msg->CurrMX
386                         = Msg->AllMX
387                         = IO->DNS.Query->VParsedDNSReply;
388                 while (Msg->CurrMX) {
389                         int i;
390                         for (i = 0; i < 2; i++) {
391                                 ParsedURL *p;
392
393                                 p = (ParsedURL*) malloc(sizeof(ParsedURL));
394                                 memset(p, 0, sizeof(ParsedURL));
395                                 p->Priority = Msg->CurrMX->priority;
396                                 p->IsIP = 0;
397                                 p->Port = DefaultMXPort;
398                                 p->IPv6 = i == 1;
399                                 p->Host = Msg->CurrMX->host;
400                                 if (*pp == NULL)
401                                         *pp = p;
402                                 else {
403                                         ParsedURL *ppp = *pp;
404
405                                         while ((ppp->Next != NULL) &&
406                                                (ppp->Next->Priority <= p->Priority))
407                                                ppp = ppp->Next;
408                                         if ((ppp == *pp) &&
409                                             (ppp->Priority > p->Priority)) {
410                                                 p->Next = *pp;
411                                                 *pp = p;
412                                         }
413                                         else {
414                                                 p->Next = ppp->Next;
415                                                 ppp->Next = p;
416                                         }
417                                 }
418                         }
419                         Msg->CurrMX    = Msg->CurrMX->next;
420                 }
421                 Msg->CXFlags   = Msg->CXFlags & F_HAVE_MX;
422         }
423         else { /* else fall back to the plain hostname */
424                 int i;
425                 for (i = 0; i < 2; i++) {
426                         ParsedURL *p;
427
428                         p = (ParsedURL*) malloc(sizeof(ParsedURL));
429                         memset(p, 0, sizeof(ParsedURL));
430                         p->IsIP = 0;
431                         p->Port = DefaultMXPort;
432                         p->IPv6 = i == 1;
433                         p->Host = Msg->node;
434
435                         *pp = p;
436                         pp = &p->Next;
437                 }
438                 Msg->CXFlags   = Msg->CXFlags & F_DIRECT;
439         }
440         if (Msg->MyQItem->FallBackHost != NULL)
441         {
442                 Msg->MyQItem->FallBackHost->Next = *pp;
443                 *pp = Msg->MyQItem->FallBackHost;
444         }
445         Msg->pCurrRelay = Msg->Relay;
446         return get_one_mx_host_ip(IO);
447 }
448
449 eNextState resolve_mx_records(AsyncIO *IO)
450 {
451         SmtpOutMsg * Msg = IO->Data;
452
453         EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
454         /* start resolving MX records here. */
455         if (!QueueQuery(ns_t_mx,
456                         Msg->node,
457                         &Msg->IO,
458                         &Msg->MxLookup,
459                         smtp_resolve_mx_record_done))
460         {
461                 Msg->MyQEntry->Status = 5;
462                 StrBufPrintf(Msg->MyQEntry->StatusMessage,
463                              "No MX hosts found for <%s>", Msg->node);
464                 return IO->NextState;
465         }
466         Msg->IO.NextState = eReadDNSReply;
467         return IO->NextState;
468 }
469
470
471
472 /******************************************************************************
473  *  so, we're going to start a SMTP delivery.  lets get it on.                *
474  ******************************************************************************/
475
476 SmtpOutMsg *new_smtp_outmsg(OneQueItem *MyQItem,
477                             MailQEntry *MyQEntry,
478                             int MsgCount)
479 {
480         SmtpOutMsg * Msg;
481
482         Msg = (SmtpOutMsg *) malloc(sizeof(SmtpOutMsg));
483         memset(Msg, 0, sizeof(SmtpOutMsg));
484
485         Msg->n                = MsgCount;
486         Msg->MyQEntry         = MyQEntry;
487         Msg->MyQItem          = MyQItem;
488         Msg->pCurrRelay       = MyQItem->URL;
489
490         InitIOStruct(&Msg->IO,
491                      Msg,
492                      eReadMessage,
493                      SMTP_C_ReadServerStatus,
494                      SMTP_C_DNSFail,
495                      SMTP_C_DispatchWriteDone,
496                      SMTP_C_DispatchReadDone,
497                      SMTP_C_Terminate,
498                      SMTP_C_ConnFail,
499                      SMTP_C_Timeout,
500                      SMTP_C_Shutdown);
501
502         Msg->IO.ErrMsg = Msg->MyQEntry->StatusMessage;
503
504         return Msg;
505 }
506
507 void smtp_try_one_queue_entry(OneQueItem *MyQItem,
508                               MailQEntry *MyQEntry,
509                               StrBuf *MsgText,
510                         /*KeepMsgText allows us to use MsgText as ours.*/
511                               int KeepMsgText,
512                               int MsgCount)
513 {
514         SmtpOutMsg *Msg;
515
516         syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
517
518         Msg = new_smtp_outmsg(MyQItem, MyQEntry, MsgCount);
519         if (KeepMsgText) Msg->msgtext = MsgText;
520         else             Msg->msgtext = NewStrBufDup(MsgText);
521
522         if (smtp_resolve_recipients(Msg)) {
523
524                 safestrncpy(
525                         ((CitContext *)Msg->IO.CitContext)->cs_host,
526                         Msg->node,
527                         sizeof(((CitContext *)
528                                 Msg->IO.CitContext)->cs_host));
529
530                 syslog(LOG_DEBUG, "SMTP Starting: [%ld] <%s> CC <%d> \n",
531                        Msg->MyQItem->MessageID,
532                        ChrPtr(Msg->MyQEntry->Recipient),
533                        ((CitContext*)Msg->IO.CitContext)->cs_pid);
534                 if (Msg->pCurrRelay == NULL)
535                         QueueEventContext(&Msg->IO,
536                                           resolve_mx_records);
537                 else { /* oh... via relay host */
538                         if (Msg->pCurrRelay->IsIP) {
539                                 QueueEventContext(&Msg->IO,
540                                                   mx_connect_ip);
541                         }
542                         else {
543                                 /* uneducated admin has chosen to
544                                    add DNS to the equation... */
545                                 QueueEventContext(&Msg->IO,
546                                                   get_one_mx_host_ip);
547                         }
548                 }
549         }
550         else {
551                 /* No recipients? well fail then. */
552                 if ((Msg==NULL) ||
553                     (Msg->MyQEntry == NULL)) {
554                         Msg->MyQEntry->Status = 5;
555                         StrBufPlain(Msg->MyQEntry->StatusMessage,
556                                     HKEY("Invalid Recipient!"));
557                 }
558                 FinalizeMessageSend(Msg);
559         }
560 }
561
562
563
564
565
566
567 /*****************************************************************************/
568 /*                     SMTP CLIENT DISPATCHER                                */
569 /*****************************************************************************/
570
571 void SMTPSetTimeout(eNextState NextTCPState, SmtpOutMsg *Msg)
572 {
573         double Timeout = 0.0;
574         AsyncIO *IO = &Msg->IO;
575
576         EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
577
578         switch (NextTCPState) {
579         case eSendFile:
580         case eSendReply:
581         case eSendMore:
582                 Timeout = SMTP_C_SendTimeouts[Msg->State];
583                 if (Msg->State == eDATABody) {
584                         /* if we're sending a huge message,
585                          * we need more time.
586                          */
587                         Timeout += StrLength(Msg->msgtext) / 512;
588                 }
589                 break;
590         case eReadMessage:
591                 Timeout = SMTP_C_ReadTimeouts[Msg->State];
592                 if (Msg->State == eDATATerminateBody) {
593                         /*
594                          * some mailservers take a nap before accepting
595                          * the message content inspection and such.
596                          */
597                         Timeout += StrLength(Msg->msgtext) / 512;
598                 }
599                 break;
600         case eSendDNSQuery:
601         case eReadDNSReply:
602         case eDBQuery:
603         case eReadFile:
604         case eReadMore:
605         case eReadPayload:
606         case eConnect:
607         case eTerminateConnection:
608         case eAbort:
609                 return;
610         }
611         SetNextTimeout(&Msg->IO, Timeout);
612 }
613 eNextState SMTP_C_DispatchReadDone(AsyncIO *IO)
614 {
615         EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
616         SmtpOutMsg *Msg = IO->Data;
617         eNextState rc;
618
619         rc = ReadHandlers[Msg->State](Msg);
620         if (rc != eAbort)
621         {
622                 Msg->State++;
623                 SMTPSetTimeout(rc, Msg);
624         }
625         return rc;
626 }
627 eNextState SMTP_C_DispatchWriteDone(AsyncIO *IO)
628 {
629         EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
630         SmtpOutMsg *Msg = IO->Data;
631         eNextState rc;
632
633         rc = SendHandlers[Msg->State](Msg);
634         SMTPSetTimeout(rc, Msg);
635         return rc;
636 }
637
638
639 /*****************************************************************************/
640 /*                     SMTP CLIENT ERROR CATCHERS                            */
641 /*****************************************************************************/
642 eNextState SMTP_C_Terminate(AsyncIO *IO)
643 {
644         SmtpOutMsg *Msg = IO->Data;
645
646         EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
647         FinalizeMessageSend(Msg);
648         return eAbort;
649 }
650 eNextState SMTP_C_Timeout(AsyncIO *IO)
651 {
652         SmtpOutMsg *Msg = IO->Data;
653
654         Msg->MyQEntry->Status = 4;
655         EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
656         StrBufPlain(IO->ErrMsg, CKEY(ReadErrors[Msg->State]));
657         if (Msg->State > eRCPT)
658                 return eAbort;
659         else
660                 return FailOneAttempt(IO);
661 }
662 eNextState SMTP_C_ConnFail(AsyncIO *IO)
663 {
664         SmtpOutMsg *Msg = IO->Data;
665
666         Msg->MyQEntry->Status = 4;
667         EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
668         StrBufPlain(IO->ErrMsg, CKEY(ReadErrors[Msg->State]));
669         return FailOneAttempt(IO);
670 }
671 eNextState SMTP_C_DNSFail(AsyncIO *IO)
672 {
673         SmtpOutMsg *Msg = IO->Data;
674         Msg->MyQEntry->Status = 4;
675         EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
676         return FailOneAttempt(IO);
677 }
678 eNextState SMTP_C_Shutdown(AsyncIO *IO)
679 {
680         EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
681         SmtpOutMsg *Msg = IO->Data;
682
683         Msg->MyQEntry->Status = 3;
684         StrBufPlain(Msg->MyQEntry->StatusMessage,
685                     HKEY("server shutdown during message submit."));
686         FinalizeMessageSend(Msg);
687         return eAbort;
688 }
689
690
691 /**
692  * @brief lineread Handler;
693  * understands when to read more SMTP lines, and when this is a one-lined reply.
694  */
695 eReadState SMTP_C_ReadServerStatus(AsyncIO *IO)
696 {
697         eReadState Finished = eBufferNotEmpty;
698
699         while (Finished == eBufferNotEmpty) {
700                 Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf);
701
702                 switch (Finished) {
703                 case eMustReadMore: /// read new from socket...
704                         return Finished;
705                         break;
706                 case eBufferNotEmpty: /* shouldn't happen... */
707                 case eReadSuccess: /// done for now...
708                         if (StrLength(IO->IOBuf) < 4)
709                                 continue;
710                         if (ChrPtr(IO->IOBuf)[3] == '-')
711                                 Finished = eBufferNotEmpty;
712                         else
713                                 return Finished;
714                         break;
715                 case eReadFail: /// WHUT?
716                         ///todo: shut down!
717                         break;
718                 }
719         }
720         return Finished;
721 }
722
723 CTDL_MODULE_INIT(smtp_eventclient)
724 {
725         return "smtpeventclient";
726 }