53 |
|
|
54 |
|
IMAPHandler::IMAPHandler(const string& server, bool tls) |
55 |
|
{ |
56 |
+ |
letter = 'a'; |
57 |
+ |
number = 0; |
58 |
+ |
|
59 |
|
this->tls = tls; |
60 |
|
buffer = new char[BUFSIZ + 1]; |
61 |
|
|
131 |
|
} |
132 |
|
} |
133 |
|
|
134 |
< |
string input = getline(); |
132 |
< |
if (debug) cerr << input << "\n"; |
134 |
> |
getline(); |
135 |
|
} |
136 |
|
|
137 |
|
IMAPHandler::~IMAPHandler() |
158 |
|
|
159 |
|
if (success) |
160 |
|
{ |
161 |
< |
// |
161 |
> |
tls = true; |
162 |
> |
|
163 |
> |
SSL_load_error_strings(); |
164 |
> |
SSL_library_init(); |
165 |
> |
|
166 |
> |
char* seed = new char[BUFSIZ + 1]; |
167 |
> |
time_t moment = time(NULL); |
168 |
> |
sprintf(seed, "froofy%sfoofoo", ctime(&moment)); |
169 |
> |
|
170 |
> |
RAND_add(seed, strlen(seed), 0.007456); |
171 |
> |
|
172 |
> |
delete [] seed; |
173 |
> |
|
174 |
> |
ctx = SSL_CTX_new(TLSv1_client_method()); |
175 |
> |
if (ctx == NULL) |
176 |
> |
{ |
177 |
> |
cerr << program << ": SSL CTX New: " << |
178 |
> |
ERR_reason_error_string(ERR_get_error()) << "\n"; |
179 |
> |
exit(1); |
180 |
> |
} |
181 |
> |
|
182 |
> |
ssl = SSL_new(ctx); |
183 |
> |
|
184 |
> |
if (SSL_set_fd(ssl, sock) == 0) |
185 |
> |
{ |
186 |
> |
cerr << program << ": SSL Set FD: " << |
187 |
> |
ERR_reason_error_string(ERR_get_error()) << "\n"; |
188 |
> |
exit(1); |
189 |
> |
} |
190 |
> |
|
191 |
> |
if (int code = SSL_connect(ssl) <= 0) |
192 |
> |
{ |
193 |
> |
error(program + ": SSL Connect", code); |
194 |
> |
exit(1); |
195 |
> |
} |
196 |
|
} |
197 |
|
|
198 |
|
return answer; |
199 |
|
} |
200 |
|
|
201 |
+ |
string IMAPHandler::command() |
202 |
+ |
{ |
203 |
+ |
char buffer[4]; |
204 |
+ |
sprintf(buffer, "%03u", number++); |
205 |
+ |
|
206 |
+ |
string sequence = letter + string(buffer); |
207 |
+ |
|
208 |
+ |
if (number > 999) |
209 |
+ |
{ |
210 |
+ |
letter++; |
211 |
+ |
number = 0; |
212 |
+ |
} |
213 |
+ |
|
214 |
+ |
if (letter > 'z') |
215 |
+ |
{ |
216 |
+ |
letter = 'a'; |
217 |
+ |
} |
218 |
+ |
|
219 |
+ |
return sequence; |
220 |
+ |
} |
221 |
+ |
|
222 |
|
string IMAPHandler::imap(const string& imap) |
223 |
|
{ |
224 |
|
string result; |
225 |
|
|
226 |
< |
// |
226 |
> |
string sequence = command(); |
227 |
> |
putline(sequence + " " + imap); |
228 |
> |
|
229 |
> |
while (true) |
230 |
> |
{ |
231 |
> |
string input = getline(); |
232 |
> |
|
233 |
> |
if (input.find(sequence + " OK") == 0) |
234 |
> |
{ |
235 |
> |
success = true; |
236 |
> |
break; |
237 |
> |
} |
238 |
> |
else if (input.find(sequence + " NO") == 0 || input.find(sequence + |
239 |
> |
" BAD") == 0) |
240 |
> |
{ |
241 |
> |
success = false; |
242 |
> |
break; |
243 |
> |
} |
244 |
> |
else |
245 |
> |
{ |
246 |
> |
result += input + "\r\n"; |
247 |
> |
} |
248 |
> |
} |
249 |
> |
|
250 |
> |
return result; |
251 |
> |
} |
252 |
> |
|
253 |
> |
string IMAPHandler::imap(const string& imap, const string& args) |
254 |
> |
{ |
255 |
> |
string result; |
256 |
> |
|
257 |
> |
string sequence = command(); |
258 |
> |
putline(sequence + " " + imap + " " + args); |
259 |
> |
|
260 |
> |
while (true) |
261 |
> |
{ |
262 |
> |
string input = getline(); |
263 |
> |
|
264 |
> |
if (input.find(sequence + " OK") == 0) |
265 |
> |
{ |
266 |
> |
success = true; |
267 |
> |
break; |
268 |
> |
} |
269 |
> |
else if (input.find(sequence + " NO") == 0 || input.find(sequence + |
270 |
> |
" BAD") == 0) |
271 |
> |
{ |
272 |
> |
success = false; |
273 |
> |
break; |
274 |
> |
} |
275 |
> |
else |
276 |
> |
{ |
277 |
> |
result += input + "\r\n"; |
278 |
> |
} |
279 |
> |
} |
280 |
> |
|
281 |
> |
return result; |
282 |
> |
} |
283 |
> |
|
284 |
> |
string IMAPHandler::imap(const string& imap, const string& args, const string& |
285 |
> |
message) |
286 |
> |
{ |
287 |
> |
string result; |
288 |
> |
|
289 |
> |
string sequence = command(); |
290 |
> |
putline(sequence + " " + imap + " " + args); |
291 |
> |
putline(message); |
292 |
> |
|
293 |
> |
while (true) |
294 |
> |
{ |
295 |
> |
string input = getline(); |
296 |
> |
|
297 |
> |
if (input.find(sequence + " OK") == 0) |
298 |
> |
{ |
299 |
> |
success = true; |
300 |
> |
break; |
301 |
> |
} |
302 |
> |
else if (input.find(sequence + " NO") == 0 || input.find(sequence + |
303 |
> |
" BAD") == 0) |
304 |
> |
{ |
305 |
> |
success = false; |
306 |
> |
break; |
307 |
> |
} |
308 |
> |
else |
309 |
> |
{ |
310 |
> |
result += input + "\r\n"; |
311 |
> |
} |
312 |
> |
} |
313 |
|
|
314 |
|
return result; |
315 |
|
} |
316 |
|
|
317 |
|
void IMAPHandler::putline(const string line) |
318 |
|
{ |
319 |
+ |
if (debug) cerr << line << "\n"; |
320 |
+ |
|
321 |
|
sprintf(buffer, "%s\r\n", line.c_str()); |
322 |
|
|
323 |
|
if (tls) |
369 |
|
} |
370 |
|
while (byte != '\n'); |
371 |
|
|
372 |
+ |
if (debug) cerr << line << "\n"; |
373 |
+ |
|
374 |
|
return line; |
375 |
|
} |
376 |
|
|