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