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