| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
 | /*
** Copyright 2002-2008, Double Precision Inc.
**
** See COPYING for distribution information.
*/
#ifndef libmail_smtp_H
#define libmail_smtp_H
#include "libmail_config.h"
#if HAVE_FCNTL_H
#include <fcntl.h>
#endif
#include	"mail.H"
#include	<sys/types.h>
#include	"smtpinfo.H"
#include	"logininfo.H"
#include	"fd.H"
#include	<stdio.h>
#include	<time.h>
#include	<list>
#include	<string>
#include	<set>
#include	<queue>
#include	<vector>
LIBMAIL_START
///////////////////////////////////////////////////////////////////////////
//
// An SMTP implementation
//
class smtp : public fd, public loginInfo::callbackTarget {
private:
	void resumed();
	void handler(std::vector<pollfd> &fds, int &timeout);
	std::set<std::string> capabilities;
	bool orderlyShutdown;
	loginInfo smtpLoginInfo;
	int socketRead(const std::string &readbuffer);
	void disconnect(const char *reason);
	std::list<std::string> smtpResponse;
	// SMTP server response being accumulated.
	void (smtp::*responseHandler)(int numCode,
					    std::string multilineResponse);
	// Handler called when an SMTP response is received in entirety
	void installHandler(void (smtp::*)(int numCode,
						 std::string multilineResponse));
	// Install next handler.
	time_t responseTimeout;
	// SMTP timeout
	void error(std::string);	/* Login failed */
	void success(std::string);	/* Login succeeded */
	void howdy(const char *); /* send ehlo/helo */
	void greetingResponse(int, std::string);
	void ehloResponse(int, std::string);
	void heloResponse(int, std::string);
	void auth_external_response(int, std::string);
	void quitResponse(int, std::string);
	int hmac_method_index;
	/* Next HMAC authentication hash function to try */
	void authenticate_hmac();
	void hmacResponse(int, std::string);
	void starttls();
	void starttlsResponse(int, std::string);
	void authenticate_login();
	void begin_auth();
	void begin_auth_nonexternal();
	void loginInfoCallback(std::string str);
	void loginInfoCallbackCancel();
	void loginUseridResponse(int, std::string);
	void loginPasswdResponse(int, std::string);
	void loginResponse(int, std::string);
	void authenticated();
	bool ready2send;	/* Fully logged in */
	struct messageQueueInfo {
		FILE *message;
		mail::smtpInfo messageInfo;
		mail::callback *callback;
		bool flag8bit;
	};
	std::queue<messageQueueInfo> sendQueue;
	/* Queue of messages waiting to go out */
	bool fatalError;	// Fatal connection error occured
	time_t pingTimeout;	// When idling, ping occasionally
	// Custom WriteBuffer that spits out the whole message
	class Blast : public fd::WriteBuffer {
		bool eofSent;
	public:
		smtp *mySmtp;
		char lastChar;
		size_t bytesTotal;
		size_t bytesDone;
		Blast(smtp *smtpArg);
		~Blast();
		bool fillWriteBuffer();
	};
	Blast *myBlaster;
public:
	friend class Blast;
	smtp(std::string url, std::string passwd,
	     std::vector<std::string> &certificates,
	     mail::callback &callback,
	     mail::callback::disconnect &disconnectCallback,
	     loginCallback *loginCallbackFunc);
	smtp(const smtp &); // UNDEFINED
	smtp &operator=(const smtp &); // UNDEFINED
	~smtp();
	void logout(mail::callback &callback);
	void checkNewMail(mail::callback &callback);
	bool hasCapability(std::string capability);
	std::string getCapability(std::string capability);
	mail::folder *folderFromString(std::string);
	void readTopLevelFolders(mail::callback::folderList &callback1,
				 mail::callback &callback2);
	void findFolder(std::string folder,
			class mail::callback::folderList &callback1,
			class mail::callback &callback2);
	std::string translatePath(std::string path);
	mail::folder *getSendFolder(const mail::smtpInfo &info,
				    const mail::folder *folder,
				    std::string &errmsg);
	void readMessageAttributes(const std::vector<size_t> &messages,
				   MessageAttributes attributes,
				   mail::callback::message &callback);
	void readMessageContent(const std::vector<size_t> &messages,
				bool peek,
				enum mail::readMode readType,
				mail::callback::message &callback);
	void readMessageContent(size_t messageNum,
				bool peek,
				const mimestruct &msginfo,
				enum mail::readMode readType,
				mail::callback::message &callback);
	void readMessageContentDecoded(size_t messageNum,
				       bool peek,
				       const mimestruct &msginfo,
				       mail::callback::message &callback);
	size_t getFolderIndexSize();
	mail::messageInfo getFolderIndexInfo(size_t);
	void saveFolderIndexInfo(size_t,
				 const mail::messageInfo &,
				 mail::callback &);
	void updateFolderIndexFlags(const std::vector<size_t> &messages,
				    bool doFlip,
				    bool enableDisable,
				    const mail::messageInfo &flags,
				    mail::callback &callback);
	void updateFolderIndexInfo(mail::callback &);
	void removeMessages(const std::vector<size_t> &messages,
			    callback &cb);
	void copyMessagesTo(const std::vector<size_t> &messages,
			    mail::folder *copyTo,
			    mail::callback &callback);
	void searchMessages(const searchParams &searchInfo,
			    searchCallback &callback);
	void send(FILE *tmpfile, mail::smtpInfo &info,
		  mail::callback *callback,
		  bool flag8bit);
private:
	void startNextMessage();
	void messageProcessed(int, std::string);
	void rsetResponse(int, std::string);
	bool validString(std::string s);
	std::string pipelineError;
	unsigned pipelineCount; // Count pipelined replies received
	unsigned rcptCount; // RCPT TO count.
	std::string pipelineCmd; // Command that got rejected.
	void pipelinedResponse(int, std::string);
	void mailfromResponse(int, std::string);
	void dataResponse(int, std::string);
	void data2Response(int, std::string);
};
LIBMAIL_END
#endif
 |