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