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