Removed the logging facility from citserver, use syslog instead
[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         FreeURL(&Msg->Relay);
103         FreeStrBuf(&Msg->msgtext);
104         FreeAsyncIOContents(&Msg->IO);
105         memset (Msg, 0, sizeof(SmtpOutMsg)); /* just to be shure... */
106         free(Msg);
107 }
108
109 eNextState SMTP_C_Shutdown(AsyncIO *IO);
110 eNextState SMTP_C_Timeout(AsyncIO *IO);
111 eNextState SMTP_C_ConnFail(AsyncIO *IO);
112 eNextState SMTP_C_DispatchReadDone(AsyncIO *IO);
113 eNextState SMTP_C_DispatchWriteDone(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.Timeout       = SMTP_C_Timeout;
440         SendMsg->IO.ShutdownAbort = SMTP_C_Shutdown;
441
442         SendMsg->IO.SendBuf.Buf   = NewStrBufPlain(NULL, 1024);
443         SendMsg->IO.RecvBuf.Buf   = NewStrBufPlain(NULL, 1024);
444         SendMsg->IO.IOBuf         = NewStrBuf();
445
446         SendMsg->IO.NextState     = eReadMessage;
447         
448         return SendMsg;
449 }
450
451 void smtp_try_one_queue_entry(OneQueItem *MyQItem, 
452                               MailQEntry *MyQEntry, 
453                               StrBuf *MsgText, 
454                               int KeepMsgText,  /* KeepMsgText allows us to use MsgText as ours. */
455                               int MsgCount)
456 {
457         SmtpOutMsg *SendMsg;
458
459         syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
460
461         SendMsg = new_smtp_outmsg(MyQItem, MyQEntry, MsgCount);
462         if (KeepMsgText) SendMsg->msgtext = MsgText;
463         else             SendMsg->msgtext = NewStrBufDup(MsgText);
464         
465         if (smtp_resolve_recipients(SendMsg)) {
466                 CitContext *SubC;
467                 SubC = CloneContext (CC);
468                 SubC->session_specific_data = (char*) SendMsg;
469                 SendMsg->IO.CitContext = SubC;
470
471                 if (SendMsg->pCurrRelay == NULL)
472                         QueueEventContext(&SendMsg->IO,
473                                           resolve_mx_records);
474                 else { /* oh... via relay host */
475                         if (SendMsg->pCurrRelay->IsIP) {
476                                 QueueEventContext(&SendMsg->IO,
477                                                   mx_connect_ip);
478                         }
479                         else { /* uneducated admin has chosen to add DNS to the equation... */
480                                 QueueEventContext(&SendMsg->IO,
481                                                   get_one_mx_host_ip);
482                         }
483                 }
484         }
485         else {
486                 /* No recipients? well fail then. */
487                 if ((SendMsg==NULL) || 
488                     (SendMsg->MyQEntry == NULL)) {
489                         SendMsg->MyQEntry->Status = 5;
490                         StrBufPlain(SendMsg->MyQEntry->StatusMessage, 
491                                     HKEY("Invalid Recipient!"));
492                 }
493                 FinalizeMessageSend(SendMsg);
494         }
495 }
496
497
498
499
500
501
502 /*****************************************************************************/
503 /*                     SMTP CLIENT DISPATCHER                                */
504 /*****************************************************************************/
505
506 void SMTPSetTimeout(eNextState NextTCPState, SmtpOutMsg *pMsg)
507 {
508         double Timeout = 0.0;
509
510         syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
511
512         switch (NextTCPState) {
513         case eSendReply:
514         case eSendMore:
515                 Timeout = SMTP_C_SendTimeouts[pMsg->State];
516                 if (pMsg->State == eDATABody) {
517                         /* if we're sending a huge message, we need more time. */
518                         Timeout += StrLength(pMsg->msgtext) / 1024;
519                 }
520                 break;
521         case eReadMessage:
522                 Timeout = SMTP_C_ReadTimeouts[pMsg->State];
523                 if (pMsg->State == eDATATerminateBody) {
524                         /* 
525                          * some mailservers take a nap before accepting the message
526                          * content inspection and such.
527                          */
528                         Timeout += StrLength(pMsg->msgtext) / 1024;
529                 }
530                 break;
531         case eSendDNSQuery:
532         case eReadDNSReply:
533         case eConnect:
534         case eTerminateConnection:
535         case eAbort:
536                 return;
537         }
538         SetNextTimeout(&pMsg->IO, Timeout);
539 }
540 eNextState SMTP_C_DispatchReadDone(AsyncIO *IO)
541 {
542         syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
543         SmtpOutMsg *pMsg = IO->Data;
544         eNextState rc;
545
546         rc = ReadHandlers[pMsg->State](pMsg);
547         pMsg->State++;
548         SMTPSetTimeout(rc, pMsg);
549         return rc;
550 }
551 eNextState SMTP_C_DispatchWriteDone(AsyncIO *IO)
552 {
553         syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
554         SmtpOutMsg *pMsg = IO->Data;
555         eNextState rc;
556
557         rc = SendHandlers[pMsg->State](pMsg);
558         SMTPSetTimeout(rc, pMsg);
559         return rc;
560 }
561
562
563 /*****************************************************************************/
564 /*                     SMTP CLIENT ERROR CATCHERS                            */
565 /*****************************************************************************/
566 eNextState SMTP_C_Terminate(AsyncIO *IO)
567 {
568         SmtpOutMsg *pMsg = IO->Data;
569
570         syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
571         FinalizeMessageSend(pMsg);
572         return eAbort;
573 }
574 eNextState SMTP_C_Timeout(AsyncIO *IO)
575 {
576         SmtpOutMsg *pMsg = IO->Data;
577
578         syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
579         StrBufPlain(IO->ErrMsg, CKEY(ReadErrors[pMsg->State]));
580         return FailOneAttempt(IO);
581 }
582 eNextState SMTP_C_ConnFail(AsyncIO *IO)
583 {
584         SmtpOutMsg *pMsg = IO->Data;
585
586         syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
587         StrBufPlain(IO->ErrMsg, CKEY(ReadErrors[pMsg->State]));
588         return FailOneAttempt(IO);
589 }
590 eNextState SMTP_C_Shutdown(AsyncIO *IO)
591 {
592         syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
593         SmtpOutMsg *pMsg = IO->Data;
594
595         pMsg->MyQEntry->Status = 3;
596         StrBufPlain(pMsg->MyQEntry->StatusMessage, HKEY("server shutdown during message submit."));
597         FinalizeMessageSend(pMsg);
598         return eAbort;
599 }
600
601
602 /**
603  * @brief lineread Handler; understands when to read more SMTP lines, and when this is a one-lined reply.
604  */
605 eReadState SMTP_C_ReadServerStatus(AsyncIO *IO)
606 {
607         eReadState Finished = eBufferNotEmpty; 
608
609         while (Finished == eBufferNotEmpty) {
610                 Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf);
611                 
612                 switch (Finished) {
613                 case eMustReadMore: /// read new from socket... 
614                         return Finished;
615                         break;
616                 case eBufferNotEmpty: /* shouldn't happen... */
617                 case eReadSuccess: /// done for now...
618                         if (StrLength(IO->IOBuf) < 4)
619                                 continue;
620                         if (ChrPtr(IO->IOBuf)[3] == '-')
621                                 Finished = eBufferNotEmpty;
622                         else 
623                                 return Finished;
624                         break;
625                 case eReadFail: /// WHUT?
626                         ///todo: shut down! 
627                         break;
628                 }
629         }
630         return Finished;
631 }
632
633 #endif
634 CTDL_MODULE_INIT(smtp_eventclient)
635 {
636         return "smtpeventclient";
637 }