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 DNSQueryParts MxLookup;
164 DNSQueryParts HostLookup;
165 struct hostent *OneMX;
166 /// struct hostent *HEnt;
169 ParsedURL *pCurrRelay;
178 void DeleteSmtpOutMsg(void *v)
182 ares_free_data(Msg->AllMX);
183 if (Msg->HostLookup.VParsedDNSReply != NULL)
184 Msg->HostLookup.DNSReplyFree(Msg->HostLookup.VParsedDNSReply);
185 FreeStrBuf(&Msg->msgtext);
186 FreeAsyncIOContents(&Msg->IO);
190 eNextState SMTP_C_Shutdown(AsyncIO *IO);
191 eNextState SMTP_C_Timeout(AsyncIO *IO);
192 eNextState SMTP_C_ConnFail(AsyncIO *IO);
193 eNextState SMTP_C_DispatchReadDone(AsyncIO *IO);
194 eNextState SMTP_C_DispatchWriteDone(AsyncIO *IO);
195 eNextState SMTP_C_Terminate(AsyncIO *IO);
196 eReadState SMTP_C_ReadServerStatus(AsyncIO *IO);
198 typedef eNextState (*SMTPReadHandler)(SmtpOutMsg *Msg);
199 typedef eNextState (*SMTPSendHandler)(SmtpOutMsg *Msg);
202 #define SMTP_ERROR(WHICH_ERR, ERRSTR) do {\
203 SendMsg->MyQEntry->Status = WHICH_ERR; \
204 StrBufAppendBufPlain(SendMsg->MyQEntry->StatusMessage, HKEY(ERRSTR), 0); \
208 #define SMTP_VERROR(WHICH_ERR) do {\
209 SendMsg->MyQEntry->Status = WHICH_ERR; \
210 StrBufPlain(SendMsg->MyQEntry->StatusMessage, \
211 ChrPtr(SendMsg->IO.IOBuf) + 4, \
212 StrLength(SendMsg->IO.IOBuf) - 4); \
216 #define SMTP_IS_STATE(WHICH_STATE) (ChrPtr(SendMsg->IO.IOBuf)[0] == WHICH_STATE)
218 #define SMTP_DBG_SEND() CtdlLogPrintf(CTDL_DEBUG, "SMTP client[%ld]: > %s\n", SendMsg->n, ChrPtr(SendMsg->IO.SendBuf.Buf))
219 #define SMTP_DBG_READ() CtdlLogPrintf(CTDL_DEBUG, "SMTP client[%ld]: < %s\n", SendMsg->n, ChrPtr(SendMsg->IO.IOBuf))
222 void FinalizeMessageSend(SmtpOutMsg *Msg)
224 CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
226 if (DecreaseQReference(Msg->MyQItem))
231 nRemain = CountActiveQueueEntries(Msg->MyQItem);
233 MsgData = SerializeQueueItem(Msg->MyQItem);
235 * Uncompleted delivery instructions remain, so delete the old
236 * instructions and replace with the updated ones.
238 CtdlDeleteMessages(SMTP_SPOOLOUT_ROOM, &Msg->MyQItem->QueMsgID, 1, "");
239 smtpq_do_bounce(Msg->MyQItem,
242 struct CtdlMessage *msg;
243 msg = malloc(sizeof(struct CtdlMessage));
244 memset(msg, 0, sizeof(struct CtdlMessage));
245 msg->cm_magic = CTDLMESSAGE_MAGIC;
246 msg->cm_anon_type = MES_NORMAL;
247 msg->cm_format_type = FMT_RFC822;
248 msg->cm_fields['M'] = SmashStrBuf(&MsgData);
249 CtdlSubmitMsg(msg, NULL, SMTP_SPOOLOUT_ROOM, QP_EADDR);
250 CtdlFreeMessage(msg);
253 CtdlDeleteMessages(SMTP_SPOOLOUT_ROOM, &Msg->MyQItem->MessageID, 1, "");
254 FreeStrBuf(&MsgData);
257 RemoveQItem(Msg->MyQItem);
259 DeleteSmtpOutMsg(Msg);
263 void SetConnectStatus(AsyncIO *IO)
266 SmtpOutMsg *SendMsg = IO->Data;
273 src = &IO->Addr.sin6_addr;
276 struct sockaddr_in *addr = (struct sockaddr_in *)&IO->Addr;
278 src = &addr->sin_addr.s_addr;
281 inet_ntop((IO->IP6)?AF_INET6:AF_INET,
284 if (SendMsg->mx_host == NULL)
285 SendMsg->mx_host = "<no name>";
287 CtdlLogPrintf(CTDL_DEBUG,
288 "SMTP client[%ld]: connecting to %s [%s]:%d ...\n",
294 SendMsg->MyQEntry->Status = 5;
295 StrBufPrintf(SendMsg->MyQEntry->StatusMessage,
296 "Timeout while connecting %s [%s]:%d ",
302 eNextState mx_connect_relay_ip(AsyncIO *IO)
305 SmtpOutMsg *SendMsg = IO->Data;
307 CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
309 IO->IP6 = SendMsg->pCurrRelay->af == AF_INET6;
311 if (SendMsg->pCurrRelay->Port != 0)
312 IO->dport = SendMsg->pCurrRelay->Port;
314 memset(&IO->Addr, 0, sizeof(struct sockaddr_in6));
316 memcpy(&IO->Addr.sin6_addr.s6_addr,
317 &SendMsg->pCurrRelay->Addr,
318 sizeof(struct in6_addr));
320 IO->Addr.sin6_family = AF_INET6;
321 IO->Addr.sin6_port = htons(IO->dport);
324 struct sockaddr_in *addr = (struct sockaddr_in*) &IO->Addr;
325 /* Bypass the ns lookup result like this: IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1"); */
326 memcpy(&addr->sin_addr,///.s_addr,
327 &SendMsg->pCurrRelay->Addr,
328 sizeof(struct in_addr));
330 addr->sin_family = AF_INET;
331 addr->sin_port = htons(IO->dport);
334 SetConnectStatus(IO);
336 return InitEventIO(IO, SendMsg,
338 SMTP_C_ReadTimeouts[0],
342 eNextState get_one_mx_host_ip_done(AsyncIO *IO)
344 SmtpOutMsg *SendMsg = IO->Data;
345 struct hostent *hostent;
349 hostent = SendMsg->HostLookup.VParsedDNSReply;
350 if ((SendMsg->HostLookup.DNSStatus == ARES_SUCCESS) &&
351 (hostent != NULL) ) {
353 IO->IP6 = hostent->h_addrtype == AF_INET6;
354 ////IO->HEnt = hostent;
356 memset(&IO->Addr, 0, sizeof(struct in6_addr));
358 memcpy(&IO->Addr.sin6_addr.s6_addr,
359 &hostent->h_addr_list[0],
360 sizeof(struct in6_addr));
362 IO->Addr.sin6_family = hostent->h_addrtype;
363 IO->Addr.sin6_port = htons(IO->dport);
366 struct sockaddr_in *addr = (struct sockaddr_in*) &IO->Addr;
367 /* Bypass the ns lookup result like this: IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1"); */
368 // addr->sin_addr.s_addr = htonl((uint32_t)&hostent->h_addr_list[0]);
369 memcpy(&addr->sin_addr.s_addr, hostent->h_addr_list[0], sizeof(uint32_t));
371 addr->sin_family = hostent->h_addrtype;
372 addr->sin_port = htons(IO->dport);
375 ////SendMsg->IO.HEnt = hostent;
376 SetConnectStatus(IO);
377 return InitEventIO(IO, SendMsg,
379 SMTP_C_ReadTimeouts[0],
383 return SMTP_C_Terminate(IO);
386 const unsigned short DefaultMXPort = 25;
387 eNextState get_one_mx_host_ip(AsyncIO *IO)
389 SmtpOutMsg * SendMsg = IO->Data;
390 const char *Hostname;
395 if (SendMsg->CurrMX) {
396 SendMsg->mx_host = SendMsg->CurrMX->host;
397 SendMsg->CurrMX = SendMsg->CurrMX->next;
400 if (SendMsg->pCurrRelay != NULL) {
401 SendMsg->mx_host = Hostname = SendMsg->pCurrRelay->Host;
402 if (SendMsg->pCurrRelay->Port != 0)
403 SendMsg->IO.dport = SendMsg->pCurrRelay->Port;
405 else if (SendMsg->mx_host != NULL) Hostname = SendMsg->mx_host;
406 else Hostname = SendMsg->node;
408 CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
410 CtdlLogPrintf(CTDL_DEBUG,
411 "SMTP client[%ld]: looking up %s : %d ...\n",
416 if (!QueueQuery(ns_t_a,
419 &SendMsg->HostLookup,
420 get_one_mx_host_ip_done))
422 SendMsg->MyQEntry->Status = 5;
423 StrBufPrintf(SendMsg->MyQEntry->StatusMessage,
424 "No MX hosts found for <%s>", SendMsg->node);
425 return IO->NextState;
427 return IO->NextState;
431 eNextState smtp_resolve_mx_done(AsyncIO *IO)
433 SmtpOutMsg * SendMsg = IO->Data;
437 CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
439 SendMsg->IO.ErrMsg = SendMsg->MyQEntry->StatusMessage;
441 SendMsg->CurrMX = SendMsg->AllMX = IO->DNSQuery->VParsedDNSReply;
442 //// TODO: should we remove the current ares context???
443 get_one_mx_host_ip(IO);
444 return IO->NextState;
448 eNextState resolve_mx_records(AsyncIO *IO)
450 SmtpOutMsg * SendMsg = IO->Data;
452 CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
454 if (!QueueQuery(ns_t_mx,
458 smtp_resolve_mx_done))
460 SendMsg->MyQEntry->Status = 5;
461 StrBufPrintf(SendMsg->MyQEntry->StatusMessage,
462 "No MX hosts found for <%s>", SendMsg->node);
463 return IO->NextState;
469 int smtp_resolve_recipients(SmtpOutMsg *SendMsg)
477 CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
479 if ((SendMsg==NULL) ||
480 (SendMsg->MyQEntry == NULL) ||
481 (StrLength(SendMsg->MyQEntry->Recipient) == 0)) {
485 /* Parse out the host portion of the recipient address */
486 process_rfc822_addr(ChrPtr(SendMsg->MyQEntry->Recipient),
491 CtdlLogPrintf(CTDL_DEBUG, "SMTP client[%ld]: Attempting delivery to <%s> @ <%s> (%s)\n",
492 SendMsg->n, SendMsg->user, SendMsg->node, SendMsg->name);
493 /* If no envelope_from is supplied, extract one from the message */
494 if ( (SendMsg->envelope_from == NULL) ||
495 (IsEmptyStr(SendMsg->envelope_from)) ) {
496 SendMsg->mailfrom[0] = '\0';
498 ptr = ChrPtr(SendMsg->msgtext);
500 if (ptr = cmemreadline(ptr, buf, sizeof buf), *ptr == 0) {
503 if (!strncasecmp(buf, "From:", 5)) {
504 safestrncpy(SendMsg->mailfrom, &buf[5], sizeof SendMsg->mailfrom);
505 striplt(SendMsg->mailfrom);
506 for (i=0; SendMsg->mailfrom[i]; ++i) {
507 if (!isprint(SendMsg->mailfrom[i])) {
508 strcpy(&SendMsg->mailfrom[i], &SendMsg->mailfrom[i+1]);
513 /* Strip out parenthesized 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 strcpy(&SendMsg->mailfrom[lp-1], &SendMsg->mailfrom[rp+1]);
524 /* Prefer brokketized names */
527 for (i=0; !IsEmptyStr(SendMsg->mailfrom + i); ++i) {
528 if (SendMsg->mailfrom[i] == '<') lp = i;
529 if (SendMsg->mailfrom[i] == '>') rp = i;
531 if ( (lp>=0) && (rp>lp) ) {
532 SendMsg->mailfrom[rp] = 0;
533 memmove(SendMsg->mailfrom,
534 &SendMsg->mailfrom[lp + 1],
540 } while (scan_done == 0);
541 if (IsEmptyStr(SendMsg->mailfrom)) strcpy(SendMsg->mailfrom, "someone@somewhere.org");
542 stripallbut(SendMsg->mailfrom, '<', '>');
543 SendMsg->envelope_from = SendMsg->mailfrom;
551 void smtp_try(OneQueItem *MyQItem,
552 MailQEntry *MyQEntry,
554 int KeepMsgText, /* KeepMsgText allows us to use MsgText as ours. */
557 SmtpOutMsg * SendMsg;
559 CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
561 SendMsg = (SmtpOutMsg *) malloc(sizeof(SmtpOutMsg));
562 memset(SendMsg, 0, sizeof(SmtpOutMsg));
563 SendMsg->IO.sock = (-1);
564 SendMsg->IO.NextState = eReadMessage;
565 SendMsg->n = MsgCount++;
566 SendMsg->MyQEntry = MyQEntry;
567 SendMsg->MyQItem = MyQItem;
568 SendMsg->pCurrRelay = MyQItem->URL;
570 SendMsg->IO.dport = DefaultMXPort;
571 SendMsg->IO.Data = SendMsg;
572 SendMsg->IO.SendDone = SMTP_C_DispatchWriteDone;
573 SendMsg->IO.ReadDone = SMTP_C_DispatchReadDone;
574 SendMsg->IO.Terminate = SMTP_C_Terminate;
575 SendMsg->IO.LineReader = SMTP_C_ReadServerStatus;
576 SendMsg->IO.ConnFail = SMTP_C_ConnFail;
577 SendMsg->IO.Timeout = SMTP_C_Timeout;
578 SendMsg->IO.ShutdownAbort = SMTP_C_Shutdown;
579 SendMsg->IO.SendBuf.Buf = NewStrBufPlain(NULL, 1024);
580 SendMsg->IO.RecvBuf.Buf = NewStrBufPlain(NULL, 1024);
581 SendMsg->IO.IOBuf = NewStrBuf();
585 SendMsg->msgtext = MsgText;
588 SendMsg->msgtext = NewStrBufDup(MsgText);
591 if (smtp_resolve_recipients(SendMsg)) {
592 if (SendMsg->pCurrRelay == NULL)
593 QueueEventContext(&SendMsg->IO,
596 if (SendMsg->pCurrRelay->IsIP) {
597 QueueEventContext(&SendMsg->IO,
598 mx_connect_relay_ip);
601 QueueEventContext(&SendMsg->IO,
607 if ((SendMsg==NULL) ||
608 (SendMsg->MyQEntry == NULL)) {
609 SendMsg->MyQEntry->Status = 5;
610 StrBufPlain(SendMsg->MyQEntry->StatusMessage,
611 HKEY("Invalid Recipient!"));
613 FinalizeMessageSend(SendMsg);
621 /*****************************************************************************/
622 /* SMTP CLIENT STATE CALLBACKS */
623 /*****************************************************************************/
624 eNextState SMTPC_read_greeting(SmtpOutMsg *SendMsg)
626 /* Process the SMTP greeting from the server */
629 if (!SMTP_IS_STATE('2')) {
630 if (SMTP_IS_STATE('4'))
638 eNextState SMTPC_send_EHLO(SmtpOutMsg *SendMsg)
640 /* At this point we know we are talking to a real SMTP server */
642 /* Do a EHLO command. If it fails, try the HELO command. */
643 StrBufPrintf(SendMsg->IO.SendBuf.Buf,
644 "EHLO %s\r\n", config.c_fqdn);
650 eNextState SMTPC_read_EHLO_reply(SmtpOutMsg *SendMsg)
654 if (SMTP_IS_STATE('2')) {
657 if ((SendMsg->pCurrRelay == NULL) ||
658 (SendMsg->pCurrRelay->User == NULL))
659 SendMsg->State ++; /* Skip auth... */
661 /* else we fall back to 'helo' */
665 eNextState STMPC_send_HELO(SmtpOutMsg *SendMsg)
667 StrBufPrintf(SendMsg->IO.SendBuf.Buf,
668 "HELO %s\r\n", config.c_fqdn);
674 eNextState SMTPC_read_HELO_reply(SmtpOutMsg *SendMsg)
678 if (!SMTP_IS_STATE('2')) {
679 if (SMTP_IS_STATE('4'))
684 if ((SendMsg->pCurrRelay == NULL) ||
685 (SendMsg->pCurrRelay->User == NULL))
686 SendMsg->State ++; /* Skip auth... */
690 eNextState SMTPC_send_auth(SmtpOutMsg *SendMsg)
695 if ((SendMsg->pCurrRelay == NULL) ||
696 (SendMsg->pCurrRelay->User == NULL))
697 SendMsg->State ++; /* Skip auth, shouldn't even come here!... */
699 /* Do an AUTH command if necessary */
700 sprintf(buf, "%s%c%s%c%s",
701 SendMsg->pCurrRelay->User, '\0',
702 SendMsg->pCurrRelay->User, '\0',
703 SendMsg->pCurrRelay->Pass);
704 CtdlEncodeBase64(encoded, buf,
705 strlen(SendMsg->pCurrRelay->User) * 2 +
706 strlen(SendMsg->pCurrRelay->Pass) + 2, 0);
707 StrBufPrintf(SendMsg->IO.SendBuf.Buf,
708 "AUTH PLAIN %s\r\n", encoded);
714 eNextState SMTPC_read_auth_reply(SmtpOutMsg *SendMsg)
716 /* Do an AUTH command if necessary */
720 if (!SMTP_IS_STATE('2')) {
721 if (SMTP_IS_STATE('4'))
729 eNextState SMTPC_send_FROM(SmtpOutMsg *SendMsg)
731 /* previous command succeeded, now try the MAIL FROM: command */
732 StrBufPrintf(SendMsg->IO.SendBuf.Buf,
733 "MAIL FROM:<%s>\r\n",
734 SendMsg->envelope_from);
740 eNextState SMTPC_read_FROM_reply(SmtpOutMsg *SendMsg)
744 if (!SMTP_IS_STATE('2')) {
745 if (SMTP_IS_STATE('4'))
754 eNextState SMTPC_send_RCPT(SmtpOutMsg *SendMsg)
756 /* MAIL succeeded, now try the RCPT To: command */
757 StrBufPrintf(SendMsg->IO.SendBuf.Buf,
758 "RCPT TO:<%s@%s>\r\n",
766 eNextState SMTPC_read_RCPT_reply(SmtpOutMsg *SendMsg)
770 if (!SMTP_IS_STATE('2')) {
771 if (SMTP_IS_STATE('4'))
779 eNextState SMTPC_send_DATAcmd(SmtpOutMsg *SendMsg)
781 /* RCPT succeeded, now try the DATA command */
782 StrBufPlain(SendMsg->IO.SendBuf.Buf,
789 eNextState SMTPC_read_DATAcmd_reply(SmtpOutMsg *SendMsg)
793 if (!SMTP_IS_STATE('3')) {
794 if (SMTP_IS_STATE('4'))
802 eNextState SMTPC_send_data_body(SmtpOutMsg *SendMsg)
805 /* If we reach this point, the server is expecting data.*/
807 Buf = SendMsg->IO.SendBuf.Buf;
808 SendMsg->IO.SendBuf.Buf = SendMsg->msgtext;
809 SendMsg->msgtext = Buf;
815 eNextState SMTPC_send_terminate_data_body(SmtpOutMsg *SendMsg)
819 Buf = SendMsg->IO.SendBuf.Buf;
820 SendMsg->IO.SendBuf.Buf = SendMsg->msgtext;
821 SendMsg->msgtext = Buf;
823 StrBufPlain(SendMsg->IO.SendBuf.Buf,
830 eNextState SMTPC_read_data_body_reply(SmtpOutMsg *SendMsg)
834 if (!SMTP_IS_STATE('2')) {
835 if (SMTP_IS_STATE('4'))
842 StrBufPlain(SendMsg->MyQEntry->StatusMessage,
843 &ChrPtr(SendMsg->IO.RecvBuf.Buf)[4],
844 StrLength(SendMsg->IO.RecvBuf.Buf) - 4);
845 SendMsg->MyQEntry->Status = 2;
849 eNextState SMTPC_send_QUIT(SmtpOutMsg *SendMsg)
851 StrBufPlain(SendMsg->IO.SendBuf.Buf,
858 eNextState SMTPC_read_QUIT_reply(SmtpOutMsg *SendMsg)
862 CtdlLogPrintf(CTDL_INFO, "SMTP client[%ld]: delivery to <%s> @ <%s> (%s) succeeded\n",
863 SendMsg->n, SendMsg->user, SendMsg->node, SendMsg->name);
864 return eTerminateConnection;
867 eNextState SMTPC_read_dummy(SmtpOutMsg *SendMsg)
872 eNextState SMTPC_send_dummy(SmtpOutMsg *SendMsg)
878 /*****************************************************************************/
879 /* SMTP CLIENT DISPATCHER */
880 /*****************************************************************************/
881 SMTPReadHandler ReadHandlers[eMaxSMTPC] = {
883 SMTPC_read_EHLO_reply,
884 SMTPC_read_HELO_reply,
885 SMTPC_read_auth_reply,
886 SMTPC_read_FROM_reply,
887 SMTPC_read_RCPT_reply,
888 SMTPC_read_DATAcmd_reply,
890 SMTPC_read_data_body_reply,
891 SMTPC_read_QUIT_reply
893 SMTPSendHandler SendHandlers[eMaxSMTPC] = {
894 SMTPC_send_dummy, /* we don't send a greeting, the server does... */
901 SMTPC_send_data_body,
902 SMTPC_send_terminate_data_body,
906 void SMTPSetTimeout(eNextState NextTCPState, SmtpOutMsg *pMsg)
908 CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
910 switch (NextTCPState) {
913 Timeout = SMTP_C_SendTimeouts[pMsg->State];
914 if (pMsg->State == eDATABody) {
915 /* if we're sending a huge message, we need more time. */
916 Timeout += StrLength(pMsg->msgtext) / 1024;
920 Timeout = SMTP_C_ReadTimeouts[pMsg->State];
921 if (pMsg->State == eDATATerminateBody) {
923 * some mailservers take a nap before accepting the message
924 * content inspection and such.
926 Timeout += StrLength(pMsg->msgtext) / 1024;
929 case eTerminateConnection:
933 SetNextTimeout(&pMsg->IO, Timeout);
935 eNextState SMTP_C_DispatchReadDone(AsyncIO *IO)
937 CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
938 SmtpOutMsg *pMsg = IO->Data;
941 rc = ReadHandlers[pMsg->State](pMsg);
943 SMTPSetTimeout(rc, pMsg);
946 eNextState SMTP_C_DispatchWriteDone(AsyncIO *IO)
948 CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
949 SmtpOutMsg *pMsg = IO->Data;
952 rc = SendHandlers[pMsg->State](pMsg);
953 SMTPSetTimeout(rc, pMsg);
958 /*****************************************************************************/
959 /* SMTP CLIENT ERROR CATCHERS */
960 /*****************************************************************************/
961 eNextState SMTP_C_Terminate(AsyncIO *IO)
963 CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
964 SmtpOutMsg *pMsg = IO->Data;
965 FinalizeMessageSend(pMsg);
968 eNextState SMTP_C_Timeout(AsyncIO *IO)
970 CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
971 SmtpOutMsg *pMsg = IO->Data;
972 StrBufPlain(IO->ErrMsg, CKEY(ReadErrors[pMsg->State]));
973 FinalizeMessageSend(pMsg);
976 eNextState SMTP_C_ConnFail(AsyncIO *IO)
978 CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
979 SmtpOutMsg *pMsg = IO->Data;
980 FinalizeMessageSend(pMsg);
983 eNextState SMTP_C_Shutdown(AsyncIO *IO)
985 CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
986 SmtpOutMsg *pMsg = IO->Data;
988 pMsg->MyQEntry->Status = 3;
989 StrBufPlain(pMsg->MyQEntry->StatusMessage, HKEY("server shutdown during message submit."));
990 FinalizeMessageSend(pMsg);
996 * @brief lineread Handler; understands when to read more SMTP lines, and when this is a one-lined reply.
998 eReadState SMTP_C_ReadServerStatus(AsyncIO *IO)
1000 eReadState Finished = eBufferNotEmpty;
1002 while (Finished == eBufferNotEmpty) {
1003 Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf);
1006 case eMustReadMore: /// read new from socket...
1009 case eBufferNotEmpty: /* shouldn't happen... */
1010 case eReadSuccess: /// done for now...
1011 if (StrLength(IO->IOBuf) < 4)
1013 if (ChrPtr(IO->IOBuf)[3] == '-')
1014 Finished = eBufferNotEmpty;
1018 case eReadFail: /// WHUT?
1027 CTDL_MODULE_INIT(smtp_eventclient)
1029 return "smtpeventclient";