(imap_reader, imap_writer)
(imap_stream_ctl): New static functions. (tls): Uses now mu_tls_begin() from libmuauth. (folder_imap_open): Use MU_IMAP_PORT.
Showing
1 changed file
with
41 additions
and
18 deletions
... | @@ -54,6 +54,7 @@ | ... | @@ -54,6 +54,7 @@ |
54 | #include <mailutils/argcv.h> | 54 | #include <mailutils/argcv.h> |
55 | #include <mailutils/tls.h> | 55 | #include <mailutils/tls.h> |
56 | #include <mailutils/nls.h> | 56 | #include <mailutils/nls.h> |
57 | #include <mu_umaxtostr.h> | ||
57 | 58 | ||
58 | /* For dbg purposes set to one to see different level of traffic. */ | 59 | /* For dbg purposes set to one to see different level of traffic. */ |
59 | /* Print to stderr the command sent to the IMAP server. */ | 60 | /* Print to stderr the command sent to the IMAP server. */ |
... | @@ -537,37 +538,59 @@ check_capa (f_imap_t f_imap, char *capa) | ... | @@ -537,37 +538,59 @@ check_capa (f_imap_t f_imap, char *capa) |
537 | 538 | ||
538 | 539 | ||
539 | static int | 540 | static int |
541 | imap_reader (void *iodata) | ||
542 | { | ||
543 | f_imap_t iop = iodata; | ||
544 | int status = imap_parse (iop); | ||
545 | CHECK_EAGAIN (iop, status); | ||
546 | FOLDER_DEBUG0 (iop->folder, MU_DEBUG_PROT, iop->buffer); | ||
547 | return status; | ||
548 | } | ||
549 | |||
550 | static int | ||
551 | imap_writer (void *iodata, char *buf) | ||
552 | { | ||
553 | f_imap_t iop = iodata; | ||
554 | FOLDER_DEBUG2 (iop->folder, MU_DEBUG_PROT, "g%s %s\n", | ||
555 | mu_umaxtostr (0, iop->seq), buf); | ||
556 | int status = imap_writeline (iop, "g%s %s\r\n", | ||
557 | mu_umaxtostr (0, iop->seq++), buf); | ||
558 | CHECK_ERROR (iop, status); | ||
559 | status = imap_send (iop); | ||
560 | CHECK_ERROR (iop, status); | ||
561 | return status; | ||
562 | } | ||
563 | |||
564 | static void | ||
565 | imap_stream_ctl (void *iodata, mu_stream_t *pold, mu_stream_t new) | ||
566 | { | ||
567 | f_imap_t iop = iodata; | ||
568 | if (pold) | ||
569 | *pold = iop->folder->stream; | ||
570 | if (new) | ||
571 | iop->folder->stream = new; | ||
572 | } | ||
573 | |||
574 | static int | ||
540 | tls (mu_folder_t folder) | 575 | tls (mu_folder_t folder) |
541 | { | 576 | { |
542 | #ifdef WITH_TLS | 577 | #ifdef WITH_TLS |
543 | int status; | 578 | int status; |
544 | f_imap_t f_imap = folder->data; | 579 | f_imap_t f_imap = folder->data; |
580 | char *keywords[] = { "STARTTLS", "CAPABILITY", NULL }; | ||
545 | 581 | ||
546 | if (!mu_tls_enable || check_capa (f_imap, "STARTTLS")) | 582 | if (!mu_tls_enable || check_capa (f_imap, "STARTTLS")) |
547 | return -1; | 583 | return -1; |
548 | 584 | ||
549 | FOLDER_DEBUG1 (folder, MU_DEBUG_PROT, "g%u STARTTLS\n", f_imap->seq); | 585 | status = mu_tls_begin (f_imap, imap_reader, imap_writer, |
550 | status = imap_writeline (f_imap, "g%u STARTTLS\r\n", f_imap->seq++); | 586 | imap_stream_ctl, keywords); |
551 | CHECK_ERROR (f_imap, status); | 587 | |
552 | status = imap_send (f_imap); | ||
553 | CHECK_ERROR (f_imap, status); | ||
554 | status = imap_parse (f_imap); | ||
555 | if (status == 0) | ||
556 | { | ||
557 | mu_stream_t str; | ||
558 | status = mu_tls_stream_create_client_from_tcp (&str, folder->stream, 0); | ||
559 | CHECK_ERROR (f_imap, status); | ||
560 | status = mu_stream_open (str); | ||
561 | if (status == 0) | ||
562 | folder->stream = str; | ||
563 | FOLDER_DEBUG1 (folder, MU_DEBUG_PROT, "TLS negotiation %s\n", | 588 | FOLDER_DEBUG1 (folder, MU_DEBUG_PROT, "TLS negotiation %s\n", |
564 | status == 0 ? "succeeded" : "failed"); | 589 | status == 0 ? "succeeded" : "failed"); |
565 | read_capa (f_imap, 1); | ||
566 | } | ||
567 | return status; | 590 | return status; |
568 | #else | 591 | #else |
569 | return -1; | 592 | return -1; |
570 | #endif | 593 | #endif /* WITH_TLS */ |
571 | } | 594 | } |
572 | 595 | ||
573 | /* Create/Open the stream for IMAP. */ | 596 | /* Create/Open the stream for IMAP. */ |
... | @@ -576,7 +599,7 @@ folder_imap_open (mu_folder_t folder, int flags) | ... | @@ -576,7 +599,7 @@ folder_imap_open (mu_folder_t folder, int flags) |
576 | { | 599 | { |
577 | f_imap_t f_imap = folder->data; | 600 | f_imap_t f_imap = folder->data; |
578 | const char *host; | 601 | const char *host; |
579 | long port = 143; /* default imap port. */ | 602 | long port = MU_IMAP_PORT; /* default imap port. */ |
580 | int status = 0; | 603 | int status = 0; |
581 | 604 | ||
582 | /* If we are already open for business, noop. */ | 605 | /* If we are already open for business, noop. */ | ... | ... |
-
Please register or sign in to post a comment