summaryrefslogtreecommitdiffabout
Unidiff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--kmicromail/libetpan/generic/pop3driver.c9
-rw-r--r--kmicromail/libetpan/pop3/mailpop3.c5
-rw-r--r--kmicromail/libetpan/tools/mailstream_helper.c11
3 files changed, 19 insertions, 6 deletions
diff --git a/kmicromail/libetpan/generic/pop3driver.c b/kmicromail/libetpan/generic/pop3driver.c
index 375879e..475dfcc 100644
--- a/kmicromail/libetpan/generic/pop3driver.c
+++ b/kmicromail/libetpan/generic/pop3driver.c
@@ -177,196 +177,199 @@ static void pop3driver_uninitialize(mailsession * session)
177 177
178static int pop3driver_connect_stream(mailsession * session, mailstream * s) 178static int pop3driver_connect_stream(mailsession * session, mailstream * s)
179{ 179{
180 int r; 180 int r;
181 181
182 r = mailpop3_connect(get_pop3_session(session), s); 182 r = mailpop3_connect(get_pop3_session(session), s);
183 183
184 switch (r) { 184 switch (r) {
185 case MAILPOP3_NO_ERROR: 185 case MAILPOP3_NO_ERROR:
186 return MAIL_NO_ERROR_NON_AUTHENTICATED; 186 return MAIL_NO_ERROR_NON_AUTHENTICATED;
187 187
188 default: 188 default:
189 return pop3driver_pop3_error_to_mail_error(r); 189 return pop3driver_pop3_error_to_mail_error(r);
190 } 190 }
191} 191}
192 192
193static int pop3driver_starttls(mailsession * session) 193static int pop3driver_starttls(mailsession * session)
194{ 194{
195 int r; 195 int r;
196 int fd; 196 int fd;
197 mailstream_low * low; 197 mailstream_low * low;
198 mailstream_low * new_low; 198 mailstream_low * new_low;
199 mailpop3 * pop3; 199 mailpop3 * pop3;
200 200
201 pop3 = get_pop3_session(session); 201 pop3 = get_pop3_session(session);
202 202
203 r = mailpop3_stls(pop3); 203 r = mailpop3_stls(pop3);
204 204
205 switch (r) { 205 switch (r) {
206 case MAILPOP3_NO_ERROR: 206 case MAILPOP3_NO_ERROR:
207 break; 207 break;
208 default: 208 default:
209 return pop3driver_pop3_error_to_mail_error(r); 209 return pop3driver_pop3_error_to_mail_error(r);
210 } 210 }
211 211
212 low = mailstream_get_low(pop3->pop3_stream); 212 low = mailstream_get_low(pop3->pop3_stream);
213 fd = mailstream_low_get_fd(low); 213 fd = mailstream_low_get_fd(low);
214 if (fd == -1) 214 if (fd == -1)
215 return MAIL_ERROR_STREAM; 215 return MAIL_ERROR_STREAM;
216 216
217 new_low = mailstream_low_ssl_open(fd); 217 new_low = mailstream_low_ssl_open(fd);
218 if (new_low == NULL) 218 if (new_low == NULL)
219 return MAIL_ERROR_STREAM; 219 return MAIL_ERROR_STREAM;
220 mailstream_low_free(low); 220 mailstream_low_free(low);
221 mailstream_set_low(pop3->pop3_stream, new_low); 221 mailstream_set_low(pop3->pop3_stream, new_low);
222 222
223 return MAIL_NO_ERROR; 223 return MAIL_NO_ERROR;
224} 224}
225 225
226static int pop3driver_parameters(mailsession * session, 226static int pop3driver_parameters(mailsession * session,
227 int id, void * value) 227 int id, void * value)
228{ 228{
229 struct pop3_session_state_data * data; 229 struct pop3_session_state_data * data;
230 230
231 data = get_data(session); 231 data = get_data(session);
232 232
233 switch (id) { 233 switch (id) {
234 case POP3DRIVER_SET_AUTH_TYPE: 234 case POP3DRIVER_SET_AUTH_TYPE:
235 { 235 {
236 int * param; 236 int * param;
237 237
238 param = value; 238 param = value;
239 239
240 data->pop3_auth_type = * param; 240 data->pop3_auth_type = * param;
241 return MAIL_NO_ERROR; 241 return MAIL_NO_ERROR;
242 } 242 }
243 } 243 }
244 244
245 return MAIL_ERROR_INVAL; 245 return MAIL_ERROR_INVAL;
246} 246}
247 247
248static int pop3driver_login(mailsession * session, 248static int pop3driver_login(mailsession * session,
249 char * userid, char * password) 249 char * userid, char * password)
250{ 250{
251 int r; 251 int r;
252 carray * msg_tab; 252 carray * msg_tab;
253 struct pop3_session_state_data * data; 253 struct pop3_session_state_data * data;
254 254
255 data = get_data(session); 255 data = get_data(session);
256 256
257 switch (data->pop3_auth_type) { 257 switch (data->pop3_auth_type) {
258 case POP3DRIVER_AUTH_TYPE_TRY_APOP: 258 case POP3DRIVER_AUTH_TYPE_TRY_APOP:
259 r = mailpop3_login_apop(get_pop3_session(session), userid, password); 259 r = mailpop3_login_apop(get_pop3_session(session), userid, password);
260 if (r != MAILPOP3_NO_ERROR) 260 if (r != MAILPOP3_NO_ERROR)
261 r = mailpop3_login(get_pop3_session(session), userid, password); 261 r = mailpop3_login(get_pop3_session(session), userid, password);
262 break; 262 break;
263 263
264 case POP3DRIVER_AUTH_TYPE_APOP: 264 case POP3DRIVER_AUTH_TYPE_APOP:
265 r = mailpop3_login_apop(get_pop3_session(session), userid, password); 265 r = mailpop3_login_apop(get_pop3_session(session), userid, password);
266 break; 266 break;
267 267
268 default: 268 default:
269 case POP3DRIVER_AUTH_TYPE_PLAIN: 269 case POP3DRIVER_AUTH_TYPE_PLAIN:
270 r = mailpop3_login(get_pop3_session(session), userid, password); 270 r = mailpop3_login(get_pop3_session(session), userid, password);
271 break; 271 break;
272 } 272 }
273 // LR 2 lines
274 int ret = pop3driver_pop3_error_to_mail_error(r);
275 if ( ret == MAIL_NO_ERROR )
276 mailpop3_list(get_pop3_session(session), &msg_tab);
273 277
274 mailpop3_list(get_pop3_session(session), &msg_tab); 278 // LR
275 279 return ret;
276 return pop3driver_pop3_error_to_mail_error(r);
277} 280}
278 281
279static int pop3driver_logout(mailsession * session) 282static int pop3driver_logout(mailsession * session)
280{ 283{
281 int r; 284 int r;
282 285
283 r = mailpop3_quit(get_pop3_session(session)); 286 r = mailpop3_quit(get_pop3_session(session));
284 287
285 return pop3driver_pop3_error_to_mail_error(r); 288 return pop3driver_pop3_error_to_mail_error(r);
286} 289}
287 290
288static int pop3driver_noop(mailsession * session) 291static int pop3driver_noop(mailsession * session)
289{ 292{
290 int r; 293 int r;
291 294
292 r = mailpop3_noop(get_pop3_session(session)); 295 r = mailpop3_noop(get_pop3_session(session));
293 296
294 return pop3driver_pop3_error_to_mail_error(r); 297 return pop3driver_pop3_error_to_mail_error(r);
295} 298}
296 299
297static int pop3driver_status_folder(mailsession * session, char * mb, 300static int pop3driver_status_folder(mailsession * session, char * mb,
298 uint32_t * result_messages, 301 uint32_t * result_messages,
299 uint32_t * result_recent, 302 uint32_t * result_recent,
300 uint32_t * result_unseen) 303 uint32_t * result_unseen)
301{ 304{
302 uint32_t count; 305 uint32_t count;
303 int r; 306 int r;
304 307
305 r = pop3driver_messages_number(session, mb, &count); 308 r = pop3driver_messages_number(session, mb, &count);
306 if (r != MAIL_NO_ERROR) 309 if (r != MAIL_NO_ERROR)
307 return r; 310 return r;
308 311
309 * result_messages = count; 312 * result_messages = count;
310 * result_recent = count; 313 * result_recent = count;
311 * result_unseen = count; 314 * result_unseen = count;
312 315
313 return MAIL_NO_ERROR; 316 return MAIL_NO_ERROR;
314} 317}
315 318
316static int pop3driver_messages_number(mailsession * session, char * mb, 319static int pop3driver_messages_number(mailsession * session, char * mb,
317 uint32_t * result) 320 uint32_t * result)
318{ 321{
319 carray * msg_tab; 322 carray * msg_tab;
320 323
321 mailpop3_list(get_pop3_session(session), &msg_tab); 324 mailpop3_list(get_pop3_session(session), &msg_tab);
322 325
323 * result = carray_count(msg_tab) - 326 * result = carray_count(msg_tab) -
324 get_pop3_session(session)->pop3_deleted_count; 327 get_pop3_session(session)->pop3_deleted_count;
325 328
326 return MAIL_NO_ERROR; 329 return MAIL_NO_ERROR;
327} 330}
328 331
329 332
330/* messages operations */ 333/* messages operations */
331 334
332static int pop3driver_remove_message(mailsession * session, uint32_t num) 335static int pop3driver_remove_message(mailsession * session, uint32_t num)
333{ 336{
334 mailpop3 * pop3; 337 mailpop3 * pop3;
335 int r; 338 int r;
336 339
337 pop3 = get_pop3_session(session); 340 pop3 = get_pop3_session(session);
338 341
339 r = mailpop3_dele(pop3, num); 342 r = mailpop3_dele(pop3, num);
340 switch (r) { 343 switch (r) {
341 case MAILPOP3_ERROR_BAD_STATE: 344 case MAILPOP3_ERROR_BAD_STATE:
342 return MAIL_ERROR_BAD_STATE; 345 return MAIL_ERROR_BAD_STATE;
343 346
344 case MAILPOP3_ERROR_NO_SUCH_MESSAGE: 347 case MAILPOP3_ERROR_NO_SUCH_MESSAGE:
345 return MAIL_ERROR_MSG_NOT_FOUND; 348 return MAIL_ERROR_MSG_NOT_FOUND;
346 349
347 case MAILPOP3_ERROR_STREAM: 350 case MAILPOP3_ERROR_STREAM:
348 return MAIL_ERROR_STREAM; 351 return MAIL_ERROR_STREAM;
349 352
350 case MAILPOP3_NO_ERROR: 353 case MAILPOP3_NO_ERROR:
351 return MAIL_NO_ERROR; 354 return MAIL_NO_ERROR;
352 355
353 default: 356 default:
354 return MAIL_ERROR_REMOVE; 357 return MAIL_ERROR_REMOVE;
355 } 358 }
356} 359}
357 360
358static int pop3driver_get_messages_list(mailsession * session, 361static int pop3driver_get_messages_list(mailsession * session,
359 struct mailmessage_list ** result) 362 struct mailmessage_list ** result)
360{ 363{
361 mailpop3 * pop3; 364 mailpop3 * pop3;
362 365
363 pop3 = get_pop3_session(session); 366 pop3 = get_pop3_session(session);
364 367
365 return pop3_get_messages_list(pop3, session, 368 return pop3_get_messages_list(pop3, session,
366 pop3_message_driver, result); 369 pop3_message_driver, result);
367} 370}
368 371
369static int pop3driver_get_message(mailsession * session, 372static int pop3driver_get_message(mailsession * session,
370 uint32_t num, mailmessage ** result) 373 uint32_t num, mailmessage ** result)
371{ 374{
372 mailmessage * msg_info; 375 mailmessage * msg_info;
diff --git a/kmicromail/libetpan/pop3/mailpop3.c b/kmicromail/libetpan/pop3/mailpop3.c
index 28fafe9..691b07a 100644
--- a/kmicromail/libetpan/pop3/mailpop3.c
+++ b/kmicromail/libetpan/pop3/mailpop3.c
@@ -415,194 +415,197 @@ int mailpop3_apop(mailpop3 * f,
415{ 415{
416 char command[POP3_STRING_SIZE]; 416 char command[POP3_STRING_SIZE];
417 MD5_CTX md5context; 417 MD5_CTX md5context;
418 unsigned char md5digest[16]; 418 unsigned char md5digest[16];
419 char md5string[33]; 419 char md5string[33];
420 char * cmd_ptr; 420 char * cmd_ptr;
421 int r; 421 int r;
422 int i; 422 int i;
423 char * response; 423 char * response;
424 424
425 if (f->pop3_state != POP3_STATE_AUTHORIZATION) 425 if (f->pop3_state != POP3_STATE_AUTHORIZATION)
426 return MAILPOP3_ERROR_BAD_STATE; 426 return MAILPOP3_ERROR_BAD_STATE;
427 427
428 if (f->pop3_timestamp == NULL) 428 if (f->pop3_timestamp == NULL)
429 return MAILPOP3_ERROR_APOP_NOT_SUPPORTED; 429 return MAILPOP3_ERROR_APOP_NOT_SUPPORTED;
430 430
431 /* calculate md5 sum */ 431 /* calculate md5 sum */
432 432
433 MD5Init(&md5context); 433 MD5Init(&md5context);
434 MD5Update(&md5context, f->pop3_timestamp, strlen (f->pop3_timestamp)); 434 MD5Update(&md5context, f->pop3_timestamp, strlen (f->pop3_timestamp));
435 MD5Update(&md5context, password, strlen (password)); 435 MD5Update(&md5context, password, strlen (password));
436 MD5Final(md5digest, &md5context); 436 MD5Final(md5digest, &md5context);
437 437
438 cmd_ptr = md5string; 438 cmd_ptr = md5string;
439 for(i = 0 ; i < 16 ; i++, cmd_ptr += 2) 439 for(i = 0 ; i < 16 ; i++, cmd_ptr += 2)
440 snprintf(cmd_ptr, 3, "%02x", md5digest[i]); 440 snprintf(cmd_ptr, 3, "%02x", md5digest[i]);
441 * cmd_ptr = 0; 441 * cmd_ptr = 0;
442 442
443 /* send apop command */ 443 /* send apop command */
444 444
445 snprintf(command, POP3_STRING_SIZE, "APOP %s %s\r\n", user, md5string); 445 snprintf(command, POP3_STRING_SIZE, "APOP %s %s\r\n", user, md5string);
446 r = send_command(f, command); 446 r = send_command(f, command);
447 if (r == -1) 447 if (r == -1)
448 return MAILPOP3_ERROR_STREAM; 448 return MAILPOP3_ERROR_STREAM;
449 449
450 response = read_line(f); 450 response = read_line(f);
451 451
452 if (response == NULL) 452 if (response == NULL)
453 return MAILPOP3_ERROR_STREAM; 453 return MAILPOP3_ERROR_STREAM;
454 r = parse_response(f, response); 454 r = parse_response(f, response);
455 if (r != RESPONSE_OK) 455 if (r != RESPONSE_OK)
456 return MAILPOP3_ERROR_DENIED; 456 return MAILPOP3_ERROR_DENIED;
457 457
458 f->pop3_state = POP3_STATE_TRANSACTION; 458 f->pop3_state = POP3_STATE_TRANSACTION;
459 459
460 return MAILPOP3_NO_ERROR; 460 return MAILPOP3_NO_ERROR;
461} 461}
462 462
463int mailpop3_user(mailpop3 * f, const char * user) 463int mailpop3_user(mailpop3 * f, const char * user)
464{ 464{
465 char command[POP3_STRING_SIZE]; 465 char command[POP3_STRING_SIZE];
466 int r; 466 int r;
467 char * response; 467 char * response;
468 468
469 if (f->pop3_state != POP3_STATE_AUTHORIZATION) 469 if (f->pop3_state != POP3_STATE_AUTHORIZATION)
470 return MAILPOP3_ERROR_BAD_STATE; 470 return MAILPOP3_ERROR_BAD_STATE;
471 471
472 /* send user command */ 472 /* send user command */
473 473
474 snprintf(command, POP3_STRING_SIZE, "USER %s\r\n", user); 474 snprintf(command, POP3_STRING_SIZE, "USER %s\r\n", user);
475 r = send_command(f, command); 475 r = send_command(f, command);
476 if (r == -1) 476 if (r == -1)
477 return MAILPOP3_ERROR_STREAM; 477 return MAILPOP3_ERROR_STREAM;
478 478
479 response = read_line(f); 479 response = read_line(f);
480 if (response == NULL) 480 if (response == NULL)
481 return MAILPOP3_ERROR_STREAM; 481 return MAILPOP3_ERROR_STREAM;
482 r = parse_response(f, response); 482 r = parse_response(f, response);
483 483
484 if (r != RESPONSE_OK) 484 if (r != RESPONSE_OK)
485 return MAILPOP3_ERROR_BAD_USER; 485 return MAILPOP3_ERROR_BAD_USER;
486 486
487 return MAILPOP3_NO_ERROR; 487 return MAILPOP3_NO_ERROR;
488} 488}
489 489
490int mailpop3_pass(mailpop3 * f, const char * password) 490int mailpop3_pass(mailpop3 * f, const char * password)
491{ 491{
492 char command[POP3_STRING_SIZE]; 492 char command[POP3_STRING_SIZE];
493 int r; 493 int r;
494 char * response; 494 char * response;
495 495
496 if (f->pop3_state != POP3_STATE_AUTHORIZATION) 496 if (f->pop3_state != POP3_STATE_AUTHORIZATION)
497 return MAILPOP3_ERROR_BAD_STATE; 497 return MAILPOP3_ERROR_BAD_STATE;
498 498
499 /* send password command */ 499 /* send password command */
500 500
501 snprintf(command, POP3_STRING_SIZE, "PASS %s\r\n", password); 501 snprintf(command, POP3_STRING_SIZE, "PASS %s\r\n", password);
502 r = send_command(f, command); 502 r = send_command(f, command);
503 if (r == -1) 503 if (r == -1)
504 return MAILPOP3_ERROR_STREAM; 504 return MAILPOP3_ERROR_STREAM;
505 505
506 response = read_line(f); 506 response = read_line(f);
507 if (response == NULL) 507 if (response == NULL)
508 return MAILPOP3_ERROR_STREAM; 508 return MAILPOP3_ERROR_STREAM;
509 r = parse_response(f, response); 509 r = parse_response(f, response);
510 510
511 if (r != RESPONSE_OK) 511 if (r != RESPONSE_OK) {
512 // LR
513 fprintf(stderr,"POP3 login error. Response from server:\n%s\n",response );
512 return MAILPOP3_ERROR_BAD_PASSWORD; 514 return MAILPOP3_ERROR_BAD_PASSWORD;
515 }
513 516
514 f->pop3_state = POP3_STATE_TRANSACTION; 517 f->pop3_state = POP3_STATE_TRANSACTION;
515 518
516 return MAILPOP3_NO_ERROR; 519 return MAILPOP3_NO_ERROR;
517} 520}
518 521
519static int read_list(mailpop3 * f, carray ** result); 522static int read_list(mailpop3 * f, carray ** result);
520 523
521 524
522 525
523static int read_uidl(mailpop3 * f, carray * msg_tab); 526static int read_uidl(mailpop3 * f, carray * msg_tab);
524 527
525 528
526 529
527static int mailpop3_do_uidl(mailpop3 * f, carray * msg_tab) 530static int mailpop3_do_uidl(mailpop3 * f, carray * msg_tab)
528{ 531{
529 char command[POP3_STRING_SIZE]; 532 char command[POP3_STRING_SIZE];
530 int r; 533 int r;
531 char * response; 534 char * response;
532 535
533 if (f->pop3_state != POP3_STATE_TRANSACTION) 536 if (f->pop3_state != POP3_STATE_TRANSACTION)
534 return MAILPOP3_ERROR_BAD_STATE; 537 return MAILPOP3_ERROR_BAD_STATE;
535 538
536 /* send list command */ 539 /* send list command */
537 540
538 snprintf(command, POP3_STRING_SIZE, "UIDL\r\n"); 541 snprintf(command, POP3_STRING_SIZE, "UIDL\r\n");
539 r = send_command(f, command); 542 r = send_command(f, command);
540 if (r == -1) 543 if (r == -1)
541 return MAILPOP3_ERROR_STREAM; 544 return MAILPOP3_ERROR_STREAM;
542 545
543 response = read_line(f); 546 response = read_line(f);
544 if (response == NULL) 547 if (response == NULL)
545 return MAILPOP3_ERROR_STREAM; 548 return MAILPOP3_ERROR_STREAM;
546 r = parse_response(f, response); 549 r = parse_response(f, response);
547 550
548 if (r != RESPONSE_OK) 551 if (r != RESPONSE_OK)
549 return MAILPOP3_ERROR_CANT_LIST; 552 return MAILPOP3_ERROR_CANT_LIST;
550 553
551 r = read_uidl(f, msg_tab); 554 r = read_uidl(f, msg_tab);
552 if (r != MAILPOP3_NO_ERROR) 555 if (r != MAILPOP3_NO_ERROR)
553 return r; 556 return r;
554 557
555 return MAILPOP3_NO_ERROR; 558 return MAILPOP3_NO_ERROR;
556} 559}
557 560
558 561
559 562
560static int mailpop3_do_list(mailpop3 * f) 563static int mailpop3_do_list(mailpop3 * f)
561{ 564{
562 char command[POP3_STRING_SIZE]; 565 char command[POP3_STRING_SIZE];
563 int r; 566 int r;
564 carray * msg_tab; 567 carray * msg_tab;
565 char * response; 568 char * response;
566 569
567 if (f->pop3_msg_tab != NULL) { 570 if (f->pop3_msg_tab != NULL) {
568 mailpop3_msg_info_tab_free(f->pop3_msg_tab); 571 mailpop3_msg_info_tab_free(f->pop3_msg_tab);
569 f->pop3_msg_tab = NULL; 572 f->pop3_msg_tab = NULL;
570 } 573 }
571 574
572 if (f->pop3_state != POP3_STATE_TRANSACTION) 575 if (f->pop3_state != POP3_STATE_TRANSACTION)
573 return MAILPOP3_ERROR_BAD_STATE; 576 return MAILPOP3_ERROR_BAD_STATE;
574 577
575 /* send list command */ 578 /* send list command */
576 579
577 snprintf(command, POP3_STRING_SIZE, "LIST\r\n"); 580 snprintf(command, POP3_STRING_SIZE, "LIST\r\n");
578 r = send_command(f, command); 581 r = send_command(f, command);
579 if (r == -1) 582 if (r == -1)
580 return MAILPOP3_ERROR_STREAM; 583 return MAILPOP3_ERROR_STREAM;
581 584
582 response = read_line(f); 585 response = read_line(f);
583 if (response == NULL) 586 if (response == NULL)
584 return MAILPOP3_ERROR_STREAM; 587 return MAILPOP3_ERROR_STREAM;
585 r = parse_response(f, response); 588 r = parse_response(f, response);
586 589
587 if (r != RESPONSE_OK) 590 if (r != RESPONSE_OK)
588 return MAILPOP3_ERROR_CANT_LIST; 591 return MAILPOP3_ERROR_CANT_LIST;
589 592
590 r = read_list(f, &msg_tab); 593 r = read_list(f, &msg_tab);
591 if (r != MAILPOP3_NO_ERROR) 594 if (r != MAILPOP3_NO_ERROR)
592 return r; 595 return r;
593 596
594 f->pop3_msg_tab = msg_tab; 597 f->pop3_msg_tab = msg_tab;
595 f->pop3_deleted_count = 0; 598 f->pop3_deleted_count = 0;
596 599
597 mailpop3_do_uidl(f, msg_tab); 600 mailpop3_do_uidl(f, msg_tab);
598 601
599 return MAILPOP3_NO_ERROR; 602 return MAILPOP3_NO_ERROR;
600} 603}
601 604
602 605
603 606
604static void mailpop3_list_if_needed(mailpop3 * f) 607static void mailpop3_list_if_needed(mailpop3 * f)
605{ 608{
606 if (f->pop3_msg_tab == NULL) 609 if (f->pop3_msg_tab == NULL)
607 mailpop3_do_list(f); 610 mailpop3_do_list(f);
608} 611}
diff --git a/kmicromail/libetpan/tools/mailstream_helper.c b/kmicromail/libetpan/tools/mailstream_helper.c
index 146f955..92f4ffe 100644
--- a/kmicromail/libetpan/tools/mailstream_helper.c
+++ b/kmicromail/libetpan/tools/mailstream_helper.c
@@ -5,194 +5,201 @@
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the libEtPan! project nor the names of its 15 * 3. Neither the name of the libEtPan! project nor the names of its
16 * contributors may be used to endorse or promote products derived 16 * contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission. 17 * from this software without specific prior written permission.
18 * 18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE. 29 * SUCH DAMAGE.
30 */ 30 */
31 31
32/* 32/*
33 * $Id$ 33 * $Id$
34 */ 34 */
35 35
36#include "mailstream_helper.h" 36#include "mailstream_helper.h"
37#include <string.h> 37#include <string.h>
38#include <stdio.h> 38#include <stdio.h>
39#include "mail.h" 39#include "mail.h"
40 40
41static void remove_trailing_eol(MMAPString * mmapstr) 41static void remove_trailing_eol(MMAPString * mmapstr)
42{ 42{
43 if (mmapstr->str[mmapstr->len - 1] == '\n') { 43 if (mmapstr->str[mmapstr->len - 1] == '\n') {
44 mmapstr->len --; 44 mmapstr->len --;
45 mmapstr->str[mmapstr->len] = '\0'; 45 mmapstr->str[mmapstr->len] = '\0';
46 } 46 }
47 if (mmapstr->str[mmapstr->len - 1] == '\r') { 47 if (mmapstr->str[mmapstr->len - 1] == '\r') {
48 mmapstr->len --; 48 mmapstr->len --;
49 mmapstr->str[mmapstr->len] = '\0'; 49 mmapstr->str[mmapstr->len] = '\0';
50 } 50 }
51} 51}
52 52
53char * mailstream_read_line(mailstream * stream, MMAPString * line) 53char * mailstream_read_line(mailstream * stream, MMAPString * line)
54{ 54{
55 if (mmap_string_assign(line, "") == NULL) 55 if (mmap_string_assign(line, "") == NULL)
56 return NULL; 56 return NULL;
57 57
58 return mailstream_read_line_append(stream, line); 58 return mailstream_read_line_append(stream, line);
59} 59}
60 60
61static char * mailstream_read_len_append(mailstream * stream, 61static char * mailstream_read_len_append(mailstream * stream,
62 MMAPString * line, 62 MMAPString * line,
63 size_t i) 63 size_t i)
64{ 64{
65 size_t cur_size; 65 size_t cur_size;
66 66
67 cur_size = line->len; 67 cur_size = line->len;
68 if (mmap_string_set_size(line, line->len + i) == NULL) 68 if (mmap_string_set_size(line, line->len + i) == NULL)
69 return NULL; 69 return NULL;
70 if (mailstream_read(stream, line->str + cur_size, i) < 0) 70 if (mailstream_read(stream, line->str + cur_size, i) < 0)
71 return NULL; 71 return NULL;
72 return line->str; 72 return line->str;
73} 73}
74 74
75char * mailstream_read_line_append(mailstream * stream, MMAPString * line) 75char * mailstream_read_line_append(mailstream * stream, MMAPString * line)
76{ 76{
77 if (stream == NULL) 77 if (stream == NULL)
78 return NULL; 78 return NULL;
79 79
80 do { 80 do {
81 if (stream->read_buffer_len > 0) { 81 if (stream->read_buffer_len > 0) {
82 size_t i; 82 size_t i;
83 83
84 i = 0; 84 i = 0;
85 while (i < stream->read_buffer_len) { 85 while (i < stream->read_buffer_len) {
86 if (stream->read_buffer[i] == '\n') 86 if (stream->read_buffer[i] == '\n')
87 return mailstream_read_len_append(stream, line, i + 1); 87 return mailstream_read_len_append(stream, line, i + 1);
88 i++; 88 i++;
89 } 89 }
90 if (mailstream_read_len_append(stream, line, 90 if (mailstream_read_len_append(stream, line,
91 stream->read_buffer_len) == NULL) 91 stream->read_buffer_len) == NULL)
92 return NULL; 92 return NULL;
93 } 93 }
94 else { 94 else {
95 ssize_t r; 95 ssize_t r;
96 96
97 r = mailstream_feed_read_buffer(stream); 97 r = mailstream_feed_read_buffer(stream);
98 if (r == -1) 98 if (r == -1)
99 return NULL; 99 return NULL;
100 100
101 if (r == 0) 101 if (r == 0) {
102 break; 102 // LR
103 // this avoids a memory access violation later when trying
104 // to remove_trailing_eol from a null string
105 if ( line->len == 0 )
106 return NULL;
107 else
108 break;
109 }
103 } 110 }
104 } 111 }
105 while (1); 112 while (1);
106 113
107 return line->str; 114 return line->str;
108} 115}
109 116
110char * mailstream_read_line_remove_eol(mailstream * stream, MMAPString * line) 117char * mailstream_read_line_remove_eol(mailstream * stream, MMAPString * line)
111{ 118{
112 if (!mailstream_read_line(stream, line)) 119 if (!mailstream_read_line(stream, line))
113 return NULL; 120 return NULL;
114 121
115 remove_trailing_eol(line); 122 remove_trailing_eol(line);
116 123
117 return line->str; 124 return line->str;
118} 125}
119 126
120int mailstream_is_end_multiline(const char * line) 127int mailstream_is_end_multiline(const char * line)
121{ 128{
122 if (line[0] != '.') 129 if (line[0] != '.')
123 return FALSE; 130 return FALSE;
124 if (line[1] != 0) 131 if (line[1] != 0)
125 return FALSE; 132 return FALSE;
126 return TRUE; 133 return TRUE;
127} 134}
128 135
129#if 1 136#if 1
130char * mailstream_read_multiline(mailstream * s, size_t size, 137char * mailstream_read_multiline(mailstream * s, size_t size,
131 MMAPString * stream_buffer, 138 MMAPString * stream_buffer,
132 MMAPString * multiline_buffer, 139 MMAPString * multiline_buffer,
133 size_t progr_rate, 140 size_t progr_rate,
134 progress_function * progr_fun) 141 progress_function * progr_fun)
135{ 142{
136 size_t count; 143 size_t count;
137 char * line; 144 char * line;
138 size_t last; 145 size_t last;
139 146
140 if (mmap_string_assign(multiline_buffer, "") == NULL) 147 if (mmap_string_assign(multiline_buffer, "") == NULL)
141 return NULL; 148 return NULL;
142 149
143 count = 0; 150 count = 0;
144 last = 0; 151 last = 0;
145 152
146 while ((line = mailstream_read_line_remove_eol(s, stream_buffer)) != NULL) { 153 while ((line = mailstream_read_line_remove_eol(s, stream_buffer)) != NULL) {
147 if (mailstream_is_end_multiline(line)) 154 if (mailstream_is_end_multiline(line))
148 return multiline_buffer->str; 155 return multiline_buffer->str;
149 156
150 if (line[0] == '.') { 157 if (line[0] == '.') {
151 if (mmap_string_append(multiline_buffer, line + 1) == NULL) 158 if (mmap_string_append(multiline_buffer, line + 1) == NULL)
152 return NULL; 159 return NULL;
153 } 160 }
154 else { 161 else {
155 if (mmap_string_append(multiline_buffer, line) == NULL) 162 if (mmap_string_append(multiline_buffer, line) == NULL)
156 return NULL; 163 return NULL;
157 } 164 }
158 if (mmap_string_append(multiline_buffer, "\r\n") == NULL) 165 if (mmap_string_append(multiline_buffer, "\r\n") == NULL)
159 return NULL; 166 return NULL;
160 167
161 count += strlen(line); 168 count += strlen(line);
162 if ((size != 0) && (progr_rate != 0) && (progr_fun != NULL)) 169 if ((size != 0) && (progr_rate != 0) && (progr_fun != NULL))
163 if (count - last >= progr_rate) { 170 if (count - last >= progr_rate) {
164 (* progr_fun)(count, size); 171 (* progr_fun)(count, size);
165 last = count; 172 last = count;
166 } 173 }
167 } 174 }
168 175
169 return NULL; 176 return NULL;
170} 177}
171 178
172#else 179#else
173 180
174/* 181/*
175 high speed but don't replace the line break with '\n' and neither 182 high speed but don't replace the line break with '\n' and neither
176 remove the '.' 183 remove the '.'
177*/ 184*/
178 185
179static gboolean end_of_multiline(const char * str, gint len) 186static gboolean end_of_multiline(const char * str, gint len)
180{ 187{
181 gint index; 188 gint index;
182 189
183 index = len - 1; 190 index = len - 1;
184 191
185 if (str[index] != '\n') 192 if (str[index] != '\n')
186 return FALSE; 193 return FALSE;
187 if (index == 0) 194 if (index == 0)
188 return FALSE; 195 return FALSE;
189 196
190 index --; 197 index --;
191 198
192 if (str[index] == '\r') { 199 if (str[index] == '\r') {
193 index --; 200 index --;
194 if (index == 0) 201 if (index == 0)
195 return FALSE; 202 return FALSE;
196 } 203 }
197 204
198 if (str[index] != '.') 205 if (str[index] != '.')