Style cleanup
[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         InitC_ares_dns(IO);
334
335         EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
336
337         EVS_syslog(LOG_DEBUG,
338                   "SMTP client[%ld]: looking up %s-Record %s : %d ...\n",
339                   SendMsg->n,
340                   (SendMsg->pCurrRelay->IPv6)? "aaaa": "a",
341                   SendMsg->pCurrRelay->Host,
342                   SendMsg->pCurrRelay->Port);
343
344         if (!QueueQuery((SendMsg->pCurrRelay->IPv6)? ns_t_aaaa : ns_t_a,
345                         SendMsg->pCurrRelay->Host,
346                         &SendMsg->IO,
347                         &SendMsg->HostLookup,
348                         get_one_mx_host_ip_done))
349         {
350                 SendMsg->MyQEntry->Status = 5;
351                 StrBufPrintf(SendMsg->MyQEntry->StatusMessage,
352                              "No MX hosts found for <%s>", SendMsg->node);
353                 SendMsg->IO.NextState = eTerminateConnection;
354                 return IO->NextState;
355         }
356         IO->NextState = eReadDNSReply;
357         return IO->NextState;
358 }
359
360
361 /*****************************************************************************
362  * here we try to find out about the MX records for our recipients.          *
363  *****************************************************************************/
364 eNextState smtp_resolve_mx_record_done(AsyncIO *IO)
365 {
366         SmtpOutMsg * SendMsg = IO->Data;
367         ParsedURL **pp;
368
369         QueryCbDone(IO);
370
371         EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
372         pp = &SendMsg->Relay;
373         while ((pp != NULL) && (*pp != NULL) && ((*pp)->Next != NULL))
374                 pp = &(*pp)->Next;
375
376         if ((IO->DNS.Query->DNSStatus == ARES_SUCCESS) &&
377             (IO->DNS.Query->VParsedDNSReply != NULL))
378         { /* ok, we found mx records. */
379                 SendMsg->IO.ErrMsg = SendMsg->MyQEntry->StatusMessage;
380
381                 SendMsg->CurrMX
382                         = SendMsg->AllMX
383                         = IO->DNS.Query->VParsedDNSReply;
384                 while (SendMsg->CurrMX) {
385                         int i;
386                         for (i = 0; i < 2; i++) {
387                                 ParsedURL *p;
388
389                                 p = (ParsedURL*) malloc(sizeof(ParsedURL));
390                                 memset(p, 0, sizeof(ParsedURL));
391                                 p->IsIP = 0;
392                                 p->Port = DefaultMXPort;
393                                 p->IPv6 = i == 1;
394                                 p->Host = SendMsg->CurrMX->host;
395
396                                 *pp = p;
397                                 pp = &p->Next;
398                         }
399                         SendMsg->CurrMX    = SendMsg->CurrMX->next;
400                 }
401                 SendMsg->CXFlags   = SendMsg->CXFlags & F_HAVE_MX;
402         }
403         else { /* else fall back to the plain hostname */
404                 int i;
405                 for (i = 0; i < 2; i++) {
406                         ParsedURL *p;
407
408                         p = (ParsedURL*) malloc(sizeof(ParsedURL));
409                         memset(p, 0, sizeof(ParsedURL));
410                         p->IsIP = 0;
411                         p->Port = DefaultMXPort;
412                         p->IPv6 = i == 1;
413                         p->Host = SendMsg->node;
414
415                         *pp = p;
416                         pp = &p->Next;
417                 }
418                 SendMsg->CXFlags   = SendMsg->CXFlags & F_DIRECT;
419         }
420         *pp = SendMsg->MyQItem->FallBackHost;
421         SendMsg->pCurrRelay = SendMsg->Relay;
422         return get_one_mx_host_ip(IO);
423 }
424
425 eNextState resolve_mx_records(AsyncIO *IO)
426 {
427         SmtpOutMsg * SendMsg = IO->Data;
428
429         EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
430         /* start resolving MX records here. */
431         if (!QueueQuery(ns_t_mx,
432                         SendMsg->node,
433                         &SendMsg->IO,
434                         &SendMsg->MxLookup,
435                         smtp_resolve_mx_record_done))
436         {
437                 SendMsg->MyQEntry->Status = 5;
438                 StrBufPrintf(SendMsg->MyQEntry->StatusMessage,
439                              "No MX hosts found for <%s>", SendMsg->node);
440                 return IO->NextState;
441         }
442         SendMsg->IO.NextState = eReadDNSReply;
443         return IO->NextState;
444 }
445
446
447
448 /******************************************************************************
449  *  so, we're going to start a SMTP delivery.  lets get it on.                *
450  ******************************************************************************/
451
452 SmtpOutMsg *new_smtp_outmsg(OneQueItem *MyQItem,
453                             MailQEntry *MyQEntry,
454                             int MsgCount)
455 {
456         SmtpOutMsg * SendMsg;
457
458         SendMsg = (SmtpOutMsg *) malloc(sizeof(SmtpOutMsg));
459         memset(SendMsg, 0, sizeof(SmtpOutMsg));
460
461         SendMsg->n                = MsgCount;
462         SendMsg->MyQEntry         = MyQEntry;
463         SendMsg->MyQItem          = MyQItem;
464         SendMsg->pCurrRelay       = MyQItem->URL;
465
466         InitIOStruct(&SendMsg->IO,
467                      SendMsg,
468                      eReadMessage,
469                      SMTP_C_ReadServerStatus,
470                      SMTP_C_DNSFail,
471                      SMTP_C_DispatchWriteDone,
472                      SMTP_C_DispatchReadDone,
473                      SMTP_C_Terminate,
474                      SMTP_C_ConnFail,
475                      SMTP_C_Timeout,
476                      SMTP_C_Shutdown);
477
478         return SendMsg;
479 }
480
481 void smtp_try_one_queue_entry(OneQueItem *MyQItem,
482                               MailQEntry *MyQEntry,
483                               StrBuf *MsgText,
484                         /*KeepMsgText allows us to use MsgText as ours.*/
485                               int KeepMsgText,
486                               int MsgCount)
487 {
488         SmtpOutMsg *SendMsg;
489
490         syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
491
492         SendMsg = new_smtp_outmsg(MyQItem, MyQEntry, MsgCount);
493         if (KeepMsgText) SendMsg->msgtext = MsgText;
494         else             SendMsg->msgtext = NewStrBufDup(MsgText);
495
496         if (smtp_resolve_recipients(SendMsg)) {
497
498                 safestrncpy(
499                         ((CitContext *)SendMsg->IO.CitContext)->cs_host,
500                         SendMsg->node,
501                         sizeof(((CitContext *)
502                                 SendMsg->IO.CitContext)->cs_host));
503
504                 syslog(LOG_DEBUG, "SMTP Starting: [%ld] <%s> CC <%d> \n",
505                        SendMsg->MyQItem->MessageID,
506                        ChrPtr(SendMsg->MyQEntry->Recipient),
507                        ((CitContext*)SendMsg->IO.CitContext)->cs_pid);
508                 if (SendMsg->pCurrRelay == NULL)
509                         QueueEventContext(&SendMsg->IO,
510                                           resolve_mx_records);
511                 else { /* oh... via relay host */
512                         if (SendMsg->pCurrRelay->IsIP) {
513                                 QueueEventContext(&SendMsg->IO,
514                                                   mx_connect_ip);
515                         }
516                         else {
517                                 /* uneducated admin has chosen to
518                                    add DNS to the equation... */
519                                 QueueEventContext(&SendMsg->IO,
520                                                   get_one_mx_host_ip);
521                         }
522                 }
523         }
524         else {
525                 /* No recipients? well fail then. */
526                 if ((SendMsg==NULL) ||
527                     (SendMsg->MyQEntry == NULL)) {
528                         SendMsg->MyQEntry->Status = 5;
529                         StrBufPlain(SendMsg->MyQEntry->StatusMessage,
530                                     HKEY("Invalid Recipient!"));
531                 }
532                 FinalizeMessageSend(SendMsg);
533         }
534 }
535
536
537
538
539
540
541 /*****************************************************************************/
542 /*                     SMTP CLIENT DISPATCHER                                */
543 /*****************************************************************************/
544
545 void SMTPSetTimeout(eNextState NextTCPState, SmtpOutMsg *pMsg)
546 {
547         double Timeout = 0.0;
548         AsyncIO *IO = &pMsg->IO;
549
550         EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
551
552         switch (NextTCPState) {
553         case eSendFile:
554         case eSendReply:
555         case eSendMore:
556                 Timeout = SMTP_C_SendTimeouts[pMsg->State];
557                 if (pMsg->State == eDATABody) {
558                         /* if we're sending a huge message,
559                          * we need more time.
560                          */
561                         Timeout += StrLength(pMsg->msgtext) / 1024;
562                 }
563                 break;
564         case eReadMessage:
565                 Timeout = SMTP_C_ReadTimeouts[pMsg->State];
566                 if (pMsg->State == eDATATerminateBody) {
567                         /*
568                          * some mailservers take a nap before accepting
569                          * the message content inspection and such.
570                          */
571                         Timeout += StrLength(pMsg->msgtext) / 1024;
572                 }
573                 break;
574         case eSendDNSQuery:
575         case eReadDNSReply:
576         case eDBQuery:
577         case eReadFile:
578         case eReadMore:
579         case eReadPayload:
580         case eConnect:
581         case eTerminateConnection:
582         case eAbort:
583                 return;
584         }
585         SetNextTimeout(&pMsg->IO, Timeout);
586 }
587 eNextState SMTP_C_DispatchReadDone(AsyncIO *IO)
588 {
589         EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
590         SmtpOutMsg *pMsg = IO->Data;
591         eNextState rc;
592
593         rc = ReadHandlers[pMsg->State](pMsg);
594         if (rc != eAbort)
595         {
596                 pMsg->State++;
597                 SMTPSetTimeout(rc, pMsg);
598         }
599         return rc;
600 }
601 eNextState SMTP_C_DispatchWriteDone(AsyncIO *IO)
602 {
603         EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
604         SmtpOutMsg *pMsg = IO->Data;
605         eNextState rc;
606
607         rc = SendHandlers[pMsg->State](pMsg);
608         SMTPSetTimeout(rc, pMsg);
609         return rc;
610 }
611
612
613 /*****************************************************************************/
614 /*                     SMTP CLIENT ERROR CATCHERS                            */
615 /*****************************************************************************/
616 eNextState SMTP_C_Terminate(AsyncIO *IO)
617 {
618         SmtpOutMsg *pMsg = IO->Data;
619
620         EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
621         FinalizeMessageSend(pMsg);
622         return eAbort;
623 }
624 eNextState SMTP_C_Timeout(AsyncIO *IO)
625 {
626         SmtpOutMsg *pMsg = IO->Data;
627
628         EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
629         StrBufPlain(IO->ErrMsg, CKEY(ReadErrors[pMsg->State]));
630         return FailOneAttempt(IO);
631 }
632 eNextState SMTP_C_ConnFail(AsyncIO *IO)
633 {
634         SmtpOutMsg *pMsg = IO->Data;
635
636         EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
637         StrBufPlain(IO->ErrMsg, CKEY(ReadErrors[pMsg->State]));
638         return FailOneAttempt(IO);
639 }
640 eNextState SMTP_C_DNSFail(AsyncIO *IO)
641 {
642         EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
643         return FailOneAttempt(IO);
644 }
645 eNextState SMTP_C_Shutdown(AsyncIO *IO)
646 {
647         EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
648         SmtpOutMsg *pMsg = IO->Data;
649
650         pMsg->MyQEntry->Status = 3;
651         StrBufPlain(pMsg->MyQEntry->StatusMessage,
652                     HKEY("server shutdown during message submit."));
653         FinalizeMessageSend(pMsg);
654         return eAbort;
655 }
656
657
658 /**
659  * @brief lineread Handler;
660  * understands when to read more SMTP lines, and when this is a one-lined reply.
661  */
662 eReadState SMTP_C_ReadServerStatus(AsyncIO *IO)
663 {
664         eReadState Finished = eBufferNotEmpty;
665
666         while (Finished == eBufferNotEmpty) {
667                 Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf);
668
669                 switch (Finished) {
670                 case eMustReadMore: /// read new from socket...
671                         return Finished;
672                         break;
673                 case eBufferNotEmpty: /* shouldn't happen... */
674                 case eReadSuccess: /// done for now...
675                         if (StrLength(IO->IOBuf) < 4)
676                                 continue;
677                         if (ChrPtr(IO->IOBuf)[3] == '-')
678                                 Finished = eBufferNotEmpty;
679                         else
680                                 return Finished;
681                         break;
682                 case eReadFail: /// WHUT?
683                         ///todo: shut down!
684                         break;
685                 }
686         }
687         return Finished;
688 }
689
690 CTDL_MODULE_INIT(smtp_eventclient)
691 {
692         return "smtpeventclient";
693 }