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