2 * This module is an SMTP and ESMTP implementation for the Citadel system.
3 * It is compliant with all of the following:
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
20 * The VRFY and EXPN commands have been removed from this implementation
21 * because nobody uses these commands anymore, except for spammers.
23 * Copyright (c) 1998-2009 by the citadel.org team
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.
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.
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
49 #include <sys/types.h>
52 #if TIME_WITH_SYS_TIME
53 # include <sys/time.h>
57 # include <sys/time.h>
66 #include <sys/socket.h>
67 #include <netinet/in.h>
68 #include <arpa/inet.h>
69 #include <libcitadel.h>
72 #include "citserver.h"
79 #include "internet_addressing.h"
82 #include "clientsocket.h"
83 #include "locate_host.h"
84 #include "citadel_dirs.h"
86 #include "ctdl_module.h"
88 #include "smtp_util.h"
89 #include "event_client.h"
90 #include "smtpqueue.h"
92 #ifdef EXPERIMENTAL_SMTP_EVENT_CLIENT
93 /*****************************************************************************/
94 /* SMTP CLIENT (OUTBOUND PROCESSING) STUFF */
95 /*****************************************************************************/
97 typedef enum _eSMTP_C_States {
111 const double SMTP_C_ConnTimeout = 300.; /* wail 1 minute for connections... */
112 const double SMTP_C_ReadTimeouts[eMaxSMTPC] = {
113 300., /* Greeting... */
121 90., /* end of body... */
124 const double SMTP_C_SendTimeouts[eMaxSMTPC] = {
125 90., /* Greeting... */
133 900., /* end of body... */
137 static const ConstStr ReadErrors[eMaxSMTPC] = {
138 {HKEY("Connection broken during SMTP conversation")},
139 {HKEY("Connection broken during SMTP EHLO")},
140 {HKEY("Connection broken during SMTP HELO")},
141 {HKEY("Connection broken during SMTP AUTH")},
142 {HKEY("Connection broken during SMTP MAIL FROM")},
143 {HKEY("Connection broken during SMTP RCPT")},
144 {HKEY("Connection broken during SMTP DATA")},
145 {HKEY("Connection broken during SMTP message transmit")},
146 {HKEY("")}/* quit reply, don't care. */
150 typedef struct _stmp_out_msg {
151 MailQEntry *MyQEntry;
156 eSMTP_C_States State;
158 struct ares_mx_reply *AllMX;
159 struct ares_mx_reply *CurrMX;
163 struct hostent *OneMX;
166 ParsedURL *pCurrRelay;
175 void DeleteSmtpOutMsg(void *v)
179 ares_free_data(Msg->AllMX);
181 FreeStrBuf(&Msg->msgtext);
182 FreeAsyncIOContents(&Msg->IO);
186 eNextState SMTP_C_Timeout(AsyncIO *IO);
187 eNextState SMTP_C_ConnFail(AsyncIO *IO);
188 eNextState SMTP_C_DispatchReadDone(AsyncIO *IO);
189 eNextState SMTP_C_DispatchWriteDone(AsyncIO *IO);
190 eNextState SMTP_C_Terminate(AsyncIO *IO);
191 eReadState SMTP_C_ReadServerStatus(AsyncIO *IO);
193 typedef eNextState (*SMTPReadHandler)(SmtpOutMsg *Msg);
194 typedef eNextState (*SMTPSendHandler)(SmtpOutMsg *Msg);
197 #define SMTP_ERROR(WHICH_ERR, ERRSTR) do {\
198 SendMsg->MyQEntry->Status = WHICH_ERR; \
199 StrBufAppendBufPlain(SendMsg->MyQEntry->StatusMessage, HKEY(ERRSTR), 0); \
203 #define SMTP_VERROR(WHICH_ERR) do {\
204 SendMsg->MyQEntry->Status = WHICH_ERR; \
205 StrBufPlain(SendMsg->MyQEntry->StatusMessage, \
206 ChrPtr(SendMsg->IO.IOBuf) + 4, \
207 StrLength(SendMsg->IO.IOBuf) - 4); \
211 #define SMTP_IS_STATE(WHICH_STATE) (ChrPtr(SendMsg->IO.IOBuf)[0] == WHICH_STATE)
213 #define SMTP_DBG_SEND() CtdlLogPrintf(CTDL_DEBUG, "SMTP client[%ld]: > %s\n", SendMsg->n, ChrPtr(SendMsg->IO.SendBuf.Buf))
214 #define SMTP_DBG_READ() CtdlLogPrintf(CTDL_DEBUG, "SMTP client[%ld]: < %s\n", SendMsg->n, ChrPtr(SendMsg->IO.IOBuf))
217 void FinalizeMessageSend(SmtpOutMsg *Msg)
219 CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
221 if (DecreaseQReference(Msg->MyQItem))
226 nRemain = CountActiveQueueEntries(Msg->MyQItem);
228 MsgData = SerializeQueueItem(Msg->MyQItem);
230 * Uncompleted delivery instructions remain, so delete the old
231 * instructions and replace with the updated ones.
233 CtdlDeleteMessages(SMTP_SPOOLOUT_ROOM, &Msg->MyQItem->QueMsgID, 1, "");
234 smtpq_do_bounce(Msg->MyQItem,
237 struct CtdlMessage *msg;
238 msg = malloc(sizeof(struct CtdlMessage));
239 memset(msg, 0, sizeof(struct CtdlMessage));
240 msg->cm_magic = CTDLMESSAGE_MAGIC;
241 msg->cm_anon_type = MES_NORMAL;
242 msg->cm_format_type = FMT_RFC822;
243 msg->cm_fields['M'] = SmashStrBuf(&MsgData);
244 CtdlSubmitMsg(msg, NULL, SMTP_SPOOLOUT_ROOM, QP_EADDR);
245 CtdlFreeMessage(msg);
248 CtdlDeleteMessages(SMTP_SPOOLOUT_ROOM, &Msg->MyQItem->MessageID, 1, "");
249 FreeStrBuf(&MsgData);
252 RemoveQItem(Msg->MyQItem);
254 DeleteSmtpOutMsg(Msg);
258 void SetConnectStatus(AsyncIO *IO)
261 SmtpOutMsg *SendMsg = IO->Data;
268 src = &IO->Addr.sin6_addr;
271 struct sockaddr_in *addr = (struct sockaddr_in *)&IO->Addr;
273 src = &addr->sin_addr.s_addr;
276 inet_ntop((IO->IP6)?AF_INET6:AF_INET,
279 if (SendMsg->mx_host == NULL)
280 SendMsg->mx_host = "<no name>";
282 CtdlLogPrintf(CTDL_DEBUG,
283 "SMTP client[%ld]: connecting to %s [%s]:%d ...\n",
289 SendMsg->MyQEntry->Status = 5;
290 StrBufPrintf(SendMsg->MyQEntry->StatusMessage,
291 "Timeout while connecting %s [%s]:%d ",
297 eNextState mx_connect_relay_ip(AsyncIO *IO)
300 SmtpOutMsg *SendMsg = IO->Data;
302 CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
304 IO->IP6 = SendMsg->pCurrRelay->af == AF_INET6;
306 if (SendMsg->pCurrRelay->Port != 0)
307 IO->dport = SendMsg->pCurrRelay->Port;
309 memset(&IO->Addr, 0, sizeof(struct in6_addr));
311 memcpy(&IO->Addr.sin6_addr.s6_addr,
312 &SendMsg->pCurrRelay->Addr,
313 sizeof(struct in6_addr));
315 IO->Addr.sin6_family = AF_INET6;
316 IO->Addr.sin6_port = htons(IO->dport);
319 struct sockaddr_in *addr = (struct sockaddr_in*) &IO->Addr;
320 /* Bypass the ns lookup result like this: IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1"); */
321 memcpy(&addr->sin_addr,///.s_addr,
322 &SendMsg->pCurrRelay->Addr,
323 sizeof(struct in_addr));
325 addr->sin_family = AF_INET;
326 addr->sin_port = htons(IO->dport);
329 SetConnectStatus(IO);
331 return InitEventIO(IO, SendMsg,
333 SMTP_C_ReadTimeouts[0],
337 void get_one_mx_host_ip_done(void *Ctx,
340 struct hostent *hostent)
342 AsyncIO *IO = (AsyncIO *) Ctx;
343 SmtpOutMsg *SendMsg = IO->Data;
344 eNextState State = eAbort;
346 if ((status == ARES_SUCCESS) && (hostent != NULL) ) {
348 IO->IP6 = hostent->h_addrtype == AF_INET6;
351 memset(&IO->Addr, 0, sizeof(struct in6_addr));
353 memcpy(&IO->Addr.sin6_addr.s6_addr,
354 &hostent->h_addr_list[0],
355 sizeof(struct in6_addr));
357 IO->Addr.sin6_family = hostent->h_addrtype;
358 IO->Addr.sin6_port = htons(IO->dport);
361 struct sockaddr_in *addr = (struct sockaddr_in*) &IO->Addr;
362 /* Bypass the ns lookup result like this: IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1"); */
363 // addr->sin_addr.s_addr = htonl((uint32_t)&hostent->h_addr_list[0]);
364 memcpy(&addr->sin_addr.s_addr, hostent->h_addr_list[0], sizeof(uint32_t));
366 addr->sin_family = hostent->h_addrtype;
367 addr->sin_port = htons(IO->dport);
370 SendMsg->IO.HEnt = hostent;
371 SetConnectStatus(IO);
372 State = InitEventIO(IO, SendMsg,
374 SMTP_C_ReadTimeouts[0],
377 if ((State == eAbort) && (IO->sock != -1))
378 SMTP_C_Terminate(IO);
381 const unsigned short DefaultMXPort = 25;
382 eNextState get_one_mx_host_ip(AsyncIO *IO)
384 SmtpOutMsg * SendMsg = IO->Data;
385 const char *Hostname;
390 if (SendMsg->CurrMX) {
391 SendMsg->mx_host = SendMsg->CurrMX->host;
392 SendMsg->CurrMX = SendMsg->CurrMX->next;
395 if (SendMsg->pCurrRelay != NULL) {
396 SendMsg->mx_host = Hostname = SendMsg->pCurrRelay->Host;
397 if (SendMsg->pCurrRelay->Port != 0)
398 SendMsg->IO.dport = SendMsg->pCurrRelay->Port;
400 else if (SendMsg->mx_host != NULL) Hostname = SendMsg->mx_host;
401 else Hostname = SendMsg->node;
403 CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
405 CtdlLogPrintf(CTDL_DEBUG,
406 "SMTP client[%ld]: looking up %s : %d ...\n",
411 ares_gethostbyname(SendMsg->IO.DNSChannel,
413 AF_INET6, /* it falls back to ipv4 in doubt... */
414 get_one_mx_host_ip_done,
416 return IO->NextState;
420 eNextState smtp_resolve_mx_done(AsyncIO *IO)
422 SmtpOutMsg * SendMsg = IO->Data;
424 CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
426 SendMsg->IO.ErrMsg = SendMsg->MyQEntry->StatusMessage;
428 SendMsg->CurrMX = SendMsg->AllMX = IO->VParsedDNSReply;
429 //// TODO: should we remove the current ares context???
430 get_one_mx_host_ip(IO);
431 return IO->NextState;
435 eNextState resolve_mx_records(AsyncIO *IO)
437 SmtpOutMsg * SendMsg = IO->Data;
439 CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
441 if (!QueueQuery(ns_t_mx,
444 smtp_resolve_mx_done))
446 SendMsg->MyQEntry->Status = 5;
447 StrBufPrintf(SendMsg->MyQEntry->StatusMessage,
448 "No MX hosts found for <%s>", SendMsg->node);
449 return IO->NextState;
455 int smtp_resolve_recipients(SmtpOutMsg *SendMsg)
463 CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
465 if ((SendMsg==NULL) ||
466 (SendMsg->MyQEntry == NULL) ||
467 (StrLength(SendMsg->MyQEntry->Recipient) == 0)) {
471 /* Parse out the host portion of the recipient address */
472 process_rfc822_addr(ChrPtr(SendMsg->MyQEntry->Recipient),
477 CtdlLogPrintf(CTDL_DEBUG, "SMTP client[%ld]: Attempting delivery to <%s> @ <%s> (%s)\n",
478 SendMsg->n, SendMsg->user, SendMsg->node, SendMsg->name);
479 /* If no envelope_from is supplied, extract one from the message */
480 if ( (SendMsg->envelope_from == NULL) ||
481 (IsEmptyStr(SendMsg->envelope_from)) ) {
482 SendMsg->mailfrom[0] = '\0';
484 ptr = ChrPtr(SendMsg->msgtext);
486 if (ptr = cmemreadline(ptr, buf, sizeof buf), *ptr == 0) {
489 if (!strncasecmp(buf, "From:", 5)) {
490 safestrncpy(SendMsg->mailfrom, &buf[5], sizeof SendMsg->mailfrom);
491 striplt(SendMsg->mailfrom);
492 for (i=0; SendMsg->mailfrom[i]; ++i) {
493 if (!isprint(SendMsg->mailfrom[i])) {
494 strcpy(&SendMsg->mailfrom[i], &SendMsg->mailfrom[i+1]);
499 /* Strip out parenthesized names */
502 for (i=0; !IsEmptyStr(SendMsg->mailfrom + i); ++i) {
503 if (SendMsg->mailfrom[i] == '(') lp = i;
504 if (SendMsg->mailfrom[i] == ')') rp = i;
506 if ((lp>0)&&(rp>lp)) {
507 strcpy(&SendMsg->mailfrom[lp-1], &SendMsg->mailfrom[rp+1]);
510 /* Prefer brokketized names */
513 for (i=0; !IsEmptyStr(SendMsg->mailfrom + i); ++i) {
514 if (SendMsg->mailfrom[i] == '<') lp = i;
515 if (SendMsg->mailfrom[i] == '>') rp = i;
517 if ( (lp>=0) && (rp>lp) ) {
518 SendMsg->mailfrom[rp] = 0;
519 memmove(SendMsg->mailfrom,
520 &SendMsg->mailfrom[lp + 1],
526 } while (scan_done == 0);
527 if (IsEmptyStr(SendMsg->mailfrom)) strcpy(SendMsg->mailfrom, "someone@somewhere.org");
528 stripallbut(SendMsg->mailfrom, '<', '>');
529 SendMsg->envelope_from = SendMsg->mailfrom;
537 void smtp_try(OneQueItem *MyQItem,
538 MailQEntry *MyQEntry,
540 int KeepMsgText, /* KeepMsgText allows us to use MsgText as ours. */
543 SmtpOutMsg * SendMsg;
545 CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
547 SendMsg = (SmtpOutMsg *) malloc(sizeof(SmtpOutMsg));
548 memset(SendMsg, 0, sizeof(SmtpOutMsg));
549 SendMsg->IO.sock = (-1);
550 SendMsg->IO.NextState = eReadMessage;
551 SendMsg->n = MsgCount++;
552 SendMsg->MyQEntry = MyQEntry;
553 SendMsg->MyQItem = MyQItem;
554 SendMsg->pCurrRelay = MyQItem->URL;
556 SendMsg->IO.dport = DefaultMXPort;
557 SendMsg->IO.Data = SendMsg;
558 SendMsg->IO.SendDone = SMTP_C_DispatchWriteDone;
559 SendMsg->IO.ReadDone = SMTP_C_DispatchReadDone;
560 SendMsg->IO.Terminate = SMTP_C_Terminate;
561 SendMsg->IO.LineReader = SMTP_C_ReadServerStatus;
562 SendMsg->IO.ConnFail = SMTP_C_ConnFail;
563 SendMsg->IO.Timeout = SMTP_C_Timeout;
564 SendMsg->IO.SendBuf.Buf = NewStrBufPlain(NULL, 1024);
565 SendMsg->IO.RecvBuf.Buf = NewStrBufPlain(NULL, 1024);
566 SendMsg->IO.IOBuf = NewStrBuf();
569 SendMsg->msgtext = MsgText;
572 SendMsg->msgtext = NewStrBufDup(MsgText);
575 if (smtp_resolve_recipients(SendMsg)) {
576 if (SendMsg->pCurrRelay == NULL)
577 QueueEventContext(&SendMsg->IO,
580 if (SendMsg->pCurrRelay->IsIP) {
581 QueueEventContext(&SendMsg->IO,
582 mx_connect_relay_ip);
585 QueueEventContext(&SendMsg->IO,
591 if ((SendMsg==NULL) ||
592 (SendMsg->MyQEntry == NULL)) {
593 SendMsg->MyQEntry->Status = 5;
594 StrBufPlain(SendMsg->MyQEntry->StatusMessage,
595 HKEY("Invalid Recipient!"));
597 FinalizeMessageSend(SendMsg);
605 /*****************************************************************************/
606 /* SMTP CLIENT STATE CALLBACKS */
607 /*****************************************************************************/
608 eNextState SMTPC_read_greeting(SmtpOutMsg *SendMsg)
610 /* Process the SMTP greeting from the server */
613 if (!SMTP_IS_STATE('2')) {
614 if (SMTP_IS_STATE('4'))
622 eNextState SMTPC_send_EHLO(SmtpOutMsg *SendMsg)
624 /* At this point we know we are talking to a real SMTP server */
626 /* Do a EHLO command. If it fails, try the HELO command. */
627 StrBufPrintf(SendMsg->IO.SendBuf.Buf,
628 "EHLO %s\r\n", config.c_fqdn);
634 eNextState SMTPC_read_EHLO_reply(SmtpOutMsg *SendMsg)
638 if (SMTP_IS_STATE('2')) {
641 if ((SendMsg->pCurrRelay == NULL) ||
642 (SendMsg->pCurrRelay->User == NULL))
643 SendMsg->State ++; /* Skip auth... */
645 /* else we fall back to 'helo' */
649 eNextState STMPC_send_HELO(SmtpOutMsg *SendMsg)
651 StrBufPrintf(SendMsg->IO.SendBuf.Buf,
652 "HELO %s\r\n", config.c_fqdn);
658 eNextState SMTPC_read_HELO_reply(SmtpOutMsg *SendMsg)
662 if (!SMTP_IS_STATE('2')) {
663 if (SMTP_IS_STATE('4'))
668 if ((SendMsg->pCurrRelay == NULL) ||
669 (SendMsg->pCurrRelay->User == NULL))
670 SendMsg->State ++; /* Skip auth... */
674 eNextState SMTPC_send_auth(SmtpOutMsg *SendMsg)
679 if ((SendMsg->pCurrRelay == NULL) ||
680 (SendMsg->pCurrRelay->User == NULL))
681 SendMsg->State ++; /* Skip auth, shouldn't even come here!... */
683 /* Do an AUTH command if necessary */
684 sprintf(buf, "%s%c%s%c%s",
685 SendMsg->pCurrRelay->User, '\0',
686 SendMsg->pCurrRelay->User, '\0',
687 SendMsg->pCurrRelay->Pass);
688 CtdlEncodeBase64(encoded, buf,
689 strlen(SendMsg->pCurrRelay->User) * 2 +
690 strlen(SendMsg->pCurrRelay->Pass) + 2, 0);
691 StrBufPrintf(SendMsg->IO.SendBuf.Buf,
692 "AUTH PLAIN %s\r\n", encoded);
698 eNextState SMTPC_read_auth_reply(SmtpOutMsg *SendMsg)
700 /* Do an AUTH command if necessary */
704 if (!SMTP_IS_STATE('2')) {
705 if (SMTP_IS_STATE('4'))
713 eNextState SMTPC_send_FROM(SmtpOutMsg *SendMsg)
715 /* previous command succeeded, now try the MAIL FROM: command */
716 StrBufPrintf(SendMsg->IO.SendBuf.Buf,
717 "MAIL FROM:<%s>\r\n",
718 SendMsg->envelope_from);
724 eNextState SMTPC_read_FROM_reply(SmtpOutMsg *SendMsg)
728 if (!SMTP_IS_STATE('2')) {
729 if (SMTP_IS_STATE('4'))
738 eNextState SMTPC_send_RCPT(SmtpOutMsg *SendMsg)
740 /* MAIL succeeded, now try the RCPT To: command */
741 StrBufPrintf(SendMsg->IO.SendBuf.Buf,
742 "RCPT TO:<%s@%s>\r\n",
750 eNextState SMTPC_read_RCPT_reply(SmtpOutMsg *SendMsg)
754 if (!SMTP_IS_STATE('2')) {
755 if (SMTP_IS_STATE('4'))
763 eNextState SMTPC_send_DATAcmd(SmtpOutMsg *SendMsg)
765 /* RCPT succeeded, now try the DATA command */
766 StrBufPlain(SendMsg->IO.SendBuf.Buf,
773 eNextState SMTPC_read_DATAcmd_reply(SmtpOutMsg *SendMsg)
777 if (!SMTP_IS_STATE('3')) {
778 if (SMTP_IS_STATE('4'))
786 eNextState SMTPC_send_data_body(SmtpOutMsg *SendMsg)
789 /* If we reach this point, the server is expecting data.*/
791 Buf = SendMsg->IO.SendBuf.Buf;
792 SendMsg->IO.SendBuf.Buf = SendMsg->msgtext;
793 SendMsg->msgtext = Buf;
799 eNextState SMTPC_send_terminate_data_body(SmtpOutMsg *SendMsg)
803 Buf = SendMsg->IO.SendBuf.Buf;
804 SendMsg->IO.SendBuf.Buf = SendMsg->msgtext;
805 SendMsg->msgtext = Buf;
807 StrBufPlain(SendMsg->IO.SendBuf.Buf,
814 eNextState SMTPC_read_data_body_reply(SmtpOutMsg *SendMsg)
818 if (!SMTP_IS_STATE('2')) {
819 if (SMTP_IS_STATE('4'))
826 StrBufPlain(SendMsg->MyQEntry->StatusMessage,
827 &ChrPtr(SendMsg->IO.RecvBuf.Buf)[4],
828 StrLength(SendMsg->IO.RecvBuf.Buf) - 4);
829 SendMsg->MyQEntry->Status = 2;
833 eNextState SMTPC_send_QUIT(SmtpOutMsg *SendMsg)
835 StrBufPlain(SendMsg->IO.SendBuf.Buf,
842 eNextState SMTPC_read_QUIT_reply(SmtpOutMsg *SendMsg)
846 CtdlLogPrintf(CTDL_INFO, "SMTP client[%ld]: delivery to <%s> @ <%s> (%s) succeeded\n",
847 SendMsg->n, SendMsg->user, SendMsg->node, SendMsg->name);
848 return eTerminateConnection;
851 eNextState SMTPC_read_dummy(SmtpOutMsg *SendMsg)
856 eNextState SMTPC_send_dummy(SmtpOutMsg *SendMsg)
862 /*****************************************************************************/
863 /* SMTP CLIENT DISPATCHER */
864 /*****************************************************************************/
865 SMTPReadHandler ReadHandlers[eMaxSMTPC] = {
867 SMTPC_read_EHLO_reply,
868 SMTPC_read_HELO_reply,
869 SMTPC_read_auth_reply,
870 SMTPC_read_FROM_reply,
871 SMTPC_read_RCPT_reply,
872 SMTPC_read_DATAcmd_reply,
874 SMTPC_read_data_body_reply,
875 SMTPC_read_QUIT_reply
877 SMTPSendHandler SendHandlers[eMaxSMTPC] = {
878 SMTPC_send_dummy, /* we don't send a greeting, the server does... */
885 SMTPC_send_data_body,
886 SMTPC_send_terminate_data_body,
890 void SMTPSetTimeout(eNextState NextTCPState, SmtpOutMsg *pMsg)
892 CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
894 switch (NextTCPState) {
897 Timeout = SMTP_C_SendTimeouts[pMsg->State];
898 if (pMsg->State == eDATABody) {
899 /* if we're sending a huge message, we need more time. */
900 Timeout += StrLength(pMsg->msgtext) / 1024;
904 Timeout = SMTP_C_ReadTimeouts[pMsg->State];
905 if (pMsg->State == eDATATerminateBody) {
907 * some mailservers take a nap before accepting the message
908 * content inspection and such.
910 Timeout += StrLength(pMsg->msgtext) / 1024;
913 case eTerminateConnection:
917 SetNextTimeout(&pMsg->IO, Timeout);
919 eNextState SMTP_C_DispatchReadDone(AsyncIO *IO)
921 CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
922 SmtpOutMsg *pMsg = IO->Data;
925 rc = ReadHandlers[pMsg->State](pMsg);
927 SMTPSetTimeout(rc, pMsg);
930 eNextState SMTP_C_DispatchWriteDone(AsyncIO *IO)
932 CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
933 SmtpOutMsg *pMsg = IO->Data;
936 rc = SendHandlers[pMsg->State](pMsg);
937 SMTPSetTimeout(rc, pMsg);
942 /*****************************************************************************/
943 /* SMTP CLIENT ERROR CATCHERS */
944 /*****************************************************************************/
945 eNextState SMTP_C_Terminate(AsyncIO *IO)
947 CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
948 SmtpOutMsg *pMsg = IO->Data;
949 FinalizeMessageSend(pMsg);
952 eNextState SMTP_C_Timeout(AsyncIO *IO)
954 CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
955 SmtpOutMsg *pMsg = IO->Data;
956 StrBufPlain(IO->ErrMsg, CKEY(ReadErrors[pMsg->State]));
957 FinalizeMessageSend(pMsg);
960 eNextState SMTP_C_ConnFail(AsyncIO *IO)
962 CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
963 SmtpOutMsg *pMsg = IO->Data;
964 FinalizeMessageSend(pMsg);
970 * @brief lineread Handler; understands when to read more SMTP lines, and when this is a one-lined reply.
972 eReadState SMTP_C_ReadServerStatus(AsyncIO *IO)
974 eReadState Finished = eBufferNotEmpty;
976 while (Finished == eBufferNotEmpty) {
977 Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf);
980 case eMustReadMore: /// read new from socket...
983 case eBufferNotEmpty: /* shouldn't happen... */
984 case eReadSuccess: /// done for now...
985 if (StrLength(IO->IOBuf) < 4)
987 if (ChrPtr(IO->IOBuf)[3] == '-')
988 Finished = eBufferNotEmpty;
992 case eReadFail: /// WHUT?
1001 CTDL_MODULE_INIT(smtp_eventclient)
1003 return "smtpeventclient";