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