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
|
/*
* Baltisot
* Copyright (C) 1999-2007 Nicolas "Pixel" Noble
*
* 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 2 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* $Id: MailClient.cc,v 1.2 2007-10-15 13:47:12 pixel Exp $ */
#include <TaskMan.h>
#include <MailClient.h>
#include <BRegex.h>
#include <CopyJob.h>
#include <ReadJob.h>
#include <Variables.h>
const timeval timeout = { 8, 0 };
MailClient::MailClient(const String & _smtp, const String & _subject, const String & _from, strings_t _headers, strings_t _tos, strings_t _ccs, strings_t _bccs, strings_t _fakes, Handle * _body) : SocketClient(_smtp, 25), smtp(_smtp), subject(_subject), from(_from), headers(_headers), tos(_tos), ccs(_ccs), bccs(_bccs), fakes(_fakes), body(_body), cur_rcpt(RCPT_BEGIN) {
SetStatus("Starting");
}
MailClient::~MailClient() {
}
static Regex final_line("^[0-9]{3} ");
int MailClient::readCode() {
do {
b >> msg;
} while (!final_line.Match(msg));
return msg.to_int();
}
String MailClient::getEmail(const String & recipient) {
int pos_1, pos_2;
if ((pos_1 = recipient.strchr('<')) == -1)
return recipient;
if ((pos_2 = recipient.strrchr('>')) == -1)
return recipient;
return recipient.extract(pos_1 + 1, pos_2 - 1);
}
void MailClient::writeEmail(Handle * out) {
String l;
std::vector<String>::iterator i;
(*out) << "Subject: " << subject << "\r\n";
(*out) << "From: " << from << "\r\n";
for (i = headers.begin(); i != headers.end(); i++) {
(*out) << *i << "\r\n";
}
if (!fakes.empty()) {
(*out) << "To: ";
for (i = fakes.begin(); i != fakes.end(); i++) {
if (i != fakes.begin())
(*out) << ";";
(*out) << *i;
}
(*out) << "\r\n";
} else {
if (!tos.empty()) {
(*out) << "To: ";
for (i = tos.begin(); i != tos.end(); i++) {
if (i != tos.begin())
(*out) << ";";
(*out) << *i;
}
(*out) << "\r\n";
}
if (!ccs.empty()) {
(*out) << "To: ";
for (i = ccs.begin(); i != ccs.end(); i++) {
if (i != ccs.begin())
(*out) << ";";
(*out) << *i;
}
(*out) << "\r\n";
}
if (!bccs.empty()) {
(*out) << "To: ";
for (i = bccs.begin(); i != bccs.end(); i++) {
if (i != bccs.begin())
(*out) << ";";
(*out) << *i;
}
(*out) << "\r\n";
}
}
(*out) << "\r\n";
while (!body->IsClosed()) {
(*body) >> l;
if (l[0] == '.')
(*out) << ".";
(*out) << l << "\r\n";
}
(*out) << ".\r\n";
}
String MailClient::getNext() {
if (cur_rcpt == RCPT_BEGIN) {
ptr = tos.begin();
end = tos.end();
cur_rcpt = RCPT_TOS;
}
while (ptr == end) {
switch (cur_rcpt) {
case RCPT_TOS:
cur_rcpt = RCPT_CCS;
ptr = ccs.begin();
end = ccs.end();
break;
case RCPT_CCS:
cur_rcpt = RCPT_BCCS;
ptr = bccs.begin();
end = bccs.end();
break;
case RCPT_BCCS:
cur_rcpt = RCPT_END;
return "**END**";
}
}
return getEmail(*(ptr++));
}
int MailClient::Do() throw (GeneralException) {
int code;
std::vector<String>::iterator i;
String n;
subDo();
switch (current) {
case 0:
WaitFor(c = new ReadJob(&s, &b, final_line));
WaitTimeout();
current = 1;
Suspend(TASK_ON_HOLD);
case 1:
if (readCode() != 220) {
status = "Can not establish connection: " + msg;
return TASK_DONE;
}
b << "EHLO foobar\r\n";
WaitFor(c = new CopyJob(&b, &s));
WaitTimeout();
current = 2;
Suspend(TASK_ON_HOLD);
case 2:
WaitFor(c = new ReadJob(&s, &b, final_line));
WaitTimeout();
current = 3;
Suspend(TASK_ON_HOLD);
case 3:
if (readCode() != 250) {
status = "SMTP server rejected our EHLO: " + msg;
return TASK_DONE;
}
b << "MAIL FROM: " << getEmail(from) << "\r\n";
WaitFor(c = new CopyJob(&b, &s));
WaitTimeout();
current = 4;
Suspend(TASK_ON_HOLD);
case 4:
WaitFor(c = new ReadJob(&s, &b, final_line));
WaitTimeout();
current = 5;
Suspend(TASK_ON_HOLD);
case 5:
if (readCode() != 250) {
if (cur_rcpt == RCPT_BEGIN) {
status = "SMTP server rejected our MAIL FROM: " + msg;
return TASK_DONE;
// } else { // SMTP server rejected one RCPT TO, nevermind it.
}
}
n = getNext();
if (cur_rcpt != RCPT_END) {
b << "RCPT TO: " << n << "\r\n";
WaitFor(c = new CopyJob(&b, &s));
WaitTimeout();
current = 4;
Suspend(TASK_ON_HOLD);
}
b << "DATA\r\n";
WaitFor(c = new CopyJob(&b, &s));
WaitTimeout();
current = 6;
Suspend(TASK_ON_HOLD);
case 6:
WaitFor(c = new ReadJob(&s, &b, final_line));
WaitTimeout();
current = 7;
Suspend(TASK_ON_HOLD);
case 7:
if (readCode() != 354) {
status = "SMTP rejected our DATA command: " + msg;
return TASK_DONE;
}
writeEmail(&b);
WaitFor(c = new CopyJob(&b, &s));
WaitTimeout();
current = 8;
Suspend(TASK_ON_HOLD);
case 8:
WaitFor(c = new ReadJob(&s, &b, final_line));
WaitTimeout();
current = 9;
Suspend(TASK_ON_HOLD);
case 9:
if (readCode() != 250) {
status = "SMTP rejected our mail: " + msg;
return TASK_DONE;
}
b << "QUIT\r\n";
WaitFor(c = new CopyJob(&b, &s));
WaitTimeout();
current = 10;
Suspend(TASK_ON_HOLD);
case 10:
WaitFor(c = new ReadJob(&s, &b, final_line));
WaitTimeout();
current = 11;
Suspend(TASK_ON_HOLD);
case 11:
if (readCode() != 221)
status = "SMTP wasn't pleased by our QUIT command: " + msg;
status = "Mail sent.";
return TASK_DONE;
}
}
|