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