aboutsummaryrefslogtreecommitdiff
path: root/vmime-master/src/vmime/net/smtp/SMTPResponse.cpp
blob: 223470538b51ba900c7d77c6ceebc3b7ebe6e6a0 (plain)
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
//
// VMime library (http://www.vmime.org)
// Copyright (C) 2002 Vincent Richard <vincent@vmime.org>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 3 of
// the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// Linking this library statically or dynamically with other modules is making
// a combined work based on this library.  Thus, the terms and conditions of
// the GNU General Public License cover the whole combination.
//

#include "vmime/config.hpp"


#if VMIME_HAVE_MESSAGING_FEATURES && VMIME_HAVE_MESSAGING_PROTO_SMTP


#include "vmime/net/smtp/SMTPResponse.hpp"

#include "vmime/platform.hpp"
#include "vmime/utility/stringUtils.hpp"

#include "vmime/net/socket.hpp"
#include "vmime/net/timeoutHandler.hpp"
#include "vmime/net/tracer.hpp"

#include <cctype>


namespace vmime {
namespace net {
namespace smtp {


SMTPResponse::SMTPResponse(
	const shared_ptr <tracer>& tr,
	const shared_ptr <socket>& sok,
	const shared_ptr <timeoutHandler>& toh,
	const state& st
)
	: m_socket(sok),
	  m_timeoutHandler(toh),
	  m_tracer(tr),
	  m_responseBuffer(st.responseBuffer),
	  m_responseContinues(false) {

}


SMTPResponse::SMTPResponse(const SMTPResponse&)
	: vmime::object() {

	// Not used
}


int SMTPResponse::getCode() const {

	const int firstCode = m_lines[0].getCode();

	for (unsigned int i = 1 ; i < m_lines.size() ; ++i) {

		// All response codes returned must be equal
		// or else this in an error...
		if (m_lines[i].getCode() != firstCode) {
			return 0;
		}
	}

	return firstCode;
}


const SMTPResponse::enhancedStatusCode SMTPResponse::getEnhancedCode() const {

	return m_lines[m_lines.size() - 1].getEnhancedCode();
}


const string SMTPResponse::getText() const {

	string text = m_lines[0].getText();

	for (unsigned int i = 1 ; i < m_lines.size() ; ++i) {

		text += '\n';
		text += m_lines[i].getText();
	}

	return text;
}


// static
shared_ptr <SMTPResponse> SMTPResponse::readResponse(
	const shared_ptr <tracer>& tr,
	const shared_ptr <socket>& sok,
	const shared_ptr <timeoutHandler>& toh,
	const state& st
) {

	shared_ptr <SMTPResponse> resp =
		shared_ptr <SMTPResponse>(new SMTPResponse(tr, sok, toh, st));

	resp->readResponse();

	return resp;
}


void SMTPResponse::readResponse() {

	responseLine line = getNextResponse();
	m_lines.push_back(line);

	while (m_responseContinues) {
		line = getNextResponse();
		m_lines.push_back(line);
	}
}


const string SMTPResponse::readResponseLine() {

	string currentBuffer = m_responseBuffer;

	if (m_timeoutHandler) {
		m_timeoutHandler->resetTimeOut();
	}

	while (true) {

		// Get a line from the response buffer
		const size_t lineEnd = currentBuffer.find_first_of('\n');

		if (lineEnd != string::npos) {

			size_t actualLineEnd = lineEnd;

			if (actualLineEnd != 0 && currentBuffer[actualLineEnd - 1] == '\r') {  // CRLF case
				actualLineEnd--;
			}

			const string line(currentBuffer.begin(), currentBuffer.begin() + actualLineEnd);

			currentBuffer.erase(currentBuffer.begin(), currentBuffer.begin() + lineEnd + 1);
			m_responseBuffer = currentBuffer;

			if (m_tracer) {
				m_tracer->traceReceive(line);
			}

			return line;
		}

		// Check whether the time-out delay is elapsed
		if (m_timeoutHandler && m_timeoutHandler->isTimeOut()) {

			if (!m_timeoutHandler->handleTimeOut()) {
				throw exceptions::operation_timed_out();
			}

			m_timeoutHandler->resetTimeOut();
		}

		// Receive data from the socket
		string receiveBuffer;
		m_socket->receive(receiveBuffer);

		if (receiveBuffer.empty()) {   // buffer is empty
			m_socket->waitForRead();
			continue;
		}

		currentBuffer += receiveBuffer;
	}
}


const SMTPResponse::responseLine SMTPResponse::getNextResponse() {

	string line = readResponseLine();

	const int code = extractResponseCode(line);
	string text;

	m_responseContinues = (line.length() >= 4 && line[3] == '-');

	if (line.length() > 4) {
		text = utility::stringUtils::trim(line.substr(4));
	} else {
		text = "";
	}

	return responseLine(code, text, extractEnhancedCode(text));
}


// static
int SMTPResponse::extractResponseCode(const string& response) {

	int code = 0;

	if (response.length() >= 3) {

		code = (response[0] - '0') * 100
		     + (response[1] - '0') * 10
		     + (response[2] - '0');
	}

	return code;
}


// static
const SMTPResponse::enhancedStatusCode SMTPResponse::extractEnhancedCode(const string& responseText) {

	enhancedStatusCode enhCode;

	std::istringstream iss(responseText);
	iss.imbue(std::locale::classic());

	if (std::isdigit(iss.peek())) {

		iss >> enhCode.klass;

		if (iss.get() == '.' && std::isdigit(iss.peek())) {

			iss >> enhCode.subject;

			if (iss.get() == '.' && std::isdigit(iss.peek())) {

				iss >> enhCode.detail;
				return enhCode;
			}
		}
	}

	return enhancedStatusCode();   // no enhanced code found
}


const SMTPResponse::responseLine SMTPResponse::getLineAt(const size_t pos) const {

	return m_lines[pos];
}


size_t SMTPResponse::getLineCount() const {

	return m_lines.size();
}


const SMTPResponse::responseLine SMTPResponse::getLastLine() const {

	return m_lines[m_lines.size() - 1];
}


const SMTPResponse::state SMTPResponse::getCurrentState() const {

	state st;
	st.responseBuffer = m_responseBuffer;

	return st;
}



// SMTPResponse::responseLine

SMTPResponse::responseLine::responseLine(
	const int code,
	const string& text,
	const enhancedStatusCode& enhCode
)
	: m_code(code),
	  m_text(text),
	  m_enhCode(enhCode) {

}


void SMTPResponse::responseLine::setCode(const int code) {

	m_code = code;
}


int SMTPResponse::responseLine::getCode() const {

	return m_code;
}


void SMTPResponse::responseLine::setEnhancedCode(const enhancedStatusCode& enhCode) {

	m_enhCode = enhCode;
}


const SMTPResponse::enhancedStatusCode SMTPResponse::responseLine::getEnhancedCode() const {

	return m_enhCode;
}


void SMTPResponse::responseLine::setText(const string& text) {

	m_text = text;
}


const string SMTPResponse::responseLine::getText() const {

	return m_text;
}



// SMTPResponse::enhancedStatusCode


SMTPResponse::enhancedStatusCode::enhancedStatusCode()
	: klass(0),
	  subject(0),
	  detail(0) {

}


SMTPResponse::enhancedStatusCode::enhancedStatusCode(const enhancedStatusCode& enhCode)
	: klass(enhCode.klass),
	  subject(enhCode.subject),
	  detail(enhCode.detail) {

}


std::ostream& operator<<(std::ostream& os, const SMTPResponse::enhancedStatusCode& code) {

	os << code.klass << '.' << code.subject << '.' << code.detail;
	return os;
}


} // smtp
} // net
} // vmime


#endif // VMIME_HAVE_MESSAGING_FEATURES && VMIME_HAVE_MESSAGING_PROTO_SMTP