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