commit-mailutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[SCM] GNU Mailutils branch, stream-cleanup, updated. rel-2_1-122-ga08bf2


From: Sergey Poznyakoff
Subject: [SCM] GNU Mailutils branch, stream-cleanup, updated. rel-2_1-122-ga08bf28
Date: Sat, 04 Sep 2010 23:27:56 +0000

This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "GNU Mailutils".

http://git.savannah.gnu.org/cgit/mailutils.git/commit/?id=a08bf28c50373eb68b18f7537d8eac8746da4097

The branch, stream-cleanup has been updated
       via  a08bf28c50373eb68b18f7537d8eac8746da4097 (commit)
       via  566cb796939c33fd7cec50cf56eee8390118eac5 (commit)
       via  a544404651b5275dee81f7ca3b26e1eff8f33033 (commit)
      from  ed83b5a6fec2174efdec37cffa658d7069dc318b (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit a08bf28c50373eb68b18f7537d8eac8746da4097
Author: Sergey Poznyakoff <address@hidden>
Date:   Sun Sep 5 02:26:51 2010 +0300

    Re-implement pop3 client functions.
    
    * include/mailutils/filter.h (mu_filter_io) <eof>: New member.
    * mailbox/fltstream.c (init_iobuf): Initialize eof to 0.
    (filter_read): Break the loop if the decoder has set eof.
    * mailbox/xscript-stream.c (_xscript_ctl) <MU_IOCTL_SWAP_STREAM>: Handle
    a special case when the transport does not support stream swapping.
    
    * include/mailutils/pop3.h (mu_pop3_set_debug): Remove.
    (MU_POP3_TRACE_CLR, MU_POP3_TRACE_SET, MU_POP3_TRACE_QRY): New macros.
    (mu_pop3_trace): New proto.
    (mu_pop3_readline): Remove.
    (mu_pop3_getline): New proto.
    (mu_pop3_capa): Change signature.
    * include/mailutils/sys/pop3.h (mu_pop3_state): Remove the *_ACK states.
    (mu_pop3_work_buf): Remove.
    (MU_POP3_ACK, MU_POP3_TRACE): New defines.
    (_mu_pop3): Rewrite the structure.
    (mu_pop3_debug_cmd, mu_pop3_debug_ack): Remove functions.
    (MU_POP3_FISSET, MU_POP3_FSET, MU_POP3_FCLR): New macros.
    (_mu_pop3_trace_enable, _mu_pop3_trace_disable)
    (_mu_pop3_init): New protos.
    
    * include/mailutils/tls.h (mu_tls_stream_ctl_fn)
    (mu_tls_writeline_fn): Change typedefs.
    * libmu_auth/tls.c (mu_tls_begin): Update function calls
    accordingly.
    
    * libproto/pop/pop3_debug.c: Remove.
    * libproto/pop/pop3_trace.c: New function.
    * libproto/pop/Makefile.am (libmu_pop_la_SOURCES): Temporarly
    comment out folder.c, mbox.c and url.c.
    Remove pop3_debug.c.
    Add pop3_trace.c.
    * libproto/pop/pop3_capa.c: Rewrite.
    * libproto/pop/pop3_create.c: Rewrite.
    * libproto/pop/pop3_iterator.c: Rewrite.
    * libproto/pop/pop3_response.c: Rewrite.
    * libproto/pop/pop3_sendline.c: Rewrite.
    * libproto/pop/pop3_stls.c: Rewrite.
    * libproto/pop/pop3_stream.c: Rewrite.
    * libproto/pop/pop3_apop.c: Reflect changes to the pop3 framework.
    * libproto/pop/pop3_carrier.c: Likewise.
    * libproto/pop/pop3_connect.c: Likewise.
    * libproto/pop/pop3_dele.c: Likewise.
    * libproto/pop/pop3_destroy.c: Likewise.
    * libproto/pop/pop3_disconnect.c: Likewise.
    * libproto/pop/pop3_list.c: Likewise.
    * libproto/pop/pop3_lista.c: Likewise.
    * libproto/pop/pop3_noop.c: Likewise.
    * libproto/pop/pop3_pass.c: Likewise.
    * libproto/pop/pop3_quit.c: Likewise.
    * libproto/pop/pop3_retr.c: Likewise.
    * libproto/pop/pop3_readline.c: Likewise.
    * libproto/pop/pop3_rset.c: Likewise.
    * libproto/pop/pop3_stat.c: Likewise.
    * libproto/pop/pop3_top.c: Likewise.
    * libproto/pop/pop3_uidl.c: Likewise.
    * libproto/pop/pop3_uidla.c: Likewise.
    * libproto/pop/pop3_user.c: Likewise.
    
    * examples/pop3client.c: Implement the stls comand.
    (main) [WITH_TLS]: Call mu_init_tls_libs.
    (com_verbose): Redo verbose support.
    (com_capa): Implement "reread" option.

commit 566cb796939c33fd7cec50cf56eee8390118eac5
Author: Sergey Poznyakoff <address@hidden>
Date:   Sat Sep 4 12:31:37 2010 +0300

    Reorganize directory tree.
    
    Move libproto/include/(*)0.h to include/mailutils/sys/\1.h
    Remove libproto/include.

commit a544404651b5275dee81f7ca3b26e1eff8f33033
Author: Sergey Poznyakoff <address@hidden>
Date:   Sat Sep 4 00:00:09 2010 +0300

    Remove unused constants.
    
    * pop3d/pop3d.h (POP_MAXCMDLEN, BUFFERSIZE): Remove constants.
    (_QNX_SOURCE): Remove define.

-----------------------------------------------------------------------

Summary of changes:
 configure.ac                                       |    3 +-
 examples/pop3client.c                              |   45 +++-
 include/mailutils/filter.h                         |    3 +-
 include/mailutils/pop3.h                           |   74 +++---
 include/mailutils/sys/Makefile.am                  |   21 ++-
 {libproto/include => include/mailutils/sys}/amd.h  |   10 +-
 .../mailutils/sys/attribute.h                      |   10 +-
 .../auth0.h => include/mailutils/sys/auth.h        |   12 +-
 .../body0.h => include/mailutils/sys/body.h        |   10 +-
 .../debug0.h => include/mailutils/sys/debug.h      |   10 +-
 .../mailutils/sys/envelope.h                       |   10 +-
 .../folder0.h => include/mailutils/sys/folder.h    |   30 +--
 .../imap0.h => include/mailutils/sys/imap.h        |   95 ++++----
 .../mailutils/sys/iterator.h                       |   20 +-
 .../list0.h => include/mailutils/sys/list.h        |   26 +-
 .../mailbox0.h => include/mailutils/sys/mailbox.h  |   28 +--
 .../mailer0.h => include/mailutils/sys/mailer.h    |   30 +--
 .../monitor0.h => include/mailutils/sys/monitor.h  |   26 +-
 .../mailutils/sys/observer.h                       |   22 +-
 include/mailutils/sys/pop3.h                       |  166 ++++++------
 .../mailutils/sys/property.h                       |   30 +--
 .../mailutils/sys/registrar.h                      |   76 +++---
 .../include/url0.h => include/mailutils/sys/url.h  |   21 +-
 include/mailutils/tls.h                            |   11 +-
 libmu_auth/tls.c                                   |   45 +++-
 libproto/Makefile.am                               |    2 +-
 libproto/imap/Makefile.am                          |    2 +-
 libproto/imap/folder.c                             |    6 +-
 libproto/imap/mbox.c                               |    8 +-
 libproto/imap/url.c                                |    4 +-
 libproto/include/.gitignore                        |    2 -
 libproto/include/Makefile.am                       |   39 ---
 libproto/include/stream0.h                         |   76 ------
 libproto/maildir/Makefile.am                       |    2 +-
 libproto/maildir/folder.c                          |    8 +-
 libproto/maildir/mbox.c                            |    6 +-
 libproto/mailer/Makefile.am                        |    2 +-
 libproto/mailer/mbox.c                             |    4 +-
 libproto/mailer/prog.c                             |    8 +-
 libproto/mailer/remote.c                           |    6 +-
 libproto/mailer/sendmail.c                         |    8 +-
 libproto/mailer/smtp.c                             |    8 +-
 libproto/mbox/Makefile.am                          |    2 +-
 libproto/mbox/folder.c                             |    4 +-
 libproto/mbox/mbox0.h                              |    4 +-
 libproto/mh/Makefile.am                            |    2 +-
 libproto/mh/folder.c                               |   10 +-
 libproto/mh/mbox.c                                 |    6 +-
 libproto/nntp/Makefile.am                          |    2 +-
 libproto/nntp/folder.c                             |    6 +-
 libproto/nntp/mbox.c                               |    6 +-
 libproto/nntp/url.c                                |    2 +-
 libproto/pop/Makefile.am                           |   10 +-
 libproto/pop/folder.c                              |    8 +-
 libproto/pop/mbox.c                                |    8 +-
 libproto/pop/pop3_apop.c                           |   28 +--
 libproto/pop/pop3_capa.c                           |   80 +++++--
 libproto/pop/pop3_carrier.c                        |    4 +
 libproto/pop/pop3_connect.c                        |   30 +-
 libproto/pop/pop3_create.c                         |   40 +--
 libproto/pop/pop3_debug.c                          |   51 ----
 libproto/pop/pop3_dele.c                           |   11 +-
 libproto/pop/pop3_destroy.c                        |   13 +-
 libproto/pop/pop3_disconnect.c                     |    9 +-
 libproto/pop/pop3_iterator.c                       |   71 +++---
 libproto/pop/pop3_list.c                           |   14 +-
 libproto/pop/pop3_lista.c                          |   14 +-
 libproto/pop/pop3_noop.c                           |   11 +-
 libproto/pop/pop3_pass.c                           |   14 +-
 libproto/pop/pop3_quit.c                           |   12 +-
 libproto/pop/pop3_readline.c                       |  149 ++---------
 libproto/pop/pop3_response.c                       |   47 ++--
 libproto/pop/pop3_retr.c                           |   12 +-
 libproto/pop/pop3_rset.c                           |   11 +-
 libproto/pop/pop3_sendline.c                       |   80 +------
 libproto/pop/pop3_stat.c                           |   14 +-
 libproto/pop/pop3_stls.c                           |   73 +++---
 libproto/pop/pop3_stream.c                         |  276 ++++++++++++++------
 libproto/pop/pop3_top.c                            |   15 +-
 libproto/pop/pop3_trace.c                          |  118 +++++++++
 libproto/pop/pop3_uidl.c                           |   69 +++---
 libproto/pop/pop3_uidla.c                          |   14 +-
 libproto/pop/pop3_user.c                           |   11 +-
 libproto/pop/url.c                                 |    4 +-
 mailbox/amd.c                                      |    8 +-
 mailbox/assoc.c                                    |    2 +-
 mailbox/attribute.c                                |    2 +-
 mailbox/auth.c                                     |    2 +-
 mailbox/body.c                                     |    2 +-
 mailbox/debug.c                                    |    2 +-
 mailbox/envelope.c                                 |    2 +-
 mailbox/fltstream.c                                |   10 +-
 mailbox/folder.c                                   |    2 +-
 mailbox/iterator.c                                 |    4 +-
 mailbox/list.c                                     |    4 +-
 mailbox/listlist.c                                 |    4 +-
 mailbox/mailbox.c                                  |    4 +-
 mailbox/mailer.c                                   |    2 +-
 mailbox/mbx_default.c                              |    2 +-
 mailbox/mbxitr.c                                   |    2 +-
 mailbox/monitor.c                                  |    2 +-
 mailbox/mutil.c                                    |    2 +-
 mailbox/observer.c                                 |    2 +-
 mailbox/property.c                                 |    2 +-
 mailbox/registrar.c                                |    2 +-
 mailbox/ticket.c                                   |    2 +-
 mailbox/url.c                                      |    2 +-
 mailbox/wicket.c                                   |    4 +-
 mailbox/xscript-stream.c                           |   31 ++-
 pop3d/pop3d.h                                      |    7 -
 110 files changed, 1186 insertions(+), 1314 deletions(-)
 rename {libproto/include => include/mailutils/sys}/amd.h (95%)
 rename libproto/include/attribute0.h => include/mailutils/sys/attribute.h (91%)
 rename libproto/include/auth0.h => include/mailutils/sys/auth.h (94%)
 rename libproto/include/body0.h => include/mailutils/sys/body.h (92%)
 rename libproto/include/debug0.h => include/mailutils/sys/debug.h (92%)
 rename libproto/include/envelope0.h => include/mailutils/sys/envelope.h (91%)
 rename libproto/include/folder0.h => include/mailutils/sys/folder.h (84%)
 rename libproto/include/imap0.h => include/mailutils/sys/imap.h (80%)
 rename libproto/include/iterator0.h => include/mailutils/sys/iterator.h (88%)
 rename libproto/include/list0.h => include/mailutils/sys/list.h (84%)
 rename libproto/include/mailbox0.h => include/mailutils/sys/mailbox.h (88%)
 rename libproto/include/mailer0.h => include/mailutils/sys/mailer.h (81%)
 rename libproto/include/monitor0.h => include/mailutils/sys/monitor.h (79%)
 rename libproto/include/observer0.h => include/mailutils/sys/observer.h (86%)
 rename libproto/include/property0.h => include/mailutils/sys/property.h (76%)
 rename libproto/include/registrar0.h => include/mailutils/sys/registrar.h (57%)
 rename libproto/include/url0.h => include/mailutils/sys/url.h (90%)
 delete mode 100644 libproto/include/.gitignore
 delete mode 100644 libproto/include/Makefile.am
 delete mode 100644 libproto/include/stream0.h
 delete mode 100644 libproto/pop/pop3_debug.c
 create mode 100644 libproto/pop/pop3_trace.c

diff --git a/configure.ac b/configure.ac
index 832b332..7903774 100644
--- a/configure.ac
+++ b/configure.ac
@@ -69,7 +69,7 @@ AC_SUBST(MU_APP_LIBRARIES,'../libmu_argp/libmu_argp.a 
../libmu_cfg/libmu_cfg.la
 # header files. There are two exceptions, though. See NOTE before the
 # AC_CONFIG_LINKS statements, below.
 
-AC_SUBST(MU_LIB_COMMON_INCLUDES,'-I${top_builddir} -I${top_srcdir}/include  
-I${top_builddir}/include -I${top_srcdir}/mailbox 
-I${top_srcdir}/libproto/include')
+AC_SUBST(MU_LIB_COMMON_INCLUDES,'-I${top_builddir} -I${top_srcdir}/include  
-I${top_builddir}/include -I${top_srcdir}/mailbox')
 
 AC_SUBST(MU_APP_COMMON_INCLUDES,'-I${srcdir} -I${top_srcdir}/include 
-I${top_srcdir}/lib -I${top_builddir}/lib -I${top_builddir} 
-I${top_builddir}/include -I${top_srcdir}/mailbox -I${top_srcdir}/libmu_argp 
-I${top_srcdir}/libmu_cfg')
 
@@ -1363,7 +1363,6 @@ AC_CONFIG_FILES([
  libproto/pop/Makefile
  libproto/nntp/Makefile
  libproto/imap/Makefile
- libproto/include/Makefile
  maidag/Makefile
  mail/Makefile
  mail/testsuite/Makefile
diff --git a/examples/pop3client.c b/examples/pop3client.c
index b8f3db2..3751c0a 100644
--- a/examples/pop3client.c
+++ b/examples/pop3client.c
@@ -48,6 +48,7 @@
 #include <mailutils/argcv.h>
 #include <mailutils/cctype.h>
 #include <mailutils/cstr.h>
+#include <mailutils/tls.h>
 
 /* A structure which contains information on the commands this program
    can understand. */
@@ -80,6 +81,7 @@ int com_uidl (char *);
 int com_user (char *);
 int com_verbose (char *);
 int com_prompt (char *);
+int com_stls (char *);
 
 void initialize_readline (void);
 COMMAND *find_command (char *);
@@ -106,6 +108,7 @@ COMMAND commands[] = {
   { "retr", com_retr, "Dowload message: RETR msgno" },
   { "rset", com_rset, "Unmark all messages: RSET" },
   { "stat", com_stat, "Get the size and count of mailbox : STAT [msgno]" },
+  { "stls", com_stls, "Start TLS negotiation" },
   { "top", com_top, "Get the header of message: TOP msgno [lines]" },
   { "uidl", com_uidl, "Get the unique id of message: UIDL [msgno]" },
   { "user", com_user, "send login: USER user" },
@@ -310,7 +313,9 @@ main (int argc MU_ARG_UNUSED, char **argv)
   mu_set_program_name (argv[0]);
   prompt = strdup (DEFAULT_PROMPT);
   initialize_readline ();      /* Bind our completer. */
-
+#ifdef WITH_TLS
+  mu_init_tls_libs ();
+#endif  
   /* Loop reading and executing lines until the user quits. */
   while (!done)
     {
@@ -393,16 +398,9 @@ com_verbose (char *arg)
   if (pop3 != NULL)
     {
       if (verbose == 1)
-       {
-         mu_debug_t debug;
-         mu_debug_create (&debug, NULL);
-         mu_debug_set_level (debug, MU_DEBUG_LEVEL_UPTO (MU_DEBUG_PROT));
-         status = mu_pop3_set_debug (pop3, debug);
-       }
+       mu_pop3_trace (pop3, MU_POP3_TRACE_SET);
       else
-       {
-         status = mu_pop3_set_debug (pop3, NULL);
-       }
+       mu_pop3_trace (pop3, MU_POP3_TRACE_CLR);
     }
   return status;
 }
@@ -442,10 +440,24 @@ com_apop (char *arg)
 }
 
 int
-com_capa (char *arg MU_ARG_UNUSED)
+com_capa (char *arg)
 {
   mu_iterator_t iterator = NULL;
-  int status = mu_pop3_capa (pop3, &iterator);
+  int status;
+  int reread = 0;
+
+  if (arg && *arg)
+    {
+      if (strcmp (arg, "reread") == 0)
+       reread = 1;
+      else
+       {
+         mu_error ("%s: unknown argument", "capa");
+         return 0;
+       }
+    }
+
+  status = mu_pop3_capa (pop3, reread, &iterator);
 
   if (status == 0)
     {
@@ -609,6 +621,12 @@ com_stat (char *arg MU_ARG_UNUSED)
 }
 
 int
+com_stls (char *arg MU_ARG_UNUSED)
+{
+  return mu_pop3_stls (pop3);
+}
+
+int
 com_dele (char *arg)
 {
   unsigned msgno;
@@ -799,8 +817,7 @@ com_connect (char *arg)
 
       if (verbose)
        com_verbose ("on");
-      status =
-       mu_tcp_stream_create (&tcp, argv[0], n, MU_STREAM_READ);
+      status = mu_tcp_stream_create (&tcp, argv[0], n, MU_STREAM_READ);
       if (status == 0)
        {
          mu_pop3_set_carrier (pop3, tcp);
diff --git a/include/mailutils/filter.h b/include/mailutils/filter.h
index 03c43c2..2490f8b 100644
--- a/include/mailutils/filter.h
+++ b/include/mailutils/filter.h
@@ -42,6 +42,7 @@ struct mu_filter_io
   char *output;
   size_t osize;
   int errcode;
+  int eof;
 };
 
 enum mu_filter_command
@@ -57,7 +58,7 @@ enum mu_filter_result
     mu_filter_ok,
     mu_filter_falure,
     mu_filter_moreinput,
-    mu_filter_moreoutput,
+    mu_filter_moreoutput
   };
   
 typedef int (*mu_filter_new_data_t) (void **, int, void *);
diff --git a/include/mailutils/pop3.h b/include/mailutils/pop3.h
index d1c7db5..f815be8 100644
--- a/include/mailutils/pop3.h
+++ b/include/mailutils/pop3.h
@@ -29,7 +29,7 @@ extern "C" {
 #endif
 
 struct _mu_pop3;
-typedef struct _mu_pop3* mu_pop3_t;
+typedef struct _mu_pop3 *mu_pop3_t;
 
 #define MU_POP3_DEFAULT_PORT 110
 
@@ -45,23 +45,28 @@ extern int  mu_pop3_disconnect   (mu_pop3_t pop3);
 extern int  mu_pop3_set_timeout  (mu_pop3_t pop3, int timeout);
 extern int  mu_pop3_get_timeout  (mu_pop3_t pop3, int *timeout);
 
-extern int  mu_pop3_set_debug    (mu_pop3_t pop3, mu_debug_t debug);
+#define MU_POP3_TRACE_CLR 0
+#define MU_POP3_TRACE_SET 1
+#define MU_POP3_TRACE_QRY 2
+extern int mu_pop3_trace (mu_pop3_t pop3, int op);
 
 extern int  mu_pop3_apop         (mu_pop3_t pop3, const char *name, const char 
*digest);
 
 extern int  mu_pop3_stls         (mu_pop3_t pop3);
 
-/* It is the responsability of the caller to call mu_iterator_destroy() when 
done
-   with the iterator.  The items return by the iterator are of type "const 
char *",
-   no processing is done on the item except the removal of the trailing 
newline.  */
-extern int  mu_pop3_capa         (mu_pop3_t pop3, mu_iterator_t *iterator);
+/* It is the responsability of the caller to call mu_iterator_destroy() when
+   done with the iterator.  The items returned by the iterator are of type
+   "const char *", no processing is done on the item except the removal of
+   the trailing newline.  */
+extern int  mu_pop3_capa         (mu_pop3_t pop3, int reread,
+                                 mu_iterator_t *piter);
 
 extern int  mu_pop3_dele         (mu_pop3_t pop3, unsigned int mesgno);
 
 extern int  mu_pop3_list         (mu_pop3_t pop3, unsigned int mesgno, size_t 
*mesg_octet);
 
-/* An iterator is return with the multi-line answer.  It is the responsability 
of
-   the caller to call mu_iterator_destroy() to dispose of the iterator.  */
+/* An iterator is return with the multi-line answer.  It is the responsability
+   of the caller to call mu_iterator_destroy() to dispose of the iterator.  */
 extern int  mu_pop3_list_all     (mu_pop3_t pop3, mu_iterator_t *piterator);
 
 extern int  mu_pop3_noop         (mu_pop3_t pop3);
@@ -70,49 +75,44 @@ extern int  mu_pop3_pass         (mu_pop3_t pop3, const 
char *pass);
 
 extern int  mu_pop3_quit         (mu_pop3_t pop3);
 
-/* A stream is return with the multi-line answer.  It is the responsability of
-   the caller to call mu_stream_destroy() to dipose of the stream.  */
-extern int  mu_pop3_retr         (mu_pop3_t pop3, unsigned int mesgno, 
mu_stream_t *pstream);
+/* A stream is returned with the multi-line answer.  It is the responsability
+   of the caller to call mu_stream_destroy() to dipose of the stream.  */
+extern int  mu_pop3_retr         (mu_pop3_t pop3, unsigned int mesgno,
+                                 mu_stream_t *pstream);
 
 extern int  mu_pop3_rset         (mu_pop3_t pop3);
 
-extern int  mu_pop3_stat         (mu_pop3_t pop3, unsigned int *count, size_t 
*octets);
-
-/* A stream is return with the multi-line answer.  It is the responsability of
-   the caller to call mu_stream_destroy() to dipose of the stream.  */
-extern int  mu_pop3_top          (mu_pop3_t pop3, unsigned int mesgno, 
unsigned int lines, mu_stream_t *pstream);
-
-/* The uidl is malloc and return in puidl, it is the responsability of caller
-   to free() the uild when done.  */
-extern int  mu_pop3_uidl         (mu_pop3_t pop3, unsigned int mesgno, char 
**puidl);
-/* An iterator is return with the multi-line answer.  It is the responsability 
of
-   the caller to call mu_iterator_destroy() to dispose of the iterator.  */
+extern int  mu_pop3_stat         (mu_pop3_t pop3, unsigned int *count,
+                                 size_t *octets);
+
+/* A stream is returned with the multi-line answer.  It is the responsability
+   of the caller to call mu_stream_destroy() to dipose of the stream.  */
+extern int  mu_pop3_top          (mu_pop3_t pop3, unsigned int mesgno,
+                                 unsigned int lines, mu_stream_t *pstream);
+
+/* The uidl is malloc'ed and returned in puidl; it is the responsability of
+   the caller to free() the uild when done.  */
+extern int  mu_pop3_uidl         (mu_pop3_t pop3, unsigned int mesgno,
+                                 char **puidl);
+/* An iterator is returned with the multi-line answer.  It is the
+   responsability of the caller to call mu_iterator_destroy() to dispose of
+   the iterator.  */
 extern int  mu_pop3_uidl_all     (mu_pop3_t pop3, mu_iterator_t *piterator);
 
 extern int  mu_pop3_user         (mu_pop3_t pop3, const char *user);
 
 
-/* Reads the multi-line response of the server, nread will be 0 when the 
termination octets
-   are detected.  Clients should not use this function unless they are sending 
direct command.  */
-extern int  mu_pop3_readline     (mu_pop3_t pop3, char *buffer, size_t buflen, 
size_t *nread);
 
-/* Returns the last command acknowledge.  If the server supports RESP-CODE, 
the message
-   could be retrieve, but it is up the caller to do the parsing.  */
-extern int  mu_pop3_response     (mu_pop3_t pop3, char *buffer, size_t buflen, 
size_t *nread);
+/* Returns the last command acknowledge.  If the server supports RESP-CODE,
+   the message could be retrieved, but it is up to the caller to do the
+   parsing.  */
+extern int  mu_pop3_response     (mu_pop3_t pop3, size_t *nread);
 
-/* pop3_writeline copies the line in the internal buffer, a mu_pop3_send() is
-   needed to do the actual transmission.  */
 extern int  mu_pop3_writeline    (mu_pop3_t pop3, const char *format, ...)
                                   MU_PRINTFLIKE(2,3);
 
-/* mu_pop3_sendline() is equivalent to:
-       mu_pop3_writeline (pop3, line);
-       mu_pop3_send (pop3);
- */
 extern int  mu_pop3_sendline     (mu_pop3_t pop3, const char *line);
-
-/* Transmit via the carrier the internal buffer data.  */
-extern int  mu_pop3_send         (mu_pop3_t pop3);
+extern int mu_pop3_getline       (mu_pop3_t pop3);
 
 #ifdef __cplusplus
 }
diff --git a/include/mailutils/sys/Makefile.am 
b/include/mailutils/sys/Makefile.am
index bc813e9..e8f073c 100644
--- a/include/mailutils/sys/Makefile.am
+++ b/include/mailutils/sys/Makefile.am
@@ -19,25 +19,42 @@
 
 sysincludedir=$(pkgincludedir)/sys
 sysinclude_HEADERS = \
+ amd.h\
+ attribute.h\
+ auth.h\
+ body.h\
  dbgstream.h\
+ debug.h\
+ envelope.h\
  file_stream.h\
  filter.h\
+ folder.h\
  gsasl-stream.h\
  header_stream.h\
  header.h\
+ imap.h\
+ iterator.h\
  iostream.h\
+ list.h\
+ mailbox.h\
+ mailer.h\
  mapfile_stream.h\
  memory_stream.h\
  message_stream.h\
  message.h\
  mime.h\
+ monitor.h\
+ nntp.h\
+ observer.h\
+ pop3.h\
  prog_stream.h\
+ property.h\
+ registrar.h\
  socket_stream.h\
  stdio_stream.h\
  streamref.h\
  streamtrans.h\
  stream.h\
  tls-stream.h\
- pop3.h\
- nntp.h\
+ url.h\
  xscript-stream.h
diff --git a/libproto/include/amd.h b/include/mailutils/sys/amd.h
similarity index 95%
rename from libproto/include/amd.h
rename to include/mailutils/sys/amd.h
index f643453..b72c3c1 100644
--- a/libproto/include/amd.h
+++ b/include/mailutils/sys/amd.h
@@ -17,10 +17,12 @@
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301 USA */
 
-#define MAX_OPEN_STREAMS 16
+#ifndef _MAILUTILS_SYS_AMD_H
+# define _MAILUTILS_SYS_AMD_H
+# define MAX_OPEN_STREAMS 16
 
 /* Notifications ADD_MESG. */
-#define DISPATCH_ADD_MSG(mbox,mhd,n)                                   \
+# define DISPATCH_ADD_MSG(mbox,mhd,n)                                  \
   do                                                                   \
     {                                                                  \
       int bailing = 0;                                                 \
@@ -42,7 +44,7 @@
       mu_monitor_wrlock (mbox->monitor);                               \
 } while (0);
 
-#define MU_AMD_SIZE_FILE_NAME ".mu-size"
+# define MU_AMD_SIZE_FILE_NAME ".mu-size"
 
 struct _amd_data;
 struct _amd_message
@@ -110,3 +112,5 @@ void amd_cleanup (void *arg);
 struct _amd_message *_amd_get_message (struct _amd_data *amd, size_t msgno);
 int amd_msg_lookup (struct _amd_data *amd, struct _amd_message *msg,
                    size_t *pret);
+
+#endif             
diff --git a/libproto/include/attribute0.h b/include/mailutils/sys/attribute.h
similarity index 91%
rename from libproto/include/attribute0.h
rename to include/mailutils/sys/attribute.h
index f379f7f..83e1f11 100644
--- a/libproto/include/attribute0.h
+++ b/include/mailutils/sys/attribute.h
@@ -17,12 +17,8 @@
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301 USA */
 
-#ifndef _ATTRIBUTE0_H
-# define _ATTRIBUTE0_H
-
-#ifdef DMALLOC
-#  include <dmalloc.h>
-#endif
+#ifndef _MAILUTILS_SYS_ATTRIBUTE_H
+# define _MAILUTILS_SYS_ATTRIBUTE_H
 
 #include <mailutils/attribute.h>
 
@@ -46,4 +42,4 @@ struct _mu_attribute
 }
 #endif
 
-#endif /* _ATTRIBUTE0_H */
+#endif /* _MAILUTILS_SYS_ATTRIBUTE_H */
diff --git a/libproto/include/auth0.h b/include/mailutils/sys/auth.h
similarity index 94%
rename from libproto/include/auth0.h
rename to include/mailutils/sys/auth.h
index 7868bad..0a4308d 100644
--- a/libproto/include/auth0.h
+++ b/include/mailutils/sys/auth.h
@@ -17,16 +17,12 @@
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301 USA */
 
-#ifndef _AUTH0_H
-#define _AUTH0_H
-
-#ifdef DMALLOC
-#  include <dmalloc.h>
-#endif
+#ifndef _MAILUTILS_SYS_AUTH_H
+# define _MAILUTILS_SYS_AUTH_H
 
+#include <sys/types.h>
 #include <mailutils/auth.h>
 #include <mailutils/list.h>
-#include <sys/types.h>
 
 #ifdef __cplusplus
 extern "C" {
@@ -67,4 +63,4 @@ struct _mu_wicket
 }
 #endif
 
-#endif /* _AUTH0_H */
+#endif /* _MAILUTILS_SYS_AUTH_H */
diff --git a/libproto/include/body0.h b/include/mailutils/sys/body.h
similarity index 92%
rename from libproto/include/body0.h
rename to include/mailutils/sys/body.h
index 156793c..9c82a2a 100644
--- a/libproto/include/body0.h
+++ b/include/mailutils/sys/body.h
@@ -17,12 +17,8 @@
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301 USA */
 
-#ifndef _BODY0_H
-#define _BODY0_H
-
-#ifdef DMALLOC
-#  include <dmalloc.h>
-#endif
+#ifndef _MAILUTILS_SYS_BODY_H
+# define _MAILUTILS_SYS_BODY_H
 
 #include <mailutils/stream.h>
 #include <mailutils/body.h>
@@ -50,4 +46,4 @@ struct _mu_body
 }
 #endif
 
-#endif /* _BODY0_H */
+#endif /* _MAILUTILS_SYS_BODY_H */
diff --git a/libproto/include/debug0.h b/include/mailutils/sys/debug.h
similarity index 92%
rename from libproto/include/debug0.h
rename to include/mailutils/sys/debug.h
index 1b4fc46..30ac959 100644
--- a/libproto/include/debug0.h
+++ b/include/mailutils/sys/debug.h
@@ -17,12 +17,8 @@
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301 USA */
 
-#ifndef _DEBUG0_H
-#define _DEBUG0_H
-
-#ifdef DMALLOC
-#  include <dmalloc.h>
-#endif
+#ifndef _MAILUTILS_SYS_DEBUG_H
+# define _MAILUTILS_SYS_DEBUG_H
 
 #include <mailutils/debug.h>
 #include <mailutils/stream.h>
@@ -47,4 +43,4 @@ struct _mu_debug
 }
 #endif
 
-#endif /* _DEBUG0_H */
+#endif /* _MAILUTILS_SYS_DEBUG_H */
diff --git a/libproto/include/envelope0.h b/include/mailutils/sys/envelope.h
similarity index 91%
rename from libproto/include/envelope0.h
rename to include/mailutils/sys/envelope.h
index 5e1cae4..fe4689a 100644
--- a/libproto/include/envelope0.h
+++ b/include/mailutils/sys/envelope.h
@@ -17,12 +17,8 @@
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301 USA */
 
-#ifndef _ENVELOPE0_H
-#define _ENVELOPE0_H
-
-#ifdef DMALLOC
-#include <dmalloc.h>
-#endif
+#ifndef _MAILUTILS_SYS_ENVELOPE_H
+# define _MAILUTILS_SYS_ENVELOPE_H
 
 #include <mailutils/envelope.h>
 
@@ -44,4 +40,4 @@ struct _mu_envelope
 }
 #endif
 
-#endif /* _ENVELOPE0_H */
+#endif /* _MAILUTILS_SYS_ENVELOPE_H */
diff --git a/libproto/include/folder0.h b/include/mailutils/sys/folder.h
similarity index 84%
rename from libproto/include/folder0.h
rename to include/mailutils/sys/folder.h
index 060e6a3..912fb87 100644
--- a/libproto/include/folder0.h
+++ b/include/mailutils/sys/folder.h
@@ -17,25 +17,21 @@
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301 USA */
 
-#ifndef _FOLDER0_H
-#define _FOLDER0_H
+#ifndef _MAILUTILS_SYS_FOLDER_H
+# define _MAILUTILS_SYS_FOLDER_H
 
-#ifdef DMALLOC
-#  include <dmalloc.h>
-#endif
+# include <sys/types.h>
+# include <stdio.h>
 
-#include <sys/types.h>
-#include <stdio.h>
+# include <mailutils/monitor.h>
+# include <mailutils/folder.h>
 
-#include <mailutils/monitor.h>
-#include <mailutils/folder.h>
-
-#ifdef __cplusplus
+# ifdef __cplusplus
 extern "C" {
-#endif
+# endif
 
-#define MU_FOLDER_LIST 0
-#define MU_FOLDER_ENUM 1
+# define MU_FOLDER_LIST 0
+# define MU_FOLDER_ENUM 1
   
 struct _mu_folder
 {
@@ -70,8 +66,8 @@ struct _mu_folder
   int  (*_unsubscribe) (mu_folder_t, const char *);
 };
 
-#ifdef __cplusplus
+# ifdef __cplusplus
 }
-#endif
+# endif
 
-#endif /* _FOLDER0_H */
+#endif /* _MAILUTILS_SYS_FOLDER_H */
diff --git a/libproto/include/imap0.h b/include/mailutils/sys/imap.h
similarity index 80%
rename from libproto/include/imap0.h
rename to include/mailutils/sys/imap.h
index 0b4ff33..bbad6e5 100644
--- a/libproto/include/imap0.h
+++ b/include/mailutils/sys/imap.h
@@ -17,65 +17,60 @@
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301 USA */
 
-#ifndef _IMAP0_H
-#define _IMAP0_H
+#ifndef _MAILUTILS_SYS_IMAP_H
+# define _MAILUTILS_SYS_IMAP_H
 
-#ifdef DMALLOC
-#  include <dmalloc.h>
-#endif
+# include <mailutils/sys/folder.h>
+# include <mailutils/sys/mailbox.h>
+# include <mailutils/sys/registrar.h>
+# include <mailutils/sys/auth.h>
 
-#include <folder0.h>
-#include <mailbox0.h>
-#include <registrar0.h>
-#include <auth0.h>
-
-#ifdef __cplusplus
+# ifdef __cplusplus
 extern "C" {
-#endif
+# endif
 
-#define CLEAR_STATE(f_imap) \
+# define CLEAR_STATE(f_imap)                           \
  f_imap->selected = NULL, f_imap->state = IMAP_NO_STATE
 
 /* Clear the state and close the stream.  */
-#define CHECK_ERROR_CLOSE(folder, f_imap, status) \
-do \
-  { \
-     if (status != 0) \
-       { \
-          mu_stream_close (folder->stream); \
-          CLEAR_STATE (f_imap); \
-          return status; \
-       } \
-  } \
-while (0)
+# define CHECK_ERROR_CLOSE(folder, f_imap, status)     \
+  do                                                   \
+    {                                                  \
+      if (status != 0)                                 \
+       {                                               \
+          mu_stream_close (folder->stream);            \
+          CLEAR_STATE (f_imap);                                \
+          return status;                               \
+       }                                               \
+    }                                                  \
+  while (0)
 
 /* Clear the state.  */
-#define CHECK_ERROR(f_imap, status) \
-do \
-  { \
-     if (status != 0) \
-       { \
-          CLEAR_STATE (f_imap); \
-          return status; \
-       } \
-  } \
+# define CHECK_ERROR(f_imap, status)           \
+  do                                           \
+    {                                          \
+      if (status != 0)                         \
+       {                                       \
+          CLEAR_STATE (f_imap);                        \
+          return status;                       \
+       }                                       \
+    }                                          \
 while (0)
 
 /* Clear the state for non recoverable error.  */
-#define CHECK_EAGAIN(f_imap, status) \
-do \
-  { \
-    if (status != 0) \
-      { \
-         if (status != EAGAIN && status != EINPROGRESS && status != EINTR) \
-           { \
-             CLEAR_STATE (f_imap); \
-           } \
-         return status; \
-      } \
-   }  \
-while (0)
-
+# define CHECK_EAGAIN(f_imap, status)          \
+  do                                           \
+    {                                          \
+      if (status != 0)                         \
+       {                                                               \
+         if (status != EAGAIN && status != EINPROGRESS && status != EINTR) \
+           {                                                           \
+             CLEAR_STATE (f_imap);                                     \
+           }                                                           \
+         return status;                                                \
+       }                                                               \
+    }                                                                  \
+  while (0)
 
 struct _f_imap;
 struct _m_imap;
@@ -234,8 +229,8 @@ int imap_parse        (f_imap_t);
 int imap_readline     (f_imap_t);
 char *section_name    (msg_imap_t);
 
-#ifdef __cplusplus
+# ifdef __cplusplus
 }
-#endif
+# endif
 
-#endif /* _IMAP0_H */
+#endif /* _MAILUTILS_SYS_IMAP_H */
diff --git a/libproto/include/iterator0.h b/include/mailutils/sys/iterator.h
similarity index 88%
rename from libproto/include/iterator0.h
rename to include/mailutils/sys/iterator.h
index 8abbb80..5b5475f 100644
--- a/libproto/include/iterator0.h
+++ b/include/mailutils/sys/iterator.h
@@ -17,18 +17,14 @@
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301 USA */
 
-#ifndef _ITERATOR0_H
-#define _ITERATOR0_H
+#ifndef _MAILUTILS_SYS_ITERATOR_H
+# define _MAILUTILS_SYS_ITERATOR_H
 
-#ifdef DMALLOC
-#  include <dmalloc.h>
-#endif
+# include <mailutils/iterator.h>
 
-#include <mailutils/iterator.h>
-
-#ifdef __cplusplus
+# ifdef __cplusplus
 extern "C" {
-#endif
+# endif
 
 struct _mu_iterator
 {
@@ -46,8 +42,8 @@ struct _mu_iterator
   int (*itrctl) (void *owner, enum mu_itrctl_req req, void *arg);
 };
 
-#ifdef __cplusplus
+# ifdef __cplusplus
 }
-#endif
+# endif
 
-#endif /* _ITERATOR0_H */
+#endif /* _MAILUTILS_SYS_ITERATOR_H */
diff --git a/libproto/include/list0.h b/include/mailutils/sys/list.h
similarity index 84%
rename from libproto/include/list0.h
rename to include/mailutils/sys/list.h
index 3ac00fb..2e9f83a 100644
--- a/libproto/include/list0.h
+++ b/include/mailutils/sys/list.h
@@ -17,22 +17,18 @@
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301 USA */
 
-#ifndef _LIST0_H
-#define _LIST0_H
+#ifndef _MAILUTILS_SYS_LIST_H
+# define _MAILUTILS_SYS_LIST_H
 
-#ifdef DMALLOC
-#  include <dmalloc.h>
-#endif
+# include <sys/types.h>
 
-#include <sys/types.h>
+# include <mailutils/list.h>
+# include <mailutils/monitor.h>
+# include <mailutils/iterator.h>
 
-#include <mailutils/list.h>
-#include <mailutils/monitor.h>
-#include <mailutils/iterator.h>
-
-#ifdef __cplusplus
+# ifdef __cplusplus
 extern "C" {
-#endif
+# endif
 
 struct list_data
 {
@@ -59,8 +55,8 @@ extern void _mu_list_insert_sublist (mu_list_t list,
                                     size_t count,
                                     int insert_before);
 
-#ifdef __cplusplus
+# ifdef __cplusplus
 }
-#endif
+# endif
 
-#endif /* _LIST0_H */
+#endif /* _MAILUTILS_SYS_LIST_H */
diff --git a/libproto/include/mailbox0.h b/include/mailutils/sys/mailbox.h
similarity index 88%
rename from libproto/include/mailbox0.h
rename to include/mailutils/sys/mailbox.h
index a052e43..12af8fe 100644
--- a/libproto/include/mailbox0.h
+++ b/include/mailutils/sys/mailbox.h
@@ -17,23 +17,19 @@
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301 USA */
 
-#ifndef _MAILBOX0_H
-#define _MAILBOX0_H
+#ifndef _MAILUTILS_SYS_MAILBOX_H
+# define _MAILUTILS_SYS_MAILBOX_H
 
-#ifdef DMALLOC
-#  include <dmalloc.h>
-#endif
+# include <sys/types.h>
+# include <stdio.h>
 
-#include <sys/types.h>
-#include <stdio.h>
+# include <mailutils/monitor.h>
+# include <mailutils/mailbox.h>
+# include <mailutils/iterator.h>
 
-#include <mailutils/monitor.h>
-#include <mailutils/mailbox.h>
-#include <mailutils/iterator.h>
-
-#ifdef __cplusplus
+# ifdef __cplusplus
 extern "C" {
-#endif
+# endif
 
 struct _mu_mailbox
 {
@@ -80,8 +76,8 @@ struct _mu_mailbox
   int  (*_get_uidls) (mu_mailbox_t, mu_list_t);
 };
 
-#ifdef __cplusplus
+# ifdef __cplusplus
 }
-#endif
+# endif
 
-#endif /* _MAILBOX0_H */
+#endif /* _MAILUTILS_SYS_MAILBOX_H */
diff --git a/libproto/include/mailer0.h b/include/mailutils/sys/mailer.h
similarity index 81%
rename from libproto/include/mailer0.h
rename to include/mailutils/sys/mailer.h
index 949bb15..b9413a3 100644
--- a/libproto/include/mailer0.h
+++ b/include/mailutils/sys/mailer.h
@@ -17,26 +17,22 @@
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301 USA */
 
-#ifndef _MAILER0_H
-#define _MAILER0_H
+#ifndef _MAILUTILS_SYS_MAILER_H
+# define _MAILUTILS_SYS_MAILER_H
 
-#ifdef DMALLOC
-#  include <dmalloc.h>
-#endif
+# include <sys/types.h>
+# include <mailutils/mailer.h>
+# include <mailutils/monitor.h>
 
-#include <sys/types.h>
-#include <mailutils/mailer.h>
-#include <mailutils/monitor.h>
-
-#ifdef __cplusplus
+# ifdef __cplusplus
 extern "C" {
-#endif
+# endif
 
 /* Default mailer URL. */
 
-#define MAILER_URL_DEFAULT "sendmail:"
+# define MAILER_URL_DEFAULT "sendmail:"
 
-#define MAILER_LINE_BUF_SIZE   1000
+# define MAILER_LINE_BUF_SIZE  1000
 
 struct _mu_mailer
 {
@@ -61,11 +57,11 @@ struct _mu_mailer
 int _mu_mailer_mailbox_init (mu_mailbox_t mailbox);
 int _mu_mailer_folder_init (mu_folder_t folder MU_ARG_UNUSED);
   
-#define MAILER_NOTIFY(mailer, type) \
+# define MAILER_NOTIFY(mailer, type) \
   if (mailer->observer) observer_notify (mailer->observer, type)
 
-#ifdef __cplusplus
+# ifdef __cplusplus
 }
-#endif
+# endif
 
-#endif /* MAILER0_H */
+#endif /* _MAILUTILS_SYS_MAILER_H */
diff --git a/libproto/include/monitor0.h b/include/mailutils/sys/monitor.h
similarity index 79%
rename from libproto/include/monitor0.h
rename to include/mailutils/sys/monitor.h
index 3541e15..2d76265 100644
--- a/libproto/include/monitor0.h
+++ b/include/mailutils/sys/monitor.h
@@ -16,26 +16,22 @@
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301 USA */
 
-#ifndef _MONITOR0_H
-#define _MONITOR0_H
+#ifndef _MAILUTILS_SYS_MONITOR_H
+# define _MAILUTILS_SYS_MONITOR_H
 
-#ifdef DMALLOC
-#  include <dmalloc.h>
-#endif
+# ifdef HAVE_CONFIG_H
+#  include <config.h>
+# endif
 
-#ifdef HAVE_CONFIG_H
-# include <config.h>
-#endif
+# include <sys/types.h>
+# include <mailutils/monitor.h>
 
-#include <sys/types.h>
-#include <mailutils/monitor.h>
-
-#ifdef __cplusplus
+# ifdef __cplusplus
 extern "C" {
-#endif
+# endif
 /* FIXME: any protos? */
-#ifdef __cplusplus
+# ifdef __cplusplus
 }
-#endif
+# endif
 
 #endif /* _MONITOR0_H */
diff --git a/libproto/include/observer0.h b/include/mailutils/sys/observer.h
similarity index 86%
rename from libproto/include/observer0.h
rename to include/mailutils/sys/observer.h
index 164ac26..7aede1c 100644
--- a/libproto/include/observer0.h
+++ b/include/mailutils/sys/observer.h
@@ -17,18 +17,14 @@
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301 USA */
 
-#ifndef _OBSERVER0_H
-#define _OBSERVER0_H
+#ifndef _MAILUTILS_SYS_OBSERVER_H
+# define _MAILUTILS_SYS_OBSERVER_H
 
-#ifdef DMALLOC
-#  include <dmalloc.h>
-#endif
+# include <mailutils/observer.h>
 
-#include <mailutils/observer.h>
-
-#ifdef __cplusplus
+# ifdef __cplusplus
 extern "C" {
-#endif
+# endif
 
 struct _mu_observer
 {
@@ -53,10 +49,8 @@ struct _event
 
 typedef struct _event *event_t;
 
-
-
-#ifdef __cplusplus
+# ifdef __cplusplus
 }
-#endif
+# endif
 
-#endif /* _OBSERVER0_H */
+#endif /* _MAILUTILS_SYS_OBSERVER_H */
diff --git a/include/mailutils/sys/pop3.h b/include/mailutils/sys/pop3.h
index 35b1165..1c81579 100644
--- a/include/mailutils/sys/pop3.h
+++ b/include/mailutils/sys/pop3.h
@@ -37,115 +37,111 @@ enum mu_pop3_state
   {
     MU_POP3_NO_STATE,
     MU_POP3_CONNECT, MU_POP3_GREETINGS,
-    MU_POP3_APOP,    MU_POP3_APOP_ACK,
-    MU_POP3_AUTH,    MU_POP3_AUTH_ACK,
-    MU_POP3_CAPA,    MU_POP3_CAPA_ACK, MU_POP3_CAPA_RX,
-    MU_POP3_DELE,    MU_POP3_DELE_ACK,
-    MU_POP3_LIST,    MU_POP3_LIST_ACK, MU_POP3_LIST_RX,
-    MU_POP3_NOOP,    MU_POP3_NOOP_ACK,
-    MU_POP3_PASS,    MU_POP3_PASS_ACK,
-    MU_POP3_QUIT,    MU_POP3_QUIT_ACK,
-    MU_POP3_RETR,    MU_POP3_RETR_ACK, MU_POP3_RETR_RX,
-    MU_POP3_RSET,    MU_POP3_RSET_ACK,
-    MU_POP3_STAT,    MU_POP3_STAT_ACK,
-    MU_POP3_STLS,    MU_POP3_STLS_ACK, MU_POP3_STLS_CONNECT,
-    MU_POP3_TOP,     MU_POP3_TOP_ACK,  MU_POP3_TOP_RX,
-    MU_POP3_UIDL,    MU_POP3_UIDL_ACK, MU_POP3_UIDL_RX,
-    MU_POP3_USER,    MU_POP3_USER_ACK,
-    MU_POP3_DONE,    MU_POP3_UNKNOWN,  MU_POP3_ERROR
-  };
-
-/* Structure holding the data necessary to do proper buffering.  */
-struct mu_pop3_work_buf
-  {
-    char *buf;
-    char *ptr;
-    char *nl;
-    size_t len;
+    MU_POP3_APOP,
+    MU_POP3_AUTH,
+    MU_POP3_CAPA, MU_POP3_CAPA_RX,
+    MU_POP3_DELE,
+    MU_POP3_LIST, MU_POP3_LIST_RX,
+    MU_POP3_NOOP,
+    MU_POP3_PASS,
+    MU_POP3_QUIT,
+    MU_POP3_RETR, MU_POP3_RETR_RX,
+    MU_POP3_RSET,
+    MU_POP3_STAT,
+    MU_POP3_STLS, MU_POP3_STLS_CONNECT,
+    MU_POP3_TOP,  MU_POP3_TOP_RX,
+    MU_POP3_UIDL, MU_POP3_UIDL_RX,
+    MU_POP3_USER,
+    MU_POP3_DONE,
+    MU_POP3_UNKNOWN,
+    MU_POP3_ERROR
   };
 
+#define MU_POP3_ACK   0x01
+#define MU_POP3_TRACE 0x02  
+  
 /* Structure to hold things general to POP3 mailbox, like its state, etc ... */
 struct _mu_pop3
   {
-    /* Working I/O buffer.
-       io.buf: Working io buffer
-       io.ptr: Points to the end of the buffer, the non consumed chars
-       io.nl: Points to the '\n' char in the string
-       io.len: Len of io_buf.  */
-    struct mu_pop3_work_buf io;
-
-    /* Holds the first line response of the last command, i.e the ACK:
-       ack.buf: Buffer for the ack
-       ack.ptr: Working pointer, indicate the start of the non consumed chars
-       ack.len: Size 512 according to RFC2449.  */
-    struct mu_pop3_work_buf ack;
-    int acknowledge;
-
-    char *timestamp; /* For apop, if supported.  */
-    unsigned timeout;  /* Default is 10 minutes.  */
-
-    mu_debug_t debug; /* debugging trace.  */
-
-    enum mu_pop3_state state;  /* Indicate the state of the running command.  
*/
-
-    mu_stream_t carrier; /* TCP Connection.  */
+    int flags;
+    
+    /* Holds the first line response of the last command, i.e the ACK  */
+    char *ackbuf;
+    size_t acksize;
+
+    char *rdbuf;
+    size_t rdsize;
+    
+    char *timestamp;    /* For apop, if supported.  */
+    unsigned timeout;   /* Default is 10 minutes.  */
+
+    enum mu_pop3_state state;  /* Indicate the state of the running
+                                 command.  */
+    mu_list_t capa;            /* Capabilities. */ 
+    mu_stream_t carrier;       /* TCP Connection. */
   };
 
-extern int  mu_pop3_debug_cmd       (mu_pop3_t);
-extern int  mu_pop3_debug_ack       (mu_pop3_t);
+#define MU_POP3_FSET(p,f) ((p)->flags |= (f))
+#define MU_POP3_FISSET(p,f) ((p)->flags & (f))  
+#define MU_POP3_FCLR(p,f) ((p)->flags &= ~(f))
+  
 extern int  mu_pop3_iterator_create (mu_pop3_t pop3, mu_iterator_t *piterator);
 extern int  mu_pop3_stream_create (mu_pop3_t pop3, mu_stream_t *pstream);
-extern int  mu_pop3_carrier_is_ready (mu_stream_t carrier, int flag, int 
timeout);
+extern int  mu_pop3_carrier_is_ready (mu_stream_t carrier, int flag,
+                                     int timeout);
+
+int _mu_pop3_trace_enable (mu_pop3_t pop3);
+int _mu_pop3_trace_disable (mu_pop3_t pop3);  
 
+int _mu_pop3_init (mu_pop3_t pop3);
+  
 /* Check for non recoverable error.
    The error is consider not recoverable if not part of the signal set:
    EAGAIN, EINPROGRESS, EINTR.
    For unrecoverable error we reset, by moving the working ptr
    to the begining of the buffer and setting the state to error.
  */
-#define MU_POP3_CHECK_EAGAIN(pop3, status) \
-do \
-  { \
-    if (status != 0) \
-      { \
-         if (status != EAGAIN && status != EINPROGRESS && status != EINTR) \
-           { \
-             pop3->io.ptr = pop3->io.buf; \
-             pop3->state = MU_POP3_ERROR; \
-           } \
-         return status; \
-      } \
-   }  \
-while (0)
+#define MU_POP3_CHECK_EAGAIN(pop3, status)     \
+  do                                           \
+    {                                          \
+      if (status != 0)                         \
+       {                                                               \
+         if (status != EAGAIN && status != EINPROGRESS && status != EINTR) \
+           {                                                           \
+             pop3->state = MU_POP3_ERROR;                              \
+           }                                                           \
+         return status;                                                \
+       }                                                               \
+    }                                                                  \
+  while (0)
 
 /* If error return.
    Check status an reset(see MU_POP2_CHECK_EAGAIN) the buffer.
   */
-#define MU_POP3_CHECK_ERROR(pop3, status) \
-do \
-  { \
-     if (status != 0) \
-       { \
-          pop3->io.ptr = pop3->io.buf; \
-          pop3->state = MU_POP3_ERROR; \
-          return status; \
-       } \
-  } \
-while (0)
+#define MU_POP3_CHECK_ERROR(pop3, status)      \
+  do                                           \
+    {                                          \
+      if (status != 0)                         \
+       {                                       \
+          pop3->state = MU_POP3_ERROR;         \
+          return status;                       \
+       }                                       \
+    }                                          \
+  while (0)
 
 /* Check if we got "+OK".
    In POP3 protocol and ack of "+OK" means the command was successfull.
  */
-#define MU_POP3_CHECK_OK(pop3) \
-do \
-  { \
-     if (mu_c_strncasecmp (pop3->ack.buf, "+OK", 3) != 0) \
-       { \
-          pop3->state = MU_POP3_NO_STATE; \
-          return EACCES; \
-       } \
-  } \
-while (0)
+#define MU_POP3_CHECK_OK(pop3)                                 \
+  do                                                           \
+    {                                                          \
+      if (mu_c_strncasecmp (pop3->ackbuf, "+OK", 3) != 0)      \
+       {                                                       \
+          pop3->state = MU_POP3_NO_STATE;                      \
+          return EACCES;                                       \
+       }                                                       \
+    }                                                          \
+  while (0)
 
 #ifdef __cplusplus
 }
diff --git a/libproto/include/property0.h b/include/mailutils/sys/property.h
similarity index 76%
rename from libproto/include/property0.h
rename to include/mailutils/sys/property.h
index 25d1d2f..4faac9c 100644
--- a/libproto/include/property0.h
+++ b/include/mailutils/sys/property.h
@@ -17,24 +17,20 @@
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301 USA */
 
-#ifndef _PROPERTY0_H
-#define _PROPERTY0_H
+#ifndef _MAILUTILS_SYS_PROPERTY_H
+# define _MAILUTILS_SYS_PROPERTY_H
 
-#ifdef DMALLOC
-#  include <dmalloc.h>
-#endif
+# include <errno.h>
+# include <stdlib.h>
+# include <string.h>
 
-#include <errno.h>
-#include <stdlib.h>
-#include <string.h>
+# include <mailutils/property.h>
+# include <mailutils/monitor.h>
+# include <mailutils/assoc.h>
 
-#include <mailutils/property.h>
-#include <mailutils/monitor.h>
-#include <mailutils/assoc.h>
-
-#ifdef __cplusplus
+# ifdef __cplusplus
 extern "C" {
-#endif
+# endif
 
 struct property_item
 {
@@ -48,8 +44,8 @@ struct _mu_property
   mu_monitor_t lock;
 };
 
-#ifdef __cplusplus
+# ifdef __cplusplus
 }
-#endif
+# endif
 
-#endif /* _PROPERTY0_H */
+#endif /* _MAILUTILS_SYS_PROPERTY_H */
diff --git a/libproto/include/registrar0.h b/include/mailutils/sys/registrar.h
similarity index 57%
rename from libproto/include/registrar0.h
rename to include/mailutils/sys/registrar.h
index 13e15f6..8b3d83d 100644
--- a/libproto/include/registrar0.h
+++ b/include/mailutils/sys/registrar.h
@@ -17,14 +17,14 @@
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301 USA */
 
-#ifndef _REGISTRAR0_H
-#define _REGISTRAR0_H
+#ifndef _MAILUTILS_SYS_REGISTRAR_H
+# define _MAILUTILS_SYS_REGISTRAR_H
 
-#include <mailutils/registrar.h>
+# include <mailutils/registrar.h>
 
-#ifdef __cplusplus
+# ifdef __cplusplus
 extern "C" {
-#endif
+# endif
 
 /* The pop and imap defines are all wrong now, since they seem intertwined
    with the old url parsing code. Also, "pop://" is not the POP scheme,
@@ -32,67 +32,67 @@ extern "C" {
 
    Perhaps they can be changed?
 */
-#define MU_POP_PORT 110
-#define MU_POP_SCHEME "pop"
-#define MU_POP_SCHEME_LEN (sizeof (MU_POP_SCHEME) - 1)
+# define MU_POP_PORT 110
+# define MU_POP_SCHEME "pop"
+# define MU_POP_SCHEME_LEN (sizeof (MU_POP_SCHEME) - 1)
 extern int _url_pop_init          (mu_url_t);
 extern int _mailbox_pop_init      (mu_mailbox_t);
 extern int _folder_pop_init       (mu_folder_t);
 
-#define MU_POPS_PORT 995
-#define MU_POPS_SCHEME "pops"
-#define MU_POPS_SCHEME_LEN (sizeof (MU_POPS_SCHEME) - 1)
+# define MU_POPS_PORT 995
+# define MU_POPS_SCHEME "pops"
+# define MU_POPS_SCHEME_LEN (sizeof (MU_POPS_SCHEME) - 1)
 extern int _url_pops_init         (mu_url_t);
 extern int _mailbox_pops_init     (mu_mailbox_t);
 
-#define MU_IMAP_PORT 143
-#define MU_IMAP_SCHEME "imap"
-#define MU_IMAP_SCHEME_LEN (sizeof (MU_IMAP_SCHEME) - 1)
+# define MU_IMAP_PORT 143
+# define MU_IMAP_SCHEME "imap"
+# define MU_IMAP_SCHEME_LEN (sizeof (MU_IMAP_SCHEME) - 1)
 extern int _url_imap_init         (mu_url_t);
 extern int _mailbox_imap_init     (mu_mailbox_t);
 extern int _folder_imap_init      (mu_folder_t);
 
-#define MU_IMAPS_PORT 993
-#define MU_IMAPS_SCHEME "imaps"
-#define MU_IMAPS_SCHEME_LEN (sizeof (MU_IMAPS_SCHEME) - 1)
+# define MU_IMAPS_PORT 993
+# define MU_IMAPS_SCHEME "imaps"
+# define MU_IMAPS_SCHEME_LEN (sizeof (MU_IMAPS_SCHEME) - 1)
 extern int _url_imaps_init        (mu_url_t);
 extern int _mailbox_imaps_init    (mu_mailbox_t);
 
-#define MU_MBOX_SCHEME "mbox"
-#define MU_MBOX_SCHEME_LEN (sizeof (MU_MBOX_SCHEME) - 1)
+# define MU_MBOX_SCHEME "mbox"
+# define MU_MBOX_SCHEME_LEN (sizeof (MU_MBOX_SCHEME) - 1)
 extern int _mailbox_mbox_init     (mu_mailbox_t);
 extern int _folder_mbox_init      (mu_folder_t);
 
-#define MU_FILE_SCHEME "file"
-#define MU_FILE_SCHEME_LEN (sizeof (MU_FILE_SCHEME) - 1)
+# define MU_FILE_SCHEME "file"
+# define MU_FILE_SCHEME_LEN (sizeof (MU_FILE_SCHEME) - 1)
 
-#define MU_PATH_SCHEME "/"
-#define MU_PATH_SCHEME_LEN (sizeof (MU_PATH_SCHEME) - 1)
+# define MU_PATH_SCHEME "/"
+# define MU_PATH_SCHEME_LEN (sizeof (MU_PATH_SCHEME) - 1)
 extern int _mailbox_path_init     (mu_mailbox_t);
 extern int _folder_path_init      (mu_folder_t);
 
-#define MU_SMTP_SCHEME "smtp"
-#define MU_SMTP_SCHEME_LEN (sizeof (MU_SMTP_SCHEME) - 1)
-#define MU_SMTP_PORT 25
+# define MU_SMTP_SCHEME "smtp"
+# define MU_SMTP_SCHEME_LEN (sizeof (MU_SMTP_SCHEME) - 1)
+# define MU_SMTP_PORT 25
 
-#define MU_SENDMAIL_SCHEME "sendmail"
-#define MU_SENDMAIL_SCHEME_LEN (sizeof (MU_SENDMAIL_SCHEME) - 1)
+# define MU_SENDMAIL_SCHEME "sendmail"
+# define MU_SENDMAIL_SCHEME_LEN (sizeof (MU_SENDMAIL_SCHEME) - 1)
 extern int _mu_mailer_sendmail_init (mu_mailer_t mailer);
   
-#define MU_PROG_SCHEME "prog"
-#define MU_PROG_SCHEME_LEN (sizeof (MU_PROG_SCHEME) - 1)
-  extern int _mu_mailer_prog_init  (mu_mailer_t);  
+# define MU_PROG_SCHEME "prog"
+# define MU_PROG_SCHEME_LEN (sizeof (MU_PROG_SCHEME) - 1)
+extern int _mu_mailer_prog_init  (mu_mailer_t);  
   
-#define MU_MH_SCHEME "mh"
-#define MU_MH_SCHEME_LEN (sizeof (MU_MH_SCHEME) - 1)
+# define MU_MH_SCHEME "mh"
+# define MU_MH_SCHEME_LEN (sizeof (MU_MH_SCHEME) - 1)
 extern int _mailbox_mh_init (mu_mailbox_t mailbox);
 
-#define MU_MAILDIR_SCHEME "maildir"
-#define MU_MAILDIR_SCHEME_LEN (sizeof (MU_MAILDIR_SCHEME) - 1)
+# define MU_MAILDIR_SCHEME "maildir"
+# define MU_MAILDIR_SCHEME_LEN (sizeof (MU_MAILDIR_SCHEME) - 1)
 extern int _mailbox_maildir_init (mu_mailbox_t mailbox);
 
-#ifdef __cplusplus
+# ifdef __cplusplus
 }
-#endif
+# endif
 
-#endif /* _REGISTRAR0_H */
+#endif /* _MAILUTILS_SYS_REGISTRAR_H */
diff --git a/libproto/include/url0.h b/include/mailutils/sys/url.h
similarity index 90%
rename from libproto/include/url0.h
rename to include/mailutils/sys/url.h
index 1cedd31..8e300ba 100644
--- a/libproto/include/url0.h
+++ b/include/mailutils/sys/url.h
@@ -17,18 +17,14 @@
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301 USA */
 
-#ifndef _URL0_H
-#define _URL0_H        1
+#ifndef _MAILUTILS_SYS_URL_H
+# define _MAILUTILS_SYS_URL_H
 
-#ifdef DMALLOC
-#  include <dmalloc.h>
-#endif
+# include <mailutils/url.h>
 
-#include <mailutils/url.h>
-
-#ifdef __cplusplus
+# ifdef __cplusplus
 extern "C" {
-#endif
+# endif
 
 struct _mu_url
 {
@@ -63,9 +59,8 @@ struct _mu_url
   int   (*_uplevel)    (const mu_url_t, mu_url_t *);
 };
 
-
-#ifdef __cplusplus
+# ifdef __cplusplus
 }
-#endif
+# endif
 
-#endif /* URL_H */
+#endif /* _MAILUTILS_SYS_URL_H */
diff --git a/include/mailutils/tls.h b/include/mailutils/tls.h
index 856e474..41a5234 100644
--- a/include/mailutils/tls.h
+++ b/include/mailutils/tls.h
@@ -47,10 +47,13 @@ extern int mu_check_tls_environment (void);
 extern int mu_init_tls_libs (void);
 extern void mu_deinit_tls_libs (void);
 
-typedef int (*mu_tls_readline_fn) (void *iodata);
-typedef int (*mu_tls_writeline_fn) (void *iodata, char *buf); 
-typedef void (*mu_tls_stream_ctl_fn) (void *iodata, mu_stream_t *pold,
-                                     mu_stream_t __new);
+typedef int (*mu_tls_readline_fn) (void *iodata, int n);
+typedef int (*mu_tls_writeline_fn) (void *iodata, char *buf);
+
+#define MU_TLS_SESS_GET_STREAMS 0
+#define MU_TLS_SESS_SET_STREAMS 1  
+typedef int (*mu_tls_stream_ctl_fn) (void *iodata, int __op,
+                                    mu_stream_t *pstr);
 
 extern int mu_tls_begin (void *iodata, mu_tls_readline_fn reader,
                         mu_tls_writeline_fn writer,
diff --git a/libmu_auth/tls.c b/libmu_auth/tls.c
index 4ad23be..3b89bac 100644
--- a/libmu_auth/tls.c
+++ b/libmu_auth/tls.c
@@ -115,11 +115,23 @@ mu_check_tls_environment (void)
 
 int mu_tls_enable = 0;
 
+#if 0
+void
+_mu_gtls_logger(int level, const char *text)
+{
+  mu_diag_output (MU_DIAG_DEBUG, "GnuTLS(%d): %s", level, text);
+}
+#endif
+
 int
 mu_init_tls_libs (void)
 {
   if (mu_tls_module_config.enable && !mu_tls_enable)
     mu_tls_enable = !gnutls_global_init (); /* Returns 1 on success */
+#if 0
+  gnutls_global_set_log_function (_mu_gtls_logger);
+  gnutls_global_set_log_level (110);
+#endif
   return mu_tls_enable;
 }
 
@@ -161,7 +173,7 @@ mu_tls_begin (void *iodata,
 {
   int i = 0;
   int status;
-  mu_stream_t oldstr, newstr;
+  mu_stream_t streams[2], newstr;
   
   if (keywords == NULL)
     return EINVAL;
@@ -181,19 +193,23 @@ mu_tls_begin (void *iodata,
              return status;
            }
           
-          status = reader (iodata);
+          status = reader (iodata, i);
           if (status != 0)
            {
              mu_error ("mu_tls_begin: reader (0): %s", mu_strerror (status));
              return status;
            }
 
-          stream_ctl (iodata, &oldstr, NULL);
-          status = mu_tls_client_stream_create (&newstr, oldstr, oldstr, 0);
+          status = stream_ctl (iodata, MU_TLS_SESS_GET_STREAMS, streams);
+         if (status)
+           return status;
+          status = mu_tls_client_stream_create (&newstr,
+                                               streams[0], streams[1], 0);
           if (status != 0)
            {
              mu_error ("mu_tls_begin: mu_tls_client_stream_create(0): %s",
                        mu_strerror (status));
+             stream_ctl (iodata, MU_TLS_SESS_SET_STREAMS, streams);
              return status;
            }
 
@@ -202,10 +218,13 @@ mu_tls_begin (void *iodata,
            {
              mu_error ("mu_tls_begin: mu_stream_open (0): %s",
                        mu_strerror (status));
+             stream_ctl (iodata, MU_TLS_SESS_SET_STREAMS, streams);
              return status;
            }
 
-          stream_ctl (iodata, NULL, newstr);
+         streams[0] = streams[1] = newstr;
+         stream_ctl (iodata, MU_TLS_SESS_SET_STREAMS, streams);
+         /* FIXME: Unref newstr */
           break;
 
         case 1:
@@ -219,7 +238,7 @@ mu_tls_begin (void *iodata,
              return status;
            }
 
-          status = reader (iodata);
+          status = reader (iodata, i);
           if (status != 0)
            {
              mu_error ("mu_tls_begin: reader (1): %s", mu_strerror (status));
@@ -402,7 +421,7 @@ _tls_stream_pull (gnutls_transport_ptr fd, void *buf, 
size_t size)
        
   while ((rc = mu_stream_read (stream, buf, size, &rdbytes)) == EAGAIN)
     ;
-
+  
   if (rc)
     return -1;
   return rdbytes;
@@ -420,6 +439,7 @@ _tls_stream_push (gnutls_transport_ptr fd, const void *buf, 
size_t size)
       mu_error ("_tls_stream_push: %s", mu_strerror (rc)); /* FIXME */
       return -1;
     }
+
   mu_stream_flush (stream);
   return size;
 }
@@ -475,9 +495,11 @@ _tls_server_open (mu_stream_t stream)
 }
 
 static int
-prepare_client_session (struct _mu_tls_stream *sp)
+prepare_client_session (mu_stream_t stream)
 {
+  struct _mu_tls_stream *sp = (struct _mu_tls_stream *) stream;
   int rc;
+  mu_transport_t transport[2];
   static int protocol_priority[] = {GNUTLS_TLS1, GNUTLS_SSL3, 0};
   static int kx_priority[] = {GNUTLS_KX_RSA, 0};
   static int cipher_priority[] = {GNUTLS_CIPHER_3DES_CBC,
@@ -508,9 +530,10 @@ prepare_client_session (struct _mu_tls_stream *sp)
 
   gnutls_credentials_set (sp->session, GNUTLS_CRD_CERTIFICATE, x509_cred);
 
+  mu_stream_ioctl (stream, MU_IOCTL_GET_TRANSPORT, transport);
   gnutls_transport_set_ptr2 (sp->session,
-                            (gnutls_transport_ptr) sp->transport[0],
-                            (gnutls_transport_ptr) sp->transport[1]);
+                            (gnutls_transport_ptr) transport[0],
+                            (gnutls_transport_ptr) transport[1]);
   gnutls_transport_set_pull_function (sp->session, _tls_stream_pull);
   gnutls_transport_set_push_function (sp->session, _tls_stream_push);
       
@@ -532,7 +555,7 @@ _tls_client_open (mu_stream_t stream)
       /* FALLTHROUGH */
       
     case state_init:
-      prepare_client_session (sp);
+      prepare_client_session (stream);
       rc = gnutls_handshake (sp->session);
       if (rc < 0)
        {
diff --git a/libproto/Makefile.am b/libproto/Makefile.am
index 30a423d..3ba7d19 100644
--- a/libproto/Makefile.am
+++ b/libproto/Makefile.am
@@ -37,5 +37,5 @@ if MU_COND_SUPPORT_MAILDIR
   MAILDIR_DIR = maildir
 endif
 
-SUBDIRS = include mbox $(POP_DIR) $(NNTP_DIR) $(IMAP_DIR) $(MH_DIR) 
$(MAILDIR_DIR) mailer
+SUBDIRS = mbox $(POP_DIR) $(NNTP_DIR) $(IMAP_DIR) $(MH_DIR) $(MAILDIR_DIR) 
mailer
 
diff --git a/libproto/imap/Makefile.am b/libproto/imap/Makefile.am
index 1acdfd4..dd37326 100644
--- a/libproto/imap/Makefile.am
+++ b/libproto/imap/Makefile.am
@@ -18,7 +18,7 @@
 ##   Foundation, Inc.  51 Franklin Street, Fifth Floor, Boston, MA
 ##   02110-1301 USA
 
-INCLUDES = @MU_LIB_COMMON_INCLUDES@ -I${top_srcdir}/libproto/include 
+INCLUDES = @MU_LIB_COMMON_INCLUDES@ 
 
 lib_LTLIBRARIES = libmu_imap.la
 libmu_imap_la_LDFLAGS=-version-info @VI_CURRENT@:@VI_REVISION@:@VI_AGE@
diff --git a/libproto/imap/folder.c b/libproto/imap/folder.c
index e974ced..4f27c49 100644
--- a/libproto/imap/folder.c
+++ b/libproto/imap/folder.c
@@ -34,8 +34,8 @@
 # include <strings.h>
 #endif
 
-#include <imap0.h>
-#include <url0.h>
+#include <mailutils/sys/imap.h>
+#include <mailutils/sys/url.h>
 
 #include <mailutils/auth.h>
 #include <mailutils/attribute.h>
@@ -2612,7 +2612,7 @@ imap_parse (f_imap_t f_imap)
 
 #else
 #include <stdio.h>
-#include <registrar0.h>
+#include <mailutils/sys/registrar.h>
 mu_record_t mu_imap_record = NULL;
 mu_record_t mu_imaps_record = NULL;
 #endif /* ENABLE_IMAP */
diff --git a/libproto/imap/mbox.c b/libproto/imap/mbox.c
index 3bf1d1b..5163eeb 100644
--- a/libproto/imap/mbox.c
+++ b/libproto/imap/mbox.c
@@ -47,10 +47,10 @@
 #include <mailutils/stream.h>
 #include <mailutils/io.h>
 
-#include <imap0.h>
-#include <mailbox0.h>
-#include <registrar0.h>
-#include <url0.h>
+#include <mailutils/sys/imap.h>
+#include <mailutils/sys/mailbox.h>
+#include <mailutils/sys/registrar.h>
+#include <mailutils/sys/url.h>
 
 #undef min
 #define min(a,b) ((a) < (b) ? (a) : (b))
diff --git a/libproto/imap/url.c b/libproto/imap/url.c
index 5fa06f5..8e3ae1a 100644
--- a/libproto/imap/url.c
+++ b/libproto/imap/url.c
@@ -31,8 +31,8 @@
 # include <strings.h>
 #endif
 
-#include <registrar0.h>
-#include <url0.h>
+#include <mailutils/sys/registrar.h>
+#include <mailutils/sys/url.h>
 
 static void url_imap_destroy (mu_url_t url);
 
diff --git a/libproto/include/.gitignore b/libproto/include/.gitignore
deleted file mode 100644
index 282522d..0000000
--- a/libproto/include/.gitignore
+++ /dev/null
@@ -1,2 +0,0 @@
-Makefile
-Makefile.in
diff --git a/libproto/include/Makefile.am b/libproto/include/Makefile.am
deleted file mode 100644
index bf020dd..0000000
--- a/libproto/include/Makefile.am
+++ /dev/null
@@ -1,39 +0,0 @@
-## Process this file with GNU Automake to create Makefile.in
-
-##   Copyright (C) 2000, 2001, 2002, 2007, 2009, 2010 Free Software
-##   Foundation, Inc.
-##
-##   GNU Mailutils 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 3, 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.  51 Franklin Street, Fifth Floor, Boston, MA
-##   02110-1301 USA
-
-noinst_HEADERS = \
-attribute0.h \
-amd.h \
-auth0.h \
-body0.h \
-debug0.h \
-envelope0.h \
-folder0.h \
-iterator0.h \
-imap0.h \
-list0.h \
-mailbox0.h \
-mailer0.h \
-monitor0.h  \
-observer0.h \
-property0.h \
-registrar0.h \
-stream0.h \
-url0.h
diff --git a/libproto/include/stream0.h b/libproto/include/stream0.h
deleted file mode 100644
index ff8021b..0000000
--- a/libproto/include/stream0.h
+++ /dev/null
@@ -1,76 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 1999, 2000, 2001, 2005, 2007, 2010 Free Software
-   Foundation, Inc.
-
-   This library is free software; you can redistribute it and/or
-   modify it under the terms of the GNU Lesser General Public
-   License as published by the Free Software Foundation; either
-   version 3 of the License, or (at your option) any later version.
-
-   This library 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
-   Lesser General Public License for more details.
-
-   You should have received a copy of the GNU Lesser General
-   Public License along with this library; if not, write to the
-   Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
-   Boston, MA 02110-1301 USA */
-
-#ifndef _STREAM0_H
-#define _STREAM0_H
-
-#include <mailutils/stream.h>
-#ifdef DMALLOC
-#include <dmalloc.h>
-#endif
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/* Read buffer */
-struct rbuffer
-{
-  char *base;
-  char *ptr;
-  size_t count;
-  size_t bufsiz;
-  mu_off_t offset;
-};
-
-struct _mu_stream
-{
-  void *owner;
-  mu_property_t property;
-
-  int flags;
-  int state;
-
-  /* Read space */
-  struct rbuffer rbuffer;
-  
-  /* Stream pointer for sequential offset.  */
-  mu_off_t offset;
-  
-  void (*_destroy) (mu_stream_t);
-  int (*_open)     (mu_stream_t);
-  int (*_close)    (mu_stream_t);
-  int (*_get_transport2) (mu_stream_t, mu_transport_t *, mu_transport_t *);
-  int (*_read)     (mu_stream_t, char *, size_t, mu_off_t, size_t *);
-  int (*_readline) (mu_stream_t, char *, size_t, mu_off_t, size_t *);
-  int (*_write)    (mu_stream_t, const char *, size_t, mu_off_t, size_t *);
-  int (*_truncate) (mu_stream_t, mu_off_t);
-  int (*_size)     (mu_stream_t, mu_off_t *);
-  int (*_flush)    (mu_stream_t);
-  int (*_setbufsiz)(mu_stream_t, size_t);
-  int (*_strerror) (mu_stream_t, const char **);
-  int (*_wait)     (mu_stream_t, int *pflags, struct timeval *tvp);
-  int (*_shutdown) (mu_stream_t, int how);
-};
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* _STREAM0_H */
diff --git a/libproto/maildir/Makefile.am b/libproto/maildir/Makefile.am
index 4e0da11..939cd03 100644
--- a/libproto/maildir/Makefile.am
+++ b/libproto/maildir/Makefile.am
@@ -18,7 +18,7 @@
 ##   Foundation, Inc.  51 Franklin Street, Fifth Floor, Boston, MA
 ##   02110-1301 USA
 
-INCLUDES = @MU_LIB_COMMON_INCLUDES@ -I${top_srcdir}/libproto/include 
+INCLUDES = @MU_LIB_COMMON_INCLUDES@ 
 
 lib_LTLIBRARIES = libmu_maildir.la
 libmu_maildir_la_LDFLAGS=-version-info @VI_CURRENT@:@VI_REVISION@:@VI_AGE@
diff --git a/libproto/maildir/folder.c b/libproto/maildir/folder.c
index 38a9d72..f6431a8 100644
--- a/libproto/maildir/folder.c
+++ b/libproto/maildir/folder.c
@@ -28,13 +28,13 @@
 #include <string.h>
 #include <errno.h>
 
-#include <folder0.h>
-#include <registrar0.h>
+#include <mailutils/sys/folder.h>
+#include <mailutils/sys/registrar.h>
 
 #include <maildir.h>
 #include <mailutils/mutil.h>
 #include <mailutils/url.h>
-#include <amd.h>
+#include <mailutils/sys/amd.h>
 
 static int
 _maildir_folder_init (mu_folder_t folder MU_ARG_UNUSED)
@@ -115,6 +115,6 @@ mu_record_t mu_maildir_record = &_maildir_record;
 
 #else
 #include <stdio.h>
-#include <registrar0.h>
+#include <mailutils/sys/registrar.h>
 mu_record_t mu_maildir_record = NULL;
 #endif
diff --git a/libproto/maildir/mbox.c b/libproto/maildir/mbox.c
index c9d7470..d2d7b9e 100644
--- a/libproto/maildir/mbox.c
+++ b/libproto/maildir/mbox.c
@@ -66,9 +66,9 @@
 #include <mailutils/observer.h>
 #include <mailutils/errno.h>
 #include <mailutils/locker.h>
-#include <mailbox0.h>
-#include <registrar0.h>
-#include <amd.h>
+#include <mailutils/sys/mailbox.h>
+#include <mailutils/sys/registrar.h>
+#include <mailutils/sys/amd.h>
 #include <maildir.h>
 
 #ifndef PATH_MAX 
diff --git a/libproto/mailer/Makefile.am b/libproto/mailer/Makefile.am
index bb272e0..bd952e6 100644
--- a/libproto/mailer/Makefile.am
+++ b/libproto/mailer/Makefile.am
@@ -17,7 +17,7 @@
 ##   Foundation, Inc.  51 Franklin Street, Fifth Floor, Boston, MA
 ##   02110-1301 USA
 
-INCLUDES = @MU_LIB_COMMON_INCLUDES@ -I${top_srcdir}/libproto/include 
+INCLUDES = @MU_LIB_COMMON_INCLUDES@
 
 lib_LTLIBRARIES = libmu_mailer.la
 libmu_mailer_la_LDFLAGS=-version-info @VI_CURRENT@:@VI_REVISION@:@VI_AGE@
diff --git a/libproto/mailer/mbox.c b/libproto/mailer/mbox.c
index 367492c..ab6f905 100644
--- a/libproto/mailer/mbox.c
+++ b/libproto/mailer/mbox.c
@@ -30,8 +30,8 @@
 #include <mailutils/mailer.h>
 #include <mailutils/url.h>
 #include <mailutils/mutil.h>
-#include <mailbox0.h>
-#include <mailer0.h>
+#include <mailutils/sys/mailbox.h>
+#include <mailutils/sys/mailer.h>
 
 struct remote_mbox_data
 {
diff --git a/libproto/mailer/prog.c b/libproto/mailer/prog.c
index 83b371a..216869c 100644
--- a/libproto/mailer/prog.c
+++ b/libproto/mailer/prog.c
@@ -34,9 +34,9 @@
 #include <mailutils/progmailer.h>
 #include <mailutils/vartab.h>
 
-#include <url0.h>
-#include <mailer0.h>
-#include <registrar0.h>
+#include <mailutils/sys/url.h>
+#include <mailutils/sys/mailer.h>
+#include <mailutils/sys/registrar.h>
 
 static int _url_prog_init     (mu_url_t);
 
@@ -379,6 +379,6 @@ prog_send_message (mu_mailer_t mailer, mu_message_t msg, 
mu_address_t from,
 
 #else
 #include <stdio.h>
-#include <registrar0.h>
+#include <mailutils/sys/registrar.h>
 mu_record_t mu_prog_record = NULL;
 #endif
diff --git a/libproto/mailer/remote.c b/libproto/mailer/remote.c
index a99e4ab..1781687 100644
--- a/libproto/mailer/remote.c
+++ b/libproto/mailer/remote.c
@@ -34,9 +34,9 @@
 #include <mailutils/error.h>
 #include <mailutils/diag.h>
 
-#include <url0.h>
-#include <mailer0.h>
-#include <registrar0.h>
+#include <mailutils/sys/url.h>
+#include <mailutils/sys/mailer.h>
+#include <mailutils/sys/registrar.h>
 
 #ifdef ENABLE_SMTP
 
diff --git a/libproto/mailer/sendmail.c b/libproto/mailer/sendmail.c
index 651603e..bc944d2 100644
--- a/libproto/mailer/sendmail.c
+++ b/libproto/mailer/sendmail.c
@@ -40,9 +40,9 @@
 #include <mailutils/errno.h>
 #include <mailutils/progmailer.h>
 
-#include <url0.h>
-#include <mailer0.h>
-#include <registrar0.h>
+#include <mailutils/sys/url.h>
+#include <mailutils/sys/mailer.h>
+#include <mailutils/sys/registrar.h>
 
 static void sendmail_destroy (mu_mailer_t);
 static int sendmail_open (mu_mailer_t, int);
@@ -293,6 +293,6 @@ mu_record_t mu_sendmail_record = &_sendmail_record;
 
 #else
 #include <stdio.h>
-#include <registrar0.h>
+#include <mailutils/sys/registrar.h>
 mu_record_t mu_sendmail_record = NULL;
 #endif
diff --git a/libproto/mailer/smtp.c b/libproto/mailer/smtp.c
index ec8a810..5fb70f9 100644
--- a/libproto/mailer/smtp.c
+++ b/libproto/mailer/smtp.c
@@ -55,9 +55,9 @@
 #include <mailutils/cctype.h>
 #include <mailutils/cstr.h>
 
-#include <mailer0.h>
-#include <url0.h>
-#include <registrar0.h>
+#include <mailutils/sys/mailer.h>
+#include <mailutils/sys/url.h>
+#include <mailutils/sys/registrar.h>
 
 static int      _mailer_smtp_init (mu_mailer_t);
 
@@ -1513,7 +1513,7 @@ smtp_readline (smtp_t smtp)
 
 #else
 #include <stdio.h>
-#include <registrar0.h>
+#include <mailutils/sys/registrar.h>
 mu_record_t     mu_smtp_record = NULL;
 mu_record_t     mu_remote_smtp_record = NULL;
 #endif
diff --git a/libproto/mbox/Makefile.am b/libproto/mbox/Makefile.am
index 935659a..b65cf1e 100644
--- a/libproto/mbox/Makefile.am
+++ b/libproto/mbox/Makefile.am
@@ -18,7 +18,7 @@
 ##   Foundation, Inc.  51 Franklin Street, Fifth Floor, Boston, MA
 ##   02110-1301 USA
 
-INCLUDES = @MU_LIB_COMMON_INCLUDES@ -I${top_srcdir}/libproto/include 
+INCLUDES = @MU_LIB_COMMON_INCLUDES@ 
 
 lib_LTLIBRARIES = libmu_mbox.la
 libmu_mbox_la_LDFLAGS=-version-info @VI_CURRENT@:@VI_REVISION@:@VI_AGE@
diff --git a/libproto/mbox/folder.c b/libproto/mbox/folder.c
index 3557a88..0c44323 100644
--- a/libproto/mbox/folder.c
+++ b/libproto/mbox/folder.c
@@ -33,8 +33,8 @@
 #include <stdio.h>
 #include <stdlib.h>
 
-#include <folder0.h>
-#include <registrar0.h>
+#include <mailutils/sys/folder.h>
+#include <mailutils/sys/registrar.h>
 
 #include <mailutils/auth.h>
 #include <mailutils/url.h>
diff --git a/libproto/mbox/mbox0.h b/libproto/mbox/mbox0.h
index b53aa65..85e8408 100644
--- a/libproto/mbox/mbox0.h
+++ b/libproto/mbox/mbox0.h
@@ -42,8 +42,8 @@
 # include <strings.h>
 #endif
 
-#include <mailbox0.h>
-#include <registrar0.h>
+#include <mailutils/sys/mailbox.h>
+#include <mailutils/sys/registrar.h>
 
 #include <mailutils/address.h>
 #include <mailutils/attribute.h>
diff --git a/libproto/mh/Makefile.am b/libproto/mh/Makefile.am
index 7eadc92..a5be198 100644
--- a/libproto/mh/Makefile.am
+++ b/libproto/mh/Makefile.am
@@ -18,7 +18,7 @@
 ##   Foundation, Inc.  51 Franklin Street, Fifth Floor, Boston, MA
 ##   02110-1301 USA
 
-INCLUDES = @MU_LIB_COMMON_INCLUDES@ -I${top_srcdir}/libproto/include 
+INCLUDES = @MU_LIB_COMMON_INCLUDES@
 
 lib_LTLIBRARIES = libmu_mh.la
 libmu_mh_la_LDFLAGS=-version-info @VI_CURRENT@:@VI_REVISION@:@VI_AGE@
diff --git a/libproto/mh/folder.c b/libproto/mh/folder.c
index 80251b2..1b2c033 100644
--- a/libproto/mh/folder.c
+++ b/libproto/mh/folder.c
@@ -31,10 +31,10 @@
 #include <unistd.h>
 #include <dirent.h>
 
-#include <url0.h>
-#include <folder0.h>
-#include <registrar0.h>
-#include <amd.h>
+#include <mailutils/sys/url.h>
+#include <mailutils/sys/folder.h>
+#include <mailutils/sys/registrar.h>
+#include <mailutils/sys/amd.h>
 #include <mailutils/mutil.h>
 #include <mailutils/cctype.h>
 
@@ -155,6 +155,6 @@ mu_record_t mu_mh_record = &_mh_record;
 
 #else
 #include <stdio.h>
-#include <registrar0.h>
+#include <mailutils/sys/registrar.h>
 mu_record_t mu_mh_record = NULL;
 #endif
diff --git a/libproto/mh/mbox.c b/libproto/mh/mbox.c
index da7da4d..b39ce6c 100644
--- a/libproto/mh/mbox.c
+++ b/libproto/mh/mbox.c
@@ -64,9 +64,9 @@
 #include <mailutils/observer.h>
 #include <mailutils/io.h>
 #include <mailutils/cctype.h>
-#include <mailbox0.h>
-#include <registrar0.h>
-#include <amd.h>
+#include <mailutils/sys/mailbox.h>
+#include <mailutils/sys/registrar.h>
+#include <mailutils/sys/amd.h>
 
 struct _mh_message
 {
diff --git a/libproto/nntp/Makefile.am b/libproto/nntp/Makefile.am
index 6f1c2fa..aecc0c7 100644
--- a/libproto/nntp/Makefile.am
+++ b/libproto/nntp/Makefile.am
@@ -18,7 +18,7 @@
 ##   Foundation, Inc.  51 Franklin Street, Fifth Floor, Boston, MA
 ##   02110-1301 USA
 
-INCLUDES = @MU_LIB_COMMON_INCLUDES@ -I${top_srcdir}/libproto/include 
+INCLUDES = @MU_LIB_COMMON_INCLUDES@
 
 lib_LTLIBRARIES = libmu_nntp.la
 libmu_nntp_la_LDFLAGS=-version-info @VI_CURRENT@:@VI_REVISION@:@VI_AGE@
diff --git a/libproto/nntp/folder.c b/libproto/nntp/folder.c
index c6378c6..0e3b43d 100644
--- a/libproto/nntp/folder.c
+++ b/libproto/nntp/folder.c
@@ -36,8 +36,8 @@
 #include <mailutils/registrar.h>
 #include <mailutils/url.h>
 
-#include <folder0.h>
-#include "nntp0.h"
+#include <mailutils/sys/folder.h>
+#include "mailutils/sys/nntp.h"
 
 /* We export url parsing and the initialisation of
    the mailbox, via the register entry/record.  */
@@ -194,6 +194,6 @@ nntp_folder_list (mu_folder_t folder, const char *ref, void 
*pat, int flags,
 }
 #else
 #include <stdio.h>
-#include <registrar0.h>
+#include <mailutils/sys/registrar.h>
 mu_record_t mu_nntp_record = NULL;
 #endif
diff --git a/libproto/nntp/mbox.c b/libproto/nntp/mbox.c
index 3e58ada..2d09be2 100644
--- a/libproto/nntp/mbox.c
+++ b/libproto/nntp/mbox.c
@@ -51,9 +51,9 @@
 #include <mailutils/url.h>
 #include <mailutils/nntp.h>
 
-#include <folder0.h>
-#include <mailbox0.h>
-#include "nntp0.h"
+#include <mailutils/sys/folder.h>
+#include <mailutils/sys/mailbox.h>
+#include "mailutils/sys/nntp.h"
 
 
 /*  Functions/Methods that implements the mu_mailbox_t API.  */
diff --git a/libproto/nntp/url.c b/libproto/nntp/url.c
index 73173dd..73a66eb 100644
--- a/libproto/nntp/url.c
+++ b/libproto/nntp/url.c
@@ -32,7 +32,7 @@
 
 #include <mailutils/nntp.h>
 
-#include <url0.h>
+#include <mailutils/sys/url.h>
 
 static void url_nntp_destroy (mu_url_t url);
 
diff --git a/libproto/pop/Makefile.am b/libproto/pop/Makefile.am
index 3a3ad8b..b4bf3a8 100644
--- a/libproto/pop/Makefile.am
+++ b/libproto/pop/Makefile.am
@@ -18,23 +18,22 @@
 ##   Foundation, Inc.  51 Franklin Street, Fifth Floor, Boston, MA
 ##   02110-1301 USA
 
-INCLUDES = @MU_LIB_COMMON_INCLUDES@ -I${top_srcdir}/libproto/include 
+INCLUDES = @MU_LIB_COMMON_INCLUDES@
 
 lib_LTLIBRARIES = libmu_pop.la
 libmu_pop_la_LDFLAGS=-version-info @VI_CURRENT@:@VI_REVISION@:@VI_AGE@
 libmu_pop_la_LIBADD = ${MU_LIB_AUTH} ${MU_LIB_MAILUTILS} @INTLLIBS@
 
+#  folder.c\
+#  mbox.c\
+#  url.c
 libmu_pop_la_SOURCES = \
- folder.c\
- mbox.c\
- url.c \
  \
  pop3_apop.c \
  pop3_capa.c \
  pop3_carrier.c \
  pop3_connect.c \
  pop3_create.c \
- pop3_debug.c \
  pop3_dele.c \
  pop3_destroy.c \
  pop3_disconnect.c \
@@ -54,6 +53,7 @@ libmu_pop_la_SOURCES = \
  pop3_stream.c \
  pop3_timeout.c \
  pop3_top.c \
+ pop3_trace.c \
  pop3_uidla.c \
  pop3_uidl.c \
  pop3_user.c
diff --git a/libproto/pop/folder.c b/libproto/pop/folder.c
index 9e792b8..b1ab364 100644
--- a/libproto/pop/folder.c
+++ b/libproto/pop/folder.c
@@ -37,9 +37,9 @@
 #include <mailutils/cstr.h>
 #include <mailutils/cctype.h>
 
-#include <folder0.h>
-#include <registrar0.h>
-#include <url0.h>
+#include <mailutils/sys/folder.h>
+#include <mailutils/sys/registrar.h>
+#include <mailutils/sys/url.h>
 
 /* We export url parsing and the initialisation of
    the mailbox, via the register entry/record.  */
@@ -160,7 +160,7 @@ folder_pop_get_authority (mu_folder_t folder, 
mu_authority_t *pauth)
 
 #else
 #include <stdio.h>
-#include <registrar0.h>
+#include <mailutils/sys/registrar.h>
 mu_record_t mu_pop_record = NULL;
 mu_record_t mu_pops_record = NULL;
 #endif /* ENABLE_POP */
diff --git a/libproto/pop/mbox.c b/libproto/pop/mbox.c
index b9a21d6..5268a9f 100644
--- a/libproto/pop/mbox.c
+++ b/libproto/pop/mbox.c
@@ -58,10 +58,10 @@
 #include <mailutils/cstr.h>
 #include <mailutils/cctype.h>
 
-#include <folder0.h>
-#include <mailbox0.h>
-#include <registrar0.h>
-#include <url0.h>
+#include <mailutils/sys/folder.h>
+#include <mailutils/sys/mailbox.h>
+#include <mailutils/sys/registrar.h>
+#include <mailutils/sys/url.h>
 
 #define PROP_RFC822 1
 
diff --git a/libproto/pop/pop3_apop.c b/libproto/pop/pop3_apop.c
index 454c3e5..c358b93 100644
--- a/libproto/pop/pop3_apop.c
+++ b/libproto/pop/pop3_apop.c
@@ -39,15 +39,11 @@ mu_pop3_apop (mu_pop3_t pop3, const char *user, const char 
*secret)
 
   /* Sanity checks.  */
   if (pop3 == NULL || user == NULL || secret == NULL)
-    {
-      return EINVAL;
-    }
+    return EINVAL;
 
   /* The server did not offer a timestamp in the greeting, bailout early.  */
   if (pop3->timestamp == NULL)
-    {
-      return ENOTSUP;
-    }
+    return ENOTSUP;
 
   switch (pop3->state)
     {
@@ -61,35 +57,25 @@ mu_pop3_apop (mu_pop3_t pop3, const char *user, const char 
*secret)
        size_t n;
 
        mu_md5_init_ctx (&md5context);
-       mu_md5_process_bytes (pop3->timestamp, strlen (pop3->timestamp), 
&md5context);
+       mu_md5_process_bytes (pop3->timestamp, strlen (pop3->timestamp),
+                             &md5context);
        mu_md5_process_bytes (secret, strlen (secret), &md5context);
        mu_md5_finish_ctx (&md5context, md5digest);
        for (tmp = digest, n = 0; n < 16; n++, tmp += 2)
-         {
-           sprintf (tmp, "%02x", md5digest[n]);
-         }
+         sprintf (tmp, "%02x", md5digest[n]);
        *tmp = '\0';
 
        status = mu_pop3_writeline (pop3, "APOP %s %s\r\n", user, digest);
        /* Obscure the digest, for security reasons.  */
        memset (digest, '\0', sizeof digest);
        MU_POP3_CHECK_ERROR (pop3, status);
-       mu_pop3_debug_cmd (pop3);
+       MU_POP3_FCLR (pop3, MU_POP3_ACK); 
        pop3->state = MU_POP3_APOP;
       }
 
     case MU_POP3_APOP:
-      status = mu_pop3_send (pop3);
-      MU_POP3_CHECK_EAGAIN (pop3, status);
-      /* Obscure the digest, for security reasons.  */
-      memset (pop3->io.buf, '\0', pop3->io.len);
-      pop3->acknowledge = 0;
-      pop3->state = MU_POP3_APOP_ACK;
-
-    case MU_POP3_APOP_ACK:
-      status = mu_pop3_response (pop3, NULL, 0, NULL);
+      status = mu_pop3_response (pop3, NULL);
       MU_POP3_CHECK_EAGAIN (pop3, status);
-      mu_pop3_debug_ack (pop3);
       MU_POP3_CHECK_OK (pop3);
       pop3->state = MU_POP3_NO_STATE;
       break;
diff --git a/libproto/pop/pop3_capa.c b/libproto/pop/pop3_capa.c
index 24646ee..181c602 100644
--- a/libproto/pop/pop3_capa.c
+++ b/libproto/pop/pop3_capa.c
@@ -27,47 +27,95 @@
 #include <stdlib.h>
 #include <errno.h>
 #include <mailutils/error.h>
+#include <mailutils/list.h>
+#include <mailutils/cctype.h>
+#include <mailutils/cstr.h>
 #include <mailutils/sys/pop3.h>
 
+static int
+string_comp (const void *item, const void *value)
+{
+  return strcmp (item, value);
+}
+
+int
+_mu_pop3_fill_list (mu_pop3_t pop3, mu_list_t list)
+{
+  mu_stream_t stream;
+  size_t n;
+  int status = mu_pop3_stream_create (pop3, &stream);
+  if (status)
+    return status;
+  
+  while (mu_stream_getline (stream, &pop3->rdbuf, &pop3->rdsize, &n) == 0
+        && n > 0)
+    {
+      char *np = strdup (pop3->rdbuf);
+      if (!np)
+       {
+         status = ENOMEM;
+         break;
+       }
+      mu_rtrim_class (np, MU_CTYPE_SPACE);
+      status = mu_list_append (list, np);
+      if (status)
+       break;
+    }
+  mu_stream_destroy (&stream);
+  return status;
+}
+
 /*
   CAPA command, return a list that contains the result.
   It is the responsability of the caller to destroy the list(mu_list_destroy).
  */
 int
-mu_pop3_capa (mu_pop3_t pop3, mu_iterator_t *piterator)
+mu_pop3_capa (mu_pop3_t pop3, int reread, mu_iterator_t *piter)
 {
   int status = 0;
-
+  
   if (pop3 == NULL)
     return EINVAL;
-  if (piterator == NULL)
-    return MU_ERR_OUT_PTR_NULL;
 
+  if (pop3->capa)
+    {
+      if (!reread)
+       {
+         if (!piter)
+           return 0;
+         return mu_list_get_iterator (pop3->capa, piter);
+       }
+      mu_list_destroy (&pop3->capa);
+    }
+
+  status = mu_list_create (&pop3->capa);
+  if (status)
+    return status;
+  mu_list_set_comparator (pop3->capa, string_comp);
+  mu_list_set_destroy_item (pop3->capa, mu_list_free_item);
+  
   switch (pop3->state)
     {
     case MU_POP3_NO_STATE:
       status = mu_pop3_writeline (pop3, "CAPA\r\n");
       MU_POP3_CHECK_ERROR (pop3, status);
-      mu_pop3_debug_cmd (pop3);
+      MU_POP3_FCLR (pop3, MU_POP3_ACK);
       pop3->state = MU_POP3_CAPA;
 
     case MU_POP3_CAPA:
-      status = mu_pop3_send (pop3);
+      status = mu_pop3_response (pop3, NULL);
       MU_POP3_CHECK_EAGAIN (pop3, status);
-      pop3->acknowledge = 0;
-      pop3->state = MU_POP3_CAPA_ACK;
-
-    case MU_POP3_CAPA_ACK:
-      status = mu_pop3_response (pop3, NULL, 0, NULL);
-      MU_POP3_CHECK_EAGAIN (pop3, status);
-      mu_pop3_debug_ack (pop3);
       MU_POP3_CHECK_OK (pop3);
-      status = mu_pop3_iterator_create (pop3, piterator);
-      MU_POP3_CHECK_ERROR (pop3, status);
       pop3->state = MU_POP3_CAPA_RX;
 
     case MU_POP3_CAPA_RX:
-      /* The mu_iterator_t will read the stream and set the state to 
MU_POP3_NO_STATE when done.  */
+      status = _mu_pop3_fill_list (pop3, pop3->capa);
+      MU_POP3_CHECK_ERROR (pop3, status);
+      if (piter)
+       status = mu_list_get_iterator (pop3->capa, piter);
+      else
+       status = 0;
+      pop3->state = MU_POP3_NO_STATE;
       break;
 
       /* They must deal with the error first by reopening.  */
diff --git a/libproto/pop/pop3_carrier.c b/libproto/pop/pop3_carrier.c
index d92136d..7331756 100644
--- a/libproto/pop/pop3_carrier.c
+++ b/libproto/pop/pop3_carrier.c
@@ -38,10 +38,14 @@ mu_pop3_set_carrier (mu_pop3_t pop3, mu_stream_t carrier)
       mu_pop3_disconnect (pop3);
       mu_stream_destroy (&pop3->carrier);
     }
+  mu_stream_ref (carrier);
   pop3->carrier = carrier;
+  if (MU_POP3_FISSET (pop3, MU_POP3_TRACE))
+    _mu_pop3_trace_enable (pop3);
   return 0;
 }
 
+/* FIXME: Is it needed? */
 int
 mu_pop3_get_carrier (mu_pop3_t pop3, mu_stream_t *pcarrier)
 {
diff --git a/libproto/pop/pop3_connect.c b/libproto/pop/pop3_connect.c
index f71083c..9377e84 100644
--- a/libproto/pop/pop3_connect.c
+++ b/libproto/pop/pop3_connect.c
@@ -53,14 +53,15 @@ mu_pop3_connect (mu_pop3_t pop3)
 
     case MU_POP3_NO_STATE:
       /* If the stream was previoulsy open this is sudden death:
-         for many pop servers, it is important to let them time to remove any 
locks or move
-         the .user.pop files.  This happen when we do close() and immediately 
open().
-         For example, the user does not want to read the entire file, and 
wants to start
-        to read a new message, closing the connection and immediately
-        contacting the server again, and he'll end up having
-        "-ERR Mail Lock busy" or something similar. To prevent this race
-        condition we sleep 2 seconds.  You can see this behaviour in an
-        environment where QPopper(Qualcomm POP3 server) is use and the user as 
a big mailbox. */
+         For many pop servers, it is important to allow them some time to
+        remove any locks or move the .user.pop files.  This happen when we
+        do close() and immediately open().  For example, the user does not
+        want to read the entire file, and wants to start to read a new
+        message, closing the connection and immediately contacting the
+        server again, and he'll end up having "-ERR Mail Lock busy" or
+        something similar. To prevent this race condition we sleep 2 seconds.
+        You can see this behaviour in an environment where QPopper (Qualcomm
+        POP3 server) is used and the user has a big mailbox. */
       status = mu_pop3_disconnect (pop3);
       if (status != 0)
         mu_pop3_sleep (2);
@@ -70,7 +71,7 @@ mu_pop3_connect (mu_pop3_t pop3)
       /* Establish the connection.  */
       status = mu_stream_open (pop3->carrier);
       MU_POP3_CHECK_EAGAIN (pop3, status);
-      pop3->acknowledge = 0;
+      MU_POP3_FCLR (pop3, MU_POP3_ACK);
       pop3->state = MU_POP3_GREETINGS;
 
     case MU_POP3_GREETINGS:
@@ -78,10 +79,9 @@ mu_pop3_connect (mu_pop3_t pop3)
       {
        size_t len = 0;
        char *right, *left;
-       status = mu_pop3_response (pop3, NULL, 0, &len);
+       status = mu_pop3_response (pop3, &len);
        MU_POP3_CHECK_EAGAIN (pop3, status);
-       mu_pop3_debug_ack (pop3);
-       if (mu_c_strncasecmp (pop3->ack.buf, "+OK", 3) != 0)
+       if (mu_c_strncasecmp (pop3->ackbuf, "+OK", 3) != 0)
          {
            mu_stream_close (pop3->carrier);
            pop3->state = MU_POP3_NO_STATE;
@@ -89,10 +89,10 @@ mu_pop3_connect (mu_pop3_t pop3)
          }
 
        /* Get the timestamp.  */
-       right = memchr (pop3->ack.buf, '<', len);
+       right = memchr (pop3->ackbuf, '<', len);
        if (right)
          {
-           len = len - (right - pop3->ack.buf);
+           len = len - (right - pop3->ackbuf);
            left = memchr (right, '>', len);
            if (left)
              {
@@ -121,5 +121,5 @@ mu_pop3_sleep (int seconds)
   struct timeval tval;
   tval.tv_sec = seconds;
   tval.tv_usec = 0;
-  return select (1, NULL, NULL, NULL, &tval);
+  return select (0, NULL, NULL, NULL, &tval);
 }
diff --git a/libproto/pop/pop3_create.c b/libproto/pop/pop3_create.c
index c9169ba..eb6a6e8 100644
--- a/libproto/pop/pop3_create.c
+++ b/libproto/pop/pop3_create.c
@@ -40,34 +40,24 @@ mu_pop3_create (mu_pop3_t *ppop3)
   if (pop3 == NULL)
     return ENOMEM;
 
-  /* Reserve space for the ack(nowledgement) response.
-     According to RFC 2449: The maximum length of the first line of a
-     command response (including the initial greeting) is unchanged at
-     512 octets (including the terminating CRLF).  */
-  pop3->ack.len = 512;
-  pop3->ack.buf = calloc (pop3->ack.len, 1);
-  if (pop3->ack.buf == NULL)
-    {
-      mu_pop3_destroy (&pop3);
-      return ENOMEM;
-    }
-  pop3->ack.ptr = pop3->ack.buf;
-
-  /* Reserve space for the data response/content.
-     RFC 2449 recommands 255, but we grow it as needed.  */
-  pop3->io.len = 255;
-  pop3->io.buf = calloc (pop3->io.len, 1);
-  if (pop3->io.buf == NULL)
-    {
-      mu_pop3_destroy (&pop3);
-      return ENOMEM;
-    }
-  pop3->io.ptr = pop3->io.buf;
-
   pop3->state = MU_POP3_NO_STATE; /* Init with no state.  */
   pop3->timeout = (10 * 60) * 100; /* The default Timeout is 10 minutes.  */
-  pop3->acknowledge = 0; /* No Ack received.  */
+  MU_POP3_FCLR (pop3, MU_POP3_ACK); /* No Ack received.  */
 
   *ppop3 = pop3;
   return 0; /* Okdoke.  */
 }
+
+int
+_mu_pop3_init (mu_pop3_t pop3)
+{
+  if (pop3 == NULL)
+    return EINVAL;
+  if (pop3->carrier == 0)
+    {
+      mu_list_destroy (&pop3->capa);
+      pop3->flags = 0;
+    }
+}
+
+  
diff --git a/libproto/pop/pop3_debug.c b/libproto/pop/pop3_debug.c
deleted file mode 100644
index a196029..0000000
--- a/libproto/pop/pop3_debug.c
+++ /dev/null
@@ -1,51 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 2003, 2007, 2010 Free Software Foundation, Inc.
-
-   This library is free software; you can redistribute it and/or
-   modify it under the terms of the GNU Lesser General Public
-   License as published by the Free Software Foundation; either
-   version 3 of the License, or (at your option) any later version.
-
-   This library 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
-   Lesser General Public License for more details.
-
-   You should have received a copy of the GNU Lesser General
-   Public License along with this library; if not, write to the
-   Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
-   Boston, MA 02110-1301 USA */
-
-#ifdef HAVE_CONFIG_H
-# include <config.h>
-#endif
-
-#include <stdlib.h>
-#include <errno.h>
-#include <mailutils/sys/pop3.h>
-
-int
-mu_pop3_set_debug (mu_pop3_t pop3, mu_debug_t debug)
-{
-  if (pop3 == NULL)
-    return EINVAL;
-  if (pop3->debug)
-    mu_debug_destroy (&pop3->debug, NULL);
-  pop3->debug = debug;
-  return 0;
-}
-
-/* FIXME: This should be a macro */
-int
-mu_pop3_debug_cmd (mu_pop3_t pop3)
-{
-  MU_DEBUG (pop3->debug, MU_DEBUG_PROT, pop3->io.buf);
-  return 0;
-}
-
-int
-mu_pop3_debug_ack (mu_pop3_t pop3)
-{
-  MU_DEBUG1 (pop3->debug, MU_DEBUG_PROT, "%s\n", pop3->ack.buf);
-  return 0;
-}
diff --git a/libproto/pop/pop3_dele.c b/libproto/pop/pop3_dele.c
index 0e866dd..e4b06c4 100644
--- a/libproto/pop/pop3_dele.c
+++ b/libproto/pop/pop3_dele.c
@@ -38,19 +38,12 @@ mu_pop3_dele (mu_pop3_t pop3, unsigned msgno)
     case MU_POP3_NO_STATE:
       status = mu_pop3_writeline (pop3, "DELE %d\r\n", msgno);
       MU_POP3_CHECK_ERROR (pop3, status);
-      mu_pop3_debug_cmd (pop3);
+      MU_POP3_FCLR (pop3, MU_POP3_ACK);
       pop3->state = MU_POP3_DELE;
 
     case MU_POP3_DELE:
-      status = mu_pop3_send (pop3);
+      status = mu_pop3_response (pop3, NULL);
       MU_POP3_CHECK_EAGAIN (pop3, status);
-      pop3->acknowledge = 0;
-      pop3->state = MU_POP3_DELE_ACK;
-
-    case MU_POP3_DELE_ACK:
-      status = mu_pop3_response (pop3, NULL, 0, NULL);
-      MU_POP3_CHECK_EAGAIN (pop3, status);
-      mu_pop3_debug_ack (pop3);
       MU_POP3_CHECK_OK (pop3);
       pop3->state = MU_POP3_NO_STATE;
       break;
diff --git a/libproto/pop/pop3_destroy.c b/libproto/pop/pop3_destroy.c
index 7bf82b2..75ee7d7 100644
--- a/libproto/pop/pop3_destroy.c
+++ b/libproto/pop/pop3_destroy.c
@@ -32,17 +32,18 @@ mu_pop3_destroy (mu_pop3_t *ppop3)
       mu_pop3_t pop3 = *ppop3;
 
       /* Free the response buffer.  */
-      if (pop3->ack.buf)
-       free (pop3->ack.buf);
-
-      /* Free the io buffer.  */
-      if (pop3->io.buf)
-       free (pop3->io.buf);
+      if (pop3->ackbuf)
+       free (pop3->ackbuf);
+      /* Free the read buffer.  */
+      if (pop3->rdbuf)
+       free (pop3->rdbuf);
 
       /* Free the timestamp use for APOP.  */
       if (pop3->timestamp)
        free (pop3->timestamp);
 
+      mu_list_destroy (&pop3->capa);
+      
       /* Release the carrier.  */
       if (pop3->carrier)
        mu_stream_destroy (&pop3->carrier);
diff --git a/libproto/pop/pop3_disconnect.c b/libproto/pop/pop3_disconnect.c
index 558ffb6..621a566 100644
--- a/libproto/pop/pop3_disconnect.c
+++ b/libproto/pop/pop3_disconnect.c
@@ -36,13 +36,10 @@ mu_pop3_disconnect (mu_pop3_t pop3)
   /* We can keep some of the fields, if they decide to pop3_connect() again but
      clear the states.  */
   pop3->state = MU_POP3_NO_STATE;
-  pop3->acknowledge = 0;
+  MU_POP3_FCLR (pop3, MU_POP3_ACK);
 
-  /* Clear the buffers.  */
-  memset (pop3->io.buf, '\0', pop3->io.len);
-  pop3->io.ptr = pop3->io.buf;
-  memset (pop3->ack.buf, '\0', pop3->ack.len);
-  pop3->ack.ptr = pop3->ack.buf;
+  if (pop3->rdbuf)
+    pop3->rdbuf[0] = 0;
 
   /* Free the timestamp, it will be different on each connection.  */
   if (pop3->timestamp)
diff --git a/libproto/pop/pop3_iterator.c b/libproto/pop/pop3_iterator.c
index edffe75..d7a2313 100644
--- a/libproto/pop/pop3_iterator.c
+++ b/libproto/pop/pop3_iterator.c
@@ -25,7 +25,10 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <errno.h>
+#include <mailutils/pop3.h>
 #include <mailutils/sys/pop3.h>
+#include <mailutils/cctype.h>
+#include <mailutils/cstr.h>
 
 static int  pop3_itr_dup     (void **ptr, void *owner);
 static int  pop3_itr_destroy (mu_iterator_t itr, void *owner);
@@ -38,8 +41,11 @@ static int  pop3_itr_finished_p (void *owner);
 struct pop3_iterator
 {
   mu_pop3_t pop3;
+  mu_stream_t stream;
   int done;
   char *item;
+  char *rdbuf;
+  size_t rdsize;
 };
 
 int
@@ -53,9 +59,17 @@ mu_pop3_iterator_create (mu_pop3_t pop3, mu_iterator_t 
*piterator)
   if (pop3_iterator == NULL)
     return ENOMEM;
 
+  status = mu_pop3_stream_create (pop3, &pop3_iterator->stream);
+  if (status)
+    {
+      free (pop3_iterator);
+      return status;
+    }
   pop3_iterator->item = NULL;
+  pop3_iterator->rdbuf = NULL;
+  pop3_iterator->rdsize = 0;
   pop3_iterator->done = 0;
-  pop3_iterator->pop3= pop3;
+  pop3_iterator->pop3 = pop3;
 
   status = mu_iterator_create (&iterator, pop3_iterator);
   if (status != 0)
@@ -93,17 +107,21 @@ pop3_itr_destroy (mu_iterator_t iterator, void *owner)
 {
   struct pop3_iterator *pop3_iterator = (struct pop3_iterator *)owner;
   /* Delicate situation if they did not finish to drain the result
-     We take te approach to do it for the user.  FIXME: Not sure
+     We take the approach to do it for the user.  FIXME: Not sure
      if this is the rigth thing to do. The other way is to close the stream  */
   if (!pop3_iterator->done)
     {
       char buf[128];
       size_t n = 0;
-      while (mu_pop3_readline (pop3_iterator->pop3, buf, sizeof buf, &n) > 0 
&& n > 0)
+      mu_stream_t str = pop3_iterator->pop3->carrier;
+      while (mu_stream_readline (str, buf, sizeof buf, &n) > 0
+            && n > 0)
        n = 0;
     }
   if (pop3_iterator->item)
     free (pop3_iterator->item);
+  if (pop3_iterator->rdbuf)
+    free (pop3_iterator->rdbuf);
   pop3_iterator->pop3->state = MU_POP3_NO_STATE;
   free (pop3_iterator);
   return 0;
@@ -119,40 +137,27 @@ static int
 pop3_itr_next (void *owner)
 {
   struct pop3_iterator *pop3_iterator = (struct pop3_iterator *)owner;
-  size_t n = 0;
   int status = 0;
+  size_t n;
+  
+  status = mu_stream_getline (pop3_iterator->stream, &pop3_iterator->rdbuf,
+                             &pop3_iterator->rdsize, &n);
+  
+  if (status || n == 0)
+    {
+      pop3_iterator->done = 1;
+      pop3_iterator->pop3->state = MU_POP3_NO_STATE;
+      return 0;
+    }
 
-  if (!pop3_iterator->done)
+  n = mu_rtrim_class (pop3_iterator->rdbuf, MU_CTYPE_SPACE);
+  if (n == 1 && pop3_iterator->rdbuf[0] == '.')
     {
-      /* The first readline will not consume the buffer, we just need to
-        know how much to read.  */
-      status = mu_pop3_readline (pop3_iterator->pop3, NULL, 0, &n);
-      if (status == 0)
-       {
-         if (n)
-           {
-             char *buf;
-             buf = calloc (n + 1, 1);
-             if (buf)
-               {
-                 /* Consume.  */
-                 mu_pop3_readline (pop3_iterator->pop3, buf, n + 1, NULL);
-                 if (buf[n - 1] == '\n')
-                   buf[n - 1] = '\0';
-                 if (pop3_iterator->item)
-                   free (pop3_iterator->item);
-                 pop3_iterator->item = buf;
-               }
-             else
-               status = ENOMEM;
-           }
-         else
-           {
-             pop3_iterator->done = 1;
-             pop3_iterator->pop3->state = MU_POP3_NO_STATE;
-           }
-       }
+      pop3_iterator->done = 1;
+      pop3_iterator->pop3->state = MU_POP3_NO_STATE;
     }
+  else
+    pop3_iterator->item = pop3_iterator->rdbuf;
   return status;
 }
 
diff --git a/libproto/pop/pop3_list.c b/libproto/pop/pop3_list.c
index ed50f98..d63272e 100644
--- a/libproto/pop/pop3_list.c
+++ b/libproto/pop/pop3_list.c
@@ -42,25 +42,19 @@ mu_pop3_list (mu_pop3_t pop3, unsigned int msgno, size_t 
*psize)
     case MU_POP3_NO_STATE:
       status = mu_pop3_writeline (pop3, "LIST %d\r\n", msgno);
       MU_POP3_CHECK_ERROR (pop3, status);
-      mu_pop3_debug_cmd (pop3);
+      MU_POP3_FCLR (pop3, MU_POP3_ACK);
       pop3->state = MU_POP3_LIST;
 
     case MU_POP3_LIST:
-      status = mu_pop3_send (pop3);
-      MU_POP3_CHECK_EAGAIN (pop3, status);
-      pop3->acknowledge = 0;
-      pop3->state = MU_POP3_LIST_ACK;
-
-    case MU_POP3_LIST_ACK:
-      status = mu_pop3_response (pop3, NULL, 0, NULL);
-      mu_pop3_debug_ack (pop3);
+      status = mu_pop3_response (pop3, NULL);
       MU_POP3_CHECK_EAGAIN (pop3, status);
       MU_POP3_CHECK_OK (pop3);
       pop3->state = MU_POP3_NO_STATE;
 
       /* Parse the answer.  */
       lv = 0;
-      sscanf (pop3->ack.buf, "+OK %d %lu", &msgno, &lv);
+      /* FIXME: Error checking */
+      sscanf (pop3->ackbuf, "+OK %d %lu", &msgno, &lv);
       *psize = lv;
       break;
 
diff --git a/libproto/pop/pop3_lista.c b/libproto/pop/pop3_lista.c
index d2f825d..66ee948 100644
--- a/libproto/pop/pop3_lista.c
+++ b/libproto/pop/pop3_lista.c
@@ -42,26 +42,20 @@ mu_pop3_list_all (mu_pop3_t pop3, mu_iterator_t *piterator)
     case MU_POP3_NO_STATE:
       status = mu_pop3_writeline (pop3, "LIST\r\n");
       MU_POP3_CHECK_ERROR (pop3, status);
-      mu_pop3_debug_cmd (pop3);
+      MU_POP3_FCLR (pop3, MU_POP3_ACK);
       pop3->state = MU_POP3_LIST;
 
     case MU_POP3_LIST:
-      status = mu_pop3_send (pop3);
+      status = mu_pop3_response (pop3, NULL);
       MU_POP3_CHECK_EAGAIN (pop3, status);
-      pop3->acknowledge = 0;
-      pop3->state = MU_POP3_LIST_ACK;
-
-    case MU_POP3_LIST_ACK:
-      status = mu_pop3_response (pop3, NULL, 0, NULL);
-      MU_POP3_CHECK_EAGAIN (pop3, status);
-      mu_pop3_debug_ack (pop3);
       MU_POP3_CHECK_OK (pop3);
       status = mu_pop3_iterator_create (pop3, piterator);
       MU_POP3_CHECK_ERROR (pop3, status);
       pop3->state = MU_POP3_LIST_RX;
 
     case MU_POP3_LIST_RX:
-      /* The mu_iterator_t will read the stream and set the state to 
MU_POP3_NO_STATE when done.  */
+      /* The mu_iterator_t will read the stream and set the state to
+        MU_POP3_NO_STATE when done.  */
       break;
 
       /* They must deal with the error first by reopening.  */
diff --git a/libproto/pop/pop3_noop.c b/libproto/pop/pop3_noop.c
index 906ca93..beed039 100644
--- a/libproto/pop/pop3_noop.c
+++ b/libproto/pop/pop3_noop.c
@@ -38,19 +38,12 @@ mu_pop3_noop (mu_pop3_t pop3)
     case MU_POP3_NO_STATE:
       status = mu_pop3_writeline (pop3, "NOOP\r\n");
       MU_POP3_CHECK_ERROR (pop3, status);
-      mu_pop3_debug_cmd (pop3);
+      MU_POP3_FCLR (pop3, MU_POP3_ACK);
       pop3->state = MU_POP3_NOOP;
 
     case MU_POP3_NOOP:
-      status = mu_pop3_send (pop3);
+      status = mu_pop3_response (pop3, NULL);
       MU_POP3_CHECK_EAGAIN (pop3, status);
-      pop3->acknowledge = 0;
-      pop3->state = MU_POP3_NOOP_ACK;
-
-    case MU_POP3_NOOP_ACK:
-      status = mu_pop3_response (pop3, NULL, 0, NULL);
-      MU_POP3_CHECK_EAGAIN (pop3, status);
-      mu_pop3_debug_ack (pop3);
       MU_POP3_CHECK_OK (pop3);
       pop3->state = MU_POP3_NO_STATE;
       break;
diff --git a/libproto/pop/pop3_pass.c b/libproto/pop/pop3_pass.c
index b6cfa67..77b860b 100644
--- a/libproto/pop/pop3_pass.c
+++ b/libproto/pop/pop3_pass.c
@@ -37,21 +37,13 @@ mu_pop3_pass (mu_pop3_t pop3, const char *passwd)
     case MU_POP3_NO_STATE:
       status = mu_pop3_writeline (pop3, "PASS %s\r\n", passwd);
       MU_POP3_CHECK_ERROR (pop3, status);
-      mu_pop3_debug_cmd (pop3);
+      /* FIXME: how to obscure the passwd in the stream buffer? */
+      MU_POP3_FCLR (pop3, MU_POP3_ACK);
       pop3->state = MU_POP3_PASS;
 
     case MU_POP3_PASS:
-      status = mu_pop3_send (pop3);
+      status = mu_pop3_response (pop3, NULL);
       MU_POP3_CHECK_EAGAIN (pop3, status);
-      /* Obscure the passwd.  */
-      memset (pop3->io.buf, '\0', pop3->io.len);
-      pop3->acknowledge = 0;
-      pop3->state = MU_POP3_PASS_ACK;
-
-    case MU_POP3_PASS_ACK:
-      status = mu_pop3_response (pop3, NULL, 0, NULL);
-      MU_POP3_CHECK_EAGAIN (pop3, status);
-      mu_pop3_debug_ack (pop3);
       MU_POP3_CHECK_OK (pop3);
       pop3->state = MU_POP3_NO_STATE;
       break;
diff --git a/libproto/pop/pop3_quit.c b/libproto/pop/pop3_quit.c
index f17d711..8ec8178 100644
--- a/libproto/pop/pop3_quit.c
+++ b/libproto/pop/pop3_quit.c
@@ -38,21 +38,15 @@ mu_pop3_quit (mu_pop3_t pop3)
     case MU_POP3_NO_STATE:
       status = mu_pop3_writeline (pop3, "QUIT\r\n");
       MU_POP3_CHECK_ERROR (pop3, status);
-      mu_pop3_debug_cmd (pop3);
+      MU_POP3_FCLR (pop3, MU_POP3_ACK);
       pop3->state = MU_POP3_QUIT;
 
     case MU_POP3_QUIT:
-      status = mu_pop3_send (pop3);
+      status = mu_pop3_response (pop3, NULL);
       MU_POP3_CHECK_EAGAIN (pop3, status);
-      pop3->acknowledge = 0;
-      pop3->state = MU_POP3_QUIT_ACK;
-
-    case MU_POP3_QUIT_ACK:
-      status = mu_pop3_response (pop3, NULL, 0, NULL);
-      MU_POP3_CHECK_EAGAIN (pop3, status);
-      mu_pop3_debug_ack (pop3);
       MU_POP3_CHECK_OK (pop3);
       pop3->state = MU_POP3_NO_STATE;
+      _mu_pop3_init (pop3);
       break;
 
     default:
diff --git a/libproto/pop/pop3_readline.c b/libproto/pop/pop3_readline.c
index a35d6a9..566735b 100644
--- a/libproto/pop/pop3_readline.c
+++ b/libproto/pop/pop3_readline.c
@@ -29,6 +29,8 @@
 #include <errno.h>
 #include <mailutils/sys/pop3.h>
 #include <mailutils/error.h>
+#include <mailutils/cctype.h>
+#include <mailutils/cstr.h>
 
 int
 mu_pop3_carrier_is_ready (mu_stream_t carrier, int flag, int timeout)
@@ -52,143 +54,28 @@ mu_pop3_carrier_is_ready (mu_stream_t carrier, int flag, 
int timeout)
 /* Read a complete line from the pop server. Transform CRLF to LF, remove
    the stuff byte termination octet ".", put a null in the buffer
    when done.  And Do a select() (stream_is_readready()) for the timeout.  */
-static int
+/* FIXME: Is it needed? */
+int
 mu_pop3_getline (mu_pop3_t pop3)
 {
-  size_t n = 0;
-  size_t total = pop3->io.ptr - pop3->io.buf;
-  int status = 0;
-
-  /* Must get a full line before bailing out.  */
-  do
+  size_t n;
+  int status = mu_stream_getline (pop3->carrier, &pop3->rdbuf,
+                                 &pop3->rdsize, &n);
+  if (status == 0)
     {
-      /* Timeout with select(), note that we have to reset select()
-        since on linux tv is modified when error.  */
-      if (pop3->timeout)
-       {
-         int ready = mu_pop3_carrier_is_ready (pop3->carrier,
-                                               MU_STREAM_READY_RD,
-                                               pop3->timeout);
-         if (ready == 0)
-           return ETIMEDOUT;
-       }
-
-      status = mu_stream_readline (pop3->carrier,
-                                  pop3->io.buf + total,
-                                  pop3->io.len - total, &n);
-      if (status != 0)
-       return status;
-
-      /* The server went away:  It maybe a timeout and some pop server
-        does not send the -ERR.  Consider this like an error.  */
       if (n == 0)
        return EIO;
-
-      total += n;
-      pop3->io.nl = memchr (pop3->io.buf, '\n', total);
-      if (pop3->io.nl == NULL)  /* Do we have a full line.  */
-       {
-         /* Allocate a bigger buffer ?  */
-         if (total >= pop3->io.len - 1)
-           {
-             pop3->io.len *= 2;
-             pop3->io.buf = realloc (pop3->io.buf, pop3->io.len + 1);
-             if (pop3->io.buf == NULL)
-               return ENOMEM;
-           }
-       }
-      pop3->io.ptr = pop3->io.buf + total;
-    }
-  while (pop3->io.nl == NULL); /* Bail only if we have a complete line.  */
-
-  /* When examining a multi-line response, the client checks to see if the
-     line begins with the termination octet "."(DOT). If yes and if octets
-     other than CRLF follow, the first octet of the line (the termination
-     octet) is stripped away.  */
-  if (total >= 3  && pop3->io.buf[0] == '.')
-    {
-      if (pop3->io.buf[1] != '\r' && pop3->io.buf[2] != '\n')
-       {
-         memmove (pop3->io.buf, pop3->io.buf + 1, total - 1);
-         pop3->io.ptr--;
-         pop3->io.nl--;
-       }
-      /* And if CRLF immediately follows the termination character, then
-        the response from the POP server is ended and the line containing
-        ".CRLF" is not considered part of the multi-line response.  */
-      else if (pop3->io.buf[1] == '\r' && pop3->io.buf[2] == '\n')
-       {
-         pop3->io.buf[0] = '\0';
-         pop3->io.ptr = pop3->io.buf;
-         pop3->io.nl = NULL;
-       }
-    }
-  /* \r\n --> \n\0, conversion.  */
-  if (pop3->io.nl > pop3->io.buf)
-    {
-      *(pop3->io.nl - 1) = '\n';
-      *(pop3->io.nl) = '\0';
-      pop3->io.ptr = pop3->io.nl;
+      n = mu_rtrim_class (pop3->rdbuf, MU_CTYPE_SPACE);
+
+      /* When examining a multi-line response, the client checks to see if the
+        line begins with the termination octet "."(DOT). If yes and if octets
+        other than CRLF follow, the first octet of the line (the termination
+        octet) is stripped away.  */
+      if (n >= 2 &&
+         pop3->rdbuf[0] == '.' &&
+         pop3->rdbuf[1] != '\n')
+       memmove (pop3->rdbuf, pop3->rdbuf + 1, n);
     }
   return status;
 }
 
-/* Call pop3_getline() for the dirty work,  and consume i.e. put
-   in the user buffer only buflen. If buflen == 0 or buffer == NULL
-   nothing is consume, the data is save for another call to pop3_readline()
-   with a buffer != NULL.
-  */
-int
-mu_pop3_readline (mu_pop3_t pop3, char *buffer, size_t buflen, size_t *pnread)
-{
-  size_t nread = 0;
-  size_t n = 0;
-  int status = 0;
-
-  /* Do we need to fill up? Yes if no NL or the buffer is empty.  */
-  if (pop3->carrier && (pop3->io.nl == NULL || pop3->io.ptr == pop3->io.buf))
-    {
-      status = mu_pop3_getline (pop3);
-      if (status != 0)
-       return status;
-    }
-
-  /* How much we can copy ?  */
-  n = pop3->io.ptr - pop3->io.buf;
-
-  /* Consume the line?  */
-  if (buffer && buflen)
-    {
-      buflen--; /* For the null.  */
-      if (buflen)
-       {
-         int nleft = buflen - n;
-         /* We got more then requested.  */
-         if (nleft < 0)
-           {
-             size_t sentinel;
-             nread = buflen;
-             sentinel = pop3->io.ptr - (pop3->io.buf + nread);
-             memcpy (buffer, pop3->io.buf, nread);
-             memmove (pop3->io.buf, pop3->io.buf + nread, sentinel);
-             pop3->io.ptr = pop3->io.buf + sentinel;
-           }
-         else
-           {
-             /* Drain our buffer.  */;
-             nread = n;
-             memcpy (buffer, pop3->io.buf, nread);
-             pop3->io.ptr = pop3->io.buf;
-             /* Clear of all residue.  */
-             memset (pop3->io.buf, '\0', pop3->io.len);
-           }
-       }
-      buffer[nread] = '\0';
-    }
-  else
-    nread = n;
-
-  if (pnread)
-    *pnread = nread;
-  return status;
-}
diff --git a/libproto/pop/pop3_response.c b/libproto/pop/pop3_response.c
index 8ff4144..2a106ab 100644
--- a/libproto/pop/pop3_response.c
+++ b/libproto/pop/pop3_response.c
@@ -20,14 +20,19 @@
 # include <config.h>
 #endif
 
+#include <stdlib.h>
 #include <string.h>
 #include <errno.h>
+#include <mailutils/cctype.h>
+#include <mailutils/cstr.h>
 #include <mailutils/sys/pop3.h>
 
+#define POP3_DEFERR "-ERR POP3 IO ERROR"
+
 /* If we did not grap the ack already, call pop3_readline() but handle
    Nonblocking also.  */
 int
-mu_pop3_response (mu_pop3_t pop3, char *buffer, size_t buflen, size_t *pnread)
+mu_pop3_response (mu_pop3_t pop3, size_t *pnread)
 {
   size_t n = 0;
   int status = 0;
@@ -35,37 +40,33 @@ mu_pop3_response (mu_pop3_t pop3, char *buffer, size_t 
buflen, size_t *pnread)
   if (pop3 == NULL)
     return EINVAL;
 
-  if (!pop3->acknowledge)
+  if (!MU_POP3_FISSET (pop3, MU_POP3_ACK))
     {
-      size_t len = pop3->ack.len - (pop3->ack.ptr  - pop3->ack.buf);
-      status = mu_pop3_readline (pop3, pop3->ack.ptr, len, &n);
-      pop3->ack.ptr += n;
+      status = mu_stream_getline (pop3->carrier, &pop3->ackbuf,
+                                 &pop3->acksize, NULL);
       if (status == 0)
        {
-         len = pop3->ack.ptr - pop3->ack.buf;
-         if (len && pop3->ack.buf[len - 1] == '\n')
-           pop3->ack.buf[len - 1] = '\0';
-         pop3->acknowledge = 1; /* Flag that we have the ack.  */
-         pop3->ack.ptr = pop3->ack.buf;
+         n = mu_rtrim_class (pop3->ackbuf, MU_CTYPE_SPACE);
+         MU_POP3_FSET (pop3, MU_POP3_ACK); /* Flag that we have the ack.  */
        }
       else
        {
          /* Provide them with an error.  */
-         const char *econ = "-ERR POP3 IO ERROR";
-         n = strlen (econ);
-         strcpy (pop3->ack.buf, econ);
+         if (pop3->acksize < sizeof (POP3_DEFERR))
+           {
+             char *p = realloc (pop3->ackbuf, sizeof (POP3_DEFERR));
+             if (p)
+               {
+                 pop3->ackbuf = p;
+                 pop3->acksize = sizeof (POP3_DEFERR);
+               }
+           }
+         if (pop3->ackbuf)
+           strncpy (pop3->ackbuf, POP3_DEFERR, pop3->acksize);
        }
     }
-  else
-    n = strlen (pop3->ack.buf);
-
-  if (buffer)
-    {
-      buflen--; /* Leave space for the NULL.  */
-      n = (buflen < n) ? buflen : n;
-      memcpy (buffer, pop3->ack.buf, n);
-      buffer[n] = '\0';
-    }
+  else if (pop3->ackbuf)
+    n = strlen (pop3->ackbuf);
 
   if (pnread)
     *pnread = n;
diff --git a/libproto/pop/pop3_retr.c b/libproto/pop/pop3_retr.c
index 7f1d071..12a5c0a 100644
--- a/libproto/pop/pop3_retr.c
+++ b/libproto/pop/pop3_retr.c
@@ -41,25 +41,19 @@ mu_pop3_retr (mu_pop3_t pop3, unsigned int msgno, 
mu_stream_t *pstream)
     case MU_POP3_NO_STATE:
       status = mu_pop3_writeline (pop3, "RETR %d\r\n", msgno);
       MU_POP3_CHECK_ERROR (pop3, status);
-      mu_pop3_debug_cmd (pop3);
+      MU_POP3_FCLR (pop3, MU_POP3_ACK);
       pop3->state = MU_POP3_RETR;
 
     case MU_POP3_RETR:
-      status = mu_pop3_send (pop3);
+      status = mu_pop3_response (pop3, NULL);
       MU_POP3_CHECK_EAGAIN (pop3, status);
-      pop3->acknowledge = 0;
-      pop3->state = MU_POP3_RETR_ACK;
-
-    case MU_POP3_RETR_ACK:
-      status = mu_pop3_response (pop3, NULL, 0, NULL);
-      MU_POP3_CHECK_EAGAIN (pop3, status);
-      mu_pop3_debug_ack (pop3);
       MU_POP3_CHECK_OK (pop3);
       pop3->state = MU_POP3_RETR_RX;
 
     case MU_POP3_RETR_RX:
       status = mu_pop3_stream_create (pop3, pstream);
       MU_POP3_CHECK_ERROR (pop3, status);
+      pop3->state = MU_POP3_NO_STATE;
       break;
 
       /* They must deal with the error first by reopening.  */
diff --git a/libproto/pop/pop3_rset.c b/libproto/pop/pop3_rset.c
index 9ad5797..d233ec4 100644
--- a/libproto/pop/pop3_rset.c
+++ b/libproto/pop/pop3_rset.c
@@ -38,19 +38,12 @@ mu_pop3_rset (mu_pop3_t pop3)
     case MU_POP3_NO_STATE:
       status = mu_pop3_writeline (pop3, "RSET\r\n");
       MU_POP3_CHECK_ERROR (pop3, status);
-      mu_pop3_debug_cmd (pop3);
+      MU_POP3_FCLR (pop3, MU_POP3_ACK);
       pop3->state = MU_POP3_RSET;
 
     case MU_POP3_RSET:
-      status = mu_pop3_send (pop3);
+      status = mu_pop3_response (pop3, NULL);
       MU_POP3_CHECK_EAGAIN (pop3, status);
-      pop3->acknowledge = 0;
-      pop3->state = MU_POP3_RSET_ACK;
-
-    case MU_POP3_RSET_ACK:
-      status = mu_pop3_response (pop3, NULL, 0, NULL);
-      MU_POP3_CHECK_EAGAIN (pop3, status);
-      mu_pop3_debug_ack (pop3);
       MU_POP3_CHECK_OK (pop3);
       pop3->state = MU_POP3_NO_STATE;
       break;
diff --git a/libproto/pop/pop3_sendline.c b/libproto/pop/pop3_sendline.c
index e87c308..0de5e6a 100644
--- a/libproto/pop/pop3_sendline.c
+++ b/libproto/pop/pop3_sendline.c
@@ -31,91 +31,23 @@
 #include <errno.h>
 #include <mailutils/sys/pop3.h>
 
-/* A socket may write less then expected but stream.c:mu_stream_write() will
-   always try to send the entire buffer unless an error is reported.  We have
-   to cope with nonblocking, it is done by keeping track with the pop3->ptr
-   pointer if the write failed we keep track and restart where we left.  */
-int
-mu_pop3_send (mu_pop3_t pop3)
-{
-  int status = 0;
-  if (pop3->carrier && (pop3->io.ptr > pop3->io.buf))
-    {
-      size_t n = 0;
-      size_t len = pop3->io.ptr - pop3->io.buf;
-
-      /* Timeout with select(), note that we have to reset select()
-        since on linux tv is modified when error.  */
-      if (pop3->timeout)
-       {
-         int ready = mu_pop3_carrier_is_ready (pop3->carrier,
-                                               MU_STREAM_READY_WR,
-                                               pop3->timeout);
-         if (ready == 0)
-           return ETIMEDOUT;
-       }
-
-      status = mu_stream_write (pop3->carrier, pop3->io.buf, len, &n);
-      if (n)
-       {
-         /* Consume what we sent.  */
-         memmove (pop3->io.buf, pop3->io.buf + n, len - n);
-         pop3->io.ptr -= n;
-       }
-    }
-  else
-    pop3->io.ptr = pop3->io.buf;
-  return status;
-}
-
-/* According to RFC 2449: The maximum length of a command is increased from
-   47 characters (4 character command, single space, 40 character argument,
-   CRLF) to 255 octets, including the terminating CRLF.  But we are flexible
-   on this and realloc() as needed. NOTE: The terminated CRLF is not
-   included.  */
 int
 mu_pop3_writeline (mu_pop3_t pop3, const char *format, ...)
 {
-  int len;
+  int status;
   va_list ap;
-  int done = 1;
 
-  va_start(ap, format);
-  /* C99 says that a conforming implementation of snprintf () should
-     return the number of char that would have been call but many old
-     GNU/Linux && BSD implementations return -1 on error.  Worse,
-     QnX/Neutrino actually does not put the terminal null char.  So
-     let's try to cope.  */
-  do
-    {
-      len = vsnprintf (pop3->io.buf, pop3->io.len - 1, format, ap);
-      if (len < 0 || len >= (int)pop3->io.len
-         || !memchr (pop3->io.buf, '\0', len + 1))
-       {
-         pop3->io.len *= 2;
-         pop3->io.buf = realloc (pop3->io.buf, pop3->io.len);
-         if (pop3->io.buf == NULL)
-           return ENOMEM;
-         done = 0;
-       }
-      else
-       done = 1;
-    }
-  while (!done);
+  va_start (ap, format);
+  status = mu_stream_vprintf (pop3->carrier, format, ap);
   va_end(ap);
-  pop3->io.ptr = pop3->io.buf + len;
-  return 0;
+  return status;
 }
 
 int
 mu_pop3_sendline (mu_pop3_t pop3, const char *line)
 {
   if (line)
-    {
-      int status = mu_pop3_writeline (pop3, line);
-      if (status)
-       return status;
-    }
-  return mu_pop3_send (pop3);
+    return mu_stream_write (pop3->carrier, line, strlen (line), NULL);
+  return mu_stream_flush (pop3->carrier);
 }
 
diff --git a/libproto/pop/pop3_stat.c b/libproto/pop/pop3_stat.c
index 807d932..3dd5de0 100644
--- a/libproto/pop/pop3_stat.c
+++ b/libproto/pop/pop3_stat.c
@@ -42,26 +42,20 @@ mu_pop3_stat (mu_pop3_t pop3, unsigned *msg_count, size_t 
*size)
     case MU_POP3_NO_STATE:
       status = mu_pop3_writeline (pop3, "STAT\r\n");
       MU_POP3_CHECK_ERROR (pop3, status);
-      mu_pop3_debug_cmd (pop3);
+      MU_POP3_FCLR (pop3, MU_POP3_ACK);
       pop3->state = MU_POP3_STAT;
 
     case MU_POP3_STAT:
-      status = mu_pop3_send (pop3);
+      status = mu_pop3_response (pop3, NULL);
       MU_POP3_CHECK_EAGAIN (pop3, status);
-      pop3->acknowledge = 0;
-      pop3->state = MU_POP3_STAT_ACK;
-
-    case MU_POP3_STAT_ACK:
-      status = mu_pop3_response (pop3, NULL, 0, NULL);
-      MU_POP3_CHECK_EAGAIN (pop3, status);
-      mu_pop3_debug_ack (pop3);
       MU_POP3_CHECK_OK (pop3);
       pop3->state = MU_POP3_NO_STATE;
 
       /* Parse the answer.  */
       *msg_count = 0;
       lv = 0;
-      sscanf (pop3->ack.buf, "+OK %d %lu", msg_count, &lv);
+      /* FIXME: Error checking */
+      sscanf (pop3->ackbuf, "+OK %d %lu", msg_count, &lv);
       *size = lv;
       break;
 
diff --git a/libproto/pop/pop3_stls.c b/libproto/pop/pop3_stls.c
index 2c09222..ec31414 100644
--- a/libproto/pop/pop3_stls.c
+++ b/libproto/pop/pop3_stls.c
@@ -25,9 +25,29 @@
 #include <string.h>
 #include <errno.h>
 
+#include <mailutils/pop3.h>
 #include <mailutils/sys/pop3.h>
 #include <mailutils/tls.h>
-#include <mailutils/md5.h>
+#include <mailutils/list.h>
+
+static int
+pop3_swap_streams (mu_pop3_t pop3, mu_stream_t *streams)
+{
+  int rc;
+  
+  if (MU_POP3_FISSET (pop3, MU_POP3_TRACE))
+    rc = mu_stream_ioctl (pop3->carrier, MU_IOCTL_SWAP_STREAM, streams);
+  else if (streams[0] != streams[1])
+    rc = EINVAL;
+  else
+    {
+      mu_stream_t str = streams[0];
+      streams[0] = streams[1] = pop3->carrier;
+      pop3->carrier = str;
+      rc = 0;
+    }
+  return rc;
+}
 
 /*
  * STLS
@@ -38,49 +58,43 @@ mu_pop3_stls (mu_pop3_t pop3)
 {
 #ifdef WITH_TLS
   int status;
-
+  mu_stream_t tlsstream, streams[2];
+  
   /* Sanity checks.  */
   if (pop3 == NULL)
-    {
-      return EINVAL;
-    }
+    return EINVAL;
 
   switch (pop3->state)
     {
     case MU_POP3_NO_STATE:
       status = mu_pop3_writeline (pop3, "STLS\r\n");
       MU_POP3_CHECK_ERROR (pop3, status);
-      mu_pop3_debug_cmd (pop3);
+      MU_POP3_FCLR (pop3, MU_POP3_ACK);
       pop3->state = MU_POP3_STLS;
 
     case MU_POP3_STLS:
-      status = mu_pop3_send (pop3);
+      status = mu_pop3_response (pop3, NULL);
       MU_POP3_CHECK_EAGAIN (pop3, status);
-      pop3->acknowledge = 0;
-      pop3->state = MU_POP3_STLS_ACK;
-
-    case MU_POP3_STLS_ACK:
-      {
-        mu_stream_t tls_stream;
-        status = mu_pop3_response (pop3, NULL, 0, NULL);
-        MU_POP3_CHECK_EAGAIN (pop3, status);
-        mu_pop3_debug_ack (pop3);
-        MU_POP3_CHECK_OK (pop3);
-        status = mu_tls_client_stream_create (&tls_stream, 
-                                              pop3->carrier, 
-                                              pop3->carrier, 0);
-        MU_POP3_CHECK_ERROR (pop3, status);
-        pop3->carrier = tls_stream; 
-        pop3->state = MU_POP3_STLS_CONNECT;
-        break;
-      }
-
+      MU_POP3_CHECK_OK (pop3);
+      pop3->state = MU_POP3_STLS_CONNECT;
+      
     case MU_POP3_STLS_CONNECT:
-      status = mu_stream_open (pop3->carrier);
+      streams[0] = streams[1] = NULL;
+      status = pop3_swap_streams (pop3, streams);
+      MU_POP3_CHECK_EAGAIN (pop3, status);
+      status = mu_tls_client_stream_create (&tlsstream,
+                                           streams[0], streams[1], 0);
+      MU_POP3_CHECK_EAGAIN (pop3, status);
+      status = mu_stream_open (tlsstream);
       MU_POP3_CHECK_EAGAIN (pop3, status);
+      streams[0] = streams[1] = tlsstream;
+      status = pop3_swap_streams (pop3, streams);
+      MU_POP3_CHECK_EAGAIN (pop3, status);
+      /* Invalidate the capability list */
+      mu_list_destroy (&pop3->capa);
       pop3->state = MU_POP3_NO_STATE;
-      break;
-
+      return 0;
+      
       /* They must deal with the error first by reopening.  */
     case MU_POP3_ERROR:
       status = ECANCELED;
@@ -93,7 +107,6 @@ mu_pop3_stls (mu_pop3_t pop3)
 
   return status;
 #else
-  (void)pop3;
   return ENOTSUP;
 #endif
 }
diff --git a/libproto/pop/pop3_stream.c b/libproto/pop/pop3_stream.c
index d22588c..ae4024a 100644
--- a/libproto/pop/pop3_stream.c
+++ b/libproto/pop/pop3_stream.c
@@ -23,124 +23,242 @@
 #include <string.h>
 #include <errno.h>
 #include <stdlib.h>
+#include <mailutils/filter.h>
+#include <mailutils/stream.h>
+#include <mailutils/sys/stream.h>
 #include <mailutils/sys/pop3.h>
 
 /* Implementation of the stream for TOP and RETR.  */
 struct mu_pop3_stream
 {
+  struct _mu_stream stream;
   mu_pop3_t pop3;
   int done;
 };
 
-static void
-mu_pop3_stream_destroy (mu_stream_t stream)
+enum pop3_decode_state
+  {
+    pds_init,  /* initial state */
+    pds_cr,    /* prev. char was \r */
+    pds_crlf,  /* 2 prev. char were \r\n */
+    pds_dot,   /* 3 prev. chars were \r\n. */
+    pds_dotcr, /* 4 prev. chars were \r\n.\r */
+    pds_end    /* final state, a \r\n.\r\n seen. */
+  };
+
+static int
+newstate (int state, int c)
 {
-  struct mu_pop3_stream *pop3_stream = mu_stream_get_owner (stream);
-  if (pop3_stream)
+  switch (state)
     {
-      free (pop3_stream);
+    case pds_init:
+      switch (c)
+       {
+       case '\r':
+         return pds_cr;
+       }
+      break;
+      
+    case pds_cr:
+      switch (c)
+       {
+       case '\r':
+         return pds_cr;
+       case '\n':
+         return pds_crlf;
+       }
+      break;
+      
+    case pds_crlf:
+      switch (c)
+       {
+       case '\r':
+         return pds_cr;
+       case '.':
+         return pds_dot;
+       }
+      
+    case pds_dot:
+      switch (c)
+       {
+       case '\r':
+         return pds_dotcr;
+       }
+      break;
+
+    case pds_dotcr:
+      switch (c)
+       {
+       case '\n':
+         return pds_end;
+       }
+    }
+  return pds_init;
+}
+
+/* Move min(isize,osize) bytes from iptr to optr, replacing each \r\n
+   with \n. */
+static enum mu_filter_result
+_pop3_decoder (void *xd,
+              enum mu_filter_command cmd,
+              struct mu_filter_io *iobuf)
+{
+  int *pstate = xd;
+  size_t i, j;
+  const unsigned char *iptr;
+  size_t isize;
+  char *optr;
+  size_t osize;
+
+  switch (cmd)
+    {
+    case mu_filter_init:
+      *pstate = pds_init;
+      return mu_filter_ok;
+      
+    case mu_filter_done:
+      return mu_filter_ok;
+      
+    default:
+      break;
+    }
+  
+  iptr = (const unsigned char *) iobuf->input;
+  isize = iobuf->isize;
+  optr = iobuf->output;
+  osize = iobuf->osize;
+
+  for (i = j = 0; *pstate != pds_end && i < isize && j < osize; i++)
+    {
+      unsigned char c = *iptr++;
+
+      if (c == '\r')
+       {
+         if (i + 1 == isize)
+           break;
+         *pstate = newstate (*pstate, c);
+         if (*iptr == '\n')
+           continue;
+       }
+      else if (c == '.' && *pstate == pds_crlf)
+       {
+         if (i + 1 == isize)
+           break;
+         *pstate = newstate (*pstate, c);
+         if (*iptr != '\r')
+           continue;
+       }
+      else
+       *pstate = newstate (*pstate, c);
+      optr[j++] = c;
     }
+  
+  if (*pstate == pds_end)
+    iobuf->eof = 1;
+  iobuf->isize = i;
+  iobuf->osize = j;
+  return mu_filter_ok;
+}
+
+static int
+mu_pop3_filter_create (mu_stream_t *pstream, mu_stream_t stream)
+{
+  int *state = malloc (sizeof (*state));
+  if (!state)
+    return ENOMEM;
+  return mu_filter_stream_create (pstream, stream,
+                                 MU_FILTER_DECODE,
+                                 _pop3_decoder, state,
+                                 MU_STREAM_READ);
 }
 
+
 static int
-mu_pop3_stream_read (mu_stream_t stream, char *buf, size_t buflen, mu_off_t 
offset, size_t *pn)
+_mu_pop3_read (struct _mu_stream *str, char *buf, size_t bufsize,
+              size_t *pnread)
 {
-  struct mu_pop3_stream *pop3_stream = mu_stream_get_owner (stream);
-  size_t n = 0;
+  struct mu_pop3_stream *sp = (struct mu_pop3_stream *)str;
+  mu_pop3_t pop3 = sp->pop3;
+  size_t nread;
   int status = 0;
-  char *p = buf;
 
-  (void)offset;
-  if (pop3_stream)
+  if (sp->done)
+    nread = 0;
+  else
     {
-      if (!pop3_stream->done)
+      status = mu_stream_read (pop3->carrier, buf, bufsize, &nread);
+      if (status == 0 && nread == 0)
        {
-         do
-           {
-             size_t nread = 0;
-
-             /* The pop3_readline () function will always read one less to
-                be able to null terminate the buffer, this will cause
-                serious grief for mu_stream_read() where it is legitimate to
-                have a buffer of 1 char.  So we must catch it here.  */
-             if (buflen == 1)
-               {
-                 char buffer[2];
-                 *buffer = '\0';
-                 status = mu_pop3_readline (pop3_stream->pop3, buffer, 2, 
&nread);
-                 *p = *buffer;
-               }
-             else
-               status = mu_pop3_readline (pop3_stream->pop3, p, buflen, 
&nread);
-
-             if (status != 0)
-               break;
-             if (nread == 0)
-               {
-                 pop3_stream->pop3->state = MU_POP3_NO_STATE;
-                 pop3_stream->done = 1;
-                 break;
-               }
-             n += nread;
-             buflen -= nread;
-             p += nread;
-           }
-         while (buflen > 0);
+         pop3->state = MU_POP3_NO_STATE;
+         sp->done = 1;
        }
     }
-  if (pn)
-    *pn = n;
+  *pnread = nread;
   return status;
 }
 
 static int
-mu_pop3_stream_readline (mu_stream_t stream, char *buf, size_t buflen, 
mu_off_t offset, size_t *pn)
+_mu_pop3_readdelim (struct _mu_stream *str, char *buf, size_t bufsize,
+                   int delim, size_t *pnread)
 {
-  struct mu_pop3_stream *pop3_stream = mu_stream_get_owner (stream);
-  size_t n = 0;
+  struct mu_pop3_stream *sp = (struct mu_pop3_stream *)str;
+  mu_pop3_t pop3 = sp->pop3;
+  size_t nread;
   int status = 0;
-
-  (void)offset;
-  if (pop3_stream)
+  
+  if (sp->done)
+    nread = 0;
+  else
     {
-      if (!pop3_stream->done)
+      status = mu_stream_readdelim (pop3->carrier, buf, bufsize, delim,
+                                   &nread);
+      if (status == 0 && nread == 0)
        {
-         status = mu_pop3_readline (pop3_stream->pop3, buf, buflen, &n);
-         if (n == 0)
-           {
-             pop3_stream->pop3->state = MU_POP3_NO_STATE;
-             pop3_stream->done = 1;
-           }
+         pop3->state = MU_POP3_NO_STATE;
+         sp->done = 1;
        }
     }
-  if (pn)
-    *pn = n;
+  *pnread = nread;
   return status;
 }
 
+static int
+_mu_pop3_flush (struct _mu_stream *str)
+{
+  struct mu_pop3_stream *sp = (struct mu_pop3_stream *)str;
+  mu_pop3_t pop3 = sp->pop3;
+  return mu_stream_flush (pop3->carrier);
+}
+
+static int
+_mu_pop3_wait (struct _mu_stream *str, int *pflags, struct timeval *tvp)
+{
+  struct mu_pop3_stream *sp = (struct mu_pop3_stream *)str;
+  mu_pop3_t pop3 = sp->pop3;
+  return mu_stream_wait (pop3->carrier, pflags, tvp);
+}
+
 int
 mu_pop3_stream_create (mu_pop3_t pop3, mu_stream_t *pstream)
 {
-  struct mu_pop3_stream *pop3_stream;
+  struct mu_pop3_stream *sp;
+  mu_stream_t str;
   int status;
-
-  pop3_stream = malloc (sizeof *pop3_stream);
-  if (pop3_stream == NULL)
+  
+  sp = (struct mu_pop3_stream *) _mu_stream_create (sizeof (*sp),
+                                                   MU_STREAM_READ);
+  if (!sp)
     return ENOMEM;
+  sp->stream.read = _mu_pop3_read; 
+  sp->stream.readdelim = _mu_pop3_readdelim; 
+  sp->stream.flush = _mu_pop3_flush;
+  sp->stream.wait = _mu_pop3_wait;
+  sp->pop3 = pop3;
+  sp->done = 0;
+  str = (mu_stream_t) sp;
+  mu_stream_set_buffer (str, mu_buffer_line, 1024);
 
-  pop3_stream->pop3 = pop3;
-  pop3_stream->done = 0;
-
-  status = mu_stream_create (pstream, MU_STREAM_READ, pop3_stream);
-  if (status != 0)
-    {
-      free (pop3_stream);
-      return status;
-    }
- 
-  mu_stream_set_read (*pstream, mu_pop3_stream_read, pop3_stream);
-  mu_stream_set_readline (*pstream, mu_pop3_stream_readline, pop3_stream);
-  mu_stream_set_destroy (*pstream, mu_pop3_stream_destroy, pop3_stream);
-                                                                               
                                              
-  return 0;
+  status = mu_pop3_filter_create (pstream, str);
+  mu_stream_unref (str);
+  return status;
 }
diff --git a/libproto/pop/pop3_top.c b/libproto/pop/pop3_top.c
index 4057bb1..0ca0379 100644
--- a/libproto/pop/pop3_top.c
+++ b/libproto/pop/pop3_top.c
@@ -26,7 +26,8 @@
 #include <mailutils/sys/pop3.h>
 
 int
-mu_pop3_top (mu_pop3_t pop3, unsigned msgno, unsigned int lines, mu_stream_t 
*pstream)
+mu_pop3_top (mu_pop3_t pop3, unsigned msgno, unsigned int lines,
+            mu_stream_t *pstream)
 {
   int status;
 
@@ -40,25 +41,19 @@ mu_pop3_top (mu_pop3_t pop3, unsigned msgno, unsigned int 
lines, mu_stream_t *ps
     case MU_POP3_NO_STATE:
       status = mu_pop3_writeline (pop3, "TOP %d %d\r\n", msgno, lines);
       MU_POP3_CHECK_ERROR (pop3, status);
-      mu_pop3_debug_cmd (pop3);
+      MU_POP3_FCLR (pop3, MU_POP3_ACK);
       pop3->state = MU_POP3_TOP;
 
     case MU_POP3_TOP:
-      status = mu_pop3_send (pop3);
+      status = mu_pop3_response (pop3, NULL);
       MU_POP3_CHECK_EAGAIN (pop3, status);
-      pop3->acknowledge = 0;
-      pop3->state = MU_POP3_TOP_ACK;
-
-    case MU_POP3_TOP_ACK:
-      status = mu_pop3_response (pop3, NULL, 0, NULL);
-      MU_POP3_CHECK_EAGAIN (pop3, status);
-      mu_pop3_debug_ack (pop3);
       MU_POP3_CHECK_OK (pop3);
       pop3->state = MU_POP3_TOP_RX;
 
     case MU_POP3_TOP_RX:
       status = mu_pop3_stream_create (pop3, pstream);
       MU_POP3_CHECK_ERROR (pop3, status);
+      pop3->state = MU_POP3_NO_STATE;
       break;
 
       /* They must deal with the error first by reopening.  */
diff --git a/libproto/pop/pop3_trace.c b/libproto/pop/pop3_trace.c
new file mode 100644
index 0000000..1a4c27f
--- /dev/null
+++ b/libproto/pop/pop3_trace.c
@@ -0,0 +1,118 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 2007, 2009, 2010 Free Software
+   Foundation, Inc.
+
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 3 of the License, or (at your option) any later version.
+
+   This library 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
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General
+   Public License along with this library; if not, write to the
+   Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+   Boston, MA 02110-1301 USA */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <string.h>
+#include <errno.h>
+#include <stdio.h>
+#include <mailutils/error.h>
+#include <mailutils/nls.h>
+#include <mailutils/stream.h>
+#include <mailutils/sys/pop3.h>
+
+static const char *pop3_prefix[] = {
+  "S: ", "C: "
+};
+
+int
+_mu_pop3_trace_enable (mu_pop3_t pop3)
+{
+  int rc = 0;
+  mu_debug_t debug;
+  mu_stream_t dstr, xstr;
+
+  if (!pop3->carrier)
+    {
+      MU_POP3_FSET (pop3, MU_POP3_TRACE);
+      return 0;
+    }
+  
+  mu_diag_get_debug (&debug);
+  
+  rc = mu_dbgstream_create (&dstr, debug, MU_DIAG_DEBUG, 0);
+  if (rc)
+    mu_error (_("cannot create debug stream; transcript disabled: %s"),
+             mu_strerror (rc));
+  else
+    {
+      rc = mu_xscript_stream_create (&xstr, pop3->carrier, dstr,
+                                    pop3_prefix);
+      if (rc)
+       mu_error (_("cannot create transcript stream: %s"),
+                 mu_strerror (rc));
+      else
+       {
+         mu_stream_unref (pop3->carrier);
+         pop3->carrier = xstr;
+         MU_POP3_FSET (pop3, MU_POP3_TRACE);
+       }
+    }
+
+  return rc;
+}
+
+int
+_mu_pop3_trace_disable (mu_pop3_t pop3)
+{
+  mu_stream_t xstr = pop3->carrier;
+  mu_stream_t stream[2];
+  int rc;
+
+  if (!xstr)
+    return 0;
+  
+  rc = mu_stream_ioctl (xstr, MU_IOCTL_GET_TRANSPORT, stream);
+  if (rc)
+    return rc;
+
+  pop3->carrier = stream[0];
+  mu_stream_destroy (&xstr);
+  MU_POP3_FCLR (pop3, MU_POP3_TRACE);
+  return 0;
+}
+
+int
+mu_pop3_trace (mu_pop3_t pop3, int op)
+{
+  int trace_on = MU_POP3_FISSET (pop3, MU_POP3_TRACE);
+  
+  switch (op)
+    {
+    case MU_POP3_TRACE_SET:
+      if (trace_on)
+       return MU_ERR_EXISTS;
+      return _mu_pop3_trace_enable (pop3);
+      
+    case MU_POP3_TRACE_CLR:
+      if (!trace_on)
+       return MU_ERR_NOENT;
+      return _mu_pop3_trace_disable (pop3);
+
+    case MU_POP3_TRACE_QRY:
+      if (!trace_on)
+       return MU_ERR_NOENT;
+      return 0;
+    }
+  return EINVAL;
+}
+
+  
diff --git a/libproto/pop/pop3_uidl.c b/libproto/pop/pop3_uidl.c
index 71c3ec3..c97cc32 100644
--- a/libproto/pop/pop3_uidl.c
+++ b/libproto/pop/pop3_uidl.c
@@ -30,6 +30,7 @@ int
 mu_pop3_uidl (mu_pop3_t pop3, unsigned int msgno, char **uidl)
 {
   int status;
+  char *space;
 
   if (pop3 == NULL)
     return EINVAL;
@@ -41,52 +42,44 @@ mu_pop3_uidl (mu_pop3_t pop3, unsigned int msgno, char 
**uidl)
     case MU_POP3_NO_STATE:
       status = mu_pop3_writeline (pop3, "UIDL %d\r\n", msgno);
       MU_POP3_CHECK_ERROR (pop3, status);
-      mu_pop3_debug_cmd (pop3);
+      MU_POP3_FCLR (pop3, MU_POP3_ACK);
       pop3->state = MU_POP3_UIDL;
 
     case MU_POP3_UIDL:
-      status = mu_pop3_send (pop3);
+      status = mu_pop3_response (pop3, NULL);
       MU_POP3_CHECK_EAGAIN (pop3, status);
-      pop3->acknowledge = 0;
-      pop3->state = MU_POP3_UIDL_ACK;
-
-    case MU_POP3_UIDL_ACK:
-      status = mu_pop3_response (pop3, NULL, 0, NULL);
-      MU_POP3_CHECK_EAGAIN (pop3, status);
-      mu_pop3_debug_ack (pop3);
       MU_POP3_CHECK_OK (pop3);
       pop3->state = MU_POP3_NO_STATE;
 
       *uidl = NULL;
-      {
-       char *space;
-       /* Format:  +OK msgno uidlstring  */
-
-       /* Pass the "+OK".  */
-       space = strchr (pop3->ack.buf, ' ');
-       if (space)
-         {
-           /* Skip spaces.  */
-           while (*space == ' ') space++;
-           /* Pass the number.  */
-           space = strchr (space, ' ');
-           if (space)
-             {
-               size_t len;
-               /* Skip spaces between msgno and uidlstring  */
-               while (*space == ' ') space++;
-               len = strlen (space);
-               if (space[len - 1] == '\n')
-                 {
-                   space[len - 1] = '\0';
-                   len--;
-                 }
-               *uidl = calloc (len + 1, 1);
-               if (*uidl)
-                 memcpy (*uidl, space, len);
-             }
-         }
-      }
+
+      /* Format:  +OK msgno uidlstring  */
+
+      /* Pass the "+OK".  */
+      space = strchr (pop3->ackbuf, ' ');
+      if (space)
+       {
+         /* Skip spaces.  */
+         while (*space == ' ') space++;
+         /* Pass the number.  */
+         space = strchr (space, ' ');
+         if (space)
+           {
+             size_t len;
+             /* Skip spaces between msgno and uidlstring  */
+             while (*space == ' ') space++;
+             len = strlen (space);
+             if (space[len - 1] == '\n')
+               {
+                 space[len - 1] = '\0';
+                 len--;
+               }
+             *uidl = calloc (len + 1, 1);
+             if (*uidl)
+               memcpy (*uidl, space, len);
+           }
+       }
+
       if (*uidl == NULL) /* What can we do?  */
        {
          *uidl = malloc (1);
diff --git a/libproto/pop/pop3_uidla.c b/libproto/pop/pop3_uidla.c
index f572e7f..3054b6b 100644
--- a/libproto/pop/pop3_uidla.c
+++ b/libproto/pop/pop3_uidla.c
@@ -40,26 +40,20 @@ mu_pop3_uidl_all (mu_pop3_t pop3, mu_iterator_t *piterator)
     case MU_POP3_NO_STATE:
       status = mu_pop3_writeline (pop3, "UIDL\r\n");
       MU_POP3_CHECK_ERROR (pop3, status);
-      mu_pop3_debug_cmd (pop3);
+      MU_POP3_FCLR (pop3, MU_POP3_ACK);
       pop3->state = MU_POP3_UIDL;
 
     case MU_POP3_UIDL:
-      status = mu_pop3_send (pop3);
+      status = mu_pop3_response (pop3, NULL);
       MU_POP3_CHECK_EAGAIN (pop3, status);
-      pop3->acknowledge = 0;
-      pop3->state = MU_POP3_UIDL_ACK;
-
-    case MU_POP3_UIDL_ACK:
-      status = mu_pop3_response (pop3, NULL, 0, NULL);
-      MU_POP3_CHECK_EAGAIN (pop3, status);
-      mu_pop3_debug_ack (pop3);
       MU_POP3_CHECK_OK (pop3);
       status = mu_pop3_iterator_create (pop3, piterator);
       MU_POP3_CHECK_ERROR (pop3, status);
       pop3->state = MU_POP3_UIDL_RX;
 
     case MU_POP3_UIDL_RX:
-      /* The mu_iterator_t will read the stream and set the state to 
MU_POP3_NO_STATE when done.  */
+      /* The mu_iterator_t will read the stream and set the state to
+        MU_POP3_NO_STATE when done.  */
       break;
 
       /* They must deal with the error first by reopening.  */
diff --git a/libproto/pop/pop3_user.c b/libproto/pop/pop3_user.c
index 334050f..5298a42 100644
--- a/libproto/pop/pop3_user.c
+++ b/libproto/pop/pop3_user.c
@@ -37,19 +37,12 @@ mu_pop3_user (mu_pop3_t pop3, const char *user)
     case MU_POP3_NO_STATE:
       status = mu_pop3_writeline (pop3, "USER %s\r\n", user);
       MU_POP3_CHECK_ERROR (pop3, status);
-      mu_pop3_debug_cmd (pop3);
+      MU_POP3_FCLR (pop3, MU_POP3_ACK);
       pop3->state = MU_POP3_USER;
 
     case MU_POP3_USER:
-      status = mu_pop3_send (pop3);
+      status = mu_pop3_response (pop3, NULL);
       MU_POP3_CHECK_EAGAIN (pop3, status);
-      pop3->acknowledge = 0;
-      pop3->state = MU_POP3_USER_ACK;
-
-    case MU_POP3_USER_ACK:
-      status = mu_pop3_response (pop3, NULL, 0, NULL);
-      MU_POP3_CHECK_EAGAIN (pop3, status);
-      mu_pop3_debug_ack (pop3);
       MU_POP3_CHECK_OK (pop3);
       pop3->state = MU_POP3_NO_STATE;
       break;
diff --git a/libproto/pop/url.c b/libproto/pop/url.c
index 1f112bc..e98b3a6 100644
--- a/libproto/pop/url.c
+++ b/libproto/pop/url.c
@@ -31,8 +31,8 @@
 # include <strings.h>
 #endif
 
-#include <url0.h>
-#include <registrar0.h>
+#include <mailutils/sys/url.h>
+#include <mailutils/sys/registrar.h>
 
 static void url_pop_destroy (mu_url_t url);
 
diff --git a/mailbox/amd.c b/mailbox/amd.c
index 06b92e2..a263178 100644
--- a/mailbox/amd.c
+++ b/mailbox/amd.c
@@ -69,10 +69,10 @@
 #include <mailutils/url.h>
 #include <mailutils/observer.h>
 #include <mailutils/sys/stream.h>
-#include <mailbox0.h>
-#include <registrar0.h>
-#include <url0.h>
-#include <amd.h>
+#include <mailutils/sys/mailbox.h>
+#include <mailutils/sys/registrar.h>
+#include <mailutils/sys/url.h>
+#include <mailutils/sys/amd.h>
 
 static void amd_destroy (mu_mailbox_t mailbox);
 static int amd_open (mu_mailbox_t, int);
diff --git a/mailbox/assoc.c b/mailbox/assoc.c
index 418fa08..a0c13a0 100644
--- a/mailbox/assoc.c
+++ b/mailbox/assoc.c
@@ -29,7 +29,7 @@
 #include <mailutils/iterator.h>
 #include <mailutils/mutil.h>
 #include <mailutils/cstr.h>
-#include <iterator0.h>
+#include <mailutils/sys/iterator.h>
 
 /* |hash_size| defines a sequence of symbol table sizes. These are prime
    numbers, the distance between each pair of them grows exponentially,
diff --git a/mailbox/attribute.c b/mailbox/attribute.c
index d5c5d67..3e76855 100644
--- a/mailbox/attribute.c
+++ b/mailbox/attribute.c
@@ -33,7 +33,7 @@
 #include <mailutils/errno.h>
 #include <mailutils/mutil.h>
 #include <mailutils/cstr.h>
-#include <attribute0.h>
+#include <mailutils/sys/attribute.h>
 
 int
 mu_attribute_create (mu_attribute_t *pattr, void *owner)
diff --git a/mailbox/auth.c b/mailbox/auth.c
index 7975050..ad566a3 100644
--- a/mailbox/auth.c
+++ b/mailbox/auth.c
@@ -27,7 +27,7 @@
 #include <stdlib.h>
 
 #include <mailutils/errno.h>
-#include <auth0.h>
+#include <mailutils/sys/auth.h>
 
 static int
 _authenticate_null (mu_authority_t auth MU_ARG_UNUSED)
diff --git a/mailbox/body.c b/mailbox/body.c
index 8c5dba1..038d29a 100644
--- a/mailbox/body.c
+++ b/mailbox/body.c
@@ -34,7 +34,7 @@
 #include <mailutils/mutil.h>
 #include <mailutils/errno.h>
 #include <mailutils/sys/stream.h>
-#include <body0.h>
+#include <mailutils/sys/body.h>
 
 #define BODY_MODIFIED 0x10000
 
diff --git a/mailbox/debug.c b/mailbox/debug.c
index 93fab7c..7570f23 100644
--- a/mailbox/debug.c
+++ b/mailbox/debug.c
@@ -28,7 +28,7 @@
 
 #include <mailutils/errno.h>
 #include <mailutils/nls.h>
-#include <debug0.h>
+#include <mailutils/sys/debug.h>
 
 mu_debug_printer_fp mu_debug_default_printer = mu_debug_stderr_printer;
 
diff --git a/mailbox/envelope.c b/mailbox/envelope.c
index 21facf7..1e5aa4a 100644
--- a/mailbox/envelope.c
+++ b/mailbox/envelope.c
@@ -25,7 +25,7 @@
 #include <string.h>
 #include <mailutils/errno.h>
 #include <mailutils/mutil.h>
-#include <envelope0.h>
+#include <mailutils/sys/envelope.h>
 
 int
 mu_envelope_create (mu_envelope_t *penvelope, void *owner)
diff --git a/mailbox/fltstream.c b/mailbox/fltstream.c
index 38a114f..3190c2c 100644
--- a/mailbox/fltstream.c
+++ b/mailbox/fltstream.c
@@ -49,6 +49,8 @@ init_iobuf (struct mu_filter_io *io, struct _mu_filter_stream 
*fs)
   io->isize = MFB_LEVEL (fs->inbuf);
   io->output = MFB_ENDPTR (fs->outbuf);
   io->osize = MFB_FREESIZE (fs->outbuf);
+  io->errcode = 0;
+  io->eof = 0;
 }
 
 static int
@@ -114,8 +116,9 @@ filter_read (mu_stream_t stream, char *buf, size_t size, 
size_t *pret)
   size_t min_input_level = MU_FILTER_BUF_SIZE;
   size_t min_output_size = MU_FILTER_BUF_SIZE;
   size_t total = 0;
+  int stop = 0;
   
-  while (total < size && cmd != mu_filter_lastbuf)
+  while (!stop && total < size && cmd != mu_filter_lastbuf)
     {
       size_t rdsize;
 
@@ -168,6 +171,11 @@ filter_read (mu_stream_t stream, char *buf, size_t size, 
size_t *pret)
              if (iobuf.isize > MFB_RDBYTES (fs->inbuf)
                  || iobuf.osize > MFB_FREESIZE (fs->outbuf))
                return MU_ERR_FAILURE; /* FIXME: special error code? */
+             if (iobuf.eof)
+               {
+                 stream->flags |= _MU_STR_EOF;
+                 stop = 1;
+               }
              break;
          
            case mu_filter_falure:
diff --git a/mailbox/folder.c b/mailbox/folder.c
index 39f5d41..3ce430a 100644
--- a/mailbox/folder.c
+++ b/mailbox/folder.c
@@ -37,7 +37,7 @@
 #include <mailutils/url.h>
 #include <mailutils/errno.h>
 
-#include <folder0.h>
+#include <mailutils/sys/folder.h>
 
 /* Internal folder list.  */
 static mu_list_t known_folder_list;
diff --git a/mailbox/iterator.c b/mailbox/iterator.c
index aba94ab..3897925 100644
--- a/mailbox/iterator.c
+++ b/mailbox/iterator.c
@@ -24,8 +24,8 @@
 #include <errno.h>
 #include <stdlib.h>
 
-#include <list0.h>
-#include <iterator0.h>
+#include <mailutils/sys/list.h>
+#include <mailutils/sys/iterator.h>
 #include <mailutils/errno.h>
 
 int
diff --git a/mailbox/list.c b/mailbox/list.c
index f47af25..91f6a6e 100644
--- a/mailbox/list.c
+++ b/mailbox/list.c
@@ -25,8 +25,8 @@
 #include <stdlib.h>
 #include <string.h>
 
-#include <list0.h>
-#include <iterator0.h>
+#include <mailutils/sys/list.h>
+#include <mailutils/sys/iterator.h>
 #include <mailutils/errno.h>
 
 #define DESTROY_ITEM(list, elt)                        \
diff --git a/mailbox/listlist.c b/mailbox/listlist.c
index 060c724..94172ac 100644
--- a/mailbox/listlist.c
+++ b/mailbox/listlist.c
@@ -25,8 +25,8 @@
 #include <stdlib.h>
 #include <string.h>
 
-#include <list0.h>
-#include <iterator0.h>
+#include <mailutils/sys/list.h>
+#include <mailutils/sys/iterator.h>
 #include <mailutils/errno.h>
 
 void
diff --git a/mailbox/mailbox.c b/mailbox/mailbox.c
index 17a4b0a..e7c0dcc 100644
--- a/mailbox/mailbox.c
+++ b/mailbox/mailbox.c
@@ -41,8 +41,8 @@
 #include <mailutils/message.h>
 #include <mailutils/mutil.h>
 
-#include <mailbox0.h>
-#include <url0.h>
+#include <mailutils/sys/mailbox.h>
+#include <mailutils/sys/url.h>
 
 static int
 mailbox_folder_create (mu_mailbox_t mbox, const char *name,
diff --git a/mailbox/mailer.c b/mailbox/mailer.c
index 553898b..2240b65 100644
--- a/mailbox/mailer.c
+++ b/mailbox/mailer.c
@@ -50,7 +50,7 @@
 #include <mailutils/mime.h>
 #include <mailutils/io.h>
 
-#include <mailer0.h>
+#include <mailutils/sys/mailer.h>
 
 static char *mailer_url_default;
 
diff --git a/mailbox/mbx_default.c b/mailbox/mbx_default.c
index e970d15..348e7f7 100644
--- a/mailbox/mbx_default.c
+++ b/mailbox/mbx_default.c
@@ -40,7 +40,7 @@
 #include <mailutils/folder.h>
 #include <mailutils/auth.h>
 
-#include <mailbox0.h>
+#include <mailutils/sys/mailbox.h>
 
 char *mu_ticket_file = "~/.mu-tickets";
 
diff --git a/mailbox/mbxitr.c b/mailbox/mbxitr.c
index 6137e12..8463201 100644
--- a/mailbox/mbxitr.c
+++ b/mailbox/mbxitr.c
@@ -29,7 +29,7 @@
 #include <mailutils/error.h>
 #include <mailutils/iterator.h>
 
-#include <mailbox0.h>
+#include <mailutils/sys/mailbox.h>
 
 struct mailbox_iterator
 {
diff --git a/mailbox/monitor.c b/mailbox/monitor.c
index 55c94a9..45e7dc9 100644
--- a/mailbox/monitor.c
+++ b/mailbox/monitor.c
@@ -34,7 +34,7 @@
 #include <errno.h>
 #include <stdlib.h>
 
-#include <monitor0.h>
+#include <mailutils/sys/monitor.h>
 #include <mailutils/errno.h>
 
 #ifdef WITH_PTHREAD
diff --git a/mailbox/mutil.c b/mailbox/mutil.c
index 1572c8f..aa11d56 100644
--- a/mailbox/mutil.c
+++ b/mailbox/mutil.c
@@ -64,7 +64,7 @@
 #include <mailutils/cctype.h>
 #include <mailutils/cstr.h>
 
-#include <registrar0.h>
+#include <mailutils/sys/registrar.h>
 
 /* convert a sequence of hex characters into an integer */
 
diff --git a/mailbox/observer.c b/mailbox/observer.c
index 355a4fa..51b0260 100644
--- a/mailbox/observer.c
+++ b/mailbox/observer.c
@@ -27,7 +27,7 @@
 #include <mailutils/list.h>
 #include <mailutils/iterator.h>
 #include <mailutils/errno.h>
-#include <observer0.h>
+#include <mailutils/sys/observer.h>
 
 int
 mu_observer_create (mu_observer_t *pobserver, void *owner)
diff --git a/mailbox/property.c b/mailbox/property.c
index e5980b9..435d8e2 100644
--- a/mailbox/property.c
+++ b/mailbox/property.c
@@ -24,7 +24,7 @@
 # include <strings.h>
 #endif
 
-#include <property0.h>
+#include <mailutils/sys/property.h>
 #include <mailutils/errno.h>
 #include <mailutils/assoc.h>
 #include <stdlib.h>
diff --git a/mailbox/registrar.c b/mailbox/registrar.c
index 1f6cc6f..a1a2b4f 100644
--- a/mailbox/registrar.c
+++ b/mailbox/registrar.c
@@ -36,7 +36,7 @@
 #include <mailutils/error.h>
 #include <mailutils/url.h>
 #include <mailutils/mutil.h>
-#include <registrar0.h>
+#include <mailutils/sys/registrar.h>
 
 /* NOTE: We will leak here since the monitor and the registrar will never
    be released. That's ok we can live with this, it's only done once.  */
diff --git a/mailbox/ticket.c b/mailbox/ticket.c
index 774b711..ae49520 100644
--- a/mailbox/ticket.c
+++ b/mailbox/ticket.c
@@ -32,7 +32,7 @@
 #include <mailutils/mutil.h>
 #include <mailutils/errno.h>
 #include <mailutils/secret.h>
-#include <auth0.h>
+#include <mailutils/sys/auth.h>
 
 static void
 echo_off (struct termios *stored_settings)
diff --git a/mailbox/url.c b/mailbox/url.c
index 5580f8c..9a5d2de 100644
--- a/mailbox/url.c
+++ b/mailbox/url.c
@@ -35,7 +35,7 @@
 #include <mailutils/secret.h>
 #include <mailutils/cctype.h>
 #include <mailutils/cstr.h>
-#include <url0.h>
+#include <mailutils/sys/url.h>
 
 #define AC2(a,b) a ## b
 #define AC4(a,b,c,d) a ## b ## c ## d
diff --git a/mailbox/wicket.c b/mailbox/wicket.c
index 676970a..82b179b 100644
--- a/mailbox/wicket.c
+++ b/mailbox/wicket.c
@@ -36,8 +36,8 @@
 #include <mailutils/mu_auth.h>
 #include <mailutils/stream.h>
 
-#include <auth0.h>
-#include <url0.h>
+#include <mailutils/sys/auth.h>
+#include <mailutils/sys/url.h>
 
 int
 mu_wicket_create (mu_wicket_t *pwicket)
diff --git a/mailbox/xscript-stream.c b/mailbox/xscript-stream.c
index 64a5734..51e2016 100644
--- a/mailbox/xscript-stream.c
+++ b/mailbox/xscript-stream.c
@@ -176,6 +176,7 @@ _xscript_ctl (struct _mu_stream *str, int op, void *arg)
 {
   struct _mu_xscript_stream *sp = (struct _mu_xscript_stream *)str;
   mu_transport_t *ptrans;
+  int status = 0;
   
   switch (op)
     {
@@ -198,11 +199,37 @@ _xscript_ctl (struct _mu_stream *str, int op, void *arg)
       break;
 
     case MU_IOCTL_SWAP_STREAM:
-      /* fall through */
+      if (!arg)
+       return EINVAL;
+      status = mu_stream_ioctl (sp->transport, op, arg);
+      if (status == EINVAL || status == ENOSYS)
+       {
+         mu_stream_t *pstr = arg;
+         mu_stream_t tmp;
+
+         if (pstr[0] != pstr[1])
+           return EINVAL; /* FIXME */
+         tmp = pstr[0];
+         pstr[0] = sp->transport;
+         pstr[1] = sp->transport;
+         sp->transport = tmp;
+         if (!(str->flags & MU_STREAM_AUTOCLOSE))
+           {
+             if (pstr[0])
+               mu_stream_unref (pstr[0]);
+             if (tmp)
+               mu_stream_ref (tmp);
+           }
+         if (tmp)
+           mu_stream_ref (tmp);
+         status = 0;
+       }
+      break;
+      
     default:
       return mu_stream_ioctl (sp->transport, op, arg);
     }
-  return 0;
+  return status;
 }
 
 static int
diff --git a/pop3d/pop3d.h b/pop3d/pop3d.h
index f77652f..d10a330 100644
--- a/pop3d/pop3d.h
+++ b/pop3d/pop3d.h
@@ -67,16 +67,9 @@ extern int expire_on_exit;
 /* Size of the MD5 digest for APOP */
 #define APOP_DIGEST    70
 
-/* Longest legal POP command */
-#define POP_MAXCMDLEN  255
-
-/* Buffer size to use for output */
-#define BUFFERSIZE     1024
-
 #ifndef _GNU_SOURCE
 # define _GNU_SOURCE
 #endif
-#define _QNX_SOURCE
 #include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>


hooks/post-receive
-- 
GNU Mailutils



reply via email to

[Prev in Thread] Current Thread [Next in Thread]