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