summaryrefslogtreecommitdiffabout
path: root/kmicromail/libetpan/pop3/mailpop3.c
Unidiff
Diffstat (limited to 'kmicromail/libetpan/pop3/mailpop3.c') (more/less context) (ignore whitespace changes)
-rw-r--r--kmicromail/libetpan/pop3/mailpop3.c5
1 files changed, 4 insertions, 1 deletions
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
@@ -319,386 +319,389 @@ int mailpop3_connect(mailpop3 * f, mailstream * s)
319 319
320 f->pop3_state = POP3_STATE_AUTHORIZATION; 320 f->pop3_state = POP3_STATE_AUTHORIZATION;
321 321
322 timestamp = mailpop3_get_timestamp(f->pop3_response); 322 timestamp = mailpop3_get_timestamp(f->pop3_response);
323 if (timestamp != NULL) 323 if (timestamp != NULL)
324 f->pop3_timestamp = timestamp; 324 f->pop3_timestamp = timestamp;
325 325
326 return MAILPOP3_NO_ERROR; 326 return MAILPOP3_NO_ERROR;
327} 327}
328 328
329 329
330/* 330/*
331 disconnect from a pop3 server 331 disconnect from a pop3 server
332*/ 332*/
333 333
334int mailpop3_quit(mailpop3 * f) 334int mailpop3_quit(mailpop3 * f)
335{ 335{
336 char command[POP3_STRING_SIZE]; 336 char command[POP3_STRING_SIZE];
337 char * response; 337 char * response;
338 int r; 338 int r;
339 int res; 339 int res;
340 340
341 if ((f->pop3_state != POP3_STATE_AUTHORIZATION) 341 if ((f->pop3_state != POP3_STATE_AUTHORIZATION)
342 && (f->pop3_state != POP3_STATE_TRANSACTION)) { 342 && (f->pop3_state != POP3_STATE_TRANSACTION)) {
343 res = MAILPOP3_ERROR_BAD_STATE; 343 res = MAILPOP3_ERROR_BAD_STATE;
344 goto close; 344 goto close;
345 } 345 }
346 346
347 snprintf(command, POP3_STRING_SIZE, "QUIT\r\n"); 347 snprintf(command, POP3_STRING_SIZE, "QUIT\r\n");
348 r = send_command(f, command); 348 r = send_command(f, command);
349 if (r == -1) { 349 if (r == -1) {
350 res = MAILPOP3_ERROR_STREAM; 350 res = MAILPOP3_ERROR_STREAM;
351 goto close; 351 goto close;
352 } 352 }
353 353
354 response = read_line(f); 354 response = read_line(f);
355 if (response == NULL) { 355 if (response == NULL) {
356 res = MAILPOP3_ERROR_STREAM; 356 res = MAILPOP3_ERROR_STREAM;
357 goto close; 357 goto close;
358 } 358 }
359 parse_response(f, response); 359 parse_response(f, response);
360 360
361 res = MAILPOP3_NO_ERROR; 361 res = MAILPOP3_NO_ERROR;
362 362
363 close: 363 close:
364 mailstream_close(f->pop3_stream); 364 mailstream_close(f->pop3_stream);
365 365
366 if (f->pop3_timestamp != NULL) { 366 if (f->pop3_timestamp != NULL) {
367 free(f->pop3_timestamp); 367 free(f->pop3_timestamp);
368 f->pop3_timestamp = NULL; 368 f->pop3_timestamp = NULL;
369 } 369 }
370 370
371 f->pop3_stream = NULL; 371 f->pop3_stream = NULL;
372 if (f->pop3_msg_tab != NULL) { 372 if (f->pop3_msg_tab != NULL) {
373 mailpop3_msg_info_tab_free(f->pop3_msg_tab); 373 mailpop3_msg_info_tab_free(f->pop3_msg_tab);
374 f->pop3_msg_tab = NULL; 374 f->pop3_msg_tab = NULL;
375 } 375 }
376 376
377 f->pop3_state = POP3_STATE_DISCONNECTED; 377 f->pop3_state = POP3_STATE_DISCONNECTED;
378 378
379 return res; 379 return res;
380} 380}
381 381
382 382
383 383
384 384
385 385
386 386
387 387
388 388
389 389
390 390
391 391
392 392
393 393
394 394
395 395
396 396
397 397
398 398
399 399
400 400
401 401
402 402
403 403
404 404
405 405
406 406
407 407
408 408
409 409
410 410
411 411
412 412
413int mailpop3_apop(mailpop3 * f, 413int mailpop3_apop(mailpop3 * f,
414 const char * user, const char * password) 414 const char * user, const char * password)
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}
609 612
610/* 613/*
611 mailpop3_list 614 mailpop3_list
612*/ 615*/
613 616
614void mailpop3_list(mailpop3 * f, carray ** result) 617void mailpop3_list(mailpop3 * f, carray ** result)
615{ 618{
616 mailpop3_list_if_needed(f); 619 mailpop3_list_if_needed(f);
617 * result = f->pop3_msg_tab; 620 * result = f->pop3_msg_tab;
618} 621}
619 622
620static inline struct mailpop3_msg_info * 623static inline struct mailpop3_msg_info *
621find_msg(mailpop3 * f, unsigned int index) 624find_msg(mailpop3 * f, unsigned int index)
622{ 625{
623 mailpop3_list_if_needed(f); 626 mailpop3_list_if_needed(f);
624 627
625 if (f->pop3_msg_tab == NULL) 628 if (f->pop3_msg_tab == NULL)
626 return NULL; 629 return NULL;
627 630
628 return mailpop3_msg_info_tab_find_msg(f->pop3_msg_tab, index); 631 return mailpop3_msg_info_tab_find_msg(f->pop3_msg_tab, index);
629} 632}
630 633
631 634
632 635
633 636
634 637
635 638
636 639
637 640
638static void mailpop3_multiline_response_free(char * str) 641static void mailpop3_multiline_response_free(char * str)
639{ 642{
640 mmap_string_unref(str); 643 mmap_string_unref(str);
641} 644}
642 645
643void mailpop3_top_free(char * str) 646void mailpop3_top_free(char * str)
644{ 647{
645 mailpop3_multiline_response_free(str); 648 mailpop3_multiline_response_free(str);
646} 649}
647 650
648void mailpop3_retr_free(char * str) 651void mailpop3_retr_free(char * str)
649{ 652{
650 mailpop3_multiline_response_free(str); 653 mailpop3_multiline_response_free(str);
651} 654}
652 655
653/* 656/*
654 mailpop3_retr 657 mailpop3_retr
655 658
656 message content in (* result) is still there until the 659 message content in (* result) is still there until the
657 next retrieve or top operation on the mailpop3 structure 660 next retrieve or top operation on the mailpop3 structure
658*/ 661*/
659 662
660static int 663static int
661mailpop3_get_content(mailpop3 * f, struct mailpop3_msg_info * msginfo, 664mailpop3_get_content(mailpop3 * f, struct mailpop3_msg_info * msginfo,
662 char ** result, size_t * result_len) 665 char ** result, size_t * result_len)
663{ 666{
664 char * response; 667 char * response;
665 char * result_multiline; 668 char * result_multiline;
666 MMAPString * buffer; 669 MMAPString * buffer;
667 int r; 670 int r;
668 671
669 response = read_line(f); 672 response = read_line(f);
670 if (response == NULL) 673 if (response == NULL)
671 return MAILPOP3_ERROR_STREAM; 674 return MAILPOP3_ERROR_STREAM;
672 r = parse_response(f, response); 675 r = parse_response(f, response);
673 if (r != RESPONSE_OK) 676 if (r != RESPONSE_OK)
674 return MAILPOP3_ERROR_NO_SUCH_MESSAGE; 677 return MAILPOP3_ERROR_NO_SUCH_MESSAGE;
675 678
676 buffer = mmap_string_new(""); 679 buffer = mmap_string_new("");
677 if (buffer == NULL) 680 if (buffer == NULL)
678 return MAILPOP3_ERROR_MEMORY; 681 return MAILPOP3_ERROR_MEMORY;
679 682
680 result_multiline = read_multiline(f, msginfo->msg_size, buffer); 683 result_multiline = read_multiline(f, msginfo->msg_size, buffer);
681 if (result_multiline == NULL) { 684 if (result_multiline == NULL) {
682 mmap_string_free(buffer); 685 mmap_string_free(buffer);
683 return MAILPOP3_ERROR_STREAM; 686 return MAILPOP3_ERROR_STREAM;
684 } 687 }
685 else { 688 else {
686 r = mmap_string_ref(buffer); 689 r = mmap_string_ref(buffer);
687 if (r < 0) { 690 if (r < 0) {
688 mmap_string_free(buffer); 691 mmap_string_free(buffer);
689 return MAILPOP3_ERROR_MEMORY; 692 return MAILPOP3_ERROR_MEMORY;
690 } 693 }
691 694
692 * result = result_multiline; 695 * result = result_multiline;
693 * result_len = buffer->len; 696 * result_len = buffer->len;
694 return MAILPOP3_NO_ERROR; 697 return MAILPOP3_NO_ERROR;
695 } 698 }
696} 699}
697 700
698int mailpop3_retr(mailpop3 * f, unsigned int index, char ** result, 701int mailpop3_retr(mailpop3 * f, unsigned int index, char ** result,
699 size_t * result_len) 702 size_t * result_len)
700{ 703{
701 char command[POP3_STRING_SIZE]; 704 char command[POP3_STRING_SIZE];
702 struct mailpop3_msg_info * msginfo; 705 struct mailpop3_msg_info * msginfo;
703 int r; 706 int r;
704 707