rename InitEventIO to EvConnectSock, since this suits better what this function does...
[citadel.git] / citadel / modules / smtp / serv_smtpeventclient.c
1 /*
2  * This module is an SMTP and ESMTP implementation for the Citadel system.
3  * It is compliant with all of the following:
4  *
5  * RFC  821 - Simple Mail Transfer Protocol
6  * RFC  876 - Survey of SMTP Implementations
7  * RFC 1047 - Duplicate messages and SMTP
8  * RFC 1652 - 8 bit MIME
9  * RFC 1869 - Extended Simple Mail Transfer Protocol
10  * RFC 1870 - SMTP Service Extension for Message Size Declaration
11  * RFC 2033 - Local Mail Transfer Protocol
12  * RFC 2197 - SMTP Service Extension for Command Pipelining
13  * RFC 2476 - Message Submission
14  * RFC 2487 - SMTP Service Extension for Secure SMTP over TLS
15  * RFC 2554 - SMTP Service Extension for Authentication
16  * RFC 2821 - Simple Mail Transfer Protocol
17  * RFC 2822 - Internet Message Format
18  * RFC 2920 - SMTP Service Extension for Command Pipelining
19  *
20  * The VRFY and EXPN commands have been removed from this implementation
21  * because nobody uses these commands anymore, except for spammers.
22  *
23  * Copyright (c) 1998-2009 by the citadel.org team
24  *
25  *  This program is free software; you can redistribute it and/or modify
26  *  it under the terms of the GNU General Public License as published by
27  *  the Free Software Foundation; either version 3 of the License, or
28  *  (at your option) any later version.
29  *
30  *  This program is distributed in the hope that it will be useful,
31  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
32  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33  *  GNU General Public License for more details.
34  *
35  *  You should have received a copy of the GNU General Public License
36  *  along with this program; if not, write to the Free Software
37  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
38  */
39
40 #include "sysdep.h"
41 #include <stdlib.h>
42 #include <unistd.h>
43 #include <stdio.h>
44 #include <termios.h>
45 #include <fcntl.h>
46 #include <signal.h>
47 #include <pwd.h>
48 #include <errno.h>
49 #include <sys/types.h>
50 #include <syslog.h>
51
52 #if TIME_WITH_SYS_TIME
53 # include <sys/time.h>
54 # include <time.h>
55 #else
56 # if HAVE_SYS_TIME_H
57 #  include <sys/time.h>
58 # else
59 #  include <time.h>
60 # endif
61 #endif
62 #include <sys/wait.h>
63 #include <ctype.h>
64 #include <string.h>
65 #include <limits.h>
66 #include <sys/socket.h>
67 #include <netinet/in.h>
68 #include <arpa/inet.h>
69 #include <libcitadel.h>
70 #include "citadel.h"
71 #include "server.h"
72 #include "citserver.h"
73 #include "support.h"
74 #include "config.h"
75 #include "control.h"
76 #include "user_ops.h"
77 #include "database.h"
78 #include "msgbase.h"
79 #include "internet_addressing.h"
80 #include "genstamp.h"
81 #include "domain.h"
82 #include "clientsocket.h"
83 #include "locate_host.h"
84 #include "citadel_dirs.h"
85
86 #include "ctdl_module.h"
87
88 #include "smtp_util.h"
89 #include "event_client.h"
90 #include "smtpqueue.h"
91 #include "smtp_clienthandlers.h"
92
93 const unsigned short DefaultMXPort = 25;
94 void DeleteSmtpOutMsg(void *v)
95 {
96         SmtpOutMsg *Msg = v;
97
98         ares_free_data(Msg->AllMX);
99         if (Msg->HostLookup.VParsedDNSReply != NULL)
100                 Msg->HostLookup.DNSReplyFree(Msg->HostLookup.VParsedDNSReply);
101         FreeURL(&Msg->Relay);
102         FreeStrBuf(&Msg->msgtext);
103         FreeAsyncIOContents(&Msg->IO);
104         memset (Msg, 0, sizeof(SmtpOutMsg)); /* just to be shure... */
105         free(Msg);
106 }
107
108 eNextState SMTP_C_Shutdown(AsyncIO *IO);
109 eNextState SMTP_C_Timeout(AsyncIO *IO);
110 eNextState SMTP_C_ConnFail(AsyncIO *IO);
111 eNextState SMTP_C_DispatchReadDone(AsyncIO *IO);
112 eNextState SMTP_C_DispatchWriteDone(AsyncIO *IO);
113 eNextState SMTP_C_DNSFail(AsyncIO *IO);
114 eNextState SMTP_C_Terminate(AsyncIO *IO);
115 eReadState SMTP_C_ReadServerStatus(AsyncIO *IO);
116
117 eNextState mx_connect_ip(AsyncIO *IO);
118 eNextState get_one_mx_host_ip(AsyncIO *IO);
119
120 /******************************************************************************
121  * So, we're finished with sending (regardless of success or failure)         *
122  * This Message might be referenced by several Queue-Items, if we're the last,*
123  * we need to free the memory and send bounce messages (on terminal failure)  *
124  * else we just free our SMTP-Message struct.                                 *
125  ******************************************************************************/
126 void FinalizeMessageSend(SmtpOutMsg *Msg)
127 {
128         int IDestructQueItem;
129         int nRemain;
130         StrBuf *MsgData;
131         AsyncIO *IO = &Msg->IO;
132
133         IDestructQueItem = DecreaseQReference(Msg->MyQItem);
134
135         nRemain = CountActiveQueueEntries(Msg->MyQItem);
136
137         if ((nRemain > 0) || IDestructQueItem)
138                 MsgData = SerializeQueueItem(Msg->MyQItem);
139         else
140                 MsgData = NULL;
141
142         /*
143          * Uncompleted delivery instructions remain, so delete the old
144          * instructions and replace with the updated ones.
145          */
146         EVS_syslog(LOG_DEBUG, "SMTPQD: %ld", Msg->MyQItem->QueMsgID);
147         CtdlDeleteMessages(SMTP_SPOOLOUT_ROOM, &Msg->MyQItem->QueMsgID, 1, "");
148
149         if (IDestructQueItem)
150                 smtpq_do_bounce(Msg->MyQItem,Msg->msgtext);
151
152         if (nRemain > 0)
153         {
154                 struct CtdlMessage *msg;
155                 msg = malloc(sizeof(struct CtdlMessage));
156                 memset(msg, 0, sizeof(struct CtdlMessage));
157                 msg->cm_magic = CTDLMESSAGE_MAGIC;
158                 msg->cm_anon_type = MES_NORMAL;
159                 msg->cm_format_type = FMT_RFC822;
160                 msg->cm_fields['M'] = SmashStrBuf(&MsgData);
161                 Msg->MyQItem->QueMsgID =
162                         CtdlSubmitMsg(msg, NULL, SMTP_SPOOLOUT_ROOM, QP_EADDR);
163                 EVS_syslog(LOG_DEBUG, "SMTPQ: %ld", Msg->MyQItem->QueMsgID);
164                 CtdlFreeMessage(msg);
165         }
166         else {
167                 CtdlDeleteMessages(SMTP_SPOOLOUT_ROOM,
168                                    &Msg->MyQItem->MessageID,
169                                    1,
170                                    "");
171                 FreeStrBuf(&MsgData);
172         }
173         RemoveContext(Msg->IO.CitContext);
174         if (IDestructQueItem)
175                 RemoveQItem(Msg->MyQItem);
176         DeleteSmtpOutMsg(Msg);
177 }
178
179 eNextState FailOneAttempt(AsyncIO *IO)
180 {
181         SmtpOutMsg *SendMsg = IO->Data;
182
183         if (SendMsg->MyQEntry->Status == 2)
184                 return eAbort;
185
186         /* 
187          * possible ways here: 
188          * - connection timeout 
189          * - dns lookup failed
190          */
191         StopClientWatchers(IO);
192
193         if (SendMsg->pCurrRelay != NULL)
194                 SendMsg->pCurrRelay = SendMsg->pCurrRelay->Next;
195
196         if (SendMsg->pCurrRelay == NULL) {
197                 EVS_syslog(LOG_DEBUG, "SMTP: %s Aborting\n", __FUNCTION__);
198                 return eAbort;
199         }
200         if (SendMsg->pCurrRelay->IsIP) {
201                 EVS_syslog(LOG_DEBUG, "SMTP: %s connecting IP\n", __FUNCTION__);
202                 return mx_connect_ip(IO);
203         }
204         else {
205                 EVS_syslog(LOG_DEBUG, "SMTP: %s resolving next MX Record\n", __FUNCTION__);
206                 return get_one_mx_host_ip(IO);
207         }
208 }
209
210
211 void SetConnectStatus(AsyncIO *IO)
212 {
213         
214         SmtpOutMsg *SendMsg = IO->Data;
215         char buf[256];
216         void *src;
217
218         buf[0] = '\0';
219
220         if (IO->ConnectMe->IPv6) {
221                 src = &IO->ConnectMe->Addr.sin6_addr;
222         }
223         else {
224                 struct sockaddr_in *addr = (struct sockaddr_in *)&IO->ConnectMe->Addr;
225
226                 src = &addr->sin_addr.s_addr;
227         }
228
229         inet_ntop((IO->ConnectMe->IPv6)?AF_INET6:AF_INET,
230                   src,
231                   buf, 
232                   sizeof(buf));
233
234         if (SendMsg->mx_host == NULL)
235                 SendMsg->mx_host = "<no MX-Record>";
236
237         EVS_syslog(LOG_DEBUG,
238                   "SMTP client[%ld]: connecting to %s [%s]:%d ...\n", 
239                   SendMsg->n, 
240                   SendMsg->mx_host, 
241                   buf,
242                   SendMsg->IO.ConnectMe->Port);
243
244         SendMsg->MyQEntry->Status = 5; 
245         StrBufPrintf(SendMsg->MyQEntry->StatusMessage, 
246                      "Timeout while connecting %s [%s]:%d ", 
247                      SendMsg->mx_host,
248                      buf,
249                      SendMsg->IO.ConnectMe->Port);
250         SendMsg->IO.NextState = eConnect;
251 }
252
253 /*****************************************************************************
254  * So we connect our Relay IP here.                                          *
255  *****************************************************************************/
256 eNextState mx_connect_ip(AsyncIO *IO)
257 {
258         SmtpOutMsg *SendMsg = IO->Data;
259
260         EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
261         
262         IO->ConnectMe = SendMsg->pCurrRelay;
263         /*  Bypass the ns lookup result like this: IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1"); */
264
265         SetConnectStatus(IO);
266
267         return EvConnectSock(IO, SendMsg, 
268                              SMTP_C_ConnTimeout, 
269                              SMTP_C_ReadTimeouts[0],
270                              1);
271 }
272
273 eNextState get_one_mx_host_ip_done(AsyncIO *IO)
274 {
275         SmtpOutMsg *SendMsg = IO->Data;
276         struct hostent *hostent;
277
278         QueryCbDone(IO);
279
280         hostent = SendMsg->HostLookup.VParsedDNSReply;
281         if ((SendMsg->HostLookup.DNSStatus == ARES_SUCCESS) && 
282             (hostent != NULL) ) {
283                 memset(&SendMsg->pCurrRelay->Addr, 0, sizeof(struct in6_addr));
284                 if (SendMsg->pCurrRelay->IPv6) {
285                         memcpy(&SendMsg->pCurrRelay->Addr.sin6_addr.s6_addr, 
286                                &hostent->h_addr_list[0],
287                                sizeof(struct in6_addr));
288                         
289                         SendMsg->pCurrRelay->Addr.sin6_family = hostent->h_addrtype;
290                         SendMsg->pCurrRelay->Addr.sin6_port   = htons(DefaultMXPort);
291                 }
292                 else {
293                         struct sockaddr_in *addr = (struct sockaddr_in*) &SendMsg->pCurrRelay->Addr;
294                         /* Bypass the ns lookup result like this: IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1"); */
295 //                      addr->sin_addr.s_addr = htonl((uint32_t)&hostent->h_addr_list[0]);
296                         memcpy(&addr->sin_addr.s_addr, 
297                                hostent->h_addr_list[0], 
298                                sizeof(uint32_t));
299                         
300                         addr->sin_family = hostent->h_addrtype;
301                         addr->sin_port   = htons(DefaultMXPort);
302                         
303                 }
304                 SendMsg->mx_host = SendMsg->pCurrRelay->Host;
305                 return mx_connect_ip(IO);
306         }
307         else // TODO: here we need to find out whether there are more mx'es, backup relay, and so on
308                 return FailOneAttempt(IO);
309 }
310
311 eNextState get_one_mx_host_ip(AsyncIO *IO)
312 {
313         SmtpOutMsg * SendMsg = IO->Data;
314         /* 
315          * here we start with the lookup of one host. it might be...
316          * - the relay host *sigh*
317          * - the direct hostname if there was no mx record
318          * - one of the mx'es
319          */ 
320
321         InitC_ares_dns(IO);
322
323         EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
324
325         EVS_syslog(LOG_DEBUG, 
326                   "SMTP client[%ld]: looking up %s-Record %s : %d ...\n", 
327                   SendMsg->n, 
328                   (SendMsg->pCurrRelay->IPv6)? "aaaa": "a",
329                   SendMsg->pCurrRelay->Host, 
330                   SendMsg->pCurrRelay->Port);
331
332         if (!QueueQuery((SendMsg->pCurrRelay->IPv6)? ns_t_aaaa : ns_t_a, 
333                         SendMsg->pCurrRelay->Host, 
334                         &SendMsg->IO, 
335                         &SendMsg->HostLookup, 
336                         get_one_mx_host_ip_done))
337         {
338                 SendMsg->MyQEntry->Status = 5;
339                 StrBufPrintf(SendMsg->MyQEntry->StatusMessage, 
340                              "No MX hosts found for <%s>", SendMsg->node);
341                 SendMsg->IO.NextState = eTerminateConnection;
342                 return IO->NextState;
343         }
344         IO->NextState = eReadDNSReply;
345         return IO->NextState;
346 }
347
348
349 /*****************************************************************************
350  * here we try to find out about the MX records for our recipients.          *
351  *****************************************************************************/
352 eNextState smtp_resolve_mx_record_done(AsyncIO *IO)
353 {
354         SmtpOutMsg * SendMsg = IO->Data;
355         ParsedURL **pp;
356
357         QueryCbDone(IO);
358
359         EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
360         pp = &SendMsg->Relay;
361         while ((pp != NULL) && (*pp != NULL) && ((*pp)->Next != NULL))
362                 pp = &(*pp)->Next;
363
364         if ((IO->DNS.Query->DNSStatus == ARES_SUCCESS) && 
365             (IO->DNS.Query->VParsedDNSReply != NULL))
366         { /* ok, we found mx records. */
367                 SendMsg->IO.ErrMsg = SendMsg->MyQEntry->StatusMessage;
368                 
369                 SendMsg->CurrMX    = SendMsg->AllMX 
370                                    = IO->DNS.Query->VParsedDNSReply;
371                 while (SendMsg->CurrMX) {
372                         int i;
373                         for (i = 0; i < 2; i++) {
374                                 ParsedURL *p;
375
376                                 p = (ParsedURL*) malloc(sizeof(ParsedURL));
377                                 memset(p, 0, sizeof(ParsedURL));
378                                 p->IsIP = 0;
379                                 p->Port = DefaultMXPort;
380                                 p->IPv6 = i == 1;
381                                 p->Host = SendMsg->CurrMX->host;
382                                 
383                                 *pp = p;
384                                 pp = &p->Next;
385                         }
386                         SendMsg->CurrMX    = SendMsg->CurrMX->next;
387                 }
388                 SendMsg->CXFlags   = SendMsg->CXFlags & F_HAVE_MX;
389         }
390         else { /* else fall back to the plain hostname */
391                 int i;
392                 for (i = 0; i < 2; i++) {
393                         ParsedURL *p;
394
395                         p = (ParsedURL*) malloc(sizeof(ParsedURL));
396                         memset(p, 0, sizeof(ParsedURL));
397                         p->IsIP = 0;
398                         p->Port = DefaultMXPort;
399                         p->IPv6 = i == 1;
400                         p->Host = SendMsg->node;
401                                 
402                         *pp = p;
403                         pp = &p->Next;
404                 }
405                 SendMsg->CXFlags   = SendMsg->CXFlags & F_DIRECT;
406         }
407         *pp = SendMsg->MyQItem->FallBackHost;
408         SendMsg->pCurrRelay = SendMsg->Relay;
409         return get_one_mx_host_ip(IO);
410 }
411
412 eNextState resolve_mx_records(AsyncIO *IO)
413 {
414         SmtpOutMsg * SendMsg = IO->Data;
415
416         EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
417         /* start resolving MX records here. */
418         if (!QueueQuery(ns_t_mx, 
419                         SendMsg->node, 
420                         &SendMsg->IO, 
421                         &SendMsg->MxLookup, 
422                         smtp_resolve_mx_record_done))
423         {
424                 SendMsg->MyQEntry->Status = 5;
425                 StrBufPrintf(SendMsg->MyQEntry->StatusMessage, 
426                              "No MX hosts found for <%s>", SendMsg->node);
427                 return IO->NextState;
428         }
429         SendMsg->IO.NextState = eReadDNSReply;
430         return IO->NextState;
431 }
432
433
434
435 /******************************************************************************
436  *  so, we're going to start a SMTP delivery.  lets get it on.                *
437  ******************************************************************************/
438
439 SmtpOutMsg *new_smtp_outmsg(OneQueItem *MyQItem, 
440                             MailQEntry *MyQEntry, 
441                             int MsgCount)
442 {
443         SmtpOutMsg * SendMsg;
444
445         SendMsg = (SmtpOutMsg *) malloc(sizeof(SmtpOutMsg));
446         memset(SendMsg, 0, sizeof(SmtpOutMsg));
447
448         SendMsg->n                = MsgCount;
449         SendMsg->MyQEntry         = MyQEntry;
450         SendMsg->MyQItem          = MyQItem;
451         SendMsg->pCurrRelay       = MyQItem->URL;
452
453         SendMsg->IO.Data          = SendMsg;
454
455         SendMsg->IO.SendDone      = SMTP_C_DispatchWriteDone;
456         SendMsg->IO.ReadDone      = SMTP_C_DispatchReadDone;
457         SendMsg->IO.Terminate     = SMTP_C_Terminate;
458         SendMsg->IO.LineReader    = SMTP_C_ReadServerStatus;
459         SendMsg->IO.ConnFail      = SMTP_C_ConnFail;
460         SendMsg->IO.DNS.Fail      = SMTP_C_DNSFail;
461         SendMsg->IO.Timeout       = SMTP_C_Timeout;
462         SendMsg->IO.ShutdownAbort = SMTP_C_Shutdown;
463
464         SendMsg->IO.SendBuf.Buf   = NewStrBufPlain(NULL, 1024);
465         SendMsg->IO.RecvBuf.Buf   = NewStrBufPlain(NULL, 1024);
466         SendMsg->IO.IOBuf         = NewStrBuf();
467
468         SendMsg->IO.NextState     = eReadMessage;
469
470         return SendMsg;
471 }
472
473 void smtp_try_one_queue_entry(OneQueItem *MyQItem, 
474                               MailQEntry *MyQEntry, 
475                               StrBuf *MsgText, 
476                               int KeepMsgText,  /* KeepMsgText allows us to use MsgText as ours. */
477                               int MsgCount)
478 {
479         AsyncIO *IO;
480         SmtpOutMsg *SendMsg;
481
482         syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
483
484         SendMsg = new_smtp_outmsg(MyQItem, MyQEntry, MsgCount);
485         IO = &SendMsg->IO;
486         if (KeepMsgText) SendMsg->msgtext = MsgText;
487         else             SendMsg->msgtext = NewStrBufDup(MsgText);
488         
489         if (smtp_resolve_recipients(SendMsg)) {
490                 CitContext *SubC;
491                 SubC = CloneContext (CC);
492                 SubC->session_specific_data = (char*) SendMsg;
493                 SendMsg->IO.CitContext = SubC;
494                 
495                 safestrncpy(SubC->cs_host, SendMsg->node, sizeof(SubC->cs_host));
496                 syslog(LOG_DEBUG, "SMTP Starting: [%ld] <%s> CC <%d> \n",
497                        SendMsg->MyQItem->MessageID, 
498                        ChrPtr(SendMsg->MyQEntry->Recipient),
499                        ((CitContext*)SendMsg->IO.CitContext)->cs_pid);
500                 if (SendMsg->pCurrRelay == NULL)
501                         QueueEventContext(&SendMsg->IO,
502                                           resolve_mx_records);
503                 else { /* oh... via relay host */
504                         if (SendMsg->pCurrRelay->IsIP) {
505                                 QueueEventContext(&SendMsg->IO,
506                                                   mx_connect_ip);
507                         }
508                         else { /* uneducated admin has chosen to add DNS to the equation... */
509                                 QueueEventContext(&SendMsg->IO,
510                                                   get_one_mx_host_ip);
511                         }
512                 }
513         }
514         else {
515                 /* No recipients? well fail then. */
516                 if ((SendMsg==NULL) || 
517                     (SendMsg->MyQEntry == NULL)) {
518                         SendMsg->MyQEntry->Status = 5;
519                         StrBufPlain(SendMsg->MyQEntry->StatusMessage, 
520                                     HKEY("Invalid Recipient!"));
521                 }
522                 FinalizeMessageSend(SendMsg);
523         }
524 }
525
526
527
528
529
530
531 /*****************************************************************************/
532 /*                     SMTP CLIENT DISPATCHER                                */
533 /*****************************************************************************/
534
535 void SMTPSetTimeout(eNextState NextTCPState, SmtpOutMsg *pMsg)
536 {
537         double Timeout = 0.0;
538         AsyncIO *IO = &pMsg->IO;
539
540         EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
541
542         switch (NextTCPState) {
543         case eSendFile:
544         case eSendReply:
545         case eSendMore:
546                 Timeout = SMTP_C_SendTimeouts[pMsg->State];
547                 if (pMsg->State == eDATABody) {
548                         /* if we're sending a huge message, we need more time. */
549                         Timeout += StrLength(pMsg->msgtext) / 1024;
550                 }
551                 break;
552         case eReadMessage:
553                 Timeout = SMTP_C_ReadTimeouts[pMsg->State];
554                 if (pMsg->State == eDATATerminateBody) {
555                         /* 
556                          * some mailservers take a nap before accepting the message
557                          * content inspection and such.
558                          */
559                         Timeout += StrLength(pMsg->msgtext) / 1024;
560                 }
561                 break;
562         case eSendDNSQuery:
563         case eReadDNSReply:
564         case eDBQuery:
565         case eReadFile:
566         case eReadMore:
567         case eReadPayload:
568         case eConnect:
569         case eTerminateConnection:
570         case eAbort:
571                 return;
572         }
573         SetNextTimeout(&pMsg->IO, Timeout);
574 }
575 eNextState SMTP_C_DispatchReadDone(AsyncIO *IO)
576 {
577         EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
578         SmtpOutMsg *pMsg = IO->Data;
579         eNextState rc;
580
581         rc = ReadHandlers[pMsg->State](pMsg);
582         if (rc != eAbort)
583         {
584                 pMsg->State++;
585                 SMTPSetTimeout(rc, pMsg);
586         }
587         return rc;
588 }
589 eNextState SMTP_C_DispatchWriteDone(AsyncIO *IO)
590 {
591         EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
592         SmtpOutMsg *pMsg = IO->Data;
593         eNextState rc;
594
595         rc = SendHandlers[pMsg->State](pMsg);
596         SMTPSetTimeout(rc, pMsg);
597         return rc;
598 }
599
600
601 /*****************************************************************************/
602 /*                     SMTP CLIENT ERROR CATCHERS                            */
603 /*****************************************************************************/
604 eNextState SMTP_C_Terminate(AsyncIO *IO)
605 {
606         SmtpOutMsg *pMsg = IO->Data;
607
608         EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
609         FinalizeMessageSend(pMsg);
610         return eAbort;
611 }
612 eNextState SMTP_C_Timeout(AsyncIO *IO)
613 {
614         SmtpOutMsg *pMsg = IO->Data;
615
616         EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
617         StrBufPlain(IO->ErrMsg, CKEY(ReadErrors[pMsg->State]));
618         return FailOneAttempt(IO);
619 }
620 eNextState SMTP_C_ConnFail(AsyncIO *IO)
621 {
622         SmtpOutMsg *pMsg = IO->Data;
623
624         EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
625         StrBufPlain(IO->ErrMsg, CKEY(ReadErrors[pMsg->State]));
626         return FailOneAttempt(IO);
627 }
628 eNextState SMTP_C_DNSFail(AsyncIO *IO)
629 {
630         SmtpOutMsg *pMsg = IO->Data;
631
632         EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
633         return FailOneAttempt(IO);
634 }
635 eNextState SMTP_C_Shutdown(AsyncIO *IO)
636 {
637         EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
638         SmtpOutMsg *pMsg = IO->Data;
639
640         pMsg->MyQEntry->Status = 3;
641         StrBufPlain(pMsg->MyQEntry->StatusMessage, HKEY("server shutdown during message submit."));
642         FinalizeMessageSend(pMsg);
643         return eAbort;
644 }
645
646
647 /**
648  * @brief lineread Handler; understands when to read more SMTP lines, and when this is a one-lined reply.
649  */
650 eReadState SMTP_C_ReadServerStatus(AsyncIO *IO)
651 {
652         eReadState Finished = eBufferNotEmpty; 
653
654         while (Finished == eBufferNotEmpty) {
655                 Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf);
656                 
657                 switch (Finished) {
658                 case eMustReadMore: /// read new from socket... 
659                         return Finished;
660                         break;
661                 case eBufferNotEmpty: /* shouldn't happen... */
662                 case eReadSuccess: /// done for now...
663                         if (StrLength(IO->IOBuf) < 4)
664                                 continue;
665                         if (ChrPtr(IO->IOBuf)[3] == '-')
666                                 Finished = eBufferNotEmpty;
667                         else 
668                                 return Finished;
669                         break;
670                 case eReadFail: /// WHUT?
671                         ///todo: shut down! 
672                         break;
673                 }
674         }
675         return Finished;
676 }
677
678 CTDL_MODULE_INIT(smtp_eventclient)
679 {
680         return "smtpeventclient";
681 }