e080167bd2405f87226080aa259bb66925564eb2
[citadel.git] / citadel / modules / smtp / smtp_clienthandlers.h
1 /*
2  *
3  * Copyright (c) 1998-2012 by the citadel.org team
4  *
5  *  This program is open source software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 3 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19
20 typedef enum _eSMTP_C_States {
21         eConnectMX,
22         eEHLO,
23         eHELO,
24         eSMTPAuth,
25         eSMTPAuthPlain1,
26         eSMTPAuthPlain2,
27         eFROM,
28         eRCPT,
29         eDATA,
30         eDATABody,
31         eDATATerminateBody,
32         eQUIT,
33         eMaxSMTPC
34 } eSMTP_C_States;
35
36
37 typedef struct _stmp_out_msg {
38         MailQEntry *MyQEntry;
39         OneQueItem *MyQItem;
40         long n;
41         AsyncIO IO;
42         long CXFlags;
43         int IDestructQueItem;
44         int nRemain;
45
46         eSMTP_C_States State;
47
48         struct ares_mx_reply *AllMX;
49         struct ares_mx_reply *CurrMX;
50         const char *mx_port;
51         const char *mx_host;
52         const char *LookupHostname;
53         int iMX, nMX;
54         int LookupWhich;
55
56         DNSQueryParts MxLookup;
57         DNSQueryParts HostLookup;
58         struct hostent *OneMX;
59         char **pIP;
60
61         ParsedURL *Relay;
62         ParsedURL *pCurrRelay;
63         StrBuf *msgtext;
64         StrBuf *QMsgData;
65         StrBuf *MultiLineBuf;
66         const char *envelope_from;
67
68         char user[1024];
69         char node[1024];
70         char name[1024];
71         char mailfrom[1024];
72         long SendLogin;
73         long Flags;
74         long IsRelay;
75 } SmtpOutMsg;
76
77
78 typedef eNextState (*SMTPReadHandler)(SmtpOutMsg *Msg);
79 typedef eNextState (*SMTPSendHandler)(SmtpOutMsg *Msg);
80
81 SMTPReadHandler ReadHandlers[eMaxSMTPC];
82 SMTPSendHandler SendHandlers[eMaxSMTPC];
83 const ConstStr ReadErrors[eMaxSMTPC+1];
84 const double SMTP_C_ReadTimeouts[eMaxSMTPC];
85 const double SMTP_C_SendTimeouts[eMaxSMTPC];
86 const double SMTP_C_ConnTimeout;
87
88 #define F_RELAY          (1<<0) /* we have a Relay    host configuration */
89 #define F_HAVE_FALLBACK  (1<<1) /* we have a fallback host configuration */
90 #define F_FALLBACK       (1<<2)
91 #define F_HAVE_MX        (1<<3) /* we have a list of mx records to go through.*/
92 #define F_DIRECT         (1<<4) /* no mx record found, trying direct connect. */
93
94 extern int SMTPClientDebugEnabled;
95
96 int smtp_resolve_recipients(SmtpOutMsg *SendMsg);
97
98 #define QID ((SmtpOutMsg*)IO->Data)->MyQItem->MessageID
99 #define N ((SmtpOutMsg*)IO->Data)->n
100 #define DBGLOG(LEVEL) if ((LEVEL != LOG_DEBUG) || (SMTPClientDebugEnabled != 0))
101
102 #define EVS_syslog(LEVEL, FORMAT, ...) \
103         DBGLOG(LEVEL) syslog(LEVEL,               \
104                "SMTPC:IO[%ld]CC[%d]S[%ld][%ld] " FORMAT, \
105                IO->ID, CCID, QID, N, __VA_ARGS__)
106
107 #define EVSM_syslog(LEVEL, FORMAT) \
108         DBGLOG(LEVEL) syslog(LEVEL, \
109                "SMTPC:IO[%ld]CC[%d]S[%ld][%ld] " FORMAT, \
110                IO->ID, CCID, QID, N)
111
112 #define EVNCS_syslog(LEVEL, FORMAT, ...) \
113         DBGLOG(LEVEL) syslog(LEVEL, "SMTPC:IO[%ld]S[%ld][%ld] " FORMAT, \
114                IO->ID, QID, N, __VA_ARGS__)
115
116 #define EVNCSM_syslog(LEVEL, FORMAT) \
117         DBGLOG(LEVEL) syslog(LEVEL, "SMTPC:IO[%ld]S[%ld][%ld] " FORMAT, \
118                IO->ID, QID, N)
119
120 #define SMTPC_syslog(LEVEL, FORMAT, ...)          \
121         DBGLOG(LEVEL) syslog(LEVEL,               \
122                              "SMTPCQ: " FORMAT,   \
123                              __VA_ARGS__)
124
125 #define SMTPCM_syslog(LEVEL, FORMAT)            \
126         DBGLOG(LEVEL) syslog(LEVEL,             \
127                              "SMTPCQ: " FORMAT)
128
129
130
131 typedef enum __smtpstate {
132         eSTMPmxlookup,
133         eSTMPevaluatenext,
134         eSTMPalookup,
135         eSTMPaaaalookup,
136         eSTMPconnecting,
137         eSTMPsmtp,
138         eSTMPsmtpdata,
139         eSTMPsmtpdone,
140         eSTMPfinished,
141         eSTMPfailOne,
142         eSMTPFailTemporary,
143         eSMTPFailTotal
144 } smtpstate;
145
146 void SetSMTPState(AsyncIO *IO, smtpstate State);