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