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_Shutdown(AsyncIO *IO);
187 eNextState SMTP_C_Timeout(AsyncIO *IO);
188 eNextState SMTP_C_ConnFail(AsyncIO *IO);
189 eNextState SMTP_C_DispatchReadDone(AsyncIO *IO);
190 eNextState SMTP_C_DispatchWriteDone(AsyncIO *IO);
191 eNextState SMTP_C_Terminate(AsyncIO *IO);
192 eReadState SMTP_C_ReadServerStatus(AsyncIO *IO);
194 typedef eNextState (*SMTPReadHandler)(SmtpOutMsg *Msg);
195 typedef eNextState (*SMTPSendHandler)(SmtpOutMsg *Msg);
198 #define SMTP_ERROR(WHICH_ERR, ERRSTR) do {\
199 SendMsg->MyQEntry->Status = WHICH_ERR; \
200 StrBufAppendBufPlain(SendMsg->MyQEntry->StatusMessage, HKEY(ERRSTR), 0); \
204 #define SMTP_VERROR(WHICH_ERR) do {\
205 SendMsg->MyQEntry->Status = WHICH_ERR; \
206 StrBufPlain(SendMsg->MyQEntry->StatusMessage, \
207 ChrPtr(SendMsg->IO.IOBuf) + 4, \
208 StrLength(SendMsg->IO.IOBuf) - 4); \
212 #define SMTP_IS_STATE(WHICH_STATE) (ChrPtr(SendMsg->IO.IOBuf)[0] == WHICH_STATE)
214 #define SMTP_DBG_SEND() CtdlLogPrintf(CTDL_DEBUG, "SMTP client[%ld]: > %s\n", SendMsg->n, ChrPtr(SendMsg->IO.SendBuf.Buf))
215 #define SMTP_DBG_READ() CtdlLogPrintf(CTDL_DEBUG, "SMTP client[%ld]: < %s\n", SendMsg->n, ChrPtr(SendMsg->IO.IOBuf))
218 void FinalizeMessageSend(SmtpOutMsg *Msg)
220 CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
222 if (DecreaseQReference(Msg->MyQItem))
227 nRemain = CountActiveQueueEntries(Msg->MyQItem);
229 MsgData = SerializeQueueItem(Msg->MyQItem);
231 * Uncompleted delivery instructions remain, so delete the old
232 * instructions and replace with the updated ones.
234 CtdlDeleteMessages(SMTP_SPOOLOUT_ROOM, &Msg->MyQItem->QueMsgID, 1, "");
235 smtpq_do_bounce(Msg->MyQItem,
238 struct CtdlMessage *msg;
239 msg = malloc(sizeof(struct CtdlMessage));
240 memset(msg, 0, sizeof(struct CtdlMessage));
241 msg->cm_magic = CTDLMESSAGE_MAGIC;
242 msg->cm_anon_type = MES_NORMAL;
243 msg->cm_format_type = FMT_RFC822;
244 msg->cm_fields['M'] = SmashStrBuf(&MsgData);
245 CtdlSubmitMsg(msg, NULL, SMTP_SPOOLOUT_ROOM, QP_EADDR);
246 CtdlFreeMessage(msg);
249 CtdlDeleteMessages(SMTP_SPOOLOUT_ROOM, &Msg->MyQItem->MessageID, 1, "");
250 FreeStrBuf(&MsgData);
253 RemoveQItem(Msg->MyQItem);
255 DeleteSmtpOutMsg(Msg);
259 void SetConnectStatus(AsyncIO *IO)
262 SmtpOutMsg *SendMsg = IO->Data;
269 src = &IO->Addr.sin6_addr;
272 struct sockaddr_in *addr = (struct sockaddr_in *)&IO->Addr;
274 src = &addr->sin_addr.s_addr;
277 inet_ntop((IO->IP6)?AF_INET6:AF_INET,
280 if (SendMsg->mx_host == NULL)
281 SendMsg->mx_host = "<no name>";
283 CtdlLogPrintf(CTDL_DEBUG,
284 "SMTP client[%ld]: connecting to %s [%s]:%d ...\n",
290 SendMsg->MyQEntry->Status = 5;
291 StrBufPrintf(SendMsg->MyQEntry->StatusMessage,
292 "Timeout while connecting %s [%s]:%d ",
298 eNextState mx_connect_relay_ip(AsyncIO *IO)
301 SmtpOutMsg *SendMsg = IO->Data;
303 CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
305 IO->IP6 = SendMsg->pCurrRelay->af == AF_INET6;
307 if (SendMsg->pCurrRelay->Port != 0)
308 IO->dport = SendMsg->pCurrRelay->Port;
310 memset(&IO->Addr, 0, sizeof(struct sockaddr_in6));
312 memcpy(&IO->Addr.sin6_addr.s6_addr,
313 &SendMsg->pCurrRelay->Addr,
314 sizeof(struct in6_addr));
316 IO->Addr.sin6_family = AF_INET6;
317 IO->Addr.sin6_port = htons(IO->dport);
320 struct sockaddr_in *addr = (struct sockaddr_in*) &IO->Addr;
321 /* Bypass the ns lookup result like this: IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1"); */
322 memcpy(&addr->sin_addr,///.s_addr,
323 &SendMsg->pCurrRelay->Addr,
324 sizeof(struct in_addr));
326 addr->sin_family = AF_INET;
327 addr->sin_port = htons(IO->dport);
330 SetConnectStatus(IO);
332 return InitEventIO(IO, SendMsg,
334 SMTP_C_ReadTimeouts[0],
338 void get_one_mx_host_ip_done(void *Ctx,
341 struct hostent *hostent)
343 AsyncIO *IO = (AsyncIO *) Ctx;
344 SmtpOutMsg *SendMsg = IO->Data;
345 eNextState State = eAbort;
347 if ((status == ARES_SUCCESS) && (hostent != NULL) ) {
349 IO->IP6 = hostent->h_addrtype == AF_INET6;
352 memset(&IO->Addr, 0, sizeof(struct in6_addr));
354 memcpy(&IO->Addr.sin6_addr.s6_addr,
355 &hostent->h_addr_list[0],
356 sizeof(struct in6_addr));
358 IO->Addr.sin6_family = hostent->h_addrtype;
359 IO->Addr.sin6_port = htons(IO->dport);
362 struct sockaddr_in *addr = (struct sockaddr_in*) &IO->Addr;
363 /* Bypass the ns lookup result like this: IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1"); */
364 // addr->sin_addr.s_addr = htonl((uint32_t)&hostent->h_addr_list[0]);
365 memcpy(&addr->sin_addr.s_addr, hostent->h_addr_list[0], sizeof(uint32_t));
367 addr->sin_family = hostent->h_addrtype;
368 addr->sin_port = htons(IO->dport);
371 SendMsg->IO.HEnt = hostent;
372 SetConnectStatus(IO);
373 State = InitEventIO(IO, SendMsg,
375 SMTP_C_ReadTimeouts[0],
378 if ((State == eAbort) && (IO->sock != -1))
379 SMTP_C_Terminate(IO);
382 const unsigned short DefaultMXPort = 25;
383 eNextState get_one_mx_host_ip(AsyncIO *IO)
385 SmtpOutMsg * SendMsg = IO->Data;
386 const char *Hostname;
391 if (SendMsg->CurrMX) {
392 SendMsg->mx_host = SendMsg->CurrMX->host;
393 SendMsg->CurrMX = SendMsg->CurrMX->next;
396 if (SendMsg->pCurrRelay != NULL) {
397 SendMsg->mx_host = Hostname = SendMsg->pCurrRelay->Host;
398 if (SendMsg->pCurrRelay->Port != 0)
399 SendMsg->IO.dport = SendMsg->pCurrRelay->Port;
401 else if (SendMsg->mx_host != NULL) Hostname = SendMsg->mx_host;
402 else Hostname = SendMsg->node;
404 CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
406 CtdlLogPrintf(CTDL_DEBUG,
407 "SMTP client[%ld]: looking up %s : %d ...\n",
412 ares_gethostbyname(SendMsg->IO.DNSChannel,
414 AF_INET6, /* it falls back to ipv4 in doubt... */
415 get_one_mx_host_ip_done,
417 return IO->NextState;
421 eNextState smtp_resolve_mx_done(AsyncIO *IO)
423 SmtpOutMsg * SendMsg = IO->Data;
427 CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
429 SendMsg->IO.ErrMsg = SendMsg->MyQEntry->StatusMessage;
431 SendMsg->CurrMX = SendMsg->AllMX = IO->VParsedDNSReply;
432 //// TODO: should we remove the current ares context???
433 get_one_mx_host_ip(IO);
434 return IO->NextState;
438 eNextState resolve_mx_records(AsyncIO *IO)
440 SmtpOutMsg * SendMsg = IO->Data;
442 CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
444 if (!QueueQuery(ns_t_mx,
447 smtp_resolve_mx_done))
449 SendMsg->MyQEntry->Status = 5;
450 StrBufPrintf(SendMsg->MyQEntry->StatusMessage,
451 "No MX hosts found for <%s>", SendMsg->node);
452 return IO->NextState;
458 int smtp_resolve_recipients(SmtpOutMsg *SendMsg)
466 CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
468 if ((SendMsg==NULL) ||
469 (SendMsg->MyQEntry == NULL) ||
470 (StrLength(SendMsg->MyQEntry->Recipient) == 0)) {
474 /* Parse out the host portion of the recipient address */
475 process_rfc822_addr(ChrPtr(SendMsg->MyQEntry->Recipient),
480 CtdlLogPrintf(CTDL_DEBUG, "SMTP client[%ld]: Attempting delivery to <%s> @ <%s> (%s)\n",
481 SendMsg->n, SendMsg->user, SendMsg->node, SendMsg->name);
482 /* If no envelope_from is supplied, extract one from the message */
483 if ( (SendMsg->envelope_from == NULL) ||
484 (IsEmptyStr(SendMsg->envelope_from)) ) {
485 SendMsg->mailfrom[0] = '\0';
487 ptr = ChrPtr(SendMsg->msgtext);
489 if (ptr = cmemreadline(ptr, buf, sizeof buf), *ptr == 0) {
492 if (!strncasecmp(buf, "From:", 5)) {
493 safestrncpy(SendMsg->mailfrom, &buf[5], sizeof SendMsg->mailfrom);
494 striplt(SendMsg->mailfrom);
495 for (i=0; SendMsg->mailfrom[i]; ++i) {
496 if (!isprint(SendMsg->mailfrom[i])) {
497 strcpy(&SendMsg->mailfrom[i], &SendMsg->mailfrom[i+1]);
502 /* Strip out parenthesized names */
505 for (i=0; !IsEmptyStr(SendMsg->mailfrom + i); ++i) {
506 if (SendMsg->mailfrom[i] == '(') lp = i;
507 if (SendMsg->mailfrom[i] == ')') rp = i;
509 if ((lp>0)&&(rp>lp)) {
510 strcpy(&SendMsg->mailfrom[lp-1], &SendMsg->mailfrom[rp+1]);
513 /* Prefer brokketized names */
516 for (i=0; !IsEmptyStr(SendMsg->mailfrom + i); ++i) {
517 if (SendMsg->mailfrom[i] == '<') lp = i;
518 if (SendMsg->mailfrom[i] == '>') rp = i;
520 if ( (lp>=0) && (rp>lp) ) {
521 SendMsg->mailfrom[rp] = 0;
522 memmove(SendMsg->mailfrom,
523 &SendMsg->mailfrom[lp + 1],
529 } while (scan_done == 0);
530 if (IsEmptyStr(SendMsg->mailfrom)) strcpy(SendMsg->mailfrom, "someone@somewhere.org");
531 stripallbut(SendMsg->mailfrom, '<', '>');
532 SendMsg->envelope_from = SendMsg->mailfrom;
540 void smtp_try(OneQueItem *MyQItem,
541 MailQEntry *MyQEntry,
543 int KeepMsgText, /* KeepMsgText allows us to use MsgText as ours. */
546 SmtpOutMsg * SendMsg;
548 CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
550 SendMsg = (SmtpOutMsg *) malloc(sizeof(SmtpOutMsg));
551 memset(SendMsg, 0, sizeof(SmtpOutMsg));
552 SendMsg->IO.sock = (-1);
553 SendMsg->IO.NextState = eReadMessage;
554 SendMsg->n = MsgCount++;
555 SendMsg->MyQEntry = MyQEntry;
556 SendMsg->MyQItem = MyQItem;
557 SendMsg->pCurrRelay = MyQItem->URL;
559 SendMsg->IO.dport = DefaultMXPort;
560 SendMsg->IO.Data = SendMsg;
561 SendMsg->IO.SendDone = SMTP_C_DispatchWriteDone;
562 SendMsg->IO.ReadDone = SMTP_C_DispatchReadDone;
563 SendMsg->IO.Terminate = SMTP_C_Terminate;
564 SendMsg->IO.LineReader = SMTP_C_ReadServerStatus;
565 SendMsg->IO.ConnFail = SMTP_C_ConnFail;
566 SendMsg->IO.Timeout = SMTP_C_Timeout;
567 SendMsg->IO.ShutdownAbort = SMTP_C_Shutdown;
568 SendMsg->IO.SendBuf.Buf = NewStrBufPlain(NULL, 1024);
569 SendMsg->IO.RecvBuf.Buf = NewStrBufPlain(NULL, 1024);
570 SendMsg->IO.IOBuf = NewStrBuf();
574 SendMsg->msgtext = MsgText;
577 SendMsg->msgtext = NewStrBufDup(MsgText);
580 if (smtp_resolve_recipients(SendMsg)) {
581 if (SendMsg->pCurrRelay == NULL)
582 QueueEventContext(&SendMsg->IO,
585 if (SendMsg->pCurrRelay->IsIP) {
586 QueueEventContext(&SendMsg->IO,
587 mx_connect_relay_ip);
590 QueueEventContext(&SendMsg->IO,
596 if ((SendMsg==NULL) ||
597 (SendMsg->MyQEntry == NULL)) {
598 SendMsg->MyQEntry->Status = 5;
599 StrBufPlain(SendMsg->MyQEntry->StatusMessage,
600 HKEY("Invalid Recipient!"));
602 FinalizeMessageSend(SendMsg);
610 /*****************************************************************************/
611 /* SMTP CLIENT STATE CALLBACKS */
612 /*****************************************************************************/
613 eNextState SMTPC_read_greeting(SmtpOutMsg *SendMsg)
615 /* Process the SMTP greeting from the server */
618 if (!SMTP_IS_STATE('2')) {
619 if (SMTP_IS_STATE('4'))
627 eNextState SMTPC_send_EHLO(SmtpOutMsg *SendMsg)
629 /* At this point we know we are talking to a real SMTP server */
631 /* Do a EHLO command. If it fails, try the HELO command. */
632 StrBufPrintf(SendMsg->IO.SendBuf.Buf,
633 "EHLO %s\r\n", config.c_fqdn);
639 eNextState SMTPC_read_EHLO_reply(SmtpOutMsg *SendMsg)
643 if (SMTP_IS_STATE('2')) {
646 if ((SendMsg->pCurrRelay == NULL) ||
647 (SendMsg->pCurrRelay->User == NULL))
648 SendMsg->State ++; /* Skip auth... */
650 /* else we fall back to 'helo' */
654 eNextState STMPC_send_HELO(SmtpOutMsg *SendMsg)
656 StrBufPrintf(SendMsg->IO.SendBuf.Buf,
657 "HELO %s\r\n", config.c_fqdn);
663 eNextState SMTPC_read_HELO_reply(SmtpOutMsg *SendMsg)
667 if (!SMTP_IS_STATE('2')) {
668 if (SMTP_IS_STATE('4'))
673 if ((SendMsg->pCurrRelay == NULL) ||
674 (SendMsg->pCurrRelay->User == NULL))
675 SendMsg->State ++; /* Skip auth... */
679 eNextState SMTPC_send_auth(SmtpOutMsg *SendMsg)
684 if ((SendMsg->pCurrRelay == NULL) ||
685 (SendMsg->pCurrRelay->User == NULL))
686 SendMsg->State ++; /* Skip auth, shouldn't even come here!... */
688 /* Do an AUTH command if necessary */
689 sprintf(buf, "%s%c%s%c%s",
690 SendMsg->pCurrRelay->User, '\0',
691 SendMsg->pCurrRelay->User, '\0',
692 SendMsg->pCurrRelay->Pass);
693 CtdlEncodeBase64(encoded, buf,
694 strlen(SendMsg->pCurrRelay->User) * 2 +
695 strlen(SendMsg->pCurrRelay->Pass) + 2, 0);
696 StrBufPrintf(SendMsg->IO.SendBuf.Buf,
697 "AUTH PLAIN %s\r\n", encoded);
703 eNextState SMTPC_read_auth_reply(SmtpOutMsg *SendMsg)
705 /* Do an AUTH command if necessary */
709 if (!SMTP_IS_STATE('2')) {
710 if (SMTP_IS_STATE('4'))
718 eNextState SMTPC_send_FROM(SmtpOutMsg *SendMsg)
720 /* previous command succeeded, now try the MAIL FROM: command */
721 StrBufPrintf(SendMsg->IO.SendBuf.Buf,
722 "MAIL FROM:<%s>\r\n",
723 SendMsg->envelope_from);
729 eNextState SMTPC_read_FROM_reply(SmtpOutMsg *SendMsg)
733 if (!SMTP_IS_STATE('2')) {
734 if (SMTP_IS_STATE('4'))
743 eNextState SMTPC_send_RCPT(SmtpOutMsg *SendMsg)
745 /* MAIL succeeded, now try the RCPT To: command */
746 StrBufPrintf(SendMsg->IO.SendBuf.Buf,
747 "RCPT TO:<%s@%s>\r\n",
755 eNextState SMTPC_read_RCPT_reply(SmtpOutMsg *SendMsg)
759 if (!SMTP_IS_STATE('2')) {
760 if (SMTP_IS_STATE('4'))
768 eNextState SMTPC_send_DATAcmd(SmtpOutMsg *SendMsg)
770 /* RCPT succeeded, now try the DATA command */
771 StrBufPlain(SendMsg->IO.SendBuf.Buf,
778 eNextState SMTPC_read_DATAcmd_reply(SmtpOutMsg *SendMsg)
782 if (!SMTP_IS_STATE('3')) {
783 if (SMTP_IS_STATE('4'))
791 eNextState SMTPC_send_data_body(SmtpOutMsg *SendMsg)
794 /* If we reach this point, the server is expecting data.*/
796 Buf = SendMsg->IO.SendBuf.Buf;
797 SendMsg->IO.SendBuf.Buf = SendMsg->msgtext;
798 SendMsg->msgtext = Buf;
804 eNextState SMTPC_send_terminate_data_body(SmtpOutMsg *SendMsg)
808 Buf = SendMsg->IO.SendBuf.Buf;
809 SendMsg->IO.SendBuf.Buf = SendMsg->msgtext;
810 SendMsg->msgtext = Buf;
812 StrBufPlain(SendMsg->IO.SendBuf.Buf,
819 eNextState SMTPC_read_data_body_reply(SmtpOutMsg *SendMsg)
823 if (!SMTP_IS_STATE('2')) {
824 if (SMTP_IS_STATE('4'))
831 StrBufPlain(SendMsg->MyQEntry->StatusMessage,
832 &ChrPtr(SendMsg->IO.RecvBuf.Buf)[4],
833 StrLength(SendMsg->IO.RecvBuf.Buf) - 4);
834 SendMsg->MyQEntry->Status = 2;
838 eNextState SMTPC_send_QUIT(SmtpOutMsg *SendMsg)
840 StrBufPlain(SendMsg->IO.SendBuf.Buf,
847 eNextState SMTPC_read_QUIT_reply(SmtpOutMsg *SendMsg)
851 CtdlLogPrintf(CTDL_INFO, "SMTP client[%ld]: delivery to <%s> @ <%s> (%s) succeeded\n",
852 SendMsg->n, SendMsg->user, SendMsg->node, SendMsg->name);
853 return eTerminateConnection;
856 eNextState SMTPC_read_dummy(SmtpOutMsg *SendMsg)
861 eNextState SMTPC_send_dummy(SmtpOutMsg *SendMsg)
867 /*****************************************************************************/
868 /* SMTP CLIENT DISPATCHER */
869 /*****************************************************************************/
870 SMTPReadHandler ReadHandlers[eMaxSMTPC] = {
872 SMTPC_read_EHLO_reply,
873 SMTPC_read_HELO_reply,
874 SMTPC_read_auth_reply,
875 SMTPC_read_FROM_reply,
876 SMTPC_read_RCPT_reply,
877 SMTPC_read_DATAcmd_reply,
879 SMTPC_read_data_body_reply,
880 SMTPC_read_QUIT_reply
882 SMTPSendHandler SendHandlers[eMaxSMTPC] = {
883 SMTPC_send_dummy, /* we don't send a greeting, the server does... */
890 SMTPC_send_data_body,
891 SMTPC_send_terminate_data_body,
895 void SMTPSetTimeout(eNextState NextTCPState, SmtpOutMsg *pMsg)
897 CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
899 switch (NextTCPState) {
902 Timeout = SMTP_C_SendTimeouts[pMsg->State];
903 if (pMsg->State == eDATABody) {
904 /* if we're sending a huge message, we need more time. */
905 Timeout += StrLength(pMsg->msgtext) / 1024;
909 Timeout = SMTP_C_ReadTimeouts[pMsg->State];
910 if (pMsg->State == eDATATerminateBody) {
912 * some mailservers take a nap before accepting the message
913 * content inspection and such.
915 Timeout += StrLength(pMsg->msgtext) / 1024;
918 case eTerminateConnection:
922 SetNextTimeout(&pMsg->IO, Timeout);
924 eNextState SMTP_C_DispatchReadDone(AsyncIO *IO)
926 CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
927 SmtpOutMsg *pMsg = IO->Data;
930 rc = ReadHandlers[pMsg->State](pMsg);
932 SMTPSetTimeout(rc, pMsg);
935 eNextState SMTP_C_DispatchWriteDone(AsyncIO *IO)
937 CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
938 SmtpOutMsg *pMsg = IO->Data;
941 rc = SendHandlers[pMsg->State](pMsg);
942 SMTPSetTimeout(rc, pMsg);
947 /*****************************************************************************/
948 /* SMTP CLIENT ERROR CATCHERS */
949 /*****************************************************************************/
950 eNextState SMTP_C_Terminate(AsyncIO *IO)
952 CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
953 SmtpOutMsg *pMsg = IO->Data;
954 FinalizeMessageSend(pMsg);
957 eNextState SMTP_C_Timeout(AsyncIO *IO)
959 CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
960 SmtpOutMsg *pMsg = IO->Data;
961 StrBufPlain(IO->ErrMsg, CKEY(ReadErrors[pMsg->State]));
962 FinalizeMessageSend(pMsg);
965 eNextState SMTP_C_ConnFail(AsyncIO *IO)
967 CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
968 SmtpOutMsg *pMsg = IO->Data;
969 FinalizeMessageSend(pMsg);
972 eNextState SMTP_C_Shutdown(AsyncIO *IO)
974 CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
975 SmtpOutMsg *pMsg = IO->Data;
977 pMsg->MyQEntry->Status = 3;
978 StrBufPlain(pMsg->MyQEntry->StatusMessage, HKEY("server shutdown during message submit."));
979 FinalizeMessageSend(pMsg);
985 * @brief lineread Handler; understands when to read more SMTP lines, and when this is a one-lined reply.
987 eReadState SMTP_C_ReadServerStatus(AsyncIO *IO)
989 eReadState Finished = eBufferNotEmpty;
991 while (Finished == eBufferNotEmpty) {
992 Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf);
995 case eMustReadMore: /// read new from socket...
998 case eBufferNotEmpty: /* shouldn't happen... */
999 case eReadSuccess: /// done for now...
1000 if (StrLength(IO->IOBuf) < 4)
1002 if (ChrPtr(IO->IOBuf)[3] == '-')
1003 Finished = eBufferNotEmpty;
1007 case eReadFail: /// WHUT?
1016 CTDL_MODULE_INIT(smtp_eventclient)
1018 return "smtpeventclient";