From MAILER-DAEMON Sun Oct 03 14:52:32 2010
Received: from mailman by lists.gnu.org with archive (Exim 4.43)
	id 1P2Tfj-0005Gz-Uu
	for mharc-commit-mailutils@gnu.org; Sun, 03 Oct 2010 14:52:32 -0400
Received: from [140.186.70.92] (port=51138 helo=eggs.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.43) id 1P2TfZ-0005Bh-Jd
	for commit-mailutils@gnu.org; Sun, 03 Oct 2010 14:52:30 -0400
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P2TfR-000675-NG
	for commit-mailutils@gnu.org; Sun, 03 Oct 2010 14:52:21 -0400
Received: from colonialone.fsf.org ([140.186.70.51]:60546
	helo=internal.in.savannah.gnu.org)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P2TfR-000671-HK
	for commit-mailutils@gnu.org; Sun, 03 Oct 2010 14:52:13 -0400
Received: from [10.1.0.108] (helo=vcs-noshell.in.savannah.gnu.org)
	by internal.in.savannah.gnu.org with esmtp (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P2TfR-0002Ui-9j
	for commit-mailutils@gnu.org; Sun, 03 Oct 2010 18:52:13 +0000
Received: from gray by vcs-noshell.in.savannah.gnu.org with local (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P2TfQ-0001SI-Hq
	for commit-mailutils@gnu.org; Sun, 03 Oct 2010 18:52:13 +0000
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: 9d1a990614d460252b6217cb4f98742b69d29314
X-Git-Newrev: e105df67dcc3f11787259c6334e68a9dabb167c1
Message-Id: <E1P2TfQ-0001SI-Hq@vcs-noshell.in.savannah.gnu.org>
Date: Sun, 03 Oct 2010 18:52:12 +0000
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
Subject: [SCM] GNU Mailutils branch, master,
	updated. release-2.2-114-ge105df6
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.5
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=unsubscribe>
List-Archive: <http://lists.gnu.org/archive/html/commit-mailutils>
List-Post: <mailto:commit-mailutils@gnu.org>
List-Help: <mailto:commit-mailutils-request@gnu.org?subject=help>
List-Subscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Sun, 03 Oct 2010 18:52:30 -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=e105df67dcc3f11787259c6334e68a9dabb167c1

The branch, master has been updated
       via  e105df67dcc3f11787259c6334e68a9dabb167c1 (commit)
       via  aa0826526df4dc51cafc2297b7b8306831c38f98 (commit)
       via  af4665d48e3dbfe8b9179740509a61722291e13b (commit)
       via  f223b22f5a8cc3ed3428092ddf30dd315a3213f3 (commit)
      from  9d1a990614d460252b6217cb4f98742b69d29314 (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 e105df67dcc3f11787259c6334e68a9dabb167c1
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Sun Oct 3 21:42:58 2010 +0300

    Bugfix.
    
    * examples/http.c (main): Fix open/wait loop.
    * libmailutils/stream.c (mu_stream_wait): May be used
    on not-open streams.
    * examples/.gitignore: Add more files.

commit aa0826526df4dc51cafc2297b7b8306831c38f98
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Sun Oct 3 21:24:52 2010 +0300

    New utility: mu
    
    The `mu' program is a multi-purpose tool for doing various mail-
    and mailutils-related operations.  It includes a pop3 shell,
    a coder/decoder for all filter formats supported by Mailutils,
    a utility to extract arbitrary values from the MU configuration
    files, a configuration information utility and many more, in the
    short run.  It supercedes examples/pop3client and (partially)
    mailutils-config, both of which will be removed in the future.
    
    * Makefile.am (SUBDIRS): Add mu.
    * configure.ac: Likewise.
    * po/POTFILES.in: Add new files.
    * mu/.gitignore: New file.
    * mu/Makefile.am: New file.
    * mu/filter.c: New file.
    * mu/flt2047.c: New file.
    * mu/info.c: New file.
    * mu/mu.c: New file.
    * mu/mu.h: New file.
    * mu/pop.c: New file.
    * mu/query.c: New file.
    * mu/shell.c: New file.

commit af4665d48e3dbfe8b9179740509a61722291e13b
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Sun Oct 3 14:43:44 2010 +0300

    Improve stream creation consistency.
    
    One of stream flags bits indicates whether the stream is
    open or not.  The mu_stream_open function refuses to act if it
    already is.  The rest of functions (including mu_stream_close)
    refuses to act if the stream was not open.  A special function
    (mu_stream_is_open) is provided to check if the stream is in
    open state.
    
    Any stream creation function returns an already open stream.  The
    function mu_stream_open should be called only if the stream was
    previously closed by a call to mu_stream_close.
    
    * include/mailutils/stream.h (mu_stream_is_open): New proto.
    * include/mailutils/sys/stream.h (_MU_STR_OPEN): New flag.
    * libmailutils/stream.c (_mu_stream_create): Permit setting the
    _MU_STR_OPEN flag.
    (_stream_init): New static.
    (mu_stream_open): Return MU_ERR_OPEN if the stream is already open.
    (mu_stream_is_open): New function.
    (mu_stream_seek, mu_stream_read, mu_stream_readdelim)
    (mu_stream_getdelim, mu_stream_write, mu_stream_flush)
    (mu_stream_close, mu_stream_size, mu_stream_wait)
    (mu_stream_truncate): Check if the stream is open. Return MU_ERR_NOT_OPEN
    otherwise.
    
    * libmailutils/errors (MU_ERR_OPEN): Reword the diagnostics.
    
    * libmailutils/dbgstream.c (mu_dbgstream_create): Open the stream.
    * libmailutils/file_stream.c (mu_file_stream_create)
    (mu_fd_stream_create): Open the stream.
    * libmailutils/filter_iconv.c (mu_filter_iconv_create): Open the stream.
    * libmailutils/mapfile_stream.c (mu_mapfile_stream_create): Open the stream.
    * libmailutils/fltstream.c (mu_filter_stream_create): Mark the stream
    as open.
    * libmailutils/iostream.c (mu_iostream_create): Likewise.
    * libmailutils/memory_stream.c: Likewise.
    * libmailutils/message.c (_message_stream_create): Likewise.
    * libmailutils/message_stream.c (mu_message_stream_create): Likewise.
    * libmailutils/prog_stream.c (mu_prog_stream_create): Likewise.
    * libmailutils/socket_stream.c (mu_socket_stream_create): Likewise.
    * libmailutils/temp_file_stream.c (mu_temp_file_stream_create): Likewise.
    
    * libmailutils/amd.c (_amd_attach_message): Mark stream as open.
    * libmailutils/body.c (_body_get_stream): Likewise.
    * libmailutils/memory_stream.c: Likewise.
    * libmailutils/rdcache_stream.c (mu_rdcache_stream_create: Likewise.
    * libmailutils/stdio_stream.c (mu_stdio_stream_create): Likewise.
    * libmailutils/streamref.c (mu_streamref_create_abridged): Likewise.
    * libmailutils/tcp.c (mu_tcp_stream_create_with_source_ip): Likewise.
    * libmailutils/xscript-stream.c (mu_xscript_stream_create): Likewise.
    * libmu_auth/tls.c (_mu_tls_io_stream_create): Likewise.
    (_mu_tls_stream_create): Likewise.
    * libproto/pop/pop3_stream.c (mu_pop3_stream_create): Likewise.
    
    * examples/base64.c: Remove unnecessary mu_stream_open.
    * examples/http.c: Likewise.
    * examples/iconv.c: Likewise.
    * examples/msg-send.c: Likewise.
    * examples/murun.c: Likewise.
    * examples/musocio.c: Likewise.
    * examples/pop3client.c: Likewise.
    * imap4d/append.c: Likewise.
    * imap4d/io.c: Likewise.
    * imap4d/preauth.c: Likewise.
    * lib/mailcap.c: Likewise.
    * libmailutils/attachment.c: Likewise.
    * libmailutils/cfg_driver.c: Likewise.
    * libmailutils/header.c: Likewise.
    * libmailutils/mailcap.c: Likewise.
    * libmailutils/mimehdr.c: Likewise.
    * libmailutils/mutil.c: Likewise.
    * libmailutils/tests/fltst.c: Likewise.
    * libmailutils/tests/mailcap.c: Likewise.
    * libmailutils/vartab.c: Likewise.
    * libmailutils/wicket.c: Likewise.
    * libmu_argp/muinit.c: Likewise.
    * libmu_auth/ldap.c: Likewise.
    * libmu_sieve/extensions/pipe.c: Likewise.
    * libmu_sieve/extensions/spamd.c: Likewise.
    * libmu_sieve/extensions/vacation.c: Likewise.
    * libproto/imap/folder.c: Likewise.
    * libproto/mailer/smtp.c: Likewise.
    * libproto/mailer/smtp_starttls.c: Likewise.
    * libproto/mbox/mbox.c: Likewise.
    * libproto/nntp/nntp_connect.c: Likewise.
    * libproto/pop/mbox.c: Likewise.
    * libproto/pop/pop3_connect.c: Likewise.
    * libproto/pop/pop3_stls.c: Likewise.
    * maidag/mailtmp.c: Likewise.
    * mail/decode.c: Likewise.
    * mail/pipe.c: Likewise.
    * mail/send.c: Likewise.
    * mail/send.c: Likewise.
    * mail/write.c: Likewise.
    * mh/burst.c: Likewise.
    * mh/comp.c: Likewise.
    * mh/compcommon.c: Likewise.
    * mh/forw.c: Likewise.
    * mh/mh_init.c: Likewise.
    * mh/mh_whatnow.c: Likewise.
    * mh/mhl.c: Likewise.
    * mh/mhn.c: Likewise.
    * mh/repl.c: Likewise.
    * mimeview/mimeview.c: Likewise.
    * pop3d/extra.c: Likewise.
    * sieve/sieve.c: Likewise.
    * testsuite/smtpsend.c: Likewise.

commit f223b22f5a8cc3ed3428092ddf30dd315a3213f3
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Sun Oct 3 14:39:42 2010 +0300

    Fix the SMTP SASL (client) without trace.
    
    * include/mailutils/sys/smtp.h (_mu_smtp_get_streams)
    (_mu_smtp_set_streams): New protos.
    * libproto/mailer/smtp_gsasl.c (insert_gsasl_stream): Use _mu_smtp_get_streams
    and _mu_smtp_set_streams to change streams.
    * libproto/mailer/smtp_starttls.c (smtp_get_streams): Rename to
    _mu_smtp_get_streams. All callers changed.
    (smtp_set_streams): Rename to
    _mu_smtp_set_streams. All callers changed.

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

Summary of changes:
 Makefile.am                       |    1 +
 configure.ac                      |    1 +
 examples/.gitignore               |    2 +
 examples/base64.c                 |    2 -
 examples/http.c                   |    9 +-
 examples/iconv.c                  |    3 -
 examples/msg-send.c               |    1 -
 examples/murun.c                  |   10 -
 examples/musocio.c                |    1 -
 examples/pop3client.c             |    9 -
 imap4d/append.c                   |    3 +-
 imap4d/io.c                       |    5 -
 imap4d/preauth.c                  |    9 -
 include/mailutils/stream.h        |    1 +
 include/mailutils/sys/smtp.h      |    4 +
 include/mailutils/sys/stream.h    |   11 +-
 lib/mailcap.c                     |   10 -
 libmailutils/amd.c                |   11 +-
 libmailutils/attachment.c         |   56 ++--
 libmailutils/body.c               |    5 +-
 libmailutils/cfg_driver.c         |    1 -
 libmailutils/dbgstream.c          |   13 +-
 libmailutils/errors               |    2 +-
 libmailutils/file_stream.c        |   18 +-
 libmailutils/filter_iconv.c       |   11 +-
 libmailutils/fltstream.c          |    1 +
 libmailutils/header.c             |    6 -
 libmailutils/iostream.c           |    1 +
 libmailutils/mailcap.c            |  106 +++---
 libmailutils/mapfile_stream.c     |   13 +-
 libmailutils/memory_stream.c      |   26 +-
 libmailutils/message.c            |    4 +-
 libmailutils/message_stream.c     |   19 +-
 libmailutils/mimehdr.c            |    4 -
 libmailutils/mutil.c              |    9 +-
 libmailutils/prog_stream.c        |   36 +-
 libmailutils/rdcache_stream.c     |    5 +-
 libmailutils/socket_stream.c      |    8 +-
 libmailutils/stdio_stream.c       |    1 +
 libmailutils/stream.c             |  109 +++++-
 libmailutils/streamref.c          |    3 +-
 libmailutils/tcp.c                |   13 +-
 libmailutils/temp_file_stream.c   |   17 +-
 libmailutils/tests/fltst.c        |    3 -
 libmailutils/tests/mailcap.c      |    8 -
 libmailutils/vartab.c             |    1 -
 libmailutils/wicket.c             |  105 +++---
 libmailutils/xscript-stream.c     |    3 +-
 libmu_argp/muinit.c               |    1 -
 libmu_auth/ldap.c                 |    4 -
 libmu_auth/tls.c                  |   14 +-
 libmu_sieve/extensions/pipe.c     |    3 -
 libmu_sieve/extensions/spamd.c    |   13 -
 libmu_sieve/extensions/vacation.c |   11 -
 libproto/imap/folder.c            |   16 +-
 libproto/mailer/smtp.c            |    7 -
 libproto/mailer/smtp_gsasl.c      |   11 +-
 libproto/mailer/smtp_starttls.c   |   18 +-
 libproto/mbox/mbox.c              |   60 +--
 libproto/nntp/nntp_connect.c      |    7 +-
 libproto/pop/mbox.c               |   13 -
 libproto/pop/pop3_connect.c       |    9 +-
 libproto/pop/pop3_stls.c          |    2 -
 libproto/pop/pop3_stream.c        |    3 +-
 maidag/mailtmp.c                  |    7 -
 mail/decode.c                     |    6 -
 mail/pipe.c                       |    2 -
 mail/send.c                       |    2 -
 mail/write.c                      |    2 -
 mh/burst.c                        |    7 +-
 mh/comp.c                         |    3 +-
 mh/compcommon.c                   |    3 +-
 mh/forw.c                         |    3 +-
 mh/mh_init.c                      |   14 +-
 mh/mh_whatnow.c                   |    6 -
 mh/mhl.c                          |   12 -
 mh/mhn.c                          |   56 ---
 mh/repl.c                         |    7 -
 mimeview/mimeview.c               |    1 -
 mu/.gitignore                     |    1 +
 mu/Makefile.am                    |   43 ++
 mu/filter.c                       |  159 ++++++++
 mu/flt2047.c                      |  153 ++++++++
 mu/info.c                         |   93 +++++
 mu/mu.c                           |  150 +++++++
 mu/mu.h                           |   40 ++
 mu/pop.c                          |  778 +++++++++++++++++++++++++++++++++++++
 mu/query.c                        |  121 ++++++
 mu/shell.c                        |  492 +++++++++++++++++++++++
 po/POTFILES.in                    |    8 +
 pop3d/extra.c                     |   12 +-
 sieve/sieve.c                     |    6 -
 testsuite/smtpsend.c              |    6 +-
 93 files changed, 2494 insertions(+), 591 deletions(-)
 create mode 100644 mu/.gitignore
 create mode 100644 mu/Makefile.am
 create mode 100644 mu/filter.c
 create mode 100644 mu/flt2047.c
 create mode 100644 mu/info.c
 create mode 100644 mu/mu.c
 create mode 100644 mu/mu.h
 create mode 100644 mu/pop.c
 create mode 100644 mu/query.c
 create mode 100644 mu/shell.c

diff --git a/Makefile.am b/Makefile.am
index e8f8e94..7c403d6 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -109,6 +109,7 @@ SUBDIRS = . \
  doc\
  config\
  examples\
+ mu\
  $(FRM_DIR)\
  $(POP3D_DIR)\
  $(IMAP4D_DIR)\
diff --git a/configure.ac b/configure.ac
index 13cf4fa..509a569 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1389,6 +1389,7 @@ AC_CONFIG_FILES([
  mu-aux/Makefile
  mu-aux/mailutils.spec
  sieve/Makefile
+ mu/Makefile
  ])
 AC_OUTPUT
 
diff --git a/examples/.gitignore b/examples/.gitignore
index f5ec94c..b42a928 100644
--- a/examples/.gitignore
+++ b/examples/.gitignore
@@ -15,8 +15,10 @@ mimetest
 msg-send
 mta
 muauth
+mucat
 muemail
 murun
+musocio
 nntpclient
 pop3client
 sfrom
diff --git a/examples/base64.c b/examples/base64.c
index aa6bdeb..1f54ea7 100644
--- a/examples/base64.c
+++ b/examples/base64.c
@@ -151,14 +151,12 @@ main (int argc, char * argv [])
     MU_ASSERT (mu_file_stream_create (&in, input, MU_STREAM_READ|MU_STREAM_SEEK));
   else
     MU_ASSERT (mu_stdio_stream_create (&in, MU_STDIN_FD, 0));
-  MU_ASSERT (mu_stream_open (in));
 
   if (output)
     MU_ASSERT (mu_file_stream_create (&out, output, 
                                       MU_STREAM_WRITE|MU_STREAM_CREAT));
   else
     MU_ASSERT (mu_stdio_stream_create (&out, MU_STDOUT_FD, 0));
-  MU_ASSERT (mu_stream_open (out));
 
   if (line_length_option)
     reset_line_length (encoding, line_length);
diff --git a/examples/http.c b/examples/http.c
index a2bb6b7..44666e5 100644
--- a/examples/http.c
+++ b/examples/http.c
@@ -91,19 +91,22 @@ main (int argc, char **argv)
 	    argc == 3 ? argv[2] : "/");
 
   ret = mu_tcp_stream_create (&stream, url, 80, MU_STREAM_NONBLOCK);
-  if (ret != 0)
+  if (ret != 0 && !(ret == EAGAIN || ret == EINPROGRESS))
     {
       mu_error ("mu_tcp_stream_create: %s", mu_strerror (ret));
       exit (EXIT_FAILURE);
     }
 
-  for (attempt = 0; (ret = mu_stream_open (stream)); )
+  for (attempt = 0; ret; )
     {
       if ((ret == EAGAIN || ret == EINPROGRESS) && attempt < io_attempts)
 	{
 	  ret = http_stream_wait(stream, MU_STREAM_READY_WR, &attempt);
 	  if (ret == 0)
-	    continue;
+	    {
+	      ret = mu_stream_open (stream);
+	      continue;
+	    }
 	}
       mu_error ("mu_stream_open: %s", mu_strerror (ret));
       exit (EXIT_FAILURE);
diff --git a/examples/iconv.c b/examples/iconv.c
index fd293bf..7876dc6 100644
--- a/examples/iconv.c
+++ b/examples/iconv.c
@@ -43,13 +43,10 @@ main (int argc, char **argv)
     }
 
   MU_ASSERT (mu_stdio_stream_create (&in, MU_STDIN_FD, 0));
-  MU_ASSERT (mu_stream_open (in));
   MU_ASSERT (mu_filter_iconv_create (&cvt, in, argv[1], argv[2], 
                                      0, mu_fallback_none));
-  MU_ASSERT (mu_stream_open (cvt));
   
   MU_ASSERT (mu_stdio_stream_create (&out, MU_STDOUT_FD, 0));
-  MU_ASSERT (mu_stream_open (out));
 
   while ((rc = mu_stream_read (cvt, buffer, sizeof (buffer), &size)) == 0
 	 && size > 0)
diff --git a/examples/msg-send.c b/examples/msg-send.c
index 5c4f70f..46dd82a 100644
--- a/examples/msg-send.c
+++ b/examples/msg-send.c
@@ -109,7 +109,6 @@ main (int argc, char *argv[])
     }
 
   MU_ASSERT (mu_stdio_stream_create (&in, MU_STDIN_FD, MU_STREAM_SEEK));
-  MU_ASSERT (mu_stream_open (in));
   MU_ASSERT (mu_message_create (&msg, NULL));
   MU_ASSERT (mu_message_set_stream (msg, in, NULL));
   MU_ASSERT (mu_mailer_create (&mailer, optmailer));
diff --git a/examples/murun.c b/examples/murun.c
index 51b4710..c4f822f 100644
--- a/examples/murun.c
+++ b/examples/murun.c
@@ -69,7 +69,6 @@ main (int argc, char *argv[])
     {
       mu_stream_t in;
       MU_ASSERT (mu_stdio_stream_create (&in, MU_STDIN_FD, 0));
-      MU_ASSERT (mu_stream_open (in));
       rc = mu_filter_prog_stream_create (&stream, cmdline, in);
       /* Make sure closing/destroying stream will close/destroy in */
       mu_stream_unref (in);
@@ -83,16 +82,7 @@ main (int argc, char *argv[])
       exit (1);
     }
   
-  rc = mu_stream_open (stream);
-  if (rc)
-    {
-      fprintf (stderr, "%s: cannot open program filter stream: %s\n",
-	       argv[0], mu_strerror (rc));
-      exit (1);
-    }
-
   MU_ASSERT (mu_stdio_stream_create (&out, MU_STDOUT_FD, 0));
-  MU_ASSERT (mu_stream_open (out));
   
   read_and_print (stream, out);
   mu_stream_close (stream);
diff --git a/examples/musocio.c b/examples/musocio.c
index d19a200..31ca7d3 100644
--- a/examples/musocio.c
+++ b/examples/musocio.c
@@ -90,7 +90,6 @@ main (int argc, char * argv [])
   mu_stream_set_buffer (out, mu_buffer_line, 0);
   MU_ASSERT (mu_socket_stream_create (&sock, argv[1], MU_STREAM_RDWR));
   mu_stream_set_buffer (sock, mu_buffer_line, 0);
-  MU_ASSERT (mu_stream_open (sock));
   
   pid = fork ();
   if (pid == -1)
diff --git a/examples/pop3client.c b/examples/pop3client.c
index c924638..87f0c44 100644
--- a/examples/pop3client.c
+++ b/examples/pop3client.c
@@ -1030,15 +1030,6 @@ com_connect (int argc, char **argv)
 	    {
 	      mu_stream_t tlsstream;
 	      
-	      status = mu_stream_open (tcp);
-	      if (status)
-		{
-		  mu_error ("cannot open connection: %s",
-			    mu_stream_strerror (tcp, status));
-		  mu_stream_destroy (&tcp);
-		  return 0;
-		}
-
 	      status = mu_tls_client_stream_create (&tlsstream, tcp, tcp, 0);
 	      mu_stream_unref (tcp);
 	      if (status)
diff --git a/imap4d/append.c b/imap4d/append.c
index bf126b8..3ec431c 100644
--- a/imap4d/append.c
+++ b/imap4d/append.c
@@ -76,8 +76,7 @@ imap4d_append0 (mu_mailbox_t mbox, int flags, char *date_time, char *text,
   if (mu_message_create (&msg, &tm))
     return 1;
 
-  if (mu_memory_stream_create (&stream, MU_STREAM_RDWR)
-      || mu_stream_open (stream))
+  if (mu_memory_stream_create (&stream, MU_STREAM_RDWR))
     {
       mu_message_destroy (&msg, &tm);
       return 1;
diff --git a/imap4d/io.c b/imap4d/io.c
index e61a89a..14959da 100644
--- a/imap4d/io.c
+++ b/imap4d/io.c
@@ -98,14 +98,9 @@ imap4d_init_tls_server ()
   
   rc = mu_tls_server_stream_create (&tlsstream, stream[0], stream[1], 0);
   if (rc)
-    return 1;
-
-  rc = mu_stream_open (tlsstream);
-  if (rc)
     {
       mu_diag_output (MU_DIAG_ERROR, _("cannot open TLS stream: %s"),
 		      mu_stream_strerror (tlsstream, rc));
-      mu_stream_destroy (&tlsstream);
       return 1;
     }
 
diff --git a/imap4d/preauth.c b/imap4d/preauth.c
index 4c505b3..b876ea8 100644
--- a/imap4d/preauth.c
+++ b/imap4d/preauth.c
@@ -213,7 +213,6 @@ decode64_buf (const char *name, unsigned char **pbuf, size_t *psize)
   mu_memory_stream_create (&str, 0);
   mu_filter_create (&flt, str, "base64", MU_FILTER_DECODE,
 		    MU_STREAM_READ | MU_STREAM_AUTOCLOSE);
-  mu_stream_open (str);
   mu_stream_write (str, name, namelen, NULL);
   mu_stream_read (flt, buf, sizeof buf, &size);
   mu_stream_destroy (&flt);
@@ -357,14 +356,6 @@ do_preauth_ident (struct sockaddr *clt_sa, struct sockaddr *srv_sa)
       return NULL;
     }
 
-  rc = mu_stream_open (stream);
-  if (rc)
-    {
-      mu_diag_output (MU_DIAG_INFO, _("cannot open TCP stream to %s:%d: %s"),
-		      hostaddr, ident_port, mu_strerror (rc));
-      return NULL;
-    }
-
   mu_stream_printf (stream, "%u , %u\n",
 		    ntohs (clt_addr->sin_port),
 		    ntohs (srv_addr->sin_port));
diff --git a/include/mailutils/stream.h b/include/mailutils/stream.h
index a8eb9c9..c076ade 100644
--- a/include/mailutils/stream.h
+++ b/include/mailutils/stream.h
@@ -99,6 +99,7 @@ int mu_stream_last_error (mu_stream_t stream);
 void mu_stream_clearerr (mu_stream_t stream);
 int mu_stream_seterr (struct _mu_stream *stream, int code, int perm);
 
+int mu_stream_is_open (mu_stream_t stream);
 int mu_stream_eof (mu_stream_t stream);
 int mu_stream_seek (mu_stream_t stream, mu_off_t offset, int whence,
 		    mu_off_t *pres);
diff --git a/include/mailutils/sys/smtp.h b/include/mailutils/sys/smtp.h
index 0afe1e5..4099824 100644
--- a/include/mailutils/sys/smtp.h
+++ b/include/mailutils/sys/smtp.h
@@ -97,4 +97,8 @@ int _mu_smtp_mech_impl (mu_smtp_t smtp, mu_list_t list);
 int _mu_smtp_data_begin (mu_smtp_t smtp);
 int _mu_smtp_data_end (mu_smtp_t smtp);
 
+int _mu_smtp_get_streams (mu_smtp_t smtp, mu_stream_t *streams);
+int _mu_smtp_set_streams (mu_smtp_t smtp, mu_stream_t *streams);
+
+
 #endif
diff --git a/include/mailutils/sys/stream.h b/include/mailutils/sys/stream.h
index c3c405a..aa95f7e 100644
--- a/include/mailutils/sys/stream.h
+++ b/include/mailutils/sys/stream.h
@@ -17,12 +17,13 @@
 #ifndef _MAILUTILS_SYS_STREAM_H
 #define _MAILUTILS_SYS_STREAM_H
 
-#define _MU_STR_DIRTY         0x10000000    /* Buffer dirty */
-#define _MU_STR_WRT           0x20000000    /* Unflushed write pending */
-#define _MU_STR_ERR           0x40000000    /* Permanent error state */
-#define _MU_STR_EOF           0x80000000    /* EOF encountered */
+#define _MU_STR_OPEN          0x01000000    /* Stream open */
+#define _MU_STR_DIRTY         0x02000000    /* Buffer dirty */
+#define _MU_STR_WRT           0x04000000    /* Unflushed write pending */
+#define _MU_STR_ERR           0x08000000    /* Permanent error state */
+#define _MU_STR_EOF           0x10000000    /* EOF encountered */
 
-#define _MU_STR_INTERN_MASK   0xf0000000
+#define _MU_STR_INTERN_MASK   0xff000000
 
 #define _MU_STR_EVENT_SETFLAG   0
 #define _MU_STR_EVENT_CLRFLAG   1
diff --git a/lib/mailcap.c b/lib/mailcap.c
index 752a965..4a05484 100644
--- a/lib/mailcap.c
+++ b/lib/mailcap.c
@@ -602,16 +602,6 @@ find_entry (const char *file, struct mime_context *ctx)
       return -1;
     }
 
-  status = mu_stream_open (stream);
-  if (status)
-    {
-      mu_stream_destroy (&stream);
-      if (status != ENOENT)
-	mu_error ("cannot open file stream %s: %s",
-		  file, mu_strerror (status));
-      return -1;
-    }
-
   status = mu_mailcap_create (&mailcap, stream);
   if (status == 0)
     {
diff --git a/libmailutils/amd.c b/libmailutils/amd.c
index a0dec4e..a5dac3f 100644
--- a/libmailutils/amd.c
+++ b/libmailutils/amd.c
@@ -485,7 +485,8 @@ _amd_attach_message (mu_mailbox_t mailbox, struct _amd_message *mhm,
 
     str = (struct _amd_body_stream *)
               _mu_stream_create (sizeof (*str),
-				 mailbox->flags | MU_STREAM_SEEK);
+				 mailbox->flags | MU_STREAM_SEEK | 
+				 _MU_STR_OPEN);
     if (!str)
       {
 	mu_body_destroy (&body, msg);
@@ -1596,13 +1597,7 @@ amd_message_stream_open (struct _amd_message *mhm)
   /* FIXME: Select buffer size dynamically */
   mu_stream_set_buffer (mhm->stream, mu_buffer_full, 16384);
   
-  status = mu_stream_open (mhm->stream);
-
-  if (status != 0)
-    mu_stream_destroy (&mhm->stream);
-
-  if (status == 0)
-    status = amd_scan_message (mhm);
+  status = amd_scan_message (mhm);
 
   return status;
 }
diff --git a/libmailutils/attachment.c b/libmailutils/attachment.c
index 6bed79f..c642681 100644
--- a/libmailutils/attachment.c
+++ b/libmailutils/attachment.c
@@ -104,16 +104,13 @@ mu_message_create_attachment (const char *content_type, const char *encoding,
 		       mu_file_stream_create (&fstream, filename,
 					      MU_STREAM_READ)) == 0)
 		    {
-		      if ((ret = mu_stream_open (fstream)) == 0)
+		      if ((ret =
+			   mu_filter_create (&tstream, fstream, encoding,
+					     MU_FILTER_ENCODE,
+					     MU_STREAM_READ)) == 0)
 			{
-			  if ((ret =
-			       mu_filter_create (&tstream, fstream, encoding,
-						 MU_FILTER_ENCODE,
-						 MU_STREAM_READ)) == 0)
-			    {
-			      mu_body_set_stream (body, tstream, *newmsg);
-			      mu_message_set_header (*newmsg, hdr, NULL);
-			    }
+			  mu_body_set_stream (body, tstream, *newmsg);
+			  mu_message_set_header (*newmsg, hdr, NULL);
 			}
 		    }
 		}
@@ -293,30 +290,27 @@ mu_message_save_attachment (mu_message_t msg, const char *filename,
 	      mu_file_stream_create (&info->fstream, fname,
 				     MU_STREAM_WRITE | MU_STREAM_CREAT)) == 0)
 	{
-	  if ((ret = mu_stream_open (info->fstream)) == 0)
+	  char *content_encoding;
+	  char *content_encoding_mem = NULL;
+	  
+	  mu_header_get_value (hdr, "Content-Transfer-Encoding", NULL, 0,
+			       &size);
+	  if (size)
 	    {
-	      char *content_encoding;
-	      char *content_encoding_mem = NULL;
-
-	      mu_header_get_value (hdr, "Content-Transfer-Encoding", NULL, 0,
-				   &size);
-	      if (size)
-		{
-		  content_encoding_mem = malloc (size + 1);
-		  if (content_encoding_mem == NULL)
-		    ret = ENOMEM;
-		  content_encoding = content_encoding_mem;
-		  mu_header_get_value (hdr, "Content-Transfer-Encoding",
-				       content_encoding, size + 1, 0);
-		}
-	      else
-		content_encoding = "7bit";
-	      ret =
-		mu_filter_create (&info->stream, istream, content_encoding,
-				  MU_FILTER_DECODE,
-				  MU_STREAM_READ);
-	      free (content_encoding_mem);
+	      content_encoding_mem = malloc (size + 1);
+	      if (content_encoding_mem == NULL)
+		ret = ENOMEM;
+	      content_encoding = content_encoding_mem;
+	      mu_header_get_value (hdr, "Content-Transfer-Encoding",
+				   content_encoding, size + 1, 0);
 	    }
+	  else
+	    content_encoding = "7bit";
+	  ret =
+	    mu_filter_create (&info->stream, istream, content_encoding,
+			      MU_FILTER_DECODE,
+			      MU_STREAM_READ);
+	  free (content_encoding_mem);
 	}
     }
   if (info->stream && istream)
diff --git a/libmailutils/body.c b/libmailutils/body.c
index f076e4a..05b47f1 100644
--- a/libmailutils/body.c
+++ b/libmailutils/body.c
@@ -173,7 +173,7 @@ _body_get_stream (mu_body_t body, mu_stream_t *pstream, int ref)
 	  struct _mu_body_stream *str =
 	    (struct _mu_body_stream *)
 	    _mu_stream_create (sizeof (*str),
-			       MU_STREAM_RDWR|MU_STREAM_SEEK);
+			       MU_STREAM_RDWR|MU_STREAM_SEEK|_MU_STR_OPEN);
 	  if (!str)
 	    return ENOMEM;
 	  
@@ -184,9 +184,6 @@ _body_get_stream (mu_body_t body, mu_stream_t *pstream, int ref)
 	  if (status != 0)
 	    return status;
 	  mu_stream_set_buffer (body->fstream, mu_buffer_full, 0);
-	  status = mu_stream_open (body->fstream);
-	  if (status != 0)
-	    return status;
 	  str->stream.ctl = _body_ioctl;
 	  str->stream.read = _body_read;
 	  str->stream.write = _body_write;
diff --git a/libmailutils/cfg_driver.c b/libmailutils/cfg_driver.c
index f4d56f7..f07a22f 100644
--- a/libmailutils/cfg_driver.c
+++ b/libmailutils/cfg_driver.c
@@ -590,7 +590,6 @@ mu_cfg_tree_reduce (mu_cfg_tree_t *parse_tree, const char *progname,
     {
       mu_stream_t stream;
       mu_stdio_stream_create (&stream, MU_STDERR_FD, 0);
-      mu_stream_open (stream);
       mu_cfg_format_parse_tree (stream, parse_tree, MU_CFG_FMT_LOCUS);
       mu_stream_destroy (&stream);
     }
diff --git a/libmailutils/dbgstream.c b/libmailutils/dbgstream.c
index 18f8834..b784813 100644
--- a/libmailutils/dbgstream.c
+++ b/libmailutils/dbgstream.c
@@ -66,6 +66,8 @@ int
 mu_dbgstream_create(mu_stream_t *pref, mu_debug_t debug, mu_log_level_t level,
 		    int flags)
 {
+  int rc;
+  mu_stream_t stream;
   struct _mu_dbgstream *sp;
 
   sp = (struct _mu_dbgstream *)
@@ -79,8 +81,13 @@ mu_dbgstream_create(mu_stream_t *pref, mu_debug_t debug, mu_log_level_t level,
   
   sp->debug = debug;
   sp->level = level;
-  mu_stream_set_buffer ((mu_stream_t) sp, mu_buffer_line, 0);
-  *pref = (mu_stream_t) sp;
-  return 0;
+  stream = (mu_stream_t) sp;
+  mu_stream_set_buffer (stream, mu_buffer_line, 0);
+  rc = mu_stream_open (stream);
+  if (rc)
+    mu_stream_destroy (&stream);
+  else
+    *pref = stream;
+  return rc;
 }
   
diff --git a/libmailutils/errors b/libmailutils/errors
index 7828f20..8593c79 100644
--- a/libmailutils/errors
+++ b/libmailutils/errors
@@ -29,7 +29,7 @@ MU_ERR_MBX_NULL             _("Mailbox null")
 MU_ERR_MBX_REMOVED          _("Mailbox removed")
 
 MU_ERR_NOT_OPEN             _("Resource not open")
-MU_ERR_OPEN                 _("Resource still open")
+MU_ERR_OPEN                 _("Resource is already open")
 
 MU_ERR_BAD_822_FORMAT       _("Format of RFC822 object is bad")
 MU_ERR_EMPTY_ADDRESS        _("Address contains no addr specs")
diff --git a/libmailutils/file_stream.c b/libmailutils/file_stream.c
index 814e80d..247768c 100644
--- a/libmailutils/file_stream.c
+++ b/libmailutils/file_stream.c
@@ -295,8 +295,13 @@ mu_file_stream_create (mu_stream_t *pstream, const char *filename, int flags)
 				   flags | MU_STREAM_SEEK | MU_STREAM_AUTOCLOSE);
   if (rc == 0)
     {
-      *pstream = (mu_stream_t) fstr;
-      mu_stream_set_buffer ((mu_stream_t) fstr, mu_buffer_full, 0);
+      mu_stream_t stream = (mu_stream_t) fstr;
+      mu_stream_set_buffer (stream, mu_buffer_full, 0);
+      rc = mu_stream_open (stream);
+      if (rc)
+	mu_stream_unref (stream);
+      else
+	*pstream = stream;
     }
   return rc;
 }
@@ -310,8 +315,13 @@ mu_fd_stream_create (mu_stream_t *pstream, char *filename, int fd, int flags)
 				   filename, fd, flags);
   if (rc == 0)
     {
-      *pstream = (mu_stream_t) fstr;
-      mu_stream_set_buffer ((mu_stream_t) fstr, mu_buffer_full, 0);
+      mu_stream_t stream = (mu_stream_t) fstr;
+      mu_stream_set_buffer (stream, mu_buffer_full, 0);
+      rc = mu_stream_open (stream);
+      if (rc)
+	mu_stream_unref (stream);
+      else
+	*pstream = stream;
     }
   return rc;
 }
diff --git a/libmailutils/filter_iconv.c b/libmailutils/filter_iconv.c
index d47bb17..571e8b7 100644
--- a/libmailutils/filter_iconv.c
+++ b/libmailutils/filter_iconv.c
@@ -434,7 +434,9 @@ mu_filter_iconv_create (mu_stream_t *s, mu_stream_t transport,
 {
   struct icvt_stream *iptr;
   iconv_t cd;
-  
+  int rc;
+  mu_stream_t stream;
+    
   cd = iconv_open (tocode, fromcode);
   if (cd == (iconv_t) -1)
     return MU_ERR_FAILURE;
@@ -470,6 +472,11 @@ mu_filter_iconv_create (mu_stream_t *s, mu_stream_t transport,
   iptr->stream.wait = _icvt_wait;
   iptr->stream.seek = _icvt_seek;
   iptr->stream.flags = MU_STREAM_READ | MU_STREAM_SEEK;
-  *s = (mu_stream_t)iptr;
+  stream = (mu_stream_t)iptr;
+  rc = mu_stream_open (stream);
+  if (rc)
+    mu_stream_destroy (&stream);
+  else
+    *s = stream;
   return 0;
 }
diff --git a/libmailutils/fltstream.c b/libmailutils/fltstream.c
index 4359e03..f6fe6ad 100644
--- a/libmailutils/fltstream.c
+++ b/libmailutils/fltstream.c
@@ -480,6 +480,7 @@ mu_filter_stream_create (mu_stream_t *pflt,
   if (!fs)
     return ENOMEM;
   
+  flags |= _MU_STR_OPEN;
   if (flags & MU_STREAM_READ)
     {
       fs->stream.read = filter_read;
diff --git a/libmailutils/header.c b/libmailutils/header.c
index 2861c67..7c95e08 100644
--- a/libmailutils/header.c
+++ b/libmailutils/header.c
@@ -1086,12 +1086,6 @@ header_write (mu_stream_t os, const char *buf, size_t buflen, size_t *pnwrite)
       status = mu_memory_stream_create (&header->mstream, MU_STREAM_RDWR);
       if (status)
 	return status;
-      status = mu_stream_open (header->mstream);
-      if (status)
-	{
-	  mu_stream_destroy (&header->mstream);
-	  return status;
-	}
     }
 
   status = mu_stream_write (header->mstream, buf, buflen, NULL);
diff --git a/libmailutils/iostream.c b/libmailutils/iostream.c
index 6608312..d2185f7 100644
--- a/libmailutils/iostream.c
+++ b/libmailutils/iostream.c
@@ -261,6 +261,7 @@ mu_iostream_create (mu_stream_t *pref, mu_stream_t in, mu_stream_t out)
   if (!sp)
     return ENOMEM;
 
+  sp->stream.flags |= _MU_STR_OPEN;
   sp->stream.read = _iostream_read; 
   if (in->readdelim)
     sp->stream.readdelim = _iostream_readdelim; 
diff --git a/libmailutils/mailcap.c b/libmailutils/mailcap.c
index d9e96f2..8d8c700 100644
--- a/libmailutils/mailcap.c
+++ b/libmailutils/mailcap.c
@@ -672,70 +672,66 @@ int main()
 				  MU_STREAM_READ);
   if (status == 0)
     {
-      status = mu_stream_open (stream);
+      mu_mailcap_t mailcap;
+      status = mu_mailcap_create (&mailcap, stream);
       if (status == 0)
 	{
-	  mu_mailcap_t mailcap;
-	  status = mu_mailcap_create (&mailcap, stream);
-	  if (status == 0)
+	  int i, n;
+	  size_t count = 0;
+	  char buffer[256];
+	  
+	  mu_mailcap_entries_count (mailcap, &count);
+	  for (i = 1; i <= count; i++)
 	    {
-	      int i, n;
-	      size_t count = 0;
-	      char buffer[256];
-
-	      mu_mailcap_entries_count (mailcap, &count);
-	      for (i = 1; i <= count; i++)
-		{
-		  int j;
-		  mu_mailcap_entry_t entry = NULL;
-		  int fields_count = 0;
-
-		  printf ("entry[%d]\n", i);
+	      int j;
+	      mu_mailcap_entry_t entry = NULL;
+	      int fields_count = 0;
+	      
+	      printf ("entry[%d]\n", i);
 #if 1
-
-		  mu_mailcap_get_entry (mailcap, i, &entry);
-		  /* Print typefield.  */
-		  mu_mailcap_entry_get_typefield (entry, buffer,
-						  sizeof (buffer), NULL);
-		  printf ("\ttypefield: %s\n", buffer);
-
-		  /* Print view-command.  */
-		  mu_mailcap_entry_get_viewcommand (entry, buffer,
-						    sizeof (buffer), NULL);
-		  printf ("\tview-command: %s\n", buffer);
-
-		  /* Print fields.  */
-		  mu_mailcap_entry_fields_count (entry, &fields_count);
-		  for (j = 1; j <= fields_count; j++)
-		    {
-		      mu_mailcap_entry_get_field (entry, j, buffer,
-						  sizeof (buffer), NULL);
-		      printf("\tfields[%d]: %s\n", j, buffer);
-		    }
-		  n = 0;
-		  mu_mailcap_entry_get_compose (entry, buffer,
-						sizeof (buffer), &n);
-		  if (n > 0)
-		    {
-		      printf("\tcompose[%s]\n", buffer);
-		    }
-		  printf("\n");
+	      
+	      mu_mailcap_get_entry (mailcap, i, &entry);
+	      /* Print typefield.  */
+	      mu_mailcap_entry_get_typefield (entry, buffer,
+					      sizeof (buffer), NULL);
+	      printf ("\ttypefield: %s\n", buffer);
+	      
+	      /* Print view-command.  */
+	      mu_mailcap_entry_get_viewcommand (entry, buffer,
+						sizeof (buffer), NULL);
+	      printf ("\tview-command: %s\n", buffer);
+	      
+	      /* Print fields.  */
+	      mu_mailcap_entry_fields_count (entry, &fields_count);
+	      for (j = 1; j <= fields_count; j++)
+		{
+		  mu_mailcap_entry_get_field (entry, j, buffer,
+					      sizeof (buffer), NULL);
+		  printf("\tfields[%d]: %s\n", j, buffer);
+		}
+	      n = 0;
+	      mu_mailcap_entry_get_compose (entry, buffer,
+					    sizeof (buffer), &n);
+	      if (n > 0)
+		{
+		  printf("\tcompose[%s]\n", buffer);
 		}
+	      printf("\n");
+	    }
 #else
-	      for (i = 0; i < mailcap->entries_count; i++)
+	  for (i = 0; i < mailcap->entries_count; i++)
+	    {
+	      int j;
+	      mu_mailcap_entry_t entry = mailcap->entries[i];
+	      printf("[%s];[%s]", entry->typefield, entry->viewcommand);
+	      for (j = 0; j < entry->fields_count; j++)
 		{
-		  int j;
-		  mu_mailcap_entry_t entry = mailcap->entries[i];
-		  printf("[%s];[%s]", entry->typefield, entry->viewcommand);
-		  for (j = 0; j < entry->fields_count; j++)
-		    {
-		      printf(";[%s]", entry->fields[j]);
-		    }
-		  printf("\n");
+		  printf(";[%s]", entry->fields[j]);
 		}
-#endif
-	      mu_mailcap_destroy (&mailcap);
+	      printf("\n");
 	    }
+#endif
+	  mu_mailcap_destroy (&mailcap);
 	}
     }
   return 0;
diff --git a/libmailutils/mapfile_stream.c b/libmailutils/mapfile_stream.c
index 8733622..3cd7c6d 100644
--- a/libmailutils/mapfile_stream.c
+++ b/libmailutils/mapfile_stream.c
@@ -344,6 +344,8 @@ mu_mapfile_stream_create (mu_stream_t *pstream, const char *filename,
 #ifndef _POSIX_MAPPED_FILES
   return ENOSYS;
 #else
+  int rc;
+  mu_stream_t stream;
   struct _mu_mapfile_stream *str =
     (struct _mu_mapfile_stream *) _mu_stream_create (sizeof (*str),
 						     flags | MU_STREAM_SEEK);
@@ -369,9 +371,14 @@ mu_mapfile_stream_create (mu_stream_t *pstream, const char *filename,
   str->stream.flush = _mapfile_flush;
   str->stream.done = _mapfile_done;
   str->stream.seek = _mapfile_seek;
-  
-  *pstream = (mu_stream_t) str;
-  return 0;
+
+  stream = (mu_stream_t) str;
+  rc = mu_stream_open (stream);
+  if (rc)
+    mu_stream_destroy (&stream);
+  else
+    *pstream = stream;
+  return rc;
 #endif
 }
 
diff --git a/libmailutils/memory_stream.c b/libmailutils/memory_stream.c
index 98db753..67952bc 100644
--- a/libmailutils/memory_stream.c
+++ b/libmailutils/memory_stream.c
@@ -131,16 +131,16 @@ static int
 _memory_open (mu_stream_t stream)
 {
   struct _mu_memory_stream *mfs = (struct _mu_memory_stream *) stream;
-  int status = 0;
 
   /* Close any previous stream. */
   if (mfs->ptr)
     free (mfs->ptr);
   mfs->ptr = NULL;
   mfs->size = 0;
+  mfs->offset = 0;
   mfs->capacity = 0;
-
-  return status;
+  
+  return 0;
 }
 
 static int
@@ -192,6 +192,8 @@ _memory_seek (struct _mu_stream *stream, mu_off_t off, mu_off_t *presult)
 int
 mu_memory_stream_create (mu_stream_t *pstream, int flags)
 {
+  int rc;
+  mu_stream_t stream;
   struct _mu_memory_stream *str;
 
   if (!flags)
@@ -202,11 +204,6 @@ mu_memory_stream_create (mu_stream_t *pstream, int flags)
   if (!str)
     return ENOMEM;
 
-  str->ptr = NULL;
-  str->size = 0;
-  str->offset = 0;
-  str->capacity = 0;
-
   str->stream.open = _memory_open;
   str->stream.close = _memory_close;
   str->stream.read = _memory_read;
@@ -216,9 +213,14 @@ mu_memory_stream_create (mu_stream_t *pstream, int flags)
   str->stream.done = _memory_done;
   str->stream.ctl = _memory_ioctl;
   str->stream.seek = _memory_seek;
-
-  *pstream = (mu_stream_t) str;
-
-  return 0;
+  
+  stream = (mu_stream_t) str;
+  rc = mu_stream_open (stream);
+  if (rc)
+    mu_stream_destroy (&stream);
+  else
+    *pstream = stream;
+
+  return rc;
 }
   
diff --git a/libmailutils/message.c b/libmailutils/message.c
index f276d0a..caeba8f 100644
--- a/libmailutils/message.c
+++ b/libmailutils/message.c
@@ -278,7 +278,9 @@ _message_stream_create (mu_stream_t *pmsg, mu_message_t msg, int flags)
   struct _mu_message_stream *sp;
 
   sp = (struct _mu_message_stream *) _mu_stream_create (sizeof (*sp),
-							flags | MU_STREAM_SEEK);
+							flags |
+							MU_STREAM_SEEK |
+							_MU_STR_OPEN);
   if (!sp)
     return ENOMEM;
 
diff --git a/libmailutils/message_stream.c b/libmailutils/message_stream.c
index 30c440d..6bfdae5 100644
--- a/libmailutils/message_stream.c
+++ b/libmailutils/message_stream.c
@@ -324,11 +324,12 @@ _message_error_string (struct _mu_stream *stream, int rc)
 }
 
 int
-mu_message_stream_create (mu_stream_t *stream, mu_stream_t src, int flags)
+mu_message_stream_create (mu_stream_t *pstream, mu_stream_t src, int flags)
 {
   struct _mu_message_stream *s;
   int sflag;
   int rc;
+  mu_stream_t stream;
   
   mu_stream_get_flags (src, &sflag);
   sflag &= MU_STREAM_SEEK;
@@ -355,8 +356,14 @@ mu_message_stream_create (mu_stream_t *stream, mu_stream_t src, int flags)
   s->stream.size = _message_size;
   s->stream.seek = _message_seek;
   s->stream.error_string = _message_error_string;
-  *stream = (mu_stream_t)s;
-  return 0;  
+
+  stream = (mu_stream_t)s;
+  rc = mu_stream_open (stream);
+  if (rc)
+    mu_stream_destroy (&stream);
+  else
+    *pstream = stream;
+  return rc;
 }
 
 
@@ -392,12 +399,6 @@ mu_stream_to_message (mu_stream_t instream, mu_message_t *pmsg)
   if ((rc = mu_message_stream_create (&draftstream, instream, 0)))
     return rc;
 
-  if ((rc = mu_stream_open (draftstream)))
-    {
-      mu_stream_destroy (&draftstream);
-      return rc;
-    }
-
   if ((rc = mu_message_create (&msg, draftstream)))
     {
       mu_stream_destroy (&draftstream);
diff --git a/libmailutils/mimehdr.c b/libmailutils/mimehdr.c
index 0314734..dd5eb34 100644
--- a/libmailutils/mimehdr.c
+++ b/libmailutils/mimehdr.c
@@ -525,10 +525,6 @@ mu_mimehdr_decode_param (const char *value, int flags,
 		  if (rc)
 		    break;
 		  
-		  rc = mu_stream_open (cvt);
-		  if (rc)
-		    break;
-
 		  while (mu_stream_read (cvt, iobuf, sizeof (iobuf),
 					 &nbytes) == 0
 			 && nbytes)
diff --git a/libmailutils/mutil.c b/libmailutils/mutil.c
index aa11d56..5342523 100644
--- a/libmailutils/mutil.c
+++ b/libmailutils/mutil.c
@@ -1333,13 +1333,8 @@ mu_decode_filter (mu_stream_t *pfilter, mu_stream_t input,
 				       0, mu_default_fallback_mode);
       if (status == 0)
 	{
-	  if (mu_stream_open (cvt))
-	    mu_stream_destroy (&cvt);
-	  else
-            {
-              mu_stream_unref (filter);
-              filter = cvt;
-	    }
+          mu_stream_unref (filter);
+          filter = cvt;
 	}
     }
   *pfilter = filter;
diff --git a/libmailutils/prog_stream.c b/libmailutils/prog_stream.c
index 7ef63a3..b789def 100644
--- a/libmailutils/prog_stream.c
+++ b/libmailutils/prog_stream.c
@@ -307,12 +307,6 @@ _prog_open (mu_stream_t stream)
 	  _prog_close (stream);
 	  return rc;
 	}
-      rc = mu_stream_open (fs->in);
-      if (rc)
-	{
-	  _prog_close (stream);
-	  return rc;
-	}
     }
   
   if (REDIRECT_STDIN_P (flags))
@@ -324,12 +318,6 @@ _prog_open (mu_stream_t stream)
 	  _prog_close (stream);
 	  return rc;
 	}
-      rc = mu_stream_open (fs->out);
-      if (rc)
-	{
-	  _prog_close (stream);
-	  return rc;
-	}
     }
 
   _prog_stream_register (fs);
@@ -428,21 +416,32 @@ _prog_stream_create (const char *progname, int flags)
 int
 mu_prog_stream_create (mu_stream_t *pstream, const char *progname, int flags)
 {
+  int rc;
+  mu_stream_t stream;
+  
   if (pstream == NULL)
     return MU_ERR_OUT_PTR_NULL;
 
   if (progname == NULL)
     return EINVAL;
 
-  if ((*pstream = (mu_stream_t) _prog_stream_create (progname, flags)) == NULL)
+  if ((stream = (mu_stream_t) _prog_stream_create (progname, flags)) == NULL)
     return ENOMEM;
-  return 0;
+
+  rc = mu_stream_open (stream);
+  if (rc)
+    mu_stream_destroy (&stream);
+  else
+    *pstream = stream;
+  return rc;
 }
 
 int
 mu_filter_prog_stream_create (mu_stream_t *pstream, const char *progname,
 			      mu_stream_t input)
 {
+  int rc;
+  mu_stream_t stream;
   struct _mu_prog_stream *fs;
 
   if (pstream == NULL)
@@ -456,7 +455,12 @@ mu_filter_prog_stream_create (mu_stream_t *pstream, const char *progname,
     return ENOMEM;
   mu_stream_ref (input);
   fs->input = input;
-  *pstream = (mu_stream_t) fs;
-  return 0;
+  stream = (mu_stream_t) fs;
+  rc = mu_stream_open (stream);
+  if (rc)
+    mu_stream_destroy (&stream);
+  else
+    *pstream = stream;
+  return rc;
 }
 
diff --git a/libmailutils/rdcache_stream.c b/libmailutils/rdcache_stream.c
index 4789811..c1d0013 100644
--- a/libmailutils/rdcache_stream.c
+++ b/libmailutils/rdcache_stream.c
@@ -183,7 +183,7 @@ mu_rdcache_stream_create (mu_stream_t *pstream, mu_stream_t transport,
     return EINVAL;
   
   sp = (struct _mu_rdcache_stream *)
-          _mu_stream_create (sizeof (*sp), sflags);
+          _mu_stream_create (sizeof (*sp), sflags | _MU_STR_OPEN);
   if (!sp)
     return ENOMEM;
 
@@ -200,8 +200,7 @@ mu_rdcache_stream_create (mu_stream_t *pstream, mu_stream_t transport,
     mu_stream_ref (transport);
   sp->transport = transport;
 
-  if ((rc = mu_memory_stream_create (&sp->cache, MU_STREAM_RDWR))
-      || (rc = mu_stream_open (sp->cache)))
+  if ((rc = mu_memory_stream_create (&sp->cache, MU_STREAM_RDWR)))
     {
       mu_stream_destroy ((mu_stream_t*) &sp);
       return rc;
diff --git a/libmailutils/socket_stream.c b/libmailutils/socket_stream.c
index 2764b2b..825a9da 100644
--- a/libmailutils/socket_stream.c
+++ b/libmailutils/socket_stream.c
@@ -117,6 +117,10 @@ mu_socket_stream_create (mu_stream_t *pstream, const char *filename, int flags)
       transport = str;
     }
 
-  *pstream = transport;
-  return 0;
+  rc = mu_stream_open (transport);
+  if (rc)
+    mu_stream_unref (transport);
+  else
+    *pstream = transport;
+  return rc;
 }
diff --git a/libmailutils/stdio_stream.c b/libmailutils/stdio_stream.c
index f525d9c..f9a5660 100644
--- a/libmailutils/stdio_stream.c
+++ b/libmailutils/stdio_stream.c
@@ -63,6 +63,7 @@ mu_stdio_stream_create (mu_stream_t *pstream, int fd, int flags)
 			       filename, fd, flags & ~MU_STREAM_SEEK);
   if (rc)
     return rc;
+  fstr->stream.flags |= _MU_STR_OPEN;
   fstr->stream.open = NULL;
   transport = (mu_stream_t) fstr;
   mu_stream_set_buffer (transport, mu_buffer_line, 0);
diff --git a/libmailutils/stream.c b/libmailutils/stream.c
index 92faeb4..2dfbc82 100644
--- a/libmailutils/stream.c
+++ b/libmailutils/stream.c
@@ -249,7 +249,7 @@ _mu_stream_create (size_t size, int flags)
   if (size < sizeof (str))
     abort ();
   str = mu_zalloc (size);
-  str->flags = flags & ~_MU_STR_INTERN_MASK;
+  str->flags = flags & ~(_MU_STR_INTERN_MASK & ~_MU_STR_OPEN);
   mu_stream_ref (str);
   return str;
 }
@@ -289,21 +289,34 @@ mu_stream_unref (mu_stream_t stream)
   mu_stream_destroy (&stream);
 }
 
+static void
+_stream_init (mu_stream_t stream)
+{
+  stream->bytes_in = stream->bytes_out = 0;
+  stream->flags &= ~_MU_STR_INTERN_MASK;
+  _stream_setflag (stream, _MU_STR_OPEN);
+  stream->offset = 0;
+  stream->level = stream->pos = 0;
+  stream->last_err = 0;
+}  
+
 int
 mu_stream_open (mu_stream_t stream)
 {
   int rc;
 
+  if (stream->flags & _MU_STR_OPEN)
+    return MU_ERR_OPEN;
   if (stream->open)
     {
       if ((rc = stream->open (stream)))
 	return mu_stream_seterr (stream, rc, 1);
-      if ((stream->flags & (MU_STREAM_APPEND|MU_STREAM_SEEK)) ==
-	  (MU_STREAM_APPEND|MU_STREAM_SEEK) &&
-	  (rc = mu_stream_seek (stream, 0, MU_SEEK_END, NULL)))
-	return mu_stream_seterr (stream, rc, 1);
     }
-  stream->bytes_in = stream->bytes_out = 0;
+  _stream_init (stream);
+  if ((stream->flags & (MU_STREAM_APPEND|MU_STREAM_SEEK)) ==
+      (MU_STREAM_APPEND|MU_STREAM_SEEK) &&
+      (rc = mu_stream_seek (stream, 0, MU_SEEK_END, NULL)))
+    return mu_stream_seterr (stream, rc, 1);
   return 0;
 }
 
@@ -345,12 +358,25 @@ mu_stream_eof (mu_stream_t stream)
 }
 
 int
+mu_stream_is_open (mu_stream_t stream)
+{
+  return stream->flags & _MU_STR_OPEN;
+}
+
+int
 mu_stream_seek (mu_stream_t stream, mu_off_t offset, int whence,
 		mu_off_t *pres)
 {    
   int rc;
   mu_off_t size;
   
+  if (!(stream->flags & _MU_STR_OPEN))
+    {
+      if (stream->open)
+	return MU_ERR_NOT_OPEN;
+      _stream_init (stream);
+    }
+  
   if (!stream->seek)
     return mu_stream_seterr (stream, ENOSYS, 0);
 
@@ -654,6 +680,13 @@ _stream_write_unbuffered (mu_stream_t stream,
 int
 mu_stream_read (mu_stream_t stream, void *buf, size_t size, size_t *pread)
 {
+  if (!(stream->flags & _MU_STR_OPEN))
+    {
+      if (stream->open)
+	return MU_ERR_NOT_OPEN;
+      _stream_init (stream);
+    }
+  
   if (stream->buftype == mu_buffer_none)
     return _stream_read_unbuffered (stream, buf, size, !pread, pread);
   else
@@ -770,6 +803,13 @@ mu_stream_readdelim (mu_stream_t stream, char *buf, size_t size,
   if (size == 0)
     return EINVAL;
 
+  if (!(stream->flags & _MU_STR_OPEN))
+    {
+      if (stream->open)
+	return MU_ERR_NOT_OPEN;
+      _stream_init (stream);
+    }
+
   if (stream->buftype == mu_buffer_none)
     {
       if (stream->readdelim)
@@ -803,6 +843,13 @@ mu_stream_getdelim (mu_stream_t stream, char **pbuf, size_t *psize,
   size_t n = *psize;
   size_t cur_len = 0;
     
+  if (!(stream->flags & _MU_STR_OPEN))
+    {
+      if (stream->open)
+	return MU_ERR_NOT_OPEN;
+      _stream_init (stream);
+    }
+
   if (lineptr == NULL || n == 0)
     {
       char *new_lineptr;
@@ -883,6 +930,13 @@ mu_stream_write (mu_stream_t stream, const void *buf, size_t size,
 {
   int rc = 0;
   
+  if (!(stream->flags & _MU_STR_OPEN))
+    {
+      if (stream->open)
+	return MU_ERR_NOT_OPEN;
+      _stream_init (stream);
+    }
+
   if (stream->buftype == mu_buffer_none)
     rc = _stream_write_unbuffered (stream, buf, size,
 				   !pnwritten, pnwritten);
@@ -937,6 +991,12 @@ mu_stream_flush (mu_stream_t stream)
   
   if (!stream)
     return EINVAL;
+  if (!(stream->flags & _MU_STR_OPEN))
+    {
+      if (stream->open)
+	return MU_ERR_NOT_OPEN;
+      _stream_init (stream);
+    }
   rc = _stream_flush_buffer (stream, 1);
   if (rc)
     return rc;
@@ -953,6 +1013,8 @@ mu_stream_close (mu_stream_t stream)
     
   if (!stream)
     return EINVAL;
+  if (!(stream->flags & _MU_STR_OPEN))
+    return MU_ERR_NOT_OPEN;
   
   mu_stream_flush (stream);
   /* Do close the stream only if it is not used by anyone else */
@@ -961,6 +1023,7 @@ mu_stream_close (mu_stream_t stream)
   _stream_event (stream, _MU_STR_EVENT_CLOSE, 0, NULL);
   if (stream->close)
     rc = stream->close (stream);
+  _stream_clrflag (stream, _MU_STR_OPEN);
   return rc;
 }
 
@@ -969,6 +1032,12 @@ mu_stream_size (mu_stream_t stream, mu_off_t *psize)
 {
   int rc;
     
+  if (!(stream->flags & _MU_STR_OPEN))
+    {
+      if (stream->open)
+	return MU_ERR_NOT_OPEN;
+      _stream_init (stream);
+    }
   if (!stream->size)
     return mu_stream_seterr (stream, ENOSYS, 0);
   rc = stream->size (stream, psize);
@@ -999,9 +1068,21 @@ int
 mu_stream_wait (mu_stream_t stream, int *pflags, struct timeval *tvp)
 {
   int flg = 0;
+
   if (stream == NULL)
     return EINVAL;
-  
+#if 0
+  /* NOTE: Sometimes mu_stream_wait is called after a failed mu_stream_open.
+     In particular, this is needed for a TCP stream opened with a
+     MU_STREAM_NONBLOCK flag (see examples/http.c).  Until a better
+     solution is found, this check is commented out. */
+  if (!(stream->flags & _MU_STR_OPEN))
+    {
+      if (stream->open)
+	return MU_ERR_NOT_OPEN;
+      _stream_init (stream);
+    }
+#endif
   /* Take to acount if we have any buffering.  */
   /* FIXME: How about MU_STREAM_READY_WR? */
   if ((*pflags) & MU_STREAM_READY_RD 
@@ -1026,6 +1107,13 @@ mu_stream_wait (mu_stream_t stream, int *pflags, struct timeval *tvp)
 int
 mu_stream_truncate (mu_stream_t stream, mu_off_t size)
 {
+  if (!(stream->flags & _MU_STR_OPEN))
+    {
+      if (stream->open)
+	return MU_ERR_NOT_OPEN;
+      _stream_init (stream);
+    }
+  
   if (stream->truncate)
     {
       int rc;
@@ -1040,6 +1128,13 @@ mu_stream_truncate (mu_stream_t stream, mu_off_t size)
 int
 mu_stream_shutdown (mu_stream_t stream, int how)
 {
+  if (!(stream->flags & _MU_STR_OPEN))
+    {
+      if (stream->open)
+	return MU_ERR_NOT_OPEN;
+      _stream_init (stream);
+    }
+  
   if (stream->shutdown)
     return stream->shutdown (stream, how);
   return ENOSYS;
diff --git a/libmailutils/streamref.c b/libmailutils/streamref.c
index 83f401e..e860a55 100644
--- a/libmailutils/streamref.c
+++ b/libmailutils/streamref.c
@@ -272,7 +272,8 @@ mu_streamref_create_abridged (mu_stream_t *pref, mu_stream_t str,
     return rc;
   mu_stream_get_flags (str, &flags);
   sp = (struct _mu_streamref *)
-         _mu_stream_create (sizeof (*sp), flags & ~MU_STREAM_AUTOCLOSE);
+         _mu_stream_create (sizeof (*sp), 
+                            (flags & ~MU_STREAM_AUTOCLOSE) | _MU_STR_OPEN);
   if (!sp)
     return ENOMEM;
 
diff --git a/libmailutils/tcp.c b/libmailutils/tcp.c
index ba15ae8..aea7809 100644
--- a/libmailutils/tcp.c
+++ b/libmailutils/tcp.c
@@ -304,11 +304,13 @@ _create_tcp_stream (int flags)
 }
 
 int
-mu_tcp_stream_create_with_source_ip (mu_stream_t *stream,
+mu_tcp_stream_create_with_source_ip (mu_stream_t *pstream,
 				     const char *host, int port,
 				     unsigned long source_ip,
 				     int flags)
 {
+  int rc;
+  mu_stream_t stream;
   struct _tcp_instance *tcp;
 
   if (host == NULL)
@@ -329,8 +331,13 @@ mu_tcp_stream_create_with_source_ip (mu_stream_t *stream,
   tcp->port = port;
   tcp->state = TCP_STATE_INIT;
   tcp->source_addr = source_ip;
-  *stream = (mu_stream_t) tcp;
-  return 0;
+  stream = (mu_stream_t) tcp;
+  rc = mu_stream_open (stream);
+  if (rc == 0 || rc == EAGAIN || rc == EINPROGRESS)
+    *pstream = stream;
+  else
+    mu_stream_destroy (&stream);
+  return rc;
 }
 
 int
diff --git a/libmailutils/temp_file_stream.c b/libmailutils/temp_file_stream.c
index c5346b2..857bada 100644
--- a/libmailutils/temp_file_stream.c
+++ b/libmailutils/temp_file_stream.c
@@ -49,7 +49,7 @@ mu_temp_file_stream_create (mu_stream_t *pstream, const char *dir)
 {
   int rc;
   struct _mu_file_stream *str;
-
+  mu_stream_t stream;
   rc = _mu_file_stream_create (&str,
 			       sizeof (struct _mu_file_stream),
 			       dir,
@@ -57,9 +57,16 @@ mu_temp_file_stream_create (mu_stream_t *pstream, const char *dir)
 			       MU_STREAM_RDWR | MU_STREAM_SEEK |
 			       MU_STREAM_CREAT | 
 			       MU_STREAM_AUTOCLOSE);
-
-  str->stream.open = fd_temp_open;
-  str->flags = _MU_FILE_STREAM_TEMP;
-  *pstream = (mu_stream_t) str;
+  if (rc == 0)
+    {
+      str->stream.open = fd_temp_open;
+      str->flags = _MU_FILE_STREAM_TEMP;
+      stream = (mu_stream_t) str;
+      rc = mu_stream_open (stream);
+      if (rc)
+	mu_stream_unref (stream);
+      else
+	*pstream = stream;
+    }
   return 0;
 }
diff --git a/libmailutils/tests/fltst.c b/libmailutils/tests/fltst.c
index 6e36ede..8597dd5 100644
--- a/libmailutils/tests/fltst.c
+++ b/libmailutils/tests/fltst.c
@@ -153,10 +153,7 @@ main (int argc, char * argv [])
     }
   
   MU_ASSERT (mu_stdio_stream_create (&in, MU_STDIN_FD, 0));
-  MU_ASSERT (mu_stream_open (in));
-
   MU_ASSERT (mu_stdio_stream_create (&out, MU_STDOUT_FD, 0));
-  MU_ASSERT (mu_stream_open (out));
 
   if (line_length_option)
     reset_line_length (fltname, line_length);
diff --git a/libmailutils/tests/mailcap.c b/libmailutils/tests/mailcap.c
index c4f8818..136b971 100644
--- a/libmailutils/tests/mailcap.c
+++ b/libmailutils/tests/mailcap.c
@@ -42,14 +42,6 @@ main (int argc, char **argv)
       exit (1);
     }
 
-  status = mu_stream_open (stream);
-  if (status)
-    {
-      mu_error ("cannot open input stream: %s",
-		mu_strerror (status));
-      exit (1);
-    }
-
   status = mu_mailcap_create (&mailcap, stream);
   if (status == 0)
     {
diff --git a/libmailutils/vartab.c b/libmailutils/vartab.c
index 7364ba8..566a507 100644
--- a/libmailutils/vartab.c
+++ b/libmailutils/vartab.c
@@ -227,7 +227,6 @@ mu_vartab_expand (mu_vartab_t vt, const char *str, char **pres)
       rc = mu_memory_stream_create (&vt->stream, 0);
       if (rc)
 	return rc;
-      rc = mu_stream_open (vt->stream);
     }
   else
     mu_stream_truncate (vt->stream, 0);
diff --git a/libmailutils/wicket.c b/libmailutils/wicket.c
index 82b179b..8b0fbbb 100644
--- a/libmailutils/wicket.c
+++ b/libmailutils/wicket.c
@@ -249,72 +249,69 @@ get_ticket_url (mu_ticket_t ticket, mu_url_t url, mu_url_t *pticket_url)
   struct file_ticket *ft = mu_ticket_get_data (ticket);
   int rc;
   mu_url_t u = NULL;
+  char *buf = NULL;
+  size_t bufsize = 0;
+  size_t len;
   
   rc = mu_file_stream_create (&stream, ft->filename, MU_STREAM_READ);
   if (rc)
     return rc;
-  rc = mu_stream_open (stream);
-  if (rc == 0)
+    
+  while ((rc = mu_stream_getline (stream, &buf, &bufsize, &len)) == 0
+	 && len > 0)
     {
-      char *buf = NULL;
-      size_t bufsize = 0;
-      size_t len;
-
-      while ((rc = mu_stream_getline (stream, &buf, &bufsize, &len)) == 0
-	     && len > 0)
+      char *p;
+      int err;
+      
+      /* Truncate a trailing newline. */
+      if (len && buf[len - 1] == '\n')
+	buf[--len] = 0;
+      
+      /* Skip leading spaces  */
+      for (p = buf; *p == ' ' || *p == '\t'; p++)
+	;
+      /* Skip trailing spaces */
+      for (; len > 0 && (p[len-1] == ' ' || p[len-1] == '\t'); )
+	p[--len] = 0;
+      
+      /* Skip empty lines and comments. */
+      if (*p == 0 || *p == '#')
+	continue;
+      
+      if ((err = mu_url_create (&u, p)) != 0)
 	{
-	  char *p;
-	  int err;
-	  
-	  /* Truncate a trailing newline. */
-	  if (len && buf[len - 1] == '\n')
-	    buf[--len] = 0;
-
-	  /* Skip leading spaces  */
-	  for (p = buf; *p == ' ' || *p == '\t'; p++)
-	    ;
-	  /* Skip trailing spaces */
-	  for (; len > 0 && (p[len-1] == ' ' || p[len-1] == '\t'); )
-	    p[--len] = 0;
-	  
-	  /* Skip empty lines and comments. */
-	  if (*p == 0 || *p == '#')
-	    continue;
-
-	  if ((err = mu_url_create (&u, p)) != 0)
-	    {
-	      /* Skip erroneous entry */
-	      /* FIXME: Error message */
-	      continue;
-	    }
-	  if ((err = mu_url_parse (u)) != 0)
-	    {
-	      /* FIXME: See above */
-	      mu_url_destroy (&u);
-	      continue;
-	    }
-	  
-	  if (!mu_url_is_ticket (u, url))
+	  /* Skip erroneous entry */
+	  /* FIXME: Error message */
+	  continue;
+	}
+      if ((err = mu_url_parse (u)) != 0)
+	{
+	  /* FIXME: See above */
+	  mu_url_destroy (&u);
+	  continue;
+	}
+      
+      if (!mu_url_is_ticket (u, url))
+	{
+	  mu_url_destroy (&u);
+	  continue;
+	}
+      
+      if (ft->user)
+	{
+	  if (u->name && strcmp (u->name, "*") != 0
+	      && strcmp (ft->user, u->name) != 0)
 	    {
 	      mu_url_destroy (&u);
 	      continue;
 	    }
-	  
-	  if (ft->user)
-	    {
-	      if (u->name && strcmp (u->name, "*") != 0
-		  && strcmp (ft->user, u->name) != 0)
-		{
-		  mu_url_destroy (&u);
-		  continue;
-		}
-	    }
-	  
-	  break;
 	}
-      mu_stream_close (stream);
-      free (buf);
+      
+      break;
     }
+  mu_stream_close (stream);
+  free (buf);
+    
   mu_stream_destroy (&stream);
 
   if (rc == 0)
diff --git a/libmailutils/xscript-stream.c b/libmailutils/xscript-stream.c
index b8639c2..ff5d796 100644
--- a/libmailutils/xscript-stream.c
+++ b/libmailutils/xscript-stream.c
@@ -402,7 +402,8 @@ mu_xscript_stream_create(mu_stream_t *pref, mu_stream_t transport,
   struct _mu_xscript_stream *sp;
 
   mu_stream_get_flags (transport, &flags);
-  sp = (struct _mu_xscript_stream *) _mu_stream_create (sizeof (*sp), flags);
+  sp = (struct _mu_xscript_stream *) _mu_stream_create (sizeof (*sp),
+                                                        flags | _MU_STR_OPEN);
   if (!sp)
     return ENOMEM;
 
diff --git a/libmu_argp/muinit.c b/libmu_argp/muinit.c
index 5471116..85b65ed 100644
--- a/libmu_argp/muinit.c
+++ b/libmu_argp/muinit.c
@@ -133,7 +133,6 @@ mu_app_init (struct argp *myargp, const char **capa,
       char *canonical_name = get_canonical_name ();
       mu_stream_t stream;
       mu_stdio_stream_create (&stream, MU_STDOUT_FD, 0);
-      mu_stream_open (stream);
       asprintf (&comment,
 		"Configuration file structure for %s utility.",
 		mu_program_name);
diff --git a/libmu_auth/ldap.c b/libmu_auth/ldap.c
index 5ced397..419ea6c 100644
--- a/libmu_auth/ldap.c
+++ b/libmu_auth/ldap.c
@@ -597,7 +597,6 @@ chk_md5 (const char *db_pass, const char *pass)
   mu_memory_stream_create (&str, 0);
   mu_filter_create (&flt, str, "base64", MU_FILTER_DECODE,
 		    MU_STREAM_READ | MU_STREAM_AUTOCLOSE);
-  mu_stream_open (str);
   mu_stream_write (str, db_pass, strlen (db_pass), NULL);
 
   mu_stream_read (flt, (char*) d1, sizeof d1, NULL);
@@ -620,7 +619,6 @@ chk_smd5 (const char *db_pass, const char *pass)
   mu_memory_stream_create (&str, 0);
   mu_filter_create (&flt, str, "base64", MU_FILTER_DECODE,
 		    MU_STREAM_READ | MU_STREAM_AUTOCLOSE);
-  mu_stream_open (str);
   size = strlen (db_pass);
   mu_stream_write (str, db_pass, size, NULL);
 
@@ -666,7 +664,6 @@ chk_sha (const char *db_pass, const char *pass)
   mu_memory_stream_create (&str, 0);
   mu_filter_create (&flt, str, "base64", MU_FILTER_DECODE,
 		    MU_STREAM_READ | MU_STREAM_AUTOCLOSE);
-  mu_stream_open (str);
   mu_stream_write (str, db_pass, strlen (db_pass), NULL);
 
   mu_stream_read (flt, (char*) d1, sizeof d1, NULL);
@@ -689,7 +686,6 @@ chk_ssha (const char *db_pass, const char *pass)
   mu_memory_stream_create (&str, 0);
   mu_filter_create (&flt, str, "base64", MU_FILTER_DECODE,
 		    MU_STREAM_READ | MU_STREAM_AUTOCLOSE);
-  mu_stream_open (str);
   size = strlen (db_pass);
   mu_stream_write (str, db_pass, size, NULL);
 
diff --git a/libmu_auth/tls.c b/libmu_auth/tls.c
index e31b856..e6e4551 100644
--- a/libmu_auth/tls.c
+++ b/libmu_auth/tls.c
@@ -292,7 +292,7 @@ _mu_tls_io_stream_create (mu_stream_t *pstream,
   struct _mu_tls_io_stream *sp;
 
   sp = (struct _mu_tls_io_stream *)
-    _mu_stream_create (sizeof (*sp), flags & MU_STREAM_RDWR);
+    _mu_stream_create (sizeof (*sp), (flags & MU_STREAM_RDWR) | _MU_STR_OPEN);
   if (!sp)
     return ENOMEM;
 
@@ -617,6 +617,7 @@ _mu_tls_stream_create (mu_stream_t *pstream,
   struct _mu_tls_stream *sp;
   int autoclose = flags & MU_STREAM_AUTOCLOSE;
   int rc;
+  mu_stream_t stream;
   
   sp = (struct _mu_tls_stream *)
     _mu_stream_create (sizeof (*sp), MU_STREAM_RDWR);
@@ -653,9 +654,14 @@ _mu_tls_stream_create (mu_stream_t *pstream,
       return rc;
     }
 
-  mu_stream_set_buffer ((mu_stream_t) sp, mu_buffer_line, 0);
-  *pstream = (mu_stream_t) sp;
-  return 0;
+  stream = (mu_stream_t) sp;
+  mu_stream_set_buffer (stream, mu_buffer_line, 0);
+  rc = mu_stream_open (stream);
+  if (rc)
+    mu_stream_destroy (&stream);
+  else
+    *pstream = stream;
+  return rc;
 }
 
 int
diff --git a/libmu_sieve/extensions/pipe.c b/libmu_sieve/extensions/pipe.c
index 1378461..664997d 100644
--- a/libmu_sieve/extensions/pipe.c
+++ b/libmu_sieve/extensions/pipe.c
@@ -94,9 +94,6 @@ sieve_action_pipe (mu_sieve_machine_t mach, mu_list_t args, mu_list_t tags)
       rc = mu_prog_stream_create (&pstr, cmd, MU_STREAM_WRITE);
       ONERR (rc, _("cannot create command stream"), cmd);
 
-      rc = mu_stream_open (pstr);
-      ONERR (rc, _("cannot open command stream"), cmd);
-
       if (mu_sieve_tag_lookup (tags, "envelope", &val))
 	{
 	  char *p;
diff --git a/libmu_sieve/extensions/spamd.c b/libmu_sieve/extensions/spamd.c
index e620e58..b679cd3 100644
--- a/libmu_sieve/extensions/spamd.c
+++ b/libmu_sieve/extensions/spamd.c
@@ -52,12 +52,6 @@ spamd_connect_tcp (mu_sieve_machine_t mach, mu_stream_t *stream,
       mu_sieve_error (mach, "mu_tcp_stream_create: %s", mu_strerror (rc));
       return rc;
     }
-  rc = mu_stream_open (*stream);
-  if (rc)
-    {
-      mu_sieve_error (mach, "opening tcp stream: %s", mu_strerror (rc));
-      mu_stream_destroy (stream);
-    }
   return rc;
 }
 
@@ -70,13 +64,6 @@ spamd_connect_socket (mu_sieve_machine_t mach, mu_stream_t *stream, char *path)
       mu_sieve_error (mach, "mu_socket_stream_create: %s", mu_strerror (rc));
       return rc;
     }
-  rc = mu_stream_open (*stream);
-  if (rc)
-    {
-      mu_sieve_error (mach, "opening socket stream: %s", mu_strerror (rc));
-      mu_stream_destroy (stream);
-    }
-
   return rc;
 }
 
diff --git a/libmu_sieve/extensions/vacation.c b/libmu_sieve/extensions/vacation.c
index 6b220df..a41103a 100644
--- a/libmu_sieve/extensions/vacation.c
+++ b/libmu_sieve/extensions/vacation.c
@@ -71,17 +71,6 @@ build_mime (mu_sieve_machine_t mach, mu_list_t tags, mu_mime_t *pmime,
       return 1;
     }
 
-  if ((rc = mu_stream_open (input)))
-    {
-      mu_sieve_error (mach,
-		      _("cannot open temporary stream: %s"),
-		      mu_strerror (rc));
-      mu_mime_destroy (&mime);
-      mu_message_destroy (&newmsg, NULL);
-      mu_stream_destroy (&input);
-      return 1;
-    }
-  
   mu_stream_write (input, text, strlen (text), NULL);
 
   if (mu_sieve_tag_lookup (tags, "mime", NULL))
diff --git a/libproto/imap/folder.c b/libproto/imap/folder.c
index 4f27c49..e555c12 100644
--- a/libproto/imap/folder.c
+++ b/libproto/imap/folder.c
@@ -661,7 +661,6 @@ folder_imap_open (mu_folder_t folder, int flags)
             }
 	  status = mu_memory_stream_create (&f_imap->string.stream, NULL, MU_STREAM_RDWR);
           CHECK_ERROR (f_imap, status);
-	  mu_stream_open (f_imap->string.stream);
         }
       else
         {
@@ -684,10 +683,6 @@ folder_imap_open (mu_folder_t folder, int flags)
 	    {
 	      mu_stream_t newstr;
 
-	      status = mu_stream_open (folder->stream);
-	      CHECK_EAGAIN (f_imap, status);
-	      CHECK_ERROR_CLOSE (folder, f_imap, status);
-
 	      status = mu_tls_client_stream_create (&newstr, 
 	                                            folder->stream,
 	                                            folder->stream, 0);
@@ -712,10 +707,13 @@ folder_imap_open (mu_folder_t folder, int flags)
 
     case IMAP_OPEN_CONNECTION:
       /* Establish the connection.  */
-      status = mu_stream_open (folder->stream);
-      CHECK_EAGAIN (f_imap, status);
-      /* Can't recover bailout.  */
-      CHECK_ERROR_CLOSE (folder, f_imap, status);
+      if (!mu_stream_is_open (folder->stream))
+        {
+          status = mu_stream_open (folder->stream);
+          CHECK_EAGAIN (f_imap, status);
+          /* Can't recover bailout.  */
+          CHECK_ERROR_CLOSE (folder, f_imap, status);
+        }
       f_imap->state = IMAP_GREETINGS;
 
     case IMAP_GREETINGS:
diff --git a/libproto/mailer/smtp.c b/libproto/mailer/smtp.c
index 742f95d..ae78cae 100644
--- a/libproto/mailer/smtp.c
+++ b/libproto/mailer/smtp.c
@@ -17,10 +17,6 @@
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301 USA */
 
-/** @file smtp.c
-@brief an SMTP mailer
-*/
-/* FIXME: Bufferization stuff is spurious. Remove it */
 #ifdef HAVE_CONFIG_H
 # include <config.h>
 #endif
@@ -175,9 +171,6 @@ smtp_open (mu_mailer_t mailer, int flags)
       if (rc)
 	return rc;
       mu_stream_set_buffer (mailer->stream, mu_buffer_line, 0);
-      rc = mu_stream_open (mailer->stream);
-      if (rc)
-	return rc;
     }
   mu_smtp_set_carrier (smtp_mailer->smtp, mailer->stream);
   /* FIXME: Unref the stream */
diff --git a/libproto/mailer/smtp_gsasl.c b/libproto/mailer/smtp_gsasl.c
index f957c87..d43631f 100644
--- a/libproto/mailer/smtp_gsasl.c
+++ b/libproto/mailer/smtp_gsasl.c
@@ -148,8 +148,8 @@ insert_gsasl_stream (mu_smtp_t smtp, Gsasl_session *sess_ctx)
 {
   mu_stream_t stream[2], newstream[2];
   int rc;
-  
-  rc = mu_stream_ioctl (smtp->carrier, MU_IOCTL_GET_STREAM, stream);
+
+  rc = _mu_smtp_get_streams (smtp, stream);
   if (rc)
     {
       mu_error ("%s failed: %s", "MU_IOCTL_GET_STREAM",
@@ -158,7 +158,7 @@ insert_gsasl_stream (mu_smtp_t smtp, Gsasl_session *sess_ctx)
     }
 
   rc = gsasl_encoder_stream (&newstream[0], stream[0], sess_ctx,
-			   MU_STREAM_READ);
+			     MU_STREAM_READ);
   if (rc)
     {
       mu_error ("%s failed: %s", "gsasl_encoder_stream",
@@ -179,10 +179,9 @@ insert_gsasl_stream (mu_smtp_t smtp, Gsasl_session *sess_ctx)
   mu_stream_flush (stream[1]);
   mu_stream_unref (stream[0]);
   mu_stream_unref (stream[1]);
+
+  rc = _mu_smtp_set_streams (smtp, newstream);
   
-  rc = mu_stream_ioctl (smtp->carrier, MU_IOCTL_SET_STREAM, newstream);
-  mu_stream_unref (newstream[0]);
-  mu_stream_unref (newstream[1]);
   if (rc)
     {
       mu_error ("%s failed when it should not: %s",
diff --git a/libproto/mailer/smtp_starttls.c b/libproto/mailer/smtp_starttls.c
index 0e4c7f1..8c0662c 100644
--- a/libproto/mailer/smtp_starttls.c
+++ b/libproto/mailer/smtp_starttls.c
@@ -27,8 +27,8 @@
 #include <mailutils/sys/smtp.h>
 #include <mailutils/tls.h>
 
-static int
-smtp_get_streams (mu_smtp_t smtp, mu_stream_t *streams)
+int
+_mu_smtp_get_streams (mu_smtp_t smtp, mu_stream_t *streams)
 {
   int rc;
   
@@ -45,8 +45,8 @@ smtp_get_streams (mu_smtp_t smtp, mu_stream_t *streams)
   return rc;
 }
 
-static int
-smtp_set_streams (mu_smtp_t smtp, mu_stream_t *streams)
+int
+_mu_smtp_set_streams (mu_smtp_t smtp, mu_stream_t *streams)
 {
   int rc;
   
@@ -100,7 +100,7 @@ mu_smtp_starttls (mu_smtp_t smtp)
     return MU_ERR_FAILURE;
 
   mu_stream_flush (smtp->carrier);
-  status = smtp_get_streams (smtp, streams);
+  status = _mu_smtp_get_streams (smtp, streams);
   MU_SMTP_CHECK_ERROR (smtp, status);
   
   status = mu_tls_client_stream_create (&tlsstream,
@@ -108,14 +108,8 @@ mu_smtp_starttls (mu_smtp_t smtp)
   mu_stream_unref (streams[0]);
   mu_stream_unref (streams[1]);
   MU_SMTP_CHECK_ERROR (smtp, status);
-  status = mu_stream_open (tlsstream);
-  if (status)
-    {
-      mu_stream_destroy (&tlsstream);
-      return status;
-    }
   streams[0] = streams[1] = tlsstream;
-  status = smtp_set_streams (smtp, streams);
+  status = _mu_smtp_set_streams (smtp, streams);
   mu_stream_unref (streams[0]);
   mu_stream_unref (streams[1]);
   MU_SMTP_CHECK_ERROR (smtp, status);
diff --git a/libproto/mbox/mbox.c b/libproto/mbox/mbox.c
index 04e548a..6250765 100644
--- a/libproto/mbox/mbox.c
+++ b/libproto/mbox/mbox.c
@@ -99,24 +99,10 @@ mbox_open (mu_mailbox_t mailbox, int flags)
 
       if (status)
 	return status;
-      mu_stream_set_buffer (mailbox->stream, mu_buffer_full, 0);
-      
-      status = mu_stream_open (mailbox->stream);
-      if (status)
-	{
-	  mu_stream_destroy (&mailbox->stream);
-	  return status;
-	}
-      
-      /* FIXME: Set up buffering */
-    }
-  else
-    {
-      status = mu_stream_open (mailbox->stream);
-      if (status != 0)
-	return status;
     }
 
+  mu_stream_set_buffer (mailbox->stream, mu_buffer_full, 0);
+
   MU_DEBUG2 (mailbox->debug, MU_DEBUG_TRACE1, "mbox_open (%s, 0x%x)\n",
 	     mud->name, mailbox->flags);
 
@@ -1416,35 +1402,31 @@ mbox_expunge0 (mu_mailbox_t mailbox, int remove_deleted)
   status = mu_temp_file_stream_create (&tempstr, NULL);
   if (status == 0)
     {
-      status = mu_stream_open (tempstr);
-      if (status == 0)
-	{
-	  sigset_t signalset;
+      sigset_t signalset;
 #ifdef WITH_PTHREAD
-	  int state;
-	  pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &state);
+      int state;
+      pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &state);
 #endif
-	  sigemptyset (&signalset);
-	  sigaddset (&signalset, SIGTERM);
-	  sigaddset (&signalset, SIGHUP);
-	  sigaddset (&signalset, SIGTSTP);
-	  sigaddset (&signalset, SIGINT);
-	  sigaddset (&signalset, SIGWINCH);
-	  sigprocmask (SIG_BLOCK, &signalset, 0);
-	  
-	  status = mbox_expunge_unlocked (mailbox, dirty, remove_deleted,
-					  tempstr);
+      sigemptyset (&signalset);
+      sigaddset (&signalset, SIGTERM);
+      sigaddset (&signalset, SIGHUP);
+      sigaddset (&signalset, SIGTSTP);
+      sigaddset (&signalset, SIGINT);
+      sigaddset (&signalset, SIGWINCH);
+      sigprocmask (SIG_BLOCK, &signalset, 0);
+      
+      status = mbox_expunge_unlocked (mailbox, dirty, remove_deleted,
+				      tempstr);
 
 #ifdef WITH_PTHREAD
-	  pthread_setcancelstate (state, &state);
+      pthread_setcancelstate (state, &state);
 #endif
-	  sigprocmask (SIG_UNBLOCK, &signalset, 0);
+      sigprocmask (SIG_UNBLOCK, &signalset, 0);
       
-	  mu_stream_destroy (&tempstr);
-
-	  if (status == 0)
-	    mbox_reset (mailbox, dirty, remove_deleted);
-	}
+      mu_stream_destroy (&tempstr);
+      
+      if (status == 0)
+	mbox_reset (mailbox, dirty, remove_deleted);
     }
   if (mailbox->locker)
     mu_locker_unlock (mailbox->locker);
diff --git a/libproto/nntp/nntp_connect.c b/libproto/nntp/nntp_connect.c
index d6b9741..959f0b4 100644
--- a/libproto/nntp/nntp_connect.c
+++ b/libproto/nntp/nntp_connect.c
@@ -55,8 +55,11 @@ mu_nntp_connect (mu_nntp_t nntp)
 
     case MU_NNTP_CONNECT:
       /* Establish the connection.  */
-      status = mu_stream_open (nntp->carrier);
-      MU_NNTP_CHECK_EAGAIN (nntp, status);
+      if (!mu_stream_is_open (nntp->carrier))
+        {
+          status = mu_stream_open (nntp->carrier);
+          MU_NNTP_CHECK_EAGAIN (nntp, status);
+        }
       nntp->acknowledge = 0;
       nntp->state = MU_NNTP_GREETINGS;
 
diff --git a/libproto/pop/mbox.c b/libproto/pop/mbox.c
index 0bf17fa..eb25bcc 100644
--- a/libproto/pop/mbox.c
+++ b/libproto/pop/mbox.c
@@ -129,13 +129,6 @@ pop_open (mu_mailbox_t mbox, int flags)
     {
       mu_stream_t newstr;
       
-      status = mu_stream_open (stream);
-      if (status)
-	{
-	  mu_stream_destroy (&stream);
-	  return status;
-	}
-      
       status = mu_tls_client_stream_create (&newstr, stream, stream, 0);
       mu_stream_unref (stream);
       if (status)
@@ -373,12 +366,6 @@ _pop_message_get_stream (struct _pop3_message *mpm, mu_stream_t *pstr)
 		/* FIXME: Try to recover first */
 		break;
 
-	      status = mu_stream_open (mpd->cache);
-	      if (status)
-		{
-		  mu_stream_destroy (&mpd->cache);
-		  break;
-		}
 	      mu_stream_set_buffer (mpd->cache, mu_buffer_full, 8192);
 	    }
 
diff --git a/libproto/pop/pop3_connect.c b/libproto/pop/pop3_connect.c
index 9377e84..5b9ffab 100644
--- a/libproto/pop/pop3_connect.c
+++ b/libproto/pop/pop3_connect.c
@@ -69,9 +69,12 @@ mu_pop3_connect (mu_pop3_t pop3)
 
     case MU_POP3_CONNECT:
       /* Establish the connection.  */
-      status = mu_stream_open (pop3->carrier);
-      MU_POP3_CHECK_EAGAIN (pop3, status);
-      MU_POP3_FCLR (pop3, MU_POP3_ACK);
+      if (!mu_stream_is_open (pop3->carrier))
+        {
+          status = mu_stream_open (pop3->carrier);
+          MU_POP3_CHECK_EAGAIN (pop3, status);
+          MU_POP3_FCLR (pop3, MU_POP3_ACK);
+        }
       pop3->state = MU_POP3_GREETINGS;
 
     case MU_POP3_GREETINGS:
diff --git a/libproto/pop/pop3_stls.c b/libproto/pop/pop3_stls.c
index f9cc278..0cf617d 100644
--- a/libproto/pop/pop3_stls.c
+++ b/libproto/pop/pop3_stls.c
@@ -114,8 +114,6 @@ mu_pop3_stls (mu_pop3_t pop3)
       mu_stream_unref (streams[0]);
       mu_stream_unref (streams[1]);
       MU_POP3_CHECK_EAGAIN (pop3, status);
-      status = mu_stream_open (tlsstream);
-      MU_POP3_CHECK_EAGAIN (pop3, status);
       streams[0] = streams[1] = tlsstream;
       status = pop3_set_streams (pop3, streams);
       mu_stream_unref (streams[0]);
diff --git a/libproto/pop/pop3_stream.c b/libproto/pop/pop3_stream.c
index 446e5eb..6df096c 100644
--- a/libproto/pop/pop3_stream.c
+++ b/libproto/pop/pop3_stream.c
@@ -173,7 +173,8 @@ mu_pop3_stream_create (mu_pop3_t pop3, mu_stream_t *pstream)
   int status;
   
   sp = (struct mu_pop3_stream *) _mu_stream_create (sizeof (*sp),
-						    MU_STREAM_READ);
+						    MU_STREAM_READ |
+						    _MU_STR_OPEN);
   if (!sp)
     return ENOMEM;
   sp->stream.read = _mu_pop3_read; 
diff --git a/maidag/mailtmp.c b/maidag/mailtmp.c
index f81bc30..d981673 100644
--- a/maidag/mailtmp.c
+++ b/maidag/mailtmp.c
@@ -49,13 +49,6 @@ mail_tmp_begin (struct mail_tmp **pmtmp, const char *from)
       return status;
     }
 
-  if ((status = mu_stream_open (mtmp->stream)))
-    {
-      free (mtmp);
-      maidag_error (_("unable to open temporary file: %s"),
-		    mu_strerror (status));
-      return status;
-    }
   mtmp->from = from;
   *pmtmp = mtmp;
   return 0;
diff --git a/mail/decode.c b/mail/decode.c
index cd1d159..f508faf 100644
--- a/mail/decode.c
+++ b/mail/decode.c
@@ -418,12 +418,6 @@ run_metamail (const char *mailcap_cmd, mu_message_t mesg)
 	      break;
 	    }
 
-	  status = mu_stream_open (pstr);
-	  if (status)
-	    {
-	      mu_error ("mu_stream_open: %s", mu_strerror (status));
-	      break;
-	    }
 	  mu_stream_copy (pstr, stream, 0, NULL);
 	  mu_stream_close (pstr);
 	  mu_stream_destroy (&pstr);
diff --git a/mail/pipe.c b/mail/pipe.c
index f139edf..d057450 100644
--- a/mail/pipe.c
+++ b/mail/pipe.c
@@ -41,8 +41,6 @@ mail_pipe (int argc, char **argv)
     return 1;
 
   rc = mu_prog_stream_create (&outstr, cmd, MU_STREAM_WRITE);
-  if (rc == 0)
-    rc = mu_stream_open (outstr);
   if (rc)
     {
       mu_error (_("cannot open `%s': %s"), cmd, mu_strerror (rc));
diff --git a/mail/send.c b/mail/send.c
index fd14900..3f095b2 100644
--- a/mail/send.c
+++ b/mail/send.c
@@ -568,8 +568,6 @@ mail_send0 (compose_env_t * env, int save_to)
       mu_stream_t instr;
 
       rc = mu_file_stream_create (&instr, filename, MU_STREAM_READ);
-      if (rc == 0)
-	rc = mu_stream_open (instr);
       if (rc)
 	mu_error (_("cannot open temporary stream `%s' for reading: %s"),
 		  filename, mu_strerror (rc));
diff --git a/mail/write.c b/mail/write.c
index d7e017d..13ad960 100644
--- a/mail/write.c
+++ b/mail/write.c
@@ -72,8 +72,6 @@ mail_write (int argc, char **argv)
 
   rc = mu_file_stream_create (&output, filename,
 			      MU_STREAM_APPEND|MU_STREAM_CREAT);
-  if (rc == 0)
-    rc = mu_stream_open (output);
   if (rc)
     {
       util_error (_("can't open %s: %s"), filename, mu_strerror (rc));
diff --git a/mh/burst.c b/mh/burst.c
index 1bd99c3..b413c07 100644
--- a/mh/burst.c
+++ b/mh/burst.c
@@ -239,9 +239,7 @@ flush_stream (mu_stream_t *pstr, char *buf, size_t size)
 
   if (size == 0)
     return;
-  if (!*pstr
-      && ((rc = mu_temp_file_stream_create (pstr, NULL)) != 0
-	  || (rc = mu_stream_open (*pstr))))
+  if ((rc = mu_temp_file_stream_create (pstr, NULL))) 
     {
       mu_error (_("Cannot open temporary file: %s"),
 		mu_strerror (rc));
@@ -485,8 +483,7 @@ msg_copy (size_t num, const char *file)
   
   if ((rc = mu_file_stream_create (&ostream,
 				   file,
-				   MU_STREAM_WRITE|MU_STREAM_CREAT)) != 0
-      || (rc = mu_stream_open (ostream)))
+				   MU_STREAM_WRITE|MU_STREAM_CREAT))) 
     {
       mu_error (_("Cannot open output file `%s': %s"),
 		file, mu_strerror (rc));
diff --git a/mh/comp.c b/mh/comp.c
index 935f114..f65fab2 100644
--- a/mh/comp.c
+++ b/mh/comp.c
@@ -158,8 +158,7 @@ copy_message (mu_mailbox_t mbox, size_t n, const char *file)
   mu_message_get_streamref (msg, &in);
   
   if ((rc = mu_file_stream_create (&out,
-				   file, MU_STREAM_RDWR|MU_STREAM_CREAT)) != 0
-      || (rc = mu_stream_open (out)))
+				   file, MU_STREAM_RDWR|MU_STREAM_CREAT)))
     {
       mu_error (_("cannot open output file \"%s\": %s"),
 		file, mu_strerror (rc));
diff --git a/mh/compcommon.c b/mh/compcommon.c
index 666ef17..e85da11 100644
--- a/mh/compcommon.c
+++ b/mh/compcommon.c
@@ -54,8 +54,7 @@ mh_comp_draft (const char *formfile, const char *defformfile,
 	  
 	  if ((rc = mu_file_stream_create (&stream,
 					draftfile,
-					MU_STREAM_WRITE|MU_STREAM_CREAT)) != 0
-	      || (rc = mu_stream_open (stream)))
+					MU_STREAM_WRITE|MU_STREAM_CREAT)))
 	    {
 	      mu_error (_("cannot open output file \"%s\": %s"),
 			draftfile, mu_strerror (rc));
diff --git a/mh/forw.c b/mh/forw.c
index 8d331ec..6e63d23 100644
--- a/mh/forw.c
+++ b/mh/forw.c
@@ -332,8 +332,7 @@ finish_draft ()
   
   if ((rc = mu_file_stream_create (&stream,
 				   wh_env.file,
-				   MU_STREAM_WRITE|MU_STREAM_CREAT)) != 0
-      || (rc = mu_stream_open (stream)))
+				   MU_STREAM_WRITE|MU_STREAM_CREAT)))
     {
       mu_error (_("cannot open output file `%s': %s"),
 		wh_env.file, mu_strerror (rc));
diff --git a/mh/mh_init.c b/mh/mh_init.c
index 776e450..7043d1a 100644
--- a/mh/mh_init.c
+++ b/mh/mh_init.c
@@ -597,8 +597,7 @@ mh_file_copy (const char *from, const char *to)
   if (!bufsize)
     mh_err_memory (1);
 
-  if ((rc = mu_file_stream_create (&in, from, MU_STREAM_READ)) != 0
-      || (rc = mu_stream_open (in)))
+  if ((rc = mu_file_stream_create (&in, from, MU_STREAM_READ)))
     {
       mu_error (_("cannot open input file `%s': %s"),
 		from, mu_strerror (rc));
@@ -606,8 +605,7 @@ mh_file_copy (const char *from, const char *to)
       return 1;
     }
 
-  if ((rc = mu_file_stream_create (&out, to, MU_STREAM_RDWR|MU_STREAM_CREAT)) != 0
-      || (rc = mu_stream_open (out)))
+  if ((rc = mu_file_stream_create (&out, to, MU_STREAM_RDWR|MU_STREAM_CREAT)))
     {
       mu_error (_("cannot open output file `%s': %s"),
 		to, mu_strerror (rc));
@@ -660,14 +658,6 @@ _file_to_message (const char *file_name)
       return NULL;
     }
   
-  if ((rc = mu_stream_open (instream)))
-    {
-      mu_error (_("cannot open input stream (file %s): %s"),
-		file_name, mu_strerror (rc));
-      mu_stream_destroy (&instream);
-      return NULL;
-    }
-
   return mh_stream_to_message (instream);
 }
 
diff --git a/mh/mh_whatnow.c b/mh/mh_whatnow.c
index 8652fc0..3ec4533 100644
--- a/mh/mh_whatnow.c
+++ b/mh/mh_whatnow.c
@@ -198,12 +198,6 @@ display_file (const char *name)
 	  mu_error ("mu_file_stream_create: %s", mu_strerror (rc));
 	  return;
 	}
-      rc = mu_stream_open (stream);
-      if (rc)
-	{
-	  mu_error ("mu_stream_open: %s", mu_strerror (rc));
-	  return;
-	} 
 
       mu_stream_seek (stream, 0, MU_SEEK_SET, NULL);
       while (mu_stream_read (stream, buffer, sizeof buffer - 1, &n) == 0
diff --git a/mh/mhl.c b/mh/mhl.c
index 6a7024d..030fe42 100644
--- a/mh/mhl.c
+++ b/mh/mhl.c
@@ -161,11 +161,6 @@ open_output ()
       exit (1);
     }
 
-  if ((rc = mu_stream_open (output)))
-    {
-      mu_error (_("cannot open output stream: %s"), mu_strerror (rc));
-      exit (1);
-    }
   return output;
 }
 
@@ -186,13 +181,6 @@ list_message (char *name, mu_stream_t output)
       return;
     }
 
-  if ((rc = mu_stream_open (input)))
-    {
-      mu_error (_("cannot open input stream: %s"), mu_strerror (rc));
-      mu_stream_destroy (&input);
-      return;
-    }
-
   msg = mh_stream_to_message (input);
   if (!msg)
     {
diff --git a/mh/mhn.c b/mh/mhn.c
index 356f886..d25932f 100644
--- a/mh/mhn.c
+++ b/mh/mhn.c
@@ -1273,13 +1273,6 @@ mhn_exec (mu_stream_t *str, const char *cmd, int flags)
       mu_error (_("cannot create proc stream (command %s): %s"),
 		cmd, mu_strerror (rc));
     }
-  else
-    {
-      rc = mu_stream_open (*str);
-      if (rc)
-	mu_error (_("cannot open proc stream (command %s): %s"),
-		  cmd, mu_strerror (rc));
-    }
   return rc;
 }
 
@@ -1330,15 +1323,6 @@ mhn_run_command (mu_message_t msg, msg_part_t part,
 	  mu_argcv_free (argc, argv);
 	  return rc;
 	}
-      rc = mu_stream_open (tmp);
-      if (rc)
-	{
-	  mu_error (_("cannot open temporary stream (file %s): %s"),
-		    tempfile, mu_strerror (rc));
-	  mu_stream_destroy (&tmp);
-	  mu_argcv_free (argc, argv);
-	  return rc;
-	}
       show_internal (msg, part, encoding, tmp);      
       mu_stream_destroy (&tmp);
       rc = mu_spawnvp (argv[0], argv, &status);
@@ -1462,12 +1446,6 @@ mhn_show ()
       mu_error (_("cannot create output stream: %s"), mu_strerror (rc));
       exit (1);
     }
-  rc = mu_stream_open (ostr);
-  if (rc)
-    {
-      mu_error (_("cannot open output stream: %s"), mu_strerror (rc));
-      exit (1);
-    }
   
   if (formfile)
     {
@@ -1642,15 +1620,6 @@ store_handler (mu_message_t msg, msg_part_t part, char *type, char *encoding,
       free (name);
       return rc;
     }
-  rc = mu_stream_open (out);
-  if (rc)
-    {
-      mu_error (_("cannot open output stream (file %s): %s"),
-		name, mu_strerror (rc));
-      free (name);
-      mu_stream_destroy (&out);
-      return rc;
-    }
   show_internal (msg, part, encoding, out);      
 
   mu_stream_destroy (&out);
@@ -2246,14 +2215,6 @@ edit_mime (char *cmd, struct compose_env *env, mu_message_t *msg, int level)
 		    cmd, mu_strerror (rc));
 	  return rc;
 	}
-      rc = mu_stream_open (in);
-      if (rc)
-	{
-	  mu_error (_("cannot open input stream (file %s): %s"),
-		    cmd, mu_strerror (rc));
-	  mu_stream_destroy (&in);
-	  return rc;
-	}
     }
   
   /* Create filter */
@@ -2285,14 +2246,6 @@ edit_mime (char *cmd, struct compose_env *env, mu_message_t *msg, int level)
     }
   free (encoding);
 
-  rc = mu_stream_open (fstr);
-  if (rc)
-    {
-      mu_error (_("cannot open filter stream: %s"),
-		mu_strerror (rc));
-      return rc;
-    }
-  
   mu_message_get_body (*msg, &body);
   mu_body_get_streamref (body, &out);
   mu_stream_copy (out, fstr, 0, NULL);
@@ -2647,15 +2600,6 @@ mhn_compose ()
       free (name);
       return rc;
     }
-  rc = mu_stream_open (stream);
-  if (rc)
-    {
-      mu_error (_("cannot open output stream (file %s): %s"),
-		name, mu_strerror (rc));
-      free (name);
-      mu_stream_destroy (&stream);
-      return rc;
-    }
 
   mhn_header (message, msg);
   copy_header_to_stream (message, stream);
diff --git a/mh/repl.c b/mh/repl.c
index 9f0f5e5..afb9233 100644
--- a/mh/repl.c
+++ b/mh/repl.c
@@ -336,13 +336,6 @@ make_draft (mu_mailbox_t mbox, int disp, struct mh_whatnow_env *wh)
 	  exit (1);
 	}
 
-      if ((rc = mu_stream_open (str)))
-	{
-	  mu_error (_("cannot open draft file %s: %s"),
-		    wh->file, mu_strerror (rc));
-	  exit (1);
-	}	  
-
       if (has_fcc)
 	{
 	  mu_message_t tmp_msg;
diff --git a/mimeview/mimeview.c b/mimeview/mimeview.c
index ed90703..8b32311 100644
--- a/mimeview/mimeview.c
+++ b/mimeview/mimeview.c
@@ -260,7 +260,6 @@ display_file (const char *type)
 	  mu_stdio_stream_create (&stream, fileno (mimeview_fp),
 				  MU_STREAM_READ|
 				  MU_STREAM_SEEK);
-	  mu_stream_open (stream);
 	  
 	  display_stream_mailcap (mimeview_file, stream, hdr,
 				  no_ask_types, interactive, dry_run,
diff --git a/mu/.gitignore b/mu/.gitignore
new file mode 100644
index 0000000..92cfbd6
--- /dev/null
+++ b/mu/.gitignore
@@ -0,0 +1 @@
+mu
diff --git a/mu/Makefile.am b/mu/Makefile.am
new file mode 100644
index 0000000..0a3c50e
--- /dev/null
+++ b/mu/Makefile.am
@@ -0,0 +1,43 @@
+## This file is part of GNU Mailutils.
+## Copyright (C) 1999, 2000, 2001, 2002, 2005, 2007, 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 2, 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, see <http://www.gnu.org/licenses/>.
+
+bin_PROGRAMS = mu
+mu_SOURCES = \
+ info.c\
+ mu.h\
+ mu.c\
+ filter.c\
+ flt2047.c\
+ pop.c\
+ query.c\
+ shell.c
+
+mu_LDADD = \
+ ${MU_APP_LIBRARIES}\
+ ${MU_LIB_MBOX}\
+ ${MU_LIB_IMAP}\
+ ${MU_LIB_POP}\
+ ${MU_LIB_NNTP}\
+ ${MU_LIB_MH}\
+ ${MU_LIB_MAILDIR}\
+ ${MU_LIB_MAILER}\
+ ${MU_LIB_AUTH}\
+ @MU_AUTHLIBS@\
+ ${MU_LIB_MAILUTILS}\
+ @READLINE_LIBS@ @MU_COMMON_LIBRARIES@
+
+INCLUDES = @MU_APP_COMMON_INCLUDES@ @MU_AUTHINCS@
diff --git a/mu/filter.c b/mu/filter.c
new file mode 100644
index 0000000..373eb0d
--- /dev/null
+++ b/mu/filter.c
@@ -0,0 +1,159 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 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.
+
+   GNU Mailutils 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 GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
+
+#if defined(HAVE_CONFIG_H)
+# include <config.h>
+#endif
+#include <stdlib.h>
+#include <string.h>
+#include <mailutils/mailutils.h>
+#include "argp.h"
+#include "mu.h"
+
+static char filter_doc[] = N_("mu filter");
+static char filter_args_doc[] = N_("[OPTIONS] NAME");
+
+static struct argp_option filter_options[] = {
+  { "encode", 'e', NULL, 0, N_("encode the input (default)") },
+  { "decode", 'd', NULL, 0, N_("decode the input") },
+  { "line-length", 'l', NULL, 0, N_("limit output line length") },
+  { "newline", 'n', NULL, 0, N_("print additional newline") },
+  { NULL }
+};
+
+static int filter_mode = MU_FILTER_ENCODE;
+static int newline_option = 0;
+static size_t line_length;
+static int line_length_option = 0;
+
+static error_t
+filter_parse_opt (int key, char *arg, struct argp_state *state)
+{
+  char *p;
+  
+  switch (key)
+    {
+    case 'e':
+      filter_mode = MU_FILTER_ENCODE;
+      break;
+
+    case 'd':
+      filter_mode = MU_FILTER_DECODE;
+      break;
+
+    case 'n':
+      newline_option = 1;
+      break;
+
+    case 'l':
+      line_length = strtoul (arg, &p, 10);
+      if (*p)
+	argp_error (state, N_("not a number"));
+      line_length_option = 1;
+      break;
+      
+    default:
+      return ARGP_ERR_UNKNOWN;
+    }
+  return 0;
+}
+
+static struct argp filter_argp = {
+  filter_options,
+  filter_parse_opt,
+  filter_args_doc,
+  filter_doc,
+  NULL,
+  NULL,
+  NULL
+};
+
+/* FIXME: This is definitely a kludge. The API should provide a function
+   for that. */
+static void
+reset_line_length (const char *name, size_t length)
+{
+  mu_list_t list;
+  int status;
+  mu_filter_record_t frec;
+  
+  mu_filter_get_list (&list);
+  status = mu_list_locate (list, (void*)name, (void**)&frec);
+  if (status == 0)
+    frec->max_line_length = length;
+  /* don't bail out, leave that to mu_filter_create */
+}
+
+int
+mutool_filter (int argc, char **argv)
+{
+  int rc, index;
+  mu_stream_t in, out, flt;
+  const char *fltname;
+  
+  if (argp_parse (&filter_argp, argc, argv, ARGP_IN_ORDER, &index, NULL))
+    return 1;
+
+  argc -= index;
+  argv += index;
+
+  if (argc != 1)
+    {
+      mu_error (_("what filter do you want?"));
+      return 1;
+    }
+
+  fltname = argv[0];
+  
+  rc = mu_stdio_stream_create (&in, MU_STDIN_FD, 0);
+  if (rc)
+    {
+      mu_error (_("cannot open input stream: %s"), mu_strerror (rc));
+      return 1;
+    }
+  
+  rc = mu_stdio_stream_create (&out, MU_STDOUT_FD, 0);
+  if (rc)
+    {
+      mu_error (_("cannot open output stream: %s"), mu_strerror (rc));
+      return 1;
+    }
+
+  if (line_length_option)
+    reset_line_length (fltname, line_length);
+
+  rc = mu_filter_create (&flt, in, fltname, filter_mode, MU_STREAM_READ);
+  if (rc)
+    {
+      mu_error (_("cannot open filter stream: %s"), mu_strerror (rc));
+      return 1;
+    }
+
+  rc = mu_stream_copy (out, flt, 0, NULL);
+
+  if (rc)
+    {
+      mu_error ("%s", mu_strerror (rc));
+      return 1;
+    }
+
+  if (newline_option)
+    mu_stream_write (out, "\n", 1, NULL);
+
+  mu_stream_flush (out);
+  
+  return 0;
+}
diff --git a/mu/flt2047.c b/mu/flt2047.c
new file mode 100644
index 0000000..f2ec974
--- /dev/null
+++ b/mu/flt2047.c
@@ -0,0 +1,153 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 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.
+
+   GNU Mailutils 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 GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
+
+#if defined(HAVE_CONFIG_H)
+# include <config.h>
+#endif
+#include <stdlib.h>
+#include <string.h>
+#include <mailutils/mailutils.h>
+#include "argp.h"
+#include "mu.h"
+
+static char flt2047_doc[] = N_("mu 2047 - decode/encode message headers");
+static char flt2047_args_doc[] = N_("[OPTIONS] [text]");
+
+static struct argp_option flt2047_options[] = {
+  { "encode", 'e', NULL, 0, N_("encode the input (default)") },
+  { "decode", 'd', NULL, 0, N_("decode the input") },
+  { "newline", 'n', NULL, 0, N_("print additional newline") },
+  { "charset", 'c', NULL, 0, N_("set charset (default: iso-8859-1)") },
+  { "encoding", 'E', NULL, 0, N_("set encoding (default: quoted-printable)") },
+  { NULL }
+};
+  
+static int decode_mode = 0;
+static int newline_option = 0;
+static const char *charset = "iso-8859-1";
+static const char *encoding = "quoted-printable";
+
+static error_t
+flt2047_parse_opt (int key, char *arg, struct argp_state *state)
+{
+  switch (key)
+    {
+    case 'c':
+      charset = arg;
+      break;
+      
+    case 'e':
+      decode_mode = 0;
+      break;
+
+    case 'E':
+      encoding = arg;
+      break;
+
+    case 'd':
+      decode_mode = 1;
+      break;
+
+    case 'n':
+      newline_option = 0;
+      break;
+
+    default:
+      return ARGP_ERR_UNKNOWN;
+    }
+  return 0;
+}
+
+static struct argp flt2047_argp = {
+  flt2047_options,
+  flt2047_parse_opt,
+  flt2047_args_doc,
+  flt2047_doc,
+  NULL,
+  NULL,
+  NULL
+};
+
+int
+mutool_flt2047 (int argc, char **argv)
+{
+  int rc, index;
+  mu_stream_t in, out;
+  char *p;
+  
+  if (argp_parse (&flt2047_argp, argc, argv, ARGP_IN_ORDER, &index, NULL))
+    return 1;
+
+  argc -= index;
+  argv += index;
+
+  rc = mu_stdio_stream_create (&in, MU_STDIN_FD, 0);
+  if (rc)
+    {
+      mu_error (_("cannot open input stream: %s"), mu_strerror (rc));
+      return 1;
+    }
+  
+  rc = mu_stdio_stream_create (&out, MU_STDOUT_FD, 0);
+  if (rc)
+    {
+      mu_error (_("cannot open output stream: %s"), mu_strerror (rc));
+      return 1;
+    }
+      
+  if (argc)
+    {
+      char *p;
+
+      while (argc--)
+	{
+	  const char *text = *argv++;
+	  if (decode_mode)
+	    rc = mu_rfc2047_decode (charset, text, &p);
+	  else
+	    rc = mu_rfc2047_encode (charset, encoding, text, &p);
+	  if (rc)
+	    {
+	      mu_error ("%s", mu_strerror (rc));
+	      return 1;
+	    }
+	  mu_stream_printf (out, "%s\n", p);
+	}
+    }
+  else
+    {
+      size_t size = 0, n;
+      char *buf = NULL;
+
+      while ((rc = mu_stream_getline (in, &buf, &size, &n)) == 0 && n > 0)
+	{
+	  mu_rtrim_class (buf, MU_CTYPE_SPACE);
+	  if (decode_mode)
+	    rc = mu_rfc2047_decode (charset, buf, &p);
+	  else
+	    rc = mu_rfc2047_encode (charset, encoding, buf, &p);
+	  if (rc)
+	    {
+	      mu_error ("%s", mu_strerror (rc));
+	      return 1;
+	    }
+	  mu_stream_printf (out, "%s\n", p);
+	}
+    }
+  mu_stream_flush (out);
+  
+  return 0;
+}
diff --git a/mu/info.c b/mu/info.c
new file mode 100644
index 0000000..56d5b12
--- /dev/null
+++ b/mu/info.c
@@ -0,0 +1,93 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 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.
+
+   GNU Mailutils 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 GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
+
+#if defined(HAVE_CONFIG_H)
+# include <config.h>
+#endif
+#include <stdlib.h>
+#include <string.h>
+#include <mailutils/mailutils.h>
+#include "argp.h"
+#include "mu.h"
+
+static char info_doc[] = N_("mu info - print a list of configuration\
+options used to build mailutils; optional arguments are interpreted\
+ as a list of configuration options to check for.");
+static char info_args_doc[] = N_("[OPTIONS] [capa...]");
+
+static struct argp_option info_options[] = {
+  { "verbose", 'v', NULL, 0,
+    N_("increase output verbosity"), 0},
+  { NULL }
+};
+
+static int verbose;
+
+static error_t
+info_parse_opt (int key, char *arg, struct argp_state *state)
+{
+  switch (key)
+    {
+    case 'v':
+      verbose++;
+      break;
+      
+    default:
+      return ARGP_ERR_UNKNOWN;
+    }
+  return 0;
+}
+
+static struct argp info_argp = {
+  info_options,
+  info_parse_opt,
+  info_args_doc,
+  info_doc,
+  NULL,
+  NULL,
+  NULL
+};
+
+int
+mutool_info (int argc, char **argv)
+{
+  int index;
+  
+  if (argp_parse (&info_argp, argc, argv, ARGP_IN_ORDER, &index, NULL))
+    return 1;
+
+  argc -= index;
+  argv += index;
+
+  if (argc == 0)
+    mu_fprint_options (stdout, verbose);
+  else
+    {
+      int i, found = 0;
+      
+      for (i = 0; i < argc; i++)
+	{
+	  const struct mu_conf_option *opt = mu_check_option (argv[i]);
+	  if (opt)
+	    {
+	      found++;
+	      mu_fprint_conf_option (stdout, opt, verbose);
+	    }
+	}
+      return found == argc ? 0 : 1;
+    }
+  return 0;
+}
diff --git a/mu/mu.c b/mu/mu.c
new file mode 100644
index 0000000..cff6a12
--- /dev/null
+++ b/mu/mu.c
@@ -0,0 +1,150 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 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.
+
+   GNU Mailutils 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 GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
+
+#if defined(HAVE_CONFIG_H)
+# include <config.h>
+#endif
+#include <stdlib.h>
+#include <string.h>
+#include <mailutils/mailutils.h>
+#include <mailutils/tls.h>
+#include "mailutils/libargp.h"
+#include "mu.h"
+
+static char args_doc[] = N_("[OPTIONS] COMMAND [CMDOPTS]");
+static char doc[] = N_("mu -- GNU Mailutils test tool.\n\
+Commands are:\n\
+    mu info   - show Mailutils configuration\n\
+    mu query  - query configuration values\n\
+    mu pop    - POP3 client program\n\
+    mu filter - filter program\n\
+    mu 2047   - decode/encode message headers as per RFC 2047\n\
+\n\
+Try `mu COMMAND --help' to get help on a particular COMMAND\n\
+\n\
+Options are:\n");
+/* FIXME: add
+    mu imap
+    mu send [opts]
+ */
+
+static struct argp_option options[] = {
+  { NULL }
+};
+
+static error_t
+parse_opt (int key, char *arg, struct argp_state *state)
+{
+  switch (key)
+    {
+    default:
+      return ARGP_ERR_UNKNOWN;
+    }
+  return 0;
+}
+
+static struct argp argp = {
+  options,
+  parse_opt,
+  args_doc,
+  doc,
+  NULL,
+  NULL,
+  NULL
+};
+
+static const char *mu_tool_capa[] = {
+  "common",
+  "debug",
+  "license",
+  "locking",
+  "mailbox",
+  "auth",
+  NULL 
+};
+
+struct mu_cfg_param mu_tool_param[] = {
+  { NULL }
+};
+
+struct mutool_action_tab
+{
+  const char *name;
+  mutool_action_t action;
+};
+
+struct mutool_action_tab mutool_action_tab[] = {
+  { "info", mutool_info },
+  { "pop", mutool_pop },
+  { "filter", mutool_filter },
+  { "2047", mutool_flt2047 },
+  { "query", mutool_query },
+  { NULL }
+};
+
+static mutool_action_t
+find_action (const char *name)
+{
+  struct mutool_action_tab *p;
+
+  for (p = mutool_action_tab; p->name; p++)
+    if (strcmp (p->name, name) == 0)
+      return p->action;
+  return NULL;
+}
+
+int
+main (int argc, char **argv)
+{
+  int index;
+  mutool_action_t action;
+  
+  /* Native Language Support */
+  MU_APP_INIT_NLS ();
+  MU_AUTH_REGISTER_ALL_MODULES ();
+  
+  /* Register the desired mailbox formats.  */
+  mu_register_all_mbox_formats ();
+
+#ifdef WITH_TLS
+  mu_gocs_register ("tls", mu_tls_module_init);
+#endif
+  mu_argp_init (NULL, NULL);
+  if (mu_app_init (&argp, mu_tool_capa, mu_tool_param, 
+		   argc, argv, ARGP_IN_ORDER, &index, NULL))
+    exit (1);
+
+  argc -= index;
+  argv += index;
+
+  if (argc < 1)
+    {
+      mu_error (_("what do you want me to do?"));
+      exit (1);
+    }
+	
+  action = find_action (argv[0]);
+  if (!action)
+    {
+      mu_error (_("don't know what %s is"), argv[0]);
+      exit (1);
+    }
+
+  exit (action (argc, argv));
+}
+
+  
+  
diff --git a/mu/mu.h b/mu/mu.h
new file mode 100644
index 0000000..5fe3097
--- /dev/null
+++ b/mu/mu.h
@@ -0,0 +1,40 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 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.
+
+   GNU Mailutils 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 GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
+
+typedef int (*mutool_action_t) (int argc, char **argv);
+
+struct mutool_command
+{
+  const char *name;	/* User printable name of the function. */
+  int argmin;           /* Min. acceptable number of arguments (-1 means
+			   pass all arguments as a single string */ 
+  int argmax;           /* Max. allowed number of arguments (-1 means not
+			   limited */
+  mutool_action_t func;	/* Function to call to do the job. */
+  const char *doc;	/* Documentation for this function.  */
+};
+
+
+int mutool_pop (int argc, char **argv);
+int mutool_filter (int argc, char **argv);
+int mutool_flt2047 (int argc, char **argv);
+int mutool_info (int argc, char **argv);
+int mutool_query (int argc, char **argv);
+
+extern char *mutool_shell_prompt;
+extern mu_vartab_t mutool_prompt_vartab;
+
+int mutool_shell (const char *name, struct mutool_command *cmd);
diff --git a/mu/pop.c b/mu/pop.c
new file mode 100644
index 0000000..8375776
--- /dev/null
+++ b/mu/pop.c
@@ -0,0 +1,778 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 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.
+
+   GNU Mailutils 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 GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
+
+#if defined(HAVE_CONFIG_H)
+# include <config.h>
+#endif
+#include <stdlib.h>
+#include <string.h>
+#include <termios.h>
+#include <unistd.h>
+#include <netdb.h>
+#include <mailutils/mailutils.h>
+#include "mu.h"
+#include "argp.h"
+#include "xalloc.h"
+
+static char pop_doc[] = N_("mu pop - POP3 client shell");
+static char pop_args_doc[] = "";
+
+static struct argp_option pop_options[] = {
+  { NULL }
+};
+
+static error_t
+pop_parse_opt (int key, char *arg, struct argp_state *state)
+{
+  switch (key)
+    {
+    default:
+      return ARGP_ERR_UNKNOWN;
+    }
+  return 0;
+}
+
+static struct argp pop_argp = {
+  pop_options,
+  pop_parse_opt,
+  pop_args_doc,
+  pop_doc,
+  NULL,
+  NULL,
+  NULL
+};
+
+int
+get_bool (const char *str, int *pb)
+{
+  if (mu_c_strcasecmp (str, "yes") == 0
+      || mu_c_strcasecmp (str, "on") == 0
+      || mu_c_strcasecmp (str, "true") == 0)
+    *pb = 1;
+  else if (mu_c_strcasecmp (str, "no") == 0
+      || mu_c_strcasecmp (str, "off") == 0
+      || mu_c_strcasecmp (str, "false") == 0)
+    *pb = 0;
+  else
+    return 1;
+
+  return 0;
+}
+
+int
+get_port (const char *port_str, int *pn)
+{
+  short port_num;
+  long num;
+  char *p;
+  
+  num = port_num = strtol (port_str, &p, 0);
+  if (*p == 0)
+    {
+      if (num != port_num)
+	{
+	  mu_error ("bad port number: %s", port_str);
+	  return 1;
+	}
+    }
+  else
+    {
+      struct servent *sp = getservbyname (port_str, "tcp");
+      if (!sp)
+	{
+	  mu_error ("unknown port name");
+	  return 1;
+	}
+      port_num = ntohs (sp->s_port);
+    }
+  *pn = port_num;
+  return 0;
+}
+
+
+/* Global handle for pop3.  */
+mu_pop3_t pop3;
+
+/* Flag if verbosity is needed.  */
+int verbose;
+#define VERBOSE_MASK(n) (1<<((n)+1))
+#define SET_VERBOSE_MASK(n) (verbose |= VERBOSE_MASK (n))
+#define CLR_VERBOSE_MASK(n) (verbose &= VERBOSE_MASK (n))
+#define QRY_VERBOSE_MASK(n) (verbose & VERBOSE_MASK (n))
+#define HAS_VERBOSE_MASK(n) (verbose & ~1)
+#define SET_VERBOSE() (verbose |= 1)
+#define CLR_VERBOSE() (verbose &= ~1)
+#define QRY_VERBOSE() (verbose & 1)
+
+enum pop_session_status
+  {
+    pop_session_disconnected,
+    pop_session_connected,
+    pop_session_logged_in
+  };
+
+enum pop_session_status pop_session_status;
+
+int connect_argc;
+char **connect_argv;
+
+/* Host we are connected to. */
+#define host connect_argv[0]
+int port = 110;
+char *username;
+
+const char *
+pop_session_str (enum pop_session_status stat)
+{
+  switch (stat)
+    {
+    case pop_session_disconnected:
+      return "disconnected";
+      
+    case pop_session_connected:
+      return "connected";
+      
+    case pop_session_logged_in:
+      return "logged in";
+    }
+  return "unknown";
+}
+
+static void
+pop_prompt_vartab ()
+{
+  mu_vartab_t vtab;
+  
+  if (mu_vartab_create (&vtab))
+    return;
+  mu_vartab_define (vtab, "user",
+		    (pop_session_status == pop_session_logged_in) ?
+		      username : "not logged in", 1);
+  mu_vartab_define (vtab, "host",
+		    (pop_session_status != pop_session_disconnected) ?
+		      host : "not connected", 1);
+  mu_vartab_define (vtab, "program-name", mu_program_name, 1);
+  mu_vartab_define (vtab, "canonical-program-name", "mu", 1);
+  mu_vartab_define (vtab, "package", PACKAGE, 1);
+  mu_vartab_define (vtab, "version", PACKAGE_VERSION, 1);
+  mu_vartab_define (vtab, "status", pop_session_str (pop_session_status), 1);
+  
+  mu_vartab_destroy (&mutool_prompt_vartab);
+  mutool_prompt_vartab = vtab;
+}
+
+
+static int
+string_to_xlev (const char *name, int *pv)
+{
+  if (strcmp (name, "secure") == 0)
+    *pv = MU_XSCRIPT_SECURE;
+  else if (strcmp (name, "payload") == 0)
+    *pv = MU_XSCRIPT_PAYLOAD;
+  else
+    return 1;
+  return 0;
+}
+
+static int
+change_verbose_mask (int set, int argc, char **argv)
+{
+  int i;
+  
+  for (i = 0; i < argc; i++)
+    {
+      int lev;
+      
+      if (string_to_xlev (argv[i], &lev))
+	{
+	  mu_error ("unknown level: %s", argv[i]);
+	  return 1;
+	}
+      if (set)
+	SET_VERBOSE_MASK (lev);
+      else
+	CLR_VERBOSE_MASK (lev);
+    }
+  return 0;
+}
+
+void
+set_verbose (mu_pop3_t p)
+{
+  if (p)
+    {
+      if (QRY_VERBOSE ())
+	{
+	  mu_pop3_trace (p, MU_POP3_TRACE_SET);
+	}
+      else
+	mu_pop3_trace (p, MU_POP3_TRACE_CLR);
+    }
+}
+
+void
+set_verbose_mask (mu_pop3_t p)
+{
+  if (p)
+    {
+      mu_pop3_trace_mask (p, QRY_VERBOSE_MASK (MU_XSCRIPT_SECURE)
+			          ? MU_POP3_TRACE_SET : MU_POP3_TRACE_CLR,
+			      MU_XSCRIPT_SECURE);
+      mu_pop3_trace_mask (p, QRY_VERBOSE_MASK (MU_XSCRIPT_PAYLOAD)
+			          ? MU_POP3_TRACE_SET : MU_POP3_TRACE_CLR,
+			      MU_XSCRIPT_PAYLOAD);
+    }
+}
+
+static int
+com_verbose (int argc, char **argv)
+{
+  if (argc == 1)
+    {
+      if (QRY_VERBOSE ())
+	{
+	  printf ("verbose is on");
+	  if (HAS_VERBOSE_MASK ())
+	    {
+	      char *delim = " (";
+	    
+	      if (QRY_VERBOSE_MASK (MU_XSCRIPT_SECURE))
+		{
+		  printf("%ssecure", delim);
+		  delim = ", ";
+		}
+	      if (QRY_VERBOSE_MASK (MU_XSCRIPT_PAYLOAD))
+		printf("%spayload", delim);
+	      printf (")");
+	    }
+	  printf ("\n");
+	}
+      else
+	printf ("verbose is off\n");
+    }
+  else
+    {
+      int bv;
+
+      if (get_bool (argv[1], &bv) == 0)
+	{
+	  verbose |= bv;
+	  if (argc > 2)
+	    change_verbose_mask (verbose, argc - 2, argv + 2);
+	  set_verbose (pop3);
+	}
+      else if (strcmp (argv[1], "mask") == 0)
+	change_verbose_mask (1, argc - 2, argv + 2);
+      else if (strcmp (argv[1], "unmask") == 0)
+	change_verbose_mask (0, argc - 2, argv + 2);
+      else
+	mu_error ("unknown subcommand");
+      set_verbose_mask (pop3);
+    }
+
+  return 0;
+}
+
+static int
+com_user (int argc, char **argv)
+{
+  int status;
+  
+  status = mu_pop3_user (pop3, argv[1]);
+  if (status == 0)
+    {
+      username = strdup (argv[1]);
+      pop_prompt_vartab ();
+    }
+  return status;
+}
+
+static int
+com_apop (int argc, char **argv)
+{
+  int status;
+
+  status = mu_pop3_apop (pop3, argv[1], argv[2]);
+  if (status == 0)
+    {
+      username = strdup (argv[1]);
+      pop_session_status = pop_session_logged_in;
+    }
+  return status;
+}
+
+static int
+com_capa (int argc, char **argv)
+{
+  mu_iterator_t iterator = NULL;
+  int status = 0;
+  int reread = 0;
+  int i = 1;
+  
+  for (i = 1; i < argc; i++)
+    {
+      if (strcmp (argv[i], "-reread") == 0)
+	reread = 1;
+      else
+	break;
+    }
+
+  if (i < argc)
+    {
+      if (reread)
+	{
+	  status = mu_pop3_capa (pop3, 1, NULL);
+	  if (status)
+	    return status;
+	}
+      for (; i < argc; i++)
+	{
+	  const char *elt;
+	  int rc = mu_pop3_capa_test (pop3, argv[i], &elt);
+	  switch (rc)
+	    {
+	    case 0:
+	      if (*elt)
+		printf ("%s: %s\n", argv[i], elt);
+	      else
+		printf ("%s is set\n", argv[i]);
+	      break;
+
+	    case MU_ERR_NOENT:
+	      printf ("%s is not set\n", argv[i]);
+	      break;
+
+	    default:
+	      return rc;
+	    }
+	}
+    }
+  else
+    {
+      status = mu_pop3_capa (pop3, reread, &iterator);
+
+      if (status == 0)
+	{
+	  for (mu_iterator_first (iterator);
+	       !mu_iterator_is_done (iterator); mu_iterator_next (iterator))
+	    {
+	      char *capa = NULL;
+	      mu_iterator_current (iterator, (void **) &capa);
+	      printf ("CAPA: %s\n", capa ? capa : "");
+	    }
+	  mu_iterator_destroy (&iterator);
+	}
+    }
+  return status;
+}
+
+static int
+com_uidl (int argc, char **argv)
+{
+  int status = 0;
+  if (argc == 1)
+    {
+      mu_iterator_t uidl_iterator = NULL;
+      status = mu_pop3_uidl_all (pop3, &uidl_iterator);
+      if (status == 0)
+	{
+	  for (mu_iterator_first (uidl_iterator);
+	       !mu_iterator_is_done (uidl_iterator);
+	       mu_iterator_next (uidl_iterator))
+	    {
+	      char *uidl = NULL;
+	      mu_iterator_current (uidl_iterator, (void **) &uidl);
+	      printf ("UIDL: %s\n", uidl ? uidl : "");
+	    }
+	  mu_iterator_destroy (&uidl_iterator);
+	}
+    }
+  else
+    {
+      char *uidl = NULL;
+      unsigned int msgno = strtoul (argv[1], NULL, 10);
+      status = mu_pop3_uidl (pop3, msgno, &uidl);
+      if (status == 0)
+	printf ("Msg: %d UIDL: %s\n", msgno, uidl ? uidl : "");
+      free (uidl);
+    }
+  return status;
+}
+
+static int
+com_list (int argc, char **argv)
+{
+  int status = 0;
+  if (argc == 1)
+    {
+      mu_iterator_t list_iterator;
+      status = mu_pop3_list_all (pop3, &list_iterator);
+      if (status == 0)
+	{
+	  for (mu_iterator_first (list_iterator);
+	       !mu_iterator_is_done (list_iterator);
+	       mu_iterator_next (list_iterator))
+	    {
+	      char *list = NULL;
+	      mu_iterator_current (list_iterator, (void **) &list);
+	      printf ("LIST: %s\n", (list) ? list : "");
+	    }
+	  mu_iterator_destroy (&list_iterator);
+	}
+    }
+  else
+    {
+      size_t size = 0;
+      unsigned int msgno = strtoul (argv[1], NULL, 10);
+      status = mu_pop3_list (pop3, msgno, &size);
+      if (status == 0)
+	printf ("Msg: %u Size: %lu\n", msgno, (unsigned long) size);
+    }
+  return status;
+}
+
+static int
+com_noop (int argc MU_ARG_UNUSED, char **argv MU_ARG_UNUSED)
+{
+  return mu_pop3_noop (pop3);
+}
+
+static void
+echo_off (struct termios *stored_settings)
+{
+  struct termios new_settings;
+  tcgetattr (0, stored_settings);
+  new_settings = *stored_settings;
+  new_settings.c_lflag &= (~ECHO);
+  tcsetattr (0, TCSANOW, &new_settings);
+}
+
+static void
+echo_on (struct termios *stored_settings)
+{
+  tcsetattr (0, TCSANOW, stored_settings);
+}
+
+static int
+com_pass (int argc, char **argv)
+{
+  int status;
+  char pass[256];
+  char *pwd;
+  
+  if (argc == 1)
+    {
+      struct termios stored_settings;
+
+      printf ("passwd:");
+      fflush (stdout);
+      echo_off (&stored_settings);
+      fgets (pass, sizeof pass, stdin);
+      echo_on (&stored_settings);
+      putchar ('\n');
+      fflush (stdout);
+      pass[strlen (pass) - 1] = '\0';	/* nuke the trailing line.  */
+      pwd = pass;
+    }
+  else
+    pwd = argv[1];
+  status = mu_pop3_pass (pop3, pwd);
+  if (status == 0)
+    {
+      pop_session_status = pop_session_logged_in;
+      pop_prompt_vartab ();
+    }
+  return status;
+}
+
+static int
+com_stat (int argc MU_ARG_UNUSED, char **argv MU_ARG_UNUSED)
+{
+  size_t count = 0;
+  mu_off_t size = 0;
+  int status = 0;
+
+  status = mu_pop3_stat (pop3, &count, &size);
+  printf ("Mesgs: %lu Size %lu\n",
+	  (unsigned long) count, (unsigned long) size);
+  return status;
+}
+
+static int
+com_stls (int argc MU_ARG_UNUSED, char **argv MU_ARG_UNUSED)
+{
+  return mu_pop3_stls (pop3);
+}
+
+static int
+com_dele (int argc, char **argv)
+{
+  unsigned msgno;
+  msgno = strtoul (argv[1], NULL, 10);
+  return mu_pop3_dele (pop3, msgno);
+}
+
+static int
+com_rset (int argc MU_ARG_UNUSED, char **argv MU_ARG_UNUSED)
+{
+  return mu_pop3_rset (pop3);
+}
+
+static int
+com_top (int argc, char **argv)
+{
+  mu_stream_t stream;
+  unsigned int msgno;
+  unsigned int lines;
+  int status;
+
+  msgno = strtoul (argv[1], NULL, 10);
+  if (argc == 3)
+    lines = strtoul (argv[2], NULL, 10);
+  else
+    lines = 5;
+
+  status = mu_pop3_top (pop3, msgno, lines, &stream);
+
+  if (status == 0)
+    {
+      size_t n = 0;
+      char buf[128];
+      while ((mu_stream_readline (stream, buf, sizeof buf, &n) == 0) && n)
+	printf ("%s", buf);
+      mu_stream_destroy (&stream);
+    }
+  return status;
+}
+
+static int
+com_retr (int argc, char **argv)
+{
+  mu_stream_t stream;
+  unsigned int msgno;
+  int status;
+
+  msgno = strtoul (argv[1], NULL, 10);
+  status = mu_pop3_retr (pop3, msgno, &stream);
+
+  if (status == 0)
+    {
+      size_t n = 0;
+      char buf[128];
+      while ((mu_stream_readline (stream, buf, sizeof buf, &n) == 0) && n)
+	printf ("%s", buf);
+      mu_stream_destroy (&stream);
+    }
+  return status;
+}
+
+static int
+com_disconnect (int argc MU_ARG_UNUSED, char **argv MU_ARG_UNUSED)
+{
+  if (pop3)
+    {
+      mu_pop3_disconnect (pop3);
+      mu_pop3_destroy (&pop3);
+      pop3 = NULL;
+      
+      mu_argcv_free (connect_argc, connect_argv);
+      connect_argc = 0;
+      connect_argv = NULL;
+      pop_session_status = pop_session_disconnected;
+      pop_prompt_vartab ();
+    }
+  return 0;
+}
+
+static int
+com_connect (int argc, char **argv)
+{
+  int status;
+  int n = 0;
+  int tls = 0;
+  int i = 1;
+  
+  for (i = 1; i < argc; i++)
+    {
+      if (strcmp (argv[i], "-tls") == 0)
+	{
+	  if (WITH_TLS)
+	    tls = 1;
+	  else
+	    {
+	      mu_error ("TLS not supported");
+	      return 0;
+	    }
+	}
+      else
+	break;
+    }
+
+  argc -= i;
+  argv += i;
+  
+  if (argc >= 2)
+    {
+      if (get_port (argv[1], &n))
+	return 0;
+    }
+  else if (tls)
+    n = MU_POP3_DEFAULT_SSL_PORT;
+  else
+    n = MU_POP3_DEFAULT_PORT;
+  
+  if (pop_session_status != pop_session_disconnected)
+    com_disconnect (0, NULL);
+  
+  status = mu_pop3_create (&pop3);
+  if (status == 0)
+    {
+      mu_stream_t tcp;
+
+      if (QRY_VERBOSE ())
+	{
+	  set_verbose (pop3);
+	  set_verbose_mask (pop3);
+	}
+      status = mu_tcp_stream_create (&tcp, argv[0], n, MU_STREAM_READ);
+      if (status == 0)
+	{
+#ifdef WITH_TLS
+	  if (tls)
+	    {
+	      mu_stream_t tlsstream;
+	      
+	      status = mu_tls_client_stream_create (&tlsstream, tcp, tcp, 0);
+	      mu_stream_unref (tcp);
+	      if (status)
+		{
+		  mu_error ("cannot create TLS stream: %s",
+			    mu_strerror (status));
+		  return 0;
+		}
+	      tcp = tlsstream;
+	    }
+#endif
+	  mu_pop3_set_carrier (pop3, tcp);
+	  status = mu_pop3_connect (pop3);
+	}
+      else
+	{
+	  mu_pop3_destroy (&pop3);
+	  pop3 = NULL;
+	}
+    }
+
+  if (status)
+    mu_error ("Failed to create pop3: %s", mu_strerror (status));
+  else
+    {
+      connect_argc = argc;
+      connect_argv = xcalloc (argc, sizeof (*connect_argv));
+      for (i = 0; i < argc; i++)
+	connect_argv[i] = xstrdup (argv[i]);
+      connect_argv[i] = NULL;
+      port = n;
+      pop_session_status = pop_session_connected;
+
+      pop_prompt_vartab ();
+    }
+  
+  return status;
+}
+
+static int
+com_quit (int argc MU_ARG_UNUSED, char **argv MU_ARG_UNUSED)
+{
+  int status = 0;
+  if (pop3)
+    {
+      if (mu_pop3_quit (pop3) == 0)
+	{
+	  status = com_disconnect (0, NULL);
+	}
+      else
+	{
+	  printf ("Try 'exit' to leave %s\n", mu_program_name);
+	}
+    }
+  else
+    printf ("Try 'exit' to leave %s\n", mu_program_name);
+  return status;
+}
+
+struct mutool_command pop_comtab[] = {
+  { "apop",       3,  3, com_apop,
+    "Authenticate with APOP: APOP user secret" },
+  { "capa",       1, -1, com_capa,
+    "List capabilities: capa [-reread] [names...]" },
+  { "disconnect", 1, 1,
+    com_disconnect, "Close connection: disconnect" },
+  { "dele",       2, 2, com_dele,
+    "Mark message: DELE msgno" },
+  { "list",       1, 2, com_list,
+    "List messages: LIST [msgno]" },
+  { "noop",       1, 1, com_noop,
+    "Send no operation: NOOP" },
+  { "pass",       1, 2, com_pass,
+    "Send passwd: PASS [passwd]" },
+  { "connect",    1, 4, com_connect,
+    "Open connection: connect [-tls] hostname port" },
+  { "quit",       1, 1, com_quit,
+    "Go to Update state : QUIT" },
+  { "retr",       2, 2, com_retr,
+    "Dowload message: RETR msgno" },
+  { "rset",       1, 1, com_rset,
+    "Unmark all messages: RSET" },
+  { "stat",       1, 1, com_stat,
+    "Get the size and count of mailbox : STAT" },
+  { "stls",       1, 1, com_stls,
+    "Start TLS negotiation" },
+  { "top",        2, 3, com_top,
+    "Get the header of message: TOP msgno [lines]" },
+  { "uidl",       1, 2, com_uidl,
+    "Get the unique id of message: UIDL [msgno]" },
+  { "user",       2, 2, com_user,
+    "send login: USER user" },
+  { "verbose",    1, 4, com_verbose,
+    "Enable Protocol tracing: verbose [on|off|mask|unmask] [x1 [x2]]" },
+  { NULL }
+};
+
+
+int
+mutool_pop (int argc, char **argv)
+{
+  int index;
+
+  if (argp_parse (&pop_argp, argc, argv, ARGP_IN_ORDER, &index, NULL))
+    return 1;
+
+  argc -= index;
+  argv += index;
+
+  if (argc)
+    {
+      mu_error (_("too many arguments"));
+      return 1;
+    }
+
+  /* Command line prompt */
+  mutool_shell_prompt = xstrdup ("pop> ");
+  pop_prompt_vartab ();
+  mutool_shell ("pop", pop_comtab);
+  return 0;
+}
diff --git a/mu/query.c b/mu/query.c
new file mode 100644
index 0000000..74b6aaf
--- /dev/null
+++ b/mu/query.c
@@ -0,0 +1,121 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 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.
+
+   GNU Mailutils 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 GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
+
+#if defined(HAVE_CONFIG_H)
+# include <config.h>
+#endif
+#include <stdlib.h>
+#include <string.h>
+#include <mailutils/mailutils.h>
+#include <mailutils/libcfg.h>
+#include "argp.h"
+#include "mu.h"
+
+static char query_doc[] = N_("mu query - query configuration values.");
+static char query_args_doc[] = N_("[OPTIONS]");
+
+char *file_name;
+int verbose_option;
+
+static struct argp_option query_options[] = {
+  { "file", 'f', N_("FILE"), 0,
+    N_("query configuration values from FILE (default mailutils.rc)"),
+    0 },
+  { "verbose", 'v', NULL, 0,
+    N_("increase output verbosity"), 0},
+  { NULL }
+};
+
+static error_t
+query_parse_opt (int key, char *arg, struct argp_state *state)
+{
+  switch (key)
+    {
+    case 'f':
+      file_name = arg;
+      break;
+
+    case 'v':
+      verbose_option++;
+      break;
+      
+    default:
+      return ARGP_ERR_UNKNOWN;
+    }
+  return 0;
+}
+
+static struct argp query_argp = {
+  query_options,
+  query_parse_opt,
+  query_args_doc,
+  query_doc,
+  NULL,
+  NULL,
+  NULL
+};
+
+int
+mutool_query (int argc, char **argv)
+{
+  int rc, index;
+  mu_cfg_tree_t *tree = NULL;
+  int fmtflags = 0;
+  mu_stream_t stream;
+  
+  if (argp_parse (&query_argp, argc, argv, ARGP_IN_ORDER, &index, NULL))
+    return 1;
+
+  argc -= index;
+  argv += index;
+
+  if (argc == 0)
+    {
+      mu_error (_("query what?"));
+      return 1;
+    }
+
+  if (file_name)
+    {
+      mu_load_site_rcfile = 0;
+      mu_load_user_rcfile = 0;
+      mu_load_rcfile = file_name;
+    }
+  
+  if (mu_libcfg_parse_config (&tree))
+    return 1;
+  if (!tree)
+    return 0;
+  rc = mu_stdio_stream_create (&stream, MU_STDOUT_FD, 0);
+  if (rc)
+    {
+      mu_error ("mu_stdio_stream_create: %s", mu_strerror (rc));
+      return 1;
+    }
+  if (verbose_option)
+    fmtflags = MU_CFG_FMT_LOCUS;
+  for ( ; argc > 0; argc--, argv++)
+    {
+      char *path = *argv;
+      mu_cfg_node_t *node;
+
+      if (mu_cfg_find_node (tree, path, &node) == 0)
+	mu_cfg_format_node (stream, node, fmtflags);
+    }
+  return 0;
+}
+ 
+  
diff --git a/mu/shell.c b/mu/shell.c
new file mode 100644
index 0000000..6a04fb3
--- /dev/null
+++ b/mu/shell.c
@@ -0,0 +1,492 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 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.
+
+   GNU Mailutils 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 GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
+
+#if defined(HAVE_CONFIG_H)
+# include <config.h>
+#endif
+#include <stdlib.h>
+#include <string.h>
+#include <mailutils/mailutils.h>
+#include <mailutils/tls.h>
+#include "mailutils/libargp.h"
+#include "mu.h"
+#include "xalloc.h"
+
+#ifdef WITH_READLINE
+# include <readline/readline.h>
+# include <readline/history.h>
+#endif
+
+char *mutool_shell_prompt;
+mu_vartab_t mutool_prompt_vartab;
+
+static char *
+expand_prompt ()
+{
+  char *str;
+  
+  if (!mutool_prompt_vartab
+      || mu_vartab_expand (mutool_prompt_vartab, mutool_shell_prompt, &str))
+    str = strdup (mutool_shell_prompt);
+  return str;
+}
+
+
+static int shell_exit (int, char **);
+static int shell_help (int, char **);
+static int shell_prompt (int, char **);
+#ifdef WITH_READLINE
+static int shell_history (int, char **);
+#endif
+
+struct mutool_command default_comtab[] = {
+  { "prompt",   -1, -1, shell_prompt,  "set command prompt" },
+  { "exit",       1, 1, shell_exit,    "exit program" },
+  { "help",       1, 2, shell_help,    "display this text" },
+  { "?",          1, 1, shell_help,    "synonym for `help'" },
+#ifdef WITH_READLINE
+  { "history",    1, 1, shell_history, "show command history" },
+#endif
+  { NULL }
+};
+
+/* Print a single comtab entry */
+static void
+print_comtab (struct mutool_command *tab)
+{
+  printf ("%s\t\t%s\n", tab->name, tab->doc);
+}  
+
+/* Print a single comtab entry.
+   FIXME: Way too primitive. Rewrite. */
+int
+print_help (struct mutool_command *tab, size_t n)
+{
+  if (n)
+    {
+      for (; n > 0 && tab->name; tab++, n--)
+	print_comtab (tab);
+    }
+  else
+    {
+      for (; tab->name; tab++)
+	print_comtab (tab);
+    }
+  return 0;
+}
+
+/* Print all commands from TAB matching NAME.
+   FIXME: Way too primitive. Rewrite. */
+void
+list_commands (struct mutool_command *tab, const char *name)
+{
+  size_t namelen = strlen (name);
+  int printed = 0;
+  
+  for (; tab->name; tab++)
+    {
+      /* Print in six columns. */
+      if (printed == 6)
+	{
+	  printed = 0;
+	  printf ("\n");
+	}
+      if (mu_c_strncasecmp (tab->name, name, namelen) == 0)
+	{
+	  printf ("%s\t", tab->name);
+	  printed++;
+	}
+    }
+  if (printed && printed < 6)
+    printf ("\n");
+}
+
+static struct mutool_command *
+simple_find_command (struct mutool_command *cp, const char *name)
+{
+  for (; cp->name; cp++)
+    if (strcmp (cp->name, name) == 0)
+      return cp;
+  return NULL;
+}
+
+
+static struct mutool_command *shell_comtab;
+static size_t user_command_count;
+
+static struct mutool_command *
+find_command (const char *name)
+{
+  return simple_find_command (shell_comtab, name);
+}
+
+/* Print out help for ARG, or for all of the commands if ARG is
+   not present. */
+int
+shell_help (int argc, char **argv)
+{
+  char *name = argv[1];
+  if (name)
+    {
+      struct mutool_command *com = find_command (argv[1]);
+
+      if (com)
+	print_comtab (com);
+      else
+	{
+	  printf ("No commands match `%s'.  Possibilties are:\n", name);
+	  list_commands (shell_comtab, name);
+	}
+    }
+  else
+    {
+      print_help (shell_comtab, user_command_count);
+      printf ("\n");
+      print_help (shell_comtab + user_command_count, 0);
+    }
+  return 0;
+}
+
+static int
+shell_prompt (int argc, char **argv)
+{
+  int quote;
+  size_t size;
+  
+  free (mutool_shell_prompt);
+  size = mu_argcv_quoted_length (argv[1], &quote);
+  mutool_shell_prompt = xmalloc (size + 1);
+  mu_argcv_unquote_copy (mutool_shell_prompt, argv[1], size);
+  return 0;
+}
+
+
+
+#ifdef WITH_READLINE
+#define HISTFILE_PREFIX "~/.mu_"
+#define HISTFILE_SUFFIX "_history"
+
+static char *
+get_history_file_name ()
+{
+  static char *filename = NULL;
+
+  if (!filename)
+    {
+      char *hname;
+      
+      hname = xmalloc(sizeof HISTFILE_PREFIX + strlen (rl_readline_name) +
+		      sizeof HISTFILE_SUFFIX - 1);
+      strcpy (hname, "~/.mu_");
+      strcat (hname, rl_readline_name);
+      strcat (hname, HISTFILE_SUFFIX);
+      filename = mu_tilde_expansion (hname, "/", NULL);
+      free(hname);
+    }
+  return filename;
+}
+
+/* Interface to Readline Completion */
+
+static char *shell_command_generator (const char *, int);
+static char **shell_completion (char *, int, int);
+
+/* Tell the GNU Readline library how to complete.  We want to try to complete
+   on command names if this is the first word in the line, or on filenames
+   if not. */
+void
+mutool_initialize_readline (const char *name)
+{
+  /* Allow conditional parsing of the ~/.inputrc file. */
+  rl_readline_name = (char *) name;
+  rl_attempted_completion_function = (CPPFunction *) shell_completion;
+  read_history (get_history_file_name ());
+}
+
+static void
+finish_readline ()
+{
+  write_history (get_history_file_name());
+}
+
+/* Attempt to complete on the contents of TEXT.  START and END bound the
+   region of rl_line_buffer that contains the word to complete.  TEXT is
+   the word to complete.  We can use the entire contents of rl_line_buffer
+   in case we want to do some simple parsing.  Return the array of matches,
+   or NULL if there aren't any. */
+static char **
+shell_completion (char *text, int start, int end MU_ARG_UNUSED)
+{
+  char **matches;
+
+  matches = (char **) NULL;
+
+  /* If this word is at the start of the line, then it is a command
+     to complete.  Otherwise it is the name of a file in the current
+     directory. */
+  if (start == 0)
+    matches = rl_completion_matches (text, shell_command_generator);
+
+  return (matches);
+}
+
+/* Generator function for command completion.  STATE lets us know whether
+   to start from scratch; without any state (i.e. STATE == 0), then we
+   start at the top of the list. */
+static char *
+shell_command_generator (const char *text, int state)
+{
+  const char *name;
+  static int len;
+  static struct mutool_command *cmd;
+
+  /* If this is a new word to complete, initialize now.  This includes
+     saving the length of TEXT for efficiency, and initializing the index
+     variable to 0. */
+  if (!state)
+    {
+      cmd = shell_comtab;
+      len = strlen (text);
+    }
+
+  if (!cmd->name)
+    return NULL;
+    
+  
+  /* Return the next name which partially matches from the command list. */
+  while ((name = cmd->name))
+    {
+      cmd++;
+      if (strncmp (name, text, len) == 0)
+	return xstrdup (name);
+    }
+
+  /* If no names matched, then return NULL. */
+  return NULL;
+}
+
+static char *pre_input_line;
+
+static int
+pre_input (void)
+{
+  rl_insert_text (pre_input_line);
+  free (pre_input_line);
+  rl_pre_input_hook = NULL;
+  rl_redisplay ();
+  return 0;
+}
+
+static int
+retrieve_history (char *str)
+{
+  char *out;
+  int rc;
+
+  rc = history_expand (str, &out);
+  switch (rc)
+    {
+    case -1:
+      mu_error ("%s", out);
+      free (out);
+      return 1;
+
+    case 0:
+      break;
+
+    case 1:
+      pre_input_line = out;
+      rl_pre_input_hook = pre_input;
+      return 1;
+
+    case 2:
+      printf ("%s\n", out);
+      free (out);
+      return 1;
+    }
+  return 0;
+}
+
+static int
+shell_history (int argc, char **argv)
+{
+  int i;
+  HIST_ENTRY **hlist;
+
+  hlist = history_list ();
+  for (i = 0; i < history_length; i++)
+    printf ("%4d) %s\n", i + 1, hlist[i]->line);
+
+  return 0;
+}
+
+#else
+# define finish_readline()
+# define mutool_initialize_readline (const char *name)
+
+char *
+readline (char *prompt)
+{
+  static size_t size = 0;
+  static char *buf = NULL;
+
+  if (prompt)
+    {
+      printf ("%s", prompt);
+      fflush (stdout);
+    }
+  if (getline (&buf, &size, stdin) <= 0)
+    return NULL;
+  return buf;
+}
+
+void
+add_history (const char *s MU_ARG_UNUSED)
+{
+}
+#endif
+
+/* Parse and execute a command line. */
+int
+execute_line (char *line)
+{
+  int argc;
+  char **argv;
+  int status = 0;
+
+  if (mu_argcv_get (line, NULL, "#", &argc, &argv))
+    {
+      mu_error("cannot parse input line");
+      return 0;
+    }
+
+  if (argc >= 0)
+    {
+      struct mutool_command *cmd = find_command (argv[0]);
+      
+      if (!cmd)
+	mu_error ("%s: no such command.", argv[0]);
+      else if (cmd->argmin > 0 && argc < cmd->argmin)
+	mu_error ("%s: too few arguments", argv[0]);
+      else if (cmd->argmax > 0 && argc > cmd->argmax)
+	mu_error ("%s: too many arguments", argv[0]);
+      else
+	{
+	  if (cmd->argmin <= 0 && argc != 2)
+	    {
+	      char *word = mu_str_skip_class (line, MU_CTYPE_SPACE);
+	      char *arg = mu_str_skip_class_comp (word, MU_CTYPE_SPACE);
+	      if (*arg)
+		{
+		  *arg++ = 0;
+		  arg = mu_str_skip_class (arg, MU_CTYPE_SPACE);
+		}
+	      
+	      mu_argcv_free (argc, argv);
+	      argc = 2;
+	      argv = xcalloc (argc + 1, sizeof (argv[0]));
+	      argv[0] = xstrdup (word);
+	      argv[1] = xstrdup (arg);
+	      argv[2] = NULL;
+	    }
+	  status = cmd->func (argc, argv);
+	}
+    }
+  mu_argcv_free (argc, argv);
+  return status;
+}
+
+static char *
+input_line_interactive ()
+{
+  char *p = expand_prompt ();
+  char *line = readline (p);
+  free (p);
+  return line;
+}
+
+static char *
+input_line_script ()
+{
+  size_t size = 0;
+  char *buf = NULL;
+  if (getline (&buf, &size, stdin) <= 0)
+    return NULL;
+  return buf;
+}
+
+static int done;
+
+static int
+shell_exit (int argc MU_ARG_UNUSED, char **argv MU_ARG_UNUSED)
+{
+  done = 1;
+  return 0;
+}
+  
+int
+mutool_shell (const char *name, struct mutool_command *cmd)
+{
+  size_t n;
+  int interactive = isatty (0);
+  char *(*input_line) () = interactive ?
+                             input_line_interactive : input_line_script;
+
+  for (n = 0; cmd[n].name; n++)
+    ;
+
+  user_command_count = n;
+  shell_comtab = xcalloc (n + MU_ARRAY_SIZE (default_comtab),
+			  sizeof (shell_comtab[0]));
+  memcpy (shell_comtab, cmd, n * sizeof (shell_comtab[0]));
+  memcpy (shell_comtab + n, default_comtab, sizeof (default_comtab));
+
+  mutool_initialize_readline (name);
+  while (!done)
+    {
+      char *s, *line = input_line ();
+      if (!line)
+	break;
+
+      /* Remove leading and trailing whitespace from the line.
+         Then, if there is anything left, add it to the history list
+         and execute it. */
+      s = mu_str_stripws (line);
+
+      if (*s)
+	{
+	  int status;
+
+#ifdef WITH_READLINE
+	  if (interactive)
+	    {
+	      if (retrieve_history (s))
+		continue;
+	      add_history (s);
+	    }
+#endif
+
+	  status = execute_line (s);
+	  if (status != 0)
+	    mu_error ("Error: %s", mu_strerror (status));
+	}
+
+      free (line);
+    }
+  if (interactive)
+    finish_readline ();
+  return 0;
+}
diff --git a/po/POTFILES.in b/po/POTFILES.in
index aa42c6f..a00a3c2 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -197,5 +197,13 @@ sieve/sieve.c
 
 sql/mysql.c
 
+mu/filter.c
+mu/flt2047.c
+mu/info.c
+mu/mu.c
+mu/pop.c
+mu/query.c
+mu/shell.c
+
 # EOF
 
diff --git a/pop3d/extra.c b/pop3d/extra.c
index 26935d6..a52fee2 100644
--- a/pop3d/extra.c
+++ b/pop3d/extra.c
@@ -206,17 +206,7 @@ pop3d_init_tls_server ()
   if (rc)
     return 1;
 
-  rc = mu_stream_open (tlsstream);
-  if (rc)
-    {
-      mu_diag_output (MU_DIAG_ERROR, _("cannot open TLS stream: %s"),
-		      mu_stream_strerror (tlsstream, rc));
-      mu_stream_destroy (&tlsstream);
-      return 1;
-    }
-  else
-    stream[0] = stream[1] = tlsstream;
-
+  stream[0] = stream[1] = tlsstream;
   rc = mu_stream_ioctl (iostream, MU_IOCTL_SET_STREAM, stream);
   mu_stream_unref (stream[0]);
   mu_stream_unref (stream[1]);
diff --git a/sieve/sieve.c b/sieve/sieve.c
index 1e1d395..22374da 100644
--- a/sieve/sieve.c
+++ b/sieve/sieve.c
@@ -365,12 +365,6 @@ sieve_message (mu_sieve_machine_t mach)
       mu_error (_("cannot create stream: %s"), mu_strerror (rc));
       return EX_SOFTWARE;
     }
-  rc = mu_stream_open (instr);
-  if (rc)
-    {
-      mu_error (_("cannot open stream: %s"), mu_strerror (rc));
-      return EX_SOFTWARE;
-    }
   rc = mu_stream_to_message (instr, &msg);
   if (rc)
     {
diff --git a/testsuite/smtpsend.c b/testsuite/smtpsend.c
index bb1112b..1b843e3 100644
--- a/testsuite/smtpsend.c
+++ b/testsuite/smtpsend.c
@@ -185,16 +185,12 @@ main (int argc, char **argv)
   if (!raw)
     flags = MU_STREAM_SEEK;
   if (infile)
-    {
-      MU_ASSERT (mu_file_stream_create (&instr, infile, MU_STREAM_READ|flags));
-      MU_ASSERT (mu_stream_open (instr));
-    }
+    MU_ASSERT (mu_file_stream_create (&instr, infile, MU_STREAM_READ|flags));
   else
     MU_ASSERT (mu_stdio_stream_create (&instr, MU_STDIN_FD, flags));
   
   host = argv[1];
   MU_ASSERT (mu_tcp_stream_create (&stream, host, port, MU_STREAM_RDWR));
-  MU_ASSERT (mu_stream_open (stream));
   mu_smtp_set_carrier (smtp, stream);
   mu_stream_unref (stream);
   


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Sun Oct 03 14:57:04 2010
Received: from mailman by lists.gnu.org with archive (Exim 4.43)
	id 1P2Tk8-00064P-Eb
	for mharc-commit-mailutils@gnu.org; Sun, 03 Oct 2010 14:57:04 -0400
Received: from [140.186.70.92] (port=52512 helo=eggs.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.43) id 1P2Tk5-00064G-9F
	for commit-mailutils@gnu.org; Sun, 03 Oct 2010 14:57:02 -0400
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P2Tk4-00071w-4E
	for commit-mailutils@gnu.org; Sun, 03 Oct 2010 14:57:01 -0400
Received: from colonialone.fsf.org ([140.186.70.51]:55801
	helo=internal.in.savannah.gnu.org)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P2Tk4-00071k-2d
	for commit-mailutils@gnu.org; Sun, 03 Oct 2010 14:57:00 -0400
Received: from [10.1.0.108] (helo=vcs-noshell.in.savannah.gnu.org)
	by internal.in.savannah.gnu.org with esmtp (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P2Tk3-0002Y6-IT
	for commit-mailutils@gnu.org; Sun, 03 Oct 2010 18:56:59 +0000
Received: from gray by vcs-noshell.in.savannah.gnu.org with local (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P2Tk3-0001gQ-Dk
	for commit-mailutils@gnu.org; Sun, 03 Oct 2010 18:56:59 +0000
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: e105df67dcc3f11787259c6334e68a9dabb167c1
X-Git-Newrev: 8f2e2f1fb926ffda9cf8ea264c391aae212e43ab
Message-Id: <E1P2Tk3-0001gQ-Dk@vcs-noshell.in.savannah.gnu.org>
Date: Sun, 03 Oct 2010 18:56:59 +0000
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
Subject: [SCM] GNU Mailutils branch, master,
	updated. release-2.2-115-g8f2e2f1
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.5
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=unsubscribe>
List-Archive: <http://lists.gnu.org/archive/html/commit-mailutils>
List-Post: <mailto:commit-mailutils@gnu.org>
List-Help: <mailto:commit-mailutils-request@gnu.org?subject=help>
List-Subscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Sun, 03 Oct 2010 18:57:02 -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=8f2e2f1fb926ffda9cf8ea264c391aae212e43ab

The branch, master has been updated
       via  8f2e2f1fb926ffda9cf8ea264c391aae212e43ab (commit)
      from  e105df67dcc3f11787259c6334e68a9dabb167c1 (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 8f2e2f1fb926ffda9cf8ea264c391aae212e43ab
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Sun Oct 3 21:57:11 2010 +0300

    Bugfix.
    
    * mu/filter.c (filter_options): Add missing arguments.
    * mu/flt2047.c (flt2047_options): Likewise.

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

Summary of changes:
 mu/filter.c  |    2 +-
 mu/flt2047.c |    6 ++++--
 2 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/mu/filter.c b/mu/filter.c
index 373eb0d..ee9d79f 100644
--- a/mu/filter.c
+++ b/mu/filter.c
@@ -29,7 +29,7 @@ static char filter_args_doc[] = N_("[OPTIONS] NAME");
 static struct argp_option filter_options[] = {
   { "encode", 'e', NULL, 0, N_("encode the input (default)") },
   { "decode", 'd', NULL, 0, N_("decode the input") },
-  { "line-length", 'l', NULL, 0, N_("limit output line length") },
+  { "line-length", 'l', N_("NUMBER"), 0, N_("limit output line length") },
   { "newline", 'n', NULL, 0, N_("print additional newline") },
   { NULL }
 };
diff --git a/mu/flt2047.c b/mu/flt2047.c
index f2ec974..8220f90 100644
--- a/mu/flt2047.c
+++ b/mu/flt2047.c
@@ -30,8 +30,10 @@ static struct argp_option flt2047_options[] = {
   { "encode", 'e', NULL, 0, N_("encode the input (default)") },
   { "decode", 'd', NULL, 0, N_("decode the input") },
   { "newline", 'n', NULL, 0, N_("print additional newline") },
-  { "charset", 'c', NULL, 0, N_("set charset (default: iso-8859-1)") },
-  { "encoding", 'E', NULL, 0, N_("set encoding (default: quoted-printable)") },
+  { "charset", 'c', N_("NAME"), 0,
+    N_("set charset (default: iso-8859-1)") },
+  { "encoding", 'E', N_("NAME"), 0,
+    N_("set encoding (default: quoted-printable)") },
   { NULL }
 };
   


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Sun Oct 03 15:02:23 2010
Received: from mailman by lists.gnu.org with archive (Exim 4.43)
	id 1P2TpH-0006r3-0c
	for mharc-commit-mailutils@gnu.org; Sun, 03 Oct 2010 15:02:23 -0400
Received: from [140.186.70.92] (port=58118 helo=eggs.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.43) id 1P2TpC-0006o7-8d
	for commit-mailutils@gnu.org; Sun, 03 Oct 2010 15:02:19 -0400
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P2TpA-00080p-5V
	for commit-mailutils@gnu.org; Sun, 03 Oct 2010 15:02:18 -0400
Received: from colonialone.fsf.org ([140.186.70.51]:34437
	helo=internal.in.savannah.gnu.org)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P2Tp9-00080i-Vv
	for commit-mailutils@gnu.org; Sun, 03 Oct 2010 15:02:16 -0400
Received: from [10.1.0.108] (helo=vcs-noshell.in.savannah.gnu.org)
	by internal.in.savannah.gnu.org with esmtp (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P2Tp9-0002db-GO
	for commit-mailutils@gnu.org; Sun, 03 Oct 2010 19:02:15 +0000
Received: from gray by vcs-noshell.in.savannah.gnu.org with local (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P2Tp9-00024h-Bi
	for commit-mailutils@gnu.org; Sun, 03 Oct 2010 19:02:15 +0000
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: 8f2e2f1fb926ffda9cf8ea264c391aae212e43ab
X-Git-Newrev: e32c2d801b1f05fc423b597e6b44c66d4213bbc2
Message-Id: <E1P2Tp9-00024h-Bi@vcs-noshell.in.savannah.gnu.org>
Date: Sun, 03 Oct 2010 19:02:15 +0000
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
Subject: [SCM] GNU Mailutils branch, master,
	updated. release-2.2-116-ge32c2d8
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.5
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=unsubscribe>
List-Archive: <http://lists.gnu.org/archive/html/commit-mailutils>
List-Post: <mailto:commit-mailutils@gnu.org>
List-Help: <mailto:commit-mailutils-request@gnu.org?subject=help>
List-Subscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Sun, 03 Oct 2010 19:02:19 -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=e32c2d801b1f05fc423b597e6b44c66d4213bbc2

The branch, master has been updated
       via  e32c2d801b1f05fc423b597e6b44c66d4213bbc2 (commit)
      from  8f2e2f1fb926ffda9cf8ea264c391aae212e43ab (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 e32c2d801b1f05fc423b597e6b44c66d4213bbc2
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Sun Oct 3 22:01:30 2010 +0300

    Fix docstrings in mu.

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

Summary of changes:
 mu/filter.c  |    2 +-
 mu/flt2047.c |    2 +-
 mu/info.c    |    4 ++--
 mu/query.c   |    2 +-
 4 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/mu/filter.c b/mu/filter.c
index ee9d79f..dedc2f9 100644
--- a/mu/filter.c
+++ b/mu/filter.c
@@ -24,7 +24,7 @@
 #include "mu.h"
 
 static char filter_doc[] = N_("mu filter");
-static char filter_args_doc[] = N_("[OPTIONS] NAME");
+static char filter_args_doc[] = N_("NAME");
 
 static struct argp_option filter_options[] = {
   { "encode", 'e', NULL, 0, N_("encode the input (default)") },
diff --git a/mu/flt2047.c b/mu/flt2047.c
index 8220f90..75d3b37 100644
--- a/mu/flt2047.c
+++ b/mu/flt2047.c
@@ -24,7 +24,7 @@
 #include "mu.h"
 
 static char flt2047_doc[] = N_("mu 2047 - decode/encode message headers");
-static char flt2047_args_doc[] = N_("[OPTIONS] [text]");
+static char flt2047_args_doc[] = N_("[text]");
 
 static struct argp_option flt2047_options[] = {
   { "encode", 'e', NULL, 0, N_("encode the input (default)") },
diff --git a/mu/info.c b/mu/info.c
index 56d5b12..e101d2b 100644
--- a/mu/info.c
+++ b/mu/info.c
@@ -24,9 +24,9 @@
 #include "mu.h"
 
 static char info_doc[] = N_("mu info - print a list of configuration\
-options used to build mailutils; optional arguments are interpreted\
+ options used to build mailutils; optional arguments are interpreted\
  as a list of configuration options to check for.");
-static char info_args_doc[] = N_("[OPTIONS] [capa...]");
+static char info_args_doc[] = N_("[capa...]");
 
 static struct argp_option info_options[] = {
   { "verbose", 'v', NULL, 0,
diff --git a/mu/query.c b/mu/query.c
index 74b6aaf..f338a3a 100644
--- a/mu/query.c
+++ b/mu/query.c
@@ -25,7 +25,7 @@
 #include "mu.h"
 
 static char query_doc[] = N_("mu query - query configuration values.");
-static char query_args_doc[] = N_("[OPTIONS]");
+static char query_args_doc[] = N_("keyword [keyword...]");
 
 char *file_name;
 int verbose_option;


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Sun Oct 03 18:28:24 2010
Received: from mailman by lists.gnu.org with archive (Exim 4.43)
	id 1P2X2e-0000bg-Oq
	for mharc-commit-mailutils@gnu.org; Sun, 03 Oct 2010 18:28:24 -0400
Received: from [140.186.70.92] (port=52370 helo=eggs.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.43) id 1P2X2c-0000a3-6R
	for commit-mailutils@gnu.org; Sun, 03 Oct 2010 18:28:23 -0400
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P2X2a-0001ke-Tv
	for commit-mailutils@gnu.org; Sun, 03 Oct 2010 18:28:22 -0400
Received: from colonialone.fsf.org ([140.186.70.51]:37504
	helo=internal.in.savannah.gnu.org)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P2X2a-0001kY-R0
	for commit-mailutils@gnu.org; Sun, 03 Oct 2010 18:28:20 -0400
Received: from [10.1.0.108] (helo=vcs-noshell.in.savannah.gnu.org)
	by internal.in.savannah.gnu.org with esmtp (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P2X2a-000576-KG
	for commit-mailutils@gnu.org; Sun, 03 Oct 2010 22:28:20 +0000
Received: from gray by vcs-noshell.in.savannah.gnu.org with local (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P2X2a-0006A6-Hf
	for commit-mailutils@gnu.org; Sun, 03 Oct 2010 22:28:20 +0000
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: e32c2d801b1f05fc423b597e6b44c66d4213bbc2
X-Git-Newrev: 96e44735dce29f8cdf0f8512bd3bfccc4b350055
Message-Id: <E1P2X2a-0006A6-Hf@vcs-noshell.in.savannah.gnu.org>
Date: Sun, 03 Oct 2010 22:28:20 +0000
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
Subject: [SCM] GNU Mailutils branch, master,
	updated. release-2.2-117-g96e4473
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.5
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=unsubscribe>
List-Archive: <http://lists.gnu.org/archive/html/commit-mailutils>
List-Post: <mailto:commit-mailutils@gnu.org>
List-Help: <mailto:commit-mailutils-request@gnu.org?subject=help>
List-Subscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Sun, 03 Oct 2010 22:28:23 -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=96e44735dce29f8cdf0f8512bd3bfccc4b350055

The branch, master has been updated
       via  96e44735dce29f8cdf0f8512bd3bfccc4b350055 (commit)
      from  e32c2d801b1f05fc423b597e6b44c66d4213bbc2 (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 96e44735dce29f8cdf0f8512bd3bfccc4b350055
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Mon Oct 4 00:00:25 2010 +0300

    Improve path parsing in mu_cfg_find_node.
    
    * libmailutils/cfg_parser.y (split_cfg_path): New static.
    (mu_cfg_find_node): Use the same parsing algorithm as
    mu_cfg_create_subtree.  This complements 68b7dc9dac.
    (mu_cfg_create_subtree): Use split_cfg_path.  Handle
    escaped '=' as a regular character.
    * mu/mu.c (args_doc): Remove spurious word.
    (main): Disable --version option in action handlers.
    * mu/query.c (query_args_doc): Use `path' instead of `keyword'.

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

Summary of changes:
 libmailutils/cfg_parser.y |  108 +++++++++++++++++++++++++++------------------
 mu/mu.c                   |   10 +++-
 mu/query.c                |    2 +-
 3 files changed, 73 insertions(+), 47 deletions(-)

diff --git a/libmailutils/cfg_parser.y b/libmailutils/cfg_parser.y
index 8b13ae1..f6a5397 100644
--- a/libmailutils/cfg_parser.y
+++ b/libmailutils/cfg_parser.y
@@ -1576,6 +1576,49 @@ mu_cfg_value_eq (mu_config_value_t *a, mu_config_value_t *b)
 }
 
 
+static int
+split_cfg_path (const char *path, int *pargc, char ***pargv)
+{
+  int rc;
+  int argc;
+  char **argv;
+  char *delim = MU_CFG_PATH_DELIM_STR;
+  char static_delim[2] = { 0, 0 };
+  
+  if (path[0] == '\\')
+    {
+      argv = calloc (2, sizeof (*argv));
+      if (!argv)
+	return ENOMEM;
+      argv[0] = strdup (path + 1);
+      if (!argv[0])
+	{
+	  free (argv);
+	  return ENOMEM;
+	}
+      argv[1] = NULL;
+      argc = 1;
+      rc = 0;
+    }
+  else
+    {
+      if (mu_ispunct (path[0]))
+	{
+	  delim = static_delim;
+	  delim[0] = path[0];
+	  path++;
+	}
+      rc = mu_argcv_get_np (path, strlen (path), delim, NULL, 0,
+			    &argc, &argv, NULL);
+    }
+  if (rc == 0)
+    {
+      *pargc = argc;
+      *pargv = argv;
+    }
+  return rc;
+}
+
 struct find_data
 {
   int argc;
@@ -1714,9 +1757,7 @@ mu_cfg_find_node (mu_cfg_tree_t *tree, const char *path, mu_cfg_node_t **pval)
   struct find_data data;
   struct mu_cfg_iter_closure clos;
 
-  rc = mu_argcv_get_np (path, strlen (path),
-			MU_CFG_PATH_DELIM_STR, NULL,
-			0, &data.argc, &data.argv, NULL);
+  rc = split_cfg_path (path, &data.argc, &data.argv);
   if (rc)
     return rc;
   data.tag = 0;
@@ -1742,44 +1783,15 @@ mu_cfg_create_subtree (const char *path, mu_cfg_node_t **pnode)
 {
   int rc;
   int argc, i;
-  char *p;
   char **argv;
-  mu_cfg_locus_t locus;
   enum mu_cfg_node_type type;
   mu_cfg_node_t *node = NULL;
-  char *delim = MU_CFG_PATH_DELIM_STR;
-  char static_delim[2] = { 0, 0 };
-  
+  mu_cfg_locus_t locus;
+
   locus.file = "<int>";
   locus.line = 0;
 
-  if (path[0] == '\\')
-    {
-      argv = calloc (2, sizeof (*argv));
-      if (!argv)
-	return ENOMEM;
-      argv[0] = strdup (path + 1);
-      if (!argv[0])
-	{
-	  free (argv);
-	  return ENOMEM;
-	}
-      argv[1] = NULL;
-      argc = 1;
-      rc = 0;
-    }
-  else
-    {
-      if (mu_ispunct (path[0]))
-	{
-	  delim = static_delim;
-	  delim[0] = path[0];
-	  path++;
-	}
-      rc = mu_argcv_get_np (path, strlen (path), delim, NULL, 0,
-			    &argc, &argv, NULL);
-    }
-  
+  rc = split_cfg_path (path, &argc, &argv);
   if (rc)
     return rc;
 
@@ -1787,17 +1799,27 @@ mu_cfg_create_subtree (const char *path, mu_cfg_node_t **pnode)
     {
       mu_list_t nodelist = NULL;
       mu_config_value_t *label = NULL;
-
-      p = strrchr (argv[i], '=');
+      char *q = argv[i], *p;
+      
       type = mu_cfg_node_statement;
-      if (p)
+      do
 	{
-	  *p++ = 0;
-	  label = parse_label (p);
-	  if (i == argc - 1)
-	    type = mu_cfg_node_param;
+	  p = strchr (q, '=');
+	  if (p && p > argv[i] && p[-1] != '\\')
+	    {
+	      *p++ = 0;
+	      label = parse_label (p);
+	      if (i == argc - 1)
+		type = mu_cfg_node_param;
+	      break;
+	    }
+	  else if (p)
+	    q = p + 1;
+	  else
+	    break;
 	}
-
+      while (*q);
+      
       if (node)
 	{
 	  mu_cfg_create_node_list (&nodelist);
diff --git a/mu/mu.c b/mu/mu.c
index cff6a12..9af9e0a 100644
--- a/mu/mu.c
+++ b/mu/mu.c
@@ -24,8 +24,8 @@
 #include "mailutils/libargp.h"
 #include "mu.h"
 
-static char args_doc[] = N_("[OPTIONS] COMMAND [CMDOPTS]");
-static char doc[] = N_("mu -- GNU Mailutils test tool.\n\
+static char args_doc[] = N_("COMMAND [CMDOPTS]");
+static char doc[] = N_("mu -- GNU Mailutils multi-purpose tool.\n\
 Commands are:\n\
     mu info   - show Mailutils configuration\n\
     mu query  - query configuration values\n\
@@ -33,7 +33,7 @@ Commands are:\n\
     mu filter - filter program\n\
     mu 2047   - decode/encode message headers as per RFC 2047\n\
 \n\
-Try `mu COMMAND --help' to get help on a particular COMMAND\n\
+Try `mu COMMAND --help' to get help on a particular COMMAND.\n\
 \n\
 Options are:\n");
 /* FIXME: add
@@ -143,6 +143,10 @@ main (int argc, char **argv)
       exit (1);
     }
 
+  /* Disable --version option in action. */
+  argp_program_version = NULL;
+  argp_program_version_hook = NULL;
+  /* Run the action. */
   exit (action (argc, argv));
 }
 
diff --git a/mu/query.c b/mu/query.c
index f338a3a..fd951c2 100644
--- a/mu/query.c
+++ b/mu/query.c
@@ -25,7 +25,7 @@
 #include "mu.h"
 
 static char query_doc[] = N_("mu query - query configuration values.");
-static char query_args_doc[] = N_("keyword [keyword...]");
+static char query_args_doc[] = N_("path [path...]");
 
 char *file_name;
 int verbose_option;


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Mon Oct 04 07:49:41 2010
Received: from mailman by lists.gnu.org with archive (Exim 4.43)
	id 1P2jY4-0002Ml-Kt
	for mharc-commit-mailutils@gnu.org; Mon, 04 Oct 2010 07:49:40 -0400
Received: from [140.186.70.92] (port=36376 helo=eggs.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.43) id 1P2jY0-00018g-Qf
	for commit-mailutils@gnu.org; Mon, 04 Oct 2010 07:49:38 -0400
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P2jXv-0000zI-Nn
	for commit-mailutils@gnu.org; Mon, 04 Oct 2010 07:49:33 -0400
Received: from colonialone.fsf.org ([140.186.70.51]:53692
	helo=internal.in.savannah.gnu.org)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P2jXv-0000z5-Lo
	for commit-mailutils@gnu.org; Mon, 04 Oct 2010 07:49:31 -0400
Received: from [10.1.0.108] (helo=vcs-noshell.in.savannah.gnu.org)
	by internal.in.savannah.gnu.org with esmtp (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P2jXv-0006OU-8n
	for commit-mailutils@gnu.org; Mon, 04 Oct 2010 11:49:31 +0000
Received: from gray by vcs-noshell.in.savannah.gnu.org with local (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P2jXv-0007tp-2G
	for commit-mailutils@gnu.org; Mon, 04 Oct 2010 11:49:31 +0000
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: 96e44735dce29f8cdf0f8512bd3bfccc4b350055
X-Git-Newrev: 271d62e93d6dc12f80d785d680df34cc35fddd90
Message-Id: <E1P2jXv-0007tp-2G@vcs-noshell.in.savannah.gnu.org>
Date: Mon, 04 Oct 2010 11:49:31 +0000
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
Subject: [SCM] GNU Mailutils branch, master,
	updated. release-2.2-118-g271d62e
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.5
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=unsubscribe>
List-Archive: <http://lists.gnu.org/archive/html/commit-mailutils>
List-Post: <mailto:commit-mailutils@gnu.org>
List-Help: <mailto:commit-mailutils-request@gnu.org?subject=help>
List-Subscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Mon, 04 Oct 2010 11:49:39 -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=271d62e93d6dc12f80d785d680df34cc35fddd90

The branch, master has been updated
       via  271d62e93d6dc12f80d785d680df34cc35fddd90 (commit)
      from  96e44735dce29f8cdf0f8512bd3bfccc4b350055 (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 271d62e93d6dc12f80d785d680df34cc35fddd90
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Mon Oct 4 14:08:51 2010 +0300

    New mu submode: acl.  Supersedes examples/aclck.c
    
    * mu/acl.c: New file.
    * mu/Makefile.am: Add acl.c
    * po/POTFILES.in: Add acl.c
    * mu/mu.c (mutool_action_tab): Add acl mode.
    * mu/mu.h (mutool_acl): New proto.
    * mu/flt2047.c: Fix docstrings.
    * mu/pop.c: Likewise.

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

Summary of changes:
 mu/Makefile.am |    1 +
 mu/acl.c       |  193 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 mu/flt2047.c   |    2 +-
 mu/mu.c        |    2 +
 mu/mu.h        |    1 +
 mu/pop.c       |    2 +-
 po/POTFILES.in |    1 +
 7 files changed, 200 insertions(+), 2 deletions(-)
 create mode 100644 mu/acl.c

diff --git a/mu/Makefile.am b/mu/Makefile.am
index 0a3c50e..c9b5e39 100644
--- a/mu/Makefile.am
+++ b/mu/Makefile.am
@@ -17,6 +17,7 @@
 
 bin_PROGRAMS = mu
 mu_SOURCES = \
+ acl.c\
  info.c\
  mu.h\
  mu.c\
diff --git a/mu/acl.c b/mu/acl.c
new file mode 100644
index 0000000..0c9cc21
--- /dev/null
+++ b/mu/acl.c
@@ -0,0 +1,193 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 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.
+
+   GNU Mailutils 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 GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
+
+#if defined(HAVE_CONFIG_H)
+# include <config.h>
+#endif
+#include <stdlib.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <sys/un.h>
+#include <arpa/inet.h>
+#include <mailutils/mailutils.h>
+#include <mailutils/libcfg.h>
+#include "argp.h"
+#include "mu.h"
+#include "xalloc.h"
+
+static char acl_doc[] = N_("mu acl - test access control lists.");
+static char acl_args_doc[] = N_("ADDRESS [ADDRESS...]");
+
+static struct argp_option acl_options[] = {
+  { "file", 'f', N_("FILE"), 0, N_("read ACLs from FILE") },
+  { "path", 'p', N_("PATH"), 0,
+    N_("path to the ACL in the configuration tree") },
+  { NULL }
+};
+
+static char *input_file_name;
+static struct sockaddr *target_sa;
+static socklen_t target_salen;
+static mu_acl_t acl;
+static const char *path = "acl";
+
+static struct sockaddr *
+parse_address (socklen_t *psalen, const char *str)
+{
+  struct sockaddr_in in;
+  struct sockaddr *sa;
+  
+  in.sin_family = AF_INET;
+  if (inet_aton (str, &in.sin_addr) == 0)
+    {
+      mu_error ("Invalid IPv4: %s", str);
+      exit (1);
+    }
+  in.sin_port = 0;
+  *psalen = sizeof (in);
+  sa = malloc (*psalen);
+  if (!sa)
+    {
+      mu_error ("%s", mu_strerror (errno));
+      exit (1);
+    }
+  
+  memcpy (sa, &in, sizeof (in));
+  return sa;
+}
+
+static error_t
+acl_parse_opt (int key, char *arg, struct argp_state *state)
+{
+  switch (key)
+    {
+    case 'f':
+      input_file_name = arg;
+      break;
+
+    case 'p':
+      path = arg;
+      break;
+      
+    default:
+      return ARGP_ERR_UNKNOWN;
+    }
+  return 0;
+}
+
+static struct argp acl_argp = {
+  acl_options,
+  acl_parse_opt,
+  acl_args_doc,
+  acl_doc,
+  NULL,
+  NULL,
+  NULL
+};
+
+
+static struct mu_cfg_param acl_cfg_param[] = {
+  { "acl", mu_cfg_section, &acl, 0, NULL, "access control list" },
+  { NULL }
+};
+
+int
+mutool_acl (int argc, char **argv)
+{
+  int rc, index;
+  mu_acl_result_t result;
+  mu_cfg_tree_t *tree = NULL, *temp_tree = NULL;
+  mu_cfg_node_t *node;
+  int flags = 0;
+  
+  if (argp_parse (&acl_argp, argc, argv, ARGP_IN_ORDER, &index, NULL))
+    return 1;
+
+  argc -= index;
+  argv += index;
+
+  if (argc == 0)
+    {
+      mu_error (_("not enough arguments"));
+      return 1;
+    }
+
+  if (input_file_name)
+    {
+      mu_load_site_rcfile = 0;
+      mu_load_user_rcfile = 0;
+      mu_load_rcfile = input_file_name;
+    }
+
+  mu_acl_cfg_init ();
+  if (mu_libcfg_parse_config (&tree))
+    return 1;
+  if (!tree)
+    return 0;
+  
+  if (mu_cfg_find_node (tree, path, &node))
+    {
+      mu_error (_("cannot find node: %s"), path);
+      return 1;
+    }
+
+  mu_cfg_tree_create (&temp_tree);
+  mu_cfg_tree_add_node (temp_tree, node);
+  rc = mu_cfg_tree_reduce (temp_tree, mu_program_name, acl_cfg_param,
+			   flags, NULL);
+  if (rc)
+    return 1;
+  if (!acl)
+    {
+      mu_error (_("No ACL found in config"));
+      return 1;
+    }
+  
+  while (argc--)
+    {
+      const char *ap = *argv++;
+
+      target_sa = parse_address (&target_salen, ap);
+      printf ("Testing %s:\n", ap);
+      rc = mu_acl_check_sockaddr (acl, target_sa, target_salen, &result);
+      if (rc)
+	{
+	  mu_error ("mu_acl_check_sockaddr failed: %s", mu_strerror (rc));
+	  return 1;
+	}
+
+      switch (result)
+	{
+	case mu_acl_result_undefined:
+	  printf ("%s: undefined\n", ap);
+	  break;
+      
+	case mu_acl_result_accept:
+	  printf ("%s: accept\n", ap);
+	  break;
+
+	case mu_acl_result_deny:
+	  printf ("%s: deny\n", ap);
+	  break;
+	}
+    }
+
+  mu_cfg_destroy_tree (&tree);
+  mu_cfg_destroy_tree (&temp_tree);
+
+  return 0;
+}
diff --git a/mu/flt2047.c b/mu/flt2047.c
index 75d3b37..7c6c9d6 100644
--- a/mu/flt2047.c
+++ b/mu/flt2047.c
@@ -23,7 +23,7 @@
 #include "argp.h"
 #include "mu.h"
 
-static char flt2047_doc[] = N_("mu 2047 - decode/encode message headers");
+static char flt2047_doc[] = N_("mu 2047 - decode/encode message headers.");
 static char flt2047_args_doc[] = N_("[text]");
 
 static struct argp_option flt2047_options[] = {
diff --git a/mu/mu.c b/mu/mu.c
index 9af9e0a..784698c 100644
--- a/mu/mu.c
+++ b/mu/mu.c
@@ -32,6 +32,7 @@ Commands are:\n\
     mu pop    - POP3 client program\n\
     mu filter - filter program\n\
     mu 2047   - decode/encode message headers as per RFC 2047\n\
+    mu acl    - test access control lists\n\
 \n\
 Try `mu COMMAND --help' to get help on a particular COMMAND.\n\
 \n\
@@ -92,6 +93,7 @@ struct mutool_action_tab mutool_action_tab[] = {
   { "filter", mutool_filter },
   { "2047", mutool_flt2047 },
   { "query", mutool_query },
+  { "acl", mutool_acl },
   { NULL }
 };
 
diff --git a/mu/mu.h b/mu/mu.h
index 5fe3097..a251d7d 100644
--- a/mu/mu.h
+++ b/mu/mu.h
@@ -33,6 +33,7 @@ int mutool_filter (int argc, char **argv);
 int mutool_flt2047 (int argc, char **argv);
 int mutool_info (int argc, char **argv);
 int mutool_query (int argc, char **argv);
+int mutool_acl (int argc, char **argv);
 
 extern char *mutool_shell_prompt;
 extern mu_vartab_t mutool_prompt_vartab;
diff --git a/mu/pop.c b/mu/pop.c
index 8375776..e4b9a25 100644
--- a/mu/pop.c
+++ b/mu/pop.c
@@ -27,7 +27,7 @@
 #include "argp.h"
 #include "xalloc.h"
 
-static char pop_doc[] = N_("mu pop - POP3 client shell");
+static char pop_doc[] = N_("mu pop - POP3 client shell.");
 static char pop_args_doc[] = "";
 
 static struct argp_option pop_options[] = {
diff --git a/po/POTFILES.in b/po/POTFILES.in
index a00a3c2..386fe5b 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -197,6 +197,7 @@ sieve/sieve.c
 
 sql/mysql.c
 
+mu/acl.c
 mu/filter.c
 mu/flt2047.c
 mu/info.c


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Tue Oct 05 04:46:31 2010
Received: from mailman by lists.gnu.org with archive (Exim 4.43)
	id 1P33AN-0000V9-Dc
	for mharc-commit-mailutils@gnu.org; Tue, 05 Oct 2010 04:46:31 -0400
Received: from [140.186.70.92] (port=45707 helo=eggs.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.43) id 1P33AB-0007TO-Dj
	for commit-mailutils@gnu.org; Tue, 05 Oct 2010 04:46:29 -0400
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P32rz-000094-C7
	for commit-mailutils@gnu.org; Tue, 05 Oct 2010 04:27:32 -0400
Received: from colonialone.fsf.org ([140.186.70.51]:38229
	helo=internal.in.savannah.gnu.org)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P32rz-00008z-7h
	for commit-mailutils@gnu.org; Tue, 05 Oct 2010 04:27:31 -0400
Received: from [10.1.0.108] (helo=vcs-noshell.in.savannah.gnu.org)
	by internal.in.savannah.gnu.org with esmtp (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P32ry-0004jV-P2
	for commit-mailutils@gnu.org; Tue, 05 Oct 2010 08:27:30 +0000
Received: from gray by vcs-noshell.in.savannah.gnu.org with local (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P32ry-0001PT-Lw
	for commit-mailutils@gnu.org; Tue, 05 Oct 2010 08:27:30 +0000
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: 271d62e93d6dc12f80d785d680df34cc35fddd90
X-Git-Newrev: df67a458159e4bbfcfd59248091182e55aaa0a9a
Message-Id: <E1P32ry-0001PT-Lw@vcs-noshell.in.savannah.gnu.org>
Date: Tue, 05 Oct 2010 08:27:30 +0000
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
Subject: [SCM] GNU Mailutils branch, master,
	updated. release-2.2-119-gdf67a45
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.5
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=unsubscribe>
List-Archive: <http://lists.gnu.org/archive/html/commit-mailutils>
List-Post: <mailto:commit-mailutils@gnu.org>
List-Help: <mailto:commit-mailutils-request@gnu.org?subject=help>
List-Subscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Tue, 05 Oct 2010 08:46:29 -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=df67a458159e4bbfcfd59248091182e55aaa0a9a

The branch, master has been updated
       via  df67a458159e4bbfcfd59248091182e55aaa0a9a (commit)
      from  271d62e93d6dc12f80d785d680df34cc35fddd90 (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 df67a458159e4bbfcfd59248091182e55aaa0a9a
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Tue Oct 5 11:27:58 2010 +0300

    Minor bugfixes.

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

Summary of changes:
 libmailutils/debug.c  |    1 +
 libmailutils/mailer.c |    2 +-
 2 files changed, 2 insertions(+), 1 deletions(-)

diff --git a/libmailutils/debug.c b/libmailutils/debug.c
index 7570f23..d738cef 100644
--- a/libmailutils/debug.c
+++ b/libmailutils/debug.c
@@ -235,6 +235,7 @@ mu_debug_print (mu_debug_t debug, mu_log_level_t level,
 		const char *format, ...)
 {
   va_list ap;
+  va_start(ap, format);
   mu_debug_printv (debug, level, format, ap);
   va_end (ap);
   return 0;
diff --git a/libmailutils/mailer.c b/libmailutils/mailer.c
index 2240b65..8faf778 100644
--- a/libmailutils/mailer.c
+++ b/libmailutils/mailer.c
@@ -489,7 +489,7 @@ send_fragments (mu_mailer_t mailer,
 		struct timeval *delay,
 		mu_address_t from, mu_address_t to)
 {
-  int status;
+  int status = 0;
   size_t i;
   char *msgid = NULL;
   


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Tue Oct 05 09:15:54 2010
Received: from mailman by lists.gnu.org with archive (Exim 4.43)
	id 1P37N4-0004VG-Ej
	for mharc-commit-mailutils@gnu.org; Tue, 05 Oct 2010 09:15:54 -0400
Received: from [140.186.70.92] (port=33744 helo=eggs.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.43) id 1P37Li-0003RN-A2
	for commit-mailutils@gnu.org; Tue, 05 Oct 2010 09:15:51 -0400
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P37KL-0007vw-20
	for commit-mailutils@gnu.org; Tue, 05 Oct 2010 09:14:27 -0400
Received: from colonialone.fsf.org ([140.186.70.51]:44291
	helo=internal.in.savannah.gnu.org)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P37KK-0007uu-R8
	for commit-mailutils@gnu.org; Tue, 05 Oct 2010 09:13:04 -0400
Received: from [10.1.0.108] (helo=vcs-noshell.in.savannah.gnu.org)
	by internal.in.savannah.gnu.org with esmtp (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P37KK-0008FR-H0
	for commit-mailutils@gnu.org; Tue, 05 Oct 2010 13:13:04 +0000
Received: from gray by vcs-noshell.in.savannah.gnu.org with local (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P37KK-0002dq-Ep
	for commit-mailutils@gnu.org; Tue, 05 Oct 2010 13:13:04 +0000
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
X-Git-Refname: refs/heads/patches-2.2
X-Git-Reftype: branch
X-Git-Oldrev: dd13dedfe0580e391e2a082ff9d1ac842292a4eb
X-Git-Newrev: 3edaa0f6738e29773cc4fa92dd96900c3e4f351e
Message-Id: <E1P37KK-0002dq-Ep@vcs-noshell.in.savannah.gnu.org>
Date: Tue, 05 Oct 2010 13:13:04 +0000
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
Subject: [SCM] GNU Mailutils branch, patches-2.2,
	updated. release-2.2-10-g3edaa0f
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.5
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=unsubscribe>
List-Archive: <http://lists.gnu.org/archive/html/commit-mailutils>
List-Post: <mailto:commit-mailutils@gnu.org>
List-Help: <mailto:commit-mailutils-request@gnu.org?subject=help>
List-Subscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Tue, 05 Oct 2010 13:15:53 -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=3edaa0f6738e29773cc4fa92dd96900c3e4f351e

The branch, patches-2.2 has been updated
       via  3edaa0f6738e29773cc4fa92dd96900c3e4f351e (commit)
       via  3e13a4112af5136b08c4888f627151baee6a1aee (commit)
      from  dd13dedfe0580e391e2a082ff9d1ac842292a4eb (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 3edaa0f6738e29773cc4fa92dd96900c3e4f351e
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Tue Oct 5 15:44:04 2010 +0300

    Remove the use of MAXHOSTNAMELEN macro.
    
    * comsat/comsat.c (hostname): Change type.
    (comsat_init): Use mu_get_host_name.
    * comsat/comsat.h (hostname): Change declaration.
    * mailbox/locker.c (lock_dotlock): Use mu_get_host_name.
    * mailbox/mutil.c (mu_get_host_name): Rewrite.
    * pop3d/pop3d.c (pop3d_mainloop): Remove unnecessary call to
    gethostbyname.
    * pop3d/pop3d.h (MAXHOSTNAMELEN): Remove definition.

commit 3e13a4112af5136b08c4888f627151baee6a1aee
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Tue Oct 5 15:40:57 2010 +0300

    Fix uninitialized variables.
    
    * mailbox/debug.c (mu_debug_print): Initialize ap.
    * mailbox/list.c (list_itrctl): Initialize ptr.
    * mailbox/mailer.c (send_fragments): Initialize status.

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

Summary of changes:
 comsat/comsat.c  |   18 ++++++-----
 comsat/comsat.h  |    2 +-
 mailbox/debug.c  |    1 +
 mailbox/list.c   |    1 +
 mailbox/locker.c |   20 +++++++-----
 mailbox/mailer.c |    2 +-
 mailbox/mutil.c  |   86 ++++++++++++++++++++++++++++++++++++++++++-----------
 pop3d/pop3d.c    |   20 ++++--------
 pop3d/pop3d.h    |    6 ----
 9 files changed, 100 insertions(+), 56 deletions(-)

diff --git a/comsat/comsat.c b/comsat/comsat.c
index 82e8056..3fe9e36 100644
--- a/comsat/comsat.c
+++ b/comsat/comsat.c
@@ -100,12 +100,8 @@ static const char *comsat_argp_capa[] = {
 #define NOT_HERE 1
 #define PERMISSION_DENIED 2
 
-#ifndef MAXHOSTNAMELEN
-# define MAXHOSTNAMELEN 64
-#endif
-
 int maxlines = 5;
-char hostname[MAXHOSTNAMELEN];
+char *hostname;
 const char *username;
 int require_tty;
 mu_m_server_t server;
@@ -220,11 +216,17 @@ sig_hup (int sig)
 void
 comsat_init ()
 {
+  int rc;
+
   /* Register mailbox formats */
   mu_register_all_mbox_formats ();
 
-  gethostname (hostname, sizeof hostname);
-
+  rc = mu_get_host_name (&hostname);
+  if (rc)
+    {
+      mu_diag_funcall (MU_DIAG_ERROR, "mu_get_host_name", NULL, rc);
+      exit (EXIT_FAILURE);
+    } 
   /* Set signal handlers */
   signal (SIGTTOU, SIG_IGN);
   signal (SIGCHLD, SIG_IGN);
@@ -586,7 +588,7 @@ main (int argc, char **argv)
   
   if (mu_app_init (&argp, comsat_argp_capa, comsat_cfg_param, argc, argv, 0,
 		   &ind, server))
-    exit (1);
+    exit (EXIT_FAILURE);
 
   if (test_mode)
     {
diff --git a/comsat/comsat.h b/comsat/comsat.h
index d2a932c..f1aa604 100644
--- a/comsat/comsat.h
+++ b/comsat/comsat.h
@@ -75,7 +75,7 @@ extern time_t overflow_control_interval;
 extern time_t overflow_delay_time;
 extern int maxlines;
 extern const char *username;
-extern char hostname[];
+extern char *hostname;
 extern struct daemon_param daemon_param;
 
 void run_user_action (FILE *tty, const char *cr, mu_message_t msg);
diff --git a/mailbox/debug.c b/mailbox/debug.c
index 37f9e47..b595a7f 100644
--- a/mailbox/debug.c
+++ b/mailbox/debug.c
@@ -234,6 +234,7 @@ mu_debug_print (mu_debug_t debug, mu_log_level_t level,
 		const char *format, ...)
 {
   va_list ap;
+  va_start (ap, format);
   mu_debug_printv (debug, level, format, ap);
   va_end (ap);
   return 0;
diff --git a/mailbox/list.c b/mailbox/list.c
index f47af25..55802e2 100644
--- a/mailbox/list.c
+++ b/mailbox/list.c
@@ -599,6 +599,7 @@ list_itrctl (void *owner, enum mu_itrctl_req req, void *arg)
       /* Replace current element */
       if (!arg)
 	return EINVAL;
+      ptr = itr->cur;
       if (req == mu_itrctl_replace)
 	  DESTROY_ITEM (list, ptr);
       ptr = itr->cur;
diff --git a/mailbox/locker.c b/mailbox/locker.c
index f752c16..4f0dcf2 100644
--- a/mailbox/locker.c
+++ b/mailbox/locker.c
@@ -704,14 +704,11 @@ destroy_dotlock (mu_locker_t locker)
   free (locker->data.dot.nfslock);
 }
 
-#ifndef MAXHOSTNAMELEN
-# define MAXHOSTNAMELEN 256
-#endif
-
 static int
 lock_dotlock (mu_locker_t locker, enum mu_locker_mode mode)
 {
-  char host[MAXHOSTNAMELEN + 1] = "localhost";
+  int rc;
+  char *host = NULL;
   char pid[11];		/* 10 is strlen(2^32 = 4294967296) */
   char now[11];
   size_t sz = 0;
@@ -729,8 +726,9 @@ lock_dotlock (mu_locker_t locker, enum mu_locker_mode mode)
 
   /* build the NFS hitching-post to the lock file */
 
-  gethostname (host, sizeof (host));
-  host[MAXHOSTNAMELEN] = 0;
+  rc = mu_get_host_name (&host);
+  if (rc)
+    return rc;
 
   snprintf (now, sizeof (now), "%lu", (unsigned long) time (0));
   now[sizeof (now) - 1] = 0;
@@ -746,10 +744,14 @@ lock_dotlock (mu_locker_t locker, enum mu_locker_mode mode)
   locker->data.dot.nfslock = malloc (sz);
   
   if (!locker->data.dot.nfslock)
-    return ENOMEM;
-  
+    {
+      free (host);
+      return ENOMEM;
+    }
+
   snprintf (locker->data.dot.nfslock, sz, "%s.%s.%s.%s",
 	    locker->file, pid, now, host);
+  free (host);
   
   fd = open (locker->data.dot.nfslock,
 	     O_WRONLY | O_CREAT | O_EXCL, LOCKFILE_ATTR);
diff --git a/mailbox/mailer.c b/mailbox/mailer.c
index f2786c8..bd64683 100644
--- a/mailbox/mailer.c
+++ b/mailbox/mailer.c
@@ -487,7 +487,7 @@ send_fragments (mu_mailer_t mailer,
 		struct timeval *delay,
 		mu_address_t from, mu_address_t to)
 {
-  int status;
+  int status = 0;
   size_t i;
   char *msgid = NULL;
   
diff --git a/mailbox/mutil.c b/mailbox/mutil.c
index 656a31f..5e2691d 100644
--- a/mailbox/mutil.c
+++ b/mailbox/mutil.c
@@ -291,30 +291,80 @@ mu_cpystr (char *dst, const char *src, size_t size)
   return len;
 }
 
+#ifndef MAXHOSTNAMELEN
+# define MAXHOSTNAMELEN 64
+#endif
+
 int
 mu_get_host_name (char **host)
 {
-  char hostname[MAXHOSTNAMELEN + 1];
-  struct hostent *hp = NULL;
-  char *domain = NULL;
-
-  gethostname (hostname, sizeof hostname);
-  hostname[sizeof (hostname) - 1] = 0;
-
-  if ((hp = gethostbyname (hostname)))
-    domain = hp->h_name;
-  else
-    domain = hostname;
-
-  domain = strdup (domain);
-
-  if (!domain)
-    return ENOMEM;
+  char *hostname = NULL;
+  size_t size = 0;
+  char *p;
 
-  *host = domain;
+  while (1)
+    {
+      if (size == 0)
+	{
+	  size = MAXHOSTNAMELEN;
+	  p = malloc (size);
+	}
+      else
+	{
+	  size_t ns = size * 2;
+	  if (ns < size)
+	    {
+	      free (hostname);
+	      return ENOMEM;
+	    }
+	  size = ns;
+	  p = realloc (hostname, size);
+	}
+      if (!p)
+	{
+	  free (hostname);
+	  return ENOMEM;
+	}
+      hostname = p;
+      hostname[size - 1] = 0;
+      if (gethostname (hostname, size - 1) == 0)
+	{
+	  if (!hostname[size - 1])
+	    break;
+	}
+      else if (errno != 0 && errno != ENAMETOOLONG && errno != EINVAL
+	       && errno != ENOMEM)
+	{
+	  int rc = errno;
+	  free (hostname);
+	  return rc;
+	}
+    }
 
+  /* Try to return fully qualified host name */
+  if (!strchr (hostname, '.'))
+    {
+      struct hostent *hp = gethostbyname (hostname);
+      if (hp)
+	{
+	  size_t len = strlen (hp->h_name);
+	  if (size < len + 1)
+	    {
+	      p = realloc (hostname, len + 1);
+	      if (!p)
+		{
+		  free (hostname);
+		  return ENOMEM;
+		}
+	      hostname = p;
+	    }
+	  strcpy (hostname, hp->h_name);
+	}
+    }
+  
+  *host = hostname;
   return 0;
-}
+}  
 
 /*
  * Functions used to convert unix mailbox/user names into RFC822 addr-specs.
diff --git a/pop3d/pop3d.c b/pop3d/pop3d.c
index d0e7b60..75a5fe7 100644
--- a/pop3d/pop3d.c
+++ b/pop3d/pop3d.c
@@ -226,19 +226,13 @@ pop3d_mainloop (int fd, FILE *infile, FILE *outfile)
   /* Prepare the shared secret for APOP.  */
   {
     char *local_hostname;
-    local_hostname = mu_alloc (MAXHOSTNAMELEN + 1);
-
-    /* Get our canonical hostname. */
-    {
-      struct hostent *htbuf;
-      gethostname (local_hostname, MAXHOSTNAMELEN);
-      htbuf = gethostbyname (local_hostname);
-      if (htbuf)
-	{
-	  free (local_hostname);
-	  local_hostname = strdup (htbuf->h_name);
-	}
-    }
+    
+    status = mu_get_host_name (&local_hostname);
+    if (status)
+      {
+        mu_diag_funcall (MU_DIAG_ERROR, "mu_get_host_name", NULL, status);
+        exit (EXIT_FAILURE);
+      }
 
     md5shared = mu_alloc (strlen (local_hostname) + 51);
 
diff --git a/pop3d/pop3d.h b/pop3d/pop3d.h
index 1c1c7d6..ea773bc 100644
--- a/pop3d/pop3d.h
+++ b/pop3d/pop3d.h
@@ -137,12 +137,6 @@ extern int expire_on_exit;
 #include <shadow.h>
 #endif
 
-#ifndef MAXHOSTNAMELEN
-/* Maximum length of a hostname (is this defined somewhere else?).  */
-/* MAXHOSTNAMELEN is already defined on Solaris.  */
-# define MAXHOSTNAMELEN	64
-#endif
-
 #define POP3_ATTRIBUTE_DELE 0x0001
 #define POP3_ATTRIBUTE_RETR 0x0010
 


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Tue Oct 05 09:16:00 2010
Received: from mailman by lists.gnu.org with archive (Exim 4.43)
	id 1P37NA-0004Xj-PX
	for mharc-commit-mailutils@gnu.org; Tue, 05 Oct 2010 09:16:00 -0400
Received: from [140.186.70.92] (port=33771 helo=eggs.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.43) id 1P37Lr-0003Sa-T1
	for commit-mailutils@gnu.org; Tue, 05 Oct 2010 09:15:59 -0400
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P37KX-0008BT-CQ
	for commit-mailutils@gnu.org; Tue, 05 Oct 2010 09:14:36 -0400
Received: from colonialone.fsf.org ([140.186.70.51]:44292
	helo=internal.in.savannah.gnu.org)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P37KX-0008BP-8I
	for commit-mailutils@gnu.org; Tue, 05 Oct 2010 09:13:17 -0400
Received: from [10.1.0.108] (helo=vcs-noshell.in.savannah.gnu.org)
	by internal.in.savannah.gnu.org with esmtp (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P37KX-0008Fh-79
	for commit-mailutils@gnu.org; Tue, 05 Oct 2010 13:13:17 +0000
Received: from gray by vcs-noshell.in.savannah.gnu.org with local (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P37KX-0002mo-2r
	for commit-mailutils@gnu.org; Tue, 05 Oct 2010 13:13:17 +0000
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: df67a458159e4bbfcfd59248091182e55aaa0a9a
X-Git-Newrev: c5cc666d68f00af1bbe8e13c2c7a8bd0b224d891
Message-Id: <E1P37KX-0002mo-2r@vcs-noshell.in.savannah.gnu.org>
Date: Tue, 05 Oct 2010 13:13:17 +0000
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
Subject: [SCM] GNU Mailutils branch, master,
	updated. release-2.2-120-gc5cc666
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.5
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=unsubscribe>
List-Archive: <http://lists.gnu.org/archive/html/commit-mailutils>
List-Post: <mailto:commit-mailutils@gnu.org>
List-Help: <mailto:commit-mailutils-request@gnu.org?subject=help>
List-Subscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Tue, 05 Oct 2010 13:15:59 -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=c5cc666d68f00af1bbe8e13c2c7a8bd0b224d891

The branch, master has been updated
       via  c5cc666d68f00af1bbe8e13c2c7a8bd0b224d891 (commit)
      from  df67a458159e4bbfcfd59248091182e55aaa0a9a (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 c5cc666d68f00af1bbe8e13c2c7a8bd0b224d891
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Tue Oct 5 15:37:48 2010 +0300

    Remove the use of MAXHOSTNAMELEN macro.
    
    * comsat/comsat.c (hostname): Change type.
    (comsat_init): Use mu_get_host_name.
    * comsat/comsat.h (hostname): Change declaration.
    * libmailutils/locker.c (lock_dotlock): Use mu_get_host_name.
    * libmailutils/mutil.c (mu_get_host_name): Rewrite.
    * pop3d/pop3d.c (pop3d_mainloop): Remove unnecessary call to
    gethostbyname.
    * pop3d/pop3d.h (MAXHOSTNAMELEN): Remove definition.

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

Summary of changes:
 comsat/comsat.c       |   18 ++++++----
 comsat/comsat.h       |    2 +-
 libmailutils/locker.c |   20 ++++++-----
 libmailutils/mutil.c  |   86 ++++++++++++++++++++++++++++++++++++++----------
 pop3d/pop3d.c         |   20 ++++-------
 pop3d/pop3d.h         |    6 ---
 6 files changed, 97 insertions(+), 55 deletions(-)

diff --git a/comsat/comsat.c b/comsat/comsat.c
index 4ec589b..0b511fa 100644
--- a/comsat/comsat.c
+++ b/comsat/comsat.c
@@ -97,12 +97,8 @@ static const char *comsat_argp_capa[] = {
 #define NOT_HERE 1
 #define PERMISSION_DENIED 2
 
-#ifndef MAXHOSTNAMELEN
-# define MAXHOSTNAMELEN 64
-#endif
-
 int maxlines = 5;
-char hostname[MAXHOSTNAMELEN];
+char *hostname;
 const char *username;
 int require_tty;
 mu_m_server_t server;
@@ -190,11 +186,17 @@ sig_hup (int sig)
 void
 comsat_init ()
 {
+  int rc;
+
   /* Register mailbox formats */
   mu_register_all_mbox_formats ();
 
-  gethostname (hostname, sizeof hostname);
-
+  rc = mu_get_host_name (&hostname);
+  if (rc)
+    {
+      mu_diag_funcall (MU_DIAG_ERROR, "mu_get_host_name", NULL, rc);
+      exit (EXIT_FAILURE);
+    } 
   /* Set signal handlers */
   signal (SIGTTOU, SIG_IGN);
   signal (SIGCHLD, SIG_IGN);
@@ -556,7 +558,7 @@ main (int argc, char **argv)
   
   if (mu_app_init (&argp, comsat_argp_capa, comsat_cfg_param, argc, argv, 0,
 		   &ind, server))
-    exit (1);
+    exit (EXIT_FAILURE);
 
   if (test_mode)
     {
diff --git a/comsat/comsat.h b/comsat/comsat.h
index b9e9a0b..de4abb9 100644
--- a/comsat/comsat.h
+++ b/comsat/comsat.h
@@ -75,7 +75,7 @@ extern time_t overflow_control_interval;
 extern time_t overflow_delay_time;
 extern int maxlines;
 extern const char *username;
-extern char hostname[];
+extern char *hostname;
 extern struct daemon_param daemon_param;
 
 void run_user_action (FILE *tty, const char *cr, mu_message_t msg);
diff --git a/libmailutils/locker.c b/libmailutils/locker.c
index f752c16..4f0dcf2 100644
--- a/libmailutils/locker.c
+++ b/libmailutils/locker.c
@@ -704,14 +704,11 @@ destroy_dotlock (mu_locker_t locker)
   free (locker->data.dot.nfslock);
 }
 
-#ifndef MAXHOSTNAMELEN
-# define MAXHOSTNAMELEN 256
-#endif
-
 static int
 lock_dotlock (mu_locker_t locker, enum mu_locker_mode mode)
 {
-  char host[MAXHOSTNAMELEN + 1] = "localhost";
+  int rc;
+  char *host = NULL;
   char pid[11];		/* 10 is strlen(2^32 = 4294967296) */
   char now[11];
   size_t sz = 0;
@@ -729,8 +726,9 @@ lock_dotlock (mu_locker_t locker, enum mu_locker_mode mode)
 
   /* build the NFS hitching-post to the lock file */
 
-  gethostname (host, sizeof (host));
-  host[MAXHOSTNAMELEN] = 0;
+  rc = mu_get_host_name (&host);
+  if (rc)
+    return rc;
 
   snprintf (now, sizeof (now), "%lu", (unsigned long) time (0));
   now[sizeof (now) - 1] = 0;
@@ -746,10 +744,14 @@ lock_dotlock (mu_locker_t locker, enum mu_locker_mode mode)
   locker->data.dot.nfslock = malloc (sz);
   
   if (!locker->data.dot.nfslock)
-    return ENOMEM;
-  
+    {
+      free (host);
+      return ENOMEM;
+    }
+
   snprintf (locker->data.dot.nfslock, sz, "%s.%s.%s.%s",
 	    locker->file, pid, now, host);
+  free (host);
   
   fd = open (locker->data.dot.nfslock,
 	     O_WRONLY | O_CREAT | O_EXCL, LOCKFILE_ATTR);
diff --git a/libmailutils/mutil.c b/libmailutils/mutil.c
index 5342523..2b091ce 100644
--- a/libmailutils/mutil.c
+++ b/libmailutils/mutil.c
@@ -291,30 +291,80 @@ mu_cpystr (char *dst, const char *src, size_t size)
   return len;
 }
 
+#ifndef MAXHOSTNAMELEN
+# define MAXHOSTNAMELEN 64
+#endif
+
 int
 mu_get_host_name (char **host)
 {
-  char hostname[MAXHOSTNAMELEN + 1];
-  struct hostent *hp = NULL;
-  char *domain = NULL;
-
-  gethostname (hostname, sizeof hostname);
-  hostname[sizeof (hostname) - 1] = 0;
-
-  if ((hp = gethostbyname (hostname)))
-    domain = hp->h_name;
-  else
-    domain = hostname;
-
-  domain = strdup (domain);
-
-  if (!domain)
-    return ENOMEM;
+  char *hostname = NULL;
+  size_t size = 0;
+  char *p;
 
-  *host = domain;
+  while (1)
+    {
+      if (size == 0)
+	{
+	  size = MAXHOSTNAMELEN;
+	  p = malloc (size);
+	}
+      else
+	{
+	  size_t ns = size * 2;
+	  if (ns < size)
+	    {
+	      free (hostname);
+	      return ENOMEM;
+	    }
+	  size = ns;
+	  p = realloc (hostname, size);
+	}
+      if (!p)
+	{
+	  free (hostname);
+	  return ENOMEM;
+	}
+      hostname = p;
+      hostname[size - 1] = 0;
+      if (gethostname (hostname, size - 1) == 0)
+	{
+	  if (!hostname[size - 1])
+	    break;
+	}
+      else if (errno != 0 && errno != ENAMETOOLONG && errno != EINVAL
+	       && errno != ENOMEM)
+	{
+	  int rc = errno;
+	  free (hostname);
+	  return rc;
+	}
+    }
 
+  /* Try to return fully qualified host name */
+  if (!strchr (hostname, '.'))
+    {
+      struct hostent *hp = gethostbyname (hostname);
+      if (hp)
+	{
+	  size_t len = strlen (hp->h_name);
+	  if (size < len + 1)
+	    {
+	      p = realloc (hostname, len + 1);
+	      if (!p)
+		{
+		  free (hostname);
+		  return ENOMEM;
+		}
+	      hostname = p;
+	    }
+	  strcpy (hostname, hp->h_name);
+	}
+    }
+  
+  *host = hostname;
   return 0;
-}
+}  
 
 /*
  * Functions used to convert unix mailbox/user names into RFC822 addr-specs.
diff --git a/pop3d/pop3d.c b/pop3d/pop3d.c
index a614786..ab5ed7c 100644
--- a/pop3d/pop3d.c
+++ b/pop3d/pop3d.c
@@ -227,19 +227,13 @@ pop3d_mainloop (int ifd, int ofd)
   /* Prepare the shared secret for APOP.  */
   {
     char *local_hostname;
-    local_hostname = mu_alloc (MAXHOSTNAMELEN + 1);
-
-    /* Get our canonical hostname. */
-    {
-      struct hostent *htbuf;
-      gethostname (local_hostname, MAXHOSTNAMELEN);
-      htbuf = gethostbyname (local_hostname);
-      if (htbuf)
-	{
-	  free (local_hostname);
-	  local_hostname = strdup (htbuf->h_name);
-	}
-    }
+    
+    status = mu_get_host_name (&local_hostname);
+    if (status)
+      {
+        mu_diag_funcall (MU_DIAG_ERROR, "mu_get_host_name", NULL, status);
+        exit (EXIT_FAILURE);
+      }
 
     md5shared = mu_alloc (strlen (local_hostname) + 51);
 
diff --git a/pop3d/pop3d.h b/pop3d/pop3d.h
index a80c872..862ce72 100644
--- a/pop3d/pop3d.h
+++ b/pop3d/pop3d.h
@@ -133,12 +133,6 @@ extern int expire_on_exit;
 #include <shadow.h>
 #endif
 
-#ifndef MAXHOSTNAMELEN
-/* Maximum length of a hostname (is this defined somewhere else?).  */
-/* MAXHOSTNAMELEN is already defined on Solaris.  */
-# define MAXHOSTNAMELEN	64
-#endif
-
 #define POP3_ATTRIBUTE_DELE 0x0001
 #define POP3_ATTRIBUTE_RETR 0x0010
 


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Tue Oct 05 16:43:22 2010
Received: from mailman by lists.gnu.org with archive (Exim 4.43)
	id 1P3EM5-00055w-Ok
	for mharc-commit-mailutils@gnu.org; Tue, 05 Oct 2010 16:43:21 -0400
Received: from [140.186.70.92] (port=40532 helo=eggs.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.43) id 1P3EM1-00053g-8t
	for commit-mailutils@gnu.org; Tue, 05 Oct 2010 16:43:20 -0400
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P3ELy-0008DT-Fn
	for commit-mailutils@gnu.org; Tue, 05 Oct 2010 16:43:17 -0400
Received: from colonialone.fsf.org ([140.186.70.51]:34802
	helo=internal.in.savannah.gnu.org)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P3ELy-0008DP-CD
	for commit-mailutils@gnu.org; Tue, 05 Oct 2010 16:43:14 -0400
Received: from [10.1.0.108] (helo=vcs-noshell.in.savannah.gnu.org)
	by internal.in.savannah.gnu.org with esmtp (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P3ELy-0005Tg-0W
	for commit-mailutils@gnu.org; Tue, 05 Oct 2010 20:43:14 +0000
Received: from gray by vcs-noshell.in.savannah.gnu.org with local (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P3ELx-0008C1-G6
	for commit-mailutils@gnu.org; Tue, 05 Oct 2010 20:43:13 +0000
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: c5cc666d68f00af1bbe8e13c2c7a8bd0b224d891
X-Git-Newrev: c9e2cdac6f0c33f1175f19ca9095bc26f5a099ac
Message-Id: <E1P3ELx-0008C1-G6@vcs-noshell.in.savannah.gnu.org>
Date: Tue, 05 Oct 2010 20:43:13 +0000
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
Subject: [SCM] GNU Mailutils branch, master,
	updated. release-2.2-125-gc9e2cda
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.5
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=unsubscribe>
List-Archive: <http://lists.gnu.org/archive/html/commit-mailutils>
List-Post: <mailto:commit-mailutils@gnu.org>
List-Help: <mailto:commit-mailutils-request@gnu.org?subject=help>
List-Subscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Tue, 05 Oct 2010 20:43:20 -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=c9e2cdac6f0c33f1175f19ca9095bc26f5a099ac

The branch, master has been updated
       via  c9e2cdac6f0c33f1175f19ca9095bc26f5a099ac (commit)
       via  c2b45bdeb4d02ff41ca157f83f90399ea7ea5e1f (commit)
       via  b4f0301366ab4953e3650a0340231a9b94df93ce (commit)
       via  8575afa0eb3a3f38642e28ec5ab109bc128c62dd (commit)
       via  63d1c19652942be6b600a5855880667f16fdb8a3 (commit)
      from  c5cc666d68f00af1bbe8e13c2c7a8bd0b224d891 (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 c9e2cdac6f0c33f1175f19ca9095bc26f5a099ac
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Tue Oct 5 23:39:37 2010 +0300

    Improve mu shell interface.
    
    * mu/mu.h (mutool_command) <doc>: Rename to docstring.
    <argdoc>: New member.
    * mu/pop.c (com_apop): 2nd arg (password) is optional.  If not
    supplied, ask for it using the mu_getpass function.
    (pop_comtab): Add argdocs.  Use N_ wrappers around translatable
    strings.
    * mu/shell.c (default_comtab: Add argdocs.  Use N_ wrappers around
    translatable strings.
    (print_comtab): Rewrite.
    (shell_history): Use pager.

commit c2b45bdeb4d02ff41ca157f83f90399ea7ea5e1f
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Tue Oct 5 23:38:19 2010 +0300

    Minor bugfixes in pop client code.
    
    * libproto/pop/pop3_carrier.c (mu_pop3_set_carrier): Switch to
    MU_POP3_CONNECT state.
    * libproto/pop/pop3_disconnect.c (mu_pop3_disconnect): Check if
    the stream is actually open before trying to close it.

commit b4f0301366ab4953e3650a0340231a9b94df93ce
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Tue Oct 5 23:37:30 2010 +0300

    Add a missing function.
    
    * libmailutils/strskip.c (mu_str_skip_cset): New function.

commit 8575afa0eb3a3f38642e28ec5ab109bc128c62dd
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Tue Oct 5 21:53:39 2010 +0300

    Implement echo control on fd streams.  Implement mu_getpass function.
    
    * configure.ac: Test for tcgetattr and tcsetattr.
    * gnulib.modules (getpass-gnu): Remove.
    * include/mailutils/mutil.h (mu_getpass): New proto.
    * include/mailutils/stream.h (MU_IOCTL_GET_ECHO)
    (MU_IOCTL_SET_ECHO): New ioctls.
    * include/mailutils/sys/file_stream.h (_MU_FILE_STREAM_ECHO_OFF): New flag.
    (_mu_file_stream) <echo_state>: New member.
    * libmailutils/getpass.c: New file.
    * libmailutils/Makefile.am (libmailutils_la_SOURCES): Add getpass.c.
    * libmailutils/file_stream.c (fd_done): Free echo_state.
    (fd_ioctl): Implement MU_IOCTL_GET_ECHO and MU_IOCTL_SET_ECHO.
    * mu/mu.h (mustrin): New extern.
    * mu/pop.c (com_pass): Use mu_getpass.
    * mu/shell.c (mustrin): New variable.
    (readline): Use mu_stream_getline instead of getline.
    (mutool_shell): Create mustrin.
    * pop3d/popauth.c (options): Fix typo.
    (fill_pass): Use mu_getpass.

commit 63d1c19652942be6b600a5855880667f16fdb8a3
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Tue Oct 5 19:57:42 2010 +0300

    Implement pager in mu shell.
    
    * mu/mu.h (mutool_shell_interactive): New extern.
    (mutool_open_pager): New proto.
    * mu/pop.c (com_list, com_top, com_retr): Use pager.
    * mu/shell.c (mutool_shell_interactive, mustrout): New
    variables.
    (print_comtab, print_help): Print to stream.
    (shell_help): Use pager if necessary.
    (mutool_open_pager): New function.
    (mutool_shell): Create mutool_stream_out and destroy it before
    returning.
    (all functions): Use mu_stream_printf instead of printf.

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

Summary of changes:
 configure.ac                          |    2 +-
 gnulib.modules                        |    1 -
 include/mailutils/mutil.h             |    2 +
 include/mailutils/stream.h            |    3 +
 include/mailutils/sys/file_stream.h   |    4 +-
 libmailutils/Makefile.am              |    1 +
 libmailutils/file_stream.c            |   94 ++++++++++++++++--
 libmailutils/{nullrec.c => getpass.c} |   60 +++++++-----
 libmailutils/strskip.c                |    9 ++
 libproto/pop/pop3_carrier.c           |    1 +
 libproto/pop/pop3_disconnect.c        |    4 +-
 mu/mu.h                               |    7 +-
 mu/pop.c                              |  160 ++++++++++++++++--------------
 mu/shell.c                            |  176 +++++++++++++++++++++++++++-----
 pop3d/popauth.c                       |   68 ++++++++++---
 15 files changed, 435 insertions(+), 157 deletions(-)
 copy libmailutils/{nullrec.c => getpass.c} (54%)

diff --git a/configure.ac b/configure.ac
index 509a569..796c468 100644
--- a/configure.ac
+++ b/configure.ac
@@ -515,7 +515,7 @@ extern char *strsignal (int);
 ])
 
 AC_CHECK_FUNCS(mkstemp sigaction sysconf getdelim setreuid \
- setresuid seteuid setlocale vfork _exit)
+ setresuid seteuid setlocale vfork _exit tcgetattr tcsetattr)
 
 AC_FUNC_FSEEKO
 AC_FUNC_SETVBUF_REVERSED
diff --git a/gnulib.modules b/gnulib.modules
index 4368cf7..6639959 100644
--- a/gnulib.modules
+++ b/gnulib.modules
@@ -8,7 +8,6 @@ autobuild
 argp
 crypto/des
 getline
-getpass-gnu
 gettext
 gitlog-to-changelog
 intprops
diff --git a/include/mailutils/mutil.h b/include/mailutils/mutil.h
index 7acc021..86d2243 100644
--- a/include/mailutils/mutil.h
+++ b/include/mailutils/mutil.h
@@ -157,6 +157,8 @@ extern int mu_stream_flags_to_mode (int flags, int isdir);
 extern int mu_parse_stream_perm_string (int *pmode, const char *str,
 					const char **endp);
   
+extern int mu_getpass (mu_stream_t in, mu_stream_t out, const char *prompt,
+		       char **passptr);
 
 #ifdef __cplusplus
 }
diff --git a/include/mailutils/stream.h b/include/mailutils/stream.h
index c076ade..d951f34 100644
--- a/include/mailutils/stream.h
+++ b/include/mailutils/stream.h
@@ -74,6 +74,9 @@ enum mu_buffer_type
 #define MU_IOCTL_GET_TRANSPORT_BUFFER 10
 #define MU_IOCTL_SET_TRANSPORT_BUFFER 11
 
+#define MU_IOCTL_GET_ECHO        12     
+#define MU_IOCTL_SET_ECHO        13  
+  
 #define MU_TRANSPORT_INPUT  0
 #define MU_TRANSPORT_OUTPUT 1
 #define MU_TRANSPORT_VALID_TYPE(n) \
diff --git a/include/mailutils/sys/file_stream.h b/include/mailutils/sys/file_stream.h
index 70c576a..581d289 100644
--- a/include/mailutils/sys/file_stream.h
+++ b/include/mailutils/sys/file_stream.h
@@ -21,7 +21,8 @@
 #include <mailutils/stream.h>
 #include <mailutils/sys/stream.h>
 
-#define _MU_FILE_STREAM_TEMP 0x01
+#define _MU_FILE_STREAM_TEMP     0x01
+#define _MU_FILE_STREAM_ECHO_OFF 0x02
 
 struct _mu_file_stream
 {
@@ -29,6 +30,7 @@ struct _mu_file_stream
   int fd;
   int flags;
   char *filename;
+  void *echo_state;
 };
 
 int _mu_file_stream_create (struct _mu_file_stream **pstream, size_t size,
diff --git a/libmailutils/Makefile.am b/libmailutils/Makefile.am
index e05edd8..3f083e0 100644
--- a/libmailutils/Makefile.am
+++ b/libmailutils/Makefile.am
@@ -84,6 +84,7 @@ libmailutils_la_SOURCES = \
  folder.c\
  freeitem.c\
  gdebug.c\
+ getpass.c\
  gocs.c\
  hdritr.c\
  header.c\
diff --git a/libmailutils/file_stream.c b/libmailutils/file_stream.c
index 247768c..578f482 100644
--- a/libmailutils/file_stream.c
+++ b/libmailutils/file_stream.c
@@ -23,6 +23,9 @@
 #include <unistd.h>
 #include <fcntl.h>
 #include <sys/stat.h>
+#if HAVE_TERMIOS_H
+# include <termios.h>
+#endif
 
 #include <mailutils/types.h>
 #include <mailutils/alloc.h>
@@ -183,6 +186,8 @@ fd_done (struct _mu_stream *str)
     fd_close (str);
   if (fstr->filename)
     free (fstr->filename);
+  if (fstr->echo_state)
+    free (fstr->echo_state);
 }
 
 const char *
@@ -194,6 +199,10 @@ fd_error_string (struct _mu_stream *str, int rc)
     return mu_strerror (rc);
 }
 
+#ifndef TCSASOFT
+# define TCSASOFT 0
+#endif
+
 static int
 fd_ioctl (struct _mu_stream *str, int code, void *ptr)
 {
@@ -218,17 +227,86 @@ fd_ioctl (struct _mu_stream *str, int code, void *ptr)
       break;
       
     case MU_IOCTL_GET_TRANSPORT_BUFFER:
-      {
-        struct mu_buffer_query *qp = ptr;
-	return mu_stream_get_buffer (str, qp);
-      }
+      if (!ptr)
+	return EINVAL;
+      else
+	{
+	  struct mu_buffer_query *qp = ptr;
+	  return mu_stream_get_buffer (str, qp);
+	}
       
     case MU_IOCTL_SET_TRANSPORT_BUFFER:
-      {
-        struct mu_buffer_query *qp = ptr;
-	return mu_stream_set_buffer (str, qp->buftype, qp->bufsize);
-      }
+      if (!ptr)
+	return EINVAL;
+      else
+	{
+	  struct mu_buffer_query *qp = ptr;
+	  return mu_stream_set_buffer (str, qp->buftype, qp->bufsize);
+	}
+
+    case MU_IOCTL_SET_ECHO:
+      if (!ptr)
+	return EINVAL;
+      else
+	{
+	  int status;
+	  struct termios t;
+	  int state = *(int*)ptr;
+#if HAVE_TCGETATTR
+	  if (state == 0)
+	    {
+	      if (fstr->flags & _MU_FILE_STREAM_ECHO_OFF)
+		return 0;
+	      status = tcgetattr (fstr->fd, &t);
+	      if (status == 0)
+		{
+		  fstr->echo_state = malloc (sizeof (t));
+		  if (!fstr->echo_state)
+		    return ENOMEM;
+		  memcpy (fstr->echo_state, &t, sizeof (t));
+
+		  t.c_lflag &= ~(ECHO | ISIG);
+		  status = tcsetattr (fstr->fd, TCSAFLUSH | TCSASOFT, &t);
+		  if (status == 0)
+		    fstr->flags |= _MU_FILE_STREAM_ECHO_OFF;
+		}
+	      if (status)
+		{
+		  status = errno;
+		  if (fstr->echo_state)
+		    {
+		      free (fstr->echo_state);
+		      fstr->echo_state = NULL;
+		    }
+		}
+	    }
+	  else
+	    {
+	      if (!(fstr->flags & _MU_FILE_STREAM_ECHO_OFF))
+		return 0;
+	      if (tcsetattr (fstr->fd, TCSAFLUSH | TCSASOFT, fstr->echo_state))
+		status = errno;
+	      else
+		{
+		  status = 0;
+		  free (fstr->echo_state);
+		  fstr->echo_state = NULL;
+		  fstr->flags &= ~_MU_FILE_STREAM_ECHO_OFF;
+		}
+	    }
+	  return status;
+#else
+	  return ENOSYS;
+#endif
+	}
       
+    case MU_IOCTL_GET_ECHO:
+      if (!ptr)
+	return EINVAL;
+      else
+	*(int*)ptr = fstr->flags & _MU_FILE_STREAM_ECHO_OFF;
+      break;
+	  
     default:
       return ENOSYS;
     }
diff --git a/libmailutils/nullrec.c b/libmailutils/getpass.c
similarity index 54%
copy from libmailutils/nullrec.c
copy to libmailutils/getpass.c
index 9d080cd..8a37c52 100644
--- a/libmailutils/nullrec.c
+++ b/libmailutils/getpass.c
@@ -15,35 +15,45 @@
    You should have received a copy of the GNU Lesser General Public License
    along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
-/* This file provides replacement definitions for mu_record_t defined
-   in those MU libraries which are disabled at configure time. */
 #ifdef HAVE_CONFIG_H
 # include <config.h>
 #endif
 
 #include <stdlib.h>
-#include <mailutils/types.h>
-#include <mailutils/registrar.h>
-
-#ifndef ENABLE_IMAP
-mu_record_t mu_imap_record = NULL;
-mu_record_t mu_imaps_record = NULL;
-#endif
+#include <string.h>
 
-#ifndef ENABLE_POP
-mu_record_t mu_pop_record = NULL;
-mu_record_t mu_pops_record = NULL;
-#endif
-
-#ifndef ENABLE_NNTP
-mu_record_t mu_nntp_record = NULL;
-#endif
-
-#ifndef ENABLE_MH
-mu_record_t mu_mh_record = NULL;
-#endif
-
-#ifndef ENABLE_MAILDIR
-mu_record_t mu_maildir_record = NULL;
-#endif
+#include <mailutils/types.h>
+#include <mailutils/stream.h>
+#include <mailutils/mutil.h>
+#include <mailutils/cstr.h>
+
+int
+mu_getpass (mu_stream_t in, mu_stream_t out, const char *prompt,
+	    char **passptr)
+{
+  int status;
+  int echo_state = 0;
+  size_t size = 0;
+  char *buf = NULL;
+
+  status = mu_stream_write (out, prompt, strlen (prompt), NULL);
+  if (status)
+    return status;
+  mu_stream_flush (out);
+  status = mu_stream_ioctl (in, MU_IOCTL_SET_ECHO, &echo_state);
+  if (status == 0)
+    echo_state = 1;
+  status = mu_stream_getline (in, &buf, &size, NULL);
+  if (echo_state)
+    {
+      mu_stream_ioctl (in, MU_IOCTL_SET_ECHO, &echo_state);
+      mu_stream_write (out, "\n", 1, NULL);
+    }
+  if (status == 0)
+    {
+      mu_rtrim_cset (buf, "\n");
+      *passptr = buf;
+    }
+  return 0;
+}
 
diff --git a/libmailutils/strskip.c b/libmailutils/strskip.c
index f24d119..5b676d0 100644
--- a/libmailutils/strskip.c
+++ b/libmailutils/strskip.c
@@ -41,6 +41,14 @@ mu_str_skip_class_comp (const char *str, int class)
 }
 
 char *
+mu_str_skip_cset (const char *str, const char *cset)
+{
+  for (; *str && strchr (cset, *str); str++)
+    ;
+  return (char*) str;
+}
+
+char *
 mu_str_skip_cset_comp (const char *str, const char *cset)
 {
   for (; *str && strchr (cset, *str) == NULL; str++)
@@ -48,3 +56,4 @@ mu_str_skip_cset_comp (const char *str, const char *cset)
   return (char*) str;
 }
 
+
diff --git a/libproto/pop/pop3_carrier.c b/libproto/pop/pop3_carrier.c
index 7331756..485790f 100644
--- a/libproto/pop/pop3_carrier.c
+++ b/libproto/pop/pop3_carrier.c
@@ -42,6 +42,7 @@ mu_pop3_set_carrier (mu_pop3_t pop3, mu_stream_t carrier)
   pop3->carrier = carrier;
   if (MU_POP3_FISSET (pop3, MU_POP3_TRACE))
     _mu_pop3_trace_enable (pop3);
+  pop3->state = MU_POP3_CONNECT;
   return 0;
 }
 
diff --git a/libproto/pop/pop3_disconnect.c b/libproto/pop/pop3_disconnect.c
index 621a566..1c3d231 100644
--- a/libproto/pop/pop3_disconnect.c
+++ b/libproto/pop/pop3_disconnect.c
@@ -49,5 +49,7 @@ mu_pop3_disconnect (mu_pop3_t pop3)
     }
 
   /* Close the stream.  */
-  return mu_stream_close (pop3->carrier);
+  if (mu_stream_is_open (pop3->carrier))
+    return mu_stream_close (pop3->carrier);
+  return 0;
 }
diff --git a/mu/mu.h b/mu/mu.h
index a251d7d..7ab5b70 100644
--- a/mu/mu.h
+++ b/mu/mu.h
@@ -24,7 +24,8 @@ struct mutool_command
   int argmax;           /* Max. allowed number of arguments (-1 means not
 			   limited */
   mutool_action_t func;	/* Function to call to do the job. */
-  const char *doc;	/* Documentation for this function.  */
+  const char *argdoc;   /* Documentation for the arguments */
+  const char *docstring;/* Documentation for this function.  */
 };
 
 
@@ -37,5 +38,7 @@ int mutool_acl (int argc, char **argv);
 
 extern char *mutool_shell_prompt;
 extern mu_vartab_t mutool_prompt_vartab;
-
+extern int mutool_shell_interactive;
+extern mu_stream_t mustrin, mustrout;
 int mutool_shell (const char *name, struct mutool_command *cmd);
+mu_stream_t mutool_open_pager (void);
diff --git a/mu/pop.c b/mu/pop.c
index e4b9a25..86d3e8a 100644
--- a/mu/pop.c
+++ b/mu/pop.c
@@ -244,24 +244,24 @@ com_verbose (int argc, char **argv)
     {
       if (QRY_VERBOSE ())
 	{
-	  printf ("verbose is on");
+	  mu_stream_printf (mustrout, "verbose is on");
 	  if (HAS_VERBOSE_MASK ())
 	    {
 	      char *delim = " (";
 	    
 	      if (QRY_VERBOSE_MASK (MU_XSCRIPT_SECURE))
 		{
-		  printf("%ssecure", delim);
+		  mu_stream_printf (mustrout, "%ssecure", delim);
 		  delim = ", ";
 		}
 	      if (QRY_VERBOSE_MASK (MU_XSCRIPT_PAYLOAD))
-		printf("%spayload", delim);
-	      printf (")");
+		mu_stream_printf (mustrout, "%spayload", delim);
+	      mu_stream_printf (mustrout, ")");
 	    }
-	  printf ("\n");
+	  mu_stream_printf (mustrout, "\n");
 	}
       else
-	printf ("verbose is off\n");
+	mu_stream_printf (mustrout, "verbose is off\n");
     }
   else
     {
@@ -304,13 +304,25 @@ static int
 com_apop (int argc, char **argv)
 {
   int status;
+  char *pwd, *passbuf = NULL;
 
-  status = mu_pop3_apop (pop3, argv[1], argv[2]);
+  if (argc == 3)
+    pwd = argv[2];
+  else
+    {
+      status = mu_getpass (mustrin, mustrout, "Password:", &passbuf);
+      if (status)
+	return status;
+      pwd = passbuf;
+    }
+  
+  status = mu_pop3_apop (pop3, argv[1], pwd);
   if (status == 0)
     {
       username = strdup (argv[1]);
       pop_session_status = pop_session_logged_in;
     }
+  free (passbuf);
   return status;
 }
 
@@ -346,13 +358,13 @@ com_capa (int argc, char **argv)
 	    {
 	    case 0:
 	      if (*elt)
-		printf ("%s: %s\n", argv[i], elt);
+		mu_stream_printf (mustrout, "%s: %s\n", argv[i], elt);
 	      else
-		printf ("%s is set\n", argv[i]);
+		mu_stream_printf (mustrout, "%s is set\n", argv[i]);
 	      break;
 
 	    case MU_ERR_NOENT:
-	      printf ("%s is not set\n", argv[i]);
+	      mu_stream_printf (mustrout, "%s is not set\n", argv[i]);
 	      break;
 
 	    default:
@@ -371,7 +383,7 @@ com_capa (int argc, char **argv)
 	    {
 	      char *capa = NULL;
 	      mu_iterator_current (iterator, (void **) &capa);
-	      printf ("CAPA: %s\n", capa ? capa : "");
+	      mu_stream_printf (mustrout, "CAPA: %s\n", capa ? capa : "");
 	    }
 	  mu_iterator_destroy (&iterator);
 	}
@@ -385,6 +397,7 @@ com_uidl (int argc, char **argv)
   int status = 0;
   if (argc == 1)
     {
+      mu_stream_t out = mutool_open_pager ();
       mu_iterator_t uidl_iterator = NULL;
       status = mu_pop3_uidl_all (pop3, &uidl_iterator);
       if (status == 0)
@@ -395,10 +408,11 @@ com_uidl (int argc, char **argv)
 	    {
 	      char *uidl = NULL;
 	      mu_iterator_current (uidl_iterator, (void **) &uidl);
-	      printf ("UIDL: %s\n", uidl ? uidl : "");
+	      mu_stream_printf (out, "UIDL: %s\n", uidl ? uidl : "");
 	    }
 	  mu_iterator_destroy (&uidl_iterator);
 	}
+      mu_stream_destroy (&out);
     }
   else
     {
@@ -406,7 +420,7 @@ com_uidl (int argc, char **argv)
       unsigned int msgno = strtoul (argv[1], NULL, 10);
       status = mu_pop3_uidl (pop3, msgno, &uidl);
       if (status == 0)
-	printf ("Msg: %d UIDL: %s\n", msgno, uidl ? uidl : "");
+	mu_stream_printf (mustrout, "Msg: %d UIDL: %s\n", msgno, uidl ? uidl : "");
       free (uidl);
     }
   return status;
@@ -418,6 +432,7 @@ com_list (int argc, char **argv)
   int status = 0;
   if (argc == 1)
     {
+      mu_stream_t out = mutool_open_pager ();
       mu_iterator_t list_iterator;
       status = mu_pop3_list_all (pop3, &list_iterator);
       if (status == 0)
@@ -428,9 +443,10 @@ com_list (int argc, char **argv)
 	    {
 	      char *list = NULL;
 	      mu_iterator_current (list_iterator, (void **) &list);
-	      printf ("LIST: %s\n", (list) ? list : "");
+	      mu_stream_printf (out, "LIST: %s\n", (list) ? list : "");
 	    }
 	  mu_iterator_destroy (&list_iterator);
+	  mu_stream_destroy (&out);
 	}
     }
   else
@@ -439,7 +455,7 @@ com_list (int argc, char **argv)
       unsigned int msgno = strtoul (argv[1], NULL, 10);
       status = mu_pop3_list (pop3, msgno, &size);
       if (status == 0)
-	printf ("Msg: %u Size: %lu\n", msgno, (unsigned long) size);
+	mu_stream_printf (mustrout, "Msg: %u Size: %lu\n", msgno, (unsigned long) size);
     }
   return status;
 }
@@ -450,42 +466,18 @@ com_noop (int argc MU_ARG_UNUSED, char **argv MU_ARG_UNUSED)
   return mu_pop3_noop (pop3);
 }
 
-static void
-echo_off (struct termios *stored_settings)
-{
-  struct termios new_settings;
-  tcgetattr (0, stored_settings);
-  new_settings = *stored_settings;
-  new_settings.c_lflag &= (~ECHO);
-  tcsetattr (0, TCSANOW, &new_settings);
-}
-
-static void
-echo_on (struct termios *stored_settings)
-{
-  tcsetattr (0, TCSANOW, stored_settings);
-}
-
 static int
 com_pass (int argc, char **argv)
 {
   int status;
-  char pass[256];
-  char *pwd;
+  char *pwd, *passbuf = NULL;
   
   if (argc == 1)
     {
-      struct termios stored_settings;
-
-      printf ("passwd:");
-      fflush (stdout);
-      echo_off (&stored_settings);
-      fgets (pass, sizeof pass, stdin);
-      echo_on (&stored_settings);
-      putchar ('\n');
-      fflush (stdout);
-      pass[strlen (pass) - 1] = '\0';	/* nuke the trailing line.  */
-      pwd = pass;
+      status = mu_getpass (mustrin, mustrout, "Password:", &passbuf);
+      if (status)
+	return status;
+      pwd = passbuf;
     }
   else
     pwd = argv[1];
@@ -495,6 +487,7 @@ com_pass (int argc, char **argv)
       pop_session_status = pop_session_logged_in;
       pop_prompt_vartab ();
     }
+  free (passbuf);
   return status;
 }
 
@@ -506,8 +499,8 @@ com_stat (int argc MU_ARG_UNUSED, char **argv MU_ARG_UNUSED)
   int status = 0;
 
   status = mu_pop3_stat (pop3, &count, &size);
-  printf ("Mesgs: %lu Size %lu\n",
-	  (unsigned long) count, (unsigned long) size);
+  mu_stream_printf (mustrout, "Mesgs: %lu Size %lu\n",
+		    (unsigned long) count, (unsigned long) size);
   return status;
 }
 
@@ -549,10 +542,9 @@ com_top (int argc, char **argv)
 
   if (status == 0)
     {
-      size_t n = 0;
-      char buf[128];
-      while ((mu_stream_readline (stream, buf, sizeof buf, &n) == 0) && n)
-	printf ("%s", buf);
+      mu_stream_t out = mutool_open_pager ();
+      mu_stream_copy (out, stream, 0, NULL);
+      mu_stream_destroy (&out);
       mu_stream_destroy (&stream);
     }
   return status;
@@ -570,10 +562,9 @@ com_retr (int argc, char **argv)
 
   if (status == 0)
     {
-      size_t n = 0;
-      char buf[128];
-      while ((mu_stream_readline (stream, buf, sizeof buf, &n) == 0) && n)
-	printf ("%s", buf);
+      mu_stream_t out = mutool_open_pager ();
+      mu_stream_copy (out, stream, 0, NULL);
+      mu_stream_destroy (&out);
       mu_stream_destroy (&stream);
     }
   return status;
@@ -706,49 +697,70 @@ com_quit (int argc MU_ARG_UNUSED, char **argv MU_ARG_UNUSED)
 	}
       else
 	{
-	  printf ("Try 'exit' to leave %s\n", mu_program_name);
+	  mu_stream_printf (mustrout, "Try 'exit' to leave %s\n", mu_program_name);
 	}
     }
   else
-    printf ("Try 'exit' to leave %s\n", mu_program_name);
+    mu_stream_printf (mustrout, "Try 'exit' to leave %s\n", mu_program_name);
   return status;
 }
 
 struct mutool_command pop_comtab[] = {
-  { "apop",       3,  3, com_apop,
-    "Authenticate with APOP: APOP user secret" },
+  { "apop",       2,  3, com_apop,
+    N_("USER [PASS]"),
+    N_("authenticate with APOP") },
   { "capa",       1, -1, com_capa,
-    "List capabilities: capa [-reread] [names...]" },
+    /* TRANSLATORS: -reread is a keyword; do not translate. */
+    N_("[-reread] [NAME...]"),
+    N_("list server capabilities") },
   { "disconnect", 1, 1,
-    com_disconnect, "Close connection: disconnect" },
+    com_disconnect,
+    NULL,
+    N_("close connection") },
   { "dele",       2, 2, com_dele,
-    "Mark message: DELE msgno" },
+    N_("NUMBER"),
+    N_("mark message for deletion") },
   { "list",       1, 2, com_list,
-    "List messages: LIST [msgno]" },
+    N_("[NUMBER]"),
+    N_("list messages") },
   { "noop",       1, 1, com_noop,
-    "Send no operation: NOOP" },
+    NULL,
+    N_("send a \"no operation\"") },
   { "pass",       1, 2, com_pass,
-    "Send passwd: PASS [passwd]" },
+    N_("[PASSWORD]"),
+    N_("send password") },
   { "connect",    1, 4, com_connect,
-    "Open connection: connect [-tls] hostname port" },
+    /* TRANSLATORS: --tls is a keyword. */
+    N_("[-tls] HOSTNAME [PORT]"),
+    N_("open connection") },
   { "quit",       1, 1, com_quit,
-    "Go to Update state : QUIT" },
+    NULL,
+    N_("quit pop3 session") },
   { "retr",       2, 2, com_retr,
-    "Dowload message: RETR msgno" },
+    "NUMBER",
+    N_("retrieve a message") },
   { "rset",       1, 1, com_rset,
-    "Unmark all messages: RSET" },
+    NULL,
+    N_("remove deletion marks") },
   { "stat",       1, 1, com_stat,
-    "Get the size and count of mailbox : STAT" },
+    NULL,
+    N_("get the mailbox size and number of messages in it") },
   { "stls",       1, 1, com_stls,
-    "Start TLS negotiation" },
+    NULL,
+    N_("start TLS negotiation") },
   { "top",        2, 3, com_top,
-    "Get the header of message: TOP msgno [lines]" },
+    "MSGNO [NUMBER]",
+    N_("display message headers and first NUMBER (default 5) lines of"
+       " its body") },
   { "uidl",       1, 2, com_uidl,
-    "Get the unique id of message: UIDL [msgno]" },
+    N_("[NUMBER]"),
+    N_("show unique message identifiers") },
   { "user",       2, 2, com_user,
-    "send login: USER user" },
+    N_("NAME"),
+    N_("send login") },
   { "verbose",    1, 4, com_verbose,
-    "Enable Protocol tracing: verbose [on|off|mask|unmask] [x1 [x2]]" },
+    "[on|off|mask|unmask] [secret [payload]]",
+    N_("control the protocol tracing") },
   { NULL }
 };
 
diff --git a/mu/shell.c b/mu/shell.c
index 6a04fb3..e102eca 100644
--- a/mu/shell.c
+++ b/mu/shell.c
@@ -32,6 +32,8 @@
 
 char *mutool_shell_prompt;
 mu_vartab_t mutool_prompt_vartab;
+int mutool_shell_interactive;
+mu_stream_t mustrin, mustrout;
 
 static char *
 expand_prompt ()
@@ -53,37 +55,105 @@ static int shell_history (int, char **);
 #endif
 
 struct mutool_command default_comtab[] = {
-  { "prompt",   -1, -1, shell_prompt,  "set command prompt" },
-  { "exit",       1, 1, shell_exit,    "exit program" },
-  { "help",       1, 2, shell_help,    "display this text" },
-  { "?",          1, 1, shell_help,    "synonym for `help'" },
+  { "prompt",   -1, -1, shell_prompt,
+    N_("STRING"),
+    N_("set command prompt") },
+  { "exit",       1, 1, shell_exit,    NULL,        N_("exit program") },
+  { "help",       1, 2, shell_help,
+    N_("[COMMAND]"),
+    N_("display this text") },
+  { "?",          1, 1, shell_help,
+    N_("[COMMAND]"),
+    N_("synonym for `help'") },
 #ifdef WITH_READLINE
-  { "history",    1, 1, shell_history, "show command history" },
+  { "history",    1, 1, shell_history,
+    NULL,
+    N_("show command history") },
 #endif
   { NULL }
 };
 
+#define DESCRCOL 25
+#define NUMCOLS  80
+#define DESCRWIDTH (NUMCOLS - DESCRCOL)
+
 /* Print a single comtab entry */
 static void
-print_comtab (struct mutool_command *tab)
+print_comtab (mu_stream_t stream, struct mutool_command *tab)
 {
-  printf ("%s\t\t%s\n", tab->name, tab->doc);
+  size_t size = 0;
+  const char *text;
+  
+  if (tab->docstring == NULL)
+    return;
+
+  mu_stream_printf (stream, "%s ", tab->name);
+  size += strlen (tab->name) + 1;
+  if (tab->argdoc)
+    {
+      text = gettext (tab->argdoc);
+      mu_stream_printf (stream, "%s", text);
+      size += strlen (text);
+    }
+  if (size >= DESCRCOL)
+    mu_stream_printf (stream, "\n%-*s", DESCRCOL, "");
+  else
+    mu_stream_printf (stream, "%-*s", (int) (DESCRCOL - size), "");
+
+  text = gettext (tab->docstring);
+  size = strlen (text);
+  
+  while (*text)
+    {
+      size_t len = size;
+      if (len > DESCRWIDTH)
+	{
+	  /* Fold the text */
+	  size_t n = 0;
+	  
+	  while (n < len)
+	    {
+	      size_t delta;
+	      char *p;
+	      
+	      p = mu_str_skip_cset_comp (text + n, " \t");
+	      delta = p - (text + n);
+	      if (n + delta > DESCRWIDTH)
+		break;
+	      n += delta;
+	      
+	      p = mu_str_skip_cset (text + n, " \t");
+	      delta = p - (text + n);
+	      if (n + delta > DESCRWIDTH)
+		break;
+	      n += delta;
+	    }
+
+	  len = n;
+	}
+      mu_stream_write (stream, text, len, NULL);
+      text += len;
+      size -= len;
+      mu_stream_write (stream, "\n", 1, NULL);
+      if (size)
+	mu_stream_printf (stream, "%-*s", DESCRCOL, "");
+    }
 }  
 
 /* Print a single comtab entry.
    FIXME: Way too primitive. Rewrite. */
 int
-print_help (struct mutool_command *tab, size_t n)
+print_help (mu_stream_t stream, struct mutool_command *tab, size_t n)
 {
   if (n)
     {
       for (; n > 0 && tab->name; tab++, n--)
-	print_comtab (tab);
+	print_comtab (stream, tab);
     }
   else
     {
       for (; tab->name; tab++)
-	print_comtab (tab);
+	print_comtab (stream, tab);
     }
   return 0;
 }
@@ -102,16 +172,16 @@ list_commands (struct mutool_command *tab, const char *name)
       if (printed == 6)
 	{
 	  printed = 0;
-	  printf ("\n");
+	  mu_stream_printf (mustrout, "\n");
 	}
       if (mu_c_strncasecmp (tab->name, name, namelen) == 0)
 	{
-	  printf ("%s\t", tab->name);
+	  mu_stream_printf (mustrout, "%s\t", tab->name);
 	  printed++;
 	}
     }
   if (printed && printed < 6)
-    printf ("\n");
+    mu_stream_printf (mustrout, "\n");
 }
 
 static struct mutool_command *
@@ -139,23 +209,28 @@ int
 shell_help (int argc, char **argv)
 {
   char *name = argv[1];
+  
   if (name)
     {
       struct mutool_command *com = find_command (argv[1]);
 
       if (com)
-	print_comtab (com);
+	print_comtab (mustrout, com);
       else
 	{
-	  printf ("No commands match `%s'.  Possibilties are:\n", name);
+	  mu_stream_printf (mustrout,
+			    "No commands match `%s'.  Possibilties are:\n",
+			    name);
 	  list_commands (shell_comtab, name);
 	}
     }
   else
     {
-      print_help (shell_comtab, user_command_count);
-      printf ("\n");
-      print_help (shell_comtab + user_command_count, 0);
+      mu_stream_t str = mutool_open_pager ();
+      print_help (str, shell_comtab, user_command_count);
+      mu_stream_printf (str, "\n");
+      print_help (str, shell_comtab + user_command_count, 0);
+      mu_stream_destroy (&str);
     }
   return 0;
 }
@@ -173,6 +248,21 @@ shell_prompt (int argc, char **argv)
   return 0;
 }
 
+mu_stream_t
+mutool_open_pager ()
+{
+  char *pager;
+  if (mutool_shell_interactive && (pager = getenv ("PAGER")) != NULL)
+    {
+      mu_stream_t stream;
+      int rc = mu_prog_stream_create (&stream, pager, MU_STREAM_WRITE);
+      if (rc == 0)
+	return stream;
+      mu_error (_("cannot start pager: %s"), mu_strerror (rc));
+    }
+  mu_stream_ref (mustrout);
+  return mustrout;
+}
 
 
 #ifdef WITH_READLINE
@@ -313,7 +403,7 @@ retrieve_history (char *str)
       return 1;
 
     case 2:
-      printf ("%s\n", out);
+      mu_stream_printf (mustrout, "%s\n", out);
       free (out);
       return 1;
     }
@@ -325,11 +415,13 @@ shell_history (int argc, char **argv)
 {
   int i;
   HIST_ENTRY **hlist;
-
+  mu_stream_t out = mutool_open_pager ();
+  
   hlist = history_list ();
   for (i = 0; i < history_length; i++)
-    printf ("%4d) %s\n", i + 1, hlist[i]->line);
+    mu_stream_printf (out, "%4d) %s\n", i + 1, hlist[i]->line);
 
+  mu_stream_destroy (&out);
   return 0;
 }
 
@@ -345,11 +437,16 @@ readline (char *prompt)
 
   if (prompt)
     {
-      printf ("%s", prompt);
+      mu_stream_printf (mustrout, "%s", prompt);
       fflush (stdout);
     }
-  if (getline (&buf, &size, stdin) <= 0)
-    return NULL;
+  if (mu_stream_getline (mustrin, &buf, &size, &n) || n == 0)
+    {
+      free (buf);
+      buf = NULL;
+      size = 0;
+      return NULL;
+    }
   return buf;
 }
 
@@ -440,9 +537,30 @@ shell_exit (int argc MU_ARG_UNUSED, char **argv MU_ARG_UNUSED)
 int
 mutool_shell (const char *name, struct mutool_command *cmd)
 {
+  int rc;
   size_t n;
-  int interactive = isatty (0);
-  char *(*input_line) () = interactive ?
+  char *(*input_line) ();
+
+  rc = mu_stdio_stream_create (&mustrin, MU_STDIN_FD,
+			       MU_STREAM_READ);
+  if (rc)
+    {
+      mu_diag_funcall (MU_DIAG_ERROR, "mu_stdio_stream_create",
+		       "MU_STDIN_FD", rc);
+      return 1;
+    } 
+ 
+  rc = mu_stdio_stream_create (&mustrout, MU_STDOUT_FD,
+			       MU_STREAM_WRITE);
+  if (rc)
+    {
+      mu_diag_funcall (MU_DIAG_ERROR, "mu_stdio_stream_create",
+		       "MU_STDOUT_FD", rc);
+      return 1;
+    } 
+ 
+  mutool_shell_interactive = isatty (0);
+  input_line = mutool_shell_interactive ?
                              input_line_interactive : input_line_script;
 
   for (n = 0; cmd[n].name; n++)
@@ -471,7 +589,7 @@ mutool_shell (const char *name, struct mutool_command *cmd)
 	  int status;
 
 #ifdef WITH_READLINE
-	  if (interactive)
+	  if (mutool_shell_interactive)
 	    {
 	      if (retrieve_history (s))
 		continue;
@@ -486,7 +604,9 @@ mutool_shell (const char *name, struct mutool_command *cmd)
 
       free (line);
     }
-  if (interactive)
+  if (mutool_shell_interactive)
     finish_readline ();
+  mu_stream_destroy (&mustrin);
+  mu_stream_destroy (&mustrout);
   return 0;
 }
diff --git a/pop3d/popauth.c b/pop3d/popauth.c
index 9f73d60..f8505f9 100644
--- a/pop3d/popauth.c
+++ b/pop3d/popauth.c
@@ -74,7 +74,7 @@ static struct argp_option options[] =
   { NULL, 0, NULL, 0,
     N_("Default action is:\n"
     "  For the file owner: --list\n"
-    "  For a user: --modify --username <username>\n"), 2 },
+    "  For a user: --modify --user <username>\n"), 2 },
   
   { NULL, 0, NULL, 0, N_("Options are:"), 3 },
   { "file", 'f', N_("FILE"), 0, N_("read input from FILE (default stdin)"), 3 },
@@ -105,7 +105,7 @@ set_db_perms (struct argp_state *astate, char *opt, int *pperm)
 {
   int perm = 0;
    
-  if (mu_isdigit(opt[0]))
+  if (mu_isdigit (opt[0]))
     {
       char *p;
       perm = strtoul (opt, &p, 8);
@@ -446,21 +446,57 @@ fill_pass (struct action_data *ap)
   if (!ap->passwd)
     {
       char *p;
+      mu_stream_t in, out;
+      int rc;
+      
+      rc = mu_stdio_stream_create (&in, MU_STDIN_FD, MU_STREAM_READ);
+      if (rc)
+	{
+	  mu_diag_funcall (MU_DIAG_ERROR, "mu_stdio_stream_create",
+			   "MU_STDIN_FD", rc);
+	  return;
+	}
+ 
+      rc = mu_stdio_stream_create (&out, MU_STDOUT_FD, MU_STREAM_WRITE);
+      if (rc)
+	{
+	  mu_diag_funcall (MU_DIAG_ERROR, "mu_stdio_stream_create",
+			   "MU_STDOUT_FD", rc);
+	  return;
+	}
 
-      while (1) {
-	if (ap->passwd)
-	  free (ap->passwd);
-	p = getpass (_("Password:"));
-	if (!p)
-	  exit (EX_DATAERR);
-	ap->passwd = strdup (p);
-	/* TRANSLATORS: Please try to format this string so that it has
-	   the same length as the translation of 'Password:' above */
-	p = getpass (_("Confirm :"));
-	if (strcmp (ap->passwd, p) == 0)
-	  break;
-	mu_error (_("Passwords differ. Please retry."));
-      } 
+      while (1)
+	{
+	  if (ap->passwd)
+	    free (ap->passwd);
+	  rc = mu_getpass (in, out, _("Password:"), &p);
+	  if (rc)
+	    {
+	      mu_diag_funcall (MU_DIAG_ERROR, "mu_getpass", NULL, rc);
+	      exit (EX_DATAERR);
+	    }
+	  
+	  if (!p)
+	    exit (EX_DATAERR);
+	  
+	  ap->passwd = strdup (p);
+	  /* TRANSLATORS: Please try to format this string so that it has
+	     the same length as the translation of 'Password:' above */
+	  rc = mu_getpass (in, out, _("Confirm :"), &p);
+	  if (rc)
+	    {
+	      mu_diag_funcall (MU_DIAG_ERROR, "mu_getpass", NULL, rc);
+	      exit (EX_DATAERR);
+	    }
+	  
+	  if (!p)
+	    exit (EX_DATAERR);
+	  if (strcmp (ap->passwd, p) == 0)
+	    break;
+	  mu_error (_("Passwords differ. Please retry."));
+	}
+      mu_stream_destroy (&in);
+      mu_stream_destroy (&out);
     }
 }
 


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Tue Oct 05 17:37:26 2010
Received: from mailman by lists.gnu.org with archive (Exim 4.43)
	id 1P3FCP-0006RN-V1
	for mharc-commit-mailutils@gnu.org; Tue, 05 Oct 2010 17:37:26 -0400
Received: from [140.186.70.92] (port=35102 helo=eggs.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.43) id 1P3FCA-00068e-I1
	for commit-mailutils@gnu.org; Tue, 05 Oct 2010 17:37:24 -0400
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P3FC3-0007sC-SI
	for commit-mailutils@gnu.org; Tue, 05 Oct 2010 17:37:05 -0400
Received: from colonialone.fsf.org ([140.186.70.51]:49431
	helo=internal.in.savannah.gnu.org)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P3FC3-0007s8-PD
	for commit-mailutils@gnu.org; Tue, 05 Oct 2010 17:37:03 -0400
Received: from [10.1.0.108] (helo=vcs-noshell.in.savannah.gnu.org)
	by internal.in.savannah.gnu.org with esmtp (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P3FC3-00069E-PF
	for commit-mailutils@gnu.org; Tue, 05 Oct 2010 21:37:03 +0000
Received: from gray by vcs-noshell.in.savannah.gnu.org with local (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P3FC3-0002yQ-LG
	for commit-mailutils@gnu.org; Tue, 05 Oct 2010 21:37:03 +0000
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: c9e2cdac6f0c33f1175f19ca9095bc26f5a099ac
X-Git-Newrev: b8f5fd61d0f6ae3f3106b7f9bdb395a8ef24967d
Message-Id: <E1P3FC3-0002yQ-LG@vcs-noshell.in.savannah.gnu.org>
Date: Tue, 05 Oct 2010 21:37:03 +0000
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
Subject: [SCM] GNU Mailutils branch, master,
	updated. release-2.2-126-gb8f5fd6
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.5
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=unsubscribe>
List-Archive: <http://lists.gnu.org/archive/html/commit-mailutils>
List-Post: <mailto:commit-mailutils@gnu.org>
List-Help: <mailto:commit-mailutils-request@gnu.org?subject=help>
List-Subscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Tue, 05 Oct 2010 21:37:24 -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=b8f5fd61d0f6ae3f3106b7f9bdb395a8ef24967d

The branch, master has been updated
       via  b8f5fd61d0f6ae3f3106b7f9bdb395a8ef24967d (commit)
      from  c9e2cdac6f0c33f1175f19ca9095bc26f5a099ac (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 b8f5fd61d0f6ae3f3106b7f9bdb395a8ef24967d
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Wed Oct 6 00:11:23 2010 +0300

    Minor changes.
    
    * mu/shell.c (input_line_script): Use mu_getline.
    * libmu_argp/compat.c: Remove.
    * libmu_argp/Makefile.am: Remove compat.c
    * po/POTFILES.in: Likewise.

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

Summary of changes:
 libmu_argp/Makefile.am |    1 -
 libmu_argp/compat.c    |  339 ------------------------------------------------
 mu/shell.c             |    5 +-
 po/POTFILES.in         |    1 -
 4 files changed, 3 insertions(+), 343 deletions(-)
 delete mode 100644 libmu_argp/compat.c

diff --git a/libmu_argp/Makefile.am b/libmu_argp/Makefile.am
index 87fd420..93c26d2 100644
--- a/libmu_argp/Makefile.am
+++ b/libmu_argp/Makefile.am
@@ -31,7 +31,6 @@ libmu_argp_a_SOURCES =\
  cmdline.c\
  cmdline.h\
  common.c\
- compat.c\
  mu_argp.c\
  muinit.c\
  sieve.c\
diff --git a/libmu_argp/compat.c b/libmu_argp/compat.c
deleted file mode 100644
index 09c9e2d..0000000
--- a/libmu_argp/compat.c
+++ /dev/null
@@ -1,339 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 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 <sys/types.h>
-#include <sys/stat.h>
-#include <string.h>
-#include "mailutils/cctype.h"
-#include "mailutils/libargp.h"
-#include "mailutils/argcv.h"
-#include "mailutils/mutil.h"
-#ifdef WITH_TLS
-# include "mailutils/tls.h"
-#endif
-#ifdef WITH_GSASL
-# include "mailutils/gsasl.h"
-#endif
-#include "mailutils/sieve.h"
-
-#ifndef MU_COMPAT_CONFIG_FILE
-# define MU_COMPAT_CONFIG_FILE SYSCONFDIR "/mailutils.rc"
-#endif
-
-#ifndef MU_COMPAT_USER_CONFIG_FILE
-# define MU_COMPAT_USER_CONFIG_FILE "~/.mailutils"
-#endif
-
-static int
-member (const char *array[], const char *text, size_t len)
-{
-  int i;
-  for (i = 0; array[i]; i++)
-    if (strncmp (array[i], text, len) == 0)
-      return 1;
-  return 0;
-}
-
-/* Appends applicable options found in file NAME to argv. If progname
-   is NULL, all the options found are assumed to apply. Otherwise they
-   apply only if the line starts with ":something", and something is
-   found in the CAPA array, or the line starts with PROGNAME.
-*/
-void
-read_rc (const char *progname, const char *name, const char *capa[],
-	 int *argc, char ***argv)
-{
-  FILE *fp;
-  char *linebuf = NULL;
-  char *buf = NULL;
-  size_t n = 0;
-  int x_argc = *argc;
-  char **x_argv = *argv;
-  char* rcfile = mu_tilde_expansion (name, "/", NULL);
-
-  if (!rcfile)
-    return;
-  
-  fp = fopen (rcfile, "r");
-  if (!fp)
-    {
-      free(rcfile);
-      return;
-    }
-  
-  while (getline (&buf, &n, fp) > 0)
-    {
-      char *kwp, *p;
-      int len;
-      
-      for (kwp = buf; *kwp && mu_isspace (*kwp); kwp++)
-	;
-
-      if (*kwp == '#' || *kwp == 0)
-	continue;
-
-      len = strlen (kwp);
-      if (kwp[len-1] == '\n')
-	kwp[--len] = 0;
-
-      if (kwp[len-1] == '\\' || linebuf)
-	{
-	  int cont;
-	  
-	  if (kwp[len-1] == '\\')
-	    {
-	      kwp[--len] = 0;
-	      cont = 1;
-	    }
-	  else
-	    cont = 0;
-	  
-	  if (!linebuf)
-	    linebuf = calloc (len + 1, 1);
-	  else
-	    linebuf = realloc (linebuf, strlen (linebuf) + len + 1);
-	  
-	  if (!linebuf)
-	    {
-	      fprintf (stderr, _("%s: not enough memory\n"), progname);
-	      exit (1);
-	    }
-	  
-	  strcpy (linebuf + strlen (linebuf), kwp);
-	  if (cont)
-	    continue;
-	  kwp = linebuf;
-	}
-
-      len = 0;
-      if (progname)
-	{
-	  for (p = kwp; *p && !mu_isspace (*p); p++)
-	    len++;
-	}
-      else
-	p = kwp; /* Use the whole line. */
-
-      if (progname == NULL
-	  || (kwp[0] == ':' && member (capa, kwp+1, len-1))
-	  || strncmp (progname, kwp, len) == 0
-	  )
-	{
-	  int i, n_argc = 0;
-	  char **n_argv;
-              
-	  if (mu_argcv_get (p, "", NULL, &n_argc, &n_argv))
-	    {
-	      mu_argcv_free (n_argc, n_argv);
-	      if (linebuf)
-		free (linebuf);
-	      linebuf = NULL;
-	      continue;
-	    }
-	  x_argv = realloc (x_argv,
-			    (x_argc + n_argc) * sizeof (x_argv[0]));
-	  if (!x_argv)
-	    {
-	      fprintf (stderr, _("%s: not enough memory\n"), progname);
-	      exit (1);
-	    }
-	  
-	  for (i = 0; i < n_argc; i++)
-	    x_argv[x_argc++] = mu_tilde_expansion (n_argv[i], "/", NULL);
-	  
-	  free (n_argv);
-	}
-      if (linebuf)
-	free (linebuf);
-      linebuf = NULL;
-    }
-  fclose (fp);
-  free(rcfile);
-
-  *argc = x_argc;
-  *argv = x_argv;
-}
-
-
-void
-mu_create_argcv (const char *capa[],
-		 int argc, char **argv, int *p_argc, char ***p_argv)
-{
-  char *progname;
-  int x_argc;
-  char **x_argv;
-  int i;
-  int rcdir = 0;
-
-  progname = strrchr (argv[0], '/');
-  if (progname)
-    progname++;
-  else
-    progname = argv[0];
-
-  x_argv = malloc (sizeof (x_argv[0]));
-  if (!x_argv)
-    {
-      fprintf (stderr, _("%s: not enough memory\n"), progname);
-      exit (1);
-    }
-
-  /* Add command name */
-  x_argc = 0;
-  x_argv[x_argc] = argv[x_argc];
-  x_argc++;
-
-  /* Add global config file. */
-  read_rc (progname, MU_COMPAT_CONFIG_FILE, capa, &x_argc, &x_argv);
-
-  /* Look for per-user config files in ~/.mailutils/ or in ~/, but
-     not both. This allows mailutils' utilities to have their config
-     files segregated, if necessary. */
-
-  {
-    struct stat s;
-    char *rcdirname = mu_tilde_expansion (MU_COMPAT_USER_CONFIG_FILE, "/", NULL);
-
-    if (!rcdirname
-	|| (stat(rcdirname, &s) == 0 && S_ISDIR(s.st_mode)))
-      rcdir = 1;
-
-    free(rcdirname);
-  }
-
-  /* Add per-user config file. */
-  if (!rcdir)
-    {
-      read_rc (progname, MU_COMPAT_USER_CONFIG_FILE, capa, &x_argc, &x_argv);
-    }
-  else
-    {
-      char *userrc = NULL;
-
-      userrc = malloc (sizeof (MU_COMPAT_USER_CONFIG_FILE)
-		       /* provides an extra slot
-			  for null byte as well */
-		       + 1 /* slash */
-		       + 9 /*mailutils*/); 
-
-      if (!userrc)
-	{
-	  fprintf (stderr, _("%s: not enough memory\n"), progname);
-	  exit (1);
-	}
-      
-      sprintf (userrc, "%s/mailutils", MU_COMPAT_USER_CONFIG_FILE);
-      read_rc (progname, userrc, capa, &x_argc, &x_argv);
-      
-      free (userrc);
-    }
-
-  /* Add per-user, per-program config file. */
-  {
-    char *progrc = NULL;
-    int size;
-    
-    if (rcdir)
-      size = sizeof (MU_COMPAT_USER_CONFIG_FILE)
-	             + 1
-		     + strlen (progname)
-		     + 2 /* rc */;
-    else
-      size = 6 /*~/.mu.*/
-	     + strlen (progname)
-	     + 3 /* "rc" + null terminator */;
-
-    progrc = malloc (size);
-
-    if (!progrc)
-      {
-	fprintf (stderr, _("%s: not enough memory\n"), progname);
-	exit (1);
-      }
-
-    if (rcdir)
-      sprintf (progrc, "%s/%src", MU_COMPAT_USER_CONFIG_FILE, progname);
-    else
-      sprintf (progrc, "~/.mu.%src", progname);
-
-    read_rc (NULL, progrc, capa, &x_argc, &x_argv);
-    free (progrc);
-  }
-
-  /* Finally, add the command line options */
-  x_argv = realloc (x_argv, (x_argc + argc) * sizeof (x_argv[0]));
-  for (i = 1; i < argc; i++)
-    x_argv[x_argc++] = argv[i];
-
-  x_argv[x_argc] = NULL;
-
-  *p_argc = x_argc;
-  *p_argv = x_argv;
-}
-
-error_t
-mu_argp_parse (const struct argp *myargp, 
-	       int *pargc, char **pargv[],  
-	       unsigned flags,
-	       const char *capa[],
-	       int *arg_index,     
-	       void *input)
-{
-  struct argp *argp;
-  error_t rc;
-  const struct argp argpnull = { 0 };
-  int i;
-  
-  /* Make sure we have program version and bug address initialized */
-  mu_argp_init (argp_program_version, argp_program_bug_address);
-
-  mu_set_program_name ((*pargv)[0]);
-  mu_libargp_init ();
-  for (i = 0; capa[i]; i++)
-    {
-#ifdef WITH_TLS
-      if (strcmp (capa[i], "tls") == 0)
-	mu_gocs_register ("tls", mu_tls_module_init);
-      else
-#endif /* WITH_TLS */
-#ifdef WITH_GSASL
-      if (strcmp (capa[i], "gsasl") == 0)
-	mu_gocs_register ("gsasl", mu_gsasl_module_init);
-      else
-#endif
-      if (strcmp (capa[i], "sieve") == 0)
-	mu_gocs_register ("sieve", mu_sieve_module_init);
-      else
-	mu_gocs_register_std (capa[i]); 
-    }
-  
-  if (!myargp)
-    myargp = &argpnull;
-  argp = mu_argp_build (myargp, NULL);
-  rc = argp_parse (argp, *pargc, *pargv, flags, arg_index, input);
-  mu_argp_done (argp);
-  if (rc)
-    return rc;
-
-  mu_gocs_flush ();
-
-  return 0;
-}
diff --git a/mu/shell.c b/mu/shell.c
index e102eca..9497b3d 100644
--- a/mu/shell.c
+++ b/mu/shell.c
@@ -518,9 +518,10 @@ input_line_interactive ()
 static char *
 input_line_script ()
 {
-  size_t size = 0;
+  size_t size = 0, n;
   char *buf = NULL;
-  if (getline (&buf, &size, stdin) <= 0)
+
+  if (mu_stream_getline (mustrin, &buf, &size, &n) || n == 0)
     return NULL;
   return buf;
 }
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 386fe5b..f600fe5 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -42,7 +42,6 @@ lib/strexit.c
 libmu_argp/auth.c
 libmu_argp/cmdline.c
 libmu_argp/common.c
-libmu_argp/compat.c
 libmu_argp/mu_argp.c
 libmu_argp/sieve.c
 libmu_argp/tls.c


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Wed Oct 06 04:41:13 2010
Received: from mailman by lists.gnu.org with archive (Exim 4.43)
	id 1P3PYn-0005Ne-J3
	for mharc-commit-mailutils@gnu.org; Wed, 06 Oct 2010 04:41:13 -0400
Received: from [140.186.70.92] (port=51618 helo=eggs.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.43) id 1P3PYg-0005NO-Jl
	for commit-mailutils@gnu.org; Wed, 06 Oct 2010 04:41:12 -0400
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P3PYc-0004Du-RE
	for commit-mailutils@gnu.org; Wed, 06 Oct 2010 04:41:06 -0400
Received: from colonialone.fsf.org ([140.186.70.51]:45138
	helo=internal.in.savannah.gnu.org)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P3PYc-0004Do-Mp
	for commit-mailutils@gnu.org; Wed, 06 Oct 2010 04:41:02 -0400
Received: from [10.1.0.108] (helo=vcs-noshell.in.savannah.gnu.org)
	by internal.in.savannah.gnu.org with esmtp (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P3PYc-0005nv-Hh
	for commit-mailutils@gnu.org; Wed, 06 Oct 2010 08:41:02 +0000
Received: from gray by vcs-noshell.in.savannah.gnu.org with local (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P3PYb-0000wf-SN
	for commit-mailutils@gnu.org; Wed, 06 Oct 2010 08:41:02 +0000
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: b8f5fd61d0f6ae3f3106b7f9bdb395a8ef24967d
X-Git-Newrev: 6b7badca005c9876033125407a3522831d80a820
Message-Id: <E1P3PYb-0000wf-SN@vcs-noshell.in.savannah.gnu.org>
Date: Wed, 06 Oct 2010 08:41:02 +0000
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
Subject: [SCM] GNU Mailutils branch, master,
	updated. release-2.2-127-g6b7badc
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.5
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=unsubscribe>
List-Archive: <http://lists.gnu.org/archive/html/commit-mailutils>
List-Post: <mailto:commit-mailutils@gnu.org>
List-Help: <mailto:commit-mailutils-request@gnu.org?subject=help>
List-Subscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Wed, 06 Oct 2010 08:41:12 -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=6b7badca005c9876033125407a3522831d80a820

The branch, master has been updated
       via  6b7badca005c9876033125407a3522831d80a820 (commit)
      from  b8f5fd61d0f6ae3f3106b7f9bdb395a8ef24967d (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 6b7badca005c9876033125407a3522831d80a820
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Wed Oct 6 11:40:37 2010 +0300

    Improve scripting in mu pop shell.
    
    * mu/pop.c (com_apop, com_pass): Require password in non-interactive
    mode.

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

Summary of changes:
 mu/pop.c |   10 ++++++++++
 1 files changed, 10 insertions(+), 0 deletions(-)

diff --git a/mu/pop.c b/mu/pop.c
index 86d3e8a..70e5708 100644
--- a/mu/pop.c
+++ b/mu/pop.c
@@ -308,6 +308,11 @@ com_apop (int argc, char **argv)
 
   if (argc == 3)
     pwd = argv[2];
+  else if (!mutool_shell_interactive)
+    {
+      mu_error (_("apop: password required"));
+      return 1;
+    }
   else
     {
       status = mu_getpass (mustrin, mustrout, "Password:", &passbuf);
@@ -474,6 +479,11 @@ com_pass (int argc, char **argv)
   
   if (argc == 1)
     {
+      if (!mutool_shell_interactive)
+	{
+	  mu_error (_("pass: password required"));
+	  return 1;
+	}
       status = mu_getpass (mustrin, mustrout, "Password:", &passbuf);
       if (status)
 	return status;


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Thu Oct 07 06:35:05 2010
Received: from mailman by lists.gnu.org with archive (Exim 4.43)
	id 1P3noX-0004fi-4A
	for mharc-commit-mailutils@gnu.org; Thu, 07 Oct 2010 06:35:05 -0400
Received: from [140.186.70.92] (port=38533 helo=eggs.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.43) id 1P3noN-0004b6-QZ
	for commit-mailutils@gnu.org; Thu, 07 Oct 2010 06:35:00 -0400
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P3noK-0006OT-2e
	for commit-mailutils@gnu.org; Thu, 07 Oct 2010 06:34:55 -0400
Received: from colonialone.fsf.org ([140.186.70.51]:48529
	helo=internal.in.savannah.gnu.org)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P3noJ-0006O5-W2
	for commit-mailutils@gnu.org; Thu, 07 Oct 2010 06:34:52 -0400
Received: from [10.1.0.108] (helo=vcs-noshell.in.savannah.gnu.org)
	by internal.in.savannah.gnu.org with esmtp (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P3noJ-0007qh-Hv
	for commit-mailutils@gnu.org; Thu, 07 Oct 2010 10:34:51 +0000
Received: from gray by vcs-noshell.in.savannah.gnu.org with local (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P3noI-0007we-Vg
	for commit-mailutils@gnu.org; Thu, 07 Oct 2010 10:34:51 +0000
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: 6b7badca005c9876033125407a3522831d80a820
X-Git-Newrev: dd8d091ba3cae58fd437c664478de8bcbdf25ac6
Message-Id: <E1P3noI-0007we-Vg@vcs-noshell.in.savannah.gnu.org>
Date: Thu, 07 Oct 2010 10:34:51 +0000
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
Subject: [SCM] GNU Mailutils branch, master,
	updated. release-2.2-129-gdd8d091
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.5
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=unsubscribe>
List-Archive: <http://lists.gnu.org/archive/html/commit-mailutils>
List-Post: <mailto:commit-mailutils@gnu.org>
List-Help: <mailto:commit-mailutils-request@gnu.org?subject=help>
List-Subscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Thu, 07 Oct 2010 10:35:01 -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=dd8d091ba3cae58fd437c664478de8bcbdf25ac6

The branch, master has been updated
       via  dd8d091ba3cae58fd437c664478de8bcbdf25ac6 (commit)
       via  f24df125b4a5f7e29eee39e89127871a38856281 (commit)
      from  6b7badca005c9876033125407a3522831d80a820 (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 dd8d091ba3cae58fd437c664478de8bcbdf25ac6
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Thu Oct 7 13:23:45 2010 +0300

    Implement a filter for quoting ^From.  Use it for appending to UNIX mailboxes.
    
    * libmailutils/fromflt.c: New file.
    * libmailutils/Makefile.am (libmailutils_la_SOURCES): Add fromflt.c
    * libmailutils/tests/testsuite.at: Include fromflt.at.
    * include/mailutils/filter.h (mu_from_filter): New filter type.
    * libmailutils/filter.c (mu_filter_get_list): Register mu_from_filter.
    
    * libmailutils/fromflt.at: New file.
    * libmailutils/tests/Makefile.am (TESTSUITE_AT): Add fromflt.at.
    * libmailutils/tests/base64d.at: Mention `filter' in the keywords.
    * libmailutils/tests/base64e.at: Likewise.
    * libmailutils/tests/fltst.c (main): Fix argc check.
    
    * libproto/mbox/mbox.c (append_message_to_stream): Use "FROM" filter.

commit f24df125b4a5f7e29eee39e89127871a38856281
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Wed Oct 6 23:47:56 2010 +0300

    Fixes in stream subsystem.  Rewrite and optimize maidag lmtp mode using streams.
    
    * libmailutils/file_stream.c (mu_fd_stream_create): Mark stream as
    open, do not call mu_stream_open explicitly.
    * libmailutils/message_stream.c (mu_stream_to_message): Bugfixes,
    wrong owner given to mu_envelope_set_ calls.
    * libmailutils/stream.c (_MU_STR_FLUSH_ALL)
    (_MU_STR_FLUSH_KEEP): New macros for _stream_flush_buffer.
    (_stream_flush_buffer): Change the meaning of the last argument.
    All callers updated.
    (mu_stream_seek): Fix operation with MU_SEEK_END.
    Call _mu_stream_cleareof on success.
    (mu_stream_read): Call _stream_flush_buffer in buffered mode.
    (mu_stream_getdelim, mu_stream_readdelim): Call _stream_flush_buffer.
    * libmailutils/streamcpy.c (mu_stream_copy): Reset size if mu_stream_seek
    fails.
    * libmailutils/temp_file_stream.c (mu_temp_file_stream_create): Set
    full buffering mode by default.
    
    * maidag/mailtmp.c: Remove.
    * maidag/Makefile.am (maidag_SOURCES): Remove mailtmp.c
    * po/POTFILES.in: Likewise.
    * maidag/deliver.c (make_tmp): Rewrite.  Return mu_mailbox_t.
    All callers changed.
    * maidag/lmtp.c (lmtp_transcript): Remove static.
    (lmpt_transcript): New function.
    (lmtp_reply): Use mu_stream_t instead of FILE.
    (xlatnl): Remove.  Superseded by mu_rtrim_cset and family.
    (mtmp, mbox): Remove globals.
    (mesg): New global.
    (cfun_unknown, cfun_mail_from, cfun_rcpt_to)
    (dot_temp_fail, dot_deliver, cfun_rset)
    (cfun_lhlo, cfun_quit, cfun_help): Use mu_stream_t instead of FILE.
    (cfun_data): Rewrite.
    (cfun_dot): Remove.
    (to_fgets): Rewrite using mu_stream_t.
    (lmtp_loop): Change signature. Rewrite using mu_stream_t.
    (lmtp_connection, maidag_lmtp_server): Update accordingly.
    * maidag/maidag.c (maidag_transcript): New global.
    (options, parse_opt): New option --transcript.
    * maidag/maidag.h (maidag_transcript): New extern.
    (mail_tmp_begin, mail_tmp_add_line, mail_tmp_finish)
    (mail_tmp_destroy): Remove.

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

Summary of changes:
 include/mailutils/filter.h      |    3 +-
 libmailutils/Makefile.am        |    1 +
 libmailutils/file_stream.c      |    8 +-
 libmailutils/filter.c           |    1 +
 libmailutils/fromflt.c          |  283 +++++++++++++++++++++++++++
 libmailutils/message_stream.c   |    4 +-
 libmailutils/stream.c           |   60 +++++--
 libmailutils/streamcpy.c        |    1 +
 libmailutils/temp_file_stream.c |    5 +-
 libmailutils/tests/Makefile.am  |    1 +
 libmailutils/tests/base64d.at   |    4 +-
 libmailutils/tests/base64e.at   |    4 +-
 libmailutils/tests/fltst.c      |    2 +-
 libmailutils/tests/fromflt.at   |   96 ++++++++++
 libmailutils/tests/testsuite.at |    1 +
 libproto/mbox/mbox.c            |   17 ++-
 maidag/Makefile.am              |    1 -
 maidag/deliver.c                |  116 ++++++++++--
 maidag/lmtp.c                   |  402 ++++++++++++++++++++++-----------------
 maidag/maidag.c                 |   38 +++--
 maidag/maidag.h                 |    7 +-
 maidag/mailtmp.c                |  187 ------------------
 po/POTFILES.in                  |    1 -
 23 files changed, 810 insertions(+), 433 deletions(-)
 create mode 100644 libmailutils/fromflt.c
 create mode 100644 libmailutils/tests/fromflt.at
 delete mode 100644 maidag/mailtmp.c

diff --git a/include/mailutils/filter.h b/include/mailutils/filter.h
index 120318d..d9c53ca 100644
--- a/include/mailutils/filter.h
+++ b/include/mailutils/filter.h
@@ -103,7 +103,8 @@ extern mu_filter_record_t mu_bit8_filter;
 extern mu_filter_record_t mu_bit7_filter;
 extern mu_filter_record_t mu_rfc_2047_Q_filter;
 extern mu_filter_record_t mu_rfc_2047_B_filter;
-  
+extern mu_filter_record_t mu_from_filter;
+
 enum mu_iconv_fallback_mode
   {
     mu_fallback_none,
diff --git a/libmailutils/Makefile.am b/libmailutils/Makefile.am
index 3f083e0..07a0a4e 100644
--- a/libmailutils/Makefile.am
+++ b/libmailutils/Makefile.am
@@ -83,6 +83,7 @@ libmailutils_la_SOURCES = \
  fltstream.c\
  folder.c\
  freeitem.c\
+ fromflt.c\
  gdebug.c\
  getpass.c\
  gocs.c\
diff --git a/libmailutils/file_stream.c b/libmailutils/file_stream.c
index 578f482..e6a6e67 100644
--- a/libmailutils/file_stream.c
+++ b/libmailutils/file_stream.c
@@ -390,16 +390,12 @@ mu_fd_stream_create (mu_stream_t *pstream, char *filename, int fd, int flags)
   struct _mu_file_stream *fstr;
   int rc = _mu_file_stream_create (&fstr,
 				   sizeof (struct _mu_file_stream),
-				   filename, fd, flags);
+				   filename, fd, flags|_MU_STR_OPEN);
   if (rc == 0)
     {
       mu_stream_t stream = (mu_stream_t) fstr;
       mu_stream_set_buffer (stream, mu_buffer_full, 0);
-      rc = mu_stream_open (stream);
-      if (rc)
-	mu_stream_unref (stream);
-      else
-	*pstream = stream;
+      *pstream = stream;
     }
   return rc;
 }
diff --git a/libmailutils/filter.c b/libmailutils/filter.c
index 8fdc25f..0008ba0 100644
--- a/libmailutils/filter.c
+++ b/libmailutils/filter.c
@@ -78,6 +78,7 @@ mu_filter_get_list (mu_list_t *plist)
       mu_list_append (filter_list, mu_dot_filter);
       mu_list_append (filter_list, mu_rfc_2047_Q_filter);
       mu_list_append (filter_list, mu_rfc_2047_B_filter);
+      mu_list_append (filter_list, mu_from_filter);
       /* FIXME: add the default encodings?  */
     }
   *plist = filter_list;
diff --git a/libmailutils/fromflt.c b/libmailutils/fromflt.c
new file mode 100644
index 0000000..d96d46d
--- /dev/null
+++ b/libmailutils/fromflt.c
@@ -0,0 +1,283 @@
+/* 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 <string.h>
+#include <mailutils/errno.h>
+#include <mailutils/filter.h>
+#include <mailutils/stream.h>
+
+enum from_decode_state
+  {
+    from_decode_init,
+    from_decode_nl,
+    from_decode_char
+  };
+
+#define GT_FROM_MARK_STR ">From "
+#define GT_FROM_MARK_LEN (sizeof (GT_FROM_MARK_STR) - 1)
+
+/* Move min(isize,osize) bytes from iptr to optr, replacing each '>From '
+   at the beginning of line with 'From '. */
+static enum mu_filter_result
+_from_decoder (void *xd,
+	       enum mu_filter_command cmd,
+	       struct mu_filter_io *iobuf)
+{
+  int *pstate = xd;
+  const unsigned char *iptr;
+  size_t isize;
+  char *optr;
+  size_t osize;
+  enum from_decode_state state;
+  size_t i, j;
+
+  switch (cmd)
+    {
+    case mu_filter_init:
+      *pstate = from_decode_init;
+      return mu_filter_ok;
+      
+    case mu_filter_done:
+      return mu_filter_ok;
+      
+    default:
+      state = *pstate;
+      break;
+    }
+  
+  iptr = (const unsigned char *) iobuf->input;
+  isize = iobuf->isize;
+  optr = iobuf->output;
+  osize = iobuf->osize;
+
+  for (i = j = 0; i < isize && j < osize; i++)
+    {
+      unsigned char c = *iptr++;
+      
+      if (c == '\n')
+	state = from_decode_nl;
+      else if (state == from_decode_init || state == from_decode_nl)
+	{
+	  size_t len = isize - i;
+
+	  if (len < GT_FROM_MARK_LEN)
+	    {
+	      if (memcmp (iptr - 1, GT_FROM_MARK_STR, len) == 0)
+		{
+		  if (i == 0)
+		    {
+		      iobuf->isize = GT_FROM_MARK_LEN - len;
+		      return mu_filter_moreinput;
+		    }
+		  break;
+		}
+	      else
+		state = from_decode_char;
+	    }
+	  else if (memcmp (iptr - 1, GT_FROM_MARK_STR, GT_FROM_MARK_LEN) == 0)
+	    {
+	      /* Skip > */
+	      state = from_decode_char;
+	      continue;
+	    }
+	}	  
+      optr[j++] = c;
+    }
+
+  *pstate = state;
+  iobuf->isize = i;
+  iobuf->osize = j;
+  return mu_filter_ok;
+}
+
+#define FROM_MARK_STR "From "
+#define FROM_MARK_LEN (sizeof (FROM_MARK_STR) - 1)
+
+enum from_encode_state
+  {
+    from_encode_init,
+    from_encode_nl,
+    from_encode_char,
+    from_encode_gt,
+    from_encode_f,
+    from_encode_r,
+    from_encode_o,
+    from_encode_m,
+    from_encode_sp
+  };
+
+static int length_to_state_tab[] = {
+  from_encode_gt,
+  from_encode_f,
+  from_encode_r,
+  from_encode_o,
+  from_encode_m,
+  from_encode_sp
+};
+
+static int state_to_length_tab[] = {
+  0, 0, 0,
+  GT_FROM_MARK_LEN,
+  GT_FROM_MARK_LEN-1,
+  GT_FROM_MARK_LEN-2,
+  GT_FROM_MARK_LEN-3,
+  GT_FROM_MARK_LEN-4,
+  GT_FROM_MARK_LEN-5
+};      
+
+/* Move min(isize,osize) bytes from iptr to optr, replacing each 'From '
+   at the beginning of line with '>From '. */
+
+static enum mu_filter_result
+_from_encoder (void *xd,
+	       enum mu_filter_command cmd,
+	       struct mu_filter_io *iobuf)
+{
+  int *pstate = xd;
+  const unsigned char *iptr;
+  size_t isize;
+  char *optr;
+  size_t osize;
+  enum from_encode_state state;
+  size_t i, j;
+  
+  switch (cmd)
+    {
+    case mu_filter_init:
+      *pstate = from_encode_init;
+      return mu_filter_ok;
+      
+    case mu_filter_done:
+      return mu_filter_ok;
+      
+    default:
+      state = *pstate;
+      switch (state)
+	{
+	case from_encode_init:
+	case from_encode_nl:
+	case from_encode_char:
+	  break;
+
+	default:
+	  osize = state_to_length_tab[state];
+	  if (iobuf->osize < osize)
+	    {
+	      iobuf->osize = osize;
+	      return mu_filter_moreoutput;
+	    }
+	  memcpy (iobuf->output, GT_FROM_MARK_STR + GT_FROM_MARK_LEN - osize,
+		  osize);
+	  iobuf->osize = osize;
+	  iobuf->isize = osize;
+	  *pstate = from_encode_init;
+	  return mu_filter_ok;
+	}
+      break;
+    }
+  
+  iptr = (const unsigned char *) iobuf->input;
+  isize = iobuf->isize;
+  optr = iobuf->output;
+  osize = iobuf->osize;
+
+  for (i = j = 0; i < isize && j < osize; i++)
+    {
+      unsigned char c = *iptr++;
+      
+      if (c == '\n')
+	state = from_encode_nl;
+      else if (state == from_encode_init || state == from_encode_nl)
+	{
+	  size_t len = isize - i;
+
+	  if (len < FROM_MARK_LEN)
+	    {
+	      if (memcmp (iptr - 1, FROM_MARK_STR, len) == 0)
+		{
+		  if (i == 0)
+		    {
+		      iobuf->isize = FROM_MARK_LEN;
+		      return mu_filter_moreinput;
+		    }
+		  break;
+		}
+	      else
+		state = from_encode_char;
+	    }
+	  else if (memcmp (iptr - 1, FROM_MARK_STR, FROM_MARK_LEN) == 0)
+	    {
+	      size_t rest = osize - j;
+	      
+	      if (rest > GT_FROM_MARK_LEN)
+		rest = GT_FROM_MARK_LEN;
+	      else if (rest < 2)
+		{
+		  if (i == 0)
+		    {
+		      iobuf->osize = GT_FROM_MARK_LEN;
+		      return mu_filter_moreoutput;
+		    }
+		  break;
+		}
+	      
+	      memcpy (optr + j, GT_FROM_MARK_STR, rest);
+	      i += rest - 2;
+	      iptr += rest - 2;
+	      j += rest;
+	      if (rest < GT_FROM_MARK_LEN)
+		state = length_to_state_tab[rest];
+	      else
+		state = from_encode_char;
+	      continue;
+	    }
+	  else
+	    state = from_encode_char;
+	}
+      optr[j++] = c;
+    }
+  *pstate = state;
+  iobuf->isize = i;
+  iobuf->osize = j;
+  return mu_filter_ok;
+}
+
+static int
+_from_alloc_state (void **pret, int mode, void *data MU_ARG_UNUSED)
+{
+  *pret = malloc (sizeof (int));
+  if (!*pret)
+    return ENOMEM;
+  return 0;
+}
+
+static struct _mu_filter_record _from_filter = {
+  "FROM",
+  0,
+  _from_alloc_state,
+  _from_encoder,
+  _from_decoder
+};
+
+mu_filter_record_t mu_from_filter = &_from_filter;
+
diff --git a/libmailutils/message_stream.c b/libmailutils/message_stream.c
index 6bfdae5..9f590d9 100644
--- a/libmailutils/message_stream.c
+++ b/libmailutils/message_stream.c
@@ -414,8 +414,8 @@ mu_stream_to_message (mu_stream_t instream, mu_message_t *pmsg)
       return rc;
     }
   
-  mu_envelope_set_date (env, _env_msg_date, msg);
-  mu_envelope_set_sender (env, _env_msg_sender, msg);
+  mu_envelope_set_date (env, _env_msg_date, draftstream);
+  mu_envelope_set_sender (env, _env_msg_sender, draftstream);
   mu_message_set_envelope (msg, env, draftstream);
 
   mu_body_create (&body, msg);
diff --git a/libmailutils/stream.c b/libmailutils/stream.c
index 2dfbc82..6084479 100644
--- a/libmailutils/stream.c
+++ b/libmailutils/stream.c
@@ -34,6 +34,9 @@
 
 size_t mu_stream_default_buffer_size = MU_STREAM_DEFBUFSIZ;
 
+#define _MU_STR_FLUSH_ALL  0x01
+#define _MU_STR_FLUSH_KEEP 0x02
+
 #define _stream_event(stream, code, n, p)			\
   do								\
     {								\
@@ -167,12 +170,12 @@ _stream_buffer_full_p (struct _mu_stream *stream)
 }
 
 static int
-_stream_flush_buffer (struct _mu_stream *stream, int all)
+_stream_flush_buffer (struct _mu_stream *stream, int flags)
 {
   int rc;
   char *start, *end;
   size_t wrsize;
-  
+
   if (stream->flags & _MU_STR_DIRTY)
     {
       if ((stream->flags & MU_STREAM_SEEK) && stream->seek)
@@ -215,7 +218,8 @@ _stream_flush_buffer (struct _mu_stream *stream, int all)
 	      if (wrsize == 0)
 		break;
 	    }
-	  if ((all && wrsize) || wrsize == stream->level)
+	  if (((flags & _MU_STR_FLUSH_ALL) && wrsize) ||
+	      wrsize == stream->level)
 	    {
 	      rc = _stream_write_unbuffered (stream,
 					     stream->buffer,
@@ -234,10 +238,11 @@ _stream_flush_buffer (struct _mu_stream *stream, int all)
 	  stream->level = stream->pos = wrsize;
 	  return 0;
 	}
+      _stream_clrflag (stream, _MU_STR_DIRTY);
     }
 
-  _stream_clrflag (stream, _MU_STR_DIRTY);
-  stream->pos = stream->level = 0;
+  if (!(flags & _MU_STR_FLUSH_KEEP))
+    stream->pos = stream->level = 0;
   return 0;
 }
 
@@ -400,8 +405,8 @@ mu_stream_seek (mu_stream_t stream, mu_off_t offset, int whence,
     case MU_SEEK_END:
       rc = mu_stream_size (stream, &size);
       if (rc)
-	return mu_stream_seterr (stream, rc, 1);
-      offset += size + stream->pos;
+	return rc;
+      offset += size;
       break;
 
     default:
@@ -414,7 +419,7 @@ mu_stream_seek (mu_stream_t stream, mu_off_t offset, int whence,
 	  || offset < stream->offset
 	  || offset > stream->offset + stream->level))
     {
-      if ((rc = _stream_flush_buffer (stream, 1)))
+      if ((rc = _stream_flush_buffer (stream, _MU_STR_FLUSH_ALL)))
 	return rc;
       rc = stream->seek (stream, offset, &stream->offset);
       if (rc == ESPIPE)
@@ -425,7 +430,9 @@ mu_stream_seek (mu_stream_t stream, mu_off_t offset, int whence,
     }
   else if (stream->buftype != mu_buffer_none)
     stream->pos = offset - stream->offset;
-			    
+
+  _mu_stream_cleareof (stream);
+  
   if (pres)
     *pres = stream->offset + stream->pos;
   return 0;
@@ -469,7 +476,7 @@ _stream_skip_input_bytes (mu_stream_t stream, mu_off_t count, mu_off_t *pres)
 	{
 	  for (pos = 0;;)
 	    {
-	      if ((rc = _stream_flush_buffer (stream, 1)))
+	      if ((rc = _stream_flush_buffer (stream, _MU_STR_FLUSH_ALL)))
 		return rc;
 	      if (stream->pos == stream->level)
 		{
@@ -693,10 +700,15 @@ mu_stream_read (mu_stream_t stream, void *buf, size_t size, size_t *pread)
     {
       char *bufp = buf;
       size_t nbytes = 0;
+      int rc;
+      
+      if ((rc = _stream_flush_buffer (stream,
+				      _MU_STR_FLUSH_ALL|_MU_STR_FLUSH_KEEP)))
+	return rc;
+
       while (size)
 	{
 	  size_t n;
-	  int rc;
 	  
 	  if (stream->pos == stream->level)
 	    {
@@ -824,7 +836,12 @@ mu_stream_readdelim (mu_stream_t stream, char *buf, size_t size,
 	rc = _stream_readdelim (stream, buf, size, delim, pread);
     }
   else
-    rc = _stream_scandelim (stream, buf, size, delim, pread);
+    {
+      if ((rc = _stream_flush_buffer (stream,
+				      _MU_STR_FLUSH_ALL|_MU_STR_FLUSH_KEEP)))
+	return rc;
+      rc = _stream_scandelim (stream, buf, size, delim, pread);
+    }
   return rc;
 }
 
@@ -850,6 +867,10 @@ mu_stream_getdelim (mu_stream_t stream, char **pbuf, size_t *psize,
       _stream_init (stream);
     }
 
+  if ((rc = _stream_flush_buffer (stream,
+				  _MU_STR_FLUSH_ALL|_MU_STR_FLUSH_KEEP)))
+    return rc;
+  
   if (lineptr == NULL || n == 0)
     {
       char *new_lineptr;
@@ -997,7 +1018,7 @@ mu_stream_flush (mu_stream_t stream)
 	return MU_ERR_NOT_OPEN;
       _stream_init (stream);
     }
-  rc = _stream_flush_buffer (stream, 1);
+  rc = _stream_flush_buffer (stream, _MU_STR_FLUSH_ALL);
   if (rc)
     return rc;
   if ((stream->flags & _MU_STR_WRT) && stream->flush)
@@ -1031,7 +1052,8 @@ int
 mu_stream_size (mu_stream_t stream, mu_off_t *psize)
 {
   int rc;
-    
+  mu_off_t size;
+  
   if (!(stream->flags & _MU_STR_OPEN))
     {
       if (stream->open)
@@ -1040,7 +1062,13 @@ mu_stream_size (mu_stream_t stream, mu_off_t *psize)
     }
   if (!stream->size)
     return mu_stream_seterr (stream, ENOSYS, 0);
-  rc = stream->size (stream, psize);
+  rc = stream->size (stream, &size);
+  if (rc == 0)
+    {
+      if (stream->buftype != mu_buffer_none && stream->offset == size)
+	size += stream->level;
+      *psize = size;
+    }
   return mu_stream_seterr (stream, rc, rc != 0);
 }
 
@@ -1118,7 +1146,7 @@ mu_stream_truncate (mu_stream_t stream, mu_off_t size)
     {
       int rc;
       
-      if ((rc = _stream_flush_buffer (stream, 1)))
+      if ((rc = _stream_flush_buffer (stream, _MU_STR_FLUSH_ALL)))
 	return rc;
       return stream->truncate (stream, size);
     }
diff --git a/libmailutils/streamcpy.c b/libmailutils/streamcpy.c
index 13742e7..2020fad 100644
--- a/libmailutils/streamcpy.c
+++ b/libmailutils/streamcpy.c
@@ -72,6 +72,7 @@ mu_stream_copy (mu_stream_t dst, mu_stream_t src, mu_off_t size,
 	    case EACCES:
 	      mu_stream_clearerr (src);
 	    case ENOSYS:
+	      size = 0;
 	      break;
 
 	    default:
diff --git a/libmailutils/temp_file_stream.c b/libmailutils/temp_file_stream.c
index 857bada..fb178a5 100644
--- a/libmailutils/temp_file_stream.c
+++ b/libmailutils/temp_file_stream.c
@@ -66,7 +66,10 @@ mu_temp_file_stream_create (mu_stream_t *pstream, const char *dir)
       if (rc)
 	mu_stream_unref (stream);
       else
-	*pstream = stream;
+	{
+	  mu_stream_set_buffer (stream, mu_buffer_full, 0);
+	  *pstream = stream;
+	}
     }
   return 0;
 }
diff --git a/libmailutils/tests/Makefile.am b/libmailutils/tests/Makefile.am
index 7324e88..a8215fb 100644
--- a/libmailutils/tests/Makefile.am
+++ b/libmailutils/tests/Makefile.am
@@ -64,6 +64,7 @@ TESTSUITE_AT = \
  base64e.at\
  decode2047.at\
  encode2047.at\
+ fromflt.at\
  list.at\
  mailcap.at\
  testsuite.at\
diff --git a/libmailutils/tests/base64d.at b/libmailutils/tests/base64d.at
index b26c415..df1efd3 100644
--- a/libmailutils/tests/base64d.at
+++ b/libmailutils/tests/base64d.at
@@ -16,7 +16,7 @@
 # This file is part of Mailfromd testsuite. 
 
 AT_SETUP([base64 decoding (read)])
-AT_KEYWORDS([base64 base64d base64dr decode])
+AT_KEYWORDS([base64 base64d base64dr decode filter])
 
 AT_CHECK([
 cp $abs_top_srcdir/libmailutils/tests/Encode expout
@@ -28,7 +28,7 @@ AT_CLEANUP
 
 
 AT_SETUP([base64 decoding (write)])
-AT_KEYWORDS([base64 base64d base64dw decode])
+AT_KEYWORDS([base64 base64d base64dw decode filter])
 
 AT_CHECK([
 cp $abs_top_srcdir/libmailutils/tests/Encode expout
diff --git a/libmailutils/tests/base64e.at b/libmailutils/tests/base64e.at
index c753471..b94522e 100644
--- a/libmailutils/tests/base64e.at
+++ b/libmailutils/tests/base64e.at
@@ -16,7 +16,7 @@
 # This file is part of Mailfromd testsuite. 
 
 AT_SETUP([base64 encoding (read)])
-AT_KEYWORDS([base64 base64e base64er encode])
+AT_KEYWORDS([base64 base64e base64er encode filter])
 
 AT_CHECK([
 cp $abs_top_srcdir/libmailutils/tests/Decode expout
@@ -28,7 +28,7 @@ AT_CLEANUP
 
 
 AT_SETUP([base64 encoding (write)])
-AT_KEYWORDS([base64 base64e base64ew encode])
+AT_KEYWORDS([base64 base64e base64ew encode filter])
 
 AT_CHECK([
 cp $abs_top_srcdir/libmailutils/tests/Decode expout
diff --git a/libmailutils/tests/fltst.c b/libmailutils/tests/fltst.c
index 8597dd5..d62a02b 100644
--- a/libmailutils/tests/fltst.c
+++ b/libmailutils/tests/fltst.c
@@ -114,7 +114,7 @@ main (int argc, char * argv [])
   
   if (argc == 1)
     usage (NULL);
-  if (argc < 3)
+  if (argc < 4)
     usage ("not enough arguments");
   
   fltname = argv[1];
diff --git a/libmailutils/tests/fromflt.at b/libmailutils/tests/fromflt.at
new file mode 100644
index 0000000..a5b3313
--- /dev/null
+++ b/libmailutils/tests/fromflt.at
@@ -0,0 +1,96 @@
+# This file is part of GNU Mailutils. -*- Autotest -*-
+# Copyright (C) 2007, 2008, 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 GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
+# This file is part of Mailfromd testsuite. 
+
+# -------------------------------------------
+# Data for 'From' filter tests.
+# -------------------------------------------
+
+m4_define([from_plain_text],[From this time on
+from that source
+ From stdin
+and
+From them
+])
+
+m4_define([from_encoded_text],[>From this time on
+from that source
+ From stdin
+and
+>From them
+])
+
+# -------------------------------------------
+# Test 'From' encoding
+# -------------------------------------------
+
+# Test read mode
+
+AT_SETUP([from filter encoding (read)])
+AT_KEYWORDS([from frome fromer encode])
+
+AT_CHECK([
+AT_DATA([input],from_plain_text)
+fltst from encode read < input
+],
+[0],
+[from_encoded_text])
+
+AT_CLEANUP
+
+# The same, in write mode
+
+AT_SETUP([from filter encoding (write)])
+AT_KEYWORDS([from frome fromew encode])
+
+AT_CHECK([
+AT_DATA([input],from_plain_text)
+fltst from encode write < input
+],
+[0],
+[from_encoded_text])
+
+AT_CLEANUP
+
+# -------------------------------------------
+# Test '>From' decoding
+# -------------------------------------------
+
+AT_SETUP([from filter decoding (read)])
+AT_KEYWORDS([from fromd fromdr decode])
+
+AT_CHECK([
+AT_DATA([input],from_encoded_text)
+fltst from decode read < input
+],
+[0],
+[from_plain_text])
+
+AT_CLEANUP
+
+# The same, in write mode
+
+AT_SETUP([from filter decoding (write)])
+AT_KEYWORDS([from fromd fromdw decode])
+
+AT_CHECK([
+AT_DATA([input],from_encoded_text)
+fltst from decode write < input
+],
+[0],
+[from_plain_text])
+
+AT_CLEANUP
diff --git a/libmailutils/tests/testsuite.at b/libmailutils/tests/testsuite.at
index 1de64b6..fbdc7c9 100644
--- a/libmailutils/tests/testsuite.at
+++ b/libmailutils/tests/testsuite.at
@@ -63,4 +63,5 @@ m4_include([base64e.at])
 m4_include([base64d.at])
 m4_include([decode2047.at])
 m4_include([encode2047.at])
+m4_include([fromflt.at])
 
diff --git a/libproto/mbox/mbox.c b/libproto/mbox/mbox.c
index 6250765..cc412ff 100644
--- a/libproto/mbox/mbox.c
+++ b/libproto/mbox/mbox.c
@@ -28,6 +28,7 @@
 #include <mbox0.h>
 #include <mailutils/cstr.h>
 #include <mailutils/io.h>
+#include <mailutils/filter.h>
 
 #define ATTRIBUTE_IS_DELETED(flag)        (flag & MU_ATTRIBUTE_DELETED)
 #define ATTRIBUTE_IS_EQUAL(flag1, flag2)  (flag1 == flag2)
@@ -1033,7 +1034,7 @@ append_message_to_stream (mu_stream_t ostr, mu_message_t msg,
 			  mbox_data_t mud, int flags)
 {
   int status;
-  mu_stream_t istr;
+  mu_stream_t istr, flt;
   
   status = msg_envelope_to_stream (ostr, msg);
   if (status)
@@ -1084,10 +1085,18 @@ append_message_to_stream (mu_stream_t ostr, mu_message_t msg,
       if (status)
 	return status;
     }
-  status = mu_stream_copy (ostr, istr, 0, NULL);
-  mu_stream_destroy (&istr);
+
+  status = mu_filter_create (&flt, istr, "FROM",
+			     MU_FILTER_ENCODE, MU_STREAM_READ);
+  mu_stream_unref (istr);
   if (status == 0)
-    status = mu_stream_write (ostr, "\n", 1, NULL);
+    {
+      status = mu_stream_copy (ostr, flt, 0, NULL);
+      mu_stream_destroy (&flt);
+      if (status == 0)
+	status = mu_stream_write (ostr, "\n", 1, NULL);
+    }
+  
   return status;
 }
 
diff --git a/maidag/Makefile.am b/maidag/Makefile.am
index 0b28d93..835f895 100644
--- a/maidag/Makefile.am
+++ b/maidag/Makefile.am
@@ -26,7 +26,6 @@ maidag_SOURCES=\
  lmtp.c\
  maidag.c\
  maidag.h\
- mailtmp.c\
  mailquota.c\
  python.c\
  sieve.c\
diff --git a/maidag/deliver.c b/maidag/deliver.c
index 24e2f0c..d5bbc1f 100644
--- a/maidag/deliver.c
+++ b/maidag/deliver.c
@@ -19,26 +19,110 @@
 
 #include "maidag.h"
 
-void
-make_tmp (const char *from, mu_mailbox_t *mbox)
+static mu_mailbox_t
+make_tmp (const char *from)
 {
-  struct mail_tmp *mtmp;
-  char *buf = NULL;
-  size_t n = 0;
   int rc;
+  mu_stream_t in, out;
+  char *buf = NULL;
+  size_t size = 0, n;
+  mu_mailbox_t mbox;
+  
+  rc = mu_stdio_stream_create (&in, MU_STDIN_FD, MU_STREAM_READ);
+  if (rc)
+    {
+      mu_diag_funcall (MU_DIAG_ERROR, "mu_stdio_stream_create",
+		       "MU_STDIN_FD", rc);
+      exit (EX_TEMPFAIL);
+    } 
+
+  rc = mu_temp_file_stream_create (&out, NULL);
+  if (rc)
+    {
+      maidag_error (_("unable to open temporary file: %s"), mu_strerror (rc));
+      exit (EX_TEMPFAIL);
+    }
 
-  if (mail_tmp_begin (&mtmp, from))
-    exit (EX_TEMPFAIL);
+  rc = mu_stream_getline (in, &buf, &size, &n);
+  if (rc)
+    {
+      maidag_error (_("read error: %s"), mu_strerror (rc));
+      mu_stream_destroy (&in);
+      mu_stream_destroy (&out);
+      exit (EX_TEMPFAIL);
+    }
+  if (n == 0)
+    {
+      maidag_error (_("unexpected EOF on input"));
+      mu_stream_destroy (&in);
+      mu_stream_destroy (&out);
+      exit (EX_TEMPFAIL);
+    } 
 
-  while (getline (&buf, &n, stdin) > 0)
-    if ((rc = mail_tmp_add_line (mtmp, buf, strlen (buf))))
-      break;
+  if (n >= 5 && memcmp (buf, "From ", 5))
+    {
+      struct mu_auth_data *auth = NULL;
+      if (!from)
+	{
+	  auth = mu_get_auth_by_uid (getuid ());
+	  if (auth)
+	    from = auth->name;
+	}
+      if (from)
+	{
+	  time_t t;
+	  
+	  time (&t);
+	  mu_stream_printf (out, "From %s %s", from, ctime (&t));
+	}
+      else
+	{
+	  maidag_error (_("cannot determine sender address"));
+	  mu_stream_destroy (&in);
+	  mu_stream_destroy (&out);
+	  exit (EX_TEMPFAIL);
+	}
+      if (auth)
+	mu_auth_data_free (auth);
+    }
+
+  mu_stream_write (out, buf, n, NULL);
   free (buf);
-  if (rc == 0)
-    rc = mail_tmp_finish (mtmp, mbox);
-  mail_tmp_destroy (&mtmp);
+  
+  rc = mu_stream_copy (out, in, 0, NULL);
+  
+  mu_stream_destroy (&in);
   if (rc)
-    exit (EX_TEMPFAIL);
+    {
+      maidag_error (_("copy error: %s"), mu_strerror (rc));
+      mu_stream_destroy (&out);
+      exit (EX_TEMPFAIL);
+    }
+
+  mu_stream_flush (out);
+  if ((rc = mu_mailbox_create (&mbox, "mbox:/dev/null")) 
+      || (rc = mu_mailbox_open (mbox, MU_STREAM_READ))
+      || (rc = mu_mailbox_set_stream (mbox, out)))
+    {
+      maidag_error (_("error opening temporary file: %s"), 
+                    mu_strerror (rc));
+      mu_stream_destroy (&out);
+      exit (EX_TEMPFAIL);
+    }
+
+  rc = mu_mailbox_messages_count (mbox, &n);
+  if (rc)
+    {
+      errno = rc;
+      maidag_error (_("error creating temporary message: %s"),
+		    mu_strerror (rc));
+      mu_stream_destroy (&out);
+      exit (EX_TEMPFAIL);
+    }
+
+  /* FIXME: mu_stream_unref (out); But mu_mailbox_set_stream
+     steals the reference */
+  return mbox;
 }
 
 int
@@ -65,9 +149,7 @@ mda (mu_mailbox_t mbx, char *username)
 int
 maidag_stdio_delivery (int argc, char **argv)
 {
-  mu_mailbox_t mbox;
-
-  make_tmp (sender_address, &mbox);
+  mu_mailbox_t mbox = make_tmp (sender_address);
   
   if (multiple_delivery)
     multiple_delivery = argc > 1;
diff --git a/maidag/lmtp.c b/maidag/lmtp.c
index 4393291..726be15 100644
--- a/maidag/lmtp.c
+++ b/maidag/lmtp.c
@@ -26,10 +26,39 @@
 #include <mu_umaxtostr.h>
 
 mu_list_t lmtp_groups;
-static int lmtp_transcript;
+
+static mu_stream_t
+lmpt_transcript (mu_stream_t iostream)
+{
+  int rc;
+  mu_debug_t debug;
+  mu_stream_t dstr, xstr;
+      
+  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, iostream, dstr, NULL);
+      if (rc)
+	mu_error (_("cannot create transcript stream: %s"),
+		  mu_strerror (rc));
+      else
+	{
+	  /* FIXME: Would do a mu_stream_unref (iostream) here,
+	     however mu_xscript_stream_create *may* steal the reference.
+	     This should be fixed in mu_xscript_stream_create. */
+	  iostream = xstr;
+	}
+    }
+  return iostream;
+}
 
 void
-lmtp_reply (FILE *fp, char *code, char *enh, char *fmt, ...)
+lmtp_reply (mu_stream_t iostr, char *code, char *enh, char *fmt, ...)
 {
   va_list ap;
   char *str;
@@ -38,14 +67,6 @@ lmtp_reply (FILE *fp, char *code, char *enh, char *fmt, ...)
   vasprintf (&str, fmt, ap);
   va_end (ap);
 
-  if (lmtp_transcript)
-    {
-      if (enh)
-	mu_diag_output (MU_DIAG_INFO, "LMTP reply: %s %s %s", code, enh, str);
-      else
-	mu_diag_output (MU_DIAG_INFO, "LMTP reply: %s %s", code, str);
-    }
-  
   if (!str)
     {
       mu_error (_("not enough memory"));
@@ -59,39 +80,23 @@ lmtp_reply (FILE *fp, char *code, char *enh, char *fmt, ...)
       if (end)
 	{
 	  size_t len = end - str;
-	  fprintf (fp, "%s-", code);
+	  mu_stream_printf (iostr, "%s-", code);
 	  if (enh)
-	    fprintf (fp, "%s ", enh);
-	  fprintf (fp, "%.*s\r\n", (int) len, str);
+	    mu_stream_printf (iostr, "%s ", enh);
+	  mu_stream_printf (iostr, "%.*s\r\n", (int) len, str);
 	  for (str = end; *str && *str == '\n'; str++);
 	}
       else
 	{
-	  fprintf (fp, "%s ", code);
+	  mu_stream_printf (iostr, "%s ", code);
 	  if (enh)
-	    fprintf (fp, "%s ", enh);
-	  fprintf (fp, "%s\r\n", str);
+	    mu_stream_printf (iostr, "%s ", enh);
+	  mu_stream_printf (iostr, "%s\r\n", str);
 	  str += strlen (str);
 	}
     }
 }
 
-void
-xlatnl (char *arg)
-{
-  size_t len = strlen (arg);
-  if (len > 0 && arg[len-1] == '\n')
-    {
-      len--;
-      if (len > 0 && arg[len-1] == '\r')
-	{
-	  arg[len-1] = '\n';
-	  arg[len] = 0;
-	}
-    }
-}
-
-
 enum lmtp_state
   {
     state_none,
@@ -152,14 +157,13 @@ int transtab[NCMD][NSTATE] = {
 char *lhlo_domain;     /* Sender domain */
 char *mail_from;       /* Sender address */
 mu_list_t rcpt_list;   /* Recipient addresses */
-struct mail_tmp *mtmp; /* Temporary mail storage */
-mu_mailbox_t mbox;     /* Collected mail body */
+mu_message_t mesg;     /* Collected message */
 
 
 int
-cfun_unknown (FILE *out, char *arg)
+cfun_unknown (mu_stream_t iostr, char *arg)
 {
-  lmtp_reply (out, "500", "5.5.1", "Command unrecognized");
+  lmtp_reply (iostr, "500", "5.5.1", "Command unrecognized");
   return 0;
 }
 
@@ -242,46 +246,46 @@ check_address (char *arg, int with_domain, char **pret)
 
 
 int
-cfun_mail_from (FILE *out, char *arg)
+cfun_mail_from (mu_stream_t iostr, char *arg)
 {
   if (*arg == 0)
     {
-      lmtp_reply (out, "501", "5.5.2", "Syntax error");
+      lmtp_reply (iostr, "501", "5.5.2", "Syntax error");
       return 1;
     }
 
   if (check_address (arg, 1, &mail_from))
     {
-      lmtp_reply (out, "553", "5.1.8", "Address format error");
+      lmtp_reply (iostr, "553", "5.1.8", "Address format error");
       return 1;
     }
-  lmtp_reply (out, "250", "2.1.0", "Go ahead");
+  lmtp_reply (iostr, "250", "2.1.0", "Go ahead");
   return 0;
 }
 
 
 int
-cfun_rcpt_to (FILE *out, char *arg)
+cfun_rcpt_to (mu_stream_t iostr, char *arg)
 {
   char *user;
   struct mu_auth_data *auth;
   
   if (*arg == 0)
     {
-      lmtp_reply (out, "501", "5.5.2", "Syntax error");
+      lmtp_reply (iostr, "501", "5.5.2", "Syntax error");
       return 1;
     }
 
   /* FIXME: Check if domain is OK */
   if (check_address (arg, 0, &user))
     {
-      lmtp_reply (out, "553", "5.1.8", "Address format error");
+      lmtp_reply (iostr, "553", "5.1.8", "Address format error");
       return 1;
     }
   auth = mu_get_auth_by_name (user);
   if (!auth)
     {
-      lmtp_reply (out, "550", "5.1.1", "User unknown");
+      lmtp_reply (iostr, "550", "5.1.1", "User unknown");
       free (user);
       return 1;
     }
@@ -292,37 +296,17 @@ cfun_rcpt_to (FILE *out, char *arg)
       mu_list_set_destroy_item (rcpt_list, mu_list_free_item);
     }
   mu_list_append (rcpt_list, user);
-  lmtp_reply (out, "250", "2.1.5", "Go ahead");
+  lmtp_reply (iostr, "250", "2.1.5", "Go ahead");
   return 0;
 }  
 
 
 int
-cfun_data (FILE *out, char *arg)
-{
-  if (*arg)
-    {
-      lmtp_reply (out, "501", "5.5.2", "Syntax error");
-      return 1;
-    }
-
-  if (mail_tmp_begin (&mtmp, mail_from))
-    {
-      /* FIXME: codes */
-      lmtp_reply (out, "450", "4.1.0", "Temporary failure, try again later");
-      return 1;
-    }
-  lmtp_reply (out, "354", NULL, "Go ahead");
-  return 0;
-}
-
-
-int
 dot_temp_fail (void *item, void *cbdata)
 {
   char *name = item;
-  FILE *out = cbdata;
-  lmtp_reply (out, "450", "4.1.0", "%s: temporary failure", name);
+  mu_stream_t iostr = cbdata;
+  lmtp_reply (iostr, "450", "4.1.0", "%s: temporary failure", name);
   return 0;
 }
 
@@ -330,39 +314,27 @@ int
 dot_deliver (void *item, void *cbdata)
 {
   char *name = item;
-  FILE *out = cbdata;
+  mu_stream_t iostr = cbdata;
   char *errp = NULL;
-  mu_message_t msg;
-  int status;
   
-  if ((status = mu_mailbox_get_message (mbox, 1, &msg)) != 0)
-    {
-      mu_error (_("cannot get message from the temporary mailbox: %s"),
-		mu_strerror (status));
-      lmtp_reply (out, "450", "4.1.0",
-		  "%s: temporary failure, try again later",
-		  name);
-      return 0;
-    }
-
-  switch (deliver (msg, name, &errp))
+  switch (deliver (mesg, name, &errp))
     {
     case 0:
-      lmtp_reply (out, "250", "2.0.0", "%s: delivered", name);
+      lmtp_reply (iostr, "250", "2.0.0", "%s: delivered", name);
       break;
 
     case EX_UNAVAILABLE:
       if (errp)
-	lmtp_reply (out, "553", "5.1.8", "%s", errp);
+	lmtp_reply (iostr, "553", "5.1.8", "%s", errp);
       else
-	lmtp_reply (out, "553", "5.1.8", "%s: delivery failed", name);
+	lmtp_reply (iostr, "553", "5.1.8", "%s: delivery failed", name);
       break;
 
     default:
       if (errp)
-	lmtp_reply (out, "450", "4.1.0", "%s", errp);
+	lmtp_reply (iostr, "450", "4.1.0", "%s", errp);
       else
-	lmtp_reply (out, "450", "4.1.0",
+	lmtp_reply (iostr, "450", "4.1.0",
 		    "%s: temporary failure, try again later",
 		    name);
       break;
@@ -372,37 +344,103 @@ dot_deliver (void *item, void *cbdata)
 }
 
 int
-cfun_dot (FILE *out, char *arg)
+cfun_data (mu_stream_t iostr, char *arg)
 {
-  if (!mtmp)
-    mu_list_do (rcpt_list, dot_temp_fail, out);
-  else
+  int rc;
+  mu_stream_t flt, tempstr;
+  time_t t;
+  int xlev = MU_XSCRIPT_PAYLOAD, xlev_switch = 0, buf_switch = 0;
+  struct mu_buffer_query oldbuf;
+  
+  if (*arg)
     {
-      int rc = mail_tmp_finish (mtmp, &mbox);
-      if (rc)
-	mu_list_do (rcpt_list, dot_temp_fail, out);
-      else
-	{
-	  mu_list_do (rcpt_list, dot_deliver, out);
-	  mail_tmp_destroy (&mtmp);
-	  mu_mailbox_destroy (&mbox);
-	}
+      lmtp_reply (iostr, "501", "5.5.2", "Syntax error");
+      return 1;
     }
-  free (mail_from);
-  mu_list_destroy (&rcpt_list);
+
+  rc = mu_filter_create (&flt, iostr, "CRLFDOT", MU_FILTER_DECODE,
+			 MU_STREAM_READ|MU_STREAM_WRTHRU);
+  if (rc)
+    {
+      maidag_error (_("unable to open filter: %s"),
+		    mu_strerror (rc));
+      lmtp_reply (iostr, "450", "4.1.0", "Temporary failure, try again later");
+      return 1;
+    }
+
+  rc = mu_temp_file_stream_create (&tempstr, NULL);
+  if (rc)
+    {
+      maidag_error (_("unable to open temporary file: %s"), mu_strerror (rc));
+      mu_stream_destroy (&flt);
+      return 1;
+    }
+
+  /* Write out envelope */
+  time (&t);
+  rc = mu_stream_printf (tempstr, "From %s %s", mail_from, ctime (&t));
+  if (rc)
+    {
+      maidag_error (_("copy error: %s"), mu_strerror (rc));
+      mu_stream_destroy (&flt);
+      mu_stream_destroy (&tempstr);
+      mu_list_do (rcpt_list, dot_temp_fail, iostr);
+    }
+
+  lmtp_reply (iostr, "354", NULL, "Go ahead");
+
+  if (mu_stream_ioctl (iostr, MU_IOCTL_GET_TRANSPORT_BUFFER, &oldbuf) == 0)
+    {
+      struct mu_buffer_query newbuf;
+
+      newbuf.type = MU_TRANSPORT_OUTPUT;
+      newbuf.buftype = mu_buffer_full;
+      newbuf.bufsize = 64*1024;
+      if (mu_stream_ioctl (iostr, MU_IOCTL_SET_TRANSPORT_BUFFER, &newbuf))
+	buf_switch = 1;
+    }
+
+  if (mu_stream_ioctl (iostr, MU_IOCTL_LEVEL, &xlev) == 0)
+    xlev_switch = 1;
+  rc = mu_stream_copy (tempstr, flt, 0, NULL);
+  mu_stream_destroy (&flt);
+  if (xlev_switch)
+    mu_stream_ioctl (iostr, MU_IOCTL_LEVEL, &xlev);
+  if (buf_switch)
+    mu_stream_ioctl (iostr, MU_IOCTL_SET_TRANSPORT_BUFFER, &oldbuf);
+  if (rc)
+    {
+      maidag_error (_("copy error: %s"), mu_strerror (rc));
+      mu_list_do (rcpt_list, dot_temp_fail, iostr);
+    }
+
+  rc = mu_stream_to_message (tempstr, &mesg);
+  if (rc)
+    {
+      maidag_error (_("error creating temporary message: %s"),
+		    mu_strerror (rc));
+      mu_list_do (rcpt_list, dot_temp_fail, iostr);
+    }
+  
+  rc = mu_list_do (rcpt_list, dot_deliver, iostr);
+
+  mu_stream_destroy (&tempstr);
+  mu_message_destroy (&mesg, mu_message_get_owner (mesg));
+  if (rc)
+    mu_list_do (rcpt_list, dot_temp_fail, iostr);
+
   return 0;
 }
-  
+
 
 int
-cfun_rset (FILE *out, char *arg)
+cfun_rset (mu_stream_t iostr, char *arg)
 {
   free (lhlo_domain);
   free (mail_from);
   mu_list_destroy (&rcpt_list);
-  mail_tmp_destroy (&mtmp);
-  mu_mailbox_destroy (&mbox);
-  lmtp_reply (out, "250", "2.0.0", "OK, forgotten");
+  mu_message_destroy (&mesg, mu_message_get_owner (mesg));
+  lmtp_reply (iostr, "250", "2.0.0", "OK, forgotten");
   return 0;
 }
 
@@ -413,32 +451,32 @@ PIPELINING\n\
 HELP";
 
 int
-cfun_lhlo (FILE *out, char *arg)
+cfun_lhlo (mu_stream_t iostr, char *arg)
 {
   if (*arg == 0)
     {
-      lmtp_reply (out, "501", "5.0.0", "Syntax error");
+      lmtp_reply (iostr, "501", "5.0.0", "Syntax error");
       return 1;
     }
   lhlo_domain = strdup (arg);
-  lmtp_reply (out, "250", NULL, "Hello\n");
-  lmtp_reply (out, "250", NULL, capa_str);
+  lmtp_reply (iostr, "250", NULL, "Hello\n");
+  lmtp_reply (iostr, "250", NULL, capa_str);
   return 0;
 }
 
 
 int
-cfun_quit (FILE *out, char *arg)
+cfun_quit (mu_stream_t iostr, char *arg)
 {
-  lmtp_reply (out, "221", "2.0.0", "Bye");
+  lmtp_reply (iostr, "221", "2.0.0", "Bye");
   return 0;
 }
 
 
 int
-cfun_help (FILE *out, char *arg)
+cfun_help (mu_stream_t iostr, char *arg)
 {
-  lmtp_reply (out, "200", "2.0.0", "Man, help yourself");
+  lmtp_reply (iostr, "200", "2.0.0", "Man, help yourself");
   return 0;
 }
 
@@ -448,7 +486,7 @@ struct command_tab
   char *cmd_verb;
   int cmd_len;
   enum lmtp_command cmd_code;
-  int (*cmd_fun) (FILE *, char *);
+  int (*cmd_fun) (mu_stream_t, char *);
 } command_tab[] = {
 #define S(s) #s, (sizeof #s - 1)
   { S(lhlo), cmd_lhlo, cfun_lhlo },
@@ -458,7 +496,6 @@ struct command_tab
   { S(quit), cmd_quit, cfun_quit },
   { S(rset), cmd_rset, cfun_rset },
   { S(help), cmd_help, cfun_help },
-  { S(.), cmd_dot, cfun_dot },
   { NULL, 0, cmd_unknown, cfun_unknown }
 };
 
@@ -479,72 +516,47 @@ getcmd (char *buf, char **sp)
   return cp;
 }
 
-static char *
-to_fgets (char *buf, size_t size, FILE *fp, unsigned int timeout)
+static int
+to_fgets (mu_stream_t iostr, char **pbuf, size_t *psize, size_t *pnread,
+	  unsigned int timeout)
 {
-  char *p;
+  int rc;
+  
   alarm (timeout);
-  p = fgets (buf, size, fp);
+  rc = mu_stream_getline (iostr, pbuf, psize, pnread);
   alarm (0);
-  return p;
+  return rc;
 }
 
 int
-lmtp_loop (FILE *in, FILE *out, unsigned int timeout)
+lmtp_loop (mu_stream_t iostr, unsigned int timeout)
 {
-  char buf[1024];
+  size_t size = 0, n;
+  char *buf = NULL;
   enum lmtp_state state = state_init;
 
-  setvbuf (in, NULL, _IOLBF, 0);
-  setvbuf (out, NULL, _IOLBF, 0);
-
-  lmtp_reply (out, "220", NULL, "At your service");
-  while (to_fgets (buf, sizeof buf, in, timeout))
+  lmtp_reply (iostr, "220", NULL, "At your service");
+  while (to_fgets (iostr, &buf, &size, &n, timeout) == 0 && n)
     {
-      if (state == state_data
-	  && !(buf[0] == '.'
-	       && (buf[1] == '\n' || (buf[1] == '\r' && buf[2] == '\n'))))
-	{
-	  /* This is a special case */
-	  if (mtmp)
-	    {
-	      size_t len;
-	      int rc;
+      char *sp;
+      struct command_tab *cp = getcmd (buf, &sp);
+      enum lmtp_command cmd = cp->cmd_code;
+      enum lmtp_state next_state = transtab[cmd][state];
 
-	      xlatnl (buf);
-	      len = strlen (buf);
-	      if ((rc = mail_tmp_add_line (mtmp, buf, len)))
-		{
-		  mail_tmp_destroy (&mtmp);
-		  /* Wait the dot to report the error */
-		}
-	    }
-	}
-      else
-	{
-	  char *sp;
-	  struct command_tab *cp = getcmd (buf, &sp);
-	  enum lmtp_command cmd = cp->cmd_code;
-	  enum lmtp_state next_state = transtab[cmd][state];
+      mu_rtrim_cset (sp, "\r\n");
 
-	  mu_rtrim_cset (sp, "\r\n");
-
-	  if (lmtp_transcript)
-	    mu_diag_output (MU_DIAG_INFO, "LMTP receive: %s", buf);
-	      
-	  if (next_state != state_none)
+      if (next_state != state_none)
+	{
+	  if (cp->cmd_fun)
 	    {
-	      if (cp->cmd_fun)
-		{
-		  sp = mu_str_skip_class (sp, MU_CTYPE_SPACE);
-		  if (cp->cmd_fun (out, sp))
-		    continue;
-		}
-	      state = next_state;
+	      sp = mu_str_skip_class (sp, MU_CTYPE_SPACE);
+	      if (cp->cmd_fun (iostr, sp))
+		continue;
 	    }
-	  else
-	    lmtp_reply (out, "503", "5.0.0", "Syntax error");
+	  state = next_state;
 	}
+      else
+	lmtp_reply (iostr, "503", "5.0.0", "Syntax error");
       
       if (state == state_end)
 	break;
@@ -563,8 +575,22 @@ int
 lmtp_connection (int fd, struct sockaddr *sa, int salen, void *data,
 		 mu_ip_server_t srv, time_t timeout, int transcript)
 {
-  lmtp_transcript = transcript;
-  lmtp_loop (fdopen (fd, "r"), fdopen (fd, "w"), timeout);
+  mu_stream_t str;
+  int rc;
+  
+  rc = mu_fd_stream_create (&str, NULL, fd,
+			    MU_STREAM_RDWR|MU_STREAM_AUTOCLOSE);
+  if (rc)
+    {
+      mu_diag_funcall (MU_DIAG_ERROR, "mu_fd_stream_create", NULL, rc);
+      return rc;
+    }
+  mu_stream_set_buffer (str, mu_buffer_line, 0);
+
+  if (transcript || maidag_transcript)
+    str = lmpt_transcript (str);
+  lmtp_loop (str, timeout);
+  mu_stream_destroy (&str);
   return 0;
 }
 
@@ -640,6 +666,40 @@ maidag_lmtp_server ()
       if (status)
 	return EX_CONFIG;
     }
-  else 
-    return lmtp_loop (stdin, stdout, 0);
+  else
+    {
+      mu_stream_t str, istream, ostream;
+
+      rc = mu_stdio_stream_create (&istream, MU_STDIN_FD, MU_STREAM_READ);
+      if (rc)
+	{
+	  mu_diag_funcall (MU_DIAG_ERROR, "mu_stdio_stream_create",
+			   "MU_STDIN_FD", rc);
+	  return EX_UNAVAILABLE;
+	} 
+ 
+      rc = mu_stdio_stream_create (&ostream, MU_STDOUT_FD, MU_STREAM_WRITE);
+      if (rc)
+	{
+	  mu_diag_funcall (MU_DIAG_ERROR, "mu_stdio_stream_create",
+			   "MU_STDOUT_FD", rc);
+	  return 1;
+	} 
+
+      rc = mu_iostream_create (&str, istream, ostream);
+      mu_stream_unref (istream);
+      mu_stream_unref (ostream);
+      if (rc)
+	{
+	  mu_diag_funcall (MU_DIAG_ERROR, "mu_iostream_create", NULL, rc);
+	  return 1;
+	} 
+
+      if (maidag_transcript)
+	str = lmpt_transcript (str);
+
+      rc = lmtp_loop (str, 0);
+      mu_stream_destroy (&str);
+      return rc;
+    }
 }
diff --git a/maidag/maidag.c b/maidag/maidag.c
index 894fa61..ff5c34b 100644
--- a/maidag/maidag.c
+++ b/maidag/maidag.c
@@ -53,6 +53,7 @@ int lmtp_mode;
 int url_option;
 char *lmtp_url_string;
 int reuse_lmtp_address = 1;
+int maidag_transcript;
 
 const char *program_version = "maidag (" PACKAGE_STRING ")";
 static char doc[] =
@@ -72,6 +73,7 @@ static char args_doc[] = N_("[recipient...]");
 #define LMTP_OPTION 258
 #define FOREGROUND_OPTION 260
 #define URL_OPTION 261
+#define TRANSCRIPT_OPTION 262
 
 static struct argp_option options[] = 
 {
@@ -79,21 +81,23 @@ static struct argp_option options[] =
  { NULL, 0, NULL, 0,
    N_("General options"), GRID },
       
-  { "foreground", FOREGROUND_OPTION, 0, 0, N_("remain in foreground"),
-    GRID + 1 },
-  { "inetd",  'i', 0, 0, N_("run in inetd mode"), GRID + 1 },
-  { "daemon", 'd', N_("NUMBER"), OPTION_ARG_OPTIONAL,
-    N_("runs in daemon mode with a maximum of NUMBER children"), GRID + 1 },
-  { "url", URL_OPTION, 0, 0, N_("deliver to given URLs"), GRID + 1 },
-  { "from", 'f', N_("EMAIL"), 0,
-    N_("specify the sender's name"), GRID + 1 },
-  { NULL, 'r', NULL, OPTION_ALIAS, NULL },
-  { "lmtp", LMTP_OPTION, N_("URL"), OPTION_ARG_OPTIONAL,
-    N_("operate in LMTP mode"), GRID + 1 },
-  { "debug", 'x', N_("FLAGS"), 0,
-    N_("enable debugging"), GRID + 1 },
-  { "stderr", STDERR_OPTION, NULL, 0,
-    N_("log to standard error"), GRID + 1 },
+ { "foreground", FOREGROUND_OPTION, 0, 0, N_("remain in foreground"),
+   GRID + 1 },
+ { "inetd",  'i', 0, 0, N_("run in inetd mode"), GRID + 1 },
+ { "daemon", 'd', N_("NUMBER"), OPTION_ARG_OPTIONAL,
+   N_("runs in daemon mode with a maximum of NUMBER children"), GRID + 1 },
+ { "url", URL_OPTION, 0, 0, N_("deliver to given URLs"), GRID + 1 },
+ { "from", 'f', N_("EMAIL"), 0,
+   N_("specify the sender's name"), GRID + 1 },
+ { NULL, 'r', NULL, OPTION_ALIAS, NULL },
+ { "lmtp", LMTP_OPTION, N_("URL"), OPTION_ARG_OPTIONAL,
+   N_("operate in LMTP mode"), GRID + 1 },
+ { "debug", 'x', N_("FLAGS"), 0,
+   N_("enable debugging"), GRID + 1 },
+ { "stderr", STDERR_OPTION, NULL, 0,
+   N_("log to standard error"), GRID + 1 },
+ { "transcript", TRANSCRIPT_OPTION, NULL, 0,
+   N_("enable session transcript"), GRID + 1 },
 #undef GRID
 
 #define GRID 2
@@ -212,6 +216,10 @@ parse_opt (int key, char *arg, struct argp_state *state)
 	mu_argp_node_list_new (lst, "listen", arg);
       break;
 
+    case TRANSCRIPT_OPTION:
+      maidag_transcript = 1;
+      break;
+      
     case 'r':
     case 'f':
       if (sender_address != NULL)
diff --git a/maidag/maidag.h b/maidag/maidag.h
index 18ea2ec..aa2195f 100644
--- a/maidag/maidag.h
+++ b/maidag/maidag.h
@@ -142,6 +142,7 @@ extern char *lmtp_url_string;
 extern int reuse_lmtp_address;
 extern mu_list_t lmtp_groups;
 extern mu_acl_t maidag_acl;
+extern int maidag_transcript;
 
 void close_fds (void);
 int switch_user_id (struct mu_auth_data *auth, int user);
@@ -160,12 +161,6 @@ int deliver (mu_message_t msg, char *name, char **errp);
 int sieve_test (struct mu_auth_data *auth, mu_message_t msg);
 int check_quota (struct mu_auth_data *auth, mu_off_t size, mu_off_t *rest);
 
-struct mail_tmp;
-int mail_tmp_begin (struct mail_tmp **pmtmp, const char *from);
-int mail_tmp_add_line (struct mail_tmp *mtmp, char *buf, size_t buflen);
-int mail_tmp_finish (struct mail_tmp *mtmp, mu_mailbox_t *mbox);
-void mail_tmp_destroy (struct mail_tmp **pmtmp);
-
 enum maidag_forward_result
   {
     maidag_forward_none,
diff --git a/maidag/mailtmp.c b/maidag/mailtmp.c
deleted file mode 100644
index d981673..0000000
--- a/maidag/mailtmp.c
+++ /dev/null
@@ -1,187 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 1999, 2000, 2001, 2002, 2005, 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.
-
-   GNU Mailutils 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 GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
-
-#include "maidag.h"
-
-struct mail_tmp
-{
-  mu_stream_t stream;
-  size_t line;
-  char *tempfile;
-  const char *from;
-  int had_nl;
-};  
-
-int
-mail_tmp_begin (struct mail_tmp **pmtmp, const char *from)
-{
-  int status;
-  struct mail_tmp *mtmp = malloc (sizeof *mtmp);
-  
-  if (!mtmp)
-    return ENOMEM;
-
-  memset (mtmp, 0, sizeof *mtmp);
-
-  mtmp->tempfile = mu_tempname (NULL);
-  if ((status = mu_file_stream_create (&mtmp->stream, mtmp->tempfile,
-				       MU_STREAM_RDWR)))
-    {
-      free (mtmp);
-      maidag_error (_("unable to open temporary file: %s"),
-		    mu_strerror (status));
-      return status;
-    }
-
-  mtmp->from = from;
-  *pmtmp = mtmp;
-  return 0;
-}
-
-int
-mail_tmp_add_line (struct mail_tmp *mtmp, char *buf, size_t buflen)
-{
-  int status = 0;
-  
-  mtmp->line++;
-  if (mtmp->line == 1)
-    {
-      const char *from = mtmp->from;
-      
-      if (buflen >= 5 && memcmp (buf, "From ", 5))
-	{
-	  struct mu_auth_data *auth = NULL;
-	  if (!from)
-	    {
-	      auth = mu_get_auth_by_uid (getuid ());
-	      if (auth)
-		from = auth->name;
-	    }
-	  if (from)
-	    {
-	      time_t t;
-	      char *envs;
-	      
-	      time (&t);
-	      asprintf (&envs, "From %s %s", from, ctime (&t));
-	      status = mu_stream_write (mtmp->stream, 
-					envs,
-					strlen (envs), NULL);
-	      free (envs);
-	    }
-	  else
-	    {
-	      maidag_error (_("cannot determine sender address"));
-	      return EINVAL;
-	    }
-	  if (auth)
-	    mu_auth_data_free (auth);
-	}
-    }
-  else if (buflen >= 5 && !memcmp (buf, "From ", 5))
-    {
-      static char *escape = ">";
-      status = mu_stream_write (mtmp->stream, escape, 1, NULL);
-    }
-
-  if (!status)
-    status = mu_stream_write (mtmp->stream, buf, buflen, NULL);
-      
-  if (status)
-    {
-      maidag_error (_("error writing temporary file: %s"), 
-                    mu_strerror (status));
-      mu_stream_destroy (&mtmp->stream);
-    }
-  mtmp->had_nl = buf[buflen-1] == '\n';
-  return status;
-}
-
-int
-mail_tmp_finish (struct mail_tmp *mtmp, mu_mailbox_t *mbox)
-{
-  int status;
-  static char *newline = "\n";
-  size_t n;
-  
-  if (!mtmp->had_nl)
-    status = mu_stream_write (mtmp->stream, newline, 1, NULL);
-
-  status = mu_stream_write (mtmp->stream, newline, 1, NULL);
-  unlink (mtmp->tempfile);
-  free (mtmp->tempfile);
-  mtmp->tempfile = NULL;
-  
-  if (status)
-    {
-      errno = status;
-      maidag_error (_("error writing temporary file: %s"), 
-                    mu_strerror (status));
-      mu_stream_destroy (&mtmp->stream);
-      return status;
-    }
-
-  mu_stream_flush (mtmp->stream);
-  if ((status = mu_mailbox_create (mbox, "mbox:/dev/null")) 
-      || (status = mu_mailbox_open (*mbox, MU_STREAM_READ))
-      || (status = mu_mailbox_set_stream (*mbox, mtmp->stream)))
-    {
-      maidag_error (_("error opening temporary file: %s"), 
-                    mu_strerror (status));
-      mu_stream_destroy (&mtmp->stream);
-      return status;
-    }
-
-  status = mu_mailbox_messages_count (*mbox, &n);
-  if (status)
-    {
-      errno = status;
-      maidag_error (_("error creating temporary message: %s"),
-		    mu_strerror (status));
-      mu_stream_destroy (&mtmp->stream);
-      return status;
-    }
-
-  mtmp->stream = NULL;
-  mtmp->line = 0;
-  
-  return status;
-  
-}
-
-void
-mail_tmp_destroy (struct mail_tmp **pmtmp)
-{
-  struct mail_tmp *mtmp = *pmtmp;
-
-  if (mtmp)
-    {
-      if (mtmp->tempfile)
-	{
-	  unlink (mtmp->tempfile);
-	  free (mtmp->tempfile);
-	}
-      mu_stream_destroy (&mtmp->stream);
-      free (*pmtmp);
-      *pmtmp = NULL;
-    }
-}
-
-
-  
diff --git a/po/POTFILES.in b/po/POTFILES.in
index f600fe5..a189a7d 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -90,7 +90,6 @@ maidag/deliver.c
 maidag/lmtp.c
 maidag/maidag.c
 maidag/mailquota.c
-maidag/mailtmp.c
 maidag/script.c
 
 mail/alias.c


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Thu Oct 07 17:08:00 2010
Received: from mailman by lists.gnu.org with archive (Exim 4.43)
	id 1P3xh2-0006Qo-1g
	for mharc-commit-mailutils@gnu.org; Thu, 07 Oct 2010 17:08:00 -0400
Received: from [140.186.70.92] (port=36602 helo=eggs.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.43) id 1P3xgs-0005wh-90
	for commit-mailutils@gnu.org; Thu, 07 Oct 2010 17:07:58 -0400
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P3xUR-0007uP-0L
	for commit-mailutils@gnu.org; Thu, 07 Oct 2010 16:55:02 -0400
Received: from colonialone.fsf.org ([140.186.70.51]:54822
	helo=internal.in.savannah.gnu.org)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P3xUQ-0007uJ-Sr
	for commit-mailutils@gnu.org; Thu, 07 Oct 2010 16:54:58 -0400
Received: from [10.1.0.108] (helo=vcs-noshell.in.savannah.gnu.org)
	by internal.in.savannah.gnu.org with esmtp (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P3xUQ-0007C1-AF
	for commit-mailutils@gnu.org; Thu, 07 Oct 2010 20:54:58 +0000
Received: from gray by vcs-noshell.in.savannah.gnu.org with local (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P3xUP-00070c-S9
	for commit-mailutils@gnu.org; Thu, 07 Oct 2010 20:54:58 +0000
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: dd8d091ba3cae58fd437c664478de8bcbdf25ac6
X-Git-Newrev: 043344271a4bb538d5a044c6757ea8617bb506ad
Message-Id: <E1P3xUP-00070c-S9@vcs-noshell.in.savannah.gnu.org>
Date: Thu, 07 Oct 2010 20:54:57 +0000
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
Subject: [SCM] GNU Mailutils branch, master,
	updated. release-2.2-131-g0433442
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.5
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=unsubscribe>
List-Archive: <http://lists.gnu.org/archive/html/commit-mailutils>
List-Post: <mailto:commit-mailutils@gnu.org>
List-Help: <mailto:commit-mailutils-request@gnu.org?subject=help>
List-Subscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Thu, 07 Oct 2010 21:07:58 -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=043344271a4bb538d5a044c6757ea8617bb506ad

The branch, master has been updated
       via  043344271a4bb538d5a044c6757ea8617bb506ad (commit)
       via  72b336385867d935a2e6d098a833a35089dbf139 (commit)
      from  dd8d091ba3cae58fd437c664478de8bcbdf25ac6 (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 043344271a4bb538d5a044c6757ea8617bb506ad
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Thu Oct 7 22:56:35 2010 +0300

    Improve maidag, add testsuite.
    
    * configure.ac: Add maidag/tests
    * maidag/Makefile.am (SUBDIRS): Add tests.
    * maidag/deliver.c (make_tmp): Write a
    message delimiter.
    (is_remote_url, is_mailer_url): Remove.
    (deliver_url): Allow for URLs without user name.
    * maidag/lmtp.c (lmtp_set_privs): Empty group list
    means do not switch to any groups.
    * maidag/maidag.c (mda_mode): New variable.
    (options): New option --mda.
    (cb_group, cb2_group): Create the list even if it is empty.
    (main): Handle --mda.
    
    * maidag/tests/.gitignore: New file.
    * maidag/tests/Makefile.am: New file.
    * maidag/tests/atlocal.in: New file.
    * maidag/tests/input.msg: New file.
    * maidag/tests/lmtp.at: New file.
    * maidag/tests/mda.at: New file.
    * maidag/tests/testsuite.at: New file.
    * maidag/tests/url-mbox.at: New file.
    * maidag/tests/input.msg: New file.

commit 72b336385867d935a2e6d098a833a35089dbf139
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Thu Oct 7 18:19:27 2010 +0300

    Fix a silly copy-paste error.

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

Summary of changes:
 configure.ac                                       |    3 +
 frm/tests/testsuite.at                             |    1 -
 libmailutils/tests/address.at                      |    1 -
 libmailutils/tests/argcv.at                        |    1 -
 libmailutils/tests/base64d.at                      |    1 -
 libmailutils/tests/base64e.at                      |    1 -
 libmailutils/tests/decode2047.at                   |    1 -
 libmailutils/tests/encode2047.at                   |    1 -
 libmailutils/tests/fromflt.at                      |    1 -
 libmailutils/tests/list.at                         |    1 -
 libmailutils/tests/mailcap.at                      |    1 -
 libmailutils/tests/testsuite.at                    |    1 -
 libmailutils/tests/url.at                          |    1 -
 maidag/Makefile.am                                 |    6 ++-
 maidag/deliver.c                                   |   37 ++-----------
 maidag/lmtp.c                                      |    9 +++-
 maidag/maidag.c                                    |   31 ++++++++---
 {messages => maidag}/tests/.gitignore              |    0
 {frm => maidag}/tests/Makefile.am                  |    8 ++-
 {messages => maidag}/tests/atlocal.in              |    2 +-
 maidag/tests/input.msg                             |   44 +++++++++++++++
 maidag/tests/lmtp.at                               |   58 ++++++++++++++++++++
 maidag/tests/mda.at                                |   50 +++++++++++++++++
 {testsuite => maidag/tests}/testsuite.at           |   17 +++++--
 testsuite/testsuite.at => maidag/tests/url-mbox.at |   23 ++++++--
 messages/tests/testsuite.at                        |    1 -
 readmsg/tests/all.at                               |    1 -
 readmsg/tests/hdr.at                               |    3 +-
 readmsg/tests/nohdr.at                             |    1 -
 readmsg/tests/testsuite.at                         |    1 -
 readmsg/tests/twomsg.at                            |    1 -
 readmsg/tests/weed.at                              |    3 +-
 sieve/tests/action.at                              |    1 -
 sieve/tests/address.at                             |    1 -
 sieve/tests/allof.at                               |    1 -
 sieve/tests/anyof.at                               |    1 -
 sieve/tests/compile.at                             |    1 -
 sieve/tests/envelope.at                            |    1 -
 sieve/tests/exists.at                              |    1 -
 sieve/tests/ext.at                                 |    1 -
 sieve/tests/false.at                               |    1 -
 sieve/tests/header.at                              |    1 -
 sieve/tests/i-casemap.at                           |    1 -
 sieve/tests/i-numeric.at                           |    1 -
 sieve/tests/i-octet.at                             |    1 -
 sieve/tests/mul-addr.at                            |    1 -
 sieve/tests/not.at                                 |    1 -
 sieve/tests/redirect.at                            |    1 -
 sieve/tests/reject.at                              |    1 -
 sieve/tests/relational.at                          |    1 -
 sieve/tests/size.at                                |    1 -
 sieve/tests/testsuite.at                           |    1 -
 sieve/tests/true.at                                |    1 -
 sieve/tests/version.at                             |    1 -
 testsuite/mbdel.at                                 |    1 -
 testsuite/mime.at                                  |    1 -
 testsuite/testsuite.at                             |    3 +-
 57 files changed, 233 insertions(+), 105 deletions(-)
 copy {messages => maidag}/tests/.gitignore (100%)
 copy {frm => maidag}/tests/Makefile.am (94%)
 copy {messages => maidag}/tests/atlocal.in (72%)
 create mode 100644 maidag/tests/input.msg
 create mode 100644 maidag/tests/lmtp.at
 create mode 100644 maidag/tests/mda.at
 copy {testsuite => maidag/tests}/testsuite.at (65%)
 copy testsuite/testsuite.at => maidag/tests/url-mbox.at (58%)

diff --git a/configure.ac b/configure.ac
index 796c468..2068a9a 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1214,6 +1214,7 @@ AC_ARG_WITH([mh-bindir],
 AC_CONFIG_TESTDIR(libmailutils/tests)
 AC_CONFIG_TESTDIR(testsuite)
 AC_CONFIG_TESTDIR(frm/tests)
+AC_CONFIG_TESTDIR(maidag/tests)
 AC_CONFIG_TESTDIR(messages/tests)
 AC_CONFIG_TESTDIR(readmsg/tests)
 AC_CONFIG_TESTDIR(sieve/tests)
@@ -1224,6 +1225,8 @@ AC_CONFIG_FILES([libmailutils/tests/Makefile
                  testsuite/atlocal
                  frm/tests/Makefile
                  frm/tests/atlocal
+		 maidag/tests/Makefile
+		 maidag/tests/atlocal
 		 messages/tests/Makefile
 		 messages/tests/atlocal
 		 readmsg/tests/Makefile
diff --git a/frm/tests/testsuite.at b/frm/tests/testsuite.at
index c55f3a6..1a3e6f0 100644
--- a/frm/tests/testsuite.at
+++ b/frm/tests/testsuite.at
@@ -13,7 +13,6 @@
 #
 # You should have received a copy of the GNU General Public License
 # along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
-# This file is part of Mailfromd testsuite. 
 
 m4_include([testsuite.inc])
 
diff --git a/libmailutils/tests/address.at b/libmailutils/tests/address.at
index c62dc22..0a2bd26 100644
--- a/libmailutils/tests/address.at
+++ b/libmailutils/tests/address.at
@@ -13,7 +13,6 @@
 #
 # You should have received a copy of the GNU General Public License
 # along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
-# This file is part of Mailfromd testsuite. 
 
 dnl ------------------------------------------------------------
 dnl TESTADDR(ADDR, [KW = `'], [STATUS = `0'], [STDOUT = `'],
diff --git a/libmailutils/tests/argcv.at b/libmailutils/tests/argcv.at
index 9ef8652..ca932e5 100644
--- a/libmailutils/tests/argcv.at
+++ b/libmailutils/tests/argcv.at
@@ -13,7 +13,6 @@
 #
 # You should have received a copy of the GNU General Public License
 # along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
-# This file is part of Mailfromd testsuite. 
 
 dnl ------------------------------------------------------------
 dnl TESTARGS([NAME], [KW = `'], [INPUT], [STDOUT = `'],
diff --git a/libmailutils/tests/base64d.at b/libmailutils/tests/base64d.at
index df1efd3..9b68e3e 100644
--- a/libmailutils/tests/base64d.at
+++ b/libmailutils/tests/base64d.at
@@ -13,7 +13,6 @@
 #
 # You should have received a copy of the GNU General Public License
 # along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
-# This file is part of Mailfromd testsuite. 
 
 AT_SETUP([base64 decoding (read)])
 AT_KEYWORDS([base64 base64d base64dr decode filter])
diff --git a/libmailutils/tests/base64e.at b/libmailutils/tests/base64e.at
index b94522e..0799fcf 100644
--- a/libmailutils/tests/base64e.at
+++ b/libmailutils/tests/base64e.at
@@ -13,7 +13,6 @@
 #
 # You should have received a copy of the GNU General Public License
 # along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
-# This file is part of Mailfromd testsuite. 
 
 AT_SETUP([base64 encoding (read)])
 AT_KEYWORDS([base64 base64e base64er encode filter])
diff --git a/libmailutils/tests/decode2047.at b/libmailutils/tests/decode2047.at
index c33e1fb..2a5fa38 100644
--- a/libmailutils/tests/decode2047.at
+++ b/libmailutils/tests/decode2047.at
@@ -13,7 +13,6 @@
 #
 # You should have received a copy of the GNU General Public License
 # along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
-# This file is part of Mailfromd testsuite. 
 
 dnl ------------------------------------------------------------
 dnl TESTDEC2047([NAME], [KW = `'], [INPUT], [STDOUT = `'],
diff --git a/libmailutils/tests/encode2047.at b/libmailutils/tests/encode2047.at
index 3b914e6..ed59672 100644
--- a/libmailutils/tests/encode2047.at
+++ b/libmailutils/tests/encode2047.at
@@ -13,7 +13,6 @@
 #
 # You should have received a copy of the GNU General Public License
 # along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
-# This file is part of Mailfromd testsuite. 
 
 AT_SETUP([encode2047])
 AT_KEYWORDS([encode])
diff --git a/libmailutils/tests/fromflt.at b/libmailutils/tests/fromflt.at
index a5b3313..e03bf11 100644
--- a/libmailutils/tests/fromflt.at
+++ b/libmailutils/tests/fromflt.at
@@ -13,7 +13,6 @@
 #
 # You should have received a copy of the GNU General Public License
 # along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
-# This file is part of Mailfromd testsuite. 
 
 # -------------------------------------------
 # Data for 'From' filter tests.
diff --git a/libmailutils/tests/list.at b/libmailutils/tests/list.at
index 2309120..093a9d4 100644
--- a/libmailutils/tests/list.at
+++ b/libmailutils/tests/list.at
@@ -13,7 +13,6 @@
 #
 # You should have received a copy of the GNU General Public License
 # along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
-# This file is part of Mailfromd testsuite. 
 
 dnl ------------------------------------------------------------
 dnl TESTLIST([NAME], [KW = `'], [INPUT], [STDOUT = `'],
diff --git a/libmailutils/tests/mailcap.at b/libmailutils/tests/mailcap.at
index ef071c6..e3673db 100644
--- a/libmailutils/tests/mailcap.at
+++ b/libmailutils/tests/mailcap.at
@@ -13,7 +13,6 @@
 #
 # You should have received a copy of the GNU General Public License
 # along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
-# This file is part of Mailfromd testsuite. 
 
 dnl ------------------------------------------------------------
 dnl MAILCAP([NAME], [KW = `'], [INPUT], [STDOUT = `'],
diff --git a/libmailutils/tests/testsuite.at b/libmailutils/tests/testsuite.at
index fbdc7c9..46e04b3 100644
--- a/libmailutils/tests/testsuite.at
+++ b/libmailutils/tests/testsuite.at
@@ -13,7 +13,6 @@
 #
 # You should have received a copy of the GNU General Public License
 # along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
-# This file is part of Mailfromd testsuite. 
 
 m4_include([testsuite.inc])
 
diff --git a/libmailutils/tests/url.at b/libmailutils/tests/url.at
index c22d998..010d328 100644
--- a/libmailutils/tests/url.at
+++ b/libmailutils/tests/url.at
@@ -13,7 +13,6 @@
 #
 # You should have received a copy of the GNU General Public License
 # along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
-# This file is part of Mailfromd testsuite. 
 
 dnl ------------------------------------------------------------
 dnl TESTURL([NAME], [KW = `'], [INPUT], [STDOUT = `'],
diff --git a/maidag/Makefile.am b/maidag/Makefile.am
index 835f895..50fb709 100644
--- a/maidag/Makefile.am
+++ b/maidag/Makefile.am
@@ -15,8 +15,7 @@
 # Foundation, Inc.  51 Franklin Street, Fifth Floor, Boston, MA
 # 02110-1301 USA
 
-INCLUDES = -I${top_srcdir} @MU_APP_COMMON_INCLUDES@ @GUILE_INCLUDES@ \
- @PYTHON_INCLUDES@
+SUBDIRS = . tests
 
 sbin_PROGRAMS=maidag
 maidag_SOURCES=\
@@ -51,6 +50,9 @@ maidag_LDADD = \
  @MU_COMMON_LIBRARIES@\
  @TCPWRAP_LIBRARIES@
 
+INCLUDES = -I${top_srcdir} @MU_APP_COMMON_INCLUDES@ @GUILE_INCLUDES@ \
+ @PYTHON_INCLUDES@
+
 install-exec-hook:
 	for i in $(sbin_PROGRAMS); do\
 		chown root:mail $(DESTDIR)$(sbindir)/$$i;\
diff --git a/maidag/deliver.c b/maidag/deliver.c
index d5bbc1f..cd211d8 100644
--- a/maidag/deliver.c
+++ b/maidag/deliver.c
@@ -90,7 +90,9 @@ make_tmp (const char *from)
   free (buf);
   
   rc = mu_stream_copy (out, in, 0, NULL);
-  
+  if (rc == 0)
+    /* Write out message delimiter */
+    mu_stream_write (out, "\n", 1, NULL);
   mu_stream_destroy (&in);
   if (rc)
     {
@@ -347,26 +349,6 @@ deliver_to_user (mu_mailbox_t mbox, mu_message_t msg,
   return failed ? exit_code : 0;
 }
 
-static int
-is_remote_url (mu_url_t url)
-{
-  const char *scheme;
-  int rc = mu_url_sget_scheme (url, &scheme);
-  return rc == 0 && strncmp (scheme, "remote+", 7) == 0;
-}
-
-static int
-is_mailer_url (mu_url_t url)
-{
-  mu_record_t record = NULL;
-  int (*pfn) (mu_mailer_t) = NULL;
-  
-  return mu_registrar_lookup_url (url, MU_FOLDER_ATTRIBUTE_FILE,
-				  &record, NULL) == 0
-         && mu_record_get_mailer (record, &pfn) == 0
-         && pfn;
-}
-
 int
 deliver_url (mu_url_t url, mu_message_t msg, const char *name, char **errp)
 {
@@ -491,18 +473,7 @@ deliver (mu_message_t msg, char *dest_id, char **errp)
 	}
       status = mu_url_sget_user (url, &name);
       if (status == MU_ERR_NOENT)
-	{
-	  if (!is_mailer_url (url) && !is_remote_url (url))
-	    {
-	      maidag_error (_("no user name"));
-	      if (errp)
-		asprintf (errp, "no such user");
-	      exit_code = EX_NOUSER;
-	      return EX_NOUSER;
-	    }
-	  else
-	    name = NULL;
-	}
+	name = NULL;
       else if (status)
 	{
 	  maidag_error (_("%s: cannot get user name from url: %s"),
diff --git a/maidag/lmtp.c b/maidag/lmtp.c
index 726be15..eca152d 100644
--- a/maidag/lmtp.c
+++ b/maidag/lmtp.c
@@ -607,7 +607,14 @@ lmtp_set_privs ()
       mu_iterator_t itr;
       int rc;
 
-      mu_list_count (lmtp_groups, &size);
+      rc = mu_list_count (lmtp_groups, &size);
+      if (rc)
+	{
+	  mu_diag_funcall (MU_DIAG_ERROR, "mu_list_count", NULL, rc);
+	  return EX_UNAVAILABLE;
+	}
+      if (size == 0)
+	return 0; /* nothing to do */
       gidset = calloc (size, sizeof (gidset[0]));
       if (!gidset)
 	{
diff --git a/maidag/maidag.c b/maidag/maidag.c
index ff5c34b..e1bebc0 100644
--- a/maidag/maidag.c
+++ b/maidag/maidag.c
@@ -18,6 +18,8 @@
 
 #include "maidag.h"
 
+int mda_mode;              /* Force local MDA mode even if not started as
+			      root */ 
 int multiple_delivery;     /* Don't return errors when delivering to multiple
 			      recipients */
 int ex_quota_tempfail;     /* Return temporary failure if mailbox quota is
@@ -74,6 +76,7 @@ static char args_doc[] = N_("[recipient...]");
 #define FOREGROUND_OPTION 260
 #define URL_OPTION 261
 #define TRANSCRIPT_OPTION 262
+#define MDA_OPTION 263
 
 static struct argp_option options[] = 
 {
@@ -87,6 +90,8 @@ static struct argp_option options[] =
  { "daemon", 'd', N_("NUMBER"), OPTION_ARG_OPTIONAL,
    N_("runs in daemon mode with a maximum of NUMBER children"), GRID + 1 },
  { "url", URL_OPTION, 0, 0, N_("deliver to given URLs"), GRID + 1 },
+ { "mda", MDA_OPTION, 0, 0, N_("force MDA mode even if not started as root"),
+   GRID + 1 },
  { "from", 'f', N_("EMAIL"), 0,
    N_("specify the sender's name"), GRID + 1 },
  { NULL, 'r', NULL, OPTION_ALIAS, NULL },
@@ -216,6 +221,10 @@ parse_opt (int key, char *arg, struct argp_state *state)
 	mu_argp_node_list_new (lst, "listen", arg);
       break;
 
+    case MDA_OPTION:
+      mda_mode = 1;
+      break;
+      
     case TRANSCRIPT_OPTION:
       maidag_transcript = 1;
       break;
@@ -292,23 +301,25 @@ cb_debug (mu_debug_t debug, void *data, mu_config_value_t *val)
 static int
 cb2_group (mu_debug_t debug, const char *gname, void *data)
 {
-  mu_list_t *plist = data;
+  mu_list_t list = data;
   struct group *group;
 
-  if (!*plist)
-    mu_list_create (plist);
   group = getgrnam (gname);
   if (!group)
     mu_cfg_format_error (debug, MU_DEBUG_ERROR, _("unknown group: %s"), gname);
   else
-    mu_list_append (*plist, (void*)group->gr_gid);
+    mu_list_append (list, (void*)group->gr_gid);
   return 0;
 }
   
 static int
 cb_group (mu_debug_t debug, void *data, mu_config_value_t *arg)
 {
-  return mu_cfg_string_value_cb (debug, arg, cb2_group, data);
+  mu_list_t *plist = data;
+
+  if (!*plist)
+    mu_list_create (plist);
+  return mu_cfg_string_value_cb (debug, arg, cb2_group, *plist);
 }
 
 static struct mu_kwd forward_checks[] = {
@@ -540,9 +551,11 @@ main (int argc, char *argv[])
     exit (EX_CONFIG);
 
   current_uid = getuid ();
-
+  if (!lmtp_mode && current_uid == 0)
+    mda_mode = 1; 
+  
   if (log_to_stderr == -1)
-    log_to_stderr = url_option || (!lmtp_mode && (current_uid != 0));
+    log_to_stderr = url_option || (!lmtp_mode && !mda_mode);
   
   mu_diag_get_debug (&debug);
   if (!log_to_stderr)
@@ -571,7 +584,7 @@ main (int argc, char *argv[])
     }
   else 
     {
-      if (current_uid)
+      if (!mda_mode)
 	{
 	  if (url_option)
 	    {
@@ -582,7 +595,7 @@ main (int argc, char *argv[])
 	      static char *s_argv[2];
 	      struct mu_auth_data *auth = mu_get_auth_by_uid (current_uid);
 	      
-	      if (!current_uid)
+	      if (!auth)
 		{
 		  mu_error (_("cannot get username"));
 		  return EX_UNAVAILABLE;
diff --git a/messages/tests/.gitignore b/maidag/tests/.gitignore
similarity index 100%
copy from messages/tests/.gitignore
copy to maidag/tests/.gitignore
diff --git a/frm/tests/Makefile.am b/maidag/tests/Makefile.am
similarity index 94%
copy from frm/tests/Makefile.am
copy to maidag/tests/Makefile.am
index b548c51..65abc82 100644
--- a/frm/tests/Makefile.am
+++ b/maidag/tests/Makefile.am
@@ -14,7 +14,7 @@
 # You should have received a copy of the GNU General Public License
 # along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
-EXTRA_DIST = $(TESTSUITE_AT) testsuite package.m4 
+EXTRA_DIST = $(TESTSUITE_AT) testsuite package.m4 input.msg
 DISTCLEANFILES       = atconfig $(check_SCRIPTS)
 MAINTAINERCLEANFILES = Makefile.in $(TESTSUITE)
 
@@ -38,7 +38,11 @@ $(srcdir)/package.m4: $(top_srcdir)/configure.ac
 ## Test suite.  ##
 ## ------------ ##
 
-TESTSUITE_AT = testsuite.at
+TESTSUITE_AT = \
+ lmtp.at\
+ mda.at\
+ testsuite.at\
+ url-mbox.at
 
 TESTSUITE = $(srcdir)/testsuite
 M4=m4
diff --git a/messages/tests/atlocal.in b/maidag/tests/atlocal.in
similarity index 72%
copy from messages/tests/atlocal.in
copy to maidag/tests/atlocal.in
index 98f8248..1fa4ce6 100644
--- a/messages/tests/atlocal.in
+++ b/maidag/tests/atlocal.in
@@ -2,5 +2,5 @@
 # Configurable variable values for Mailutils test suite.
 # Copyright (C) 2004, 2010 Free Software Foundation, Inc.
 
-PATH=@abs_builddir@:@abs_top_builddir@/messages:$top_srcdir:$srcdir:$PATH
+PATH=@abs_builddir@:@abs_top_builddir@/maidag:$top_srcdir:$srcdir:$PATH
   
diff --git a/maidag/tests/input.msg b/maidag/tests/input.msg
new file mode 100644
index 0000000..e72ad7b
--- /dev/null
+++ b/maidag/tests/input.msg
@@ -0,0 +1,44 @@
+From: "Lemuel Gulliver, esq." <gulliver@example.net>
+To: root
+Subject: Travels into several remote Nations of the World
+
+A LETTER from Capt. Gulliver, to his Cousin Sympson 
+
+I HOPE you will be ready to own publickly, whenever you shall be called 
+to it, that by your great and frequent Urgency you prevailed on me to 
+publish a very loose and uncorrect Account of my Travels; with Direction 
+to hire some young Gentlemen of either University to put them in Order, 
+and correct the Style, as my Cousin Dampier did by my Advice, in his 
+Book called A Voyage round the World. But I do not remember I gave you 
+Power to consent that any thing should be omitted, and much less that any 
+thing should be inserted: therefore, as to the latter, I do here renounce 
+every thing of that Kind; particularly a Paragraph about her Majesty the 
+late Queen Anne, of most pious and glorious Memory; although I did 
+reverence and esteem her more than any of human Species. But you, or 
+your Interpolator, ought to have considered, that as it was not my 
+Inclination, so was it not decent to praise any Animal of our Composition 
+before my Master Houyhnhnm: And besides the Fact was altogether false; 
+for to my Knowledge, being in England during some Part of her Majesty's 
+Reign, she did govern by a chief Minister; nay, even by two successively; 
+the first whereof was the Lord of Godolphin, and the second the Lord of 
+Oxford; so that you have made me say the thing that was not. Likewise, in 
+the Account of the Academy of Projectors, and several Passages of my 
+Discourse to my Master Houyhnhnm, you have either omitted some 
+material Circumstances, or minced or changed them in such a Manner, that 
+I do hardly know mine own Work. When I formerly hinted to you 
+something of this in a Letter, you were pleased to answer that you were 
+afraid of giving Offense; that People in Power were very watchful over the 
+Press, and apt not only to interpret, but to punish every thing which looked 
+like an Innuendo (as I think you called it.) But pray, how could that which 
+I spoke so many Years ago, and at about five Thousand leagues distance, in 
+another Reign, be applied to any of the Yahoos who now are said to govern 
+the Herd; especially at a time when I little thought on or feared the 
+Unhappiness of living under them. Have not I the most reason to complain, 
+when I see these very Yahoos carried by Houyhnhnms in a Vehicle, as if 
+these were Brutes, and those the rational Creatures? And indeed, to avoid 
+so monstrous and detestable a Sight was one principal Motive of my 
+Retirement hither. 
+
+Thus much I thought proper to tell you in Relation to yourself, and to the 
+Trust I reposed in you. 
+
diff --git a/maidag/tests/lmtp.at b/maidag/tests/lmtp.at
new file mode 100644
index 0000000..2cba9e7
--- /dev/null
+++ b/maidag/tests/lmtp.at
@@ -0,0 +1,58 @@
+# This file is part of GNU Mailutils. -*- Autotest -*-
+# Copyright (C) 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.
+#
+# GNU Mailutils 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 GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
+
+AT_SETUP([LMTP mode])
+AT_KEYWORDS([maidag lmtp])
+
+m4_pushdef([tocrlf],[dnl
+$abs_top_builddir/libmailutils/tests/fltst crlf encode read])
+
+AT_CHECK([
+AT_DATA([session_start],[LHLO localhost
+MAIL FROM:<gulliver@example.net>
+RCPT TO:<root@localhost>
+DATA
+])
+AT_DATA([session_end],[.
+QUIT
+])
+
+echo ENVELOPE > expout
+cat $abs_top_srcdir/maidag/tests/input.msg >> expout
+echo "" >> expout
+
+cat session_start $abs_top_srcdir/maidag/tests/input.msg session_end | tocrlf > session || exit $?
+
+mkdir spool
+maidag MAIDAG_OPTIONS --set 'group=()' --lmtp < session >transcript || exit $?
+
+sed '1s/From gulliver@example.net.*/ENVELOPE/' spool/root
+cat transcript | tr -d '\r' | sed '/...-/d;s/ .*//' >&2
+],
+[0],
+[expout],
+[220
+250
+250
+250
+354
+250
+221
+])
+
+AT_CLEANUP
+
+m4_popdef([tocrlf])
diff --git a/maidag/tests/mda.at b/maidag/tests/mda.at
new file mode 100644
index 0000000..a871eaa
--- /dev/null
+++ b/maidag/tests/mda.at
@@ -0,0 +1,50 @@
+# This file is part of GNU Mailutils. -*- Autotest -*-
+# Copyright (C) 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.
+#
+# GNU Mailutils 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 GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
+
+AT_SETUP([MDA mode])
+AT_KEYWORDS([maidag mda0])
+
+AT_CHECK([
+echo ENVELOPE > expout
+cat $abs_top_srcdir/maidag/tests/input.msg >> expout
+echo "" >> expout
+mkdir spool
+maidag MAIDAG_OPTIONS --mda --from gulliver@example.net root < dnl
+ $abs_top_srcdir/maidag/tests/input.msg || exit $?
+sed '1s/From gulliver@example.net.*/ENVELOPE/' spool/root
+],
+[0],
+[expout])
+
+AT_CLEANUP
+
+#
+AT_SETUP([MDA mode, explicit envelope])
+AT_KEYWORDS([maidag mda1])
+
+AT_CHECK([
+echo "From gulliver Thu Oct  7 19:10:57 2010" > expout
+cat $abs_top_srcdir/maidag/tests/input.msg >> expout
+mkdir spool
+maidag MAIDAG_OPTIONS --mda --from gulliver@example.net root < expout || exit $?
+echo "" >> expout
+cat spool/root
+],
+[0],
+[expout])
+
+AT_CLEANUP
+
diff --git a/testsuite/testsuite.at b/maidag/tests/testsuite.at
similarity index 65%
copy from testsuite/testsuite.at
copy to maidag/tests/testsuite.at
index e9dde07..b56d63d 100644
--- a/testsuite/testsuite.at
+++ b/maidag/tests/testsuite.at
@@ -6,18 +6,27 @@
 # 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
+# GNU Mailutils 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 GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
-# This file is part of Mailfromd testsuite. 
 
 m4_include([testsuite.inc])
 
+m4_define([MAIDAG_OPTIONS],[dnl
+--no-site-config dnl
+--no-user-config dnl
+--set '|mailbox|mailbox-pattern='`pwd`/spool/'${user}' dnl
+--set .auth.authorization=system dnl
+--stderr])
+
 AT_INIT
+AT_TESTED([maidag])
+MUT_VERSION(maidag)
+m4_include([mda.at])
+m4_include([lmtp.at])
+m4_include([url-mbox.at])
 
-m4_include([mime.at])
-m4_include([mbdel.at])
\ No newline at end of file
diff --git a/testsuite/testsuite.at b/maidag/tests/url-mbox.at
similarity index 58%
copy from testsuite/testsuite.at
copy to maidag/tests/url-mbox.at
index e9dde07..dd29ab4 100644
--- a/testsuite/testsuite.at
+++ b/maidag/tests/url-mbox.at
@@ -6,18 +6,29 @@
 # 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
+# GNU Mailutils 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 GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
-# This file is part of Mailfromd testsuite. 
 
-m4_include([testsuite.inc])
+AT_SETUP([URL mode])
+AT_KEYWORDS([maidag url-mbox url])
 
-AT_INIT
+AT_CHECK([
+echo ENVELOPE > expout
+cat $abs_top_srcdir/maidag/tests/input.msg >> expout
+echo "" >> expout
+mkdir spool
 
-m4_include([mime.at])
-m4_include([mbdel.at])
\ No newline at end of file
+maidag MAIDAG_OPTIONS --from 'gulliver@example.net' --url mbox:spool/out < dnl
+ $abs_top_srcdir/maidag/tests/input.msg || exit $?
+sed '1s/From gulliver@example.net.*/ENVELOPE/' spool/out
+],
+[0],
+[expout])
+
+AT_CLEANUP
+ 
\ No newline at end of file
diff --git a/messages/tests/testsuite.at b/messages/tests/testsuite.at
index 04ff126..627b1bb 100644
--- a/messages/tests/testsuite.at
+++ b/messages/tests/testsuite.at
@@ -13,7 +13,6 @@
 #
 # You should have received a copy of the GNU General Public License
 # along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
-# This file is part of Mailfromd testsuite. 
 
 m4_include([testsuite.inc])
 
diff --git a/readmsg/tests/all.at b/readmsg/tests/all.at
index 741d37d..67ab085 100644
--- a/readmsg/tests/all.at
+++ b/readmsg/tests/all.at
@@ -13,7 +13,6 @@
 #
 # You should have received a copy of the GNU General Public License
 # along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
-# This file is part of Mailfromd testsuite. 
 
 READMSGTEST([readmsg *],[readmsg00],
 ['*'],
diff --git a/readmsg/tests/hdr.at b/readmsg/tests/hdr.at
index ffbcff5..08395c7 100644
--- a/readmsg/tests/hdr.at
+++ b/readmsg/tests/hdr.at
@@ -13,7 +13,6 @@
 #
 # You should have received a copy of the GNU General Public License
 # along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
-# This file is part of Mailfromd testsuite. 
 
 READMSGTEST([readmsg -h],[hdr],
 [readmsg -h SOMETHING],
@@ -34,4 +33,4 @@ exactly know what they are!  However, SOMEBODY killed SOMETHING:
 that's clear, at any rate...
 
 
-])
\ No newline at end of file
+])
diff --git a/readmsg/tests/nohdr.at b/readmsg/tests/nohdr.at
index fb61e39..6ddf5e9 100644
--- a/readmsg/tests/nohdr.at
+++ b/readmsg/tests/nohdr.at
@@ -13,7 +13,6 @@
 #
 # You should have received a copy of the GNU General Public License
 # along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
-# This file is part of Mailfromd testsuite. 
 
 READMSGTEST([readmsg -n],[nohdr],
 [readmsg -n SOMETHING],
diff --git a/readmsg/tests/testsuite.at b/readmsg/tests/testsuite.at
index e905637..49305aa 100644
--- a/readmsg/tests/testsuite.at
+++ b/readmsg/tests/testsuite.at
@@ -13,7 +13,6 @@
 #
 # You should have received a copy of the GNU General Public License
 # along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
-# This file is part of Mailfromd testsuite. 
 
 # Require a reasonably recent autotest.
 m4_include([testsuite.inc])
diff --git a/readmsg/tests/twomsg.at b/readmsg/tests/twomsg.at
index a459927..577af51 100644
--- a/readmsg/tests/twomsg.at
+++ b/readmsg/tests/twomsg.at
@@ -13,7 +13,6 @@
 #
 # You should have received a copy of the GNU General Public License
 # along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
-# This file is part of Mailfromd testsuite. 
 
 READMSGTEST([readmsg 1 2],[twomsg],
 [1 2],
diff --git a/readmsg/tests/weed.at b/readmsg/tests/weed.at
index 469cf1a..bf2c1fc 100644
--- a/readmsg/tests/weed.at
+++ b/readmsg/tests/weed.at
@@ -13,7 +13,6 @@
 #
 # You should have received a copy of the GNU General Public License
 # along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
-# This file is part of Mailfromd testsuite. 
 
 READMSGTEST([readmsg -w],[weed],
 [-w date,subject SOMETHING],
@@ -27,4 +26,4 @@ exactly know what they are!  However, SOMEBODY killed SOMETHING:
 that's clear, at any rate...
 
 
-])
\ No newline at end of file
+])
diff --git a/sieve/tests/action.at b/sieve/tests/action.at
index d56f566..2da7723 100644
--- a/sieve/tests/action.at
+++ b/sieve/tests/action.at
@@ -13,7 +13,6 @@
 #
 # You should have received a copy of the GNU General Public License
 # along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
-# This file is part of Mailfromd testsuite. 
 
 MUT_TESTCASE([action stop], [], [stop;],[],[0],[],
 [STOP on msg uid 1
diff --git a/sieve/tests/address.at b/sieve/tests/address.at
index 36c5d14..88499dd 100644
--- a/sieve/tests/address.at
+++ b/sieve/tests/address.at
@@ -13,7 +13,6 @@
 #
 # You should have received a copy of the GNU General Public License
 # along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
-# This file is part of Mailfromd testsuite. 
 
 MUT_TESTCASE([address :all],[test address all address-all],
 [
diff --git a/sieve/tests/allof.at b/sieve/tests/allof.at
index 5f32395..369d32a 100644
--- a/sieve/tests/allof.at
+++ b/sieve/tests/allof.at
@@ -13,7 +13,6 @@
 #
 # You should have received a copy of the GNU General Public License
 # along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
-# This file is part of Mailfromd testsuite. 
 
 MUT_TESTCASE([allof 00],[test allof allof00],
 [
diff --git a/sieve/tests/anyof.at b/sieve/tests/anyof.at
index 1f994c1..ba4e9a1 100644
--- a/sieve/tests/anyof.at
+++ b/sieve/tests/anyof.at
@@ -13,7 +13,6 @@
 #
 # You should have received a copy of the GNU General Public License
 # along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
-# This file is part of Mailfromd testsuite. 
 
 MUT_TESTCASE([anyof 00],[test anyof anyof00],
 [
diff --git a/sieve/tests/compile.at b/sieve/tests/compile.at
index 221054b..8bdab9c 100644
--- a/sieve/tests/compile.at
+++ b/sieve/tests/compile.at
@@ -13,7 +13,6 @@
 #
 # You should have received a copy of the GNU General Public License
 # along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
-# This file is part of Mailfromd testsuite. 
 
 testcompile([ex-null.sv])
 testcompile([box.sv])
diff --git a/sieve/tests/envelope.at b/sieve/tests/envelope.at
index a12f0ae..375accc 100644
--- a/sieve/tests/envelope.at
+++ b/sieve/tests/envelope.at
@@ -13,7 +13,6 @@
 #
 # You should have received a copy of the GNU General Public License
 # along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
-# This file is part of Mailfromd testsuite. 
 
 MUT_TESTCASE([envelope],[test envelope],
 [
diff --git a/sieve/tests/exists.at b/sieve/tests/exists.at
index 1fad0a3..41095d9 100644
--- a/sieve/tests/exists.at
+++ b/sieve/tests/exists.at
@@ -13,7 +13,6 @@
 #
 # You should have received a copy of the GNU General Public License
 # along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
-# This file is part of Mailfromd testsuite. 
 
 MUT_TESTCASE([exists: single header],[test exists exists01],
 [
diff --git a/sieve/tests/ext.at b/sieve/tests/ext.at
index 16d846a..ab9fc92 100644
--- a/sieve/tests/ext.at
+++ b/sieve/tests/ext.at
@@ -13,7 +13,6 @@
 #
 # You should have received a copy of the GNU General Public License
 # along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
-# This file is part of Mailfromd testsuite. 
 
 AT_SETUP([loadable extensions: numaddr])
 AT_KEYWORDS([ext])
diff --git a/sieve/tests/false.at b/sieve/tests/false.at
index 0d0750a..5dae588 100644
--- a/sieve/tests/false.at
+++ b/sieve/tests/false.at
@@ -13,7 +13,6 @@
 #
 # You should have received a copy of the GNU General Public License
 # along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
-# This file is part of Mailfromd testsuite. 
 
 MUT_TESTCASE([false],[test constant false],
 [
diff --git a/sieve/tests/header.at b/sieve/tests/header.at
index bbf8c68..ecb99be 100644
--- a/sieve/tests/header.at
+++ b/sieve/tests/header.at
@@ -13,7 +13,6 @@
 #
 # You should have received a copy of the GNU General Public License
 # along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
-# This file is part of Mailfromd testsuite. 
 
 MUT_TESTCASE([header :is],[test header header-is],
 [
diff --git a/sieve/tests/i-casemap.at b/sieve/tests/i-casemap.at
index ec1b04a..3b34194 100644
--- a/sieve/tests/i-casemap.at
+++ b/sieve/tests/i-casemap.at
@@ -13,7 +13,6 @@
 #
 # You should have received a copy of the GNU General Public License
 # along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
-# This file is part of Mailfromd testsuite. 
 
 MUT_TESTCASE([i-casemap :is],[comparator i-casemap i-casemap-is],
 [
diff --git a/sieve/tests/i-numeric.at b/sieve/tests/i-numeric.at
index 14c72ff..a62ff28 100644
--- a/sieve/tests/i-numeric.at
+++ b/sieve/tests/i-numeric.at
@@ -13,7 +13,6 @@
 #
 # You should have received a copy of the GNU General Public License
 # along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
-# This file is part of Mailfromd testsuite. 
 
 MUT_TESTCASE([i-numeric :is], [comparator i-numeric is i-numeric-is],
 [
diff --git a/sieve/tests/i-octet.at b/sieve/tests/i-octet.at
index 8d4b6c2..13049ad 100644
--- a/sieve/tests/i-octet.at
+++ b/sieve/tests/i-octet.at
@@ -13,7 +13,6 @@
 #
 # You should have received a copy of the GNU General Public License
 # along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
-# This file is part of Mailfromd testsuite. 
 
 MUT_TESTCASE([i-octet :is],[comparator i-octet is i-octet-is],
 [
diff --git a/sieve/tests/mul-addr.at b/sieve/tests/mul-addr.at
index 08fb3d5..b11f4f5 100644
--- a/sieve/tests/mul-addr.at
+++ b/sieve/tests/mul-addr.at
@@ -13,7 +13,6 @@
 #
 # You should have received a copy of the GNU General Public License
 # along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
-# This file is part of Mailfromd testsuite. 
 
 m4_pushdef([MUT_SIEVE_MAILBOX],[relational.mbox])
 MUT_TESTCASE([multiple addresses],[test address mul-addr],
diff --git a/sieve/tests/not.at b/sieve/tests/not.at
index 26962f4..cf4e4c5 100644
--- a/sieve/tests/not.at
+++ b/sieve/tests/not.at
@@ -13,7 +13,6 @@
 #
 # You should have received a copy of the GNU General Public License
 # along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
-# This file is part of Mailfromd testsuite. 
 
 MUT_TESTCASE([not], [test not boolean],
 [
diff --git a/sieve/tests/redirect.at b/sieve/tests/redirect.at
index fbe43c2..37291f4 100644
--- a/sieve/tests/redirect.at
+++ b/sieve/tests/redirect.at
@@ -13,7 +13,6 @@
 #
 # You should have received a copy of the GNU General Public License
 # along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
-# This file is part of Mailfromd testsuite. 
 
 AT_SETUP([redirect])
 AT_KEYWORDS([action])
diff --git a/sieve/tests/reject.at b/sieve/tests/reject.at
index 653e5a1..f1253f6 100644
--- a/sieve/tests/reject.at
+++ b/sieve/tests/reject.at
@@ -13,7 +13,6 @@
 #
 # You should have received a copy of the GNU General Public License
 # along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
-# This file is part of Mailfromd testsuite. 
 
 AT_SETUP([reject])
 AT_KEYWORDS([action])
diff --git a/sieve/tests/relational.at b/sieve/tests/relational.at
index ddca148..c68ab22 100644
--- a/sieve/tests/relational.at
+++ b/sieve/tests/relational.at
@@ -13,7 +13,6 @@
 #
 # You should have received a copy of the GNU General Public License
 # along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
-# This file is part of Mailfromd testsuite. 
 
 m4_pushdef([MUT_SIEVE_MAILBOX],[relational.mbox])
 m4_pushdef([MUT_SIEVE_OPTIONS],[--set ":mailbox:folder=`pwd`" dnl
diff --git a/sieve/tests/size.at b/sieve/tests/size.at
index ab20800..e809e82 100644
--- a/sieve/tests/size.at
+++ b/sieve/tests/size.at
@@ -13,7 +13,6 @@
 #
 # You should have received a copy of the GNU General Public License
 # along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
-# This file is part of Mailfromd testsuite. 
 
 MUT_TESTCASE([size :under],[test size under size-under],
 [
diff --git a/sieve/tests/testsuite.at b/sieve/tests/testsuite.at
index c32aba6..a9ee7ce 100644
--- a/sieve/tests/testsuite.at
+++ b/sieve/tests/testsuite.at
@@ -13,7 +13,6 @@
 #
 # You should have received a copy of the GNU General Public License
 # along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
-# This file is part of Mailfromd testsuite. 
 
 m4_include([testsuite.inc])
 
diff --git a/sieve/tests/true.at b/sieve/tests/true.at
index 36e8a91..d1065de 100644
--- a/sieve/tests/true.at
+++ b/sieve/tests/true.at
@@ -13,7 +13,6 @@
 #
 # You should have received a copy of the GNU General Public License
 # along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
-# This file is part of Mailfromd testsuite. 
 
 MUT_TESTCASE([true],[test constant true],
 [
diff --git a/sieve/tests/version.at b/sieve/tests/version.at
index b4375f8..ce951a9 100644
--- a/sieve/tests/version.at
+++ b/sieve/tests/version.at
@@ -13,7 +13,6 @@
 #
 # You should have received a copy of the GNU General Public License
 # along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
-# This file is part of Mailfromd testsuite. 
 
 dnl m4_include([testsuite.inc])
 
diff --git a/testsuite/mbdel.at b/testsuite/mbdel.at
index a63e261..57dca63 100644
--- a/testsuite/mbdel.at
+++ b/testsuite/mbdel.at
@@ -13,7 +13,6 @@
 #
 # You should have received a copy of the GNU General Public License
 # along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
-# This file is part of Mailfromd testsuite. 
 
 dnl ------------------------------------------------------------
 dnl MBDEL_TEST([FORMAT], [PROG],
diff --git a/testsuite/mime.at b/testsuite/mime.at
index 3e1c1dd..ff39da6 100644
--- a/testsuite/mime.at
+++ b/testsuite/mime.at
@@ -13,7 +13,6 @@
 #
 # You should have received a copy of the GNU General Public License
 # along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
-# This file is part of Mailfromd testsuite. 
 
 AT_SETUP([mime])
 
diff --git a/testsuite/testsuite.at b/testsuite/testsuite.at
index e9dde07..5686cfa 100644
--- a/testsuite/testsuite.at
+++ b/testsuite/testsuite.at
@@ -13,11 +13,10 @@
 #
 # You should have received a copy of the GNU General Public License
 # along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
-# This file is part of Mailfromd testsuite. 
 
 m4_include([testsuite.inc])
 
 AT_INIT
 
 m4_include([mime.at])
-m4_include([mbdel.at])
\ No newline at end of file
+m4_include([mbdel.at])


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Fri Oct 08 09:04:49 2010
Received: from mailman by lists.gnu.org with archive (Exim 4.43)
	id 1P4Ccy-0002zq-KC
	for mharc-commit-mailutils@gnu.org; Fri, 08 Oct 2010 09:04:48 -0400
Received: from [140.186.70.92] (port=46460 helo=eggs.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.43) id 1P4Ccq-0002v2-VO
	for commit-mailutils@gnu.org; Fri, 08 Oct 2010 09:04:46 -0400
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P4Ccm-0002Fg-TN
	for commit-mailutils@gnu.org; Fri, 08 Oct 2010 09:04:40 -0400
Received: from colonialone.fsf.org ([140.186.70.51]:50665
	helo=internal.in.savannah.gnu.org)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P4Ccm-0002FS-QK
	for commit-mailutils@gnu.org; Fri, 08 Oct 2010 09:04:36 -0400
Received: from [10.1.0.108] (helo=vcs-noshell.in.savannah.gnu.org)
	by internal.in.savannah.gnu.org with esmtp (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P4Ccm-0002As-Gy
	for commit-mailutils@gnu.org; Fri, 08 Oct 2010 13:04:36 +0000
Received: from gray by vcs-noshell.in.savannah.gnu.org with local (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P4Ccm-0005H4-AF
	for commit-mailutils@gnu.org; Fri, 08 Oct 2010 13:04:36 +0000
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: 043344271a4bb538d5a044c6757ea8617bb506ad
X-Git-Newrev: db1ee5e0af1f25405ffb35876149eb25cf7763f2
Message-Id: <E1P4Ccm-0005H4-AF@vcs-noshell.in.savannah.gnu.org>
Date: Fri, 08 Oct 2010 13:04:36 +0000
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
Subject: [SCM] GNU Mailutils branch, master,
	updated. release-2.2-136-gdb1ee5e
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.5
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=unsubscribe>
List-Archive: <http://lists.gnu.org/archive/html/commit-mailutils>
List-Post: <mailto:commit-mailutils@gnu.org>
List-Help: <mailto:commit-mailutils-request@gnu.org?subject=help>
List-Subscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Fri, 08 Oct 2010 13:04:46 -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=db1ee5e0af1f25405ffb35876149eb25cf7763f2

The branch, master has been updated
       via  db1ee5e0af1f25405ffb35876149eb25cf7763f2 (commit)
       via  0bf8123e1c49fc4ac4383ff95e26f486d6b8948b (commit)
       via  20bcdece82079305c006b956f1340391b64ee498 (commit)
       via  c9ecb01d64d9355b72545fbd40edfc0a1aeb302e (commit)
       via  51d1695e96b9a286bcffd0eb0a674ea9ee034f23 (commit)
      from  043344271a4bb538d5a044c6757ea8617bb506ad (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 db1ee5e0af1f25405ffb35876149eb25cf7763f2
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Fri Oct 8 15:20:54 2010 +0300

    maidag: improve forwarding capability, add a testcase for it.
    
    * maidag/forward.c (check_forward_permissions): Keep track of the
    processed files.  Refuse to process a file twice.
    (process_forward): Rewrite using streams instead of FILE.
    (maidag_forward): Accept absolute file names (mainly for testing).
    * maidag/maidag.c (forward_checks): New keyword "owner".
    (cb2_forward_file_checks): New keyword "none".
    (main): Require at least one recipient in MDA mode.
    * maidag/maidag.h (FWD_OWNER): New mask.
    * maidag/tests/forward.at: New file.
    * maidag/tests/Makefile.am (TESTSUITE_AT): Add forward.at
    * maidag/tests/testsuite.at: Include forward.at.

commit 0bf8123e1c49fc4ac4383ff95e26f486d6b8948b
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Fri Oct 8 15:19:41 2010 +0300

    Improve mta simulator output.
    
    * examples/mta.c (mta_send): A line number refers to the start
    of each line, not end of it.
    * mail/testsuite/mail/send.exp: Reflect this change.
    * sieve/tests/redirect.at: Likewise.
    * sieve/tests/reject.at: Likewise.

commit 20bcdece82079305c006b956f1340391b64ee498
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Fri Oct 8 10:35:31 2010 +0300

    maidag: optimize mda mode.
    
    * maidag/deliver.c (make_tmp): Return mu_message_t.
    Use mu_stream_to_message function.
    (mda): Remove.
    (maidag_stdio_delivery): Inline mda.
    * maidag/maidag.h (mda): Remove.

commit c9ecb01d64d9355b72545fbd40edfc0a1aeb302e
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Fri Oct 8 10:29:23 2010 +0300

    maidag: remove `default' mode (superseded by --mda).

commit 51d1695e96b9a286bcffd0eb0a674ea9ee034f23
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Fri Oct 8 10:17:11 2010 +0300

    Maidag cleanup
    
    * maidag/maidag.c (mda_mode,lmtp_mode,url_option): Remove.
    (maidag_mode): New variable.
    (parse_opt): The --lmtp, --url and --mda options set the delivery-mode
    configuration statement.
    (cb_delivery_mode): New callback.
    (maidag_cfg_param) <delivery-mode>: New statement.
    (main): Redo delivery mode selection.
    * maidag/maidag.h (maidag_mode): New enum.
    (maidag_mode): New extern.
    (mda_mode,lmtp_mode,url_option): Remove.
    (maidag_delivery_fn): New typedef.
    (maidag_stdio_delivery,mda): Change signature.
    (deliver_to_url, deliver_to_user): New prototypes.
    
    * maidag/deliver.c (mda): Take delivery function as a parameter.
    (maidag_stdio_delivery): Likewise.
    (deliver_to_user): Rename to deliver_to_mailbox.
    (deliver_url): Rename to do_delivery.
    (deliver): Rename to deliver_to_url.
    (deliver_to_user): New function.
    * maidag/forward.c (process_forward): Use deliver_to_user.
    * maidag/lmtp.c (dot_deliver): Likewise.

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

Summary of changes:
 examples/mta.c               |   16 ++++-
 maidag/deliver.c             |  129 +++++++++++++++---------------------------
 maidag/forward.c             |  110 +++++++++++++++++++++++++++++-------
 maidag/lmtp.c                |    2 +-
 maidag/maidag.c              |  100 ++++++++++++++++++--------------
 maidag/maidag.h              |   32 +++++++----
 maidag/tests/Makefile.am     |    1 +
 maidag/tests/testsuite.at    |    1 +
 mail/testsuite/mail/send.exp |    1 -
 sieve/tests/redirect.at      |    3 -
 sieve/tests/reject.at        |    3 -
 11 files changed, 227 insertions(+), 171 deletions(-)

diff --git a/examples/mta.c b/examples/mta.c
index 4d27774..8fb265c 100644
--- a/examples/mta.c
+++ b/examples/mta.c
@@ -326,7 +326,8 @@ mta_send (mu_message_t msg)
   mu_stream_t stream = NULL;
   size_t line;
   char *value;
-
+  int newline = 0;
+  
   value = from_address ();
   if (value)
     {
@@ -340,7 +341,7 @@ mta_send (mu_message_t msg)
 
   mu_message_get_streamref (msg, &stream);
   line = 0;
-  fprintf (diag, "%4lu: ", (unsigned long) line);
+  newline = 1;
   while (mu_stream_read (stream, buffer, sizeof buffer - 1, &n) == 0
 	 && n != 0)
     {
@@ -348,16 +349,23 @@ mta_send (mu_message_t msg)
 
       for (i = 0; i < n; i++)
 	{
+	  if (newline)
+	    {
+	      fprintf (diag, "%4lu: ", (unsigned long) line);
+	      newline = 0;
+	    }
 	  fputc (buffer[i], diag);
 	  if (buffer[i] == '\n')
 	    {
 	      line++;
-	      fprintf (diag, "%4lu: ", (unsigned long) line);
+	      newline = 1;
 	    }
 	}
     }
   mu_stream_destroy (&stream);
-  fprintf (diag, "\nEND OF MESSAGE\n");
+  if (!newline)
+    fprintf (diag, "\n(no newline at EOF)\n");
+  fprintf (diag, "END OF MESSAGE\n");
   fflush (diag);
   return 0;
 }
diff --git a/maidag/deliver.c b/maidag/deliver.c
index cd211d8..7bdf29a 100644
--- a/maidag/deliver.c
+++ b/maidag/deliver.c
@@ -19,14 +19,14 @@
 
 #include "maidag.h"
 
-static mu_mailbox_t
+static mu_message_t
 make_tmp (const char *from)
 {
   int rc;
   mu_stream_t in, out;
   char *buf = NULL;
   size_t size = 0, n;
-  mu_mailbox_t mbox;
+  mu_message_t mesg;
   
   rc = mu_stdio_stream_create (&in, MU_STDIN_FD, MU_STREAM_READ);
   if (rc)
@@ -90,9 +90,6 @@ make_tmp (const char *from)
   free (buf);
   
   rc = mu_stream_copy (out, in, 0, NULL);
-  if (rc == 0)
-    /* Write out message delimiter */
-    mu_stream_write (out, "\n", 1, NULL);
   mu_stream_destroy (&in);
   if (rc)
     {
@@ -101,63 +98,32 @@ make_tmp (const char *from)
       exit (EX_TEMPFAIL);
     }
 
-  mu_stream_flush (out);
-  if ((rc = mu_mailbox_create (&mbox, "mbox:/dev/null")) 
-      || (rc = mu_mailbox_open (mbox, MU_STREAM_READ))
-      || (rc = mu_mailbox_set_stream (mbox, out)))
-    {
-      maidag_error (_("error opening temporary file: %s"), 
-                    mu_strerror (rc));
-      mu_stream_destroy (&out);
-      exit (EX_TEMPFAIL);
-    }
-
-  rc = mu_mailbox_messages_count (mbox, &n);
+  rc = mu_stream_to_message (out, &mesg);
+  mu_stream_destroy (&out);
   if (rc)
     {
-      errno = rc;
       maidag_error (_("error creating temporary message: %s"),
 		    mu_strerror (rc));
-      mu_stream_destroy (&out);
       exit (EX_TEMPFAIL);
     }
 
-  /* FIXME: mu_stream_unref (out); But mu_mailbox_set_stream
-     steals the reference */
-  return mbox;
+  return mesg;
 }
 
 int
-mda (mu_mailbox_t mbx, char *username)
+maidag_stdio_delivery (maidag_delivery_fn delivery_fun, int argc, char **argv)
 {
-  int status;
-  mu_message_t msg;
-  
-  if ((status = mu_mailbox_get_message (mbx, 1, &msg)) != 0)
-    {
-      maidag_error (_("cannot get message from the temporary mailbox: %s"),
-		    mu_strerror (status));
-      return EX_TEMPFAIL;
-    }
-
-  deliver (msg, username, NULL);
-
-  if (multiple_delivery)
-    exit_code = EX_OK;
-
-  return exit_code;
-}
-
-int
-maidag_stdio_delivery (int argc, char **argv)
-{
-  mu_mailbox_t mbox = make_tmp (sender_address);
+  mu_message_t mesg = make_tmp (sender_address);
   
   if (multiple_delivery)
     multiple_delivery = argc > 1;
 
   for (; *argv; argv++)
-    mda (mbox, *argv);
+    {
+      delivery_fun (mesg, *argv, NULL);
+      if (multiple_delivery)
+	exit_code = EX_OK;
+    }
   return exit_code;
 }
 
@@ -224,9 +190,9 @@ attach_notify (mu_mailbox_t mbox)
 }  
 
 int
-deliver_to_user (mu_mailbox_t mbox, mu_message_t msg,
-		 struct mu_auth_data *auth,
-		 char **errp)
+deliver_to_mailbox (mu_mailbox_t mbox, mu_message_t msg,
+		    struct mu_auth_data *auth,
+		    char **errp)
 {
   int status;
   char *path;
@@ -349,8 +315,8 @@ deliver_to_user (mu_mailbox_t mbox, mu_message_t msg,
   return failed ? exit_code : 0;
 }
 
-int
-deliver_url (mu_url_t url, mu_message_t msg, const char *name, char **errp)
+static int
+do_delivery (mu_url_t url, mu_message_t msg, const char *name, char **errp)
 {
   struct mu_auth_data *auth = NULL;
   mu_mailbox_t mbox;
@@ -437,7 +403,7 @@ deliver_url (mu_url_t url, mu_message_t msg, const char *name, char **errp)
      will be created */
   if (switch_user_id (auth, 1))
     return EX_TEMPFAIL;
-  status = deliver_to_user (mbox, msg, auth, errp);
+  status = deliver_to_mailbox (mbox, msg, auth, errp);
   if (switch_user_id (auth, 0))
     return EX_TEMPFAIL;
 
@@ -448,45 +414,42 @@ deliver_url (mu_url_t url, mu_message_t msg, const char *name, char **errp)
 }
 
 int
-deliver (mu_message_t msg, char *dest_id, char **errp)
+deliver_to_url (mu_message_t msg, char *dest_id, char **errp)
 {
   int status;
   const char *name;
   mu_url_t url = NULL;
   
-  if (url_option)
+  status = mu_url_create (&url, dest_id);
+  if (status)
     {
-      status = mu_url_create (&url, dest_id);
-      if (status)
-	{
-	  maidag_error (_("%s: cannot create url: %s"), dest_id,
-			mu_strerror (status));
-	  return EX_NOUSER;
-	}
-      status = mu_url_parse (url);
-      if (status)
-	{
-	  maidag_error (_("%s: cannot parse url: %s"), dest_id,
-			mu_strerror (status));
-	  mu_url_destroy (&url);
-	  return EX_NOUSER;
-	}
-      status = mu_url_sget_user (url, &name);
-      if (status == MU_ERR_NOENT)
-	name = NULL;
-      else if (status)
-	{
-	  maidag_error (_("%s: cannot get user name from url: %s"),
-			dest_id, mu_strerror (status));
-	  mu_url_destroy (&url);
-	  return EX_NOUSER;
-	}
+      maidag_error (_("%s: cannot create url: %s"), dest_id,
+		    mu_strerror (status));
+      return EX_NOUSER;
+    }
+  status = mu_url_parse (url);
+  if (status)
+    {
+      maidag_error (_("%s: cannot parse url: %s"), dest_id,
+		    mu_strerror (status));
+      mu_url_destroy (&url);
+      return EX_NOUSER;
     }
-  else
+  status = mu_url_sget_user (url, &name);
+  if (status == MU_ERR_NOENT)
+    name = NULL;
+  else if (status)
     {
-      name = dest_id;
-      dest_id = NULL;
+      maidag_error (_("%s: cannot get user name from url: %s"),
+		    dest_id, mu_strerror (status));
+      mu_url_destroy (&url);
+      return EX_NOUSER;
     }
-  return deliver_url (url, msg, name, errp);
+  return do_delivery (url, msg, name, errp);
 }
   
+int
+deliver_to_user (mu_message_t msg, char *dest_id, char **errp)
+{
+  return do_delivery (NULL, msg, dest_id, errp);
+}
diff --git a/maidag/forward.c b/maidag/forward.c
index ccba0db..3924eef 100644
--- a/maidag/forward.c
+++ b/maidag/forward.c
@@ -74,6 +74,52 @@ static struct perm_checker perm_check_tab[] = {
   { 0 }
 };
 
+static mu_list_t idlist;
+
+struct file_id
+{
+  dev_t dev;
+  ino_t inode;
+};
+
+static int
+file_id_cmp (const void *item, const void *data)
+{
+  const struct file_id *a = item;
+  const struct file_id *b = data;
+
+  if (a->dev != b->dev)
+    return 1;
+  if (a->inode != b->inode)
+    return 1;
+  return 0;
+}
+
+static int
+file_id_lookup (dev_t dev, ino_t ino)
+{
+  struct file_id id;
+
+  id.dev = dev;
+  id.inode = ino;
+  return mu_list_locate (idlist, &id, NULL);
+}
+
+static int
+file_id_remember (dev_t dev, ino_t ino)
+{
+  struct file_id *id = malloc (sizeof (*id));
+  if (!id)
+    {
+      mu_error ("%s", mu_strerror (errno));
+      return 1;
+    }
+  id->dev = dev;
+  id->inode = ino;
+  return mu_list_append (idlist, id);
+}
+
+
 /* Check if the forwrd file FILENAME has right permissions and file mode.
    DIRST describes the directory holding the file, AUTH gives current user
    authority. */
@@ -86,8 +132,22 @@ check_forward_permissions (const char *filename, struct stat *dirst,
   if (stat (filename, &st) == 0)
     {
       int i;
+
+      if (!idlist)
+	{
+	  mu_list_create (&idlist);
+	  mu_list_set_comparator (idlist, file_id_cmp);
+	}
+      else if (file_id_lookup (st.st_dev, st.st_ino) == 0)
+	{
+	  mu_diag_output (MU_DIAG_NOTICE,
+			  _("skipping forward file %s: already processed"),
+			  filename);
+	  return 1;
+	}
       
-      if (auth->uid != st.st_uid)
+      if ((forward_file_checks & FWD_OWNER) &&
+	  auth->uid != st.st_uid)
 	{
 	  mu_error (_("%s not owned by %s"), filename, auth->name);
 	  return 1;
@@ -99,6 +159,7 @@ check_forward_permissions (const char *filename, struct stat *dirst,
 	    mu_error ("%s: %s", filename, gettext (perm_check_tab[i].descr));
 	    return 1;
 	  }
+      file_id_remember (st.st_dev, st.st_ino);
       return 0;
     }
   else if (errno != ENOENT)
@@ -203,22 +264,23 @@ create_from_address (mu_message_t msg, mu_address_t *pfrom)
 enum maidag_forward_result
 process_forward (mu_message_t msg, char *filename, const char *myname)
 {
-  FILE *fp;
-  size_t size = 0;
+  int rc;
+  mu_stream_t file;
+  size_t size = 0, n;
   char *buf = NULL;
   enum maidag_forward_result result = maidag_forward_ok;
   mu_mailer_t mailer = NULL;
   mu_address_t from = NULL;
-  
-  fp = fopen (filename, "r");
-  if (!fp)
+
+  rc = mu_file_stream_create (&file, filename, MU_STREAM_READ);
+  if (rc)
     {
       mu_error (_("%s: cannot open forward file: %s"),
-		filename, mu_strerror (errno));
+		filename, mu_strerror (rc));
       return maidag_forward_error;
     }
 
-  while (getline (&buf, &size, fp) > 0)
+  while (mu_stream_getline (file, &buf, &size, &n) == 0 && n > 0)
     {
       char *p;
 
@@ -243,7 +305,7 @@ process_forward (mu_message_t msg, char *filename, const char *myname)
 		  if (result == maidag_forward_ok)
 		    result = maidag_forward_metoo;
 		}
-	      else if (deliver (msg, p, NULL))
+	      else if (deliver_to_user (msg, p, NULL))
 		result = maidag_forward_error;
 	    }
 	}
@@ -256,7 +318,7 @@ process_forward (mu_message_t msg, char *filename, const char *myname)
       mu_mailer_destroy (&mailer);
     }
   free (buf);
-  fclose (fp);
+  mu_stream_destroy (&file);
   return result;
 }
 
@@ -268,19 +330,25 @@ maidag_forward (mu_message_t msg, struct mu_auth_data *auth, char *fwfile)
   struct stat st;
   char *filename;
   enum maidag_forward_result result = maidag_forward_none;
-  
-  if (stat (auth->dir, &st))
+
+  if (fwfile[0] != '/')
     {
-      if (errno == ENOENT)
-	/* FIXME: a warning, maybe? */;
-      else if (!S_ISDIR (st.st_mode))
-	mu_error (_("%s: not a directory"), auth->dir);
-      else
-	mu_error (_("%s: cannot stat directory: %s"),
-		  auth->dir, mu_strerror (errno));
-      return maidag_forward_none;
+      if (stat (auth->dir, &st))
+	{
+	  if (errno == ENOENT)
+	    /* FIXME: a warning, maybe? */;
+	  else if (!S_ISDIR (st.st_mode))
+	    mu_error (_("%s: not a directory"), auth->dir);
+	  else
+	    mu_error (_("%s: cannot stat directory: %s"),
+		      auth->dir, mu_strerror (errno));
+	  return maidag_forward_none;
+	}
+      asprintf (&filename, "%s/%s", auth->dir, fwfile);
     }
-  asprintf (&filename, "%s/%s", auth->dir, fwfile);
+  else
+    filename = strdup (fwfile);
+  
   if (!filename)
     {
       mu_error ("%s", mu_strerror (errno));
diff --git a/maidag/lmtp.c b/maidag/lmtp.c
index eca152d..0595f30 100644
--- a/maidag/lmtp.c
+++ b/maidag/lmtp.c
@@ -317,7 +317,7 @@ dot_deliver (void *item, void *cbdata)
   mu_stream_t iostr = cbdata;
   char *errp = NULL;
   
-  switch (deliver (mesg, name, &errp))
+  switch (deliver_to_user (mesg, name, &errp))
     {
     case 0:
       lmtp_reply (iostr, "250", "2.0.0", "%s: delivered", name);
diff --git a/maidag/maidag.c b/maidag/maidag.c
index e1bebc0..af9abf8 100644
--- a/maidag/maidag.c
+++ b/maidag/maidag.c
@@ -18,8 +18,7 @@
 
 #include "maidag.h"
 
-int mda_mode;              /* Force local MDA mode even if not started as
-			      root */ 
+enum maidag_mode maidag_mode = mode_mda;
 int multiple_delivery;     /* Don't return errors when delivering to multiple
 			      recipients */
 int ex_quota_tempfail;     /* Return temporary failure if mailbox quota is
@@ -51,8 +50,6 @@ char *message_id_header;   /* Use the value of this header as message
 
 /* For LMTP mode */
 mu_m_server_t server;
-int lmtp_mode;
-int url_option;
 char *lmtp_url_string;
 int reuse_lmtp_address = 1;
 int maidag_transcript;
@@ -216,13 +213,13 @@ parse_opt (int key, char *arg, struct argp_state *state)
       break;
 
     case LMTP_OPTION:
-      mu_argp_node_list_new (lst, "lmtp", "yes");
+      mu_argp_node_list_new (lst, "delivery-mode", "lmtp");
       if (arg)
 	mu_argp_node_list_new (lst, "listen", arg);
       break;
 
     case MDA_OPTION:
-      mda_mode = 1;
+      mu_argp_node_list_new (lst, "delivery-mode", "mda");
       break;
       
     case TRANSCRIPT_OPTION:
@@ -267,7 +264,7 @@ parse_opt (int key, char *arg, struct argp_state *state)
       break;
 
     case URL_OPTION:
-      url_option = 1;
+      mu_argp_node_list_new (lst, "delivery-mode", "url");
       break;
       
     case ARGP_KEY_INIT:
@@ -324,6 +321,7 @@ cb_group (mu_debug_t debug, void *data, mu_config_value_t *arg)
 
 static struct mu_kwd forward_checks[] = {
   { "all", FWD_ALL },
+  { "owner", FWD_OWNER },
   { "groupwritablefile", FWD_IWGRP },
   { "file_iwgrp", FWD_IWGRP },
   { "worldwritablefile", FWD_IWOTH },
@@ -343,6 +341,12 @@ cb2_forward_file_checks (mu_debug_t debug, const char *name, void *data)
   int negate = 0;
   const char *str;
   int val;
+
+  if (strcmp (name, "none") == 0)
+    {
+      forward_file_checks = 0;
+      return 0;
+    }
   
   if (strlen (name) > 2 && mu_c_strncasecmp (name, "no", 2) == 0)
     {
@@ -418,8 +422,34 @@ struct mu_cfg_param filter_cfg_param[] = {
     N_("Set script pattern.") },
   { NULL }
 };
-    
+
+static int
+cb_delivery_mode (mu_debug_t debug, void *data, mu_config_value_t *val)
+{
+  static mu_kwd_t mode_tab[] = {
+    { "mda", mode_mda },
+    { "url", mode_url },
+    { "lmtp", mode_lmtp },
+    { NULL }
+  };
+  int n;
+  
+  if (mu_cfg_assert_value_type (val, MU_CFG_STRING, debug))
+    return 1;
+
+  if (mu_kwd_xlat_name (mode_tab, val->v.string, &n) == 0)
+    maidag_mode = n;
+  else
+    mu_cfg_format_error (debug, MU_DEBUG_ERROR,
+			 _("%s is unknonw"),
+			 val->v.string);
+  return 0;
+}
+
 struct mu_cfg_param maidag_cfg_param[] = {
+  { "delivery-mode", mu_cfg_callback, NULL, 0, cb_delivery_mode,
+    N_("Set delivery mode"),
+    N_("mode: {mda | url | lmtp}") },
   { "exit-multiple-delivery-success", mu_cfg_bool, &multiple_delivery, 0, NULL,
     N_("In case of multiple delivery, exit with code 0 if at least one "
        "delivery succeeded.") },
@@ -456,8 +486,6 @@ struct mu_cfg_param maidag_cfg_param[] = {
     N_("Configure safety checks for the forward file."),
     N_("arg: list") },
 /* LMTP support */
-  { "lmtp", mu_cfg_bool, &lmtp_mode, 0, NULL,
-    N_("Run in LMTP mode.") },
   { "group", mu_cfg_callback, &lmtp_groups, 0, cb_group,
     N_("In LMTP mode, retain these supplementary groups."),
     N_("groups: list of string") },
@@ -501,7 +529,8 @@ main (int argc, char *argv[])
 {
   int arg_index;
   mu_debug_t debug;
-
+  maidag_delivery_fn delivery_fun = NULL;
+  
   /* Preparative work: close inherited fds, force a reasonable umask
      and prepare a logging. */
   close_fds ();
@@ -551,11 +580,9 @@ main (int argc, char *argv[])
     exit (EX_CONFIG);
 
   current_uid = getuid ();
-  if (!lmtp_mode && current_uid == 0)
-    mda_mode = 1; 
   
   if (log_to_stderr == -1)
-    log_to_stderr = url_option || (!lmtp_mode && !mda_mode);
+    log_to_stderr = maidag_mode == mode_url;
   
   mu_diag_get_debug (&debug);
   if (!log_to_stderr)
@@ -573,46 +600,31 @@ main (int argc, char *argv[])
   argc -= arg_index;
   argv += arg_index;
 
-  if (lmtp_mode && !url_option)
+  switch (maidag_mode)
     {
+    case mode_lmtp:
       if (argc)
 	{
 	  mu_error (_("too many arguments"));
 	  return EX_USAGE;
 	}
       return maidag_lmtp_server ();
-    }
-  else 
-    {
-      if (!mda_mode)
+
+    case mode_url:
+      /* FIXME: Verify if the urls are deliverable? */
+      delivery_fun = deliver_to_url;
+      break;
+      
+    case mode_mda:
+      if (argc == 0)
 	{
-	  if (url_option)
-	    {
-	      /* FIXME: Verify if the urls are deliverable? */
-	    }
-	  else
-	    {
-	      static char *s_argv[2];
-	      struct mu_auth_data *auth = mu_get_auth_by_uid (current_uid);
-	      
-	      if (!auth)
-		{
-		  mu_error (_("cannot get username"));
-		  return EX_UNAVAILABLE;
-		}
-	      
-	      if (argc > 0 && strcmp (auth->name, argv[0]))
-		{
-		  mu_error (_("recipients given when running as non-root"));
-		  return EX_USAGE;
-		}
-	      s_argv[0] = auth->name;
-	      argv = s_argv;
-	      argc = 1;
-	    }
+	  mu_error (_("recipients not given"));
+	  return EX_USAGE;
 	}
-      return maidag_stdio_delivery (argc, argv);
+      delivery_fun = deliver_to_user;
+      break;
     }
+  return maidag_stdio_delivery (delivery_fun, argc, argv);
 }
   
 
diff --git a/maidag/maidag.h b/maidag/maidag.h
index aa2195f..012230d 100644
--- a/maidag/maidag.h
+++ b/maidag/maidag.h
@@ -111,13 +111,22 @@ extern int debug_level;
 #define EX_QUOTA() (ex_quota_tempfail ? EX_TEMPFAIL : EX_UNAVAILABLE)
 
 /* .forward file checks */
-#define FWD_IWGRP     0x0001 /* group writable forward file */
-#define FWD_IWOTH     0x0002 /* world writable forward file */
-#define FWD_LINK      0x0004 /* linked forward file in writable dir */
-#define FWD_DIR_IWGRP 0x0008 /* forward file in group writable directory */
-#define FWD_DIR_IWOTH 0x0010 /* forward file in world writable directory */
-#define FWD_ALL (FWD_IWGRP|FWD_IWOTH|FWD_LINK|FWD_DIR_IWOTH|FWD_DIR_IWGRP)
+#define FWD_OWNER     0x0001 /* file ownership */
+#define FWD_IWGRP     0x0002 /* group writable forward file */
+#define FWD_IWOTH     0x0004 /* world writable forward file */
+#define FWD_LINK      0x0008 /* linked forward file in writable dir */
+#define FWD_DIR_IWGRP 0x0010 /* forward file in group writable directory */
+#define FWD_DIR_IWOTH 0x0020 /* forward file in world writable directory */
+#define FWD_ALL (FWD_OWNER|FWD_IWGRP|FWD_IWOTH|FWD_LINK|FWD_DIR_IWOTH|FWD_DIR_IWGRP)
+
+enum maidag_mode
+  {
+    mode_mda,
+    mode_url,
+    mode_lmtp
+  };
 
+extern enum maidag_mode maidag_mode;
 extern int exit_code;
 extern int log_to_stderr;
 extern int multiple_delivery;
@@ -136,8 +145,6 @@ extern int sieve_debug_flags;
 extern int sieve_enable_log;  
 
 extern mu_m_server_t server;
-extern int lmtp_mode;
-extern int url_option;
 extern char *lmtp_url_string;
 extern int reuse_lmtp_address;
 extern mu_list_t lmtp_groups;
@@ -147,7 +154,12 @@ extern int maidag_transcript;
 void close_fds (void);
 int switch_user_id (struct mu_auth_data *auth, int user);
 
-int maidag_stdio_delivery (int argc, char **argv);
+typedef int (*maidag_delivery_fn) (mu_message_t, char *, char **);
+
+int deliver_to_url (mu_message_t msg, char *dest_id, char **errp);
+int deliver_to_user (mu_message_t msg, char *dest_id, char **errp);
+
+int maidag_stdio_delivery (maidag_delivery_fn fun, int argc, char **argv);
 int maidag_lmtp_server (void);
 int lmtp_connection (int fd, struct sockaddr *sa, int salen, void *data,
 		     mu_ip_server_t srv, time_t timeout, int transcript);
@@ -156,8 +168,6 @@ void maidag_error (const char *fmt, ...) MU_PRINTFLIKE(1, 2);
 void notify_biff (mu_mailbox_t mbox, char *name, size_t size);
 void guess_retval (int ec);
 
-int mda (mu_mailbox_t mbx, char *username);
-int deliver (mu_message_t msg, char *name, char **errp);
 int sieve_test (struct mu_auth_data *auth, mu_message_t msg);
 int check_quota (struct mu_auth_data *auth, mu_off_t size, mu_off_t *rest);
 
diff --git a/maidag/tests/Makefile.am b/maidag/tests/Makefile.am
index 65abc82..eec3015 100644
--- a/maidag/tests/Makefile.am
+++ b/maidag/tests/Makefile.am
@@ -39,6 +39,7 @@ $(srcdir)/package.m4: $(top_srcdir)/configure.ac
 ## ------------ ##
 
 TESTSUITE_AT = \
+ forward.at\
  lmtp.at\
  mda.at\
  testsuite.at\
diff --git a/maidag/tests/testsuite.at b/maidag/tests/testsuite.at
index b56d63d..cb8ff4f 100644
--- a/maidag/tests/testsuite.at
+++ b/maidag/tests/testsuite.at
@@ -29,4 +29,5 @@ MUT_VERSION(maidag)
 m4_include([mda.at])
 m4_include([lmtp.at])
 m4_include([url-mbox.at])
+m4_include([forward.at])
 
diff --git a/mail/testsuite/mail/send.exp b/mail/testsuite/mail/send.exp
index 5576f06..916a546 100644
--- a/mail/testsuite/mail/send.exp
+++ b/mail/testsuite/mail/send.exp
@@ -95,7 +95,6 @@ if ![mu_check_capability ENABLE_SENDMAIL] {
 	"  48: > All mimsy were the borogoves,"\
 	"  49: > And the mome raths outgrabe."\
 	"  50: > "\
-	"  51: "\
 	"END OF MESSAGE"
 }
 
diff --git a/sieve/tests/redirect.at b/sieve/tests/redirect.at
index 37291f4..0449447 100644
--- a/sieve/tests/redirect.at
+++ b/sieve/tests/redirect.at
@@ -52,7 +52,6 @@ ENVELOPE TO: <gray@gnu.org>
   13: 
   14: --
   15: Wile E. Coyote   "Super Genius"   coyote@desert.example.org
-  16: 
 END OF MESSAGE
 ENVELOPE FROM: b1ff@de.res.example.com
 ENVELOPE TO: <gray@gnu.org>
@@ -70,7 +69,6 @@ ENVELOPE TO: <gray@gnu.org>
   11: $20,000 IN LESS THAN TWO MONTHS!  AND IT'S LEGAL!!!!!!!!!
   12: !!!!!!!!!!!!!!!!!!111111111!!!!!!!11111111111!!1  JUST
   13: SEND $5 IN SMALL, UNMARKED BILLS TO THE ADDRESSES BELOW!
-  14: 
 END OF MESSAGE
 ENVELOPE FROM: bar@dontmailme.org
 ENVELOPE TO: <gray@gnu.org>
@@ -85,7 +83,6 @@ ENVELOPE TO: <gray@gnu.org>
    8: Subject: Coffee
    9: 
   10: How about some coffee?
-  11: 
 END OF MESSAGE
 ],
 [REDIRECT on msg uid 1: to gray@gnu.org
diff --git a/sieve/tests/reject.at b/sieve/tests/reject.at
index f1253f6..d7acfc0 100644
--- a/sieve/tests/reject.at
+++ b/sieve/tests/reject.at
@@ -100,7 +100,6 @@ ENVELOPE TO: <coyote@desert.example.org>
   43: Wile E. Coyote   "Super Genius"   coyote@desert.example.org
   44: 
   45: --(boundary)
-  46: 
 END OF MESSAGE
 ENVELOPE FROM: MAILER-DAEMON@nonexistent.net
 ENVELOPE TO: <b1ff@de.res.example.com>
@@ -148,7 +147,6 @@ ENVELOPE TO: <b1ff@de.res.example.com>
   41: SEND $5 IN SMALL, UNMARKED BILLS TO THE ADDRESSES BELOW!
   42: 
   43: --(boundary)
-  44: 
 END OF MESSAGE
 ENVELOPE FROM: MAILER-DAEMON@nonexistent.net
 ENVELOPE TO: <bar@dontmailme.org>
@@ -193,7 +191,6 @@ ENVELOPE TO: <bar@dontmailme.org>
   38: How about some coffee?
   39: 
   40: --(boundary)
-  41: 
 END OF MESSAGE
 ],
 [REJECT on msg uid 1


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Fri Oct 08 10:38:19 2010
Received: from mailman by lists.gnu.org with archive (Exim 4.43)
	id 1P4E5T-00052Z-TA
	for mharc-commit-mailutils@gnu.org; Fri, 08 Oct 2010 10:38:19 -0400
Received: from [140.186.70.92] (port=55633 helo=eggs.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.43) id 1P4E5O-000519-97
	for commit-mailutils@gnu.org; Fri, 08 Oct 2010 10:38:16 -0400
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P4E5M-0001ex-G5
	for commit-mailutils@gnu.org; Fri, 08 Oct 2010 10:38:14 -0400
Received: from colonialone.fsf.org ([140.186.70.51]:54016
	helo=internal.in.savannah.gnu.org)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P4E5M-0001ej-CB
	for commit-mailutils@gnu.org; Fri, 08 Oct 2010 10:38:12 -0400
Received: from [10.1.0.108] (helo=vcs-noshell.in.savannah.gnu.org)
	by internal.in.savannah.gnu.org with esmtp (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P4E5L-0003Ly-Ur
	for commit-mailutils@gnu.org; Fri, 08 Oct 2010 14:38:12 +0000
Received: from gray by vcs-noshell.in.savannah.gnu.org with local (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P4E5L-0002GS-OW
	for commit-mailutils@gnu.org; Fri, 08 Oct 2010 14:38:11 +0000
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: db1ee5e0af1f25405ffb35876149eb25cf7763f2
X-Git-Newrev: 37d7cd77d34cd4bf8d267b3010d923845fe74308
Message-Id: <E1P4E5L-0002GS-OW@vcs-noshell.in.savannah.gnu.org>
Date: Fri, 08 Oct 2010 14:38:11 +0000
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
Subject: [SCM] GNU Mailutils branch, master,
	updated. release-2.2-138-g37d7cd7
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.5
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=unsubscribe>
List-Archive: <http://lists.gnu.org/archive/html/commit-mailutils>
List-Post: <mailto:commit-mailutils@gnu.org>
List-Help: <mailto:commit-mailutils-request@gnu.org?subject=help>
List-Subscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Fri, 08 Oct 2010 14:38:16 -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=37d7cd77d34cd4bf8d267b3010d923845fe74308

The branch, master has been updated
       via  37d7cd77d34cd4bf8d267b3010d923845fe74308 (commit)
       via  e4648a8d1dcdfc40547e160d77d753e46e1c57ca (commit)
      from  db1ee5e0af1f25405ffb35876149eb25cf7763f2 (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 37d7cd77d34cd4bf8d267b3010d923845fe74308
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Fri Oct 8 16:23:55 2010 +0300

    mu: compile pop support conditionally.
    
    * mu/Makefile.am [MU_COND_SUPPORT_POP]: Compile pop.c conditionally.
    * mu/mu.c (mutool_nosys): New function.
    (mutool_action_tab): Compile pop support conditionally.

commit e4648a8d1dcdfc40547e160d77d753e46e1c57ca
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Fri Oct 8 16:14:22 2010 +0300

    examples/mta.c: use streams instead of FILE; exit with codes from sysexits.h

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

Summary of changes:
 examples/mta.c |  291 ++++++++++++++++++++++++++------------------------------
 mu/Makefile.am |    9 ++-
 mu/mu.c        |   12 +++
 3 files changed, 157 insertions(+), 155 deletions(-)

diff --git a/examples/mta.c b/examples/mta.c
index 8fb265c..d53e97f 100644
--- a/examples/mta.c
+++ b/examples/mta.c
@@ -57,6 +57,7 @@
 #endif
 #include <string.h>
 #include <pwd.h>
+#include <sysexits.h>
 #include <sys/stat.h>
 #include <sys/time.h>
 #include <sys/socket.h>
@@ -141,7 +142,7 @@ main (int argc, char **argv)
 	  break;
 
 	default:
-	  exit (1);
+	  exit (EX_USAGE);
 	}
     }
 
@@ -190,73 +191,71 @@ from_address ()
   return strdup (from_person);
 }
      
-void
-make_tmp (FILE *input, const char *from, char **tempfile)
+static mu_message_t
+make_tmp (mu_stream_t in)
 {
-  time_t t;
-  int fd = mu_tempfile (NULL, tempfile);
-  FILE *fp;
+  int rc;
+  mu_stream_t out;
   char *buf = NULL;
-  size_t n = 0;
-  int line;
+  size_t size = 0, n;
+  mu_message_t mesg;
   
-  if (fd == -1 || (fp = fdopen (fd, "w+")) == NULL)
+  rc = mu_temp_file_stream_create (&out, NULL);
+  if (rc)
     {
-      mu_error ("%s: unable to open temporary file", progname);
-      exit (1);
+      mu_error (_("unable to open temporary file: %s"), mu_strerror (rc));
+      exit (EX_UNAVAILABLE);
     }
 
-  line = 0;
-  while (getline (&buf, &n, input) > 0)
+  rc = mu_stream_getline (in, &buf, &size, &n);
+  if (rc)
     {
-      int len = strlen (buf);
-      if (len >= 2 && buf[len - 2] == '\r')
-	{
-	  buf[len - 2] = '\n';
-	  buf[len - 1] = 0;
-	}
-	  
-      line++;
-      if (line == 1)
-	{
-	  if (memcmp (buf, "From ", 5))
-	    {
-	      char *from = from_address ();
-	      if (from)
-		{
-		  time (&t);
-		  fprintf (fp, "From %s %s", from, ctime (&t));
-		  free (from);
-		}
-	      else
-		{
-		  mu_error ("%s: can't determine sender address", progname);
-		  exit (1);
-		}
-	    }
-	}
-      else if (!memcmp (buf, "From ", 5))
-	fputc ('>', fp);
+      mu_error (_("read error: %s"), mu_strerror (rc));
+      mu_stream_destroy (&out);
+      exit (EX_UNAVAILABLE);
+    }
+  if (n == 0)
+    {
+      mu_error (_("unexpected EOF on input"));
+      mu_stream_destroy (&out);
+      exit (EX_DATAERR);
+    } 
 
-      if (dot && buf[0] == '.' &&
-	       ((buf[1] == '\r' && buf[2] == '\n') || (buf[1] == '\n')))
-	break;
-      
-      if (fputs (buf, fp) == EOF)
+  if (n >= 5 && memcmp (buf, "From ", 5))
+    {
+      time_t t;
+      const char *from = from_address ();
+      if (!from)
 	{
-	  mu_error ("%s: temporary file write error", progname);
-	  fclose (fp);
-	  exit (1);
+	  mu_error ("%s: can't determine sender address", progname);
+	  exit (EX_NOUSER);
 	}
+	  
+      time (&t);
+      mu_stream_printf (out, "From %s %s", from, ctime (&t));
     }
-  
-  if (buf && strchr (buf, '\n') == NULL)
-    putc ('\n', fp);
 
-  putc ('\n', fp);
+  mu_stream_write (out, buf, n, NULL);
   free (buf);
+  
+  rc = mu_stream_copy (out, in, 0, NULL);
+  if (rc)
+    {
+      mu_error (_("copy error: %s"), mu_strerror (rc));
+      mu_stream_destroy (&out);
+      exit (EX_UNAVAILABLE);
+    }
+
+  rc = mu_stream_to_message (out, &mesg);
+  mu_stream_destroy (&out);
+  if (rc)
+    {
+      mu_error (_("error creating temporary message: %s"),
+		mu_strerror (rc));
+      exit (EX_UNAVAILABLE);
+    }
 
-  fclose (fp);
+  return mesg;
 }
 
 void
@@ -462,9 +461,8 @@ message_finalize (mu_message_t msg, int warn)
 int
 mta_stdin (int argc, char **argv)
 {
-  int c;
-  char *tempfile;
-  mu_mailbox_t mbox;
+  int c, rc;
+  mu_stream_t in;
   mu_message_t msg = NULL;
   
   for (c = 0; c < argc; c++)
@@ -476,27 +474,19 @@ mta_stdin (int argc, char **argv)
 	}
     }
 
-  make_tmp (stdin, from_person, &tempfile);
-  if ((c = mu_mailbox_create_default (&mbox, tempfile)) != 0)
+  rc = mu_stdio_stream_create (&in, MU_STDIN_FD, MU_STREAM_READ);
+  if (rc)
     {
-      mu_error ("%s: can't create mailbox %s: %s",
-		progname, tempfile, mu_strerror (c));
-      unlink (tempfile);
-      return 1;
-    }
+      mu_diag_funcall (MU_DIAG_ERROR, "mu_stdio_stream_create",
+		       "MU_STDIN_FD", rc);
+      exit (EX_UNAVAILABLE);
+    } 
 
-  if ((c = mu_mailbox_open (mbox, MU_STREAM_RDWR)) != 0)
-    {
-      mu_error ("%s: can't open mailbox %s: %s",
-		progname, tempfile, mu_strerror (c));
-      unlink (tempfile);
-      return 1;
-    }
-
-  mu_mailbox_get_message (mbox, 1, &msg);
+  msg = make_tmp (in);
+  mu_stream_destroy (&in);
   if (message_finalize (msg, 1))
     return 1;
-
+  
   if (!recipients)
     {
       mu_error ("%s: Recipient names must be specified", progname);
@@ -504,24 +494,20 @@ mta_stdin (int argc, char **argv)
     }
 
   mta_send (msg);
-  
-  unlink (tempfile);
-  free (tempfile);
+  mu_message_destroy (&msg, mu_message_get_owner (msg));
   return 0;
 }
 
-FILE *in, *out;
-
 void
-smtp_reply (int code, char *fmt, ...)
+smtp_reply (mu_stream_t str, int code, char *fmt, ...)
 {
   va_list ap;
 
+  mu_stream_printf (str, "%d ", code);
   va_start (ap, fmt);
-  fprintf (out, "%d ", code);
-  vfprintf (out, fmt, ap);
+  mu_stream_vprintf (str, fmt, ap);
   va_end (ap);
-  fprintf (out, "\r\n");
+  mu_stream_printf (str, "\r\n");
 }
 
 #define STATE_INIT   0
@@ -575,42 +561,34 @@ check_prefix (char *str, const char *prefix)
 }
 
 void
-smtp (int fd)
+smtp (mu_stream_t str)
 {
-  int state, c;
+  int state;
   char *buf = NULL;
   size_t size = 0;
-  mu_mailbox_t mbox;
-  mu_message_t msg;
-  char *tempfile;
   char *rcpt_addr;
   
-  in = fdopen (fd, "r");
-  out = fdopen (fd, "w");
-  SETVBUF (in, NULL, _IOLBF, 0);
-  SETVBUF (out, NULL, _IOLBF, 0);
-
-  smtp_reply (220, "Ready");
+  smtp_reply (str, 220, "Ready");
   for (state = STATE_INIT; state != STATE_QUIT; )
     {
+      char *s;
       int argc;
       char **argv;
-      int kw, len;
+      int kw;
+      size_t len;
       
-      if (getline (&buf, &size, in) == -1)
-	exit (1);
-      len = strlen (buf);
-      while (len > 0 && (buf[len-1] == '\n' || buf[len-1] == '\r'))
-	len --;
-      buf[len] = 0;
+      if (mu_stream_getline (str, &buf, &size, &len) || len == 0)
+	exit (EX_PROTOCOL);
+
+      s = mu_str_stripws (buf);
       
-      if (mu_argcv_get (buf, "", NULL, &argc, &argv))
-	exit (1);
+      if (mu_argcv_get (s, "", NULL, &argc, &argv))
+	exit (EX_UNAVAILABLE);
 
       kw = smtp_kw (argv[0]);
       if (kw == KW_QUIT)
 	{
-	  smtp_reply (221, "Done");
+	  smtp_reply (str, 221, "Done");
 	  state = STATE_QUIT;
 	  mu_argcv_free (argc, argv);
 	  continue;
@@ -625,15 +603,15 @@ smtp (int fd)
 	    case KW_HELO:
 	      if (argc == 2)
 		{
-		  smtp_reply (250, "pleased to meet you");
+		  smtp_reply (str, 250, "pleased to meet you");
 		  state = STATE_EHLO;
 		}
 	      else
-		smtp_reply (501, "%s requires domain address", argv[0]);
+		smtp_reply (str, 501, "%s requires domain address", argv[0]);
 	      break;
 
 	    default:
-	      smtp_reply (503, "Polite people say HELO first");
+	      smtp_reply (str, 503, "Polite people say HELO first");
 	      break;
 	    }
 	  break;
@@ -652,15 +630,15 @@ smtp (int fd)
 	      if (from_person)
 		{
 		  from_person = strdup (from_person);
-		  smtp_reply (250, "Sender OK");
+		  smtp_reply (str, 250, "Sender OK");
 		  state = STATE_MAIL;
 		}
 	      else
-		smtp_reply (501, "Syntax error");
+		smtp_reply (str, 501, "Syntax error");
 	      break;
 
 	    default:
-	      smtp_reply (503, "Need MAIL command");
+	      smtp_reply (str, 503, "Need MAIL command");
 	    }
 	  break;
 	  
@@ -678,19 +656,19 @@ smtp (int fd)
 	      if (rcpt_addr)
 		{
 		  if (add_recipient (rcpt_addr))
-		    smtp_reply (451, "Recipient not accepted");
+		    smtp_reply (str, 451, "Recipient not accepted");
 		  else
 		    {
-		      smtp_reply (250, "Recipient OK");
+		      smtp_reply (str, 250, "Recipient OK");
 		      state = STATE_RCPT;
 		    }
 		}
 	      else
-		smtp_reply (501, "Syntax error");
+		smtp_reply (str, 501, "Syntax error");
 	      break;
 	      
 	    default:
-	      smtp_reply (503, "Need RCPT command");
+	      smtp_reply (str, 503, "Need RCPT command");
 	    }
 	  break;
 	  
@@ -708,55 +686,51 @@ smtp (int fd)
 	      if (rcpt_addr)
 		{
 		  if (add_recipient (rcpt_addr))
-		    smtp_reply (451, "Recipient not accepted");
+		    smtp_reply (str, 451, "Recipient not accepted");
 		  else
 		    {
-		      smtp_reply (250, "Recipient OK");
+		      smtp_reply (str, 250, "Recipient OK");
 		      state = STATE_RCPT;
 		    }
 		}
 	      else
-		smtp_reply (501, "Syntax error");
+		smtp_reply (str, 501, "Syntax error");
 	      break;
 
 	    case KW_DATA:
-	      smtp_reply (354,
-			  "Enter mail, end with \".\" on a line by itself");
-	      make_tmp (in, from_person, &tempfile);
-	      if ((c = mu_mailbox_create_default (&mbox, tempfile)) != 0)
-		{
-		  mu_error ("%s: can't create mailbox %s: %s",
-			    progname,
-			    tempfile, mu_strerror (c));
-		  unlink (tempfile);
-		  exit (1);
-		}
-
-	      if ((c = mu_mailbox_open (mbox, MU_STREAM_RDWR)) != 0)
-		{
-		  mu_error ("%s: can't open mailbox %s: %s",
-			    progname, 
-			    tempfile, mu_strerror (c));
-		  unlink (tempfile);
-		  exit (1);
-		}
-
-	      mu_mailbox_get_message (mbox, 1, &msg);
-	      if (message_finalize (msg, 0) == 0)
-		mta_send (msg);
-	      else
-		smtp_reply (501, "can't send message"); /*FIXME: code?*/
-	      unlink (tempfile);
-
-	      mu_address_destroy (&recipients);
-	      from_person = NULL;
+	      {
+		mu_stream_t flt;
+		mu_message_t msg;
+		int rc;
+		
+		rc = mu_filter_create (&flt, str, "CRLFDOT", MU_FILTER_DECODE,
+				       MU_STREAM_READ|MU_STREAM_WRTHRU);
+		if (rc)
+		  {
+		    mu_diag_funcall (MU_DIAG_ERROR, "mu_filter_create",
+				     "CRLFDOT", rc);
+		    exit (EX_UNAVAILABLE);
+		  }
+		smtp_reply (str, 354,
+			    "Enter mail, end with \".\" on a line by itself");
+		
+		msg = make_tmp (flt);
+		mu_stream_destroy (&flt);
+		if (message_finalize (msg, 0) == 0)
+		  mta_send (msg);
+		else
+		  smtp_reply (str, 501, "can't send message"); /*FIXME: code?*/
+		mu_message_destroy (&msg, mu_message_get_owner (msg));
+		mu_address_destroy (&recipients);
+		from_person = NULL;
 	      
-	      smtp_reply (250, "Message accepted for delivery");
-	      state = STATE_EHLO;
+		smtp_reply (str, 250, "Message accepted for delivery");
+		state = STATE_EHLO;
+	      }
 	      break;
 
 	    default:
-	      smtp_reply (503, "Invalid command");
+	      smtp_reply (str, 503, "Invalid command");
 	      break;
 	    }
 	  break;
@@ -764,8 +738,6 @@ smtp (int fd)
 	}
       mu_argcv_free (argc, argv);
     }
-    
-  close (fd);
 }
 
 int
@@ -826,7 +798,9 @@ mta_smtp (int argc, char **argv)
       struct sockaddr_in his_addr;
       int sfd, status;
       socklen_t len;
-
+      int rc;
+      mu_stream_t str;
+      
       FD_ZERO (&rfds);
       FD_SET (fd, &rfds);
       
@@ -846,7 +820,16 @@ mta_smtp (int argc, char **argv)
 	  return 1;
 	}
 
-      smtp (sfd);
+      rc = mu_fd_stream_create (&str, NULL, sfd,
+				MU_STREAM_RDWR|MU_STREAM_AUTOCLOSE);
+      if (rc)
+	{
+	  mu_diag_funcall (MU_DIAG_ERROR, "mu_fd_stream_create", NULL, rc);
+	  break;
+	}
+      mu_stream_set_buffer (str, mu_buffer_line, 0);
+      smtp (str);
+      mu_stream_destroy (&str);
       break;
     }
   
diff --git a/mu/Makefile.am b/mu/Makefile.am
index c9b5e39..d2a2f3d 100644
--- a/mu/Makefile.am
+++ b/mu/Makefile.am
@@ -16,6 +16,13 @@
 ## along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 bin_PROGRAMS = mu
+
+if MU_COND_SUPPORT_POP
+ POP_C=pop.c
+endif
+
+EXTRA_DIST=pop.c
+
 mu_SOURCES = \
  acl.c\
  info.c\
@@ -23,7 +30,7 @@ mu_SOURCES = \
  mu.c\
  filter.c\
  flt2047.c\
- pop.c\
+ $(POP_C)\
  query.c\
  shell.c
 
diff --git a/mu/mu.c b/mu/mu.c
index 784698c..2490805 100644
--- a/mu/mu.c
+++ b/mu/mu.c
@@ -87,9 +87,21 @@ struct mutool_action_tab
   mutool_action_t action;
 };
 
+static int
+mutool_nosys (int argc, char **argv)
+{
+  mu_error (_("%s is not available because required code is not compiled"),
+	    argv[0]);
+  return 1;
+}
+
 struct mutool_action_tab mutool_action_tab[] = {
   { "info", mutool_info },
+#ifdef ENABLE_POP
   { "pop", mutool_pop },
+#else
+  { "pop", mutool_nosys },
+#endif
   { "filter", mutool_filter },
   { "2047", mutool_flt2047 },
   { "query", mutool_query },


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Sat Oct 09 10:09:11 2010
Received: from mailman by lists.gnu.org with archive (Exim 4.43)
	id 1P4a6p-0006ve-1d
	for mharc-commit-mailutils@gnu.org; Sat, 09 Oct 2010 10:09:11 -0400
Received: from [140.186.70.92] (port=52646 helo=eggs.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.43) id 1P4a4P-0006ev-Oj
	for commit-mailutils@gnu.org; Sat, 09 Oct 2010 10:09:05 -0400
Received: from colonialone.fsf.org ([140.186.70.51]:56403
	helo=internal.in.savannah.gnu.org)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P4a4N-0007Up-GO
	for commit-mailutils@gnu.org; Sat, 09 Oct 2010 10:06:41 -0400
Received: from [10.1.0.108] (helo=vcs-noshell.in.savannah.gnu.org)
	by internal.in.savannah.gnu.org with esmtp (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P4a4M-0003gM-QJ
	for commit-mailutils@gnu.org; Sat, 09 Oct 2010 14:06:39 +0000
Received: from gray by vcs-noshell.in.savannah.gnu.org with local (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P4a4L-0006HK-Sk
	for commit-mailutils@gnu.org; Sat, 09 Oct 2010 14:06:38 +0000
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: 37d7cd77d34cd4bf8d267b3010d923845fe74308
X-Git-Newrev: bf07c0b891aad29dd8eb45ff3c1b1ea8c23e7dce
Message-Id: <E1P4a4L-0006HK-Sk@vcs-noshell.in.savannah.gnu.org>
Date: Sat, 09 Oct 2010 14:06:38 +0000
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
Subject: [SCM] GNU Mailutils branch, master,
	updated. release-2.2-140-gbf07c0b
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.5
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=unsubscribe>
List-Archive: <http://lists.gnu.org/archive/html/commit-mailutils>
List-Post: <mailto:commit-mailutils@gnu.org>
List-Help: <mailto:commit-mailutils-request@gnu.org?subject=help>
List-Subscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Sat, 09 Oct 2010 14:09:06 -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=bf07c0b891aad29dd8eb45ff3c1b1ea8c23e7dce

The branch, master has been updated
       via  bf07c0b891aad29dd8eb45ff3c1b1ea8c23e7dce (commit)
       via  f3188dcc18d8de1192c87e7e0284800e9488b976 (commit)
      from  37d7cd77d34cd4bf8d267b3010d923845fe74308 (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 bf07c0b891aad29dd8eb45ff3c1b1ea8c23e7dce
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Sat Oct 9 16:58:21 2010 +0300

    Fix copyleft notices to follow the GNU standards.

commit f3188dcc18d8de1192c87e7e0284800e9488b976
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Sat Oct 9 13:06:58 2010 +0300

    Reorganize libmailutils directory structure.
    
    * configure.ac (AC_CONFIG_FILES): Add libmailutils/ subdirs.
    * include/mailutils/Makefile.am: Update references to libmailutils.
    * po/POTFILES.in: Likewise.
    * libmailutils/.gitignore: Update.
    * libmailutils/Makefile.am: Use convenience libraries in subdirs.
    
    * libmailutils/string/Makefile.am: New file.
    * libmailutils/cstrcasecmp.c: Move to libmailutils/string.
    * libmailutils/cstrlower.c: Likewise.
    * libmailutils/cstrupper.c: Likewise.
    * libmailutils/strltrim.c: Likewise.
    * libmailutils/strskip.c: Likewise.
    * libmailutils/stripws.c: Likewise.
    * libmailutils/strrtrim.c: Likewise.
    * libmailutils/asnprintf.c: Likewise.
    * libmailutils/asprintf.c: Likewise.
    * libmailutils/muctype.c: Likewise.
    * libmailutils/vasnprintf.c: Likewise.
    * libmailutils/mkfilename.c: Likewise.
    
    * libmailutils/stream/Makefile.am: New file.
    * libmailutils/dbgstream.c: Move to libmailutils/stream.
    * libmailutils/file_stream.c: Likewise.
    * libmailutils/fltstream.c: Likewise.
    * libmailutils/iostream.c: Likewise.
    * libmailutils/mapfile_stream.c: Likewise.
    * libmailutils/memory_stream.c: Likewise.
    * libmailutils/message_stream.c: Likewise.
    * libmailutils/prog_stream.c: Likewise.
    * libmailutils/rdcache_stream.c: Likewise.
    * libmailutils/socket_stream.c: Likewise.
    * libmailutils/stdio_stream.c: Likewise.
    * libmailutils/stream.c: Likewise.
    * libmailutils/stream_printf.c: Likewise.
    * libmailutils/stream_vprintf.c: Likewise.
    * libmailutils/streamcpy.c: Likewise.
    * libmailutils/streamref.c: Likewise.
    * libmailutils/tcp.c: Likewise.
    * libmailutils/temp_file_stream.c: Likewise.
    * libmailutils/xscript-stream.c
    
    * libmailutils/cfg/Makefile.am: New file.
    * libmailutils/cfg/.gitignore: New file.
    * libmailutils/cfg_driver.c: Move to libmailutils/cfg/driver.c.
    * libmailutils/cfg_format.c: Move to libmailutils/cfg/format.c.
    * libmailutils/cfg_lexer.l: Move to libmailutils/cfg/lexer.l.
    * libmailutils/cfg_parser.y: Move to libmailutils/cfg/parser.y.
    * libmailutils/gocs.c: Move to libmailutils/cfg/gocs.c.
    
    * libmailutils/diag/Makefile.am: New file.
    * libmailutils/diag/.gitignore: New file.
    * libmailutils/debug.c: Move to libmailutils/diag.
    * libmailutils/diag.c: Likewise.
    * libmailutils/gdebug.c: Likewise.
    * libmailutils/errors: Likewise.
    * libmailutils/muerrno.cin: Likewise.
    * libmailutils/syslog.c: Likewise.
    * libmailutils/dbgstderr.c: Likewise.
    * libmailutils/dbgsyslog.c: Likewise.
    
    * libmailutils/address/Makefile.am: New file.
    * libmailutils/address.c: Move to libmailutils/address.
    * libmailutils/parse822.c: Likewise.
    
    * libmailutils/mailbox/Makefile.am: New file.
    * libmailutils/mailbox.c: Move to libmailutils/mailbox.
    * libmailutils/mbx_default.c: Likewise.
    * libmailutils/mbxitr.c: Likewise.
    * libmailutils/attribute.c: Likewise.
    * libmailutils/body.c: Likewise.
    * libmailutils/envelope.c: Likewise.
    * libmailutils/folder.c: Likewise.
    * libmailutils/hdritr.c: Likewise.
    * libmailutils/header.c: Likewise.
    * libmailutils/message.c: Likewise.
    * libmailutils/msgscan.c: Likewise.
    
    * libmailutils/mailer/Makefile.am: New file.
    * libmailutils/mailer.c: Move to libmailutils/mailer.
    * libmailutils/progmailer.c: Likewise.
    
    * libmailutils/mime/Makefile.am: New file.
    * libmailutils/attachment.c: Move to libmailutils/mime.
    * libmailutils/mime.c: Likewise.
    * libmailutils/mimehdr.c: Likewise.
    
    * libmailutils/server/Makefile.am: New file.
    * libmailutils/acl.c: Move to libmailutils/server.
    * libmailutils/server.c: Likewise.
    * libmailutils/msrv.c: Likewise.
    * libmailutils/ipsrv.c: Likewise.
    
    * libmailutils/auth/Makefile.am: New file.
    * libmailutils/auth.c: Move to libmailutils/auth.
    * libmailutils/mu_auth.c: Likewise.
    * libmailutils/system.c: Likewise.
    
    * libmailutils/base/Makefile.am: New file.
    * libmailutils/base/.gitignore: New file.
    * libmailutils/alloc.c: Move to libmailutils/base.
    * libmailutils/amd.c: Likewise.
    * libmailutils/argcv.c: Likewise.
    * libmailutils/assoc.c: Likewise.
    * libmailutils/daemon.c: Likewise.
    * libmailutils/date.c: Likewise.
    * libmailutils/fgetpwent.c: Likewise.
    * libmailutils/freeitem.c: Likewise.
    * libmailutils/getpass.c: Likewise.
    * libmailutils/iterator.c: Likewise.
    * libmailutils/kwd.c: Likewise.
    * libmailutils/list.c: Likewise.
    * libmailutils/listlist.c: Likewise.
    * libmailutils/locale.c: Likewise.
    * libmailutils/locker.c: Likewise.
    * libmailutils/mailcap.c: Likewise.
    * libmailutils/md5.c: Likewise.
    * libmailutils/monitor.c: Likewise.
    * libmailutils/munre.c: Likewise.
    * libmailutils/mutil.c: Likewise.
    * libmailutils/nls.c: Likewise.
    * libmailutils/nullrec.c: Likewise.
    * libmailutils/observer.c: Likewise.
    * libmailutils/opool.c: Likewise.
    * libmailutils/parsedate.y: Likewise.
    * libmailutils/permstr.c: Likewise.
    * libmailutils/property.c: Likewise.
    * libmailutils/registrar.c: Likewise.
    * libmailutils/refcount.c: Likewise.
    * libmailutils/rfc2047.c: Likewise.
    * libmailutils/sha1.c: Likewise.
    * libmailutils/secret.c: Likewise.
    * libmailutils/ticket.c: Likewise.
    * libmailutils/url.c: Likewise.
    * libmailutils/vartab.c: Likewise.
    * libmailutils/version.c: Likewise.
    * libmailutils/wicket.c: Likewise.

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

Summary of changes:
 Makefile.am                                  |   29 +-
 am/config_paths.m4                           |    6 +-
 am/db2.m4                                    |    4 +-
 am/debug.m4                                  |    2 +-
 am/enable.m4                                 |    8 +-
 am/gsasl.m4                                  |    3 +-
 am/tls.m4                                    |    3 +-
 bootstrap.conf                               |   18 +-
 cmc/cmc_act_on.c                             |    4 +-
 cmc/cmc_free.c                               |    4 +-
 cmc/cmc_list.c                               |    4 +-
 cmc/cmc_logoff.c                             |    4 +-
 cmc/cmc_logon.c                              |    4 +-
 cmc/cmc_look_up.c                            |    4 +-
 cmc/cmc_query_config.c                       |    4 +-
 cmc/cmc_read.c                               |    4 +-
 cmc/cmc_send.c                               |    4 +-
 cmc/cmc_send_documents.c                     |    4 +-
 cmc/xcmc.h                                   |    4 +-
 comsat/Makefile.am                           |   29 +-
 comsat/action.c                              |    4 +-
 comsat/comsat.c                              |    4 +-
 comsat/comsat.h                              |    4 +-
 config/Makefile.am                           |   29 +-
 config/mailutils-config.c                    |    4 +-
 config/mailutils.m4                          |    4 +-
 configure.ac                                 |   17 +-
 doc/Makefile.am                              |   29 +-
 doc/man/Makefile.am                          |   29 +-
 doc/texinfo/Makefile.am                      |   29 +-
 dotlock/Makefile.am                          |   27 +-
 dotlock/dotlock.c                            |    4 +-
 examples/Makefile.am                         |   29 +-
 examples/aclck.c                             |    4 +-
 examples/addr.c                              |    4 +-
 examples/base64.c                            |    4 +-
 examples/config/Makefile.am                  |   29 +-
 examples/config/mailutils.schema             |    2 +-
 examples/cpp/Makefile.am                     |   29 +-
 examples/cpp/addr.cc                         |    8 +-
 examples/cpp/http.cc                         |    8 +-
 examples/cpp/iconv.cc                        |    8 +-
 examples/cpp/listop.cc                       |    8 +-
 examples/cpp/lsf.cc                          |    4 +-
 examples/cpp/mailcap.cc                      |    8 +-
 examples/cpp/mimetest.cc                     |    4 +-
 examples/cpp/msg-send.cc                     |    4 +-
 examples/cpp/murun.cc                        |    8 +-
 examples/cpp/sfrom.cc                        |    8 +-
 examples/cpp/url-parse.cc                    |    8 +-
 examples/header.c                            |    4 +-
 examples/http.c                              |    4 +-
 examples/iconv.c                             |    4 +-
 examples/lsf.c                               |    4 +-
 examples/msg-send.c                          |    4 +-
 examples/mta.c                               |    4 +-
 examples/muauth.c                            |    4 +-
 examples/mucat.c                             |    4 +-
 examples/muemail.c                           |    4 +-
 examples/murun.c                             |    4 +-
 examples/musocio.c                           |    4 +-
 examples/nntpclient.c                        |    4 +-
 examples/numaddr.c                           |    5 +-
 examples/pop3client.c                        |    4 +-
 examples/python/Makefile.am                  |   27 +-
 examples/python/addr.py                      |    4 +-
 examples/python/auth.py                      |    4 +-
 examples/python/iconv.py                     |    4 +-
 examples/python/lsf.py                       |    4 +-
 examples/python/mailcap.py                   |    4 +-
 examples/python/mimetest.py                  |    4 +-
 examples/python/msg-send.py                  |    4 +-
 examples/python/sfrom.py                     |    4 +-
 examples/python/url-parse.py                 |    4 +-
 examples/scheme/Makefile.am                  |   27 +-
 examples/scheme/reply.scm                    |    3 +-
 examples/sfrom.c                             |    4 +-
 frm/Makefile.am                              |   29 +-
 frm/common.c                                 |    4 +-
 frm/frm.c                                    |    4 +-
 frm/frm.h                                    |    4 +-
 frm/from.c                                   |    4 +-
 frm/tests/Makefile.am                        |    2 +-
 frm/tests/testsuite.at                       |    2 +-
 guimb/Makefile.am                            |   29 +-
 guimb/collect.c                              |    4 +-
 guimb/guimb.h                                |    4 +-
 guimb/main.c                                 |    4 +-
 guimb/scm/Makefile.am                        |   29 +-
 guimb/scm/mimeheader.scm                     |    3 +-
 guimb/scm/numaddr.scm                        |    3 +-
 guimb/scm/redirect.scm                       |    3 +-
 guimb/scm/reject.scm                         |    3 +-
 guimb/scm/sieve-core.scm                     |    3 +-
 guimb/scm/sieve.scm.in                       |    3 +-
 guimb/scm/vacation.scm                       |    3 +-
 guimb/util.c                                 |    4 +-
 imap4d/Makefile.am                           |   29 +-
 imap4d/append.c                              |    4 +-
 imap4d/auth_gsasl.c                          |    4 +-
 imap4d/auth_gss.c                            |    4 +-
 imap4d/authenticate.c                        |    4 +-
 imap4d/bye.c                                 |    4 +-
 imap4d/capability.c                          |    4 +-
 imap4d/check.c                               |    4 +-
 imap4d/close.c                               |    4 +-
 imap4d/commands.c                            |    4 +-
 imap4d/copy.c                                |    4 +-
 imap4d/create.c                              |    4 +-
 imap4d/delete.c                              |    4 +-
 imap4d/examine.c                             |    4 +-
 imap4d/expunge.c                             |    4 +-
 imap4d/fetch.c                               |    4 +-
 imap4d/id.c                                  |    4 +-
 imap4d/idle.c                                |    4 +-
 imap4d/imap4d.c                              |    4 +-
 imap4d/imap4d.h                              |    4 +-
 imap4d/io.c                                  |    4 +-
 imap4d/list.c                                |    4 +-
 imap4d/login.c                               |    4 +-
 imap4d/logout.c                              |    4 +-
 imap4d/lsub.c                                |    4 +-
 imap4d/namespace.c                           |    4 +-
 imap4d/noop.c                                |    4 +-
 imap4d/parsebuf.c                            |    4 +-
 imap4d/preauth.c                             |    4 +-
 imap4d/rename.c                              |    4 +-
 imap4d/search.c                              |    4 +-
 imap4d/select.c                              |    4 +-
 imap4d/signal.c                              |    4 +-
 imap4d/starttls.c                            |    4 +-
 imap4d/status.c                              |    4 +-
 imap4d/store.c                               |    4 +-
 imap4d/subscribe.c                           |    4 +-
 imap4d/sync.c                                |    4 +-
 imap4d/testsuite/Makefile.am                 |   27 +-
 imap4d/testsuite/imap4d/IDEF0955.exp         |    8 +-
 imap4d/testsuite/imap4d/IDEF0956.exp         |    8 +-
 imap4d/testsuite/imap4d/anystate.exp         |    8 +-
 imap4d/testsuite/imap4d/append.exp           |    8 +-
 imap4d/testsuite/imap4d/create.exp           |    8 +-
 imap4d/testsuite/imap4d/examine.exp          |    8 +-
 imap4d/testsuite/imap4d/expunge.exp          |    8 +-
 imap4d/testsuite/imap4d/fetch.exp            |    8 +-
 imap4d/testsuite/imap4d/list.exp             |    8 +-
 imap4d/testsuite/imap4d/search.exp           |    8 +-
 imap4d/testsuite/imap4d/x.exp                |    8 +-
 imap4d/testsuite/lib/imap4d.exp              |    8 +-
 imap4d/uid.c                                 |    4 +-
 imap4d/unsubscribe.c                         |    4 +-
 imap4d/util.c                                |    4 +-
 include/Makefile.am                          |   29 +-
 include/mailutils/Makefile.am                |   31 +-
 include/mailutils/address.h                  |    5 +-
 include/mailutils/alloc.h                    |    5 +-
 include/mailutils/argcv.h                    |    5 +-
 include/mailutils/assoc.h                    |    5 +-
 include/mailutils/attribute.h                |    5 +-
 include/mailutils/auth.h                     |    5 +-
 include/mailutils/body.h                     |    5 +-
 include/mailutils/cfg.h                      |    4 +-
 include/mailutils/cpp/Makefile.am            |   26 +-
 include/mailutils/cpp/address.h              |    9 +-
 include/mailutils/cpp/attribute.h            |    9 +-
 include/mailutils/cpp/body.h                 |    9 +-
 include/mailutils/cpp/debug.h                |    9 +-
 include/mailutils/cpp/envelope.h             |    9 +-
 include/mailutils/cpp/error.h                |    9 +-
 include/mailutils/cpp/filter.h               |    9 +-
 include/mailutils/cpp/folder.h               |    9 +-
 include/mailutils/cpp/header.h               |    9 +-
 include/mailutils/cpp/iterator.h             |    9 +-
 include/mailutils/cpp/list.h                 |    9 +-
 include/mailutils/cpp/mailbox.h              |    9 +-
 include/mailutils/cpp/mailcap.h              |    9 +-
 include/mailutils/cpp/mailer.h               |    9 +-
 include/mailutils/cpp/mailutils.h            |    9 +-
 include/mailutils/cpp/message.h              |    9 +-
 include/mailutils/cpp/mime.h                 |    9 +-
 include/mailutils/cpp/mutil.h                |    9 +-
 include/mailutils/cpp/pop3.h                 |    9 +-
 include/mailutils/cpp/registrar.h            |    9 +-
 include/mailutils/cpp/secret.h               |    9 +-
 include/mailutils/cpp/sieve.h                |    9 +-
 include/mailutils/cpp/stream.h               |    9 +-
 include/mailutils/cpp/url.h                  |    9 +-
 include/mailutils/daemon.h                   |    5 +-
 include/mailutils/debug.hm4                  |    5 +-
 include/mailutils/diag.h                     |    5 +-
 include/mailutils/envelope.h                 |    5 +-
 include/mailutils/errno.hin                  |    5 +-
 include/mailutils/error.h                    |    5 +-
 include/mailutils/filter.h                   |    5 +-
 include/mailutils/folder.h                   |    5 +-
 include/mailutils/gocs.h                     |    5 +-
 include/mailutils/gsasl.h                    |    5 +-
 include/mailutils/guile.h                    |    5 +-
 include/mailutils/header.h                   |    5 +-
 include/mailutils/io.h                       |    5 +-
 include/mailutils/iterator.h                 |    5 +-
 include/mailutils/ldap.h                     |    5 +-
 include/mailutils/libargp.h                  |    5 +-
 include/mailutils/libcfg.h                   |    5 +-
 include/mailutils/list.h                     |    5 +-
 include/mailutils/locker.h                   |    5 +-
 include/mailutils/mailbox.h                  |    5 +-
 include/mailutils/mailcap.h                  |    5 +-
 include/mailutils/mailer.h                   |    5 +-
 include/mailutils/md5.h                      |    3 +-
 include/mailutils/message.h                  |    5 +-
 include/mailutils/mime.h                     |    5 +-
 include/mailutils/monitor.h                  |    5 +-
 include/mailutils/mu_auth.h                  |    5 +-
 include/mailutils/mutil.h                    |    5 +-
 include/mailutils/nls.h                      |    5 +-
 include/mailutils/nntp.h                     |    5 +-
 include/mailutils/observer.h                 |    5 +-
 include/mailutils/opool.h                    |    5 +-
 include/mailutils/pam.h                      |    5 +-
 include/mailutils/parse822.h                 |    5 +-
 include/mailutils/pop3.h                     |    5 +-
 include/mailutils/progmailer.h               |    5 +-
 include/mailutils/property.h                 |    5 +-
 include/mailutils/python.h                   |    5 +-
 include/mailutils/radius.h                   |    5 +-
 include/mailutils/refcount.h                 |    5 +-
 include/mailutils/registrar.h                |    5 +-
 include/mailutils/secret.h                   |    5 +-
 include/mailutils/sha1.h                     |    3 +-
 include/mailutils/sieve.h                    |    5 +-
 include/mailutils/sql.h                      |    5 +-
 include/mailutils/sys/Makefile.am            |   27 +-
 include/mailutils/sys/amd.h                  |    5 +-
 include/mailutils/sys/attribute.h            |    5 +-
 include/mailutils/sys/auth.h                 |    5 +-
 include/mailutils/sys/body.h                 |    5 +-
 include/mailutils/sys/debug.h                |    5 +-
 include/mailutils/sys/envelope.h             |    5 +-
 include/mailutils/sys/filter.h               |    5 +-
 include/mailutils/sys/folder.h               |    5 +-
 include/mailutils/sys/header.h               |    5 +-
 include/mailutils/sys/imap.h                 |    5 +-
 include/mailutils/sys/iterator.h             |    5 +-
 include/mailutils/sys/list.h                 |    5 +-
 include/mailutils/sys/mailbox.h              |    5 +-
 include/mailutils/sys/mailer.h               |    5 +-
 include/mailutils/sys/message.h              |    5 +-
 include/mailutils/sys/mime.h                 |    5 +-
 include/mailutils/sys/monitor.h              |    5 +-
 include/mailutils/sys/nntp.h                 |    5 +-
 include/mailutils/sys/observer.h             |    5 +-
 include/mailutils/sys/pop3.h                 |    5 +-
 include/mailutils/sys/property.h             |    5 +-
 include/mailutils/sys/registrar.h            |    5 +-
 include/mailutils/sys/url.h                  |    5 +-
 include/mailutils/syslog.h                   |    5 +-
 include/mailutils/tls.h                      |    5 +-
 include/mailutils/types.hin                  |    5 +-
 include/mailutils/url.h                      |    5 +-
 include/mailutils/version.h                  |    5 +-
 lib/Makefile.am                              |   29 +-
 lib/mailcap.c                                |    4 +-
 lib/mu_asprintf.h                            |    8 +-
 lib/mu_dbm.c                                 |    4 +-
 lib/mu_dbm.h                                 |    4 +-
 lib/mu_umaxtostr.c                           |    5 +-
 lib/muaux.h                                  |    4 +-
 lib/signal.c                                 |    4 +-
 lib/strexit.c                                |    4 +-
 lib/tcpwrap.c                                |    4 +-
 lib/tcpwrap.h                                |    4 +-
 lib/userprivs.c                              |    4 +-
 lib/utmp.c                                   |    6 +-
 libmailutils/.gitignore                      |    8 -
 libmailutils/Makefile.am                     |  218 +---
 libmailutils/address.c                       |  595 --------
 libmailutils/address/Makefile.am             |   24 +
 libmailutils/address/address.c               |  594 ++++++++
 libmailutils/address/parse822.c              | 2039 +++++++++++++++++++++++++
 libmailutils/amd.c                           | 2033 -------------------------
 libmailutils/argcv.c                         |  549 -------
 libmailutils/asnprintf.c                     |   40 -
 libmailutils/asprintf.c                      |   42 -
 libmailutils/assoc.c                         |  529 -------
 libmailutils/attachment.c                    |  461 ------
 libmailutils/attribute.c                     |  470 ------
 libmailutils/auth.c                          |  164 --
 libmailutils/auth/Makefile.am                |   25 +
 libmailutils/auth/auth.c                     |  163 ++
 libmailutils/auth/mu_auth.c                  |  538 +++++++
 libmailutils/auth/system.c                   |  159 ++
 libmailutils/base/.gitignore                 |    1 +
 libmailutils/base/Makefile.am                |   80 +
 libmailutils/{ => base}/alloc.c              |    0
 libmailutils/base/amd.c                      | 2032 +++++++++++++++++++++++++
 libmailutils/base/argcv.c                    |  548 +++++++
 libmailutils/base/assoc.c                    |  527 +++++++
 libmailutils/base/daemon.c                   |  188 +++
 libmailutils/base/date.c                     |  244 +++
 libmailutils/base/fgetpwent.c                |  162 ++
 libmailutils/base/freeitem.c                 |   28 +
 libmailutils/{ => base}/getpass.c            |    0
 libmailutils/base/iterator.c                 |  283 ++++
 libmailutils/{ => base}/kwd.c                |    0
 libmailutils/base/list.c                     |  692 +++++++++
 libmailutils/base/listlist.c                 |  138 ++
 libmailutils/base/locale.c                   |  241 +++
 libmailutils/base/locker.c                   | 1026 +++++++++++++
 libmailutils/base/mailcap.c                  |  739 ++++++++++
 libmailutils/base/md5.c                      |  453 ++++++
 libmailutils/base/monitor.c                  |  281 ++++
 libmailutils/base/munre.c                    |   92 ++
 libmailutils/base/mutil.c                    | 1535 +++++++++++++++++++
 libmailutils/base/nls.c                      |   61 +
 libmailutils/{ => base}/nullrec.c            |    0
 libmailutils/base/observer.c                 |  238 +++
 libmailutils/base/opool.c                    |  446 ++++++
 libmailutils/base/parsedate.y                | 1205 +++++++++++++++
 libmailutils/base/permstr.c                  |  127 ++
 libmailutils/base/property.c                 |  191 +++
 libmailutils/base/refcount.c                 |  107 ++
 libmailutils/base/registrar.c                |  442 ++++++
 libmailutils/base/rfc2047.c                  |  313 ++++
 libmailutils/base/secret.c                   |  134 ++
 libmailutils/base/sha1.c                     |  417 ++++++
 libmailutils/base/ticket.c                   |  244 +++
 libmailutils/base/url.c                      | 1139 ++++++++++++++
 libmailutils/{ => base}/vartab.c             |    0
 libmailutils/base/version.c                  |  187 +++
 libmailutils/base/wicket.c                   |  356 +++++
 libmailutils/body.c                          |  417 ------
 libmailutils/cfg/.gitignore                  |    3 +
 libmailutils/cfg/Makefile.am                 |   54 +
 libmailutils/cfg/driver.c                    |  754 ++++++++++
 libmailutils/cfg/format.c                    |  395 +++++
 libmailutils/cfg/gocs.c                      |  396 +++++
 libmailutils/cfg/lexer.l                     |  405 +++++
 libmailutils/cfg/parser.y                    | 1839 +++++++++++++++++++++++
 libmailutils/cfg_driver.c                    |  754 ----------
 libmailutils/cfg_format.c                    |  395 -----
 libmailutils/cfg_lexer.l                     |  405 -----
 libmailutils/cfg_parser.y                    | 1839 -----------------------
 libmailutils/crlfdot.c                       |  336 -----
 libmailutils/daemon.c                        |  189 ---
 libmailutils/date.c                          |  245 ---
 libmailutils/dbgstderr.c                     |   36 -
 libmailutils/dbgsyslog.c                     |   34 -
 libmailutils/debug.c                         |  299 ----
 libmailutils/diag.c                          |  179 ---
 libmailutils/diag/.gitignore                 |    1 +
 libmailutils/diag/Makefile.am                |   42 +
 libmailutils/diag/dbgstderr.c                |   35 +
 libmailutils/diag/dbgsyslog.c                |   33 +
 libmailutils/diag/debug.c                    |  298 ++++
 libmailutils/diag/diag.c                     |  178 +++
 libmailutils/diag/errors                     |   94 ++
 libmailutils/diag/gdebug.c                   |  262 ++++
 libmailutils/diag/muerrno.cin                |   65 +
 libmailutils/diag/muerror.c                  |  116 ++
 libmailutils/diag/syslog.c                   |  145 ++
 libmailutils/dot.c                           |  268 ----
 libmailutils/envelope.c                      |  181 ---
 libmailutils/errors                          |   96 --
 libmailutils/fgetpwent.c                     |  163 --
 libmailutils/filter.c                        |  194 ---
 libmailutils/filter/Makefile.am              |   33 +
 libmailutils/{ => filter}/base64.c           |    0
 libmailutils/{ => filter}/binflt.c           |    0
 libmailutils/filter/crlfdot.c                |  335 +++++
 libmailutils/{ => filter}/crlfflt.c          |    0
 libmailutils/filter/dot.c                    |  267 ++++
 libmailutils/filter/filter.c                 |  193 +++
 libmailutils/filter/filter_iconv.c           |  481 ++++++
 libmailutils/filter/fromflt.c                |  282 ++++
 libmailutils/{ => filter}/linelenflt.c       |    0
 libmailutils/{ => filter}/qpflt.c            |    0
 libmailutils/filter_iconv.c                  |  482 ------
 libmailutils/folder.c                        |  511 -------
 libmailutils/freeitem.c                      |   29 -
 libmailutils/fromflt.c                       |  283 ----
 libmailutils/gdebug.c                        |  263 ----
 libmailutils/gocs.c                          |  397 -----
 libmailutils/hdritr.c                        |  162 --
 libmailutils/header.c                        | 1222 ---------------
 libmailutils/iterator.c                      |  284 ----
 libmailutils/list.c                          |  693 ---------
 libmailutils/listlist.c                      |  139 --
 libmailutils/locale.c                        |  242 ---
 libmailutils/locker.c                        | 1027 -------------
 libmailutils/mailbox.c                       |  819 -----------
 libmailutils/mailbox/Makefile.am             |   33 +
 libmailutils/mailbox/attribute.c             |  469 ++++++
 libmailutils/mailbox/body.c                  |  416 ++++++
 libmailutils/mailbox/envelope.c              |  180 +++
 libmailutils/mailbox/folder.c                |  510 +++++++
 libmailutils/mailbox/hdritr.c                |  161 ++
 libmailutils/mailbox/header.c                | 1221 +++++++++++++++
 libmailutils/mailbox/mailbox.c               |  818 +++++++++++
 libmailutils/mailbox/mbx_default.c           |  453 ++++++
 libmailutils/mailbox/mbxitr.c                |  159 ++
 libmailutils/mailbox/message.c               | 1293 ++++++++++++++++
 libmailutils/{ => mailbox}/msgscan.c         |    0
 libmailutils/mailcap.c                       |  740 ----------
 libmailutils/mailer.c                        |  698 ---------
 libmailutils/mailer/Makefile.am              |   24 +
 libmailutils/mailer/mailer.c                 |  697 +++++++++
 libmailutils/mailer/progmailer.c             |  313 ++++
 libmailutils/mbx_default.c                   |  454 ------
 libmailutils/mbxitr.c                        |  160 --
 libmailutils/md5.c                           |  454 ------
 libmailutils/message.c                       | 1294 ----------------
 libmailutils/message_stream.c                |  440 ------
 libmailutils/mime.c                          | 1071 --------------
 libmailutils/mime/Makefile.am                |   25 +
 libmailutils/mime/attachment.c               |  460 ++++++
 libmailutils/mime/mime.c                     | 1070 ++++++++++++++
 libmailutils/{ => mime}/mimehdr.c            |    0
 libmailutils/monitor.c                       |  282 ----
 libmailutils/mu_auth.c                       |  539 -------
 libmailutils/muerrno.cin                     |   65 -
 libmailutils/muerror.c                       |  117 --
 libmailutils/munre.c                         |   93 --
 libmailutils/mutil.c                         | 1536 -------------------
 libmailutils/nls.c                           |   62 -
 libmailutils/observer.c                      |  239 ---
 libmailutils/opool.c                         |  446 ------
 libmailutils/parse822.c                      | 2040 --------------------------
 libmailutils/parsedate.y                     | 1206 ---------------
 libmailutils/permstr.c                       |  128 --
 libmailutils/progmailer.c                    |  314 ----
 libmailutils/property.c                      |  192 ---
 libmailutils/refcount.c                      |  107 --
 libmailutils/registrar.c                     |  443 ------
 libmailutils/rfc2047.c                       |  314 ----
 libmailutils/secret.c                        |  135 --
 libmailutils/server/Makefile.am              |   26 +
 libmailutils/{ => server}/acl.c              |    0
 libmailutils/{ => server}/ipsrv.c            |    0
 libmailutils/{ => server}/msrv.c             |    0
 libmailutils/{ => server}/server.c           |    0
 libmailutils/sha1.c                          |  418 ------
 libmailutils/size_max.h                      |   31 -
 libmailutils/stream/Makefile.am              |   41 +
 libmailutils/{ => stream}/dbgstream.c        |    0
 libmailutils/{ => stream}/file_stream.c      |    0
 libmailutils/{ => stream}/fltstream.c        |    0
 libmailutils/{ => stream}/iostream.c         |    0
 libmailutils/{ => stream}/mapfile_stream.c   |    0
 libmailutils/{ => stream}/memory_stream.c    |    0
 libmailutils/stream/message_stream.c         |  438 ++++++
 libmailutils/{ => stream}/prog_stream.c      |    0
 libmailutils/{ => stream}/rdcache_stream.c   |    0
 libmailutils/{ => stream}/socket_stream.c    |    0
 libmailutils/{ => stream}/stdio_stream.c     |    0
 libmailutils/{ => stream}/stream.c           |    0
 libmailutils/{ => stream}/stream_printf.c    |    0
 libmailutils/{ => stream}/stream_vprintf.c   |    0
 libmailutils/{ => stream}/streamcpy.c        |    0
 libmailutils/{ => stream}/streamref.c        |    0
 libmailutils/stream/tcp.c                    |  362 +++++
 libmailutils/{ => stream}/temp_file_stream.c |    0
 libmailutils/{ => stream}/xscript-stream.c   |    0
 libmailutils/string/Makefile.am              |   34 +
 libmailutils/string/asnprintf.c              |   39 +
 libmailutils/string/asprintf.c               |   40 +
 libmailutils/{ => string}/cstrcasecmp.c      |    0
 libmailutils/{ => string}/cstrlower.c        |    0
 libmailutils/{ => string}/cstrupper.c        |    0
 libmailutils/{ => string}/mkfilename.c       |    0
 libmailutils/{ => string}/muctype.c          |    0
 libmailutils/{ => string}/stripws.c          |    0
 libmailutils/{ => string}/strltrim.c         |    0
 libmailutils/{ => string}/strrtrim.c         |    0
 libmailutils/{ => string}/strskip.c          |    0
 libmailutils/string/vasnprintf.c             |   86 ++
 libmailutils/syslog.c                        |  145 --
 libmailutils/system.c                        |  160 --
 libmailutils/tcp.c                           |  363 -----
 libmailutils/tests/Makefile.am               |    2 +-
 libmailutils/tests/addr.c                    |    4 +-
 libmailutils/tests/address.at                |    2 +-
 libmailutils/tests/argcv.at                  |    2 +-
 libmailutils/tests/argcv.c                   |    4 +-
 libmailutils/tests/base64d.at                |    2 +-
 libmailutils/tests/base64e.at                |    2 +-
 libmailutils/tests/decode2047.at             |    2 +-
 libmailutils/tests/decode2047.c              |    4 +-
 libmailutils/tests/encode2047.at             |    2 +-
 libmailutils/tests/encode2047.c              |    4 +-
 libmailutils/tests/fltst.c                   |    4 +-
 libmailutils/tests/fromflt.at                |    2 +-
 libmailutils/tests/list.at                   |    2 +-
 libmailutils/tests/listop.c                  |    4 +-
 libmailutils/tests/mailcap.at                |    2 +-
 libmailutils/tests/mailcap.c                 |    4 +-
 libmailutils/tests/testsuite.at              |    2 +-
 libmailutils/tests/url-parse.c               |    4 +-
 libmailutils/tests/url.at                    |    2 +-
 libmailutils/ticket.c                        |  245 ---
 libmailutils/url.c                           | 1140 --------------
 libmailutils/vasnprintf.c                    |   87 --
 libmailutils/version.c                       |  189 ---
 libmailutils/wicket.c                        |  357 -----
 libmu_argp/Makefile.am                       |    4 +-
 libmu_argp/auth.c                            |    5 +-
 libmu_argp/cmdline.c                         |    5 +-
 libmu_argp/cmdline.h                         |    5 +-
 libmu_argp/common.c                          |    5 +-
 libmu_argp/mu_argp.c                         |    5 +-
 libmu_argp/muinit.c                          |    5 +-
 libmu_argp/sieve.c                           |    5 +-
 libmu_argp/tls.c                             |    5 +-
 libmu_auth/Makefile.am                       |   29 +-
 libmu_auth/gsasl.c                           |    5 +-
 libmu_auth/ldap.c                            |    5 +-
 libmu_auth/pam.c                             |    5 +-
 libmu_auth/radius.c                          |    5 +-
 libmu_auth/sql.c                             |    5 +-
 libmu_auth/sql.h                             |    5 +-
 libmu_auth/tls.c                             |    5 +-
 libmu_auth/virtual.c                         |    5 +-
 libmu_cfg/Makefile.am                        |    4 +-
 libmu_cfg/acl.c                              |    4 +-
 libmu_cfg/auth.c                             |    4 +-
 libmu_cfg/common.c                           |    4 +-
 libmu_cfg/gsasl.c                            |    4 +-
 libmu_cfg/init.c                             |    4 +-
 libmu_cfg/ldap.c                             |    4 +-
 libmu_cfg/pam.c                              |    4 +-
 libmu_cfg/radius.c                           |    4 +-
 libmu_cfg/sieve.c                            |    4 +-
 libmu_cfg/sql.c                              |    4 +-
 libmu_cfg/tls.c                              |    4 +-
 libmu_cfg/virtdomain.c                       |    4 +-
 libmu_cpp/Makefile.am                        |   29 +-
 libmu_cpp/address.cc                         |    9 +-
 libmu_cpp/attribute.cc                       |    9 +-
 libmu_cpp/body.cc                            |    9 +-
 libmu_cpp/debug.cc                           |    9 +-
 libmu_cpp/envelope.cc                        |    9 +-
 libmu_cpp/filter.cc                          |    9 +-
 libmu_cpp/folder.cc                          |    9 +-
 libmu_cpp/header.cc                          |    9 +-
 libmu_cpp/iterator.cc                        |    9 +-
 libmu_cpp/list.cc                            |    9 +-
 libmu_cpp/mailbox.cc                         |    9 +-
 libmu_cpp/mailcap.cc                         |    9 +-
 libmu_cpp/mailer.cc                          |    9 +-
 libmu_cpp/message.cc                         |    9 +-
 libmu_cpp/mime.cc                            |    9 +-
 libmu_cpp/mutil.cc                           |    9 +-
 libmu_cpp/pop3.cc                            |    9 +-
 libmu_cpp/registrar.cc                       |    9 +-
 libmu_cpp/secret.cc                          |    9 +-
 libmu_cpp/sieve.cc                           |    9 +-
 libmu_cpp/stream.cc                          |    9 +-
 libmu_cpp/url.cc                             |    9 +-
 libmu_scm/Makefile.am                        |   29 +-
 libmu_scm/mailutils.scm.in                   |    3 +-
 libmu_scm/mu_address.c                       |    5 +-
 libmu_scm/mu_body.c                          |    5 +-
 libmu_scm/mu_dbgport.c                       |    5 +-
 libmu_scm/mu_guile.c                         |    5 +-
 libmu_scm/mu_logger.c                        |    5 +-
 libmu_scm/mu_mailbox.c                       |    5 +-
 libmu_scm/mu_message.c                       |    5 +-
 libmu_scm/mu_mime.c                          |    5 +-
 libmu_scm/mu_port.c                          |    5 +-
 libmu_scm/mu_scm.c                           |    5 +-
 libmu_scm/mu_scm.h                           |    5 +-
 libmu_scm/mu_util.c                          |    5 +-
 libmu_sieve/Makefile.am                      |   29 +-
 libmu_sieve/actions.c                        |    5 +-
 libmu_sieve/comparator.c                     |    5 +-
 libmu_sieve/conf.c                           |    5 +-
 libmu_sieve/extensions/Makefile.am           |   29 +-
 libmu_sieve/extensions/list.c                |    5 +-
 libmu_sieve/extensions/moderator.c           |    5 +-
 libmu_sieve/extensions/pipe.c                |    5 +-
 libmu_sieve/extensions/spamd.c               |    5 +-
 libmu_sieve/extensions/timestamp.c           |    5 +-
 libmu_sieve/extensions/vacation.c            |    5 +-
 libmu_sieve/load.c                           |    5 +-
 libmu_sieve/prog.c                           |    5 +-
 libmu_sieve/register.c                       |    5 +-
 libmu_sieve/relational.c                     |    5 +-
 libmu_sieve/require.c                        |    5 +-
 libmu_sieve/runtime.c                        |    5 +-
 libmu_sieve/sieve-priv.h                     |    5 +-
 libmu_sieve/sieve.l                          |    5 +-
 libmu_sieve/sieve.y                          |    5 +-
 libmu_sieve/tests.c                          |    5 +-
 libmu_sieve/util.c                           |    5 +-
 libproto/Makefile.am                         |   27 +-
 libproto/imap/Makefile.am                    |   29 +-
 libproto/imap/folder.c                       |    5 +-
 libproto/imap/mbox.c                         |    5 +-
 libproto/imap/url.c                          |    5 +-
 libproto/maildir/Makefile.am                 |   29 +-
 libproto/maildir/folder.c                    |    5 +-
 libproto/maildir/maildir.h                   |    5 +-
 libproto/maildir/mbox.c                      |    5 +-
 libproto/mailer/Makefile.am                  |   27 +-
 libproto/mailer/mbox.c                       |    5 +-
 libproto/mailer/sendmail.c                   |    5 +-
 libproto/mailer/smtp.c                       |    5 +-
 libproto/mbox/Makefile.am                    |   29 +-
 libproto/mbox/folder.c                       |    5 +-
 libproto/mbox/mbox.c                         |    5 +-
 libproto/mbox/mbox0.h                        |    5 +-
 libproto/mbox/mboxscan.c                     |    5 +-
 libproto/mh/Makefile.am                      |   29 +-
 libproto/mh/folder.c                         |    5 +-
 libproto/mh/mbox.c                           |    5 +-
 libproto/nntp/Makefile.am                    |   29 +-
 libproto/nntp/folder.c                       |    5 +-
 libproto/nntp/mbox.c                         |    5 +-
 libproto/nntp/nntp0.h                        |    5 +-
 libproto/nntp/nntp_article.c                 |    5 +-
 libproto/nntp/nntp_body.c                    |    5 +-
 libproto/nntp/nntp_carrier.c                 |    5 +-
 libproto/nntp/nntp_connect.c                 |    5 +-
 libproto/nntp/nntp_create.c                  |    5 +-
 libproto/nntp/nntp_date.c                    |    5 +-
 libproto/nntp/nntp_debug.c                   |    5 +-
 libproto/nntp/nntp_destroy.c                 |    5 +-
 libproto/nntp/nntp_disconnect.c              |    5 +-
 libproto/nntp/nntp_group.c                   |    5 +-
 libproto/nntp/nntp_head.c                    |    5 +-
 libproto/nntp/nntp_help.c                    |    5 +-
 libproto/nntp/nntp_ihave.c                   |    5 +-
 libproto/nntp/nntp_iterator.c                |    7 +-
 libproto/nntp/nntp_last.c                    |    5 +-
 libproto/nntp/nntp_list_active.c             |    5 +-
 libproto/nntp/nntp_list_distribpats.c        |    5 +-
 libproto/nntp/nntp_list_distributions.c      |    5 +-
 libproto/nntp/nntp_list_extensions.c         |    5 +-
 libproto/nntp/nntp_list_newsgroups.c         |    5 +-
 libproto/nntp/nntp_list_times.c              |    5 +-
 libproto/nntp/nntp_mode_reader.c             |    5 +-
 libproto/nntp/nntp_newgroups.c               |    5 +-
 libproto/nntp/nntp_newnews.c                 |    5 +-
 libproto/nntp/nntp_next.c                    |    5 +-
 libproto/nntp/nntp_post.c                    |    5 +-
 libproto/nntp/nntp_quit.c                    |    5 +-
 libproto/nntp/nntp_readline.c                |    5 +-
 libproto/nntp/nntp_response.c                |    5 +-
 libproto/nntp/nntp_sendline.c                |    5 +-
 libproto/nntp/nntp_stat.c                    |    5 +-
 libproto/nntp/nntp_stream.c                  |    5 +-
 libproto/nntp/nntp_timeout.c                 |    5 +-
 libproto/nntp/url.c                          |    5 +-
 libproto/pop/Makefile.am                     |   29 +-
 libproto/pop/folder.c                        |    5 +-
 libproto/pop/mbox.c                          |    5 +-
 libproto/pop/pop3_apop.c                     |    5 +-
 libproto/pop/pop3_capa.c                     |    5 +-
 libproto/pop/pop3_capatst.c                  |    5 +-
 libproto/pop/pop3_carrier.c                  |    5 +-
 libproto/pop/pop3_connect.c                  |    5 +-
 libproto/pop/pop3_create.c                   |    5 +-
 libproto/pop/pop3_dele.c                     |    5 +-
 libproto/pop/pop3_destroy.c                  |    5 +-
 libproto/pop/pop3_disconnect.c               |    5 +-
 libproto/pop/pop3_iterator.c                 |    7 +-
 libproto/pop/pop3_list.c                     |    5 +-
 libproto/pop/pop3_list_cmd.c                 |    5 +-
 libproto/pop/pop3_lista.c                    |    5 +-
 libproto/pop/pop3_listas.c                   |    5 +-
 libproto/pop/pop3_noop.c                     |    5 +-
 libproto/pop/pop3_pass.c                     |    5 +-
 libproto/pop/pop3_quit.c                     |    5 +-
 libproto/pop/pop3_rdlist.c                   |    5 +-
 libproto/pop/pop3_readline.c                 |    5 +-
 libproto/pop/pop3_response.c                 |    5 +-
 libproto/pop/pop3_retr.c                     |    5 +-
 libproto/pop/pop3_rset.c                     |    5 +-
 libproto/pop/pop3_sendline.c                 |    5 +-
 libproto/pop/pop3_stat.c                     |    5 +-
 libproto/pop/pop3_stls.c                     |    5 +-
 libproto/pop/pop3_stream.c                   |    5 +-
 libproto/pop/pop3_timeout.c                  |    5 +-
 libproto/pop/pop3_top.c                      |    5 +-
 libproto/pop/pop3_trace.c                    |    5 +-
 libproto/pop/pop3_uidl.c                     |    5 +-
 libproto/pop/pop3_uidl_cmd.c                 |    5 +-
 libproto/pop/pop3_uidla.c                    |    5 +-
 libproto/pop/pop3_uidlas.c                   |    5 +-
 libproto/pop/pop3_user.c                     |    5 +-
 libproto/pop/url.c                           |    5 +-
 maidag/Makefile.am                           |    6 +-
 maidag/deliver.c                             |    4 +-
 maidag/forward.c                             |    4 +-
 maidag/guile.c                               |    4 +-
 maidag/lmtp.c                                |    4 +-
 maidag/maidag.c                              |    4 +-
 maidag/maidag.h                              |    4 +-
 maidag/mailquota.c                           |    4 +-
 maidag/python.c                              |    4 +-
 maidag/script.c                              |    4 +-
 maidag/sieve.c                               |    4 +-
 maidag/tests/Makefile.am                     |    2 +-
 maidag/util.c                                |    4 +-
 mail/Makefile.am                             |   29 +-
 mail/alias.c                                 |    4 +-
 mail/alt.c                                   |    4 +-
 mail/cd.c                                    |    4 +-
 mail/copy.c                                  |    4 +-
 mail/decode.c                                |    4 +-
 mail/delete.c                                |    4 +-
 mail/dp.c                                    |    4 +-
 mail/echo.c                                  |    4 +-
 mail/edit.c                                  |    4 +-
 mail/eq.c                                    |    4 +-
 mail/escape.c                                |    4 +-
 mail/exit.c                                  |    4 +-
 mail/file.c                                  |    4 +-
 mail/folders.c                               |    4 +-
 mail/followup.c                              |    4 +-
 mail/from.c                                  |    4 +-
 mail/headers.c                               |    4 +-
 mail/help.c                                  |    4 +-
 mail/hold.c                                  |    4 +-
 mail/if.c                                    |    4 +-
 mail/inc.c                                   |    4 +-
 mail/list.c                                  |    4 +-
 mail/mail.c                                  |    7 +-
 mail/mail.h                                  |    4 +-
 mail/mailline.c                              |    4 +-
 mail/mbox.c                                  |    4 +-
 mail/msgset.y                                |    4 +-
 mail/next.c                                  |    4 +-
 mail/page.c                                  |    4 +-
 mail/pipe.c                                  |    4 +-
 mail/previous.c                              |    4 +-
 mail/print.c                                 |    4 +-
 mail/quit.c                                  |    4 +-
 mail/reply.c                                 |    4 +-
 mail/retain.c                                |    4 +-
 mail/save.c                                  |    4 +-
 mail/send.c                                  |    4 +-
 mail/set.c                                   |    4 +-
 mail/setenv.c                                |    4 +-
 mail/shell.c                                 |    4 +-
 mail/size.c                                  |    4 +-
 mail/source.c                                |    4 +-
 mail/summary.c                               |    4 +-
 mail/table.c                                 |    4 +-
 mail/tag.c                                   |    4 +-
 mail/testsuite/Makefile.am                   |   27 +-
 mail/testsuite/if.mail                       |    8 +-
 mail/testsuite/lib/mail.exp                  |    8 +-
 mail/testsuite/mail/alias.exp                |    8 +-
 mail/testsuite/mail/folder.exp               |    8 +-
 mail/testsuite/mail/if.exp                   |    8 +-
 mail/testsuite/mail/read.exp                 |    8 +-
 mail/testsuite/mail/send.exp                 |    8 +-
 mail/testsuite/mail/tag.exp                  |    8 +-
 mail/testsuite/mail/write.exp                |    8 +-
 mail/testsuite/mail/z.exp                    |    8 +-
 mail/testsuite/makespool                     |    3 +-
 mail/top.c                                   |    4 +-
 mail/touch.c                                 |    4 +-
 mail/unalias.c                               |    4 +-
 mail/undelete.c                              |    4 +-
 mail/unset.c                                 |    4 +-
 mail/util.c                                  |    4 +-
 mail/version.c                               |    4 +-
 mail/visual.c                                |    4 +-
 mail/write.c                                 |    4 +-
 mail/z.c                                     |    4 +-
 mapi/MAPIAddress.c                           |    4 +-
 mapi/MAPIDeleteMail.c                        |    4 +-
 mapi/MAPIDetails.c                           |    4 +-
 mapi/MAPIFindNext.c                          |    4 +-
 mapi/MAPIFreeBuffer.c                        |    4 +-
 mapi/MAPILogoff.c                            |    4 +-
 mapi/MAPILogon.c                             |    4 +-
 mapi/MAPIReadMail.c                          |    4 +-
 mapi/MAPISaveMail.c                          |    4 +-
 mapi/MAPISendDocuments.c                     |    4 +-
 mapi/MAPISendMail.c                          |    4 +-
 mapi/Makefile.am                             |   27 +-
 mapi/mapi.h                                  |    4 +-
 messages/Makefile.am                         |   29 +-
 messages/messages.c                          |    4 +-
 messages/tests/Makefile.am                   |    2 +-
 messages/tests/testsuite.at                  |    2 +-
 mh/Makefile.am                               |   29 +-
 mh/ali.c                                     |    4 +-
 mh/anno.c                                    |    4 +-
 mh/burst.c                                   |    4 +-
 mh/comp.c                                    |    4 +-
 mh/compcommon.c                              |    4 +-
 mh/fmtcheck.c                                |    4 +-
 mh/folder.c                                  |    4 +-
 mh/forw.c                                    |    4 +-
 mh/inc.c                                     |    4 +-
 mh/install-mh.c                              |    4 +-
 mh/mailutils-mh.eli                          |    3 +-
 mh/mark.c                                    |    4 +-
 mh/mh.h                                      |    4 +-
 mh/mh_alias.l                                |    4 +-
 mh/mh_alias.y                                |    4 +-
 mh/mh_argp.c                                 |    7 +-
 mh/mh_ctx.c                                  |    4 +-
 mh/mh_fmtgram.y                              |    4 +-
 mh/mh_format.c                               |    4 +-
 mh/mh_format.h                               |    4 +-
 mh/mh_getopt.c                               |    4 +-
 mh/mh_getopt.h                               |    4 +-
 mh/mh_global.c                               |    4 +-
 mh/mh_init.c                                 |    4 +-
 mh/mh_list.c                                 |    4 +-
 mh/mh_msgset.c                               |    4 +-
 mh/mh_sequence.c                             |    4 +-
 mh/mh_stream.c                               |    4 +-
 mh/mh_whatnow.c                              |    4 +-
 mh/mh_whom.c                                 |    4 +-
 mh/mhl.c                                     |    4 +-
 mh/mhn.c                                     |    4 +-
 mh/mhparam.c                                 |    4 +-
 mh/mhpath.c                                  |    4 +-
 mh/pick.c                                    |    4 +-
 mh/pick.h                                    |    4 +-
 mh/pick.y                                    |    4 +-
 mh/refile.c                                  |    4 +-
 mh/repl.c                                    |    4 +-
 mh/rmf.c                                     |    4 +-
 mh/rmm.c                                     |    4 +-
 mh/scan.c                                    |    4 +-
 mh/send.c                                    |    4 +-
 mh/sortm.c                                   |    4 +-
 mh/whatnow.c                                 |    4 +-
 mh/whom.c                                    |    4 +-
 mimeview/Makefile.am                         |   27 +-
 mimeview/mimetypes.l                         |    4 +-
 mimeview/mimetypes.y                         |    4 +-
 mimeview/mimeview.c                          |    4 +-
 mimeview/mimeview.h                          |    4 +-
 movemail/Makefile.am                         |   29 +-
 movemail/movemail.c                          |    4 +-
 mu-aux/Makefile.am                           |   29 +-
 mu-aux/debugdef.m4                           |    6 +-
 mu-aux/generr.awk                            |    8 +-
 mu-aux/gylwrap                               |    3 +-
 mu-aux/texify.sed                            |    6 +-
 mu/Makefile.am                               |    4 +-
 paths                                        |    6 +-
 po/POTFILES.in                               |   38 +-
 pop3d/Makefile.am                            |   29 +-
 pop3d/bulletin.c                             |    4 +-
 pop3d/expire.c                               |    4 +-
 pop3d/extra.c                                |    4 +-
 pop3d/lock.c                                 |    4 +-
 pop3d/logindelay.c                           |    4 +-
 pop3d/pop3d.c                                |    4 +-
 pop3d/pop3d.h                                |    4 +-
 pop3d/popauth.c                              |    4 +-
 pop3d/signal.c                               |    4 +-
 pop3d/stat.c                                 |    4 +-
 pop3d/testsuite/Makefile.am                  |   27 +-
 pop3d/testsuite/lib/pop3d.exp                |    8 +-
 pop3d/testsuite/pop3d/read.exp               |    8 +-
 python/Makefile.am                           |   27 +-
 python/libmu_py/Makefile.am                  |   27 +-
 python/libmu_py/address.c                    |    9 +-
 python/libmu_py/attribute.c                  |    9 +-
 python/libmu_py/auth.c                       |    9 +-
 python/libmu_py/body.c                       |    9 +-
 python/libmu_py/c_api.c                      |    9 +-
 python/libmu_py/debug.c                      |    9 +-
 python/libmu_py/envelope.c                   |    9 +-
 python/libmu_py/error.c                      |    9 +-
 python/libmu_py/filter.c                     |    9 +-
 python/libmu_py/folder.c                     |    9 +-
 python/libmu_py/header.c                     |    9 +-
 python/libmu_py/libmu_py.c                   |    9 +-
 python/libmu_py/libmu_py.h                   |    9 +-
 python/libmu_py/list.c                       |    9 +-
 python/libmu_py/mailbox.c                    |    9 +-
 python/libmu_py/mailcap.c                    |    9 +-
 python/libmu_py/mailer.c                     |    9 +-
 python/libmu_py/message.c                    |    9 +-
 python/libmu_py/mime.c                       |    9 +-
 python/libmu_py/nls.c                        |    9 +-
 python/libmu_py/registrar.c                  |    9 +-
 python/libmu_py/script.c                     |    9 +-
 python/libmu_py/secret.c                     |    9 +-
 python/libmu_py/sieve.c                      |    9 +-
 python/libmu_py/stream.c                     |    9 +-
 python/libmu_py/url.c                        |    9 +-
 python/libmu_py/util.c                       |    9 +-
 python/mailutils/Makefile.am                 |   27 +-
 python/mailutils/__init__.py                 |    4 +-
 python/mailutils/address.py                  |    7 +-
 python/mailutils/attribute.py                |    7 +-
 python/mailutils/auth.py                     |    7 +-
 python/mailutils/body.py                     |    7 +-
 python/mailutils/debug.py                    |    7 +-
 python/mailutils/envelope.py                 |    7 +-
 python/mailutils/error.py                    |    7 +-
 python/mailutils/filter.py                   |    7 +-
 python/mailutils/folder.py                   |    7 +-
 python/mailutils/header.py                   |    7 +-
 python/mailutils/mailbox.py                  |    7 +-
 python/mailutils/mailcap.py                  |    7 +-
 python/mailutils/mailer.py                   |    7 +-
 python/mailutils/message.py                  |    7 +-
 python/mailutils/mime.py                     |    7 +-
 python/mailutils/nls.py                      |    7 +-
 python/mailutils/registrar.py                |    7 +-
 python/mailutils/secret.py                   |    7 +-
 python/mailutils/sieve.py                    |    7 +-
 python/mailutils/stream.py                   |    7 +-
 python/mailutils/url.py                      |    7 +-
 python/mailutils/util.py                     |    7 +-
 readmsg/Makefile.am                          |   29 +-
 readmsg/msglist.c                            |    4 +-
 readmsg/readmsg.c                            |    4 +-
 readmsg/readmsg.h                            |    4 +-
 readmsg/tests/Makefile.am                    |    2 +-
 readmsg/tests/all.at                         |    2 +-
 readmsg/tests/hdr.at                         |    2 +-
 readmsg/tests/nohdr.at                       |    2 +-
 readmsg/tests/testsuite.at                   |    2 +-
 readmsg/tests/twomsg.at                      |    2 +-
 readmsg/tests/weed.at                        |    2 +-
 sieve/Makefile.am                            |   27 +-
 sieve/sieve.c                                |    4 +-
 sieve/tests/Makefile.am                      |    2 +-
 sieve/tests/action.at                        |    2 +-
 sieve/tests/address.at                       |    2 +-
 sieve/tests/allof.at                         |    2 +-
 sieve/tests/anyof.at                         |    2 +-
 sieve/tests/compile.at                       |    2 +-
 sieve/tests/envelope.at                      |    2 +-
 sieve/tests/exists.at                        |    2 +-
 sieve/tests/ext.at                           |    2 +-
 sieve/tests/false.at                         |    2 +-
 sieve/tests/header.at                        |    2 +-
 sieve/tests/i-casemap.at                     |    2 +-
 sieve/tests/i-numeric.at                     |    2 +-
 sieve/tests/i-octet.at                       |    2 +-
 sieve/tests/mul-addr.at                      |    2 +-
 sieve/tests/not.at                           |    2 +-
 sieve/tests/redirect.at                      |    2 +-
 sieve/tests/reject.at                        |    2 +-
 sieve/tests/relational.at                    |    2 +-
 sieve/tests/size.at                          |    2 +-
 sieve/tests/testsuite.at                     |    2 +-
 sieve/tests/true.at                          |    2 +-
 sieve/tests/version.at                       |    2 +-
 sql/Makefile.am                              |   25 +-
 sql/mysql.c                                  |    5 +-
 sql/odbc.c                                   |    5 +-
 sql/postgres.c                               |    5 +-
 sql/sql.c                                    |    5 +-
 testsuite/Makefile.am                        |    2 +-
 testsuite/lib/mailutils.exp                  |    8 +-
 testsuite/mbdel.at                           |    2 +-
 testsuite/mbdel.c                            |    2 +-
 testsuite/mime.at                            |    2 +-
 testsuite/mimetest.c                         |    4 +-
 testsuite/smtpsend.c                         |    2 +-
 testsuite/testsuite.at                       |    2 +-
 testsuite/testsuite.inc                      |    2 +-
 967 files changed, 36443 insertions(+), 37485 deletions(-)
 delete mode 100644 libmailutils/address.c
 create mode 100644 libmailutils/address/Makefile.am
 create mode 100644 libmailutils/address/address.c
 create mode 100644 libmailutils/address/parse822.c
 delete mode 100644 libmailutils/amd.c
 delete mode 100644 libmailutils/argcv.c
 delete mode 100644 libmailutils/asnprintf.c
 delete mode 100644 libmailutils/asprintf.c
 delete mode 100644 libmailutils/assoc.c
 delete mode 100644 libmailutils/attachment.c
 delete mode 100644 libmailutils/attribute.c
 delete mode 100644 libmailutils/auth.c
 create mode 100644 libmailutils/auth/Makefile.am
 create mode 100644 libmailutils/auth/auth.c
 create mode 100644 libmailutils/auth/mu_auth.c
 create mode 100644 libmailutils/auth/system.c
 create mode 100644 libmailutils/base/.gitignore
 create mode 100644 libmailutils/base/Makefile.am
 rename libmailutils/{ => base}/alloc.c (100%)
 create mode 100644 libmailutils/base/amd.c
 create mode 100644 libmailutils/base/argcv.c
 create mode 100644 libmailutils/base/assoc.c
 create mode 100644 libmailutils/base/daemon.c
 create mode 100644 libmailutils/base/date.c
 create mode 100644 libmailutils/base/fgetpwent.c
 create mode 100644 libmailutils/base/freeitem.c
 rename libmailutils/{ => base}/getpass.c (100%)
 create mode 100644 libmailutils/base/iterator.c
 rename libmailutils/{ => base}/kwd.c (100%)
 create mode 100644 libmailutils/base/list.c
 create mode 100644 libmailutils/base/listlist.c
 create mode 100644 libmailutils/base/locale.c
 create mode 100644 libmailutils/base/locker.c
 create mode 100644 libmailutils/base/mailcap.c
 create mode 100644 libmailutils/base/md5.c
 create mode 100644 libmailutils/base/monitor.c
 create mode 100644 libmailutils/base/munre.c
 create mode 100644 libmailutils/base/mutil.c
 create mode 100644 libmailutils/base/nls.c
 rename libmailutils/{ => base}/nullrec.c (100%)
 create mode 100644 libmailutils/base/observer.c
 create mode 100644 libmailutils/base/opool.c
 create mode 100644 libmailutils/base/parsedate.y
 create mode 100644 libmailutils/base/permstr.c
 create mode 100644 libmailutils/base/property.c
 create mode 100644 libmailutils/base/refcount.c
 create mode 100644 libmailutils/base/registrar.c
 create mode 100644 libmailutils/base/rfc2047.c
 create mode 100644 libmailutils/base/secret.c
 create mode 100644 libmailutils/base/sha1.c
 create mode 100644 libmailutils/base/ticket.c
 create mode 100644 libmailutils/base/url.c
 rename libmailutils/{ => base}/vartab.c (100%)
 create mode 100644 libmailutils/base/version.c
 create mode 100644 libmailutils/base/wicket.c
 delete mode 100644 libmailutils/body.c
 create mode 100644 libmailutils/cfg/.gitignore
 create mode 100644 libmailutils/cfg/Makefile.am
 create mode 100644 libmailutils/cfg/driver.c
 create mode 100644 libmailutils/cfg/format.c
 create mode 100644 libmailutils/cfg/gocs.c
 create mode 100644 libmailutils/cfg/lexer.l
 create mode 100644 libmailutils/cfg/parser.y
 delete mode 100644 libmailutils/cfg_driver.c
 delete mode 100644 libmailutils/cfg_format.c
 delete mode 100644 libmailutils/cfg_lexer.l
 delete mode 100644 libmailutils/cfg_parser.y
 delete mode 100644 libmailutils/crlfdot.c
 delete mode 100644 libmailutils/daemon.c
 delete mode 100644 libmailutils/date.c
 delete mode 100644 libmailutils/dbgstderr.c
 delete mode 100644 libmailutils/dbgsyslog.c
 delete mode 100644 libmailutils/debug.c
 delete mode 100644 libmailutils/diag.c
 create mode 100644 libmailutils/diag/.gitignore
 create mode 100644 libmailutils/diag/Makefile.am
 create mode 100644 libmailutils/diag/dbgstderr.c
 create mode 100644 libmailutils/diag/dbgsyslog.c
 create mode 100644 libmailutils/diag/debug.c
 create mode 100644 libmailutils/diag/diag.c
 create mode 100644 libmailutils/diag/errors
 create mode 100644 libmailutils/diag/gdebug.c
 create mode 100644 libmailutils/diag/muerrno.cin
 create mode 100644 libmailutils/diag/muerror.c
 create mode 100644 libmailutils/diag/syslog.c
 delete mode 100644 libmailutils/dot.c
 delete mode 100644 libmailutils/envelope.c
 delete mode 100644 libmailutils/errors
 delete mode 100644 libmailutils/fgetpwent.c
 delete mode 100644 libmailutils/filter.c
 create mode 100644 libmailutils/filter/Makefile.am
 rename libmailutils/{ => filter}/base64.c (100%)
 rename libmailutils/{ => filter}/binflt.c (100%)
 create mode 100644 libmailutils/filter/crlfdot.c
 rename libmailutils/{ => filter}/crlfflt.c (100%)
 create mode 100644 libmailutils/filter/dot.c
 create mode 100644 libmailutils/filter/filter.c
 create mode 100644 libmailutils/filter/filter_iconv.c
 create mode 100644 libmailutils/filter/fromflt.c
 rename libmailutils/{ => filter}/linelenflt.c (100%)
 rename libmailutils/{ => filter}/qpflt.c (100%)
 delete mode 100644 libmailutils/filter_iconv.c
 delete mode 100644 libmailutils/folder.c
 delete mode 100644 libmailutils/freeitem.c
 delete mode 100644 libmailutils/fromflt.c
 delete mode 100644 libmailutils/gdebug.c
 delete mode 100644 libmailutils/gocs.c
 delete mode 100644 libmailutils/hdritr.c
 delete mode 100644 libmailutils/header.c
 delete mode 100644 libmailutils/iterator.c
 delete mode 100644 libmailutils/list.c
 delete mode 100644 libmailutils/listlist.c
 delete mode 100644 libmailutils/locale.c
 delete mode 100644 libmailutils/locker.c
 delete mode 100644 libmailutils/mailbox.c
 create mode 100644 libmailutils/mailbox/Makefile.am
 create mode 100644 libmailutils/mailbox/attribute.c
 create mode 100644 libmailutils/mailbox/body.c
 create mode 100644 libmailutils/mailbox/envelope.c
 create mode 100644 libmailutils/mailbox/folder.c
 create mode 100644 libmailutils/mailbox/hdritr.c
 create mode 100644 libmailutils/mailbox/header.c
 create mode 100644 libmailutils/mailbox/mailbox.c
 create mode 100644 libmailutils/mailbox/mbx_default.c
 create mode 100644 libmailutils/mailbox/mbxitr.c
 create mode 100644 libmailutils/mailbox/message.c
 rename libmailutils/{ => mailbox}/msgscan.c (100%)
 delete mode 100644 libmailutils/mailcap.c
 delete mode 100644 libmailutils/mailer.c
 create mode 100644 libmailutils/mailer/Makefile.am
 create mode 100644 libmailutils/mailer/mailer.c
 create mode 100644 libmailutils/mailer/progmailer.c
 delete mode 100644 libmailutils/mbx_default.c
 delete mode 100644 libmailutils/mbxitr.c
 delete mode 100644 libmailutils/md5.c
 delete mode 100644 libmailutils/message.c
 delete mode 100644 libmailutils/message_stream.c
 delete mode 100644 libmailutils/mime.c
 create mode 100644 libmailutils/mime/Makefile.am
 create mode 100644 libmailutils/mime/attachment.c
 create mode 100644 libmailutils/mime/mime.c
 rename libmailutils/{ => mime}/mimehdr.c (100%)
 delete mode 100644 libmailutils/monitor.c
 delete mode 100644 libmailutils/mu_auth.c
 delete mode 100644 libmailutils/muerrno.cin
 delete mode 100644 libmailutils/muerror.c
 delete mode 100644 libmailutils/munre.c
 delete mode 100644 libmailutils/mutil.c
 delete mode 100644 libmailutils/nls.c
 delete mode 100644 libmailutils/observer.c
 delete mode 100644 libmailutils/opool.c
 delete mode 100644 libmailutils/parse822.c
 delete mode 100644 libmailutils/parsedate.y
 delete mode 100644 libmailutils/permstr.c
 delete mode 100644 libmailutils/progmailer.c
 delete mode 100644 libmailutils/property.c
 delete mode 100644 libmailutils/refcount.c
 delete mode 100644 libmailutils/registrar.c
 delete mode 100644 libmailutils/rfc2047.c
 delete mode 100644 libmailutils/secret.c
 create mode 100644 libmailutils/server/Makefile.am
 rename libmailutils/{ => server}/acl.c (100%)
 rename libmailutils/{ => server}/ipsrv.c (100%)
 rename libmailutils/{ => server}/msrv.c (100%)
 rename libmailutils/{ => server}/server.c (100%)
 delete mode 100644 libmailutils/sha1.c
 delete mode 100644 libmailutils/size_max.h
 create mode 100644 libmailutils/stream/Makefile.am
 rename libmailutils/{ => stream}/dbgstream.c (100%)
 rename libmailutils/{ => stream}/file_stream.c (100%)
 rename libmailutils/{ => stream}/fltstream.c (100%)
 rename libmailutils/{ => stream}/iostream.c (100%)
 rename libmailutils/{ => stream}/mapfile_stream.c (100%)
 rename libmailutils/{ => stream}/memory_stream.c (100%)
 create mode 100644 libmailutils/stream/message_stream.c
 rename libmailutils/{ => stream}/prog_stream.c (100%)
 rename libmailutils/{ => stream}/rdcache_stream.c (100%)
 rename libmailutils/{ => stream}/socket_stream.c (100%)
 rename libmailutils/{ => stream}/stdio_stream.c (100%)
 rename libmailutils/{ => stream}/stream.c (100%)
 rename libmailutils/{ => stream}/stream_printf.c (100%)
 rename libmailutils/{ => stream}/stream_vprintf.c (100%)
 rename libmailutils/{ => stream}/streamcpy.c (100%)
 rename libmailutils/{ => stream}/streamref.c (100%)
 create mode 100644 libmailutils/stream/tcp.c
 rename libmailutils/{ => stream}/temp_file_stream.c (100%)
 rename libmailutils/{ => stream}/xscript-stream.c (100%)
 create mode 100644 libmailutils/string/Makefile.am
 create mode 100644 libmailutils/string/asnprintf.c
 create mode 100644 libmailutils/string/asprintf.c
 rename libmailutils/{ => string}/cstrcasecmp.c (100%)
 rename libmailutils/{ => string}/cstrlower.c (100%)
 rename libmailutils/{ => string}/cstrupper.c (100%)
 rename libmailutils/{ => string}/mkfilename.c (100%)
 rename libmailutils/{ => string}/muctype.c (100%)
 rename libmailutils/{ => string}/stripws.c (100%)
 rename libmailutils/{ => string}/strltrim.c (100%)
 rename libmailutils/{ => string}/strrtrim.c (100%)
 rename libmailutils/{ => string}/strskip.c (100%)
 create mode 100644 libmailutils/string/vasnprintf.c
 delete mode 100644 libmailutils/syslog.c
 delete mode 100644 libmailutils/system.c
 delete mode 100644 libmailutils/tcp.c
 delete mode 100644 libmailutils/ticket.c
 delete mode 100644 libmailutils/url.c
 delete mode 100644 libmailutils/vasnprintf.c
 delete mode 100644 libmailutils/version.c
 delete mode 100644 libmailutils/wicket.c

diff --git a/Makefile.am b/Makefile.am
index 7c403d6..3558c92 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1,22 +1,19 @@
-## Process this file with GNU Automake to create Makefile.in
-
-##   Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
-##   2009, 2010 Free Software Foundation, Inc.
+## This file is part of GNU Mailutils
+## Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
+## 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.
+## 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.
+## GNU Mailutils 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
+## You should have received a copy of the GNU General Public License
+## along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
 ACLOCAL_AMFLAGS = -I m4 -I am -I gint
 
diff --git a/am/config_paths.m4 b/am/config_paths.m4
index 1d3ea9e..89e1f37 100644
--- a/am/config_paths.m4
+++ b/am/config_paths.m4
@@ -5,18 +5,18 @@ dnl
 dnl Written by Miles Bader <miles@gnu.ai.mit.edu> and
 dnl Sergey Poznyakoff <gray@gnu.org>
 dnl
-dnl This program is free software; you can redistribute it and/or modify
+dnl GNU Mailutils is free software; you can redistribute it and/or modify
 dnl it under the terms of the GNU General Public License as published by
 dnl the Free Software Foundation; either version 3, or (at your option)
 dnl any later version.
 dnl
-dnl This program is distributed in the hope that it will be useful,
+dnl GNU Mailutils is distributed in the hope that it will be useful,
 dnl but WITHOUT ANY WARRANTY; without even the implied warranty of
 dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 dnl GNU General Public License for more details.
 dnl
 dnl You should have received a copy of the GNU General Public License
-dnl along with this program.  If not, see <http://www.gnu.org/licenses/>.
+dnl along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 dnl
 
 dnl MU_CONFIG_PATHS -- Configure system paths for use by programs
diff --git a/am/db2.m4 b/am/db2.m4
index fa305ce..8c3ee8e 100644
--- a/am/db2.m4
+++ b/am/db2.m4
@@ -12,9 +12,7 @@ dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 dnl GNU General Public License for more details.
 dnl 
 dnl You should have received a copy of the GNU General Public License along
-dnl with GNU Mailutils; if not, write to the Free Software Foundation,
-dnl Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
-dnl
+dnl with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
 AH_TEMPLATE(BDB2_CURSOR_LASTARG,
             [Last argument to the cursor member of Berkeley 2 DB structure])
diff --git a/am/debug.m4 b/am/debug.m4
index ff64ba8..9b84bba 100644
--- a/am/debug.m4
+++ b/am/debug.m4
@@ -5,7 +5,7 @@ dnl This file is free software; as a special exception the author gives
 dnl unlimited permission to copy and/or distribute it, with or without
 dnl modifications, as long as this notice is preserved.
 dnl
-dnl This program is distributed in the hope that it will be useful, but
+dnl GNU Mailutils is distributed in the hope that it will be useful, but
 dnl WITHOUT ANY WARRANTY, to the extent permitted by law; without even the
 dnl implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 dnl
diff --git a/am/enable.m4 b/am/enable.m4
index e531c98..0596812 100644
--- a/am/enable.m4
+++ b/am/enable.m4
@@ -2,20 +2,18 @@ dnl This file is part of GNU mailutils.
 dnl Copyright (C) 2002, 2005, 2007, 2009, 2010 Free Software Foundation,
 dnl Inc.
 dnl
-dnl This program is free software; you can redistribute it and/or modify
+dnl GNU Mailutils is free software; you can redistribute it and/or modify
 dnl it under the terms of the GNU General Public License as published by
 dnl the Free Software Foundation; either version 3 of the License, or
 dnl (at your option) any later version.
 dnl
-dnl This program is distributed in the hope that it will be useful,
+dnl GNU Mailutils is distributed in the hope that it will be useful,
 dnl but WITHOUT ANY WARRANTY; without even the implied warranty of
 dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 dnl GNU General Public License for more details.
 dnl
 dnl You should have received a copy of the GNU General Public License
-dnl along with this program; if not, write to the Free Software Foundation,
-dnl Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
-dnl
+dnl along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
 dnl MU_ENABLE_SUPPORT(feature, [action-if-true], [action-if-false],
 dnl                   [default-value])
diff --git a/am/gsasl.m4 b/am/gsasl.m4
index 3ce16f0..88e0b28 100644
--- a/am/gsasl.m4
+++ b/am/gsasl.m4
@@ -12,8 +12,7 @@ dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 dnl GNU General Public License for more details.
 dnl 
 dnl You should have received a copy of the GNU General Public License along
-dnl with GNU Mailutils; if not, write to the Free Software Foundation,
-dnl Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+dnl with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 dnl
 AC_DEFUN([MU_CHECK_GSASL],
 [AC_CACHE_CHECK([whether to use GNU SASL library],
diff --git a/am/tls.m4 b/am/tls.m4
index 8bcf191..23ed7ea 100644
--- a/am/tls.m4
+++ b/am/tls.m4
@@ -12,8 +12,7 @@ dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 dnl GNU General Public License for more details.
 dnl 
 dnl You should have received a copy of the GNU General Public License along
-dnl with GNU Mailutils; if not, write to the Free Software Foundation,
-dnl Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+dnl with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 dnl
 AC_DEFUN([MU_CHECK_TLS],
 [
diff --git a/bootstrap.conf b/bootstrap.conf
index df7f359..52a1552 100644
--- a/bootstrap.conf
+++ b/bootstrap.conf
@@ -1,22 +1,20 @@
-# Bootstrap configuration.
-
+# Bootstrap configuration for GNU Mailutils.
+#
 # Copyright (C) 2006, 2007, 2008, 2009, 2010 Free Software Foundation,
 # Inc.
-
-# This program is free software; you can redistribute it and/or modify
+#
+# 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,
+#
+# GNU Mailutils 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.
+# along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
 gnulib_name=libmuaux
 
diff --git a/cmc/cmc_act_on.c b/cmc/cmc_act_on.c
index 3abd286..1f5f208 100644
--- a/cmc/cmc_act_on.c
+++ b/cmc/cmc_act_on.c
@@ -13,8 +13,8 @@
    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., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA  */
+   License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #include <mailutils/xcmc.h>
 
diff --git a/cmc/cmc_free.c b/cmc/cmc_free.c
index 31a7bdb..b3731d3 100644
--- a/cmc/cmc_free.c
+++ b/cmc/cmc_free.c
@@ -13,8 +13,8 @@
    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., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA  */
+   License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #include <mailutils/xcmc.h>
 
diff --git a/cmc/cmc_list.c b/cmc/cmc_list.c
index f831e7d..1b7478e 100644
--- a/cmc/cmc_list.c
+++ b/cmc/cmc_list.c
@@ -13,8 +13,8 @@
    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., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA  */
+   License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #include <mailutils/xcmc.h>
 
diff --git a/cmc/cmc_logoff.c b/cmc/cmc_logoff.c
index 4b56f1b..847e2f4 100644
--- a/cmc/cmc_logoff.c
+++ b/cmc/cmc_logoff.c
@@ -13,8 +13,8 @@
    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., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA  */
+   License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #include <mailutils/xcmc.h>
 
diff --git a/cmc/cmc_logon.c b/cmc/cmc_logon.c
index 9dc33d7..414b528 100644
--- a/cmc/cmc_logon.c
+++ b/cmc/cmc_logon.c
@@ -13,8 +13,8 @@
    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., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA  */
+   License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #include <mailutils/xcmc.h>
 
diff --git a/cmc/cmc_look_up.c b/cmc/cmc_look_up.c
index 2aecbd4..3f841cf 100644
--- a/cmc/cmc_look_up.c
+++ b/cmc/cmc_look_up.c
@@ -13,8 +13,8 @@
    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., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA  */
+   License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #include <mailutils/xcmc.h>
 
diff --git a/cmc/cmc_query_config.c b/cmc/cmc_query_config.c
index 5d10ae3..491850f 100644
--- a/cmc/cmc_query_config.c
+++ b/cmc/cmc_query_config.c
@@ -13,8 +13,8 @@
    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., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA  */
+   License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #include <mailutils/xcmc.h>
 
diff --git a/cmc/cmc_read.c b/cmc/cmc_read.c
index 7add8ea..03cb98f 100644
--- a/cmc/cmc_read.c
+++ b/cmc/cmc_read.c
@@ -13,8 +13,8 @@
    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., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA  */
+   License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #include <mailutils/xcmc.h>
 
diff --git a/cmc/cmc_send.c b/cmc/cmc_send.c
index 80ced54..9d1513e 100644
--- a/cmc/cmc_send.c
+++ b/cmc/cmc_send.c
@@ -13,8 +13,8 @@
    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., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA  */
+   License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #include <mailutils/xcmc.h>
 
diff --git a/cmc/cmc_send_documents.c b/cmc/cmc_send_documents.c
index c4da56b..5217c04 100644
--- a/cmc/cmc_send_documents.c
+++ b/cmc/cmc_send_documents.c
@@ -13,8 +13,8 @@
    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., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA  */
+   License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #include <mailutils/xcmc.h>
 
diff --git a/cmc/xcmc.h b/cmc/xcmc.h
index c960873..c0fceb7 100644
--- a/cmc/xcmc.h
+++ b/cmc/xcmc.h
@@ -13,8 +13,8 @@
    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., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA  */
+   License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _XCMC_H
 #define _XCMC_H
diff --git a/comsat/Makefile.am b/comsat/Makefile.am
index e218e65..752aed3 100644
--- a/comsat/Makefile.am
+++ b/comsat/Makefile.am
@@ -1,22 +1,19 @@
-## Process this file with GNU Automake to create Makefile.in
-
-##   Copyright (C) 2001, 2002, 2007, 2009, 2010 Free Software
-##   Foundation, Inc.
+## This file is part of GNU Mailutils.
+## Copyright (C) 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.
+## 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.
 ##
-##   GNU Mailutils 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.
+## GNU Mailutils 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 GNU Mailutils; if not, write to the Free Software
-##   Foundation, Inc.  51 Franklin Street, Fifth Floor, Boston, MA
-##   02110-1301 USA
+## You should have received a copy of the GNU General Public License
+## along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
 INCLUDES = @MU_APP_COMMON_INCLUDES@ 
 
diff --git a/comsat/action.c b/comsat/action.c
index 738521c..afba30f 100644
--- a/comsat/action.c
+++ b/comsat/action.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "comsat.h"
 #include <mailutils/io.h>
diff --git a/comsat/comsat.c b/comsat/comsat.c
index 0b511fa..373998f 100644
--- a/comsat/comsat.c
+++ b/comsat/comsat.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "comsat.h"
 #define MU_CFG_COMPATIBILITY /* This source uses deprecated cfg interfaces */
diff --git a/comsat/comsat.h b/comsat/comsat.h
index de4abb9..df4a19c 100644
--- a/comsat/comsat.h
+++ b/comsat/comsat.h
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/config/Makefile.am b/config/Makefile.am
index 986a234..b850f7f 100644
--- a/config/Makefile.am
+++ b/config/Makefile.am
@@ -1,22 +1,19 @@
-## Process this file with GNU Automake to create Makefile.in
-
-##   Copyright (C) 2005, 2006, 2007, 2009, 2010 Free Software
-##   Foundation, Inc.
+## This file is part of GNU Mailutils.
+## Copyright (C) 2005, 2006, 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.
+## 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.
+## GNU Mailutils 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
+## You should have received a copy of the GNU General Public License
+## along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
 INCLUDES = @MU_APP_COMMON_INCLUDES@ 
 
diff --git a/config/mailutils-config.c b/config/mailutils-config.c
index ccf55ab..6a34497 100644
--- a/config/mailutils-config.c
+++ b/config/mailutils-config.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/config/mailutils.m4 b/config/mailutils.m4
index 055d56c..4e8bb47 100644
--- a/config/mailutils.m4
+++ b/config/mailutils.m4
@@ -5,13 +5,13 @@ dnl modify it under the terms of the GNU General Public License as
 dnl published by the Free Software Foundation; either version 3, or (at
 dnl your option) any later version.
 dnl
-dnl This program is distributed in the hope that it will be useful, but
+dnl GNU Mailutils is distributed in the hope that it will be useful, but
 dnl WITHOUT ANY WARRANTY; without even the implied warranty of
 dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 dnl General Public License for more details.
 dnl
 dnl You should have received a copy of the GNU General Public License
-dnl along with this program.  If not, see <http://www.gnu.org/licenses/>.
+dnl along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
 dnl AM_GNU_MAILUTILS(minversion, link-req, [act-if-found], [ac-if-not-found])
 dnl                      $1         $2           $3              $4
diff --git a/configure.ac b/configure.ac
index 2068a9a..c7a627d 100644
--- a/configure.ac
+++ b/configure.ac
@@ -14,12 +14,11 @@ dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 dnl GNU General Public License for more details.
 dnl
 dnl You should have received a copy of the GNU General Public License along
-dnl with GNU Mailutils; if not, write to the Free Software Foundation,
-dnl Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+dnl with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
 AC_PREREQ(2.63)
 AC_INIT([GNU Mailutils], [2.2.90], [bug-mailutils@gnu.org], [mailutils])
-AC_CONFIG_SRCDIR([libmailutils/mailbox.c])
+AC_CONFIG_SRCDIR([libmailutils/mailbox/mailbox.c])
 AC_CONFIG_AUX_DIR([build-aux])
 AM_INIT_AUTOMAKE([gnits 1.11 dist-bzip2 dist-xz std-options silent-rules])
 AC_CONFIG_HEADERS([config.h])
@@ -1377,6 +1376,18 @@ AC_CONFIG_FILES([
  maidag/Makefile
  mail/Makefile
  mail/testsuite/Makefile
+ libmailutils/auth/Makefile
+ libmailutils/base/Makefile
+ libmailutils/address/Makefile
+ libmailutils/cfg/Makefile
+ libmailutils/diag/Makefile
+ libmailutils/filter/Makefile
+ libmailutils/mailbox/Makefile
+ libmailutils/mailer/Makefile
+ libmailutils/mime/Makefile
+ libmailutils/server/Makefile
+ libmailutils/string/Makefile
+ libmailutils/stream/Makefile
  libmailutils/Makefile
  messages/Makefile
  mh/Makefile
diff --git a/doc/Makefile.am b/doc/Makefile.am
index 878bb83..4fa4fea 100644
--- a/doc/Makefile.am
+++ b/doc/Makefile.am
@@ -1,22 +1,19 @@
-## Process this file with GNU Automake to create Makefile.in
-
-##   Copyright (C) 2000, 2001, 2002, 2007, 2009, 2010 Free Software
-##   Foundation, Inc.
+## This file is part of GNU Mailutils.
+## 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.
+## 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.
+## GNU Mailutils 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
+## You should have received a copy of the GNU General Public License
+## along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
 SUBDIRS = texinfo man
 EXTRA_DIST = ChangeLog.CVS rfc/README
diff --git a/doc/man/Makefile.am b/doc/man/Makefile.am
index e2689a2..acd4cf5 100644
--- a/doc/man/Makefile.am
+++ b/doc/man/Makefile.am
@@ -1,22 +1,19 @@
-## Process this file with GNU Automake to create Makefile.in
-
-##   Copyright (C) 2001, 2002, 2003, 2004, 2007, 2010 Free Software
-##   Foundation, Inc.
+## This file is part of GNU Mailutils.
+## Copyright (C) 2001, 2002, 2003, 2004, 2007, 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.
+## 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.
+## GNU Mailutils 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
+## You should have received a copy of the GNU General Public License
+## along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
 man_MANS = pop3d.1 imap4d.1 popauth.1 mail.1
 EXTRA_DIST = $(man_MANS)
diff --git a/doc/texinfo/Makefile.am b/doc/texinfo/Makefile.am
index 2142053..1bbe196 100644
--- a/doc/texinfo/Makefile.am
+++ b/doc/texinfo/Makefile.am
@@ -1,22 +1,19 @@
-## Process this file with GNU Automake to create Makefile.in
-
-##   Copyright (C) 2001, 2002, 2003, 2004, 2006, 2007, 2009, 2010 Free
-##   Software Foundation, Inc.
+## This file is part of GNU Mailutils.
+## Copyright (C) 2001, 2002, 2003, 2004, 2006, 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.
+## 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.
+## GNU Mailutils 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
+## You should have received a copy of the GNU General Public License
+## along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
 info_TEXINFOS = mailutils.texi 
 
diff --git a/dotlock/Makefile.am b/dotlock/Makefile.am
index 820066a..f9aeb48 100644
--- a/dotlock/Makefile.am
+++ b/dotlock/Makefile.am
@@ -1,21 +1,18 @@
-## Process this file with GNU Automake to create Makefile.in
-
-##   Copyright (C) 2002, 2007, 2009, 2010 Free Software Foundation, Inc.
+## This file is part of GNU Mailutils.
+## Copyright (C) 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.
+## 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.
+## GNU Mailutils 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
+## You should have received a copy of the GNU General Public License
+## along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
 INCLUDES = @MU_APP_COMMON_INCLUDES@ 
 
diff --git a/dotlock/dotlock.c b/dotlock/dotlock.c
index d561d18..84d322e 100644
--- a/dotlock/dotlock.c
+++ b/dotlock/dotlock.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/examples/Makefile.am b/examples/Makefile.am
index e4c5eb9..1addf44 100644
--- a/examples/Makefile.am
+++ b/examples/Makefile.am
@@ -1,22 +1,19 @@
-## Process this file with GNU Automake to create Makefile.in
-
-##   Copyright (C) 2002, 2003, 2004, 2007, 2008, 2009, 2010 Free
-##   Software Foundation, Inc.
+## This file is part of GNU Mailutils.
+## Copyright (C) 2002, 2003, 2004, 2007, 2008, 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.
+## 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.
+## GNU Mailutils 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
+## You should have received a copy of the GNU General Public License
+## along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
 if MU_COND_SUPPORT_POP
   POP3CLIENT = pop3client
diff --git a/examples/aclck.c b/examples/aclck.c
index 713a955..d4d9b95 100644
--- a/examples/aclck.c
+++ b/examples/aclck.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/examples/addr.c b/examples/addr.c
index aae456a..009e13e 100644
--- a/examples/addr.c
+++ b/examples/addr.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/examples/base64.c b/examples/base64.c
index 1f54ea7..deadfd2 100644
--- a/examples/base64.c
+++ b/examples/base64.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/examples/config/Makefile.am b/examples/config/Makefile.am
index 83983a8..5d51206 100644
--- a/examples/config/Makefile.am
+++ b/examples/config/Makefile.am
@@ -1,22 +1,19 @@
-## Process this file with GNU Automake to create Makefile.in
-
-##   Copyright (C) 2004, 2005, 2007, 2008, 2010 Free Software
-##   Foundation, Inc.
+## This file is part of GNU Mailutils.
+## Copyright (C) 2004, 2005, 2007, 2008, 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.
+## 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.
+## GNU Mailutils 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
+## You should have received a copy of the GNU General Public License
+## along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
 EXTRA_DIST = \
  dot.biffrc\
diff --git a/examples/config/mailutils.schema b/examples/config/mailutils.schema
index d0f2b9c..f61bd36 100644
--- a/examples/config/mailutils.schema
+++ b/examples/config/mailutils.schema
@@ -7,7 +7,7 @@
 # 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,
+# GNU Mailutils 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.
diff --git a/examples/cpp/Makefile.am b/examples/cpp/Makefile.am
index 8176c6c..d06b0fb 100644
--- a/examples/cpp/Makefile.am
+++ b/examples/cpp/Makefile.am
@@ -1,22 +1,19 @@
-##   Process this file with GNU Automake to create Makefile.in
+## This file is part of GNU Mailutils.
+## Copyright (C) 2004, 2006, 2007, 2009, 2010 Free Software
+## Foundation, Inc.
 ##
-##   Copyright (C) 2004, 2006, 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.
 ##
-##   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.
+## GNU Mailutils 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.
 ##
-##   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
+## You should have received a copy of the GNU General Public License
+## along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
 MU_CXX_EXAMPLES_LIST = \
  addr\
diff --git a/examples/cpp/addr.cc b/examples/cpp/addr.cc
index c861c0c..6fde214 100644
--- a/examples/cpp/addr.cc
+++ b/examples/cpp/addr.cc
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2004, 2006, 2007, 2009, 2010 Free Software Foundation,
    Inc.
 
@@ -14,10 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA
-*/
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include <iostream>
 #include <cstring>
diff --git a/examples/cpp/http.cc b/examples/cpp/http.cc
index f523951..7e0d2e8 100644
--- a/examples/cpp/http.cc
+++ b/examples/cpp/http.cc
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2004, 2006, 2007, 2009, 2010 Free Software Foundation,
    Inc.
 
@@ -14,10 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA
-*/
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.  */
 
 /* This is an example program to illustrate the use of stream functions.
    It connects to a remote HTTP server and prints the contents of its
diff --git a/examples/cpp/iconv.cc b/examples/cpp/iconv.cc
index e94c47b..9889b3c 100644
--- a/examples/cpp/iconv.cc
+++ b/examples/cpp/iconv.cc
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2004, 2006, 2007, 2009, 2010 Free Software Foundation,
    Inc.
 
@@ -14,10 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA
-*/
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include <iostream>
 #include <cstdlib>
diff --git a/examples/cpp/listop.cc b/examples/cpp/listop.cc
index 994b79c..01bab7b 100644
--- a/examples/cpp/listop.cc
+++ b/examples/cpp/listop.cc
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2004, 2006, 2007, 2009, 2010 Free Software Foundation,
    Inc.
 
@@ -14,10 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA
-*/
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include <iostream>
 #include <cstdlib>
diff --git a/examples/cpp/lsf.cc b/examples/cpp/lsf.cc
index 306ee97..68a9a49 100644
--- a/examples/cpp/lsf.cc
+++ b/examples/cpp/lsf.cc
@@ -12,9 +12,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include <iostream>
 #include <cstdlib>
diff --git a/examples/cpp/mailcap.cc b/examples/cpp/mailcap.cc
index d1a0f45..9dcf90d 100644
--- a/examples/cpp/mailcap.cc
+++ b/examples/cpp/mailcap.cc
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2004, 2006, 2007, 2009, 2010 Free Software Foundation,
    Inc.
 
@@ -14,10 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA
-*/
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include <iostream>
 #include <cstdlib>
diff --git a/examples/cpp/mimetest.cc b/examples/cpp/mimetest.cc
index 2215b94..4eae88b 100644
--- a/examples/cpp/mimetest.cc
+++ b/examples/cpp/mimetest.cc
@@ -12,9 +12,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include <iostream>
 #include <cstdlib>
diff --git a/examples/cpp/msg-send.cc b/examples/cpp/msg-send.cc
index 79b300d..76bac8c 100644
--- a/examples/cpp/msg-send.cc
+++ b/examples/cpp/msg-send.cc
@@ -12,9 +12,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include <iostream>
 #include <cstdlib>
diff --git a/examples/cpp/murun.cc b/examples/cpp/murun.cc
index 6dc1e1a..b9133b1 100644
--- a/examples/cpp/murun.cc
+++ b/examples/cpp/murun.cc
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2004, 2006, 2007, 2009, 2010 Free Software Foundation,
    Inc.
 
@@ -14,10 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA
-*/
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include <iostream>
 #include <mailutils/cpp/mailutils.h>
diff --git a/examples/cpp/sfrom.cc b/examples/cpp/sfrom.cc
index 590b07e..779f42c 100644
--- a/examples/cpp/sfrom.cc
+++ b/examples/cpp/sfrom.cc
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2004, 2006, 2007, 2009, 2010 Free Software Foundation,
    Inc.
 
@@ -14,10 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA
-*/
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include <iostream>
 #include <cstdlib>
diff --git a/examples/cpp/url-parse.cc b/examples/cpp/url-parse.cc
index 3c01181..b0dd0af 100644
--- a/examples/cpp/url-parse.cc
+++ b/examples/cpp/url-parse.cc
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2004, 2006, 2007, 2008, 2009, 2010 Free Software
    Foundation, Inc.
 
@@ -14,10 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA
-*/
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include <iostream>
 #include <vector>
diff --git a/examples/header.c b/examples/header.c
index 63733f8..1d6c790 100644
--- a/examples/header.c
+++ b/examples/header.c
@@ -12,9 +12,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/examples/http.c b/examples/http.c
index 44666e5..f44339a 100644
--- a/examples/http.c
+++ b/examples/http.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 /* This is an example program to illustrate the use of stream functions.
    It connects to a remote HTTP server and prints the contents of its
diff --git a/examples/iconv.c b/examples/iconv.c
index 7876dc6..6a03349 100644
--- a/examples/iconv.c
+++ b/examples/iconv.c
@@ -12,9 +12,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/examples/lsf.c b/examples/lsf.c
index 3998c58..936c572 100644
--- a/examples/lsf.c
+++ b/examples/lsf.c
@@ -12,9 +12,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/examples/msg-send.c b/examples/msg-send.c
index 46dd82a..321f5f5 100644
--- a/examples/msg-send.c
+++ b/examples/msg-send.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/examples/mta.c b/examples/mta.c
index d53e97f..705a2b0 100644
--- a/examples/mta.c
+++ b/examples/mta.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 /* This is a "fake" mta designed for testing purposes. It imitates
    sendmail sending and daemon modes. It does not actually send anything,
diff --git a/examples/muauth.c b/examples/muauth.c
index c771ebf..a80cda8 100644
--- a/examples/muauth.c
+++ b/examples/muauth.c
@@ -12,9 +12,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/examples/mucat.c b/examples/mucat.c
index 92f55eb..094e454 100644
--- a/examples/mucat.c
+++ b/examples/mucat.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/examples/muemail.c b/examples/muemail.c
index 6bceb25..b2635cb 100644
--- a/examples/muemail.c
+++ b/examples/muemail.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/examples/murun.c b/examples/murun.c
index c4f822f..71e2a10 100644
--- a/examples/murun.c
+++ b/examples/murun.c
@@ -12,9 +12,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/examples/musocio.c b/examples/musocio.c
index 31ca7d3..ca49501 100644
--- a/examples/musocio.c
+++ b/examples/musocio.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/examples/nntpclient.c b/examples/nntpclient.c
index 4c7722f..19fec3d 100644
--- a/examples/nntpclient.c
+++ b/examples/nntpclient.c
@@ -16,9 +16,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>  
diff --git a/examples/numaddr.c b/examples/numaddr.c
index 9a5bea0..8e0345d 100644
--- a/examples/numaddr.c
+++ b/examples/numaddr.c
@@ -13,9 +13,8 @@
    GNU Lesser General Public License for more details.
 
    You should have received a copy of the GNU Lesser General Public
-   License along with GNU Mailutils; if not, write to the Free
-   Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
-   Boston, MA 02110-1301 USA */
+   License along with GNU Mailutils.  If not, see
+   <http://www.gnu.org/licenses/>. */; 
 
 /*  This is an example on how to write extension tests for GNU sieve.
     It provides test "numaddr".
diff --git a/examples/pop3client.c b/examples/pop3client.c
index 87f0c44..f2d81a3 100644
--- a/examples/pop3client.c
+++ b/examples/pop3client.c
@@ -16,9 +16,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>  
diff --git a/examples/python/Makefile.am b/examples/python/Makefile.am
index 4f816ee..d87bf90 100644
--- a/examples/python/Makefile.am
+++ b/examples/python/Makefile.am
@@ -1,21 +1,18 @@
-## Process this file with GNU Automake to create Makefile.in
-
-##   Copyright (C) 2009, 2010 Free Software Foundation, Inc.
+## This file is part of GNU Mailutils.
+## Copyright (C) 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.
+## 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.
+## GNU Mailutils 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
+## You should have received a copy of the GNU General Public License
+## along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. 
 
 EXTRA_DIST = \
  addr.py\
diff --git a/examples/python/addr.py b/examples/python/addr.py
index 2b844ed..99399da 100644
--- a/examples/python/addr.py
+++ b/examples/python/addr.py
@@ -12,9 +12,7 @@
 #  GNU General Public License for more details.
 #
 #  You should have received a copy of the GNU General Public License
-#  along with GNU Mailutils; if not, write to the Free Software
-#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-#  MA 02110-1301 USA
+#  along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. 
 
 import sys
 from mailutils import address
diff --git a/examples/python/auth.py b/examples/python/auth.py
index e1e1c07..1312e7b 100644
--- a/examples/python/auth.py
+++ b/examples/python/auth.py
@@ -12,9 +12,7 @@
 #  GNU General Public License for more details.
 #
 #  You should have received a copy of the GNU General Public License
-#  along with GNU Mailutils; if not, write to the Free Software
-#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-#  MA 02110-1301 USA
+#  along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. 
 
 import sys
 import getopt
diff --git a/examples/python/iconv.py b/examples/python/iconv.py
index 3e36381..d1d77c9 100644
--- a/examples/python/iconv.py
+++ b/examples/python/iconv.py
@@ -12,9 +12,7 @@
 #  GNU General Public License for more details.
 #
 #  You should have received a copy of the GNU General Public License
-#  along with GNU Mailutils; if not, write to the Free Software
-#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-#  MA 02110-1301 USA
+#  along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. 
 
 import sys
 from mailutils import stream
diff --git a/examples/python/lsf.py b/examples/python/lsf.py
index 6bd9b03..56d4fc3 100644
--- a/examples/python/lsf.py
+++ b/examples/python/lsf.py
@@ -12,9 +12,7 @@
 #  GNU General Public License for more details.
 #
 #  You should have received a copy of the GNU General Public License
-#  along with GNU Mailutils; if not, write to the Free Software
-#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-#  MA 02110-1301 USA
+#  along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
 import sys
 from mailutils import folder
diff --git a/examples/python/mailcap.py b/examples/python/mailcap.py
index ca70dce..956ffc9 100644
--- a/examples/python/mailcap.py
+++ b/examples/python/mailcap.py
@@ -12,9 +12,7 @@
 #  GNU General Public License for more details.
 #
 #  You should have received a copy of the GNU General Public License
-#  along with GNU Mailutils; if not, write to the Free Software
-#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-#  MA 02110-1301 USA
+#  along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. 
 
 from mailutils import stream, mailcap
 
diff --git a/examples/python/mimetest.py b/examples/python/mimetest.py
index 471a052..7ec6ee2 100644
--- a/examples/python/mimetest.py
+++ b/examples/python/mimetest.py
@@ -12,9 +12,7 @@
 #  GNU General Public License for more details.
 #
 #  You should have received a copy of the GNU General Public License
-#  along with GNU Mailutils; if not, write to the Free Software
-#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-#  MA 02110-1301 USA
+#  along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. 
 
 import sys
 import getopt
diff --git a/examples/python/msg-send.py b/examples/python/msg-send.py
index ce339b0..cbca586 100644
--- a/examples/python/msg-send.py
+++ b/examples/python/msg-send.py
@@ -12,9 +12,7 @@
 #  GNU General Public License for more details.
 #
 #  You should have received a copy of the GNU General Public License
-#  along with GNU Mailutils; if not, write to the Free Software
-#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-#  MA 02110-1301 USA
+#  along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
 import sys
 import getopt
diff --git a/examples/python/sfrom.py b/examples/python/sfrom.py
index d942870..3c461a7 100644
--- a/examples/python/sfrom.py
+++ b/examples/python/sfrom.py
@@ -12,9 +12,7 @@
 #  GNU General Public License for more details.
 #
 #  You should have received a copy of the GNU General Public License
-#  along with GNU Mailutils; if not, write to the Free Software
-#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-#  MA 02110-1301 USA
+#  along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
 import sys
 from mailutils import *
diff --git a/examples/python/url-parse.py b/examples/python/url-parse.py
index d331f0e..0ea8a12 100644
--- a/examples/python/url-parse.py
+++ b/examples/python/url-parse.py
@@ -12,9 +12,7 @@
 #  GNU General Public License for more details.
 #
 #  You should have received a copy of the GNU General Public License
-#  along with GNU Mailutils; if not, write to the Free Software
-#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-#  MA 02110-1301 USA
+#  along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
 import sys
 from mailutils import url
diff --git a/examples/scheme/Makefile.am b/examples/scheme/Makefile.am
index 5e4baeb..121f595 100644
--- a/examples/scheme/Makefile.am
+++ b/examples/scheme/Makefile.am
@@ -1,21 +1,18 @@
-## Process this file with GNU Automake to create Makefile.in
-
-##   Copyright (C) 2004, 2007, 2010 Free Software Foundation, Inc.
+## This file is part of GNU Mailutils.
+## Copyright (C) 2004, 2007, 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.
+## 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.
+## GNU Mailutils 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
+## You should have received a copy of the GNU General Public License
+## along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
 EXTRA_DIST = reply.scm
 
diff --git a/examples/scheme/reply.scm b/examples/scheme/reply.scm
index d246249..68d1d79 100644
--- a/examples/scheme/reply.scm
+++ b/examples/scheme/reply.scm
@@ -13,8 +13,7 @@
 ;;;; GNU General Public License for more details.
 ;;;; 
 ;;;; You should have received a copy of the GNU General Public License along
-;;;; with GNU Mailutils; if not, write to the Free Software Foundation,
-;;;; Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+;;;; with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
 ;;; This is a simple Guile program that generates automatic reply to 
 ;;; incoming mail messages.
diff --git a/examples/sfrom.c b/examples/sfrom.c
index 8e6384c..930a8ac 100644
--- a/examples/sfrom.c
+++ b/examples/sfrom.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/frm/Makefile.am b/frm/Makefile.am
index fdb1bfb..03d7703 100644
--- a/frm/Makefile.am
+++ b/frm/Makefile.am
@@ -1,22 +1,19 @@
-## Process this file with GNU Automake to create Makefile.in
-
-##   Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007, 2010 Free
-##   Software Foundation, Inc.
+## This file is part of GNU Mailutils.
+## Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007, 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.
+## 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.
+## GNU Mailutils 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
+## You should have received a copy of the GNU General Public License
+## along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
 INCLUDES = @MU_APP_COMMON_INCLUDES@ 
 
diff --git a/frm/common.c b/frm/common.c
index 901c774..a604a2d 100644
--- a/frm/common.c
+++ b/frm/common.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include <frm.h>
 
diff --git a/frm/frm.c b/frm/frm.c
index f978560..fdd43c1 100644
--- a/frm/frm.c
+++ b/frm/frm.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include <frm.h>
 
diff --git a/frm/frm.h b/frm/frm.h
index 38ce39e..24d59ef 100644
--- a/frm/frm.h
+++ b/frm/frm.h
@@ -12,9 +12,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/frm/from.c b/frm/from.c
index 6b9fdf8..d86cfea 100644
--- a/frm/from.c
+++ b/frm/from.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include <frm.h>
 
diff --git a/frm/tests/Makefile.am b/frm/tests/Makefile.am
index b548c51..d587c2f 100644
--- a/frm/tests/Makefile.am
+++ b/frm/tests/Makefile.am
@@ -6,7 +6,7 @@
 # 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
+# GNU Mailutils 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.
diff --git a/frm/tests/testsuite.at b/frm/tests/testsuite.at
index 1a3e6f0..b2d8558 100644
--- a/frm/tests/testsuite.at
+++ b/frm/tests/testsuite.at
@@ -6,7 +6,7 @@
 # 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
+# GNU Mailutils 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.
diff --git a/guimb/Makefile.am b/guimb/Makefile.am
index 273371f..b288042 100644
--- a/guimb/Makefile.am
+++ b/guimb/Makefile.am
@@ -1,22 +1,19 @@
-## Process this file with GNU Automake to create Makefile.in
-
-##   Copyright (C) 2001, 2002, 2007, 2009, 2010 Free Software
-##   Foundation, Inc.
+## This file is part of GNU Mailutils.
+## Copyright (C) 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.
+## 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.
+## GNU Mailutils 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
+## You should have received a copy of the GNU General Public License
+## along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
 INCLUDES = @MU_APP_COMMON_INCLUDES@ @GUILE_INCLUDES@ 
 
diff --git a/guimb/collect.c b/guimb/collect.c
index 532fc47..342e593 100644
--- a/guimb/collect.c
+++ b/guimb/collect.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "guimb.h"
 
diff --git a/guimb/guimb.h b/guimb/guimb.h
index 1cae077..d1fc759 100644
--- a/guimb/guimb.h
+++ b/guimb/guimb.h
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #if defined(HAVE_CONFIG_H)
 # include <config.h>
diff --git a/guimb/main.c b/guimb/main.c
index f08d450..4a3f39e 100644
--- a/guimb/main.c
+++ b/guimb/main.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "guimb.h"
 #include "mailutils/libargp.h"
diff --git a/guimb/scm/Makefile.am b/guimb/scm/Makefile.am
index 6c0c708..2d70b08 100644
--- a/guimb/scm/Makefile.am
+++ b/guimb/scm/Makefile.am
@@ -1,22 +1,19 @@
-## Process this file with GNU Automake to create Makefile.in
-
-##   Copyright (C) 2001, 2002, 2006, 2007, 2009, 2010 Free Software
-##   Foundation, Inc.
+## This file is part of GNU Mailutils.
+## Copyright (C) 2001, 2002, 2006, 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.
+## 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.
+## GNU Mailutils 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
+## You should have received a copy of the GNU General Public License
+## along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. 
 
 MU_GUILE_SIEVE_SCRIPTS_X=sieve.scm
 bin_SCRIPTS = sieve.scm
diff --git a/guimb/scm/mimeheader.scm b/guimb/scm/mimeheader.scm
index a354694..9543e22 100644
--- a/guimb/scm/mimeheader.scm
+++ b/guimb/scm/mimeheader.scm
@@ -13,8 +13,7 @@
 ;;;; GNU General Public License for more details.
 ;;;; 
 ;;;; You should have received a copy of the GNU General Public License along
-;;;; with GNU Mailutils; if not, write to the Free Software Foundation,
-;;;; Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+;;;; with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
 ;;;; This module provides GNU extension test "mimeheader".
 
diff --git a/guimb/scm/numaddr.scm b/guimb/scm/numaddr.scm
index 40032fd..6854556 100644
--- a/guimb/scm/numaddr.scm
+++ b/guimb/scm/numaddr.scm
@@ -13,8 +13,7 @@
 ;;;; GNU General Public License for more details.
 ;;;; 
 ;;;; You should have received a copy of the GNU General Public License along
-;;;; with GNU Mailutils; if not, write to the Free Software Foundation,
-;;;; Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+;;;; with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. 
 
 ;;;; This module provides GNU extension test "numaddr".
 
diff --git a/guimb/scm/redirect.scm b/guimb/scm/redirect.scm
index ade16d6..dd62b76 100644
--- a/guimb/scm/redirect.scm
+++ b/guimb/scm/redirect.scm
@@ -13,8 +13,7 @@
 ;;;; GNU General Public License for more details.
 ;;;; 
 ;;;; You should have received a copy of the GNU General Public License along
-;;;; with GNU Mailutils; if not, write to the Free Software Foundation,
-;;;; Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+;;;; with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
 ;;;; This module provides sieve's "redirect" action.
 
diff --git a/guimb/scm/reject.scm b/guimb/scm/reject.scm
index b399193..0447ad1 100644
--- a/guimb/scm/reject.scm
+++ b/guimb/scm/reject.scm
@@ -13,8 +13,7 @@
 ;;;; GNU General Public License for more details.
 ;;;; 
 ;;;; You should have received a copy of the GNU General Public License along
-;;;; with GNU Mailutils; if not, write to the Free Software Foundation,
-;;;; Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+;;;; with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
 ;;;; This module provides sieve's "reject" action.
 
diff --git a/guimb/scm/sieve-core.scm b/guimb/scm/sieve-core.scm
index f5f335c..be61adb 100644
--- a/guimb/scm/sieve-core.scm
+++ b/guimb/scm/sieve-core.scm
@@ -13,8 +13,7 @@
 ;;;; GNU General Public License for more details.
 ;;;; 
 ;;;; You should have received a copy of the GNU General Public License along
-;;;; with GNU Mailutils; if not, write to the Free Software Foundation,
-;;;; Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+;;;; with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
 ;;;; This module provides core functionality for the sieve scripts.
 
diff --git a/guimb/scm/sieve.scm.in b/guimb/scm/sieve.scm.in
index 68f0e49..23fd9eb 100644
--- a/guimb/scm/sieve.scm.in
+++ b/guimb/scm/sieve.scm.in
@@ -16,8 +16,7 @@
 ;;;; GNU General Public License for more details.
 ;;;; 
 ;;;; You should have received a copy of the GNU General Public License along
-;;;; with GNU Mailutils; if not, write to the Free Software Foundation,
-;;;; Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+;;;; with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 ;;;;
 
 ;;;; This is a Sieve to Scheme translator.
diff --git a/guimb/scm/vacation.scm b/guimb/scm/vacation.scm
index 3e28729..f493c48 100644
--- a/guimb/scm/vacation.scm
+++ b/guimb/scm/vacation.scm
@@ -13,8 +13,7 @@
 ;;;; GNU General Public License for more details.
 ;;;; 
 ;;;; You should have received a copy of the GNU General Public License along
-;;;; with GNU Mailutils; if not, write to the Free Software Foundation,
-;;;; Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+;;;; with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
 ;;;; This module provides "vacation" extension
 
diff --git a/guimb/util.c b/guimb/util.c
index 71a6769..8eb8c0d 100644
--- a/guimb/util.c
+++ b/guimb/util.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "guimb.h"
 
diff --git a/imap4d/Makefile.am b/imap4d/Makefile.am
index 3c06e97..1202289 100644
--- a/imap4d/Makefile.am
+++ b/imap4d/Makefile.am
@@ -1,22 +1,19 @@
-## Process this file with GNU Automake to create Makefile.in
-
-##   Copyright (C) 1999, 2000, 2001, 2002, 2003, 2005, 2007, 2008, 2009,
-##   2010 Free Software Foundation, Inc.
+## This file is part of GNU Mailutils.
+## Copyright (C) 1999, 2000, 2001, 2002, 2003, 2005, 2007, 2008, 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.
+## 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.
 ##
-##   GNU Mailutils 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.
+## GNU Mailutils 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 GNU Mailutils; if not, write to the Free Software
-##   Foundation, Inc.  51 Franklin Street, Fifth Floor, Boston, MA
-##   02110-1301 USA
+## You should have received a copy of the GNU General Public License
+## along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
 INCLUDES = @MU_APP_COMMON_INCLUDES@ @MU_AUTHINCS@ @SERV_AUTHINCS@ 
 SUBDIRS = testsuite
diff --git a/imap4d/append.c b/imap4d/append.c
index 3ec431c..f610cff 100644
--- a/imap4d/append.c
+++ b/imap4d/append.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "imap4d.h"
 
diff --git a/imap4d/auth_gsasl.c b/imap4d/auth_gsasl.c
index 8b11ad9..0c09d2c 100644
--- a/imap4d/auth_gsasl.c
+++ b/imap4d/auth_gsasl.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "imap4d.h"
 #include <gsasl.h>
diff --git a/imap4d/auth_gss.c b/imap4d/auth_gss.c
index 28ce74a..11f0745 100644
--- a/imap4d/auth_gss.c
+++ b/imap4d/auth_gss.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 /*
   GSSAPI authentication for imap (rfc 1731). 
diff --git a/imap4d/authenticate.c b/imap4d/authenticate.c
index 166294c..5be6c3f 100644
--- a/imap4d/authenticate.c
+++ b/imap4d/authenticate.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "imap4d.h"
 
diff --git a/imap4d/bye.c b/imap4d/bye.c
index 16b67df..e592d6e 100644
--- a/imap4d/bye.c
+++ b/imap4d/bye.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "imap4d.h"
 
diff --git a/imap4d/capability.c b/imap4d/capability.c
index 02b5664..1bd4df7 100644
--- a/imap4d/capability.c
+++ b/imap4d/capability.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "imap4d.h"
 
diff --git a/imap4d/check.c b/imap4d/check.c
index 4713a94..40964d2 100644
--- a/imap4d/check.c
+++ b/imap4d/check.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "imap4d.h"
 
diff --git a/imap4d/close.c b/imap4d/close.c
index 49ede1c..07501c5 100644
--- a/imap4d/close.c
+++ b/imap4d/close.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "imap4d.h"
 
diff --git a/imap4d/commands.c b/imap4d/commands.c
index a40c623..a96c31c 100644
--- a/imap4d/commands.c
+++ b/imap4d/commands.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "imap4d.h"
 
diff --git a/imap4d/copy.c b/imap4d/copy.c
index ad61fb3..f59fb72 100644
--- a/imap4d/copy.c
+++ b/imap4d/copy.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "imap4d.h"
 
diff --git a/imap4d/create.c b/imap4d/create.c
index 918926a..6ccf081 100644
--- a/imap4d/create.c
+++ b/imap4d/create.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "imap4d.h"
 #include <unistd.h>
diff --git a/imap4d/delete.c b/imap4d/delete.c
index 11f78be..678c2e7 100644
--- a/imap4d/delete.c
+++ b/imap4d/delete.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "imap4d.h"
 
diff --git a/imap4d/examine.c b/imap4d/examine.c
index cd4d669..705f86a 100644
--- a/imap4d/examine.c
+++ b/imap4d/examine.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "imap4d.h"
 
diff --git a/imap4d/expunge.c b/imap4d/expunge.c
index 46df985..8d88647 100644
--- a/imap4d/expunge.c
+++ b/imap4d/expunge.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "imap4d.h"
 
diff --git a/imap4d/fetch.c b/imap4d/fetch.c
index 34860ff..d0475cf 100644
--- a/imap4d/fetch.c
+++ b/imap4d/fetch.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "imap4d.h"
 #include <ctype.h>
diff --git a/imap4d/id.c b/imap4d/id.c
index 2561a1a..d141b3a 100644
--- a/imap4d/id.c
+++ b/imap4d/id.c
@@ -12,9 +12,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 /* Implementation of ID extension (RFC 2971) */
 
diff --git a/imap4d/idle.c b/imap4d/idle.c
index fde91ab..0e4f812 100644
--- a/imap4d/idle.c
+++ b/imap4d/idle.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "imap4d.h"
 
diff --git a/imap4d/imap4d.c b/imap4d/imap4d.c
index 90a6252..c80af2b 100644
--- a/imap4d/imap4d.c
+++ b/imap4d/imap4d.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "imap4d.h"
 #ifdef WITH_GSASL
diff --git a/imap4d/imap4d.h b/imap4d/imap4d.h
index 30b32e1..2e54f66 100644
--- a/imap4d/imap4d.h
+++ b/imap4d/imap4d.h
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #ifndef _IMAP4D_H
 #define _IMAP4D_H 1
diff --git a/imap4d/io.c b/imap4d/io.c
index 14959da..0de6464 100644
--- a/imap4d/io.c
+++ b/imap4d/io.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "imap4d.h"
 
diff --git a/imap4d/list.c b/imap4d/list.c
index 8f1dc6d..1c545c0 100644
--- a/imap4d/list.c
+++ b/imap4d/list.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "imap4d.h"
 #include <dirent.h>
diff --git a/imap4d/login.c b/imap4d/login.c
index e14649c..1da89e5 100644
--- a/imap4d/login.c
+++ b/imap4d/login.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "imap4d.h"
 
diff --git a/imap4d/logout.c b/imap4d/logout.c
index 4ce2d52..e3dffc5 100644
--- a/imap4d/logout.c
+++ b/imap4d/logout.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "imap4d.h"
 
diff --git a/imap4d/lsub.c b/imap4d/lsub.c
index 6ae650e..d743c3c 100644
--- a/imap4d/lsub.c
+++ b/imap4d/lsub.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "imap4d.h"
 
diff --git a/imap4d/namespace.c b/imap4d/namespace.c
index 335a909..b864d6e 100644
--- a/imap4d/namespace.c
+++ b/imap4d/namespace.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "imap4d.h"
 
diff --git a/imap4d/noop.c b/imap4d/noop.c
index cfbafd5..1f91ec5 100644
--- a/imap4d/noop.c
+++ b/imap4d/noop.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "imap4d.h"
 
diff --git a/imap4d/parsebuf.c b/imap4d/parsebuf.c
index 028b57a..7eab2f3 100644
--- a/imap4d/parsebuf.c
+++ b/imap4d/parsebuf.c
@@ -12,9 +12,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "imap4d.h"
 
diff --git a/imap4d/preauth.c b/imap4d/preauth.c
index b876ea8..1584df4 100644
--- a/imap4d/preauth.c
+++ b/imap4d/preauth.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 /* Preauth support for imap4d */
 
diff --git a/imap4d/rename.c b/imap4d/rename.c
index a7a65cb..bab035f 100644
--- a/imap4d/rename.c
+++ b/imap4d/rename.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "imap4d.h"
 
diff --git a/imap4d/search.c b/imap4d/search.c
index 9e4a904..96e4fed 100644
--- a/imap4d/search.c
+++ b/imap4d/search.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "imap4d.h"
 
diff --git a/imap4d/select.c b/imap4d/select.c
index 2897870..2e35dd8 100644
--- a/imap4d/select.c
+++ b/imap4d/select.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "imap4d.h"
 
diff --git a/imap4d/signal.c b/imap4d/signal.c
index d71138e..2a0053a 100644
--- a/imap4d/signal.c
+++ b/imap4d/signal.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #define __USE_MISC
 #include "imap4d.h"
diff --git a/imap4d/starttls.c b/imap4d/starttls.c
index f71d135..5ca9484 100644
--- a/imap4d/starttls.c
+++ b/imap4d/starttls.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "imap4d.h"
 
diff --git a/imap4d/status.c b/imap4d/status.c
index a462829..b572a33 100644
--- a/imap4d/status.c
+++ b/imap4d/status.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "imap4d.h"
 
diff --git a/imap4d/store.c b/imap4d/store.c
index e8d592d..44cd80c 100644
--- a/imap4d/store.c
+++ b/imap4d/store.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "imap4d.h"
 
diff --git a/imap4d/subscribe.c b/imap4d/subscribe.c
index e694636..49feb90 100644
--- a/imap4d/subscribe.c
+++ b/imap4d/subscribe.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "imap4d.h"
 
diff --git a/imap4d/sync.c b/imap4d/sync.c
index c4a5268..5901a53 100644
--- a/imap4d/sync.c
+++ b/imap4d/sync.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "imap4d.h"
 #include <mailutils/observer.h>
diff --git a/imap4d/testsuite/Makefile.am b/imap4d/testsuite/Makefile.am
index 5f3a9ee..aa5c344 100644
--- a/imap4d/testsuite/Makefile.am
+++ b/imap4d/testsuite/Makefile.am
@@ -1,21 +1,18 @@
-## Process this file with GNU Automake to create Makefile.in
-
-##   Copyright (C) 2002, 2007, 2008, 2010 Free Software Foundation, Inc.
+## This file is part of GNU Mailutils.
+## Copyright (C) 2002, 2007, 2008, 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.
+## 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.
+## GNU Mailutils 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
+## You should have received a copy of the GNU General Public License
+## along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
 AUTOMAKE_OPTIONS = dejagnu
 DEJATOOL = imap4d
diff --git a/imap4d/testsuite/imap4d/IDEF0955.exp b/imap4d/testsuite/imap4d/IDEF0955.exp
index 10e97f5..c9d0769 100644
--- a/imap4d/testsuite/imap4d/IDEF0955.exp
+++ b/imap4d/testsuite/imap4d/IDEF0955.exp
@@ -2,20 +2,18 @@
 # This file is part of Mailutils testsuite.
 # Copyright (C) 2005, 2007, 2008, 2010 Free Software Foundation, Inc.
 #  
-# This program is free software; you can redistribute it and/or modify
+# 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 of the License, or
 # (at your option) any later version.
 #   
-# This program is distributed in the hope that it will be useful,
+# GNU Mailutils 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.
+# along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
 # Mailutils versions prior to 0.6.1 allowed to use any numbers in message
 # sets, even those greater than the maximum id (message number or uid),
diff --git a/imap4d/testsuite/imap4d/IDEF0956.exp b/imap4d/testsuite/imap4d/IDEF0956.exp
index 09dabe7..63016ed 100644
--- a/imap4d/testsuite/imap4d/IDEF0956.exp
+++ b/imap4d/testsuite/imap4d/IDEF0956.exp
@@ -2,20 +2,18 @@
 # This file is part of Mailutils testsuite.
 # Copyright (C) 2005, 2007, 2010 Free Software Foundation, Inc.
 #  
-# This program is free software; you can redistribute it and/or modify
+# 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 of the License, or
 # (at your option) any later version.
 #   
-# This program is distributed in the hope that it will be useful,
+# GNU Mailutils 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.
+# along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
 # Mailutils versions prior to 0.6.1 missed check for integer overflow
 # in fetch_io. 
diff --git a/imap4d/testsuite/imap4d/anystate.exp b/imap4d/testsuite/imap4d/anystate.exp
index 7e9ef7f..f1e4dd9 100644
--- a/imap4d/testsuite/imap4d/anystate.exp
+++ b/imap4d/testsuite/imap4d/anystate.exp
@@ -2,20 +2,18 @@
 # This file is part of Mailutils testsuite.
 # Copyright (C) 2002, 2007, 2008, 2010 Free Software Foundation, Inc.
 #  
-# This program is free software; you can redistribute it and/or modify
+# 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 of the License, or
 # (at your option) any later version.
 #   
-# This program is distributed in the hope that it will be useful,
+# GNU Mailutils 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.
+# along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
 imap4d_start 
 
diff --git a/imap4d/testsuite/imap4d/append.exp b/imap4d/testsuite/imap4d/append.exp
index c635be3..e87b746 100644
--- a/imap4d/testsuite/imap4d/append.exp
+++ b/imap4d/testsuite/imap4d/append.exp
@@ -2,20 +2,18 @@
 # This file is part of Mailutils testsuite.
 # Copyright (C) 2002, 2007, 2010 Free Software Foundation, Inc.
 #  
-# This program is free software; you can redistribute it and/or modify
+# 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 of the License, or
 # (at your option) any later version.
 #   
-# This program is distributed in the hope that it will be useful,
+# GNU Mailutils 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.
+# along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
 # 6.3.11. APPEND Command
 #   Arguments:  mailbox name
diff --git a/imap4d/testsuite/imap4d/create.exp b/imap4d/testsuite/imap4d/create.exp
index 4c38351..5cd1c1d 100644
--- a/imap4d/testsuite/imap4d/create.exp
+++ b/imap4d/testsuite/imap4d/create.exp
@@ -2,20 +2,18 @@
 # This file is part of Mailutils testsuite.
 # Copyright (C) 2002, 2007, 2010 Free Software Foundation, Inc.
 #  
-# This program is free software; you can redistribute it and/or modify
+# 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 of the License, or
 # (at your option) any later version.
 #   
-# This program is distributed in the hope that it will be useful,
+# GNU Mailutils 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.
+# along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
 # 6.3.3.  CREATE Command
 #   Arguments:  mailbox name
diff --git a/imap4d/testsuite/imap4d/examine.exp b/imap4d/testsuite/imap4d/examine.exp
index a77a1b7..cde587f 100644
--- a/imap4d/testsuite/imap4d/examine.exp
+++ b/imap4d/testsuite/imap4d/examine.exp
@@ -2,20 +2,18 @@
 # This file is part of Mailutils testsuite.
 # Copyright (C) 2002, 2007, 2008, 2010 Free Software Foundation, Inc.
 #  
-# This program is free software; you can redistribute it and/or modify
+# 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 of the License, or
 # (at your option) any later version.
 #   
-# This program is distributed in the hope that it will be useful,
+# GNU Mailutils 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.
+# along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. 
 
 imap4d_start 
 imap4d_auth "user!passwd" "guessme"
diff --git a/imap4d/testsuite/imap4d/expunge.exp b/imap4d/testsuite/imap4d/expunge.exp
index d8b84eb..cc114c2 100644
--- a/imap4d/testsuite/imap4d/expunge.exp
+++ b/imap4d/testsuite/imap4d/expunge.exp
@@ -2,20 +2,18 @@
 # This file is part of Mailutils testsuite.
 # Copyright (C) 2002, 2007, 2010 Free Software Foundation, Inc.
 #  
-# This program is free software; you can redistribute it and/or modify
+# 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 of the License, or
 # (at your option) any later version.
 #   
-# This program is distributed in the hope that it will be useful,
+# GNU Mailutils 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.
+# along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
 # 6.4.3.  EXPUNGE Command
 #   Arguments:  none
diff --git a/imap4d/testsuite/imap4d/fetch.exp b/imap4d/testsuite/imap4d/fetch.exp
index 005282f..dc04bf3 100644
--- a/imap4d/testsuite/imap4d/fetch.exp
+++ b/imap4d/testsuite/imap4d/fetch.exp
@@ -2,20 +2,18 @@
 # This file is part of Mailutils testsuite.
 # Copyright (C) 2002, 2007, 2010 Free Software Foundation, Inc.
 #  
-# This program is free software; you can redistribute it and/or modify
+# 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 of the License, or
 # (at your option) any later version.
 #   
-# This program is distributed in the hope that it will be useful,
+# GNU Mailutils 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.
+# along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
 # 6.4.5.  FETCH Command
 # Arguments:  message set
diff --git a/imap4d/testsuite/imap4d/list.exp b/imap4d/testsuite/imap4d/list.exp
index 6cf551c..9233194 100644
--- a/imap4d/testsuite/imap4d/list.exp
+++ b/imap4d/testsuite/imap4d/list.exp
@@ -2,20 +2,18 @@
 # This file is part of Mailutils testsuite.
 # Copyright (C) 2002, 2005, 2007, 2008, 2010 Free Software Foundation, Inc.
 #  
-# This program is free software; you can redistribute it and/or modify
+# 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 of the License, or
 # (at your option) any later version.
 #   
-# This program is distributed in the hope that it will be useful,
+# GNU Mailutils 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.
+# along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
 imap4d_start 
 imap4d_auth "user!passwd" "guessme"
diff --git a/imap4d/testsuite/imap4d/search.exp b/imap4d/testsuite/imap4d/search.exp
index 4ae413a..8741bdb 100644
--- a/imap4d/testsuite/imap4d/search.exp
+++ b/imap4d/testsuite/imap4d/search.exp
@@ -2,20 +2,18 @@
 # This file is part of Mailutils testsuite.
 # Copyright (C) 2002, 2007, 2010 Free Software Foundation, Inc.
 #  
-# This program is free software; you can redistribute it and/or modify
+# 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 of the License, or
 # (at your option) any later version.
 #   
-# This program is distributed in the hope that it will be useful,
+# GNU Mailutils 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.
+# along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. 
 
 # 6.4.4.  SEARCH Command
 # Arguments:  OPTIONAL [CHARSET] specification
diff --git a/imap4d/testsuite/imap4d/x.exp b/imap4d/testsuite/imap4d/x.exp
index f0e6754..6c747b8 100644
--- a/imap4d/testsuite/imap4d/x.exp
+++ b/imap4d/testsuite/imap4d/x.exp
@@ -2,20 +2,18 @@
 # This file is part of Mailutils testsuite.
 # Copyright (C) 2002, 2007, 2008, 2010 Free Software Foundation, Inc.
 #  
-# This program is free software; you can redistribute it and/or modify
+# 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 of the License, or
 # (at your option) any later version.
 #   
-# This program is distributed in the hope that it will be useful,
+# GNU Mailutils 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.
+# along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
 imap4d_start 
 
diff --git a/imap4d/testsuite/lib/imap4d.exp b/imap4d/testsuite/lib/imap4d.exp
index 213c5ef..55285f5 100644
--- a/imap4d/testsuite/lib/imap4d.exp
+++ b/imap4d/testsuite/lib/imap4d.exp
@@ -2,20 +2,18 @@
 # This file is part of Mailutils testsuite.
 # Copyright (C) 2002, 2005, 2007, 2008, 2010 Free Software Foundation, Inc.
 #  
-# This program is free software; you can redistribute it and/or modify
+# 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 of the License, or
 # (at your option) any later version.
 #   
-# This program is distributed in the hope that it will be useful,
+# GNU Mailutils 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.
+# along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
 source $top_srcdir/testsuite/lib/mailutils.exp
     
diff --git a/imap4d/uid.c b/imap4d/uid.c
index e4e5859..954451d 100644
--- a/imap4d/uid.c
+++ b/imap4d/uid.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "imap4d.h"
 
diff --git a/imap4d/unsubscribe.c b/imap4d/unsubscribe.c
index 9132095..fb17bd8 100644
--- a/imap4d/unsubscribe.c
+++ b/imap4d/unsubscribe.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "imap4d.h"
 
diff --git a/imap4d/util.c b/imap4d/util.c
index 73d4031..956ddc4 100644
--- a/imap4d/util.c
+++ b/imap4d/util.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "imap4d.h"
 
diff --git a/include/Makefile.am b/include/Makefile.am
index 4fabc9d..fcb8338 100644
--- a/include/Makefile.am
+++ b/include/Makefile.am
@@ -1,21 +1,18 @@
-## Process this file with GNU Automake to create Makefile.in
-
-##   Copyright (C) 2000, 2002, 2007, 2010 Free Software Foundation, Inc.
+## This file is part of GNU Mailutils.
+## Copyright (C) 2000, 2002, 2007, 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.
+## 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.
+## GNU Mailutils 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
+## You should have received a copy of the GNU General Public License
+## along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
 SUBDIRS=mailutils
-EXTRA_DIST=confpaths.h.in
\ No newline at end of file
+EXTRA_DIST=confpaths.h.in
diff --git a/include/mailutils/Makefile.am b/include/mailutils/Makefile.am
index 13e4490..5b94709 100644
--- a/include/mailutils/Makefile.am
+++ b/include/mailutils/Makefile.am
@@ -1,26 +1,23 @@
-## Process this file with GNU Automake to create Makefile.in
-
-##   Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009,
-##   2010 Free Software Foundation, Inc.
+## This file is part of GNU Mailutils.
+## Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 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.
+## 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.
 ##
-##   GNU Mailutils 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.
+## GNU Mailutils 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 GNU Mailutils; if not, write to the Free Software
-##   Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA
-##   02110-1301 USA
+## You should have received a copy of the GNU General Public License
+## along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
 BUILT_SOURCES=errno.h debug.h
 EXTRA_DIST=errno.hin debug.hm4 types.hin
-errno.h: $(top_srcdir)/libmailutils/errors errno.hin
+errno.h: $(top_srcdir)/libmailutils/diag/errors errno.hin
 	$(AM_V_GEN)$(AWK) -f $(mu_aux_dir)/generr.awk $^ > $@
 
 debug.h: $(mu_aux_dir)/debugdef.m4 debug.hm4
diff --git a/include/mailutils/address.h b/include/mailutils/address.h
index 1ae0a48..be006a8 100644
--- a/include/mailutils/address.h
+++ b/include/mailutils/address.h
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_ADDRESS_H
 #define _MAILUTILS_ADDRESS_H
diff --git a/include/mailutils/alloc.h b/include/mailutils/alloc.h
index 27ab193..ff2c95e 100644
--- a/include/mailutils/alloc.h
+++ b/include/mailutils/alloc.h
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_ALLOC_H
 #define _MAILUTILS_ALLOC_H
diff --git a/include/mailutils/argcv.h b/include/mailutils/argcv.h
index 3dff1e5..dd9482f 100644
--- a/include/mailutils/argcv.h
+++ b/include/mailutils/argcv.h
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _ARGCV_H
 #define _ARGCV_H 1
diff --git a/include/mailutils/assoc.h b/include/mailutils/assoc.h
index a3faea8..32c8076 100644
--- a/include/mailutils/assoc.h
+++ b/include/mailutils/assoc.h
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_ASSOC_H
 #define _MAILUTILS_ASSOC_H
diff --git a/include/mailutils/attribute.h b/include/mailutils/attribute.h
index c8c066f..922ebd2 100644
--- a/include/mailutils/attribute.h
+++ b/include/mailutils/attribute.h
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_ATTRIBUTE_H
 #define _MAILUTILS_ATTRIBUTE_H
diff --git a/include/mailutils/auth.h b/include/mailutils/auth.h
index 84c6cc1..72bfa51 100644
--- a/include/mailutils/auth.h
+++ b/include/mailutils/auth.h
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_AUTH_H
 #define _MAILUTILS_AUTH_H
diff --git a/include/mailutils/body.h b/include/mailutils/body.h
index d935d00..2a116e8 100644
--- a/include/mailutils/body.h
+++ b/include/mailutils/body.h
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_BODY_H
 #define _MAILUTILS_BODY_H
diff --git a/include/mailutils/cfg.h b/include/mailutils/cfg.h
index 3fc7220..a9c5ddb 100644
--- a/include/mailutils/cfg.h
+++ b/include/mailutils/cfg.h
@@ -6,13 +6,13 @@
    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
+   GNU Mailutils 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, see <http://www.gnu.org/licenses/>.
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #ifndef _MAILUTILS_CFG_H
diff --git a/include/mailutils/cpp/Makefile.am b/include/mailutils/cpp/Makefile.am
index d9be44b..0f9ec82 100644
--- a/include/mailutils/cpp/Makefile.am
+++ b/include/mailutils/cpp/Makefile.am
@@ -1,20 +1,18 @@
-##   Process this file with GNU Automake to create Makefile.in
+## This file is part of GNU Mailutils.
+## Copyright (C) 2004, 2007, 2009, 2010 Free Software Foundation, Inc.
 ##
-##   Copyright (C) 2004, 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.
 ##
-##   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.
+## GNU Mailutils 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.
 ##
-##   GNU Mailutils 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 GNU Mailutils; if not, write to the Free Software
-##   Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+## You should have received a copy of the GNU General Public License
+## along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
 cppincludedir=$(pkgincludedir)/cpp
 cppinclude_HEADERS = \
diff --git a/include/mailutils/cpp/address.h b/include/mailutils/cpp/address.h
index 71e7913..f752f07 100644
--- a/include/mailutils/cpp/address.h
+++ b/include/mailutils/cpp/address.h
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2004, 2006, 2007, 2009, 2010 Free Software Foundation,
    Inc.
 
@@ -14,10 +13,8 @@
    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
-*/
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MUCPP_ADDRESS_H
 #define _MUCPP_ADDRESS_H
diff --git a/include/mailutils/cpp/attribute.h b/include/mailutils/cpp/attribute.h
index 64135d2..a205bad 100644
--- a/include/mailutils/cpp/attribute.h
+++ b/include/mailutils/cpp/attribute.h
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2004, 2006, 2007, 2009, 2010 Free Software Foundation,
    Inc.
 
@@ -14,10 +13,8 @@
    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
-*/
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MUCPP_ATTRIBUTE_H
 #define _MUCPP_ATTRIBUTE_H
diff --git a/include/mailutils/cpp/body.h b/include/mailutils/cpp/body.h
index 06e99ef..7115779 100644
--- a/include/mailutils/cpp/body.h
+++ b/include/mailutils/cpp/body.h
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 
    This library is free software; you can redistribute it and/or
@@ -13,10 +12,8 @@
    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
-*/
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MUCPP_BODY_H
 #define _MUCPP_BODY_H
diff --git a/include/mailutils/cpp/debug.h b/include/mailutils/cpp/debug.h
index 68e7488..15db126 100644
--- a/include/mailutils/cpp/debug.h
+++ b/include/mailutils/cpp/debug.h
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 
    This library is free software; you can redistribute it and/or
@@ -13,10 +12,8 @@
    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
-*/
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MUCPP_DEBUG_H
 #define _MUCPP_DEBUG_H
diff --git a/include/mailutils/cpp/envelope.h b/include/mailutils/cpp/envelope.h
index df33c92..9af1d72 100644
--- a/include/mailutils/cpp/envelope.h
+++ b/include/mailutils/cpp/envelope.h
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 
    This library is free software; you can redistribute it and/or
@@ -13,10 +12,8 @@
    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
-*/
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MUCPP_ENVELOPE_H
 #define _MUCPP_ENVELOPE_H
diff --git a/include/mailutils/cpp/error.h b/include/mailutils/cpp/error.h
index 1806188..eec5362 100644
--- a/include/mailutils/cpp/error.h
+++ b/include/mailutils/cpp/error.h
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2004, 2006, 2007, 2009, 2010 Free Software Foundation,
    Inc.
 
@@ -14,10 +13,8 @@
    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
-*/
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MUCPP_ERROR_H
 #define _MUCPP_ERROR_H
diff --git a/include/mailutils/cpp/filter.h b/include/mailutils/cpp/filter.h
index 08d75d4..ea8283d 100644
--- a/include/mailutils/cpp/filter.h
+++ b/include/mailutils/cpp/filter.h
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2004, 2006, 2007, 2009, 2010 Free Software Foundation,
    Inc.
 
@@ -14,10 +13,8 @@
    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
-*/
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MUCPP_FILTER_H
 #define _MUCPP_FILTER_H
diff --git a/include/mailutils/cpp/folder.h b/include/mailutils/cpp/folder.h
index f80b6de..ea004ab 100644
--- a/include/mailutils/cpp/folder.h
+++ b/include/mailutils/cpp/folder.h
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 
    This library is free software; you can redistribute it and/or
@@ -13,10 +12,8 @@
    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
-*/
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MUCPP_FOLDER_H
 #define _MUCPP_FOLDER_H
diff --git a/include/mailutils/cpp/header.h b/include/mailutils/cpp/header.h
index a302305..7f17a7c 100644
--- a/include/mailutils/cpp/header.h
+++ b/include/mailutils/cpp/header.h
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2004, 2006, 2007, 2009, 2010 Free Software Foundation,
    Inc.
 
@@ -14,10 +13,8 @@
    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
-*/
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MUCPP_HEADER_H
 #define _MUCPP_HEADER_H
diff --git a/include/mailutils/cpp/iterator.h b/include/mailutils/cpp/iterator.h
index 9c5dc7d..b03ca1b 100644
--- a/include/mailutils/cpp/iterator.h
+++ b/include/mailutils/cpp/iterator.h
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2004, 2006, 2007, 2009, 2010 Free Software Foundation,
    Inc.
 
@@ -14,10 +13,8 @@
    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
-*/
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MUCPP_ITERATOR_H
 #define _MUCPP_ITERATOR_H
diff --git a/include/mailutils/cpp/list.h b/include/mailutils/cpp/list.h
index c579b69..504f5a2 100644
--- a/include/mailutils/cpp/list.h
+++ b/include/mailutils/cpp/list.h
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2004, 2006, 2007, 2009, 2010 Free Software Foundation,
    Inc.
 
@@ -14,10 +13,8 @@
    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
-*/
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MUCPP_LIST_H
 #define _MUCPP_LIST_H
diff --git a/include/mailutils/cpp/mailbox.h b/include/mailutils/cpp/mailbox.h
index 42df080..fe2db9d 100644
--- a/include/mailutils/cpp/mailbox.h
+++ b/include/mailutils/cpp/mailbox.h
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2004, 2006, 2007, 2009, 2010 Free Software Foundation,
    Inc.
 
@@ -14,10 +13,8 @@
    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
-*/
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MUCPP_MAILBOX_H
 #define _MUCPP_MAILBOX_H
diff --git a/include/mailutils/cpp/mailcap.h b/include/mailutils/cpp/mailcap.h
index 435cbde..05c7379 100644
--- a/include/mailutils/cpp/mailcap.h
+++ b/include/mailutils/cpp/mailcap.h
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2004, 2006, 2007, 2009, 2010 Free Software Foundation,
    Inc.
 
@@ -14,10 +13,8 @@
    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
-*/
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MUCPP_MAILCAP_H
 #define _MUCPP_MAILCAP_H
diff --git a/include/mailutils/cpp/mailer.h b/include/mailutils/cpp/mailer.h
index 46bb8ef..b4be0f0 100644
--- a/include/mailutils/cpp/mailer.h
+++ b/include/mailutils/cpp/mailer.h
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2004, 2006, 2007, 2009, 2010 Free Software Foundation,
    Inc.
 
@@ -14,10 +13,8 @@
    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
-*/
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MUCPP_MAILER_H
 #define _MUCPP_MAILER_H
diff --git a/include/mailutils/cpp/mailutils.h b/include/mailutils/cpp/mailutils.h
index 9689f6c..d5e820d 100644
--- a/include/mailutils/cpp/mailutils.h
+++ b/include/mailutils/cpp/mailutils.h
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2004, 2006, 2007, 2009, 2010 Free Software Foundation,
    Inc.
 
@@ -14,10 +13,8 @@
    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
-*/
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #include <mailutils/cpp/address.h>
 #include <mailutils/cpp/attribute.h>
diff --git a/include/mailutils/cpp/message.h b/include/mailutils/cpp/message.h
index bdd50af..cd408d4 100644
--- a/include/mailutils/cpp/message.h
+++ b/include/mailutils/cpp/message.h
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2004, 2006, 2007, 2009, 2010 Free Software Foundation,
    Inc.
 
@@ -14,10 +13,8 @@
    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
-*/
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MUCPP_MESSAGE_H
 #define _MUCPP_MESSAGE_H
diff --git a/include/mailutils/cpp/mime.h b/include/mailutils/cpp/mime.h
index 1ced7b4..b3afcda 100644
--- a/include/mailutils/cpp/mime.h
+++ b/include/mailutils/cpp/mime.h
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 
    This library is free software; you can redistribute it and/or
@@ -13,10 +12,8 @@
    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
-*/
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MUCPP_MIME_H
 #define _MUCPP_MIME_H
diff --git a/include/mailutils/cpp/mutil.h b/include/mailutils/cpp/mutil.h
index 6222442..c64d9a7 100644
--- a/include/mailutils/cpp/mutil.h
+++ b/include/mailutils/cpp/mutil.h
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 
    This library is free software; you can redistribute it and/or
@@ -13,10 +12,8 @@
    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
-*/
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MUCPP_MUTIL_H
 #define _MUCPP_MUTIL_H
diff --git a/include/mailutils/cpp/pop3.h b/include/mailutils/cpp/pop3.h
index bd288e5..bff72cd 100644
--- a/include/mailutils/cpp/pop3.h
+++ b/include/mailutils/cpp/pop3.h
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2004, 2006, 2007, 2009, 2010 Free Software Foundation,
    Inc.
 
@@ -14,10 +13,8 @@
    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
-*/
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MUCPP_POP3_H
 #define _MUCPP_POP3_H
diff --git a/include/mailutils/cpp/registrar.h b/include/mailutils/cpp/registrar.h
index ba9de52..e6e639d 100644
--- a/include/mailutils/cpp/registrar.h
+++ b/include/mailutils/cpp/registrar.h
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 
    This library is free software; you can redistribute it and/or
@@ -13,10 +12,8 @@
    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
-*/
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MUCPP_REGISTRAR_H
 #define _MUCPP_REGISTRAR_H
diff --git a/include/mailutils/cpp/secret.h b/include/mailutils/cpp/secret.h
index ab4ea50..20afdfb 100644
--- a/include/mailutils/cpp/secret.h
+++ b/include/mailutils/cpp/secret.h
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 
    This library is free software; you can redistribute it and/or
@@ -13,10 +12,8 @@
    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
-*/
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MUCPP_SECRET_H
 #define _MUCPP_SECRET_H
diff --git a/include/mailutils/cpp/sieve.h b/include/mailutils/cpp/sieve.h
index c7b1ae1..c1814b8 100644
--- a/include/mailutils/cpp/sieve.h
+++ b/include/mailutils/cpp/sieve.h
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 
    This library is free software; you can redistribute it and/or
@@ -13,10 +12,8 @@
    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
-*/
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MUCPP_SIEVE_H
 #define _MUCPP_SIEVE_H
diff --git a/include/mailutils/cpp/stream.h b/include/mailutils/cpp/stream.h
index 5b67b58..5f53e7b 100644
--- a/include/mailutils/cpp/stream.h
+++ b/include/mailutils/cpp/stream.h
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2004, 2006, 2007, 2009, 2010 Free Software Foundation,
    Inc.
 
@@ -14,10 +13,8 @@
    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
-*/
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MUCPP_STREAM_H
 #define _MUCPP_STREAM_H
diff --git a/include/mailutils/cpp/url.h b/include/mailutils/cpp/url.h
index 7f218e4..dd7d065 100644
--- a/include/mailutils/cpp/url.h
+++ b/include/mailutils/cpp/url.h
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2004, 2006, 2007, 2008, 2009, 2010 Free Software
    Foundation, Inc.
 
@@ -14,10 +13,8 @@
    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
-*/
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MUCPP_URL_H
 #define _MUCPP_URL_H
diff --git a/include/mailutils/daemon.h b/include/mailutils/daemon.h
index 49c43c5..2aca128 100644
--- a/include/mailutils/daemon.h
+++ b/include/mailutils/daemon.h
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_DAEMON_H
 #define _MAILUTILS_DAEMON_H
diff --git a/include/mailutils/debug.hm4 b/include/mailutils/debug.hm4
index 7682d25..097fb2f 100644
--- a/include/mailutils/debug.hm4
+++ b/include/mailutils/debug.hm4
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_DEBUG_H
 #define _MAILUTILS_DEBUG_H
diff --git a/include/mailutils/diag.h b/include/mailutils/diag.h
index 3f3695c..1c0b595 100644
--- a/include/mailutils/diag.h
+++ b/include/mailutils/diag.h
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_DIAG_H
 #define _MAILUTILS_DIAG_H
diff --git a/include/mailutils/envelope.h b/include/mailutils/envelope.h
index 7d0f439..3c17de8 100644
--- a/include/mailutils/envelope.h
+++ b/include/mailutils/envelope.h
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_ENVELOPE_H
 # define _MAILUTILS_ENVELOPE_H
diff --git a/include/mailutils/errno.hin b/include/mailutils/errno.hin
index 306f345..81e53e6 100644
--- a/include/mailutils/errno.hin
+++ b/include/mailutils/errno.hin
@@ -14,9 +14,8 @@
    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  */
+   License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_ERRNO_H
 #define _MAILUTILS_ERRNO_H	1
diff --git a/include/mailutils/error.h b/include/mailutils/error.h
index 4b3afe3..b3b24b3 100644
--- a/include/mailutils/error.h
+++ b/include/mailutils/error.h
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_ERROR_H
 #define _MAILUTILS_ERROR_H
diff --git a/include/mailutils/filter.h b/include/mailutils/filter.h
index d9c53ca..3ee19be 100644
--- a/include/mailutils/filter.h
+++ b/include/mailutils/filter.h
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_FILTER_H
 #define _MAILUTILS_FILTER_H
diff --git a/include/mailutils/folder.h b/include/mailutils/folder.h
index b16dc47..6e5fc7f 100644
--- a/include/mailutils/folder.h
+++ b/include/mailutils/folder.h
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_FOLDER_H
 # define _MAILUTILS_FOLDER_H
diff --git a/include/mailutils/gocs.h b/include/mailutils/gocs.h
index 80d9cbe..91530b9 100644
--- a/include/mailutils/gocs.h
+++ b/include/mailutils/gocs.h
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_GOCS_H
 #define _MAILUTILS_GOCS_H
diff --git a/include/mailutils/gsasl.h b/include/mailutils/gsasl.h
index be5f2bb..e1948f1 100644
--- a/include/mailutils/gsasl.h
+++ b/include/mailutils/gsasl.h
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_GSASL_H
 #define _MAILUTILS_GSASL_H
diff --git a/include/mailutils/guile.h b/include/mailutils/guile.h
index 1db5b8a..2628e11 100644
--- a/include/mailutils/guile.h
+++ b/include/mailutils/guile.h
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MU_SCM_H
 #define _MU_SCM_H
diff --git a/include/mailutils/header.h b/include/mailutils/header.h
index 57a1107..fe617fb 100644
--- a/include/mailutils/header.h
+++ b/include/mailutils/header.h
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_HEADER_H
 #define _MAILUTILS_HEADER_H
diff --git a/include/mailutils/io.h b/include/mailutils/io.h
index 6dff003..e9c862d 100644
--- a/include/mailutils/io.h
+++ b/include/mailutils/io.h
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_IO_H
 #define _MAILUTILS_IO_H
diff --git a/include/mailutils/iterator.h b/include/mailutils/iterator.h
index 815b605..d67c063 100644
--- a/include/mailutils/iterator.h
+++ b/include/mailutils/iterator.h
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_ITERATOR_H
 #define _MAILUTILS_ITERATOR_H
diff --git a/include/mailutils/ldap.h b/include/mailutils/ldap.h
index cd167cf..66a8620 100644
--- a/include/mailutils/ldap.h
+++ b/include/mailutils/ldap.h
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_LDAP_H
 #define _MAILUTILS_LDAP_H
diff --git a/include/mailutils/libargp.h b/include/mailutils/libargp.h
index c0317be..586d74a 100644
--- a/include/mailutils/libargp.h
+++ b/include/mailutils/libargp.h
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_LIBARGP_H
 #define _MAILUTILS_LIBARGP_H
diff --git a/include/mailutils/libcfg.h b/include/mailutils/libcfg.h
index d8600ed..5333b0d 100644
--- a/include/mailutils/libcfg.h
+++ b/include/mailutils/libcfg.h
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_LIBCFG_H
 #define _MAILUTILS_LIBCFG_H
diff --git a/include/mailutils/list.h b/include/mailutils/list.h
index d8d1c91..fdf7615 100644
--- a/include/mailutils/list.h
+++ b/include/mailutils/list.h
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_LIST_H
 #define _MAILUTILS_LIST_H
diff --git a/include/mailutils/locker.h b/include/mailutils/locker.h
index da9b3b1..0147944 100644
--- a/include/mailutils/locker.h
+++ b/include/mailutils/locker.h
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_LOCKER_H
 #define _MAILUTILS_LOCKER_H
diff --git a/include/mailutils/mailbox.h b/include/mailutils/mailbox.h
index 74d3407..298e6e8 100644
--- a/include/mailutils/mailbox.h
+++ b/include/mailutils/mailbox.h
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_MAILBOX_H
 #define _MAILUTILS_MAILBOX_H
diff --git a/include/mailutils/mailcap.h b/include/mailutils/mailcap.h
index b6998d1..ed00296 100644
--- a/include/mailutils/mailcap.h
+++ b/include/mailutils/mailcap.h
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_MAILCAP_H
 #define _MAILUTILS_MAILCAP_H
diff --git a/include/mailutils/mailer.h b/include/mailutils/mailer.h
index 5463d30..f42f0eb 100644
--- a/include/mailutils/mailer.h
+++ b/include/mailutils/mailer.h
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_MAILER_H
 #define _MAILUTILS_MAILER_H
diff --git a/include/mailutils/md5.h b/include/mailutils/md5.h
index d6933e4..3471683 100644
--- a/include/mailutils/md5.h
+++ b/include/mailutils/md5.h
@@ -15,8 +15,7 @@
    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.  */
+   along with this program.  If not, see <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_MD5_H
 #define _MAILUTILS_MD5_H 1
diff --git a/include/mailutils/message.h b/include/mailutils/message.h
index 14df6f6..09c7c9e 100644
--- a/include/mailutils/message.h
+++ b/include/mailutils/message.h
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_MESSAGE_H
 #define _MAILUTILS_MESSAGE_H
diff --git a/include/mailutils/mime.h b/include/mailutils/mime.h
index cd70682..fa82ed3 100644
--- a/include/mailutils/mime.h
+++ b/include/mailutils/mime.h
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_MIME_H
 #define _MAILUTILS_MIME_H
diff --git a/include/mailutils/monitor.h b/include/mailutils/monitor.h
index 4e170da..0ba6881 100644
--- a/include/mailutils/monitor.h
+++ b/include/mailutils/monitor.h
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_MONITOR_H
 #define _MAILUTILS_MONITOR_H
diff --git a/include/mailutils/mu_auth.h b/include/mailutils/mu_auth.h
index 7453d30..978ad93 100644
--- a/include/mailutils/mu_auth.h
+++ b/include/mailutils/mu_auth.h
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_MU_AUTH_H
 #define _MAILUTILS_MU_AUTH_H
diff --git a/include/mailutils/mutil.h b/include/mailutils/mutil.h
index 86d2243..1953971 100644
--- a/include/mailutils/mutil.h
+++ b/include/mailutils/mutil.h
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_MUTIL_H
 #define _MAILUTILS_MUTIL_H
diff --git a/include/mailutils/nls.h b/include/mailutils/nls.h
index c84a772..9617d0b 100644
--- a/include/mailutils/nls.h
+++ b/include/mailutils/nls.h
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_NLS_H
 #define _MAILUTILS_NLS_H
diff --git a/include/mailutils/nntp.h b/include/mailutils/nntp.h
index d16fb54..d509877 100644
--- a/include/mailutils/nntp.h
+++ b/include/mailutils/nntp.h
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_NNTP_H
 #define _MAILUTILS_NNTP_H
diff --git a/include/mailutils/observer.h b/include/mailutils/observer.h
index ffb9ea5..f4d6dc9 100644
--- a/include/mailutils/observer.h
+++ b/include/mailutils/observer.h
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_OBSERVER_H
 #define _MAILUTILS_OBSERVER_H
diff --git a/include/mailutils/opool.h b/include/mailutils/opool.h
index 49b1b02..f8b56e6 100644
--- a/include/mailutils/opool.h
+++ b/include/mailutils/opool.h
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_OPOOL_H
 #define _MAILUTILS_OPOOL_H
diff --git a/include/mailutils/pam.h b/include/mailutils/pam.h
index 207bb23..40dca1d 100644
--- a/include/mailutils/pam.h
+++ b/include/mailutils/pam.h
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_PAM_H
 #define _MAILUTILS_PAM_H
diff --git a/include/mailutils/parse822.h b/include/mailutils/parse822.h
index 2105539..b212d18 100644
--- a/include/mailutils/parse822.h
+++ b/include/mailutils/parse822.h
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 /**
 * Parses syntatic elements defined in RFC 822.
diff --git a/include/mailutils/pop3.h b/include/mailutils/pop3.h
index 53d6c16..ebea5d8 100644
--- a/include/mailutils/pop3.h
+++ b/include/mailutils/pop3.h
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_POP3_H
 #define _MAILUTILS_POP3_H
diff --git a/include/mailutils/progmailer.h b/include/mailutils/progmailer.h
index e9a82c3..47c1280 100644
--- a/include/mailutils/progmailer.h
+++ b/include/mailutils/progmailer.h
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_PROGMAILER_H
 #define _MAILUTILS_PROGMAILER_H
diff --git a/include/mailutils/property.h b/include/mailutils/property.h
index e57bba4..1fb7334 100644
--- a/include/mailutils/property.h
+++ b/include/mailutils/property.h
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */; 
 
 #ifndef _MAILUTILS_PROPERTY_H
 #define _MAILUTILS_PROPERTY_H
diff --git a/include/mailutils/python.h b/include/mailutils/python.h
index 9bd32dd..81348fb 100644
--- a/include/mailutils/python.h
+++ b/include/mailutils/python.h
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_PYTHON_H
 #define _MAILUTILS_PYTHON_H
diff --git a/include/mailutils/radius.h b/include/mailutils/radius.h
index a62cddc..1ce4c24 100644
--- a/include/mailutils/radius.h
+++ b/include/mailutils/radius.h
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_RADIUS_H
 #define _MAILUTILS_RADIUS_H
diff --git a/include/mailutils/refcount.h b/include/mailutils/refcount.h
index 2c8d287..40f4c9a 100644
--- a/include/mailutils/refcount.h
+++ b/include/mailutils/refcount.h
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_REFCOUNT_H
 #define _MAILUTILS_REFCOUNT_H
diff --git a/include/mailutils/registrar.h b/include/mailutils/registrar.h
index 91a3acf..2042966 100644
--- a/include/mailutils/registrar.h
+++ b/include/mailutils/registrar.h
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_REGISTRAR_H
 #define _MAILUTILS_REGISTRAR_H
diff --git a/include/mailutils/secret.h b/include/mailutils/secret.h
index a97c968..6cb5c33 100644
--- a/include/mailutils/secret.h
+++ b/include/mailutils/secret.h
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_SECRET_H
 #define _MAILUTILS_SECRET_H
diff --git a/include/mailutils/sha1.h b/include/mailutils/sha1.h
index fecb294..c4624af 100644
--- a/include/mailutils/sha1.h
+++ b/include/mailutils/sha1.h
@@ -14,8 +14,7 @@
    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.  */
+   along with this program.  If not, see <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_SHA1_H
 # define _MAILUTILS_SHA1_H 1
diff --git a/include/mailutils/sieve.h b/include/mailutils/sieve.h
index f89d409..7f17f57 100644
--- a/include/mailutils/sieve.h
+++ b/include/mailutils/sieve.h
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_LIBSIEVE_H
 #define _MAILUTILS_LIBSIEVE_H
diff --git a/include/mailutils/sql.h b/include/mailutils/sql.h
index 84672a2..8c9d975 100644
--- a/include/mailutils/sql.h
+++ b/include/mailutils/sql.h
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_SQL_H
 #define _MAILUTILS_SQL_H
diff --git a/include/mailutils/sys/Makefile.am b/include/mailutils/sys/Makefile.am
index 74a732c..90610cd 100644
--- a/include/mailutils/sys/Makefile.am
+++ b/include/mailutils/sys/Makefile.am
@@ -1,21 +1,18 @@
-## Process this file with GNU Automake to create Makefile.in
-
-##   Copyright (C) 2002, 2007, 2009, 2010 Free Software Foundation, Inc.
+## This file is part of GNU Mailutils.
+## Copyright (C) 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.
+## 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.
+## GNU Mailutils 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
+## You should have received a copy of the GNU General Public License
+## along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
 sysincludedir=$(pkgincludedir)/sys
 sysinclude_HEADERS = \
diff --git a/include/mailutils/sys/amd.h b/include/mailutils/sys/amd.h
index 52f0395..2f921a3 100644
--- a/include/mailutils/sys/amd.h
+++ b/include/mailutils/sys/amd.h
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_SYS_AMD_H
 # define _MAILUTILS_SYS_AMD_H
diff --git a/include/mailutils/sys/attribute.h b/include/mailutils/sys/attribute.h
index 83e1f11..9a244c9 100644
--- a/include/mailutils/sys/attribute.h
+++ b/include/mailutils/sys/attribute.h
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_SYS_ATTRIBUTE_H
 # define _MAILUTILS_SYS_ATTRIBUTE_H
diff --git a/include/mailutils/sys/auth.h b/include/mailutils/sys/auth.h
index 0a4308d..e24c364 100644
--- a/include/mailutils/sys/auth.h
+++ b/include/mailutils/sys/auth.h
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_SYS_AUTH_H
 # define _MAILUTILS_SYS_AUTH_H
diff --git a/include/mailutils/sys/body.h b/include/mailutils/sys/body.h
index 7ec58e3..a7be7eb 100644
--- a/include/mailutils/sys/body.h
+++ b/include/mailutils/sys/body.h
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_SYS_BODY_H
 # define _MAILUTILS_SYS_BODY_H
diff --git a/include/mailutils/sys/debug.h b/include/mailutils/sys/debug.h
index 30ac959..ee26e16 100644
--- a/include/mailutils/sys/debug.h
+++ b/include/mailutils/sys/debug.h
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_SYS_DEBUG_H
 # define _MAILUTILS_SYS_DEBUG_H
diff --git a/include/mailutils/sys/envelope.h b/include/mailutils/sys/envelope.h
index fe4689a..037524f 100644
--- a/include/mailutils/sys/envelope.h
+++ b/include/mailutils/sys/envelope.h
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_SYS_ENVELOPE_H
 # define _MAILUTILS_SYS_ENVELOPE_H
diff --git a/include/mailutils/sys/filter.h b/include/mailutils/sys/filter.h
index ceceb78..5eb91f5 100644
--- a/include/mailutils/sys/filter.h
+++ b/include/mailutils/sys/filter.h
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_SYS_FILTER_H
 #define _MAILUTILS_SYS_FILTER_H
diff --git a/include/mailutils/sys/folder.h b/include/mailutils/sys/folder.h
index 912fb87..53b909d 100644
--- a/include/mailutils/sys/folder.h
+++ b/include/mailutils/sys/folder.h
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_SYS_FOLDER_H
 # define _MAILUTILS_SYS_FOLDER_H
diff --git a/include/mailutils/sys/header.h b/include/mailutils/sys/header.h
index 1898509..ac4be3c 100644
--- a/include/mailutils/sys/header.h
+++ b/include/mailutils/sys/header.h
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _HEADER0_H
 #define _HEADER0_H
diff --git a/include/mailutils/sys/imap.h b/include/mailutils/sys/imap.h
index bbad6e5..6bb154d 100644
--- a/include/mailutils/sys/imap.h
+++ b/include/mailutils/sys/imap.h
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_SYS_IMAP_H
 # define _MAILUTILS_SYS_IMAP_H
diff --git a/include/mailutils/sys/iterator.h b/include/mailutils/sys/iterator.h
index 5b5475f..2eaf4d5 100644
--- a/include/mailutils/sys/iterator.h
+++ b/include/mailutils/sys/iterator.h
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_SYS_ITERATOR_H
 # define _MAILUTILS_SYS_ITERATOR_H
diff --git a/include/mailutils/sys/list.h b/include/mailutils/sys/list.h
index 2e9f83a..abe5b70 100644
--- a/include/mailutils/sys/list.h
+++ b/include/mailutils/sys/list.h
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_SYS_LIST_H
 # define _MAILUTILS_SYS_LIST_H
diff --git a/include/mailutils/sys/mailbox.h b/include/mailutils/sys/mailbox.h
index fe36269..0b57fe0 100644
--- a/include/mailutils/sys/mailbox.h
+++ b/include/mailutils/sys/mailbox.h
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_SYS_MAILBOX_H
 # define _MAILUTILS_SYS_MAILBOX_H
diff --git a/include/mailutils/sys/mailer.h b/include/mailutils/sys/mailer.h
index b9413a3..4bd2381 100644
--- a/include/mailutils/sys/mailer.h
+++ b/include/mailutils/sys/mailer.h
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_SYS_MAILER_H
 # define _MAILUTILS_SYS_MAILER_H
diff --git a/include/mailutils/sys/message.h b/include/mailutils/sys/message.h
index 5e96d85..53f3672 100644
--- a/include/mailutils/sys/message.h
+++ b/include/mailutils/sys/message.h
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MESSAGE0_H
 #define _MESSAGE0_H
diff --git a/include/mailutils/sys/mime.h b/include/mailutils/sys/mime.h
index ce30042..e99766e 100644
--- a/include/mailutils/sys/mime.h
+++ b/include/mailutils/sys/mime.h
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_SYS_MIME_H
 #define _MAILUTILS_SYS_MIME_H
diff --git a/include/mailutils/sys/monitor.h b/include/mailutils/sys/monitor.h
index 2d76265..122eb81 100644
--- a/include/mailutils/sys/monitor.h
+++ b/include/mailutils/sys/monitor.h
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_SYS_MONITOR_H
 # define _MAILUTILS_SYS_MONITOR_H
diff --git a/include/mailutils/sys/nntp.h b/include/mailutils/sys/nntp.h
index 5b28ad5..8d0dbc8 100644
--- a/include/mailutils/sys/nntp.h
+++ b/include/mailutils/sys/nntp.h
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_SYS_NNTP_H
 #define _MAILUTILS_SYS_NNTP_H
diff --git a/include/mailutils/sys/observer.h b/include/mailutils/sys/observer.h
index 7aede1c..6cccd9c 100644
--- a/include/mailutils/sys/observer.h
+++ b/include/mailutils/sys/observer.h
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_SYS_OBSERVER_H
 # define _MAILUTILS_SYS_OBSERVER_H
diff --git a/include/mailutils/sys/pop3.h b/include/mailutils/sys/pop3.h
index 6d4b588..96d8d61 100644
--- a/include/mailutils/sys/pop3.h
+++ b/include/mailutils/sys/pop3.h
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_SYS_POP3_H
 #define _MAILUTILS_SYS_POP3_H
diff --git a/include/mailutils/sys/property.h b/include/mailutils/sys/property.h
index 4faac9c..cbc090f 100644
--- a/include/mailutils/sys/property.h
+++ b/include/mailutils/sys/property.h
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_SYS_PROPERTY_H
 # define _MAILUTILS_SYS_PROPERTY_H
diff --git a/include/mailutils/sys/registrar.h b/include/mailutils/sys/registrar.h
index 8b3d83d..535e5ce 100644
--- a/include/mailutils/sys/registrar.h
+++ b/include/mailutils/sys/registrar.h
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_SYS_REGISTRAR_H
 # define _MAILUTILS_SYS_REGISTRAR_H
diff --git a/include/mailutils/sys/url.h b/include/mailutils/sys/url.h
index 8e300ba..095a111 100644
--- a/include/mailutils/sys/url.h
+++ b/include/mailutils/sys/url.h
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_SYS_URL_H
 # define _MAILUTILS_SYS_URL_H
diff --git a/include/mailutils/syslog.h b/include/mailutils/syslog.h
index 34554f9..3abeea8 100644
--- a/include/mailutils/syslog.h
+++ b/include/mailutils/syslog.h
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_SYSLOG_H
 #define _MAILUTILS_SYSLOG_H
diff --git a/include/mailutils/tls.h b/include/mailutils/tls.h
index dc630a2..bedd9fd 100644
--- a/include/mailutils/tls.h
+++ b/include/mailutils/tls.h
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_TLS_H
 #define _MAILUTILS_TLS_H
diff --git a/include/mailutils/types.hin b/include/mailutils/types.hin
index 5917c7f..a3f3ca6 100644
--- a/include/mailutils/types.hin
+++ b/include/mailutils/types.hin
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_TYPES_H
 #define _MAILUTILS_TYPES_H
diff --git a/include/mailutils/url.h b/include/mailutils/url.h
index ca0dcb3..1531d0d 100644
--- a/include/mailutils/url.h
+++ b/include/mailutils/url.h
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_URL_H
 #define _MAILUTILS_URL_H	1
diff --git a/include/mailutils/version.h b/include/mailutils/version.h
index 60468c1..566d735 100644
--- a/include/mailutils/version.h
+++ b/include/mailutils/version.h
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAILUTILS_VERSION_H
 #define _MAILUTILS_VERSION_H
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 4f7ad17..1e584d8 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -1,22 +1,19 @@
-## Process this file with GNU Automake to create Makefile.in
-
-##   Copyright (C) 1999, 2000, 2001, 2002, 2005, 2007, 2008, 2009, 2010
-##   Free Software Foundation, Inc.
+## This file is part of GNU Mailutils.
+## Copyright (C) 1999, 2000, 2001, 2002, 2005, 2007, 2008, 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.
+## 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.
+## GNU Mailutils 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
+## You should have received a copy of the GNU General Public License
+## along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. 
 
 include gnulib.mk
 
diff --git a/lib/mailcap.c b/lib/mailcap.c
index 4a05484..fd1c40a 100644
--- a/lib/mailcap.c
+++ b/lib/mailcap.c
@@ -12,9 +12,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/lib/mu_asprintf.h b/lib/mu_asprintf.h
index 9a70cec..0f893e9 100644
--- a/lib/mu_asprintf.h
+++ b/lib/mu_asprintf.h
@@ -1,5 +1,4 @@
-/*
-   Copyright (C) 2007, 2010 Free Software Foundation, Inc.
+/* Copyright (C) 2007, 2010 Free Software Foundation, Inc.
 
    GNU Mailutils is free software; you can redistribute it and/or modify
    it under the terms of the GNU Library General Public License as published by
@@ -12,9 +11,8 @@
    GNU Library General Public License for more details.
 
    You should have received a copy of the GNU Library General
-   Public License along with GNU Mailutils; if not, write to the
-   Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
-   Boston, MA 02110-1301 USA */
+   Public License along with GNU Mailutils.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef MUASPRINTF_H
 #define MUASPRINTF_H
diff --git a/lib/mu_dbm.c b/lib/mu_dbm.c
index f18cda8..7638b4d 100644
--- a/lib/mu_dbm.c
+++ b/lib/mu_dbm.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/lib/mu_dbm.h b/lib/mu_dbm.h
index 45b868c..bec1756 100644
--- a/lib/mu_dbm.h
+++ b/lib/mu_dbm.h
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #if defined(WITH_GDBM)
 
diff --git a/lib/mu_umaxtostr.c b/lib/mu_umaxtostr.c
index c3e9e91..8d86c3f 100644
--- a/lib/mu_umaxtostr.c
+++ b/lib/mu_umaxtostr.c
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #if HAVE_CONFIG_H
 # include <config.h>
diff --git a/lib/muaux.h b/lib/muaux.h
index 4e27a55..efd037a 100644
--- a/lib/muaux.h
+++ b/lib/muaux.h
@@ -12,9 +12,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 int mu_switch_to_privs (uid_t uid, gid_t gid, mu_list_t retain_groups);
 void mu_set_signals (RETSIGTYPE (*handler) (int signo), int *sigv, int sigc);
diff --git a/lib/signal.c b/lib/signal.c
index 8ce2108..bd92e1c 100644
--- a/lib/signal.c
+++ b/lib/signal.c
@@ -12,9 +12,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/lib/strexit.c b/lib/strexit.c
index 7f1d01c..d3021b9 100644
--- a/lib/strexit.c
+++ b/lib/strexit.c
@@ -12,9 +12,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/lib/tcpwrap.c b/lib/tcpwrap.c
index 21e6310..001fc9c 100644
--- a/lib/tcpwrap.c
+++ b/lib/tcpwrap.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/lib/tcpwrap.h b/lib/tcpwrap.h
index af2d188..9ac0337 100644
--- a/lib/tcpwrap.h
+++ b/lib/tcpwrap.h
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include <mailutils/types.h>
 
diff --git a/lib/userprivs.c b/lib/userprivs.c
index 0a51dd9..2185777 100644
--- a/lib/userprivs.c
+++ b/lib/userprivs.c
@@ -12,9 +12,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/lib/utmp.c b/lib/utmp.c
index a9e00be..f6fb96b 100644
--- a/lib/utmp.c
+++ b/lib/utmp.c
@@ -12,10 +12,8 @@ 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
-Licensealong with this program; if not, write to the Free
-SoftwareFoundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-MA 02110-1301 USA. */
+You should have received a copy of the GNU General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libmailutils/.gitignore b/libmailutils/.gitignore
index 8ce64ea..048e16e 100644
--- a/libmailutils/.gitignore
+++ b/libmailutils/.gitignore
@@ -4,13 +4,5 @@
 .libs
 Makefile
 Makefile.in
-T
-_*
 .gdbinit
-mailutils-config
-parsedate.c
 *y.output
-muerrno.c
-cfg_lexer.c
-cfg_parser.c
-cfg_parser.h
diff --git a/libmailutils/Makefile.am b/libmailutils/Makefile.am
index 07a0a4e..f13085f 100644
--- a/libmailutils/Makefile.am
+++ b/libmailutils/Makefile.am
@@ -1,188 +1,42 @@
-## Process this file with GNU Automake to create Makefile.in
-
-##   Copyright (C) 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2008, 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
-
-INCLUDES = @MU_LIB_COMMON_INCLUDES@ -I${top_srcdir}/libmailutils
-
-YLWRAP = $(SHELL) $(mu_aux_dir)/gylwrap
-AM_YFLAGS=-vt
-AM_LFLAGS=-dvp
-
-SUBDIRS = . tests 
+# GNU Mailutils -- a suite of utilities for electronic mail
+# Copyright (C) 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2008, 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, see
+# <http://www.gnu.org/licenses/>.
+
+SUBDIRS = auth base address cfg diag filter mailbox mailer mime\
+ server string stream . tests 
 
 lib_LTLIBRARIES = libmailutils.la
 
-localedir = $(datadir)/locale
-AM_CPPFLAGS = \
- -DSYSCONFDIR=\"$(sysconfdir)\"\
- -DSITE_VIRTUAL_PWDDIR=\"@SITE_VIRTUAL_PWDDIR@\"\
- -DLOCALEDIR=\"$(localedir)\"
-
-EXTRA_DIST = \
- errors\
- muerrno.cin\
- parsedate.y\
- fgetpwent.c\
- cfg_lexer.l\
- cfg_parser.y\
- cfg_parser.h
-
-libmailutils_la_SOURCES = \
- acl.c\
- address.c\
- alloc.c\
- amd.c\
- argcv.c\
- asnprintf.c\
- asprintf.c\
- assoc.c\
- attachment.c\
- attribute.c\
- auth.c\
- base64.c\
- binflt.c\
- body.c\
- cstrcasecmp.c\
- cfg_driver.c\
- cfg_format.c\
- cfg_lexer.c\
- cfg_parser.c\
- crlfdot.c\
- crlfflt.c\
- cstrlower.c\
- cstrupper.c\
- daemon.c\
- date.c\
- dbgstderr.c\
- dbgstream.c\
- dbgsyslog.c\
- debug.c\
- diag.c\
- dot.c\
- envelope.c\
- fgetpwent.c\
- file_stream.c\
- filter.c\
- filter_iconv.c\
- fltstream.c\
- folder.c\
- freeitem.c\
- fromflt.c\
- gdebug.c\
- getpass.c\
- gocs.c\
- hdritr.c\
- header.c\
- iostream.c\
- iterator.c\
- ipsrv.c\
- kwd.c\
- linelenflt.c\
- list.c\
- listlist.c\
- locale.c\
- locker.c\
- mailbox.c\
- mailcap.c\
- mailer.c\
- mapfile_stream.c\
- mbx_default.c\
- mbxitr.c\
- md5.c\
- message.c\
- memory_stream.c\
- message_stream.c\
- mime.c\
- mimehdr.c\
- mkfilename.c\
- monitor.c\
- msgscan.c\
- msrv.c\
- mu_auth.c\
- muctype.c\
- munre.c\
- mutil.c\
- muerror.c\
- muerrno.c\
- nls.c\
- nullrec.c\
- observer.c\
- opool.c\
- parse822.c\
- parsedate.c\
- permstr.c\
- progmailer.c\
- prog_stream.c\
- property.c\
- qpflt.c\
- rdcache_stream.c\
- registrar.c\
- refcount.c\
- rfc2047.c\
- sha1.c\
- secret.c\
- server.c\
- socket_stream.c\
- stdio_stream.c\
- stream.c\
- stream_printf.c\
- stream_vprintf.c\
- streamcpy.c\
- streamref.c\
- strltrim.c\
- strskip.c\
- stripws.c\
- strrtrim.c\
- syslog.c\
- system.c\
- temp_file_stream.c\
- ticket.c\
- tcp.c\
- url.c\
- vartab.c\
- vasnprintf.c\
- version.c\
- wicket.c\
- xscript-stream.c
- 
-BUILT_SOURCES = parsedate.c muerrno.c cfg_parser.c cfg_parser.h cfg_lexer.c
-MOSTLYCLEANFILES=
-
-parsedate.c: $(srcdir)/parsedate.y
-	$(YLWRAP) "$(YACC) $(AM_YFLAGS)" $< \
-	          y.tab.c parsedate.c y.output parsedate.y.output \
-                  -- -yy pd_yy
-
-cfg_parser.c cfg_parser.h: $(srcdir)/cfg_parser.y
-	$(YLWRAP) "$(YACC) $(AM_YFLAGS) -d" $< \
-	          y.tab.c cfg_parser.c y.tab.h cfg_parser.h \
-                  y.output cfg_parser.y.output \
-                  -- -yy mu_cfg_yy
-
-cfg_lexer.c: $(srcdir)/cfg_lexer.l cfg_parser.h
-	$(YLWRAP) "$(LEX) $(AM_LFLAGS) $(LFLAGS)" \
-                  $(srcdir)/cfg_lexer.l lex.yy.c cfg_lexer.c \
-                  -- -yy mu_cfg_yy
-
-muerrno.c: errors muerrno.cin
-	$(AWK) -f $(mu_aux_dir)/generr.awk $^ > $@
+libmailutils_la_SOURCES =  
+
+libmailutils_la_LIBADD = \
+ @MU_COMMON_LIBRARIES@\
+ auth/libauth.la\
+ base/libbase.la\
+ address/libaddress.la\
+ cfg/libcfg.la\
+ diag/libdiag.la\
+ filter/libfilter.la\
+ mailbox/libmailbox.la\
+ mailer/libmailer.la\
+ mime/libmime.la\
+ server/libserver.la\
+ string/libstring.la\
+ stream/libstream.la
 
-libmailutils_la_LIBADD = @MU_COMMON_LIBRARIES@
 libmailutils_la_LDFLAGS = -version-info @VI_CURRENT@:@VI_REVISION@:@VI_AGE@
 
diff --git a/libmailutils/address.c b/libmailutils/address.c
deleted file mode 100644
index cd151c1..0000000
--- a/libmailutils/address.c
+++ /dev/null
@@ -1,595 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 1999, 2000, 2001, 2005, 2006, 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 <assert.h>
-#include <ctype.h>
-#include <errno.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <string.h>
-
-#include <sys/types.h>
-
-#include <mailutils/errno.h>
-#include <mailutils/mutil.h>
-#include <mailutils/parse822.h>
-#include <mailutils/address.h>
-#include <mailutils/cstr.h>
-
-/* Get email addresses from rfc822 address.  */
-int
-mu_address_create_hint (mu_address_t *a, const char *s, mu_address_t hint,
-			int hflags)
-{
-  /* 'a' must exist, and can't already have been initialized
-   */
-  int status;
-
-  if (!a)
-    return MU_ERR_OUT_PTR_NULL;
-
-  if (!s)
-    return EINVAL;
-
-  *a = NULL;
-  status = mu_parse822_address_list (a, s, hint, hflags);
-  if (status == 0)
-    {
-      /* And address-list may contain 0 addresses but parse correctly.
-       */
-      if (!*a)
-	return MU_ERR_EMPTY_ADDRESS;
-
-      (*a)->addr = strdup (s);
-      if (!(*a)->addr)
-	{
-	  mu_address_destroy (a);
-	  return ENOMEM;
-	}
-    }
-  return status;
-}
-
-int
-mu_address_create (mu_address_t *a, const char *s)
-{
-  struct mu_address hint;
-  const char *d;
-  mu_get_user_email_domain (&d);
-  hint.domain = (char*) d;
-  return mu_address_create_hint (a, s, &hint, MU_ADDR_HINT_DOMAIN);
-}
-
-/* Get email addresses from array of rfc822 addresses.
-   FIXME: No hints? */
-int
-mu_address_createv (mu_address_t *a, const char *sv[], size_t len)
-{
-  int status = 0;
-  size_t buflen = 0;
-  char *buf = 0;
-  size_t i;
-
-  if (!a)
-    return MU_ERR_OUT_PTR_NULL;
-
-  if (!sv)
-    return EINVAL;
-
-  if (len == (size_t) - 1)
-    {
-      const char **vp = sv;
-
-      len = 0;
-
-      for (len = 0; *vp; vp++, len++)
-	;
-    }
-
-  if (len == 0)
-    return EINVAL;
-
-  for (i = 0; i < len; i++)
-    {
-      /* NULL strings are allowed */
-      if (sv[i])
-	buflen += strlen (sv[i]);
-    }
-
-  buflen += (len - 1) * strlen (", ");
-  buflen += 1;			/* Termination null.  */
-
-  buf = malloc (buflen);
-
-  if (!buf)
-    return ENOMEM;
-
-  for (i = 0, buf[0] = '\0'; i < len; i++)
-    {
-      if (i != 0)
-	strcat (buf, ", ");
-
-      if (sv[i])
-	strcat (buf, sv[i]);
-    }
-
-  status = mu_address_create (a, buf);
-
-  free (buf);
-
-  return status;
-}
-
-void
-mu_address_destroy (mu_address_t *paddress)
-{
-  if (paddress && *paddress)
-    {
-      mu_address_t address = *paddress;
-      mu_address_t current;
-      for (; address; address = current)
-	{
-	  if (address->addr)
-	    free (address->addr);
-	  if (address->comments)
-	    free (address->comments);
-	  if (address->personal)
-	    free (address->personal);
-	  if (address->email)
-	    free (address->email);
-	  if (address->local_part)
-	    free (address->local_part);
-	  if (address->domain)
-	    free (address->domain);
-	  if (address->route)
-	    free (address->route);
-	  current = address->next;
-	  free (address);
-	}
-      *paddress = NULL;
-    }
-}
-
-int
-mu_address_concatenate (mu_address_t to, mu_address_t *from)
-{
-  if (!to || !from || !*from)
-    return EINVAL;
-
-  while (to->next)
-    to = to->next;
-
-  assert (to && !to->next);
-
-  to->next = *from;
-  *from = NULL;
-
-  /* discard the current string cache as it is now inaccurate */
-  if (to->addr)
-    {
-      free (to->addr);
-      to->addr = NULL;
-    }
-
-  to = to->next;
-
-  /* only the first address must have a cache */
-  if (to->addr)
-    {
-      free (to->addr);
-      to->addr = NULL;
-    }
-
-  return 0;
-}
-
-mu_address_t 
-_address_get_nth (mu_address_t addr, size_t no)
-{
-  int i;
-  
-  for (i = 1; addr; addr = addr->next, i++)
-    if (i == no)
-      break;
-  return addr;
-}
-
-int
-mu_address_get_nth (mu_address_t addr, size_t no, mu_address_t *pret)
-{
-  mu_address_t subaddr = _address_get_nth (addr, no);
-  if (!subaddr)
-    return MU_ERR_NOENT;
-  *pret = mu_address_dup (subaddr);
-  return 0;
-}
-
-
-/* General accessors: */
-#define AC4(a,b,c,d) a ## b ## c ## d
-#define ACCESSOR(action,field) AC4(mu_address_,action,_,field)
-
-#define DECL_SET(field)							\
-int									\
-ACCESSOR(set, field) (mu_address_t addr, size_t no, const char *buf)	\
-{									\
-  char *s;								\
-  mu_address_t subaddr;							\
-  									\
-  if (addr == NULL)							\
-    return EINVAL;							\
-									\
-  subaddr = _address_get_nth (addr, no);				\
-  if (!subaddr)								\
-    return MU_ERR_NOENT;						\
-									\
-  s = strdup (buf);							\
-  if (!s)								\
-    return errno;							\
-  									\
-  free (subaddr->field);						\
-  subaddr->field = s;							\
-									\
-  return 0;								\
-}
-
-#define DECL_SGET(field)						\
-int									\
-ACCESSOR(sget,field) (mu_address_t addr, size_t no, char const **sptr)	\
-{									\
-  mu_address_t subaddr;							\
-  									\
-  if (addr == NULL)							\
-    return EINVAL;							\
-									\
-  subaddr = _address_get_nth (addr, no);				\
-  if (!subaddr)								\
-    return MU_ERR_NOENT;						\
-  *sptr = subaddr->field;						\
-  return 0;								\
-}
-
-#define DECL_GET(field)							  \
-int									  \
-ACCESSOR(get,field) (mu_address_t addr, size_t no, char *buf, size_t len, \
-		     size_t *n)						  \
-{									  \
-  size_t i;								  \
-  const char *str;							  \
-  int status = ACCESSOR(sget, field) (addr, no, &str);			  \
-  									  \
-  if (status)								  \
-    return status;							  \
-									  \
-  i = mu_cpystr (buf, str, len);					  \
-  if (n)								  \
-    *n = i;								  \
-  return 0;								  \
-}
-
-#define DECL_AGET(field)						\
-int									\
-ACCESSOR(aget, field) (mu_address_t addr, size_t no, char **buf)	\
-{									\
-  const char *str;							\
-  int status = ACCESSOR(sget, field) (addr, no, &str);			\
-									\
-  if (status)								\
-    return status;							\
-									\
-  if (str)								\
-    {									\
-      *buf = strdup (str);						\
-      if (!*buf)							\
-	status = ENOMEM;						\
-    }									\
-  else									\
-    *buf = NULL;							\
-  return status;							\
-}
-
-#define DECL_ACCESSORS(field)			\
-DECL_SET(field)					\
-DECL_SGET(field)				\
-DECL_GET(field)					\
-DECL_AGET(field)          
-
-
-
-/* Personal part */
-DECL_ACCESSORS(personal)
-/* Comments */
-DECL_ACCESSORS(comments)
-/* Email */
-DECL_ACCESSORS(email)     
-/* Local part */
-DECL_ACCESSORS(local_part)
-/* Domain */
-DECL_ACCESSORS(domain)
-/* Route */
-DECL_ACCESSORS(route)
-
-
-
-#define format_char(c) do {\
- if (buflen) \
-   {\
-      *buf++ = c;\
-      buflen--;\
-   }\
- else\
-   rc++;\
-} while(0) 
-
-#define format_string(str) do {\
- if (buflen) \
-   {\
-      int n = snprintf (buf, buflen, "%s", str);\
-      buf += n;\
-      buflen -= n;\
-   }\
- else\
-   rc += strlen (str);\
-} while (0)
-     
-size_t
-mu_address_format_string (mu_address_t addr, char *buf, size_t buflen)
-{
-  int rc = 0;
-  int comma = 0;
-  
-  for (;addr; addr = addr->next)
-    {
-      if (addr->email)
-	{
-	  int space = 0;
-
-	  if (comma)
-	    format_char (',');
-	  
-	  if (addr->personal)
-	    {
-	      format_char ('"');
-	      format_string (addr->personal);
-	      format_char ('"');
-	      space++;
-	    }
-	  
-	  if (addr->comments)
-	    {
-	      if (space)
-		format_char (' ');
-	      format_char ('(');
-	      format_string (addr->comments);
-	      format_char (')');
-	      space++;
-	    }
-	  
-	  if (space)
-	    format_char (' ');
-	  format_char ('<');
-	  format_string (addr->email);
-	  format_char ('>');
-	  comma++;
-	}
-    }
-  format_char (0);
-  return rc;
-}
-
-static int
-_address_is_group (mu_address_t addr)
-{
-  if (addr->personal && !addr->local_part && !addr->domain)
-    return 1;
-  return 0;
-}
-
-static int
-_address_is_email (mu_address_t addr)
-{
-  if (addr->email)
-    return 1;
-  return 0;
-}
-
-static int
-_address_is_unix_mailbox (mu_address_t addr)
-{
-  if (addr->local_part && !addr->email)
-    return 1;
-  return 0;
-}
-
-int
-mu_address_is_group (mu_address_t addr, size_t no, int *yes)
-{
-  mu_address_t subaddr;
-  
-  if (addr == NULL)
-    return EINVAL;
-
-  subaddr = _address_get_nth (addr, no);
-  if (!subaddr)
-    return MU_ERR_NOENT;
-  
-  if (yes)
-    *yes = _address_is_group (subaddr);
-  return 0;
-}
-
-int
-mu_address_to_string (mu_address_t addr, char *buf, size_t len, size_t *n)
-{
-  size_t i;
-  if (addr == NULL)
-    return EINVAL;
-  if (buf)
-    *buf = '\0';
-
-  if (!addr->addr)
-    {
-      i = mu_address_format_string (addr, NULL, 0);
-      addr->addr = malloc (i + 1);
-      if (!addr->addr)
-	return ENOMEM;
-      mu_address_format_string (addr, addr->addr, i+1);
-    }
-      
-  i = mu_cpystr (buf, addr->addr, len);
-  if (n)
-    *n = i;
-  return 0;
-}
-
-int
-mu_address_get_count (mu_address_t addr, size_t *pcount)
-{
-  size_t j;
-  for (j = 0; addr; addr = addr->next, j++)
-    ;
-  if (pcount)
-    *pcount = j;
-  return 0;
-}
-
-int
-mu_address_get_group_count (mu_address_t addr, size_t *pcount)
-{
-  size_t j;
-  for (j = 0; addr; addr = addr->next)
-    {
-      if (_address_is_group (addr))
-	j++;
-    }
-  if (pcount)
-    *pcount = j;
-  return 0;
-}
-
-int
-mu_address_get_email_count (mu_address_t addr, size_t *pcount)
-{
-  size_t j;
-  for (j = 0; addr; addr = addr->next)
-    {
-      if (_address_is_email (addr))
-	j++;
-    }
-  if (pcount)
-    *pcount = j;
-  return 0;
-}
-
-int
-mu_address_get_unix_mailbox_count (mu_address_t addr, size_t *pcount)
-{
-  size_t j;
-  for (j = 0; addr; addr = addr->next)
-    {
-      if (_address_is_unix_mailbox (addr))
-	j++;
-    }
-  if (pcount)
-    *pcount = j;
-  return 0;
-}
-
-int
-mu_address_contains_email (mu_address_t addr, const char *email)
-{
-  for (; addr; addr = addr->next)
-    if (mu_c_strcasecmp (addr->email, email) == 0)
-      return 1;
-  return 0;
-}
-
-mu_address_t
-mu_address_dup (mu_address_t src)
-{
-  mu_address_t dst = calloc (1, sizeof (*dst));
-
-  if (!dst)
-    return NULL;
-
-  /* FIXME: How about:
-    if (src->addr)
-      dst->addr = strdup (src->addr);
-    ?
-  */
-  if (src->comments)
-    dst->comments = strdup (src->comments);
-  if (src->personal)
-    dst->personal = strdup (src->personal);
-  if (src->email)
-    dst->email = strdup (src->email);
-  if (src->local_part)
-    dst->local_part = strdup (src->local_part);
-  if (src->domain)
-    dst->domain = strdup (src->domain);
-  if (src->route)
-    dst->route = strdup (src->route);
-
-  return dst;
-}
-  
-int
-mu_address_union (mu_address_t *a, mu_address_t b)
-{
-  mu_address_t last = NULL;
-    
-  if (!a || !b)
-    return EINVAL;
-
-  if (!*a)
-    {
-      *a = mu_address_dup (b);
-      if (!*a)
-	return ENOMEM;
-      last = *a;
-      b = b->next;
-    }
-  else
-    {
-      if ((*a)->addr)
-	{
-	  free ((*a)->addr);
-	  (*a)->addr = NULL;
-	}
-      for (last = *a; last->next; last = last->next)
-	;
-    }
-
-  for (; b; b = b->next)
-    if (!mu_address_contains_email (*a, b->email))
-      {
-	mu_address_t next = mu_address_dup (b);
-	if (!next)
-	  return ENOMEM;
-	last->next = next;
-	last = next;
-      }
-  return 0;
-}
-  
diff --git a/libmailutils/address/Makefile.am b/libmailutils/address/Makefile.am
new file mode 100644
index 0000000..525fcc1
--- /dev/null
+++ b/libmailutils/address/Makefile.am
@@ -0,0 +1,24 @@
+# GNU Mailutils -- a suite of utilities for electronic mail
+# Copyright (C) 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, see
+# <http://www.gnu.org/licenses/>.
+
+noinst_LTLIBRARIES = libaddress.la
+
+libaddress_la_SOURCES = \
+ address.c\
+ parse822.c
+
+INCLUDES = @MU_LIB_COMMON_INCLUDES@ -I/libmailutils
diff --git a/libmailutils/address/address.c b/libmailutils/address/address.c
new file mode 100644
index 0000000..0bdd93e
--- /dev/null
+++ b/libmailutils/address/address.c
@@ -0,0 +1,594 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 2005, 2006, 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, see 
+   <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <assert.h>
+#include <ctype.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <string.h>
+
+#include <sys/types.h>
+
+#include <mailutils/errno.h>
+#include <mailutils/mutil.h>
+#include <mailutils/parse822.h>
+#include <mailutils/address.h>
+#include <mailutils/cstr.h>
+
+/* Get email addresses from rfc822 address.  */
+int
+mu_address_create_hint (mu_address_t *a, const char *s, mu_address_t hint,
+			int hflags)
+{
+  /* 'a' must exist, and can't already have been initialized
+   */
+  int status;
+
+  if (!a)
+    return MU_ERR_OUT_PTR_NULL;
+
+  if (!s)
+    return EINVAL;
+
+  *a = NULL;
+  status = mu_parse822_address_list (a, s, hint, hflags);
+  if (status == 0)
+    {
+      /* And address-list may contain 0 addresses but parse correctly.
+       */
+      if (!*a)
+	return MU_ERR_EMPTY_ADDRESS;
+
+      (*a)->addr = strdup (s);
+      if (!(*a)->addr)
+	{
+	  mu_address_destroy (a);
+	  return ENOMEM;
+	}
+    }
+  return status;
+}
+
+int
+mu_address_create (mu_address_t *a, const char *s)
+{
+  struct mu_address hint;
+  const char *d;
+  mu_get_user_email_domain (&d);
+  hint.domain = (char*) d;
+  return mu_address_create_hint (a, s, &hint, MU_ADDR_HINT_DOMAIN);
+}
+
+/* Get email addresses from array of rfc822 addresses.
+   FIXME: No hints? */
+int
+mu_address_createv (mu_address_t *a, const char *sv[], size_t len)
+{
+  int status = 0;
+  size_t buflen = 0;
+  char *buf = 0;
+  size_t i;
+
+  if (!a)
+    return MU_ERR_OUT_PTR_NULL;
+
+  if (!sv)
+    return EINVAL;
+
+  if (len == (size_t) - 1)
+    {
+      const char **vp = sv;
+
+      len = 0;
+
+      for (len = 0; *vp; vp++, len++)
+	;
+    }
+
+  if (len == 0)
+    return EINVAL;
+
+  for (i = 0; i < len; i++)
+    {
+      /* NULL strings are allowed */
+      if (sv[i])
+	buflen += strlen (sv[i]);
+    }
+
+  buflen += (len - 1) * strlen (", ");
+  buflen += 1;			/* Termination null.  */
+
+  buf = malloc (buflen);
+
+  if (!buf)
+    return ENOMEM;
+
+  for (i = 0, buf[0] = '\0'; i < len; i++)
+    {
+      if (i != 0)
+	strcat (buf, ", ");
+
+      if (sv[i])
+	strcat (buf, sv[i]);
+    }
+
+  status = mu_address_create (a, buf);
+
+  free (buf);
+
+  return status;
+}
+
+void
+mu_address_destroy (mu_address_t *paddress)
+{
+  if (paddress && *paddress)
+    {
+      mu_address_t address = *paddress;
+      mu_address_t current;
+      for (; address; address = current)
+	{
+	  if (address->addr)
+	    free (address->addr);
+	  if (address->comments)
+	    free (address->comments);
+	  if (address->personal)
+	    free (address->personal);
+	  if (address->email)
+	    free (address->email);
+	  if (address->local_part)
+	    free (address->local_part);
+	  if (address->domain)
+	    free (address->domain);
+	  if (address->route)
+	    free (address->route);
+	  current = address->next;
+	  free (address);
+	}
+      *paddress = NULL;
+    }
+}
+
+int
+mu_address_concatenate (mu_address_t to, mu_address_t *from)
+{
+  if (!to || !from || !*from)
+    return EINVAL;
+
+  while (to->next)
+    to = to->next;
+
+  assert (to && !to->next);
+
+  to->next = *from;
+  *from = NULL;
+
+  /* discard the current string cache as it is now inaccurate */
+  if (to->addr)
+    {
+      free (to->addr);
+      to->addr = NULL;
+    }
+
+  to = to->next;
+
+  /* only the first address must have a cache */
+  if (to->addr)
+    {
+      free (to->addr);
+      to->addr = NULL;
+    }
+
+  return 0;
+}
+
+mu_address_t 
+_address_get_nth (mu_address_t addr, size_t no)
+{
+  int i;
+  
+  for (i = 1; addr; addr = addr->next, i++)
+    if (i == no)
+      break;
+  return addr;
+}
+
+int
+mu_address_get_nth (mu_address_t addr, size_t no, mu_address_t *pret)
+{
+  mu_address_t subaddr = _address_get_nth (addr, no);
+  if (!subaddr)
+    return MU_ERR_NOENT;
+  *pret = mu_address_dup (subaddr);
+  return 0;
+}
+
+
+/* General accessors: */
+#define AC4(a,b,c,d) a ## b ## c ## d
+#define ACCESSOR(action,field) AC4(mu_address_,action,_,field)
+
+#define DECL_SET(field)							\
+int									\
+ACCESSOR(set, field) (mu_address_t addr, size_t no, const char *buf)	\
+{									\
+  char *s;								\
+  mu_address_t subaddr;							\
+  									\
+  if (addr == NULL)							\
+    return EINVAL;							\
+									\
+  subaddr = _address_get_nth (addr, no);				\
+  if (!subaddr)								\
+    return MU_ERR_NOENT;						\
+									\
+  s = strdup (buf);							\
+  if (!s)								\
+    return errno;							\
+  									\
+  free (subaddr->field);						\
+  subaddr->field = s;							\
+									\
+  return 0;								\
+}
+
+#define DECL_SGET(field)						\
+int									\
+ACCESSOR(sget,field) (mu_address_t addr, size_t no, char const **sptr)	\
+{									\
+  mu_address_t subaddr;							\
+  									\
+  if (addr == NULL)							\
+    return EINVAL;							\
+									\
+  subaddr = _address_get_nth (addr, no);				\
+  if (!subaddr)								\
+    return MU_ERR_NOENT;						\
+  *sptr = subaddr->field;						\
+  return 0;								\
+}
+
+#define DECL_GET(field)							  \
+int									  \
+ACCESSOR(get,field) (mu_address_t addr, size_t no, char *buf, size_t len, \
+		     size_t *n)						  \
+{									  \
+  size_t i;								  \
+  const char *str;							  \
+  int status = ACCESSOR(sget, field) (addr, no, &str);			  \
+  									  \
+  if (status)								  \
+    return status;							  \
+									  \
+  i = mu_cpystr (buf, str, len);					  \
+  if (n)								  \
+    *n = i;								  \
+  return 0;								  \
+}
+
+#define DECL_AGET(field)						\
+int									\
+ACCESSOR(aget, field) (mu_address_t addr, size_t no, char **buf)	\
+{									\
+  const char *str;							\
+  int status = ACCESSOR(sget, field) (addr, no, &str);			\
+									\
+  if (status)								\
+    return status;							\
+									\
+  if (str)								\
+    {									\
+      *buf = strdup (str);						\
+      if (!*buf)							\
+	status = ENOMEM;						\
+    }									\
+  else									\
+    *buf = NULL;							\
+  return status;							\
+}
+
+#define DECL_ACCESSORS(field)			\
+DECL_SET(field)					\
+DECL_SGET(field)				\
+DECL_GET(field)					\
+DECL_AGET(field)          
+
+
+
+/* Personal part */
+DECL_ACCESSORS(personal)
+/* Comments */
+DECL_ACCESSORS(comments)
+/* Email */
+DECL_ACCESSORS(email)     
+/* Local part */
+DECL_ACCESSORS(local_part)
+/* Domain */
+DECL_ACCESSORS(domain)
+/* Route */
+DECL_ACCESSORS(route)
+
+
+
+#define format_char(c) do {\
+ if (buflen) \
+   {\
+      *buf++ = c;\
+      buflen--;\
+   }\
+ else\
+   rc++;\
+} while(0) 
+
+#define format_string(str) do {\
+ if (buflen) \
+   {\
+      int n = snprintf (buf, buflen, "%s", str);\
+      buf += n;\
+      buflen -= n;\
+   }\
+ else\
+   rc += strlen (str);\
+} while (0)
+     
+size_t
+mu_address_format_string (mu_address_t addr, char *buf, size_t buflen)
+{
+  int rc = 0;
+  int comma = 0;
+  
+  for (;addr; addr = addr->next)
+    {
+      if (addr->email)
+	{
+	  int space = 0;
+
+	  if (comma)
+	    format_char (',');
+	  
+	  if (addr->personal)
+	    {
+	      format_char ('"');
+	      format_string (addr->personal);
+	      format_char ('"');
+	      space++;
+	    }
+	  
+	  if (addr->comments)
+	    {
+	      if (space)
+		format_char (' ');
+	      format_char ('(');
+	      format_string (addr->comments);
+	      format_char (')');
+	      space++;
+	    }
+	  
+	  if (space)
+	    format_char (' ');
+	  format_char ('<');
+	  format_string (addr->email);
+	  format_char ('>');
+	  comma++;
+	}
+    }
+  format_char (0);
+  return rc;
+}
+
+static int
+_address_is_group (mu_address_t addr)
+{
+  if (addr->personal && !addr->local_part && !addr->domain)
+    return 1;
+  return 0;
+}
+
+static int
+_address_is_email (mu_address_t addr)
+{
+  if (addr->email)
+    return 1;
+  return 0;
+}
+
+static int
+_address_is_unix_mailbox (mu_address_t addr)
+{
+  if (addr->local_part && !addr->email)
+    return 1;
+  return 0;
+}
+
+int
+mu_address_is_group (mu_address_t addr, size_t no, int *yes)
+{
+  mu_address_t subaddr;
+  
+  if (addr == NULL)
+    return EINVAL;
+
+  subaddr = _address_get_nth (addr, no);
+  if (!subaddr)
+    return MU_ERR_NOENT;
+  
+  if (yes)
+    *yes = _address_is_group (subaddr);
+  return 0;
+}
+
+int
+mu_address_to_string (mu_address_t addr, char *buf, size_t len, size_t *n)
+{
+  size_t i;
+  if (addr == NULL)
+    return EINVAL;
+  if (buf)
+    *buf = '\0';
+
+  if (!addr->addr)
+    {
+      i = mu_address_format_string (addr, NULL, 0);
+      addr->addr = malloc (i + 1);
+      if (!addr->addr)
+	return ENOMEM;
+      mu_address_format_string (addr, addr->addr, i+1);
+    }
+      
+  i = mu_cpystr (buf, addr->addr, len);
+  if (n)
+    *n = i;
+  return 0;
+}
+
+int
+mu_address_get_count (mu_address_t addr, size_t *pcount)
+{
+  size_t j;
+  for (j = 0; addr; addr = addr->next, j++)
+    ;
+  if (pcount)
+    *pcount = j;
+  return 0;
+}
+
+int
+mu_address_get_group_count (mu_address_t addr, size_t *pcount)
+{
+  size_t j;
+  for (j = 0; addr; addr = addr->next)
+    {
+      if (_address_is_group (addr))
+	j++;
+    }
+  if (pcount)
+    *pcount = j;
+  return 0;
+}
+
+int
+mu_address_get_email_count (mu_address_t addr, size_t *pcount)
+{
+  size_t j;
+  for (j = 0; addr; addr = addr->next)
+    {
+      if (_address_is_email (addr))
+	j++;
+    }
+  if (pcount)
+    *pcount = j;
+  return 0;
+}
+
+int
+mu_address_get_unix_mailbox_count (mu_address_t addr, size_t *pcount)
+{
+  size_t j;
+  for (j = 0; addr; addr = addr->next)
+    {
+      if (_address_is_unix_mailbox (addr))
+	j++;
+    }
+  if (pcount)
+    *pcount = j;
+  return 0;
+}
+
+int
+mu_address_contains_email (mu_address_t addr, const char *email)
+{
+  for (; addr; addr = addr->next)
+    if (mu_c_strcasecmp (addr->email, email) == 0)
+      return 1;
+  return 0;
+}
+
+mu_address_t
+mu_address_dup (mu_address_t src)
+{
+  mu_address_t dst = calloc (1, sizeof (*dst));
+
+  if (!dst)
+    return NULL;
+
+  /* FIXME: How about:
+    if (src->addr)
+      dst->addr = strdup (src->addr);
+    ?
+  */
+  if (src->comments)
+    dst->comments = strdup (src->comments);
+  if (src->personal)
+    dst->personal = strdup (src->personal);
+  if (src->email)
+    dst->email = strdup (src->email);
+  if (src->local_part)
+    dst->local_part = strdup (src->local_part);
+  if (src->domain)
+    dst->domain = strdup (src->domain);
+  if (src->route)
+    dst->route = strdup (src->route);
+
+  return dst;
+}
+  
+int
+mu_address_union (mu_address_t *a, mu_address_t b)
+{
+  mu_address_t last = NULL;
+    
+  if (!a || !b)
+    return EINVAL;
+
+  if (!*a)
+    {
+      *a = mu_address_dup (b);
+      if (!*a)
+	return ENOMEM;
+      last = *a;
+      b = b->next;
+    }
+  else
+    {
+      if ((*a)->addr)
+	{
+	  free ((*a)->addr);
+	  (*a)->addr = NULL;
+	}
+      for (last = *a; last->next; last = last->next)
+	;
+    }
+
+  for (; b; b = b->next)
+    if (!mu_address_contains_email (*a, b->email))
+      {
+	mu_address_t next = mu_address_dup (b);
+	if (!next)
+	  return ENOMEM;
+	last->next = next;
+	last = next;
+      }
+  return 0;
+}
+  
diff --git a/libmailutils/address/parse822.c b/libmailutils/address/parse822.c
new file mode 100644
index 0000000..6dc95b9
--- /dev/null
+++ b/libmailutils/address/parse822.c
@@ -0,0 +1,2039 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 2005, 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, see
+   <http://www.gnu.org/licenses/>. */
+
+/*
+
+FIXME: what is the status of this TODO list?
+
+Things to consider:
+
+  - When parsing phrase, should I ignore non-ascii, or replace with a
+    '?' character? Right now parsing fails.
+
+    --> Should ignore non-ascii, it is unicode or iso8892-1.
+
+  - Are comments allowed in domain-literals?
+
+  - Need a way to mark the *end* of a group. Maybe add a field to _mu_address,
+    int group_end;, so if you care, you can search for the end of
+    a group with address_is_group_end();
+
+    --> Groups no longer show up in the mu_address_t list.
+
+  - Need a way to parse ",,,", it's a valid address-list, it just doesn't
+    have any addresses.
+
+  - The personal for ""Sam"" <sam@here> is "Sam", and for "'s@b'" <s@b>
+    is 's@b', should I strip those outside parentheses, or is that
+    too intrusive? Maybe an apps business if it wants to?
+
+  - Should we do best effort parsing, so parsing "sam@locahost, foo@"
+    gets one address, or just say it is or it isn't in RFC format?
+    Right now we're strict, we'll see how it goes.
+
+  - parse Received: field?
+
+  - test for memory leaks on malloc failure
+
+  - fix the realloc, try a struct _string { char* b, size_t sz };
+
+The lexer finds consecutive sequences of characters, so it should
+define:
+
+struct parse822_token_t {
+    const char* b;  // beginning of token
+    const char* e;  // one past end of token
+}
+typedef struct parse822_token_t TOK;
+
+Then I can have str_append_token(), and the lexer functions can
+look like:
+
+int mu_parse822_atom(const char** p, const char* e, TOK* atom);
+
+Just a quick thought, I'll have to see how many functions that will
+actually help.
+
+  - get example addresses from rfc2822, and from the perl code.
+*/
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <assert.h>
+#include <errno.h>
+#include <string.h>
+#include <stdlib.h>
+#include <time.h>
+
+#ifdef HAVE_STRINGS_H
+# include <strings.h>
+#endif
+
+#include <mailutils/cctype.h>
+#include <mailutils/cstr.h>
+#include <mailutils/errno.h>
+#include <mailutils/parse822.h>
+#include <mailutils/address.h>
+
+#ifdef EOK
+# undef EOK
+#endif
+
+#define EOK	0
+#define EPARSE	MU_ERR_BAD_822_FORMAT
+
+/*
+ * Some convenience functions for dealing with dynamically re-sized
+ * strings.
+ */
+
+static int
+str_append_n (char **to, const char *from, size_t n)
+{
+  size_t l = 0;
+
+  /* if not to, then silently discard data */
+  if (!to)
+    {
+      return EOK;
+    }
+
+  if (*to)
+    {
+      char *bigger;
+
+      l = strlen (*to);
+
+      bigger = realloc (*to, l + n + 1);
+
+      if (!bigger)
+	{
+	  return ENOMEM;
+	}
+
+      *to = bigger;
+    }
+  else
+    {
+      *to = malloc (n + 1);
+    }
+
+  strncpy (&to[0][l], from, n);
+
+  /* strncpy is lame, nul terminate our buffer */
+
+  to[0][l + n] = 0;
+
+  return EOK;
+}
+
+static int
+str_append (char **to, const char *from)
+{
+  if (!from)
+    return 0;
+  return str_append_n (to, from, strlen (from));
+}
+
+static int
+str_append_char (char **to, char c)
+{
+  return str_append_n (to, &c, 1);
+}
+
+static int
+str_append_range (char **to, const char *b, const char *e)
+{
+  return str_append_n (to, b, e - b);
+}
+
+static void
+str_free (char **s)
+{
+  if (s && *s)
+    {
+      free (*s);
+      *s = 0;
+    }
+}
+
+/*
+ * Character Classification - could be rewritten in a C library
+ * independent way, my system's C library matches the RFC
+ * definitions. I don't know if that's guaranteed.
+ *  
+ * Note that all return values are:
+ *   1 -> TRUE
+ *   0 -> FALSE
+ * This may be appear different than the 0 == success return
+ * values of the other functions, but I was getting lost in
+ * boolean arithmetic.
+ */
+int
+mu_parse822_is_char (char c)
+{
+  return mu_isascii (c);
+}
+
+int
+mu_parse822_is_digit (char c)
+{
+  /* digit = <any ASCII decimal digit> */
+
+  return mu_isdigit ((unsigned) c);
+}
+
+int
+mu_parse822_is_ctl (char c)
+{
+  return mu_iscntrl ((unsigned) c) || c == 127 /* DEL */ ;
+}
+
+int
+mu_parse822_is_space (char c)
+{
+  return c == ' ';
+}
+
+int
+mu_parse822_is_htab (char c)
+{
+  return c == '\t';
+}
+
+int
+mu_parse822_is_lwsp_char (char c)
+{
+  return mu_parse822_is_space (c) || mu_parse822_is_htab (c);
+}
+
+int
+mu_parse822_is_special (char c)
+{
+  return strchr ("()<>@,;:\\\".[]", c) ? 1 : 0;
+}
+
+int
+parse822_is_atom_char_ex (char c)
+{
+  return !mu_parse822_is_special (c)
+    && !mu_parse822_is_space (c)
+    && !mu_parse822_is_ctl (c);
+}
+
+int
+mu_parse822_is_atom_char (char c)
+{
+  return mu_parse822_is_char (c) && parse822_is_atom_char_ex (c);
+}
+
+int
+mu_parse822_is_q_text (char c)
+{
+  return
+    mu_parse822_is_char (c) &&
+    c != '"' &&
+    c != '\\' &&
+    c != '\r';
+}
+
+int
+mu_parse822_is_d_text (char c)
+{
+  return
+    mu_parse822_is_char (c) &&
+    c != '[' &&
+    c != ']' &&
+    c != '\\' &&
+    c != '\r';
+}
+/*
+ * SMTP's version of qtext, called <q> in the RFC 821 syntax,
+ * also excludes <LF>.
+ */
+int
+mu_parse822_is_smtp_q (char c)
+{
+  return
+    mu_parse822_is_q_text (c) &&
+    c != '\n';
+}
+
+/***** From RFC 822, 3.3 Lexical Tokens *****/
+
+int
+mu_parse822_skip_nl (const char **p, const char *e)
+{
+  /* Here we consider a new-line (NL) to be either a bare LF, or
+   * a CRLF pair as required by the RFC.
+   */
+  const char *s = *p;
+
+  if ((&s[1] < e) && s[0] == '\r' && s[1] == '\n')
+    {
+      *p += 2;
+
+      return EOK;
+    }
+
+  if ((&s[0] < e) && s[0] == '\n')
+    {
+      *p += 1;
+
+      return EOK;
+    }
+
+  return EPARSE;
+}
+
+int
+mu_parse822_skip_lwsp_char (const char **p, const char *e)
+{
+  if (*p < e && mu_parse822_is_lwsp_char (**p))
+    {
+      *p += 1;
+      return EOK;
+    }
+  return EPARSE;
+}
+
+int
+mu_parse822_skip_lwsp (const char **p, const char *e)
+{
+  /*
+   * linear-white-space = 1*([[CR]LF] LWSP-char)
+   *
+   *   We interpret a bare LF as identical to the canonical CRLF
+   *   line ending, I don't know another way since on a Unix system
+   *   all CRLF will be translated to the local convention, a bare
+   *   LF, and thus we can not deal with bare NLs in the message.
+   */
+  int space = 0;
+
+  while (*p != e)
+    {
+      const char *save = *p;
+
+      if (mu_parse822_skip_lwsp_char (p, e) == EOK)
+	{
+	  space = 1;
+	  continue;
+	}
+      if (mu_parse822_skip_nl (p, e) == EOK)
+	{
+	  if (mu_parse822_skip_lwsp_char (p, e) == EOK)
+	    {
+	      continue;
+	    }
+	  *p = save;
+	  return EPARSE;
+	}
+      break;
+    }
+  return space ? EOK : EPARSE;
+}
+
+int
+mu_parse822_skip_comments (const char **p, const char *e)
+{
+  int status;
+
+  while ((status = mu_parse822_comment (p, e, 0)) == EOK)
+    ;
+
+  return EOK;
+}
+
+int
+mu_parse822_digits (const char **p, const char *e, int min, int max, int *digits)
+{
+  const char *save = *p;
+
+  int i = 0;
+
+  assert (digits);
+
+  *digits = 0;
+
+  while (*p < e && mu_parse822_is_digit (**p))
+    {
+      *digits = *digits * 10 + **p - '0';
+      *p += 1;
+      ++i;
+      if (max != 0 && i == max)
+	{
+	  break;
+	}
+    }
+  if (i < min)
+    {
+      *p = save;
+      return EPARSE;
+    }
+
+  return EOK;
+}
+
+int
+mu_parse822_special (const char **p, const char *e, char c)
+{
+  mu_parse822_skip_lwsp (p, e);	/* not comments, they start with a special... */
+
+  if ((*p != e) && **p == c)
+    {
+      *p += 1;
+      return EOK;
+    }
+  return EPARSE;
+}
+
+int
+mu_parse822_comment (const char **p, const char *e, char **comment)
+{
+  /* comment = "(" *(ctext / quoted-pair / comment) ")"
+   * ctext = <any char except "(", ")", "\", & CR, including lwsp>
+   */
+  const char *save = *p;
+  int rc;
+
+  if ((rc = mu_parse822_special (p, e, '(')))
+    {
+      return rc;
+    }
+
+  while (*p != e)
+    {
+      char c = **p;
+
+      if (c == ')')
+	{
+	  *p += 1;
+	  return EOK;		/* found end-of-comment */
+	}
+      else if (c == '(')
+	{
+	  rc = mu_parse822_comment (p, e, comment);
+	}
+      else if (c == '\\')
+	{
+	  rc = mu_parse822_quoted_pair (p, e, comment);
+	}
+      else if (c == '\r')
+	{
+	  /* invalid character... */
+	  *p += 1;
+	}
+      else if (mu_parse822_is_char (c))
+	{
+	  rc = str_append_char (comment, c);
+	  *p += 1;
+	}
+      else
+	{
+	  /* invalid character... */
+	  *p += 1;
+	}
+      if (rc != EOK)
+	break;
+    }
+
+  if (*p == e)
+    {
+      rc = EPARSE;		/* end-of-comment not found */
+    }
+
+  *p = save;
+
+  assert (rc != EOK);
+
+  return rc;
+}
+
+int
+mu_parse822_atom (const char **p, const char *e, char **atom)
+{
+  /* atom = 1*<an atom char> */
+
+  const char *save = *p;
+  int rc = EPARSE;
+
+  mu_parse822_skip_comments (p, e);
+
+  save = *p;
+
+  while ((*p != e) && (**p == '.' || mu_parse822_is_atom_char (**p)))
+    {
+      rc = str_append_char (atom, **p);
+      *p += 1;
+      if (rc != EOK)
+	{
+	  *p = save;
+	  break;
+	}
+    }
+  return rc;
+}
+
+int
+parse822_atom_ex (const char **p, const char *e, char **atom)
+{
+  /* atom = 1*<an atom char> */
+  const char *ptr;
+  int rc;
+
+  mu_parse822_skip_comments (p, e);
+
+  for (ptr = *p; (ptr != e) && parse822_is_atom_char_ex (*ptr); ptr++)
+    ;
+  if (ptr - *p == 0)
+    return EPARSE;
+  rc = str_append_n (atom, *p, ptr - *p);
+  if (rc == 0)
+    *p = ptr;
+  return rc;
+}
+
+int
+mu_parse822_quoted_pair (const char **p, const char *e, char **qpair)
+{
+  /* quoted-pair = "\" char */
+
+  int rc;
+
+  /* need TWO characters to be available */
+  if ((e - *p) < 2)
+    return EPARSE;
+
+  if (**p != '\\')
+    return EPARSE;
+
+  if ((rc = str_append_char (qpair, *(*p + 1))))
+    return rc;
+
+  *p += 2;
+
+  return EOK;
+}
+
+int
+mu_parse822_quoted_string (const char **p, const char *e, char **qstr)
+{
+  /* quoted-string = <"> *(qtext/quoted-pair) <">
+   * qtext = char except <">, "\", & CR, including lwsp-char
+   */
+
+  const char *save = *p;
+  int rc;
+
+  mu_parse822_skip_comments (p, e);
+
+  save = *p;
+
+  if ((rc = mu_parse822_special (p, e, '"')))
+    return rc;
+
+  while (*p != e)
+    {
+      char c = **p;
+
+      if (c == '"')
+	{
+	  *p += 1;
+	  return EOK;		/* found end-of-qstr */
+	}
+      else if (c == '\\')
+	{
+	  rc = mu_parse822_quoted_pair (p, e, qstr);
+	}
+      else if (c == '\r')
+	{
+	  /* invalid character... */
+	  *p += 1;
+	}
+      else if (mu_parse822_is_char (c))
+	{
+	  rc = str_append_char (qstr, c);
+	  *p += 1;
+	}
+      else
+	{
+	  /* invalid character... */
+	  *p += 1;
+	}
+      if (rc)
+	{
+	  *p = save;
+	  str_free (qstr);
+	  return rc;
+	}
+    }
+  *p = save;
+  str_free (qstr);
+  return EPARSE;		/* end-of-qstr not found */
+}
+
+int
+mu_parse822_word (const char **p, const char *e, char **word)
+{
+  /* word = atom / quoted-string */
+
+  const char *save = *p;
+  int rc = EOK;
+
+  mu_parse822_skip_comments (p, e);
+
+  save = *p;
+
+  {
+    char *qstr = 0;
+    if ((rc = mu_parse822_quoted_string (p, e, &qstr)) == EOK && qstr)
+      {
+	rc = str_append (word, qstr);
+
+	str_free (&qstr);
+
+	if (rc != EOK)
+	  *p = save;
+
+	return rc;
+      }
+    assert (qstr == NULL);
+  }
+
+  if (rc != EPARSE)
+    {
+      /* it's fatal */
+      return rc;
+    }
+
+  /* Necessary because the quoted string could have found
+   * a partial string (invalid syntax). Thus reset, the atom
+   * will fail to if the syntax is invalid.
+   * We use parse822_atom_ex to allow for non-rfc-compliant atoms:
+   *
+   * "Be liberal in what you accept, and conservative in what you send."
+   */
+
+  {
+    char *atom = 0;
+    if (parse822_atom_ex (p, e, &atom) == EOK)
+      {
+	rc = str_append (word, atom);
+
+	str_free (&atom);
+
+	if (rc != EOK)
+	  *p = save;
+
+	return rc;
+      }
+    assert (atom == NULL);
+  }
+
+  return EPARSE;
+}
+
+/* Some mailers do not quote personal part even if it contains dot.
+   Try to be smart about it.
+*/
+	
+int
+parse822_word_dot (const char **p, const char *e, char **word)
+{
+  int rc = mu_parse822_word (p, e, word);
+  for (;rc == 0 && (*p != e) && **p == '.'; ++*p)
+    rc = str_append (word, ".");
+  return rc;
+}
+
+int
+mu_parse822_phrase (const char **p, const char *e, char **phrase)
+{
+  /* phrase = 1*word */
+
+  const char *save = *p;
+  int rc;
+
+  if ((rc = parse822_word_dot (p, e, phrase)))
+    return rc;
+
+  /* ok, got the 1 word, now append all the others we can */
+  {
+    char *word = 0;
+
+    while ((rc = parse822_word_dot (p, e, &word)) == EOK)
+      {
+	rc = str_append_char (phrase, ' ');
+
+	if (rc == EOK)
+	  rc = str_append (phrase, word);
+
+	str_free (&word);
+
+	if (rc != EOK)
+	  break;
+      }
+    assert (word == NULL);
+    if (rc == EPARSE)
+      rc = EOK;			/* its not an error to find no more words */
+  }
+  if (rc)
+    *p = save;
+
+  return rc;
+}
+
+/***** From RFC 822, 6.1 Address Specification Syntax *****/
+
+static mu_address_t
+new_mb (void)
+{
+  return calloc (1, sizeof (struct mu_address));
+}
+
+static char *
+addr_field_by_mask (mu_address_t addr, int mask)
+{
+  switch (mask)						
+    {
+    case MU_ADDR_HINT_ADDR:
+      return addr->addr;
+	  
+    case MU_ADDR_HINT_COMMENTS:				
+      return addr->comments;					
+	  
+    case MU_ADDR_HINT_PERSONAL:				
+      return addr->personal;					
+
+    case MU_ADDR_HINT_EMAIL:
+      return addr->email;
+
+    case MU_ADDR_HINT_LOCAL:
+      return addr->local_part;
+      
+    case MU_ADDR_HINT_DOMAIN:				
+      return addr->domain;					
+
+    case MU_ADDR_HINT_ROUTE:
+      return addr->route;
+    }
+  return NULL;
+}							
+
+static char *
+get_val (mu_address_t hint, int hflags, char *value, int mask, int *memflag)
+{
+  if (!value && hint && (hflags & mask))
+    {
+      char *p = addr_field_by_mask (hint, mask);
+      if (p)							
+	{
+	  if (memflag)
+	    *memflag |= mask;
+	  value = strdup (p);
+	}
+    }
+  return value;
+}
+
+static void
+addr_free_fields (mu_address_t a, int memflag)
+{
+  char *p;
+  
+  if ((p = addr_field_by_mask (a, memflag & MU_ADDR_HINT_ADDR)))
+    free (p);
+  if ((p = addr_field_by_mask (a, memflag & MU_ADDR_HINT_COMMENTS)))
+    free (p);
+  if ((p = addr_field_by_mask (a, memflag & MU_ADDR_HINT_PERSONAL)))
+    free (p);
+  if ((p = addr_field_by_mask (a, memflag & MU_ADDR_HINT_EMAIL)))
+    free (p);
+  if ((p = addr_field_by_mask (a, memflag & MU_ADDR_HINT_LOCAL)))
+    free (p);
+  if ((p = addr_field_by_mask (a, memflag & MU_ADDR_HINT_DOMAIN)))
+    free (p);
+  if ((p = addr_field_by_mask (a, memflag & MU_ADDR_HINT_ROUTE)))
+    free (p);
+}
+
+static int
+fill_mb (mu_address_t *pa,
+	 char *comments, char *personal, char *local, char *domain,
+	 mu_address_t hint, int hflags)
+{
+  int rc = EOK;
+  mu_address_t a;
+  int memflag = 0;
+
+  a = new_mb ();
+
+  if (!a)
+    return ENOMEM;
+
+  a->comments = get_val (hint, hflags, comments, MU_ADDR_HINT_COMMENTS,
+			 &memflag);
+  a->personal = get_val (hint, hflags, personal, MU_ADDR_HINT_PERSONAL,
+			 &memflag);
+
+  domain = get_val (hint, hflags, domain, MU_ADDR_HINT_DOMAIN,
+		    &memflag);
+  local = get_val (hint, hflags, local, MU_ADDR_HINT_LOCAL,
+		   &memflag);
+  do
+    {
+      /* loop exists only to break out of */
+      if (!local)
+	/* no email to construct */
+	break;
+
+      if ((rc = mu_parse822_quote_local_part (&a->email, local)))
+	break;
+      if (domain)
+	{
+	  if ((rc = str_append (&a->email, "@")))
+	    break;
+	  if ((rc = str_append (&a->email, domain)))
+	    break;
+	}
+    }
+  while (0);
+
+  a->local_part = local;
+  a->domain = domain;
+
+  if (rc != EOK)
+    {
+      addr_free_fields (a, memflag);
+      /* note that the arguments have NOT been freed, we only own
+       * them on success. */
+      free (a);
+    }
+  else
+    *pa = a;
+
+  return rc;
+}
+
+int
+mu_parse822_address_list (mu_address_t *a, const char *s,
+			  mu_address_t hint, int hflags)
+{
+  /* address-list = #(address) */
+
+  const char **p = &s;
+  const char *e = &s[strlen (s)];
+  int rc = EOK;
+  mu_address_t *n = a;		/* the next address we'll be creating */
+
+  rc = mu_parse822_address (p, e, n, hint, hflags);
+
+  /* A list may start with a leading <,>, we'll find out if
+   * that's not the case at the top of the while, but give
+   * this a conditional OK unless there was some other kind
+   * of error.
+   */
+  if (rc != EOK && rc != EPARSE)
+    {
+      return rc;
+    }
+  while (*p < e)
+    {
+      mu_parse822_skip_comments (p, e);
+
+      /* An address can contain a group, so an entire
+       * list of addresses may have been appended, or no
+       * addresses at all. Walk to the end.
+       */
+      while (*n)
+	{
+	  n = &(*n)->next;
+	}
+
+      /* Remember that ',,a@b' is a valid list! So, we must find
+       * the <,>, but the address after it can be empty.
+       */
+      if ((rc = mu_parse822_special (p, e, ',')))
+	{
+	  break;
+	}
+      mu_parse822_skip_comments (p, e);
+
+      rc = mu_parse822_address (p, e, n, hint, hflags);
+
+      if (rc == EOK || rc == EPARSE)
+	{
+	  /* that's cool, it may be a <,>, we'll find out if it isn't
+	   * at the top of the loop
+	   */
+	  rc = EOK;
+	}
+      else
+	{
+	  /* anything else is a fatal error, break out */
+	  break;
+	}
+    }
+
+  if (rc)
+    {
+      mu_address_destroy (a);
+    }
+
+  return rc;
+}
+
+int
+mu_parse822_address (const char **p, const char *e, mu_address_t *a,
+		     mu_address_t hint, int hflags)
+{
+  /* address = mailbox / group / unix-mbox */
+
+  int rc;
+
+  if ((rc = mu_parse822_mail_box (p, e, a, hint, hflags)) == EPARSE)
+    {
+      if ((rc = mu_parse822_group (p, e, a, hint, hflags)) == EPARSE)
+	{
+	  rc = mu_parse822_unix_mbox (p, e, a, hint, hflags);
+	}
+    }
+
+  if (rc == 0 && *a && !(*a)->route)
+    (*a)->route = get_val (hint, hflags, NULL, MU_ADDR_HINT_ROUTE, NULL);
+
+  return rc;
+}
+
+/* No longer put groups into an address node, it wasn't useful, was
+ * troublesome, and since there wasn't an end-group marker, wasn't
+ * even conceivably useful.
+ */
+#undef ADD_GROUPS
+int
+mu_parse822_group (const char **p, const char *e, mu_address_t *a,
+		   mu_address_t hint, int hflags)
+{
+  /* group = phrase ":" [#mailbox] ";" */
+
+  const char *save = *p;
+  mu_address_t *asave = a;		/* so we can destroy these if parsing fails */
+  int rc;
+  char *phrase = 0;
+
+  mu_parse822_skip_comments (p, e);
+
+  *p = save;
+
+  if ((rc = mu_parse822_phrase (p, e, &phrase)))
+    {
+      return rc;
+    }
+
+  mu_parse822_skip_comments (p, e);
+
+  if ((rc = mu_parse822_special (p, e, ':')))
+    {
+      *p = save;
+      str_free (&phrase);
+      return rc;
+    }
+#ifdef ADD_GROUPS
+  /* fake up an address node for the group's descriptive phrase, if
+   * it fails, clean-up will happen after the loop
+   */
+  if ((rc = fill_mb (a, 0, phrase, 0, 0, hint, hflags)) == EOK)
+    {
+      a = &(*a)->next;
+    }
+  else
+    {
+      str_free (&phrase);
+    }
+#else
+  str_free (&phrase);
+#endif
+
+  /* Basically, on each loop, we may find a mailbox, but we must find
+   * a comma after the mailbox, otherwise we've popped off the end
+   * of the list.
+   */
+  while (!rc)
+    {
+      mu_parse822_skip_comments (p, e);
+
+      /* it's ok not be a mailbox, but other errors are fatal */
+      rc = mu_parse822_mail_box (p, e, a, hint, hflags);
+      if (rc == EOK)
+	{
+	  a = &(*a)->next;
+
+	  mu_parse822_skip_comments (p, e);
+	}
+      else if (rc != EPARSE)
+	{
+	  break;
+	}
+
+      if ((rc = mu_parse822_special (p, e, ',')))
+	{
+	  /* the commas aren't optional */
+	  break;
+	}
+    }
+  if (rc == EPARSE)
+    {
+      rc = EOK;			/* ok, as long as we find the ";" next */
+    }
+
+  if (rc || (rc = mu_parse822_special (p, e, ';')))
+    {
+      *p = save;
+
+      mu_address_destroy (asave);
+    }
+
+  return rc;
+}
+
+int
+mu_parse822_mail_box (const char **p, const char *e, mu_address_t *a,
+		      mu_address_t hint, int hflags)
+{
+  /* mailbox =
+   *     addr-spec [ "(" comment ")" ] /
+   *     [phrase] route-addr
+   *
+   *  Note: we parse the ancient comment on the right since
+   *    it's such "common practice". :-(
+   *  Note: phrase is called display-name in drums.
+   *  Note: phrase is optional in drums, though not in RFC 822.
+   */
+  const char *save = *p;
+  int rc;
+
+  /* -> addr-spec */
+  if ((rc = mu_parse822_addr_spec (p, e, a, hint, hflags)) == EOK)
+    {
+      mu_parse822_skip_lwsp (p, e);
+
+      /* yuck. */
+      if ((rc = mu_parse822_comment (p, e, &(*a)->personal)) == EPARSE)
+	{
+	  rc = EOK;
+	  /* cool if there's no comment, */
+	}
+      /* but if something else is wrong, destroy the address */
+      if (rc)
+	{
+	  mu_address_destroy (a);
+	  *p = save;
+	}
+
+      return rc;
+    }
+
+  /* -> phrase route-addr */
+  {
+    char *phrase = 0;
+
+    rc = mu_parse822_phrase (p, e, &phrase);
+
+    if (rc != EPARSE && rc != EOK)
+      {
+	return rc;
+      }
+
+    if ((rc = mu_parse822_route_addr (p, e, a, hint, hflags)) == EOK)
+      {
+	/* add the phrase */
+	(*a)->personal = phrase;
+
+	return EOK;
+      }
+    /* some internal error, fail out */
+    str_free (&phrase);
+    *p = save;
+
+    return rc;
+  }
+
+  return rc;
+}
+
+int
+mu_parse822_route_addr (const char **p, const char *e, mu_address_t *a,
+			mu_address_t hint, int hflags)
+{
+  /* route-addr = "<" [route] addr-spec ">" */
+
+  const char *save = *p;
+  char *route = NULL;
+  int rc;
+  int memflag = 0;
+  
+  mu_parse822_skip_comments (p, e);
+
+  if ((rc = mu_parse822_special (p, e, '<')))
+    {
+      *p = save;
+
+      return rc;
+    }
+  if (!(rc = mu_parse822_special (p, e, '>')))
+    {
+      if ((rc = fill_mb (a, 0, 0, 0, 0, hint, hflags)) == EOK)
+	rc = str_append (&(*a)->email, "");
+       
+      return rc;
+    }
+
+  mu_parse822_route (p, e, &route);
+
+  if ((rc = mu_parse822_addr_spec (p, e, a, hint, hflags)))
+    {
+      *p = save;
+
+      str_free (&route);
+
+      return rc;
+    }
+
+  (*a)->route = get_val (hint, hflags, route, MU_ADDR_HINT_ROUTE,
+			 &memflag);
+
+  mu_parse822_skip_comments (p, e);
+
+  if ((rc = mu_parse822_special (p, e, '>')))
+    {
+      *p = save;
+
+      mu_address_destroy (a);
+
+      return rc;
+    }
+
+  return EOK;
+}
+
+int
+mu_parse822_route (const char **p, const char *e, char **route)
+{
+  /* route = 1#("@" domain ) ":" */
+
+  const char *save = *p;
+  char *accumulator = 0;
+  int rc = EOK;
+
+  for (;;)
+    {
+      mu_parse822_skip_comments (p, e);
+
+      if ((rc = mu_parse822_special (p, e, '@')))
+	{
+	  break;
+	}
+
+      if ((rc = str_append (&accumulator, "@")))
+	{
+	  break;
+	}
+
+      mu_parse822_skip_comments (p, e);
+
+      if ((rc = mu_parse822_domain (p, e, &accumulator)))
+	{
+	  /* it looked like a route, but there's no domain! */
+	  break;
+	}
+
+      mu_parse822_skip_comments (p, e);
+
+      if ((rc = mu_parse822_special (p, e, ',')) == EPARSE)
+	{
+	  /* no more routes, but we got one so its ok */
+	  rc = EOK;
+	  break;
+	}
+      if ((rc = str_append (&accumulator, ", ")))
+	{
+	  break;
+	}
+    }
+
+  mu_parse822_skip_comments (p, e);
+
+  if (!rc)
+    {
+      rc = mu_parse822_special (p, e, ':');
+    }
+
+  if (!rc)
+    {
+      rc = str_append (route, accumulator);
+    }
+  if (rc)
+    {
+      *p = save;
+    }
+  str_free (&accumulator);
+  return rc;
+}
+
+int
+mu_parse822_addr_spec (const char **p, const char *e, mu_address_t *a,
+		       mu_address_t hint, int hflags)
+{
+  /* addr-spec = local-part "@" domain */
+
+  const char *save = *p;
+  char *local_part = 0;
+  char *domain = 0;
+  int rc;
+
+  rc = mu_parse822_local_part (p, e, &local_part);
+
+  mu_parse822_skip_comments (p, e);
+
+  if (!rc)
+    {
+      rc = mu_parse822_special (p, e, '@');
+      
+      if (!rc)
+	{
+	  rc = mu_parse822_domain (p, e, &domain);
+
+	  if (!rc)
+	    rc = fill_mb (a, 0, 0, local_part, domain, hint, hflags);
+	}
+    }
+  
+  if (rc)
+    {
+      *p = save;
+      str_free (&local_part);
+      str_free (&domain);
+    }
+  return rc;
+}
+
+int
+mu_parse822_unix_mbox (const char **p, const char *e, mu_address_t *a,
+		       mu_address_t hint, int hflags)
+{
+  /* unix-mbox = atom */
+
+  const char *save = *p;
+  char *mbox = 0;
+  int rc;
+
+  mu_parse822_skip_comments (p, e);
+
+  rc = mu_parse822_atom (p, e, &mbox);
+
+  if (!rc)
+    rc = fill_mb (a, 0, 0, mbox, 0, hint, hflags);
+
+  if (rc)
+    {
+      *p = save;
+      str_free (&mbox);
+    }
+  return rc;
+}
+
+int
+mu_parse822_local_part (const char **p, const char *e, char **local_part)
+{
+  /* local-part = word *("." word)
+   *
+   * Note: rewrite as ->  word ["." local-part]
+   */
+
+  const char *save = *p;
+  const char *save2 = *p;
+  int rc;
+
+  mu_parse822_skip_comments (p, e);
+
+  if ((rc = mu_parse822_word (p, e, local_part)))
+    {
+      *p = save;
+      return rc;
+    }
+  /* We've got a local-part, but keep looking for more. */
+
+  mu_parse822_skip_comments (p, e);
+
+  /* If we get a parse error, we roll back to save2, but if
+   * something else failed, we have to roll back to save.
+   */
+  save2 = *p;
+
+  rc = mu_parse822_special (p, e, '.');
+
+  if (!rc)
+    {
+      char *more = 0;
+      if ((rc = mu_parse822_local_part (p, e, &more)) == EOK)
+	{
+	  /* append more */
+	  if ((rc = str_append (local_part, ".")) == EOK)
+	    {
+	      rc = str_append (local_part, more);
+	    }
+	}
+      str_free (&more);
+    }
+
+  if (rc == EPARSE)
+    {
+      /* if we didn't get more ("." word) pairs, that's ok */
+      *p = save2;
+      rc = EOK;
+    }
+  if (rc)
+    {
+      /* if anything else failed, that's real */
+      *p = save;
+      str_free (local_part);
+    }
+  return rc;
+}
+
+int
+mu_parse822_domain (const char **p, const char *e, char **domain)
+{
+  /* domain = sub-domain *("." sub-domain)
+   *
+   * Note: rewrite as -> sub-domain ("." domain)
+   */
+
+  const char *save = *p;
+  const char *save2 = 0;
+  int rc;
+
+  mu_parse822_skip_comments (p, e);
+
+  if ((rc = mu_parse822_sub_domain (p, e, domain)))
+    {
+      *p = save;
+      return rc;
+    }
+
+
+  /* We save before skipping comments to preserve the comment
+   * at the end of a domain, the addr-spec may want to abuse it
+   * for a personal name.
+   */
+  save2 = *p;
+
+  /* we've got the 1, keep looking for more */
+
+  mu_parse822_skip_comments (p, e);
+
+  rc = mu_parse822_special (p, e, '.');
+
+  if (!rc)
+    {
+      char *more = 0;
+      if ((rc = mu_parse822_domain (p, e, &more)) == EOK)
+	{
+	  if ((rc = str_append (domain, ".")) == EOK)
+	    {
+	      rc = str_append (domain, more);
+	    }
+	}
+      str_free (&more);
+    }
+  if (rc == EPARSE)
+    {
+      /* we didn't parse more ("." sub-domain) pairs, that's ok */
+      *p = save2;
+      rc = EOK;
+    }
+
+  if (rc)
+    {
+      /* something else failed, roll it all back */
+      *p = save;
+      str_free (domain);
+    }
+  return rc;
+}
+
+int
+mu_parse822_sub_domain (const char **p, const char *e, char **sub_domain)
+{
+  /* sub-domain = domain-ref / domain-literal
+   */
+
+  int rc;
+
+  if ((rc = mu_parse822_domain_ref (p, e, sub_domain)) == EPARSE)
+    rc = mu_parse822_domain_literal (p, e, sub_domain);
+
+  return rc;
+}
+
+int
+mu_parse822_domain_ref (const char **p, const char *e, char **domain_ref)
+{
+  /* domain-ref = atom */
+
+  return mu_parse822_atom (p, e, domain_ref);
+}
+
+int
+mu_parse822_d_text (const char **p, const char *e, char **dtext)
+{
+  /* d-text = 1*dtext
+   *
+   * Note: dtext is only defined as a character class in
+   *  RFC822, but this definition is more useful for
+   *  slurping domain literals.
+   */
+
+  const char *start = *p;
+  int rc = EOK;
+
+  while (*p < e && mu_parse822_is_d_text (**p))
+    {
+      *p += 1;
+    }
+
+  if (start == *p)
+    {
+      return EPARSE;
+    }
+
+  if ((rc = str_append_range (dtext, start, *p)))
+    {
+      *p = start;
+    }
+
+  return rc;
+}
+
+int
+mu_parse822_domain_literal (const char **p, const char *e, char **domain_literal)
+{
+  /* domain-literal = "[" *(dtext / quoted-pair) "]" */
+
+  const char *save = *p;
+  char *literal = 0;
+  int rc;
+
+  if ((rc = mu_parse822_special (p, e, '[')))
+    {
+      return rc;
+    }
+  if ((rc = str_append_char (&literal, '[')))
+    {
+      *p = save;
+      return rc;
+    }
+
+  while ((rc = mu_parse822_d_text (p, e, &literal)) == EOK ||
+	 (rc = mu_parse822_quoted_pair (p, e, &literal)) == EOK)
+    {
+      /* Eat all of this we can get! */
+    }
+  if (rc == EPARSE)
+    {
+      rc = EOK;
+    }
+  if (!rc)
+    {
+      rc = mu_parse822_special (p, e, ']');
+    }
+  if (!rc)
+    {
+      rc = str_append_char (&literal, ']');
+    }
+  if (!rc)
+    {
+      rc = str_append (domain_literal, literal);
+    }
+
+  str_free (&literal);
+
+  if (rc)
+    {
+      *p = save;
+    }
+  return rc;
+}
+
+/***** From RFC 822, 5.1 Date and Time Specification Syntax *****/
+
+int
+mu_parse822_day (const char **p, const char *e, int *day)
+{
+  /* day = "Mon" / "Tue" / "Wed" / "Thu" / "Fri" / "Sat" / "Sun" */
+
+  const char *days[] = {
+    "Sun",
+    "Mon",
+    "Tue",
+    "Wed",
+    "Thu",
+    "Fri",
+    "Sat",
+    NULL
+  };
+
+  int d;
+
+  mu_parse822_skip_comments (p, e);
+
+  if ((e - *p) < 3)
+    return EPARSE;
+
+  for (d = 0; days[d]; d++)
+    {
+      if (mu_c_strncasecmp (*p, days[d], 3) == 0)
+	{
+	  *p += 3;
+	  if (day)
+	    *day = d;
+	  return EOK;
+	}
+    }
+  return EPARSE;
+}
+
+int
+mu_parse822_date (const char **p, const char *e, int *day, int *mon, int *year)
+{
+  /* date = 1*2DIGIT month 2*4DIGIT
+   * month =  "Jan"  /  "Feb" /  "Mar"  /  "Apr"
+   *       /  "May"  /  "Jun" /  "Jul"  /  "Aug"
+   *       /  "Sep"  /  "Oct" /  "Nov"  /  "Dec"
+   */
+
+  const char *mons[] = {
+    "Jan",
+    "Feb",
+    "Mar",
+    "Apr",
+    "May",
+    "Jun",
+    "Jul",
+    "Aug",
+    "Sep",
+    "Oct",
+    "Nov",
+    "Dec",
+    NULL
+  };
+
+  const char *save = *p;
+  int rc = EOK;
+  int m = 0;
+  int yr = 0;
+  const char *yrbeg = 0;
+
+  mu_parse822_skip_comments (p, e);
+
+  if ((rc = mu_parse822_digits (p, e, 1, 2, day)))
+    {
+      *p = save;
+      return rc;
+    }
+
+  mu_parse822_skip_comments (p, e);
+
+  if ((e - *p) < 3)
+    return EPARSE;
+
+  for (m = 0; mons[m]; m++)
+    {
+      if (mu_c_strncasecmp (*p, mons[m], 3) == 0)
+	{
+	  *p += 3;
+	  if (mon)
+	    *mon = m;
+	  break;
+	}
+    }
+
+  if (!mons[m])
+    {
+      *p = save;
+      return EPARSE;
+    }
+
+  mu_parse822_skip_comments (p, e);
+
+  /* We need to count how many digits their were, and adjust the
+   * interpretation of the year accordingly. This is from RFC 2822,
+   * Section 4.3, Obsolete Date and Time. */
+  yrbeg = *p;
+
+  if ((rc = mu_parse822_digits (p, e, 2, 4, &yr)))
+    {
+      *p = save;
+      return rc;
+    }
+
+  /* rationalize year to four digit, then adjust to tz notation */
+  switch (*p - yrbeg)
+    {
+    case 2:
+      if (yr >= 0 && yr <= 49)
+	{
+	  yr += 2000;
+	  break;
+	}
+    case 3:
+      yr += 1900;
+      break;
+    }
+
+  if (year)
+    *year = yr - 1900;
+
+  return EOK;
+}
+
+int
+mu_parse822_time (const char **p, const char *e,
+	       int *hour, int *min, int *sec, int *tz, const char **tz_name)
+{
+  /* time        =  hour zone
+   * hour        =  2DIGIT ":" 2DIGIT [":" 2DIGIT] ; 00:00:00 - 23:59:59
+   * zone        =  "UT"  / "GMT"           ; Universal Time
+   *                                        ; North American : UT
+   *             /  "EST" / "EDT"           ;  Eastern:  - 5/ - 4
+   *             /  "CST" / "CDT"           ;  Central:  - 6/ - 5
+   *             /  "MST" / "MDT"           ;  Mountain: - 7/ - 6
+   *             /  "PST" / "PDT"           ;  Pacific:  - 8/ - 7
+   *             /  1ALPHA                  ; RFC 822 was wrong, RFC 2822
+   *                                        ; says treat these all as -0000.
+   *             / ( ("+" / "-") 4DIGIT )   ; Local differential
+   *                                        ;  hours+min. (HHMM)
+   */
+
+  struct
+  {
+    const char *tzname;
+    int tz;
+  }
+  tzs[] =
+  {
+    { "UT",   0 * 60 * 60 },
+    { "UTC",  0 * 60 * 60 },
+    { "GMT",  0 * 60 * 60 },
+    { "EST", -5 * 60 * 60 },
+    { "EDT", -4 * 60 * 60 },
+    { "CST", -6 * 60 * 60 },
+    { "CDT", -5 * 60 * 60 },
+    { "MST", -7 * 60 * 60 },
+    { "MDT", -6 * 60 * 60 },
+    { "PST", -8 * 60 * 60 },
+    { "PDT", -7 * 60 * 60 },
+    { NULL, 0}
+  };
+
+  const char *save = *p;
+  int rc = EOK;
+  int z = 0;
+  char *zone = NULL;
+
+  mu_parse822_skip_comments (p, e);
+
+  if ((rc = mu_parse822_digits (p, e, 1, 2, hour)))
+    {
+      *p = save;
+      return rc;
+    }
+
+  if ((rc = mu_parse822_special (p, e, ':')))
+    {
+      *p = save;
+      return rc;
+    }
+
+  if ((rc = mu_parse822_digits (p, e, 1, 2, min)))
+    {
+      *p = save;
+      return rc;
+    }
+
+  if ((rc = mu_parse822_special (p, e, ':')))
+    {
+      *sec = 0;
+    }
+  else if ((rc = mu_parse822_digits (p, e, 1, 2, sec)))
+    {
+      *p = save;
+      return rc;
+    }
+
+  mu_parse822_skip_comments (p, e);
+
+  if ((rc = mu_parse822_atom (p, e, &zone)))
+    {
+      /* zone is optional */
+      if (tz)
+	*tz = 0;
+      return EOK;
+    }
+
+  /* see if it's a timezone */
+  for (; tzs[z].tzname; z++)
+    {
+      if (mu_c_strcasecmp (zone, tzs[z].tzname) == 0)
+	break;
+    }
+  if (tzs[z].tzname)
+    {
+      if (tz_name)
+	*tz_name = tzs[z].tzname;
+
+      if (tz)
+	*tz = tzs[z].tz;
+    }
+  else if (strlen (zone) > 5 || strlen (zone) < 4)
+    {
+      if (*tz)
+	*tz = 0; /* Assume UTC */
+    }
+  else
+    {
+      /* zone = ( + / - ) hhmm */
+      int hh;
+      int mm;
+      int sign;
+      char *zp = zone;
+
+      switch (zp[0])
+	{
+	case '-':
+	  sign = -1;
+	  zp++;
+	  break;
+	case '+':
+	  sign = +1;
+	  zp++;
+	  break;
+	default:
+	  sign = 1;
+	  break;
+	}
+
+      if (strspn (zp, "0123456789") == 4)
+	{
+      /* convert to seconds from UTC */
+      hh = (zone[1] - '0') * 10 + (zone[2] - '0');
+      mm = (zone[3] - '0') * 10 + (zone[4] - '0');
+	}
+      else
+	{
+	  hh = mm = 0; /* Consider equivalent to -0000 */
+	}
+      if (tz)
+	*tz = sign * (hh * 60 * 60 + mm * 60);
+    }
+
+  str_free (&zone);
+
+  return EOK;
+}
+
+#if 0
+For reference, especially the for the required range and values of the
+integer fields.
+
+struct tm
+{
+  int tm_sec;			/* Seconds.	[0-60] (1 leap second) */
+  int tm_min;			/* Minutes.	[0-59] */
+  int tm_hour;			/* Hours.	[0-23] */
+  int tm_mday;			/* Day.		[1-31] */
+  int tm_mon;			/* Month.	[0-11] */
+  int tm_year;			/* Year	- 1900.  */
+  int tm_wday;			/* Day of week.	[0-6] */
+  int tm_yday;			/* Days in year.[0-365]	*/
+  int tm_isdst;			/* DST.		[-1/0/1]*/
+
+  int tm_gmtoff;        /* Seconds east of UTC. */
+  const char *tm_zone;	/* Timezone abbreviation.  */
+};
+#endif
+
+int
+mu_parse822_date_time (const char **p, const char *e, struct tm *tm,
+		       struct mu_timezone *tz)
+{
+  /* date-time = [ day "," ] date time */
+
+  const char *save = *p;
+  int rc = 0;
+
+  int wday = 0;
+
+  int mday = 0;
+  int mon = 0;
+  int year = 0;
+
+  int hour = 0;
+  int min = 0;
+  int sec = 0;
+
+  int tzoffset = 0;
+  const char *tz_name = 0;
+
+  if ((rc = mu_parse822_day (p, e, &wday)))
+    {
+      if (rc != EPARSE)
+	return rc;
+    }
+  else
+    {
+      /* If we got a day, we MUST have a ','. */
+      mu_parse822_skip_comments (p, e);
+
+      if ((rc = mu_parse822_special (p, e, ',')))
+	{
+	  *p = save;
+	  return rc;
+	}
+    }
+
+  if ((rc = mu_parse822_date (p, e, &mday, &mon, &year)))
+    {
+      *p = save;
+      return rc;
+    }
+  if ((rc = mu_parse822_time (p, e, &hour, &min, &sec, &tzoffset, &tz_name)))
+    {
+      *p = save;
+      return rc;
+    }
+
+  if (tm)
+    {
+      memset (tm, 0, sizeof (*tm));
+
+      tm->tm_wday = wday;
+
+      tm->tm_mday = mday;
+      tm->tm_mon = mon;
+      tm->tm_year = year;
+
+      tm->tm_hour = hour;
+      tm->tm_min = min;
+      tm->tm_sec = sec;
+
+#ifdef HAVE_STRUCT_TM_TM_ISDST
+      tm->tm_isdst = -1;	/* unknown whether it's dst or not */
+#endif
+#ifdef HAVE_STRUCT_TM_TM_GMTOFF
+      tm->tm_gmtoff = tzoffset;
+#endif
+#ifdef HAVE_STRUCT_TM_TM_ZONE
+      tm->tm_zone = (char*) tz_name;
+#endif
+    }
+
+  if (tz)
+    {
+      tz->utc_offset = tzoffset;
+      tz->tz_name = tz_name;
+    }
+
+  return EOK;
+}
+
+/***** From RFC 822, 3.2 Header Field Definitions *****/
+
+int
+mu_parse822_field_name (const char **p, const char *e, char **fieldname)
+{
+  /* field-name = 1*<any char, excluding ctls, space, and ":"> ":" */
+
+  const char *save = *p;
+
+  char *fn = NULL;
+
+  while (*p != e)
+    {
+      char c = **p;
+
+      if (!mu_parse822_is_char (c))
+	break;
+
+      if (mu_parse822_is_ctl (c))
+	break;
+      if (mu_parse822_is_space (c))
+	break;
+      if (c == ':')
+	break;
+
+      str_append_char (&fn, c);
+      *p += 1;
+    }
+  /* must be at least one char in the field name */
+  if (!fn)
+    {
+      *p = save;
+      return EPARSE;
+    }
+  mu_parse822_skip_comments (p, e);
+
+  if (!mu_parse822_special (p, e, ':'))
+    {
+      *p = save;
+      if (fn)
+	free (fn);
+      return EPARSE;
+    }
+
+  *fieldname = fn;
+
+  return EOK;
+}
+
+int
+mu_parse822_field_body (const char **p, const char *e, char **fieldbody)
+{
+  /* field-body = *text [CRLF lwsp-char field-body] */
+
+  /*const char *save = *p; */
+
+  char *fb = NULL;
+
+  for (;;)
+    {
+      const char *eol = *p;
+      while (eol != e)
+	{
+	  /*char c = *eol; */
+	  if (eol[0] == '\r' && (eol + 1) != e && eol[1] == '\n')
+	    break;
+	  ++eol;
+	}
+      str_append_range (&fb, *p, eol);
+      *p = eol;
+      if (eol == e)
+	break;			/* no more, so we're done */
+
+      /*assert(p[0] == '\r'); */
+      /*assert(p[1] == '\n'); */
+
+      *p += 2;
+
+      if (*p == e)
+	break;			/* no more, so we're done */
+
+      /* check if next line is a continuation line */
+      if (**p != ' ' && **p != '\t')
+	break;
+    }
+
+  *fieldbody = fb;
+
+  return EOK;
+}
+
+/***** RFC 822 Quoting Functions *****/
+
+int
+mu_parse822_quote_string (char **quoted, const char *raw)
+{
+  /* quoted-string = <"> *(qtext/quoted-pair) <">
+   *
+   * So double quote the string, and back quote anything that
+   * isn't qtext.
+   */
+
+  int rc = EOK;
+  const char *s;
+
+  if (!raw || !quoted || *quoted)
+    {
+      return EINVAL;
+    }
+
+  s = raw;
+
+  rc = str_append_char (quoted, '"');
+
+  while (!rc && *s)
+    {
+      if (!mu_parse822_is_q_text (*s))
+	{
+	  rc = str_append_char (quoted, '\\');
+	}
+
+      if (!rc)
+	{
+	  rc = str_append_char (quoted, *s);
+	}
+      ++s;
+    }
+
+  if (!rc)
+    {
+      rc = str_append_char (quoted, '"');
+    }
+
+  if (rc)
+    {
+      str_free (quoted);
+    }
+  return rc;
+}
+
+int
+mu_parse822_quote_local_part (char **quoted, const char *raw)
+{
+  /* local-part = word * ("." word)
+   * word = atom / quoted-string
+   *
+   * So, if any character isn't a "." or an atom character, we quote
+   * the whole thing as a string, for simplicity, otherwise just
+   * copy it.
+   */
+
+  const char *s = 0;
+
+  if (!raw || !quoted || *quoted)
+    {
+      return EINVAL;
+    }
+  s = raw;
+
+  while (*s)
+    {
+      if (*s != '.' && !mu_parse822_is_atom_char (*s))
+	{
+	  return mu_parse822_quote_string (quoted, raw);
+	}
+      ++s;
+    }
+
+  /* if we don't have to quote it, just copy it over */
+
+  return str_append (quoted, raw);
+}
+
diff --git a/libmailutils/amd.c b/libmailutils/amd.c
deleted file mode 100644
index a5dac3f..0000000
--- a/libmailutils/amd.c
+++ /dev/null
@@ -1,2033 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
-   2008, 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 */
-
-/* Mailutils Abstract Mail Directory Layer 
-   First draft by Sergey Poznyakoff.
-   Thanks Tang Yong Ping <yongping.tang@radixs.com> for initial
-   patch (although not used here).
-
-   This module provides basic support for "MH" and "Maildir" formats. */
-
-#ifdef HAVE_CONFIG_H
-# include <config.h>
-#endif
-
-#include <sys/types.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <time.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include <errno.h>
-#include <dirent.h>
-
-#ifdef WITH_PTHREAD
-# ifdef HAVE_PTHREAD_H
-#  ifndef _XOPEN_SOURCE
-#   define _XOPEN_SOURCE  500
-#  endif
-#  include <pthread.h>
-# endif
-#endif
-
-#include <string.h>
-#ifdef HAVE_STRINGS_H
-# include <strings.h>
-#endif
-
-#include <mailutils/cctype.h>
-#include <mailutils/cstr.h>
-#include <mailutils/attribute.h>
-#include <mailutils/body.h>
-#include <mailutils/debug.h>
-#include <mailutils/envelope.h>
-#include <mailutils/error.h>
-#include <mailutils/errno.h>
-#include <mailutils/header.h>
-#include <mailutils/locker.h>
-#include <mailutils/message.h>
-#include <mailutils/mutil.h>
-#include <mailutils/property.h>
-#include <mailutils/stream.h>
-#include <mailutils/url.h>
-#include <mailutils/observer.h>
-#include <mailutils/sys/stream.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);
-static int amd_close (mu_mailbox_t);
-static int amd_get_message (mu_mailbox_t, size_t, mu_message_t *);
-static int amd_quick_get_message (mu_mailbox_t mailbox, mu_message_qid_t qid,
-				  mu_message_t *pmsg);
-static int amd_append_message (mu_mailbox_t, mu_message_t);
-static int amd_messages_count (mu_mailbox_t, size_t *);
-static int amd_messages_recent (mu_mailbox_t, size_t *);
-static int amd_message_unseen (mu_mailbox_t, size_t *);
-static int amd_expunge (mu_mailbox_t);
-static int amd_sync (mu_mailbox_t);
-static int amd_uidnext (mu_mailbox_t mailbox, size_t *puidnext);
-static int amd_uidvalidity (mu_mailbox_t, unsigned long *);
-static int amd_scan (mu_mailbox_t, size_t, size_t *);
-static int amd_is_updated (mu_mailbox_t);
-static int amd_get_size (mu_mailbox_t, mu_off_t *);
-
-static int amd_body_size (mu_body_t body, size_t *psize);
-static int amd_body_lines (mu_body_t body, size_t *plines);
-
-static int amd_header_fill (void *data, char **pbuf, size_t *plen);
-
-static int amd_get_attr_flags (mu_attribute_t attr, int *pflags);
-static int amd_set_attr_flags (mu_attribute_t attr, int flags);
-static int amd_unset_attr_flags (mu_attribute_t attr, int flags);
-
-static void _amd_message_delete (struct _amd_data *amd,
-				 struct _amd_message *msg);
-static int amd_pool_open (struct _amd_message *mhm);
-static int amd_pool_open_count (struct _amd_data *amd);
-static void amd_pool_flush (struct _amd_data *amd);
-static struct _amd_message **amd_pool_lookup (struct _amd_message *mhm);
-
-static int amd_envelope_date (mu_envelope_t envelope, char *buf, size_t len,
-			      size_t *psize);
-static int amd_envelope_sender (mu_envelope_t envelope, char *buf, size_t len,
-			        size_t *psize);
-static int amd_remove_mbox (mu_mailbox_t mailbox);
-
-
-static int amd_body_stream_read (mu_stream_t str, char *buffer,
-				 size_t buflen,
-				 size_t *pnread);
-static int amd_body_stream_readdelim (mu_stream_t is,
-				      char *buffer, size_t buflen,
-				      int delim,
-				      size_t *pnread);
-static int amd_body_stream_size (mu_stream_t str, mu_off_t *psize);
-static int amd_body_stream_seek (mu_stream_t str, mu_off_t off, 
-				 mu_off_t *presult);
-
-struct _amd_body_stream
-{
-  struct _mu_stream stream;
-  mu_body_t body;
-  mu_off_t off;
-};
-
-
-/* Operations on message array */
-
-/* Perform binary search for message MSG on a segment of message array
-   of AMD between the indexes FIRST and LAST inclusively.
-   If found, return 0 and store index of the located entry in the
-   variable PRET. Otherwise, return 1 and place into PRET index of
-   the nearest array element that is less than MSG (in the sense of
-   amd->msg_cmp)
-   Indexes are zero-based. */
-   
-static int
-amd_msg_bsearch (struct _amd_data *amd, mu_off_t first, mu_off_t last,
-		 struct _amd_message *msg,
-		 mu_off_t *pret)
-{
-  mu_off_t mid;
-  int rc;
-
-  if (last < first)
-    return 1;
-  
-  mid = (first + last) / 2;
-  rc = amd->msg_cmp (amd->msg_array[mid], msg);
-  if (rc > 0)
-    return amd_msg_bsearch (amd, first, mid-1, msg, pret);
-  *pret = mid;
-  if (rc < 0)
-    return amd_msg_bsearch (amd, mid+1, last, msg, pret);
-  /* else */
-  return 0;
-}
-
-/* Search for message MSG in the message array of AMD.
-   If found, return 0 and store index of the located entry in the
-   variable PRET. Otherwise, return 1 and place into PRET index of
-   the array element that is less than MSG (in the sense of
-   amd->msg_cmp)
-   Index returned in PRET is 1-based, so *PRET == 0 means that MSG
-   is less than the very first element of the message array.
-
-   In other words, when amd_msg_lookup() returns 1, the value in *PRET
-   can be regarded as a 0-based index of the array slot where MSG can
-   be inserted */
-
-int
-amd_msg_lookup (struct _amd_data *amd, struct _amd_message *msg,
-		 size_t *pret)
-{
-  int rc;
-  mu_off_t i;
-  
-  if (amd->msg_count == 0)
-    {
-      *pret = 0;
-      return 1;
-    }
-  
-  rc = amd->msg_cmp (msg, amd->msg_array[0]);
-  if (rc < 0)
-    {
-      *pret = 0;
-      return 1;
-    }
-  else if (rc == 0)
-    {
-      *pret = 1;
-      return 0;
-    }
-  
-  rc = amd->msg_cmp (msg, amd->msg_array[amd->msg_count - 1]);
-  if (rc > 0)
-    {
-      *pret = amd->msg_count;
-      return 1;
-    }
-  else if (rc == 0)
-    {
-      *pret = amd->msg_count;
-      return 0;
-    }
-  
-  rc = amd_msg_bsearch (amd, 0, amd->msg_count - 1, msg, &i);
-  *pret = i + 1;
-  return rc;
-}
-
-#define AMD_MSG_INC 64
-
-/* Prepare the message array for insertion of a new message
-   at position INDEX (zero based), by moving its contents
-   one slot to the right. If necessary, expand the array by
-   AMD_MSG_INC */
-int
-amd_array_expand (struct _amd_data *amd, size_t index)
-{
-  if (amd->msg_count == amd->msg_max)
-    {
-      struct _amd_message **p;
-      
-      amd->msg_max += AMD_MSG_INC; /* FIXME: configurable? */
-      p = realloc (amd->msg_array, amd->msg_max * amd->msg_size);
-      if (!p)
-	{
-	  amd->msg_max -= AMD_MSG_INC;
-	  return ENOMEM;
-	}
-      amd->msg_array = p;
-    }
-  memmove (&amd->msg_array[index+1], &amd->msg_array[index],
-	   (amd->msg_count-index) * amd->msg_size);
-  amd->msg_count++;
-  return 0;
-}
-
-/* Shrink the message array by removing element at INDEX-1 and
-   shifting left by one position all the elements on the right of
-   it. */
-int
-amd_array_shrink (struct _amd_data *amd, size_t index)
-{
-  memmove (&amd->msg_array[index-1], &amd->msg_array[index],
-	   (amd->msg_count-index) * amd->msg_size);
-  amd->msg_count--;
-  return 0;
-}
-
-
-int
-amd_init_mailbox (mu_mailbox_t mailbox, size_t amd_size,
-		  struct _amd_data **pamd) 
-{
-  int status;
-  struct _amd_data *amd;
-
-  if (mailbox == NULL)
-    return MU_ERR_MBX_NULL;
-  if (amd_size < sizeof (*amd))
-    return EINVAL;
-
-  amd = mailbox->data = calloc (1, amd_size);
-  if (mailbox->data == NULL)
-    return ENOMEM;
-
-  /* Back pointer.  */
-  amd->mailbox = mailbox;
-
-  status = mu_url_aget_path (mailbox->url, &amd->name);
-  if (status)
-    {
-      free (amd);
-      mailbox->data = NULL;
-      return status;
-    }
-
-  /* Overloading the defaults.  */
-  mailbox->_destroy = amd_destroy;
-
-  mailbox->_open = amd_open;
-  mailbox->_close = amd_close;
-
-  /* Overloading of the entire mailbox object methods.  */
-  mailbox->_get_message = amd_get_message;
-  mailbox->_quick_get_message = amd_quick_get_message;
-  mailbox->_append_message = amd_append_message;
-  mailbox->_messages_count = amd_messages_count;
-  mailbox->_messages_recent = amd_messages_recent;
-  mailbox->_message_unseen = amd_message_unseen;
-  mailbox->_expunge = amd_expunge;
-  mailbox->_sync = amd_sync;
-  mailbox->_uidvalidity = amd_uidvalidity;
-  mailbox->_uidnext = amd_uidnext;
-
-  mailbox->_scan = amd_scan;
-  mailbox->_is_updated = amd_is_updated;
-
-  mailbox->_get_size = amd_get_size;
-  mailbox->_remove = amd_remove_mbox;
-  
-  MU_DEBUG1 (mailbox->debug, MU_DEBUG_TRACE1, "amd_init(%s)\n", amd->name);
-  *pamd = amd;
-  return 0;
-}
-
-static void
-amd_destroy (mu_mailbox_t mailbox)
-{
-  struct _amd_data *amd = mailbox->data;
-  size_t i;
-  
-  if (!amd)
-    return;
-
-  amd_pool_flush (amd);
-  mu_monitor_wrlock (mailbox->monitor);
-  for (i = 0; i < amd->msg_count; i++)
-    {
-      mu_message_destroy (&amd->msg_array[i]->message, amd->msg_array[i]);
-      free (amd->msg_array[i]);
-    }
-  free (amd->msg_array);
-	
-  if (amd->name)
-    free (amd->name);
-
-  free (amd);
-  mailbox->data = NULL;
-  mu_monitor_unlock (mailbox->monitor);
-}
-
-static int
-amd_open (mu_mailbox_t mailbox, int flags)
-{
-  struct _amd_data *amd = mailbox->data;
-  struct stat st;
-
-  mailbox->flags = flags;
-  if (stat (amd->name, &st) < 0)
-    {
-      if ((flags & MU_STREAM_CREAT) && errno == ENOENT)
-	{
-	  int rc;
-	  int perms = mu_stream_flags_to_mode (flags, 1);
-	  if (mkdir (amd->name, S_IRUSR|S_IWUSR|S_IXUSR|perms))
-	    return errno;
-	  if (stat (amd->name, &st) < 0)
-	    return errno;
-	  if (amd->create && (rc = amd->create (amd, flags)))
-	    return rc;
-	}
-      else
-	return errno;
-    }
-  
-  if (!S_ISDIR (st.st_mode))
-    return EINVAL;
-
-  if (mailbox->locker == NULL)
-    mu_locker_create (&mailbox->locker, "/dev/null", 0);
-  
-  return 0;
-}
-
-static int
-amd_close (mu_mailbox_t mailbox)
-{
-  struct _amd_data *amd;
-  int i;
-    
-  if (!mailbox)
-    return MU_ERR_MBX_NULL;
-
-  amd = mailbox->data;
-  
-  /* Destroy all cached data */
-  amd_pool_flush (amd);
-  mu_monitor_wrlock (mailbox->monitor);
-  for (i = 0; i < amd->msg_count; i++)
-    {
-      mu_message_destroy (&amd->msg_array[i]->message, amd->msg_array[i]);
-      free (amd->msg_array[i]);
-    }
-  free (amd->msg_array);
-  amd->msg_array = NULL;
-
-  amd->msg_count = 0; /* number of messages in the list */
-  amd->msg_max = 0;   /* maximum message buffer capacity */
-
-  amd->uidvalidity = 0;
-  mu_monitor_unlock (mailbox->monitor);
-  
-  return 0;
-}
-
-static int
-amd_message_qid (mu_message_t msg, mu_message_qid_t *pqid)
-{
-  struct _amd_message *mhm = mu_message_get_owner (msg);
-  
-  return mhm->amd->cur_msg_file_name (mhm, pqid);
-}
-
-struct _amd_message *
-_amd_get_message (struct _amd_data *amd, size_t msgno)
-{
-  msgno--;
-  if (msgno >= amd->msg_count)
-    return NULL;
-  return amd->msg_array[msgno];
-}
-
-static int
-_amd_attach_message (mu_mailbox_t mailbox, struct _amd_message *mhm,
-		     mu_message_t *pmsg)
-{
-  int status;
-  mu_message_t msg;
-
-  /* Check if we already have it.  */
-  if (mhm->message)
-    {
-      if (pmsg)
-	*pmsg = mhm->message;
-      return 0;
-    }
-
-  /* Get an empty message struct.  */
-  status = mu_message_create (&msg, mhm);
-  if (status != 0)
-    return status;
-
-  /* Set the header.  */
-  {
-    mu_header_t header = NULL;
-    status = mu_header_create (&header, NULL, 0);
-    if (status != 0)
-      {
-	mu_message_destroy (&msg, mhm);
-	return status;
-      }
-    mu_header_set_fill (header, amd_header_fill, msg);
-    /*FIXME:
-    mu_header_set_get_fvalue (header, amd_header_get_fvalue, msg);
-    */
-    mu_message_set_header (msg, header, mhm);
-  }
-
-  /* Set the attribute.  */
-  {
-    mu_attribute_t attribute;
-    status = mu_attribute_create (&attribute, msg);
-    if (status != 0)
-      {
-	mu_message_destroy (&msg, mhm);
-	return status;
-      }
-    mu_attribute_set_get_flags (attribute, amd_get_attr_flags, msg);
-    mu_attribute_set_set_flags (attribute, amd_set_attr_flags, msg);
-    mu_attribute_set_unset_flags (attribute, amd_unset_attr_flags, msg);
-    mu_message_set_attribute (msg, attribute, mhm);
-  }
-
-  /* Prepare the body.  */
-  {
-    mu_body_t body = NULL;
-    struct _amd_body_stream *str;
-    
-    if ((status = mu_body_create (&body, msg)) != 0)
-      return status;
-
-    str = (struct _amd_body_stream *)
-              _mu_stream_create (sizeof (*str),
-				 mailbox->flags | MU_STREAM_SEEK | 
-				 _MU_STR_OPEN);
-    if (!str)
-      {
-	mu_body_destroy (&body, msg);
-	mu_message_destroy (&msg, mhm);
-	return ENOMEM;
-      }
-    str->stream.read = amd_body_stream_read;
-    str->stream.readdelim = amd_body_stream_readdelim;
-    str->stream.size = amd_body_stream_size;
-    str->stream.seek = amd_body_stream_seek;
-    mu_body_set_stream (body, (mu_stream_t) str, msg);
-    mu_body_clear_modified (body);
-    mu_body_set_size (body, amd_body_size, msg);
-    mu_body_set_lines (body, amd_body_lines, msg);
-    mu_message_set_body (msg, body, mhm);
-    str->body = body;
-  }
-
-  /* Set the envelope.  */
-  {
-    mu_envelope_t envelope = NULL;
-    status = mu_envelope_create (&envelope, msg);
-    if (status != 0)
-      {
-	mu_message_destroy (&msg, mhm);
-	return status;
-      }
-    mu_envelope_set_sender (envelope, amd_envelope_sender, msg);
-    mu_envelope_set_date (envelope, amd_envelope_date, msg);
-    mu_message_set_envelope (msg, envelope, mhm);
-  }
-
-  /* Set the UID.  */
-  if (mhm->amd->message_uid)
-    mu_message_set_uid (msg, mhm->amd->message_uid, mhm);
-  mu_message_set_qid (msg, amd_message_qid, mhm);
-  
-  /* Attach the message to the mailbox mbox data.  */
-  mhm->message = msg;
-  mu_message_set_mailbox (msg, mailbox, mhm);
-
-  /* Some of mu_message_set_ functions above mark message as modified.
-     Undo it now.
-
-     FIXME: Marking message as modified is not always appropriate. Find
-     a better way. */
-     
-  mu_message_clear_modified (msg);
-
-  if (pmsg)
-    *pmsg = msg;
-
-  return 0;
-}
-
-static int
-amd_get_message (mu_mailbox_t mailbox, size_t msgno, mu_message_t *pmsg)
-{
-  int status;
-  struct _amd_data *amd = mailbox->data;
-  struct _amd_message *mhm;
-
-  /* Sanity checks.  */
-  if (pmsg == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-  if (amd == NULL)
-    return EINVAL;
-
-  /* If we did not start a scanning yet do it now.  */
-  if (amd->msg_count == 0)
-    {
-      status = amd->scan0 (mailbox, 1, NULL, 0);
-      if (status != 0)
-	return status;
-    }
-
-  if ((mhm = _amd_get_message (amd, msgno)) == NULL)
-    return EINVAL;
-  return _amd_attach_message (mailbox, mhm, pmsg);
-}
-
-static int
-amd_quick_get_message (mu_mailbox_t mailbox, mu_message_qid_t qid,
-		       mu_message_t *pmsg)
-{
-  int status;
-  struct _amd_data *amd = mailbox->data;
-  if (amd->msg_count)
-    {
-      mu_message_qid_t vqid;
-      mu_message_t msg = amd->msg_array[0]->message;
-      status = mu_message_get_qid (msg, &vqid);
-      if (status)
-	return status;
-      status = strcmp (qid, vqid);
-      free (vqid);
-      if (status)
-	return MU_ERR_EXISTS;
-      *pmsg = msg;
-    }
-  else if (amd->qfetch)
-    {
-      status = amd->qfetch (amd, qid);
-      if (status)
-	return status;
-      return _amd_attach_message (mailbox, amd->msg_array[0], pmsg);
-    }
-  
-  return ENOSYS;
-}
-
-static FILE *
-_amd_tempfile(struct _amd_data *amd, char **namep)
-{
-  int fd = mu_tempfile (amd->name, namep);
-  if (fd == -1)
-      return NULL;
-  return fdopen (fd, "w");
-}
-
-static int
-_amd_delim (char *str)
-{
-  if (str[0] == '-')
-    {
-      for (; *str == '-'; str++)
-	;
-      for (; *str == ' ' || *str == '\t'; str++)
-	;
-    }
-  return str[0] == '\n';
-}
-
-static int
-_amd_message_save (struct _amd_data *amd, struct _amd_message *mhm,
-		   int expunge)
-{
-  mu_stream_t stream = NULL;
-  char *name = NULL, *buf = NULL, *msg_name, *old_name;
-  size_t n;
-  size_t bsize;
-  size_t nlines, nbytes;
-  size_t new_body_start, new_header_lines;
-  FILE *fp;
-  mu_message_t msg = mhm->message;
-  mu_header_t hdr;
-  int status;
-  mu_attribute_t attr;
-  mu_body_t body;
-  const char *sbuf;
-  mu_envelope_t env = NULL;
-  char statbuf[MU_STATUS_BUF_SIZE];
-
-  status = mu_message_size (msg, &bsize);
-  if (status)
-    return status;
-
-  status = amd->new_msg_file_name (mhm, mhm->attr_flags, expunge, &msg_name);
-  if (status)
-    return status;
-  if (!msg_name)
-    {
-      /* Unlink the original file */
-      char *old_name;
-      status = amd->cur_msg_file_name (mhm, &old_name);
-      free (msg_name);
-      if (status == 0 && unlink (old_name))
-	status = errno;
-      free (old_name);
-      return status;
-    }      
-    
-  fp = _amd_tempfile (mhm->amd, &name);
-  if (!fp)
-    {
-      free (msg_name);
-      return errno;
-    }
-
-  /* Try to allocate large buffer */
-  for (; bsize > 1; bsize /= 2)
-    if ((buf = malloc (bsize)))
-      break;
-
-  if (!bsize)
-    {
-      unlink (name);
-      free (name);
-      free (msg_name);
-      return ENOMEM;
-    }
-
-  /* Copy flags */
-  mu_message_get_header (msg, &hdr);
-  mu_header_get_streamref (hdr, &stream);
-  status = mu_stream_seek (stream, 0, MU_SEEK_SET, NULL);
-  if (status)
-    {
-      /* FIXME: Provide a common exit point for all error
-	 cases */
-      unlink (name);
-      free (name);
-      free (msg_name);
-      mu_stream_destroy (&stream);
-      return status;
-    }
-      
-  nlines = nbytes = 0;
-  while ((status = mu_stream_readline (stream, buf, bsize, &n)) == 0
-	 && n != 0)
-    {
-      if (_amd_delim (buf))
-	break;
-
-      if (!(mu_c_strncasecmp (buf, "status:", 7) == 0
-	    || mu_c_strncasecmp (buf, "x-imapbase:", 11) == 0
-	    || mu_c_strncasecmp (buf, "x-uid:", 6) == 0
-	    || mu_c_strncasecmp (buf, 
-                MU_HEADER_ENV_DATE ":", sizeof (MU_HEADER_ENV_DATE)) == 0
-	    || mu_c_strncasecmp (buf, 
-                MU_HEADER_ENV_SENDER ":", sizeof (MU_HEADER_ENV_SENDER)) == 0))
-	{
-	  nlines++;
-	  nbytes += fprintf (fp, "%s", buf);
-	}
-    }
-  mu_stream_destroy (&stream);
-  
-  /* Add imapbase */
-  if (!(amd->mailbox->flags & MU_STREAM_APPEND)
-      && amd->next_uid
-      && (!amd->msg_array || (amd->msg_array[0] == mhm))) /*FIXME*/
-    {
-      nbytes += fprintf (fp, "X-IMAPbase: %lu %u\n",
-			 (unsigned long) amd->uidvalidity,
-			 (unsigned) amd->next_uid (amd));
-      nlines++;
-    }
-  
-  mu_message_get_envelope (msg, &env);
-  if (mu_envelope_sget_date (env, &sbuf) == 0)
-    {
-      /* NOTE: buffer might be terminated with \n */
-      while (*sbuf && mu_isspace (*sbuf))
-	sbuf++;
-      nbytes += fprintf (fp, "%s: %s", MU_HEADER_ENV_DATE, sbuf);
-
-      if (*sbuf && sbuf[strlen (sbuf) - 1] != '\n')
-	nbytes += fprintf (fp, "\n");
-      
-      nlines++;
-    }
-	  
-  if (mu_envelope_sget_sender (env, &sbuf) == 0)
-    {
-      fprintf (fp, "%s: %s\n", MU_HEADER_ENV_SENDER, sbuf);
-      nlines++;
-    }
-  
-  /* Add status */
-  mu_message_get_attribute (msg, &attr);
-  mu_attribute_to_string (attr, statbuf, sizeof (statbuf), &n);
-  if (n)
-    {
-      nbytes += fprintf (fp, "Status: %s\n", statbuf);
-      nlines++;
-    }
-  nbytes += fprintf (fp, "\n");
-  nlines++;
-  
-  new_header_lines = nlines;
-  new_body_start = nbytes;
-
-  /* Copy message body */
-
-  mu_message_get_body (msg, &body);
-  mu_body_get_streamref (body, &stream);
-  status = mu_stream_seek (stream, 0, MU_SEEK_SET, NULL);
-  if (status)
-    {
-      unlink (name);
-      free (name);
-      free (msg_name);
-      mu_stream_destroy (&stream);
-      return status;
-    }
-    
-  nlines = 0;
-  while (mu_stream_read (stream, buf, bsize, &n) == 0 && n != 0)
-    {
-      char *p;
-      for (p = buf; p < buf + n; p++)
-	if (*p == '\n')
-	  nlines++;
-      fwrite (buf, 1, n, fp);
-      nbytes += n;
-    }
-  mu_stream_destroy (&stream);
-  
-  mhm->header_lines = new_header_lines;
-  mhm->body_start = new_body_start;
-  mhm->body_lines = nlines;
-  mhm->body_end = nbytes;
-
-  free (buf);
-  fclose (fp);
-
-  status = amd->cur_msg_file_name (mhm, &old_name);
-  if (status == 0)
-    {
-      if (rename (name, msg_name))
-	status = errno;
-      else
-	{
-	  mode_t perms;
-	  
-	  perms = mu_stream_flags_to_mode (amd->mailbox->flags, 0);
-	  if (perms != 0)
-	    {
-	      /* It is documented that the mailbox permissions are
-		 affected by the current umask, so take it into account
-		 here.
-		 FIXME: I'm still not sure we should honor umask, though.
-		 --gray
-	      */
-	      mode_t mask = umask (0);
-	      chmod (msg_name, (0600 | perms) & ~mask);
-	      umask (mask);
-	    }
-	  if (strcmp (old_name, msg_name))
-	    /* Unlink original message */
-	    unlink (old_name);
-	}
-      free (old_name);
-      
-      mhm->orig_flags = mhm->attr_flags;
-    }
-  free (msg_name);
-  free (name);
-
-  return status;
-}
-
-static int
-amd_append_message (mu_mailbox_t mailbox, mu_message_t msg)
-{
-  int status;
-  struct _amd_data *amd = mailbox->data;
-  struct _amd_message *mhm;
-
-  if (!mailbox)
-    return MU_ERR_MBX_NULL;
-  if (!msg)
-    return EINVAL;
-
-  mhm = calloc (1, amd->msg_size);
-  if (!mhm)
-    return ENOMEM;
-    
-  /* If we did not start a scanning yet do it now.  */
-  if (amd->msg_count == 0)
-    {
-      status = amd->scan0 (mailbox, 1, NULL, 0);
-      if (status != 0)
-	{
-	  free (mhm);
-	  return status;
-	}
-    }
-
-  amd->has_new_msg = 1;
-  
-  mhm->amd = amd;
-  if (amd->msg_init_delivery)
-    {
-      status = amd->msg_init_delivery (amd, mhm);
-      if (status)
-	{
-	  free (mhm);
-	  return status;
-	}
-    }
-  
-  mhm->message = msg;
-  status = _amd_message_save (amd, mhm, 0);
-  if (status)
-    {
-      free (mhm);
-      return status;
-    }
-
-  mhm->message = NULL;
-  /* Insert and re-scan the message */
-  status = _amd_message_insert (amd, mhm);
-  if (status)
-    {
-      free (mhm);
-      return status;
-    }
-
-  if (amd->msg_finish_delivery)
-    status = amd->msg_finish_delivery (amd, mhm, msg);
-  
-  if (status == 0 && mailbox->observable)
-    {
-      char *qid;
-      if (amd->cur_msg_file_name (mhm, &qid) == 0)
-	{
-	  mu_observable_notify (mailbox->observable, MU_EVT_MESSAGE_APPEND,
-				qid);
-	  free (qid);
-	}
-    }
-  
-  return status;
-}
-
-static int
-amd_messages_count (mu_mailbox_t mailbox, size_t *pcount)
-{
-  struct _amd_data *amd = mailbox->data;
-
-  if (amd == NULL)
-    return EINVAL;
-
-  if (!amd_is_updated (mailbox))
-    return amd->scan0 (mailbox,  amd->msg_count, pcount, 0);
-
-  if (pcount)
-    *pcount = amd->msg_count;
-
-  return 0;
-}
-
-/* A "recent" message is the one not marked with MU_ATTRIBUTE_SEEN
-   ('O' in the Status header), i.e. a message that is first seen
-   by the current session (see attributes.h) */
-static int
-amd_messages_recent (mu_mailbox_t mailbox, size_t *pcount)
-{
-  struct _amd_data *amd = mailbox->data;
-  size_t count, i;
-
-  /* If we did not start a scanning yet do it now.  */
-  if (amd->msg_count == 0)
-    {
-      int status = amd->scan0 (mailbox, 1, NULL, 0);
-      if (status != 0)
-	return status;
-    }
-  count = 0;
-  for (i = 0; i < amd->msg_count; i++)
-    {
-      if (MU_ATTRIBUTE_IS_UNSEEN(amd->msg_array[i]->attr_flags))
-	count++;
-    }
-  *pcount = count;
-  return 0;
-}
-
-/* An "unseen" message is the one that has not been read yet */
-static int
-amd_message_unseen (mu_mailbox_t mailbox, size_t *pmsgno)
-{
-  struct _amd_data *amd = mailbox->data;
-  size_t i;
-
-  /* If we did not start a scanning yet do it now.  */
-  if (amd->msg_count == 0)
-    {
-      int status = amd->scan0 (mailbox, 1, NULL, 0);
-      if (status != 0)
-	return status;
-    }
-
-  for (i = 0; i < amd->msg_count; i++)
-    {
-      if (MU_ATTRIBUTE_IS_UNREAD(amd->msg_array[0]->attr_flags))
-	{
-	  *pmsgno = i + 1;
-	  break;
-	}
-    }
-  return 0;
-}
-
-static char *
-make_size_file_name (struct _amd_data *amd)
-{
-  size_t size = strlen (amd->name) + 1 + sizeof (MU_AMD_SIZE_FILE_NAME);
-  char *name = malloc (size);
-  if (name)
-    {
-      strcpy (name, amd->name);
-      strcat (name, "/");
-      strcat (name, MU_AMD_SIZE_FILE_NAME);
-    }
-  return name;
-}
-
-static int
-read_size_file (struct _amd_data *amd, mu_off_t *psize)
-{
-  FILE *fp;
-  int rc;
-  char *name = make_size_file_name (amd);
-  if (!name)
-    return 1;
-  fp = fopen (name, "r");
-  if (fp)
-    {
-      unsigned long size;
-      if (fscanf (fp, "%lu", &size) == 1)
-	{
-	  *psize = size;
-	  rc = 0;
-	}
-      else
-	rc = 1;
-      fclose (fp);
-    }
-  else
-    rc = 1;
-  free (name);
-  return rc;
-}
-
-static int
-write_size_file (struct _amd_data *amd, mu_off_t size)
-{
-  FILE *fp;
-  int rc;
-  char *name = make_size_file_name (amd);
-  if (!name)
-    return 1;
-  fp = fopen (name, "w");
-  if (fp)
-    {
-      fprintf (fp, "%lu", (unsigned long) size);
-      fclose (fp);
-      rc = 0;
-    }
-  else
-    rc = 1;
-  free (name);
-  return rc;
-}
-      
-static int
-compute_mailbox_size (struct _amd_data *amd, const char *name, mu_off_t *psize)
-{
-  DIR *dir;
-  struct dirent *entry;
-  char *buf;
-  size_t bufsize;
-  size_t dirlen;
-  size_t flen;
-  int status = 0;
-  struct stat sb;
-
-  dir = opendir (name);
-  if (!dir)
-    return errno;
-
-  dirlen = strlen (name);
-  bufsize = dirlen + 32;
-  buf = malloc (bufsize);
-  if (!buf)
-    {
-      closedir (dir);
-      return ENOMEM;
-    }
-  
-  strcpy (buf, name);
-  if (buf[dirlen-1] != '/')
-    buf[++dirlen - 1] = '/';
-	  
-  while ((entry = readdir (dir)))
-    {
-      switch (entry->d_name[0])
-	{
-	case '.':
-	  break;
-
-	default:
-	  flen = strlen (entry->d_name);
-	  if (dirlen + flen + 1 > bufsize)
-	    {
-	      bufsize = dirlen + flen + 1;
-	      buf = realloc (buf, bufsize);
-	      if (!buf)
-		{
-		  status = ENOMEM;
-		  break;
-		}
-	    }
-	  strcpy (buf + dirlen, entry->d_name);
-	  if (stat (buf, &sb) == 0)
-	    {
-	      if (S_ISREG (sb.st_mode))
-		*psize += sb.st_size;
-	      else if (S_ISDIR (sb.st_mode))
-		compute_mailbox_size (amd, buf, psize);
-	    }
-	  /* FIXME: else? */
-	  break;
-	}
-    }
-
-  free (buf);
-  
-  closedir (dir);
-  return 0;
-}
-
-static int
-amd_remove_mbox (mu_mailbox_t mailbox)
-{
-  int rc;
-  struct _amd_data *amd = mailbox->data;
-  
-  if (!amd->remove)
-    return ENOSYS;
-  rc = amd->remove (amd);
-  if (rc == 0)
-    {
-      char *name = make_size_file_name (amd);
-      if (!name)
-	return ENOMEM;
-      if (unlink (name) && errno != ENOENT)
-	rc = errno;
-      free (name);
-    }
-
-  if (rc == 0)
-    {
-      if (rmdir (amd->name) && errno != ENOENT)
-	{
-	  rc = errno;
-	  /* POSIX.1-2001 allows EEXIST to be returned if the directory
-	     contained entries other than . and .. */
-	  if (rc == EEXIST)
-	    rc = ENOTEMPTY;
-	}
-    }
-  
-  return rc;
-}
-
-static int
-amd_expunge (mu_mailbox_t mailbox)
-{
-  struct _amd_data *amd = mailbox->data;
-  struct _amd_message *mhm;
-  size_t i;
-  int updated = amd->has_new_msg;
-  
-  if (amd == NULL)
-    return EINVAL;
-
-  if (amd->msg_count == 0)
-    return 0;
-
-  /* Find the first dirty(modified) message.  */
-  for (i = 0; i < amd->msg_count; i++)
-    {
-      mhm = amd->msg_array[i];
-      if ((mhm->attr_flags & MU_ATTRIBUTE_MODIFIED) ||
-	  (mhm->attr_flags & MU_ATTRIBUTE_DELETED) ||
-	  (mhm->message && mu_message_is_modified (mhm->message)))
-	break;
-    }
-
-  while (i < amd->msg_count)
-    {
-      mhm = amd->msg_array[i];
-      
-      if (mhm->attr_flags & MU_ATTRIBUTE_DELETED)
-	{
-	  int rc;
-	  char *old_name;
-	  char *new_name;
-
-	  rc = amd->cur_msg_file_name (mhm, &old_name);
-	  if (rc)
-	    return rc;
-	  rc = amd->new_msg_file_name (mhm, mhm->attr_flags, 1,
-				       &new_name);
-	  if (rc)
-	    {
-	      free (old_name);
-	      return rc;
-	    }
-
-	  if (new_name)
-	    {
-	      /* FIXME: It may be a good idea to have a capability flag
-		 in struct _amd_data indicating that no actual removal
-		 is needed (e.g. for traditional MH). It will allow to
-		 bypass lots of no-op code here. */
-	      if (strcmp (old_name, new_name))
-		/* Rename original message */
-		rename (old_name, new_name);
-	    }
-	  else
-	    /* Unlink original file */
-	    unlink (old_name);
-	  
-	  free (old_name);
-	  free (new_name);
-
-	  _amd_message_delete (amd, mhm);
-	  updated = 1;
-	  /* Do not increase i! */
-	}
-      else
-	{
-	  if ((mhm->attr_flags & MU_ATTRIBUTE_MODIFIED)
-	      || (mhm->message && mu_message_is_modified (mhm->message)))
-	    {
-	      _amd_attach_message (mailbox, mhm, NULL);
-	      _amd_message_save (amd, mhm, 1);
-	      updated = 1;
-	    }
-	  i++; /* Move to the next message */
-	}
-    }
-
-  if (updated && !amd->mailbox_size)
-    {
-      mu_off_t size = 0;
-      int rc = compute_mailbox_size (amd, amd->name, &size);
-      if (rc == 0)
-	write_size_file (amd, size);
-    }
-  return 0;
-}
-
-static int
-amd_sync (mu_mailbox_t mailbox)
-{
-  struct _amd_data *amd = mailbox->data;
-  struct _amd_message *mhm;
-  size_t i;
-  int updated = amd->has_new_msg;
-  
-  if (amd == NULL)
-    return EINVAL;
-
-  if (amd->msg_count == 0)
-    return 0;
-
-  /* Find the first dirty(modified) message.  */
-  for (i = 0; i < amd->msg_count; i++)
-    {
-      mhm = amd->msg_array[i];
-      if ((mhm->attr_flags & MU_ATTRIBUTE_MODIFIED)
-	  || (mhm->message && mu_message_is_modified (mhm->message)))
-	break;
-    }
-
-  for ( ; i < amd->msg_count; i++)
-    {
-      mhm = amd->msg_array[i];
-
-      if ((mhm->attr_flags & MU_ATTRIBUTE_MODIFIED)
-	  || (mhm->message && mu_message_is_modified (mhm->message)))
-	{
-	  _amd_attach_message (mailbox, mhm, NULL);
-	  _amd_message_save (amd, mhm, 0);
-	  updated = 1;
-	}
-    }
-
-  if (updated && !amd->mailbox_size)
-    {
-      mu_off_t size = 0;
-      int rc = compute_mailbox_size (amd, amd->name, &size);
-      if (rc == 0)
-	write_size_file (amd, size);
-    }
-
-  return 0;
-}
-
-static int
-amd_uidvalidity (mu_mailbox_t mailbox, unsigned long *puidvalidity)
-{
-  struct _amd_data *amd = mailbox->data;
-  int status = amd_messages_count (mailbox, NULL);
-  if (status != 0)
-    return status;
-  /* If we did not start a scanning yet do it now.  */
-  if (amd->msg_count == 0)
-    {
-      status = amd->scan0 (mailbox, 1, NULL, 0);
-      if (status != 0)
-	return status;
-    }
-  if (puidvalidity)
-    *puidvalidity = amd->uidvalidity;
-  return 0;
-}
-
-static int
-amd_uidnext (mu_mailbox_t mailbox, size_t *puidnext)
-{
-  struct _amd_data *amd = mailbox->data;
-  int status;
-  
-  if (!amd->next_uid)
-    return ENOSYS;
-  status = mu_mailbox_messages_count (mailbox, NULL);
-  if (status != 0)
-    return status;
-  /* If we did not start a scanning yet do it now.  */
-  if (amd->msg_count == 0)
-    {
-      status = amd->scan0 (mailbox, 1, NULL, 0);
-      if (status != 0)
-	return status;
-    }
-   if (puidnext)
-     *puidnext = amd->next_uid (amd);
-  return 0;
-}
-
-/* FIXME: effectively the same as mbox_cleanup */
-void
-amd_cleanup (void *arg)
-{
-  mu_mailbox_t mailbox = arg;
-  mu_monitor_unlock (mailbox->monitor);
-  mu_locker_unlock (mailbox->locker);
-}
-
-/* Insert message msg into the message list on the appropriate position */
-int
-_amd_message_insert (struct _amd_data *amd, struct _amd_message *msg)
-{
-  size_t index;
-
-  if (amd_msg_lookup (amd, msg, &index))
-    {
-      /* Not found. Index is the index of the array cell where msg
-	 must be placed */
-      int rc = amd_array_expand (amd, index);
-      if (rc)
-	return rc;
-      amd->msg_array[index] = msg;
-      msg->amd = amd;
-    }
-  else
-    {
-      /*FIXME: Found? Shouldn't happen */
-      return EEXIST;
-    }
-  return 0;
-}
-
-static void
-_amd_message_delete (struct _amd_data *amd, struct _amd_message *msg)
-{
-  size_t index;
-  struct _amd_message **pp;
-
-  if (amd_msg_lookup (amd, msg, &index))
-    {
-      /* FIXME: Not found? */
-      return;
-    }
-
-  msg = _amd_get_message (amd, index);
-
-  pp = amd_pool_lookup (msg);
-  if (pp)
-    *pp = NULL;
-  
-  mu_message_destroy (&msg->message, msg);
-  if (amd->msg_free)
-    amd->msg_free (msg);
-  free (msg);
-  amd_array_shrink (amd, index);
-}
-
-/* Scan given message and fill amd_message_t fields.
-   NOTE: the function assumes mhm->stream != NULL. */
-static int
-amd_scan_message (struct _amd_message *mhm)
-{
-  mu_stream_t stream = mhm->stream;
-  char buf[1024];
-  size_t off;
-  size_t n;
-  int status;
-  int in_header = 1;
-  size_t hlines = 0;
-  size_t blines = 0;
-  size_t body_start = 0;
-  struct stat st;
-  char *msg_name;
-
-  /* Check if the message was modified after the last scan */
-  status = mhm->amd->cur_msg_file_name (mhm, &msg_name);
-  if (status)
-    return status;
-
-  if (stat (msg_name, &st) == 0 && st.st_mtime == mhm->mtime)
-    {
-      /* Nothing to do */
-      free (msg_name);
-      return 0;
-    }
-  free (msg_name);
-
-  off = 0;
-  status = mu_stream_seek (stream, 0, MU_SEEK_SET, NULL);
-  if (status == 0)
-    while ((status = mu_stream_readline (stream, buf, sizeof (buf), &n)) == 0
-	   && n != 0)
-      {
-	if (in_header)
-	  {
-	    if (buf[0] == '\n')
-	      {
-		in_header = 0;
-		body_start = off + 1;
-	      }
-	    if (buf[n - 1] == '\n')
-	      hlines++;
-	    
-	    /* Process particular attributes */
-	    if (mu_c_strncasecmp (buf, "status:", 7) == 0)
-	      {
-		int deleted = mhm->attr_flags & MU_ATTRIBUTE_DELETED;
-		mu_string_to_flags (buf, &mhm->attr_flags);
-		mhm->attr_flags |= deleted;
-	      }
-	    else if (mu_c_strncasecmp (buf, "x-imapbase:", 11) == 0)
-	      {
-		char *p;
-		mhm->amd->uidvalidity = strtoul (buf + 11, &p, 10);
-		/* second number is next uid. Ignored */
-	      }
-	  }
-	else
-	  {
-	    if (buf[n - 1] == '\n')
-	      blines++;
-	  }
-	off += n;
-      }
-
-  if (status == 0)
-    {
-      mhm->mtime = st.st_mtime;
-      if (!body_start)
-	body_start = off;
-      mhm->header_lines = hlines;
-      mhm->body_lines = blines;
-      mhm->body_start = body_start;
-      mhm->body_end = off;
-    }
-  return status;
-}
-
-static int
-amd_scan (mu_mailbox_t mailbox, size_t msgno, size_t *pcount)
-{
-  struct _amd_data *amd = mailbox->data;
-
-  if (! amd_is_updated (mailbox))
-    return amd->scan0 (mailbox, msgno, pcount, 1);
-
-  if (pcount)
-    *pcount = amd->msg_count;
-
-  return 0;
-}
-
-/* Is the internal representation of the mailbox up to date.
-   Return 1 if so, 0 otherwise. */
-static int
-amd_is_updated (mu_mailbox_t mailbox)
-{
-  struct stat st;
-  struct _amd_data *amd = mailbox->data;
-
-  if (stat (amd->name, &st) < 0)
-    return 1;
-
-  return amd->mtime == st.st_mtime;
-}
-
-static int
-amd_get_size (mu_mailbox_t mailbox, mu_off_t *psize)
-{
-  struct _amd_data *amd = mailbox->data;
-  if (amd->mailbox_size)
-    return amd->mailbox_size (mailbox, psize);
-  *psize = 0;
-  if (read_size_file (amd, psize))
-    {
-      int rc = compute_mailbox_size (amd, amd->name, psize);
-      if (rc == 0)
-	write_size_file (amd, *psize);
-      return rc;
-    }
-  return 0;
-}
-
-/* Return number of open streams residing in a message pool */
-static int
-amd_pool_open_count (struct _amd_data *amd)
-{
-  int cnt = amd->pool_last - amd->pool_first;
-  if (cnt < 0)
-    cnt += MAX_OPEN_STREAMS;
-  return cnt;
-}
-
-/* Look up a _amd_message in the pool of open messages.
-   If the message is found in the pool, returns the address of
-   the pool slot occupied by it. Otherwise returns NULL. */
-static struct _amd_message **
-amd_pool_lookup (struct _amd_message *mhm)
-{
-  struct _amd_data *amd = mhm->amd;
-  int i;
-
-  for (i = amd->pool_first; i != amd->pool_last; )
-    {
-      if (amd->msg_pool[i] == mhm)
-	return &amd->msg_pool[i];
-      if (++i == MAX_OPEN_STREAMS)
-	i = 0;
-    }
-  return NULL;
-}
-
-/* Open a stream associated with the message mhm. If the stream is
-   already open, do nothing.
-   NOTE: We could have reused the NULL holes in the msg_pool, but
-   that hardly is worth the effort, since the holes appear only when
-   expunging. On the other hand this may be useful when MAX_OPEN_STREAMS
-   size is very big. "Premature optimization is the root of all evil" */
-static int
-amd_pool_open (struct _amd_message *mhm)
-{
-  int status;
-  struct _amd_data *amd = mhm->amd;
-  if (amd_pool_lookup (mhm))
-    return 0;
-  if (amd_pool_open_count(amd) == MAX_OPEN_STREAMS-1)
-    {
-      amd_message_stream_close (amd->msg_pool[amd->pool_first++]);
-      amd->pool_first %= MAX_OPEN_STREAMS;
-    }
-  status = amd_message_stream_open (mhm);
-  if (status)
-    return status;
-  amd->msg_pool[amd->pool_last++] = mhm;
-  amd->pool_last %= MAX_OPEN_STREAMS;
-  return 0;
-}
-
-static void
-amd_pool_flush (struct _amd_data *amd)
-{
-  int i;
-  
-  for (i = amd->pool_first; i != amd->pool_last; )
-    {
-      if (amd->msg_pool[i])
-	amd_message_stream_close (amd->msg_pool[i]);
-      if (++i == MAX_OPEN_STREAMS)
-	i = 0;
-    }
-  amd->pool_first = amd->pool_last = 0;
-}
-
-/* Attach a stream to a given message structure. The latter is supposed
-   to be already added to the open message pool. */
-int
-amd_message_stream_open (struct _amd_message *mhm)
-{
-  struct _amd_data *amd = mhm->amd;
-  char *filename;
-  int status;
-  int flags = MU_STREAM_ALLOW_LINKS;
-
-  status = amd->cur_msg_file_name (mhm, &filename);
-  if (status)
-    return status;
-
-  /* The message should be at least readable */
-  if (amd->mailbox->flags & (MU_STREAM_RDWR|MU_STREAM_WRITE|MU_STREAM_APPEND))
-    flags |= MU_STREAM_RDWR;
-  else 
-    flags |= MU_STREAM_READ;
-  status = mu_file_stream_create (&mhm->stream, filename, flags);
-
-  free (filename);
-
-  if (status != 0)
-    return status;
-
-  /* FIXME: Select buffer size dynamically */
-  mu_stream_set_buffer (mhm->stream, mu_buffer_full, 16384);
-  
-  status = amd_scan_message (mhm);
-
-  return status;
-}
-
-/* Close the stream associated with the given message. */
-void
-amd_message_stream_close (struct _amd_message *mhm)
-{
-  if (mhm)
-    {
-      mu_stream_close (mhm->stream);
-      mhm->stream = NULL;
-    }
-}
-
-void
-amd_check_message (struct _amd_message *mhm)
-{
-  if (mhm->body_end == 0)
-    amd_pool_open (mhm);
-}
-
-/* Reading functions */
-static int
-amd_body_stream_read (mu_stream_t is, char *buffer, size_t buflen,
-		      size_t *pnread)
-{
-  struct _amd_body_stream *amdstr = (struct _amd_body_stream *)is;
-  mu_body_t body = amdstr->body;
-  mu_message_t msg = mu_body_get_owner (body);
-  struct _amd_message *mhm = mu_message_get_owner (msg);
-  size_t nread = 0;
-  int status = 0;
-  mu_off_t ln;
-
-  amd_pool_open (mhm);
-
-  if (buffer == NULL || buflen == 0)
-    {
-      *pnread = nread;
-      return 0;
-    }
-
-  mu_monitor_rdlock (mhm->amd->mailbox->monitor);
-#ifdef WITH_PTHREAD
-  /* read() is cancellation point since we're doing a potentially
-     long operation.  Lets make sure we clean the state.  */
-  pthread_cleanup_push (amd_cleanup, (void *)mhm->amd->mailbox);
-#endif
-
-  ln = mhm->body_end - (mhm->body_start + amdstr->off);
-  if (ln > 0)
-    {
-      nread = ((size_t)ln < buflen) ? (size_t)ln : buflen;
-      status = mu_stream_seek (mhm->stream, mhm->body_start + amdstr->off,
-			       MU_SEEK_SET, NULL);
-      if (status == 0)
-	{
-	  status = mu_stream_read (mhm->stream, buffer, nread, &nread);
-	  amdstr->off += nread;
-	}
-    }
-
-  *pnread = nread;
-
-  mu_monitor_unlock (mhm->amd->mailbox->monitor);
-#ifdef WITH_PTHREAD
-  pthread_cleanup_pop (0);
-#endif
-
-  return status;
-}
-
-static int
-amd_body_stream_readdelim (mu_stream_t is, char *buffer, size_t buflen,
-			   int delim,
-			   size_t *pnread)
-{
-  struct _amd_body_stream *amdstr = (struct _amd_body_stream *)is;
-  mu_body_t body = amdstr->body;
-  mu_message_t msg = mu_body_get_owner (body);
-  struct _amd_message *mhm = mu_message_get_owner (msg);
-  int status = 0;
-
-  amd_pool_open (mhm);
-
-  if (buffer == NULL || buflen == 0)
-    {
-      if (pnread)
-	*pnread = 0;
-      return 0;
-    }
-
-  mu_monitor_rdlock (mhm->amd->mailbox->monitor);
-#ifdef WITH_PTHREAD
-  /* read() is cancellation point since we're doing a potentially
-     long operation.  Lets make sure we clean the state.  */
-  pthread_cleanup_push (amd_cleanup, (void *)mhm->amd->mailbox);
-#endif
-
-  status = mu_stream_seek (mhm->stream, mhm->body_start + amdstr->off,
-			   MU_SEEK_SET, NULL);
-  if (status == 0)
-    {
-      size_t nread = 0;
-      size_t ln;
-	  
-      ln = mhm->body_end - (mhm->body_start + amdstr->off) + 1;
-      if (ln > 0)
-	{
-	  size_t rdsize = ((size_t)ln < buflen) ? (size_t)ln : buflen;
-	  status = mu_stream_readdelim (mhm->stream, buffer, rdsize,
-					delim, &nread);
-	  amdstr->off += nread;
-	}
-
-      if (pnread)
-	*pnread = nread;
-    }
-
-  mu_monitor_unlock (mhm->amd->mailbox->monitor);
-#ifdef WITH_PTHREAD
-  pthread_cleanup_pop (0);
-#endif
-
-  return status;
-}
-
-static int
-amd_body_stream_seek (mu_stream_t str, mu_off_t off, mu_off_t *presult)
-{
-  size_t size;
-  struct _amd_body_stream *amdstr = (struct _amd_body_stream *)str;
-  
-  amd_body_size (amdstr->body, &size);
-
-  if (off < 0 || off > size)
-    return ESPIPE;
-
-  amdstr->off = off;
-  if (presult)
-    *presult = off;
-  return 0;
-}
-
-/* Return corresponding sizes */
-
-static int
-amd_body_stream_size (mu_stream_t stream, mu_off_t *psize)
-{
-  mu_body_t body = ((struct _amd_body_stream *)stream)->body;
-  size_t size;
-  int rc = amd_body_size (body, &size);
-  if (rc == 0)
-    *psize = size;
-  return rc;
-}
-
-static int
-amd_body_size (mu_body_t body, size_t *psize)
-{
-  mu_message_t msg = mu_body_get_owner (body);
-  struct _amd_message *mhm = mu_message_get_owner (msg);
-  if (mhm == NULL)
-    return EINVAL;
-  amd_check_message (mhm);
-  if (psize)
-    *psize = mhm->body_end - mhm->body_start;
-  return 0;
-}
-
-static int
-amd_body_lines (mu_body_t body, size_t *plines)
-{
-  mu_message_t msg = mu_body_get_owner (body);
-  struct _amd_message *mhm = mu_message_get_owner (msg);
-  if (mhm == NULL)
-    return EINVAL;
-  amd_check_message (mhm);
-  if (plines)
-    *plines = mhm->body_lines;
-  return 0;
-}
-
-/* Headers */
-static int
-amd_header_fill (void *data, char **pbuf, size_t *plen)
-{
-  char *buffer;
-  size_t len;
-  mu_message_t msg = data;
-  struct _amd_message *mhm = mu_message_get_owner (msg);
-  int status, rc;
-  mu_off_t pos;
-  
-  status = amd_pool_open (mhm);
-  if (status)
-    return status;
-
-  len = mhm->body_start;
-  buffer = malloc (len);
-  if (!buffer)
-    return ENOMEM;
-  
-  status = mu_stream_seek (mhm->stream, 0, MU_SEEK_CUR, &pos);
-  if (status)
-    return status;
-  status = mu_stream_seek (mhm->stream, 0, MU_SEEK_SET, NULL);
-  if (status)
-    return status;
-
-  status = mu_stream_read (mhm->stream, buffer, len, NULL);
-  rc = mu_stream_seek (mhm->stream, pos, MU_SEEK_SET, NULL);
-
-  if (!status)
-    status = rc;
-  
-  if (status)
-    {
-      free (buffer);
-      return status;
-    }
-
-  *plen = len;
-  *pbuf = buffer;
-  return 0;
-}
-
-/* Attributes */
-static int
-amd_get_attr_flags (mu_attribute_t attr, int *pflags)
-{
-  mu_message_t msg = mu_attribute_get_owner (attr);
-  struct _amd_message *mhm = mu_message_get_owner (msg);
-
-  if (mhm == NULL)
-    return EINVAL;
-  if (pflags)
-    *pflags = mhm->attr_flags;
-  return 0;
-}
-
-static int
-amd_set_attr_flags (mu_attribute_t attr, int flags)
-{
-  mu_message_t msg = mu_attribute_get_owner (attr);
-  struct _amd_message *mhm = mu_message_get_owner (msg);
-
-  if (mhm == NULL)
-    return EINVAL;
-  mhm->attr_flags |= flags;
-  return 0;
-}
-
-static int
-amd_unset_attr_flags (mu_attribute_t attr, int flags)
-{
-  mu_message_t msg = mu_attribute_get_owner (attr);
-  struct _amd_message *mhm = mu_message_get_owner (msg);
-
-  if (mhm == NULL)
-    return EINVAL;
-  mhm->attr_flags &= ~flags;
-  return 0;
-}
-
-/* Envelope */
-static int
-amd_envelope_date (mu_envelope_t envelope, char *buf, size_t len,
-		   size_t *psize)
-{
-  mu_message_t msg = mu_envelope_get_owner (envelope);
-  struct _amd_message *mhm = mu_message_get_owner (msg);
-  mu_header_t hdr = NULL;
-  char *date;
-  int status;
-  
-  if (mhm == NULL)
-    return EINVAL;
-
-  if ((status = mu_message_get_header (msg, &hdr)) != 0)
-    return status;
-  if (mu_header_aget_value (hdr, MU_HEADER_ENV_DATE, &date)
-      && mu_header_aget_value (hdr, MU_HEADER_DELIVERY_DATE, &date))
-    return MU_ERR_NOENT;
-  else
-    {
-      time_t t;
-      int rc;
-      
-      /* Convert to ctime format */
-      rc = mu_parse_date (date, &t, NULL); /* FIXME: TZ info is lost */
-      free (date);
-      if (rc)
-	return MU_ERR_NOENT;
-      date = strdup (ctime (&t)); 
-    }
-
-  /* Format:  "sender date" */
-  if (buf && len > 0)
-    {
-      len--; /* Leave space for the null.  */
-      strncpy (buf, date, len);
-      if (strlen (date) < len)
-	{
-	  len = strlen (buf);
-	  if (buf[len-1] != '\n')
-	    buf[len++] = '\n';
-	}
-      buf[len] = '\0';
-    }
-  else
-    len = strlen (date);
-  
-  free (date);
-  
-  if (psize)
-    *psize = len;
-  return 0;
-}
-
-static int
-amd_envelope_sender (mu_envelope_t envelope, char *buf, size_t len, size_t *psize)
-{
-  mu_message_t msg = mu_envelope_get_owner (envelope);
-  struct _amd_message *mhm = mu_message_get_owner (msg);
-  mu_header_t hdr = NULL;
-  char *from;
-  int status;
-
-  if (mhm == NULL)
-    return EINVAL;
-
-  if ((status = mu_message_get_header (msg, &hdr)))
-    return status;
-  if ((status = mu_header_aget_value (hdr, MU_HEADER_ENV_SENDER, &from)))
-    return status;
-
-  if (buf && len > 0)
-    {
-      int slen = strlen (from);
-
-      if (len < slen + 1)
-	slen = len - 1;
-      memcpy (buf, from, slen);
-      buf[slen] = 0;
-    }
-  else
-    len = strlen (from);
-
-  if (psize)
-    *psize = len;
-  return 0;
-}
-
-
-int
-amd_remove_dir (const char *name)
-{
-  DIR *dir;
-  struct dirent *ent;
-  char *namebuf;
-  size_t namelen, namesize;
-  int rc = 0;
-  int has_subdirs = 0;
-  
-  namelen = strlen (name);
-  namesize = namelen + 128;
-  namebuf = malloc (namesize);
-  if (!namebuf)
-    return ENOMEM;
-  memcpy (namebuf, name, namelen);
-  if (namebuf[namelen - 1] != '/')
-    namebuf[namelen++] = '/';
-  
-  dir = opendir (name);
-  if (!dir)
-    return errno;
-  while ((ent = readdir (dir)))
-    {
-      struct stat st;
-      size_t len;
-
-      if (strcmp (ent->d_name, ".") == 0 ||
-	  strcmp (ent->d_name, "..") == 0)
-	continue;
-      len = strlen (ent->d_name);
-      if (namelen + len >= namesize)
-	{
-	  char *p;
-
-	  namesize += len + 1;
-	  p = realloc (namebuf, namesize);
-	  if (!p)
-	    {
-	      rc = ENOMEM;
-	      break;
-	    }
-	}
-      strcpy (namebuf + namelen, ent->d_name);
-      if (stat (namebuf, &st) == 0 && S_ISDIR (st.st_mode))
-	{
-	  has_subdirs = 1;
-	  continue;
-	}
-      
-      if (unlink (namebuf))
-	{
-	  rc = errno;
-	  mu_diag_output (MU_DIAG_WARNING,
-			  "failed to remove %s: %s",
-			  namebuf, mu_strerror (rc));
-	  break;
-	}
-    }
-  closedir (dir);
-  free (namebuf);
-
-  if (rc == 0 && !has_subdirs)
-    {
-      if (rmdir (name))
-	{
-	  rc = errno;
-	  /* POSIX.1-2001 allows EEXIST to be returned if the directory
-	     contained entries other than . and .. */
-	  if (rc == EEXIST)
-	    rc = ENOTEMPTY;
-	}
-    }
-  return rc;
-}
-
-
diff --git a/libmailutils/argcv.c b/libmailutils/argcv.c
deleted file mode 100644
index 636b9af..0000000
--- a/libmailutils/argcv.c
+++ /dev/null
@@ -1,549 +0,0 @@
-/* argcv.c - simple functions for parsing input based on whitespace
-   Copyright (C) 1999, 2000, 2001, 2003, 2004, 2005, 2006, 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 <ctype.h>
-#include <errno.h>
-#include <mailutils/argcv.h>
-
-/* Keep mailutils namespace clean */
-#define argcv_get            mu_argcv_get 
-#define argcv_get_n          mu_argcv_get_n 
-#define argcv_get_np         mu_argcv_get_np 
-#define argcv_string         mu_argcv_string
-#define argcv_free           mu_argcv_free
-#define argv_free            mu_argv_free 
-#define argcv_unquote_char   mu_argcv_unquote_char
-#define argcv_quote_char     mu_argcv_quote_char  
-#define argcv_quoted_length  mu_argcv_quoted_length
-#define argcv_unquote_copy   mu_argcv_unquote_copy 
-#define argcv_quote_copy     mu_argcv_quote_copy     
-
-/*
- * takes a string and splits it into several strings, breaking at ' '
- * command is the string to split
- * the number of strings is placed into argc
- * the split strings are put into argv
- * returns 0 on success, nonzero on failure
- */
-
-#define isws(c) ((c)==' '||(c)=='\t'||(c)=='\n')
-#define isdelim(c,delim) (strchr(delim,(c))!=NULL)
-
-struct argcv_info
-{
-  int len;
-  const char *command;
-  const char *delim;
-  const char *comment;
-  int flags;
-  
-  int start;
-  int end;
-  int save;
-  int finish_pos;
-};
-
-static void
-init_argcv_info (struct argcv_info *ap, int flags,
-		 int len, const char *command, const char *delim,
-		 const char *comment)
-{
-  memset (ap, 0, sizeof *ap);
-  ap->len = len;
-  ap->command = command;
-  ap->delim = delim;
-  ap->comment = comment;
-  ap->flags = flags;
-}
-
-static int
-argcv_scan (struct argcv_info *ap)
-{
-  int i = 0;
-  int len = ap->len;
-  const char *command = ap->command;
-  const char *delim = ap->delim;
-  const char *comment = ap->comment;
-  
-  for (;;)
-    {
-      i = ap->save;
-
-      if (i >= len)
-	return i + 1;
-
-      /* Skip initial whitespace */
-      while (i < len && isws (command[i]))
-	i++;
-      ap->start = i;
-
-      if (!isdelim (command[i], delim))
-	{
-	  while (i < len)
-	    {
-	      if (command[i] == '\\')
-		{
-		  if (++i == len)
-		    break;
-		  i++;
-		  continue;
-		}
-	      
-	      if (command[i] == '\'' || command[i] == '"')
-		{
-		  int j;
-		  for (j = i + 1; j < len && command[j] != command[i]; j++)
-		    if (command[j] == '\\')
-		      j++;
-		  if (j < len)
-		    i = j + 1;
-		  else
-		    i++;
-		}
-	      else if (isws (command[i]) || isdelim (command[i], delim))
-		break;
-	      else
-		i++; /* skip the escaped character */
-	    }
-	  i--;
-	}
-      else if (!(ap->flags & MU_ARGCV_RETURN_DELIMS))
-	{
-	  while (i < len && isdelim (command[i], delim))
-	    i++;
-	  ap->save = i;
-	  continue;
-	}
-      
-
-      ap->end = i;
-      ap->save = ap->finish_pos = i + 1;
-
-      /* If we have a token, and it starts with a comment character, skip
-         to the newline and restart the token search. */
-      if (ap->save <= len)
-	{
-	  if (strchr (comment, command[ap->start]) != NULL)
-	    {
-	      ap->finish_pos = ap->start;
-	      i = ap->save;
-	      while (i < len && command[i] != '\n')
-		i++;
-
-	      ap->save = i;
-	      continue;
-	    }
-	}
-      break;
-    }
-  return ap->save;
-}
-
-static char quote_transtab[] = "\\\\\"\"a\ab\bf\fn\nr\rt\tv\v";
-
-int
-argcv_unquote_char (int c)
-{
-  char *p;
-
-  for (p = quote_transtab; *p; p += 2)
-    {
-      if (*p == c)
-	return p[1];
-    }
-  return c;
-}
-
-int
-argcv_quote_char (int c)
-{
-  char *p;
-  
-  for (p = quote_transtab + sizeof(quote_transtab) - 2;
-       p > quote_transtab; p -= 2)
-    {
-      if (*p == c)
-	return p[-1];
-    }
-  return -1;
-}
-  
-#define to_num(c) \
-  (isdigit(c) ? c - '0' : (isxdigit(c) ? toupper(c) - 'A' + 10 : 255 ))
-
-static int
-xtonum (int *pval, const char *src, int base, int cnt)
-{
-  int i, val;
-  
-  for (i = 0, val = 0; i < cnt; i++, src++)
-    {
-      int n = *(unsigned char*)src;
-      if (n > 127 || (n = to_num(n)) >= base)
-	break;
-      val = val*base + n;
-    }
-  *pval = val;
-  return i;
-}
-
-size_t
-argcv_quoted_length (const char *str, int *quote)
-{
-  size_t len = 0;
-
-  *quote = 0;
-  for (; *str; str++)
-    {
-      if (*str == ' ')
-	{
-	  len++;
-	  *quote = 1;
-	}
-      else if (*str == '"')
-	{
-	  len += 2;
-	  *quote = 1;
-	}
-      else if (*str != '\t' && *str != '\\' && isprint (*str))
-	len++;
-      else if (argcv_quote_char (*str) != -1)
-	len += 2;
-      else
-	len += 4;
-    }
-  return len;
-}
-
-void
-argcv_unquote_copy (char *dst, const char *src, size_t n)
-{
-  int i = 0;
-  int c;
-  int expect_delim = 0; 
-    
-  while (i < n)
-    {
-      switch (src[i])
-	{
-	case '\'':
-	case '"':
-	  if (!expect_delim)
-	    {
-	      const char *p;
-	      
-	      for (p = src+i+1; *p && *p != src[i]; p++)
-		if (*p == '\\')
-		  p++;
-	      if (*p)
-		expect_delim = src[i++];
-	      else
-		*dst++ = src[i++];
-	    }
-	  else if (expect_delim == src[i])
-	    ++i;
-	  else
-	    *dst++ = src[i++];
-	  break;
-	  
-	case '\\':
-	  ++i;
-	  if (src[i] == 'x' || src[i] == 'X')
-	    {
-	      if (n - i < 2)
-		{
-		  *dst++ = '\\';
-		  *dst++ = src[i++];
-		}
-	      else 
-		{
-		  int off = xtonum(&c, src + i + 1, 16, 2);
-		  if (off == 0)
-		    {
-		      *dst++ = '\\';
-		      *dst++ = src[i++];
-		    }
-		  else
-		    {
-		      *dst++ = c;
-		      i += off + 1;
-		    }
-		}
-	    }
-	  else if ((unsigned char)src[i] < 128 && isdigit (src[i]))
-	    {
-	      if (n - i < 1)
-		{
-		  *dst++ = '\\';
-		  *dst++ = src[i++];
-		}
-	      else
-		{
-		  int off = xtonum (&c, src+i, 8, 3);
-		  if (off == 0)
-		    {
-		      *dst++ = '\\';
-		      *dst++ = src[i++];
-		    }
-		  else
-		    {
-		      *dst++ = c;
-		      i += off;
-		    }
-		}
-	    }
-	  else
-	    *dst++ = argcv_unquote_char (src[i++]);
-	  break;
-	  
-	default:
-	  *dst++ = src[i++];
-	}
-    }
-  *dst = 0;
-}
-
-void
-argcv_quote_copy (char *dst, const char *src)
-{
-  for (; *src; src++)
-    {
-      if (*src == '"')
-	{
-	  *dst++ = '\\';
-	  *dst++ = *src;
-	}
-      else if (*src != '\t' && *src != '\\' && isprint(*src))
-	*dst++ = *src;      
-      else
-	{
-	  int c = argcv_quote_char (*src);
-	  *dst++ = '\\';
-	  if (c != -1)
-	    *dst++ = c;
-	  else
-	    {
-	      char tmp[4];
-	      snprintf (tmp, sizeof tmp, "%03o", *(unsigned char*)src);
-	      memcpy (dst, tmp, 3);
-	      dst += 3;
-	    }
-	}
-    }
-}
-
-int
-argcv_get_np (const char *command, int len,
-	      const char *delim, const char *cmnt,
-	      int flags,
-	      int *pargc, char ***pargv, char **endp)
-{
-  int i = 0;
-  struct argcv_info info;
-  int argc;
-  char **argv;
-  
-  if (!delim)
-    delim = "";
-  if (!cmnt)
-    cmnt = "";
-
-  init_argcv_info (&info, flags, len, command, delim, cmnt);
-
-  /* Count number of arguments */
-  argc = 0;
-  while (argcv_scan (&info) <= len)
-    argc++;
-
-  argv = calloc ((argc + 1), sizeof (char *));
-  if (argv == NULL)
-    return ENOMEM;
-  
-  i = 0;
-  info.save = 0;
-  for (i = 0; i < argc; i++)
-    {
-      int n;
-      int unquote;
-      
-      argcv_scan (&info);
-
-      if ((command[info.start] == '"' || command[info.end] == '\'')
-	  && command[info.end] == command[info.start])
-	{
-	  if (info.start < info.end)
-	    {
-	      info.start++;
-	      info.end--;
-	    }
-	  unquote = 0;
-	}
-      else
-	unquote = 1;
-      
-      n = info.end - info.start + 1;
-      argv[i] = calloc (n + 1,  sizeof (char));
-      if (argv[i] == NULL)
-	{
-	  argcv_free (i, argv);
-	  return ENOMEM;
-	}
-      if (unquote)
-	argcv_unquote_copy (argv[i], &command[info.start], n);
-      else
-	memcpy (argv[i], &command[info.start], n);
-      argv[i][n] = 0;
-    }
-  argv[i] = NULL;
-
-  *pargc = argc;
-  *pargv = argv;
-  if (endp)
-    *endp = (char*) (command + info.finish_pos);
-  return 0;
-}
-
-int
-argcv_get_n (const char *command, int len, const char *delim, const char *cmnt,
-	     int *pargc, char ***pargv)
-{
-  return argcv_get_np (command, len, delim, cmnt, MU_ARGCV_RETURN_DELIMS,
-		       pargc, pargv, NULL);
-}
-
-int
-argcv_get (const char *command, const char *delim, const char *cmnt,
-	   int *argc, char ***argv)
-{
-  return argcv_get_n (command, strlen (command), delim, cmnt, argc, argv);
-}
-
-
-/*
- * frees all elements of an argv array
- * argc is the number of elements
- * argv is the array
- */
-void
-argcv_free (int argc, char **argv)
-{
-  if (argc <= 0)
-    return;
-  while (--argc >= 0)
-    if (argv[argc])
-      free (argv[argc]);
-  free (argv);
-}
-
-void
-argv_free (char **argv)
-{
-  int i;
-
-  for (i = 0; argv[i]; i++)
-    free (argv[i]);
-  free (argv);
-}
-
-/* Make a argv an make string separated by ' '.  */
-
-int
-argcv_string (int argc, char **argv, char **pstring)
-{
-  size_t i, j, len;
-  char *buffer;
-
-  /* No need.  */
-  if (pstring == NULL)
-    return EINVAL;
-
-  buffer = malloc (1);
-  if (buffer == NULL)
-    return ENOMEM;
-  *buffer = '\0';
-
-  for (len = i = j = 0; i < argc; i++)
-    {
-      int quote;
-      int toklen;
-
-      toklen = argcv_quoted_length (argv[i], &quote);
-      
-      len += toklen + 2;
-      if (quote)
-	len += 2;
-      
-      buffer = realloc (buffer, len);
-      if (buffer == NULL)
-        return ENOMEM;
-
-      if (i != 0)
-	buffer[j++] = ' ';
-      if (quote)
-	buffer[j++] = '"';
-      argcv_quote_copy (buffer + j, argv[i]);
-      j += toklen;
-      if (quote)
-	buffer[j++] = '"';
-    }
-
-  for (; j > 0 && isspace (buffer[j-1]); j--)
-    ;
-  buffer[j] = 0;
-  if (pstring)
-    *pstring = buffer;
-  return 0;
-}
-
-void
-mu_argcv_remove (int *pargc, char ***pargv,
-		 int (*sel) (const char *, void *), void *data)
-{
-  int i, j;
-  int argc = *pargc;
-  char **argv = *pargv;
-  int cnt = 0;
-  
-  for (i = j = 0; i < argc; i++)
-    {
-      if (sel (argv[i], data))
-	{
-	  free (argv[i]);
-	  cnt++;
-	}
-      else
-	{
-	  if (i != j)
-	    argv[j] = argv[i];
-	  j++;
-	}
-    }
-  if (i != j)
-    argv[j] = NULL;
-  argc -= cnt;
-
-  *pargc = argc;
-  *pargv = argv;
-}
-      
- 
diff --git a/libmailutils/asnprintf.c b/libmailutils/asnprintf.c
deleted file mode 100644
index f8ef543..0000000
--- a/libmailutils/asnprintf.c
+++ /dev/null
@@ -1,40 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 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 */
-
-#if HAVE_CONFIG_H
-# include <config.h>
-#endif
-
-#include <stdlib.h>
-#include <stdarg.h>
-#include <stdio.h>
-
-#include <mailutils/io.h>
-
-int
-mu_asnprintf (char **pbuf, size_t *psize, const char *fmt, ...)
-{
-  int rc;
-  va_list ap;
-
-  va_start (ap, fmt);
-  rc = mu_vasnprintf (pbuf, psize, fmt, ap);
-  va_end (ap);
-  return rc;
-}
-
diff --git a/libmailutils/asprintf.c b/libmailutils/asprintf.c
deleted file mode 100644
index c563bcb..0000000
--- a/libmailutils/asprintf.c
+++ /dev/null
@@ -1,42 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 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 */
-
-#if HAVE_CONFIG_H
-# include <config.h>
-#endif
-
-#include <stdlib.h>
-#include <stdarg.h>
-#include <stdio.h>
-
-#include <mailutils/io.h>
-
-int
-mu_asprintf (char **pbuf, const char *fmt, ...)
-{
-  va_list ap;
-  size_t size;
-  int rc;
-  
-  va_start (ap, fmt);
-  *pbuf = NULL;
-  size = 0;
-  rc = mu_vasnprintf (pbuf, &size, fmt, ap);
-  va_end (ap);
-  return rc;
-}
diff --git a/libmailutils/assoc.c b/libmailutils/assoc.c
deleted file mode 100644
index a0c13a0..0000000
--- a/libmailutils/assoc.c
+++ /dev/null
@@ -1,529 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 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.
-
-   GNU Mailutils 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 GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
-
-#if HAVE_CONFIG_H
-# include <config.h>
-#endif
-
-#include <stdlib.h>
-#include <string.h>
-#include <mailutils/types.h>
-#include <mailutils/assoc.h>
-#include <mailutils/errno.h>
-#include <mailutils/error.h>
-#include <mailutils/iterator.h>
-#include <mailutils/mutil.h>
-#include <mailutils/cstr.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,
-   starting from 64. Hardly someone will need more than 16411 symbols, and
-   even if someone will, it is easy enough to add more numbers to the
-   sequence. */
-
-static unsigned int hash_size[] = {
-    37,   101,  229,  487, 1009, 2039, 4091, 8191, 16411
-};
-
-/* |max_rehash| keeps the number of entries in |hash_size| table. */
-static unsigned int max_rehash = sizeof (hash_size) / sizeof (hash_size[0]);
-
-struct _mu_assoc_elem
-{
-  char *name;
-  char data[1];
-};
-
-struct _mu_assoc
-{
-  int flags;
-  unsigned int hash_num;  /* Index to hash_size table */
-  size_t elsize;          /* Size of an element */
-  void *tab;
-  mu_assoc_free_fn free;
-  mu_iterator_t itr;
-};
-
-struct _mu_assoc_elem_align
-{
-  char c;
-  struct _mu_assoc_elem x;
-};
-
-#define __ASSOC_ELEM_ALIGNMENT (mu_offsetof(struct _mu_assoc_elem_align, x))
-
-#define __ASSOC_ELEM_SIZE(a) \
-   ((a)->elsize + mu_offsetof(struct _mu_assoc_elem, data))
-#define __ASSOC_ALIGN(a, b) (((a) + (b) - 1) & ~((b) - 1))
-#define ASSOC_ELEM_SIZE(a) \
-   __ASSOC_ALIGN(__ASSOC_ELEM_SIZE(a),__ASSOC_ELEM_ALIGNMENT)
-
-#define __ASSOC_ELEM(a,p,n) \
- ((struct _mu_assoc_elem*) ((char*) (p) + ASSOC_ELEM_SIZE (a) * n))
-
-#define ASSOC_ELEM(a,n) __ASSOC_ELEM(a,(a)->tab,n)
-
-#define ASSOC_ELEM_INDEX(a,e) \
- (((char*)(e) - (char*)(a)->tab) / ASSOC_ELEM_SIZE (a))
-
-
-static unsigned
-hash (const char *name, unsigned long hash_num)
-{
-  unsigned i;
-	
-  for (i = 0; *name; name++)
-    {
-      i <<= 1;
-      i ^= *(unsigned char*) name;
-    }
-  return i % hash_size[hash_num];
-};
-
-static int
-assoc_lookup_or_install (struct _mu_assoc_elem **elp,
-			 mu_assoc_t assoc, const char *name, int *install);
-
-static int
-assoc_rehash (mu_assoc_t assoc)
-{
-  void *old_tab = assoc->tab;
-  void *new_tab;
-  unsigned int i;
-  unsigned int hash_num = assoc->hash_num + 1;
-  
-  if (hash_num >= max_rehash)
-      return MU_ERR_BUFSPACE;
-
-  new_tab = calloc (hash_size[hash_num], ASSOC_ELEM_SIZE (assoc));
-  assoc->tab = new_tab;
-  if (old_tab)
-    {
-      assoc->hash_num = hash_num;
-      for (i = 0; i < hash_size[hash_num-1]; i++)
-	{
-	  struct _mu_assoc_elem *elt = __ASSOC_ELEM (assoc, old_tab, i);
-	  if (elt->name)
-	    {
-	      int tmp;
-	      struct _mu_assoc_elem *newp;
-	      
-	      int rc = assoc_lookup_or_install (&newp, assoc, elt->name, &tmp);
-	      if (rc)
-		return rc;
-	      newp->name = elt->name;
-	      memcpy(newp->data, elt->data, assoc->elsize);
-	    }
-	}
-      free (old_tab);
-    }
-  return 0;
-}
-
-static void
-assoc_free_elem (mu_assoc_t assoc, struct _mu_assoc_elem *elem)
-{
-  if (assoc->free)
-    assoc->free (elem->data);
-  if (!(assoc->flags & MU_ASSOC_COPY_KEY))
-    free (elem->name);
-}
-
-static int
-assoc_remove (mu_assoc_t assoc, struct _mu_assoc_elem *elem)
-{
-  unsigned int i, j, r;
-
-  if (!(ASSOC_ELEM (assoc, 0) <= elem
-	&& elem < ASSOC_ELEM (assoc, hash_size[assoc->hash_num])))
-    return EINVAL;
-
-  assoc_free_elem (assoc, elem);
-  
-  for (i = ASSOC_ELEM_INDEX (assoc, elem);;)
-    {
-      struct _mu_assoc_elem *p = ASSOC_ELEM (assoc, i);
-      p->name = NULL;
-      j = i;
-
-      do
-	{
-	  if (++i >= hash_size[assoc->hash_num])
-	    i = 0;
-	  p = ASSOC_ELEM (assoc, i);
-	  if (!p->name)
-	    return 0;
-	  r = hash (p->name, assoc->hash_num);
-	}
-      while ((j < r && r <= i) || (i < j && j < r) || (r <= i && i < j));
-
-      if (j != i)
-	      memcpy (ASSOC_ELEM (assoc, j), ASSOC_ELEM (assoc, i),
-		      assoc->elsize);
-    }
-  return 0;
-}
-
-#define name_cmp(assoc,a,b) (((assoc)->flags & MU_ASSOC_ICASE) ? \
-                             mu_c_strcasecmp(a,b) : strcmp(a,b))
-
-static int
-assoc_lookup_or_install (struct _mu_assoc_elem **elp,
-			 mu_assoc_t assoc, const char *name, int *install)
-{
-  int rc;
-  unsigned i, pos;
-  struct _mu_assoc_elem *elem;
-  
-  if (!assoc->tab)
-    {
-      if (install)
-	{
-	  rc = assoc_rehash (assoc);
-	  if (rc)
-	    return rc;
-	}
-      else
-	return MU_ERR_NOENT;
-    }
-
-  pos = hash (name, assoc->hash_num);
-
-  for (i = pos; (elem = ASSOC_ELEM (assoc, i))->name;)
-    {
-      if (name_cmp (assoc, elem->name, name) == 0)
-	{
-	  if (install)
-	    *install = 0;
-	  *elp = elem;
-	  return 0;
-	}
-      
-      if (++i >= hash_size[assoc->hash_num])
-	i = 0;
-      if (i == pos)
-	break;
-    }
-
-  if (!install)
-    return MU_ERR_NOENT;
-  
-  if (elem->name == NULL)
-    {
-      *install = 1;
-      if (assoc->flags & MU_ASSOC_COPY_KEY)
-	elem->name = (char *) name;
-      else
-	{
-	  elem->name = strdup (name);
-	  if (!elem->name)
-	    return ENOMEM;
-	}
-      *elp = elem;
-      return 0; 
-    }
-
-  if ((rc = assoc_rehash (assoc)) != 0)
-    return rc;
-
-  return assoc_lookup_or_install (elp, assoc, name, install);
-}
-
-int
-mu_assoc_create (mu_assoc_t *passoc, size_t elsize, int flags)
-{
-  mu_assoc_t assoc = calloc (1, sizeof (*assoc));
-  if (!assoc)
-    return ENOMEM;
-  assoc->flags = flags;
-  assoc->elsize = elsize;
-  *passoc = assoc;
-  return 0;
-}
-
-void
-mu_assoc_clear (mu_assoc_t assoc)
-{
-  unsigned i, hs;
-  
-  if (!assoc || !assoc->tab)
-    return;
-
-  hs = hash_size[assoc->hash_num];
-  for (i = 0; i < hs; i++)
-    {
-      struct _mu_assoc_elem *elem = ASSOC_ELEM (assoc, i);
-      if (elem->name)
-	{
-	  assoc_free_elem (assoc, elem);
-	  elem->name = NULL;
-	}
-    }
-}
-
-void
-mu_assoc_destroy (mu_assoc_t *passoc)
-{
-  mu_assoc_t assoc;
-  if (passoc && (assoc = *passoc) != NULL)
-    {
-      mu_assoc_clear (assoc);
-      free (assoc->tab);
-      free (assoc);
-      *passoc = NULL;
-    }
-}
-
-int
-mu_assoc_set_free (mu_assoc_t assoc, mu_assoc_free_fn fn)
-{
-  if (!assoc)
-    return EINVAL;
-  assoc->free = fn;
-  return 0;
-}
-
-void *
-mu_assoc_ref (mu_assoc_t assoc, const char *name)
-{
-  int rc;
-  static struct _mu_assoc_elem *elp;
-
-  if (!assoc || !name)
-    return NULL;
-  
-  rc = assoc_lookup_or_install (&elp, assoc, name, NULL);
-  if (rc == 0)
-    return elp->data;
-  return NULL;
-}
-
-int
-mu_assoc_install (mu_assoc_t assoc, const char *name, void *value)
-{
-  int rc;
-  int inst;
-  static struct _mu_assoc_elem *elp;
-
-  if (!assoc || !name)
-    return EINVAL;
-
-  rc = assoc_lookup_or_install (&elp, assoc, name, &inst);
-  if (rc)
-    return rc;
-  if (!inst)
-    return MU_ERR_EXISTS;
-  memcpy (elp->data, value, assoc->elsize);
-  return 0;
-}
-
-int
-mu_assoc_ref_install (mu_assoc_t assoc, const char *name, void **pval)
-{
-  int rc;
-  int inst;
-  static struct _mu_assoc_elem *elp;
-
-  if (!assoc || !name)
-    return EINVAL;
-
-  rc = assoc_lookup_or_install (&elp, assoc, name, &inst);
-  if (rc)
-    return rc;
-  *pval = elp->data;
-  return inst ? 0 : MU_ERR_EXISTS;
-}  
-
-int
-mu_assoc_remove (mu_assoc_t assoc, const char *name)
-{
-  int rc;
-  static struct _mu_assoc_elem *elem;
-
-  if (!assoc || !name)
-    return EINVAL;
-  rc = assoc_lookup_or_install (&elem, assoc, name, NULL);
-  if (rc)
-    return rc;
-  return assoc_remove (assoc, elem);
-}
-
-#define OFFSET(s,f) (size_t)(&((s*)0)->f)
-
-int
-mu_assoc_remove_ref (mu_assoc_t assoc, void *data)
-{
-  struct _mu_assoc_elem *elem;
-
-  elem = (struct _mu_assoc_elem *) ((char*)data -
-				    OFFSET(struct _mu_assoc_elem, data));
-  return assoc_remove (assoc, elem);
-}
-
-
-/* Iterator interface */
-
-struct assoc_iterator
-{
-  mu_assoc_t assoc;
-  unsigned start;
-  unsigned index;
-};
-
-static int
-first (void *owner)
-{
-  struct assoc_iterator *itr = owner;
-  mu_assoc_t assoc = itr->assoc;
-  unsigned hash_max = hash_size[assoc->hash_num];
-  unsigned i;
-  
-  for (i = 0; i < hash_max; i++)
-    if ((ASSOC_ELEM (assoc, i))->name)
-      break;
-  itr->index = i;
-  return 0;
-}
-
-static int
-next (void *owner)
-{
-  struct assoc_iterator *itr = owner;
-  mu_assoc_t assoc = itr->assoc;
-  unsigned hash_max = hash_size[assoc->hash_num];
-  unsigned i;
-  
-  for (i = itr->index + 1; i < hash_max; i++)
-    if ((ASSOC_ELEM (assoc, i))->name)
-      break;
-
-  itr->index = i;
-  return 0;
-}
-
-static int
-getitem (void *owner, void **pret, const void **pkey)
-{
-  struct assoc_iterator *itr = owner;
-  struct _mu_assoc_elem *elem;
-
-  if (itr->index >= hash_size[itr->assoc->hash_num])
-    return EINVAL;
-  elem = ASSOC_ELEM (itr->assoc, itr->index);
-  *pret = elem->data;
-  if (pkey)
-    *pkey = elem->name;
-  return 0;
-}
-
-static int
-finished_p (void *owner)
-{
-  struct assoc_iterator *itr = owner;
-  return itr->index >= hash_size[itr->assoc->hash_num];
-}
-
-static int
-destroy (mu_iterator_t iterator, void *data)
-{
-  struct assoc_iterator *itr = data;
-  mu_iterator_detach (&itr->assoc->itr, iterator);
-  free (data);
-  return 0;
-}
-
-static int
-curitem_p (void *owner, void *item)
-{
-  struct assoc_iterator *itr = owner;
-  mu_assoc_t assoc = itr->assoc;
-  struct _mu_assoc_elem *elem = ASSOC_ELEM (assoc, itr->index);
-  
-  return elem == item;
-}
-
-static int
-assoc_data_dup (void **ptr, void *owner)
-{
-  *ptr = malloc (sizeof (struct assoc_iterator));
-  if (*ptr == NULL)
-    return ENOMEM;
-  memcpy (*ptr, owner, sizeof (struct assoc_iterator));
-  return 0;
-}
-
-int
-mu_assoc_get_iterator (mu_assoc_t assoc, mu_iterator_t *piterator)
-{
-  mu_iterator_t iterator;
-  int status;
-  struct assoc_iterator *itr;
-
-  if (!assoc)
-    return EINVAL;
-
-  itr = calloc (1, sizeof *itr);
-  if (!itr)
-    return ENOMEM;
-  itr->assoc = assoc;
-  itr->index = 0;
-
-  status = mu_iterator_create (&iterator, itr);
-  if (status)
-    {
-      free (itr);
-      return status;
-    }
-
-  mu_iterator_set_first (iterator, first);
-  mu_iterator_set_next (iterator, next);
-  mu_iterator_set_getitem (iterator, getitem);
-  mu_iterator_set_finished_p (iterator, finished_p);
-  mu_iterator_set_curitem_p (iterator, curitem_p);
-  mu_iterator_set_destroy (iterator, destroy);
-  mu_iterator_set_dup (iterator, assoc_data_dup);
-  
-  mu_iterator_attach (&assoc->itr, iterator);
-
-  *piterator = iterator;
-  return 0;
-}  
-
-
-
-int
-mu_assoc_count (mu_assoc_t assoc, size_t *pcount)
-{
-  mu_iterator_t itr;
-  int rc;
-  size_t count = 0;
-  
-  if (!assoc || !pcount)
-    return EINVAL;
-  rc = mu_assoc_get_iterator (assoc, &itr);
-  if (rc)
-    return rc;
-  for (mu_iterator_first (itr); !mu_iterator_is_done (itr);
-       mu_iterator_next (itr))
-    count++;
-  mu_iterator_destroy (&itr);
-  *pcount = count;
-  return 0;
-}
-
diff --git a/libmailutils/attachment.c b/libmailutils/attachment.c
deleted file mode 100644
index c642681..0000000
--- a/libmailutils/attachment.c
+++ /dev/null
@@ -1,461 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 1999, 2000, 2001, 2004, 2005, 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 <errno.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-
-#ifdef HAVE_LIBGEN_H
-# include <libgen.h>
-#endif
-
-#ifdef HAVE_STRINGS_H
-# include <strings.h>
-#endif
-
-#include <mailutils/cctype.h>
-#include <mailutils/cstr.h>
-#include <mailutils/body.h>
-#include <mailutils/filter.h>
-#include <mailutils/header.h>
-#include <mailutils/message.h>
-#include <mailutils/stream.h>
-#include <mailutils/errno.h>
-#include <mailutils/mutil.h>
-
-#define BUF_SIZE	2048
-
-struct _mu_mime_io_buffer
-{
-  unsigned int refcnt;
-  char *buf;
-  size_t bufsize;
-  char *charset;
-  mu_header_t hdr;
-  mu_message_t msg;
-  mu_stream_t stream;	/* output file/decoding stream for saving attachment */
-  mu_stream_t fstream;	/* output file stream for saving attachment */
-};
-
-#define MSG_HDR "Content-Type: %s; name=%s\nContent-Transfer-Encoding: %s\nContent-Disposition: attachment; filename=%s\n\n"
-
-int
-mu_message_create_attachment (const char *content_type, const char *encoding,
-			      const char *filename, mu_message_t *newmsg)
-{
-  mu_header_t hdr;
-  mu_body_t body;
-  mu_stream_t fstream = NULL, tstream = NULL;
-  char *header = NULL, *name = NULL, *fname = NULL;
-  int ret;
-
-  if (newmsg == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-  if (filename == NULL)
-    return EINVAL;
-
-  if ((ret = mu_message_create (newmsg, NULL)) == 0)
-    {
-      if (content_type == NULL)
-	content_type = "text/plain";
-      if (encoding == NULL)
-	encoding = "7bit";
-      if ((fname = strdup (filename)) != NULL)
-	{
-	  name = strrchr (fname, '/');
-	  if (name)
-	    name++;
-	  else
-	    name = fname;
-	  if ((header =
-	       malloc (strlen (MSG_HDR) + strlen (content_type) +
-		       strlen (name) * 2 + strlen (encoding) + 1)) == NULL)
-	    ret = ENOMEM;
-	  else
-	    {
-	      sprintf (header, MSG_HDR, content_type, name, encoding, name);
-	      if ((ret = mu_header_create (&hdr, header, strlen (header)))
-		  == 0)
-		{
-		  mu_message_get_body (*newmsg, &body);
-		  if ((ret =
-		       mu_file_stream_create (&fstream, filename,
-					      MU_STREAM_READ)) == 0)
-		    {
-		      if ((ret =
-			   mu_filter_create (&tstream, fstream, encoding,
-					     MU_FILTER_ENCODE,
-					     MU_STREAM_READ)) == 0)
-			{
-			  mu_body_set_stream (body, tstream, *newmsg);
-			  mu_message_set_header (*newmsg, hdr, NULL);
-			}
-		    }
-		}
-	      free (header);
-	    }
-	}
-    }
-  if (ret)
-    {
-      if (*newmsg)
-	mu_message_destroy (newmsg, NULL);
-      if (hdr)
-	mu_header_destroy (&hdr);
-      if (fstream)
-	mu_stream_destroy (&fstream);
-      if (fname)
-	free (fname);
-    }
-  return ret;
-}
-
-int
-mu_mime_io_buffer_create (mu_mime_io_buffer_t *pinfo)
-{
-  mu_mime_io_buffer_t info;
-  
-  if ((info = calloc (1, sizeof (*info))) == NULL)
-    return ENOMEM;
-  info->refcnt = 1;
-  info->bufsize = BUF_SIZE;
-  *pinfo = info;
-  return 0;
-}
-
-void
-mu_mime_io_buffer_set_size (mu_mime_io_buffer_t info, size_t size)
-{
-  info->bufsize = size;
-}
-
-void
-mu_mime_io_buffer_get_size (mu_mime_io_buffer_t info, size_t *psize)
-{
-  *psize = info->bufsize;
-}
-
-int
-mu_mime_io_buffer_set_charset (mu_mime_io_buffer_t info, const char *charset)
-{
-  char *cp = strdup (charset);
-  if (!cp)
-    return ENOMEM;
-  free (info->charset);
-  info->charset = cp;
-  return 0;
-}
-
-void
-mu_mime_io_buffer_sget_charset (mu_mime_io_buffer_t info, const char **charset)
-{
-  *charset = info->charset;
-}
-
-int
-mu_mime_io_buffer_aget_charset (mu_mime_io_buffer_t info, const char **charset)
-{
-  *charset = strdup (info->charset);
-  if (!charset)
-    return ENOMEM;
-  return 0;
-}
-
-void
-mu_mime_io_buffer_destroy (mu_mime_io_buffer_t *pinfo)
-{
-  if (pinfo && *pinfo)
-    {
-      mu_mime_io_buffer_t info = *pinfo;
-      free (info->charset);
-      free (info->buf);
-      free (info);
-      *pinfo = NULL;
-    }
-}
-
-static void
-_attachment_free (struct _mu_mime_io_buffer *info, int free_message)
-{
-  if (free_message)
-    {
-      if (info->msg)
-	mu_message_destroy (&info->msg, NULL);
-      else if (info->hdr)
-	mu_header_destroy (&info->hdr);
-    }
-  info->msg = NULL;
-  info->hdr = NULL;
-  info->stream = NULL;
-  info->fstream = NULL;
-  if (--info->refcnt == 0)
-    {
-      free (info->charset);
-      free (info->buf);
-      free (info);
-    }
-}
-
-static int
-_attachment_setup (mu_mime_io_buffer_t *pinfo, mu_message_t msg,
-		   mu_stream_t *pstream)
-{
-  int ret;
-  mu_body_t body;
-  mu_mime_io_buffer_t info;
-  mu_stream_t stream;
-    
-  if ((ret = mu_message_get_body (msg, &body)) != 0 ||
-      (ret = mu_body_get_streamref (body, &stream)) != 0)
-    return ret;
-  ret = mu_stream_seek (stream, 0, SEEK_SET, NULL);
-  if (ret)
-    return ret;
-  *pstream = stream;
-  if (*pinfo)
-    {
-      info = *pinfo;
-      info->refcnt++;
-    }
-  else
-    {
-      ret = mu_mime_io_buffer_create (&info);
-      if (ret)
-	return ret;
-    }
-  
-  if (!info->buf && ((info->buf = malloc (info->bufsize)) == NULL))
-    {
-      _attachment_free (info, 0);
-      return ENOMEM;
-    }
-  info->msg = msg;
-  *pinfo = info;
-  return 0;
-}
-
-int
-mu_message_save_attachment (mu_message_t msg, const char *filename,
-			    mu_mime_io_buffer_t info)
-{
-  mu_stream_t istream;
-  int ret;
-  size_t size;
-  size_t nbytes;
-  mu_header_t hdr;
-  const char *fname = NULL;
-  char *partname = NULL;
-
-  if (msg == NULL)
-    return EINVAL;
-
-  if ((ret = _attachment_setup (&info, msg, &istream)) != 0)
-    return ret;
-
-  if (ret == 0 && (ret = mu_message_get_header (msg, &hdr)) == 0)
-    {
-      if (filename == NULL)
-	{
-	  ret = mu_message_aget_decoded_attachment_name (msg, info->charset,
-							 &partname, NULL);
-	  if (partname)
-	    fname = partname;
-	}
-      else
-	fname = filename;
-      if (fname
-	  && (ret =
-	      mu_file_stream_create (&info->fstream, fname,
-				     MU_STREAM_WRITE | MU_STREAM_CREAT)) == 0)
-	{
-	  char *content_encoding;
-	  char *content_encoding_mem = NULL;
-	  
-	  mu_header_get_value (hdr, "Content-Transfer-Encoding", NULL, 0,
-			       &size);
-	  if (size)
-	    {
-	      content_encoding_mem = malloc (size + 1);
-	      if (content_encoding_mem == NULL)
-		ret = ENOMEM;
-	      content_encoding = content_encoding_mem;
-	      mu_header_get_value (hdr, "Content-Transfer-Encoding",
-				   content_encoding, size + 1, 0);
-	    }
-	  else
-	    content_encoding = "7bit";
-	  ret =
-	    mu_filter_create (&info->stream, istream, content_encoding,
-			      MU_FILTER_DECODE,
-			      MU_STREAM_READ);
-	  free (content_encoding_mem);
-	}
-    }
-  if (info->stream && istream)
-    {
-      while (((ret =
-	       mu_stream_read (info->stream, info->buf, BUF_SIZE, 
-			       &nbytes)) == 0 && nbytes))
-	{
-	  if ((ret =
-	       mu_stream_write (info->fstream, info->buf, nbytes, NULL)) != 0)
-	    break;
-	}
-    }
-  if (ret != EAGAIN && info)
-    {
-      mu_stream_close (info->fstream);
-      mu_stream_destroy (&info->stream);
-      mu_stream_destroy (&info->fstream);
-    }
-
-  mu_stream_destroy (&istream);
-  _attachment_free (info, ret); /* FIXME: or 0? */
-  
-  /* Free fname if we allocated it. */
-  if (partname)
-    free (partname);
-
-  return ret;
-}
-
-int
-mu_message_encapsulate (mu_message_t msg, mu_message_t *newmsg,
-			mu_mime_io_buffer_t info)
-{
-  mu_stream_t istream, ostream;
-  const char *header;
-  int ret = 0;
-  size_t nbytes;
-  mu_message_t tmsg = NULL;
-  
-  if (newmsg == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-
-  if (msg == NULL)
-    {
-      mu_header_t hdr;
-      
-      ret = mu_message_create (&tmsg, NULL);
-      if (ret)
-	return ret;
-      msg = tmsg;
-      header =
-	"Content-Type: message/rfc822\nContent-Transfer-Encoding: 7bit\n\n";
-      if ((ret =
-	   mu_header_create (&hdr, header, strlen (header))) == 0)
-	ret = mu_message_set_header (msg, hdr, NULL);
-      if (ret)
-	{
-	  mu_message_destroy (&msg, NULL);
-	  return ret;
-	}
-    }
-      
-  if ((ret = _attachment_setup (&info, msg, &ostream)) != 0)
-    {
-      mu_message_destroy (&tmsg, NULL);
-      return ret;
-    }
-
-  if (ret == 0 && (ret = mu_message_get_streamref (msg, &istream)) == 0)
-    {
-      mu_stream_seek (istream, 0, MU_SEEK_SET, NULL);
-      while (((ret = mu_stream_read (istream, info->buf, BUF_SIZE, 
-				     &nbytes)) == 0 && nbytes))
-	{
-	  if ((ret =
-	       mu_stream_write (ostream, info->buf, nbytes, NULL)) != 0)
-	    break;
-	}
-      mu_stream_destroy (&istream);
-    }
-  if (ret == 0)
-    *newmsg = info->msg;
-  mu_stream_destroy (&ostream);
-  _attachment_free (info, ret && ret != EAGAIN);
-  return ret;
-}
-
-#define MESSAGE_RFC822_STR "message/rfc822"
-
-int
-mu_message_unencapsulate (mu_message_t msg, mu_message_t *newmsg,
-			  mu_mime_io_buffer_t info)
-{
-  size_t size, nbytes;
-  int ret = 0;
-  mu_header_t hdr;
-  mu_stream_t istream, ostream;
-
-  if (msg == NULL)
-    return EINVAL;
-  if (newmsg == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-
-  if (info == NULL /* FIXME: not needed? */
-      && (ret = mu_message_get_header (msg, &hdr)) == 0)
-    {
-      mu_header_get_value (hdr, "Content-Type", NULL, 0, &size);
-      if (size)
-	{
-	  char *content_type;
-	  if ((content_type = malloc (size + 1)) == NULL)
-	    return ENOMEM;
-	  mu_header_get_value (hdr, "Content-Type", content_type, size + 1,
-			       0);
-	  ret = mu_c_strncasecmp (content_type, MESSAGE_RFC822_STR,
-				  sizeof (MESSAGE_RFC822_STR) - 1);
-	  free (content_type);
-	  if (ret != 0)
-	    return EINVAL;
-	}
-      else
-	return EINVAL;
-    }
-  if ((ret = _attachment_setup (&info, msg, &istream)) != 0)
-    return ret;
-  if (info->msg == NULL)
-    ret = mu_message_create (&info->msg, NULL);
-  if (ret == 0)
-    {
-      mu_message_get_streamref (info->msg, &ostream);
-      mu_stream_seek (ostream, 0, MU_SEEK_SET, NULL);
-      while (((ret =
-	       mu_stream_read (istream, info->buf, BUF_SIZE, 
-			       &nbytes)) == 0 && nbytes))
-	{
-	  if ((ret =
-	       mu_stream_write (ostream, info->buf, nbytes, NULL)) != 0)
-	    break;
-	}
-      mu_stream_destroy (&ostream);
-    }
-  if (ret == 0)
-    *newmsg = info->msg;
-  mu_stream_destroy (&istream);
-  _attachment_free (info, ret && ret != EAGAIN);
-  return ret;
-}
diff --git a/libmailutils/attribute.c b/libmailutils/attribute.c
deleted file mode 100644
index 3e76855..0000000
--- a/libmailutils/attribute.c
+++ /dev/null
@@ -1,470 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 1999, 2000, 2001, 2004, 2005, 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 <sys/types.h>
-#include <stdlib.h>
-#include <string.h>
-#include <errno.h>
-
-#ifdef HAVE_STRINGS_H
-# include <strings.h>
-#endif
-
-#include <mailutils/errno.h>
-#include <mailutils/mutil.h>
-#include <mailutils/cstr.h>
-#include <mailutils/sys/attribute.h>
-
-int
-mu_attribute_create (mu_attribute_t *pattr, void *owner)
-{
-  mu_attribute_t attr;
-  if (pattr == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-  attr = calloc (1, sizeof(*attr));
-  if (attr == NULL)
-    return ENOMEM;
-  attr->owner = owner;
-  *pattr = attr;
-  return 0;
-}
-
-void
-mu_attribute_destroy (mu_attribute_t *pattr, void *owner)
-{
-  if (pattr && *pattr)
-    {
-      mu_attribute_t attr = *pattr;
-      if (attr->owner == owner)
-	free (*pattr);
-      /* Loose the link */
-      *pattr = NULL;
-    }
-}
-
-void *
-mu_attribute_get_owner (mu_attribute_t attr)
-{
-  return (attr) ? attr->owner : NULL;
-}
-
-int
-mu_attribute_is_modified (mu_attribute_t attr)
-{
-  return (attr) ? attr->flags & MU_ATTRIBUTE_MODIFIED : 0;
-}
-
-int
-mu_attribute_clear_modified (mu_attribute_t attr)
-{
-  if (attr)
-    attr->flags &= ~MU_ATTRIBUTE_MODIFIED;
-  return 0;
-}
-
-int
-mu_attribute_set_modified (mu_attribute_t attr)
-{
-  if (attr)
-    attr->flags |= MU_ATTRIBUTE_MODIFIED;
-  return 0;
-}
-
-int
-mu_attribute_get_flags (mu_attribute_t attr, int *pflags)
-{
-  if (attr == NULL)
-    return EINVAL;
-  if (pflags == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-  if (attr->_get_flags)
-    return attr->_get_flags (attr, pflags);
-  *pflags = attr->flags;
-  return 0;
-}
-
-int
-mu_attribute_set_flags (mu_attribute_t attr, int flags)
-{
-  int status = 0;
-  int oflags = 0;
-  
-  if (attr == NULL)
-    return EINVAL;
-
-  /* If the required bits are already set, do not modify anything */
-  mu_attribute_get_flags (attr, &oflags);
-  if ((oflags & flags) == flags)
-    return 0;
-  
-  if (attr->_set_flags)
-    status = attr->_set_flags (attr, flags);
-  else
-    attr->flags |= flags;
-  if (status == 0)
-    mu_attribute_set_modified (attr);
-  return 0;
-}
-
-int
-mu_attribute_unset_flags (mu_attribute_t attr, int flags)
-{
-  int status = 0;
-  int oflags = 0;
-
-  if (attr == NULL)
-    return EINVAL;
-
-  /* If the required bits are already cleared, do not modify anything */
-  mu_attribute_get_flags (attr, &oflags);
-  if ((oflags & flags) == 0)
-    return 0;
-
-  if (attr->_unset_flags)
-    status = attr->_unset_flags (attr, flags);
-  else
-    attr->flags &= ~flags;
-  if (status == 0)
-    mu_attribute_set_modified (attr);
-  return 0;
-}
-
-int
-mu_attribute_set_get_flags (mu_attribute_t attr, int (*_get_flags)
-			 (mu_attribute_t, int *), void *owner)
-{
-  if (attr == NULL)
-    return EINVAL;
-  if (attr->owner != owner)
-    return EACCES;
-  attr->_get_flags = _get_flags;
-  return 0;
-}
-
-int
-mu_attribute_set_set_flags (mu_attribute_t attr, int (*_set_flags)
-			 (mu_attribute_t, int), void *owner)
-{
-  if (attr == NULL)
-    return EINVAL;
-  if (attr->owner != owner)
-    return EACCES;
-  attr->_set_flags = _set_flags;
-  return 0;
-}
-
-int
-mu_attribute_set_unset_flags (mu_attribute_t attr, int (*_unset_flags)
-			 (mu_attribute_t, int), void *owner)
-{
-  if (attr == NULL)
-    return EINVAL;
-  if (attr->owner != owner)
-    return EACCES;
-  attr->_unset_flags = _unset_flags;
-  return 0;
-}
-
-/* We add support for "USER" flag, it is a way for external objects
-   Not being the owner to add custom flags.  */
-int
-mu_attribute_set_userflag (mu_attribute_t attr, int flag)
-{
-  if (attr == NULL)
-    return EINVAL;
-  attr->user_flags |= flag;
-  return 0;
-}
-
-int
-mu_attribute_set_seen (mu_attribute_t attr)
-{
-  return mu_attribute_set_flags (attr, MU_ATTRIBUTE_SEEN);
-}
-
-int
-mu_attribute_set_answered (mu_attribute_t attr)
-{
-  return mu_attribute_set_flags (attr, MU_ATTRIBUTE_ANSWERED);
-}
-
-int
-mu_attribute_set_flagged (mu_attribute_t attr)
-{
-  return mu_attribute_set_flags (attr, MU_ATTRIBUTE_FLAGGED);
-}
-
-int
-mu_attribute_set_read (mu_attribute_t attr)
-{
-  return mu_attribute_set_flags (attr, MU_ATTRIBUTE_READ);
-}
-
-int
-mu_attribute_set_deleted (mu_attribute_t attr)
-{
-  return mu_attribute_set_flags (attr, MU_ATTRIBUTE_DELETED);
-}
-
-int
-mu_attribute_set_draft (mu_attribute_t attr)
-{
-  return mu_attribute_set_flags (attr, MU_ATTRIBUTE_DRAFT);
-}
-
-int
-mu_attribute_set_recent (mu_attribute_t attr)
-{
-  int status = mu_attribute_unset_flags (attr, MU_ATTRIBUTE_READ);
-  if (status == 0)
-    status = mu_attribute_unset_flags (attr, MU_ATTRIBUTE_SEEN);
-  return status;
-}
-
-int
-mu_attribute_is_userflag (mu_attribute_t attr, int flag)
-{
-  if (attr == NULL)
-    return 0;
-  return attr->user_flags & flag;
-}
-
-int
-mu_attribute_is_seen (mu_attribute_t attr)
-{
-  int flags = 0;
-  if (mu_attribute_get_flags (attr, &flags) == 0)
-    return flags & MU_ATTRIBUTE_SEEN;
-  return 0;
-}
-
-int
-mu_attribute_is_answered (mu_attribute_t attr)
-{
-  int flags = 0;
-  if (mu_attribute_get_flags (attr, &flags) == 0)
-    return flags & MU_ATTRIBUTE_ANSWERED;
-  return 0;
-}
-
-int
-mu_attribute_is_flagged (mu_attribute_t attr)
-{
-  int flags = 0;
-  if (mu_attribute_get_flags (attr, &flags) == 0)
-    return flags & MU_ATTRIBUTE_FLAGGED;
-  return 0;
-}
-
-int
-mu_attribute_is_read (mu_attribute_t attr)
-{
-  int flags = 0;
-  if (mu_attribute_get_flags (attr, &flags) == 0)
-    return flags & MU_ATTRIBUTE_READ;
-  return 0;
-}
-
-int
-mu_attribute_is_deleted (mu_attribute_t attr)
-{
-  int flags = 0;
-  if (mu_attribute_get_flags (attr, &flags) == 0)
-    return flags & MU_ATTRIBUTE_DELETED;
-  return 0;
-}
-
-int
-mu_attribute_is_draft (mu_attribute_t attr)
-{
-  int flags = 0;
-  if (mu_attribute_get_flags (attr, &flags) == 0)
-    return flags & MU_ATTRIBUTE_DRAFT;
-  return 0;
-}
-
-int
-mu_attribute_is_recent (mu_attribute_t attr)
-{
-  int flags = 0;
-  if (mu_attribute_get_flags (attr, &flags) == 0)
-    return MU_ATTRIBUTE_IS_UNSEEN(flags);
-  return 0;
-}
-
-int
-mu_attribute_unset_userflag (mu_attribute_t attr, int flag)
-{
-  if (attr == NULL)
-    return 0;
-  attr->user_flags &= ~flag;
-  return 0;
-}
-
-int
-mu_attribute_unset_seen (mu_attribute_t attr)
-{
-  return mu_attribute_unset_flags (attr, MU_ATTRIBUTE_SEEN);
-}
-
-int
-mu_attribute_unset_answered (mu_attribute_t attr)
-{
-  return mu_attribute_unset_flags (attr, MU_ATTRIBUTE_ANSWERED);
-}
-
-int
-mu_attribute_unset_flagged (mu_attribute_t attr)
-{
-  return mu_attribute_unset_flags (attr, MU_ATTRIBUTE_FLAGGED);
-}
-
-int
-mu_attribute_unset_read (mu_attribute_t attr)
-{
-  return mu_attribute_unset_flags (attr, MU_ATTRIBUTE_READ);
-}
-
-int
-mu_attribute_unset_deleted (mu_attribute_t attr)
-{
-  return mu_attribute_unset_flags (attr, MU_ATTRIBUTE_DELETED);
-}
-
-int
-mu_attribute_unset_draft (mu_attribute_t attr)
-{
-  return mu_attribute_unset_flags (attr, MU_ATTRIBUTE_DRAFT);
-}
-
-int
-mu_attribute_unset_recent (mu_attribute_t attr)
-{
-  return mu_attribute_unset_flags (attr, MU_ATTRIBUTE_SEEN);
-}
-
-int
-mu_attribute_is_equal (mu_attribute_t attr, mu_attribute_t attr2)
-{
-  int flags2 = 0, flags = 0;
-  mu_attribute_get_flags (attr, &flags);
-  mu_attribute_get_flags (attr2, &flags2);
-  return flags == flags;
-}
-
-/*   Miscellaneous.  */
-int
-mu_attribute_copy (mu_attribute_t dest, mu_attribute_t src)
-{
-  if (dest == NULL || src == NULL)
-    return EINVAL;
-  /* Can not be a deep copy.  */
-  /* memcpy (dest, src, sizeof (*dest)); */
-  dest->flags = src->flags;
-  return 0;
-}
-
-struct flagtrans
-{
-  int flag;
-  char letter;
-};
-
-/* The two macros below are taken from gnulib module verify.h */
-#define mu_verify_true(R) \
-  (!!sizeof \
-   (struct { unsigned int verify_error_if_negative_size__: (R) ? 1 : -1; }))
-#define mu_verify(R) extern int (* verify_function__ (void)) [mu_verify_true (R)]
-
-static struct flagtrans flagtrans[] = {
-  { MU_ATTRIBUTE_SEEN, 'O' },
-  { MU_ATTRIBUTE_ANSWERED, 'A' },
-  { MU_ATTRIBUTE_FLAGGED, 'F' },
-  { MU_ATTRIBUTE_READ, 'R' },
-  { MU_ATTRIBUTE_DELETED, 'd' },
-  { 0 }
-};
-
-/* If cc reports an error in this statement, fix the MU_STATUS_BUF_SIZE
-   declaration in include/mailutils/attribute.h */
-mu_verify (MU_ARRAY_SIZE (flagtrans) == MU_STATUS_BUF_SIZE);
-
-int
-mu_string_to_flags (const char *buffer, int *pflags)
-{
-  const char *sep;
-
-  if (pflags == NULL)
-    return EINVAL;
-
-  /* Set the attribute */
-  if (mu_c_strncasecmp (buffer, "Status:", 7) == 0)
-    {
-      sep = strchr(buffer, ':'); /* pass the ':' */
-      sep++;
-    }
-  else
-    sep = buffer;
-
-  for (; *sep; sep++)
-    {
-      struct flagtrans *ft;
-
-      for (ft = flagtrans; ft->flag; ft++)
-	if (ft->letter == *sep)
-	  {
-	    *pflags |= ft->flag;
-	    break;
-	  }
-    }
-  return 0;
-}
-
-/* NOTE: When adding/removing flags, make sure to update the
-   MU_STATUS_BUF_SIZE define in include/mailutils/attribute.h */
-int
-mu_attribute_to_string (mu_attribute_t attr, char *buffer, size_t len,
-			size_t *pn)
-{
-  int flags = 0;
-  char buf[MU_STATUS_BUF_SIZE];
-  int i;
-  int rc;
-  struct flagtrans *ft;
-  
-  rc = mu_attribute_get_flags (attr, &flags);
-  if (rc)
-    return rc;
-
-  i = 0;
-  for (ft = flagtrans; ft->flag; ft++)
-    if (ft->flag & flags)
-      buf[i++] = ft->letter;
-  buf[i++] = 0;
-
-  i = mu_cpystr (buffer, buf, i);
-  if (pn)
-    *pn = i;
-  return 0;
-}
-
diff --git a/libmailutils/auth.c b/libmailutils/auth.c
deleted file mode 100644
index 2d3e974..0000000
--- a/libmailutils/auth.c
+++ /dev/null
@@ -1,164 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 1999, 2000, 2001, 2004, 2005, 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 <errno.h>
-#include <sys/types.h>
-#include <string.h>
-#include <stdlib.h>
-
-#include <mailutils/errno.h>
-#include <mailutils/sys/auth.h>
-
-static int
-_authenticate_null (mu_authority_t auth MU_ARG_UNUSED)
-{
-  return 0;
-}
-
-int
-mu_authority_create_null (mu_authority_t *pauthority, void *owner)
-{
-  int rc = mu_authority_create(pauthority, NULL, owner);
-  if (rc)
-    return rc;
-  mu_authority_set_authenticate (*pauthority, _authenticate_null,  owner);
-  return 0;
-}
-
-int
-mu_authority_create (mu_authority_t *pauthority, mu_ticket_t ticket, void *owner)
-{
-  mu_authority_t authority;
-  if (pauthority == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-  authority = calloc (1, sizeof (*authority));
-  if (authority == NULL)
-    return ENOMEM;
-  authority->ticket = ticket;
-  authority->owner = owner;
-  *pauthority = authority;
-  return 0;
-}
-
-void
-mu_authority_destroy (mu_authority_t *pauthority, void *owner)
-{
-  if (pauthority && *pauthority)
-    {
-      mu_authority_t authority = *pauthority;
-      if (authority->owner == owner)
-	{
-	  mu_ticket_destroy (&authority->ticket);
-	  mu_list_destroy (&authority->auth_methods);
-	  free (authority);
-	}
-      *pauthority = NULL;
-    }
-}
-
-void *
-mu_authority_get_owner (mu_authority_t authority)
-{
-  return (authority) ? authority->owner : NULL;
-}
-
-int
-mu_authority_set_ticket (mu_authority_t authority, mu_ticket_t ticket)
-{
-  if (authority == NULL)
-    return EINVAL;
-  if (authority->ticket)
-    mu_ticket_destroy (&authority->ticket);
-  authority->ticket = ticket;
-  return 0;
-}
-
-int
-mu_authority_get_ticket (mu_authority_t authority, mu_ticket_t *pticket)
-{
-  if (authority == NULL)
-    return EINVAL;
-  if (pticket == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-  if (authority->ticket == NULL)
-    {
-      int status = mu_ticket_create (&(authority->ticket), authority);
-      if (status != 0)
-	return status;
-    }
-  *pticket = authority->ticket;
-  return 0;
-}
-
-struct auth_cb
-{
-  int status;
-  mu_authority_t authority;
-};
-
-static int
-try_auth (void *item, void *data)
-{
-  int (*authenticate) (mu_authority_t) = item;
-  struct auth_cb *cb = data;
-  if (authenticate (cb->authority) == 0)
-    {
-      cb->status = 0;
-      return 1;
-    }
-  return 0;
-}
-
-int
-mu_authority_authenticate (mu_authority_t authority)
-{
-  if (authority && authority->auth_methods)
-    {
-      struct auth_cb cb;
-      cb.status = MU_ERR_AUTH_FAILURE;
-      cb.authority = authority;
-      mu_list_do (authority->auth_methods, try_auth, &cb);
-      return cb.status;
-    }
-  return EINVAL;
-}
-
-int
-mu_authority_set_authenticate (mu_authority_t authority,
-			    int (*_authenticate) (mu_authority_t),
-			    void *owner)
-{
-  if (authority == NULL)
-    return EINVAL;
-
-  if (authority->owner != owner)
-    return EACCES;
-  if (!authority->auth_methods)
-    {
-      int rc = mu_list_create (&authority->auth_methods);
-      if (rc)
-	return rc;
-    }
-  mu_list_append (authority->auth_methods, _authenticate);
-  return 0;
-}
diff --git a/libmailutils/auth/Makefile.am b/libmailutils/auth/Makefile.am
new file mode 100644
index 0000000..5a797cd
--- /dev/null
+++ b/libmailutils/auth/Makefile.am
@@ -0,0 +1,25 @@
+# GNU Mailutils -- a suite of utilities for electronic mail
+# Copyright (C) 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, see
+# <http://www.gnu.org/licenses/>.
+
+noinst_LTLIBRARIES = libauth.la
+
+libauth_la_SOURCES = \
+ auth.c\
+ mu_auth.c\
+ system.c
+
+INCLUDES = @MU_LIB_COMMON_INCLUDES@ -I/libmailutils
diff --git a/libmailutils/auth/auth.c b/libmailutils/auth/auth.c
new file mode 100644
index 0000000..647e32b
--- /dev/null
+++ b/libmailutils/auth/auth.c
@@ -0,0 +1,163 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 2004, 2005, 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, see
+   <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <errno.h>
+#include <sys/types.h>
+#include <string.h>
+#include <stdlib.h>
+
+#include <mailutils/errno.h>
+#include <mailutils/sys/auth.h>
+
+static int
+_authenticate_null (mu_authority_t auth MU_ARG_UNUSED)
+{
+  return 0;
+}
+
+int
+mu_authority_create_null (mu_authority_t *pauthority, void *owner)
+{
+  int rc = mu_authority_create(pauthority, NULL, owner);
+  if (rc)
+    return rc;
+  mu_authority_set_authenticate (*pauthority, _authenticate_null,  owner);
+  return 0;
+}
+
+int
+mu_authority_create (mu_authority_t *pauthority, mu_ticket_t ticket, void *owner)
+{
+  mu_authority_t authority;
+  if (pauthority == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+  authority = calloc (1, sizeof (*authority));
+  if (authority == NULL)
+    return ENOMEM;
+  authority->ticket = ticket;
+  authority->owner = owner;
+  *pauthority = authority;
+  return 0;
+}
+
+void
+mu_authority_destroy (mu_authority_t *pauthority, void *owner)
+{
+  if (pauthority && *pauthority)
+    {
+      mu_authority_t authority = *pauthority;
+      if (authority->owner == owner)
+	{
+	  mu_ticket_destroy (&authority->ticket);
+	  mu_list_destroy (&authority->auth_methods);
+	  free (authority);
+	}
+      *pauthority = NULL;
+    }
+}
+
+void *
+mu_authority_get_owner (mu_authority_t authority)
+{
+  return (authority) ? authority->owner : NULL;
+}
+
+int
+mu_authority_set_ticket (mu_authority_t authority, mu_ticket_t ticket)
+{
+  if (authority == NULL)
+    return EINVAL;
+  if (authority->ticket)
+    mu_ticket_destroy (&authority->ticket);
+  authority->ticket = ticket;
+  return 0;
+}
+
+int
+mu_authority_get_ticket (mu_authority_t authority, mu_ticket_t *pticket)
+{
+  if (authority == NULL)
+    return EINVAL;
+  if (pticket == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+  if (authority->ticket == NULL)
+    {
+      int status = mu_ticket_create (&(authority->ticket), authority);
+      if (status != 0)
+	return status;
+    }
+  *pticket = authority->ticket;
+  return 0;
+}
+
+struct auth_cb
+{
+  int status;
+  mu_authority_t authority;
+};
+
+static int
+try_auth (void *item, void *data)
+{
+  int (*authenticate) (mu_authority_t) = item;
+  struct auth_cb *cb = data;
+  if (authenticate (cb->authority) == 0)
+    {
+      cb->status = 0;
+      return 1;
+    }
+  return 0;
+}
+
+int
+mu_authority_authenticate (mu_authority_t authority)
+{
+  if (authority && authority->auth_methods)
+    {
+      struct auth_cb cb;
+      cb.status = MU_ERR_AUTH_FAILURE;
+      cb.authority = authority;
+      mu_list_do (authority->auth_methods, try_auth, &cb);
+      return cb.status;
+    }
+  return EINVAL;
+}
+
+int
+mu_authority_set_authenticate (mu_authority_t authority,
+			    int (*_authenticate) (mu_authority_t),
+			    void *owner)
+{
+  if (authority == NULL)
+    return EINVAL;
+
+  if (authority->owner != owner)
+    return EACCES;
+  if (!authority->auth_methods)
+    {
+      int rc = mu_list_create (&authority->auth_methods);
+      if (rc)
+	return rc;
+    }
+  mu_list_append (authority->auth_methods, _authenticate);
+  return 0;
+}
diff --git a/libmailutils/auth/mu_auth.c b/libmailutils/auth/mu_auth.c
new file mode 100644
index 0000000..f9f33ca
--- /dev/null
+++ b/libmailutils/auth/mu_auth.c
@@ -0,0 +1,538 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 2002, 2004, 2005, 2006, 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, see
+   <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <unistd.h>
+#include <sys/types.h>
+#include <pwd.h>
+#ifdef HAVE_SHADOW_H
+# include <shadow.h>
+#endif
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#ifdef HAVE_STRINGS_H
+# include <strings.h>
+#endif
+#ifdef HAVE_SECURITY_PAM_APPL_H
+# include <security/pam_appl.h>
+#endif
+#ifdef HAVE_CRYPT_H
+# include <crypt.h>
+#endif
+
+#include <mailutils/argcv.h>
+#include <mailutils/list.h>
+#include <mailutils/iterator.h>
+#include <mailutils/mailbox.h>
+#include <mailutils/mu_auth.h>
+#include <mailutils/error.h>
+#include <mailutils/errno.h>
+#include <mailutils/nls.h>
+#include <mailutils/debug.h>
+
+static mu_debug_t mu_auth_debug;
+
+#define S(s) ((s) ? (s) : "(none)")
+#define DEBUG_AUTH(a)                                                         \
+     MU_DEBUG11 (mu_auth_debug, MU_DEBUG_TRACE,                               \
+                       "source=%s, name=%s, passwd=%s, uid=%lu, gid=%lu, "    \
+                       "gecos=%s, dir=%s, shell=%s, mailbox=%s, quota=%lu, "  \
+                       "change_uid=%d\n",                                     \
+                       S ((a)->source),                                       \
+                       S ((a)->name),                                         \
+                       S ((a)->passwd),                                       \
+                       (unsigned long) (a)->uid,                              \
+                       (unsigned long) (a)->gid,                              \
+                       S ((a)->gecos),                                        \
+                       S ((a)->dir),                                          \
+                       S ((a)->shell),                                        \
+                       S ((a)->mailbox),                                      \
+                       (unsigned long) (a)->quota,                            \
+                       (a)->change_uid)
+     
+mu_debug_t
+mu_auth_set_debug (mu_debug_t debug)
+{
+  mu_debug_t prev = mu_auth_debug;
+  mu_auth_debug = debug;
+  return prev;
+}
+
+
+/* memory allocation */
+int 
+mu_auth_data_alloc (struct mu_auth_data **ptr,
+                    const char *name,
+                    const char *passwd,
+                    uid_t uid,
+                    gid_t gid,
+                    const char *gecos,
+                    const char *dir,
+                    const char *shell,
+                    const char *mailbox,
+                    int change_uid)
+{
+  size_t size;
+  char *p;
+  char *tmp_mailbox_name = NULL;
+  
+  if (!name)
+    return EINVAL;
+  if (!passwd)
+    passwd = "x";
+  if (!gecos)
+    gecos = "";
+  if (!dir)
+    dir = "/nonexisting";
+  if (!shell)
+    shell = "/dev/null";
+  if (!mailbox)
+    {
+      int rc = mu_construct_user_mailbox_url (&tmp_mailbox_name, name);
+      if (rc)
+	return rc;
+      mailbox = tmp_mailbox_name;
+    }
+
+  size = sizeof (**ptr) +
+         strlen (name) + 1 +
+         strlen (passwd) + 1 +
+         strlen (gecos) + 1 +
+         strlen (dir) + 1 +
+         strlen (shell) + 1 +
+         strlen (mailbox) + 1;
+  
+  *ptr = calloc (1, size);
+  if (!*ptr)
+    return ENOMEM;
+
+  p = (char *)(*ptr + 1);
+  
+#define COPY(f) \
+  (*ptr)->f = p; \
+  strcpy (p, f); \
+  p += strlen (f) + 1
+  
+  COPY (name);
+  COPY (passwd);
+  COPY (gecos);
+  COPY (dir);
+  COPY (shell);
+  COPY (mailbox);
+  (*ptr)->uid = uid;
+  (*ptr)->gid = gid;
+  (*ptr)->change_uid = change_uid;
+
+  free (tmp_mailbox_name);
+  return 0;
+}
+
+void
+mu_auth_data_set_quota (struct mu_auth_data *ptr, mu_off_t q)
+{
+  ptr->flags |= MU_AF_QUOTA;
+  ptr->quota = q;
+}
+
+void
+mu_auth_data_free (struct mu_auth_data *ptr)
+{
+  free (ptr);
+}
+
+void
+mu_auth_data_destroy (struct mu_auth_data **pptr)
+{
+  if (pptr)
+    {
+      mu_auth_data_free (*pptr);
+      *pptr = NULL;
+    }
+}
+
+/* Generic functions */
+
+struct auth_stack_entry {
+  char *name;        /* for diagnostics purposes */
+  mu_auth_fp fun;
+  void *func_data;
+};
+
+void
+mu_insert_stack_entry (mu_list_t *pflist, struct auth_stack_entry *entry)
+{
+  if (!*pflist && mu_list_create (pflist))
+    return;
+  mu_list_append (*pflist, entry);
+}
+
+int
+mu_auth_runlist (mu_list_t flist, struct mu_auth_data **return_data,
+                 const void *key, void *data)
+{
+  int status = MU_ERR_AUTH_FAILURE;
+  int rc;
+  mu_iterator_t itr;
+
+  if (mu_list_get_iterator (flist, &itr) == 0)
+    {
+      struct auth_stack_entry *ep;
+      
+      for (mu_iterator_first (itr); !mu_iterator_is_done (itr);
+           mu_iterator_next (itr))
+        {
+          mu_iterator_current (itr, (void **)&ep);
+          MU_DEBUG1 (mu_auth_debug, MU_DEBUG_TRACE, "Trying %s...", ep->name);
+          rc = ep->fun (return_data, key, ep->func_data, data);
+          MU_DEBUG2 (mu_auth_debug, MU_DEBUG_TRACE, 
+                     "result: %d=%s\n", rc, mu_strerror (rc));
+          if (rc == 0)
+            {
+              if (return_data)
+                {
+                  struct mu_auth_data *auth = *return_data;
+                  if (auth->source == NULL)
+                    auth->source = ep->name;
+                  DEBUG_AUTH (auth);
+                }
+              status = rc;
+              break;
+            }
+          else if (rc == ENOSYS && status != 0)
+	    /* nothing: do not overwrite last meaningful return code */;
+	  else if (status != EAGAIN)
+            status = rc;
+        }
+
+      mu_iterator_destroy (&itr);
+    }
+  return status;
+}
+
+int
+mu_auth_nosupport (struct mu_auth_data **return_data MU_ARG_UNUSED,
+                   const void *key MU_ARG_UNUSED,
+                   void *func_data MU_ARG_UNUSED,
+                   void *call_data MU_ARG_UNUSED)
+{
+  return ENOSYS;
+}
+
+/* II. Authorization: retrieving information about user */
+
+static mu_list_t mu_auth_by_name_list, _tmp_auth_by_name_list;
+static mu_list_t mu_auth_by_uid_list, _tmp_auth_by_uid_list;
+
+int
+mu_get_auth (struct mu_auth_data **auth, enum mu_auth_key_type type,
+             const void *key)
+{
+  mu_list_t list;
+  
+  if (!mu_auth_by_name_list)
+    mu_auth_begin_setup ();
+  switch (type)
+    {
+    case mu_auth_key_name:
+      MU_DEBUG1 (mu_auth_debug, MU_DEBUG_TRACE, 
+                 "Getting auth info for user %s\n", 
+                 (char*) key);
+      list = mu_auth_by_name_list;
+      break;
+
+    case mu_auth_key_uid:
+      MU_DEBUG1 (mu_auth_debug, MU_DEBUG_TRACE, 
+                 "Getting auth info for UID %lu\n",
+	         (unsigned long) *(uid_t*) key);
+      list = mu_auth_by_uid_list;
+      break;
+
+    default:
+      MU_DEBUG1 (mu_auth_debug, MU_DEBUG_ERROR, 
+                 "Unknown mu_auth_key_type: %d\n", type);
+      return EINVAL;
+    }
+  return mu_auth_runlist (list, auth, key, NULL);
+}
+
+struct mu_auth_data *
+mu_get_auth_by_name (const char *username)
+{
+  struct mu_auth_data *auth = NULL;
+  mu_get_auth (&auth, mu_auth_key_name, username);
+  return auth;
+}
+
+struct mu_auth_data *
+mu_get_auth_by_uid (uid_t uid)
+{
+  struct mu_auth_data *auth = NULL;
+  mu_get_auth (&auth, mu_auth_key_uid, &uid);
+  return auth;
+}
+
+/* III. Authentication: determining the authenticity of a user */
+
+static mu_list_t mu_authenticate_list, _tmp_authenticate_list;
+
+int
+mu_authenticate (struct mu_auth_data *auth_data, const char *pass)
+{
+  if (!auth_data)
+    return EINVAL;
+  MU_DEBUG2 (mu_auth_debug, MU_DEBUG_TRACE, 
+             "mu_authenticate, user %s, source %s\n", 
+             auth_data->name, auth_data->source);
+  if (!mu_authenticate_list)
+    mu_auth_begin_setup ();
+  return mu_auth_runlist (mu_authenticate_list, NULL, auth_data, (void*) pass);
+}
+
+
+/* ************************************************************************* */
+
+struct _module_handler {
+  struct auth_stack_entry authenticate;
+  struct auth_stack_entry auth_by_name;
+  struct auth_stack_entry auth_by_uid;
+};
+
+static mu_list_t module_handler_list;
+
+void
+mu_auth_register_module (struct mu_auth_module *mod)
+{
+  struct _module_handler *entry;
+  
+  if (mod->init)
+    mu_gocs_register (mod->name, mod->init);
+  
+  if (!module_handler_list && mu_list_create (&module_handler_list))
+    abort ();
+
+  entry = malloc (sizeof (*entry));
+  if (!entry)
+    {
+      mu_error ("not enough memory");
+      exit (1);
+    }
+  entry->authenticate.name = mod->name;
+  entry->authenticate.fun  = mod->authenticate;
+  entry->authenticate.func_data = mod->authenticate_data;
+  entry->auth_by_name.name = mod->name;
+  entry->auth_by_name.fun = mod->auth_by_name;
+  entry->auth_by_name.func_data = mod->auth_by_name_data;
+  entry->auth_by_uid.name = mod->name;
+  entry->auth_by_uid.fun = mod->auth_by_uid;
+  entry->auth_by_uid.func_data = mod->auth_by_uid_data;
+    
+  mu_list_append (module_handler_list, entry);
+  
+}
+
+static struct _module_handler *
+_locate (const char *name)
+{
+  struct _module_handler *rp = NULL;
+  mu_iterator_t itr;
+
+  if (mu_list_get_iterator (module_handler_list, &itr) == 0)
+    {
+      struct _module_handler *p;
+      
+      for (mu_iterator_first (itr); !rp && !mu_iterator_is_done (itr);
+           mu_iterator_next (itr))
+        {
+          mu_iterator_current (itr, (void **)&p);
+          if (strcmp (p->authenticate.name, name) == 0)
+            rp = p;
+        }
+
+      mu_iterator_destroy (&itr);
+    }
+  return rp;
+}
+
+static void
+_add_module_list (const char *modlist, int (*fun)(const char *name))
+{
+  int argc;
+  char **argv;
+  int rc, i;
+  
+  rc = mu_argcv_get (modlist, ":", NULL, &argc, &argv);
+  if (rc)
+    {
+      mu_error (_("cannot split line `%s': %s"), modlist, mu_strerror (rc));
+      exit (1);
+    }
+
+  for (i = 0; i < argc; i += 2)
+    {
+      if (fun (argv[i]))
+        {
+          /* Historically,auth functions used ENOENT. We support this
+             return value for backward compatibility. */
+          if (errno == ENOENT || errno == MU_ERR_NOENT)
+            mu_error (_("no such module: %s"), argv[i]);
+          else
+            mu_error (_("failed to add module %s: %s"),
+                      argv[i], strerror (errno));
+          exit (1);
+        }
+    }
+  mu_argcv_free (argc, argv);
+}
+
+
+int
+mu_authorization_add_module (const char *name)
+{
+  struct _module_handler *mod = _locate (name);
+  
+  if (!mod)
+    {
+      errno = MU_ERR_NOENT;
+      return 1;
+    }
+  mu_insert_stack_entry (&_tmp_auth_by_name_list, &mod->auth_by_name);
+  mu_insert_stack_entry (&_tmp_auth_by_uid_list, &mod->auth_by_uid);
+  return 0;
+}
+
+void
+mu_authorization_add_module_list (const char *modlist)
+{
+  _add_module_list (modlist, mu_authorization_add_module);
+}
+
+void
+mu_authorization_clear_list ()
+{
+  mu_list_destroy (&_tmp_auth_by_name_list);
+  mu_list_destroy (&_tmp_auth_by_uid_list);
+}
+
+
+int
+mu_authentication_add_module (const char *name)
+{
+  struct _module_handler *mod = _locate (name);
+
+  if (!mod)
+    {
+      errno = MU_ERR_NOENT;
+      return 1;
+    }
+  mu_insert_stack_entry (&_tmp_authenticate_list, &mod->authenticate);
+  return 0;
+}
+
+void
+mu_authentication_add_module_list (const char *modlist)
+{
+  _add_module_list (modlist, mu_authentication_add_module);
+}
+
+void
+mu_authentication_clear_list ()
+{
+  mu_list_destroy (&_tmp_authenticate_list);
+}
+
+
+/* ************************************************************************ */
+
+/* Setup functions. Note that:
+   1) If libmuath is not linked in, then "generic" and "system" modules
+      are used unconditionally. This provides compatibility with the
+      standard getpw.* functions.
+   2) --authentication and --authorization modify only temporary lists,
+      which get flushed to the main ones when mu_auth_finish_setup() is
+      run. Thus, the default "generic:system" remain in force until
+      argp_parse() exits. */
+   
+void
+mu_auth_begin_setup ()
+{
+  mu_iterator_t itr;
+
+  if (!module_handler_list)
+    {
+      mu_auth_register_module (&mu_auth_generic_module); 
+      mu_auth_register_module (&mu_auth_system_module);
+    }
+  
+  if (!mu_authenticate_list)
+    {
+      if (mu_list_get_iterator (module_handler_list, &itr) == 0)
+        {
+          struct _module_handler *mod;
+          
+          for (mu_iterator_first (itr); !mu_iterator_is_done (itr);
+               mu_iterator_next (itr))
+            {
+              mu_iterator_current (itr, (void **)&mod);
+              mu_insert_stack_entry (&mu_authenticate_list,
+                                     &mod->authenticate);
+            }
+          mu_iterator_destroy (&itr);
+        }
+    }
+
+  if (!mu_auth_by_name_list)
+    {
+      if (mu_list_get_iterator (module_handler_list, &itr) == 0)
+        {
+          struct _module_handler *mod;
+          
+          for (mu_iterator_first (itr); !mu_iterator_is_done (itr);
+               mu_iterator_next (itr))
+            {
+              mu_iterator_current (itr, (void **)&mod);
+              mu_insert_stack_entry (&mu_auth_by_name_list, &mod->auth_by_name);
+              mu_insert_stack_entry (&mu_auth_by_uid_list, &mod->auth_by_uid);
+            }
+          mu_iterator_destroy (&itr);
+        }
+    }
+}
+
+void
+mu_auth_finish_setup ()
+{
+  mu_list_destroy (&mu_authenticate_list);
+  mu_list_destroy (&mu_auth_by_name_list);
+  mu_list_destroy (&mu_auth_by_uid_list);
+  
+  mu_authenticate_list = _tmp_authenticate_list;
+  _tmp_authenticate_list = NULL;
+  mu_auth_by_name_list = _tmp_auth_by_name_list;
+  _tmp_auth_by_name_list = NULL;
+  mu_auth_by_uid_list = _tmp_auth_by_uid_list;
+  _tmp_auth_by_uid_list = NULL;
+  
+  mu_auth_begin_setup ();
+}
+
+
diff --git a/libmailutils/auth/system.c b/libmailutils/auth/system.c
new file mode 100644
index 0000000..4e54296
--- /dev/null
+++ b/libmailutils/auth/system.c
@@ -0,0 +1,159 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 2002, 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, see 
+   <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <unistd.h>
+#include <sys/types.h>
+#include <pwd.h>
+#ifdef HAVE_SHADOW_H
+# include <shadow.h>
+#endif
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#ifdef HAVE_STRINGS_H
+# include <strings.h>
+#endif
+#ifdef HAVE_CRYPT_H
+# include <crypt.h>
+#endif
+
+#include <mailutils/list.h>
+#include <mailutils/iterator.h>
+#include <mailutils/mailbox.h>
+#include <mailutils/mu_auth.h>
+#include <mailutils/errno.h>
+
+/* System database */
+static int
+mu_auth_system (struct mu_auth_data **return_data, const struct passwd *pw)
+{
+  char *mailbox_name;
+  int rc;
+  
+  if (!pw)
+    return MU_ERR_AUTH_FAILURE;
+
+  rc = mu_construct_user_mailbox_url (&mailbox_name, pw->pw_name);
+  if (rc)
+    return rc; /* FIXME: Return code is lost */
+  
+  rc = mu_auth_data_alloc (return_data,
+			   pw->pw_name,
+			   pw->pw_passwd,
+			   pw->pw_uid,
+			   pw->pw_gid,
+			   pw->pw_gecos,
+			   pw->pw_dir,
+			   pw->pw_shell,
+			   mailbox_name,
+			   1);
+  free (mailbox_name);
+  return rc;
+}
+
+int
+mu_auth_system_by_name (struct mu_auth_data **return_data,
+			const void *key,
+			void *func_data MU_ARG_UNUSED,
+			void *call_data MU_ARG_UNUSED)
+{
+  if (!key)
+    return EINVAL;
+  return mu_auth_system (return_data, getpwnam (key));
+}
+
+static int
+mu_auth_system_by_uid (struct mu_auth_data **return_data,
+		       const void *key,
+		       void *func_data MU_ARG_UNUSED,
+		       void *call_data MU_ARG_UNUSED)
+{
+  if (!key)
+    return EINVAL;
+  return mu_auth_system (return_data, getpwuid (*(uid_t*) key));
+}
+
+static int
+mu_authenticate_generic (struct mu_auth_data **return_data MU_ARG_UNUSED,
+			 const void *key,
+			 void *func_data MU_ARG_UNUSED,
+			 void *call_data)
+{
+  const struct mu_auth_data *auth_data = key;
+  char *pass = call_data;
+
+  if (!auth_data || !pass)
+    return EINVAL;
+  
+  return auth_data->passwd
+         && strcmp (auth_data->passwd, crypt (pass, auth_data->passwd)) == 0 ?
+          0 : MU_ERR_AUTH_FAILURE;
+}
+
+/* Called only if generic fails */
+static int
+mu_authenticate_system (struct mu_auth_data **return_data MU_ARG_UNUSED,
+			const void *key,
+			void *func_data MU_ARG_UNUSED,
+			void *call_data)
+{
+  const struct mu_auth_data *auth_data = key;
+
+#ifdef HAVE_SHADOW_H
+  char *pass = call_data;
+  
+  if (auth_data)
+    {
+      struct spwd *spw;
+      spw = getspnam (auth_data->name);
+      if (spw)
+	return strcmp (spw->sp_pwdp, crypt (pass, spw->sp_pwdp)) == 0 ?
+	        0 : MU_ERR_AUTH_FAILURE;
+    }
+#endif
+  return MU_ERR_AUTH_FAILURE;
+}
+
+
+struct mu_auth_module mu_auth_system_module = {
+  "system",
+  NULL,
+  mu_authenticate_system,
+  NULL,
+  mu_auth_system_by_name,
+  NULL,
+  mu_auth_system_by_uid,
+  NULL
+};
+
+
+struct mu_auth_module mu_auth_generic_module = {
+  "generic",
+  NULL,
+  mu_authenticate_generic,
+  NULL,
+  mu_auth_nosupport,
+  NULL,
+  mu_auth_nosupport,
+  NULL
+};
+
diff --git a/libmailutils/base/.gitignore b/libmailutils/base/.gitignore
new file mode 100644
index 0000000..e3e0cf0
--- /dev/null
+++ b/libmailutils/base/.gitignore
@@ -0,0 +1 @@
+parsedate.c
diff --git a/libmailutils/base/Makefile.am b/libmailutils/base/Makefile.am
new file mode 100644
index 0000000..4bb7acb
--- /dev/null
+++ b/libmailutils/base/Makefile.am
@@ -0,0 +1,80 @@
+# GNU Mailutils -- a suite of utilities for electronic mail
+# Copyright (C) 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, see
+# <http://www.gnu.org/licenses/>.
+
+noinst_LTLIBRARIES = libbase.la
+
+libbase_la_SOURCES = \
+ alloc.c\
+ amd.c\
+ argcv.c\
+ assoc.c\
+ daemon.c\
+ date.c\
+ fgetpwent.c\
+ freeitem.c\
+ getpass.c\
+ iterator.c\
+ kwd.c\
+ list.c\
+ listlist.c\
+ locale.c\
+ locker.c\
+ mailcap.c\
+ md5.c\
+ monitor.c\
+ munre.c\
+ mutil.c\
+ nls.c\
+ nullrec.c\
+ observer.c\
+ opool.c\
+ parsedate.c\
+ permstr.c\
+ property.c\
+ registrar.c\
+ refcount.c\
+ rfc2047.c\
+ sha1.c\
+ secret.c\
+ ticket.c\
+ url.c\
+ vartab.c\
+ version.c\
+ wicket.c
+
+localedir = $(datadir)/locale
+AM_CPPFLAGS = \
+ -DSYSCONFDIR=\"$(sysconfdir)\"\
+ -DSITE_VIRTUAL_PWDDIR=\"@SITE_VIRTUAL_PWDDIR@\"\
+ -DLOCALEDIR=\"$(localedir)\"
+
+INCLUDES = @MU_LIB_COMMON_INCLUDES@ -I/libmailutils
+
+YLWRAP = $(SHELL) $(mu_aux_dir)/gylwrap
+AM_YFLAGS=-vt
+AM_LFLAGS=-dvp
+
+EXTRA_DIST = parsedate.y
+
+BUILT_SOURCES = parsedate.c
+
+parsedate.c: $(srcdir)/parsedate.y
+	$(YLWRAP) "$(YACC) $(AM_YFLAGS)" $< \
+	          y.tab.c parsedate.c y.output parsedate.y.output \
+                  -- -yy pd_yy
+
+
diff --git a/libmailutils/alloc.c b/libmailutils/base/alloc.c
similarity index 100%
rename from libmailutils/alloc.c
rename to libmailutils/base/alloc.c
diff --git a/libmailutils/base/amd.c b/libmailutils/base/amd.c
new file mode 100644
index 0000000..1a32228
--- /dev/null
+++ b/libmailutils/base/amd.c
@@ -0,0 +1,2032 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
+   2008, 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, see 
+   <http://www.gnu.org/licenses/>. */
+
+/* Mailutils Abstract Mail Directory Layer 
+   First draft by Sergey Poznyakoff.
+   Thanks Tang Yong Ping <yongping.tang@radixs.com> for initial
+   patch (although not used here).
+
+   This module provides basic support for "MH" and "Maildir" formats. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <sys/types.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <time.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <errno.h>
+#include <dirent.h>
+
+#ifdef WITH_PTHREAD
+# ifdef HAVE_PTHREAD_H
+#  ifndef _XOPEN_SOURCE
+#   define _XOPEN_SOURCE  500
+#  endif
+#  include <pthread.h>
+# endif
+#endif
+
+#include <string.h>
+#ifdef HAVE_STRINGS_H
+# include <strings.h>
+#endif
+
+#include <mailutils/cctype.h>
+#include <mailutils/cstr.h>
+#include <mailutils/attribute.h>
+#include <mailutils/body.h>
+#include <mailutils/debug.h>
+#include <mailutils/envelope.h>
+#include <mailutils/error.h>
+#include <mailutils/errno.h>
+#include <mailutils/header.h>
+#include <mailutils/locker.h>
+#include <mailutils/message.h>
+#include <mailutils/mutil.h>
+#include <mailutils/property.h>
+#include <mailutils/stream.h>
+#include <mailutils/url.h>
+#include <mailutils/observer.h>
+#include <mailutils/sys/stream.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);
+static int amd_close (mu_mailbox_t);
+static int amd_get_message (mu_mailbox_t, size_t, mu_message_t *);
+static int amd_quick_get_message (mu_mailbox_t mailbox, mu_message_qid_t qid,
+				  mu_message_t *pmsg);
+static int amd_append_message (mu_mailbox_t, mu_message_t);
+static int amd_messages_count (mu_mailbox_t, size_t *);
+static int amd_messages_recent (mu_mailbox_t, size_t *);
+static int amd_message_unseen (mu_mailbox_t, size_t *);
+static int amd_expunge (mu_mailbox_t);
+static int amd_sync (mu_mailbox_t);
+static int amd_uidnext (mu_mailbox_t mailbox, size_t *puidnext);
+static int amd_uidvalidity (mu_mailbox_t, unsigned long *);
+static int amd_scan (mu_mailbox_t, size_t, size_t *);
+static int amd_is_updated (mu_mailbox_t);
+static int amd_get_size (mu_mailbox_t, mu_off_t *);
+
+static int amd_body_size (mu_body_t body, size_t *psize);
+static int amd_body_lines (mu_body_t body, size_t *plines);
+
+static int amd_header_fill (void *data, char **pbuf, size_t *plen);
+
+static int amd_get_attr_flags (mu_attribute_t attr, int *pflags);
+static int amd_set_attr_flags (mu_attribute_t attr, int flags);
+static int amd_unset_attr_flags (mu_attribute_t attr, int flags);
+
+static void _amd_message_delete (struct _amd_data *amd,
+				 struct _amd_message *msg);
+static int amd_pool_open (struct _amd_message *mhm);
+static int amd_pool_open_count (struct _amd_data *amd);
+static void amd_pool_flush (struct _amd_data *amd);
+static struct _amd_message **amd_pool_lookup (struct _amd_message *mhm);
+
+static int amd_envelope_date (mu_envelope_t envelope, char *buf, size_t len,
+			      size_t *psize);
+static int amd_envelope_sender (mu_envelope_t envelope, char *buf, size_t len,
+			        size_t *psize);
+static int amd_remove_mbox (mu_mailbox_t mailbox);
+
+
+static int amd_body_stream_read (mu_stream_t str, char *buffer,
+				 size_t buflen,
+				 size_t *pnread);
+static int amd_body_stream_readdelim (mu_stream_t is,
+				      char *buffer, size_t buflen,
+				      int delim,
+				      size_t *pnread);
+static int amd_body_stream_size (mu_stream_t str, mu_off_t *psize);
+static int amd_body_stream_seek (mu_stream_t str, mu_off_t off, 
+				 mu_off_t *presult);
+
+struct _amd_body_stream
+{
+  struct _mu_stream stream;
+  mu_body_t body;
+  mu_off_t off;
+};
+
+
+/* Operations on message array */
+
+/* Perform binary search for message MSG on a segment of message array
+   of AMD between the indexes FIRST and LAST inclusively.
+   If found, return 0 and store index of the located entry in the
+   variable PRET. Otherwise, return 1 and place into PRET index of
+   the nearest array element that is less than MSG (in the sense of
+   amd->msg_cmp)
+   Indexes are zero-based. */
+   
+static int
+amd_msg_bsearch (struct _amd_data *amd, mu_off_t first, mu_off_t last,
+		 struct _amd_message *msg,
+		 mu_off_t *pret)
+{
+  mu_off_t mid;
+  int rc;
+
+  if (last < first)
+    return 1;
+  
+  mid = (first + last) / 2;
+  rc = amd->msg_cmp (amd->msg_array[mid], msg);
+  if (rc > 0)
+    return amd_msg_bsearch (amd, first, mid-1, msg, pret);
+  *pret = mid;
+  if (rc < 0)
+    return amd_msg_bsearch (amd, mid+1, last, msg, pret);
+  /* else */
+  return 0;
+}
+
+/* Search for message MSG in the message array of AMD.
+   If found, return 0 and store index of the located entry in the
+   variable PRET. Otherwise, return 1 and place into PRET index of
+   the array element that is less than MSG (in the sense of
+   amd->msg_cmp)
+   Index returned in PRET is 1-based, so *PRET == 0 means that MSG
+   is less than the very first element of the message array.
+
+   In other words, when amd_msg_lookup() returns 1, the value in *PRET
+   can be regarded as a 0-based index of the array slot where MSG can
+   be inserted */
+
+int
+amd_msg_lookup (struct _amd_data *amd, struct _amd_message *msg,
+		 size_t *pret)
+{
+  int rc;
+  mu_off_t i;
+  
+  if (amd->msg_count == 0)
+    {
+      *pret = 0;
+      return 1;
+    }
+  
+  rc = amd->msg_cmp (msg, amd->msg_array[0]);
+  if (rc < 0)
+    {
+      *pret = 0;
+      return 1;
+    }
+  else if (rc == 0)
+    {
+      *pret = 1;
+      return 0;
+    }
+  
+  rc = amd->msg_cmp (msg, amd->msg_array[amd->msg_count - 1]);
+  if (rc > 0)
+    {
+      *pret = amd->msg_count;
+      return 1;
+    }
+  else if (rc == 0)
+    {
+      *pret = amd->msg_count;
+      return 0;
+    }
+  
+  rc = amd_msg_bsearch (amd, 0, amd->msg_count - 1, msg, &i);
+  *pret = i + 1;
+  return rc;
+}
+
+#define AMD_MSG_INC 64
+
+/* Prepare the message array for insertion of a new message
+   at position INDEX (zero based), by moving its contents
+   one slot to the right. If necessary, expand the array by
+   AMD_MSG_INC */
+int
+amd_array_expand (struct _amd_data *amd, size_t index)
+{
+  if (amd->msg_count == amd->msg_max)
+    {
+      struct _amd_message **p;
+      
+      amd->msg_max += AMD_MSG_INC; /* FIXME: configurable? */
+      p = realloc (amd->msg_array, amd->msg_max * amd->msg_size);
+      if (!p)
+	{
+	  amd->msg_max -= AMD_MSG_INC;
+	  return ENOMEM;
+	}
+      amd->msg_array = p;
+    }
+  memmove (&amd->msg_array[index+1], &amd->msg_array[index],
+	   (amd->msg_count-index) * amd->msg_size);
+  amd->msg_count++;
+  return 0;
+}
+
+/* Shrink the message array by removing element at INDEX-1 and
+   shifting left by one position all the elements on the right of
+   it. */
+int
+amd_array_shrink (struct _amd_data *amd, size_t index)
+{
+  memmove (&amd->msg_array[index-1], &amd->msg_array[index],
+	   (amd->msg_count-index) * amd->msg_size);
+  amd->msg_count--;
+  return 0;
+}
+
+
+int
+amd_init_mailbox (mu_mailbox_t mailbox, size_t amd_size,
+		  struct _amd_data **pamd) 
+{
+  int status;
+  struct _amd_data *amd;
+
+  if (mailbox == NULL)
+    return MU_ERR_MBX_NULL;
+  if (amd_size < sizeof (*amd))
+    return EINVAL;
+
+  amd = mailbox->data = calloc (1, amd_size);
+  if (mailbox->data == NULL)
+    return ENOMEM;
+
+  /* Back pointer.  */
+  amd->mailbox = mailbox;
+
+  status = mu_url_aget_path (mailbox->url, &amd->name);
+  if (status)
+    {
+      free (amd);
+      mailbox->data = NULL;
+      return status;
+    }
+
+  /* Overloading the defaults.  */
+  mailbox->_destroy = amd_destroy;
+
+  mailbox->_open = amd_open;
+  mailbox->_close = amd_close;
+
+  /* Overloading of the entire mailbox object methods.  */
+  mailbox->_get_message = amd_get_message;
+  mailbox->_quick_get_message = amd_quick_get_message;
+  mailbox->_append_message = amd_append_message;
+  mailbox->_messages_count = amd_messages_count;
+  mailbox->_messages_recent = amd_messages_recent;
+  mailbox->_message_unseen = amd_message_unseen;
+  mailbox->_expunge = amd_expunge;
+  mailbox->_sync = amd_sync;
+  mailbox->_uidvalidity = amd_uidvalidity;
+  mailbox->_uidnext = amd_uidnext;
+
+  mailbox->_scan = amd_scan;
+  mailbox->_is_updated = amd_is_updated;
+
+  mailbox->_get_size = amd_get_size;
+  mailbox->_remove = amd_remove_mbox;
+  
+  MU_DEBUG1 (mailbox->debug, MU_DEBUG_TRACE1, "amd_init(%s)\n", amd->name);
+  *pamd = amd;
+  return 0;
+}
+
+static void
+amd_destroy (mu_mailbox_t mailbox)
+{
+  struct _amd_data *amd = mailbox->data;
+  size_t i;
+  
+  if (!amd)
+    return;
+
+  amd_pool_flush (amd);
+  mu_monitor_wrlock (mailbox->monitor);
+  for (i = 0; i < amd->msg_count; i++)
+    {
+      mu_message_destroy (&amd->msg_array[i]->message, amd->msg_array[i]);
+      free (amd->msg_array[i]);
+    }
+  free (amd->msg_array);
+	
+  if (amd->name)
+    free (amd->name);
+
+  free (amd);
+  mailbox->data = NULL;
+  mu_monitor_unlock (mailbox->monitor);
+}
+
+static int
+amd_open (mu_mailbox_t mailbox, int flags)
+{
+  struct _amd_data *amd = mailbox->data;
+  struct stat st;
+
+  mailbox->flags = flags;
+  if (stat (amd->name, &st) < 0)
+    {
+      if ((flags & MU_STREAM_CREAT) && errno == ENOENT)
+	{
+	  int rc;
+	  int perms = mu_stream_flags_to_mode (flags, 1);
+	  if (mkdir (amd->name, S_IRUSR|S_IWUSR|S_IXUSR|perms))
+	    return errno;
+	  if (stat (amd->name, &st) < 0)
+	    return errno;
+	  if (amd->create && (rc = amd->create (amd, flags)))
+	    return rc;
+	}
+      else
+	return errno;
+    }
+  
+  if (!S_ISDIR (st.st_mode))
+    return EINVAL;
+
+  if (mailbox->locker == NULL)
+    mu_locker_create (&mailbox->locker, "/dev/null", 0);
+  
+  return 0;
+}
+
+static int
+amd_close (mu_mailbox_t mailbox)
+{
+  struct _amd_data *amd;
+  int i;
+    
+  if (!mailbox)
+    return MU_ERR_MBX_NULL;
+
+  amd = mailbox->data;
+  
+  /* Destroy all cached data */
+  amd_pool_flush (amd);
+  mu_monitor_wrlock (mailbox->monitor);
+  for (i = 0; i < amd->msg_count; i++)
+    {
+      mu_message_destroy (&amd->msg_array[i]->message, amd->msg_array[i]);
+      free (amd->msg_array[i]);
+    }
+  free (amd->msg_array);
+  amd->msg_array = NULL;
+
+  amd->msg_count = 0; /* number of messages in the list */
+  amd->msg_max = 0;   /* maximum message buffer capacity */
+
+  amd->uidvalidity = 0;
+  mu_monitor_unlock (mailbox->monitor);
+  
+  return 0;
+}
+
+static int
+amd_message_qid (mu_message_t msg, mu_message_qid_t *pqid)
+{
+  struct _amd_message *mhm = mu_message_get_owner (msg);
+  
+  return mhm->amd->cur_msg_file_name (mhm, pqid);
+}
+
+struct _amd_message *
+_amd_get_message (struct _amd_data *amd, size_t msgno)
+{
+  msgno--;
+  if (msgno >= amd->msg_count)
+    return NULL;
+  return amd->msg_array[msgno];
+}
+
+static int
+_amd_attach_message (mu_mailbox_t mailbox, struct _amd_message *mhm,
+		     mu_message_t *pmsg)
+{
+  int status;
+  mu_message_t msg;
+
+  /* Check if we already have it.  */
+  if (mhm->message)
+    {
+      if (pmsg)
+	*pmsg = mhm->message;
+      return 0;
+    }
+
+  /* Get an empty message struct.  */
+  status = mu_message_create (&msg, mhm);
+  if (status != 0)
+    return status;
+
+  /* Set the header.  */
+  {
+    mu_header_t header = NULL;
+    status = mu_header_create (&header, NULL, 0);
+    if (status != 0)
+      {
+	mu_message_destroy (&msg, mhm);
+	return status;
+      }
+    mu_header_set_fill (header, amd_header_fill, msg);
+    /*FIXME:
+    mu_header_set_get_fvalue (header, amd_header_get_fvalue, msg);
+    */
+    mu_message_set_header (msg, header, mhm);
+  }
+
+  /* Set the attribute.  */
+  {
+    mu_attribute_t attribute;
+    status = mu_attribute_create (&attribute, msg);
+    if (status != 0)
+      {
+	mu_message_destroy (&msg, mhm);
+	return status;
+      }
+    mu_attribute_set_get_flags (attribute, amd_get_attr_flags, msg);
+    mu_attribute_set_set_flags (attribute, amd_set_attr_flags, msg);
+    mu_attribute_set_unset_flags (attribute, amd_unset_attr_flags, msg);
+    mu_message_set_attribute (msg, attribute, mhm);
+  }
+
+  /* Prepare the body.  */
+  {
+    mu_body_t body = NULL;
+    struct _amd_body_stream *str;
+    
+    if ((status = mu_body_create (&body, msg)) != 0)
+      return status;
+
+    str = (struct _amd_body_stream *)
+              _mu_stream_create (sizeof (*str),
+				 mailbox->flags | MU_STREAM_SEEK | 
+				 _MU_STR_OPEN);
+    if (!str)
+      {
+	mu_body_destroy (&body, msg);
+	mu_message_destroy (&msg, mhm);
+	return ENOMEM;
+      }
+    str->stream.read = amd_body_stream_read;
+    str->stream.readdelim = amd_body_stream_readdelim;
+    str->stream.size = amd_body_stream_size;
+    str->stream.seek = amd_body_stream_seek;
+    mu_body_set_stream (body, (mu_stream_t) str, msg);
+    mu_body_clear_modified (body);
+    mu_body_set_size (body, amd_body_size, msg);
+    mu_body_set_lines (body, amd_body_lines, msg);
+    mu_message_set_body (msg, body, mhm);
+    str->body = body;
+  }
+
+  /* Set the envelope.  */
+  {
+    mu_envelope_t envelope = NULL;
+    status = mu_envelope_create (&envelope, msg);
+    if (status != 0)
+      {
+	mu_message_destroy (&msg, mhm);
+	return status;
+      }
+    mu_envelope_set_sender (envelope, amd_envelope_sender, msg);
+    mu_envelope_set_date (envelope, amd_envelope_date, msg);
+    mu_message_set_envelope (msg, envelope, mhm);
+  }
+
+  /* Set the UID.  */
+  if (mhm->amd->message_uid)
+    mu_message_set_uid (msg, mhm->amd->message_uid, mhm);
+  mu_message_set_qid (msg, amd_message_qid, mhm);
+  
+  /* Attach the message to the mailbox mbox data.  */
+  mhm->message = msg;
+  mu_message_set_mailbox (msg, mailbox, mhm);
+
+  /* Some of mu_message_set_ functions above mark message as modified.
+     Undo it now.
+
+     FIXME: Marking message as modified is not always appropriate. Find
+     a better way. */
+     
+  mu_message_clear_modified (msg);
+
+  if (pmsg)
+    *pmsg = msg;
+
+  return 0;
+}
+
+static int
+amd_get_message (mu_mailbox_t mailbox, size_t msgno, mu_message_t *pmsg)
+{
+  int status;
+  struct _amd_data *amd = mailbox->data;
+  struct _amd_message *mhm;
+
+  /* Sanity checks.  */
+  if (pmsg == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+  if (amd == NULL)
+    return EINVAL;
+
+  /* If we did not start a scanning yet do it now.  */
+  if (amd->msg_count == 0)
+    {
+      status = amd->scan0 (mailbox, 1, NULL, 0);
+      if (status != 0)
+	return status;
+    }
+
+  if ((mhm = _amd_get_message (amd, msgno)) == NULL)
+    return EINVAL;
+  return _amd_attach_message (mailbox, mhm, pmsg);
+}
+
+static int
+amd_quick_get_message (mu_mailbox_t mailbox, mu_message_qid_t qid,
+		       mu_message_t *pmsg)
+{
+  int status;
+  struct _amd_data *amd = mailbox->data;
+  if (amd->msg_count)
+    {
+      mu_message_qid_t vqid;
+      mu_message_t msg = amd->msg_array[0]->message;
+      status = mu_message_get_qid (msg, &vqid);
+      if (status)
+	return status;
+      status = strcmp (qid, vqid);
+      free (vqid);
+      if (status)
+	return MU_ERR_EXISTS;
+      *pmsg = msg;
+    }
+  else if (amd->qfetch)
+    {
+      status = amd->qfetch (amd, qid);
+      if (status)
+	return status;
+      return _amd_attach_message (mailbox, amd->msg_array[0], pmsg);
+    }
+  
+  return ENOSYS;
+}
+
+static FILE *
+_amd_tempfile(struct _amd_data *amd, char **namep)
+{
+  int fd = mu_tempfile (amd->name, namep);
+  if (fd == -1)
+      return NULL;
+  return fdopen (fd, "w");
+}
+
+static int
+_amd_delim (char *str)
+{
+  if (str[0] == '-')
+    {
+      for (; *str == '-'; str++)
+	;
+      for (; *str == ' ' || *str == '\t'; str++)
+	;
+    }
+  return str[0] == '\n';
+}
+
+static int
+_amd_message_save (struct _amd_data *amd, struct _amd_message *mhm,
+		   int expunge)
+{
+  mu_stream_t stream = NULL;
+  char *name = NULL, *buf = NULL, *msg_name, *old_name;
+  size_t n;
+  size_t bsize;
+  size_t nlines, nbytes;
+  size_t new_body_start, new_header_lines;
+  FILE *fp;
+  mu_message_t msg = mhm->message;
+  mu_header_t hdr;
+  int status;
+  mu_attribute_t attr;
+  mu_body_t body;
+  const char *sbuf;
+  mu_envelope_t env = NULL;
+  char statbuf[MU_STATUS_BUF_SIZE];
+
+  status = mu_message_size (msg, &bsize);
+  if (status)
+    return status;
+
+  status = amd->new_msg_file_name (mhm, mhm->attr_flags, expunge, &msg_name);
+  if (status)
+    return status;
+  if (!msg_name)
+    {
+      /* Unlink the original file */
+      char *old_name;
+      status = amd->cur_msg_file_name (mhm, &old_name);
+      free (msg_name);
+      if (status == 0 && unlink (old_name))
+	status = errno;
+      free (old_name);
+      return status;
+    }      
+    
+  fp = _amd_tempfile (mhm->amd, &name);
+  if (!fp)
+    {
+      free (msg_name);
+      return errno;
+    }
+
+  /* Try to allocate large buffer */
+  for (; bsize > 1; bsize /= 2)
+    if ((buf = malloc (bsize)))
+      break;
+
+  if (!bsize)
+    {
+      unlink (name);
+      free (name);
+      free (msg_name);
+      return ENOMEM;
+    }
+
+  /* Copy flags */
+  mu_message_get_header (msg, &hdr);
+  mu_header_get_streamref (hdr, &stream);
+  status = mu_stream_seek (stream, 0, MU_SEEK_SET, NULL);
+  if (status)
+    {
+      /* FIXME: Provide a common exit point for all error
+	 cases */
+      unlink (name);
+      free (name);
+      free (msg_name);
+      mu_stream_destroy (&stream);
+      return status;
+    }
+      
+  nlines = nbytes = 0;
+  while ((status = mu_stream_readline (stream, buf, bsize, &n)) == 0
+	 && n != 0)
+    {
+      if (_amd_delim (buf))
+	break;
+
+      if (!(mu_c_strncasecmp (buf, "status:", 7) == 0
+	    || mu_c_strncasecmp (buf, "x-imapbase:", 11) == 0
+	    || mu_c_strncasecmp (buf, "x-uid:", 6) == 0
+	    || mu_c_strncasecmp (buf, 
+                MU_HEADER_ENV_DATE ":", sizeof (MU_HEADER_ENV_DATE)) == 0
+	    || mu_c_strncasecmp (buf, 
+                MU_HEADER_ENV_SENDER ":", sizeof (MU_HEADER_ENV_SENDER)) == 0))
+	{
+	  nlines++;
+	  nbytes += fprintf (fp, "%s", buf);
+	}
+    }
+  mu_stream_destroy (&stream);
+  
+  /* Add imapbase */
+  if (!(amd->mailbox->flags & MU_STREAM_APPEND)
+      && amd->next_uid
+      && (!amd->msg_array || (amd->msg_array[0] == mhm))) /*FIXME*/
+    {
+      nbytes += fprintf (fp, "X-IMAPbase: %lu %u\n",
+			 (unsigned long) amd->uidvalidity,
+			 (unsigned) amd->next_uid (amd));
+      nlines++;
+    }
+  
+  mu_message_get_envelope (msg, &env);
+  if (mu_envelope_sget_date (env, &sbuf) == 0)
+    {
+      /* NOTE: buffer might be terminated with \n */
+      while (*sbuf && mu_isspace (*sbuf))
+	sbuf++;
+      nbytes += fprintf (fp, "%s: %s", MU_HEADER_ENV_DATE, sbuf);
+
+      if (*sbuf && sbuf[strlen (sbuf) - 1] != '\n')
+	nbytes += fprintf (fp, "\n");
+      
+      nlines++;
+    }
+	  
+  if (mu_envelope_sget_sender (env, &sbuf) == 0)
+    {
+      fprintf (fp, "%s: %s\n", MU_HEADER_ENV_SENDER, sbuf);
+      nlines++;
+    }
+  
+  /* Add status */
+  mu_message_get_attribute (msg, &attr);
+  mu_attribute_to_string (attr, statbuf, sizeof (statbuf), &n);
+  if (n)
+    {
+      nbytes += fprintf (fp, "Status: %s\n", statbuf);
+      nlines++;
+    }
+  nbytes += fprintf (fp, "\n");
+  nlines++;
+  
+  new_header_lines = nlines;
+  new_body_start = nbytes;
+
+  /* Copy message body */
+
+  mu_message_get_body (msg, &body);
+  mu_body_get_streamref (body, &stream);
+  status = mu_stream_seek (stream, 0, MU_SEEK_SET, NULL);
+  if (status)
+    {
+      unlink (name);
+      free (name);
+      free (msg_name);
+      mu_stream_destroy (&stream);
+      return status;
+    }
+    
+  nlines = 0;
+  while (mu_stream_read (stream, buf, bsize, &n) == 0 && n != 0)
+    {
+      char *p;
+      for (p = buf; p < buf + n; p++)
+	if (*p == '\n')
+	  nlines++;
+      fwrite (buf, 1, n, fp);
+      nbytes += n;
+    }
+  mu_stream_destroy (&stream);
+  
+  mhm->header_lines = new_header_lines;
+  mhm->body_start = new_body_start;
+  mhm->body_lines = nlines;
+  mhm->body_end = nbytes;
+
+  free (buf);
+  fclose (fp);
+
+  status = amd->cur_msg_file_name (mhm, &old_name);
+  if (status == 0)
+    {
+      if (rename (name, msg_name))
+	status = errno;
+      else
+	{
+	  mode_t perms;
+	  
+	  perms = mu_stream_flags_to_mode (amd->mailbox->flags, 0);
+	  if (perms != 0)
+	    {
+	      /* It is documented that the mailbox permissions are
+		 affected by the current umask, so take it into account
+		 here.
+		 FIXME: I'm still not sure we should honor umask, though.
+		 --gray
+	      */
+	      mode_t mask = umask (0);
+	      chmod (msg_name, (0600 | perms) & ~mask);
+	      umask (mask);
+	    }
+	  if (strcmp (old_name, msg_name))
+	    /* Unlink original message */
+	    unlink (old_name);
+	}
+      free (old_name);
+      
+      mhm->orig_flags = mhm->attr_flags;
+    }
+  free (msg_name);
+  free (name);
+
+  return status;
+}
+
+static int
+amd_append_message (mu_mailbox_t mailbox, mu_message_t msg)
+{
+  int status;
+  struct _amd_data *amd = mailbox->data;
+  struct _amd_message *mhm;
+
+  if (!mailbox)
+    return MU_ERR_MBX_NULL;
+  if (!msg)
+    return EINVAL;
+
+  mhm = calloc (1, amd->msg_size);
+  if (!mhm)
+    return ENOMEM;
+    
+  /* If we did not start a scanning yet do it now.  */
+  if (amd->msg_count == 0)
+    {
+      status = amd->scan0 (mailbox, 1, NULL, 0);
+      if (status != 0)
+	{
+	  free (mhm);
+	  return status;
+	}
+    }
+
+  amd->has_new_msg = 1;
+  
+  mhm->amd = amd;
+  if (amd->msg_init_delivery)
+    {
+      status = amd->msg_init_delivery (amd, mhm);
+      if (status)
+	{
+	  free (mhm);
+	  return status;
+	}
+    }
+  
+  mhm->message = msg;
+  status = _amd_message_save (amd, mhm, 0);
+  if (status)
+    {
+      free (mhm);
+      return status;
+    }
+
+  mhm->message = NULL;
+  /* Insert and re-scan the message */
+  status = _amd_message_insert (amd, mhm);
+  if (status)
+    {
+      free (mhm);
+      return status;
+    }
+
+  if (amd->msg_finish_delivery)
+    status = amd->msg_finish_delivery (amd, mhm, msg);
+  
+  if (status == 0 && mailbox->observable)
+    {
+      char *qid;
+      if (amd->cur_msg_file_name (mhm, &qid) == 0)
+	{
+	  mu_observable_notify (mailbox->observable, MU_EVT_MESSAGE_APPEND,
+				qid);
+	  free (qid);
+	}
+    }
+  
+  return status;
+}
+
+static int
+amd_messages_count (mu_mailbox_t mailbox, size_t *pcount)
+{
+  struct _amd_data *amd = mailbox->data;
+
+  if (amd == NULL)
+    return EINVAL;
+
+  if (!amd_is_updated (mailbox))
+    return amd->scan0 (mailbox,  amd->msg_count, pcount, 0);
+
+  if (pcount)
+    *pcount = amd->msg_count;
+
+  return 0;
+}
+
+/* A "recent" message is the one not marked with MU_ATTRIBUTE_SEEN
+   ('O' in the Status header), i.e. a message that is first seen
+   by the current session (see attributes.h) */
+static int
+amd_messages_recent (mu_mailbox_t mailbox, size_t *pcount)
+{
+  struct _amd_data *amd = mailbox->data;
+  size_t count, i;
+
+  /* If we did not start a scanning yet do it now.  */
+  if (amd->msg_count == 0)
+    {
+      int status = amd->scan0 (mailbox, 1, NULL, 0);
+      if (status != 0)
+	return status;
+    }
+  count = 0;
+  for (i = 0; i < amd->msg_count; i++)
+    {
+      if (MU_ATTRIBUTE_IS_UNSEEN(amd->msg_array[i]->attr_flags))
+	count++;
+    }
+  *pcount = count;
+  return 0;
+}
+
+/* An "unseen" message is the one that has not been read yet */
+static int
+amd_message_unseen (mu_mailbox_t mailbox, size_t *pmsgno)
+{
+  struct _amd_data *amd = mailbox->data;
+  size_t i;
+
+  /* If we did not start a scanning yet do it now.  */
+  if (amd->msg_count == 0)
+    {
+      int status = amd->scan0 (mailbox, 1, NULL, 0);
+      if (status != 0)
+	return status;
+    }
+
+  for (i = 0; i < amd->msg_count; i++)
+    {
+      if (MU_ATTRIBUTE_IS_UNREAD(amd->msg_array[0]->attr_flags))
+	{
+	  *pmsgno = i + 1;
+	  break;
+	}
+    }
+  return 0;
+}
+
+static char *
+make_size_file_name (struct _amd_data *amd)
+{
+  size_t size = strlen (amd->name) + 1 + sizeof (MU_AMD_SIZE_FILE_NAME);
+  char *name = malloc (size);
+  if (name)
+    {
+      strcpy (name, amd->name);
+      strcat (name, "/");
+      strcat (name, MU_AMD_SIZE_FILE_NAME);
+    }
+  return name;
+}
+
+static int
+read_size_file (struct _amd_data *amd, mu_off_t *psize)
+{
+  FILE *fp;
+  int rc;
+  char *name = make_size_file_name (amd);
+  if (!name)
+    return 1;
+  fp = fopen (name, "r");
+  if (fp)
+    {
+      unsigned long size;
+      if (fscanf (fp, "%lu", &size) == 1)
+	{
+	  *psize = size;
+	  rc = 0;
+	}
+      else
+	rc = 1;
+      fclose (fp);
+    }
+  else
+    rc = 1;
+  free (name);
+  return rc;
+}
+
+static int
+write_size_file (struct _amd_data *amd, mu_off_t size)
+{
+  FILE *fp;
+  int rc;
+  char *name = make_size_file_name (amd);
+  if (!name)
+    return 1;
+  fp = fopen (name, "w");
+  if (fp)
+    {
+      fprintf (fp, "%lu", (unsigned long) size);
+      fclose (fp);
+      rc = 0;
+    }
+  else
+    rc = 1;
+  free (name);
+  return rc;
+}
+      
+static int
+compute_mailbox_size (struct _amd_data *amd, const char *name, mu_off_t *psize)
+{
+  DIR *dir;
+  struct dirent *entry;
+  char *buf;
+  size_t bufsize;
+  size_t dirlen;
+  size_t flen;
+  int status = 0;
+  struct stat sb;
+
+  dir = opendir (name);
+  if (!dir)
+    return errno;
+
+  dirlen = strlen (name);
+  bufsize = dirlen + 32;
+  buf = malloc (bufsize);
+  if (!buf)
+    {
+      closedir (dir);
+      return ENOMEM;
+    }
+  
+  strcpy (buf, name);
+  if (buf[dirlen-1] != '/')
+    buf[++dirlen - 1] = '/';
+	  
+  while ((entry = readdir (dir)))
+    {
+      switch (entry->d_name[0])
+	{
+	case '.':
+	  break;
+
+	default:
+	  flen = strlen (entry->d_name);
+	  if (dirlen + flen + 1 > bufsize)
+	    {
+	      bufsize = dirlen + flen + 1;
+	      buf = realloc (buf, bufsize);
+	      if (!buf)
+		{
+		  status = ENOMEM;
+		  break;
+		}
+	    }
+	  strcpy (buf + dirlen, entry->d_name);
+	  if (stat (buf, &sb) == 0)
+	    {
+	      if (S_ISREG (sb.st_mode))
+		*psize += sb.st_size;
+	      else if (S_ISDIR (sb.st_mode))
+		compute_mailbox_size (amd, buf, psize);
+	    }
+	  /* FIXME: else? */
+	  break;
+	}
+    }
+
+  free (buf);
+  
+  closedir (dir);
+  return 0;
+}
+
+static int
+amd_remove_mbox (mu_mailbox_t mailbox)
+{
+  int rc;
+  struct _amd_data *amd = mailbox->data;
+  
+  if (!amd->remove)
+    return ENOSYS;
+  rc = amd->remove (amd);
+  if (rc == 0)
+    {
+      char *name = make_size_file_name (amd);
+      if (!name)
+	return ENOMEM;
+      if (unlink (name) && errno != ENOENT)
+	rc = errno;
+      free (name);
+    }
+
+  if (rc == 0)
+    {
+      if (rmdir (amd->name) && errno != ENOENT)
+	{
+	  rc = errno;
+	  /* POSIX.1-2001 allows EEXIST to be returned if the directory
+	     contained entries other than . and .. */
+	  if (rc == EEXIST)
+	    rc = ENOTEMPTY;
+	}
+    }
+  
+  return rc;
+}
+
+static int
+amd_expunge (mu_mailbox_t mailbox)
+{
+  struct _amd_data *amd = mailbox->data;
+  struct _amd_message *mhm;
+  size_t i;
+  int updated = amd->has_new_msg;
+  
+  if (amd == NULL)
+    return EINVAL;
+
+  if (amd->msg_count == 0)
+    return 0;
+
+  /* Find the first dirty(modified) message.  */
+  for (i = 0; i < amd->msg_count; i++)
+    {
+      mhm = amd->msg_array[i];
+      if ((mhm->attr_flags & MU_ATTRIBUTE_MODIFIED) ||
+	  (mhm->attr_flags & MU_ATTRIBUTE_DELETED) ||
+	  (mhm->message && mu_message_is_modified (mhm->message)))
+	break;
+    }
+
+  while (i < amd->msg_count)
+    {
+      mhm = amd->msg_array[i];
+      
+      if (mhm->attr_flags & MU_ATTRIBUTE_DELETED)
+	{
+	  int rc;
+	  char *old_name;
+	  char *new_name;
+
+	  rc = amd->cur_msg_file_name (mhm, &old_name);
+	  if (rc)
+	    return rc;
+	  rc = amd->new_msg_file_name (mhm, mhm->attr_flags, 1,
+				       &new_name);
+	  if (rc)
+	    {
+	      free (old_name);
+	      return rc;
+	    }
+
+	  if (new_name)
+	    {
+	      /* FIXME: It may be a good idea to have a capability flag
+		 in struct _amd_data indicating that no actual removal
+		 is needed (e.g. for traditional MH). It will allow to
+		 bypass lots of no-op code here. */
+	      if (strcmp (old_name, new_name))
+		/* Rename original message */
+		rename (old_name, new_name);
+	    }
+	  else
+	    /* Unlink original file */
+	    unlink (old_name);
+	  
+	  free (old_name);
+	  free (new_name);
+
+	  _amd_message_delete (amd, mhm);
+	  updated = 1;
+	  /* Do not increase i! */
+	}
+      else
+	{
+	  if ((mhm->attr_flags & MU_ATTRIBUTE_MODIFIED)
+	      || (mhm->message && mu_message_is_modified (mhm->message)))
+	    {
+	      _amd_attach_message (mailbox, mhm, NULL);
+	      _amd_message_save (amd, mhm, 1);
+	      updated = 1;
+	    }
+	  i++; /* Move to the next message */
+	}
+    }
+
+  if (updated && !amd->mailbox_size)
+    {
+      mu_off_t size = 0;
+      int rc = compute_mailbox_size (amd, amd->name, &size);
+      if (rc == 0)
+	write_size_file (amd, size);
+    }
+  return 0;
+}
+
+static int
+amd_sync (mu_mailbox_t mailbox)
+{
+  struct _amd_data *amd = mailbox->data;
+  struct _amd_message *mhm;
+  size_t i;
+  int updated = amd->has_new_msg;
+  
+  if (amd == NULL)
+    return EINVAL;
+
+  if (amd->msg_count == 0)
+    return 0;
+
+  /* Find the first dirty(modified) message.  */
+  for (i = 0; i < amd->msg_count; i++)
+    {
+      mhm = amd->msg_array[i];
+      if ((mhm->attr_flags & MU_ATTRIBUTE_MODIFIED)
+	  || (mhm->message && mu_message_is_modified (mhm->message)))
+	break;
+    }
+
+  for ( ; i < amd->msg_count; i++)
+    {
+      mhm = amd->msg_array[i];
+
+      if ((mhm->attr_flags & MU_ATTRIBUTE_MODIFIED)
+	  || (mhm->message && mu_message_is_modified (mhm->message)))
+	{
+	  _amd_attach_message (mailbox, mhm, NULL);
+	  _amd_message_save (amd, mhm, 0);
+	  updated = 1;
+	}
+    }
+
+  if (updated && !amd->mailbox_size)
+    {
+      mu_off_t size = 0;
+      int rc = compute_mailbox_size (amd, amd->name, &size);
+      if (rc == 0)
+	write_size_file (amd, size);
+    }
+
+  return 0;
+}
+
+static int
+amd_uidvalidity (mu_mailbox_t mailbox, unsigned long *puidvalidity)
+{
+  struct _amd_data *amd = mailbox->data;
+  int status = amd_messages_count (mailbox, NULL);
+  if (status != 0)
+    return status;
+  /* If we did not start a scanning yet do it now.  */
+  if (amd->msg_count == 0)
+    {
+      status = amd->scan0 (mailbox, 1, NULL, 0);
+      if (status != 0)
+	return status;
+    }
+  if (puidvalidity)
+    *puidvalidity = amd->uidvalidity;
+  return 0;
+}
+
+static int
+amd_uidnext (mu_mailbox_t mailbox, size_t *puidnext)
+{
+  struct _amd_data *amd = mailbox->data;
+  int status;
+  
+  if (!amd->next_uid)
+    return ENOSYS;
+  status = mu_mailbox_messages_count (mailbox, NULL);
+  if (status != 0)
+    return status;
+  /* If we did not start a scanning yet do it now.  */
+  if (amd->msg_count == 0)
+    {
+      status = amd->scan0 (mailbox, 1, NULL, 0);
+      if (status != 0)
+	return status;
+    }
+   if (puidnext)
+     *puidnext = amd->next_uid (amd);
+  return 0;
+}
+
+/* FIXME: effectively the same as mbox_cleanup */
+void
+amd_cleanup (void *arg)
+{
+  mu_mailbox_t mailbox = arg;
+  mu_monitor_unlock (mailbox->monitor);
+  mu_locker_unlock (mailbox->locker);
+}
+
+/* Insert message msg into the message list on the appropriate position */
+int
+_amd_message_insert (struct _amd_data *amd, struct _amd_message *msg)
+{
+  size_t index;
+
+  if (amd_msg_lookup (amd, msg, &index))
+    {
+      /* Not found. Index is the index of the array cell where msg
+	 must be placed */
+      int rc = amd_array_expand (amd, index);
+      if (rc)
+	return rc;
+      amd->msg_array[index] = msg;
+      msg->amd = amd;
+    }
+  else
+    {
+      /*FIXME: Found? Shouldn't happen */
+      return EEXIST;
+    }
+  return 0;
+}
+
+static void
+_amd_message_delete (struct _amd_data *amd, struct _amd_message *msg)
+{
+  size_t index;
+  struct _amd_message **pp;
+
+  if (amd_msg_lookup (amd, msg, &index))
+    {
+      /* FIXME: Not found? */
+      return;
+    }
+
+  msg = _amd_get_message (amd, index);
+
+  pp = amd_pool_lookup (msg);
+  if (pp)
+    *pp = NULL;
+  
+  mu_message_destroy (&msg->message, msg);
+  if (amd->msg_free)
+    amd->msg_free (msg);
+  free (msg);
+  amd_array_shrink (amd, index);
+}
+
+/* Scan given message and fill amd_message_t fields.
+   NOTE: the function assumes mhm->stream != NULL. */
+static int
+amd_scan_message (struct _amd_message *mhm)
+{
+  mu_stream_t stream = mhm->stream;
+  char buf[1024];
+  size_t off;
+  size_t n;
+  int status;
+  int in_header = 1;
+  size_t hlines = 0;
+  size_t blines = 0;
+  size_t body_start = 0;
+  struct stat st;
+  char *msg_name;
+
+  /* Check if the message was modified after the last scan */
+  status = mhm->amd->cur_msg_file_name (mhm, &msg_name);
+  if (status)
+    return status;
+
+  if (stat (msg_name, &st) == 0 && st.st_mtime == mhm->mtime)
+    {
+      /* Nothing to do */
+      free (msg_name);
+      return 0;
+    }
+  free (msg_name);
+
+  off = 0;
+  status = mu_stream_seek (stream, 0, MU_SEEK_SET, NULL);
+  if (status == 0)
+    while ((status = mu_stream_readline (stream, buf, sizeof (buf), &n)) == 0
+	   && n != 0)
+      {
+	if (in_header)
+	  {
+	    if (buf[0] == '\n')
+	      {
+		in_header = 0;
+		body_start = off + 1;
+	      }
+	    if (buf[n - 1] == '\n')
+	      hlines++;
+	    
+	    /* Process particular attributes */
+	    if (mu_c_strncasecmp (buf, "status:", 7) == 0)
+	      {
+		int deleted = mhm->attr_flags & MU_ATTRIBUTE_DELETED;
+		mu_string_to_flags (buf, &mhm->attr_flags);
+		mhm->attr_flags |= deleted;
+	      }
+	    else if (mu_c_strncasecmp (buf, "x-imapbase:", 11) == 0)
+	      {
+		char *p;
+		mhm->amd->uidvalidity = strtoul (buf + 11, &p, 10);
+		/* second number is next uid. Ignored */
+	      }
+	  }
+	else
+	  {
+	    if (buf[n - 1] == '\n')
+	      blines++;
+	  }
+	off += n;
+      }
+
+  if (status == 0)
+    {
+      mhm->mtime = st.st_mtime;
+      if (!body_start)
+	body_start = off;
+      mhm->header_lines = hlines;
+      mhm->body_lines = blines;
+      mhm->body_start = body_start;
+      mhm->body_end = off;
+    }
+  return status;
+}
+
+static int
+amd_scan (mu_mailbox_t mailbox, size_t msgno, size_t *pcount)
+{
+  struct _amd_data *amd = mailbox->data;
+
+  if (! amd_is_updated (mailbox))
+    return amd->scan0 (mailbox, msgno, pcount, 1);
+
+  if (pcount)
+    *pcount = amd->msg_count;
+
+  return 0;
+}
+
+/* Is the internal representation of the mailbox up to date.
+   Return 1 if so, 0 otherwise. */
+static int
+amd_is_updated (mu_mailbox_t mailbox)
+{
+  struct stat st;
+  struct _amd_data *amd = mailbox->data;
+
+  if (stat (amd->name, &st) < 0)
+    return 1;
+
+  return amd->mtime == st.st_mtime;
+}
+
+static int
+amd_get_size (mu_mailbox_t mailbox, mu_off_t *psize)
+{
+  struct _amd_data *amd = mailbox->data;
+  if (amd->mailbox_size)
+    return amd->mailbox_size (mailbox, psize);
+  *psize = 0;
+  if (read_size_file (amd, psize))
+    {
+      int rc = compute_mailbox_size (amd, amd->name, psize);
+      if (rc == 0)
+	write_size_file (amd, *psize);
+      return rc;
+    }
+  return 0;
+}
+
+/* Return number of open streams residing in a message pool */
+static int
+amd_pool_open_count (struct _amd_data *amd)
+{
+  int cnt = amd->pool_last - amd->pool_first;
+  if (cnt < 0)
+    cnt += MAX_OPEN_STREAMS;
+  return cnt;
+}
+
+/* Look up a _amd_message in the pool of open messages.
+   If the message is found in the pool, returns the address of
+   the pool slot occupied by it. Otherwise returns NULL. */
+static struct _amd_message **
+amd_pool_lookup (struct _amd_message *mhm)
+{
+  struct _amd_data *amd = mhm->amd;
+  int i;
+
+  for (i = amd->pool_first; i != amd->pool_last; )
+    {
+      if (amd->msg_pool[i] == mhm)
+	return &amd->msg_pool[i];
+      if (++i == MAX_OPEN_STREAMS)
+	i = 0;
+    }
+  return NULL;
+}
+
+/* Open a stream associated with the message mhm. If the stream is
+   already open, do nothing.
+   NOTE: We could have reused the NULL holes in the msg_pool, but
+   that hardly is worth the effort, since the holes appear only when
+   expunging. On the other hand this may be useful when MAX_OPEN_STREAMS
+   size is very big. "Premature optimization is the root of all evil" */
+static int
+amd_pool_open (struct _amd_message *mhm)
+{
+  int status;
+  struct _amd_data *amd = mhm->amd;
+  if (amd_pool_lookup (mhm))
+    return 0;
+  if (amd_pool_open_count(amd) == MAX_OPEN_STREAMS-1)
+    {
+      amd_message_stream_close (amd->msg_pool[amd->pool_first++]);
+      amd->pool_first %= MAX_OPEN_STREAMS;
+    }
+  status = amd_message_stream_open (mhm);
+  if (status)
+    return status;
+  amd->msg_pool[amd->pool_last++] = mhm;
+  amd->pool_last %= MAX_OPEN_STREAMS;
+  return 0;
+}
+
+static void
+amd_pool_flush (struct _amd_data *amd)
+{
+  int i;
+  
+  for (i = amd->pool_first; i != amd->pool_last; )
+    {
+      if (amd->msg_pool[i])
+	amd_message_stream_close (amd->msg_pool[i]);
+      if (++i == MAX_OPEN_STREAMS)
+	i = 0;
+    }
+  amd->pool_first = amd->pool_last = 0;
+}
+
+/* Attach a stream to a given message structure. The latter is supposed
+   to be already added to the open message pool. */
+int
+amd_message_stream_open (struct _amd_message *mhm)
+{
+  struct _amd_data *amd = mhm->amd;
+  char *filename;
+  int status;
+  int flags = MU_STREAM_ALLOW_LINKS;
+
+  status = amd->cur_msg_file_name (mhm, &filename);
+  if (status)
+    return status;
+
+  /* The message should be at least readable */
+  if (amd->mailbox->flags & (MU_STREAM_RDWR|MU_STREAM_WRITE|MU_STREAM_APPEND))
+    flags |= MU_STREAM_RDWR;
+  else 
+    flags |= MU_STREAM_READ;
+  status = mu_file_stream_create (&mhm->stream, filename, flags);
+
+  free (filename);
+
+  if (status != 0)
+    return status;
+
+  /* FIXME: Select buffer size dynamically */
+  mu_stream_set_buffer (mhm->stream, mu_buffer_full, 16384);
+  
+  status = amd_scan_message (mhm);
+
+  return status;
+}
+
+/* Close the stream associated with the given message. */
+void
+amd_message_stream_close (struct _amd_message *mhm)
+{
+  if (mhm)
+    {
+      mu_stream_close (mhm->stream);
+      mhm->stream = NULL;
+    }
+}
+
+void
+amd_check_message (struct _amd_message *mhm)
+{
+  if (mhm->body_end == 0)
+    amd_pool_open (mhm);
+}
+
+/* Reading functions */
+static int
+amd_body_stream_read (mu_stream_t is, char *buffer, size_t buflen,
+		      size_t *pnread)
+{
+  struct _amd_body_stream *amdstr = (struct _amd_body_stream *)is;
+  mu_body_t body = amdstr->body;
+  mu_message_t msg = mu_body_get_owner (body);
+  struct _amd_message *mhm = mu_message_get_owner (msg);
+  size_t nread = 0;
+  int status = 0;
+  mu_off_t ln;
+
+  amd_pool_open (mhm);
+
+  if (buffer == NULL || buflen == 0)
+    {
+      *pnread = nread;
+      return 0;
+    }
+
+  mu_monitor_rdlock (mhm->amd->mailbox->monitor);
+#ifdef WITH_PTHREAD
+  /* read() is cancellation point since we're doing a potentially
+     long operation.  Lets make sure we clean the state.  */
+  pthread_cleanup_push (amd_cleanup, (void *)mhm->amd->mailbox);
+#endif
+
+  ln = mhm->body_end - (mhm->body_start + amdstr->off);
+  if (ln > 0)
+    {
+      nread = ((size_t)ln < buflen) ? (size_t)ln : buflen;
+      status = mu_stream_seek (mhm->stream, mhm->body_start + amdstr->off,
+			       MU_SEEK_SET, NULL);
+      if (status == 0)
+	{
+	  status = mu_stream_read (mhm->stream, buffer, nread, &nread);
+	  amdstr->off += nread;
+	}
+    }
+
+  *pnread = nread;
+
+  mu_monitor_unlock (mhm->amd->mailbox->monitor);
+#ifdef WITH_PTHREAD
+  pthread_cleanup_pop (0);
+#endif
+
+  return status;
+}
+
+static int
+amd_body_stream_readdelim (mu_stream_t is, char *buffer, size_t buflen,
+			   int delim,
+			   size_t *pnread)
+{
+  struct _amd_body_stream *amdstr = (struct _amd_body_stream *)is;
+  mu_body_t body = amdstr->body;
+  mu_message_t msg = mu_body_get_owner (body);
+  struct _amd_message *mhm = mu_message_get_owner (msg);
+  int status = 0;
+
+  amd_pool_open (mhm);
+
+  if (buffer == NULL || buflen == 0)
+    {
+      if (pnread)
+	*pnread = 0;
+      return 0;
+    }
+
+  mu_monitor_rdlock (mhm->amd->mailbox->monitor);
+#ifdef WITH_PTHREAD
+  /* read() is cancellation point since we're doing a potentially
+     long operation.  Lets make sure we clean the state.  */
+  pthread_cleanup_push (amd_cleanup, (void *)mhm->amd->mailbox);
+#endif
+
+  status = mu_stream_seek (mhm->stream, mhm->body_start + amdstr->off,
+			   MU_SEEK_SET, NULL);
+  if (status == 0)
+    {
+      size_t nread = 0;
+      size_t ln;
+	  
+      ln = mhm->body_end - (mhm->body_start + amdstr->off) + 1;
+      if (ln > 0)
+	{
+	  size_t rdsize = ((size_t)ln < buflen) ? (size_t)ln : buflen;
+	  status = mu_stream_readdelim (mhm->stream, buffer, rdsize,
+					delim, &nread);
+	  amdstr->off += nread;
+	}
+
+      if (pnread)
+	*pnread = nread;
+    }
+
+  mu_monitor_unlock (mhm->amd->mailbox->monitor);
+#ifdef WITH_PTHREAD
+  pthread_cleanup_pop (0);
+#endif
+
+  return status;
+}
+
+static int
+amd_body_stream_seek (mu_stream_t str, mu_off_t off, mu_off_t *presult)
+{
+  size_t size;
+  struct _amd_body_stream *amdstr = (struct _amd_body_stream *)str;
+  
+  amd_body_size (amdstr->body, &size);
+
+  if (off < 0 || off > size)
+    return ESPIPE;
+
+  amdstr->off = off;
+  if (presult)
+    *presult = off;
+  return 0;
+}
+
+/* Return corresponding sizes */
+
+static int
+amd_body_stream_size (mu_stream_t stream, mu_off_t *psize)
+{
+  mu_body_t body = ((struct _amd_body_stream *)stream)->body;
+  size_t size;
+  int rc = amd_body_size (body, &size);
+  if (rc == 0)
+    *psize = size;
+  return rc;
+}
+
+static int
+amd_body_size (mu_body_t body, size_t *psize)
+{
+  mu_message_t msg = mu_body_get_owner (body);
+  struct _amd_message *mhm = mu_message_get_owner (msg);
+  if (mhm == NULL)
+    return EINVAL;
+  amd_check_message (mhm);
+  if (psize)
+    *psize = mhm->body_end - mhm->body_start;
+  return 0;
+}
+
+static int
+amd_body_lines (mu_body_t body, size_t *plines)
+{
+  mu_message_t msg = mu_body_get_owner (body);
+  struct _amd_message *mhm = mu_message_get_owner (msg);
+  if (mhm == NULL)
+    return EINVAL;
+  amd_check_message (mhm);
+  if (plines)
+    *plines = mhm->body_lines;
+  return 0;
+}
+
+/* Headers */
+static int
+amd_header_fill (void *data, char **pbuf, size_t *plen)
+{
+  char *buffer;
+  size_t len;
+  mu_message_t msg = data;
+  struct _amd_message *mhm = mu_message_get_owner (msg);
+  int status, rc;
+  mu_off_t pos;
+  
+  status = amd_pool_open (mhm);
+  if (status)
+    return status;
+
+  len = mhm->body_start;
+  buffer = malloc (len);
+  if (!buffer)
+    return ENOMEM;
+  
+  status = mu_stream_seek (mhm->stream, 0, MU_SEEK_CUR, &pos);
+  if (status)
+    return status;
+  status = mu_stream_seek (mhm->stream, 0, MU_SEEK_SET, NULL);
+  if (status)
+    return status;
+
+  status = mu_stream_read (mhm->stream, buffer, len, NULL);
+  rc = mu_stream_seek (mhm->stream, pos, MU_SEEK_SET, NULL);
+
+  if (!status)
+    status = rc;
+  
+  if (status)
+    {
+      free (buffer);
+      return status;
+    }
+
+  *plen = len;
+  *pbuf = buffer;
+  return 0;
+}
+
+/* Attributes */
+static int
+amd_get_attr_flags (mu_attribute_t attr, int *pflags)
+{
+  mu_message_t msg = mu_attribute_get_owner (attr);
+  struct _amd_message *mhm = mu_message_get_owner (msg);
+
+  if (mhm == NULL)
+    return EINVAL;
+  if (pflags)
+    *pflags = mhm->attr_flags;
+  return 0;
+}
+
+static int
+amd_set_attr_flags (mu_attribute_t attr, int flags)
+{
+  mu_message_t msg = mu_attribute_get_owner (attr);
+  struct _amd_message *mhm = mu_message_get_owner (msg);
+
+  if (mhm == NULL)
+    return EINVAL;
+  mhm->attr_flags |= flags;
+  return 0;
+}
+
+static int
+amd_unset_attr_flags (mu_attribute_t attr, int flags)
+{
+  mu_message_t msg = mu_attribute_get_owner (attr);
+  struct _amd_message *mhm = mu_message_get_owner (msg);
+
+  if (mhm == NULL)
+    return EINVAL;
+  mhm->attr_flags &= ~flags;
+  return 0;
+}
+
+/* Envelope */
+static int
+amd_envelope_date (mu_envelope_t envelope, char *buf, size_t len,
+		   size_t *psize)
+{
+  mu_message_t msg = mu_envelope_get_owner (envelope);
+  struct _amd_message *mhm = mu_message_get_owner (msg);
+  mu_header_t hdr = NULL;
+  char *date;
+  int status;
+  
+  if (mhm == NULL)
+    return EINVAL;
+
+  if ((status = mu_message_get_header (msg, &hdr)) != 0)
+    return status;
+  if (mu_header_aget_value (hdr, MU_HEADER_ENV_DATE, &date)
+      && mu_header_aget_value (hdr, MU_HEADER_DELIVERY_DATE, &date))
+    return MU_ERR_NOENT;
+  else
+    {
+      time_t t;
+      int rc;
+      
+      /* Convert to ctime format */
+      rc = mu_parse_date (date, &t, NULL); /* FIXME: TZ info is lost */
+      free (date);
+      if (rc)
+	return MU_ERR_NOENT;
+      date = strdup (ctime (&t)); 
+    }
+
+  /* Format:  "sender date" */
+  if (buf && len > 0)
+    {
+      len--; /* Leave space for the null.  */
+      strncpy (buf, date, len);
+      if (strlen (date) < len)
+	{
+	  len = strlen (buf);
+	  if (buf[len-1] != '\n')
+	    buf[len++] = '\n';
+	}
+      buf[len] = '\0';
+    }
+  else
+    len = strlen (date);
+  
+  free (date);
+  
+  if (psize)
+    *psize = len;
+  return 0;
+}
+
+static int
+amd_envelope_sender (mu_envelope_t envelope, char *buf, size_t len, size_t *psize)
+{
+  mu_message_t msg = mu_envelope_get_owner (envelope);
+  struct _amd_message *mhm = mu_message_get_owner (msg);
+  mu_header_t hdr = NULL;
+  char *from;
+  int status;
+
+  if (mhm == NULL)
+    return EINVAL;
+
+  if ((status = mu_message_get_header (msg, &hdr)))
+    return status;
+  if ((status = mu_header_aget_value (hdr, MU_HEADER_ENV_SENDER, &from)))
+    return status;
+
+  if (buf && len > 0)
+    {
+      int slen = strlen (from);
+
+      if (len < slen + 1)
+	slen = len - 1;
+      memcpy (buf, from, slen);
+      buf[slen] = 0;
+    }
+  else
+    len = strlen (from);
+
+  if (psize)
+    *psize = len;
+  return 0;
+}
+
+
+int
+amd_remove_dir (const char *name)
+{
+  DIR *dir;
+  struct dirent *ent;
+  char *namebuf;
+  size_t namelen, namesize;
+  int rc = 0;
+  int has_subdirs = 0;
+  
+  namelen = strlen (name);
+  namesize = namelen + 128;
+  namebuf = malloc (namesize);
+  if (!namebuf)
+    return ENOMEM;
+  memcpy (namebuf, name, namelen);
+  if (namebuf[namelen - 1] != '/')
+    namebuf[namelen++] = '/';
+  
+  dir = opendir (name);
+  if (!dir)
+    return errno;
+  while ((ent = readdir (dir)))
+    {
+      struct stat st;
+      size_t len;
+
+      if (strcmp (ent->d_name, ".") == 0 ||
+	  strcmp (ent->d_name, "..") == 0)
+	continue;
+      len = strlen (ent->d_name);
+      if (namelen + len >= namesize)
+	{
+	  char *p;
+
+	  namesize += len + 1;
+	  p = realloc (namebuf, namesize);
+	  if (!p)
+	    {
+	      rc = ENOMEM;
+	      break;
+	    }
+	}
+      strcpy (namebuf + namelen, ent->d_name);
+      if (stat (namebuf, &st) == 0 && S_ISDIR (st.st_mode))
+	{
+	  has_subdirs = 1;
+	  continue;
+	}
+      
+      if (unlink (namebuf))
+	{
+	  rc = errno;
+	  mu_diag_output (MU_DIAG_WARNING,
+			  "failed to remove %s: %s",
+			  namebuf, mu_strerror (rc));
+	  break;
+	}
+    }
+  closedir (dir);
+  free (namebuf);
+
+  if (rc == 0 && !has_subdirs)
+    {
+      if (rmdir (name))
+	{
+	  rc = errno;
+	  /* POSIX.1-2001 allows EEXIST to be returned if the directory
+	     contained entries other than . and .. */
+	  if (rc == EEXIST)
+	    rc = ENOTEMPTY;
+	}
+    }
+  return rc;
+}
+
+
diff --git a/libmailutils/base/argcv.c b/libmailutils/base/argcv.c
new file mode 100644
index 0000000..6e63b23
--- /dev/null
+++ b/libmailutils/base/argcv.c
@@ -0,0 +1,548 @@
+/* argcv.c - simple functions for parsing input based on whitespace
+   Copyright (C) 1999, 2000, 2001, 2003, 2004, 2005, 2006, 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, see 
+   <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <ctype.h>
+#include <errno.h>
+#include <mailutils/argcv.h>
+
+/* Keep mailutils namespace clean */
+#define argcv_get            mu_argcv_get 
+#define argcv_get_n          mu_argcv_get_n 
+#define argcv_get_np         mu_argcv_get_np 
+#define argcv_string         mu_argcv_string
+#define argcv_free           mu_argcv_free
+#define argv_free            mu_argv_free 
+#define argcv_unquote_char   mu_argcv_unquote_char
+#define argcv_quote_char     mu_argcv_quote_char  
+#define argcv_quoted_length  mu_argcv_quoted_length
+#define argcv_unquote_copy   mu_argcv_unquote_copy 
+#define argcv_quote_copy     mu_argcv_quote_copy     
+
+/*
+ * takes a string and splits it into several strings, breaking at ' '
+ * command is the string to split
+ * the number of strings is placed into argc
+ * the split strings are put into argv
+ * returns 0 on success, nonzero on failure
+ */
+
+#define isws(c) ((c)==' '||(c)=='\t'||(c)=='\n')
+#define isdelim(c,delim) (strchr(delim,(c))!=NULL)
+
+struct argcv_info
+{
+  int len;
+  const char *command;
+  const char *delim;
+  const char *comment;
+  int flags;
+  
+  int start;
+  int end;
+  int save;
+  int finish_pos;
+};
+
+static void
+init_argcv_info (struct argcv_info *ap, int flags,
+		 int len, const char *command, const char *delim,
+		 const char *comment)
+{
+  memset (ap, 0, sizeof *ap);
+  ap->len = len;
+  ap->command = command;
+  ap->delim = delim;
+  ap->comment = comment;
+  ap->flags = flags;
+}
+
+static int
+argcv_scan (struct argcv_info *ap)
+{
+  int i = 0;
+  int len = ap->len;
+  const char *command = ap->command;
+  const char *delim = ap->delim;
+  const char *comment = ap->comment;
+  
+  for (;;)
+    {
+      i = ap->save;
+
+      if (i >= len)
+	return i + 1;
+
+      /* Skip initial whitespace */
+      while (i < len && isws (command[i]))
+	i++;
+      ap->start = i;
+
+      if (!isdelim (command[i], delim))
+	{
+	  while (i < len)
+	    {
+	      if (command[i] == '\\')
+		{
+		  if (++i == len)
+		    break;
+		  i++;
+		  continue;
+		}
+	      
+	      if (command[i] == '\'' || command[i] == '"')
+		{
+		  int j;
+		  for (j = i + 1; j < len && command[j] != command[i]; j++)
+		    if (command[j] == '\\')
+		      j++;
+		  if (j < len)
+		    i = j + 1;
+		  else
+		    i++;
+		}
+	      else if (isws (command[i]) || isdelim (command[i], delim))
+		break;
+	      else
+		i++; /* skip the escaped character */
+	    }
+	  i--;
+	}
+      else if (!(ap->flags & MU_ARGCV_RETURN_DELIMS))
+	{
+	  while (i < len && isdelim (command[i], delim))
+	    i++;
+	  ap->save = i;
+	  continue;
+	}
+      
+
+      ap->end = i;
+      ap->save = ap->finish_pos = i + 1;
+
+      /* If we have a token, and it starts with a comment character, skip
+         to the newline and restart the token search. */
+      if (ap->save <= len)
+	{
+	  if (strchr (comment, command[ap->start]) != NULL)
+	    {
+	      ap->finish_pos = ap->start;
+	      i = ap->save;
+	      while (i < len && command[i] != '\n')
+		i++;
+
+	      ap->save = i;
+	      continue;
+	    }
+	}
+      break;
+    }
+  return ap->save;
+}
+
+static char quote_transtab[] = "\\\\\"\"a\ab\bf\fn\nr\rt\tv\v";
+
+int
+argcv_unquote_char (int c)
+{
+  char *p;
+
+  for (p = quote_transtab; *p; p += 2)
+    {
+      if (*p == c)
+	return p[1];
+    }
+  return c;
+}
+
+int
+argcv_quote_char (int c)
+{
+  char *p;
+  
+  for (p = quote_transtab + sizeof(quote_transtab) - 2;
+       p > quote_transtab; p -= 2)
+    {
+      if (*p == c)
+	return p[-1];
+    }
+  return -1;
+}
+  
+#define to_num(c) \
+  (isdigit(c) ? c - '0' : (isxdigit(c) ? toupper(c) - 'A' + 10 : 255 ))
+
+static int
+xtonum (int *pval, const char *src, int base, int cnt)
+{
+  int i, val;
+  
+  for (i = 0, val = 0; i < cnt; i++, src++)
+    {
+      int n = *(unsigned char*)src;
+      if (n > 127 || (n = to_num(n)) >= base)
+	break;
+      val = val*base + n;
+    }
+  *pval = val;
+  return i;
+}
+
+size_t
+argcv_quoted_length (const char *str, int *quote)
+{
+  size_t len = 0;
+
+  *quote = 0;
+  for (; *str; str++)
+    {
+      if (*str == ' ')
+	{
+	  len++;
+	  *quote = 1;
+	}
+      else if (*str == '"')
+	{
+	  len += 2;
+	  *quote = 1;
+	}
+      else if (*str != '\t' && *str != '\\' && isprint (*str))
+	len++;
+      else if (argcv_quote_char (*str) != -1)
+	len += 2;
+      else
+	len += 4;
+    }
+  return len;
+}
+
+void
+argcv_unquote_copy (char *dst, const char *src, size_t n)
+{
+  int i = 0;
+  int c;
+  int expect_delim = 0; 
+    
+  while (i < n)
+    {
+      switch (src[i])
+	{
+	case '\'':
+	case '"':
+	  if (!expect_delim)
+	    {
+	      const char *p;
+	      
+	      for (p = src+i+1; *p && *p != src[i]; p++)
+		if (*p == '\\')
+		  p++;
+	      if (*p)
+		expect_delim = src[i++];
+	      else
+		*dst++ = src[i++];
+	    }
+	  else if (expect_delim == src[i])
+	    ++i;
+	  else
+	    *dst++ = src[i++];
+	  break;
+	  
+	case '\\':
+	  ++i;
+	  if (src[i] == 'x' || src[i] == 'X')
+	    {
+	      if (n - i < 2)
+		{
+		  *dst++ = '\\';
+		  *dst++ = src[i++];
+		}
+	      else 
+		{
+		  int off = xtonum(&c, src + i + 1, 16, 2);
+		  if (off == 0)
+		    {
+		      *dst++ = '\\';
+		      *dst++ = src[i++];
+		    }
+		  else
+		    {
+		      *dst++ = c;
+		      i += off + 1;
+		    }
+		}
+	    }
+	  else if ((unsigned char)src[i] < 128 && isdigit (src[i]))
+	    {
+	      if (n - i < 1)
+		{
+		  *dst++ = '\\';
+		  *dst++ = src[i++];
+		}
+	      else
+		{
+		  int off = xtonum (&c, src+i, 8, 3);
+		  if (off == 0)
+		    {
+		      *dst++ = '\\';
+		      *dst++ = src[i++];
+		    }
+		  else
+		    {
+		      *dst++ = c;
+		      i += off;
+		    }
+		}
+	    }
+	  else
+	    *dst++ = argcv_unquote_char (src[i++]);
+	  break;
+	  
+	default:
+	  *dst++ = src[i++];
+	}
+    }
+  *dst = 0;
+}
+
+void
+argcv_quote_copy (char *dst, const char *src)
+{
+  for (; *src; src++)
+    {
+      if (*src == '"')
+	{
+	  *dst++ = '\\';
+	  *dst++ = *src;
+	}
+      else if (*src != '\t' && *src != '\\' && isprint(*src))
+	*dst++ = *src;      
+      else
+	{
+	  int c = argcv_quote_char (*src);
+	  *dst++ = '\\';
+	  if (c != -1)
+	    *dst++ = c;
+	  else
+	    {
+	      char tmp[4];
+	      snprintf (tmp, sizeof tmp, "%03o", *(unsigned char*)src);
+	      memcpy (dst, tmp, 3);
+	      dst += 3;
+	    }
+	}
+    }
+}
+
+int
+argcv_get_np (const char *command, int len,
+	      const char *delim, const char *cmnt,
+	      int flags,
+	      int *pargc, char ***pargv, char **endp)
+{
+  int i = 0;
+  struct argcv_info info;
+  int argc;
+  char **argv;
+  
+  if (!delim)
+    delim = "";
+  if (!cmnt)
+    cmnt = "";
+
+  init_argcv_info (&info, flags, len, command, delim, cmnt);
+
+  /* Count number of arguments */
+  argc = 0;
+  while (argcv_scan (&info) <= len)
+    argc++;
+
+  argv = calloc ((argc + 1), sizeof (char *));
+  if (argv == NULL)
+    return ENOMEM;
+  
+  i = 0;
+  info.save = 0;
+  for (i = 0; i < argc; i++)
+    {
+      int n;
+      int unquote;
+      
+      argcv_scan (&info);
+
+      if ((command[info.start] == '"' || command[info.end] == '\'')
+	  && command[info.end] == command[info.start])
+	{
+	  if (info.start < info.end)
+	    {
+	      info.start++;
+	      info.end--;
+	    }
+	  unquote = 0;
+	}
+      else
+	unquote = 1;
+      
+      n = info.end - info.start + 1;
+      argv[i] = calloc (n + 1,  sizeof (char));
+      if (argv[i] == NULL)
+	{
+	  argcv_free (i, argv);
+	  return ENOMEM;
+	}
+      if (unquote)
+	argcv_unquote_copy (argv[i], &command[info.start], n);
+      else
+	memcpy (argv[i], &command[info.start], n);
+      argv[i][n] = 0;
+    }
+  argv[i] = NULL;
+
+  *pargc = argc;
+  *pargv = argv;
+  if (endp)
+    *endp = (char*) (command + info.finish_pos);
+  return 0;
+}
+
+int
+argcv_get_n (const char *command, int len, const char *delim, const char *cmnt,
+	     int *pargc, char ***pargv)
+{
+  return argcv_get_np (command, len, delim, cmnt, MU_ARGCV_RETURN_DELIMS,
+		       pargc, pargv, NULL);
+}
+
+int
+argcv_get (const char *command, const char *delim, const char *cmnt,
+	   int *argc, char ***argv)
+{
+  return argcv_get_n (command, strlen (command), delim, cmnt, argc, argv);
+}
+
+
+/*
+ * frees all elements of an argv array
+ * argc is the number of elements
+ * argv is the array
+ */
+void
+argcv_free (int argc, char **argv)
+{
+  if (argc <= 0)
+    return;
+  while (--argc >= 0)
+    if (argv[argc])
+      free (argv[argc]);
+  free (argv);
+}
+
+void
+argv_free (char **argv)
+{
+  int i;
+
+  for (i = 0; argv[i]; i++)
+    free (argv[i]);
+  free (argv);
+}
+
+/* Make a argv an make string separated by ' '.  */
+
+int
+argcv_string (int argc, char **argv, char **pstring)
+{
+  size_t i, j, len;
+  char *buffer;
+
+  /* No need.  */
+  if (pstring == NULL)
+    return EINVAL;
+
+  buffer = malloc (1);
+  if (buffer == NULL)
+    return ENOMEM;
+  *buffer = '\0';
+
+  for (len = i = j = 0; i < argc; i++)
+    {
+      int quote;
+      int toklen;
+
+      toklen = argcv_quoted_length (argv[i], &quote);
+      
+      len += toklen + 2;
+      if (quote)
+	len += 2;
+      
+      buffer = realloc (buffer, len);
+      if (buffer == NULL)
+        return ENOMEM;
+
+      if (i != 0)
+	buffer[j++] = ' ';
+      if (quote)
+	buffer[j++] = '"';
+      argcv_quote_copy (buffer + j, argv[i]);
+      j += toklen;
+      if (quote)
+	buffer[j++] = '"';
+    }
+
+  for (; j > 0 && isspace (buffer[j-1]); j--)
+    ;
+  buffer[j] = 0;
+  if (pstring)
+    *pstring = buffer;
+  return 0;
+}
+
+void
+mu_argcv_remove (int *pargc, char ***pargv,
+		 int (*sel) (const char *, void *), void *data)
+{
+  int i, j;
+  int argc = *pargc;
+  char **argv = *pargv;
+  int cnt = 0;
+  
+  for (i = j = 0; i < argc; i++)
+    {
+      if (sel (argv[i], data))
+	{
+	  free (argv[i]);
+	  cnt++;
+	}
+      else
+	{
+	  if (i != j)
+	    argv[j] = argv[i];
+	  j++;
+	}
+    }
+  if (i != j)
+    argv[j] = NULL;
+  argc -= cnt;
+
+  *pargc = argc;
+  *pargv = argv;
+}
+      
+ 
diff --git a/libmailutils/base/assoc.c b/libmailutils/base/assoc.c
new file mode 100644
index 0000000..4d27cb7
--- /dev/null
+++ b/libmailutils/base/assoc.c
@@ -0,0 +1,527 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 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.
+
+   GNU Mailutils 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 GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
+
+#if HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <string.h>
+#include <mailutils/types.h>
+#include <mailutils/assoc.h>
+#include <mailutils/errno.h>
+#include <mailutils/error.h>
+#include <mailutils/iterator.h>
+#include <mailutils/mutil.h>
+#include <mailutils/cstr.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,
+   starting from 64. Hardly someone will need more than 16411 symbols, and
+   even if someone will, it is easy enough to add more numbers to the
+   sequence. */
+
+static unsigned int hash_size[] = {
+    37,   101,  229,  487, 1009, 2039, 4091, 8191, 16411
+};
+
+/* |max_rehash| keeps the number of entries in |hash_size| table. */
+static unsigned int max_rehash = sizeof (hash_size) / sizeof (hash_size[0]);
+
+struct _mu_assoc_elem
+{
+  char *name;
+  char data[1];
+};
+
+struct _mu_assoc
+{
+  int flags;
+  unsigned int hash_num;  /* Index to hash_size table */
+  size_t elsize;          /* Size of an element */
+  void *tab;
+  mu_assoc_free_fn free;
+  mu_iterator_t itr;
+};
+
+struct _mu_assoc_elem_align
+{
+  char c;
+  struct _mu_assoc_elem x;
+};
+
+#define __ASSOC_ELEM_ALIGNMENT (mu_offsetof(struct _mu_assoc_elem_align, x))
+
+#define __ASSOC_ELEM_SIZE(a) \
+   ((a)->elsize + mu_offsetof(struct _mu_assoc_elem, data))
+#define __ASSOC_ALIGN(a, b) (((a) + (b) - 1) & ~((b) - 1))
+#define ASSOC_ELEM_SIZE(a) \
+   __ASSOC_ALIGN(__ASSOC_ELEM_SIZE(a),__ASSOC_ELEM_ALIGNMENT)
+
+#define __ASSOC_ELEM(a,p,n) \
+ ((struct _mu_assoc_elem*) ((char*) (p) + ASSOC_ELEM_SIZE (a) * n))
+
+#define ASSOC_ELEM(a,n) __ASSOC_ELEM(a,(a)->tab,n)
+
+#define ASSOC_ELEM_INDEX(a,e) \
+ (((char*)(e) - (char*)(a)->tab) / ASSOC_ELEM_SIZE (a))
+
+
+static unsigned
+hash (const char *name, unsigned long hash_num)
+{
+  unsigned i;
+	
+  for (i = 0; *name; name++)
+    {
+      i <<= 1;
+      i ^= *(unsigned char*) name;
+    }
+  return i % hash_size[hash_num];
+};
+
+static int
+assoc_lookup_or_install (struct _mu_assoc_elem **elp,
+			 mu_assoc_t assoc, const char *name, int *install);
+
+static int
+assoc_rehash (mu_assoc_t assoc)
+{
+  void *old_tab = assoc->tab;
+  void *new_tab;
+  unsigned int i;
+  unsigned int hash_num = assoc->hash_num + 1;
+  
+  if (hash_num >= max_rehash)
+      return MU_ERR_BUFSPACE;
+
+  new_tab = calloc (hash_size[hash_num], ASSOC_ELEM_SIZE (assoc));
+  assoc->tab = new_tab;
+  if (old_tab)
+    {
+      assoc->hash_num = hash_num;
+      for (i = 0; i < hash_size[hash_num-1]; i++)
+	{
+	  struct _mu_assoc_elem *elt = __ASSOC_ELEM (assoc, old_tab, i);
+	  if (elt->name)
+	    {
+	      int tmp;
+	      struct _mu_assoc_elem *newp;
+	      
+	      int rc = assoc_lookup_or_install (&newp, assoc, elt->name, &tmp);
+	      if (rc)
+		return rc;
+	      newp->name = elt->name;
+	      memcpy(newp->data, elt->data, assoc->elsize);
+	    }
+	}
+      free (old_tab);
+    }
+  return 0;
+}
+
+static void
+assoc_free_elem (mu_assoc_t assoc, struct _mu_assoc_elem *elem)
+{
+  if (assoc->free)
+    assoc->free (elem->data);
+  if (!(assoc->flags & MU_ASSOC_COPY_KEY))
+    free (elem->name);
+}
+
+static int
+assoc_remove (mu_assoc_t assoc, struct _mu_assoc_elem *elem)
+{
+  unsigned int i, j, r;
+
+  if (!(ASSOC_ELEM (assoc, 0) <= elem
+	&& elem < ASSOC_ELEM (assoc, hash_size[assoc->hash_num])))
+    return EINVAL;
+
+  assoc_free_elem (assoc, elem);
+  
+  for (i = ASSOC_ELEM_INDEX (assoc, elem);;)
+    {
+      struct _mu_assoc_elem *p = ASSOC_ELEM (assoc, i);
+      p->name = NULL;
+      j = i;
+
+      do
+	{
+	  if (++i >= hash_size[assoc->hash_num])
+	    i = 0;
+	  p = ASSOC_ELEM (assoc, i);
+	  if (!p->name)
+	    return 0;
+	  r = hash (p->name, assoc->hash_num);
+	}
+      while ((j < r && r <= i) || (i < j && j < r) || (r <= i && i < j));
+
+      if (j != i)
+	      memcpy (ASSOC_ELEM (assoc, j), ASSOC_ELEM (assoc, i),
+		      assoc->elsize);
+    }
+  return 0;
+}
+
+#define name_cmp(assoc,a,b) (((assoc)->flags & MU_ASSOC_ICASE) ? \
+                             mu_c_strcasecmp(a,b) : strcmp(a,b))
+
+static int
+assoc_lookup_or_install (struct _mu_assoc_elem **elp,
+			 mu_assoc_t assoc, const char *name, int *install)
+{
+  int rc;
+  unsigned i, pos;
+  struct _mu_assoc_elem *elem;
+  
+  if (!assoc->tab)
+    {
+      if (install)
+	{
+	  rc = assoc_rehash (assoc);
+	  if (rc)
+	    return rc;
+	}
+      else
+	return MU_ERR_NOENT;
+    }
+
+  pos = hash (name, assoc->hash_num);
+
+  for (i = pos; (elem = ASSOC_ELEM (assoc, i))->name;)
+    {
+      if (name_cmp (assoc, elem->name, name) == 0)
+	{
+	  if (install)
+	    *install = 0;
+	  *elp = elem;
+	  return 0;
+	}
+      
+      if (++i >= hash_size[assoc->hash_num])
+	i = 0;
+      if (i == pos)
+	break;
+    }
+
+  if (!install)
+    return MU_ERR_NOENT;
+  
+  if (elem->name == NULL)
+    {
+      *install = 1;
+      if (assoc->flags & MU_ASSOC_COPY_KEY)
+	elem->name = (char *) name;
+      else
+	{
+	  elem->name = strdup (name);
+	  if (!elem->name)
+	    return ENOMEM;
+	}
+      *elp = elem;
+      return 0; 
+    }
+
+  if ((rc = assoc_rehash (assoc)) != 0)
+    return rc;
+
+  return assoc_lookup_or_install (elp, assoc, name, install);
+}
+
+int
+mu_assoc_create (mu_assoc_t *passoc, size_t elsize, int flags)
+{
+  mu_assoc_t assoc = calloc (1, sizeof (*assoc));
+  if (!assoc)
+    return ENOMEM;
+  assoc->flags = flags;
+  assoc->elsize = elsize;
+  *passoc = assoc;
+  return 0;
+}
+
+void
+mu_assoc_clear (mu_assoc_t assoc)
+{
+  unsigned i, hs;
+  
+  if (!assoc || !assoc->tab)
+    return;
+
+  hs = hash_size[assoc->hash_num];
+  for (i = 0; i < hs; i++)
+    {
+      struct _mu_assoc_elem *elem = ASSOC_ELEM (assoc, i);
+      if (elem->name)
+	{
+	  assoc_free_elem (assoc, elem);
+	  elem->name = NULL;
+	}
+    }
+}
+
+void
+mu_assoc_destroy (mu_assoc_t *passoc)
+{
+  mu_assoc_t assoc;
+  if (passoc && (assoc = *passoc) != NULL)
+    {
+      mu_assoc_clear (assoc);
+      free (assoc->tab);
+      free (assoc);
+      *passoc = NULL;
+    }
+}
+
+int
+mu_assoc_set_free (mu_assoc_t assoc, mu_assoc_free_fn fn)
+{
+  if (!assoc)
+    return EINVAL;
+  assoc->free = fn;
+  return 0;
+}
+
+void *
+mu_assoc_ref (mu_assoc_t assoc, const char *name)
+{
+  int rc;
+  static struct _mu_assoc_elem *elp;
+
+  if (!assoc || !name)
+    return NULL;
+  
+  rc = assoc_lookup_or_install (&elp, assoc, name, NULL);
+  if (rc == 0)
+    return elp->data;
+  return NULL;
+}
+
+int
+mu_assoc_install (mu_assoc_t assoc, const char *name, void *value)
+{
+  int rc;
+  int inst;
+  static struct _mu_assoc_elem *elp;
+
+  if (!assoc || !name)
+    return EINVAL;
+
+  rc = assoc_lookup_or_install (&elp, assoc, name, &inst);
+  if (rc)
+    return rc;
+  if (!inst)
+    return MU_ERR_EXISTS;
+  memcpy (elp->data, value, assoc->elsize);
+  return 0;
+}
+
+int
+mu_assoc_ref_install (mu_assoc_t assoc, const char *name, void **pval)
+{
+  int rc;
+  int inst;
+  static struct _mu_assoc_elem *elp;
+
+  if (!assoc || !name)
+    return EINVAL;
+
+  rc = assoc_lookup_or_install (&elp, assoc, name, &inst);
+  if (rc)
+    return rc;
+  *pval = elp->data;
+  return inst ? 0 : MU_ERR_EXISTS;
+}  
+
+int
+mu_assoc_remove (mu_assoc_t assoc, const char *name)
+{
+  int rc;
+  static struct _mu_assoc_elem *elem;
+
+  if (!assoc || !name)
+    return EINVAL;
+  rc = assoc_lookup_or_install (&elem, assoc, name, NULL);
+  if (rc)
+    return rc;
+  return assoc_remove (assoc, elem);
+}
+
+#define OFFSET(s,f) (size_t)(&((s*)0)->f)
+
+int
+mu_assoc_remove_ref (mu_assoc_t assoc, void *data)
+{
+  struct _mu_assoc_elem *elem;
+
+  elem = (struct _mu_assoc_elem *) ((char*)data -
+				    OFFSET(struct _mu_assoc_elem, data));
+  return assoc_remove (assoc, elem);
+}
+
+
+/* Iterator interface */
+
+struct assoc_iterator
+{
+  mu_assoc_t assoc;
+  unsigned start;
+  unsigned index;
+};
+
+static int
+first (void *owner)
+{
+  struct assoc_iterator *itr = owner;
+  mu_assoc_t assoc = itr->assoc;
+  unsigned hash_max = hash_size[assoc->hash_num];
+  unsigned i;
+  
+  for (i = 0; i < hash_max; i++)
+    if ((ASSOC_ELEM (assoc, i))->name)
+      break;
+  itr->index = i;
+  return 0;
+}
+
+static int
+next (void *owner)
+{
+  struct assoc_iterator *itr = owner;
+  mu_assoc_t assoc = itr->assoc;
+  unsigned hash_max = hash_size[assoc->hash_num];
+  unsigned i;
+  
+  for (i = itr->index + 1; i < hash_max; i++)
+    if ((ASSOC_ELEM (assoc, i))->name)
+      break;
+
+  itr->index = i;
+  return 0;
+}
+
+static int
+getitem (void *owner, void **pret, const void **pkey)
+{
+  struct assoc_iterator *itr = owner;
+  struct _mu_assoc_elem *elem;
+
+  if (itr->index >= hash_size[itr->assoc->hash_num])
+    return EINVAL;
+  elem = ASSOC_ELEM (itr->assoc, itr->index);
+  *pret = elem->data;
+  if (pkey)
+    *pkey = elem->name;
+  return 0;
+}
+
+static int
+finished_p (void *owner)
+{
+  struct assoc_iterator *itr = owner;
+  return itr->index >= hash_size[itr->assoc->hash_num];
+}
+
+static int
+destroy (mu_iterator_t iterator, void *data)
+{
+  struct assoc_iterator *itr = data;
+  mu_iterator_detach (&itr->assoc->itr, iterator);
+  free (data);
+  return 0;
+}
+
+static int
+curitem_p (void *owner, void *item)
+{
+  struct assoc_iterator *itr = owner;
+  mu_assoc_t assoc = itr->assoc;
+  struct _mu_assoc_elem *elem = ASSOC_ELEM (assoc, itr->index);
+  
+  return elem == item;
+}
+
+static int
+assoc_data_dup (void **ptr, void *owner)
+{
+  *ptr = malloc (sizeof (struct assoc_iterator));
+  if (*ptr == NULL)
+    return ENOMEM;
+  memcpy (*ptr, owner, sizeof (struct assoc_iterator));
+  return 0;
+}
+
+int
+mu_assoc_get_iterator (mu_assoc_t assoc, mu_iterator_t *piterator)
+{
+  mu_iterator_t iterator;
+  int status;
+  struct assoc_iterator *itr;
+
+  if (!assoc)
+    return EINVAL;
+
+  itr = calloc (1, sizeof *itr);
+  if (!itr)
+    return ENOMEM;
+  itr->assoc = assoc;
+  itr->index = 0;
+
+  status = mu_iterator_create (&iterator, itr);
+  if (status)
+    {
+      free (itr);
+      return status;
+    }
+
+  mu_iterator_set_first (iterator, first);
+  mu_iterator_set_next (iterator, next);
+  mu_iterator_set_getitem (iterator, getitem);
+  mu_iterator_set_finished_p (iterator, finished_p);
+  mu_iterator_set_curitem_p (iterator, curitem_p);
+  mu_iterator_set_destroy (iterator, destroy);
+  mu_iterator_set_dup (iterator, assoc_data_dup);
+  
+  mu_iterator_attach (&assoc->itr, iterator);
+
+  *piterator = iterator;
+  return 0;
+}  
+
+
+
+int
+mu_assoc_count (mu_assoc_t assoc, size_t *pcount)
+{
+  mu_iterator_t itr;
+  int rc;
+  size_t count = 0;
+  
+  if (!assoc || !pcount)
+    return EINVAL;
+  rc = mu_assoc_get_iterator (assoc, &itr);
+  if (rc)
+    return rc;
+  for (mu_iterator_first (itr); !mu_iterator_is_done (itr);
+       mu_iterator_next (itr))
+    count++;
+  mu_iterator_destroy (&itr);
+  *pcount = count;
+  return 0;
+}
+
diff --git a/libmailutils/base/daemon.c b/libmailutils/base/daemon.c
new file mode 100644
index 0000000..b5e0df6
--- /dev/null
+++ b/libmailutils/base/daemon.c
@@ -0,0 +1,188 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 2004, 2005, 2007, 2008, 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, see
+   <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <string.h>
+
+#include <mailutils/daemon.h>
+#include <mailutils/errno.h>
+#include <mailutils/error.h>
+#include <mailutils/nls.h>
+
+static char *pidfile;
+static pid_t current_pid;
+
+/* Return 0 if DIR is writable for EUID/EGID.
+   Otherwise, return error code. */
+static int
+ewraccess (const char *dir)
+{
+  struct stat st;
+  if (stat (dir, &st))
+    return errno;
+  if ((st.st_mode & S_IWOTH)
+      || (st.st_gid == getegid () && (st.st_mode & S_IWGRP))
+      || (st.st_uid == geteuid () && (st.st_mode & S_IWUSR)))
+    return 0;
+  else
+    return EACCES;
+}
+
+/* Return 0 if DIR is writable. If necessary and possible, raise to
+   EUID 0, in that case return prior EUID in the memory location pointed to
+   by PUID. */
+static int
+access_dir (const char *dir, uid_t *puid)
+{
+  int ec = ewraccess (dir);
+  if (ec)
+    {
+      if (ec == EACCES && access (dir, W_OK) == 0)
+	{
+	  uid_t uid = geteuid ();
+	  /* See if we can become root */
+	  if (uid && getuid () == 0 && seteuid (0) == 0)
+	    {
+	      *puid = uid;
+	      return 0;
+	    }
+	}
+    }
+  return ec;
+}
+
+int
+mu_daemon_create_pidfile (const char *filename)
+{
+  char *p;
+  int fd;
+  uid_t uid = 0;
+  int rc;
+  
+  if (filename[0] != '/')
+    return EINVAL;
+
+  if (pidfile)
+    free (pidfile);
+  pidfile = strdup (filename);
+  if (!pidfile)
+    return ENOMEM;
+
+  /* Determine the hosting directory name */
+  p = strrchr (pidfile, '/');
+  if (pidfile == p)
+    {
+      free (pidfile);
+      pidfile = NULL;
+      /* Sorry, pidfiles in root dir are not allowed */
+      return EINVAL;
+    }
+  /* Check if we have write access to the directory */
+  *p = 0;
+  rc = access_dir (pidfile, &uid);
+  if (rc)
+    {
+      /* Nope, clean up and return */
+      free (pidfile);
+      pidfile = NULL;
+      return rc;
+    }
+
+  /* Restore directory separator */
+  *p = '/';
+    
+  unlink (pidfile);
+  current_pid = getpid ();
+  
+  if ((fd = open (pidfile, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0644)) != -1)
+    {
+      FILE *fp = fdopen (fd, "w");
+      if (!fp)
+	{
+	  rc = errno;
+	  free (pidfile);
+	  close (fd);
+	}
+      else
+	{
+	  fprintf (fp, "%lu", (unsigned long) current_pid);
+	  fclose (fp);
+	  atexit (mu_daemon_remove_pidfile);
+	}
+    }
+  else
+    {
+      rc = errno;
+      free (pidfile);
+      pidfile = NULL;
+    }
+
+  /* Restore previous EUID value. */
+  if (uid)
+    seteuid (uid);
+  
+  return rc;
+}
+
+void
+mu_daemon_remove_pidfile (void)
+{
+  if (getpid () == current_pid)
+    {
+      int rc;
+      uid_t uid = 0;
+
+      /* Determine the hosting directory name */
+      char *p = strrchr (pidfile, '/');
+      if (pidfile == p)
+	{
+	  /* Should not happen */
+	  abort ();
+	}
+      /* Check if we have write access to the directory */
+      *p = 0;
+      rc = access_dir (pidfile, &uid);
+      *p = '/';
+      if (rc == 0)
+	{
+	  if (unlink (pidfile) && errno != ENOENT)
+	    rc = errno;
+	  else
+	    rc = 0;
+	}
+      
+      if (rc)
+	mu_error (_("cannot remove pidfile %s: %s"),
+		  pidfile, mu_strerror (rc));
+
+      free (pidfile);
+      pidfile = NULL;
+    }
+}
+
+
+
diff --git a/libmailutils/base/date.c b/libmailutils/base/date.c
new file mode 100644
index 0000000..efb187b
--- /dev/null
+++ b/libmailutils/base/date.c
@@ -0,0 +1,244 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 2002, 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, see
+   <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <mailutils/mutil.h>
+#include <mailutils/cstr.h>
+
+#define SECS_PER_DAY 86400
+#define ADJUSTMENT -719162L
+
+static time_t
+jan1st (int year)
+{
+  year--;               /* Do not consider the current year */
+  return  year*365L
+               + year/4L    /* Years divisible by 4 are leap years */
+               + year/400L  /* Years divisible by 400 are always leap years */
+               - year/100L; /* Years divisible by 100 but not 400 aren't */
+}
+
+static int  month_start[]=
+    {    0, 31, 59,  90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
+     /* Jan Feb Mar  Apr  May  Jun  Jul  Aug  Sep  Oct  Nov  Dec
+         31  28  31   30   31   30   31   31   30   31   30   31
+     */
+
+/* NOTE: ignore GCC warning. The precedence of operators is OK here */
+#define leap_year(y) ((y) % 4 == 0 && (y) % 100 != 0 || (y) % 400 == 0)
+
+static int
+dayofyear (time_t *pday, int year, int month, int day)
+{
+  int  leap, month_days;
+
+  if (year < 0 || month < 0 || month > 11)
+    return -1;
+    
+  leap = leap_year (year);
+  
+  month_days = month_start[month + 1] - month_start[month]
+               + ((month == 2) ? leap : 0);
+
+  if (day < 0 || day > month_days)
+    return -1;  /* Illegal Date */
+
+  if (month <= 2)
+    leap = 0;
+
+  *pday = month_start[month] + day + leap;
+  return 0;
+}
+
+
+/* Convert struct tm into time_t, taking into account timezone offset. */
+/* FIXME: It does not take DST into account */
+time_t
+mu_tm2time (struct tm *tm, mu_timezone *tz)
+{
+  time_t t;
+  
+  if (dayofyear (&t, tm->tm_year, tm->tm_mon, tm->tm_mday - 1))
+    return -1;
+  t = (t + ADJUSTMENT + jan1st (1900 + tm->tm_year)) * SECS_PER_DAY
+            + (tm->tm_hour * 60 + tm->tm_min) * 60 + tm->tm_sec
+      - tz->utc_offset;
+  return t;
+}
+
+/* Convert time 0 at UTC to our localtime, that tells us the offset
+   of our current timezone from UTC. */
+time_t
+mu_utc_offset (void)
+{
+  time_t t = 0;
+  struct tm *tm = gmtime (&t);
+
+  return - mktime (tm);
+}
+
+static const char *months[] =
+{
+  "Jan", "Feb", "Mar", "Apr", "May", "Jun",
+  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", NULL
+};
+
+static const char *wdays[] =
+{
+  "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL
+};
+
+int
+mu_parse_imap_date_time (const char **p, struct tm *tm, mu_timezone *tz)
+{
+  int year, mon, day, hour, min, sec;
+  char zone[6] = "+0000";	/* ( "+" / "-" ) hhmm */
+  char month[5] = "";
+  int hh = 0;
+  int mm = 0;
+  int sign = 1;
+  int scanned = 0, scanned3;
+  int i;
+  int tzoffset;
+
+  day = mon = year = hour = min = sec = 0;
+
+  memset (tm, 0, sizeof (*tm));
+
+  switch (sscanf (*p,
+		  "%2d-%3s-%4d%n %2d:%2d:%2d %5s%n",
+		  &day, month, &year, &scanned3, &hour, &min, &sec, zone,
+		  &scanned))
+    {
+    case 3:
+      scanned = scanned3;
+      break;
+    case 7:
+      break;
+    default:
+      return -1;
+    }
+
+  tm->tm_sec = sec;
+  tm->tm_min = min;
+  tm->tm_hour = hour;
+  tm->tm_mday = day;
+
+  for (i = 0; i < 12; i++)
+    {
+      if (mu_c_strncasecmp (month, months[i], 3) == 0)
+	{
+	  mon = i;
+	  break;
+	}
+    }
+  tm->tm_mon = mon;
+  tm->tm_year = (year > 1900) ? year - 1900 : year;
+  tm->tm_yday = 0;		/* unknown. */
+  tm->tm_wday = 0;		/* unknown. */
+#if HAVE_STRUCT_TM_TM_ISDST
+  tm->tm_isdst = -1;		/* unknown. */
+#endif
+
+  hh = (zone[1] - '0') * 10 + (zone[2] - '0');
+  mm = (zone[3] - '0') * 10 + (zone[4] - '0');
+  sign = (zone[0] == '-') ? -1 : +1;
+  tzoffset = sign * (hh * 60 * 60 + mm * 60);
+
+#if HAVE_STRUCT_TM_TM_GMTOFF
+  tm->tm_gmtoff = tzoffset;
+#endif
+
+  if (tz)
+    {
+      tz->utc_offset = tzoffset;
+      tz->tz_name = NULL;
+    }
+
+  *p += scanned;
+
+  return 0;
+}
+
+/* "ctime" format is: Thu Jul 01 15:58:27 1999, with no trailing \n.  */
+int
+mu_parse_ctime_date_time (const char **p, struct tm *tm, mu_timezone * tz)
+{
+  int wday = 0;
+  int year = 0;
+  int mon = 0;
+  int day = 0;
+  int hour = 0;
+  int min = 0;
+  int sec = 0;
+  int n = 0;
+  int i;
+  char weekday[5] = "";
+  char month[5] = "";
+
+  if (sscanf (*p, "%3s %3s %2d %2d:%2d:%2d %d%n\n",
+	weekday, month, &day, &hour, &min, &sec, &year, &n) != 7)
+    return -1;
+
+  *p += n;
+
+  for (i = 0; i < 7; i++)
+    {
+      if (mu_c_strncasecmp (weekday, wdays[i], 3) == 0)
+	{
+	  wday = i;
+	  break;
+	}
+    }
+
+  for (i = 0; i < 12; i++)
+    {
+      if (mu_c_strncasecmp (month, months[i], 3) == 0)
+	{
+	  mon = i;
+	  break;
+	}
+    }
+
+  if (tm)
+    {
+      memset (tm, 0, sizeof (struct tm));
+
+      tm->tm_sec = sec;
+      tm->tm_min = min;
+      tm->tm_hour = hour;
+      tm->tm_mday = day;
+      tm->tm_wday = wday;
+      tm->tm_mon = mon;
+      tm->tm_year = (year > 1900) ? year - 1900 : year;
+#ifdef HAVE_STRUCT_TM_TM_ISDST
+      tm->tm_isdst = -1;	/* unknown. */
+#endif
+    }
+
+  /* ctime has no timezone information, set tz to UTC if they ask. */
+  if (tz)
+    memset (tz, 0, sizeof (struct mu_timezone));
+
+  return 0;
+}
diff --git a/libmailutils/base/fgetpwent.c b/libmailutils/base/fgetpwent.c
new file mode 100644
index 0000000..79f6be3
--- /dev/null
+++ b/libmailutils/base/fgetpwent.c
@@ -0,0 +1,162 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 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, see 
+   <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <stdio.h>
+#include <pwd.h>
+#include <string.h>
+#include <stdlib.h>
+
+/*
+  Written by Alain Magloire.
+  Simple replacement for fgetpwent(), it is not :
+  - thread safe;
+  - static buffer was not use since it will limit the size
+    of the entry.  But rather memory is allocated and __never__
+    release.  The memory will grow if need be.
+  - no support for shadow
+  - no support for NIS(+)
+*/
+
+static char *buffer;
+static size_t buflen;
+static struct passwd pw;
+
+static char *
+parse_line (char *s, char **p)
+{
+  if (*s)
+    {
+      char *sep = strchr (s, ':');
+      if (sep)
+	{
+	  *sep++ = '\0';
+	  *p = sep;
+	}
+      else
+	*p = s + strlen (s);
+    }
+  else
+    *p = s;
+  return s;
+}
+
+static struct passwd *
+getentry (char *s)
+{
+  char *p;
+  pw.pw_name = parse_line (s, &p);
+  s = p;
+  pw.pw_passwd = parse_line (s, &p);
+  s = p;
+  pw.pw_uid = strtoul (parse_line (s, &p), NULL, 10);
+  s = p;
+  pw.pw_gid = strtoul (parse_line (s, &p), NULL, 10);
+  s = p;
+  pw.pw_gecos = parse_line (s, &p);
+  s = p;
+  pw.pw_dir = parse_line (s, &p);
+  s = p;
+  pw.pw_shell = parse_line (s, &p);
+  return &pw;
+}
+
+struct passwd *
+mu_fgetpwent (FILE *fp)
+{
+  size_t pos = 0;
+  int done = 0;
+  struct passwd *pw = NULL;
+
+  /* Allocate buffer if not yet available.  */
+  /* This buffer will be never free().  */
+  if (buffer == NULL)
+    {
+      buflen = 1024;
+      buffer = malloc (buflen);
+      if (buffer == NULL)
+	return NULL;
+    }
+
+  do
+    {
+      if (fgets (buffer + pos, buflen, fp) != NULL)
+	{
+	  /* Need a full line.  */
+	  if (buffer[strlen (buffer) - 1] == '\n')
+	    {
+	      /* reset marker position.  */
+	      pos = 0;
+	      /* Nuke trailing newline.  */
+	      buffer[strlen (buffer) - 1] = '\0';
+
+	      /* Skip comments.  */
+	      if (buffer[0] != '#')
+		{
+		  done = 1;
+		  pw = getentry (buffer);
+		}
+	    }
+	  else
+	    {
+	      /* Line is too long reallocate the buffer.  */
+	      char *tmp;
+	      pos = strlen (buffer);
+	      buflen *= 2;
+	      tmp = realloc (buffer, buflen);
+	      if (tmp)
+		buffer = tmp;
+	      else
+		done = 1;
+	    }
+	}
+      else
+	done = 1;
+    } while (!done);
+
+  return pw;
+
+}
+
+#ifdef STANDALONE
+int
+main ()
+{
+  FILE *fp = fopen ("/etc/passwd", "r");
+  if (fp)
+    {
+      struct passwd *pwd;
+      while ((pwd = fgetpwent (fp)))
+        {
+          printf ("--------------------------------------\n");
+          printf ("name %s\n", pwd->pw_name);
+          printf ("passwd %s\n", pwd->pw_passwd);
+          printf ("uid %d\n", pwd->pw_uid);
+          printf ("gid %d\n", pwd->pw_gid);
+          printf ("gecos %s\n", pwd->pw_gecos);
+          printf ("dir %s\n", pwd->pw_dir);
+          printf ("shell %s\n", pwd->pw_shell);
+        }
+    }
+  return 0;
+}
+
+#endif
diff --git a/libmailutils/base/freeitem.c b/libmailutils/base/freeitem.c
new file mode 100644
index 0000000..ad382a4
--- /dev/null
+++ b/libmailutils/base/freeitem.c
@@ -0,0 +1,28 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 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, see 
+   <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+#include <stdlib.h>
+
+/* Default destroy_item function. */
+void
+mu_list_free_item (void *item)
+{
+  free (item);
+}
diff --git a/libmailutils/getpass.c b/libmailutils/base/getpass.c
similarity index 100%
rename from libmailutils/getpass.c
rename to libmailutils/base/getpass.c
diff --git a/libmailutils/base/iterator.c b/libmailutils/base/iterator.c
new file mode 100644
index 0000000..97f7b75
--- /dev/null
+++ b/libmailutils/base/iterator.c
@@ -0,0 +1,283 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2004, 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, see
+   <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <errno.h>
+#include <stdlib.h>
+
+#include <mailutils/sys/list.h>
+#include <mailutils/sys/iterator.h>
+#include <mailutils/errno.h>
+
+int
+mu_iterator_create (mu_iterator_t *piterator, void *owner)
+{
+  mu_iterator_t iterator;
+  if (piterator == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+  if (owner == NULL)
+    return EINVAL;
+  iterator = calloc (1, sizeof (*iterator));
+  if (iterator == NULL)
+    return ENOMEM;
+  iterator->owner = owner;
+  *piterator = iterator;
+  return 0;
+}
+
+int
+mu_iterator_set_first (mu_iterator_t itr, int (*first) (void *))
+{
+  if (!itr)
+    return EINVAL;
+  itr->first = first;
+  return 0;
+}
+
+int
+mu_iterator_set_next (mu_iterator_t itr, int (*next) (void *))
+{
+  if (!itr)
+    return EINVAL;
+  itr->next = next;
+  return 0;
+}
+
+int
+mu_iterator_set_getitem (mu_iterator_t itr,
+                         int (*getitem) (void *, void **, const void **))
+{
+  if (!itr)
+    return EINVAL;
+  itr->getitem = getitem;
+  return 0;
+}
+
+int
+mu_iterator_set_finished_p (mu_iterator_t itr, int (*finished_p) (void *))
+{
+  if (!itr)
+    return EINVAL;
+  itr->finished_p = finished_p;
+  return 0;
+}
+
+int
+mu_iterator_set_curitem_p (mu_iterator_t itr,
+			int (*curitem_p) (void *, void *))
+{
+  if (!itr)
+    return EINVAL;
+  itr->curitem_p = curitem_p;
+  return 0;
+}
+
+int
+mu_iterator_set_itrctl (mu_iterator_t itr,
+			int (*itrctl) (void *,
+				       enum mu_itrctl_req,
+				       void *))
+{
+  if (!itr)
+    return EINVAL;
+  itr->itrctl = itrctl;
+  return 0;
+}
+
+int
+mu_iterator_set_destroy (mu_iterator_t itr, int (*destroy) (mu_iterator_t, void *))
+{
+  if (!itr)
+    return EINVAL;
+  itr->destroy = destroy;
+  return 0;
+}
+
+int
+mu_iterator_set_dup (mu_iterator_t itr, int (*dup) (void **ptr, void *data))
+{
+  if (!itr)
+    return EINVAL;
+  itr->dup = dup;
+  return 0;
+}
+
+
+
+int
+mu_iterator_dup (mu_iterator_t *piterator, mu_iterator_t orig)
+{
+  mu_iterator_t iterator;
+  int status;
+  
+  if (piterator == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+  if (orig == NULL)
+    return EINVAL;
+
+  status = mu_iterator_create (&iterator, orig->owner);
+  if (status)
+    return status;
+
+  status = orig->dup(&iterator->owner, orig->owner);
+  if (status)
+    {
+      free (iterator);
+      return status;
+    }
+  iterator->is_advanced = orig->is_advanced;   
+  iterator->dup = orig->dup;
+  iterator->destroy = orig->destroy;
+  iterator->first = orig->first;
+  iterator->next = orig->next;
+  iterator->getitem = orig->getitem;
+  iterator->curitem_p = orig->curitem_p;
+  iterator->finished_p = orig->finished_p;
+  iterator->itrctl = orig->itrctl;
+  
+  *piterator = iterator;
+  return 0;
+}
+
+void
+mu_iterator_destroy (mu_iterator_t *piterator)
+{
+  if (!piterator || !*piterator)
+    return;
+
+  if ((*piterator)->destroy)
+    (*piterator)->destroy (*piterator, (*piterator)->owner);
+  
+  free (*piterator);
+  *piterator = NULL;
+}
+
+int
+mu_iterator_first (mu_iterator_t iterator)
+{
+  iterator->is_advanced = 0;
+  return iterator->first (iterator->owner);
+}
+
+int
+mu_iterator_next (mu_iterator_t iterator)
+{
+  int status = 0;
+  if (!iterator->is_advanced)
+    status = iterator->next (iterator->owner);
+  iterator->is_advanced = 0;
+  return status;
+}
+
+int
+mu_iterator_skip (mu_iterator_t iterator, ssize_t count)
+{
+  int status;
+  if (count < 0)
+    return ENOSYS; /* Need prev method */
+  while (count--)
+    if ((status = mu_iterator_next (iterator)))
+      break;
+  return status;
+}
+
+int
+mu_iterator_current (mu_iterator_t iterator, void **pitem)
+{
+  return iterator->getitem (iterator->owner, pitem, NULL);
+}
+
+int
+mu_iterator_current_kv (mu_iterator_t iterator, 
+                        const void **pkey, void **pitem)
+{
+  return iterator->getitem (iterator->owner, (void**)pitem, pkey);
+}
+
+int
+mu_iterator_is_done (mu_iterator_t iterator)
+{
+  if (iterator == NULL)
+    return 1;
+  return iterator->finished_p (iterator->owner);
+}
+
+int
+iterator_get_owner (mu_iterator_t iterator, void **powner)
+{
+  if (!iterator)
+    return EINVAL;
+  if (!powner)
+    return MU_ERR_OUT_PTR_NULL;
+  *powner = iterator->owner;
+  return 0;
+}
+
+void
+mu_iterator_advance (mu_iterator_t iterator, void *e)
+{
+  for (; iterator; iterator = iterator->next_itr)
+    {
+      if (iterator->curitem_p (iterator->owner, e))
+	{
+	  iterator->next (iterator->owner);
+	  iterator->is_advanced++;
+	}
+    }
+}
+
+int
+mu_iterator_attach (mu_iterator_t *root, mu_iterator_t iterator)
+{
+  iterator->next_itr = *root;
+  *root = iterator;
+  return 0;
+}
+
+int
+mu_iterator_detach (mu_iterator_t *root, mu_iterator_t iterator)
+{
+  mu_iterator_t itr, prev;
+  
+  for (itr = *root, prev = NULL; itr; prev = itr, itr = itr->next_itr)
+    if (iterator == itr)
+      break;
+
+  if (itr)
+    {
+      if (prev)
+	prev->next_itr = itr->next_itr;
+      else
+	*root = itr->next_itr;
+    }
+  
+  return 0;
+}
+
+int
+mu_iterator_ctl (mu_iterator_t iterator, enum mu_itrctl_req req, void *arg)
+{
+  if (!iterator)
+    return EINVAL;
+  if (!iterator->itrctl)
+    return ENOSYS;
+  return iterator->itrctl (iterator->owner, req, arg);
+}
diff --git a/libmailutils/kwd.c b/libmailutils/base/kwd.c
similarity index 100%
rename from libmailutils/kwd.c
rename to libmailutils/base/kwd.c
diff --git a/libmailutils/base/list.c b/libmailutils/base/list.c
new file mode 100644
index 0000000..9147ef8
--- /dev/null
+++ b/libmailutils/base/list.c
@@ -0,0 +1,692 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 2004, 2005, 2007, 2008, 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, see 
+   <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <mailutils/sys/list.h>
+#include <mailutils/sys/iterator.h>
+#include <mailutils/errno.h>
+
+#define DESTROY_ITEM(list, elt)			\
+  do						\
+    {						\
+       if ((list)->destroy_item)		\
+	 (list)->destroy_item ((elt)->item);	\
+    }						\
+  while (0)
+
+int
+mu_list_create (mu_list_t *plist)
+{
+  mu_list_t list;
+  int status;
+
+  if (plist == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+  list = calloc (sizeof (*list), 1);
+  if (list == NULL)
+    return ENOMEM;
+  status = mu_monitor_create (&list->monitor, 0,  list);
+  if (status != 0)
+    {
+      free (list);
+      return status;
+    }
+  list->head.next = &list->head;
+  list->head.prev = &list->head;
+  *plist = list;
+  return 0;
+}
+
+void
+mu_list_clear (mu_list_t list)
+{
+  struct list_data *current;
+  struct list_data *previous;
+  
+  mu_monitor_wrlock (list->monitor);
+  for (current = list->head.next; current != &list->head;)
+    {
+      previous = current;
+      current = current->next;
+      DESTROY_ITEM (list, previous);
+      free (previous);
+    }
+  list->head.next = list->head.prev = &list->head;
+  mu_monitor_unlock (list->monitor);
+}
+
+void
+mu_list_destroy (mu_list_t *plist)
+{
+  if (plist && *plist)
+    {
+      mu_list_t list = *plist;
+      mu_list_clear (list);
+      mu_monitor_destroy (&list->monitor, list);
+      free (list);
+      *plist = NULL;
+    }
+}
+
+int
+mu_list_append (mu_list_t list, void *item)
+{
+  struct list_data *ldata;
+  struct list_data *last;
+
+  if (list == NULL)
+    return EINVAL;
+  last = list->head.prev;
+  ldata = calloc (sizeof (*ldata), 1);
+  if (ldata == NULL)
+    return ENOMEM;
+  ldata->item = item;
+  mu_monitor_wrlock (list->monitor);
+  ldata->next = &list->head;
+  ldata->prev = list->head.prev;
+  last->next = ldata;
+  list->head.prev = ldata;
+  list->count++;
+  mu_monitor_unlock (list->monitor);
+  return 0;
+}
+
+int
+mu_list_prepend (mu_list_t list, void *item)
+{
+  struct list_data *ldata;
+  struct list_data *first;
+
+  if (list == NULL)
+    return EINVAL;
+  first = list->head.next;
+  ldata = calloc (sizeof (*ldata), 1);
+  if (ldata == NULL)
+    return ENOMEM;
+  ldata->item = item;
+  mu_monitor_wrlock (list->monitor);
+  ldata->prev = &list->head;
+  ldata->next = list->head.next;
+  first->prev = ldata;
+  list->head.next = ldata;
+  list->count++;
+  mu_monitor_unlock (list->monitor);
+  return 0;
+}
+
+int
+mu_list_is_empty (mu_list_t list)
+{
+  size_t n = 0;
+
+  mu_list_count (list, &n);
+  return (n == 0);
+}
+
+int
+mu_list_count (mu_list_t list, size_t *pcount)
+{
+  if (list == NULL)
+    return EINVAL;
+  if (pcount == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+  *pcount = list->count;
+  return 0;
+}
+
+mu_list_comparator_t
+mu_list_set_comparator (mu_list_t list, mu_list_comparator_t comp)
+{
+  mu_list_comparator_t old_comp;
+
+  if (list == NULL)
+    return NULL;
+  old_comp = list->comp;
+  list->comp = comp;
+  return old_comp;
+}
+
+int
+mu_list_get_comparator (mu_list_t list, mu_list_comparator_t *comp)
+{
+  if (!list)
+    return EINVAL;
+  *comp = list->comp;
+  return 0;
+}
+
+int
+_mu_list_ptr_comparator (const void *item, const void *value)
+{
+  return item != value;
+}
+
+int
+mu_list_locate (mu_list_t list, void *item, void **ret_item)
+{
+  struct list_data *current, *previous;
+  mu_list_comparator_t comp;
+  int status = MU_ERR_NOENT;
+
+  if (list == NULL)
+    return EINVAL;
+  comp = list->comp ? list->comp : _mu_list_ptr_comparator;
+  mu_monitor_wrlock (list->monitor);
+  for (previous = &list->head, current = list->head.next;
+       current != &list->head; previous = current, current = current->next)
+    {
+      if (comp (current->item, item) == 0)
+	{
+	  if (ret_item)
+	    *ret_item = current->item;
+	  status = 0;
+	  break;
+	}
+    }
+  mu_monitor_unlock (list->monitor);
+  return status;
+}
+
+static int
+_insert_item (mu_list_t list, struct list_data *current, void *new_item,
+	      int insert_before)
+{
+  int status;
+  struct list_data *ldata = calloc (sizeof (*ldata), 1);
+  if (ldata == NULL)
+    status = ENOMEM;
+  else
+    {
+      ldata->item = new_item;
+      _mu_list_insert_sublist (list, current,
+			       ldata, ldata,
+			       1,
+			       insert_before);
+      status = 0;
+    }
+  return status;
+}
+
+int
+mu_list_insert (mu_list_t list, void *item, void *new_item, int insert_before)
+{
+  struct list_data *current;
+  mu_list_comparator_t comp;
+  int status = MU_ERR_NOENT;
+
+  if (list == NULL)
+    return EINVAL;
+  comp = list->comp ? list->comp : _mu_list_ptr_comparator;
+
+  mu_monitor_wrlock (list->monitor);
+  for (current = list->head.next;
+       current != &list->head;
+       current = current->next)
+    {
+      if (comp (current->item, item) == 0)
+	{
+	  status = _insert_item (list, current, new_item, insert_before);
+	  break;
+	}
+    }
+  mu_monitor_unlock (list->monitor);
+  return status;
+}
+
+int
+mu_list_remove (mu_list_t list, void *item)
+{
+  struct list_data *current;
+  mu_list_comparator_t comp;
+  int status = MU_ERR_NOENT;
+
+  if (list == NULL)
+    return EINVAL;
+  comp = list->comp ? list->comp : _mu_list_ptr_comparator;
+  mu_monitor_wrlock (list->monitor);
+  for (current = list->head.next;
+       current != &list->head; current = current->next)
+    {
+      if (comp (current->item, item) == 0)
+	{
+	  struct list_data *previous = current->prev;
+	  
+	  mu_iterator_advance (list->itr, current);
+	  previous->next = current->next;
+	  current->next->prev = previous;
+	  DESTROY_ITEM (list, current);
+	  free (current);
+	  list->count--;
+	  status = 0;
+	  break;
+	}
+    }
+  mu_monitor_unlock (list->monitor);
+  return status;
+}
+
+int
+mu_list_remove_nd (mu_list_t list, void *item)
+{
+  mu_list_destroy_item_t dptr = mu_list_set_destroy_item (list, NULL);
+  int rc = mu_list_remove (list, item);
+  mu_list_set_destroy_item (list, dptr);
+  return rc;
+}
+
+int
+mu_list_replace (mu_list_t list, void *old_item, void *new_item)
+{
+  struct list_data *current, *previous;
+  mu_list_comparator_t comp;
+  int status = MU_ERR_NOENT;
+
+  if (list == NULL)
+    return EINVAL;
+  comp = list->comp ? list->comp : _mu_list_ptr_comparator;
+  mu_monitor_wrlock (list->monitor);
+  for (previous = &list->head, current = list->head.next;
+       current != &list->head; previous = current, current = current->next)
+    {
+      if (comp (current->item, old_item) == 0)
+	{
+	  DESTROY_ITEM (list, current);
+	  current->item = new_item;
+	  status = 0;
+	  break;
+	}
+    }
+  mu_monitor_unlock (list->monitor);
+  return status;
+}
+
+int
+mu_list_replace_nd (mu_list_t list, void *item, void *new_item)
+{
+  mu_list_destroy_item_t dptr = mu_list_set_destroy_item (list, NULL);
+  int rc = mu_list_replace (list, item, new_item);
+  mu_list_set_destroy_item (list, dptr);
+  return rc;
+}
+
+int
+mu_list_get (mu_list_t list, size_t indx, void **pitem)
+{
+  struct list_data *current;
+  size_t count;
+  int status = MU_ERR_NOENT;
+
+  if (list == NULL)
+    return EINVAL;
+  if (pitem == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+  mu_monitor_rdlock (list->monitor);
+  for (current = list->head.next, count = 0; current != &list->head;
+       current = current->next, count++)
+    {
+      if (count == indx)
+        {
+          *pitem = current->item;
+	  status = 0;
+	  break;
+        }
+    }
+  mu_monitor_unlock (list->monitor);
+  return status;
+}
+
+int
+mu_list_do (mu_list_t list, mu_list_action_t *action, void *cbdata)
+{
+  mu_iterator_t itr;
+  int status = 0;
+
+  if (list == NULL || action == NULL)
+    return EINVAL;
+  status = mu_list_get_iterator (list, &itr);
+  if (status)
+    return status;
+  for (mu_iterator_first (itr); !mu_iterator_is_done (itr);
+       mu_iterator_next (itr))
+    {
+      void *item;
+      mu_iterator_current (itr, &item);
+      if ((status = action (item, cbdata)))
+	break;
+    }
+  mu_iterator_destroy (&itr);
+  return status;
+}
+
+mu_list_destroy_item_t
+mu_list_set_destroy_item (mu_list_t list, void (*destroy_item)(void *item))
+{
+  mu_list_destroy_item_t ret = list->destroy_item;
+  list->destroy_item = destroy_item;
+  return ret;
+}
+
+int
+mu_list_to_array (mu_list_t list, void **array, size_t count, size_t *pcount)
+{
+  size_t total = 0;
+
+  if (!list)
+    return EINVAL;
+
+  total = (count < list->count) ? count : list->count;
+
+  if (array)
+    {
+      size_t i;
+      struct list_data *current;
+
+      for (i = 0, current = list->head.next;
+	   i < total && current != &list->head; current = current->next)
+	array[i++] = current->item;
+    }
+  if (pcount)
+    *pcount = total;
+  return 0;
+}
+
+/* Computes an intersection of two lists and returns it in PDEST.
+   The resulting list contains elements from A that are
+   also encountered in B (as per comparison function of
+   the latter).
+
+   If DUP_ITEM is not NULL, it is used to create copies of
+   items to be stored in PDEST.  In this case, the destroy_item
+   function of B is also attached to PDEST.  Otherwise, if
+   DUP_ITEM is NULL, pointers to elements are stored and
+   no destroy_item function is assigned. */
+int
+mu_list_intersect_dup (mu_list_t *pdest, mu_list_t a, mu_list_t b,
+		       int (*dup_item) (void **, void *, void *),
+		       void *dup_closure)
+{
+  mu_list_t dest;
+  int rc;
+  mu_iterator_t itr;
+  
+  rc = mu_list_create (&dest);
+  if (rc)
+    return rc;
+
+  mu_list_set_comparator (dest, b->comp);
+  if (dup_item)
+    mu_list_set_destroy_item (dest, b->destroy_item);
+  
+  rc = mu_list_get_iterator (a, &itr);
+  if (rc)
+    {
+      mu_list_destroy (&dest);
+      return rc;
+    }
+
+  rc = 0;
+  for (mu_iterator_first (itr); !mu_iterator_is_done (itr);
+       mu_iterator_next (itr))
+    {
+      void *data;
+      mu_iterator_current (itr, &data);
+      if (mu_list_locate (b, data, NULL) == 0)
+	{
+	  void *new_data;
+	  if (dup_item && data)
+	    {
+	      rc = dup_item (&new_data, data, dup_closure);
+	      if (rc)
+		break;
+	    }
+	  else
+	    new_data = data;
+	
+	  mu_list_append (dest, new_data); /* FIXME: Check return, and? */
+	}
+    }
+  mu_iterator_destroy (&itr);
+  *pdest = dest;
+  return rc;
+}
+
+int
+mu_list_intersect (mu_list_t *pdest, mu_list_t a, mu_list_t b)
+{
+  return mu_list_intersect_dup (pdest, a, b, NULL, NULL);
+}
+
+
+/* Iterator interface */
+
+struct list_iterator
+{
+  mu_list_t list;
+  struct list_data *cur;
+  int backwards; /* true if iterating backwards */
+};
+
+static int
+first (void *owner)
+{
+  struct list_iterator *itr = owner;
+  if (itr->backwards)
+    itr->cur = itr->list->head.prev;
+  else
+    itr->cur = itr->list->head.next;
+  return 0;
+}
+
+static int
+next (void *owner)
+{
+  struct list_iterator *itr = owner;
+  if (itr->backwards)
+    itr->cur = itr->cur->prev;
+  else
+    itr->cur = itr->cur->next;
+  return 0;
+}
+
+static int
+getitem (void *owner, void **pret, const void **pkey)
+{
+  struct list_iterator *itr = owner;
+  *pret = itr->cur->item;
+  if (pkey)
+    *pkey = NULL;
+  return 0;
+}
+
+static int
+finished_p (void *owner)
+{
+  struct list_iterator *itr = owner;
+  return itr->cur == &itr->list->head;
+}
+
+static int
+destroy (mu_iterator_t iterator, void *data)
+{
+  struct list_iterator *itr = data;
+  mu_iterator_detach (&itr->list->itr, iterator);
+  free (data);
+  return 0;
+}
+
+static int
+curitem_p (void *owner, void *item)
+{
+  struct list_iterator *itr = owner;
+  return itr->cur == item;
+}
+
+static int
+list_data_dup (void **ptr, void *owner)
+{
+  *ptr = malloc (sizeof (struct list_iterator));
+  if (*ptr == NULL)
+    return ENOMEM;
+  memcpy (*ptr, owner, sizeof (struct list_iterator));
+  return 0;
+}
+
+static int
+list_itrctl (void *owner, enum mu_itrctl_req req, void *arg)
+{
+  struct list_iterator *itr = owner;
+  mu_list_t list = itr->list;
+  struct list_data *ptr;
+  
+  if (itr->cur == NULL)
+    return MU_ERR_NOENT;
+  switch (req)
+    {
+    case mu_itrctl_tell:
+      /* Return current position in the object */
+      {
+	size_t count;
+
+	for (count = 0, ptr = list->head.next; ptr != &list->head;
+	     ptr = ptr->next, count++)
+	  {
+	    if (ptr == itr->cur)
+	      {
+		*(size_t*)arg = count;
+		return 0;
+	      }
+	  }
+	return MU_ERR_NOENT;
+      }
+	
+    case mu_itrctl_delete:
+    case mu_itrctl_delete_nd:
+      /* Delete current element */
+      {
+	struct list_data *prev;
+	
+	ptr = itr->cur;
+	prev = ptr->prev;
+	
+	mu_iterator_advance (list->itr, ptr);
+	prev->next = ptr->next;
+	ptr->next->prev = prev;
+	if (req == mu_itrctl_delete)
+	  DESTROY_ITEM (list, ptr);
+	free (ptr);
+	list->count--;
+      }
+      break;
+      
+    case mu_itrctl_replace:
+    case mu_itrctl_replace_nd:
+      /* Replace current element */
+      if (!arg)
+	return EINVAL;
+      ptr = itr->cur;
+      if (req == mu_itrctl_replace)
+	  DESTROY_ITEM (list, ptr);
+      ptr = itr->cur;
+      ptr->item = arg;
+      break;
+      
+    case mu_itrctl_insert:
+      /* Insert new element in the current position */
+      if (!arg)
+	return EINVAL;
+      return _insert_item (list, itr->cur, arg, 0);
+
+    case mu_itrctl_insert_list:
+      /* Insert a list of elements */
+      if (!arg)
+	return EINVAL;
+      else
+	{
+	  mu_list_t new_list = arg;
+	  _mu_list_insert_sublist (list, itr->cur,
+				   new_list->head.next, new_list->head.prev,
+				   new_list->count,
+				   0);
+	  _mu_list_clear (new_list);
+	}
+      break;
+
+    case mu_itrctl_qry_direction:
+      if (!arg)
+	return EINVAL;
+      else
+	*(int*)arg = itr->backwards;
+      break;
+
+    case mu_itrctl_set_direction:
+      if (!arg)
+	return EINVAL;
+      else
+	itr->backwards = !!*(int*)arg;
+      break;
+      
+    default:
+      return ENOSYS;
+    }
+  return 0;
+}
+
+int
+mu_list_get_iterator (mu_list_t list, mu_iterator_t *piterator)
+{
+  mu_iterator_t iterator;
+  int status;
+  struct list_iterator *itr;
+
+  if (!list)
+    return EINVAL;
+
+  itr = calloc (1, sizeof *itr);
+  if (!itr)
+    return ENOMEM;
+  itr->list = list;
+  itr->cur = NULL;
+
+  status = mu_iterator_create (&iterator, itr);
+  if (status)
+    {
+      free (itr);
+      return status;
+    }
+
+  mu_iterator_set_first (iterator, first);
+  mu_iterator_set_next (iterator, next);
+  mu_iterator_set_getitem (iterator, getitem);
+  mu_iterator_set_finished_p (iterator, finished_p);
+  mu_iterator_set_curitem_p (iterator, curitem_p);
+  mu_iterator_set_destroy (iterator, destroy);
+  mu_iterator_set_dup (iterator, list_data_dup);
+  mu_iterator_set_itrctl (iterator, list_itrctl);
+  
+  mu_iterator_attach (&list->itr, iterator);
+
+  *piterator = iterator;
+  return 0;
+}
diff --git a/libmailutils/base/listlist.c b/libmailutils/base/listlist.c
new file mode 100644
index 0000000..723c0d4
--- /dev/null
+++ b/libmailutils/base/listlist.c
@@ -0,0 +1,138 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 2004, 2005, 2007, 2008, 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, see
+   <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <mailutils/sys/list.h>
+#include <mailutils/sys/iterator.h>
+#include <mailutils/errno.h>
+
+void
+_mu_list_insert_sublist (mu_list_t list,
+			 struct list_data *current,
+			 struct list_data *head,
+			 struct list_data *tail,
+			 size_t count,
+			 int insert_before)
+{
+  if (insert_before)
+    {
+      head->prev = current->prev;
+      tail->next = current;
+      if (current->prev != &list->head)
+	current->prev->next = head;
+      else
+	list->head.next = head;
+
+      current->prev = tail;
+    }
+  else
+    {
+      tail->next = current->next;
+      head->prev = current;
+      if (current->next != &list->head)
+	current->next->prev = tail;
+      else
+	list->head.prev = tail;
+
+      current->next = head;
+    }
+  list->count += count;
+}
+
+void
+_mu_list_clear (mu_list_t list)
+{
+  list->head.next = list->head.prev = &list->head;
+  list->count = 0;
+}
+
+int
+mu_list_insert_list (mu_list_t list, void *item, mu_list_t new_list,
+		     int insert_before)
+{
+  struct list_data *current;
+  mu_list_comparator_t comp;
+  int status = MU_ERR_NOENT;
+
+  if (list == NULL)
+    return EINVAL;
+  comp = list->comp ? list->comp : _mu_list_ptr_comparator;
+
+  mu_monitor_wrlock (list->monitor);
+  for (current = list->head.next;
+       current != &list->head;
+       current = current->next)
+    {
+      if (comp (current->item, item) == 0)
+	{
+	  _mu_list_insert_sublist (list, current,
+				   new_list->head.next, new_list->head.prev,
+				   new_list->count,
+				   insert_before);
+	  _mu_list_clear (new_list);
+	  status = 0;
+	  break;
+	}
+    }
+  mu_monitor_unlock (list->monitor);
+  return status;
+}
+
+void
+mu_list_append_list (mu_list_t list, mu_list_t new_list)
+{
+  if (list->count == 0)
+    {
+      list->head = new_list->head;
+      list->head.next->prev = list->head.prev->next = &list->head;
+      list->count = new_list->count;
+    }
+  else
+    _mu_list_insert_sublist (list, list->head.prev,
+			     new_list->head.next, new_list->head.prev,
+			     new_list->count,
+			     0);
+  _mu_list_clear (new_list);
+}
+
+void
+mu_list_prepend_list (mu_list_t list, mu_list_t new_list)
+{
+  if (list->count == 0)
+    {
+      list->head = new_list->head;
+      list->head.next->prev = list->head.prev->next = &list->head;
+      list->count = new_list->count;
+    }
+  else
+    _mu_list_insert_sublist (list, list->head.next,
+			     new_list->head.next, new_list->head.prev,
+			     new_list->count,
+			     1);
+  _mu_list_clear (new_list);
+}
+
+
+
diff --git a/libmailutils/base/locale.c b/libmailutils/base/locale.c
new file mode 100644
index 0000000..4234ad4
--- /dev/null
+++ b/libmailutils/base/locale.c
@@ -0,0 +1,241 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 2003, 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, see 
+   <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <string.h>
+#include <mailutils/mutil.h>
+#include <mailutils/cstr.h>
+
+struct langtab
+{
+  char *lang;        /* Language code */
+  char *terr;        /* Territory code */
+  char *charset;     /* Corresponding charset */
+};
+
+/* The list of language codes defined in ISO 639 with the corresponding
+   default character sets.
+
+   NOTES:
+
+   1) The list must be ordered by:
+        a) lang field in ascending order
+	b) terr field in descending order.
+      NULL fields are considered less than non-null ones.
+   2) Many entries have NULL charset fields. Please help fill them!
+   3) The "default" character set for a given language is a matter
+      of preference. Possibly the table should contain a *list* of
+      possible character sets.
+   4) LC_ALL "modifier" field is not taken into account */
+
+static struct langtab langtab[] = {
+  { "C",     NULL, "ASCII"},
+  { "POSIX", NULL, "ASCII" },
+  { "aa",    NULL, NULL},              /* Afar */
+  { "ab",    NULL, NULL},              /* Abkhazian */
+  { "ae",    NULL, NULL},              /* Avestan */
+  { "af",    NULL, "iso-8859-1"},      /* Afrikaans */
+  { "am",    NULL, "UTF-8"},           /* Amharic */
+  { "ar",    NULL, "iso-8859-6"},      /* Arabic */
+  { "as",    NULL, NULL},              /* Assamese */
+  { "ay",    NULL, "iso-8859-1"},      /* Aymara */
+  { "az",    NULL, NULL},              /* Azerbaijani */
+  { "ba",    NULL, NULL},              /* Bashkir */
+  { "be",    NULL, "UTF-8"},           /* Byelorussian; Belarusian */
+  { "bg",    NULL, "iso-8859-5"},      /* Bulgarian */
+  { "bh",    NULL, NULL},              /* Bihari */
+  { "bi",    NULL, NULL},              /* Bislama */
+  { "bn",    NULL, NULL},              /* Bengali; Bangla */
+  { "bo",    NULL, NULL},              /* Tibetan */
+  { "br",    NULL, "iso-8859-1"},      /* Breton: 1,5,8,9 */
+  { "bs",    NULL, NULL},              /* Bosnian */
+  { "ca",    NULL, "iso-8859-1"},      /* Catalan: 1,5,8,9 */
+  { "ce",    NULL, NULL},              /* Chechen */
+  { "ch",    NULL, NULL},              /* Chamorro */
+  { "co",    NULL, "iso-8859-1"},      /* Corsican */
+  { "cs",    NULL, "iso-8859-2"},      /* Czech */
+  { "cu",    NULL, NULL },             /* Church Slavic */
+  { "cv",    NULL, NULL},              /* Chuvash */
+  { "cy",    NULL, "iso-8859-1"},      /* Welsh */
+  { "da",    NULL, "iso-8859-1"},      /* Danish: 4-9 */
+  { "de",    NULL, "iso-8859-1"},      /* German */
+  { "dz",    NULL, NULL },             /* Dzongkha; Bhutani */
+  { "el",    NULL, "iso-8859-7"},      /* Greek */
+  { "en",    NULL, "iso-8859-1"},      /* English */
+  { "eo",    NULL, "iso-8859-3"},      /* Esperanto */
+  { "es",    NULL, "iso-8859-1"},      /* Spanish */
+  { "et",    NULL, "iso-8859-15"},     /* Estonian: 6,7,9 */
+  { "eu",    NULL, "iso-8859-1"},      /* Basque: 5,8,9 */
+  { "fa",    NULL, "UTF-8"},           /* Persian */
+  { "fi",    NULL, "iso-8859-15"},     /* Finnish */
+  { "fj",    NULL, NULL },             /* Fijian; Fiji */
+  { "fo",    NULL, "iso-8859-1"},      /* Faroese: 6,9 */
+  { "fr",    NULL, "iso-8859-1"},      /* French */
+  { "fy",    NULL, "iso-8859-1"},      /* Frisian */
+  { "ga",    NULL, "iso-8859-14"},     /* Irish */
+  { "gd",    NULL, "iso-8859-14" },    /* Scots; Gaelic */
+  { "gl",    NULL, NULL },             /* Gallegan; Galician */
+  { "gn",    NULL, NULL},              /* Guarani */
+  { "gu",    NULL, NULL},              /* Gujarati */
+  { "gv",    NULL, "iso-8859-14"},     /* Manx */
+  { "ha",    NULL, NULL },             /* Hausa (?) */
+  { "he",    NULL, "iso-8859-8" },     /* Hebrew */
+  { "hi",    NULL, NULL},              /* Hindi */
+  { "ho",    NULL, NULL},              /* Hiri Motu */
+  { "hr",    NULL, "iso-8859-2"},      /* Croatian: 10 */
+  { "hu",    NULL, "iso-8859-2"},      /* Hungarian */
+  { "hy",    NULL, NULL},              /* Armenian */
+  { "hz",    NULL, NULL},              /* Herero */
+  { "id",    NULL, "iso-8859-1"},      /* Indonesian (formerly in) */
+  { "ia",    NULL, NULL},              /* Interlingua */
+  { "ie",    NULL, NULL},              /* Interlingue */
+  { "ik",    NULL, NULL},              /* Inupiak */
+  { "io",    NULL, NULL},              /* Ido */
+  { "is",    NULL, "iso-8859-1"},      /* Icelandic */
+  { "it",    NULL, "iso-8859-1"},      /* Italian */
+  { "iu",    NULL, NULL},              /* Inuktitut */
+  { "ja",    NULL, "EUC-JP"},          /* Japanese */
+  { "jv",    NULL, NULL},              /* Javanese */
+  { "ka",    NULL, NULL},              /* Georgian */
+  { "ki",    NULL, NULL},              /* Kikuyu */
+  { "kj",    NULL, NULL},              /* Kuanyama */
+  { "kk",    NULL, NULL},              /* Kazakh */
+  { "kl",    NULL, "iso-8859-1"},      /* Kalaallisut; Greenlandic */
+  { "km",    NULL, NULL},              /* Khmer; Cambodian */
+  { "kn",    NULL, NULL},              /* Kannada */
+  { "ko",    NULL, "EUC-KR"},          /* Korean */
+  { "ks",    NULL, NULL},              /* Kashmiri */
+  { "ku",    NULL, NULL},              /* Kurdish */
+  { "kv",    NULL, NULL},              /* Komi */
+  { "kw",    NULL, "iso-8859-14"},     /* Cornish: 1,5,8 */
+  { "ky",    NULL, NULL},              /* Kirghiz */
+  { "la",    NULL, "iso-8859-1"},      /* Latin */
+  { "lb",    NULL, "iso-8859-1"},      /* Letzeburgesch */
+  { "ln",    NULL, NULL},              /* Lingala */
+  { "lo",    NULL, NULL},              /* Lao; Laotian */
+  { "lt",    NULL, "iso-8859-4"},      /* Lithuanian */
+  { "lv",    NULL, "iso-8859-4"},      /* Latvian; Lettish */
+  { "mg",    NULL, NULL},              /* Malagasy */
+  { "mh",    NULL, NULL},              /* Marshall */
+  { "mi",    NULL, NULL},              /* Maori */
+  { "mk",    NULL, NULL},              /* Macedonian */
+  { "ml",    NULL, NULL},              /* Malayalam */
+  { "mn",    NULL, NULL},              /* Mongolian */
+  { "mo",    NULL, "iso-8859-2"},      /* Moldavian */
+  { "mr",    NULL, NULL},              /* Marathi */
+  { "ms",    NULL, NULL},              /* Malay */
+  { "mt",    NULL, "iso-8859-3"},      /* Maltese */
+  { "my",    NULL, NULL},              /* Burmese */
+  { "na",    NULL, NULL},              /* Nauru */
+  { "nb",    NULL, "iso-8859-1"},      /* Norwegian Bokmål; Bokm@aa{}l  */
+  { "nd",    NULL, NULL},              /* Ndebele, North */
+  { "ne",    NULL, NULL},              /* Nepali */
+  { "ng",    NULL, NULL},              /* Ndonga */
+  { "nl",    NULL, "iso-8859-1"},      /* Dutch: 5,9 */
+  { "nn",    NULL, "iso-8859-1"},      /* Norwegian Nynorsk */
+  { "no",    NULL, "iso-8859-1"},      /* Norwegian */
+  { "nr",    NULL, NULL},              /* Ndebele, South */
+  { "nv",    NULL, NULL},              /* Navajo */
+  { "ny",    NULL, NULL},              /* Chichewa; Nyanja */
+  { "oc",    NULL, NULL},              /* Occitan; Provençal; Proven@,{c}al */
+  { "om",    NULL, NULL},              /* (Afan) Oromo */
+  { "or",    NULL, NULL},              /* Oriya */
+  { "os",    NULL, NULL},              /* Ossetian; Ossetic */
+  { "pa",    NULL, NULL},              /* Panjabi; Punjabi */
+  { "pi",    NULL, NULL},              /* Pali */
+  { "pl",    NULL, "iso-8859-2"},      /* Polish */
+  { "ps",    NULL, NULL},              /* Pashto, Pushto */
+  { "pt",    NULL, "iso-8859-1"},      /* Portuguese */
+  { "qu",    NULL, "iso-8859-1"},      /* Quechua */
+  { "rm",    NULL, "iso-8859-1"},      /* Rhaeto-Romance */
+  { "rn",    NULL, NULL },             /* Rundi; Kirundi */
+  { "ro",    NULL, "iso-8859-2"},      /* Romanian */
+  { "ru",    NULL, "koi8-r"},          /* Russian */
+  { "rw",    NULL, NULL},              /* Kinyarwanda */
+  { "sa",    NULL, NULL},              /* Sanskrit */
+  { "sc",    NULL, "iso-8859-1"},      /* Sardinian */
+  { "sd",    NULL, NULL},              /* Sindhi */
+  { "se",    NULL, "iso-8859-10"},     /* Northern Sami */
+  { "sg",    NULL, NULL},              /* Sango; Sangro */
+  { "si",    NULL, NULL},              /* Sinhalese */
+  { "sk",    NULL, "iso-8859-2"},      /* Slovak */
+  { "sl",    NULL, "iso-8859-1"},      /* Slovenian */
+  { "sm",    NULL, NULL},              /* Samoan */
+  { "sn",    NULL, NULL},              /* Shona */
+  { "so",    NULL, NULL},              /* Somali */
+  { "sq",    NULL, "iso-8859-1"},      /* Albanian: 2,5,8,9,10 */
+  { "sr",    NULL, "iso-8859-2"},      /* Serbian */
+  { "ss",    NULL, NULL},              /* Swati; Siswati */
+  { "st",    NULL, NULL},              /* Sesotho; Sotho, Southern */
+  { "su",    NULL, NULL},              /* Sundanese */
+  { "sv",    NULL, "iso-8859-1"},      /* Swedish */
+  { "sw",    NULL, NULL},              /* Swahili */
+  { "ta",    NULL, NULL},              /* Tamil */
+  { "te",    NULL, NULL},              /* Telugu */
+  { "tg",    NULL, NULL},              /* Tajik */
+  { "th",    NULL, "iso-8859-11"},     /* Thai */
+  { "ti",    NULL, NULL},              /* Tigrinya */
+  { "tk",    NULL, NULL},              /* Turkmen */
+  { "tl",    NULL, "iso-8859-1"},      /* Tagalog */
+  { "tn",    NULL, NULL},              /* Tswana; Setswana */
+  { "to",    NULL, NULL},              /* Tonga (?) */
+  { "tr",    NULL, "iso-8859-9"},      /* Turkish */
+  { "ts",    NULL, NULL},              /* Tsonga */
+  { "tt",    NULL, NULL},              /* Tatar */
+  { "tw",    NULL, NULL},              /* Twi */
+  { "ty",    NULL, NULL},              /* Tahitian */
+  { "ug",    NULL, NULL},              /* Uighur */
+  { "uk",    NULL, "koi8-u"},          /* Ukrainian */
+  { "ur",    NULL, NULL},              /* Urdu */
+  { "uz",    NULL, NULL},              /* Uzbek */
+  { "vi",    NULL, NULL},              /* Vietnamese */
+  { "vo",    NULL, NULL},              /* Volapük; Volap@"{u}k; Volapuk */
+  { "wa",    NULL, "iso-8859-1"},      /* Walloon */
+  { "wo",    NULL, NULL},              /* Wolof */
+  { "xh",    NULL, NULL},              /* Xhosa */
+  { "yi",    NULL, "iso-8859-8"},      /* Yiddish (formerly ji) */
+  { "yo",    NULL, NULL},              /* Yoruba */
+  { "za",    NULL, NULL},              /* Zhuang */
+  { "zh",    "TW", "big5"},            /* Chinese */
+  { "zh",    NULL, "gb2312"},          /* Chinese */
+  { "zu",    NULL, NULL},              /* Zulu */
+  { NULL }
+};
+
+/* Given the language and (optionally) territory code, return the
+   default character set for that language. See notes above. */
+
+const char *
+mu_charset_lookup (char *lang, char *terr)
+{
+  static struct langtab *p;
+
+  if (!lang)
+    return NULL;
+  for (p = langtab; p->lang; p++)
+    if (mu_c_strcasecmp (p->lang, lang) == 0
+	&& (terr == NULL 
+	    || p->terr == NULL
+	    || !mu_c_strcasecmp (p->terr, terr) == 0))
+      return p->charset;
+  return NULL;
+}
+
diff --git a/libmailutils/base/locker.c b/libmailutils/base/locker.c
new file mode 100644
index 0000000..b720e20
--- /dev/null
+++ b/libmailutils/base/locker.c
@@ -0,0 +1,1026 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 2005, 2006, 2007, 2008, 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, see 
+   <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <assert.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <utime.h>
+
+#include <sys/param.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+
+#include <mailutils/errno.h>
+#include <mailutils/locker.h>
+#include <mailutils/mutil.h>
+
+#define LOCKFILE_ATTR           0644
+
+/* First draft by Brian Edmond. */
+/* For subsequent modifications, see the GNU mailutils ChangeLog. */
+
+struct _mu_locker
+{
+  unsigned refcnt;             /* Number of times mu_locker_lock was called */
+  enum mu_locker_mode mode;    /* Current locking mode (if refcnt > 0) */
+
+  char *file;
+  int flags;
+  int expire_time;
+  int retries;
+  int retry_sleep;
+
+  union lock_data {
+    struct {
+      char *dotlock;
+      char *nfslock;
+    } dot;
+    
+    struct {
+      char *name;
+    } external;
+    
+    struct {
+      int fd;
+    } kernel;
+  } data;
+};
+
+#define MU_LOCKER_TYPE(l) MU_LOCKER_FLAG_TO_TYPE((l)->flags)
+
+struct locker_tab
+{
+  int (*init) (mu_locker_t);
+  void (*destroy) (mu_locker_t);
+  int (*prelock) (mu_locker_t); 
+  int (*lock) (mu_locker_t, enum mu_locker_mode);
+  int (*unlock) (mu_locker_t);
+};
+
+static int init_dotlock (mu_locker_t);
+static void destroy_dotlock (mu_locker_t);
+static int lock_dotlock (mu_locker_t, enum mu_locker_mode);
+static int unlock_dotlock (mu_locker_t);
+
+static int init_external (mu_locker_t);
+static void destroy_external (mu_locker_t);
+static int lock_external (mu_locker_t, enum mu_locker_mode);
+static int unlock_external (mu_locker_t);
+
+static int init_kernel (mu_locker_t);
+static int lock_kernel (mu_locker_t, enum mu_locker_mode);
+static int unlock_kernel (mu_locker_t);
+
+static int prelock_common (mu_locker_t);
+
+static struct locker_tab locker_tab[] = {
+  /* MU_LOCKER_TYPE_DOTLOCK */
+  { init_dotlock, destroy_dotlock, prelock_common,
+    lock_dotlock, unlock_dotlock },
+  /* MU_LOCKER_TYPE_EXTERNAL */
+  { init_external, destroy_external, prelock_common,
+    lock_external, unlock_external },
+  /* MU_LOCKER_TYPE_KERNEL */
+  { init_kernel, NULL, NULL, lock_kernel, unlock_kernel },
+  /* MU_LOCKER_TYPE_NULL */
+  { NULL, NULL, NULL, NULL, NULL }
+};
+
+#define MU_LOCKER_NTYPES (sizeof (locker_tab) / sizeof (locker_tab[0]))
+
+
+static int
+stat_check (const char *file, int fd, int links)
+{
+  struct stat fn_stat;
+  struct stat fd_stat;
+  int err = 0;
+  int localfd = -1;
+
+  if (fd == -1)
+    {
+      localfd = open (file, O_RDONLY);
+      
+      if (localfd == -1)
+	return errno;
+      fd = localfd;
+    }
+
+  /* We should always be able to stat a valid fd, so this
+     is an error condition. */
+  if (lstat (file, &fn_stat) || fstat (fd, &fd_stat))
+    err = errno;
+  else
+    {
+      /* If the link and stat don't report the same info, or the
+         file is a symlink, fail the locking. */
+#define CHK(X) if(X) err = EINVAL
+
+      CHK (!S_ISREG (fn_stat.st_mode));
+      CHK (!S_ISREG (fd_stat.st_mode));
+      CHK (fn_stat.st_nlink != links);
+      CHK (fn_stat.st_dev != fd_stat.st_dev);
+      CHK (fn_stat.st_ino != fd_stat.st_ino);
+      CHK (fn_stat.st_mode != fd_stat.st_mode);
+      CHK (fn_stat.st_nlink != fd_stat.st_nlink);
+      CHK (fn_stat.st_uid != fd_stat.st_uid);
+      CHK (fn_stat.st_gid != fd_stat.st_gid);
+      CHK (fn_stat.st_rdev != fd_stat.st_rdev);
+
+#undef CHK
+    }
+  if (localfd != -1)
+    close (localfd);
+
+  return err;
+}
+
+static int
+check_file_permissions (const char *file)
+{
+  int fd = -1;
+  int err = 0;
+
+  if ((fd = open (file, O_RDONLY)) == -1)
+    return errno == ENOENT ? 0 : errno;
+
+  err = stat_check (file, fd, 1);
+  close (fd);
+  fd = -1;
+  if (err)
+    {
+      if (err == EINVAL)
+	err = MU_ERR_LOCK_BAD_FILE;
+      return err;
+    }
+
+  return 0;
+}
+
+static int
+prelock_common (mu_locker_t locker)
+{
+  /* Check if we are trying to lock a regular file, with a link count
+     of 1, that we have permission to read, etc., or don't lock it. */
+  return check_file_permissions (locker->file);
+}
+
+
+static int mu_locker_default_flags = MU_LOCKER_DEFAULT;
+static time_t mu_locker_retry_timeout = MU_LOCKER_RETRY_SLEEP;
+static size_t mu_locker_retry_count = MU_LOCKER_RETRIES;
+static time_t mu_locker_expire_timeout = MU_LOCKER_EXPIRE_TIME;
+static char *mu_locker_external_program = NULL;
+
+int
+mu_locker_set_default_flags (int flags, enum mu_locker_set_mode mode)
+{
+  switch (mode)
+    {
+    case mu_locker_assign:
+      mu_locker_default_flags = flags;
+      break;
+
+    case mu_locker_set_bit:
+      mu_locker_default_flags |= flags;
+      break;
+
+    case mu_locker_clear_bit:
+      mu_locker_default_flags &= ~flags;
+      break;
+
+    default:
+      return EINVAL;
+    }
+  return 0;
+}
+
+void
+mu_locker_set_default_retry_timeout (time_t to)
+{
+  mu_locker_retry_timeout = to;
+}
+
+void
+mu_locker_set_default_retry_count (size_t n)
+{
+  mu_locker_retry_count = n;
+}
+
+void
+mu_locker_set_default_expire_timeout (time_t t)
+{
+  mu_locker_expire_timeout = t;
+}
+
+void
+mu_locker_set_default_external_program (char *path)
+{
+  free (mu_locker_external_program);
+  mu_locker_external_program = strdup (path);
+}
+
+int
+mu_locker_mod_flags (mu_locker_t locker, int flags,
+		     enum mu_locker_set_mode mode)
+{
+  unsigned otype, ntype;
+  int new_flags;
+  
+  if (!locker)
+    return MU_ERR_LOCKER_NULL;
+
+  switch (mode)
+    {
+    case mu_locker_assign:
+      new_flags = flags;
+      break;
+
+    case mu_locker_set_bit:
+      new_flags = locker->flags | flags;
+      break;
+
+    case mu_locker_clear_bit:
+      new_flags = locker->flags & ~flags;
+      break;
+
+    default:
+      return EINVAL;
+    }
+
+  otype = MU_LOCKER_TYPE (locker);
+  if (otype >= MU_LOCKER_NTYPES)
+    return EINVAL;
+  ntype = MU_LOCKER_FLAG_TO_TYPE (new_flags);
+  if (ntype >= MU_LOCKER_NTYPES)
+    return EINVAL;
+
+  if (ntype != otype)
+    {
+      int rc;
+      
+      if (locker_tab[otype].destroy)
+	locker_tab[otype].destroy (locker);
+      locker->flags = new_flags;
+      if (locker_tab[ntype].init)
+	{
+	  rc = locker_tab[ntype].init (locker);
+	  if (rc)
+	    locker->flags = MU_LOCKER_NULL;
+	  return rc;
+	}
+    }
+  else
+    locker->flags = new_flags;
+
+  return 0;
+}
+
+int
+mu_locker_set_flags (mu_locker_t locker, int flags)
+{
+  return mu_locker_mod_flags (locker, flags, mu_locker_assign);
+}
+
+int
+mu_locker_set_expire_time (mu_locker_t locker, int etime)
+{
+  if (!locker)
+    return MU_ERR_LOCKER_NULL;
+
+  if (etime <= 0)
+    return EINVAL;
+
+  locker->expire_time = etime;
+
+  return 0;
+}
+
+int
+mu_locker_set_retries (mu_locker_t locker, int retries)
+{
+  if (!locker)
+    return MU_ERR_LOCKER_NULL;
+
+  if (retries <= 0)
+    return EINVAL;
+
+  locker->retries = retries;
+
+  return 0;
+}
+
+int
+mu_locker_set_retry_sleep (mu_locker_t locker, int retry_sleep)
+{
+  if (!locker)
+    return MU_ERR_LOCKER_NULL;
+
+  if (retry_sleep <= 0)
+    return EINVAL;
+
+  locker->retry_sleep = retry_sleep;
+
+  return 0;
+}
+
+int
+mu_locker_set_external (mu_locker_t locker, const char* program)
+{
+  char* p = NULL;
+
+  if (!locker)
+    return MU_ERR_LOCKER_NULL;
+  if (MU_LOCKER_TYPE (locker) != MU_LOCKER_TYPE_EXTERNAL)
+    return EINVAL;
+    
+  /* program can be NULL */
+  if (program != 0)
+    {
+      p = strdup (program);
+      if (!p)
+	return ENOMEM;
+  }
+
+  free (locker->data.external.name);
+  locker->data.external.name = p;
+
+  return 0;
+}
+
+int
+mu_locker_get_flags (mu_locker_t locker, int *flags)
+{
+  if (!locker)
+    return MU_ERR_LOCKER_NULL;
+
+  if (!flags)
+    return EINVAL;
+
+  *flags = locker->flags;
+
+  return 0;
+}
+
+int
+mu_locker_get_expire_time (mu_locker_t locker, int *ptime)
+{
+  if (!locker)
+    return MU_ERR_LOCKER_NULL;
+
+  if (!ptime)
+    return EINVAL;
+
+  *ptime = locker->expire_time;
+
+  return 0;
+}
+
+int
+mu_locker_get_retries (mu_locker_t locker, int *retries)
+{
+  if (!locker)
+    return MU_ERR_LOCKER_NULL;
+
+  if (!retries)
+    return EINVAL;
+
+  *retries = locker->retries;
+
+  return 0;
+}
+
+int
+mu_locker_get_retry_sleep (mu_locker_t locker, int *retry_sleep)
+{
+  if (!locker)
+    return MU_ERR_LOCKER_NULL;
+
+  if (!retry_sleep)
+    return EINVAL;
+
+  *retry_sleep = locker->retry_sleep;
+
+  return 0;
+}
+
+int
+mu_locker_create (mu_locker_t *plocker, const char *fname, int flags)
+{
+  unsigned type;
+  mu_locker_t l;
+  char filename[_POSIX_PATH_MAX];
+  int err = 0;
+
+  if (plocker == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+
+  if (fname == NULL)
+    return EINVAL;
+
+  if ((err = mu_unroll_symlink (filename, sizeof (filename), fname)))
+    return err;
+
+  l = calloc (1, sizeof (*l));
+
+  if (l == NULL)
+    return ENOMEM;
+
+  l->file = strdup (filename);
+  
+  if (l->file == NULL)
+    {
+      free (l);
+      return ENOMEM;
+    }
+
+  if (strcmp (filename, "/dev/null") == 0)
+    l->flags = MU_LOCKER_NULL;
+  else if (flags)
+    l->flags = flags;
+  else
+    l->flags = mu_locker_default_flags;
+
+  l->expire_time = mu_locker_expire_timeout;
+  l->retries = mu_locker_retry_count;
+  l->retry_sleep = mu_locker_retry_timeout;
+
+  type = MU_LOCKER_TYPE (l);
+
+  if (type >= MU_LOCKER_NTYPES)
+    {
+      free (l->file);
+      return EINVAL;
+    }
+
+  /* Initialize locker-type-specific data */
+  err = locker_tab[type].init ? locker_tab[type].init (l) : 0;
+  if (err)
+    {
+      mu_locker_destroy (&l);
+      return err;
+    }
+
+  *plocker = l;
+
+  return 0;
+}
+
+void
+mu_locker_destroy (mu_locker_t *plocker)
+{
+  if (plocker && *plocker)
+    {
+      unsigned type = MU_LOCKER_TYPE (*plocker);
+      if (type < MU_LOCKER_NTYPES)
+	{
+	  if (locker_tab[type].destroy)
+	    locker_tab[type].destroy (*plocker);
+	  free ((*plocker)->file);
+	  free (*plocker);
+	  *plocker = NULL;
+	}
+    }
+}
+
+int
+_mu_locker_lock (mu_locker_t lock, enum mu_locker_mode mode)
+{
+  int rc;
+  unsigned type;
+  unsigned retries = 1;
+  
+  if (lock == NULL || (type = MU_LOCKER_TYPE (lock)) >= MU_LOCKER_NTYPES)
+    return EINVAL;
+
+  if (locker_tab[type].prelock && (rc = locker_tab[type].prelock (lock)))
+    return rc;
+  
+  /* Is the lock already applied? */
+  if (lock->refcnt > 0)
+    {
+      lock->refcnt++;
+      if (mode == lock->mode)
+	return 0;
+    }
+
+  lock->mode = mode;
+
+  if (lock->flags & MU_LOCKER_RETRY)
+    retries = lock->retries;
+
+  if (locker_tab[type].lock)
+    {
+      while (retries--)
+	{
+	  rc = locker_tab[type].lock (lock, mode);
+	  if (rc == EAGAIN && retries)
+	    {
+	      sleep (lock->retry_sleep);
+	      continue;
+	    }
+
+	  if (rc == 0)
+	    lock->refcnt++;
+
+	  break;
+	}
+    }
+  else
+    rc = 0;
+  
+  return rc;
+}
+
+int
+mu_locker_lock (mu_locker_t lock)
+{
+  return _mu_locker_lock (lock, mu_lck_exc);
+}
+
+int
+mu_locker_unlock (mu_locker_t lock)
+{
+  int rc = 0;
+  unsigned type;
+  
+  if (!lock)
+    return MU_ERR_LOCKER_NULL;
+
+  if (lock->refcnt == 0)
+    return MU_ERR_LOCK_NOT_HELD;
+
+  if ((rc = check_file_permissions (lock->file)))
+    return rc;
+
+  if (--lock->refcnt > 0)
+    return 0;
+
+  type = MU_LOCKER_TYPE (lock);
+  if (locker_tab[type].unlock)
+    rc = locker_tab[type].unlock (lock);
+  else
+    rc = 0;
+  
+  return rc;
+}
+
+int
+mu_locker_remove_lock (mu_locker_t lock)
+{
+  if (!lock)
+    return MU_ERR_LOCKER_NULL;
+
+  /* Force the reference count to 1 to unlock the file. */
+  lock->refcnt = 1;
+  return mu_locker_unlock (lock);
+}
+
+
+#define DOTLOCK_SUFFIX ".lock"
+
+/* expire a stale lock (if MU_LOCKER_PID or MU_LOCKER_TIME) */
+static void
+expire_stale_lock (mu_locker_t lock)
+{
+  int stale = 0;
+  int fd = open (lock->data.dot.dotlock, O_RDONLY);
+  if (fd == -1)
+    return;
+
+  /* Check to see if this process is still running.  */
+  if (lock->flags & MU_LOCKER_PID)
+    {
+      char buf[16];
+      pid_t pid;
+      int nread = read (fd, buf, sizeof (buf) - 1);
+      if (nread > 0)
+	{
+	  buf[nread] = '\0';
+	  pid = strtol (buf, NULL, 10);
+	  if (pid > 0)
+	    {
+	      /* Process is gone so we try to remove the lock. */
+	      if (kill (pid, 0) == -1)
+		stale = 1;
+	    }
+	  else
+	    stale = 1;		/* Corrupted file, remove the lock. */
+	}
+    }
+  
+  /* Check to see if the lock expired.  */
+  if (lock->flags & MU_LOCKER_TIME)
+    {
+      struct stat stbuf;
+
+      fstat (fd, &stbuf);
+      /* The lock has expired. */
+      if ((time (NULL) - stbuf.st_mtime) > lock->expire_time)
+	stale = 1;
+    }
+
+  close (fd);
+  if (stale)
+    unlink (lock->data.dot.dotlock);
+}
+
+static int
+init_dotlock (mu_locker_t locker)
+{
+  char *tmp, *p;
+
+  /* Make sure the spool directory is writable */
+  tmp = strdup (locker->file);
+  if (!tmp)
+    return ENOMEM;
+
+  strcpy (tmp, locker->file);
+  p = strrchr (tmp, '/');
+  if (!p)
+    {
+      free (tmp);
+      tmp = strdup (".");
+      if (!tmp)
+	return ENOMEM;
+    }
+  else
+    *p = 0; 
+
+  if (access (tmp, W_OK))
+    {
+      /* Fallback to kernel locking */
+      free (tmp);
+      return mu_locker_set_flags (locker,
+			   MU_LOCKER_KERNEL|MU_LOCKER_OPTIONS(locker->flags));
+    }
+  
+  free (tmp);
+  
+  locker->data.dot.dotlock = malloc (strlen (locker->file)
+				     + sizeof (DOTLOCK_SUFFIX));
+  
+  if (!locker->data.dot.dotlock)
+    return ENOMEM;
+  strcpy (locker->data.dot.dotlock, locker->file);
+  strcat (locker->data.dot.dotlock, DOTLOCK_SUFFIX);
+
+  return 0;
+}
+
+static void
+destroy_dotlock (mu_locker_t locker)
+{
+  free (locker->data.dot.dotlock);
+  free (locker->data.dot.nfslock);
+}
+
+static int
+lock_dotlock (mu_locker_t locker, enum mu_locker_mode mode)
+{
+  int rc;
+  char *host = NULL;
+  char pid[11];		/* 10 is strlen(2^32 = 4294967296) */
+  char now[11];
+  size_t sz = 0;
+  int err = 0;
+  int fd;
+    
+  if (locker->data.dot.nfslock)
+    {
+      unlink (locker->data.dot.nfslock);
+      free (locker->data.dot.nfslock);
+      locker->data.dot.nfslock = NULL;
+    }
+
+  expire_stale_lock (locker);
+
+  /* build the NFS hitching-post to the lock file */
+
+  rc = mu_get_host_name (&host);
+  if (rc)
+    return rc;
+
+  snprintf (now, sizeof (now), "%lu", (unsigned long) time (0));
+  now[sizeof (now) - 1] = 0;
+
+  snprintf (pid, sizeof (pid), "%lu", (unsigned long) getpid ());
+  pid[sizeof (pid) - 1] = 0;
+		  
+  sz = strlen (locker->file) + 1 /* "." */
+    + strlen (pid) + 1 /* "." */
+    + strlen (now) + 1 /* "." */
+    + strlen (host) + 1;
+  
+  locker->data.dot.nfslock = malloc (sz);
+  
+  if (!locker->data.dot.nfslock)
+    {
+      free (host);
+      return ENOMEM;
+    }
+
+  snprintf (locker->data.dot.nfslock, sz, "%s.%s.%s.%s",
+	    locker->file, pid, now, host);
+  free (host);
+  
+  fd = open (locker->data.dot.nfslock,
+	     O_WRONLY | O_CREAT | O_EXCL, LOCKFILE_ATTR);
+  if (fd == -1)
+    {
+      if (errno == EEXIST)
+	return EAGAIN;
+      else
+	return errno;
+    }
+  close (fd);
+  
+  /* Try to link to the lockfile. */
+  if (link (locker->data.dot.nfslock, locker->data.dot.dotlock) == -1)
+    {
+      unlink (locker->data.dot.nfslock);
+      if (errno == EEXIST)
+	return MU_ERR_LOCK_CONFLICT;
+      return errno;
+    }
+
+  if ((fd = open (locker->data.dot.dotlock, O_RDWR)) == -1)
+    {
+      unlink (locker->data.dot.nfslock);
+      return errno;
+    }
+  
+  err = stat_check (locker->data.dot.nfslock, fd, 2);
+  if (err)
+    {
+      unlink (locker->data.dot.nfslock);
+      if (err == EINVAL)
+	return MU_ERR_LOCK_BAD_LOCK;
+      return errno;
+    }
+
+  unlink (locker->data.dot.nfslock);
+
+  /* FIXME: If no errors, we have the lock. */
+  assert (locker->refcnt == 0);
+
+  if (locker->flags & MU_LOCKER_PID)
+    {
+      char buf[16];
+      sprintf (buf, "%ld", (long) getpid ());
+      write (fd, buf, strlen (buf));
+    }
+  close (fd);
+  return 0;
+}
+
+static int
+unlock_dotlock (mu_locker_t locker)
+{
+  if (unlink (locker->data.dot.dotlock) == -1)
+    {
+      int err = errno;
+      if (err == ENOENT)
+	{
+	  locker->refcnt = 0; /*FIXME?*/
+	  err = MU_ERR_LOCK_NOT_HELD;
+	  return err;
+	}
+      return err;
+    }
+  return 0;
+}
+
+int
+mu_locker_touchlock (mu_locker_t lock)
+{
+  if (!lock)
+    return MU_ERR_LOCKER_NULL;
+
+  if (MU_LOCKER_TYPE (lock) != MU_LOCKER_TYPE_DOTLOCK)
+    return 0;
+  
+  if (lock->refcnt > 0)
+    return utime (lock->data.dot.dotlock, NULL);
+
+  return MU_ERR_LOCK_NOT_HELD;
+}
+
+
+/* Kernel locking */
+static int
+init_kernel (mu_locker_t locker)
+{
+  return 0;
+}
+
+static int
+lock_kernel (mu_locker_t locker, enum mu_locker_mode mode)
+{
+  int fd;
+  struct flock fl;
+
+  switch (mode)
+    {
+    case mu_lck_shr:
+    case mu_lck_opt:
+      mode = O_RDONLY;
+      fl.l_type = F_RDLCK;
+      break;
+
+    case mu_lck_exc:
+      mode = O_RDWR;
+      fl.l_type = F_WRLCK;
+      break;
+
+    default:
+      return EINVAL;
+    }
+  
+  fd = open (locker->file, O_RDWR);
+  if (fd == -1)
+    return errno;
+  locker->data.kernel.fd = fd;
+  
+  fl.l_whence = SEEK_SET;
+  fl.l_start = 0;
+  fl.l_len = 0; /* Lock entire file */
+  if (fcntl (fd, F_SETLK, &fl))
+    {
+#ifdef EACCESS      
+      if (errno == EACCESS)
+	return EAGAIN;
+#endif
+      if (errno == EAGAIN)
+	return EAGAIN;
+      return errno;
+    }
+  return 0;
+}
+
+static int
+unlock_kernel (mu_locker_t locker)
+{
+  struct flock fl;
+
+  fl.l_type = F_UNLCK;
+  fl.l_whence = SEEK_SET;
+  fl.l_start = 0;
+  fl.l_len = 0; /* Unlock entire file */
+  if (fcntl (locker->data.kernel.fd, F_SETLK, &fl))
+    {
+#ifdef EACCESS
+      if (errno == EACCESS)
+	return EAGAIN;
+#endif
+      if (errno == EAGAIN)
+	return EAGAIN;
+      return errno;
+    }
+  close (locker->data.kernel.fd);
+  return 0;
+}
+
+static int
+init_external (mu_locker_t locker)
+{
+  if (!(locker->data.external.name = strdup (mu_locker_external_program ?
+					     mu_locker_external_program :
+					     MU_LOCKER_EXTERNAL_PROGRAM)))
+    return ENOMEM;
+  return 0;
+}
+
+static void
+destroy_external (mu_locker_t locker)
+{
+  free (locker->data.external.name);
+}
+
+/*
+  Estimate 1 decimal digit per 3 bits, + 1 for round off.
+*/
+#define DEC_DIGS_PER_INT (sizeof(int) * 8 / 3 + 1)
+
+static int
+external_locker (mu_locker_t l, int lock)
+{
+  int err = 0;
+  char *av[6];
+  int ac = 0;
+  char aforce[3 + DEC_DIGS_PER_INT + 1];
+  char aretry[3 + DEC_DIGS_PER_INT + 1];
+  int status = 0;
+
+  assert (l);
+  assert (l->flags & MU_LOCKER_EXTERNAL);
+  /* FIXME */
+  assert (lock == !l->refcnt);
+  /* lock is true, refcnt is 0 or lock is false and refcnt is 1 */
+
+  av[ac++] = l->data.external.name ?
+                   l->data.external.name : MU_LOCKER_EXTERNAL_PROGRAM;
+
+  if (l->flags & MU_LOCKER_TIME)
+    {
+      snprintf (aforce, sizeof (aforce), "-f%d", l->expire_time);
+      aforce[sizeof (aforce) - 1] = 0;
+      av[ac++] = aforce;
+    }
+  
+  if (l->flags & MU_LOCKER_RETRY)
+    {
+      snprintf (aretry, sizeof (aretry), "-r%d", l->retries);
+      aretry[sizeof (aretry) - 1] = 0;
+      av[ac++] = aretry;
+    }
+
+  if (!lock)
+    av[ac++] = "-u";
+
+  av[ac++] = l->file;
+
+  av[ac++] = NULL;
+
+  if ((err = mu_spawnvp (av[0], av, &status)))
+    return err;
+
+  if (!WIFEXITED (status))
+    {
+      err = MU_ERR_LOCK_EXT_KILLED;
+    }
+  else
+    {
+      switch (WEXITSTATUS (status))
+	{
+	case 127:
+	  err = MU_ERR_LOCK_EXT_FAIL;
+	  break;
+	  
+	case MU_DL_EX_OK:
+	  err = 0;
+	  l->refcnt = lock;
+	  break;
+	  
+	case MU_DL_EX_NEXIST:
+	  err = MU_ERR_LOCK_NOT_HELD;
+	  break;
+	  
+	case MU_DL_EX_EXIST:
+	  err = MU_ERR_LOCK_CONFLICT;
+	  break;
+	  
+	case MU_DL_EX_PERM:
+	  err = EPERM;
+	  break;
+	  
+	default:
+	case MU_DL_EX_ERROR:
+	  err = MU_ERR_LOCK_EXT_ERR;
+	  break;
+	}
+    }
+
+  return err;
+}
+
+static int
+lock_external (mu_locker_t locker, enum mu_locker_mode mode)
+{
+  return external_locker (locker, 1);
+}
+
+static int
+unlock_external (mu_locker_t locker)
+{
+  return external_locker (locker, 0);
+}
+
diff --git a/libmailutils/base/mailcap.c b/libmailutils/base/mailcap.c
new file mode 100644
index 0000000..ede3f67
--- /dev/null
+++ b/libmailutils/base/mailcap.c
@@ -0,0 +1,739 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2003, 2004, 2005, 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, see 
+   <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+
+#include <mailutils/cctype.h>
+#include <mailutils/mailcap.h>
+#include <mailutils/stream.h>
+#include <mailutils/mutil.h>
+#include <mailutils/cstr.h>
+
+/* Definition of the structure, this should be in mailutils/sys/mailcap.h.  */
+struct _mu_mailcap_entry
+{
+  char * typefield;
+  char * viewcommand;
+  char ** fields;
+  size_t fields_count;
+};
+
+/* Definition of the structure, this should be in mailutils/sys/mailcap.h.  */
+struct _mu_mailcap
+{
+  mu_mailcap_entry_t *entries;
+  size_t entries_count;
+};
+
+
+static int mu_mailcap_parse (mu_mailcap_t mailcap, mu_stream_t stream);
+static int mu_mailcap_parse_entry (mu_mailcap_entry_t entry, char *buffer);
+static char * tokenize (char *s, char **save_ptr);
+
+int
+mu_mailcap_create (mu_mailcap_t * pmailcap, mu_stream_t stream)
+{
+  mu_mailcap_t mailcap;
+  int status = 0;
+
+  if (stream == NULL)
+    return EINVAL;
+  if (pmailcap == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+
+  mailcap = calloc (1, sizeof (*mailcap));
+  if (mailcap != NULL)
+    {
+      status = mu_mailcap_parse (mailcap, stream);
+      if (status != 0)
+	{
+	  mu_mailcap_destroy (&mailcap);
+	}
+      else
+	{
+	  *pmailcap = mailcap;
+	}
+    }
+  else
+    {
+      status = ENOMEM;
+    }
+  return status;
+}
+
+void
+mu_mailcap_destroy (mu_mailcap_t * pmailcap)
+{
+  if (pmailcap != NULL && *pmailcap != NULL)
+    {
+      int i;
+      mu_mailcap_t mailcap = *pmailcap;
+
+      for (i = 0; i < mailcap->entries_count; i++)
+	{
+	  int j;
+	  mu_mailcap_entry_t entry = mailcap->entries[i];
+	  free (entry->typefield);
+	  free (entry->viewcommand);
+	  for (j = 0; j < entry->fields_count; j++)
+	    {
+	      free (entry->fields[j]);
+	    }
+	}
+    }
+}
+
+int
+mu_mailcap_entries_count (mu_mailcap_t mailcap, size_t *pcount)
+{
+  int status = 0;
+
+  if (mailcap == NULL)
+    status = EINVAL;
+  if (pcount != NULL)
+    {
+      *pcount = mailcap->entries_count;
+    }
+  return status;
+}
+
+int
+mu_mailcap_get_entry (mu_mailcap_t mailcap, size_t no,
+		      mu_mailcap_entry_t *pentry)
+{
+  int status = 0;
+  if (mailcap == NULL)
+    status = EINVAL;
+  else if (pentry == NULL)
+    status = MU_ERR_OUT_PTR_NULL;
+  else if (no == 0 || no > mailcap->entries_count)
+    {
+      status = MU_ERR_NOENT;
+    }
+  else
+    {
+      *pentry = mailcap->entries[no - 1];
+    }
+  return status;
+}
+
+int
+mu_mailcap_entry_get_typefield (mu_mailcap_entry_t entry, char *buffer,
+				size_t buflen, size_t *pn)
+{
+  int status = 0;
+  int len = 0;
+
+  if (entry == NULL)
+    {
+      status = EINVAL;
+    }
+  else
+    {
+      len = strlen(entry->typefield);
+      if (buffer != NULL && buflen > 0)
+	{
+	  buflen--;
+	  len = (len < buflen) ? len : buflen;
+	  memcpy (buffer, entry->typefield, len);
+	  buffer[len] = '\0';
+	}
+    }
+  if (pn)
+    {
+      *pn = len;
+    }
+  return status;
+}
+
+int
+mu_mailcap_entry_get_viewcommand (mu_mailcap_entry_t entry, char *buffer,
+				  size_t buflen, size_t *pn)
+{
+  int status = 0;
+  int len = 0;
+
+  if (entry == NULL)
+    {
+      status = EINVAL;
+    }
+  else
+    {
+      len = strlen(entry->viewcommand);
+      if (buffer != NULL && buflen > 0)
+	{
+	  buflen--;
+	  len = (len < buflen) ? len : buflen;
+	  memcpy (buffer, entry->viewcommand, len);
+	  buffer[len] = '\0';
+	}
+    }
+  if (pn)
+    {
+      *pn = len;
+    }
+  return status;
+}
+
+int
+mu_mailcap_entry_fields_count (mu_mailcap_entry_t entry, size_t *pcount)
+{
+  int status = 0;
+  if (entry == NULL)
+    {
+      status = EINVAL;
+    }
+  if (pcount != NULL)
+    {
+      *pcount = entry->fields_count;
+    }
+  return status;
+}
+
+int
+mu_mailcap_entry_get_field (mu_mailcap_entry_t entry, size_t no,
+			    char *buffer, size_t buflen, size_t *pn)
+{
+  int status = 0;
+  int len = 0;
+
+  if (entry == NULL)
+    {
+      status = EINVAL;
+    }
+  else if ( no == 0 || no > entry->fields_count)
+    {
+      status = MU_ERR_NOENT;
+    }
+  else
+    {
+      len = strlen(entry->fields[no - 1]);
+      if (buffer != NULL && buflen > 0)
+	{
+	  buflen--;
+	  len = (len < buflen) ? len : buflen;
+	  memcpy (buffer, entry->fields[no - 1], len);
+	  buffer[len] = '\0';
+	}
+    }
+  if (pn)
+    {
+      *pn = len;
+    }
+  return status;
+}
+
+int
+mu_mailcap_entry_get_compose (mu_mailcap_entry_t entry,
+			      char *buffer, size_t buflen, size_t *pn)
+{
+  return mu_mailcap_entry_get_value (entry, "compose", buffer, buflen, pn);
+}
+
+int
+mu_mailcap_entry_get_composetyped (mu_mailcap_entry_t entry, char *buffer,
+				   size_t buflen, size_t *pn)
+{
+  return mu_mailcap_entry_get_value (entry, "composetyped", buffer, buflen, pn);
+}
+
+int
+mu_mailcap_entry_get_edit (mu_mailcap_entry_t entry, char *buffer,
+			   size_t buflen, size_t *pn)
+{
+  return mu_mailcap_entry_get_value (entry, "edit", buffer, buflen, pn);
+}
+
+int
+mu_mailcap_entry_get_test (mu_mailcap_entry_t entry,
+			   char *buffer, size_t buflen, size_t *pn)
+{
+  return mu_mailcap_entry_get_value (entry, "test", buffer, buflen, pn);
+}
+
+int
+mu_mailcap_entry_get_x11bitmap (mu_mailcap_entry_t entry,
+				char *buffer, size_t buflen, size_t *pn)
+{
+  return mu_mailcap_entry_get_value (entry, "x11-bitmap", buffer, buflen, pn);
+}
+
+int
+mu_mailcap_entry_get_description (mu_mailcap_entry_t entry,
+				  char *buffer, size_t buflen, size_t *pn)
+{
+  return mu_mailcap_entry_get_value (entry, "description", buffer, buflen, pn);
+}
+
+int
+mu_mailcap_entry_needsterminal (mu_mailcap_entry_t entry, int *on)
+{
+  int status = 0;
+  int found = 0;
+  if (entry == NULL)
+    {
+      status = EINVAL;
+    }
+  else
+    {
+      int i;
+      for (i = 0; i < entry->fields_count; i++)
+	{
+	  int n = mu_c_strcasecmp (entry->fields[i], "needsterminal");
+	  if (n == 0)
+	    {
+	      found = 1;
+	      break;
+	    }
+	}
+    }
+  if (on)
+    *on = found;
+  return status;
+}
+
+int
+mu_mailcap_entry_copiousoutput (mu_mailcap_entry_t entry, int *on)
+{
+  int status = 0;
+  int found = 0;
+  if (entry == NULL)
+    {
+      status = EINVAL;
+    }
+  else
+    {
+      int i;
+      for (i = 0; i < entry->fields_count; i++)
+	{
+	  int n = mu_c_strcasecmp (entry->fields[i], "copiousoutput");
+	  if (n == 0)
+	    {
+	      found = 1;
+	      break;
+	    }
+	}
+    }
+  if (on)
+    *on = found;
+  return status;
+}
+
+int
+mu_mailcap_entry_get_value (mu_mailcap_entry_t entry, const char *key,
+			    char *buffer, size_t buflen, size_t *pn)
+{
+  int len = 0;
+  int status = ENOENT;
+
+  if (!entry)
+    status = EINVAL;
+  else
+    {
+      int i;
+      int key_len = strlen (key);
+      for (i = 0; i < entry->fields_count; i++)
+	{
+	  int n = mu_c_strncasecmp (entry->fields[i], key, key_len);
+	  if (n == 0)
+	    {
+	      int field_len = strlen(entry->fields[i]);
+	      status = 0;
+	      if (field_len > key_len)
+		{
+		  int c = entry->fields[i][key_len];
+		  if (mu_isspace (c) || c == '=')
+		    {
+		      char *value = strchr (entry->fields[i], '=');
+		      if (value != NULL)
+			{
+			  value++; /* Pass the equal.  */
+			  /* Remove leading space.  */
+			  for (; mu_isspace (*value); value++)
+			    ;
+			  len = strlen (value);
+			  /* Strip surrounding double quotes */
+			  if (len > 1 && value[0] == '"' && value[len - 1] == '"')
+			    {
+			      value++;
+			      len -= 2;
+			    }
+			  if (buffer != NULL && buflen > 0)
+			    {
+			      buflen--;
+			      len = (len < buflen) ? len : buflen;
+			      memcpy (buffer, value, len);
+			      buffer[len] = '\0';
+			    }
+			  break;
+			}
+		    }
+		}
+	    }
+	}
+    }
+  if (pn)
+    *pn = len;
+  return status;
+}
+
+/*
+ * break the line on ';'.  Same as strtok() but
+ * check for escaped "\;"
+ */
+static char *
+tokenize (char *s, char **save_ptr)
+{
+  int c;
+  char *token;
+
+  if (s == NULL)
+    {
+      s = *save_ptr;
+    }
+
+  if (*s == '\0')
+    {
+      *save_ptr = s;
+      return NULL;
+    }
+
+  for (token = s, c = 0; *s; s++)
+    {
+      if (*s == ';' && c != '\\')
+	{
+	  break;
+	}
+      c = *s;
+    }
+
+  if (*s == '\0')
+    {
+      *save_ptr = s;
+    }
+  else
+    {
+      *s = '\0';
+      *save_ptr = s + 1;
+    }
+  return token;
+}
+
+/**
+ * parse the mailcap line, fields are separated by ';'
+ */
+static int
+mu_mailcap_parse_entry (mu_mailcap_entry_t entry, char *buffer)
+{
+  char *token = NULL;
+  char *s = NULL;
+  int i;
+  for (i = 0, token = tokenize (buffer, &s);
+       token != NULL; token = tokenize (NULL, &s), i++)
+    {
+      switch (i)
+	{
+	  /* The first entry in a mailcap line is the typefield.  */
+	case 0:
+	  entry->typefield = strdup (mu_str_stripws (token));
+	  break;
+
+	  /* The second entry in a mailcap line is the view-command.  */
+	case 1:
+	  entry->viewcommand = strdup (mu_str_stripws (token));
+	  break;
+
+	  /* The rest are the optional fields.  */
+	default:
+	  {
+	    char **fields = realloc (entry->fields,
+				     (entry->fields_count + 1) *
+				     sizeof (*fields));
+	    if (fields != NULL)
+	      {
+		entry->fields = fields;
+		entry->fields[entry->fields_count] =
+		  strdup (mu_str_stripws (token));
+		entry->fields_count++;
+	      }
+	  }
+	}
+    }
+  /* Make sure typefield and viewcommand are not null.  */
+  if (entry->typefield == NULL)
+    {
+      entry->typefield = strdup ("");
+    }
+  if (entry->viewcommand == NULL)
+    {
+      entry->viewcommand = strdup ("");
+    }
+  return 0;
+}
+
+/*
+ * parse a mailcap file or stream,
+ * - ignore empty line.
+ * - ignore line starting with '#'
+ * - multiline is done with the '\' as continuation
+ * example:
+# comment
+application/pgp; gpg < %s | metamail; needsterminal; \
+ test=test %{encapsulation}=entity ; copiousoutput
+ */
+static int
+mu_mailcap_parse (mu_mailcap_t mailcap, mu_stream_t stream)
+{
+  int status;
+  size_t n;
+  char *previous;
+  char *buffer;
+  size_t buflen = 512;
+
+  buffer = malloc (buflen);
+  if (buffer == NULL)
+    {
+      return ENOMEM;
+    }
+
+  /*
+   * We are doing this a little more complex then expected, because we do not
+   * want to seek() back in the stream:
+   * - we have to take care of continuation line i.e. line ending with '\'
+   * - we have to take to account that the line may be bigger then the buffer
+   *   and reallocate
+   * - check the return of malloc/realloc
+   * The old continuation line is saved in the "previous" pointer and
+   * prepended to the buffer.
+   */
+  status = mu_stream_seek (stream, 0, MU_SEEK_SET, NULL);
+  if (status)
+    return status;
+  previous = NULL;
+  while ((status = mu_stream_readline (stream, buffer, buflen, &n)) == 0
+	 && n > 0)
+    {
+      int len;
+
+      /* If there is no trailing newline, that means the buffer was too small,
+       * make room for the buffer and continue reading  */
+      if (buffer[n - 1] != '\n')
+        {
+	  char *b = realloc (buffer, buflen * sizeof (*buffer));
+          buflen *= 2;
+          if (b == NULL)
+	    {
+	      status = ENOMEM;
+	      break;
+	    }
+	  buffer = b;
+	  /*
+	   * Fake this as a continuation line, for simplicity.
+	   */
+	  strcat (buffer, "\\");
+        }
+      else
+	{
+	  /* Nuke the trailing newline.  */
+	  buffer[n - 1] = '\0';
+	}
+
+      /* recalculate the len.  */
+      len = strlen (buffer);
+
+      /* Ending with a '\' means continuation line.  */
+      if (len && buffer[len - 1] == '\\')
+	{
+	  buffer[len - 1] = '\0';
+	  /*
+	   * Check for any previous line:
+	   * - if yes append the buffer to the previous line.
+	   * - if not set the buffer as the previous line and continue.
+	   */
+	  if (previous == NULL)
+	    {
+	      previous = strdup (buffer);
+	      if (previous == NULL)
+		{
+		  status = ENOMEM;
+		  break;
+		}
+	    }
+	  else
+	    {
+	      char *b = realloc (previous, strlen (previous) + len + 1);
+	      if (b == NULL)
+		{
+		  status = ENOMEM;
+		  break;
+		}
+	      previous = b;
+	      strcat(previous, buffer);
+	    }
+	}
+      else
+	{
+	  /* Did we have a previous incomplete line?
+	   * if yes make one line from the previous and the buffer.
+	   */
+	  if (previous != NULL)
+	    {
+	      int prev_len = strlen (previous);
+	      int total =  prev_len + len + 1;
+	      if (total > buflen)
+		{
+		  char *b = realloc (buffer, total * sizeof (*buffer));
+		  if (b == NULL)
+		    {
+		      status = ENOMEM;
+		      break;
+		    }
+		  buffer = b;
+		  buflen = total;
+		}
+	      memmove (buffer + prev_len, buffer, len + 1);
+	      memcpy (buffer, previous, prev_len);
+	      free (previous);
+	      previous = NULL;
+	    }
+	}
+
+      /* Parse the well-form mailcap line entry.  */
+      if (previous == NULL) {
+	/* Nuke the trailing/prepend spaces.  */
+	char *line = mu_str_stripws (buffer);
+	/* Ignore comments or empty lines  */
+	if (*line != '#' && *line != '\0')
+	  {
+	    mu_mailcap_entry_t *entries;
+	    entries = realloc (mailcap->entries,
+			       (mailcap->entries_count + 1) *
+			       sizeof (*entries));
+	    if (entries != NULL)
+	      {
+		mailcap->entries = entries;
+		mailcap->entries[mailcap->entries_count] = calloc (1,
+							   sizeof(**entries));
+		if (mailcap->entries[mailcap->entries_count] != NULL)
+		  {
+		    mu_mailcap_parse_entry (mailcap->entries[mailcap->entries_count], line);
+		  }
+		mailcap->entries_count++;
+	      }
+	    else
+	      {
+		status = ENOMEM;
+		break;
+	      }
+	  }
+      }
+    }
+
+  if (buffer != NULL)
+    {
+      free (buffer);
+    }
+  if (previous != NULL)
+    {
+      free (previous);
+    }
+  return status;
+}
+
+#ifdef STANDALONE_TEST
+int main()
+{
+  mu_stream_t stream = NULL;
+  int status = 0;
+
+  status = mu_file_stream_create (&stream, "/home/alain/mailcap",
+				  MU_STREAM_READ);
+  if (status == 0)
+    {
+      mu_mailcap_t mailcap;
+      status = mu_mailcap_create (&mailcap, stream);
+      if (status == 0)
+	{
+	  int i, n;
+	  size_t count = 0;
+	  char buffer[256];
+	  
+	  mu_mailcap_entries_count (mailcap, &count);
+	  for (i = 1; i <= count; i++)
+	    {
+	      int j;
+	      mu_mailcap_entry_t entry = NULL;
+	      int fields_count = 0;
+	      
+	      printf ("entry[%d]\n", i);
+#if 1
+	      
+	      mu_mailcap_get_entry (mailcap, i, &entry);
+	      /* Print typefield.  */
+	      mu_mailcap_entry_get_typefield (entry, buffer,
+					      sizeof (buffer), NULL);
+	      printf ("\ttypefield: %s\n", buffer);
+	      
+	      /* Print view-command.  */
+	      mu_mailcap_entry_get_viewcommand (entry, buffer,
+						sizeof (buffer), NULL);
+	      printf ("\tview-command: %s\n", buffer);
+	      
+	      /* Print fields.  */
+	      mu_mailcap_entry_fields_count (entry, &fields_count);
+	      for (j = 1; j <= fields_count; j++)
+		{
+		  mu_mailcap_entry_get_field (entry, j, buffer,
+					      sizeof (buffer), NULL);
+		  printf("\tfields[%d]: %s\n", j, buffer);
+		}
+	      n = 0;
+	      mu_mailcap_entry_get_compose (entry, buffer,
+					    sizeof (buffer), &n);
+	      if (n > 0)
+		{
+		  printf("\tcompose[%s]\n", buffer);
+		}
+	      printf("\n");
+	    }
+#else
+	  for (i = 0; i < mailcap->entries_count; i++)
+	    {
+	      int j;
+	      mu_mailcap_entry_t entry = mailcap->entries[i];
+	      printf("[%s];[%s]", entry->typefield, entry->viewcommand);
+	      for (j = 0; j < entry->fields_count; j++)
+		{
+		  printf(";[%s]", entry->fields[j]);
+		}
+	      printf("\n");
+	    }
+#endif
+	  mu_mailcap_destroy (&mailcap);
+	}
+    }
+  return 0;
+}
+
+#endif
diff --git a/libmailutils/base/md5.c b/libmailutils/base/md5.c
new file mode 100644
index 0000000..c46ae79
--- /dev/null
+++ b/libmailutils/base/md5.c
@@ -0,0 +1,453 @@
+/* Functions to compute MD5 message digest of files or memory blocks.
+   according to the definition of MD5 in RFC 1321 from April 1992.
+   Copyright (C) 1995,1996,1997,1999,2000,2001,2005,2006,2010
+	Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   This program 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 2, 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, see <http://www.gnu.org/licenses/>. */
+
+/* Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.  */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include "mailutils/md5.h"
+
+#include <stddef.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+
+#if USE_UNLOCKED_IO
+# include "unlocked-io.h"
+#endif
+
+#ifdef _LIBC
+# include <endian.h>
+# if __BYTE_ORDER == __BIG_ENDIAN
+#  define WORDS_BIGENDIAN 1
+# endif
+#endif
+
+/* We need to keep the namespace clean so define the MD5 function
+   protected using leading __ .  */
+# define md5_init_ctx __md5_init_ctx
+# define md5_process_block __md5_process_block
+# define md5_process_bytes __md5_process_bytes
+# define md5_finish_ctx __md5_finish_ctx
+# define md5_read_ctx __md5_read_ctx
+# define md5_stream __md5_stream
+# define md5_buffer __md5_buffer
+
+#ifdef WORDS_BIGENDIAN
+# define SWAP(n)							\
+    (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24))
+#else
+# define SWAP(n) (n)
+#endif
+
+#define BLOCKSIZE 4096
+#if BLOCKSIZE % 64 != 0
+# error "invalid BLOCKSIZE"
+#endif
+
+/* This array contains the bytes used to pad the buffer to the next
+   64-byte boundary.  (RFC 1321, 3.1: Step 1)  */
+static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ...  */ };
+
+
+/* Initialize structure containing state of computation.
+   (RFC 1321, 3.3: Step 3)  */
+void
+md5_init_ctx (struct md5_ctx *ctx)
+{
+  ctx->A = 0x67452301;
+  ctx->B = 0xefcdab89;
+  ctx->C = 0x98badcfe;
+  ctx->D = 0x10325476;
+
+  ctx->total[0] = ctx->total[1] = 0;
+  ctx->buflen = 0;
+}
+
+/* Put result from CTX in first 16 bytes following RESBUF.  The result
+   must be in little endian byte order.
+
+   IMPORTANT: On some systems it is required that RESBUF is correctly
+   aligned for a 32-bit value.  */
+void *
+md5_read_ctx (const struct md5_ctx *ctx, void *resbuf)
+{
+  ((uint32_t *) resbuf)[0] = SWAP (ctx->A);
+  ((uint32_t *) resbuf)[1] = SWAP (ctx->B);
+  ((uint32_t *) resbuf)[2] = SWAP (ctx->C);
+  ((uint32_t *) resbuf)[3] = SWAP (ctx->D);
+
+  return resbuf;
+}
+
+/* Process the remaining bytes in the internal buffer and the usual
+   prolog according to the standard and write the result to RESBUF.
+
+   IMPORTANT: On some systems it is required that RESBUF is correctly
+   aligned for a 32-bit value.  */
+void *
+md5_finish_ctx (struct md5_ctx *ctx, void *resbuf)
+{
+  /* Take yet unprocessed bytes into account.  */
+  uint32_t bytes = ctx->buflen;
+  size_t size = (bytes < 56) ? 64 / 4 : 64 * 2 / 4;
+
+  /* Now count remaining bytes.  */
+  ctx->total[0] += bytes;
+  if (ctx->total[0] < bytes)
+    ++ctx->total[1];
+
+  /* Put the 64-bit file length in *bits* at the end of the buffer.  */
+  ctx->buffer[size - 2] = SWAP (ctx->total[0] << 3);
+  ctx->buffer[size - 1] = SWAP ((ctx->total[1] << 3) | (ctx->total[0] >> 29));
+
+  memcpy (&((char *) ctx->buffer)[bytes], fillbuf, (size - 2) * 4 - bytes);
+
+  /* Process last bytes.  */
+  md5_process_block (ctx->buffer, size * 4, ctx);
+
+  return md5_read_ctx (ctx, resbuf);
+}
+
+/* Compute MD5 message digest for bytes read from STREAM.  The
+   resulting message digest number will be written into the 16 bytes
+   beginning at RESBLOCK.  */
+int
+md5_stream (FILE *stream, void *resblock)
+{
+  struct md5_ctx ctx;
+  char buffer[BLOCKSIZE + 72];
+  size_t sum;
+
+  /* Initialize the computation context.  */
+  md5_init_ctx (&ctx);
+
+  /* Iterate over full file contents.  */
+  while (1)
+    {
+      /* We read the file in blocks of BLOCKSIZE bytes.  One call of the
+         computation function processes the whole buffer so that with the
+         next round of the loop another block can be read.  */
+      size_t n;
+      sum = 0;
+
+      /* Read block.  Take care for partial reads.  */
+      while (1)
+	{
+	  n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
+
+	  sum += n;
+
+	  if (sum == BLOCKSIZE)
+	    break;
+
+	  if (n == 0)
+	    {
+	      /* Check for the error flag IFF N == 0, so that we don't
+	         exit the loop after a partial read due to e.g., EAGAIN
+	         or EWOULDBLOCK.  */
+	      if (ferror (stream))
+		return 1;
+	      goto process_partial_block;
+	    }
+
+	  /* We've read at least one byte, so ignore errors.  But always
+	     check for EOF, since feof may be true even though N > 0.
+	     Otherwise, we could end up calling fread after EOF.  */
+	  if (feof (stream))
+	    goto process_partial_block;
+	}
+
+      /* Process buffer with BLOCKSIZE bytes.  Note that
+         BLOCKSIZE % 64 == 0
+       */
+      md5_process_block (buffer, BLOCKSIZE, &ctx);
+    }
+
+process_partial_block:
+
+  /* Process any remaining bytes.  */
+  if (sum > 0)
+    md5_process_bytes (buffer, sum, &ctx);
+
+  /* Construct result in desired memory.  */
+  md5_finish_ctx (&ctx, resblock);
+  return 0;
+}
+
+/* Compute MD5 message digest for LEN bytes beginning at BUFFER.  The
+   result is always in little endian byte order, so that a byte-wise
+   output yields to the wanted ASCII representation of the message
+   digest.  */
+void *
+md5_buffer (const char *buffer, size_t len, void *resblock)
+{
+  struct md5_ctx ctx;
+
+  /* Initialize the computation context.  */
+  md5_init_ctx (&ctx);
+
+  /* Process whole buffer but last len % 64 bytes.  */
+  md5_process_bytes (buffer, len, &ctx);
+
+  /* Put result in desired memory area.  */
+  return md5_finish_ctx (&ctx, resblock);
+}
+
+
+void
+md5_process_bytes (const void *buffer, size_t len, struct md5_ctx *ctx)
+{
+  /* When we already have some bits in our internal buffer concatenate
+     both inputs first.  */
+  if (ctx->buflen != 0)
+    {
+      size_t left_over = ctx->buflen;
+      size_t add = 128 - left_over > len ? len : 128 - left_over;
+
+      memcpy (&((char *) ctx->buffer)[left_over], buffer, add);
+      ctx->buflen += add;
+
+      if (ctx->buflen > 64)
+	{
+	  md5_process_block (ctx->buffer, ctx->buflen & ~63, ctx);
+
+	  ctx->buflen &= 63;
+	  /* The regions in the following copy operation cannot overlap.  */
+	  memcpy (ctx->buffer,
+		  &((char *) ctx->buffer)[(left_over + add) & ~63],
+		  ctx->buflen);
+	}
+
+      buffer = (const char *) buffer + add;
+      len -= add;
+    }
+
+  /* Process available complete blocks.  */
+  if (len >= 64)
+    {
+#if !_STRING_ARCH_unaligned
+# define alignof(type) offsetof (struct { char c; type x; }, x)
+# define UNALIGNED_P(p) (((size_t) p) % alignof (uint32_t) != 0)
+      if (UNALIGNED_P (buffer))
+	while (len > 64)
+	  {
+	    md5_process_block (memcpy (ctx->buffer, buffer, 64), 64, ctx);
+	    buffer = (const char *) buffer + 64;
+	    len -= 64;
+	  }
+      else
+#endif
+	{
+	  md5_process_block (buffer, len & ~63, ctx);
+	  buffer = (const char *) buffer + (len & ~63);
+	  len &= 63;
+	}
+    }
+
+  /* Move remaining bytes in internal buffer.  */
+  if (len > 0)
+    {
+      size_t left_over = ctx->buflen;
+
+      memcpy (&((char *) ctx->buffer)[left_over], buffer, len);
+      left_over += len;
+      if (left_over >= 64)
+	{
+	  md5_process_block (ctx->buffer, 64, ctx);
+	  left_over -= 64;
+	  memcpy (ctx->buffer, &ctx->buffer[16], left_over);
+	}
+      ctx->buflen = left_over;
+    }
+}
+
+
+/* These are the four functions used in the four steps of the MD5 algorithm
+   and defined in the RFC 1321.  The first function is a little bit optimized
+   (as found in Colin Plumbs public domain implementation).  */
+/* #define FF(b, c, d) ((b & c) | (~b & d)) */
+#define FF(b, c, d) (d ^ (b & (c ^ d)))
+#define FG(b, c, d) FF (d, b, c)
+#define FH(b, c, d) (b ^ c ^ d)
+#define FI(b, c, d) (c ^ (b | ~d))
+
+/* Process LEN bytes of BUFFER, accumulating context into CTX.
+   It is assumed that LEN % 64 == 0.  */
+
+void
+md5_process_block (const void *buffer, size_t len, struct md5_ctx *ctx)
+{
+  uint32_t correct_words[16];
+  const uint32_t *words = buffer;
+  size_t nwords = len / sizeof (uint32_t);
+  const uint32_t *endp = words + nwords;
+  uint32_t A = ctx->A;
+  uint32_t B = ctx->B;
+  uint32_t C = ctx->C;
+  uint32_t D = ctx->D;
+
+  /* First increment the byte count.  RFC 1321 specifies the possible
+     length of the file up to 2^64 bits.  Here we only compute the
+     number of bytes.  Do a double word increment.  */
+  ctx->total[0] += len;
+  if (ctx->total[0] < len)
+    ++ctx->total[1];
+
+  /* Process all bytes in the buffer with 64 bytes in each round of
+     the loop.  */
+  while (words < endp)
+    {
+      uint32_t *cwp = correct_words;
+      uint32_t A_save = A;
+      uint32_t B_save = B;
+      uint32_t C_save = C;
+      uint32_t D_save = D;
+
+      /* First round: using the given function, the context and a constant
+         the next context is computed.  Because the algorithms processing
+         unit is a 32-bit word and it is determined to work on words in
+         little endian byte order we perhaps have to change the byte order
+         before the computation.  To reduce the work for the next steps
+         we store the swapped words in the array CORRECT_WORDS.  */
+
+#define OP(a, b, c, d, s, T)						\
+      do								\
+        {								\
+	  a += FF (b, c, d) + (*cwp++ = SWAP (*words)) + T;		\
+	  ++words;							\
+	  CYCLIC (a, s);						\
+	  a += b;							\
+        }								\
+      while (0)
+
+      /* It is unfortunate that C does not provide an operator for
+         cyclic rotation.  Hope the C compiler is smart enough.  */
+#define CYCLIC(w, s) (w = (w << s) | (w >> (32 - s)))
+
+      /* Before we start, one word to the strange constants.
+         They are defined in RFC 1321 as
+
+         T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64
+
+         Here is an equivalent invocation using Perl:
+
+         perl -e 'foreach(1..64){printf "0x%08x\n", int (4294967296 * abs (sin $_))}'
+       */
+
+      /* Round 1.  */
+      OP (A, B, C, D, 7, 0xd76aa478);
+      OP (D, A, B, C, 12, 0xe8c7b756);
+      OP (C, D, A, B, 17, 0x242070db);
+      OP (B, C, D, A, 22, 0xc1bdceee);
+      OP (A, B, C, D, 7, 0xf57c0faf);
+      OP (D, A, B, C, 12, 0x4787c62a);
+      OP (C, D, A, B, 17, 0xa8304613);
+      OP (B, C, D, A, 22, 0xfd469501);
+      OP (A, B, C, D, 7, 0x698098d8);
+      OP (D, A, B, C, 12, 0x8b44f7af);
+      OP (C, D, A, B, 17, 0xffff5bb1);
+      OP (B, C, D, A, 22, 0x895cd7be);
+      OP (A, B, C, D, 7, 0x6b901122);
+      OP (D, A, B, C, 12, 0xfd987193);
+      OP (C, D, A, B, 17, 0xa679438e);
+      OP (B, C, D, A, 22, 0x49b40821);
+
+      /* For the second to fourth round we have the possibly swapped words
+         in CORRECT_WORDS.  Redefine the macro to take an additional first
+         argument specifying the function to use.  */
+#undef OP
+#define OP(f, a, b, c, d, k, s, T)					\
+      do								\
+	{								\
+	  a += f (b, c, d) + correct_words[k] + T;			\
+	  CYCLIC (a, s);						\
+	  a += b;							\
+	}								\
+      while (0)
+
+      /* Round 2.  */
+      OP (FG, A, B, C, D, 1, 5, 0xf61e2562);
+      OP (FG, D, A, B, C, 6, 9, 0xc040b340);
+      OP (FG, C, D, A, B, 11, 14, 0x265e5a51);
+      OP (FG, B, C, D, A, 0, 20, 0xe9b6c7aa);
+      OP (FG, A, B, C, D, 5, 5, 0xd62f105d);
+      OP (FG, D, A, B, C, 10, 9, 0x02441453);
+      OP (FG, C, D, A, B, 15, 14, 0xd8a1e681);
+      OP (FG, B, C, D, A, 4, 20, 0xe7d3fbc8);
+      OP (FG, A, B, C, D, 9, 5, 0x21e1cde6);
+      OP (FG, D, A, B, C, 14, 9, 0xc33707d6);
+      OP (FG, C, D, A, B, 3, 14, 0xf4d50d87);
+      OP (FG, B, C, D, A, 8, 20, 0x455a14ed);
+      OP (FG, A, B, C, D, 13, 5, 0xa9e3e905);
+      OP (FG, D, A, B, C, 2, 9, 0xfcefa3f8);
+      OP (FG, C, D, A, B, 7, 14, 0x676f02d9);
+      OP (FG, B, C, D, A, 12, 20, 0x8d2a4c8a);
+
+      /* Round 3.  */
+      OP (FH, A, B, C, D, 5, 4, 0xfffa3942);
+      OP (FH, D, A, B, C, 8, 11, 0x8771f681);
+      OP (FH, C, D, A, B, 11, 16, 0x6d9d6122);
+      OP (FH, B, C, D, A, 14, 23, 0xfde5380c);
+      OP (FH, A, B, C, D, 1, 4, 0xa4beea44);
+      OP (FH, D, A, B, C, 4, 11, 0x4bdecfa9);
+      OP (FH, C, D, A, B, 7, 16, 0xf6bb4b60);
+      OP (FH, B, C, D, A, 10, 23, 0xbebfbc70);
+      OP (FH, A, B, C, D, 13, 4, 0x289b7ec6);
+      OP (FH, D, A, B, C, 0, 11, 0xeaa127fa);
+      OP (FH, C, D, A, B, 3, 16, 0xd4ef3085);
+      OP (FH, B, C, D, A, 6, 23, 0x04881d05);
+      OP (FH, A, B, C, D, 9, 4, 0xd9d4d039);
+      OP (FH, D, A, B, C, 12, 11, 0xe6db99e5);
+      OP (FH, C, D, A, B, 15, 16, 0x1fa27cf8);
+      OP (FH, B, C, D, A, 2, 23, 0xc4ac5665);
+
+      /* Round 4.  */
+      OP (FI, A, B, C, D, 0, 6, 0xf4292244);
+      OP (FI, D, A, B, C, 7, 10, 0x432aff97);
+      OP (FI, C, D, A, B, 14, 15, 0xab9423a7);
+      OP (FI, B, C, D, A, 5, 21, 0xfc93a039);
+      OP (FI, A, B, C, D, 12, 6, 0x655b59c3);
+      OP (FI, D, A, B, C, 3, 10, 0x8f0ccc92);
+      OP (FI, C, D, A, B, 10, 15, 0xffeff47d);
+      OP (FI, B, C, D, A, 1, 21, 0x85845dd1);
+      OP (FI, A, B, C, D, 8, 6, 0x6fa87e4f);
+      OP (FI, D, A, B, C, 15, 10, 0xfe2ce6e0);
+      OP (FI, C, D, A, B, 6, 15, 0xa3014314);
+      OP (FI, B, C, D, A, 13, 21, 0x4e0811a1);
+      OP (FI, A, B, C, D, 4, 6, 0xf7537e82);
+      OP (FI, D, A, B, C, 11, 10, 0xbd3af235);
+      OP (FI, C, D, A, B, 2, 15, 0x2ad7d2bb);
+      OP (FI, B, C, D, A, 9, 21, 0xeb86d391);
+
+      /* Add the starting values of the context.  */
+      A += A_save;
+      B += B_save;
+      C += C_save;
+      D += D_save;
+    }
+
+  /* Put checksum in context given as argument.  */
+  ctx->A = A;
+  ctx->B = B;
+  ctx->C = C;
+  ctx->D = D;
+}
diff --git a/libmailutils/base/monitor.c b/libmailutils/base/monitor.c
new file mode 100644
index 0000000..f9b1593
--- /dev/null
+++ b/libmailutils/base/monitor.c
@@ -0,0 +1,281 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 2004, 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, see
+   <http://www.gnu.org/licenses/>. */
+
+/* Tell GLIBC that we want UNIX98 pthread_rwlock_xx() functions.  */
+#define _XOPEN_SOURCE   500
+/* Tell QNX/Neutrino to define pthread_rwlock_xx() functions.  */
+#define _QNX_SOURCE
+#define _POSIX_C_SOURCE 199506
+#ifdef HAVE_CONFIG_H
+#  include <config.h>
+#endif
+
+#ifdef WITH_PTHREAD
+#  ifdef HAVE_PTHREAD_H
+#    include <pthread.h>
+#  endif
+#endif
+#include <errno.h>
+#include <stdlib.h>
+
+#include <mailutils/sys/monitor.h>
+#include <mailutils/errno.h>
+
+#ifdef WITH_PTHREAD
+pthread_mutex_t monitor_lock = PTHREAD_MUTEX_INITIALIZER;
+#  define STATIC_LOCK(m) pthread_mutex_lock(m)
+#  define STATIC_UNLOCK(m) pthread_mutex_unlock(m)
+#else
+#  define STATIC_LOCK(m) 0
+#  define STATIC_UNLOCK(m)
+int monitor_lock;
+#endif
+
+union _p_lock
+{
+#ifdef WITH_PTHREAD
+#  ifdef HAVE_PTHREAD_RWLOCK_INIT
+     pthread_rwlock_t mutex;
+#  else
+     pthread_mutex_t mutex;
+#  endif
+#else
+  int dummy;
+#endif
+};
+
+typedef union _p_lock *p_lock_t;
+static int  monitor_pthread_create (p_lock_t *);
+static void monitor_pthread_destroy (p_lock_t *);
+static int monitor_pthread_rdlock (p_lock_t);
+static int monitor_pthread_wrlock (p_lock_t);
+static int monitor_pthread_unlock (p_lock_t);
+
+/* The idea was to have a general/portable object mu_monitor_t.
+   The mu_monitor_t object could have different implementation (on the fly ?)
+   of locking.  Also the rest of the library would not have to know about
+   different threading implementation.  So far we've pretty much hardcoded
+   the concrete implementation of monitor on pthread and read/write locks,
+   but changing to a different concrete implementation will not be hard if
+   the need arise.
+   For static initializers we take a small penality and since we have
+   a global static lock.
+ */
+
+int
+mu_monitor_create (mu_monitor_t *pmonitor, int flags, void *owner)
+{
+  mu_monitor_t monitor;
+
+  if (pmonitor == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+
+  monitor = calloc (1, sizeof (*monitor));
+  if (monitor == NULL)
+    return ENOMEM;
+
+  if (flags == MU_MONITOR_PTHREAD)
+    {
+      int status = monitor_pthread_create ((p_lock_t *)&(monitor->data));
+      if (status != 0)
+	{
+	  free (monitor);
+	  return status;
+	}
+    }
+  monitor->owner = owner;
+  monitor->allocated = 1;
+  monitor->flags = flags;
+  *pmonitor = monitor;
+  return 0;
+}
+
+void *
+mu_monitor_get_owner (mu_monitor_t monitor)
+{
+  return (monitor == NULL) ? NULL : monitor->owner;
+}
+
+void
+mu_monitor_destroy (mu_monitor_t *pmonitor, void *owner)
+{
+  if (pmonitor && *pmonitor)
+    {
+      mu_monitor_t monitor = *pmonitor;
+      if (monitor->owner == owner)
+	{
+	  if (monitor->flags == MU_MONITOR_PTHREAD)
+	    monitor_pthread_destroy ((p_lock_t *)&(monitor->data));
+	}
+      free (monitor);
+      *pmonitor = NULL;
+    }
+}
+
+int
+mu_monitor_rdlock (mu_monitor_t monitor)
+{
+  if (monitor)
+    {
+      if (!monitor->allocated)
+	{
+	  int status = STATIC_LOCK (&monitor_lock);
+	  if (monitor->data == NULL)
+	    {
+	      if (monitor->flags == MU_MONITOR_PTHREAD)
+		status = monitor_pthread_create ((p_lock_t*)&(monitor->data));
+	      if (status != 0)
+		{
+		  STATIC_UNLOCK (&monitor_lock);
+		  return status;
+		}
+	    }
+	  monitor->allocated = 1;
+	  STATIC_UNLOCK (&monitor_lock);
+	}
+      if (monitor->flags == MU_MONITOR_PTHREAD)
+	return monitor_pthread_rdlock ((p_lock_t)monitor->data);
+    }
+  return 0;
+}
+
+int
+mu_monitor_wrlock  (mu_monitor_t monitor)
+{
+  if (monitor)
+    {
+      if (!monitor->allocated)
+	{
+	  int status = STATIC_LOCK (&monitor_lock);
+	  if (monitor->data == NULL)
+	    {
+	      if (monitor->flags == MU_MONITOR_PTHREAD)
+		status = monitor_pthread_create ((p_lock_t *)&(monitor->data));
+	      if (status != 0)
+		{
+		  STATIC_UNLOCK (&monitor_lock);
+		  return status;
+		}
+	    }
+	  monitor->allocated = 1;
+	  STATIC_UNLOCK (&monitor_lock);
+	}
+      if (monitor->flags == MU_MONITOR_PTHREAD)
+	return monitor_pthread_wrlock ((p_lock_t)monitor->data);
+    }
+  return 0;
+}
+
+int
+mu_monitor_unlock (mu_monitor_t monitor)
+{
+  if (monitor)
+    {
+      if (monitor->flags == MU_MONITOR_PTHREAD)
+	return monitor_pthread_unlock ((p_lock_t)monitor->data);
+    }
+  return 0;
+}
+
+int
+mu_monitor_wait (mu_monitor_t monitor MU_ARG_UNUSED)
+{
+  return ENOSYS;
+}
+
+int
+mu_monitor_notify (mu_monitor_t monitor MU_ARG_UNUSED)
+{
+  return ENOSYS;
+}
+
+
+/* Concrete Implementation of pthread base on rwlocks.  */
+
+#ifdef WITH_PTHREAD
+#  ifdef HAVE_PTHREAD_RWLOCK_INIT
+#    define RWLOCK_INIT(rwl, attr)  pthread_rwlock_init (rwl, attr)
+#    define RWLOCK_DESTROY(rwl)     pthread_rwlock_destroy (rwl)
+#    define RWLOCK_RDLOCK(rwl)      pthread_rwlock_rdlock (rwl)
+#    define RWLOCK_WRLOCK(rwl)      pthread_rwlock_wrlock (rwl)
+#    define RWLOCK_UNLOCK(rwl)      pthread_rwlock_unlock (rwl)
+#  else
+#    define RWLOCK_INIT(rwl, attr)  pthread_mutex_init (rwl, attr)
+#    define RWLOCK_DESTROY(rwl)     pthread_mutex_destroy (rwl)
+#    define RWLOCK_RDLOCK(rwl)      pthread_mutex_lock (rwl)
+#    define RWLOCK_WRLOCK(rwl)      pthread_mutex_lock (rwl)
+#    define RWLOCK_UNLOCK(rwl)      pthread_mutex_unlock (rwl)
+#  endif
+#else
+#  define RWLOCK_INIT(rwl, attr)    0
+#  define RWLOCK_DESTROY(rwl)
+#  define RWLOCK_RDLOCK(rwl)        0
+#  define RWLOCK_WRLOCK(rwl)        0
+#  define RWLOCK_UNLOCK(rwl)        0
+#  define flockfile(arg)
+#  define funlockfile(arg)
+#endif
+
+
+
+static int
+monitor_pthread_create (p_lock_t *plock)
+{
+  int status;
+  p_lock_t lock = calloc (1, sizeof (*lock));
+  if (lock == NULL)
+    return ENOMEM;
+  status = RWLOCK_INIT (&(lock->mutex), NULL);
+  if (status != 0)
+    {
+      free (lock);
+      return status;
+    }
+  *plock = lock;
+  return 0;
+}
+
+static void
+monitor_pthread_destroy (p_lock_t *plock)
+{
+  p_lock_t lock = *plock;
+  if (lock)
+    {
+      RWLOCK_DESTROY (&(lock->mutex));
+      free (lock);
+    }
+  *plock = NULL;
+}
+
+static int
+monitor_pthread_rdlock (p_lock_t lock)
+{
+  return RWLOCK_RDLOCK (&(lock->mutex));
+}
+
+static int
+monitor_pthread_wrlock (p_lock_t lock)
+{
+  return RWLOCK_WRLOCK (&(lock->mutex));
+}
+
+static int
+monitor_pthread_unlock (p_lock_t lock)
+{
+  return RWLOCK_UNLOCK (&(lock->mutex));
+}
diff --git a/libmailutils/base/munre.c b/libmailutils/base/munre.c
new file mode 100644
index 0000000..69f6d7e
--- /dev/null
+++ b/libmailutils/base/munre.c
@@ -0,0 +1,92 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 2002, 2003, 2006, 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, see 
+   <http://www.gnu.org/licenses/>. */
+
+#if HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <errno.h>
+#include <stdlib.h>
+
+#include <mailutils/error.h>
+#include <mailutils/errno.h>
+
+#include <regex.h>
+
+static regex_t *re_prefix;
+
+int
+mu_unre_set_regex (const char *str, int caseflag, char **errp)
+{
+  int rc;
+  int flags = REG_EXTENDED;
+
+  if (errp)
+    *errp = NULL;
+
+  if (!str)
+    str = "^re: *";
+  if (re_prefix)
+    regfree (re_prefix);
+  else
+    {
+      re_prefix = malloc (sizeof (*re_prefix));
+      if (!re_prefix)
+	return ENOMEM;
+    }
+  if (!caseflag)
+    flags |= REG_ICASE;
+  rc = regcomp (re_prefix, str, flags);
+  if (rc)
+    {
+      if (errp)
+	{
+	  size_t s = regerror (rc, re_prefix, NULL, 0);
+	  s++;
+	  *errp = malloc (s);
+	  if (*errp)
+	    regerror (rc, re_prefix, *errp, s);
+	}
+      regfree (re_prefix);
+      free (re_prefix);
+      return MU_ERR_FAILURE;
+    }
+  return 0;
+}
+
+int
+mu_unre_subject (const char *subject, const char **new_subject)
+{
+  int rc;
+  regmatch_t rm;
+
+  if (!subject)
+    return EINVAL;
+  
+  if (!re_prefix)
+    {
+      rc = mu_unre_set_regex (NULL, 0, NULL);
+      if (rc)
+	return rc;
+    }
+
+  rc = regexec (re_prefix, subject, 1, &rm, 0);
+  if (rc == 0 && rm.rm_eo != -1 && new_subject)
+    *new_subject = subject + rm.rm_eo;
+  return rc;
+}
diff --git a/libmailutils/base/mutil.c b/libmailutils/base/mutil.c
new file mode 100644
index 0000000..8612106
--- /dev/null
+++ b/libmailutils/base/mutil.c
@@ -0,0 +1,1535 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 2002, 2003, 2005, 2006, 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, see 
+   <http://www.gnu.org/licenses/>. */
+
+#if HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <errno.h>
+#include <limits.h>
+#include <netdb.h>
+#include <pwd.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+#include <ctype.h>
+#include <sys/param.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/time.h>
+#include <sys/wait.h>
+#include <sys/select.h>
+
+#ifdef HAVE_STRINGS_H
+#include <strings.h>
+#endif
+
+#include <mailutils/address.h>
+#include <mailutils/assoc.h>
+#include <mailutils/argcv.h>
+#include <mailutils/error.h>
+#include <mailutils/errno.h>
+#include <mailutils/iterator.h>
+#include <mailutils/mutil.h>
+#include <mailutils/parse822.h>
+#include <mailutils/mu_auth.h>
+#include <mailutils/header.h>
+#include <mailutils/message.h>
+#include <mailutils/envelope.h>
+#include <mailutils/nls.h>
+#include <mailutils/stream.h>
+#include <mailutils/filter.h>
+#include <mailutils/sql.h>
+#include <mailutils/url.h>
+#include <mailutils/io.h>
+#include <mailutils/cctype.h>
+#include <mailutils/cstr.h>
+
+#include <mailutils/sys/registrar.h>
+
+/* convert a sequence of hex characters into an integer */
+
+unsigned long
+mu_hex2ul (char hex)
+{
+  if (hex >= '0' && hex <= '9')
+   return hex - '0';
+
+  if (hex >= 'a' && hex <= 'z')
+    return hex - 'a' + 10;
+
+  if (hex >= 'A' && hex <= 'Z')
+    return hex - 'A' + 10;
+
+  return -1;
+}
+
+size_t
+mu_hexstr2ul (unsigned long *ul, const char *hex, size_t len)
+{
+  size_t r;
+
+  *ul = 0;
+
+  for (r = 0; r < len; r++)
+    {
+      unsigned long v = mu_hex2ul (hex[r]);
+
+      if (v == (unsigned long)-1)
+	return r;
+
+      *ul = *ul * 16 + v;
+    }
+  return r;
+}
+
+
+char *
+mu_get_homedir (void)
+{
+  char *homedir = getenv ("HOME");
+  if (homedir)
+    homedir = strdup (homedir);
+  else
+    {
+      struct mu_auth_data *auth = mu_get_auth_by_uid (geteuid ());
+      if (!auth)
+	return NULL;
+      homedir = strdup (auth->dir);
+      mu_auth_data_free (auth);
+    }
+  return homedir;
+}
+
+char *
+mu_getcwd ()
+{
+  char *ret;
+  unsigned path_max;
+  char buf[128];
+
+  errno = 0;
+  ret = getcwd (buf, sizeof (buf));
+  if (ret != NULL)
+    return strdup (buf);
+
+  if (errno != ERANGE)
+    return NULL;
+
+  path_max = 128;
+  path_max += 2;                /* The getcwd docs say to do this. */
+
+  for (;;)
+    {
+      char *cwd = (char *) malloc (path_max);
+
+      errno = 0;
+      ret = getcwd (cwd, path_max);
+      if (ret != NULL)
+        return ret;
+      if (errno != ERANGE)
+        {
+          int save_errno = errno;
+          free (cwd);
+          errno = save_errno;
+          return NULL;
+        }
+
+      free (cwd);
+
+      path_max += path_max / 16;
+      path_max += 32;
+    }
+  /* oops?  */
+  return NULL;
+}
+
+char *
+mu_get_full_path (const char *file)
+{
+  char *p = NULL;
+
+  if (!file)
+    p = mu_getcwd ();
+  else if (*file != '/')
+    {
+      char *cwd = mu_getcwd ();
+      if (cwd)
+	{
+	  p = calloc (strlen (cwd) + 1 + strlen (file) + 1, 1);
+	  if (p)
+	    sprintf (p, "%s/%s", cwd, file);
+	  free (cwd);
+	}
+    }
+
+  if (!p)
+    p = strdup (file);
+  return p;
+}
+
+/* NOTE: Allocates Memory.  */
+/* Expand: ~ --> /home/user and to ~guest --> /home/guest.  */
+char *
+mu_tilde_expansion (const char *ref, const char *delim, const char *homedir)
+{
+  char *base = strdup (ref);
+  char *home = NULL;
+  char *proto = NULL;
+  size_t proto_len = 0;
+  char *p;
+
+  for (p = base; *p && isascii (*p) && isalnum (*p); p++)
+    ;
+  
+  if (*p == ':')
+    {
+      p++;
+      proto_len = p - base;
+      proto = malloc (proto_len + 1);
+      if (!proto)
+	return NULL;
+      memcpy (proto, base, proto_len);
+      proto[proto_len] = 0;
+      /* Allow for extra pair of slashes after the protocol specifier */
+      if (*p == delim[0])
+	p++;
+      if (*p == delim[0])
+	p++;
+    }
+  else
+    p = base;
+  
+  if (*p == '~')
+    {
+      p++;
+      if (*p == delim[0] || *p == '\0')
+        {
+	  char *s;
+	  if (!homedir)
+	    {
+	      home = mu_get_homedir ();
+	      if (!home)
+		return base;
+	      homedir = home;
+	    }
+	  s = calloc (proto_len + strlen (homedir) + strlen (p) + 1, 1);
+	  if (proto_len)
+	    strcpy (s, proto);
+	  else
+	    s[0] = 0;
+          strcat (s, homedir);
+          strcat (s, p);
+          free (base);
+          base = s;
+        }
+      else
+        {
+          struct mu_auth_data *auth;
+          char *s = p;
+          char *name;
+          while (*s && *s != delim[0])
+            s++;
+          name = calloc (s - p + 1, 1);
+          memcpy (name, p, s - p);
+          name[s - p] = '\0';
+	  
+          auth = mu_get_auth_by_name (name);
+          free (name);
+          if (auth)
+            {
+              char *buf = calloc (proto_len + strlen (auth->dir)
+				  + strlen (s) + 1, 1);
+	      if (proto_len)
+		strcpy (buf, proto);
+	      else
+		buf[0] = 0;
+	      strcat (buf, auth->dir);
+              strcat (buf, s);
+              free (base);
+              base = buf;
+	      mu_auth_data_free (auth);
+            }
+        }
+    }
+  if (home)
+    free (home);
+  return base;
+}
+
+/* Smart strncpy that always add the null and returns the number of bytes
+   written.  */
+size_t
+mu_cpystr (char *dst, const char *src, size_t size)
+{
+  size_t len = src ? strlen (src) : 0 ;
+  if (dst == NULL || size == 0)
+    return len;
+  if (len >= size)
+    len = size - 1;
+  memcpy (dst, src, len);
+  dst[len] = '\0';
+  return len;
+}
+
+#ifndef MAXHOSTNAMELEN
+# define MAXHOSTNAMELEN 64
+#endif
+
+int
+mu_get_host_name (char **host)
+{
+  char *hostname = NULL;
+  size_t size = 0;
+  char *p;
+
+  while (1)
+    {
+      if (size == 0)
+	{
+	  size = MAXHOSTNAMELEN;
+	  p = malloc (size);
+	}
+      else
+	{
+	  size_t ns = size * 2;
+	  if (ns < size)
+	    {
+	      free (hostname);
+	      return ENOMEM;
+	    }
+	  size = ns;
+	  p = realloc (hostname, size);
+	}
+      if (!p)
+	{
+	  free (hostname);
+	  return ENOMEM;
+	}
+      hostname = p;
+      hostname[size - 1] = 0;
+      if (gethostname (hostname, size - 1) == 0)
+	{
+	  if (!hostname[size - 1])
+	    break;
+	}
+      else if (errno != 0 && errno != ENAMETOOLONG && errno != EINVAL
+	       && errno != ENOMEM)
+	{
+	  int rc = errno;
+	  free (hostname);
+	  return rc;
+	}
+    }
+
+  /* Try to return fully qualified host name */
+  if (!strchr (hostname, '.'))
+    {
+      struct hostent *hp = gethostbyname (hostname);
+      if (hp)
+	{
+	  size_t len = strlen (hp->h_name);
+	  if (size < len + 1)
+	    {
+	      p = realloc (hostname, len + 1);
+	      if (!p)
+		{
+		  free (hostname);
+		  return ENOMEM;
+		}
+	      hostname = p;
+	    }
+	  strcpy (hostname, hp->h_name);
+	}
+    }
+  
+  *host = hostname;
+  return 0;
+}  
+
+/*
+ * Functions used to convert unix mailbox/user names into RFC822 addr-specs.
+ */
+
+static char *mu_user_email = 0;
+
+int
+mu_set_user_email (const char *candidate)
+{
+  int err = 0;
+  mu_address_t addr = NULL;
+  size_t emailno = 0;
+  char *email = NULL;
+  const char *domain = NULL;
+  
+  if ((err = mu_address_create (&addr, candidate)) != 0)
+    return err;
+
+  if ((err = mu_address_get_email_count (addr, &emailno)) != 0)
+    goto cleanup;
+
+  if (emailno != 1)
+    {
+      errno = EINVAL;
+      goto cleanup;
+    }
+
+  if ((err = mu_address_aget_email (addr, 1, &email)) != 0)
+    goto cleanup;
+
+  if (mu_user_email)
+    free (mu_user_email);
+
+  mu_user_email = email;
+
+  if ((err = mu_address_sget_domain (addr, 1, &domain)) == 0)
+    mu_set_user_email_domain (domain);
+  
+cleanup:
+  mu_address_destroy (&addr);
+
+  return err;
+}
+
+static char *mu_user_email_domain = 0;
+
+int
+mu_set_user_email_domain (const char *domain)
+{
+  char *d = NULL;
+  
+  if (!domain)
+    return EINVAL;
+  
+  d = strdup (domain);
+
+  if (!d)
+    return ENOMEM;
+
+  if (mu_user_email_domain)
+    free (mu_user_email_domain);
+
+  mu_user_email_domain = d;
+
+  return 0;
+}
+
+/* FIXME: must be called _sget_ */
+int
+mu_get_user_email_domain (const char **domain)
+{
+  int err = 0;
+
+  if (!mu_user_email_domain)
+    {
+      if ((err = mu_get_host_name (&mu_user_email_domain)))
+	return err;
+    }
+
+  *domain = mu_user_email_domain;
+
+  return 0;
+}
+
+int
+mu_aget_user_email_domain (char **pdomain)
+{
+  const char *domain;
+  int status = mu_get_user_email_domain (&domain);
+  if (status)
+    return status;
+  if (domain == NULL)
+    *pdomain = NULL;
+  else
+    {
+      *pdomain = strdup (domain);
+      if (*pdomain == NULL)
+	return ENOMEM;
+    }
+  return 0;
+}
+
+/* Note: allocates memory */
+char *
+mu_get_user_email (const char *name)
+{
+  int status = 0;
+  char *localpart = NULL;
+  const char *domainpart = NULL;
+  char *email = NULL;
+  char *tmpname = NULL;
+
+  if (!name && mu_user_email)
+    {
+      email = strdup (mu_user_email);
+      if (!email)
+	errno = ENOMEM;
+      return email;
+    }
+
+  if (!name)
+    {
+      struct mu_auth_data *auth = mu_get_auth_by_uid (getuid ());
+      if (!auth)
+	{
+	  errno = EINVAL;
+	  return NULL;
+	}
+      name = tmpname = strdup(auth->name);
+      if (auth)
+	mu_auth_data_free (auth);
+    }
+
+  status = mu_get_user_email_domain (&domainpart);
+
+  if (status)
+    {
+      free(tmpname);
+      errno = status;
+      return NULL;
+    }
+
+  if ((status = mu_parse822_quote_local_part (&localpart, name)))
+    {
+      free(tmpname);
+      errno = status;
+      return NULL;
+    }
+
+
+  email = malloc (strlen (localpart) + 1
+		  + strlen (domainpart) + 1);
+  if (!email)
+    errno = ENOMEM;
+  else
+    sprintf (email, "%s@%s", localpart, domainpart);
+
+  free(tmpname);
+  free (localpart);
+
+  return email;
+}
+
+/* mu_normalize_path: convert pathname containig relative paths specs (../)
+   into an equivalent absolute path. Strip trailing delimiter if present,
+   unless it is the only character left. E.g.:
+
+         /home/user/../smith   -->   /home/smith
+	 /home/user/../..      -->   /
+
+*/
+char *
+mu_normalize_path (char *path)
+{
+  int len;
+  char *p;
+
+  if (!path)
+    return path;
+
+  len = strlen (path);
+
+  /* Empty string is returned as is */
+  if (len == 0)
+    return path;
+
+  /* delete trailing delimiter if any */
+  if (len && path[len-1] == '/')
+    path[len-1] = 0;
+
+  /* Eliminate any /../ */
+  for (p = strchr (path, '.'); p; p = strchr (p, '.'))
+    {
+      if (p > path && p[-1] == '/')
+	{
+	  if (p[1] == '.' && (p[2] == 0 || p[2] == '/'))
+	    /* found */
+	    {
+	      char *q, *s;
+
+	      /* Find previous delimiter */
+	      for (q = p-2; *q != '/' && q >= path; q--)
+		;
+
+	      if (q < path)
+		break;
+	      /* Copy stuff */
+	      s = p + 2;
+	      p = q;
+	      while ((*q++ = *s++))
+		;
+	      continue;
+	    }
+	}
+
+      p++;
+    }
+
+  if (path[0] == 0)
+    {
+      path[0] = '/';
+      path[1] = 0;
+    }
+
+  return path;
+}
+
+/* Create and open a temporary file. Be very careful about it, since we
+   may be running with extra privilege i.e setgid().
+   Returns file descriptor of the open file.
+   If namep is not NULL, the pointer to the malloced file name will
+   be stored there. Otherwise, the file is unlinked right after open,
+   i.e. it will disappear after close(fd). */
+
+#ifndef P_tmpdir
+# define P_tmpdir "/tmp"
+#endif
+
+int
+mu_tempfile (const char *tmpdir, char **namep)
+{
+  char *filename;
+  int fd;
+
+  if (!tmpdir)
+    tmpdir = (getenv ("TMPDIR")) ? getenv ("TMPDIR") : P_tmpdir;
+
+  filename = malloc (strlen (tmpdir) + /*'/'*/1 + /* "muXXXXXX" */8 + 1);
+  if (!filename)
+    return -1;
+  sprintf (filename, "%s/muXXXXXX", tmpdir);
+
+#ifdef HAVE_MKSTEMP
+  {
+    int save_mask = umask (077);
+    fd = mkstemp (filename);
+    umask (save_mask);
+  }
+#else
+  if (mktemp (filename))
+    fd = open (filename, O_CREAT|O_EXCL|O_RDWR, 0600);
+  else
+    fd = -1;
+#endif
+
+  if (fd == -1)
+    {
+      mu_error (_("cannot open temporary file: %s"), mu_strerror (errno));
+      free (filename);
+      return -1;
+    }
+
+  if (namep)
+    *namep = filename;
+  else
+    {
+      unlink (filename);
+      free (filename);
+    }
+
+  return fd;
+}
+
+/* Create a unique temporary file name in tmpdir. The function
+   creates an empty file with this name to avoid possible race
+   conditions. Returns a pointer to the malloc'ed file name.
+   If tmpdir is NULL, the value of the environment variable
+   TMPDIR or the hardcoded P_tmpdir is used, whichever is defined. */
+
+char *
+mu_tempname (const char *tmpdir)
+{
+  char *filename = NULL;
+  int fd = mu_tempfile (tmpdir, &filename);
+  close (fd);
+  return filename;
+}
+
+/* See Advanced Programming in the UNIX Environment, Stevens,
+ * program  10.20 for the rational for the signal handling. I
+ * had to look it up, so if somebody else is curious, thats where
+ * to find it.
+ */
+int 
+mu_spawnvp (const char *prog, char *av[], int *stat)
+{
+  pid_t pid;
+  int err = 0;
+  int progstat;
+  struct sigaction ignore;
+  struct sigaction saveintr;
+  struct sigaction savequit;
+  sigset_t chldmask;
+  sigset_t savemask;
+
+  if (!prog || !av)
+    return EINVAL;
+
+  ignore.sa_handler = SIG_IGN;	/* ignore SIGINT and SIGQUIT */
+  ignore.sa_flags = 0;
+  sigemptyset (&ignore.sa_mask);
+
+  if (sigaction (SIGINT, &ignore, &saveintr) < 0)
+    return errno;
+  if (sigaction (SIGQUIT, &ignore, &savequit) < 0)
+    {
+      sigaction (SIGINT, &saveintr, NULL);
+      return errno;
+    }
+
+  sigemptyset (&chldmask);	/* now block SIGCHLD */
+  sigaddset (&chldmask, SIGCHLD);
+
+  if (sigprocmask (SIG_BLOCK, &chldmask, &savemask) < 0)
+    {
+      sigaction (SIGINT, &saveintr, NULL);
+      sigaction (SIGQUIT, &savequit, NULL);
+      return errno;
+    }
+
+#ifdef HAVE_VFORK
+  pid = vfork ();
+#else
+  pid = fork ();
+#endif
+
+  if (pid < 0)
+    {
+      err = errno;
+    }
+  else if (pid == 0)
+    {				/* child */
+      /* restore previous signal actions & reset signal mask */
+      sigaction (SIGINT, &saveintr, NULL);
+      sigaction (SIGQUIT, &savequit, NULL);
+      sigprocmask (SIG_SETMASK, &savemask, NULL);
+
+      execvp (prog, av);
+#ifdef HAVE__EXIT      
+      _exit (127);		/* exec error */
+#else
+      exit (127);
+#endif
+    }
+  else
+    {				/* parent */
+      while (waitpid (pid, &progstat, 0) < 0)
+	if (errno != EINTR)
+	  {
+	    err = errno;	/* error other than EINTR from waitpid() */
+	    break;
+	  }
+      if (err == 0 && stat)
+	*stat = progstat;
+    }
+
+  /* restore previous signal actions & reset signal mask */
+  /* preserve first error number, but still try and reset the signals */
+  if (sigaction (SIGINT, &saveintr, NULL) < 0)
+    err = err ? err : errno;
+  if (sigaction (SIGQUIT, &savequit, NULL) < 0)
+    err = err ? err : errno;
+  if (sigprocmask (SIG_SETMASK, &savemask, NULL) < 0)
+    err = err ? err : errno;
+
+  return err;
+}
+
+/* The result of readlink() may be a path relative to that link, 
+ * qualify it if necessary.
+ */
+static void
+mu_qualify_link (const char *path, const char *link, char *qualified)
+{
+  const char *lb = NULL;
+  size_t len;
+
+  /* link is full path */
+  if (*link == '/')
+    {
+      mu_cpystr (qualified, link, _POSIX_PATH_MAX);
+      return;
+    }
+
+  if ((lb = strrchr (path, '/')) == NULL)
+    {
+      /* no path in link */
+      mu_cpystr (qualified, link, _POSIX_PATH_MAX);
+      return;
+    }
+
+  len = lb - path + 1;
+  memcpy (qualified, path, len);
+  mu_cpystr (qualified + len, link, _POSIX_PATH_MAX - len);
+}
+
+#ifndef _POSIX_SYMLOOP_MAX
+# define _POSIX_SYMLOOP_MAX 255
+#endif
+
+int
+mu_unroll_symlink (char *out, size_t outsz, const char *in)
+{
+  char path[_POSIX_PATH_MAX];
+  int symloops = 0;
+
+  while (symloops++ < _POSIX_SYMLOOP_MAX)
+    {
+      struct stat s;
+      char link[_POSIX_PATH_MAX];
+      char qualified[_POSIX_PATH_MAX];
+      int len;
+
+      if (lstat (in, &s) == -1)
+	return errno;
+
+      if (!S_ISLNK (s.st_mode))
+	{
+	  mu_cpystr (path, in, sizeof (path));
+	  break;
+	}
+
+      if ((len = readlink (in, link, sizeof (link))) == -1)
+	return errno;
+
+      link[(len >= sizeof (link)) ? (sizeof (link) - 1) : len] = '\0';
+
+      mu_qualify_link (in, link, qualified);
+
+      mu_cpystr (path, qualified, sizeof (path));
+
+      in = path;
+    }
+
+  mu_cpystr (out, path, outsz);
+
+  return 0;
+}
+
+/* Expand a PATTERN to the pathname. PATTERN may contain the following
+   macro-notations:
+   ---------+------------ 
+   notation |  expands to
+   ---------+------------
+   %u         user name
+   %h         user's home dir
+   ~          Likewise
+   ---------+------------
+
+   Allocates memory. 
+*/
+char *
+mu_expand_path_pattern (const char *pattern, const char *username)
+{
+  const char *p;
+  char *q;
+  char *path;
+  size_t len = 0;
+  struct mu_auth_data *auth = NULL;
+  
+  for (p = pattern; *p; p++)
+    {
+      if (*p == '~')
+        {
+          if (!auth)
+            {
+              auth = mu_get_auth_by_name (username);
+              if (!auth)
+                return NULL;
+            }
+          len += strlen (auth->dir);
+        }
+      else if (*p == '%')
+	switch (*++p)
+	  {
+	  case 'u':
+	    len += strlen (username);
+	    break;
+	    
+	  case 'h':
+	    if (!auth)
+	      {
+		auth = mu_get_auth_by_name (username);
+		if (!auth)
+		  return NULL;
+	      }
+	    len += strlen (auth->dir);
+	    break;
+	    
+	  case '%':
+	    len++;
+	    break;
+	    
+	  default:
+	    len += 2;
+	  }
+      else
+	len++;
+    }
+  
+  path = malloc (len + 1);
+  if (!path)
+    return NULL;
+
+  p = pattern;
+  q = path;
+  while (*p)
+    {
+      size_t off = strcspn (p, "~%");
+      memcpy (q, p, off);
+      q += off;
+      p += off;
+      if (*p == '~')
+	{
+	  strcpy (q, auth->dir);
+	  q += strlen (auth->dir);
+	  p++;
+	}
+      else if (*p == '%')
+	{
+	  switch (*++p)
+	    {
+	    case 'u':
+	      strcpy (q, username);
+	      q += strlen (username);
+	      break;
+	    
+	    case 'h':
+	      strcpy (q, auth->dir);
+	      q += strlen (auth->dir);
+	      break;
+	  
+	    case '%':
+	      *q++ = '%';
+	      break;
+	  
+	    default:
+	      *q++ = '%';
+	      *q++ = *p;
+	    }
+	  p++;
+	}
+    }
+
+  *q = 0;
+  if (auth)
+    mu_auth_data_free (auth);
+  return path;
+}
+
+#define ST_INIT  0
+#define ST_MSGID 1
+
+static int
+strip_message_id (char *msgid, char **pval)
+{
+  char *p, *q;
+  int state;
+  
+  *pval = strdup (msgid);
+  if (!*pval)
+    return ENOMEM;
+  state = ST_INIT;
+  for (p = q = *pval; *p; p++)
+    {
+      switch (state)
+	{
+	case ST_INIT:
+	  if (*p == '<')
+	    {
+	      *q++ = *p;
+	      state = ST_MSGID;
+	    }
+	  else if (isspace (*p))
+	    *q++ = *p;
+	  break;
+
+	case ST_MSGID:
+	  *q++ = *p;
+	  if (*p == '>')
+	    state = ST_INIT;
+	  break;
+	}
+    }
+  *q = 0;
+  return 0;
+}
+
+int
+get_msgid_header (mu_header_t hdr, const char *name, char **val)
+{
+  char *p;
+  int status = mu_header_aget_value (hdr, name, &p);
+  if (status)
+    return status;
+  status = strip_message_id (p, val);
+  free (p);
+  return status;
+}
+
+static char *
+concat (const char *s1, const char *s2)
+{
+  int len = (s1 ? strlen (s1) : 0) + (s2 ? strlen (s2) : 0) + 2;
+  char *s = malloc (len);
+  if (s)
+    {
+      char *p = s;
+      
+      if (s1)
+	{
+	  strcpy (p, s1);
+	  p += strlen (s1);
+	  *p++ = ' ';
+	}
+      if (s2)
+	strcpy (p, s2);
+    }
+  return s;
+}
+
+/* rfc2822:
+   
+   The "References:" field will contain the contents of the parent's
+   "References:" field (if any) followed by the contents of the parent's
+   "Message-ID:" field (if any).  If the parent message does not contain
+   a "References:" field but does have an "In-Reply-To:" field
+   containing a single message identifier, then the "References:" field
+   will contain the contents of the parent's "In-Reply-To:" field
+   followed by the contents of the parent's "Message-ID:" field (if
+   any).  If the parent has none of the "References:", "In-Reply-To:",
+   or "Message-ID:" fields, then the new message will have no
+   References:" field. */
+
+int
+mu_rfc2822_references (mu_message_t msg, char **pstr)
+{
+  char *ref = NULL, *msgid = NULL;
+  mu_header_t hdr;
+  int rc;
+  
+  rc = mu_message_get_header (msg, &hdr);
+  if (rc)
+    return rc;
+  get_msgid_header (hdr, MU_HEADER_MESSAGE_ID, &msgid);
+  if (get_msgid_header (hdr, MU_HEADER_REFERENCES, &ref))
+    get_msgid_header (hdr, MU_HEADER_IN_REPLY_TO, &ref);
+
+  if (ref || msgid)
+    {
+      *pstr = concat (ref, msgid);
+      free (ref);
+      free (msgid);
+      return 0;
+    }
+  return MU_ERR_FAILURE;
+}
+
+int
+mu_rfc2822_msg_id (int subpart, char **pval)
+{
+  char date[4+2+2+2+2+2+1];
+  time_t t = time (NULL);
+  struct tm *tm = localtime (&t);
+  char *host;
+  char *p;
+	  
+  mu_strftime (date, sizeof date, "%Y%m%d%H%M%S", tm);
+  mu_get_host_name (&host);
+
+  if (subpart)
+    {
+      struct timeval tv;
+      gettimeofday (&tv, NULL);
+      mu_asprintf (&p, "<%s.%lu.%d@%s>",
+		   date,
+		   (unsigned long) getpid (),
+		   subpart,
+		   host);
+    }
+  else
+    mu_asprintf (&p, "<%s.%lu@%s>", date, (unsigned long) getpid (), host);
+  free (host);
+  *pval = p;
+  return 0;
+}
+
+#define DATEBUFSIZE 128
+#define COMMENT "Your message of "
+
+/*
+   The "In-Reply-To:" field will contain the contents of the "Message-
+   ID:" field of the message to which this one is a reply (the "parent
+   message").  If there is more than one parent message, then the "In-
+   Reply-To:" field will contain the contents of all of the parents'
+   "Message-ID:" fields.  If there is no "Message-ID:" field in any of
+   the parent messages, then the new message will have no "In-Reply-To:"
+   field.
+*/
+int
+mu_rfc2822_in_reply_to (mu_message_t msg, char **pstr)
+{
+  const char *value = NULL;
+  char *s1 = NULL, *s2 = NULL;
+  mu_header_t hdr;
+  int rc;
+  
+  rc = mu_message_get_header (msg, &hdr);
+  if (rc)
+    return rc;
+  
+  if (mu_header_sget_value (hdr, MU_HEADER_DATE, &value))
+    {
+      mu_envelope_t envelope = NULL;
+      mu_message_get_envelope (msg, &envelope);
+      mu_envelope_sget_date (envelope, &value);
+    }
+
+  if (value)
+    {
+      s1 = malloc (sizeof (COMMENT) + strlen (value));
+      if (!s1)
+	return ENOMEM;
+      strcat (strcpy (s1, COMMENT), value);
+    }
+  
+  if (mu_header_sget_value (hdr, MU_HEADER_MESSAGE_ID, &value) == 0)
+    {
+      s2 = malloc (strlen (value) + 3);
+      if (!s2)
+	{
+	  free (s1);
+	  return ENOMEM;
+	}
+      strcat (strcpy (s2, "\n\t"), value);
+    }
+
+  if (s1 || s2)
+    {
+      *pstr = concat (s1, s2);
+      free (s1);
+      free (s2);
+      return 0;
+    }
+  return MU_ERR_FAILURE;
+}
+
+/* Based on strstr from GNU libc (Stephen R. van den Berg,
+   berg@pool.informatik.rwth-aachen.de) */
+
+char *
+mu_strcasestr (const char *a_haystack, const char *a_needle)
+{
+  register const unsigned char *haystack = (unsigned char*) a_haystack,
+    *needle = (unsigned char*) a_needle;
+  register unsigned int b, c;
+
+#define U(c) mu_toupper (c)
+  if ((b = U (*needle)))
+    {
+      haystack--;		
+      do
+	{
+	  if (!(c = *++haystack))
+	    goto ret0;
+	}
+      while (U (c) != b);
+
+      if (!(c = *++needle))
+	goto foundneedle;
+
+      c = U (c);
+      ++needle;
+      goto jin;
+
+      for (;;)
+        { 
+          register unsigned int a;
+	  register const unsigned char *rhaystack, *rneedle;
+
+	  do
+	    {
+	      if (!(a = *++haystack))
+		goto ret0;
+	      if (U (a) == b)
+		break;
+	      if (!(a = *++haystack))
+		goto ret0;
+shloop:       ;
+            }
+          while (U (a) != b);
+	  
+jin:     if (!(a = *++haystack))
+	    goto ret0;
+
+	  if (U (a) != c)
+	    goto shloop;
+
+	  if (U (*(rhaystack = haystack-- + 1)) ==
+	      (a = U (*(rneedle = needle))))
+	    do
+	      {
+		if (!a)
+		  goto foundneedle;
+		if (U (*++rhaystack) != (a = U (*++needle)))
+		  break;
+		if (!a)
+		  goto foundneedle;
+	      }
+	    while (U (*++rhaystack) == (a = U (*++needle)));
+
+	  needle = rneedle;
+
+	  if (!a)
+	    break;
+        }
+    }
+foundneedle:
+  return (char*)haystack;
+ret0:
+  return NULL;
+
+#undef U
+}  
+
+int
+mu_string_unfold (char *text, size_t *plen)
+{
+  char *p, *q;
+  enum uf_state { uf_init, uf_nl, uf_fold } state = uf_init;
+#define ISSPACE(c) (c == '\r' || c == ' ' || c == '\t')
+  
+  if (!text)
+    return EINVAL;
+  
+  for (p = q = text; *q; q++)
+    {
+      switch (state)
+	{
+	case uf_init:
+	  if (*q == '\n')
+	    state = uf_nl;
+	  else
+	    *p++ = *q;
+	  break;
+
+	case uf_nl:
+	  if (ISSPACE (*q))
+	    state = uf_fold;
+	  else
+	    {
+	      state = uf_init;
+	      *p++ = *q;
+	    }
+	  break;
+
+	case uf_fold:
+	  if (!ISSPACE (*q))
+	    {
+	      *p++ = ' ';
+	      *p++ = *q;
+	      state = uf_init;
+	    }
+	  break;
+	}
+    }
+  
+  *p++ = 0;
+  if (plen)
+    *plen = p - text;
+  return 0;
+}
+
+int
+mu_true_answer_p (const char *p)
+{
+  if (!p)
+    return -1;
+
+  while (*p && isspace (*p))
+    p++;
+
+  if (*p)
+    {
+      /* TRANSLATORS: This is a list of characters which start
+	 an affirmative answer. Whenever possible, please preserve
+	 'yY' in your translation, e.g., for Euskara:
+
+	 msgstr "yYbB";
+      */
+      if (strchr (_("yY"), *p))
+	return 1;
+
+      /* TRANSLATORS: This is a list of characters which start
+	 a negative answer. Whenever possible, please preserve
+	 'nN' in your translation, e.g., for Euskara:
+
+	 msgstr "nNeE";
+      */
+      else if (strchr (_("nN"), *p))
+	return 0;
+    }
+  return -1;
+}
+
+/* Returns true if SCHEME represents a local (autodetect) mail folder.  */
+int
+mu_scheme_autodetect_p (mu_url_t url)
+{
+  if (mu_url_is_scheme (url, "file"))
+    {
+      mu_url_expand_path (url);
+      return 1;
+    }
+  return 0;
+}
+    
+int
+mu_fd_wait (int fd, int *pflags, struct timeval *tvp)
+{
+  fd_set rdset, wrset, exset;
+  int rc;
+
+  FD_ZERO (&rdset);
+  FD_ZERO (&wrset);
+  FD_ZERO (&exset);
+  if ((*pflags) & MU_STREAM_READY_RD)
+    FD_SET (fd, &rdset);
+  if ((*pflags) & MU_STREAM_READY_WR)
+    FD_SET (fd, &wrset);
+  if ((*pflags) & MU_STREAM_READY_EX)
+    FD_SET (fd, &exset);
+  
+  do
+    {
+      if (tvp)
+	{
+	  struct timeval tv = *tvp; 
+	  rc = select (fd + 1, &rdset, &wrset, &exset, &tv);
+	}
+      else
+	rc = select (fd + 1, &rdset, &wrset, &exset, NULL);
+    }
+  while (rc == -1 && errno == EINTR);
+
+  if (rc < 0)
+    return errno;
+  
+  *pflags = 0;
+  if (rc > 0)
+    {
+      if (FD_ISSET (fd, &rdset))
+	*pflags |= MU_STREAM_READY_RD;
+      if (FD_ISSET (fd, &wrset))
+	*pflags |= MU_STREAM_READY_WR;
+      if (FD_ISSET (fd, &exset))
+	*pflags |= MU_STREAM_READY_EX;
+    }
+  return 0;
+}
+
+enum mu_iconv_fallback_mode mu_default_fallback_mode = mu_fallback_copy_octal;
+
+int
+mu_set_default_fallback (const char *str)
+{
+  if (strcmp (str, "none") == 0)
+    mu_default_fallback_mode = mu_fallback_none;
+  else if (strcmp (str, "copy-pass") == 0)
+    mu_default_fallback_mode = mu_fallback_copy_pass;
+  else if (strcmp (str, "copy-octal") == 0)
+    mu_default_fallback_mode = mu_fallback_copy_octal;
+  else
+    return EINVAL;
+  return 0;
+}
+
+int
+mu_decode_filter (mu_stream_t *pfilter, mu_stream_t input,
+		  const char *filter_type,
+		  const char *fromcode, const char *tocode)
+{
+  mu_stream_t filter;
+  
+  int status = mu_filter_create (&filter, input, filter_type,
+				 MU_FILTER_DECODE, MU_STREAM_READ);
+  if (status)
+    return status;
+
+  if (fromcode && tocode && mu_c_strcasecmp (fromcode, tocode))
+    {
+      mu_stream_t cvt;
+
+      status = mu_filter_iconv_create (&cvt, filter, fromcode, tocode,
+				       0, mu_default_fallback_mode);
+      if (status == 0)
+	{
+          mu_stream_unref (filter);
+          filter = cvt;
+	}
+    }
+  *pfilter = filter;
+  return 0;
+}
+
+int
+mu_is_proto (const char *p)
+{
+  if (*p == '|')
+    return 1;
+  for (; *p && *p != '/'; p++)
+    if (*p == ':')
+      return 1;
+  return 0;
+}
+
+int
+mu_mh_delim (const char *str)
+{
+  if (str[0] == '-')
+    {
+      for (; *str == '-'; str++)
+	;
+      for (; *str == ' ' || *str == '\t'; str++)
+	;
+    }
+  return str[0] == '\n';
+}
+
+char *
+__argp_base_name (const char *arg)
+{
+  char *p = strrchr (arg, '/');
+  return (char *)(p ? p + 1 : arg);
+}
+
+/* A locale-independent version of strftime */
+size_t
+mu_strftime (char *s, size_t max, const char *format, const struct tm *tm)
+{
+  size_t size;
+  mu_set_locale ("C");
+  size = strftime(s, max, format, tm);
+  mu_restore_locale ();
+  return size;
+}
+  
+
+static void
+assoc_str_free (void *data)
+{
+  free (data);
+}
+
+int
+mutil_parse_field_map (const char *map, mu_assoc_t *passoc_tab, int *perr)
+{
+  int rc;
+  int i;
+  int argc;
+  char **argv;
+  mu_assoc_t assoc_tab = NULL;
+
+  rc = mu_argcv_get (map, ":", NULL, &argc, &argv);
+  if (rc)
+    {
+      mu_error (_("cannot split line `%s': %s"), map, mu_strerror (rc));
+      return rc;
+    }
+
+  for (i = 0; i < argc; i += 2)
+    {
+      char *tok = argv[i];
+      char *p = strchr (tok, '=');
+      char *pptr;
+      
+      if (!p)
+	{
+	  rc = EINVAL;
+	  break;
+	}
+      if (!assoc_tab)
+	{
+	  rc = mu_assoc_create (&assoc_tab, sizeof(char*), 0);
+	  if (rc)
+	    break;
+	  mu_assoc_set_free (assoc_tab, assoc_str_free);
+	  *passoc_tab = assoc_tab;
+	}
+      *p++ = 0;
+      pptr = strdup (p);
+      if (!pptr)
+	{
+	  rc = errno;
+	  break;
+	}
+      rc = mu_assoc_install (assoc_tab, tok, &pptr);
+      if (rc)
+	{
+	  free (p);
+	  break;
+	}
+    }
+
+  mu_argcv_free (argc, argv);
+  if (rc && perr)
+    *perr = i;
+  return rc;
+}
+
+/* FIXME: should it be here? */
+int
+mu_sql_decode_password_type (const char *arg, enum mu_password_type *t)
+{
+  if (strcmp (arg, "plain") == 0)
+    *t = password_plaintext;
+  else if (strcmp (arg, "hash") == 0)
+    *t = password_hash;
+  else if (strcmp (arg, "scrambled") == 0)
+    *t = password_scrambled;
+  else
+    return 1;
+  return 0;
+}
+
+int
+mu_stream_flags_to_mode (int flags, int isdir)
+{
+  int mode = 0;
+  if (flags & MU_STREAM_IRGRP)
+    mode |= S_IRGRP;
+  if (flags & MU_STREAM_IWGRP)
+    mode |= S_IWGRP;
+  if (flags & MU_STREAM_IROTH)
+    mode |= S_IROTH;
+  if (flags & MU_STREAM_IWOTH)
+    mode |= S_IWOTH;
+
+  if (isdir)
+    {
+      if (mode & (S_IRGRP|S_IWGRP))
+	mode |= S_IXGRP;
+      if (mode & (S_IROTH|S_IWOTH))
+	mode |= S_IXOTH;
+    }
+  
+  return mode;
+}
diff --git a/libmailutils/base/nls.c b/libmailutils/base/nls.c
new file mode 100644
index 0000000..badda2f
--- /dev/null
+++ b/libmailutils/base/nls.c
@@ -0,0 +1,61 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 2002, 2006, 2007, 2008, 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, see
+   <http://www.gnu.org/licenses/>. */
+
+#if HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <mailutils/nls.h>
+#include <locale.h>
+
+/* Initialize Native Language Support */
+
+char *mu_locale_set;
+
+#ifndef HAVE_SETLOCALE
+# define setlocale(c,s) NULL
+#endif
+
+/* Set locale via LC_ALL.  */
+char *
+mu_set_locale (const char *locale)
+{
+#if defined HAVE_SETLOCALE
+  return setlocale (LC_ALL, locale);
+#else
+  return NULL;
+#endif
+}
+
+void
+mu_restore_locale (void)
+{
+  if (mu_locale_set)
+    mu_set_locale (mu_locale_set);
+}
+
+void
+mu_init_nls (void)
+{
+#ifdef ENABLE_NLS
+  mu_locale_set = mu_set_locale ("");
+  bindtextdomain (PACKAGE, LOCALEDIR);
+#endif /* ENABLE_NLS */
+}
+
diff --git a/libmailutils/nullrec.c b/libmailutils/base/nullrec.c
similarity index 100%
rename from libmailutils/nullrec.c
rename to libmailutils/base/nullrec.c
diff --git a/libmailutils/base/observer.c b/libmailutils/base/observer.c
new file mode 100644
index 0000000..81140c2
--- /dev/null
+++ b/libmailutils/base/observer.c
@@ -0,0 +1,238 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2004, 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, see
+   <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <errno.h>
+#include <stdlib.h>
+
+#include <mailutils/list.h>
+#include <mailutils/iterator.h>
+#include <mailutils/errno.h>
+#include <mailutils/sys/observer.h>
+
+int
+mu_observer_create (mu_observer_t *pobserver, void *owner)
+{
+  mu_observer_t observer;
+  observer = calloc (sizeof (*observer), 1);
+  if (observer == NULL)
+    return ENOMEM;
+  observer->owner = owner;
+  *pobserver = observer;
+  return 0;
+}
+
+void
+mu_observer_destroy (mu_observer_t *pobserver, void *owner)
+{
+  if (pobserver && *pobserver)
+    {
+      mu_observer_t observer = *pobserver;
+      if (observer->owner == owner || observer->flags & MU_OBSERVER_NO_CHECK)
+	{
+	  if (observer->_destroy)
+	    observer->_destroy (observer, observer->_action_data);
+	  free (observer);
+	}
+      *pobserver = NULL;
+    }
+}
+
+void *
+mu_observer_get_owner (mu_observer_t observer)
+{
+  return (observer) ? observer->owner : NULL;
+}
+
+int
+mu_observer_action (mu_observer_t observer, size_t type, void *data)
+{
+  if (observer == NULL)
+    return EINVAL;
+  if (observer->_action)
+    return observer->_action (observer, type, data, observer->_action_data);
+  return 0;
+}
+
+int
+mu_observer_set_action (mu_observer_t observer,
+			int (*_action) (mu_observer_t, size_t, void *, void *),
+			void *owner)
+{
+  if (observer == NULL)
+    return EINVAL;
+  if (observer->owner != owner)
+    return EACCES;
+  observer->_action = _action;
+  return 0;
+}
+
+int
+mu_observer_set_action_data  (mu_observer_t observer, void *data, void *owner)
+{
+  if (observer == NULL)
+    return EINVAL;
+  if (observer->owner != owner)
+    return EACCES;
+  observer->_action_data = data;
+  return 0;
+}
+
+int
+mu_observer_set_destroy (mu_observer_t observer,
+			 int (*_destroy) (mu_observer_t, void *),
+			 void *owner)
+{
+  if (observer == NULL)
+    return EINVAL;
+  if (observer->owner != owner)
+    return EACCES;
+  observer->_destroy = _destroy;
+  return 0;
+}
+
+int
+mu_observer_set_flags (mu_observer_t observer, int flags)
+{
+  if (observer == NULL)
+    return EINVAL;
+  observer->flags |= flags;
+  return 0;
+}
+
+static void
+_free_event (void *ptr)
+{
+  event_t event = ptr;
+  mu_observer_destroy (&event->observer, NULL);
+  free (event);
+}
+
+int
+mu_observable_create (mu_observable_t *pobservable, void *owner)
+{
+  mu_observable_t observable;
+  int status;
+  if (pobservable == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+  observable = calloc (sizeof (*observable), 1);
+  if (observable == NULL)
+    return ENOMEM;
+  status = mu_list_create (&observable->list);
+  if (status != 0 )
+    {
+      free (observable);
+      return status;
+    }
+  mu_list_set_destroy_item (observable->list, _free_event);
+  observable->owner = owner;
+  *pobservable = observable;
+  return 0;
+}
+
+void
+mu_observable_destroy (mu_observable_t *pobservable, void *owner)
+{
+  if (pobservable && *pobservable)
+    {
+      mu_observable_t observable = *pobservable;
+      if (observable->owner == owner)
+	{
+	  mu_list_destroy (&observable->list);
+	  free (observable);
+	}
+      *pobservable = NULL;
+    }
+}
+
+void *
+mu_observable_get_owner (mu_observable_t observable)
+{
+  return (observable) ? observable->owner : NULL;
+}
+
+int
+mu_observable_attach (mu_observable_t observable, size_t type,  mu_observer_t observer)
+{
+  event_t event;
+  if (observable == NULL || observer == NULL)
+    return EINVAL;
+  event = calloc (1, sizeof (*event));
+  if (event == NULL)
+    return ENOMEM;
+  event->type = type;
+  event->observer = observer;
+  return mu_list_append (observable->list, event);
+}
+
+int
+mu_observable_detach (mu_observable_t observable, mu_observer_t observer)
+{
+  mu_iterator_t iterator;
+  int status;
+  event_t event = NULL;
+
+  if (observable == NULL || observer == NULL)
+    return EINVAL;
+  status = mu_list_get_iterator (observable->list, &iterator);
+  if (status != 0)
+    return status;
+  status = MU_ERR_NOENT;
+  for (mu_iterator_first (iterator); !mu_iterator_is_done (iterator);
+       mu_iterator_next (iterator))
+    {
+      event = NULL;
+      mu_iterator_current (iterator, (void **)&event);
+      if (event && event->observer == observer)
+        {
+          mu_iterator_ctl (iterator, mu_itrctl_delete, NULL);
+	  status = 0;
+          break;
+        }
+    }
+  mu_iterator_destroy (&iterator);
+  return status;
+}
+
+int
+mu_observable_notify (mu_observable_t observable, int type, void *data)
+{
+  mu_iterator_t iterator;
+  event_t event = NULL;
+  int status = 0;
+  if (observable == NULL)
+    return EINVAL;
+  status = mu_list_get_iterator (observable->list, &iterator);
+  if (status != 0)
+    return status;
+  for (mu_iterator_first (iterator); !mu_iterator_is_done (iterator);
+       mu_iterator_next (iterator))
+    {
+      event = NULL;
+      mu_iterator_current (iterator, (void **)&event);
+      if (event && event->type & type)
+        {
+	  status |= mu_observer_action (event->observer, type, data);
+        }
+    }
+  mu_iterator_destroy (&iterator);
+  return status;
+}
diff --git a/libmailutils/base/opool.c b/libmailutils/base/opool.c
new file mode 100644
index 0000000..9cb6b0c
--- /dev/null
+++ b/libmailutils/base/opool.c
@@ -0,0 +1,446 @@
+/* String-list functions for GNU Mailutils.
+   Copyright (C) 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
+
+   Based on slist module from GNU Radius.  Written by Sergey Poznyakoff.
+   
+   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.
+
+   GNU Mailutils 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 GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <mailutils/types.h>
+#include <mailutils/alloc.h>
+#include <mailutils/opool.h>
+#include <mailutils/errno.h>
+#include <mailutils/error.h>
+#include <mailutils/nls.h>
+#include <mailutils/iterator.h>
+
+struct mu_opool_bucket
+{
+  struct mu_opool_bucket *next;
+  char *buf;
+  size_t level;
+  size_t size;
+};
+
+struct _mu_opool
+{
+  int memerr;
+  size_t bucket_size;
+  size_t itr_count;
+  struct mu_opool_bucket *head, *tail;
+  struct mu_opool_bucket *free;
+};
+
+static struct mu_opool_bucket *
+alloc_bucket (struct _mu_opool *opool, size_t size)
+{
+  struct mu_opool_bucket *p = malloc (sizeof (*p) + size);
+  if (!p)
+    {
+      if (opool->memerr)
+	mu_alloc_die ();
+    }
+  else
+    {
+      p->buf = (char*)(p + 1);
+      p->level = 0;
+      p->size = size;
+      p->next = NULL;
+    }
+  return p;
+}
+
+static int
+alloc_pool (mu_opool_t opool, size_t size)
+{
+  struct mu_opool_bucket *p = alloc_bucket (opool, opool->bucket_size);
+  if (!p)
+    return ENOMEM;
+  if (opool->tail)
+    opool->tail->next = p;
+  else
+    opool->head = p;
+  opool->tail = p;
+  return 0;
+}
+
+static int
+copy_chars (mu_opool_t opool, const char *str, size_t n, size_t *psize)
+{
+  size_t rest;
+
+  if (!opool->head || opool->tail->level == opool->tail->size)
+    if (alloc_pool (opool, opool->bucket_size))
+      return ENOMEM;
+  rest = opool->tail->size - opool->tail->level;
+  if (n > rest)
+    n = rest;
+  memcpy (opool->tail->buf + opool->tail->level, str, n);
+  opool->tail->level += n;
+  *psize = n;
+  return 0;
+}
+
+int
+mu_opool_create (mu_opool_t *pret, int memerr)
+{
+  struct _mu_opool *x = malloc (sizeof (x[0]));
+  if (!x)
+    {
+      if (memerr)
+	mu_alloc_die ();
+      return ENOMEM;
+    }
+  x->memerr = memerr;
+  x->bucket_size = MU_OPOOL_BUCKET_SIZE;
+  x->itr_count = 0;
+  x->head = x->tail = x->free = 0;
+  *pret = x;
+  return 0;
+}
+
+int
+mu_opool_set_bucket_size (mu_opool_t opool, size_t size)
+{
+  if (!opool)
+    return EINVAL;
+  opool->bucket_size = size;
+  return 0;
+}
+
+int
+mu_opool_get_bucket_size (mu_opool_t opool, size_t *psize)
+{
+  if (!opool || !psize)
+    return EINVAL;
+  *psize = opool->bucket_size;
+  return 0;
+}
+
+void
+mu_opool_clear (mu_opool_t opool)
+{
+  if (!opool)
+    return;
+  
+  if (opool->tail)
+    {
+      opool->tail->next = opool->free;
+      opool->free = opool->head;
+      opool->head = opool->tail = NULL;
+    }
+}	
+
+void
+mu_opool_destroy (mu_opool_t *popool)
+{
+  struct mu_opool_bucket *p;
+  if (popool && *popool)
+    {
+      mu_opool_t opool = *popool;
+      mu_opool_clear (opool);
+      for (p = opool->free; p; )
+	{
+	  struct mu_opool_bucket *next = p->next;
+	  free (p);
+	  p = next;
+	}
+      free (opool);
+    }
+  *popool = NULL;
+}
+
+int
+mu_opool_append (mu_opool_t opool, const void *str, size_t n)
+{
+  const char *ptr = str;
+  while (n)
+    {
+      size_t s;
+      if (copy_chars (opool, ptr, n, &s))
+	return ENOMEM;
+      ptr += s;
+      n -= s;
+    }
+  return 0;
+}
+
+int
+mu_opool_append_char (mu_opool_t opool, char c)
+{
+  return mu_opool_append (opool, &c, 1);
+}	
+
+int
+mu_opool_appendz (mu_opool_t opool, const char *str)
+{
+  return mu_opool_append (opool, str, strlen (str))
+         || mu_opool_append_char (opool, 0);
+}
+
+size_t
+mu_opool_size (mu_opool_t opool)
+{
+  size_t size = 0;
+  struct mu_opool_bucket *p;
+  for (p = opool->head; p; p = p->next)
+    size += p->level;
+  return size;
+}
+
+size_t
+mu_opool_copy (mu_opool_t opool, void *buf, size_t size)
+{
+  char *cp = buf;
+  size_t total = 0;
+  struct mu_opool_bucket *p;
+  
+  for (p = opool->head; p && total < size; p = p->next)
+    {
+      size_t cpsize = size - total;
+      if (cpsize > p->level)
+	cpsize = p->level;
+      memcpy (cp, p->buf, cpsize);
+      cp += cpsize;
+      total += cpsize;
+    }
+  return total;
+}
+
+int
+mu_opool_coalesce (mu_opool_t opool, size_t *psize)
+{
+  size_t size;
+
+  if (opool->itr_count)
+    return MU_ERR_FAILURE;
+  if (opool->head && opool->head->next == NULL)
+    size = opool->head->level;
+  else {
+    struct mu_opool_bucket *bucket;
+    struct mu_opool_bucket *p;
+
+    size = mu_opool_size (opool);
+	
+    bucket = alloc_bucket (opool, size);
+    if (!bucket)
+      return ENOMEM;
+    for (p = opool->head; p; )
+      {
+	struct mu_opool_bucket *next = p->next;
+	memcpy (bucket->buf + bucket->level, p->buf, p->level);
+	bucket->level += p->level;
+	free (p);
+	p = next;
+      }
+    opool->head = opool->tail = bucket;
+  }
+  if (psize)
+    *psize = size;
+  return 0;
+}
+
+void *
+mu_opool_head (mu_opool_t opool, size_t *psize)
+{
+  if (*psize) 
+    *psize = opool->head ? opool->head->level : 0;
+  return opool->head ? opool->head->buf : NULL;
+}
+
+void *
+mu_opool_finish (mu_opool_t opool, size_t *psize)
+{
+  if (mu_opool_coalesce (opool, psize))
+    return NULL;
+  mu_opool_clear (opool);
+  return opool->free->buf;
+}
+
+int
+mu_opool_union (mu_opool_t *pdst, mu_opool_t *psrc)
+{
+  mu_opool_t src, dst;
+  
+  if (!psrc)
+    return EINVAL;
+  if (!*psrc)
+    return 0;
+  src = *psrc;
+  
+  if (!pdst)
+    return EINVAL;
+  if (!*pdst)
+    {
+      *pdst = src;
+      *psrc = NULL;
+      return 0;
+    }
+  else
+    dst = *pdst;
+
+  if (dst->tail)
+    dst->tail->next = src->head;
+  else
+    dst->head = src->head;
+  dst->tail = src->tail;
+
+  if (src->free)
+    {
+      struct mu_opool_bucket *p;
+
+      for (p = src->free; p->next; p = p->next)
+	;
+      p->next = dst->free;
+      dst->free = src->free;
+    }
+
+  free (src);
+  *psrc = NULL;
+  return 0;
+}
+
+
+/* Iterator support */
+struct opool_iterator
+{
+  mu_opool_t opool;
+  struct mu_opool_bucket *cur;
+};
+
+static int
+opitr_first (void *owner)
+{
+  struct opool_iterator *itr = owner;
+  itr->cur = itr->opool->head;
+  return 0;
+}
+
+static int
+opitr_next (void *owner)
+{
+  struct opool_iterator *itr = owner;
+  if (itr->cur)
+    {
+      itr->cur = itr->cur->next;
+      return 0;
+    }
+  return EINVAL;
+}
+
+static int
+opitr_getitem (void *owner, void **pret, const void **pkey)
+{
+  struct opool_iterator *itr = owner;
+  if (!itr->cur)
+    return MU_ERR_NOENT;
+
+  *pret = itr->cur->buf;
+  if (pkey)
+    *(size_t*) pkey = itr->cur->level;
+  return 0;
+}
+
+static int
+opitr_finished_p (void *owner)
+{
+  struct opool_iterator *itr = owner;
+  return itr->cur == NULL;
+}
+
+static int
+opitr_curitem_p (void *owner, void *item)
+{
+  struct opool_iterator *itr = owner;
+  return itr->cur && itr->cur->buf == item;
+}
+
+static int
+opitr_destroy (mu_iterator_t iterator, void *data)
+{
+  struct opool_iterator *itr = data;
+  if (itr->opool->itr_count == 0)
+    {
+      /* oops! */
+      mu_error (_("%s: INTERNAL ERROR: zero reference count"),
+		"opool_destroy");
+    }
+  else
+    itr->opool->itr_count--;
+  free (data);
+  return 0;
+}
+
+static int
+opitr_data_dup (void **ptr, void *owner)
+{
+  struct opool_iterator *itr = owner;
+
+  *ptr = malloc (sizeof (struct opool_iterator));
+  if (*ptr == NULL)
+    return ENOMEM;
+  memcpy (*ptr, owner, sizeof (struct opool_iterator));
+  itr->opool->itr_count++;
+  return 0;
+}
+
+int
+mu_opool_get_iterator (mu_opool_t opool, mu_iterator_t *piterator)
+{
+  mu_iterator_t iterator;
+  int status;
+  struct opool_iterator *itr;
+
+  if (!opool)
+    return EINVAL;
+
+  itr = calloc (1, sizeof *itr);
+  if (!itr)
+    return ENOMEM;
+  itr->opool = opool;
+  itr->cur = opool->head;
+  
+  status = mu_iterator_create (&iterator, itr);
+  if (status)
+    {
+      free (itr);
+      return status;
+    }
+
+  mu_iterator_set_first (iterator, opitr_first);
+  mu_iterator_set_next (iterator, opitr_next);
+  mu_iterator_set_getitem (iterator, opitr_getitem);
+  mu_iterator_set_finished_p (iterator, opitr_finished_p);
+  mu_iterator_set_curitem_p (iterator, opitr_curitem_p);
+  mu_iterator_set_destroy (iterator, opitr_destroy);
+  mu_iterator_set_dup (iterator, opitr_data_dup);
+
+  opool->itr_count++;
+
+  *piterator = iterator;
+  return 0;
+}
+
+
+
+
+  
+  
+ 
diff --git a/libmailutils/base/parsedate.y b/libmailutils/base/parsedate.y
new file mode 100644
index 0000000..98c932b
--- /dev/null
+++ b/libmailutils/base/parsedate.y
@@ -0,0 +1,1205 @@
+%{
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 2003, 2007, 2008, 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 2, or (at your option)
+   any later version.
+   
+   GNU Mailutils 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 GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
+  
+/* A heavily modified version of the well-known public domain getdate.y.
+   It was originally written by Steven M. Bellovin <smb@research.att.com>
+   while at the University of North Carolina at Chapel Hill.  Later tweaked
+   by a couple of people on Usenet.  Completely overhauled by Rich $alz
+   <rsalz@bbn.com> and Jim Berets <jberets@bbn.com> in August, 1990.
+   Rewritten using a proper union by Sergey Poznyakoff <gray@gnu.org> */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <stdio.h>
+#include <ctype.h>
+
+#if HAVE_STDLIB_H
+# include <stdlib.h> /* for `free'; used by Bison 1.27 */
+#endif
+
+#if defined (STDC_HEADERS) || (!defined (isascii) && !defined (HAVE_ISASCII))
+# define IN_CTYPE_DOMAIN(c) 1
+#else
+# define IN_CTYPE_DOMAIN(c) isascii(c)
+#endif
+
+#define ISSPACE(c) (IN_CTYPE_DOMAIN (c) && isspace (c))
+#define ISALPHA(c) (IN_CTYPE_DOMAIN (c) && isalpha (c))
+#define ISUPPER(c) (IN_CTYPE_DOMAIN (c) && isupper (c))
+#define ISDIGIT_LOCALE(c) (IN_CTYPE_DOMAIN (c) && isdigit (c))
+
+/* ISDIGIT differs from ISDIGIT_LOCALE, as follows:
+   - Its arg may be any int or unsigned int; it need not be an unsigned char.
+   - It's guaranteed to evaluate its argument exactly once.
+   - It's typically faster.
+   Posix 1003.2-1992 section 2.5.2.1 page 50 lines 1556-1558 says that
+   only '0' through '9' are digits.  Prefer ISDIGIT to ISDIGIT_LOCALE unless
+   it's important to use the locale's definition of `digit' even when the
+   host does not conform to Posix.  */
+#define ISDIGIT(c) ((unsigned) (c) - '0' <= 9)
+
+#if defined (STDC_HEADERS) || defined (USG)
+# include <string.h>
+#endif
+
+/* Some old versions of bison generate parsers that use bcopy.
+   That loses on systems that don't provide the function, so we have
+   to redefine it here.  */
+#if !defined (HAVE_BCOPY) && defined (HAVE_MEMCPY) && !defined (bcopy)
+# define bcopy(from, to, len) memcpy ((to), (from), (len))
+#endif
+
+static int yylex ();
+static int yyerror ();
+
+#define EPOCH		1970
+#define HOUR(x)		((x) * 60)
+
+#define MAX_BUFF_LEN    128   /* size of buffer to read the date into */
+
+/*
+**  An entry in the lexical lookup table.
+*/
+typedef struct _lex_tab {
+  const char	*name;
+  int		type;
+  int		value;
+} SYMBOL;
+
+
+/*
+**  Meridian:  am, pm, or 24-hour style.
+*/
+typedef enum meridian {
+  MERam,
+  MERpm,
+  MER24
+} MERIDIAN;
+
+#define PD_MASK_SECOND   00001
+#define PD_MASK_MINUTE   00002
+#define PD_MASK_HOUR     00004
+#define PD_MASK_DAY      00010
+#define PD_MASK_MONTH    00020
+#define PD_MASK_YEAR     00040
+#define PD_MASK_TZ       00100 
+#define PD_MASK_MERIDIAN 00200
+#define PD_MASK_ORDINAL  00400
+#define PD_MASK_NUMBER   01000
+
+#define PD_MASK_TIME PD_MASK_SECOND|PD_MASK_MINUTE|PD_MASK_HOUR
+#define PD_MASK_DATE PD_MASK_DAY|PD_MASK_MONTH|PD_MASK_YEAR
+#define PD_MASK_DOW PD_MASK_NUMBER
+ 
+#define MASK_IS_SET(f,m) (((f)&(m))==(m))
+#define MASK_TEST(f,m)   ((f)&(m)) 
+struct pd_date
+{
+  int mask;
+  int day;
+  int hour;
+  int minute;
+  int month;
+  int second;
+  int year;
+  int tz;
+  enum meridian meridian;
+  int number;
+  int ordinal;
+};
+
+#define DATE_INIT(date) memset(&(date), 0, sizeof(date))
+#define DATE_SET(date, memb, m, val, lim, onerror)                        \
+ do                                                                       \
+   {                                                                      \
+     int __x = val;                                                       \
+     if (((m) != PD_MASK_TZ && __x < 0) || (lim && __x > lim)) onerror;   \
+     date . memb = __x; date.mask |= m;                                   \
+   }                                                                      \
+ while (0)
+   
+#define __SET_SECOND(d,v,a)   DATE_SET(d,second,PD_MASK_SECOND,v,59,a)
+#define __SET_MINUTE(d,v,a)   DATE_SET(d,minute,PD_MASK_MINUTE,v,59,a)  
+#define __SET_HOUR(d,v,a)     DATE_SET(d,hour,PD_MASK_HOUR,v,23,a)
+#define __SET_DAY(d,v,a)      DATE_SET(d,day,PD_MASK_DAY,v,31,a)   
+#define __SET_MONTH(d,v,a)    DATE_SET(d,month,PD_MASK_MONTH,v,12,a)
+#define __SET_YEAR(d,v,a)     DATE_SET(d,year,PD_MASK_YEAR,v,0,a)  
+#define __SET_TZ(d,v,a)       DATE_SET(d,tz,PD_MASK_TZ,v,0,a)
+#define __SET_MERIDIAN(d,v,a) DATE_SET(d,meridian,PD_MASK_MERIDIAN,v,MER24,a)
+#define __SET_ORDINAL(d,v,a)  DATE_SET(d,ordinal,PD_MASK_ORDINAL,v,0,a)
+#define __SET_NUMBER(d,v,a)   DATE_SET(d,number,PD_MASK_NUMBER,v,0,a) 
+ 
+#define SET_SECOND(d,v)   __SET_SECOND(d,v,YYERROR)   
+#define SET_MINUTE(d,v)   __SET_MINUTE(d,v,YYERROR)   
+#define SET_HOUR(d,v)     __SET_HOUR(d,v,YYERROR)     
+#define SET_DAY(d,v)      __SET_DAY(d,v,YYERROR)      
+#define SET_MONTH(d,v)    __SET_MONTH(d,v,YYERROR)    
+#define SET_YEAR(d,v)     __SET_YEAR(d,v,YYERROR)     
+#define SET_TZ(d,v)       __SET_TZ(d,v,YYERROR)       
+#define SET_MERIDIAN(d,v) __SET_MERIDIAN(d,v,YYERROR) 
+#define SET_ORDINAL(d,v)  __SET_ORDINAL(d,v,YYERROR)  
+#define SET_NUMBER(d,v)   __SET_NUMBER(d,v,YYERROR)   
+
+int
+pd_date_union (struct pd_date *a, struct pd_date *b)
+{
+  int diff = (~a->mask) & b->mask;
+  if (!diff)
+    return 1;
+
+  a->mask |= diff;
+  
+  if (diff & PD_MASK_SECOND)
+    a->second = b->second;
+  
+  if (diff & PD_MASK_MINUTE)
+    a->minute = b->minute;
+
+  if (diff & PD_MASK_HOUR)
+    a->hour = b->hour;
+
+  if (diff & PD_MASK_DAY)
+    a->day = b->day;
+
+  if (diff & PD_MASK_MONTH)
+    a->month = b->month;
+
+  if (diff & PD_MASK_YEAR)
+    a->year = b->year;
+
+  if (diff & PD_MASK_TZ)
+    a->tz = b->tz;
+
+  if (diff & PD_MASK_MERIDIAN)
+    a->meridian = b->meridian;
+
+  if (diff & PD_MASK_ORDINAL)
+    a->ordinal = b->ordinal;
+
+  if (diff & PD_MASK_NUMBER)
+    a->number = b->number;
+
+  return 0;
+}
+
+struct pd_datespec
+{
+  struct pd_date date;
+  struct pd_date rel;
+};
+
+static struct pd_datespec pd;
+ 
+static const char	*yyinput;
+
+%}
+
+%union {
+  int number;
+  enum meridian meridian;
+  struct pd_date date;
+  struct pd_datespec datespec;
+}
+
+/*FIXME: do we need T_ID? */
+%token  T_AGO T_DST  T_ID
+
+%token <number> T_DAY T_DAY_UNIT T_DAYZONE T_HOUR_UNIT T_MINUTE_UNIT
+%token <number> T_MONTH T_MONTH_UNIT
+%token <number>	T_SEC_UNIT T_SNUMBER T_UNUMBER T_YEAR_UNIT T_ZONE
+%token <meridian> T_MERIDIAN
+
+%type <meridian> o_merid
+%type <date> item time zone date day rel relspec relunit
+%type <datespec> spec
+
+%%
+
+input   : spec
+          {
+	    pd = $1;
+	  }
+        ;
+
+spec	: /* NULL */
+          {
+	    DATE_INIT ($$.date);
+	    DATE_INIT ($$.rel);
+	  }
+	| spec item
+          {
+	    if (pd_date_union (&$1.date, &$2))
+	      YYERROR;
+	    $$ = $1;
+	  }
+        | spec rel
+          {
+	    if (pd_date_union (&$1.rel, &$2))
+	      YYERROR;
+	    $$ = $1;
+	  }
+        | spec T_UNUMBER
+          {
+	    if (MASK_IS_SET ($1.date.mask, (PD_MASK_TIME|PD_MASK_DATE))
+		&& !$1.rel.mask)
+	      SET_YEAR ($1.date, $2);
+	    else
+	      {
+		if ($2 > 10000)
+		  {
+		    SET_DAY ($1.date, $2 % 100);
+		    SET_MONTH ($1.date, ($2 / 100) %100);
+		    SET_YEAR ($1.date, $2 / 10000);
+		  }
+		else
+		  {
+		    if ($2 < 100)
+		      {
+			SET_YEAR ($1.date, $2);
+		      }
+		    else
+		      {
+		    	SET_HOUR ($1.date, $2 / 100);
+		    	SET_MINUTE ($1.date, $2 % 100);
+		      }
+		    SET_MERIDIAN ($1.date, MER24);
+		  }
+	      }
+	    $$ = $1;
+	  }
+	;
+
+item	: time
+        | zone
+	| date
+	| day
+	;
+
+time	: T_UNUMBER T_MERIDIAN
+          {
+	    DATE_INIT ($$);
+	    SET_HOUR ($$, $1);
+	    SET_MERIDIAN ($$, $2);
+	  }
+	| T_UNUMBER ':' T_UNUMBER o_merid
+          {
+	    DATE_INIT ($$);
+	    SET_HOUR ($$, $1);
+	    SET_MINUTE ($$, $3);
+	    SET_MERIDIAN ($$, $4);
+	  }
+	| T_UNUMBER ':' T_UNUMBER T_SNUMBER
+          {
+	    DATE_INIT ($$);
+	    SET_HOUR ($$, $1);
+	    SET_MINUTE ($$, $3);
+	    SET_MERIDIAN ($$, MER24);
+	    SET_TZ ($$, ($4 < 0
+			   ? -$4 % 100 + (-$4 / 100) * 60
+			   : - ($4 % 100 + ($4 / 100) * 60)));
+
+	  }
+	| T_UNUMBER ':' T_UNUMBER ':' T_UNUMBER o_merid
+          {
+	    DATE_INIT ($$);
+	    SET_HOUR ($$, $1);
+	    SET_MINUTE ($$, $3);
+	    SET_SECOND ($$, $5);
+	    SET_MERIDIAN ($$, $6);
+	  }
+	| T_UNUMBER ':' T_UNUMBER ':' T_UNUMBER T_SNUMBER
+          {
+	    DATE_INIT ($$);
+	    SET_HOUR ($$, $1);
+	    SET_MINUTE ($$, $3);
+	    SET_SECOND ($$, $5);
+	    SET_MERIDIAN ($$, MER24);
+	    SET_TZ ($$, ($6 < 0
+			 ? -$6 % 100 + (-$6 / 100) * 60
+			 : - ($6 % 100 + ($6 / 100) * 60)));
+	  }
+	;
+
+zone	: T_ZONE
+          {
+	    DATE_INIT ($$);
+	    SET_TZ ($$, $1);
+	  }
+	| T_DAYZONE
+          {
+	    DATE_INIT ($$);
+	    SET_TZ ($$, $1 - 60);
+	  }
+	| T_ZONE T_DST
+          {
+	    DATE_INIT ($$);
+	    SET_TZ ($$, $1 - 60);
+	  }
+	;
+
+day	: T_DAY
+          {
+	    DATE_INIT ($$);
+	    SET_ORDINAL ($$, 1);
+	    SET_NUMBER ($$, $1);
+	  }
+	| T_DAY ','
+          {
+	    DATE_INIT ($$);
+	    SET_ORDINAL ($$, 1);
+	    SET_NUMBER ($$, $1);
+	  }
+	| T_UNUMBER T_DAY
+          {
+	    DATE_INIT ($$);
+	    SET_ORDINAL ($$, $1);
+	    SET_NUMBER ($$, $2);
+	  }
+	;
+
+date	: T_UNUMBER '/' T_UNUMBER
+          {
+	    DATE_INIT ($$);
+	    SET_MONTH ($$, $1);
+	    SET_DAY ($$, $3);
+	  }
+	| T_UNUMBER '/' T_UNUMBER '/' T_UNUMBER
+          {
+	    DATE_INIT ($$);
+	    /* Interpret as YYYY/MM/DD if $1 >= 1000, otherwise as MM/DD/YY.
+	       The goal in recognizing YYYY/MM/DD is solely to support legacy
+	       machine-generated dates like those in an RCS log listing.  If
+	       you want portability, use the ISO 8601 format.  */
+	    if ($1 >= 1000)
+	      {
+		SET_YEAR ($$, $1);
+		SET_MONTH ($$, $3);
+		SET_DAY ($$, $5);
+	      }
+	    else
+	      {
+		SET_MONTH ($$, $1);
+		SET_DAY ($$, $3);
+		SET_YEAR ($$, $5);
+	      }
+	  }
+	| T_UNUMBER T_SNUMBER T_SNUMBER
+          {
+	    DATE_INIT ($$);
+	    /* ISO 8601 format.  yyyy-mm-dd.  */
+	    SET_YEAR ($$, $1);
+	    SET_MONTH ($$, -$2);
+	    SET_DAY ($$, -$3);
+	  }
+	| T_UNUMBER T_MONTH T_SNUMBER
+          {
+	    /* either 17-JUN-1992 or 1992-JUN-17 */
+	    DATE_INIT ($$);
+	    if ($1 < 32)
+	      {
+		SET_DAY ($$, $1);
+		SET_MONTH ($$, $2);
+		SET_YEAR ($$, -$3);
+	      }
+	    else
+	      {
+		SET_DAY ($$, -$3);
+		SET_MONTH ($$, $2);
+		SET_YEAR ($$, $1);
+	      }
+	  }
+	| T_MONTH T_UNUMBER
+          {
+	    DATE_INIT ($$);
+	    SET_MONTH ($$, $1);
+	    SET_DAY ($$, $2);
+	  }
+	| T_MONTH T_UNUMBER ',' T_UNUMBER
+          {
+	    DATE_INIT ($$);
+	    SET_MONTH ($$, $1);
+	    SET_DAY ($$, $2);
+	    SET_YEAR ($$, $4);
+	  }
+	| T_UNUMBER T_MONTH
+          {
+	    DATE_INIT ($$);
+	    SET_MONTH ($$, $2);
+	    SET_DAY ($$, $1);
+	  }
+	| T_UNUMBER T_MONTH T_UNUMBER
+          {
+	    DATE_INIT ($$);
+	    SET_MONTH ($$, $2);
+	    SET_DAY ($$, $1);
+	    SET_YEAR ($$, $3);
+	  }
+        | T_DAY T_MONTH T_UNUMBER time T_UNUMBER
+          {
+	    DATE_INIT ($$);
+
+	    SET_ORDINAL ($$, 1);
+	    SET_NUMBER ($$, $1);
+
+	    SET_MONTH ($$, $2);
+	    SET_DAY ($$, $3);
+	    SET_YEAR ($$, $5);
+	    if (pd_date_union (&$$, &$4))
+	      YYERROR;
+	  }
+	;
+
+rel	: relspec T_AGO
+          {
+	    $1.second = - $1.second;
+	    $1.minute = - $1.minute;
+	    $1.hour = - $1.hour;
+	    $1.day = - $1.day;
+	    $1.month = - $1.month;
+	    $1.year = - $1.year;
+	    $$ = $1;
+	  }
+	| relspec
+	;
+
+
+relspec : relunit
+          {
+	    DATE_INIT ($$);
+	    if (pd_date_union (&$$, &$1))
+	      YYERROR;
+	  }
+        | relspec relunit
+          {
+	    if (pd_date_union (&$1, &$2))
+	      YYERROR;
+	    $$ = $1;
+	  }
+        ;
+
+relunit	: T_UNUMBER T_YEAR_UNIT
+          {
+	    DATE_INIT ($$);
+	    SET_YEAR ($$, $1 * $2);
+	  }
+	| T_SNUMBER T_YEAR_UNIT
+          {
+	    DATE_INIT ($$);
+	    SET_YEAR ($$, $1 * $2);
+	  }
+	| T_YEAR_UNIT
+          {
+	    DATE_INIT ($$);
+	    SET_YEAR ($$, $1);
+	  }
+	| T_UNUMBER T_MONTH_UNIT
+          {
+	    DATE_INIT ($$);
+	    SET_MONTH ($$, $1 * $2);
+	  }
+	| T_SNUMBER T_MONTH_UNIT
+          {
+	    DATE_INIT ($$);
+	    SET_MONTH ($$, $1 * $2);
+	  }
+	| T_MONTH_UNIT
+          {
+	    DATE_INIT ($$);
+	    SET_MONTH ($$, $1);
+	  }
+	| T_UNUMBER T_DAY_UNIT
+          {
+	    DATE_INIT ($$);
+	    SET_DAY ($$, $1 * $2);
+	  }
+	| T_SNUMBER T_DAY_UNIT
+          {
+	    DATE_INIT ($$);
+	    SET_DAY ($$, $1 * $2);
+	  }
+	| T_DAY_UNIT
+          {
+	    DATE_INIT ($$);
+	    SET_DAY ($$, $1);
+	  }
+	| T_UNUMBER T_HOUR_UNIT
+          {
+	    DATE_INIT ($$);
+	    SET_HOUR ($$, $1 * $2);
+	  }
+	| T_SNUMBER T_HOUR_UNIT
+          {
+	    DATE_INIT ($$);
+	    SET_HOUR ($$, $1 * $2);
+	  }
+	| T_HOUR_UNIT
+          {
+	    DATE_INIT ($$);
+	    SET_HOUR ($$, $1);
+	  }
+	| T_UNUMBER T_MINUTE_UNIT
+          {
+	    DATE_INIT ($$);
+	    SET_MINUTE ($$, $1 * $2);
+	  }
+	| T_SNUMBER T_MINUTE_UNIT
+          {
+	    DATE_INIT ($$);
+	    SET_MINUTE ($$, $1 * $2);
+	  }
+	| T_MINUTE_UNIT
+          {
+	    DATE_INIT ($$);
+	    SET_MINUTE ($$, $1);
+	  }
+	| T_UNUMBER T_SEC_UNIT
+          {
+	    DATE_INIT ($$);
+	    SET_SECOND ($$, $1 * $2);
+	  }
+	| T_SNUMBER T_SEC_UNIT
+          {
+	    DATE_INIT ($$);
+	    SET_SECOND ($$, $1 * $2);
+	  }
+	| T_SEC_UNIT
+          {
+	    DATE_INIT ($$);
+	    SET_SECOND ($$, $1);
+	  }
+	;
+
+o_merid	: /* empty */
+          {
+	    $$ = MER24;
+	  }
+	| T_MERIDIAN
+	  {
+	    $$ = $1;
+	  }
+	;
+
+%%
+
+#include <mailutils/types.h>
+
+#if TIME_WITH_SYS_TIME
+# include <sys/time.h>
+# include <time.h>
+#else
+# if HAVE_SYS_TIME_H
+#  include <sys/time.h>
+# else
+#  include <time.h>
+# endif
+#endif
+
+#include <mailutils/mutil.h>
+
+/* Month and day table. */
+static SYMBOL const month_day_tab[] = {
+  { "january",	T_MONTH,  1 },
+  { "february",	T_MONTH,  2 },
+  { "march",	T_MONTH,  3 },
+  { "april",	T_MONTH,  4 },
+  { "may",	T_MONTH,  5 },
+  { "june",	T_MONTH,  6 },
+  { "july",	T_MONTH,  7 },
+  { "august",	T_MONTH,  8 },
+  { "september",T_MONTH,  9 },
+  { "sept",	T_MONTH,  9 },
+  { "october",	T_MONTH, 10 },
+  { "november",	T_MONTH, 11 },
+  { "december",	T_MONTH, 12 },
+  { "sunday",	T_DAY,   0 },
+  { "monday",	T_DAY,   1 },
+  { "tuesday",	T_DAY,   2 },
+  { "tues",	T_DAY,   2 },
+  { "wednesday",T_DAY,   3 },
+  { "wednes",	T_DAY,   3 },
+  { "thursday",	T_DAY,   4 },
+  { "thur",	T_DAY,   4 },
+  { "thurs",	T_DAY,   4 },
+  { "friday",	T_DAY,   5 },
+  { "saturday",	T_DAY,   6 },
+  { NULL, 0, 0 }
+};
+
+/* Time units table. */
+static SYMBOL const units_tab[] = {
+  { "year",	T_YEAR_UNIT,	1 },
+  { "month",	T_MONTH_UNIT,	1 },
+  { "fortnight",T_DAY_UNIT,	14 },
+  { "week",	T_DAY_UNIT,	7 },
+  { "day",	T_DAY_UNIT,	1 },
+  { "hour",	T_HOUR_UNIT,	1 },
+  { "minute",	T_MINUTE_UNIT,	1 },
+  { "min",	T_MINUTE_UNIT,	1 },
+  { "second",	T_SEC_UNIT,	1 },
+  { "sec",	T_SEC_UNIT,	1 },
+  { NULL, 0, 0 }
+};
+
+/* Assorted relative-time words. */
+static SYMBOL const other_tab[] = {
+  { "tomorrow",	T_MINUTE_UNIT,	1 * 24 * 60 },
+  { "yesterday",T_MINUTE_UNIT,	-1 * 24 * 60 },
+  { "today",	T_MINUTE_UNIT,	0 },
+  { "now",	T_MINUTE_UNIT,	0 },
+  { "last",	T_UNUMBER,	-1 },
+  { "this",	T_MINUTE_UNIT,	0 },
+  { "next",	T_UNUMBER,	1 },
+  { "first",	T_UNUMBER,	1 },
+/*  { "second",	T_UNUMBER,	2 }, */
+  { "third",	T_UNUMBER,	3 },
+  { "fourth",	T_UNUMBER,	4 },
+  { "fifth",	T_UNUMBER,	5 },
+  { "sixth",	T_UNUMBER,	6 },
+  { "seventh",	T_UNUMBER,	7 },
+  { "eighth",	T_UNUMBER,	8 },
+  { "ninth",	T_UNUMBER,	9 },
+  { "tenth",	T_UNUMBER,	10 },
+  { "eleventh",	T_UNUMBER,	11 },
+  { "twelfth",	T_UNUMBER,	12 },
+  { "ago",	T_AGO,	        1 },
+  { NULL, 0, 0 }
+};
+
+/* The timezone table. */
+static SYMBOL const tz_tab[] = {
+  { "gmt",	T_ZONE,     HOUR ( 0) },	/* Greenwich Mean */
+  { "ut",	T_ZONE,     HOUR ( 0) },	/* Universal (Coordinated) */
+  { "utc",	T_ZONE,     HOUR ( 0) },
+  { "wet",	T_ZONE,     HOUR ( 0) },	/* Western European */
+  { "bst",	T_DAYZONE,  HOUR ( 0) },	/* British Summer */
+  { "wat",	T_ZONE,     HOUR ( 1) },	/* West Africa */
+  { "at",	T_ZONE,     HOUR ( 2) },	/* Azores */
+#if	0
+  /* For completeness.  BST is also British Summer, and GST is
+   * also Guam Standard. */
+  { "bst",	T_ZONE,     HOUR ( 3) },	/* Brazil Standard */
+  { "gst",	T_ZONE,     HOUR ( 3) },	/* Greenland Standard */
+#endif
+#if 0
+  { "nft",	T_ZONE,     HOUR (3.5) },	/* Newfoundland */
+  { "nst",	T_ZONE,     HOUR (3.5) },	/* Newfoundland Standard */
+  { "ndt",	T_DAYZONE,  HOUR (3.5) },	/* Newfoundland Daylight */
+#endif
+  { "ast",	T_ZONE,     HOUR ( 4) },	/* Atlantic Standard */
+  { "adt",	T_DAYZONE,  HOUR ( 4) },	/* Atlantic Daylight */
+  { "est",	T_ZONE,     HOUR ( 5) },	/* Eastern Standard */
+  { "edt",	T_DAYZONE,  HOUR ( 5) },	/* Eastern Daylight */
+  { "cst",	T_ZONE,     HOUR ( 6) },	/* Central Standard */
+  { "cdt",	T_DAYZONE,  HOUR ( 6) },	/* Central Daylight */
+  { "mst",	T_ZONE,     HOUR ( 7) },	/* Mountain Standard */
+  { "mdt",	T_DAYZONE,  HOUR ( 7) },	/* Mountain Daylight */
+  { "pst",	T_ZONE,     HOUR ( 8) },	/* Pacific Standard */
+  { "pdt",	T_DAYZONE,  HOUR ( 8) },	/* Pacific Daylight */
+  { "yst",	T_ZONE,     HOUR ( 9) },	/* Yukon Standard */
+  { "ydt",	T_DAYZONE,  HOUR ( 9) },	/* Yukon Daylight */
+  { "hst",	T_ZONE,     HOUR (10) },	/* Hawaii Standard */
+  { "hdt",	T_DAYZONE,  HOUR (10) },	/* Hawaii Daylight */
+  { "cat",	T_ZONE,     HOUR (10) },	/* Central Alaska */
+  { "ahst",	T_ZONE,     HOUR (10) },	/* Alaska-Hawaii Standard */
+  { "nt",	T_ZONE,     HOUR (11) },	/* Nome */
+  { "idlw",	T_ZONE,     HOUR (12) },	/* International Date Line West */
+  { "cet",	T_ZONE,     -HOUR (1) },	/* Central European */
+  { "met",	T_ZONE,     -HOUR (1) },	/* Middle European */
+  { "mewt",	T_ZONE,     -HOUR (1) },	/* Middle European Winter */
+  { "mest",	T_DAYZONE,  -HOUR (1) },	/* Middle European Summer */
+  { "mesz",	T_DAYZONE,  -HOUR (1) },	/* Middle European Summer */
+  { "swt",	T_ZONE,     -HOUR (1) },	/* Swedish Winter */
+  { "sst",	T_DAYZONE,  -HOUR (1) },	/* Swedish Summer */
+  { "fwt",	T_ZONE,     -HOUR (1) },	/* French Winter */
+  { "fst",	T_DAYZONE,  -HOUR (1) },	/* French Summer */
+  { "eet",	T_ZONE,     -HOUR (2) },	/* Eastern Europe, USSR Zone 1 */
+  { "bt",	T_ZONE,     -HOUR (3) },	/* Baghdad, USSR Zone 2 */
+#if 0
+  { "it",	T_ZONE,     -HOUR (3.5) },/* Iran */
+#endif
+  { "zp4",	T_ZONE,     -HOUR (4) },	/* USSR Zone 3 */
+  { "zp5",	T_ZONE,     -HOUR (5) },	/* USSR Zone 4 */
+#if 0
+  { "ist",	T_ZONE,     -HOUR (5.5) },/* Indian Standard */
+#endif
+  { "zp6",	T_ZONE,     -HOUR (6) },	/* USSR Zone 5 */
+#if	0
+  /* For completeness.  NST is also Newfoundland Standard, and SST is
+   * also Swedish Summer. */
+  { "nst",	T_ZONE,     -HOUR (6.5) },/* North Sumatra */
+  { "sst",	T_ZONE,     -HOUR (7) },	/* South Sumatra, USSR Zone 6 */
+#endif	/* 0 */
+  { "wast",	T_ZONE,     -HOUR (7) },	/* West Australian Standard */
+  { "wadt",	T_DAYZONE,  -HOUR (7) },	/* West Australian Daylight */
+#if 0
+  { "jt",	T_ZONE,     -HOUR (7.5) },/* Java (3pm in Cronusland!) */
+#endif
+  { "cct",	T_ZONE,     -HOUR (8) },	/* China Coast, USSR Zone 7 */
+  { "jst",	T_ZONE,     -HOUR (9) },	/* Japan Standard, USSR Zone 8 */
+#if 0
+  { "cast",	T_ZONE,     -HOUR (9.5) },/* Central Australian Standard */
+  { "cadt",	T_DAYZONE,  -HOUR (9.5) },/* Central Australian Daylight */
+#endif
+  { "east",	T_ZONE,     -HOUR (10) },	/* Eastern Australian Standard */
+  { "eadt",	T_DAYZONE,  -HOUR (10) },	/* Eastern Australian Daylight */
+  { "gst",	T_ZONE,     -HOUR (10) },	/* Guam Standard, USSR Zone 9 */
+  { "nzt",	T_ZONE,     -HOUR (12) },	/* New Zealand */
+  { "nzst",	T_ZONE,     -HOUR (12) },	/* New Zealand Standard */
+  { "nzdt",	T_DAYZONE,  -HOUR (12) },	/* New Zealand Daylight */
+  { "idle",	T_ZONE,     -HOUR (12) },	/* International Date Line
+						   East */
+  {  NULL, 0, 0  }
+};
+
+/* Military timezone table. */
+static SYMBOL const mil_tz_tab[] = {
+  { "a",	T_ZONE,	HOUR (  1) },
+  { "b",	T_ZONE,	HOUR (  2) },
+  { "c",	T_ZONE,	HOUR (  3) },
+  { "d",	T_ZONE,	HOUR (  4) },
+  { "e",	T_ZONE,	HOUR (  5) },
+  { "f",	T_ZONE,	HOUR (  6) },
+  { "g",	T_ZONE,	HOUR (  7) },
+  { "h",	T_ZONE,	HOUR (  8) },
+  { "i",	T_ZONE,	HOUR (  9) },
+  { "k",	T_ZONE,	HOUR ( 10) },
+  { "l",	T_ZONE,	HOUR ( 11) },
+  { "m",	T_ZONE,	HOUR ( 12) },
+  { "n",	T_ZONE,	HOUR (- 1) },
+  { "o",	T_ZONE,	HOUR (- 2) },
+  { "p",	T_ZONE,	HOUR (- 3) },
+  { "q",	T_ZONE,	HOUR (- 4) },
+  { "r",	T_ZONE,	HOUR (- 5) },
+  { "s",	T_ZONE,	HOUR (- 6) },
+  { "t",	T_ZONE,	HOUR (- 7) },
+  { "u",	T_ZONE,	HOUR (- 8) },
+  { "v",	T_ZONE,	HOUR (- 9) },
+  { "w",	T_ZONE,	HOUR (-10) },
+  { "x",	T_ZONE,	HOUR (-11) },
+  { "y",	T_ZONE,	HOUR (-12) },
+  { "z",	T_ZONE,	HOUR (  0) },
+  { NULL, 0, 0 }
+};
+
+
+
+
+/* ARGSUSED */
+static int
+yyerror (char *s MU_ARG_UNUSED)
+{
+  return 0;
+}
+
+static int
+norm_hour (int hours, MERIDIAN meridian)
+{
+  switch (meridian)
+    {
+    case MER24:
+      if (hours < 0 || hours > 23)
+	return -1;
+      return hours;
+      
+    case MERam:
+      if (hours < 1 || hours > 12)
+	return -1;
+      if (hours == 12)
+	hours = 0;
+      return hours;
+      
+    case MERpm:
+      if (hours < 1 || hours > 12)
+	return -1;
+      if (hours == 12)
+	hours = 0;
+      return hours + 12;
+      
+    default:
+      abort ();
+    }
+  /* NOTREACHED */
+}
+
+static int
+norm_year (int year)
+{
+  if (year < 0)
+    year = -year;
+  
+  /* XPG4 suggests that years 00-68 map to 2000-2068, and
+     years 69-99 map to 1969-1999.  */
+  if (year < 69)
+    year += 2000;
+  else if (year < 100)
+    year += 1900;
+
+  return year;
+}
+
+static int
+sym_lookup (char *buff)
+{
+  register char *p;
+  register char *q;
+  register const SYMBOL *tp;
+  int i;
+  int abbrev;
+  
+  /* Make it lowercase. */
+  for (p = buff; *p; p++)
+    if (ISUPPER ((unsigned char) *p))
+      *p = tolower (*p);
+  
+  if (strcmp (buff, "am") == 0 || strcmp (buff, "a.m.") == 0)
+    {
+      yylval.meridian = MERam;
+      return T_MERIDIAN;
+    }
+  if (strcmp (buff, "pm") == 0 || strcmp (buff, "p.m.") == 0)
+    {
+      yylval.meridian = MERpm;
+      return T_MERIDIAN;
+    }
+  
+  /* See if we have an abbreviation for a month. */
+  if (strlen (buff) == 3)
+    abbrev = 1;
+  else if (strlen (buff) == 4 && buff[3] == '.')
+    {
+      abbrev = 1;
+      buff[3] = '\0';
+    }
+  else
+    abbrev = 0;
+
+  for (tp = month_day_tab; tp->name; tp++)
+    {
+      if (abbrev)
+	{
+	  if (strncmp (buff, tp->name, 3) == 0)
+	    {
+	      yylval.number = tp->value;
+	      return tp->type;
+	    }
+	}
+      else if (strcmp (buff, tp->name) == 0)
+	{
+	  yylval.number = tp->value;
+	  return tp->type;
+	}
+    }
+
+  for (tp = tz_tab; tp->name; tp++)
+    if (strcmp (buff, tp->name) == 0)
+      {
+	yylval.number = tp->value;
+	return tp->type;
+      }
+
+  if (strcmp (buff, "dst") == 0)
+    return T_DST;
+
+  for (tp = units_tab; tp->name; tp++)
+    if (strcmp (buff, tp->name) == 0)
+      {
+	yylval.number = tp->value;
+	return tp->type;
+      }
+
+  /* Strip off any plural and try the units table again. */
+  i = strlen (buff) - 1;
+  if (buff[i] == 's')
+    {
+      buff[i] = '\0';
+      for (tp = units_tab; tp->name; tp++)
+	if (strcmp (buff, tp->name) == 0)
+	  {
+	    yylval.number = tp->value;
+	    return tp->type;
+	  }
+      buff[i] = 's';		/* Put back for "this" in other_tab. */
+    }
+
+  for (tp = other_tab; tp->name; tp++)
+    if (strcmp (buff, tp->name) == 0)
+      {
+	yylval.number = tp->value;
+	return tp->type;
+      }
+
+  /* Military timezones. */
+  if (buff[1] == '\0' && ISALPHA ((unsigned char) *buff))
+    {
+      for (tp = mil_tz_tab; tp->name; tp++)
+	if (strcmp (buff, tp->name) == 0)
+	  {
+	    yylval.number = tp->value;
+	    return tp->type;
+	  }
+    }
+
+  /* Drop out any periods and try the timezone table again. */
+  for (i = 0, p = q = buff; *q; q++)
+    if (*q != '.')
+      *p++ = *q;
+    else
+      i++;
+  *p = '\0';
+  if (i)
+    for (tp = tz_tab; tp->name; tp++)
+      if (strcmp (buff, tp->name) == 0)
+	{
+	  yylval.number = tp->value;
+	  return tp->type;
+	}
+
+  return T_ID;
+}
+
+static int
+yylex ()
+{
+  register unsigned char c;
+  register char *p;
+  char buff[20];
+  int count;
+  int sign;
+
+  for (;;)
+    {
+      while (ISSPACE ((unsigned char) *yyinput))
+	yyinput++;
+
+      if (ISDIGIT (c = *yyinput) || c == '-' || c == '+')
+	{
+	  if (c == '-' || c == '+')
+	    {
+	      sign = c == '-' ? -1 : 1;
+	      if (!ISDIGIT (*++yyinput))
+		/* skip the '-' sign */
+		continue;
+	    }
+	  else
+	    sign = 0;
+	  for (yylval.number = 0; ISDIGIT (c = *yyinput++);)
+	    yylval.number = 10 * yylval.number + c - '0';
+	  yyinput--;
+	  if (sign < 0)
+	    yylval.number = -yylval.number;
+	  return sign ? T_SNUMBER : T_UNUMBER;
+	}
+      if (ISALPHA (c))
+	{
+	  for (p = buff; (c = *yyinput++, ISALPHA (c)) || c == '.';)
+	    if (p < &buff[sizeof buff - 1])
+	      *p++ = c;
+	  *p = '\0';
+	  yyinput--;
+	  return sym_lookup (buff);
+	}
+      if (c != '(')
+	return *yyinput++;
+      count = 0;
+      do
+	{
+	  c = *yyinput++;
+	  if (c == '\0')
+	    return c;
+	  if (c == '(')
+	    count++;
+	  else if (c == ')')
+	    count--;
+	}
+      while (count > 0);
+    }
+}
+
+#define TM_YEAR_ORIGIN 1900
+
+/* Yield A - B, measured in seconds.  */
+static long
+difftm (struct tm *a, struct tm *b)
+{
+  int ay = a->tm_year + (TM_YEAR_ORIGIN - 1);
+  int by = b->tm_year + (TM_YEAR_ORIGIN - 1);
+  long days = (
+  /* difference in day of year */
+		a->tm_yday - b->tm_yday
+  /* + intervening leap days */
+		+ ((ay >> 2) - (by >> 2))
+		- (ay / 100 - by / 100)
+		+ ((ay / 100 >> 2) - (by / 100 >> 2))
+  /* + difference in years * 365 */
+		+ (long) (ay - by) * 365
+  );
+  return (60 * (60 * (24 * days + (a->tm_hour - b->tm_hour))
+		+ (a->tm_min - b->tm_min))
+	  + (a->tm_sec - b->tm_sec));
+}
+
+int
+mu_parse_date (const char *p, time_t *rettime, const time_t *now)
+{
+  struct tm tm, tm0, *tmp;
+  time_t start;
+
+  yyinput = p;
+  start = now ? *now : time ((time_t *) NULL);
+  tmp = localtime (&start);
+  if (!tmp)
+    return -1;
+
+  memset (&tm, 0, sizeof tm);
+  tm.tm_isdst = tmp->tm_isdst;
+
+  if (yyparse ())
+    return -1;
+  
+  if (!MASK_IS_SET (pd.date.mask, PD_MASK_YEAR))
+    __SET_YEAR (pd.date, tmp->tm_year + TM_YEAR_ORIGIN, return -1);
+  if (!MASK_IS_SET (pd.date.mask, PD_MASK_MONTH))
+    __SET_MONTH (pd.date, tmp->tm_mon + 1, return -1);
+  if (!MASK_IS_SET (pd.date.mask, PD_MASK_DAY))
+    __SET_DAY (pd.date, tmp->tm_mday, return -1);
+  if (!MASK_IS_SET (pd.date.mask, PD_MASK_HOUR))
+    __SET_HOUR (pd.date, tmp->tm_hour, return -1);
+  if (!MASK_IS_SET (pd.date.mask, PD_MASK_MERIDIAN))
+    __SET_MERIDIAN (pd.date, MER24, return -1);
+  if (!MASK_IS_SET (pd.date.mask, PD_MASK_MINUTE))
+    __SET_MINUTE (pd.date, tmp->tm_min, return -1);
+  if (!MASK_IS_SET (pd.date.mask, PD_MASK_SECOND))
+    __SET_SECOND (pd.date, tmp->tm_sec, return -1);
+  
+  tm.tm_year = norm_year (pd.date.year) - TM_YEAR_ORIGIN + pd.rel.year;
+  tm.tm_mon = pd.date.month - 1 + pd.rel.month;
+  tm.tm_mday = pd.date.day + pd.rel.day;
+  if (MASK_TEST (pd.date.mask, PD_MASK_TIME)
+      || (pd.rel.mask && !MASK_TEST (pd.date.mask, PD_MASK_DATE)
+	  && !MASK_TEST (pd.date.mask, PD_MASK_DOW)))
+    {
+      tm.tm_hour = norm_hour (pd.date.hour, pd.date.meridian);
+      if (tm.tm_hour < 0)
+	return -1;
+      tm.tm_min = pd.date.minute;
+      tm.tm_sec = pd.date.second;
+    }
+  else
+    {
+      tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
+    }
+  tm.tm_hour += pd.rel.hour;
+  tm.tm_min += pd.rel.minute;
+  tm.tm_sec += pd.rel.second;
+
+  /* Let mktime deduce tm_isdst if we have an absolute timestamp,
+     or if the relative timestamp mentions days, months, or years.  */
+  if (MASK_TEST (pd.date.mask, PD_MASK_DATE | PD_MASK_DOW | PD_MASK_TIME)
+      || MASK_TEST (pd.rel.mask, PD_MASK_DOW | PD_MASK_MONTH | PD_MASK_YEAR))
+    tm.tm_isdst = -1;
+
+  tm0 = tm;
+
+  start = mktime (&tm);
+
+  if (start == (time_t) -1)
+    {
+
+      /* Guard against falsely reporting errors near the time_t boundaries
+         when parsing times in other time zones.  For example, if the min
+         time_t value is 1970-01-01 00:00:00 UTC and we are 8 hours ahead
+         of UTC, then the min localtime value is 1970-01-01 08:00:00; if
+         we apply mktime to 1970-01-01 00:00:00 we will get an error, so
+         we apply mktime to 1970-01-02 08:00:00 instead and adjust the time
+         zone by 24 hours to compensate.  This algorithm assumes that
+         there is no DST transition within a day of the time_t boundaries.  */
+      if (MASK_TEST (pd.date.mask, PD_MASK_TZ))
+	{
+	  tm = tm0;
+	  if (tm.tm_year <= EPOCH - TM_YEAR_ORIGIN)
+	    {
+	      tm.tm_mday++;
+	      pd.date.tz -= 24 * 60;
+	    }
+	  else
+	    {
+	      tm.tm_mday--;
+	      pd.date.tz += 24 * 60;
+	    }
+	  start = mktime (&tm);
+	}
+
+      if (start == (time_t) -1)
+	return -1;
+    }
+
+  if (MASK_TEST (pd.date.mask, PD_MASK_DOW)
+      && !MASK_TEST (pd.date.mask, PD_MASK_DATE))
+    {
+      tm.tm_mday += ((pd.date.number - tm.tm_wday + 7) % 7
+		     + 7 * (pd.date.ordinal - (0 < pd.date.ordinal)));
+      start = mktime (&tm);
+      if (start == (time_t) -1)
+	return -1;
+    }
+  
+  if (MASK_TEST (pd.date.mask, PD_MASK_TZ))
+    {
+      long delta;
+      struct tm *gmt = gmtime (&start);
+      if (gmt)
+	{
+	  delta = pd.date.tz * 60L + difftm (&tm, gmt);
+	  if ((start + delta < start) != (delta < 0))
+	    return -1;		/* time_t overflow */
+	  start += delta;
+	}
+    }
+  
+  *rettime = start;
+  return 0;
+}
+
+#ifdef STANDALONE
+int
+main (int argc, char *argv[])
+{
+  char buff[MAX_BUFF_LEN + 1];
+  time_t d;
+
+  if (argc > 1 && strcmp (argv[1], "-d") == 0)
+    yydebug++;
+  printf ("Enter date, or blank line to exit.\n\t> ");
+  fflush (stdout);
+
+  buff[MAX_BUFF_LEN] = 0;
+  while (fgets (buff, MAX_BUFF_LEN, stdin) && buff[0])
+    {
+      if (mu_parse_date (buff, &d, NULL))
+	printf ("Bad format - couldn't convert.\n");
+      else
+	printf ("%s", ctime (&d));
+      printf ("\t> ");
+      fflush (stdout);
+    }
+  exit (0);
+  /* NOTREACHED */
+}
+
+#endif
diff --git a/libmailutils/base/permstr.c b/libmailutils/base/permstr.c
new file mode 100644
index 0000000..ae16ff6
--- /dev/null
+++ b/libmailutils/base/permstr.c
@@ -0,0 +1,127 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 2008, 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, see 
+   <http://www.gnu.org/licenses/>. */
+
+#if HAVE_CONFIG_H
+# include <config.h>
+#endif
+#include <mailutils/stream.h>
+#include <mailutils/mutil.h>
+#include <mailutils/errno.h>
+
+#define FILE_PERM_READ 0x1
+#define FILE_PERM_WRITE 0x2
+
+static int
+parse_perm_bits (int *pmode, const char *str, const char **endp)
+{
+  switch (*str)
+    {
+    case '+':
+    case '=':
+      str++;
+      break;
+
+    default:
+      if (endp)
+	*endp = str;
+      return 1;
+    }
+
+  for (; *str; str++)
+    {
+      switch (*str)
+	{
+	case 'r':
+	  *pmode |= FILE_PERM_READ;
+	  break;
+
+	case 'w':
+	  *pmode |= FILE_PERM_WRITE;
+	  break;
+
+	case ',':
+	  if (endp)
+	    *endp = str;
+	  return 0;
+	  
+	default:
+	  if (endp)
+	    *endp = str;
+	  return 1;
+	}
+    }
+  if (endp)
+    *endp = str;
+  return 0;
+}
+
+/* Parse a MU stream permission specification in form:
+
+      g(+|=)[wr]+,o(+|=)[wr]+
+
+   Return 0 on success.
+   On failure, return MU_ERR_FAILURE and point endp to the offending character.
+*/
+int
+mu_parse_stream_perm_string (int *pmode, const char *str, const char **endp)
+{
+  int mode = 0;
+  int f;
+  while (*str)
+    {
+      switch (*str)
+	{
+	case 'g':
+	  if (parse_perm_bits (&f, str + 1, &str))
+	    {
+	      if (endp)
+		*endp = str;
+	      return MU_ERR_FAILURE;
+	    }
+	  if (f & FILE_PERM_READ)
+	    mode |= MU_STREAM_IRGRP;
+	  if (f & FILE_PERM_WRITE)
+	    mode |= MU_STREAM_IWGRP;
+	  break;
+	  
+	case 'o':
+	  if (parse_perm_bits (&f, str + 1, &str))
+	    {
+	      if (endp)
+		*endp = str;
+	      return MU_ERR_FAILURE;
+	    }
+	  if (f & FILE_PERM_READ)
+	    mode |= MU_STREAM_IROTH;
+	  if (f & FILE_PERM_WRITE)
+	    mode |= MU_STREAM_IWOTH;
+	  break;
+	  
+	default:
+	  if (endp)
+	    *endp = str;
+	  return MU_ERR_FAILURE;
+	}
+      if (*str == ',')
+	str++;
+    }
+  *pmode = mode;
+  if (endp)
+    *endp = str;
+  return 0;
+}
+      
diff --git a/libmailutils/base/property.c b/libmailutils/base/property.c
new file mode 100644
index 0000000..eec0ddc
--- /dev/null
+++ b/libmailutils/base/property.c
@@ -0,0 +1,191 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 2004, 2005, 2007, 2008, 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, see 
+   <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+#ifdef HAVE_STRINGS_H
+# include <strings.h>
+#endif
+
+#include <mailutils/sys/property.h>
+#include <mailutils/errno.h>
+#include <mailutils/assoc.h>
+#include <stdlib.h>
+
+static void
+prop_free_value (void *data)
+{
+  struct property_item *item = data;
+  free (item->value);
+}
+
+int
+mu_property_create (mu_property_t *pp, void *owner)
+{
+  int rc;
+  mu_property_t prop;
+  if (pp == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+  prop = calloc (1, sizeof *prop);
+  if (prop == NULL)
+    return ENOMEM;
+  rc = mu_assoc_create (&prop->assoc, sizeof (struct property_item), 0);
+  if (rc)
+    {
+      free (prop);
+      return rc;
+    }
+  mu_assoc_set_free (prop->assoc, prop_free_value);
+  mu_monitor_create (&prop->lock, 0, prop);
+  prop->owner = owner;
+  *pp = prop;
+  return 0;
+}
+
+void
+mu_property_destroy (mu_property_t *pp, void *owner)
+{
+  if (pp && *pp)
+    {
+      mu_property_t prop = *pp;
+      if (prop->owner == owner)
+	{
+	  /* Destroy the list and is properties.  */
+	  mu_monitor_wrlock (prop->lock);
+	  mu_assoc_destroy (&prop->assoc);
+	  mu_monitor_unlock (prop->lock);
+	  mu_monitor_destroy (&prop->lock, prop);
+	  free (prop);
+	}
+      *pp = NULL;
+    }
+}
+
+void *
+mu_property_get_owner (mu_property_t prop)
+{
+  return (prop == NULL) ? NULL : prop->owner;
+}
+
+int
+mu_property_set_value (mu_property_t prop, const char *key, const char *value,
+		       int overwrite)
+{
+  struct property_item *item;
+  int rc;
+
+  if (!prop)
+    return EINVAL;
+  rc = mu_assoc_ref_install (prop->assoc, key, (void **)&item);
+  if (rc == MU_ERR_NOENT)
+    {
+      item->value = strdup (value);
+    }
+  else if (overwrite)
+    {
+      free (item->value);
+      item->value = strdup (value);
+    }
+  else
+    return 0;
+  
+  if (!item->value)
+    {
+      mu_assoc_remove (prop->assoc, key);
+      return ENOMEM;
+    }
+
+  return 0;
+}
+  
+int
+mu_property_sget_value (mu_property_t prop, const char *key,
+			const char **buffer)
+{
+  struct property_item *item;
+
+  if (!prop)
+    return EINVAL;
+  item = mu_assoc_ref (prop->assoc, key);
+  if (item == NULL)
+    return MU_ERR_NOENT;
+  if (buffer)
+    *buffer = item->value;
+  return 0;
+}
+
+int
+mu_property_aget_value (mu_property_t prop, const char *key,
+			char **buffer)
+{
+  const char *value;
+  int rc = mu_property_sget_value (prop, key, &value);
+  if (rc == 0)
+    {
+      if ((*buffer = strdup (value)) == NULL)
+	return ENOMEM;
+    }
+  return rc;
+}
+
+int
+mu_property_get_value (mu_property_t prop, const char *key, char *buffer,
+		       size_t buflen, size_t *n)
+{
+  size_t len = 0;
+  const char *value;
+  int rc = mu_property_sget_value (prop, key, &value);
+  if (rc == 0)
+    {
+      len = strlen (value) + 1;
+      if (buffer && buflen)
+	{
+	  if (buflen < len)
+	    len = buflen;
+	  len--;
+	  memcpy (buffer, value, len);
+	  buffer[len] = 0;
+	}
+    }
+  if (n)
+    *n = len;
+  return rc;
+}
+
+int
+mu_property_is_set (mu_property_t prop, const char *key)
+{
+  struct property_item *item = mu_assoc_ref (prop->assoc, key);
+  return (item == NULL) ? 0 : 1;
+}
+
+int
+mu_property_set (mu_property_t prop, const char *key)
+{
+  return mu_property_set_value (prop, key, "", 1);
+}
+
+int
+mu_property_unset (mu_property_t prop, const char *key)
+{
+  if (!prop)
+    return EINVAL;
+  return mu_assoc_remove (prop->assoc, key);
+}
+  
diff --git a/libmailutils/base/refcount.c b/libmailutils/base/refcount.c
new file mode 100644
index 0000000..6d18373
--- /dev/null
+++ b/libmailutils/base/refcount.c
@@ -0,0 +1,107 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 2003, 2004, 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,
+   see <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <errno.h>
+
+#include <mailutils/errno.h>
+#include <mailutils/monitor.h>
+#include <mailutils/refcount.h>
+
+struct _mu_refcount
+{
+  unsigned int ref;
+  mu_monitor_t lock;
+};
+
+int
+mu_refcount_create (mu_refcount_t *prefcount)
+{
+  int status = 0;
+  mu_refcount_t refcount;
+  if (prefcount == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+  refcount = calloc (1, sizeof *refcount);
+  if (refcount != NULL)
+    {
+      refcount->ref = 1;
+      status = mu_monitor_create (&refcount->lock, 0, refcount);
+      if (status == 0)
+	{
+	  *prefcount = refcount;
+	}
+      else
+	{
+	  free (refcount);
+	}
+    }
+  else
+    {
+      status = ENOMEM;
+    }
+  return status;
+}
+
+unsigned
+mu_refcount_value (mu_refcount_t refcount)
+{
+  return refcount ? refcount->ref : 0;
+}
+
+void
+mu_refcount_destroy (mu_refcount_t *prefcount)
+{
+  if (prefcount && *prefcount)
+    {
+      mu_refcount_t refcount = *prefcount;
+      mu_monitor_destroy (&refcount->lock, refcount);
+      free (refcount);
+      *prefcount = NULL;
+    }
+}
+
+unsigned
+mu_refcount_inc (mu_refcount_t refcount)
+{
+  int count = 0;
+  if (refcount)
+    {
+      mu_monitor_wrlock (refcount->lock);
+      count = ++refcount->ref;
+      mu_monitor_unlock (refcount->lock);
+    }
+  return count;
+}
+
+unsigned
+mu_refcount_dec (mu_refcount_t refcount)
+{
+  int count = 0;
+  if (refcount)
+    {
+      mu_monitor_wrlock (refcount->lock);
+      if (refcount->ref)
+	count = --refcount->ref;
+      mu_monitor_unlock (refcount->lock);
+    }
+  return count;
+}
diff --git a/libmailutils/base/registrar.c b/libmailutils/base/registrar.c
new file mode 100644
index 0000000..33782e9
--- /dev/null
+++ b/libmailutils/base/registrar.c
@@ -0,0 +1,442 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 2004, 2005, 2006, 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, see
+   <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#ifdef HAVE_STRINGS_H
+# include <strings.h>
+#endif
+
+#include <mailutils/iterator.h>
+#include <mailutils/list.h>
+#include <mailutils/monitor.h>
+#include <mailutils/errno.h>
+#include <mailutils/nls.h>
+#include <mailutils/error.h>
+#include <mailutils/url.h>
+#include <mailutils/mutil.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.  */
+static mu_list_t registrar_list;
+struct mu_monitor registrar_monitor = MU_MONITOR_INITIALIZER;
+
+static mu_record_t mu_default_record;
+
+void
+mu_registrar_set_default_record (mu_record_t record)
+{
+  mu_default_record = record;
+}
+
+int
+mu_registrar_get_default_record (mu_record_t *prec)
+{
+  if (mu_default_record)
+    {
+      if (prec)
+	*prec = mu_default_record;
+      return 0;
+    }
+  return MU_ERR_NOENT;
+}
+
+int
+mu_registrar_set_default_scheme (const char *scheme)
+{
+  int status;
+  mu_record_t rec;
+  
+  status = mu_registrar_lookup_scheme (scheme, &rec);
+  if (status == 0)
+    mu_default_record = rec;
+  return status;
+}
+
+const char *
+mu_registrar_get_default_scheme ()
+{
+  return mu_default_record ? mu_default_record->scheme : NULL;
+}
+
+static int
+_registrar_get_list (mu_list_t *plist)
+{
+  int status = 0;
+
+  if (plist == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+  mu_monitor_wrlock (&registrar_monitor);
+  if (registrar_list == NULL)
+    status = mu_list_create (&registrar_list);
+  *plist = registrar_list;
+  mu_monitor_unlock (&registrar_monitor);
+  return status;
+}
+
+/* Provided for backward compatibility */
+int
+mu_registrar_get_list (mu_list_t *plist)
+{
+  static int warned;
+
+  if (!warned)
+    {
+      mu_error (_("program uses mu_registrar_get_list(), which is deprecated"));
+      warned = 1;
+    }
+  return _registrar_get_list (plist);
+}
+
+int
+mu_registrar_get_iterator (mu_iterator_t *pitr)
+{
+  int status = 0;
+  if (pitr == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+  mu_monitor_wrlock (&registrar_monitor);
+  if (registrar_list == NULL)
+    {
+      status = mu_list_create (&registrar_list);
+      if (status)
+	return status;
+    }
+  status = mu_list_get_iterator (registrar_list, pitr);
+  mu_monitor_unlock (&registrar_monitor);
+  return status;
+}
+
+int
+mu_registrar_lookup_scheme (const char *scheme, mu_record_t *precord)
+{
+  size_t len;
+  mu_iterator_t iterator;
+  int status = mu_registrar_get_iterator (&iterator);
+  if (status != 0)
+    return status;
+  status = MU_ERR_NOENT;
+  len = strcspn (scheme, ":");
+  for (mu_iterator_first (iterator); !mu_iterator_is_done (iterator);
+       mu_iterator_next (iterator))
+    {
+      mu_record_t record;
+      mu_iterator_current (iterator, (void **)&record);
+      if (strlen (record->scheme) == len
+	  && memcmp (record->scheme, scheme, len) == 0)
+	{
+	  if (precord)
+	    *precord = record;
+	  status = 0;
+	  break;
+	}
+    }
+  mu_iterator_destroy (&iterator);
+  return status;
+}
+
+int
+mu_registrar_lookup_url (mu_url_t url, int flags,
+			 mu_record_t *precord, int *pflags)
+{
+  mu_iterator_t iterator;
+  mu_record_t last_record = NULL;
+  int last_flags = 0;
+  int status = mu_registrar_get_iterator (&iterator);
+  if (status != 0)
+    return status;
+  status = MU_ERR_NOENT;
+  for (mu_iterator_first (iterator); !mu_iterator_is_done (iterator);
+       mu_iterator_next (iterator))
+    {
+      int rc;
+      mu_record_t record;
+      mu_iterator_current (iterator, (void **)&record);
+      if ((rc = mu_record_is_scheme (record, url, flags)))
+	{
+	  if (rc == flags)
+	    {
+	      status = 0;
+	      last_record = record;
+	      last_flags = rc;
+	      break;
+	    }
+	  else if (rc > last_flags)
+	    {
+	      status = 0;
+	      last_record = record;
+	      last_flags = rc;
+	    }
+	}
+    }
+  mu_iterator_destroy (&iterator);
+
+  if (status == 0)
+    {
+      if (precord)
+	*precord = last_record;
+      if (pflags)
+	*pflags = last_flags;
+    }
+  else if (!mu_is_proto (mu_url_to_string (url)) /* FIXME: This check is not
+						    enough. */
+	   && mu_registrar_get_default_record (precord) == 0)
+    {
+      status = 0;
+      if (pflags)
+	*pflags = flags & MU_FOLDER_ATTRIBUTE_FILE; /* FIXME? */
+    }
+  
+  return status;
+}
+
+int
+mu_registrar_lookup (const char *name, int flags,
+		     mu_record_t *precord, int *pflags)
+{
+  int rc;
+  mu_url_t url;
+  
+  rc = mu_url_create (&url, name);
+  if (rc)
+    return rc;
+  rc = mu_url_parse (url);
+  if (rc == 0)
+    rc = mu_registrar_lookup_url (url, flags, precord, pflags);
+  mu_url_destroy (&url);
+  return rc;
+}
+
+static int
+_compare_prio (const void *item, const void *value)
+{
+  const mu_record_t a = (const mu_record_t) item;
+  const mu_record_t b = (const mu_record_t) value;
+  if (a->priority > b->priority)
+    return 0;
+  return -1;
+}
+
+int
+mu_registrar_record (mu_record_t record)
+{
+  int status;
+  mu_list_t list;
+  mu_list_comparator_t comp;
+  
+  if (!record)
+    return 0;
+  _registrar_get_list (&list);
+  comp = mu_list_set_comparator (list, _compare_prio);
+  status = mu_list_insert (list, record, record, 1);
+  if (status == MU_ERR_NOENT)
+    status = mu_list_append (list, record);
+  mu_list_set_comparator (list, comp);
+  return status;
+}
+
+int
+mu_unregistrar_record (mu_record_t record)
+{
+  mu_list_t list;
+  _registrar_get_list (&list);
+  mu_list_remove (list, record);
+  return 0;
+}
+
+int
+mu_record_is_scheme (mu_record_t record, mu_url_t url, int flags)
+{
+  if (record == NULL)
+    return 0;
+
+  /* Overload.  */
+  if (record->_is_scheme)
+    return record->_is_scheme (record, url, flags);
+
+  if (mu_url_is_scheme (url, record->scheme))
+    return MU_FOLDER_ATTRIBUTE_ALL;
+
+  return 0;
+}
+
+int
+mu_record_set_scheme (mu_record_t record, const char *scheme)
+{
+  if (record == NULL)
+    return EINVAL;
+  record->scheme = scheme;
+  return 0;
+}
+
+int
+mu_record_set_is_scheme (mu_record_t record,
+			 int (*_is_scheme) (mu_record_t, mu_url_t, int))
+{
+  if (record == NULL)
+    return EINVAL;
+  record->_is_scheme = _is_scheme;
+  return 0;
+}
+
+int
+mu_record_get_url (mu_record_t record, int (*(*_purl)) (mu_url_t))
+{
+  if (record == NULL)
+    return EINVAL;
+  if (_purl == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+  /* Overload.  */
+  if (record->_get_url)
+    return record->_get_url (record, _purl);
+  *_purl = record->_url;
+  return 0;
+}
+
+int
+mu_record_set_url (mu_record_t record, int (*_mu_url) (mu_url_t))
+{
+  if (record == NULL)
+    return EINVAL;
+  record->_url = _mu_url;
+  return 0;
+}
+
+int
+mu_record_set_get_url (mu_record_t record, int (*_get_url)
+		    (mu_record_t, int (*(*)) (mu_url_t)))
+{
+  if (record == NULL)
+    return EINVAL;
+  record->_get_url = _get_url;
+  return 0;
+}
+
+int
+mu_record_get_mailbox (mu_record_t record, int (*(*_pmailbox)) (mu_mailbox_t))
+{
+  if (record == NULL)
+    return EINVAL;
+  if (_pmailbox == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+  /* Overload.  */
+  if (record->_get_mailbox)
+    return record->_get_mailbox (record, _pmailbox);
+  *_pmailbox = record->_mailbox;
+  return 0;
+}
+
+int
+mu_record_set_mailbox (mu_record_t record, int (*_mu_mailbox) (mu_mailbox_t))
+{
+  if (record)
+    return EINVAL;
+  record->_mailbox = _mu_mailbox;
+  return 0;
+}
+
+int
+mu_record_set_get_mailbox (mu_record_t record, 
+     int (*_get_mailbox) (mu_record_t, int (*(*)) (mu_mailbox_t)))
+{
+  if (record)
+    return EINVAL;
+  record->_get_mailbox = _get_mailbox;
+  return 0;
+}
+
+int
+mu_record_get_mailer (mu_record_t record, int (*(*_pmailer)) (mu_mailer_t))
+{
+  if (record == NULL)
+    return EINVAL;
+  if (_pmailer == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+  /* Overload.  */
+  if (record->_get_mailer)
+    return record->_get_mailer (record, _pmailer);
+  *_pmailer = record->_mailer;
+  return 0;
+}
+
+int
+mu_record_set_mailer (mu_record_t record, int (*_mu_mailer) (mu_mailer_t))
+{
+  if (record)
+    return EINVAL;
+  record->_mailer = _mu_mailer;
+  return 0;
+}
+
+int
+mu_record_set_get_mailer (mu_record_t record, 
+  int (*_get_mailer) (mu_record_t, int (*(*)) (mu_mailer_t)))
+{
+  if (record == NULL)
+    return EINVAL;
+  record->_get_mailer = _get_mailer;
+  return 0;
+}
+
+int
+mu_record_get_folder (mu_record_t record, int (*(*_pfolder)) (mu_folder_t))
+{
+  if (record == NULL)
+    return EINVAL;
+  if (_pfolder == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+  /* Overload.  */
+  if (record->_get_folder)
+    return record->_get_folder (record, _pfolder);
+  *_pfolder = record->_folder;
+  return 0;
+}
+
+int
+mu_record_set_folder (mu_record_t record, int (*_mu_folder) (mu_folder_t))
+{
+  if (record == NULL)
+    return EINVAL;
+  record->_folder = _mu_folder;
+  return 0;
+}
+
+int
+mu_record_set_get_folder (mu_record_t record, 
+   int (*_get_folder) (mu_record_t, int (*(*)) (mu_folder_t)))
+{
+  if (record == NULL)
+    return EINVAL;
+  record->_get_folder = _get_folder;
+  return 0;
+}
+
+int
+mu_record_list_p (mu_record_t record, const char *name, int flags)
+{
+  if (record == NULL)
+    return EINVAL;
+  return record == NULL
+          || !record->_list_p
+          || record->_list_p (record, name, flags);
+}
diff --git a/libmailutils/base/rfc2047.c b/libmailutils/base/rfc2047.c
new file mode 100644
index 0000000..32b0923
--- /dev/null
+++ b/libmailutils/base/rfc2047.c
@@ -0,0 +1,313 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 2003, 2004, 2005, 2006, 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, see 
+   <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <ctype.h>
+#include <mailutils/stream.h>
+#include <mailutils/filter.h>
+#include <mailutils/errno.h>
+#include <mailutils/mutil.h>
+
+static int
+realloc_buffer (char **bufp, size_t *bufsizep, size_t incr)
+{
+  size_t newsize = *bufsizep + incr;
+  char *newp = realloc (*bufp, newsize);
+  if (newp == NULL)
+    return 1;
+  *bufp = newp;
+  *bufsizep = newsize;
+  return 0;
+}
+
+int
+getword (char **pret, const char **pstr, int delim)
+{
+  size_t len;
+  char *ret;
+  const char *start = *pstr;
+  const char *end = strchr (start, delim);
+
+  free (*pret);
+  *pret = NULL;
+  if (!end)
+    return MU_ERR_BAD_2047_INPUT;
+  len = end - start;
+  ret = malloc (len + 1);
+  if (!ret)
+    return ENOMEM;
+  memcpy (ret, start, len);
+  ret[len] = 0;
+  *pstr = end + 1;
+  *pret = ret;
+  return 0;
+}
+    
+int
+mu_rfc2047_decode (const char *tocode, const char *input, char **ptostr)
+{
+  int status = 0;
+  const char *fromstr;
+  char *buffer;
+  size_t bufsize;
+  size_t bufpos;
+  size_t run_count = 0;
+  char *fromcode = NULL;
+  char *encoding_type = NULL;
+  char *encoded_text = NULL;
+
+#define BUFINC 128  
+#define CHKBUF(count) do {                       \
+  if (bufpos+count >= bufsize)                   \
+    {                                            \
+      size_t s = bufpos + count - bufsize;       \
+      if (s < BUFINC)                            \
+        s = BUFINC;                              \
+      if (realloc_buffer (&buffer, &bufsize, s)) \
+	{                                        \
+	  free (buffer);                         \
+          free (fromcode);                       \
+          free (encoding_type);                  \
+          free (encoded_text);                   \
+	  return ENOMEM;                         \
+	}                                        \
+     }                                           \
+ } while (0) 
+  
+  if (!input)
+    return EINVAL;
+  if (!ptostr)
+    return MU_ERR_OUT_PTR_NULL;
+
+  fromstr = input;
+
+  /* Allocate the buffer. It is assumed that encoded string is always
+     longer than it's decoded variant, so it's safe to use its length
+     as the first estimate */
+  bufsize = strlen (fromstr) + 1;
+  buffer = malloc (bufsize);
+  if (buffer == NULL)
+    return ENOMEM;
+  bufpos = 0;
+  
+  while (*fromstr)
+    {
+      if (strncmp (fromstr, "=?", 2) == 0)
+	{
+	  mu_stream_t filter = NULL;
+	  mu_stream_t in_stream = NULL;
+	  const char *filter_type = NULL;
+	  size_t nbytes = 0, size;
+	  const char *sp = fromstr + 2;
+	  char tmp[128];
+	  
+	  status = getword (&fromcode, &sp, '?');
+	  if (status)
+	    break;
+	  status = getword (&encoding_type, &sp, '?');
+	  if (status)
+	    break;
+	  status = getword (&encoded_text, &sp, '?');
+	  if (status)
+	    break;
+	  if (sp == NULL || sp[0] != '=')
+	    {
+	      status = MU_ERR_BAD_2047_INPUT;
+	      break;
+	    }
+      
+	  size = strlen (encoded_text);
+
+	  switch (encoding_type[0])
+	    {
+            case 'b':
+	    case 'B':
+	      filter_type = "base64";
+	      break;
+	     
+            case 'q': 
+	    case 'Q':
+	      filter_type = "Q";
+	      break;
+
+	    default:
+	      status = MU_ERR_BAD_2047_INPUT;
+	      break;
+	    }
+	  
+	  if (status != 0)
+	    break;
+
+	  mu_memory_stream_create (&in_stream, 0);
+	  mu_stream_write (in_stream, encoded_text, size, NULL);
+	  mu_stream_seek (in_stream, 0, MU_SEEK_SET, NULL);
+	  status = mu_decode_filter (&filter, in_stream, filter_type, fromcode,
+				     tocode);
+	  mu_stream_unref (in_stream);
+	  if (status != 0)
+	    break;
+
+	  while (mu_stream_read (filter, tmp, sizeof (tmp), &nbytes) == 0
+		 && nbytes)
+	    {
+	      CHKBUF (nbytes);
+	      memcpy (buffer + bufpos, tmp, nbytes);
+	      bufpos += nbytes;
+	    }
+
+	  mu_stream_close (filter);
+	  mu_stream_destroy (&filter);
+	  
+	  fromstr = sp + 1;
+	  run_count = 1;
+	}
+      else if (run_count)
+	{
+	  if (*fromstr == ' ' || *fromstr == '\t')
+	    {
+	      run_count++;
+	      fromstr++;
+	      continue;
+	    }
+	  else
+	    {
+	      if (--run_count)
+		{
+		  CHKBUF (run_count);
+		  memcpy (buffer + bufpos, fromstr - run_count, run_count);
+		  bufpos += run_count;
+		  run_count = 0;
+		}
+	      CHKBUF (1);
+	      buffer[bufpos++] = *fromstr++;
+	    }
+	}
+      else
+	{
+	  CHKBUF (1);
+	  buffer[bufpos++] = *fromstr++;
+	}
+    }
+  
+  if (*fromstr)
+    {
+      size_t len = strlen (fromstr);
+      CHKBUF (len);
+      memcpy (buffer + bufpos, fromstr, len);
+      bufpos += len;
+    }
+
+  CHKBUF (1);
+  buffer[bufpos++] = 0;
+  
+  free (fromcode);
+  free (encoding_type);
+  free (encoded_text);
+
+  if (status)
+    free (buffer);
+  else
+    *ptostr = realloc (buffer, bufpos);
+  return status;
+}
+
+
+/**
+   Encode a header according to RFC 2047
+   
+   @param charset
+     Charset of the text to encode
+   @param encoding
+     Requested encoding (must be "base64" or "quoted-printable")
+   @param text
+     Actual text to encode
+   @param result [OUT]
+     Encoded string
+
+   @return 0 on success
+*/
+int
+mu_rfc2047_encode (const char *charset, const char *encoding,
+		   const char *text, char **result)
+{
+  mu_stream_t input_stream;
+  mu_stream_t output_stream;
+  int rc;
+  
+  if (charset == NULL || encoding == NULL || text == NULL)
+    return EINVAL;
+
+  if (strcmp (encoding, "base64") == 0)
+    encoding = "B";
+  else if (strcmp (encoding, "quoted-printable") == 0)
+    encoding = "Q";
+  else if (encoding[1] || !strchr ("BQ", encoding[0]))
+    return MU_ERR_BAD_2047_ENCODING;
+
+  rc = mu_memory_stream_create (&input_stream, 0);
+  if (rc)
+    return rc;
+  
+  mu_stream_write (input_stream, text, strlen (text), NULL);
+  mu_stream_seek (input_stream, 0, MU_SEEK_SET, NULL);
+  rc = mu_filter_create (&output_stream, input_stream,
+			 encoding, MU_FILTER_ENCODE,
+			 MU_STREAM_READ | MU_STREAM_AUTOCLOSE);
+  if (rc == 0)
+    {
+      /* Assume strlen(qp_encoded_text) <= strlen(text) * 3 */
+      /* malloced length is composed of:
+	 "=?"  
+	 charset 
+	 "?"
+	 B or Q
+	 "?" 
+	 encoded_text
+	 "?="
+	 zero terminator */
+      
+      *result = malloc (2 + strlen (charset) + 3 + strlen (text) * 3 + 3);
+      if (*result)
+	{
+	  char *p = *result;
+	  size_t s;
+	  
+	  p += sprintf (p, "=?%s?%s?", charset, encoding);
+	  
+	  rc = mu_stream_read (output_stream,
+			       p,
+			       strlen (text) * 3, &s);
+
+	  strcpy (p + s, "?=");
+	}
+      else
+	rc = ENOMEM;
+      mu_stream_destroy (&output_stream);
+    }
+  else
+    mu_stream_destroy (&input_stream);
+
+  return rc;
+}
diff --git a/libmailutils/base/secret.c b/libmailutils/base/secret.c
new file mode 100644
index 0000000..23b4363
--- /dev/null
+++ b/libmailutils/base/secret.c
@@ -0,0 +1,134 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 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, see 
+   <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+#include <stdlib.h>
+#include <string.h>
+#include <mailutils/types.h>
+#include <mailutils/secret.h>
+#include <mailutils/errno.h>
+
+struct _mu_secret
+{
+  unsigned int refcnt;      /* Number of references to this object */
+  size_t length;            /* Secret length */
+  unsigned char *obptr;     /* Obfuscated data */
+  unsigned char *clptr;     /* Cleartext data */
+  unsigned int clref;       /* Number of references to clptr returned
+			       this far */
+};
+
+static unsigned char xchar;
+
+static void
+obfuscate (const unsigned char *input, unsigned char *output, size_t len)
+{
+  if (!xchar)
+    xchar = random () % 255;
+  while (len--)
+    *output++ = *input++ ^ xchar;
+}
+  
+int
+mu_secret_create (mu_secret_t *psec, const char *value, size_t len)
+{
+  mu_secret_t sec;
+  sec = calloc (1, sizeof (sec[0]) + 2 * (len + 1));
+  if (!sec)
+    return ENOMEM;
+  sec->obptr = (unsigned char*)(sec + 1);
+  sec->clptr = sec->obptr + len + 1;
+  obfuscate ((unsigned char *) value, sec->obptr, len);
+  sec->length = len;
+  *psec = sec;
+  mu_secret_ref (sec);
+  return 0;
+}
+
+int
+mu_secret_dup (mu_secret_t sec, mu_secret_t *newsec)
+{
+  const char *pass = mu_secret_password (sec);
+  int rc = mu_secret_create (newsec, pass, strlen (pass));
+  mu_secret_password_unref (sec);
+  return rc;
+}
+
+void
+mu_secret_ref (mu_secret_t sec)
+{
+  if (sec)
+    sec->refcnt++;
+}
+
+/* Decrement reference counter in SEC. If it falls to 0, free memory
+   allocated for SEC and return 0. Otherwise, return MU_ERR_EXISTS,
+   indicating that someone else is still holding it.
+   Return EINVAL if sec==NULL. */
+int
+mu_secret_unref (mu_secret_t sec)
+{
+  if (sec)
+    {
+      if (sec->refcnt)
+	sec->refcnt--;
+      if (sec->refcnt == 0)
+	{
+	  memset (sec->clptr, 0, sec->length);
+	  memset (sec->obptr, 0, sec->length);
+	  free (sec);
+	  return 0;
+	}
+      return MU_ERR_EXISTS;
+    }
+  return EINVAL;
+}
+
+void
+mu_secret_destroy (mu_secret_t *psec)
+{
+  if (psec && *psec && mu_secret_unref (*psec) == 0)
+    *psec = NULL;
+}
+
+const char *
+mu_secret_password (mu_secret_t sec)
+{
+  if (!sec)
+    return 0;
+  if (sec->clref++ == 0)
+    obfuscate (sec->obptr, sec->clptr, sec->length);
+  return (const char*) sec->clptr;
+}
+
+size_t
+mu_secret_length (mu_secret_t sec)
+{
+  if (!sec)
+    return 0;
+  return sec->length;
+}
+
+void
+mu_secret_password_unref (mu_secret_t sec)
+{
+  if (--sec->clref == 0)
+    memset (sec->clptr, 0, sec->length);
+}
+
diff --git a/libmailutils/base/sha1.c b/libmailutils/base/sha1.c
new file mode 100644
index 0000000..b9531b3
--- /dev/null
+++ b/libmailutils/base/sha1.c
@@ -0,0 +1,417 @@
+/* sha1.c - Functions to compute SHA1 message digest of files or
+   memory blocks according to the NIST specification FIPS-180-1.
+
+   Copyright (C) 2000, 2001, 2003, 2004, 2005, 2006, 2010 Free Software
+   Foundation, Inc.
+
+   This program 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 2, 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, see <http://www.gnu.org/licenses/>. */
+
+/* Written by Scott G. Miller
+   Credits:
+      Robert Klep <robert@ilse.nl>  -- Expansion function fix
+*/
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <mailutils/sha1.h>
+
+#include <stddef.h>
+#include <string.h>
+
+#if USE_UNLOCKED_IO
+# include "unlocked-io.h"
+#endif
+
+#ifdef WORDS_BIGENDIAN
+# define SWAP(n) (n)
+#else
+# define SWAP(n) \
+    (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24))
+#endif
+
+#define BLOCKSIZE 4096
+#if BLOCKSIZE % 64 != 0
+# error "invalid BLOCKSIZE"
+#endif
+
+/* This array contains the bytes used to pad the buffer to the next
+   64-byte boundary.  (RFC 1321, 3.1: Step 1)  */
+static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ...  */ };
+
+
+/* Take a pointer to a 160 bit block of data (five 32 bit ints) and
+   initialize it to the start constants of the SHA1 algorithm.  This
+   must be called before using hash in the call to sha1_hash.  */
+void
+sha1_init_ctx (struct sha1_ctx *ctx)
+{
+  ctx->A = 0x67452301;
+  ctx->B = 0xefcdab89;
+  ctx->C = 0x98badcfe;
+  ctx->D = 0x10325476;
+  ctx->E = 0xc3d2e1f0;
+
+  ctx->total[0] = ctx->total[1] = 0;
+  ctx->buflen = 0;
+}
+
+/* Put result from CTX in first 20 bytes following RESBUF.  The result
+   must be in little endian byte order.
+
+   IMPORTANT: On some systems it is required that RESBUF is correctly
+   aligned for a 32-bit value.  */
+void *
+sha1_read_ctx (const struct sha1_ctx *ctx, void *resbuf)
+{
+  ((uint32_t *) resbuf)[0] = SWAP (ctx->A);
+  ((uint32_t *) resbuf)[1] = SWAP (ctx->B);
+  ((uint32_t *) resbuf)[2] = SWAP (ctx->C);
+  ((uint32_t *) resbuf)[3] = SWAP (ctx->D);
+  ((uint32_t *) resbuf)[4] = SWAP (ctx->E);
+
+  return resbuf;
+}
+
+/* Process the remaining bytes in the internal buffer and the usual
+   prolog according to the standard and write the result to RESBUF.
+
+   IMPORTANT: On some systems it is required that RESBUF is correctly
+   aligned for a 32-bit value.  */
+void *
+sha1_finish_ctx (struct sha1_ctx *ctx, void *resbuf)
+{
+  /* Take yet unprocessed bytes into account.  */
+  uint32_t bytes = ctx->buflen;
+  size_t size = (bytes < 56) ? 64 / 4 : 64 * 2 / 4;
+
+  /* Now count remaining bytes.  */
+  ctx->total[0] += bytes;
+  if (ctx->total[0] < bytes)
+    ++ctx->total[1];
+
+  /* Put the 64-bit file length in *bits* at the end of the buffer.  */
+  ctx->buffer[size - 2] = SWAP ((ctx->total[1] << 3) | (ctx->total[0] >> 29));
+  ctx->buffer[size - 1] = SWAP (ctx->total[0] << 3);
+
+  memcpy (&((char *) ctx->buffer)[bytes], fillbuf, (size - 2) * 4 - bytes);
+
+  /* Process last bytes.  */
+  sha1_process_block (ctx->buffer, size * 4, ctx);
+
+  return sha1_read_ctx (ctx, resbuf);
+}
+
+/* Compute SHA1 message digest for bytes read from STREAM.  The
+   resulting message digest number will be written into the 16 bytes
+   beginning at RESBLOCK.  */
+int
+sha1_stream (FILE *stream, void *resblock)
+{
+  struct sha1_ctx ctx;
+  char buffer[BLOCKSIZE + 72];
+  size_t sum;
+
+  /* Initialize the computation context.  */
+  sha1_init_ctx (&ctx);
+
+  /* Iterate over full file contents.  */
+  while (1)
+    {
+      /* We read the file in blocks of BLOCKSIZE bytes.  One call of the
+	 computation function processes the whole buffer so that with the
+	 next round of the loop another block can be read.  */
+      size_t n;
+      sum = 0;
+
+      /* Read block.  Take care for partial reads.  */
+      while (1)
+	{
+	  n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
+
+	  sum += n;
+
+	  if (sum == BLOCKSIZE)
+	    break;
+
+	  if (n == 0)
+	    {
+	      /* Check for the error flag IFF N == 0, so that we don't
+		 exit the loop after a partial read due to e.g., EAGAIN
+		 or EWOULDBLOCK.  */
+	      if (ferror (stream))
+		return 1;
+	      goto process_partial_block;
+	    }
+
+	  /* We've read at least one byte, so ignore errors.  But always
+	     check for EOF, since feof may be true even though N > 0.
+	     Otherwise, we could end up calling fread after EOF.  */
+	  if (feof (stream))
+	    goto process_partial_block;
+	}
+
+      /* Process buffer with BLOCKSIZE bytes.  Note that
+			BLOCKSIZE % 64 == 0
+       */
+      sha1_process_block (buffer, BLOCKSIZE, &ctx);
+    }
+
+ process_partial_block:;
+
+  /* Process any remaining bytes.  */
+  if (sum > 0)
+    sha1_process_bytes (buffer, sum, &ctx);
+
+  /* Construct result in desired memory.  */
+  sha1_finish_ctx (&ctx, resblock);
+  return 0;
+}
+
+/* Compute SHA1 message digest for LEN bytes beginning at BUFFER.  The
+   result is always in little endian byte order, so that a byte-wise
+   output yields to the wanted ASCII representation of the message
+   digest.  */
+void *
+sha1_buffer (const char *buffer, size_t len, void *resblock)
+{
+  struct sha1_ctx ctx;
+
+  /* Initialize the computation context.  */
+  sha1_init_ctx (&ctx);
+
+  /* Process whole buffer but last len % 64 bytes.  */
+  sha1_process_bytes (buffer, len, &ctx);
+
+  /* Put result in desired memory area.  */
+  return sha1_finish_ctx (&ctx, resblock);
+}
+
+void
+sha1_process_bytes (const void *buffer, size_t len, struct sha1_ctx *ctx)
+{
+  /* When we already have some bits in our internal buffer concatenate
+     both inputs first.  */
+  if (ctx->buflen != 0)
+    {
+      size_t left_over = ctx->buflen;
+      size_t add = 128 - left_over > len ? len : 128 - left_over;
+
+      memcpy (&((char *) ctx->buffer)[left_over], buffer, add);
+      ctx->buflen += add;
+
+      if (ctx->buflen > 64)
+	{
+	  sha1_process_block (ctx->buffer, ctx->buflen & ~63, ctx);
+
+	  ctx->buflen &= 63;
+	  /* The regions in the following copy operation cannot overlap.  */
+	  memcpy (ctx->buffer,
+		  &((char *) ctx->buffer)[(left_over + add) & ~63],
+		  ctx->buflen);
+	}
+
+      buffer = (const char *) buffer + add;
+      len -= add;
+    }
+
+  /* Process available complete blocks.  */
+  if (len >= 64)
+    {
+#if !_STRING_ARCH_unaligned
+# define alignof(type) offsetof (struct { char c; type x; }, x)
+# define UNALIGNED_P(p) (((size_t) p) % alignof (uint32_t) != 0)
+      if (UNALIGNED_P (buffer))
+	while (len > 64)
+	  {
+	    sha1_process_block (memcpy (ctx->buffer, buffer, 64), 64, ctx);
+	    buffer = (const char *) buffer + 64;
+	    len -= 64;
+	  }
+      else
+#endif
+	{
+	  sha1_process_block (buffer, len & ~63, ctx);
+	  buffer = (const char *) buffer + (len & ~63);
+	  len &= 63;
+	}
+    }
+
+  /* Move remaining bytes in internal buffer.  */
+  if (len > 0)
+    {
+      size_t left_over = ctx->buflen;
+
+      memcpy (&((char *) ctx->buffer)[left_over], buffer, len);
+      left_over += len;
+      if (left_over >= 64)
+	{
+	  sha1_process_block (ctx->buffer, 64, ctx);
+	  left_over -= 64;
+	  memcpy (ctx->buffer, &ctx->buffer[16], left_over);
+	}
+      ctx->buflen = left_over;
+    }
+}
+
+/* --- Code below is the primary difference between md5.c and sha1.c --- */
+
+/* SHA1 round constants */
+#define K1 0x5a827999
+#define K2 0x6ed9eba1
+#define K3 0x8f1bbcdc
+#define K4 0xca62c1d6
+
+/* Round functions.  Note that F2 is the same as F4.  */
+#define F1(B,C,D) ( D ^ ( B & ( C ^ D ) ) )
+#define F2(B,C,D) (B ^ C ^ D)
+#define F3(B,C,D) ( ( B & C ) | ( D & ( B | C ) ) )
+#define F4(B,C,D) (B ^ C ^ D)
+
+/* Process LEN bytes of BUFFER, accumulating context into CTX.
+   It is assumed that LEN % 64 == 0.
+   Most of this code comes from GnuPG's cipher/sha1.c.  */
+
+void
+sha1_process_block (const void *buffer, size_t len, struct sha1_ctx *ctx)
+{
+  const uint32_t *words = buffer;
+  size_t nwords = len / sizeof (uint32_t);
+  const uint32_t *endp = words + nwords;
+  uint32_t x[16];
+  uint32_t a = ctx->A;
+  uint32_t b = ctx->B;
+  uint32_t c = ctx->C;
+  uint32_t d = ctx->D;
+  uint32_t e = ctx->E;
+
+  /* First increment the byte count.  RFC 1321 specifies the possible
+     length of the file up to 2^64 bits.  Here we only compute the
+     number of bytes.  Do a double word increment.  */
+  ctx->total[0] += len;
+  if (ctx->total[0] < len)
+    ++ctx->total[1];
+
+#define rol(x, n) (((x) << (n)) | ((uint32_t) (x) >> (32 - (n))))
+
+#define M(I) ( tm =   x[I&0x0f] ^ x[(I-14)&0x0f] \
+		    ^ x[(I-8)&0x0f] ^ x[(I-3)&0x0f] \
+	       , (x[I&0x0f] = rol(tm, 1)) )
+
+#define R(A,B,C,D,E,F,K,M)  do { E += rol( A, 5 )     \
+				      + F( B, C, D )  \
+				      + K	      \
+				      + M;	      \
+				 B = rol( B, 30 );    \
+			       } while(0)
+
+  while (words < endp)
+    {
+      uint32_t tm;
+      int t;
+      for (t = 0; t < 16; t++)
+	{
+	  x[t] = SWAP (*words);
+	  words++;
+	}
+
+      R( a, b, c, d, e, F1, K1, x[ 0] );
+      R( e, a, b, c, d, F1, K1, x[ 1] );
+      R( d, e, a, b, c, F1, K1, x[ 2] );
+      R( c, d, e, a, b, F1, K1, x[ 3] );
+      R( b, c, d, e, a, F1, K1, x[ 4] );
+      R( a, b, c, d, e, F1, K1, x[ 5] );
+      R( e, a, b, c, d, F1, K1, x[ 6] );
+      R( d, e, a, b, c, F1, K1, x[ 7] );
+      R( c, d, e, a, b, F1, K1, x[ 8] );
+      R( b, c, d, e, a, F1, K1, x[ 9] );
+      R( a, b, c, d, e, F1, K1, x[10] );
+      R( e, a, b, c, d, F1, K1, x[11] );
+      R( d, e, a, b, c, F1, K1, x[12] );
+      R( c, d, e, a, b, F1, K1, x[13] );
+      R( b, c, d, e, a, F1, K1, x[14] );
+      R( a, b, c, d, e, F1, K1, x[15] );
+      R( e, a, b, c, d, F1, K1, M(16) );
+      R( d, e, a, b, c, F1, K1, M(17) );
+      R( c, d, e, a, b, F1, K1, M(18) );
+      R( b, c, d, e, a, F1, K1, M(19) );
+      R( a, b, c, d, e, F2, K2, M(20) );
+      R( e, a, b, c, d, F2, K2, M(21) );
+      R( d, e, a, b, c, F2, K2, M(22) );
+      R( c, d, e, a, b, F2, K2, M(23) );
+      R( b, c, d, e, a, F2, K2, M(24) );
+      R( a, b, c, d, e, F2, K2, M(25) );
+      R( e, a, b, c, d, F2, K2, M(26) );
+      R( d, e, a, b, c, F2, K2, M(27) );
+      R( c, d, e, a, b, F2, K2, M(28) );
+      R( b, c, d, e, a, F2, K2, M(29) );
+      R( a, b, c, d, e, F2, K2, M(30) );
+      R( e, a, b, c, d, F2, K2, M(31) );
+      R( d, e, a, b, c, F2, K2, M(32) );
+      R( c, d, e, a, b, F2, K2, M(33) );
+      R( b, c, d, e, a, F2, K2, M(34) );
+      R( a, b, c, d, e, F2, K2, M(35) );
+      R( e, a, b, c, d, F2, K2, M(36) );
+      R( d, e, a, b, c, F2, K2, M(37) );
+      R( c, d, e, a, b, F2, K2, M(38) );
+      R( b, c, d, e, a, F2, K2, M(39) );
+      R( a, b, c, d, e, F3, K3, M(40) );
+      R( e, a, b, c, d, F3, K3, M(41) );
+      R( d, e, a, b, c, F3, K3, M(42) );
+      R( c, d, e, a, b, F3, K3, M(43) );
+      R( b, c, d, e, a, F3, K3, M(44) );
+      R( a, b, c, d, e, F3, K3, M(45) );
+      R( e, a, b, c, d, F3, K3, M(46) );
+      R( d, e, a, b, c, F3, K3, M(47) );
+      R( c, d, e, a, b, F3, K3, M(48) );
+      R( b, c, d, e, a, F3, K3, M(49) );
+      R( a, b, c, d, e, F3, K3, M(50) );
+      R( e, a, b, c, d, F3, K3, M(51) );
+      R( d, e, a, b, c, F3, K3, M(52) );
+      R( c, d, e, a, b, F3, K3, M(53) );
+      R( b, c, d, e, a, F3, K3, M(54) );
+      R( a, b, c, d, e, F3, K3, M(55) );
+      R( e, a, b, c, d, F3, K3, M(56) );
+      R( d, e, a, b, c, F3, K3, M(57) );
+      R( c, d, e, a, b, F3, K3, M(58) );
+      R( b, c, d, e, a, F3, K3, M(59) );
+      R( a, b, c, d, e, F4, K4, M(60) );
+      R( e, a, b, c, d, F4, K4, M(61) );
+      R( d, e, a, b, c, F4, K4, M(62) );
+      R( c, d, e, a, b, F4, K4, M(63) );
+      R( b, c, d, e, a, F4, K4, M(64) );
+      R( a, b, c, d, e, F4, K4, M(65) );
+      R( e, a, b, c, d, F4, K4, M(66) );
+      R( d, e, a, b, c, F4, K4, M(67) );
+      R( c, d, e, a, b, F4, K4, M(68) );
+      R( b, c, d, e, a, F4, K4, M(69) );
+      R( a, b, c, d, e, F4, K4, M(70) );
+      R( e, a, b, c, d, F4, K4, M(71) );
+      R( d, e, a, b, c, F4, K4, M(72) );
+      R( c, d, e, a, b, F4, K4, M(73) );
+      R( b, c, d, e, a, F4, K4, M(74) );
+      R( a, b, c, d, e, F4, K4, M(75) );
+      R( e, a, b, c, d, F4, K4, M(76) );
+      R( d, e, a, b, c, F4, K4, M(77) );
+      R( c, d, e, a, b, F4, K4, M(78) );
+      R( b, c, d, e, a, F4, K4, M(79) );
+
+      a = ctx->A += a;
+      b = ctx->B += b;
+      c = ctx->C += c;
+      d = ctx->D += d;
+      e = ctx->E += e;
+    }
+}
diff --git a/libmailutils/base/ticket.c b/libmailutils/base/ticket.c
new file mode 100644
index 0000000..4e12b56
--- /dev/null
+++ b/libmailutils/base/ticket.c
@@ -0,0 +1,244 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 2004, 2005, 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, see
+   <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <errno.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <termios.h>
+
+#include <mailutils/mutil.h>
+#include <mailutils/errno.h>
+#include <mailutils/secret.h>
+#include <mailutils/sys/auth.h>
+
+static void
+echo_off (struct termios *stored_settings)
+{
+  struct termios new_settings;
+  tcgetattr (0, stored_settings);
+  new_settings = *stored_settings;
+  new_settings.c_lflag &= (~ECHO);
+  tcsetattr (0, TCSANOW, &new_settings);
+}
+
+static void
+echo_on (struct termios *stored_settings)
+{
+  tcsetattr (0, TCSANOW, stored_settings);
+}
+
+int
+mu_ticket_create (mu_ticket_t *pticket, void *owner)
+{
+  mu_ticket_t ticket;
+  if (pticket == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+  ticket = calloc (1, sizeof (*ticket));
+  if (ticket == NULL)
+    return ENOMEM;
+  ticket->owner = owner;
+  mu_ticket_ref (ticket);
+  *pticket = ticket;
+  return 0;
+}
+
+int
+mu_ticket_ref (mu_ticket_t ticket)
+{
+  if (!ticket)
+    return EINVAL;
+  ticket->refcnt++;
+  return 0;
+}
+  
+int
+mu_ticket_unref (mu_ticket_t ticket)
+{
+  if (!ticket)
+    return EINVAL;
+  if (ticket->refcnt)
+    ticket->refcnt--;
+  if (ticket->refcnt == 0)
+    {
+      if (ticket->plain)
+	free (ticket->plain);
+      if (ticket->secret)
+	mu_secret_destroy (&ticket->secret);
+      if (ticket->_destroy)
+	ticket->_destroy (ticket);
+      free (ticket);
+      return 0;
+    }
+  return MU_ERR_EXISTS;
+}
+      
+void
+mu_ticket_destroy (mu_ticket_t *pticket)
+{
+  if (pticket && *pticket && mu_ticket_unref (*pticket) == 0)
+    *pticket = NULL;
+}
+
+int
+mu_ticket_set_destroy (mu_ticket_t ticket,
+		       void (*_destroy) (mu_ticket_t), void *owner)
+{
+  if (ticket == NULL)
+    return EINVAL;
+  if (ticket->owner != owner)
+    return EACCES;
+  ticket->_destroy = _destroy;
+  return 0;
+}
+
+void *
+mu_ticket_get_owner (mu_ticket_t ticket)
+{
+  return (ticket) ? ticket->owner : NULL;
+}
+
+int
+mu_ticket_set_get_cred (mu_ticket_t ticket,
+			int  (*_get_cred) (mu_ticket_t, mu_url_t,
+					   const char *,
+					   char **, mu_secret_t *),
+			void *owner)
+{
+  if (ticket == NULL)
+    return EINVAL;
+  if (ticket->owner != owner)
+    return EACCES;
+  ticket->_get_cred = _get_cred;
+  return 0;
+}
+
+int
+mu_ticket_set_secret (mu_ticket_t ticket, mu_secret_t secret)
+{
+  if (ticket == NULL)
+    return EINVAL;
+  if (ticket->secret)
+    mu_secret_unref (ticket->secret);
+  mu_secret_ref (secret);
+  ticket->secret = secret;
+  return 0;
+}
+
+int
+mu_ticket_set_plain (mu_ticket_t ticket, const char *text)
+{
+  if (ticket == NULL)
+    return EINVAL;
+  if (ticket->plain)
+    free (ticket->plain);
+  ticket->plain = strdup (text);
+  if (!ticket->plain)
+    return ENOMEM;
+  return 0;
+}
+
+int
+mu_ticket_get_cred (mu_ticket_t ticket, mu_url_t url, const char *challenge,
+		    char **pplain, mu_secret_t *psec)
+{
+  int rc = 0;
+  char arg[256];
+  
+  if (ticket == NULL || (pplain && psec))
+    return EINVAL;
+  if (pplain == NULL && psec == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+
+  if (ticket->_get_cred)
+    {
+      int res = ticket->_get_cred (ticket, url, challenge, pplain, psec);
+      if (res == 0)
+	return res;
+    }
+
+  if (pplain && ticket->plain)
+    {
+      *pplain = strdup (ticket->plain);
+      if (!*pplain)
+	return ENOMEM;
+    }
+  
+  if (psec && ticket->secret)
+    {
+      mu_secret_ref (ticket->secret);
+      *psec = ticket->secret;
+      return 0;
+    }
+  
+  if (isatty (fileno (stdin)))
+    {
+      struct termios stored_settings;
+      int echo = pplain != NULL;
+
+      printf ("%s", challenge);
+      fflush (stdout);
+      if (!echo)
+	echo_off (&stored_settings);
+      fgets (arg, sizeof (arg), stdin);
+      if (!echo)
+	{
+	  echo_on (&stored_settings);
+	  putchar ('\n');
+	  fflush (stdout);
+	}
+      arg [strlen (arg) - 1] = '\0'; /* nuke the trailing line.  */
+    }
+  else
+    return MU_ERR_FAILURE;
+  
+  if (pplain)
+    {
+      *pplain = strdup (arg);
+      if (!*pplain)
+	return ENOMEM;
+    }
+  else
+    rc = mu_secret_create (psec, arg, strlen (arg));
+  return rc;
+}
+
+void *
+mu_ticket_get_data (mu_ticket_t ticket)
+{
+  if (!ticket)
+    return NULL;
+  return ticket->data;
+}
+
+int
+mu_ticket_set_data (mu_ticket_t ticket, void *data, void *owner)
+{
+  if (ticket == NULL)
+    return EINVAL;
+  if (ticket->owner != owner)
+    return EACCES;
+  ticket->data = data;
+  return 0;
+}
diff --git a/libmailutils/base/url.c b/libmailutils/base/url.c
new file mode 100644
index 0000000..554eb8a
--- /dev/null
+++ b/libmailutils/base/url.c
@@ -0,0 +1,1139 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 2007, 2008, 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, see 
+   <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <ctype.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#ifdef HAVE_STRINGS_H
+# include <strings.h>
+#endif
+
+#include <mailutils/mutil.h>
+#include <mailutils/errno.h>
+#include <mailutils/argcv.h>
+#include <mailutils/secret.h>
+#include <mailutils/cctype.h>
+#include <mailutils/cstr.h>
+#include <mailutils/sys/url.h>
+
+#define AC2(a,b) a ## b
+#define AC4(a,b,c,d) a ## b ## c ## d
+
+static int url_parse0 (mu_url_t, char *, size_t *poff, int *decode);
+
+static int
+parse_query (const char *query,
+	     char *delim,
+	     int *pargc, char ***pargv, const char **pend)
+{
+  size_t count, i;
+  char **v;
+  const char *p;
+
+  for (p = query, count = 0; ; count++)
+    {
+      size_t len = strcspn (p, delim);
+      p += len;
+      if (!*p || *p == delim[1])
+	break;
+      p++;
+    }
+
+  if (pend)
+    *pend = p;
+  if (p == query)
+    return 0;
+  count++;
+
+  v = calloc (count + 1, sizeof (v[0]));
+  for (i = 0, p = query; i < count; i++)
+    {
+      size_t len = strcspn (p, delim);
+      v[i] = mu_url_decode_len (p, len);
+      if (v[i] == NULL)
+	{
+	  mu_argcv_free (i, v);
+	  return 1;
+	}
+      p += len + 1;
+    }
+  v[i] = NULL;
+
+  *pargc = count;
+  *pargv = v;
+  return 0;
+}
+
+int
+mu_url_create (mu_url_t *purl, const char *name)
+{
+  mu_url_t url = calloc (1, sizeof (*url));
+  if (url == NULL)
+    return ENOMEM;
+
+  url->name = strdup (name);
+  if (url->name == NULL)
+    {
+      free (url);
+      return ENOMEM;
+    }
+  *purl = url;
+  return 0;
+}
+
+static char **
+argcv_copy (size_t argc, char **argv)
+{
+  size_t i;
+  char **nv = calloc (argc + 1, sizeof (nv[0]));
+  if (!nv)
+    return NULL;
+  for (i = 0; i < argc; i++)
+    if ((nv[i] = strdup (argv[i])) == NULL)
+      {
+	mu_argcv_free (i, nv);
+	free (nv);
+	return NULL;
+      }
+  return nv;
+}
+
+static int
+mu_url_copy0 (mu_url_t old_url, mu_url_t new_url)
+{
+  const char *str;
+  size_t argc;
+  char **argv;
+  int rc;
+  mu_secret_t sec;
+
+#define URLCOPY(what)						\
+  do								\
+    {								\
+      rc = AC2(mu_url_sget_,what) (old_url, &str);		\
+      if (rc == 0)						\
+	{							\
+	  if ((new_url->what = strdup (str)) == NULL)		\
+	    return ENOMEM;					\
+	}							\
+      else if (rc != MU_ERR_NOENT)				\
+	return rc;						\
+    }								\
+  while (0);
+
+  URLCOPY (scheme);
+  URLCOPY (user);
+
+  rc = mu_url_get_secret (old_url, &sec);
+  if (rc == MU_ERR_NOENT)
+    new_url->secret = NULL;
+  else if (rc)
+    return rc;
+  else
+    {
+      rc = mu_secret_dup (sec, &new_url->secret);
+      if (rc)
+	return rc;
+    }
+  
+  URLCOPY (auth);
+  URLCOPY (host);
+  new_url->port = old_url->port;
+  URLCOPY (path);
+
+  rc = mu_url_sget_fvpairs (old_url, &argc, &argv);
+  if (rc == 0 && argc)
+    {
+      if ((new_url->fvpairs = argcv_copy (argc, argv)) == NULL)
+	return ENOMEM;
+      new_url->fvcount = argc;
+    }
+
+  rc = mu_url_sget_query (old_url, &argc, &argv);
+  if (rc == 0 && argc)
+    {
+      if ((new_url->qargv = argcv_copy (argc, argv)) == NULL)
+	return ENOMEM;
+      new_url->qargc = argc;
+    }
+  return 0;
+#undef URLCOPY
+}
+
+int
+mu_url_dup (mu_url_t old_url, mu_url_t *new_url)
+{
+  mu_url_t url;
+  int rc = mu_url_create (&url, mu_url_to_string (old_url));
+
+  if (rc)
+    return rc;
+
+  rc = mu_url_copy0 (old_url, url);
+  if (rc == 0)
+    *new_url = url;
+  else
+    mu_url_destroy (&url);
+  return rc;
+}
+
+int
+mu_url_uplevel (mu_url_t url, mu_url_t *upurl)
+{
+  int rc;
+  char *p;
+  mu_url_t new_url;
+
+  if (url->_uplevel)
+    return url->_uplevel (url, upurl);
+
+  if (!url->path)
+    return MU_ERR_NOENT;
+  p = strrchr (url->path, '/');
+
+  rc = mu_url_dup (url, &new_url);
+  if (rc == 0)
+    {
+      if (!p || p == url->path)
+	{
+	  free (new_url->path);
+	  new_url->path = NULL;
+	}
+      else
+	{
+	  size_t size = p - url->path;
+	  new_url->path = realloc (new_url->path, size + 1);
+	  if (!new_url->path)
+	    {
+	      mu_url_destroy (&new_url);
+	      return ENOMEM;
+	    }
+	  memcpy (new_url->path, url->path, size);
+	  new_url->path[size] = 0;
+	}
+      *upurl = new_url;
+    }
+  return rc;
+}
+
+void
+mu_url_destroy (mu_url_t * purl)
+{
+  if (purl && *purl)
+    {
+      mu_url_t url = (*purl);
+
+      if (url->_destroy)
+	url->_destroy (url);
+
+      if (url->name)
+	free (url->name);
+
+      if (url->scheme)
+	free (url->scheme);
+
+      if (url->user)
+	free (url->user);
+
+      mu_secret_destroy (&url->secret);
+
+      if (url->auth)
+	free (url->auth);
+
+      if (url->host)
+	free (url->host);
+
+      if (url->path)
+	free (url->path);
+
+      if (url->fvcount)
+	mu_argcv_free (url->fvcount, url->fvpairs);
+
+      mu_argcv_free (url->qargc, url->qargv);
+
+      free (url);
+
+      *purl = NULL;
+    }
+}
+
+int
+mu_url_parse (mu_url_t url)
+{
+  int err = 0;
+  char *n = NULL;
+  struct _mu_url u;
+  size_t pstart;
+  mu_secret_t newsec;
+  int want_decode;
+  
+  if (!url || !url->name)
+    return EINVAL;
+
+  memset (&u, 0, sizeof u);
+  /* can't have been parsed already */
+  if (url->scheme || url->user || url->secret || url->auth ||
+      url->host || url->path || url->qargc)
+    return EINVAL;
+
+  n = strdup (url->name);
+
+  if (!n)
+    return ENOMEM;
+
+  err = url_parse0 (&u, n, &pstart, &want_decode);
+
+  if (!err)
+    {
+      if (u.secret)
+	{
+	  /* Obfuscate the password */
+#define PASS_REPL "***"
+#define PASS_REPL_LEN (sizeof (PASS_REPL) - 1)
+	  size_t plen = mu_secret_length (u.secret);
+	  size_t nlen = strlen (url->name);
+	  size_t len = nlen - plen + PASS_REPL_LEN + 1;
+	  char *newname;
+
+	  memset (url->name + pstart, 0, plen);
+	  newname = realloc (url->name, len);
+	  if (!newname)
+	    goto CLEANUP;
+	  memmove (newname + pstart + PASS_REPL_LEN, newname + pstart + plen,
+		   nlen - (pstart + plen) + 1);
+	  memcpy (newname + pstart, PASS_REPL, PASS_REPL_LEN);
+	  url->name = newname;
+	}
+
+      /* Dup the strings we found. We wouldn't have to do this
+	 if we did a single alloc of the source url name, and
+	 kept it around. It's also a good time to do hex decoding,
+	 though.
+       */
+
+#define UALLOC(X)							\
+      if (u.X && u.X[0] &&						\
+	  !(url->X = (want_decode ? mu_url_decode (u.X) : strdup (u.X)))) \
+	{								\
+	  err = ENOMEM;							\
+	  goto CLEANUP;							\
+	}								\
+      else								\
+	{								\
+	  /* Set zero-length strings to NULL. */			\
+	  u.X = NULL;							\
+	}
+
+      UALLOC (scheme);
+      UALLOC (user);
+
+      if (u.secret)
+	{
+	  char *pass = mu_url_decode (mu_secret_password (u.secret));
+	  err = mu_secret_create (&newsec, pass, strlen (pass));
+	  memset (pass, 0, strlen (pass));
+	  mu_secret_destroy (&u.secret);
+	  if (err)
+	    goto CLEANUP;
+
+	  url->secret = newsec;
+	}
+
+      UALLOC (auth);
+      UALLOC (host);
+      UALLOC (path);
+
+#undef UALLOC
+      url->fvcount = u.fvcount;
+      url->fvpairs = u.fvpairs;
+
+      url->qargc = u.qargc;
+      url->qargv = u.qargv;
+
+      url->port = u.port;
+    }
+
+CLEANUP:
+  memset (n, 0, strlen (n));
+  free (n);
+
+  if (err)
+    {
+#define UFREE(X) if (X) { free(X); X = 0; }
+
+      UFREE (url->scheme);
+      UFREE (url->user);
+      mu_secret_destroy (&u.secret);
+      UFREE (url->auth);
+      UFREE (url->host);
+      UFREE (url->path);
+      mu_argcv_free (url->fvcount, url->fvpairs);
+      mu_argcv_free (url->qargc, url->qargv);
+#undef UFREE
+    }
+
+  return err;
+}
+
+/*
+
+Syntax, condensed from RFC 1738, and extended with the ;auth=
+of RFC 2384 (for POP) and RFC 2192 (for IMAP):
+
+url =
+    scheme ":" [ "//"
+
+    [ user [ ( ":" password ) | ( ";auth=" auth ) ] "@" ]
+
+    host [ ":" port ]
+
+    [ ( "/" urlpath ) | ( "?" query ) ] ]
+
+All hell will break loose in this parser if the user/pass/auth
+portion is missing, and the urlpath has any @ or : characters
+in it. A imap mailbox, say, named after the email address of
+the person the mail is from:
+
+  imap://imap.uniserve.com/alain@qnx.com
+
+Is this required to be % quoted, though? I hope so!
+
+*/
+
+static int
+url_parse0 (mu_url_t u, char *name, size_t *poff, int *decode)
+{
+  char *start = name;
+  char *p;			/* pointer into name */
+
+  /* reject the obvious */
+  if (name == NULL)
+    return EINVAL;
+
+  if (name[0] == '/')
+    {
+      u->scheme = "file";
+      *decode = 0;
+    }
+  else if (name[0] == '|')
+    {
+      int rc;
+      u->scheme = "prog";
+      *decode = 0;
+      rc = mu_argcv_get (name + 1, NULL, NULL, &u->qargc, &u->qargv);
+      if (rc == 0)
+	{
+	  u->path = strdup (u->qargv[0]);
+	  if (!u->path)
+	    rc = ENOMEM;
+	}
+      return rc;
+    }
+  else
+    {
+      *decode = 1;
+      /* Parse out the SCHEME. */
+      p = strchr (name, ':');
+      if (p == NULL)
+	return MU_ERR_PARSE;
+
+      *p++ = 0;
+
+      u->scheme = name;
+
+      /* RFC 1738, section 2.1, lower the scheme case */
+      for (; name < p; name++)
+	*name = mu_tolower (*name);
+
+      name = p;
+    }
+
+  /* Check for nothing following the scheme. */
+  if (!*name)
+    return 0;
+
+  if (strncmp (name, "//", 2) == 0)
+    {
+      name += 2;
+
+      if (name[0] == '/')
+	{
+	  u->path = name;
+	  p = u->path + strcspn (u->path, ";?");
+	}
+      else
+	{
+	  /* Split into LHS and RHS of the '@', and then parse each side. */
+	  u->host = strchr (name, '@');
+	  if (u->host == NULL)
+	    u->host = name;
+	  else
+	    {
+	      char *pass = NULL;
+
+	      /* Parse the LHS into an identification/authentication pair. */
+	      *u->host++ = 0;
+
+	      u->user = name;
+
+	      /* Try to split the user into a:
+		 <user>:<password>
+		 or
+		 <user>:<password>;AUTH=<auth>
+	      */
+
+	      for (; *name; name++)
+		{
+		  if (*name == ':')
+		    {
+		      *name++ = 0;
+		      pass = name;
+		      *poff = pass - start;
+		    }
+		  else if (*name == ';')
+		    {
+		      /* Make sure it's the auth token. */
+		      if (mu_c_strncasecmp (name + 1, "auth=", 5) == 0)
+			{
+			  *name++ = 0;
+			  name += 5;
+			  u->auth = name;
+			  break;
+			}
+		    }
+		}
+
+	      if (pass)
+		{
+		  if (mu_secret_create (&u->secret, pass, strlen (pass)))
+		    return ENOMEM;
+		  else
+		    /* Obfuscate password */
+		    memset (pass, 0, strlen (pass));
+		}
+	    }
+
+	  /* Parse the host and port from the RHS. */
+	  p = strchr (u->host, ':');
+	  if (p)
+	    {
+	      *p++ = 0;
+	      u->port = strtol (p, &p, 10);
+
+	      /* Check for garbage after the port: we should be on the start
+		 of a path, a query, or at the end of the string. */
+	      if (*p && strcspn (p, "/?") != 0)
+		return MU_ERR_PARSE;
+	    }
+	  else
+	    p = u->host + strcspn (u->host, ";/?");
+	}
+    }
+  else
+    {
+      u->path = name;
+      p = u->path + strcspn (u->path, ";?");
+    }
+
+  /* Either way, if we're not at a nul, we're at a path or query. */
+  if (u->path == NULL && *p == '/')
+    {
+      /* found a path */
+      *p++ = 0;
+      u->path = p;
+      p = u->path + strcspn (u->path, ";?");
+    }
+
+  if (*p == ';')
+    {
+      *p++ = 0;
+      if (parse_query (p, ";?", &u->fvcount, &u->fvpairs, (const char **)&p))
+	return ENOMEM;
+    }
+
+  if (*p == '?')
+    {
+      /* found a query */
+      *p++ = 0;
+      if (parse_query (p, "&", &u->qargc, &u->qargv, NULL))
+	return ENOMEM;
+    }
+
+  return 0;
+}
+
+
+/* General accessors: */
+#define ACCESSOR(action,field) AC4(mu_url_,action,_,field)
+
+#define DECL_SGET(field)						  \
+int									  \
+ACCESSOR(sget,field) (mu_url_t url, char const **sptr)	                  \
+{									  \
+  if (url == NULL)							  \
+    return EINVAL;							  \
+  if (!url->field)							  \
+    {									  \
+      if (url->AC2(_get_,field))					  \
+	{								  \
+	  size_t n;							  \
+	  char *buf;							  \
+									  \
+	  int status = url->AC2(_get_,field) (url, NULL, 0, &n);	  \
+	  if (status)							  \
+	    return status;						  \
+									  \
+	  buf = malloc (n + 1);						  \
+	  if (!buf)							  \
+	    return ENOMEM;						  \
+									  \
+	  status = url->AC2(_get_,field) (url, buf, n + 1, NULL);	  \
+	  if (status)				          \
+	    return status;						  \
+									  \
+	  if (buf[0])                                                     \
+	    {                                                             \
+	       url->field = mu_url_decode (buf);			  \
+	       free (buf);						  \
+	    }                                                             \
+	  else                                                            \
+	    url->field = buf;                                             \
+	  if (!url->field)						  \
+	    return ENOMEM;						  \
+	}								  \
+      else								  \
+	return MU_ERR_NOENT;			                          \
+    }									  \
+  *sptr = url->field;							  \
+  return 0;								  \
+}
+
+#define DECL_GET(field)							  \
+int									  \
+ACCESSOR(get,field) (mu_url_t url, char *buf, size_t len, size_t *n)      \
+{									  \
+  size_t i;								  \
+  const char *str;							  \
+  int status = ACCESSOR(sget, field) (url, &str);			  \
+									  \
+  if (status)								  \
+    return status;							  \
+									  \
+  i = mu_cpystr (buf, str, len);					  \
+  if (n)								  \
+    *n = i;								  \
+  return 0;								  \
+}
+
+#define DECL_AGET(field)						  \
+int									  \
+ACCESSOR(aget, field) (mu_url_t url, char **buf)	                  \
+{									  \
+  const char *str;							  \
+  int status = ACCESSOR(sget, field) (url, &str);			  \
+									  \
+  if (status)								  \
+    return status;							  \
+									  \
+  if (str)								  \
+    {									  \
+      *buf = strdup (str);						  \
+      if (!*buf)							  \
+	status = ENOMEM;						  \
+    }									  \
+  else									  \
+    *buf = NULL;							  \
+  return status;							  \
+}
+
+#define DECL_CMP(field)							  \
+int									  \
+ACCESSOR(is_same,field) (mu_url_t url1, mu_url_t url2)		          \
+{									  \
+  const char *s1, *s2;							  \
+  int status1, status2;							  \
+									  \
+  status1 = ACCESSOR(sget, field) (url1, &s1);				  \
+  if (status1 && status1 != MU_ERR_NOENT)				  \
+    return 0;								  \
+  status2 = ACCESSOR(sget, field) (url2, &s2);				  \
+  if (status2 && status2 != MU_ERR_NOENT)				  \
+    return 0;								  \
+									  \
+  if (status1 || status2)						  \
+    return status1 == status2; /* Both fields are missing */		  \
+  return mu_c_strcasecmp (s1, s2) == 0;					  \
+}
+
+#define DECL_ACCESSORS(field)			                          \
+DECL_SGET(field)				                          \
+DECL_GET(field)					                          \
+DECL_AGET(field)                                                          \
+DECL_CMP(field)
+
+
+/* Declare particular accessors */
+DECL_ACCESSORS (scheme)
+DECL_ACCESSORS (user)
+DECL_ACCESSORS (auth)
+DECL_ACCESSORS (host)
+DECL_ACCESSORS (path)
+
+int
+mu_url_get_secret (const mu_url_t url, mu_secret_t *psecret)
+{
+  if (url->_get_secret)
+    return url->_get_secret (url, psecret);
+  if (url->secret == NULL)
+    return MU_ERR_NOENT;
+  mu_secret_ref (url->secret);
+  *psecret = url->secret;
+  return 0;
+}
+
+int
+mu_url_sget_query (const mu_url_t url, size_t *qc, char ***qv)
+{
+  if (url == NULL)
+    return EINVAL;
+  /* See FIXME below */
+  *qc = url->qargc;
+  *qv = url->qargv;
+  return 0;
+}
+
+int
+mu_url_aget_query (const mu_url_t url, size_t *qc, char ***qv)
+{
+  size_t qargc, i;
+  char **qargv;
+  char **qcopy;
+
+  int rc = mu_url_sget_fvpairs (url, &qargc, &qargv);
+  if (rc)
+    return rc;
+
+  qcopy = calloc (qargc + 1, sizeof (qcopy[0]));
+  if (!qcopy)
+    return errno;
+  for (i = 0; i < qargc; i++)
+    {
+      if (!(qcopy[i] = strdup (qargv[i])))
+	{
+	  mu_argcv_free (i, qcopy);
+	  return errno;
+	}
+    }
+  qcopy[i] = NULL;
+  *qc = qargc;
+  *qv = qcopy;
+  return 0;
+}
+
+/* field-value pairs accessors */
+int
+mu_url_sget_fvpairs (const mu_url_t url, size_t *fvc, char ***fvp)
+{
+  if (url == NULL)
+    return EINVAL;
+  /* FIXME: no _get_fvpairs method, but the method stuff needs to be rewritten
+     anyway */
+  *fvc = url->fvcount;
+  *fvp = url->fvpairs;
+  return 0;
+}
+
+int
+mu_url_aget_fvpairs (const mu_url_t url, size_t *pfvc, char ***pfvp)
+{
+  size_t fvc, i;
+  char **fvp;
+  char **fvcopy;
+
+  int rc = mu_url_sget_fvpairs (url, &fvc, &fvp);
+  if (rc)
+    return rc;
+
+  fvcopy = calloc (fvc + 1, sizeof (fvcopy[0]));
+  if (!fvcopy)
+    return errno;
+  for (i = 0; i < fvc; i++)
+    {
+      if (!(fvcopy[i] = strdup (fvp[i])))
+	{
+	  mu_argcv_free (i, fvcopy);
+	  return errno;
+	}
+    }
+  fvcopy[i] = NULL;
+  *pfvc = fvc;
+  *pfvp = fvcopy;
+  return 0;
+}
+
+int
+mu_url_get_port (const mu_url_t url, long *pport)
+{
+  if (url == NULL)
+    return EINVAL;
+  if (url->_get_port)
+    return url->_get_port (url, pport);
+  *pport = url->port;
+  return 0;
+}
+
+const char *
+mu_url_to_string (const mu_url_t url)
+{
+  if (url == NULL || url->name == NULL)
+    return "";
+  return url->name;
+}
+
+int
+mu_url_set_scheme (mu_url_t url, const char *scheme)
+{
+  char *p;
+  if (!url || !scheme)
+    return EINVAL;
+  p = realloc (url->scheme, strlen (scheme) + 1);
+  if (!p)
+    return ENOMEM;
+  strcpy (url->scheme, scheme);
+  return 0;
+}
+
+int
+mu_url_is_scheme (mu_url_t url, const char *scheme)
+{
+  if (url && scheme && url->scheme 
+      && mu_c_strcasecmp (url->scheme, scheme) == 0)
+    return 1;
+
+  return 0;
+}
+
+int
+mu_url_is_same_port (mu_url_t url1, mu_url_t url2)
+{
+  long p1 = 0, p2 = 0;
+
+  mu_url_get_port (url1, &p1);
+  mu_url_get_port (url2, &p2);
+  return (p1 == p2);
+}
+
+/* From RFC 1738, section 2.2 */
+char *
+mu_url_decode_len (const char *s, size_t len)
+{
+  char *d;
+  const char *eos = s + len;
+  int i;
+
+  d = malloc (len + 1);
+  if (!d)
+    return NULL;
+
+  for (i = 0; s < eos; i++)
+    {
+      if (*s != '%')
+	{
+	  d[i] = *s;
+	  s++;
+	}
+      else
+	{
+	  unsigned long ul = 0;
+
+	  s++;
+
+	  /* don't check return value, it's correctly coded, or it's not,
+	     in which case we just skip the garbage, this is a decoder,
+	     not an AI project */
+
+	  mu_hexstr2ul (&ul, s, 2);
+
+	  s += 2;
+
+	  d[i] = (char) ul;
+	}
+    }
+
+  d[i] = 0;
+
+  return d;
+}
+
+char *
+mu_url_decode (const char *s)
+{
+  return mu_url_decode_len (s, strlen (s));
+}
+
+static int
+defined (const char *s)
+{
+  if (s && strcmp ("*", s) != 0)
+    return 1;
+  return 0;
+}
+
+int
+mu_url_is_ticket (mu_url_t ticket, mu_url_t url)
+{
+  if (!ticket || !url)
+    return 0;
+
+  /* If ticket has a scheme, host, port, or path, then the queries
+     equivalent must be defined and match. */
+  if (defined (ticket->scheme))
+    {
+      if (!url->scheme || mu_c_strcasecmp (ticket->scheme, url->scheme) != 0)
+	return 0;
+    }
+  if (defined (ticket->host))
+    {
+      if (!url->host || mu_c_strcasecmp (ticket->host, url->host) != 0)
+	return 0;
+    }
+  if (ticket->port && ticket->port != url->port)
+    return 0;
+  /* If ticket has a user or pass, but url doesn't, that's OK, we were
+     urling for this info. But if url does have a user/pass, it
+     must match the ticket. */
+  if (url->user)
+    {
+      if (defined (ticket->user) && strcmp (ticket->user, url->user) != 0)
+	return 0;
+    }
+
+  /* Guess it matches. */
+  return 1;
+}
+
+int
+mu_url_init (mu_url_t url, int port, const char *scheme)
+{
+  int status = 0;
+
+  url->_destroy = NULL;
+
+  status = mu_url_parse (url);
+  if (status)
+    return status;
+
+  if (!mu_url_is_scheme (url, scheme))
+    return EINVAL;
+
+  if (url->port == 0)
+    url->port = port;
+
+  return status;
+}
+
+/* Default mailbox path generator */
+static char *
+_url_path_default (const char *spooldir, const char *user, int unused)
+{
+  char *mbox = malloc (strlen (spooldir) + strlen (user) + 2);
+  if (!mbox)
+    errno = ENOMEM;
+  else
+    sprintf (mbox, "%s/%s", spooldir, user);
+  return mbox;
+}
+
+/* Hashed indexing */
+static char *
+_url_path_hashed (const char *spooldir, const char *user, int param)
+{
+  int i;
+  int ulen = strlen (user);
+  char *mbox;
+  unsigned hash;
+
+  if (param > ulen)
+    param = ulen;
+  for (i = 0, hash = 0; i < param; i++)
+    hash += user[i];
+
+  mbox = malloc (ulen + strlen (spooldir) + 5);
+  sprintf (mbox, "%s/%02X/%s", spooldir, hash % 256, user);
+  return mbox;
+}
+
+static int transtab[] = {
+  'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
+  'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
+  'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
+  'y', 'z', 'a', 'b', 'c', 'd', 'e', 'f',
+  'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
+  'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
+  'w', 'x', 'y', 'z', 'a', 'b', 'c', 'd',
+  'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
+  'm', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
+  'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
+  'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
+  'x', 'y', 'z', 'b', 'c', 'd', 'e', 'f',
+  'g', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
+  'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
+  'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
+  'x', 'y', 'z', 'b', 'c', 'd', 'e', 'f',
+  'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
+  'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
+  'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
+  'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
+  'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
+  'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
+  'x', 'y', 'z', 'a', 'b', 'c', 'd', 'e',
+  'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
+  'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
+  'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
+  'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
+  'y', 'z', 'b', 'c', 'd', 'e', 'f', 'g',
+  'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
+  'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
+  'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
+  'y', 'z', 'b', 'c', 'd', 'e', 'f', 'g'
+};
+
+/* Forward Indexing */
+static char *
+_url_path_index (const char *spooldir, const char *iuser, int index_depth)
+{
+  const unsigned char* user = (const unsigned char*) iuser;
+  int i, ulen = strlen (iuser);
+  char *mbox, *p;
+
+  if (ulen == 0)
+    return NULL;
+
+  mbox = malloc (ulen + strlen (spooldir) + 2*index_depth + 2);
+  strcpy (mbox, spooldir);
+  p = mbox + strlen (mbox);
+  for (i = 0; i < index_depth && i < ulen; i++)
+    {
+      *p++ = '/';
+      *p++ = transtab[ user[i] ];
+    }
+  for (; i < index_depth; i++)
+    {
+      *p++ = '/';
+      *p++ = transtab[ user[ulen-1] ];
+    }
+  *p++ = '/';
+  strcpy (p, iuser);
+  return mbox;
+}
+
+/* Reverse Indexing */
+static char *
+_url_path_rev_index (const char *spooldir, const char *iuser, int index_depth)
+{
+  const unsigned char* user = (const unsigned char*) iuser;
+  int i, ulen = strlen (iuser);
+  char *mbox, *p;
+
+  if (ulen == 0)
+    return NULL;
+
+  mbox = malloc (ulen + strlen (spooldir) + 2*index_depth + 1);
+  strcpy (mbox, spooldir);
+  p = mbox + strlen (mbox);
+  for (i = 0; i < index_depth && i < ulen; i++)
+    {
+      *p++ = '/';
+      *p++ = transtab[ user[ulen - i - 1] ];
+    }
+  for (; i < index_depth; i++)
+    {
+      *p++ = '/';
+      *p++ = transtab[ user[0] ];
+    }
+  *p++ = '/';
+  strcpy (p, iuser);
+  return mbox;
+}
+
+static int
+rmselector (const char *p, void *data MU_ARG_UNUSED)
+{
+  return strncmp (p, "type=", 5) == 0
+	 || strncmp (p, "user=", 5) == 0
+	 || strncmp (p, "param=", 6) == 0;
+}
+
+int
+mu_url_expand_path (mu_url_t url)
+{
+  size_t i;
+  char *user = NULL;
+  int param = 0;
+  char *p;
+  char *(*fun) (const char *, const char *, int) = _url_path_default;
+
+  if (url->fvcount == 0)
+    return 0;
+
+  for (i = 0; i < url->fvcount; i++)
+    {
+      p = url->fvpairs[i];
+      if (strncmp (p, "type=", 5) == 0)
+	{
+	  char *type = p + 5;
+
+	  if (strcmp (type, "hash") == 0)
+	    fun = _url_path_hashed;
+	  else if (strcmp (type, "index") == 0)
+	    fun = _url_path_index;
+	  else if (strcmp (type, "rev-index") == 0)
+	    fun = _url_path_rev_index;
+	  else
+	    return MU_ERR_NOENT;
+	}
+      else if (strncmp (p, "user=", 5) == 0)
+	{
+	  user = p + 5;
+	}
+      else if (strncmp (p, "param=", 6) == 0)
+	{
+	  param = strtoul (p + 6, NULL, 0);
+	}
+    }
+
+  if (user)
+    {
+      char *p = fun (url->path, user, param);
+      if (p)
+	{
+	  free (url->path);
+	  url->path = p;
+	}
+      mu_argcv_remove (&url->fvcount, &url->fvpairs, rmselector, NULL);
+    }
+  else
+    return MU_ERR_NOENT;
+
+  return 0;
+}
diff --git a/libmailutils/vartab.c b/libmailutils/base/vartab.c
similarity index 100%
rename from libmailutils/vartab.c
rename to libmailutils/base/vartab.c
diff --git a/libmailutils/base/version.c b/libmailutils/base/version.c
new file mode 100644
index 0000000..d482140
--- /dev/null
+++ b/libmailutils/base/version.c
@@ -0,0 +1,187 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 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, see
+   <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <mailutils/nls.h>
+#include <mailutils/version.h>
+#include <mailutils/cstr.h>
+#include <stdio.h>
+#include <string.h>
+
+#include <confpaths.h>
+
+char *mu_license_text =
+ N_("   GNU Mailutils is free software; you can redistribute it and/or modify\n"
+    "   it under the terms of the GNU General Public License as published by\n"
+    "   the Free Software Foundation; either version 3 of the License, or\n"
+    "   (at your option) any later version.\n"
+    "\n"
+    "   GNU Mailutils is distributed in the hope that it will be useful,\n"
+    "   but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
+    "   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
+    "   GNU General Public License for more details.\n"
+    "\n"
+    "   You should have received a copy of the GNU General Public License along\n"
+    "   with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.\n"
+    "\n"
+    "\n"
+);
+
+static struct mu_conf_option mu_conf_option[] = {
+  { "VERSION=" VERSION, N_("Version of this package") },
+  { "SYSCONFDIR=" SYSCONFDIR, N_("System configuration directory") },
+  { "MAILSPOOLDIR=" MU_PATH_MAILDIR, N_("Default mail spool directory") },
+  { "SCHEME=" MU_DEFAULT_SCHEME, N_("Default mailbox type") },
+  { "LOG_FACILITY=" LOG_FACILITY_STRING, N_("Default syslog facility") },
+#ifdef USE_LIBPAM
+  { "USE_LIBPAM", N_("PAM support") },
+#endif
+#ifdef HAVE_LIBLTDL
+  { "HAVE_LIBLTDL", N_("a portable `dlopen' wrapper library") },
+#endif
+#ifdef WITH_BDB2
+  { "WITH_BDB2", N_("Berkeley DB v. 2") },
+#endif
+#ifdef WITH_NDBM
+  { "WITH_NDBM", },
+#endif
+#ifdef WITH_OLD_DBM
+  { "WITH_OLD_DBM", N_("Old DBM support") },
+#endif
+#ifdef WITH_GDBM
+  { "WITH_GDBM", N_("GNU DBM") },
+#endif
+#ifdef WITH_TOKYOCABINET
+  { "WITH_TOKYOCABINET", N_("Tokyo Cabinet DBM") },
+#endif
+#ifdef WITH_GNUTLS
+  { "WITH_GNUTLS", N_("TLS support using GNU TLS") },
+#endif
+#ifdef WITH_GSASL
+  { "WITH_GSASL", N_("SASL support using GNU SASL") },
+#endif
+#ifdef WITH_GSSAPI
+  { "WITH_GSSAPI", N_("GSSAPI support") },
+#endif
+#ifdef WITH_GUILE
+  { "WITH_GUILE", N_("Support for Guile as extension language") },
+#endif
+#ifdef WITH_PYTHON
+  { "WITH_PYTHON", N_("Support for Python as extension language") },
+#endif
+#ifdef WITH_PTHREAD
+  { "WITH_PTHREAD", N_("Support for POSIX threads") },
+#endif
+#ifdef WITH_READLINE
+  { "WITH_READLINE", N_("GNU Readline") },
+#endif
+#ifdef HAVE_MYSQL
+  { "HAVE_MYSQL", N_("MySQL") },
+#endif
+#ifdef HAVE_PGSQL
+  { "HAVE_PGSQL", N_("PostgreSQL") },
+#endif
+#ifdef WITH_LDAP
+  { "WITH_LDAP", },
+#endif
+#ifdef WITH_LIBWRAP
+  { "WITH_LIBWRAP", N_("Support for TCP wrappers") },
+#endif
+#ifdef ENABLE_VIRTUAL_DOMAINS
+  { "ENABLE_VIRTUAL_DOMAINS", N_("Support for virtual mail domains") },
+#endif
+#ifdef ENABLE_IMAP
+  { "ENABLE_IMAP", N_("IMAP4 protocol support") },
+#endif
+#ifdef ENABLE_POP
+  { "ENABLE_POP",  N_("POP3 protocol support") },
+#endif
+#ifdef ENABLE_MH
+  { "ENABLE_MH", N_("MH mail storage support") },
+#endif
+#ifdef ENABLE_MAILDIR
+  { "ENABLE_MAILDIR", N_("Maildir mail storage support") },
+#endif
+#ifdef ENABLE_SMTP
+  { "ENABLE_SMTP", N_("SMTP protocol support") },
+#endif
+#ifdef ENABLE_SENDMAIL
+  { "ENABLE_SENDMAIL", N_("Sendmail command line interface support")},
+#endif
+#ifdef ENABLE_NNTP
+  { "ENABLE_NNTP", N_("NNTP protocol support") },
+#endif
+#ifdef ENABLE_RADIUS
+  { "ENABLE_RADIUS", N_("RADIUS protocol support") },
+#endif
+#ifdef WITH_INCLUDED_LIBINTL
+  { "WITH_INCLUDED_LIBINTL", N_("GNU libintl compiled in") },
+#endif
+  { NULL }
+};
+
+void
+mu_fprint_conf_option (FILE *fp, const struct mu_conf_option *opt, int verbose)
+{
+  fprintf (fp, "%s", opt->name);
+  if (verbose && opt->descr)
+    fprintf (fp, " \t- %s", _(opt->descr));
+  fputc('\n', fp);
+}
+
+void
+mu_fprint_options (FILE *fp, int verbose)
+{
+  int i;
+  
+  for (i = 0; mu_conf_option[i].name; i++)
+    mu_fprint_conf_option (fp, mu_conf_option + i, verbose);
+}
+
+void
+mu_print_options ()
+{
+  mu_fprint_options (stdout, 1);
+}
+
+const struct mu_conf_option *
+mu_check_option (char *name)
+{
+  int i;
+  
+  for (i = 0; mu_conf_option[i].name; i++)
+    {
+      int len;
+      char *q, *p = strchr (mu_conf_option[i].name, '=');
+      if (p)
+	len = p - mu_conf_option[i].name;
+      else
+	len = strlen (mu_conf_option[i].name);
+
+      if (mu_c_strncasecmp (mu_conf_option[i].name, name, len) == 0)
+	return &mu_conf_option[i];
+      else if ((q = strchr (mu_conf_option[i].name, '_')) != NULL
+	       && mu_c_strncasecmp (q + 1, name,
+			       len - (q - mu_conf_option[i].name) - 1) == 0)
+	return &mu_conf_option[i];
+    }
+  return NULL;
+}  
+
diff --git a/libmailutils/base/wicket.c b/libmailutils/base/wicket.c
new file mode 100644
index 0000000..1e6a657
--- /dev/null
+++ b/libmailutils/base/wicket.c
@@ -0,0 +1,356 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 2003, 2004, 2005, 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, see 
+   <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <pwd.h>
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <ctype.h>
+
+#include <mailutils/errno.h>
+#include <mailutils/mutil.h>
+#include <mailutils/mu_auth.h>
+#include <mailutils/stream.h>
+
+#include <mailutils/sys/auth.h>
+#include <mailutils/sys/url.h>
+
+int
+mu_wicket_create (mu_wicket_t *pwicket)
+{
+  mu_wicket_t wicket = calloc (1, sizeof (*wicket));
+  if (!wicket)
+    return ENOMEM;
+  wicket->refcnt = 1;
+  *pwicket = wicket;
+  return 0;
+}
+
+int
+mu_wicket_get_ticket (mu_wicket_t wicket, const char *user, mu_ticket_t *pticket)
+{
+  if (!wicket)
+    return EINVAL;
+  if (!pticket)
+    return EINVAL;
+  if (!wicket->_get_ticket)
+    return ENOSYS;
+  return wicket->_get_ticket (wicket, wicket->data, user, pticket);
+}
+
+int
+mu_wicket_ref (mu_wicket_t wicket)
+{
+  if (!wicket)
+    return EINVAL;
+  wicket->refcnt++;
+  return 0;
+}
+
+int
+mu_wicket_unref (mu_wicket_t wicket)
+{
+  if (!wicket)
+    return EINVAL;
+  if (wicket->refcnt)
+    wicket->refcnt--;
+  if (wicket->refcnt == 0)
+    {
+      if (wicket->_destroy)
+	wicket->_destroy (wicket);
+      free (wicket);
+      return 0;
+    }
+  return MU_ERR_EXISTS;
+}
+
+
+void
+mu_wicket_destroy (mu_wicket_t *pwicket)
+{
+  if (pwicket && *pwicket && mu_wicket_unref (*pwicket) == 0)
+    *pwicket = NULL;
+}
+
+int
+mu_wicket_set_destroy (mu_wicket_t wicket, void (*_destroy) (mu_wicket_t))
+{
+  if (!wicket)
+    return EINVAL;
+  wicket->_destroy = _destroy;
+  return 0;
+}
+
+int
+mu_wicket_set_data (mu_wicket_t wicket, void *data)
+{
+  if (!wicket)
+    return EINVAL;
+  wicket->data = data;
+  return 0;
+}
+
+void *
+mu_wicket_get_data (mu_wicket_t wicket)
+{
+  if (!wicket)
+    return NULL;
+  return wicket->data;
+}
+
+int
+mu_wicket_set_get_ticket (mu_wicket_t wicket,
+			  int (*_get_ticket) (mu_wicket_t, void *,
+					      const char *, mu_ticket_t *))
+{
+  if (!wicket)
+    return EINVAL;
+  wicket->_get_ticket = _get_ticket;
+  return 0;
+}
+
+
+/* A "file wicket" implementation */
+
+struct file_wicket
+{
+  char *filename;
+};
+
+static void
+_file_wicket_destroy (mu_wicket_t wicket)
+{
+  struct file_wicket *fw = mu_wicket_get_data (wicket);
+  free (fw->filename);
+  free (fw);
+}
+
+struct file_ticket
+{
+  char *filename;
+  char *user;
+  mu_url_t tickurl;
+};
+
+static void
+file_ticket_destroy (mu_ticket_t ticket)
+{
+  struct file_ticket *ft = mu_ticket_get_data (ticket);
+  if (ft)
+    {
+      free (ft->filename);
+      free (ft->user);
+      mu_url_destroy (&ft->tickurl);
+      free (ft);
+    }
+}
+
+static int get_ticket_url (mu_ticket_t ticket, mu_url_t url, mu_url_t *pticket_url);
+
+int
+file_ticket_get_cred (mu_ticket_t ticket, mu_url_t url, const char *challenge,
+		      char **pplain, mu_secret_t *psec)
+{
+  struct file_ticket *ft = mu_ticket_get_data (ticket);
+
+  if (!ft->tickurl)
+    {
+      int rc = get_ticket_url (ticket, url, &ft->tickurl);
+      if (rc)
+	return rc;
+    }
+  if (pplain)
+    {
+      if (ft->user)
+	{
+	  *pplain = strdup (ft->user);
+	  if (!*pplain)
+	    return ENOMEM;
+	}
+      else
+	return mu_url_aget_user (ft->tickurl, pplain);
+    }
+  else
+    return mu_url_get_secret (ft->tickurl, psec);
+}
+
+static int
+_file_wicket_get_ticket (mu_wicket_t wicket, void *data,
+			 const char *user, mu_ticket_t *pticket)
+{
+  int rc;
+  mu_ticket_t ticket;
+  struct file_wicket *fw = data;
+  struct file_ticket *ft = calloc (1, sizeof (*ft));
+  ft->filename = strdup (fw->filename);
+  if (!ft->filename)
+    {
+      free (ft);
+      return ENOMEM;
+    }
+  if (user)
+    {
+      ft->user = strdup (user);
+      if (!ft->user)
+	{
+	  free (ft->filename);
+	  free (ft);
+	  return ENOMEM;
+	}
+    }
+  else
+    ft->user = NULL;
+
+  rc = mu_ticket_create (&ticket, NULL);
+  if (rc)
+    {
+      free (ft->filename);
+      free (ft->user);
+      free (ft);
+      return rc;
+    }
+	
+  mu_ticket_set_destroy (ticket, file_ticket_destroy, NULL);
+  mu_ticket_set_data (ticket, ft, NULL);
+  mu_ticket_set_get_cred (ticket, file_ticket_get_cred, NULL);
+
+  *pticket = ticket;
+  return 0;
+}
+  
+static int
+get_ticket_url (mu_ticket_t ticket, mu_url_t url, mu_url_t *pticket_url)
+{
+  mu_stream_t stream;
+  struct file_ticket *ft = mu_ticket_get_data (ticket);
+  int rc;
+  mu_url_t u = NULL;
+  char *buf = NULL;
+  size_t bufsize = 0;
+  size_t len;
+  
+  rc = mu_file_stream_create (&stream, ft->filename, MU_STREAM_READ);
+  if (rc)
+    return rc;
+    
+  while ((rc = mu_stream_getline (stream, &buf, &bufsize, &len)) == 0
+	 && len > 0)
+    {
+      char *p;
+      int err;
+      
+      /* Truncate a trailing newline. */
+      if (len && buf[len - 1] == '\n')
+	buf[--len] = 0;
+      
+      /* Skip leading spaces  */
+      for (p = buf; *p == ' ' || *p == '\t'; p++)
+	;
+      /* Skip trailing spaces */
+      for (; len > 0 && (p[len-1] == ' ' || p[len-1] == '\t'); )
+	p[--len] = 0;
+      
+      /* Skip empty lines and comments. */
+      if (*p == 0 || *p == '#')
+	continue;
+      
+      if ((err = mu_url_create (&u, p)) != 0)
+	{
+	  /* Skip erroneous entry */
+	  /* FIXME: Error message */
+	  continue;
+	}
+      if ((err = mu_url_parse (u)) != 0)
+	{
+	  /* FIXME: See above */
+	  mu_url_destroy (&u);
+	  continue;
+	}
+      
+      if (!mu_url_is_ticket (u, url))
+	{
+	  mu_url_destroy (&u);
+	  continue;
+	}
+      
+      if (ft->user)
+	{
+	  if (u->name && strcmp (u->name, "*") != 0
+	      && strcmp (ft->user, u->name) != 0)
+	    {
+	      mu_url_destroy (&u);
+	      continue;
+	    }
+	}
+      
+      break;
+    }
+  mu_stream_close (stream);
+  free (buf);
+    
+  mu_stream_destroy (&stream);
+
+  if (rc == 0)
+    {
+      if (u)
+	*pticket_url = u;
+      else
+	rc = MU_ERR_NOENT;
+    }
+  
+  return rc;
+}
+
+int
+mu_file_wicket_create (mu_wicket_t *pwicket, const char *filename)
+{
+  mu_wicket_t wicket;
+  int rc;
+  struct file_wicket *fw = calloc (1, sizeof (*fw));
+
+  if (!fw)
+    return ENOMEM;
+  fw->filename = strdup (filename);
+  if (!fw->filename)
+    {
+      free (fw);
+      return ENOMEM;
+    }
+  
+  rc = mu_wicket_create (&wicket);
+  if (rc)
+    {
+      free (fw->filename);
+      free (fw);
+      return rc;
+    }
+  mu_wicket_set_data (wicket, fw);
+  mu_wicket_set_destroy (wicket, _file_wicket_destroy);
+  mu_wicket_set_get_ticket (wicket, _file_wicket_get_ticket);
+  *pwicket = wicket;
+  return 0;
+}
+
diff --git a/libmailutils/body.c b/libmailutils/body.c
deleted file mode 100644
index 05b47f1..0000000
--- a/libmailutils/body.c
+++ /dev/null
@@ -1,417 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 1999, 2000, 2001, 2004, 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 */
-
-#ifdef HAVE_CONFIG_H
-# include <config.h>
-#endif
-
-#include <errno.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-
-#include <mailutils/stream.h>
-#include <mailutils/mutil.h>
-#include <mailutils/errno.h>
-#include <mailutils/sys/stream.h>
-#include <mailutils/sys/body.h>
-
-#define BODY_MODIFIED 0x10000
-
-static int _body_flush    (mu_stream_t);
-static int _body_read     (mu_stream_t, char *, size_t, size_t *);
-static int _body_truncate (mu_stream_t, mu_off_t);
-static int _body_size     (mu_stream_t, mu_off_t *);
-static int _body_write    (mu_stream_t, const char *, size_t, size_t *);
-static int _body_ioctl    (mu_stream_t, int, void *);
-static int _body_seek     (mu_stream_t, mu_off_t, mu_off_t *);
-static const char *_body_error_string (mu_stream_t, int);
-
-/* Our own defaults for the body.  */
-static int _body_get_size   (mu_body_t, size_t *);
-static int _body_get_lines  (mu_body_t, size_t *);
-static int _body_get_size0  (mu_stream_t, size_t *);
-static int _body_get_lines0 (mu_stream_t, size_t *);
-
-int
-mu_body_create (mu_body_t *pbody, void *owner)
-{
-  mu_body_t body;
-
-  if (pbody == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-  if (owner == NULL)
-    return EINVAL;
-
-  body = calloc (1, sizeof (*body));
-  if (body == NULL)
-    return ENOMEM;
-
-  body->owner = owner;
-  *pbody = body;
-  return 0;
-}
-
-void
-mu_body_destroy (mu_body_t *pbody, void *owner)
-{
-  if (pbody && *pbody)
-    {
-      mu_body_t body = *pbody;
-      if (body->owner == owner)
-	{
-	  if (body->filename)
-	    {
-	      /* FIXME: should we do this?  */
-	      remove (body->filename);
-	      free (body->filename);
-	    }
-
-	  if (body->stream)
-	    mu_stream_destroy (&body->stream);
-
-	  if (body->fstream)
-	    {
-	      mu_stream_close (body->fstream);
-	      mu_stream_destroy (&body->fstream);
-	    }
-
-	  free (body);
-	}
-      *pbody = NULL;
-    }
-}
-
-void *
-mu_body_get_owner (mu_body_t body)
-{
-  return (body) ? body->owner : NULL;
-}
-
-/* FIXME: not implemented.  */
-int
-mu_body_is_modified (mu_body_t body)
-{
-  return (body) ? (body->flags & BODY_MODIFIED) : 0;
-}
-
-/* FIXME: not implemented.  */
-int
-mu_body_clear_modified (mu_body_t body)
-{
-  if (body)
-    body->flags &= ~BODY_MODIFIED;
-  return 0;
-}
-
-int
-mu_body_get_filename (mu_body_t body, char *filename, size_t len, size_t *pn)
-{
-  int n = 0;
-  if (body == NULL)
-    return EINVAL;
-  if (body->filename)
-    {
-      n = strlen (body->filename);
-      if (filename && len > 0)
-	{
-	  len--; /* Space for the null.  */
-	  strncpy (filename, body->filename, len)[len] = '\0';
-	}
-    }
-  if (pn)
-    *pn = n;
-  return 0;
-}
-
-
-struct _mu_body_stream
-{
-  struct _mu_stream stream;
-  mu_body_t body;
-};
-
-static int
-_body_get_stream (mu_body_t body, mu_stream_t *pstream, int ref)
-{
-  if (body == NULL)
-    return EINVAL;
-  if (pstream == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-
-  if (body->stream == NULL)
-    {
-      if (body->_get_stream)
-	{
-	  int status = body->_get_stream (body, &body->stream);
-	  if (status)
-	    return status;
-	}
-      else
-	{
-	  int status;
-	  struct _mu_body_stream *str =
-	    (struct _mu_body_stream *)
-	    _mu_stream_create (sizeof (*str),
-			       MU_STREAM_RDWR|MU_STREAM_SEEK|_MU_STR_OPEN);
-	  if (!str)
-	    return ENOMEM;
-	  
-	  /* Create the temporary file.  */
-	  body->filename = mu_tempname (NULL);
-	  status = mu_file_stream_create (&body->fstream, 
-					  body->filename, MU_STREAM_RDWR);
-	  if (status != 0)
-	    return status;
-	  mu_stream_set_buffer (body->fstream, mu_buffer_full, 0);
-	  str->stream.ctl = _body_ioctl;
-	  str->stream.read = _body_read;
-	  str->stream.write = _body_write;
-	  str->stream.truncate = _body_truncate;
-	  str->stream.size = _body_size;
-	  str->stream.seek = _body_seek;
-	  str->stream.flush = _body_flush;
-	  str->body = body;
-	  body->stream = (mu_stream_t) str;
-	  /* Override the defaults.  */
-	  body->_lines = _body_get_lines;
-	  body->_size = _body_get_size;
-	}
-    }
-  
-  if (!ref)
-    {
-      *pstream = body->stream;
-      return 0;
-    }
-  return mu_streamref_create (pstream, body->stream);
-}
-
-int
-mu_body_get_stream (mu_body_t body, mu_stream_t *pstream)
-{
-  /* FIXME: Deprecation warning */
-  return _body_get_stream (body, pstream, 0);
-}
-
-int
-mu_body_get_streamref (mu_body_t body, mu_stream_t *pstream)
-{
-  return _body_get_stream (body, pstream, 1);
-}
-
-int
-mu_body_set_stream (mu_body_t body, mu_stream_t stream, void *owner)
-{
-  if (body == NULL)
-   return EINVAL;
-  if (body->owner != owner)
-    return EACCES;
-  /* make sure we destroy the old one if it is owned by the body */
-  mu_stream_destroy (&body->stream);
-  body->stream = stream;
-  body->flags |= BODY_MODIFIED;
-  return 0;
-}
-
-int
-mu_body_set_get_stream (mu_body_t body,
-			int (*_getstr) (mu_body_t, mu_stream_t *),
-			void *owner)
-{
-  if (body == NULL)
-    return EINVAL;
-  if (body->owner != owner)
-    return EACCES;
-  body->_get_stream = _getstr;
-  return 0;
-}
-
-int
-mu_body_set_lines (mu_body_t body, int (*_lines) (mu_body_t, size_t *),
-		   void *owner)
-{
-  if (body == NULL)
-    return EINVAL;
-  if (body->owner != owner)
-    return EACCES;
-  body->_lines = _lines;
-  return 0;
-}
-
-int
-mu_body_lines (mu_body_t body, size_t *plines)
-{
-  if (body == NULL)
-    return EINVAL;
-  if (body->_lines)
-    return body->_lines (body, plines);
-  /* Fall on the stream.  */
-  if (body->stream)
-    return _body_get_lines0 (body->stream, plines);
-  if (plines)
-    *plines = 0;
-  return 0;
-}
-
-int
-mu_body_size (mu_body_t body, size_t *psize)
-{
-  if (body == NULL)
-    return EINVAL;
-  if (body->_size)
-    return body->_size (body, psize);
-  /* Fall on the stream.  */
-  if (body->stream)
-    return _body_get_size0 (body->stream, psize);
-  if (psize)
-    *psize = 0;
-  return 0;
-}
-
-int
-mu_body_set_size (mu_body_t body, int (*_size)(mu_body_t, size_t*) , void *owner)
-{
-  if (body == NULL)
-    return EINVAL;
-  if (body->owner != owner)
-    return EACCES;
-  body->_size = _size;
-  return 0;
-}
-
-/* Stub function for the body stream.  */
-
-static int
-_body_seek (mu_stream_t stream, mu_off_t off, mu_off_t *presult)
-{
-  struct _mu_body_stream *str = (struct _mu_body_stream*) stream;
-  mu_body_t body = str->body;
-  return mu_stream_seek (body->fstream, off, MU_SEEK_SET, presult);
-}
-
-static const char *
-_body_error_string (mu_stream_t stream, int rc)
-{
-  /* FIXME: How to know if rc was returned by a body->stream? */
-  return NULL;
-}
-
-static int
-_body_ioctl (mu_stream_t stream, int code, void *ptr)
-{
-  struct _mu_body_stream *str = (struct _mu_body_stream*) stream;
-  mu_body_t body = str->body;
-  return mu_stream_ioctl (body->fstream, code, ptr);
-}
-
-static int
-_body_read (mu_stream_t stream, char *buf, size_t size, size_t *pret)
-{
-  struct _mu_body_stream *str = (struct _mu_body_stream*) stream;
-  mu_body_t body = str->body;
-  return mu_stream_read (body->fstream, buf, size, pret);
-}
-
-static int
-_body_write (mu_stream_t stream, const char *buf, size_t size, size_t *pret)
-{
-  struct _mu_body_stream *str = (struct _mu_body_stream*) stream;
-  mu_body_t body = str->body;
-  return mu_stream_write (body->fstream, buf, size, pret);
-}
-
-static int
-_body_truncate (mu_stream_t stream, mu_off_t n)
-{
-  struct _mu_body_stream *str = (struct _mu_body_stream*) stream;
-  mu_body_t body = str->body;
-  return mu_stream_truncate (body->fstream, n);
-}
-
-static int
-_body_size (mu_stream_t stream, mu_off_t *size)
-{
-  struct _mu_body_stream *str = (struct _mu_body_stream*) stream;
-  mu_body_t body = str->body;
-  return mu_stream_size (body->fstream, size);
-}
-
-static int
-_body_flush (mu_stream_t stream)
-{
-  struct _mu_body_stream *str = (struct _mu_body_stream*) stream;
-  mu_body_t body = str->body;
-  return mu_stream_flush (body->fstream);
-}
-
-/* Default function for the body.  */
-static int
-_body_get_lines (mu_body_t body, size_t *plines)
-{
-  return _body_get_lines0 (body->fstream, plines);
-}
-
-static int
-_body_get_size (mu_body_t body, size_t *psize)
-{
-  return _body_get_size0 (body->fstream, psize);
-}
-
-static int
-_body_get_size0 (mu_stream_t stream, size_t *psize)
-{
-  mu_off_t off = 0;
-  int status = mu_stream_size (stream, &off);
-  if (psize)
-    *psize = off;
-  return status;
-}
-
-static int
-_body_get_lines0 (mu_stream_t stream, size_t *plines)
-{
-  int status =  mu_stream_flush (stream);
-  size_t lines = 0;
-  
-  if (status == 0)
-    {
-      char buf[128];
-      size_t n = 0;
-
-      status = mu_stream_seek (stream, 0, MU_SEEK_SET, NULL);
-      if (status)
-	return status;
-      while ((status = mu_stream_readline (stream, buf, sizeof buf,
-					   &n)) == 0 && n > 0)
-	{
-	  if (buf[n - 1] == '\n')
-	    lines++;
-	}
-    }
-  if (plines)
-    *plines = lines;
-  return status;
-}
-
-
diff --git a/libmailutils/cfg/.gitignore b/libmailutils/cfg/.gitignore
new file mode 100644
index 0000000..473803d
--- /dev/null
+++ b/libmailutils/cfg/.gitignore
@@ -0,0 +1,3 @@
+cfg_lexer.c
+cfg_parser.c
+cfg_parser.h
diff --git a/libmailutils/cfg/Makefile.am b/libmailutils/cfg/Makefile.am
new file mode 100644
index 0000000..b46b523
--- /dev/null
+++ b/libmailutils/cfg/Makefile.am
@@ -0,0 +1,54 @@
+# GNU Mailutils -- a suite of utilities for electronic mail
+# Copyright (C) 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, see
+# <http://www.gnu.org/licenses/>.
+
+noinst_LTLIBRARIES = libcfg.la
+
+libcfg_la_SOURCES = \
+ driver.c\
+ format.c\
+ lexer.c\
+ parser.c\
+ gocs.c
+
+localedir = $(datadir)/locale
+AM_CPPFLAGS = \
+ -DSYSCONFDIR=\"$(sysconfdir)\"\
+ -DLOCALEDIR=\"$(localedir)\"
+
+INCLUDES = @MU_LIB_COMMON_INCLUDES@ -I/libmailutils
+
+EXTRA_DIST = \
+ lexer.l\
+ parser.y\
+ parser.h
+
+BUILT_SOURCES = parser.c parser.h lexer.c
+
+YLWRAP = $(SHELL) $(mu_aux_dir)/gylwrap
+AM_YFLAGS=-vt
+AM_LFLAGS=-dvp
+
+parser.c parser.h: $(srcdir)/parser.y
+	$(YLWRAP) "$(YACC) $(AM_YFLAGS) -d" $< \
+	          y.tab.c parser.c y.tab.h parser.h \
+                  y.output parser.y.output \
+                  -- -yy mu_cfg_yy
+
+lexer.c: $(srcdir)/lexer.l parser.h
+	$(YLWRAP) "$(LEX) $(AM_LFLAGS) $(LFLAGS)" \
+                  $(srcdir)/lexer.l lex.yy.c lexer.c \
+                  -- -yy mu_cfg_yy
diff --git a/libmailutils/cfg/driver.c b/libmailutils/cfg/driver.c
new file mode 100644
index 0000000..33683d4
--- /dev/null
+++ b/libmailutils/cfg/driver.c
@@ -0,0 +1,754 @@
+/* cfg_driver.c -- Main driver for Mailutils configuration files
+   Copyright (C) 2007, 2008, 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.
+
+   GNU Mailutils 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 GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
+*/
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <unistd.h>
+#include <ctype.h>
+#include <mailutils/argcv.h>
+#include <mailutils/nls.h>
+#define MU_CFG_COMPATIBILITY /* This source uses deprecated cfg interfaces */
+#include <mailutils/cfg.h>
+#include <mailutils/errno.h>
+#include <mailutils/error.h>
+#include <mailutils/mutil.h>
+#include <mailutils/monitor.h>
+#include <mailutils/refcount.h>
+#include <mailutils/list.h>
+#include <mailutils/iterator.h>
+#include <mailutils/stream.h>
+#include <mailutils/assoc.h>
+#include <mailutils/alloc.h>
+
+
+static mu_assoc_t section_tab;
+
+static void
+alloc_section_tab ()
+{
+  if (!section_tab)
+    mu_assoc_create (&section_tab, sizeof (struct mu_cfg_cont **),
+		     MU_ASSOC_COPY_KEY);
+}
+
+int
+mu_create_canned_section (char *name, struct mu_cfg_section **psection)
+{
+  int rc;
+  struct mu_cfg_cont **pcont;
+  alloc_section_tab ();
+  rc = mu_assoc_ref_install (section_tab, name, (void **)&pcont);
+  if (rc == 0)
+    {
+      mu_config_create_container (pcont, mu_cfg_cont_section);
+      *psection = &(*pcont)->v.section;
+      (*psection)->ident = name;
+    }
+  else if (rc == MU_ERR_EXISTS)
+    *psection = &(*pcont)->v.section;
+  return rc;
+}
+
+int
+mu_create_canned_param (char *name, struct mu_cfg_param **pparam)
+{
+  int rc;
+  struct mu_cfg_cont **pcont;
+  alloc_section_tab ();
+  rc = mu_assoc_ref_install (section_tab, name, (void **)&pcont);
+  if (rc == 0)
+    {
+      mu_config_create_container (pcont, mu_cfg_cont_param);
+      *pparam = &(*pcont)->v.param;
+      (*pparam)->ident = name;
+    }
+  else if (rc == MU_ERR_EXISTS)
+    *pparam = &(*pcont)->v.param;
+  return rc;
+}
+
+struct mu_cfg_cont *
+mu_get_canned_container (const char *name)
+{
+  struct mu_cfg_cont **pcont = mu_assoc_ref (section_tab, name);
+  return pcont ? *pcont : NULL;
+}
+
+
+static struct mu_cfg_cont *root_container;
+
+int
+mu_config_create_container (struct mu_cfg_cont **pcont,
+			    enum mu_cfg_cont_type type)
+{
+  struct mu_cfg_cont *cont;
+  int rc;
+  
+  cont = calloc (1, sizeof (*cont));
+  if (!cont)
+    return ENOMEM;
+  rc = mu_refcount_create (&cont->refcount);
+  if (rc)
+    free (cont);
+  else
+    {
+      cont->type = type;
+      *pcont = cont;
+    }
+  return rc; 
+}  
+
+
+struct dup_data
+{
+  struct mu_cfg_cont *cont;
+};
+
+static int dup_container (struct mu_cfg_cont **pcont);
+
+static int
+_dup_cont_action (void *item, void *cbdata)
+{
+  int rc;
+  struct mu_cfg_cont *cont = item;
+  struct dup_data *pdd = cbdata;
+
+  rc = dup_container (&cont);
+  if (rc)
+    return rc;
+
+  if (!pdd->cont->v.section.children)
+    {
+      int rc = mu_list_create (&pdd->cont->v.section.children);
+      if (rc)
+	return rc;
+    }
+  return mu_list_append (pdd->cont->v.section.children, cont);
+}
+
+static int
+dup_container (struct mu_cfg_cont **pcont)
+{
+  int rc;
+  struct mu_cfg_cont *newcont, *oldcont = *pcont;
+  struct dup_data dd;
+
+  rc = mu_config_create_container (&newcont, oldcont->type);
+  if (rc)
+    return rc;
+
+  dd.cont = newcont;
+  switch (oldcont->type)
+    {
+    case mu_cfg_cont_section:
+      newcont->v.section.ident = oldcont->v.section.ident;
+      newcont->v.section.label = oldcont->v.section.label;
+      newcont->v.section.parser = oldcont->v.section.parser;
+      newcont->v.section.target = oldcont->v.section.target;
+      newcont->v.section.offset = oldcont->v.section.offset;
+      newcont->v.section.docstring = oldcont->v.section.docstring;
+      newcont->v.section.children = NULL;
+      mu_list_do (oldcont->v.section.children, _dup_cont_action, &dd);
+      break;
+
+    case mu_cfg_cont_param:
+      newcont->v.param = oldcont->v.param;
+      break;
+    }
+  *pcont = newcont;
+  return 0;
+}
+
+
+static void
+destroy_list (mu_list_t *plist)
+{
+  mu_list_t list = *plist;
+  mu_iterator_t itr = NULL;
+  
+  if (!list)
+    return;
+
+  mu_list_get_iterator (list, &itr);
+  for (mu_iterator_first (itr); !mu_iterator_is_done (itr);
+       mu_iterator_next (itr))
+    {
+      struct mu_cfg_cont *cont, *p;
+      mu_iterator_current (itr, (void**)&cont);
+      p = cont;
+      mu_config_destroy_container (&p);
+      if (!p)
+	mu_list_remove (list, cont);
+    }
+  mu_iterator_destroy (&itr);
+  if (mu_list_is_empty (list))
+    mu_list_destroy (plist);
+}
+
+void
+mu_config_destroy_container (struct mu_cfg_cont **pcont)
+{
+  struct mu_cfg_cont *cont = *pcont;
+  unsigned refcount = mu_refcount_dec (cont->refcount);
+  /* printf ("destr %p-%s: %d\n", cont, cont->v.section.ident, refcount); */
+  switch (cont->type)
+    {
+    case mu_cfg_cont_section:
+      destroy_list (&cont->v.section.children);
+      break;
+
+    case mu_cfg_cont_param:
+      break;
+    }
+
+  if (refcount == 0)
+    {
+      free (cont);
+      *pcont = 0;
+    }
+}
+     
+
+int
+mu_cfg_section_add_container (struct mu_cfg_section *sect,
+			      struct mu_cfg_cont *cont)
+{
+  if (!cont)
+    return 0;
+  if (!sect->children)
+    mu_list_create (&sect->children);
+  return mu_list_append (sect->children, cont);
+}
+
+int
+mu_cfg_section_add_params (struct mu_cfg_section *sect,
+			   struct mu_cfg_param *param)
+{
+  if (!param)
+    return 0;
+
+  for (; param->ident; param++)
+    {
+      int rc;
+      struct mu_cfg_cont *container;
+
+      if (param->type == mu_cfg_section)
+	{
+	  container = mu_get_canned_container (param->ident);
+	  if (!container)
+	    {
+	      mu_error (_("INTERNAL ERROR: Requested unknown canned "
+			  "section %s"),
+			param->ident);
+	      abort ();
+	    }
+	  if (param->ident[0] == '.')
+	    {
+	      mu_iterator_t itr;
+	      mu_list_get_iterator (container->v.section.children, &itr);
+	      for (mu_iterator_first (itr);
+		   !mu_iterator_is_done (itr);
+		   mu_iterator_next (itr))
+		{
+		  struct mu_cfg_cont *c;
+		  mu_iterator_current (itr, (void**)&c);
+		  mu_config_clone_container (c);
+		  if (mu_refcount_value (c->refcount) > 1)
+		    dup_container (&c);
+		  switch (c->type)
+		    {
+		    case mu_cfg_cont_section:
+		      c->v.section.offset += param->offset;
+		      break;
+
+		    case mu_cfg_cont_param:
+		      container->v.param.offset += param->offset;
+		      break;
+		    }
+		  mu_cfg_section_add_container (sect, c);
+		}
+	      mu_iterator_destroy (&itr);
+	      continue;
+	    }
+	  else
+	    {
+	      mu_config_clone_container (container);
+	      if (mu_refcount_value (container->refcount) > 1)
+		dup_container (&container);
+	      container->v.section.target = param->data;
+	      container->v.section.offset = param->offset;
+	    }
+	}
+      else
+	{
+	  rc = mu_config_create_container (&container, mu_cfg_cont_param);
+	  if (rc)
+	    return rc;
+	  container->v.param = *param;
+	}
+      mu_cfg_section_add_container (sect, container);
+    }
+  return 0;
+}
+
+static int
+_clone_action (void *item, void *cbdata)
+{
+  struct mu_cfg_cont *cont = item;
+  return mu_config_clone_container (cont);
+}
+
+int
+mu_config_clone_container (struct mu_cfg_cont *cont)
+{
+  if (!cont)
+    return 0;
+  mu_refcount_inc (cont->refcount);
+  /* printf("clone %p-%s: %d\n", cont, cont->v.section.ident, n); */
+  switch (cont->type)
+    {
+    case mu_cfg_cont_section:
+      mu_list_do (cont->v.section.children, _clone_action, NULL);
+      break;
+
+    case mu_cfg_cont_param:
+      break;
+    }
+  return 0;
+}  
+
+
+int
+_mu_config_register_section (struct mu_cfg_cont **proot,
+			     const char *parent_path,
+			     const char *ident,
+			     const char *label,
+			     mu_cfg_section_fp parser,
+			     struct mu_cfg_param *param,
+			     struct mu_cfg_section **psection)
+{
+  int rc;
+  struct mu_cfg_section *root_section;
+  struct mu_cfg_section *parent;
+  
+  if (!*proot)
+    {
+      rc = mu_config_create_container (proot, mu_cfg_cont_section);
+      if (rc)
+	return rc;
+      memset (&(*proot)->v.section, 0, sizeof (*proot)->v.section);
+    }
+  
+  root_section = &(*proot)->v.section;
+  
+  if (parent_path)
+    {
+      if (mu_cfg_find_section (root_section, parent_path, &parent))
+	return MU_ERR_NOENT;
+    }
+  else  
+    parent = root_section;
+
+  if (mu_refcount_value ((*proot)->refcount) > 1)
+    {
+      /* It is a clone, do copy-on-write */
+      rc = dup_container (proot);
+      if (rc)
+	return rc;
+
+      root_section = &(*proot)->v.section;
+      
+      if (parent_path)
+	{
+	  if (mu_cfg_find_section (root_section, parent_path, &parent))
+	    return MU_ERR_NOENT;
+	}
+      else  
+	parent = root_section;
+    }
+
+  if (ident)
+    {
+      struct mu_cfg_cont *container;
+      struct mu_cfg_section *s;
+      
+      if (!parent->children)
+	mu_list_create (&parent->children);
+      mu_config_create_container (&container, mu_cfg_cont_section);
+      mu_list_append (parent->children, container); 
+      s = &container->v.section;
+
+      s->ident = strdup (ident);
+      s->label = label ? strdup (label) : NULL;
+      s->parser = parser;
+      s->children = NULL;
+      mu_cfg_section_add_params (s, param);
+      if (psection)
+	*psection = s;
+    }
+  else
+    {
+      mu_cfg_section_add_params (parent, param);
+      /* FIXME: */
+      if (!parent->parser)
+	parent->parser = parser;
+      if (psection)
+	*psection = parent;
+    }
+  return 0;
+}
+  
+int
+mu_config_register_section (const char *parent_path,
+			    const char *ident,
+			    const char *label,
+			    mu_cfg_section_fp parser,
+			    struct mu_cfg_param *param)
+{
+  return _mu_config_register_section (&root_container,
+				      parent_path,
+				      ident, label,
+				      parser, param, NULL);
+}
+
+int
+mu_config_register_plain_section (const char *parent_path, const char *ident,
+				  struct mu_cfg_param *params)
+{
+  return mu_config_register_section (parent_path, ident, NULL, NULL, params);
+}
+
+static int
+prog_parser (enum mu_cfg_section_stage stage,
+	     const mu_cfg_node_t *node,
+	     const char *label, void **section_data,
+	     void *call_data,
+	     mu_cfg_tree_t *tree)
+{
+  if (stage == mu_cfg_section_start)
+    {
+      return node->label->type == MU_CFG_STRING
+	     && strcmp (node->label->v.string, label);
+    }
+  
+  return 0;
+}
+
+struct include_data
+{
+  const char *progname;
+  struct mu_cfg_param *progparam;
+  int flags;
+  void *target;
+};
+
+static int
+_cb_include (mu_debug_t debug, void *data, mu_config_value_t *val)
+{
+  int ret = 0;
+  struct stat sb;
+  const char *dirname;
+  struct include_data *idp = data;
+  char *tmp = NULL;
+
+  if (mu_cfg_assert_value_type (val, MU_CFG_STRING, debug))
+    return 1;
+
+  dirname = val->v.string;
+  if (dirname[0] != '/')
+    {
+      dirname = tmp = mu_make_file_name (SYSCONFDIR, dirname);
+      if (!dirname)
+        {
+          mu_error ("%s", mu_strerror (errno));
+          return 1;
+        }
+    }
+    
+  if (stat (dirname, &sb) == 0)
+    {
+      if (S_ISDIR (sb.st_mode))
+	{
+	  char *file = mu_make_file_name (dirname, idp->progname);
+	  ret = mu_get_config (file, idp->progname, idp->progparam,
+			       idp->flags & ~MU_PARSE_CONFIG_GLOBAL,
+			       idp->target);
+	}
+      else
+	ret = mu_get_config (dirname, idp->progname, idp->progparam,
+			     idp->flags, idp->target);
+    }
+  else if (errno == ENOENT)
+    {
+      mu_cfg_format_error (debug, MU_DEBUG_ERROR,
+			   _("include file or directory does not exist"));
+      ret = 1;
+    }
+  else
+    {
+      mu_cfg_format_error (debug, MU_DEBUG_ERROR,
+			   _("cannot stat include file or directory: %s"),
+			   mu_strerror (errno));
+      ret = 1;
+    }
+  free (tmp);
+  return ret;
+}
+
+struct mu_cfg_cont *
+mu_build_container (const char *progname, struct include_data *idp)
+{
+  struct mu_cfg_cont *cont = root_container;
+
+  mu_config_clone_container (cont);
+  
+  if (idp->flags & MU_PARSE_CONFIG_PLAIN)
+    {
+      struct mu_cfg_param mu_include_param[] = {
+	{ "include", mu_cfg_callback, NULL, 0, _cb_include,
+	  N_("Include contents of the given file.  If a directory is given, "
+	     "include contents of the file <file>/<program>, where "
+	     "<program> is the name of the program.  This latter form is "
+	     "allowed only in the site-wide configuration file."),
+	  N_("file-or-directory") },
+	{ NULL }
+      };
+
+      mu_include_param[0].data = idp;
+      _mu_config_register_section (&cont, NULL, NULL, NULL,
+				   (void*) progname, mu_include_param, NULL);
+      
+      if (idp->flags & MU_PARSE_CONFIG_GLOBAL)
+	{
+	  mu_iterator_t iter;
+	  struct mu_cfg_section *prog_sect;
+	  struct mu_cfg_cont *old_root = root_container;
+	  static struct mu_cfg_param empty_param = { NULL };
+	  
+	  _mu_config_register_section (&cont, NULL, "program", progname,
+				       prog_parser,
+				       idp->progparam ?
+				       idp->progparam : &empty_param,
+				       &prog_sect);
+      
+	  if (old_root->v.section.children)
+	    {
+	      if (!prog_sect->children)
+		mu_list_create (&prog_sect->children);
+	      mu_list_get_iterator (old_root->v.section.children, &iter);
+	      for (mu_iterator_first (iter); !mu_iterator_is_done (iter);
+		   mu_iterator_next (iter))
+		{
+		  struct mu_cfg_cont *c;
+		  mu_iterator_current (iter, (void**)&c);
+		  mu_list_append (prog_sect->children, c);
+		}
+	      mu_iterator_destroy (&iter);
+	    }
+	}
+      else if (idp->progparam)
+	_mu_config_register_section (&cont, NULL, NULL, NULL, NULL,
+				     idp->progparam, NULL);
+    }
+  else if (idp->progparam)
+    _mu_config_register_section (&cont, NULL, NULL, NULL, NULL,
+				 idp->progparam, NULL);
+  
+  return cont;
+}
+
+int
+mu_cfg_tree_reduce (mu_cfg_tree_t *parse_tree, const char *progname,
+		    struct mu_cfg_param *progparam, int flags,
+		    void *target_ptr)
+{
+  int rc = 0;
+
+  if (!parse_tree)
+    return 0;
+  if (flags & MU_PARSE_CONFIG_DUMP)
+    {
+      mu_stream_t stream;
+      mu_stdio_stream_create (&stream, MU_STDERR_FD, 0);
+      mu_cfg_format_parse_tree (stream, parse_tree, MU_CFG_FMT_LOCUS);
+      mu_stream_destroy (&stream);
+    }
+
+  if (root_container)
+    {
+      struct include_data idata;
+      struct mu_cfg_cont *cont;
+      
+      idata.progname = progname;
+      idata.progparam = progparam;
+      idata.flags = flags;
+      idata.target = target_ptr;
+      
+      cont = mu_build_container (progname, &idata);
+      
+      rc = mu_cfg_scan_tree (parse_tree, &cont->v.section, target_ptr,
+			     (void*) progname);
+      mu_config_destroy_container (&cont);
+    }
+
+  return rc;
+}
+
+void
+mu_format_config_tree (mu_stream_t stream, const char *progname,
+		       struct mu_cfg_param *progparam, int flags)
+{
+  struct include_data idata;
+  struct mu_cfg_cont *cont;
+  
+  idata.progname = progname;
+  idata.progparam = progparam;
+  idata.flags = flags;
+  idata.target = NULL;
+  cont = mu_build_container (progname, &idata);
+  mu_cfg_format_container (stream, cont);
+  mu_config_destroy_container (&cont);
+}
+
+int
+mu_parse_config (const char *file, const char *progname,
+		 struct mu_cfg_param *progparam, int flags,
+		 void *target_ptr)
+{
+  int rc;
+  char *full_name = mu_tilde_expansion (file, "/", NULL);
+  if (full_name)
+    {
+      if (access (full_name, R_OK) == 0)
+	{
+	  rc = mu_get_config (full_name, progname, progparam, flags,
+			      target_ptr);
+	}
+      else
+	rc = ENOENT;
+      free (full_name);
+    }
+  else
+    rc = ENOMEM;
+  return rc;
+}
+
+static const char *
+_first_value_ptr (mu_config_value_t *val)
+{
+  switch (val->type)
+    {
+    case MU_CFG_STRING:
+      return val->v.string;
+      
+    case MU_CFG_ARRAY:
+      return _first_value_ptr (val->v.arg.v);
+      
+    case MU_CFG_LIST:
+      mu_list_get (val->v.list, 0, (void**) &val);
+      return _first_value_ptr (val);
+    }
+  return "";  
+}
+
+int
+mu_cfg_assert_value_type (mu_config_value_t *val, int type, mu_debug_t debug)
+{
+  if (!val)
+    { 
+      mu_cfg_format_error (debug, MU_DEBUG_ERROR,
+                           _("required argument missing"));
+      return 1;
+    }
+
+  if (type == MU_CFG_ARRAY)
+    {
+      if (val->type == MU_CFG_STRING)
+	{
+	  mu_config_value_t *arr = mu_calloc (1, sizeof arr[0]);
+	  arr[0] = *val;
+	  val->v.arg.c = 1;
+	  val->v.arg.v = arr;
+	  val->type = MU_CFG_ARRAY;
+	}
+    }
+  
+  if (val->type != type)
+    {
+      /* FIXME */
+      mu_cfg_format_error (debug, MU_DEBUG_ERROR,
+			   _("unexpected value: %s"), 
+			   _first_value_ptr (val));
+      return 1;
+    }
+  return 0;
+}
+
+int
+mu_cfg_string_value_cb (mu_debug_t debug, mu_config_value_t *val,
+			int (*fun) (mu_debug_t, const char *, void *),
+			void *data)
+{
+  int rc = 0;
+  
+  switch (val->type)
+    {
+    case MU_CFG_STRING:
+      return fun (debug, val->v.string, data);
+      break;
+
+    case MU_CFG_ARRAY:
+      {
+	int i;
+
+	for (i = 0; i < val->v.arg.c; i++)
+	  {
+	    if (mu_cfg_assert_value_type (&val->v.arg.v[i],
+					  MU_CFG_STRING, debug))
+	      return 1;
+	    fun (debug, val->v.arg.v[i].v.string, data);
+	  }
+      }
+      break;
+
+    case MU_CFG_LIST:
+      {
+	mu_iterator_t itr;
+	mu_list_get_iterator (val->v.list, &itr);
+	for (mu_iterator_first (itr);
+	     !mu_iterator_is_done (itr); mu_iterator_next (itr))
+	  {
+	    mu_config_value_t *pval;
+	    mu_iterator_current (itr, (void*) &pval);
+	    if (mu_cfg_assert_value_type (pval, MU_CFG_STRING, debug))
+	      {
+		rc = 1;
+		break;
+	      }
+	    fun (debug, pval->v.string, data);
+	  }
+	mu_iterator_destroy (&itr);
+      }
+    }
+  return rc;
+}
diff --git a/libmailutils/cfg/format.c b/libmailutils/cfg/format.c
new file mode 100644
index 0000000..e7aaa7b
--- /dev/null
+++ b/libmailutils/cfg/format.c
@@ -0,0 +1,395 @@
+/* cfg_format.c -- convert configuration parse tree to human-readable format.
+   Copyright (C) 2007, 2008, 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.
+
+   GNU Mailutils 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 GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+#include <mailutils/alloc.h>
+#include <mailutils/stream.h>
+#include <mailutils/error.h>
+#include <mailutils/cfg.h>
+#include <mailutils/argcv.h>
+#include <mailutils/nls.h>
+#include <mailutils/iterator.h>
+#include <ctype.h>
+
+struct tree_print
+{
+  int flags;
+  unsigned level;
+  mu_stream_t stream;
+  char *buf;
+  size_t bufsize;
+};
+
+static void
+format_level (mu_stream_t stream, int level)
+{
+  while (level--)
+    mu_stream_write (stream, "  ", 2, NULL);
+}
+
+static void
+format_string_value (struct tree_print *tp, const char *str)
+{
+  size_t size;
+  int quote;
+  char *p;
+
+  size = mu_argcv_quoted_length (str, &quote);
+  if (quote)
+    size += 2;
+  size++;
+  if (size > tp->bufsize)
+    {
+      p = mu_realloc (tp->buf, size);
+      tp->bufsize = size;
+      tp->buf = p;
+    }
+  
+  p = tp->buf;
+  if (quote)
+    {
+      tp->buf[0] = '"';
+      tp->buf[size-2] = '"';
+      p++;
+    }
+  tp->buf[size-1] = 0;
+  mu_argcv_quote_copy (p, str);
+  mu_stream_write (tp->stream, tp->buf, size - 1, NULL);
+}
+
+static void format_value (struct tree_print *tp, mu_config_value_t *val);
+
+static void
+format_list_value (struct tree_print *tp, mu_config_value_t *val)
+{
+  int i;
+  mu_iterator_t itr;
+  mu_stream_write (tp->stream, "(", 1, NULL);
+  mu_list_get_iterator (val->v.list, &itr);
+  
+  for (mu_iterator_first (itr), i = 0;
+       !mu_iterator_is_done (itr); mu_iterator_next (itr), i++)
+    {
+      mu_config_value_t *p;
+      mu_iterator_current (itr, (void**)&p);
+      if (i)
+	mu_stream_write (tp->stream, ", ", 2, NULL);
+      format_value (tp, p);
+    }
+  mu_iterator_destroy (&itr);
+  mu_stream_write (tp->stream, ")", 1, NULL);
+}
+
+static void
+format_array_value (struct tree_print *tp, mu_config_value_t *val)
+{
+  int i;
+
+  for (i = 0; i < val->v.arg.c; i++)
+    {
+      if (i)
+	mu_stream_write (tp->stream, " ", 1, NULL);
+      format_value (tp, &val->v.arg.v[i]);
+    }
+}
+    
+static void
+format_value (struct tree_print *tp, mu_config_value_t *val)
+{
+  switch (val->type)
+    {
+    case MU_CFG_STRING:
+      format_string_value (tp, val->v.string);
+      break;
+
+    case MU_CFG_LIST:
+      format_list_value (tp, val);
+      break;
+
+    case MU_CFG_ARRAY:
+      format_array_value (tp, val);
+    }
+}
+
+static int
+format_node (const mu_cfg_node_t *node, void *data)
+{
+  struct tree_print *tp = data;
+
+  if ((tp->flags & MU_CFG_FMT_LOCUS) && node->locus.file)
+    mu_stream_printf (tp->stream, "# %lu \"%s\"\n",
+		      (unsigned long) node->locus.line, 
+		      node->locus.file);
+  format_level (tp->stream, tp->level);
+  switch (node->type)
+    {
+    case mu_cfg_node_undefined:
+      mu_stream_printf (tp->stream, "%s",
+			_("ERROR: undefined statement"));
+      break;
+
+    case mu_cfg_node_statement:
+      {
+	mu_stream_write (tp->stream, node->tag, strlen (node->tag), NULL);
+	if (node->label)
+	  {
+	    mu_stream_write (tp->stream, " ", 1, NULL);
+	    format_value (tp, node->label);
+	  }
+	mu_stream_write (tp->stream, " {", 2, NULL);
+	tp->level++;
+      }
+      break;
+
+    case mu_cfg_node_param:
+      mu_stream_write (tp->stream, node->tag, strlen (node->tag), NULL);
+      if (node->label)
+	{
+	  mu_stream_write (tp->stream, " ", 1, NULL);
+	  format_value (tp, node->label);
+	  mu_stream_write (tp->stream, ";", 1, NULL);
+	}
+      break;
+    }
+  mu_stream_write (tp->stream, "\n", 1, NULL);
+  return MU_CFG_ITER_OK;
+}
+
+static int
+format_node_end (const mu_cfg_node_t *node, void *data)
+{
+  struct tree_print *tp = data;
+  tp->level--;
+  format_level (tp->stream, tp->level);
+  mu_stream_write (tp->stream, "};\n", 3, NULL);
+  return MU_CFG_ITER_OK;
+}
+
+void
+mu_cfg_format_parse_tree (mu_stream_t stream, mu_cfg_tree_t *tree, int flags)
+{
+  struct mu_cfg_iter_closure clos;
+  struct tree_print t;
+
+  t.flags = flags;
+  t.level = 0;
+  t.stream = stream;
+  t.buf = NULL;
+  t.bufsize = 0;
+  clos.beg = format_node;
+  clos.end = format_node_end;
+  clos.data = &t;
+  mu_cfg_preorder (tree->nodes, &clos);
+  free (t.buf);
+}
+
+void
+mu_cfg_format_node (mu_stream_t stream, const mu_cfg_node_t *node, int flags)
+{
+  struct tree_print t;
+  
+  t.flags = flags;
+  t.level = 0;
+  t.stream = stream;
+  t.buf = NULL;
+  t.bufsize = 0;
+  format_node (node, &t);
+  if (node->type == mu_cfg_node_statement)
+    {
+      struct mu_cfg_iter_closure clos;
+      clos.beg = format_node;
+      clos.end = format_node_end;
+      clos.data = &t;
+      mu_cfg_preorder (node->nodes, &clos);
+      format_node_end (node, &t);
+    }
+}
+
+
+
+const char *
+mu_cfg_data_type_string (enum mu_cfg_param_data_type type)
+{
+  switch (type)
+    {
+    case mu_cfg_string:
+      return N_("string");
+    case mu_cfg_short:
+    case mu_cfg_ushort:
+    case mu_cfg_int:
+    case mu_cfg_uint:
+    case mu_cfg_long:
+    case mu_cfg_ulong:
+    case mu_cfg_size:
+    case mu_cfg_off:
+      return N_("number");
+    case mu_cfg_time:
+      return N_("time");
+    case mu_cfg_bool:
+      return N_("boolean");
+    case mu_cfg_ipv4:
+      return N_("ipv4");
+    case mu_cfg_cidr:
+      return N_("cidr");
+    case mu_cfg_host:
+      return N_("host");
+    case mu_cfg_callback:
+      return N_("string");
+    case mu_cfg_section:
+      return N_("section");
+    }
+  return N_("unknown");
+}
+
+void
+mu_cfg_format_docstring (mu_stream_t stream, const char *docstring, int level)
+{
+  size_t len = strlen (docstring);
+  int width = 78 - level * 2;
+
+  if (width < 0)
+    {
+      width = 78;
+      level = 0;
+    }
+  
+  while (len)
+    {
+      size_t seglen;
+      const char *p;
+      
+      for (seglen = 0, p = docstring; p < docstring + width && *p; p++)
+	{
+	  if (*p == '\n')
+	    {
+	      seglen = p - docstring;
+	      break;
+	    }
+	  if (isspace (*p))
+	    seglen = p - docstring;
+	}
+      if (seglen == 0 || *p == 0)
+	seglen = p - docstring;
+
+      format_level (stream, level);
+      mu_stream_write (stream, "# ", 2, NULL);
+      mu_stream_write (stream, docstring, seglen, NULL);
+      mu_stream_write (stream, "\n", 1, NULL);
+      len -= seglen;
+      docstring += seglen;
+      if (*docstring == '\n')
+	{
+	  docstring++;
+	  len--;
+	}
+      else
+	while (*docstring && isspace (*docstring))
+	  {
+	    docstring++;
+	    len--;
+	  }
+    }
+}
+
+static void
+format_param (mu_stream_t stream, struct mu_cfg_param *param, int level)
+{
+  if (param->docstring)
+    mu_cfg_format_docstring (stream, gettext (param->docstring), level);
+  format_level (stream, level);
+  if (param->argname && strchr (param->argname, ':'))
+    mu_stream_printf (stream, "%s <%s>;\n",
+		      param->ident,
+		      gettext (param->argname));
+  else if (MU_CFG_IS_LIST (param->type))
+    mu_stream_printf (stream, "%s <%s: list of %s>;\n",
+		      param->ident,
+		      gettext (param->argname ?
+			       param->argname : N_("arg")),
+	gettext (mu_cfg_data_type_string (MU_CFG_TYPE (param->type))));
+  else
+    mu_stream_printf (stream, "%s <%s: %s>;\n",
+		      param->ident,
+		      gettext (param->argname ?
+			       param->argname : N_("arg")),
+		      gettext (mu_cfg_data_type_string (param->type)));
+}
+
+static void format_container (mu_stream_t stream, struct mu_cfg_cont *cont,
+			      int level);
+
+static int
+_f_helper (void *item, void *data)
+{
+  struct tree_print *tp = data;
+  struct mu_cfg_cont *cont = item;
+  format_container (tp->stream, cont, tp->level);
+  return 0;
+}
+  
+static void
+format_section (mu_stream_t stream, struct mu_cfg_section *sect, int level)
+{
+  struct tree_print c;
+  if (sect->docstring)
+    mu_cfg_format_docstring (stream, gettext (sect->docstring), level);
+  format_level (stream, level);
+  if (sect->ident)
+    {
+      mu_stream_write (stream, sect->ident, strlen (sect->ident), NULL);
+      if (sect->label)
+	{
+	  mu_stream_write (stream, " ", 1, NULL);
+	  mu_stream_write (stream, sect->label, strlen (sect->label), NULL);
+	}
+      mu_stream_write (stream, " {\n", 3, NULL);
+      c.stream = stream;
+      c.level = level + 1; 
+      mu_list_do (sect->children, _f_helper, &c);
+      format_level (stream, level);
+      mu_stream_write (stream, "};\n\n", 4, NULL);
+    }
+  else
+    {
+      c.stream = stream;
+      c.level = level; 
+      mu_list_do (sect->children, _f_helper, &c);
+    }
+}
+
+static void
+format_container (mu_stream_t stream, struct mu_cfg_cont *cont, int level)
+{
+  switch (cont->type)
+    {
+    case mu_cfg_cont_section:
+      format_section (stream, &cont->v.section, level);
+      break;
+
+    case mu_cfg_cont_param:
+      format_param (stream, &cont->v.param, level);
+      break;
+    }
+}
+
+void
+mu_cfg_format_container (mu_stream_t stream, struct mu_cfg_cont *cont)
+{
+  format_container (stream, cont, 0);
+}
diff --git a/libmailutils/cfg/gocs.c b/libmailutils/cfg/gocs.c
new file mode 100644
index 0000000..e6b14b4
--- /dev/null
+++ b/libmailutils/cfg/gocs.c
@@ -0,0 +1,396 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 2007, 2008, 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, see
+   <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+#include <stdlib.h>
+#include <string.h>
+#include <mailutils/types.h>
+#include <mailutils/gocs.h>
+#include <mailutils/mailbox.h>
+#include <mailutils/locker.h>
+#include <mailutils/mutil.h>
+#include <mailutils/mailer.h>
+#include <mailutils/error.h>
+#include <mailutils/errno.h>
+#include <mailutils/nls.h>
+#include <mailutils/debug.h>
+#include <mailutils/syslog.h>
+#include <mailutils/registrar.h>
+#include <syslog.h>
+
+int mu_load_user_rcfile = 1;
+int mu_load_site_rcfile = 1;
+char *mu_load_rcfile = NULL;
+
+
+int
+mu_gocs_dummy (enum mu_gocs_op op, void *data)
+{
+  return 0;
+}
+
+int
+mu_gocs_mailbox_init (enum mu_gocs_op op, void *data)
+{
+  int rc;
+  struct mu_gocs_mailbox *p = data;
+
+  if (op == mu_gocs_op_set && p)
+    {
+      if (p->mail_spool)
+	{
+	  rc = mu_set_mail_directory (p->mail_spool);
+	  if (rc)
+	    mu_error (_("cannot set mail directory name to `%s': %s"),
+		      p->mail_spool, mu_strerror (rc));
+	  free (p->mail_spool);
+	  p->mail_spool = NULL;
+	}
+      if (p->mailbox_pattern)
+	{
+	  rc = mu_set_mailbox_pattern (p->mailbox_pattern);
+	  if (rc)
+	    mu_error (_("cannot set mailbox pattern to `%s': %s"),
+		      p->mailbox_pattern, mu_strerror (rc));
+	  free (p->mailbox_pattern);
+	  p->mailbox_pattern = NULL;
+	}
+      if (p->mailbox_type)
+	{
+	  if (mu_registrar_set_default_scheme (p->mailbox_type))
+	    mu_error (_("invalid mailbox type: %s"), p->mailbox_type);
+	  free (p->mailbox_type);
+	  p->mailbox_type = NULL;
+	}
+    }
+  return 0;
+}
+
+int
+mu_gocs_locking_init (enum mu_gocs_op op, void *data)
+{
+  struct mu_gocs_locking *p = data;
+  
+  if (!(op == mu_gocs_op_set && p))
+    return 0;
+
+  if (p->lock_flags)
+    {
+      int flags = 0;
+      char *s;
+      
+      for (s = p->lock_flags; *s; s++)
+	{
+	  switch (*s)
+	    {
+	    case 'E':
+	      flags |= MU_LOCKER_EXTERNAL;
+	      break;
+	      
+	    case 'R':
+	      flags |= MU_LOCKER_RETRY;
+	      break;
+	      
+	    case 'T':
+	      flags |= MU_LOCKER_TIME;
+	      break;
+	      
+	    case 'P':
+	      flags |= MU_LOCKER_PID;
+	      break;
+	      
+	    default:
+	      mu_error (_("invalid lock flag `%c'"), *s);
+	    }
+	}
+      mu_locker_set_default_flags (flags, mu_locker_assign);
+      free (p->lock_flags);
+      p->lock_flags = NULL;
+    }
+
+  if (p->lock_retry_count)
+    {
+      mu_locker_set_default_retry_count (p->lock_retry_count);
+      mu_locker_set_default_flags (MU_LOCKER_RETRY, mu_locker_set_bit);
+      p->lock_retry_count = 0;
+    }
+
+  if (p->lock_retry_timeout)
+    {
+      mu_locker_set_default_retry_timeout (p->lock_retry_timeout);
+      mu_locker_set_default_flags (MU_LOCKER_RETRY, mu_locker_set_bit);
+      p->lock_retry_timeout = 0;
+    }
+
+  if (p->lock_expire_timeout)
+    {
+      mu_locker_set_default_expire_timeout (p->lock_expire_timeout);
+      mu_locker_set_default_flags (MU_LOCKER_EXTERNAL, mu_locker_set_bit);
+      p->lock_expire_timeout = 0;
+    }
+
+  if (p->external_locker)
+    {
+      mu_locker_set_default_external_program (p->external_locker);
+      mu_locker_set_default_flags (MU_LOCKER_TIME, mu_locker_set_bit);
+      free (p->external_locker);
+      p->external_locker = NULL;
+    }
+  return 0;
+}
+
+int
+mu_gocs_source_email_init (enum mu_gocs_op op, void *data)
+{
+  struct mu_gocs_source_email *p = data;
+  int rc;
+
+  if (!(op == mu_gocs_op_set && p))
+    return 0;
+  
+  if (p->address)
+    {
+      if ((rc = mu_set_user_email (p->address)) != 0)
+	mu_error (_("invalid email address `%s': %s"),
+		  p->address, mu_strerror (rc));
+      free (p->address);
+      p->address = NULL;
+    }
+
+  if (p->domain)
+    {
+      if ((rc = mu_set_user_email_domain (p->domain)) != 0)
+	mu_error (_("invalid email domain `%s': %s"),
+		  p->domain, mu_strerror (rc));
+
+      free (p->domain);
+      p->domain = NULL;
+    }
+  return 0;
+}
+
+int
+mu_gocs_mailer_init (enum mu_gocs_op op, void *data)
+{
+  struct mu_gocs_mailer *p = data;
+  int rc;
+
+  if (!(op == mu_gocs_op_set && p))
+    return 0;
+  
+  if (p->mailer)
+    {
+      if ((rc = mu_mailer_set_url_default (p->mailer)) != 0)
+	mu_error (_("invalid mailer URL `%s': %s"),
+		  p->mailer, mu_strerror (rc));
+      free (p->mailer);
+      p->mailer = NULL;
+    }
+  return 0;
+}
+
+int
+mu_gocs_logging_init (enum mu_gocs_op op, void *data)
+{
+  struct mu_gocs_logging *p = data;
+
+  if (op == mu_gocs_op_set)
+    {
+      if (!p)
+	{
+	  static struct mu_gocs_logging default_gocs_logging = { LOG_FACILITY };
+	  p = &default_gocs_logging;
+	}
+  
+      if (p->facility)
+	{
+	  mu_log_facility = p->facility;
+	  mu_debug_default_printer = mu_debug_syslog_printer;
+	}
+      else
+	mu_debug_default_printer = mu_debug_stderr_printer;
+
+      if (p->tag)
+	mu_log_tag = strdup (p->tag);
+    }
+  return 0;
+}
+
+int
+mu_gocs_debug_init (enum mu_gocs_op op, void *data)
+{
+  if (op == mu_gocs_op_set && data)
+    {
+      struct mu_gocs_debug *p = data;
+      if (p->string && p->errpfx)
+	{
+	  mu_global_debug_from_string (p->string, p->errpfx);
+	  free (p->errpfx);
+	}
+      if (p->line_info >= 0)
+	mu_debug_line_info = p->line_info;
+    }
+  return 0;
+}
+
+
+struct mu_gocs_entry
+{
+  const char *name;
+  mu_gocs_init_fp init;
+};
+
+#define MAX_GOCS 512
+
+static struct mu_gocs_entry _gocs_table[MAX_GOCS];
+
+void
+mu_gocs_register (const char *capa, mu_gocs_init_fp init)
+{
+  int i;
+  for (i = 0; _gocs_table[i].name; i++)
+    if (i == MAX_GOCS-1)
+      {
+	mu_error (_("gocs table overflow"));
+	abort ();
+      }
+  _gocs_table[i].name = capa;
+  _gocs_table[i].init = init;
+}
+
+int
+mu_gocs_enumerate (mu_list_action_t action, void *data)
+{
+  int i;
+  
+  for (i = 0; _gocs_table[i].name; i++)
+    {
+      int rc = action ((void*) _gocs_table[i].name, data);
+      if (rc)
+	return rc;
+    }
+  return 0;
+}
+
+static mu_gocs_init_fp
+find_init_function (struct mu_gocs_entry *tab, const char *capa)
+{
+  for (; tab->name; tab++)
+    if (strcmp (tab->name, capa) == 0)
+      return tab->init;
+  return NULL;
+}
+
+static struct mu_gocs_entry std_gocs_table[] = {
+  { "common", mu_gocs_dummy },
+  { "license", mu_gocs_dummy },
+  { "mailbox", mu_gocs_mailbox_init },
+  { "locking", mu_gocs_locking_init },
+  { "address", mu_gocs_source_email_init },
+  { "mailer", mu_gocs_mailer_init },
+  { "logging", mu_gocs_logging_init },
+  { "debug", mu_gocs_debug_init },
+  { "auth", mu_gocs_dummy },
+  { NULL }
+};
+
+void
+mu_gocs_register_std (const char *name)
+{
+  mu_gocs_init_fp init = find_init_function (std_gocs_table, name);
+  if (!init)
+    {
+      mu_error (_("INTERNAL ERROR at %s:%d: unknown standard capability `%s'"),
+		__FILE__, __LINE__, name);
+      abort ();
+    }
+  mu_gocs_register (name, init);
+}
+
+
+struct mu_gocs_data
+{
+  char *capa;
+  void *data;
+};
+
+static mu_list_t /* of struct mu_gocs_data */ data_list;
+
+static int
+_gocs_comp (const void *a, const void *b)
+{
+  const struct mu_gocs_data *da = a, *db = b;
+  return !(strcmp (da->capa, db->capa) == 0 && da->data == db->data);
+}
+
+void
+mu_gocs_store (char *capa, void *data)
+{
+  struct mu_gocs_data *s;
+  if (!data_list)
+    {
+      mu_list_create (&data_list);
+      mu_list_set_destroy_item (data_list, mu_list_free_item);
+      mu_list_set_comparator (data_list, _gocs_comp);
+    }
+  s = malloc (sizeof *s);
+  if (!s)
+    {
+      mu_error ("%s", mu_strerror (ENOMEM));
+      exit (1);
+    }
+  s->capa = capa;
+  s->data = data;
+  if (mu_list_locate (data_list, s, NULL) == 0)
+    free (s);
+  else
+    mu_list_prepend (data_list, s);
+}
+
+int
+_gocs_flush (void *item, void *data)
+{
+  struct mu_gocs_data *s = item;
+  mu_gocs_init_fp initfun = find_init_function (_gocs_table, s->capa);
+
+  if (!initfun)
+    {
+      mu_error (_("INTERNAL ERROR at %s:%d: unknown capability `%s'"),
+		__FILE__, __LINE__, s->capa);
+      abort ();
+    }
+
+  if (initfun (mu_gocs_op_set, s->data))
+    {
+      mu_error (_("initialization of GOCS `%s' failed"), s->capa);
+      return 1;
+    }
+  
+  return 0;
+}
+
+void
+mu_gocs_flush ()
+{
+  int i;
+  mu_list_do (data_list, _gocs_flush, NULL);
+
+  for (i = 0; _gocs_table[i].name; i++)
+    _gocs_table[i].init (mu_gocs_op_flush, NULL);
+}
diff --git a/libmailutils/cfg/lexer.l b/libmailutils/cfg/lexer.l
new file mode 100644
index 0000000..83e8351
--- /dev/null
+++ b/libmailutils/cfg/lexer.l
@@ -0,0 +1,405 @@
+%top {
+/* cfg_lexer.l -- default lexer for Mailutils configuration files
+   Copyright (C) 2007, 2008, 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.
+
+   GNU Mailutils 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 GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
+*/
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+}
+
+%{
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <unistd.h>
+#include <mailutils/cctype.h>
+#include <mailutils/errno.h>
+#include <mailutils/error.h>
+#include <mailutils/debug.h>  
+#include <mailutils/argcv.h>
+#include <mailutils/alloc.h>  
+#include <mailutils/nls.h>
+#include <mailutils/cfg.h>
+#include <mailutils/list.h>
+#include <mailutils/mutil.h>
+  
+#include "parser.h"
+
+void _mu_line_begin (void);
+void _mu_line_add (char *text, size_t len);
+char *_mu_line_finish (void);
+
+extern void mu_cfg_set_debug (void);
+static void
+mu_cfg_set_lex_debug (void)
+{
+  yy_flex_debug = mu_debug_check_level (mu_cfg_get_debug (),
+					MU_DEBUG_TRACE2);
+}
+ 
+static void _mu_line_add_unescape_last (char *text, size_t len);
+static void multiline_begin (char *p);
+static char *multiline_strip_tabs (char *text);
+static void multiline_add (char *s);
+static char *multiline_finish (void);
+ 
+static char *multiline_delimiter;
+static size_t multiline_delimiter_len;
+static int multiline_unescape;         /* Unescape here-document contents */
+static int (*char_to_strip)(char);     /* Strip matching characters of each
+					  here-document line */
+static int isemptystr(int off);
+
+static mu_opool_t pool;
+
+%}
+
+%x COMMENT ML STR
+
+WS [ \t\f][ \t\f]*
+ID [a-zA-Z_][a-zA-Z_0-9-]+
+P [1-9][0-9]*
+
+%%
+         /* C-style comments */
+"/*"         BEGIN(COMMENT);
+<COMMENT>[^*\n]*        /* eat anything that's not a '*' */
+<COMMENT>"*"+[^*/\n]*   /* eat up '*'s not followed by '/'s */
+<COMMENT>\n             ++mu_cfg_locus.line;
+<COMMENT>"*"+"/"        BEGIN (INITIAL);
+         /* End-of-line comments */
+#debug=.*\n {
+          mu_log_level_t lev;
+          mu_debug_t dbg = mu_cfg_get_debug ();
+          if (mu_debug_level_from_string (yytext + 7, &lev, dbg) == 0)
+            {
+	      mu_debug_set_level (dbg, lev);
+	      mu_cfg_set_debug ();
+	      mu_cfg_set_lex_debug ();
+	    }
+          }
+#.*\n     { mu_cfg_locus.line++; }
+#.*     /* end-of-file comment */;
+"//".*\n  { mu_cfg_locus.line++; }
+"//".*    /* end-of-file comment */;
+        /* Identifiers */
+<INITIAL>{ID}         {
+                        _mu_line_begin ();
+			_mu_line_add (yytext, yyleng);
+			yylval.string = _mu_line_finish ();
+			return MU_TOK_IDENT; }
+         /* Strings */
+[a-zA-Z0-9_\./:\*=-]+ { _mu_line_begin ();
+                        _mu_line_add (yytext, yyleng);
+                        yylval.string = _mu_line_finish ();
+                        return MU_TOK_STRING; }
+         /* Quoted strings */
+\"[^\\"\n]*\"         { _mu_line_begin ();
+                        _mu_line_add (yytext + 1, yyleng - 2);
+                        yylval.string = _mu_line_finish ();
+                        return MU_TOK_QSTRING; }
+\"[^\\"\n]*\\. |
+\"[^\\"\n]*\\\n        { BEGIN (STR);
+                        _mu_line_begin ();
+		        _mu_line_add_unescape_last (yytext + 1, yyleng - 1); }
+<STR>[^\\"\n]*\\. |
+<STR>\"[^\\"\n]*\\\n  { _mu_line_add_unescape_last (yytext, yyleng); }
+<STR>[^\\"\n]*\"      { BEGIN (INITIAL);
+                        if (yyleng > 1) 
+                          _mu_line_add (yytext, yyleng - 1); 
+                        yylval.string = _mu_line_finish ();
+		        return MU_TOK_QSTRING; }
+         /* Multiline strings */
+"<<"(-" "?)?\\?{ID}[ \t]*#.*\n |
+"<<"(-" "?)?\\?{ID}[ \t]*"//".*\n |
+"<<"(-" "?)?\\?{ID}[ \t]*\n |
+"<<"(-" "?)?\"{ID}\"[ \t]*#.*\n |
+"<<"(-" "?)?\"{ID}\"[ \t]*"//".*\n |
+"<<"(-" "?)?\"{ID}\"[ \t]*\n {
+                        BEGIN (ML);
+			multiline_begin (yytext+2);
+			mu_cfg_locus.line++;
+		  }
+<ML>.*\n { char *p = multiline_strip_tabs (yytext);
+	   
+           if (!strncmp (p, multiline_delimiter, multiline_delimiter_len)
+	       && isemptystr (p + multiline_delimiter_len - yytext))
+	     {
+	       free (multiline_delimiter);
+	       multiline_delimiter = NULL;
+	       BEGIN (INITIAL);
+	       yylval.string = multiline_finish ();
+	       return MU_TOK_MSTRING;
+	     }
+           mu_cfg_locus.line++;
+	   multiline_add (p); } 
+{WS}     ;
+         /* Other tokens */
+\n       { mu_cfg_locus.line++; } 
+[,;{}()] return yytext[0];
+.        { if (mu_isprint (yytext[0]))
+              mu_cfg_parse_error (_("stray character %c"), yytext[0]);
+           else 
+              mu_cfg_parse_error (_("stray character \\%03o"),
+				  (unsigned char) yytext[0]);
+         }
+%%
+
+int
+yywrap ()
+{
+  return 1;
+}
+
+static void
+unescape_to_line (int c)
+{
+  if (c != '\n')
+    {
+      char t = mu_argcv_unquote_char (c);
+      if (t == c && t != '\\' && t != '\"')
+	mu_cfg_parse_error (_("unknown escape sequence '\\%c'"), c);
+      mu_opool_append_char (pool, t);
+    }
+}
+
+void
+_mu_line_add (char *text, size_t len)
+{
+  mu_opool_append (pool, text, len);
+}
+
+void
+_mu_line_add_unescape_last (char *text, size_t len)
+{
+  mu_opool_append (pool, text, len - 2);
+  unescape_to_line (text[len - 1]);
+}
+
+void
+_mu_line_begin ()
+{
+  if (!pool)
+    mu_opool_create (&pool, 1);
+  else
+    mu_opool_clear (pool);
+}
+
+char *
+_mu_line_finish ()
+{
+  mu_opool_append_char (pool, 0);
+  return mu_opool_finish (pool, NULL);
+}
+
+
+
+static int
+is_tab (char c)
+{
+    return c == '\t';
+}
+ 
+static int
+is_ws (char c)
+{
+    return c == '\t' || c == ' ';
+}
+
+static int
+isemptystr (int off)
+{
+  for (; yytext[off] && mu_isspace (yytext[off]); off++)
+    ;
+  if (yytext[off] == ';')
+    {
+      int i;
+      for (i = off + 1; yytext[i]; i++) 
+	if (!mu_isspace (yytext[i]))
+	  return 0;
+      yyless (off);
+      return 1;
+    }
+  return yytext[off] == 0;
+}
+
+static void
+multiline_begin (char *p)
+{
+  if (*p == '-')
+    {
+      if (*++p == ' ')
+	{
+	  char_to_strip = is_ws;
+	  p++;
+	}
+      else
+	char_to_strip = is_tab;
+    }
+  else
+    char_to_strip = NULL;
+  if (*p == '\\')
+    {
+      p++;
+      multiline_unescape = 0;
+    }
+  else if (*p == '"')
+    {
+      char *q;
+      
+      p++;
+      multiline_unescape = 0;
+      q = strchr (p, '"');
+      multiline_delimiter_len = q - p;
+    }
+  else
+    {
+	multiline_delimiter_len = strcspn (p, " \t");
+	multiline_unescape = 1;
+    }
+
+  /* Remove trailing newline */
+  multiline_delimiter_len--;
+  multiline_delimiter = mu_alloc (multiline_delimiter_len + 1);
+  memcpy (multiline_delimiter, p, multiline_delimiter_len);
+  multiline_delimiter[multiline_delimiter_len] = 0;
+  _mu_line_begin ();
+}
+
+static char *
+multiline_strip_tabs (char *text)
+{
+  if (char_to_strip)
+    for (; *text && char_to_strip (*text); text++)
+      ;
+  return text;
+}
+
+static void
+multiline_add (char *s)
+{
+  if (multiline_unescape)
+    {
+      for (; *s; s++)
+	{
+	  if (*s == '\\')
+	    {
+	      unescape_to_line (s[1]);
+	      ++s;
+	    }
+	  else
+	    _mu_line_add (s, 1);
+	}
+    }
+  else
+    _mu_line_add (s, strlen (s));
+}
+
+static char *
+multiline_finish ()
+{
+  return _mu_line_finish ();
+}
+
+
+int
+mu_cfg_parse_file (mu_cfg_tree_t **return_tree, const char *file, int flags)
+{
+  struct stat st;
+  FILE *fp;
+  int rc;
+  char *full_name = mu_tilde_expansion (file, "/", NULL);
+
+  if (stat (full_name, &st))
+    {
+      if (errno != ENOENT)
+	mu_error (_("cannot stat `%s': %s"), full_name, mu_strerror (errno));
+      free (full_name);
+      return ENOENT;
+    }
+  else if (!S_ISREG (st.st_mode))
+    {
+      if (flags & MU_PARSE_CONFIG_VERBOSE)
+	mu_diag_output (MU_DIAG_INFO, _("%s: not a regular file"), full_name);
+      free (full_name);
+      return ENOENT;
+    } 
+      
+  fp = fopen (full_name, "r");
+  if (!fp)
+    {
+      mu_error (_("cannot open config file `%s': %s"), full_name,
+		mu_strerror (errno));
+      free (full_name);
+      return errno;
+    }
+
+  if (flags & MU_PARSE_CONFIG_VERBOSE)
+    mu_diag_output (MU_DIAG_INFO, _("parsing file `%s'"), full_name);
+
+  mu_cfg_set_lex_debug ();
+
+  /* Initialize locus: */
+  /* 1. Save file name in the lexer object pool and point `file' member
+     to this copy. Free full_name: it is not used after that. */
+  _mu_line_begin ();
+  _mu_line_add (full_name, strlen (full_name));
+  mu_cfg_locus.file = _mu_line_finish ();
+  free (full_name); 
+  /* 2. Initialize line number */
+  mu_cfg_locus.line = 1;
+  
+  /* Parse configuration */
+  yyrestart (fp);
+  rc = mu_cfg_parse (return_tree);
+  fclose (fp);
+  if (flags & MU_PARSE_CONFIG_VERBOSE)
+    mu_diag_output (MU_DIAG_INFO, _("finished parsing file `%s'"),
+		    mu_cfg_locus.file);
+
+  return rc == 0 ? 0 : MU_ERR_FAILURE;
+}
+
+/* FIXME: Deprecated interface */
+int
+mu_get_config (const char *file, const char *progname,
+               struct mu_cfg_param *progparam, int flags, void *target_ptr)
+{
+  mu_cfg_tree_t *parse_tree;
+  int rc = mu_cfg_parse_file (&parse_tree, file, flags);
+  if (rc == 0)
+    {
+      rc = mu_cfg_tree_postprocess (parse_tree, flags);
+      if (rc == 0)
+	rc = mu_cfg_tree_reduce (parse_tree, progname, progparam, flags,
+				 target_ptr);
+      mu_cfg_destroy_tree (&parse_tree);
+    }
+
+  return rc == 0 ? 0 : MU_ERR_FAILURE;
+}
+
+
+mu_opool_t 
+mu_cfg_lexer_pool ()
+{
+  mu_opool_t p = pool;
+  pool = NULL;
+  return p;
+}
diff --git a/libmailutils/cfg/parser.y b/libmailutils/cfg/parser.y
new file mode 100644
index 0000000..8c8e060
--- /dev/null
+++ b/libmailutils/cfg/parser.y
@@ -0,0 +1,1839 @@
+%{
+/* cfg_parser.y -- general-purpose configuration file parser
+   Copyright (C) 2007, 2008, 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.
+
+   GNU Mailutils 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 GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
+*/
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <string.h>
+#include <netdb.h>
+#include "intprops.h"
+#include <mailutils/argcv.h>
+#include <mailutils/nls.h>
+#include <mailutils/cfg.h>
+#include <mailutils/alloc.h>
+#include <mailutils/errno.h>
+#include <mailutils/error.h>
+#include <mailutils/list.h>
+#include <mailutils/iterator.h>
+#include <mailutils/debug.h>
+#include <mailutils/mutil.h>
+#include <mailutils/cctype.h>
+
+int mu_cfg_parser_verbose;
+static mu_list_t /* of mu_cfg_node_t */ parse_node_list; 
+mu_cfg_locus_t mu_cfg_locus;
+size_t mu_cfg_error_count;
+
+static int _mu_cfg_errcnt;
+static mu_debug_t _mu_cfg_debug;
+
+int yylex ();
+
+void _mu_line_begin (void);
+void _mu_line_add (char *text, size_t len);
+char *_mu_line_finish (void);
+
+static int
+yyerror (char *s)
+{
+  mu_cfg_parse_error ("%s", s);
+  return 0;
+}
+
+static mu_config_value_t *
+config_value_dup (mu_config_value_t *src)
+{
+  if (!src)
+    return NULL;
+  else
+    {
+      /* FIXME: Use mu_opool_alloc */
+      mu_config_value_t *val = mu_alloc (sizeof (*val));
+      *val = *src;
+      return val;
+    }
+}
+
+static mu_cfg_node_t *
+mu_cfg_alloc_node (enum mu_cfg_node_type type, mu_cfg_locus_t *loc,
+		   const char *tag, mu_config_value_t *label,
+		   mu_list_t nodelist)
+{
+  char *p;
+  mu_cfg_node_t *np;
+  size_t size = sizeof *np + strlen (tag) + 1;
+  np = mu_alloc (size);
+  np->type = type;
+  np->locus = *loc;
+  p = (char*) (np + 1);
+  np->tag = p;
+  strcpy (p, tag);
+  np->label = label;
+  np->nodes = nodelist;
+  return np;
+}
+
+void
+mu_cfg_free_node (mu_cfg_node_t *node)
+{
+  free (node->label);
+  free (node);
+}
+
+void
+mu_cfg_format_error (mu_debug_t debug, size_t level, const char *fmt, ...)
+{
+  va_list ap;
+
+  if (!debug)
+    mu_diag_get_debug (&debug);
+  va_start (ap, fmt);
+  mu_debug_vprintf (debug, 0, fmt, ap);
+  mu_debug_printf (debug, 0, "\n");
+  va_end (ap);
+  if (level <= MU_DEBUG_ERROR)
+    mu_cfg_error_count++;
+}
+
+static void
+_mu_cfg_debug_set_locus (mu_debug_t debug, const mu_cfg_locus_t *loc)
+{
+  mu_debug_set_locus (debug, loc->file ? loc->file : _("unknown file"),
+		      loc->line);
+}
+
+void
+mu_cfg_vperror (mu_debug_t debug, const mu_cfg_locus_t *loc,
+		 const char *fmt, va_list ap)
+{
+  if (!debug)
+    mu_diag_get_debug (&debug);
+  _mu_cfg_debug_set_locus (debug, loc);
+  mu_debug_vprintf (debug, 0, fmt, ap);
+  mu_debug_printf (debug, 0, "\n");
+  mu_debug_set_locus (debug, NULL, 0);
+  mu_cfg_error_count++;
+}
+
+void
+mu_cfg_perror (mu_debug_t debug, const mu_cfg_locus_t *loc,
+	       const char *fmt, ...)
+{
+  va_list ap;
+
+  va_start (ap, fmt);
+  mu_cfg_vperror (debug, loc, fmt, ap);
+  va_end (ap);
+}
+
+void
+mu_cfg_parse_error (const char *fmt, ...)
+{
+  va_list ap;
+
+  va_start (ap, fmt);
+  mu_cfg_vperror (_mu_cfg_debug, &mu_cfg_locus, fmt, ap);
+  va_end (ap);
+}
+
+#define node_type_str(t) (((t) == mu_cfg_node_statement) ? "stmt" : "param")
+
+static void
+debug_print_node (mu_cfg_node_t *node)
+{
+  if (mu_debug_check_level (_mu_cfg_debug, MU_DEBUG_TRACE0))
+    {
+      mu_debug_set_locus (_mu_cfg_debug,
+			  node->locus.file, node->locus.line);
+      if (node->type == mu_cfg_node_undefined)
+	/* Stay on the safe side */
+	mu_cfg_format_error (_mu_cfg_debug, MU_DEBUG_ERROR,
+			     "unknown statement type!");
+      else
+	/* FIXME: How to print label? */
+	mu_cfg_format_error (_mu_cfg_debug, MU_DEBUG_TRACE0,
+			     "statement: %s, id: %s",
+			     node_type_str (node->type),
+			     node->tag ? node->tag : "(null)");
+
+      mu_debug_set_locus (_mu_cfg_debug, NULL, 0);
+    }
+}
+
+static void
+free_node_item (void *item)
+{
+  mu_cfg_node_t *node = item;
+
+  switch (node->type)
+    {
+    case mu_cfg_node_statement:
+      mu_list_destroy (&node->nodes);
+      break;
+      
+    case mu_cfg_node_undefined: /* hmm... */
+    case mu_cfg_node_param:
+      break;
+    }
+  mu_cfg_free_node (node);
+}
+
+int
+mu_cfg_create_node_list (mu_list_t *plist)
+{
+  int rc;
+  mu_list_t list;
+
+  rc = mu_list_create (&list);
+  if (rc)
+    return rc;
+  mu_list_set_destroy_item (list, free_node_item);
+  *plist = list;
+  return 0;
+}
+
+%}
+
+%union {
+  mu_cfg_node_t node;
+  mu_cfg_node_t *pnode;
+  mu_list_t /* of mu_cfg_node_t */ nodelist;
+  char *string;
+  mu_config_value_t value, *pvalue;
+  mu_list_t list;
+  struct { const char *name; mu_cfg_locus_t locus; } ident;
+}
+
+%token <string> MU_TOK_IDENT MU_TOK_STRING MU_TOK_QSTRING MU_TOK_MSTRING
+%type <string> string slist
+%type <list> slist0
+%type <value> value
+%type <pvalue> tag vallist
+%type <list> values list vlist
+%type <ident> ident
+%type <nodelist> stmtlist
+%type <pnode> stmt simple block
+
+%%
+
+input   : stmtlist
+	  {
+	    parse_node_list = $1;
+	  }
+	;
+
+stmtlist: stmt
+	  {
+	    mu_cfg_create_node_list (&$$);
+	    mu_list_append ($$, $1);
+	  }
+	| stmtlist stmt
+	  {
+	    mu_list_append ($1, $2);
+	    $$ = $1;
+	    debug_print_node ($2);
+	  }
+	;
+
+stmt    : simple
+	| block
+	;
+
+simple  : ident vallist ';'
+	  {
+	    $$ = mu_cfg_alloc_node (mu_cfg_node_param, &$1.locus,
+				    $1.name, $2,
+				    NULL);
+	  }
+	;
+
+block   : ident tag '{' '}' opt_sc
+	  {
+	    $$ = mu_cfg_alloc_node (mu_cfg_node_statement, &$1.locus,
+				    $1.name, $2,
+				    NULL);
+
+	  }
+	| ident tag '{' stmtlist '}' opt_sc
+	  {
+	    $$ = mu_cfg_alloc_node (mu_cfg_node_statement, &$1.locus,
+				    $1.name, $2, $4);
+
+	  }
+	;
+
+ident   : MU_TOK_IDENT
+	  {
+	    $$.name = $1;
+	    $$.locus = mu_cfg_locus;
+	  }
+	;
+
+tag     : /* empty */
+	  {
+	    $$ = NULL;
+	  }
+	| vallist
+	;
+
+vallist : vlist
+	  {
+	    size_t n = 0;
+	    mu_list_count($1, &n);
+	    if (n == 1)
+	      {
+		mu_list_get ($1, 0, (void**) &$$);
+	      }
+	    else
+	      {
+		size_t i;
+		mu_config_value_t val;
+
+		val.type = MU_CFG_ARRAY;
+		val.v.arg.c = n;
+		/* FIXME: Use mu_opool_alloc */
+		val.v.arg.v = mu_alloc (n * sizeof (val.v.arg.v[0]));
+		if (!val.v.arg.v)
+		  {
+		    mu_cfg_parse_error (_("not enough memory"));
+		    abort();
+		  }
+
+		for (i = 0; i < n; i++)
+		  {
+		    mu_config_value_t *v;
+		    mu_list_get ($1, i, (void **) &v);
+		    val.v.arg.v[i] = *v;
+		  }
+		$$ = config_value_dup (&val);
+	      }
+	    mu_list_destroy (&$1);
+	  }
+	;
+
+vlist   : value
+	    {
+	      int rc = mu_list_create (&$$);
+	      if (rc)
+		{
+		  mu_cfg_parse_error (_("cannot create list: %s"),
+				      mu_strerror (rc));
+		  abort ();
+		}
+	      mu_list_append ($$, config_value_dup (&$1)); /* FIXME */
+	  }
+	| vlist value
+	  {
+	    mu_list_append ($1, config_value_dup (&$2));
+	  }
+	;
+
+value   : string
+	  {
+	      $$.type = MU_CFG_STRING;
+	      $$.v.string = $1;
+	  }
+	| list
+	  {
+	      $$.type = MU_CFG_LIST;
+	      $$.v.list = $1;
+	  }
+	| MU_TOK_MSTRING
+	  {
+	      $$.type = MU_CFG_STRING;
+	      $$.v.string = $1;
+	  }
+	;
+
+string  : MU_TOK_STRING
+	| MU_TOK_IDENT
+	| slist
+	;
+
+slist   : slist0
+	  {
+	    mu_iterator_t itr;
+	    mu_list_get_iterator ($1, &itr);
+
+	    _mu_line_begin ();
+	    for (mu_iterator_first (itr);
+		 !mu_iterator_is_done (itr); mu_iterator_next (itr))
+	      {
+		char *p;
+		mu_iterator_current (itr, (void**)&p);
+		_mu_line_add (p, strlen (p));
+	      }
+	    $$ = _mu_line_finish ();
+	    mu_iterator_destroy (&itr);
+	    mu_list_destroy(&$1);
+	  }
+	;
+
+slist0  : MU_TOK_QSTRING
+	  {
+	    mu_list_create (&$$);
+	    mu_list_append ($$, $1);
+	  }
+	| slist0 MU_TOK_QSTRING
+	  {
+	    mu_list_append ($1, $2);
+	    $$ = $1;
+	  }
+	;
+
+list    : '(' values ')'
+	  {
+	      $$ = $2;
+	  }
+	| '(' values ',' ')'
+	  {
+	      $$ = $2;
+	  }
+	;
+
+values  : value
+	  {
+	    mu_list_create (&$$);
+	    mu_list_append ($$, config_value_dup (&$1));
+	  }
+	| values ',' value
+	  {
+	    mu_list_append ($1, config_value_dup (&$3));
+	    $$ = $1;
+	  }
+	;
+
+opt_sc  : /* empty */
+	| ';'
+	;
+
+
+%%
+
+static int
+_cfg_default_printer (void *unused, mu_log_level_t level, const char *str)
+{
+  fprintf (stderr, "%s", str);
+  return 0;
+}
+
+mu_debug_t
+mu_cfg_get_debug ()
+{
+  if (!_mu_cfg_debug)
+    {
+      mu_debug_create (&_mu_cfg_debug, NULL);
+      mu_debug_set_print (_mu_cfg_debug, _cfg_default_printer, NULL);
+      mu_debug_set_level (_mu_cfg_debug, mu_global_debug_level ("config"));
+    }
+  return _mu_cfg_debug;
+}
+
+void
+mu_cfg_set_debug ()
+{
+  if (mu_debug_check_level (mu_cfg_get_debug (), MU_DEBUG_TRACE7))
+    yydebug = 1;
+}
+
+int
+mu_cfg_parse (mu_cfg_tree_t **ptree)
+{
+  int rc;
+  mu_cfg_tree_t *tree;
+  mu_opool_t pool;
+  
+  mu_cfg_set_debug ();
+  _mu_cfg_errcnt = 0;
+
+  rc = yyparse ();
+  pool = mu_cfg_lexer_pool ();
+  if (rc == 0 && _mu_cfg_errcnt)
+    {
+      mu_opool_destroy (&pool);
+      rc = 1;
+    }
+  else
+    {
+      tree = mu_alloc (sizeof (*tree));
+      tree->debug = _mu_cfg_debug;
+      _mu_cfg_debug = NULL;
+      tree->nodes = parse_node_list;
+      tree->pool = pool;
+      parse_node_list = NULL;
+      *ptree = tree;
+    }
+  return rc;
+}
+
+int
+mu_cfg_tree_union (mu_cfg_tree_t **pa, mu_cfg_tree_t **pb)
+{
+  mu_cfg_tree_t *a, *b;
+  int rc;
+  
+  if (!pb)
+    return EINVAL;
+  if (!*pb)
+    return 0;
+  b = *pb;
+  if (!pa)
+    return EINVAL;
+  if (!*pa)
+    {
+      *pa = b;
+      *pb = NULL;
+      return 0;
+    }
+  else
+    a = *pa;
+  
+  /* Merge opools */
+  rc = mu_opool_union (&b->pool, &a->pool);
+  if (rc)
+    return rc;
+    
+  /* Link node lists */
+  if (b->nodes)
+    {
+      mu_list_append_list (a->nodes, b->nodes);
+      mu_list_destroy (&b->nodes);
+    }
+  
+  mu_debug_destroy (&b->debug, mu_debug_get_owner (b->debug));
+  free (b);
+  *pb = NULL;
+  return 0;
+}
+
+static mu_cfg_tree_t *
+do_include (const char *name, int flags, mu_cfg_locus_t *loc)
+{
+  struct stat sb;
+  char *tmpname = NULL;
+  mu_cfg_tree_t *tree = NULL;
+  
+  if (name[0] != '/')
+    {
+      name = tmpname = mu_make_file_name (SYSCONFDIR, name);
+      if (!name)
+        {
+          mu_error ("%s", mu_strerror (errno));
+          return NULL;
+        }
+    }
+  if (stat (name, &sb) == 0)
+    {
+      int rc = 0;
+
+      if (S_ISDIR (sb.st_mode))
+	{
+	  if (flags & MU_PARSE_CONFIG_GLOBAL)
+	    {
+	      char *file = mu_make_file_name (name, mu_program_name);
+	      rc = mu_cfg_parse_file (&tree, file, flags);
+	      free (file);
+	    }
+	}
+      else
+	rc = mu_cfg_parse_file (&tree, name, flags);
+	      
+      if (rc == 0 && tree)
+	mu_cfg_tree_postprocess (tree, flags & ~MU_PARSE_CONFIG_GLOBAL);
+    }
+  else if (errno == ENOENT)
+    mu_cfg_perror (tree->debug, loc,
+		   _("include file or directory does not exist"));
+  else
+    mu_cfg_perror (tree->debug, loc,
+		   _("cannot stat include file or directory: %s"),
+		   mu_strerror (errno));
+  free (tmpname);
+  return tree;
+}
+    
+int
+mu_cfg_tree_postprocess (mu_cfg_tree_t *tree, int flags)
+{
+  int rc;
+  mu_iterator_t itr;
+
+  if (!tree->nodes)
+    return 0;
+  rc = mu_list_get_iterator (tree->nodes, &itr);
+  if (rc)
+    return rc;
+  for (mu_iterator_first (itr); !mu_iterator_is_done (itr);
+       mu_iterator_next (itr))
+    {
+      mu_cfg_node_t *node;
+
+      mu_iterator_current (itr, (void**) &node);
+
+      if (node->type == mu_cfg_node_statement)
+	{
+	  if ((flags & MU_PARSE_CONFIG_GLOBAL) &&
+	      strcmp (node->tag, "program") == 0)
+	    {
+	      if (node->label->type == MU_CFG_STRING)
+		{
+		  if (strcmp (node->label->v.string, mu_program_name) == 0)
+		    {
+		      /* Move all nodes from this block to the topmost
+			 level */
+		      mu_iterator_ctl (itr, mu_itrctl_insert_list,
+				       node->nodes);
+		      mu_iterator_ctl (itr, mu_itrctl_delete, NULL);
+		      /*FIXME:mu_cfg_free_node (node);*/
+		    }
+		}
+	      else
+		{
+		  mu_cfg_perror (tree->debug, &node->locus,
+				 _("argument to `program' is not a string"));
+		  mu_iterator_ctl (itr, mu_itrctl_delete, NULL);
+		}
+	    }
+	}
+      else if (node->type == mu_cfg_node_param &&
+	       strcmp (node->tag, "include") == 0)
+	{
+	  if (node->label->type == MU_CFG_STRING)
+	    {
+	      mu_cfg_tree_t *t = do_include (node->label->v.string, flags,
+					     &node->locus);
+	      if (t)
+		{
+		  /* Merge the new tree into the current point and
+		     destroy the rest of it */
+		  mu_iterator_ctl (itr, mu_itrctl_insert_list, t->nodes);
+		  mu_opool_union (&tree->pool, &t->pool);
+		  mu_cfg_destroy_tree (&t);
+		}		      
+	    }
+	  else
+	    mu_cfg_perror (tree->debug, &node->locus,
+			   _("argument to `include' is not a string"));
+	  /* Remove node from the list */
+	  mu_iterator_ctl (itr, mu_itrctl_delete, NULL);
+	}
+    }
+  return 0;
+}
+
+static int
+_mu_cfg_preorder_recursive (void *item, void *cbdata)
+{
+  mu_cfg_node_t *node = item;
+  struct mu_cfg_iter_closure *clos = cbdata;
+
+  switch (node->type)
+    {
+    case mu_cfg_node_undefined:
+      abort ();
+
+    case mu_cfg_node_statement:
+      switch (clos->beg (node, clos->data))
+	{
+	case MU_CFG_ITER_OK:
+	  if (mu_cfg_preorder (node->nodes, clos))
+	    return 1;
+	  if (clos->end && clos->end (node, clos->data) == MU_CFG_ITER_STOP)
+	    return 1;
+	  break;
+
+	case MU_CFG_ITER_SKIP:
+	  break;
+
+	case MU_CFG_ITER_STOP:
+	  return 1;
+	}
+      break;
+
+    case mu_cfg_node_param:
+      return clos->beg (node, clos->data) == MU_CFG_ITER_STOP;
+    }
+  return 0;
+}
+
+int
+mu_cfg_preorder (mu_list_t nodelist, struct mu_cfg_iter_closure *clos)
+{
+  return mu_list_do (nodelist, _mu_cfg_preorder_recursive, clos);
+}
+
+
+
+void
+mu_cfg_destroy_tree (mu_cfg_tree_t **ptree)
+{
+  if (ptree && *ptree)
+    {
+      mu_cfg_tree_t *tree = *ptree;
+      mu_list_destroy (&tree->nodes);
+      mu_opool_destroy (&tree->pool);
+      *ptree = NULL;
+    }
+}
+
+
+
+struct mu_cfg_section_list
+{
+  struct mu_cfg_section_list *next;
+  struct mu_cfg_section *sec;
+};
+
+struct scan_tree_data
+{
+  struct mu_cfg_section_list *list;
+  void *target;
+  void *call_data;
+  mu_cfg_tree_t *tree;
+  int error;
+};
+
+static struct mu_cfg_cont *
+find_container (mu_list_t list, enum mu_cfg_cont_type type,
+		const char *ident, size_t len)
+{
+  mu_iterator_t iter;
+  struct mu_cfg_cont *ret = NULL;
+
+  if (len == 0)
+    len = strlen (ident);
+
+  mu_list_get_iterator (list, &iter);
+  for (mu_iterator_first (iter); !mu_iterator_is_done (iter);
+       mu_iterator_next (iter))
+    {
+      struct mu_cfg_cont *cont;
+      mu_iterator_current (iter, (void**) &cont);
+
+      if (cont->type == type
+	  && strlen (cont->v.ident) == len
+	  && memcmp (cont->v.ident, ident, len) == 0)
+	{
+	  ret = cont;
+	  break;
+	}
+    }
+  mu_iterator_destroy (&iter);
+  return ret;
+}
+
+static struct mu_cfg_section *
+find_subsection (struct mu_cfg_section *sec, const char *ident, size_t len)
+{
+  if (sec)
+    {
+      if (sec->children)
+	{
+	  struct mu_cfg_cont *cont = find_container (sec->children,
+						     mu_cfg_cont_section,
+						     ident, len);
+	  if (cont)
+	    return &cont->v.section;
+	}
+    }
+  return NULL;
+}
+
+static struct mu_cfg_param *
+find_param (struct mu_cfg_section *sec, const char *ident, size_t len)
+{
+  if (sec)
+    {
+      if (sec->children)
+	{
+	  struct mu_cfg_cont *cont = find_container (sec->children,
+						     mu_cfg_cont_param,
+						     ident, len);
+	  if (cont)
+	    return &cont->v.param;
+	}
+    }
+  return NULL;
+}
+
+static int
+push_section (struct scan_tree_data *dat, struct mu_cfg_section *sec)
+{
+  struct mu_cfg_section_list *p = mu_alloc (sizeof *p);
+  if (!p)
+    {
+      mu_cfg_perror (dat->tree->debug, NULL, _("not enough memory"));
+      return 1;
+    }
+  p->sec = sec;
+  p->next = dat->list;
+  dat->list = p;
+  return 0;
+}
+
+static struct mu_cfg_section *
+pop_section (struct scan_tree_data *dat)
+{
+  struct mu_cfg_section_list *p = dat->list;
+  struct mu_cfg_section *sec = p->sec;
+  dat->list = p->next;
+  free (p);
+  return sec;
+}
+
+#define STRTONUM(s, type, base, res, limit, d, loc)			\
+  {									\
+    type sum = 0;							\
+									\
+    for (; *s; s++)							\
+      {									\
+	type x;								\
+									\
+	if ('0' <= *s && *s <= '9')					\
+	  x = sum * base + *s - '0';					\
+	else if (base == 16 && 'a' <= *s && *s <= 'f')			\
+	  x = sum * base + *s - 'a';					\
+	else if (base == 16 && 'A' <= *s && *s <= 'F')			\
+	  x = sum * base + *s - 'A';					\
+	else								\
+	  break;							\
+	if (x <= sum)							\
+	  {								\
+	    mu_cfg_perror (d, loc, _("numeric overflow"));		\
+	    return 1;							\
+	  }								\
+	else if (limit && x > limit)					\
+	  {								\
+	    mu_cfg_perror (d, loc,					\
+			    _("value out of allowed range"));		\
+	    return 1;							\
+	  }								\
+	sum = x;							\
+      }									\
+    res = sum;								\
+  }
+
+#define STRxTONUM(s, type, res, limit, d, loc)				\
+  {									\
+    int base;								\
+    if (*s == '0')							\
+      {									\
+	s++;								\
+	if (*s == 0)							\
+	  base = 10;							\
+	else if (*s == 'x' || *s == 'X')				\
+	  {								\
+	    s++;							\
+	    base = 16;							\
+	  }								\
+	else								\
+	  base = 8;							\
+      } else								\
+      base = 10;							\
+    STRTONUM (s, type, base, res, limit, d, loc);			\
+  }
+
+#define GETUNUM(str, type, res, d, loc)					\
+  {									\
+    type tmpres;							\
+    const char *s = str;						\
+    STRxTONUM (s, type, tmpres, 0, d, loc);				\
+    if (*s)								\
+      {									\
+	mu_cfg_perror (d, loc,						\
+		       _("not a number (stopped near `%s')"),		\
+		       s);						\
+	return 1;							\
+      }									\
+    res = tmpres;							\
+  }
+
+#define GETSNUM(str, type, res, d, loc)					\
+  {									\
+    unsigned type tmpres;						\
+    const char *s = str;						\
+    int sign;								\
+    unsigned type limit;						\
+									\
+    if (*s == '-')							\
+      {									\
+	sign++;								\
+	s++;								\
+	limit = TYPE_MINIMUM (type);					\
+	limit = - limit;						\
+      }									\
+    else								\
+      {									\
+	sign = 0;							\
+	limit = TYPE_MAXIMUM (type);					\
+      }									\
+									\
+    STRxTONUM (s, unsigned type, tmpres, limit, d, loc);		\
+    if (*s)								\
+      {									\
+	mu_cfg_perror (d, loc,						\
+		       _("not a number (stopped near `%s')"),		\
+		       s);						\
+	return 1;							\
+      }									\
+    res = sign ? - tmpres : tmpres;					\
+  }
+
+static int
+parse_ipv4 (struct scan_tree_data *sdata, const mu_cfg_locus_t *locus,
+	    const char *str, struct in_addr *res)
+{
+  struct in_addr addr;
+  if (inet_aton (str, &addr) == 0)
+    {
+      mu_cfg_perror (sdata->tree->debug, locus, _("not an IPv4"));
+      return 1;
+    }
+  addr.s_addr = ntohl (addr.s_addr);
+  *res = addr;
+  return 0;
+}
+
+static int
+parse_host (struct scan_tree_data *sdata, const mu_cfg_locus_t *locus,
+	    const char *str, struct in_addr *res)
+{
+  struct in_addr addr;
+  struct hostent *hp = gethostbyname (str);
+  if (hp)
+    {
+      addr.s_addr = *(unsigned long *)hp->h_addr;
+    }
+  else if (inet_aton (str, &addr) == 0)
+    {
+      mu_cfg_perror (sdata->tree->debug, locus,
+		     _("cannot resolve hostname `%s'"),
+		     str);
+      return 1;
+    }
+  addr.s_addr = ntohl (addr.s_addr);
+  *res = addr;
+  return 0;
+}
+
+static int
+parse_cidr (struct scan_tree_data *sdata, const mu_cfg_locus_t *locus,
+	    const char *str, mu_cfg_cidr_t *res)
+{
+  struct in_addr addr;
+  unsigned long mask;
+  char astr[16];
+  const char *p, *s;
+
+  p = strchr (str, '/');
+  if (p)
+    {
+      int len = p - str;
+      if (len > sizeof astr - 1) {
+	mu_cfg_perror (sdata->tree->debug, locus,
+		       _("not a valid IPv4 address in CIDR"));
+	return 1;
+      }
+      memcpy (astr, str, len);
+      astr[len] = 0;
+      if (inet_aton (astr, &addr) == 0)
+	{
+	  mu_cfg_perror (sdata->tree->debug, locus,
+			 _("not a valid IPv4 address in CIDR"));
+	  return 1;
+	}
+      addr.s_addr = ntohl (addr.s_addr);
+
+      p++;
+      s = p;
+      STRxTONUM (s, unsigned long, mask, 0, sdata->tree->debug, locus);
+      if (*s == '.')
+	{
+	  struct in_addr a;
+	  if (inet_aton (p, &a) == 0)
+	    {
+	      mu_cfg_perror (sdata->tree->debug, locus,
+			     _("not a valid network in CIDR"));
+	      return 1;
+	    }
+	  a.s_addr = ntohl (a.s_addr);
+	  for (mask = 0; (a.s_addr & 1) == 0 && mask < 32; )
+	    {
+	      mask++;
+	      a.s_addr >>= 1;
+	    }
+	  mask = 32 - mask;
+	}
+      else if (mask > 32)
+	{
+	  mu_cfg_perror (sdata->tree->debug, locus,
+			 _("not a valid network mask in CIDR"));
+	  return 1;
+	}
+    }
+  else
+    {
+      int i;
+      unsigned short x;
+      addr.s_addr = 0;
+
+      p = str;
+      for (i = 0; i < 3; i++)
+	{
+	  STRxTONUM (p, unsigned short, x, 255, sdata->tree->debug, locus);
+	  if (*p != '.')
+	    break;
+	  addr.s_addr = (addr.s_addr << 8) + x;
+	}
+
+      if (*p)
+	{
+	  mu_cfg_perror (sdata->tree->debug, locus,
+			 _("not a CIDR (stopped near `%s')"),
+			 p);
+	  return 1;
+	}
+
+      mask = i * 8;
+
+      addr.s_addr <<= (4 - i) * 8;
+    }
+
+  res->addr = addr;
+  res->mask = mask;
+  return 0;
+}
+
+int
+mu_cfg_parse_boolean (const char *str, int *res)
+{
+  if (strcmp (str, "yes") == 0
+      || strcmp (str, "on") == 0
+      || strcmp (str, "t") == 0
+      || strcmp (str, "true") == 0
+      || strcmp (str, "1") == 0)
+    *res = 1;
+  else if (strcmp (str, "no") == 0
+	   || strcmp (str, "off") == 0
+	   || strcmp (str, "nil") == 0
+	   || strcmp (str, "false") == 0
+	   || strcmp (str, "0") == 0)
+    *res = 0;
+  else
+    return 1;
+  return 0;
+}
+
+static int
+parse_bool (struct scan_tree_data *sdata, const mu_cfg_locus_t *locus,
+	    const char *str, int *res)
+{
+  if (mu_cfg_parse_boolean (str, res))
+    {
+      mu_cfg_perror (sdata->tree->debug, locus, _("not a boolean"));
+      return 1;
+    }
+  return 0;
+}
+
+static int
+valcvt (struct scan_tree_data *sdata, const mu_cfg_locus_t *locus,
+	void *tgt,
+	enum mu_cfg_param_data_type type, mu_config_value_t *val)
+{
+  if (val->type != MU_CFG_STRING)
+    {
+      mu_cfg_perror (sdata->tree->debug, locus, _("expected string value"));
+      return 1;
+    }
+  switch (type)
+    {
+    case mu_cfg_string:
+      {
+	char *s = mu_strdup (val->v.string);
+	/* FIXME: free tgt? */
+	*(char**)tgt = s;
+	break;
+      }
+
+    case mu_cfg_short:
+      GETSNUM (val->v.string, short, *(short*)tgt, sdata->tree->debug, locus);
+      break;
+
+    case mu_cfg_ushort:
+      GETUNUM (val->v.string, unsigned short, *(unsigned short*)tgt,
+	       sdata->tree->debug, locus);
+      break;
+
+    case mu_cfg_int:
+      GETSNUM (val->v.string, int, *(int*)tgt, sdata->tree->debug, locus);
+      break;
+
+    case mu_cfg_uint:
+      GETUNUM (val->v.string, unsigned int, *(unsigned int*)tgt,
+	       sdata->tree->debug, locus);
+      break;
+
+    case mu_cfg_long:
+      GETSNUM (val->v.string, long, *(long*)tgt,
+	       sdata->tree->debug, locus);
+      break;
+
+    case mu_cfg_ulong:
+      GETUNUM (val->v.string, unsigned long, *(unsigned long*)tgt,
+	       sdata->tree->debug, locus);
+      break;
+
+    case mu_cfg_size:
+      GETUNUM (val->v.string, size_t, *(size_t*)tgt,
+	       sdata->tree->debug, locus);
+      break;
+
+    case mu_cfg_off:
+      mu_cfg_perror (sdata->tree->debug, locus, _("not implemented yet"));
+      /* GETSNUM(node->tag_label, off_t, *(off_t*)tgt); */
+      return 1;
+
+    case mu_cfg_time:
+      GETUNUM (val->v.string, time_t, *(time_t*)tgt,
+	       sdata->tree->debug, locus);
+      break;
+
+    case mu_cfg_bool:
+      if (parse_bool (sdata, locus, val->v.string, (int*) tgt))
+	return 1;
+      break;
+
+    case mu_cfg_ipv4:
+      if (parse_ipv4 (sdata, locus, val->v.string, (struct in_addr *)tgt))
+	return 1;
+      break;
+
+    case mu_cfg_cidr:
+      if (parse_cidr (sdata, locus, val->v.string, (mu_cfg_cidr_t *)tgt))
+	return 1;
+      break;
+
+    case mu_cfg_host:
+      if (parse_host (sdata, locus, val->v.string, (struct in_addr *)tgt))
+	return 1;
+      break;
+
+    default:
+      return 1;
+    }
+  return 0;
+}
+
+struct set_closure
+{
+  mu_list_t list;
+  enum mu_cfg_param_data_type type;
+  struct scan_tree_data *sdata;
+  const mu_cfg_locus_t *locus;
+};
+
+static size_t config_type_size[] = {
+  sizeof (char*),          /* mu_cfg_string */
+  sizeof (short),          /* mu_cfg_short */
+  sizeof (unsigned short), /* mu_cfg_ushort */
+  sizeof (int),            /* mu_cfg_int */
+  sizeof (unsigned),       /* mu_cfg_uint */
+  sizeof (long),           /* mu_cfg_long */
+  sizeof (unsigned long),  /* mu_cfg_ulong */
+  sizeof (size_t),         /* mu_cfg_size */
+  sizeof (mu_off_t),       /* mu_cfg_off */
+  sizeof (time_t),         /* mu_cfg_time */
+  sizeof (int),            /* mu_cfg_bool */
+  sizeof (struct in_addr), /* mu_cfg_ipv4 */
+  sizeof (mu_cfg_cidr_t),  /* mu_cfg_cidr */
+  sizeof (struct in_addr), /* mu_cfg_host */
+  0,                       /* mu_cfg_callback */
+  0,                       /* mu_cfg_section */
+}  ;
+
+static int
+_set_fun (void *item, void *data)
+{
+  mu_config_value_t *val = item;
+  struct set_closure *clos = data;
+  void *tgt;
+  size_t size;
+
+  if (clos->type >= MU_ARRAY_SIZE(config_type_size)
+      || (size = config_type_size[clos->type]) == 0)
+    {
+    mu_cfg_perror (clos->sdata->tree->debug, clos->locus,
+		   _("INTERNAL ERROR at %s:%d: unhandled data type %d"),
+		   __FILE__, __LINE__, clos->type);
+    return 1;
+    }
+
+  tgt = mu_alloc (size);
+  if (!tgt)
+    {
+      mu_cfg_perror (clos->sdata->tree->debug, clos->locus,
+		     _("not enough memory"));
+      return 1;
+    }
+
+  if (valcvt (clos->sdata, clos->locus, &tgt, clos->type, val) == 0)
+    mu_list_append (clos->list, tgt);
+  return 0;
+}
+
+static int
+parse_param (struct scan_tree_data *sdata, const mu_cfg_node_t *node)
+{
+  void *tgt;
+  struct set_closure clos;
+  struct mu_cfg_param *param = find_param (sdata->list->sec, node->tag,
+					   0);
+
+  if (!param)
+    {
+      mu_cfg_perror (sdata->tree->debug, &node->locus,
+		     _("unknown keyword `%s'"),
+		     node->tag);
+      return 1;
+    }
+
+  if (param->data)
+    tgt = param->data;
+  else if (sdata->list->sec->target)
+    tgt = (char*)sdata->list->sec->target + param->offset;
+  else if (sdata->target)
+    tgt = (char*)sdata->target + param->offset;
+  else if (param->type == mu_cfg_callback)
+    tgt = NULL;
+  else
+    {
+      mu_cfg_perror (sdata->tree->debug, &node->locus,
+		     _("INTERNAL ERROR: cannot determine target offset for "
+		       "%s"), param->ident);
+      abort ();
+    }
+
+  memset (&clos, 0, sizeof clos);
+  clos.type = MU_CFG_TYPE (param->type);
+  if (MU_CFG_IS_LIST (param->type))
+    {
+      clos.sdata = sdata;
+      clos.locus = &node->locus;
+      switch (node->label->type)
+	{
+	case MU_CFG_LIST:
+	  break;
+
+	case MU_CFG_STRING:
+	  {
+	    mu_list_t list;
+	    mu_list_create (&list);
+	    mu_list_append (list, config_value_dup (node->label));
+	    node->label->type = MU_CFG_LIST;
+	    node->label->v.list = list;
+	  }
+	  break;
+
+	case MU_CFG_ARRAY:
+	  mu_cfg_perror (sdata->tree->debug, &node->locus,
+			 _("expected list, but found array"));
+	  return 1;
+	}
+
+      mu_list_create (&clos.list);
+      mu_list_do (node->label->v.list, _set_fun, &clos);
+      *(mu_list_t*)tgt = clos.list;
+    }
+  else if (clos.type == mu_cfg_callback)
+    {
+      mu_debug_set_locus (sdata->tree->debug, node->locus.file,
+			  node->locus.line);
+      if (!param->callback)
+	{
+	  mu_cfg_perror (sdata->tree->debug, &node->locus,
+			 _("INTERNAL ERROR: %s: callback not defined"),
+			 node->tag);
+	  abort ();
+	}
+      if (param->callback (sdata->tree->debug, tgt, node->label))
+	return 1;
+
+    }
+  else
+    return valcvt (sdata, &node->locus, tgt, clos.type, node->label);
+
+  return 0;
+}
+
+
+static int
+_scan_tree_helper (const mu_cfg_node_t *node, void *data)
+{
+  struct scan_tree_data *sdata = data;
+  struct mu_cfg_section *sec;
+
+  switch (node->type)
+    {
+    case mu_cfg_node_undefined:
+      abort ();
+
+    case mu_cfg_node_statement:
+      sec = find_subsection (sdata->list->sec, node->tag, 0);
+      if (!sec)
+	{
+	  if (mu_cfg_parser_verbose)
+	    {
+	      _mu_cfg_debug_set_locus (sdata->tree->debug, &node->locus);
+	      mu_cfg_format_error (sdata->tree->debug, MU_DIAG_WARNING,
+				   _("unknown section `%s'"),
+				   node->tag);
+	    }
+	  return MU_CFG_ITER_SKIP;
+	}
+      if (!sec->children)
+	return MU_CFG_ITER_SKIP;
+      if (sdata->list->sec->target)
+	sec->target = (char*)sdata->list->sec->target + sec->offset;
+      else if (sdata->target)
+	sec->target = (char*)sdata->target + sec->offset;
+      if (sec->parser)
+	{
+	  mu_debug_set_locus (sdata->tree->debug,
+			      node->locus.file ?
+				     node->locus.file : _("unknown file"),
+			      node->locus.line);
+	  if (sec->parser (mu_cfg_section_start, node,
+			   sec->label, &sec->target,
+			   sdata->call_data, sdata->tree))
+	    {
+	      sdata->error++;
+	      return MU_CFG_ITER_SKIP;
+	    }
+	}
+      push_section(sdata, sec);
+      break;
+
+    case mu_cfg_node_param:
+      if (parse_param (sdata, node))
+	{
+	  sdata->error++;
+	  return MU_CFG_ITER_SKIP;
+	}
+      break;
+    }
+  return MU_CFG_ITER_OK;
+}
+
+static int
+_scan_tree_end_helper (const mu_cfg_node_t *node, void *data)
+{
+  struct scan_tree_data *sdata = data;
+  struct mu_cfg_section *sec;
+
+  switch (node->type)
+    {
+    default:
+      abort ();
+
+    case mu_cfg_node_statement:
+      sec = pop_section (sdata);
+      if (sec && sec->parser)
+	{
+	  if (sec->parser (mu_cfg_section_end, node,
+			   sec->label, &sec->target,
+			   sdata->call_data, sdata->tree))
+	    {
+	      sdata->error++;
+	      return MU_CFG_ITER_SKIP;
+	    }
+	}
+    }
+  return MU_CFG_ITER_OK;
+}
+
+int
+mu_cfg_scan_tree (mu_cfg_tree_t *tree, struct mu_cfg_section *sections,
+		  void *target, void *data)
+{
+  mu_debug_t debug = NULL;
+  struct scan_tree_data dat;
+  struct mu_cfg_iter_closure clos;
+    
+  dat.tree = tree;
+  dat.list = NULL;
+  dat.error = 0;
+  dat.call_data = data;
+  dat.target = target;
+  
+  if (!tree->debug)
+    {
+      mu_diag_get_debug (&debug);
+      tree->debug = debug;
+    }
+  if (push_section (&dat, sections))
+    return 1;
+  clos.beg = _scan_tree_helper;
+  clos.end = _scan_tree_end_helper;
+  clos.data = &dat;
+  mu_cfg_preorder (tree->nodes, &clos);
+  if (debug)
+    {
+      mu_debug_set_locus (debug, NULL, 0);
+      tree->debug = NULL;
+    }
+  pop_section (&dat);
+  return dat.error;
+}
+
+int
+mu_cfg_find_section (struct mu_cfg_section *root_sec,
+		     const char *path, struct mu_cfg_section **retval)
+{
+  while (path[0])
+    {
+      struct mu_cfg_section *sec;
+      size_t len;
+      const char *p;
+
+      while (*path == MU_CFG_PATH_DELIM)
+	path++;
+
+      if (*path == 0)
+	return MU_ERR_NOENT;
+
+      p = strchr (path, MU_CFG_PATH_DELIM);
+      if (p)
+	len = p - path;
+      else
+	len = strlen (path);
+
+      sec = find_subsection (root_sec, path, len);
+      if (!sec)
+	return MU_ERR_NOENT;
+      root_sec = sec;
+      path += len;
+    }
+  if (retval)
+    *retval = root_sec;
+  return 0;
+}
+
+
+int
+mu_cfg_tree_create (struct mu_cfg_tree **ptree)
+{
+  struct mu_cfg_tree *tree = calloc (1, sizeof *tree);
+  if (!tree)
+    return errno;
+  mu_opool_create (&tree->pool, 1);
+  *ptree = tree;
+  return 0;
+}
+
+void
+mu_cfg_tree_set_debug (struct mu_cfg_tree *tree, mu_debug_t debug)
+{
+  tree->debug = debug;
+}
+
+mu_cfg_node_t *
+mu_cfg_tree_create_node (struct mu_cfg_tree *tree,
+			 enum mu_cfg_node_type type,
+			 const mu_cfg_locus_t *loc,
+			 const char *tag, const char *label,
+			 mu_list_t nodelist)
+{
+  char *p;
+  mu_cfg_node_t *np;
+  size_t size = sizeof *np + strlen (tag) + 1;
+  mu_config_value_t val;
+
+  np = mu_alloc (size);
+  np->type = type;
+  if (loc)
+    np->locus = *loc;
+  else
+    memset (&np->locus, 0, sizeof np->locus);
+  p = (char*) (np + 1);
+  np->tag = p;
+  strcpy (p, tag);
+  p += strlen (p) + 1;
+  val.type = MU_CFG_STRING;
+  if (label)
+    {
+      mu_opool_clear (tree->pool);
+      mu_opool_appendz (tree->pool, label);
+      val.v.string = mu_opool_finish (tree->pool, NULL);
+      np->label = config_value_dup (&val);
+    }
+  else
+    np->label = NULL;
+  np->nodes = nodelist;
+  return np;
+}
+
+void
+mu_cfg_tree_add_node (mu_cfg_tree_t *tree, mu_cfg_node_t *node)
+{
+  if (!node)
+    return;
+  if (!tree->nodes)
+    /* FIXME: return code? */
+    mu_cfg_create_node_list (&tree->nodes);
+  mu_list_append (tree->nodes, node);
+}
+
+void
+mu_cfg_tree_add_nodelist (mu_cfg_tree_t *tree, mu_list_t nodelist)
+{
+  if (!nodelist)
+    return;
+  if (!tree->nodes)
+    /* FIXME: return code? */
+    mu_cfg_create_node_list (&tree->nodes);
+  mu_list_append_list (tree->nodes, nodelist);
+}
+
+
+/* Return 1 if configuration value A equals B */
+int
+mu_cfg_value_eq (mu_config_value_t *a, mu_config_value_t *b)
+{
+  if (a->type != b->type)
+    return 0;
+  switch (a->type)
+    {
+    case MU_CFG_STRING:
+      if (a->v.string == NULL)
+	return b->v.string == NULL;
+      return strcmp (a->v.string, b->v.string) == 0;
+		     
+    case MU_CFG_LIST:
+      {
+	int ret = 1;
+	size_t cnt;
+	size_t i;
+	mu_iterator_t aitr, bitr;
+	
+	mu_list_count (a->v.list, &cnt);
+	mu_list_count (b->v.list, &i);
+	if (i != cnt)
+	  return 1;
+
+	mu_list_get_iterator (a->v.list, &aitr);
+	mu_list_get_iterator (b->v.list, &bitr);
+	for (i = 0,
+	       mu_iterator_first (aitr),
+	       mu_iterator_first (bitr);
+	     !mu_iterator_is_done (aitr) && !mu_iterator_is_done (bitr);
+	     mu_iterator_next (aitr),
+	       mu_iterator_next (bitr),
+	       i++)
+	  {
+	    mu_config_value_t *ap, *bp;
+	    mu_iterator_current (aitr, (void**)&ap);
+	    mu_iterator_current (bitr, (void**)&bp);
+	    ret = mu_cfg_value_eq (ap, bp);
+	    if (!ret)
+	      break;
+	  }
+	mu_iterator_destroy (&aitr);
+	mu_iterator_destroy (&bitr);
+	return ret && i == cnt;
+      }
+	
+    case MU_CFG_ARRAY:
+      if (a->v.arg.c == b->v.arg.c)
+	{
+	  size_t i;
+	  for (i = 0; i < a->v.arg.c; i++)
+	    if (!mu_cfg_value_eq (&a->v.arg.v[i], &b->v.arg.v[i]))
+	      return 0;
+	  return 1;
+	}
+    }
+  return 0;
+}
+
+
+static int
+split_cfg_path (const char *path, int *pargc, char ***pargv)
+{
+  int rc;
+  int argc;
+  char **argv;
+  char *delim = MU_CFG_PATH_DELIM_STR;
+  char static_delim[2] = { 0, 0 };
+  
+  if (path[0] == '\\')
+    {
+      argv = calloc (2, sizeof (*argv));
+      if (!argv)
+	return ENOMEM;
+      argv[0] = strdup (path + 1);
+      if (!argv[0])
+	{
+	  free (argv);
+	  return ENOMEM;
+	}
+      argv[1] = NULL;
+      argc = 1;
+      rc = 0;
+    }
+  else
+    {
+      if (mu_ispunct (path[0]))
+	{
+	  delim = static_delim;
+	  delim[0] = path[0];
+	  path++;
+	}
+      rc = mu_argcv_get_np (path, strlen (path), delim, NULL, 0,
+			    &argc, &argv, NULL);
+    }
+  if (rc == 0)
+    {
+      *pargc = argc;
+      *pargv = argv;
+    }
+  return rc;
+}
+
+struct find_data
+{
+  int argc;
+  char **argv;
+  int tag;
+  mu_config_value_t *label;
+  const mu_cfg_node_t *node;
+};
+
+static void
+free_value_mem (mu_config_value_t *p)
+{
+  switch (p->type)
+    {
+    case MU_CFG_STRING:
+      free ((char*)p->v.string);
+      break;
+      
+    case MU_CFG_LIST:
+      /* FIXME */
+      break;
+      
+    case MU_CFG_ARRAY:
+      {
+	size_t i;
+	for (i = 0; i < p->v.arg.c; i++)
+	  free_value_mem (&p->v.arg.v[i]);
+      }
+    }
+}
+
+static void
+destroy_value (void *p)
+{
+  mu_config_value_t *val = p;
+  if (val)
+    {
+      free_value_mem (val);
+      free (val);
+    }
+}
+
+static mu_config_value_t *
+parse_label (const char *str)
+{
+  mu_config_value_t *val = NULL;
+  int count, i;
+  char **vect;
+  size_t len = strlen (str);
+  
+  if (len > 1 && str[0] == '(' && str[len-1] == ')')
+    {
+      mu_list_t lst;
+      mu_argcv_get_np (str + 1, len - 2,
+		       ", \t", NULL,
+		       0,
+		       &count, &vect, NULL);
+      mu_list_create (&lst);
+      mu_list_set_destroy_item (lst, destroy_value);
+      for (i = 0; i < count; i++)
+	{
+	  mu_config_value_t *p = mu_alloc (sizeof (*p));
+	  p->type = MU_CFG_STRING;
+	  p->v.string = vect[i];
+	  mu_list_append (lst, p);
+	}
+      free (vect);
+      val = mu_alloc (sizeof (*val));
+      val->type = MU_CFG_LIST;
+      val->v.list = lst;
+    }
+  else
+    {      
+      mu_argcv_get (str, NULL, NULL, &count, &vect);
+      val = mu_alloc (sizeof (*val));
+      if (count == 1)
+	{
+	  val->type = MU_CFG_STRING;
+	  val->v.string = vect[0];
+	  free (vect);
+	}
+      else
+	{
+	  val->type = MU_CFG_ARRAY;
+	  val->v.arg.c = count;
+	  val->v.arg.v = mu_alloc (count * sizeof (val->v.arg.v[0]));
+	  for (i = 0; i < count; i++)
+	    {
+	      val->v.arg.v[i].type = MU_CFG_STRING;
+	      val->v.arg.v[i].v.string = vect[i];
+	    }
+	  free (vect);
+	}
+    }
+  return val;
+}
+
+static void
+parse_tag (struct find_data *fptr)
+{
+  char *p = strchr (fptr->argv[fptr->tag], '=');
+  if (p)
+    {
+      *p++ = 0;
+      fptr->label = parse_label (p);
+    }
+  else
+    fptr->label = NULL;
+}
+
+static int
+node_finder (const mu_cfg_node_t *node, void *data)
+{
+  struct find_data *fdptr = data;
+  if (strcmp (fdptr->argv[fdptr->tag], node->tag) == 0
+      && (!fdptr->label || mu_cfg_value_eq (fdptr->label, node->label)))
+    {
+      fdptr->tag++;
+      if (fdptr->tag == fdptr->argc)
+	{
+	  fdptr->node = node;
+	  return MU_CFG_ITER_STOP;
+	}
+      parse_tag (fdptr);
+      return MU_CFG_ITER_OK;
+    }
+  
+  return node->type == mu_cfg_node_statement ?
+               MU_CFG_ITER_SKIP : MU_CFG_ITER_OK;
+}
+
+int	    
+mu_cfg_find_node (mu_cfg_tree_t *tree, const char *path, mu_cfg_node_t **pval)
+{
+  int rc;
+  struct find_data data;
+  struct mu_cfg_iter_closure clos;
+
+  rc = split_cfg_path (path, &data.argc, &data.argv);
+  if (rc)
+    return rc;
+  data.tag = 0;
+  parse_tag (&data);
+
+  clos.beg = node_finder;
+  clos.end = NULL;
+  clos.data = &data;
+  rc = mu_cfg_preorder (tree->nodes, &clos);
+  destroy_value (data.label);
+  if (rc)
+    {
+      *pval = (mu_cfg_node_t *) data.node;
+      return 0;
+    }
+  return MU_ERR_NOENT;
+}
+
+
+
+int
+mu_cfg_create_subtree (const char *path, mu_cfg_node_t **pnode)
+{
+  int rc;
+  int argc, i;
+  char **argv;
+  enum mu_cfg_node_type type;
+  mu_cfg_node_t *node = NULL;
+  mu_cfg_locus_t locus;
+
+  locus.file = "<int>";
+  locus.line = 0;
+
+  rc = split_cfg_path (path, &argc, &argv);
+  if (rc)
+    return rc;
+
+  for (i = argc - 1; i >= 0; i--)
+    {
+      mu_list_t nodelist = NULL;
+      mu_config_value_t *label = NULL;
+      char *q = argv[i], *p;
+      
+      type = mu_cfg_node_statement;
+      do
+	{
+	  p = strchr (q, '=');
+	  if (p && p > argv[i] && p[-1] != '\\')
+	    {
+	      *p++ = 0;
+	      label = parse_label (p);
+	      if (i == argc - 1)
+		type = mu_cfg_node_param;
+	      break;
+	    }
+	  else if (p)
+	    q = p + 1;
+	  else
+	    break;
+	}
+      while (*q);
+      
+      if (node)
+	{
+	  mu_cfg_create_node_list (&nodelist);
+	  mu_list_append (nodelist, node);
+	}
+      node = mu_cfg_alloc_node (type, &locus, argv[i], label, nodelist);
+    }
+
+  mu_argcv_free (argc, argv);
+  *pnode = node;
+  return 0;
+}
+
+  
+	  
+        
+  
diff --git a/libmailutils/cfg_driver.c b/libmailutils/cfg_driver.c
deleted file mode 100644
index f07a22f..0000000
--- a/libmailutils/cfg_driver.c
+++ /dev/null
@@ -1,754 +0,0 @@
-/* cfg_driver.c -- Main driver for Mailutils configuration files
-   Copyright (C) 2007, 2008, 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, see <http://www.gnu.org/licenses/>.
-*/
-#ifdef HAVE_CONFIG_H
-# include <config.h>
-#endif
-#include <stdio.h>
-#include <stdlib.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <errno.h>
-#include <unistd.h>
-#include <ctype.h>
-#include <mailutils/argcv.h>
-#include <mailutils/nls.h>
-#define MU_CFG_COMPATIBILITY /* This source uses deprecated cfg interfaces */
-#include <mailutils/cfg.h>
-#include <mailutils/errno.h>
-#include <mailutils/error.h>
-#include <mailutils/mutil.h>
-#include <mailutils/monitor.h>
-#include <mailutils/refcount.h>
-#include <mailutils/list.h>
-#include <mailutils/iterator.h>
-#include <mailutils/stream.h>
-#include <mailutils/assoc.h>
-#include <mailutils/alloc.h>
-
-
-static mu_assoc_t section_tab;
-
-static void
-alloc_section_tab ()
-{
-  if (!section_tab)
-    mu_assoc_create (&section_tab, sizeof (struct mu_cfg_cont **),
-		     MU_ASSOC_COPY_KEY);
-}
-
-int
-mu_create_canned_section (char *name, struct mu_cfg_section **psection)
-{
-  int rc;
-  struct mu_cfg_cont **pcont;
-  alloc_section_tab ();
-  rc = mu_assoc_ref_install (section_tab, name, (void **)&pcont);
-  if (rc == 0)
-    {
-      mu_config_create_container (pcont, mu_cfg_cont_section);
-      *psection = &(*pcont)->v.section;
-      (*psection)->ident = name;
-    }
-  else if (rc == MU_ERR_EXISTS)
-    *psection = &(*pcont)->v.section;
-  return rc;
-}
-
-int
-mu_create_canned_param (char *name, struct mu_cfg_param **pparam)
-{
-  int rc;
-  struct mu_cfg_cont **pcont;
-  alloc_section_tab ();
-  rc = mu_assoc_ref_install (section_tab, name, (void **)&pcont);
-  if (rc == 0)
-    {
-      mu_config_create_container (pcont, mu_cfg_cont_param);
-      *pparam = &(*pcont)->v.param;
-      (*pparam)->ident = name;
-    }
-  else if (rc == MU_ERR_EXISTS)
-    *pparam = &(*pcont)->v.param;
-  return rc;
-}
-
-struct mu_cfg_cont *
-mu_get_canned_container (const char *name)
-{
-  struct mu_cfg_cont **pcont = mu_assoc_ref (section_tab, name);
-  return pcont ? *pcont : NULL;
-}
-
-
-static struct mu_cfg_cont *root_container;
-
-int
-mu_config_create_container (struct mu_cfg_cont **pcont,
-			    enum mu_cfg_cont_type type)
-{
-  struct mu_cfg_cont *cont;
-  int rc;
-  
-  cont = calloc (1, sizeof (*cont));
-  if (!cont)
-    return ENOMEM;
-  rc = mu_refcount_create (&cont->refcount);
-  if (rc)
-    free (cont);
-  else
-    {
-      cont->type = type;
-      *pcont = cont;
-    }
-  return rc; 
-}  
-
-
-struct dup_data
-{
-  struct mu_cfg_cont *cont;
-};
-
-static int dup_container (struct mu_cfg_cont **pcont);
-
-static int
-_dup_cont_action (void *item, void *cbdata)
-{
-  int rc;
-  struct mu_cfg_cont *cont = item;
-  struct dup_data *pdd = cbdata;
-
-  rc = dup_container (&cont);
-  if (rc)
-    return rc;
-
-  if (!pdd->cont->v.section.children)
-    {
-      int rc = mu_list_create (&pdd->cont->v.section.children);
-      if (rc)
-	return rc;
-    }
-  return mu_list_append (pdd->cont->v.section.children, cont);
-}
-
-static int
-dup_container (struct mu_cfg_cont **pcont)
-{
-  int rc;
-  struct mu_cfg_cont *newcont, *oldcont = *pcont;
-  struct dup_data dd;
-
-  rc = mu_config_create_container (&newcont, oldcont->type);
-  if (rc)
-    return rc;
-
-  dd.cont = newcont;
-  switch (oldcont->type)
-    {
-    case mu_cfg_cont_section:
-      newcont->v.section.ident = oldcont->v.section.ident;
-      newcont->v.section.label = oldcont->v.section.label;
-      newcont->v.section.parser = oldcont->v.section.parser;
-      newcont->v.section.target = oldcont->v.section.target;
-      newcont->v.section.offset = oldcont->v.section.offset;
-      newcont->v.section.docstring = oldcont->v.section.docstring;
-      newcont->v.section.children = NULL;
-      mu_list_do (oldcont->v.section.children, _dup_cont_action, &dd);
-      break;
-
-    case mu_cfg_cont_param:
-      newcont->v.param = oldcont->v.param;
-      break;
-    }
-  *pcont = newcont;
-  return 0;
-}
-
-
-static void
-destroy_list (mu_list_t *plist)
-{
-  mu_list_t list = *plist;
-  mu_iterator_t itr = NULL;
-  
-  if (!list)
-    return;
-
-  mu_list_get_iterator (list, &itr);
-  for (mu_iterator_first (itr); !mu_iterator_is_done (itr);
-       mu_iterator_next (itr))
-    {
-      struct mu_cfg_cont *cont, *p;
-      mu_iterator_current (itr, (void**)&cont);
-      p = cont;
-      mu_config_destroy_container (&p);
-      if (!p)
-	mu_list_remove (list, cont);
-    }
-  mu_iterator_destroy (&itr);
-  if (mu_list_is_empty (list))
-    mu_list_destroy (plist);
-}
-
-void
-mu_config_destroy_container (struct mu_cfg_cont **pcont)
-{
-  struct mu_cfg_cont *cont = *pcont;
-  unsigned refcount = mu_refcount_dec (cont->refcount);
-  /* printf ("destr %p-%s: %d\n", cont, cont->v.section.ident, refcount); */
-  switch (cont->type)
-    {
-    case mu_cfg_cont_section:
-      destroy_list (&cont->v.section.children);
-      break;
-
-    case mu_cfg_cont_param:
-      break;
-    }
-
-  if (refcount == 0)
-    {
-      free (cont);
-      *pcont = 0;
-    }
-}
-     
-
-int
-mu_cfg_section_add_container (struct mu_cfg_section *sect,
-			      struct mu_cfg_cont *cont)
-{
-  if (!cont)
-    return 0;
-  if (!sect->children)
-    mu_list_create (&sect->children);
-  return mu_list_append (sect->children, cont);
-}
-
-int
-mu_cfg_section_add_params (struct mu_cfg_section *sect,
-			   struct mu_cfg_param *param)
-{
-  if (!param)
-    return 0;
-
-  for (; param->ident; param++)
-    {
-      int rc;
-      struct mu_cfg_cont *container;
-
-      if (param->type == mu_cfg_section)
-	{
-	  container = mu_get_canned_container (param->ident);
-	  if (!container)
-	    {
-	      mu_error (_("INTERNAL ERROR: Requested unknown canned "
-			  "section %s"),
-			param->ident);
-	      abort ();
-	    }
-	  if (param->ident[0] == '.')
-	    {
-	      mu_iterator_t itr;
-	      mu_list_get_iterator (container->v.section.children, &itr);
-	      for (mu_iterator_first (itr);
-		   !mu_iterator_is_done (itr);
-		   mu_iterator_next (itr))
-		{
-		  struct mu_cfg_cont *c;
-		  mu_iterator_current (itr, (void**)&c);
-		  mu_config_clone_container (c);
-		  if (mu_refcount_value (c->refcount) > 1)
-		    dup_container (&c);
-		  switch (c->type)
-		    {
-		    case mu_cfg_cont_section:
-		      c->v.section.offset += param->offset;
-		      break;
-
-		    case mu_cfg_cont_param:
-		      container->v.param.offset += param->offset;
-		      break;
-		    }
-		  mu_cfg_section_add_container (sect, c);
-		}
-	      mu_iterator_destroy (&itr);
-	      continue;
-	    }
-	  else
-	    {
-	      mu_config_clone_container (container);
-	      if (mu_refcount_value (container->refcount) > 1)
-		dup_container (&container);
-	      container->v.section.target = param->data;
-	      container->v.section.offset = param->offset;
-	    }
-	}
-      else
-	{
-	  rc = mu_config_create_container (&container, mu_cfg_cont_param);
-	  if (rc)
-	    return rc;
-	  container->v.param = *param;
-	}
-      mu_cfg_section_add_container (sect, container);
-    }
-  return 0;
-}
-
-static int
-_clone_action (void *item, void *cbdata)
-{
-  struct mu_cfg_cont *cont = item;
-  return mu_config_clone_container (cont);
-}
-
-int
-mu_config_clone_container (struct mu_cfg_cont *cont)
-{
-  if (!cont)
-    return 0;
-  mu_refcount_inc (cont->refcount);
-  /* printf("clone %p-%s: %d\n", cont, cont->v.section.ident, n); */
-  switch (cont->type)
-    {
-    case mu_cfg_cont_section:
-      mu_list_do (cont->v.section.children, _clone_action, NULL);
-      break;
-
-    case mu_cfg_cont_param:
-      break;
-    }
-  return 0;
-}  
-
-
-int
-_mu_config_register_section (struct mu_cfg_cont **proot,
-			     const char *parent_path,
-			     const char *ident,
-			     const char *label,
-			     mu_cfg_section_fp parser,
-			     struct mu_cfg_param *param,
-			     struct mu_cfg_section **psection)
-{
-  int rc;
-  struct mu_cfg_section *root_section;
-  struct mu_cfg_section *parent;
-  
-  if (!*proot)
-    {
-      rc = mu_config_create_container (proot, mu_cfg_cont_section);
-      if (rc)
-	return rc;
-      memset (&(*proot)->v.section, 0, sizeof (*proot)->v.section);
-    }
-  
-  root_section = &(*proot)->v.section;
-  
-  if (parent_path)
-    {
-      if (mu_cfg_find_section (root_section, parent_path, &parent))
-	return MU_ERR_NOENT;
-    }
-  else  
-    parent = root_section;
-
-  if (mu_refcount_value ((*proot)->refcount) > 1)
-    {
-      /* It is a clone, do copy-on-write */
-      rc = dup_container (proot);
-      if (rc)
-	return rc;
-
-      root_section = &(*proot)->v.section;
-      
-      if (parent_path)
-	{
-	  if (mu_cfg_find_section (root_section, parent_path, &parent))
-	    return MU_ERR_NOENT;
-	}
-      else  
-	parent = root_section;
-    }
-
-  if (ident)
-    {
-      struct mu_cfg_cont *container;
-      struct mu_cfg_section *s;
-      
-      if (!parent->children)
-	mu_list_create (&parent->children);
-      mu_config_create_container (&container, mu_cfg_cont_section);
-      mu_list_append (parent->children, container); 
-      s = &container->v.section;
-
-      s->ident = strdup (ident);
-      s->label = label ? strdup (label) : NULL;
-      s->parser = parser;
-      s->children = NULL;
-      mu_cfg_section_add_params (s, param);
-      if (psection)
-	*psection = s;
-    }
-  else
-    {
-      mu_cfg_section_add_params (parent, param);
-      /* FIXME: */
-      if (!parent->parser)
-	parent->parser = parser;
-      if (psection)
-	*psection = parent;
-    }
-  return 0;
-}
-  
-int
-mu_config_register_section (const char *parent_path,
-			    const char *ident,
-			    const char *label,
-			    mu_cfg_section_fp parser,
-			    struct mu_cfg_param *param)
-{
-  return _mu_config_register_section (&root_container,
-				      parent_path,
-				      ident, label,
-				      parser, param, NULL);
-}
-
-int
-mu_config_register_plain_section (const char *parent_path, const char *ident,
-				  struct mu_cfg_param *params)
-{
-  return mu_config_register_section (parent_path, ident, NULL, NULL, params);
-}
-
-static int
-prog_parser (enum mu_cfg_section_stage stage,
-	     const mu_cfg_node_t *node,
-	     const char *label, void **section_data,
-	     void *call_data,
-	     mu_cfg_tree_t *tree)
-{
-  if (stage == mu_cfg_section_start)
-    {
-      return node->label->type == MU_CFG_STRING
-	     && strcmp (node->label->v.string, label);
-    }
-  
-  return 0;
-}
-
-struct include_data
-{
-  const char *progname;
-  struct mu_cfg_param *progparam;
-  int flags;
-  void *target;
-};
-
-static int
-_cb_include (mu_debug_t debug, void *data, mu_config_value_t *val)
-{
-  int ret = 0;
-  struct stat sb;
-  const char *dirname;
-  struct include_data *idp = data;
-  char *tmp = NULL;
-
-  if (mu_cfg_assert_value_type (val, MU_CFG_STRING, debug))
-    return 1;
-
-  dirname = val->v.string;
-  if (dirname[0] != '/')
-    {
-      dirname = tmp = mu_make_file_name (SYSCONFDIR, dirname);
-      if (!dirname)
-        {
-          mu_error ("%s", mu_strerror (errno));
-          return 1;
-        }
-    }
-    
-  if (stat (dirname, &sb) == 0)
-    {
-      if (S_ISDIR (sb.st_mode))
-	{
-	  char *file = mu_make_file_name (dirname, idp->progname);
-	  ret = mu_get_config (file, idp->progname, idp->progparam,
-			       idp->flags & ~MU_PARSE_CONFIG_GLOBAL,
-			       idp->target);
-	}
-      else
-	ret = mu_get_config (dirname, idp->progname, idp->progparam,
-			     idp->flags, idp->target);
-    }
-  else if (errno == ENOENT)
-    {
-      mu_cfg_format_error (debug, MU_DEBUG_ERROR,
-			   _("include file or directory does not exist"));
-      ret = 1;
-    }
-  else
-    {
-      mu_cfg_format_error (debug, MU_DEBUG_ERROR,
-			   _("cannot stat include file or directory: %s"),
-			   mu_strerror (errno));
-      ret = 1;
-    }
-  free (tmp);
-  return ret;
-}
-
-struct mu_cfg_cont *
-mu_build_container (const char *progname, struct include_data *idp)
-{
-  struct mu_cfg_cont *cont = root_container;
-
-  mu_config_clone_container (cont);
-  
-  if (idp->flags & MU_PARSE_CONFIG_PLAIN)
-    {
-      struct mu_cfg_param mu_include_param[] = {
-	{ "include", mu_cfg_callback, NULL, 0, _cb_include,
-	  N_("Include contents of the given file.  If a directory is given, "
-	     "include contents of the file <file>/<program>, where "
-	     "<program> is the name of the program.  This latter form is "
-	     "allowed only in the site-wide configuration file."),
-	  N_("file-or-directory") },
-	{ NULL }
-      };
-
-      mu_include_param[0].data = idp;
-      _mu_config_register_section (&cont, NULL, NULL, NULL,
-				   (void*) progname, mu_include_param, NULL);
-      
-      if (idp->flags & MU_PARSE_CONFIG_GLOBAL)
-	{
-	  mu_iterator_t iter;
-	  struct mu_cfg_section *prog_sect;
-	  struct mu_cfg_cont *old_root = root_container;
-	  static struct mu_cfg_param empty_param = { NULL };
-	  
-	  _mu_config_register_section (&cont, NULL, "program", progname,
-				       prog_parser,
-				       idp->progparam ?
-				       idp->progparam : &empty_param,
-				       &prog_sect);
-      
-	  if (old_root->v.section.children)
-	    {
-	      if (!prog_sect->children)
-		mu_list_create (&prog_sect->children);
-	      mu_list_get_iterator (old_root->v.section.children, &iter);
-	      for (mu_iterator_first (iter); !mu_iterator_is_done (iter);
-		   mu_iterator_next (iter))
-		{
-		  struct mu_cfg_cont *c;
-		  mu_iterator_current (iter, (void**)&c);
-		  mu_list_append (prog_sect->children, c);
-		}
-	      mu_iterator_destroy (&iter);
-	    }
-	}
-      else if (idp->progparam)
-	_mu_config_register_section (&cont, NULL, NULL, NULL, NULL,
-				     idp->progparam, NULL);
-    }
-  else if (idp->progparam)
-    _mu_config_register_section (&cont, NULL, NULL, NULL, NULL,
-				 idp->progparam, NULL);
-  
-  return cont;
-}
-
-int
-mu_cfg_tree_reduce (mu_cfg_tree_t *parse_tree, const char *progname,
-		    struct mu_cfg_param *progparam, int flags,
-		    void *target_ptr)
-{
-  int rc = 0;
-
-  if (!parse_tree)
-    return 0;
-  if (flags & MU_PARSE_CONFIG_DUMP)
-    {
-      mu_stream_t stream;
-      mu_stdio_stream_create (&stream, MU_STDERR_FD, 0);
-      mu_cfg_format_parse_tree (stream, parse_tree, MU_CFG_FMT_LOCUS);
-      mu_stream_destroy (&stream);
-    }
-
-  if (root_container)
-    {
-      struct include_data idata;
-      struct mu_cfg_cont *cont;
-      
-      idata.progname = progname;
-      idata.progparam = progparam;
-      idata.flags = flags;
-      idata.target = target_ptr;
-      
-      cont = mu_build_container (progname, &idata);
-      
-      rc = mu_cfg_scan_tree (parse_tree, &cont->v.section, target_ptr,
-			     (void*) progname);
-      mu_config_destroy_container (&cont);
-    }
-
-  return rc;
-}
-
-void
-mu_format_config_tree (mu_stream_t stream, const char *progname,
-		       struct mu_cfg_param *progparam, int flags)
-{
-  struct include_data idata;
-  struct mu_cfg_cont *cont;
-  
-  idata.progname = progname;
-  idata.progparam = progparam;
-  idata.flags = flags;
-  idata.target = NULL;
-  cont = mu_build_container (progname, &idata);
-  mu_cfg_format_container (stream, cont);
-  mu_config_destroy_container (&cont);
-}
-
-int
-mu_parse_config (const char *file, const char *progname,
-		 struct mu_cfg_param *progparam, int flags,
-		 void *target_ptr)
-{
-  int rc;
-  char *full_name = mu_tilde_expansion (file, "/", NULL);
-  if (full_name)
-    {
-      if (access (full_name, R_OK) == 0)
-	{
-	  rc = mu_get_config (full_name, progname, progparam, flags,
-			      target_ptr);
-	}
-      else
-	rc = ENOENT;
-      free (full_name);
-    }
-  else
-    rc = ENOMEM;
-  return rc;
-}
-
-static const char *
-_first_value_ptr (mu_config_value_t *val)
-{
-  switch (val->type)
-    {
-    case MU_CFG_STRING:
-      return val->v.string;
-      
-    case MU_CFG_ARRAY:
-      return _first_value_ptr (val->v.arg.v);
-      
-    case MU_CFG_LIST:
-      mu_list_get (val->v.list, 0, (void**) &val);
-      return _first_value_ptr (val);
-    }
-  return "";  
-}
-
-int
-mu_cfg_assert_value_type (mu_config_value_t *val, int type, mu_debug_t debug)
-{
-  if (!val)
-    { 
-      mu_cfg_format_error (debug, MU_DEBUG_ERROR,
-                           _("required argument missing"));
-      return 1;
-    }
-
-  if (type == MU_CFG_ARRAY)
-    {
-      if (val->type == MU_CFG_STRING)
-	{
-	  mu_config_value_t *arr = mu_calloc (1, sizeof arr[0]);
-	  arr[0] = *val;
-	  val->v.arg.c = 1;
-	  val->v.arg.v = arr;
-	  val->type = MU_CFG_ARRAY;
-	}
-    }
-  
-  if (val->type != type)
-    {
-      /* FIXME */
-      mu_cfg_format_error (debug, MU_DEBUG_ERROR,
-			   _("unexpected value: %s"), 
-			   _first_value_ptr (val));
-      return 1;
-    }
-  return 0;
-}
-
-int
-mu_cfg_string_value_cb (mu_debug_t debug, mu_config_value_t *val,
-			int (*fun) (mu_debug_t, const char *, void *),
-			void *data)
-{
-  int rc = 0;
-  
-  switch (val->type)
-    {
-    case MU_CFG_STRING:
-      return fun (debug, val->v.string, data);
-      break;
-
-    case MU_CFG_ARRAY:
-      {
-	int i;
-
-	for (i = 0; i < val->v.arg.c; i++)
-	  {
-	    if (mu_cfg_assert_value_type (&val->v.arg.v[i],
-					  MU_CFG_STRING, debug))
-	      return 1;
-	    fun (debug, val->v.arg.v[i].v.string, data);
-	  }
-      }
-      break;
-
-    case MU_CFG_LIST:
-      {
-	mu_iterator_t itr;
-	mu_list_get_iterator (val->v.list, &itr);
-	for (mu_iterator_first (itr);
-	     !mu_iterator_is_done (itr); mu_iterator_next (itr))
-	  {
-	    mu_config_value_t *pval;
-	    mu_iterator_current (itr, (void*) &pval);
-	    if (mu_cfg_assert_value_type (pval, MU_CFG_STRING, debug))
-	      {
-		rc = 1;
-		break;
-	      }
-	    fun (debug, pval->v.string, data);
-	  }
-	mu_iterator_destroy (&itr);
-      }
-    }
-  return rc;
-}
diff --git a/libmailutils/cfg_format.c b/libmailutils/cfg_format.c
deleted file mode 100644
index be37bae..0000000
--- a/libmailutils/cfg_format.c
+++ /dev/null
@@ -1,395 +0,0 @@
-/* cfg_print.c -- convert configuration parse tree to human-readable format.
-   Copyright (C) 2007, 2008, 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, see <http://www.gnu.org/licenses/>.
-*/
-
-#ifdef HAVE_CONFIG_H
-# include <config.h>
-#endif
-#include <mailutils/alloc.h>
-#include <mailutils/stream.h>
-#include <mailutils/error.h>
-#include <mailutils/cfg.h>
-#include <mailutils/argcv.h>
-#include <mailutils/nls.h>
-#include <mailutils/iterator.h>
-#include <ctype.h>
-
-struct tree_print
-{
-  int flags;
-  unsigned level;
-  mu_stream_t stream;
-  char *buf;
-  size_t bufsize;
-};
-
-static void
-format_level (mu_stream_t stream, int level)
-{
-  while (level--)
-    mu_stream_write (stream, "  ", 2, NULL);
-}
-
-static void
-format_string_value (struct tree_print *tp, const char *str)
-{
-  size_t size;
-  int quote;
-  char *p;
-
-  size = mu_argcv_quoted_length (str, &quote);
-  if (quote)
-    size += 2;
-  size++;
-  if (size > tp->bufsize)
-    {
-      p = mu_realloc (tp->buf, size);
-      tp->bufsize = size;
-      tp->buf = p;
-    }
-  
-  p = tp->buf;
-  if (quote)
-    {
-      tp->buf[0] = '"';
-      tp->buf[size-2] = '"';
-      p++;
-    }
-  tp->buf[size-1] = 0;
-  mu_argcv_quote_copy (p, str);
-  mu_stream_write (tp->stream, tp->buf, size - 1, NULL);
-}
-
-static void format_value (struct tree_print *tp, mu_config_value_t *val);
-
-static void
-format_list_value (struct tree_print *tp, mu_config_value_t *val)
-{
-  int i;
-  mu_iterator_t itr;
-  mu_stream_write (tp->stream, "(", 1, NULL);
-  mu_list_get_iterator (val->v.list, &itr);
-  
-  for (mu_iterator_first (itr), i = 0;
-       !mu_iterator_is_done (itr); mu_iterator_next (itr), i++)
-    {
-      mu_config_value_t *p;
-      mu_iterator_current (itr, (void**)&p);
-      if (i)
-	mu_stream_write (tp->stream, ", ", 2, NULL);
-      format_value (tp, p);
-    }
-  mu_iterator_destroy (&itr);
-  mu_stream_write (tp->stream, ")", 1, NULL);
-}
-
-static void
-format_array_value (struct tree_print *tp, mu_config_value_t *val)
-{
-  int i;
-
-  for (i = 0; i < val->v.arg.c; i++)
-    {
-      if (i)
-	mu_stream_write (tp->stream, " ", 1, NULL);
-      format_value (tp, &val->v.arg.v[i]);
-    }
-}
-    
-static void
-format_value (struct tree_print *tp, mu_config_value_t *val)
-{
-  switch (val->type)
-    {
-    case MU_CFG_STRING:
-      format_string_value (tp, val->v.string);
-      break;
-
-    case MU_CFG_LIST:
-      format_list_value (tp, val);
-      break;
-
-    case MU_CFG_ARRAY:
-      format_array_value (tp, val);
-    }
-}
-
-static int
-format_node (const mu_cfg_node_t *node, void *data)
-{
-  struct tree_print *tp = data;
-
-  if ((tp->flags & MU_CFG_FMT_LOCUS) && node->locus.file)
-    mu_stream_printf (tp->stream, "# %lu \"%s\"\n",
-		      (unsigned long) node->locus.line, 
-		      node->locus.file);
-  format_level (tp->stream, tp->level);
-  switch (node->type)
-    {
-    case mu_cfg_node_undefined:
-      mu_stream_printf (tp->stream, "%s",
-			_("ERROR: undefined statement"));
-      break;
-
-    case mu_cfg_node_statement:
-      {
-	mu_stream_write (tp->stream, node->tag, strlen (node->tag), NULL);
-	if (node->label)
-	  {
-	    mu_stream_write (tp->stream, " ", 1, NULL);
-	    format_value (tp, node->label);
-	  }
-	mu_stream_write (tp->stream, " {", 2, NULL);
-	tp->level++;
-      }
-      break;
-
-    case mu_cfg_node_param:
-      mu_stream_write (tp->stream, node->tag, strlen (node->tag), NULL);
-      if (node->label)
-	{
-	  mu_stream_write (tp->stream, " ", 1, NULL);
-	  format_value (tp, node->label);
-	  mu_stream_write (tp->stream, ";", 1, NULL);
-	}
-      break;
-    }
-  mu_stream_write (tp->stream, "\n", 1, NULL);
-  return MU_CFG_ITER_OK;
-}
-
-static int
-format_node_end (const mu_cfg_node_t *node, void *data)
-{
-  struct tree_print *tp = data;
-  tp->level--;
-  format_level (tp->stream, tp->level);
-  mu_stream_write (tp->stream, "};\n", 3, NULL);
-  return MU_CFG_ITER_OK;
-}
-
-void
-mu_cfg_format_parse_tree (mu_stream_t stream, mu_cfg_tree_t *tree, int flags)
-{
-  struct mu_cfg_iter_closure clos;
-  struct tree_print t;
-
-  t.flags = flags;
-  t.level = 0;
-  t.stream = stream;
-  t.buf = NULL;
-  t.bufsize = 0;
-  clos.beg = format_node;
-  clos.end = format_node_end;
-  clos.data = &t;
-  mu_cfg_preorder (tree->nodes, &clos);
-  free (t.buf);
-}
-
-void
-mu_cfg_format_node (mu_stream_t stream, const mu_cfg_node_t *node, int flags)
-{
-  struct tree_print t;
-  
-  t.flags = flags;
-  t.level = 0;
-  t.stream = stream;
-  t.buf = NULL;
-  t.bufsize = 0;
-  format_node (node, &t);
-  if (node->type == mu_cfg_node_statement)
-    {
-      struct mu_cfg_iter_closure clos;
-      clos.beg = format_node;
-      clos.end = format_node_end;
-      clos.data = &t;
-      mu_cfg_preorder (node->nodes, &clos);
-      format_node_end (node, &t);
-    }
-}
-
-
-
-const char *
-mu_cfg_data_type_string (enum mu_cfg_param_data_type type)
-{
-  switch (type)
-    {
-    case mu_cfg_string:
-      return N_("string");
-    case mu_cfg_short:
-    case mu_cfg_ushort:
-    case mu_cfg_int:
-    case mu_cfg_uint:
-    case mu_cfg_long:
-    case mu_cfg_ulong:
-    case mu_cfg_size:
-    case mu_cfg_off:
-      return N_("number");
-    case mu_cfg_time:
-      return N_("time");
-    case mu_cfg_bool:
-      return N_("boolean");
-    case mu_cfg_ipv4:
-      return N_("ipv4");
-    case mu_cfg_cidr:
-      return N_("cidr");
-    case mu_cfg_host:
-      return N_("host");
-    case mu_cfg_callback:
-      return N_("string");
-    case mu_cfg_section:
-      return N_("section");
-    }
-  return N_("unknown");
-}
-
-void
-mu_cfg_format_docstring (mu_stream_t stream, const char *docstring, int level)
-{
-  size_t len = strlen (docstring);
-  int width = 78 - level * 2;
-
-  if (width < 0)
-    {
-      width = 78;
-      level = 0;
-    }
-  
-  while (len)
-    {
-      size_t seglen;
-      const char *p;
-      
-      for (seglen = 0, p = docstring; p < docstring + width && *p; p++)
-	{
-	  if (*p == '\n')
-	    {
-	      seglen = p - docstring;
-	      break;
-	    }
-	  if (isspace (*p))
-	    seglen = p - docstring;
-	}
-      if (seglen == 0 || *p == 0)
-	seglen = p - docstring;
-
-      format_level (stream, level);
-      mu_stream_write (stream, "# ", 2, NULL);
-      mu_stream_write (stream, docstring, seglen, NULL);
-      mu_stream_write (stream, "\n", 1, NULL);
-      len -= seglen;
-      docstring += seglen;
-      if (*docstring == '\n')
-	{
-	  docstring++;
-	  len--;
-	}
-      else
-	while (*docstring && isspace (*docstring))
-	  {
-	    docstring++;
-	    len--;
-	  }
-    }
-}
-
-static void
-format_param (mu_stream_t stream, struct mu_cfg_param *param, int level)
-{
-  if (param->docstring)
-    mu_cfg_format_docstring (stream, gettext (param->docstring), level);
-  format_level (stream, level);
-  if (param->argname && strchr (param->argname, ':'))
-    mu_stream_printf (stream, "%s <%s>;\n",
-		      param->ident,
-		      gettext (param->argname));
-  else if (MU_CFG_IS_LIST (param->type))
-    mu_stream_printf (stream, "%s <%s: list of %s>;\n",
-		      param->ident,
-		      gettext (param->argname ?
-			       param->argname : N_("arg")),
-	gettext (mu_cfg_data_type_string (MU_CFG_TYPE (param->type))));
-  else
-    mu_stream_printf (stream, "%s <%s: %s>;\n",
-		      param->ident,
-		      gettext (param->argname ?
-			       param->argname : N_("arg")),
-		      gettext (mu_cfg_data_type_string (param->type)));
-}
-
-static void format_container (mu_stream_t stream, struct mu_cfg_cont *cont,
-			      int level);
-
-static int
-_f_helper (void *item, void *data)
-{
-  struct tree_print *tp = data;
-  struct mu_cfg_cont *cont = item;
-  format_container (tp->stream, cont, tp->level);
-  return 0;
-}
-  
-static void
-format_section (mu_stream_t stream, struct mu_cfg_section *sect, int level)
-{
-  struct tree_print c;
-  if (sect->docstring)
-    mu_cfg_format_docstring (stream, gettext (sect->docstring), level);
-  format_level (stream, level);
-  if (sect->ident)
-    {
-      mu_stream_write (stream, sect->ident, strlen (sect->ident), NULL);
-      if (sect->label)
-	{
-	  mu_stream_write (stream, " ", 1, NULL);
-	  mu_stream_write (stream, sect->label, strlen (sect->label), NULL);
-	}
-      mu_stream_write (stream, " {\n", 3, NULL);
-      c.stream = stream;
-      c.level = level + 1; 
-      mu_list_do (sect->children, _f_helper, &c);
-      format_level (stream, level);
-      mu_stream_write (stream, "};\n\n", 4, NULL);
-    }
-  else
-    {
-      c.stream = stream;
-      c.level = level; 
-      mu_list_do (sect->children, _f_helper, &c);
-    }
-}
-
-static void
-format_container (mu_stream_t stream, struct mu_cfg_cont *cont, int level)
-{
-  switch (cont->type)
-    {
-    case mu_cfg_cont_section:
-      format_section (stream, &cont->v.section, level);
-      break;
-
-    case mu_cfg_cont_param:
-      format_param (stream, &cont->v.param, level);
-      break;
-    }
-}
-
-void
-mu_cfg_format_container (mu_stream_t stream, struct mu_cfg_cont *cont)
-{
-  format_container (stream, cont, 0);
-}
diff --git a/libmailutils/cfg_lexer.l b/libmailutils/cfg_lexer.l
deleted file mode 100644
index 998d84d..0000000
--- a/libmailutils/cfg_lexer.l
+++ /dev/null
@@ -1,405 +0,0 @@
-%top {
-/* cfg_lexer.l -- default lexer for Mailutils configuration files
-   Copyright (C) 2007, 2008, 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, see <http://www.gnu.org/licenses/>.
-*/
-#ifdef HAVE_CONFIG_H
-# include <config.h>
-#endif
-}
-
-%{
-#include <stdio.h>
-#include <stdlib.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <errno.h>
-#include <unistd.h>
-#include <mailutils/cctype.h>
-#include <mailutils/errno.h>
-#include <mailutils/error.h>
-#include <mailutils/debug.h>  
-#include <mailutils/argcv.h>
-#include <mailutils/alloc.h>  
-#include <mailutils/nls.h>
-#include <mailutils/cfg.h>
-#include <mailutils/list.h>
-#include <mailutils/mutil.h>
-  
-#include "cfg_parser.h"
-
-void _mu_line_begin (void);
-void _mu_line_add (char *text, size_t len);
-char *_mu_line_finish (void);
-
-extern void mu_cfg_set_debug (void);
-static void
-mu_cfg_set_lex_debug (void)
-{
-  yy_flex_debug = mu_debug_check_level (mu_cfg_get_debug (),
-					MU_DEBUG_TRACE2);
-}
- 
-static void _mu_line_add_unescape_last (char *text, size_t len);
-static void multiline_begin (char *p);
-static char *multiline_strip_tabs (char *text);
-static void multiline_add (char *s);
-static char *multiline_finish (void);
- 
-static char *multiline_delimiter;
-static size_t multiline_delimiter_len;
-static int multiline_unescape;         /* Unescape here-document contents */
-static int (*char_to_strip)(char);     /* Strip matching characters of each
-					  here-document line */
-static int isemptystr(int off);
-
-static mu_opool_t pool;
-
-%}
-
-%x COMMENT ML STR
-
-WS [ \t\f][ \t\f]*
-ID [a-zA-Z_][a-zA-Z_0-9-]+
-P [1-9][0-9]*
-
-%%
-         /* C-style comments */
-"/*"         BEGIN(COMMENT);
-<COMMENT>[^*\n]*        /* eat anything that's not a '*' */
-<COMMENT>"*"+[^*/\n]*   /* eat up '*'s not followed by '/'s */
-<COMMENT>\n             ++mu_cfg_locus.line;
-<COMMENT>"*"+"/"        BEGIN (INITIAL);
-         /* End-of-line comments */
-#debug=.*\n {
-          mu_log_level_t lev;
-          mu_debug_t dbg = mu_cfg_get_debug ();
-          if (mu_debug_level_from_string (yytext + 7, &lev, dbg) == 0)
-            {
-	      mu_debug_set_level (dbg, lev);
-	      mu_cfg_set_debug ();
-	      mu_cfg_set_lex_debug ();
-	    }
-          }
-#.*\n     { mu_cfg_locus.line++; }
-#.*     /* end-of-file comment */;
-"//".*\n  { mu_cfg_locus.line++; }
-"//".*    /* end-of-file comment */;
-        /* Identifiers */
-<INITIAL>{ID}         {
-                        _mu_line_begin ();
-			_mu_line_add (yytext, yyleng);
-			yylval.string = _mu_line_finish ();
-			return MU_TOK_IDENT; }
-         /* Strings */
-[a-zA-Z0-9_\./:\*=-]+ { _mu_line_begin ();
-                        _mu_line_add (yytext, yyleng);
-                        yylval.string = _mu_line_finish ();
-                        return MU_TOK_STRING; }
-         /* Quoted strings */
-\"[^\\"\n]*\"         { _mu_line_begin ();
-                        _mu_line_add (yytext + 1, yyleng - 2);
-                        yylval.string = _mu_line_finish ();
-                        return MU_TOK_QSTRING; }
-\"[^\\"\n]*\\. |
-\"[^\\"\n]*\\\n        { BEGIN (STR);
-                        _mu_line_begin ();
-		        _mu_line_add_unescape_last (yytext + 1, yyleng - 1); }
-<STR>[^\\"\n]*\\. |
-<STR>\"[^\\"\n]*\\\n  { _mu_line_add_unescape_last (yytext, yyleng); }
-<STR>[^\\"\n]*\"      { BEGIN (INITIAL);
-                        if (yyleng > 1) 
-                          _mu_line_add (yytext, yyleng - 1); 
-                        yylval.string = _mu_line_finish ();
-		        return MU_TOK_QSTRING; }
-         /* Multiline strings */
-"<<"(-" "?)?\\?{ID}[ \t]*#.*\n |
-"<<"(-" "?)?\\?{ID}[ \t]*"//".*\n |
-"<<"(-" "?)?\\?{ID}[ \t]*\n |
-"<<"(-" "?)?\"{ID}\"[ \t]*#.*\n |
-"<<"(-" "?)?\"{ID}\"[ \t]*"//".*\n |
-"<<"(-" "?)?\"{ID}\"[ \t]*\n {
-                        BEGIN (ML);
-			multiline_begin (yytext+2);
-			mu_cfg_locus.line++;
-		  }
-<ML>.*\n { char *p = multiline_strip_tabs (yytext);
-	   
-           if (!strncmp (p, multiline_delimiter, multiline_delimiter_len)
-	       && isemptystr (p + multiline_delimiter_len - yytext))
-	     {
-	       free (multiline_delimiter);
-	       multiline_delimiter = NULL;
-	       BEGIN (INITIAL);
-	       yylval.string = multiline_finish ();
-	       return MU_TOK_MSTRING;
-	     }
-           mu_cfg_locus.line++;
-	   multiline_add (p); } 
-{WS}     ;
-         /* Other tokens */
-\n       { mu_cfg_locus.line++; } 
-[,;{}()] return yytext[0];
-.        { if (mu_isprint (yytext[0]))
-              mu_cfg_parse_error (_("stray character %c"), yytext[0]);
-           else 
-              mu_cfg_parse_error (_("stray character \\%03o"),
-				  (unsigned char) yytext[0]);
-         }
-%%
-
-int
-yywrap ()
-{
-  return 1;
-}
-
-static void
-unescape_to_line (int c)
-{
-  if (c != '\n')
-    {
-      char t = mu_argcv_unquote_char (c);
-      if (t == c && t != '\\' && t != '\"')
-	mu_cfg_parse_error (_("unknown escape sequence '\\%c'"), c);
-      mu_opool_append_char (pool, t);
-    }
-}
-
-void
-_mu_line_add (char *text, size_t len)
-{
-  mu_opool_append (pool, text, len);
-}
-
-void
-_mu_line_add_unescape_last (char *text, size_t len)
-{
-  mu_opool_append (pool, text, len - 2);
-  unescape_to_line (text[len - 1]);
-}
-
-void
-_mu_line_begin ()
-{
-  if (!pool)
-    mu_opool_create (&pool, 1);
-  else
-    mu_opool_clear (pool);
-}
-
-char *
-_mu_line_finish ()
-{
-  mu_opool_append_char (pool, 0);
-  return mu_opool_finish (pool, NULL);
-}
-
-
-
-static int
-is_tab (char c)
-{
-    return c == '\t';
-}
- 
-static int
-is_ws (char c)
-{
-    return c == '\t' || c == ' ';
-}
-
-static int
-isemptystr (int off)
-{
-  for (; yytext[off] && mu_isspace (yytext[off]); off++)
-    ;
-  if (yytext[off] == ';')
-    {
-      int i;
-      for (i = off + 1; yytext[i]; i++) 
-	if (!mu_isspace (yytext[i]))
-	  return 0;
-      yyless (off);
-      return 1;
-    }
-  return yytext[off] == 0;
-}
-
-static void
-multiline_begin (char *p)
-{
-  if (*p == '-')
-    {
-      if (*++p == ' ')
-	{
-	  char_to_strip = is_ws;
-	  p++;
-	}
-      else
-	char_to_strip = is_tab;
-    }
-  else
-    char_to_strip = NULL;
-  if (*p == '\\')
-    {
-      p++;
-      multiline_unescape = 0;
-    }
-  else if (*p == '"')
-    {
-      char *q;
-      
-      p++;
-      multiline_unescape = 0;
-      q = strchr (p, '"');
-      multiline_delimiter_len = q - p;
-    }
-  else
-    {
-	multiline_delimiter_len = strcspn (p, " \t");
-	multiline_unescape = 1;
-    }
-
-  /* Remove trailing newline */
-  multiline_delimiter_len--;
-  multiline_delimiter = mu_alloc (multiline_delimiter_len + 1);
-  memcpy (multiline_delimiter, p, multiline_delimiter_len);
-  multiline_delimiter[multiline_delimiter_len] = 0;
-  _mu_line_begin ();
-}
-
-static char *
-multiline_strip_tabs (char *text)
-{
-  if (char_to_strip)
-    for (; *text && char_to_strip (*text); text++)
-      ;
-  return text;
-}
-
-static void
-multiline_add (char *s)
-{
-  if (multiline_unescape)
-    {
-      for (; *s; s++)
-	{
-	  if (*s == '\\')
-	    {
-	      unescape_to_line (s[1]);
-	      ++s;
-	    }
-	  else
-	    _mu_line_add (s, 1);
-	}
-    }
-  else
-    _mu_line_add (s, strlen (s));
-}
-
-static char *
-multiline_finish ()
-{
-  return _mu_line_finish ();
-}
-
-
-int
-mu_cfg_parse_file (mu_cfg_tree_t **return_tree, const char *file, int flags)
-{
-  struct stat st;
-  FILE *fp;
-  int rc;
-  char *full_name = mu_tilde_expansion (file, "/", NULL);
-
-  if (stat (full_name, &st))
-    {
-      if (errno != ENOENT)
-	mu_error (_("cannot stat `%s': %s"), full_name, mu_strerror (errno));
-      free (full_name);
-      return ENOENT;
-    }
-  else if (!S_ISREG (st.st_mode))
-    {
-      if (flags & MU_PARSE_CONFIG_VERBOSE)
-	mu_diag_output (MU_DIAG_INFO, _("%s: not a regular file"), full_name);
-      free (full_name);
-      return ENOENT;
-    } 
-      
-  fp = fopen (full_name, "r");
-  if (!fp)
-    {
-      mu_error (_("cannot open config file `%s': %s"), full_name,
-		mu_strerror (errno));
-      free (full_name);
-      return errno;
-    }
-
-  if (flags & MU_PARSE_CONFIG_VERBOSE)
-    mu_diag_output (MU_DIAG_INFO, _("parsing file `%s'"), full_name);
-
-  mu_cfg_set_lex_debug ();
-
-  /* Initialize locus: */
-  /* 1. Save file name in the lexer object pool and point `file' member
-     to this copy. Free full_name: it is not used after that. */
-  _mu_line_begin ();
-  _mu_line_add (full_name, strlen (full_name));
-  mu_cfg_locus.file = _mu_line_finish ();
-  free (full_name); 
-  /* 2. Initialize line number */
-  mu_cfg_locus.line = 1;
-  
-  /* Parse configuration */
-  yyrestart (fp);
-  rc = mu_cfg_parse (return_tree);
-  fclose (fp);
-  if (flags & MU_PARSE_CONFIG_VERBOSE)
-    mu_diag_output (MU_DIAG_INFO, _("finished parsing file `%s'"),
-		    mu_cfg_locus.file);
-
-  return rc == 0 ? 0 : MU_ERR_FAILURE;
-}
-
-/* FIXME: Deprecated interface */
-int
-mu_get_config (const char *file, const char *progname,
-               struct mu_cfg_param *progparam, int flags, void *target_ptr)
-{
-  mu_cfg_tree_t *parse_tree;
-  int rc = mu_cfg_parse_file (&parse_tree, file, flags);
-  if (rc == 0)
-    {
-      rc = mu_cfg_tree_postprocess (parse_tree, flags);
-      if (rc == 0)
-	rc = mu_cfg_tree_reduce (parse_tree, progname, progparam, flags,
-				 target_ptr);
-      mu_cfg_destroy_tree (&parse_tree);
-    }
-
-  return rc == 0 ? 0 : MU_ERR_FAILURE;
-}
-
-
-mu_opool_t 
-mu_cfg_lexer_pool ()
-{
-  mu_opool_t p = pool;
-  pool = NULL;
-  return p;
-}
diff --git a/libmailutils/cfg_parser.y b/libmailutils/cfg_parser.y
deleted file mode 100644
index f6a5397..0000000
--- a/libmailutils/cfg_parser.y
+++ /dev/null
@@ -1,1839 +0,0 @@
-%{
-/* cfg_parser.y -- general-purpose configuration file parser
-   Copyright (C) 2007, 2008, 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, see <http://www.gnu.org/licenses/>.
-*/
-#ifdef HAVE_CONFIG_H
-# include <config.h>
-#endif
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <stdarg.h>
-#include <string.h>
-#include <netdb.h>
-#include "intprops.h"
-#include <mailutils/argcv.h>
-#include <mailutils/nls.h>
-#include <mailutils/cfg.h>
-#include <mailutils/alloc.h>
-#include <mailutils/errno.h>
-#include <mailutils/error.h>
-#include <mailutils/list.h>
-#include <mailutils/iterator.h>
-#include <mailutils/debug.h>
-#include <mailutils/mutil.h>
-#include <mailutils/cctype.h>
-
-int mu_cfg_parser_verbose;
-static mu_list_t /* of mu_cfg_node_t */ parse_node_list; 
-mu_cfg_locus_t mu_cfg_locus;
-size_t mu_cfg_error_count;
-
-static int _mu_cfg_errcnt;
-static mu_debug_t _mu_cfg_debug;
-
-int yylex ();
-
-void _mu_line_begin (void);
-void _mu_line_add (char *text, size_t len);
-char *_mu_line_finish (void);
-
-static int
-yyerror (char *s)
-{
-  mu_cfg_parse_error ("%s", s);
-  return 0;
-}
-
-static mu_config_value_t *
-config_value_dup (mu_config_value_t *src)
-{
-  if (!src)
-    return NULL;
-  else
-    {
-      /* FIXME: Use mu_opool_alloc */
-      mu_config_value_t *val = mu_alloc (sizeof (*val));
-      *val = *src;
-      return val;
-    }
-}
-
-static mu_cfg_node_t *
-mu_cfg_alloc_node (enum mu_cfg_node_type type, mu_cfg_locus_t *loc,
-		   const char *tag, mu_config_value_t *label,
-		   mu_list_t nodelist)
-{
-  char *p;
-  mu_cfg_node_t *np;
-  size_t size = sizeof *np + strlen (tag) + 1;
-  np = mu_alloc (size);
-  np->type = type;
-  np->locus = *loc;
-  p = (char*) (np + 1);
-  np->tag = p;
-  strcpy (p, tag);
-  np->label = label;
-  np->nodes = nodelist;
-  return np;
-}
-
-void
-mu_cfg_free_node (mu_cfg_node_t *node)
-{
-  free (node->label);
-  free (node);
-}
-
-void
-mu_cfg_format_error (mu_debug_t debug, size_t level, const char *fmt, ...)
-{
-  va_list ap;
-
-  if (!debug)
-    mu_diag_get_debug (&debug);
-  va_start (ap, fmt);
-  mu_debug_vprintf (debug, 0, fmt, ap);
-  mu_debug_printf (debug, 0, "\n");
-  va_end (ap);
-  if (level <= MU_DEBUG_ERROR)
-    mu_cfg_error_count++;
-}
-
-static void
-_mu_cfg_debug_set_locus (mu_debug_t debug, const mu_cfg_locus_t *loc)
-{
-  mu_debug_set_locus (debug, loc->file ? loc->file : _("unknown file"),
-		      loc->line);
-}
-
-void
-mu_cfg_vperror (mu_debug_t debug, const mu_cfg_locus_t *loc,
-		 const char *fmt, va_list ap)
-{
-  if (!debug)
-    mu_diag_get_debug (&debug);
-  _mu_cfg_debug_set_locus (debug, loc);
-  mu_debug_vprintf (debug, 0, fmt, ap);
-  mu_debug_printf (debug, 0, "\n");
-  mu_debug_set_locus (debug, NULL, 0);
-  mu_cfg_error_count++;
-}
-
-void
-mu_cfg_perror (mu_debug_t debug, const mu_cfg_locus_t *loc,
-	       const char *fmt, ...)
-{
-  va_list ap;
-
-  va_start (ap, fmt);
-  mu_cfg_vperror (debug, loc, fmt, ap);
-  va_end (ap);
-}
-
-void
-mu_cfg_parse_error (const char *fmt, ...)
-{
-  va_list ap;
-
-  va_start (ap, fmt);
-  mu_cfg_vperror (_mu_cfg_debug, &mu_cfg_locus, fmt, ap);
-  va_end (ap);
-}
-
-#define node_type_str(t) (((t) == mu_cfg_node_statement) ? "stmt" : "param")
-
-static void
-debug_print_node (mu_cfg_node_t *node)
-{
-  if (mu_debug_check_level (_mu_cfg_debug, MU_DEBUG_TRACE0))
-    {
-      mu_debug_set_locus (_mu_cfg_debug,
-			  node->locus.file, node->locus.line);
-      if (node->type == mu_cfg_node_undefined)
-	/* Stay on the safe side */
-	mu_cfg_format_error (_mu_cfg_debug, MU_DEBUG_ERROR,
-			     "unknown statement type!");
-      else
-	/* FIXME: How to print label? */
-	mu_cfg_format_error (_mu_cfg_debug, MU_DEBUG_TRACE0,
-			     "statement: %s, id: %s",
-			     node_type_str (node->type),
-			     node->tag ? node->tag : "(null)");
-
-      mu_debug_set_locus (_mu_cfg_debug, NULL, 0);
-    }
-}
-
-static void
-free_node_item (void *item)
-{
-  mu_cfg_node_t *node = item;
-
-  switch (node->type)
-    {
-    case mu_cfg_node_statement:
-      mu_list_destroy (&node->nodes);
-      break;
-      
-    case mu_cfg_node_undefined: /* hmm... */
-    case mu_cfg_node_param:
-      break;
-    }
-  mu_cfg_free_node (node);
-}
-
-int
-mu_cfg_create_node_list (mu_list_t *plist)
-{
-  int rc;
-  mu_list_t list;
-
-  rc = mu_list_create (&list);
-  if (rc)
-    return rc;
-  mu_list_set_destroy_item (list, free_node_item);
-  *plist = list;
-  return 0;
-}
-
-%}
-
-%union {
-  mu_cfg_node_t node;
-  mu_cfg_node_t *pnode;
-  mu_list_t /* of mu_cfg_node_t */ nodelist;
-  char *string;
-  mu_config_value_t value, *pvalue;
-  mu_list_t list;
-  struct { const char *name; mu_cfg_locus_t locus; } ident;
-}
-
-%token <string> MU_TOK_IDENT MU_TOK_STRING MU_TOK_QSTRING MU_TOK_MSTRING
-%type <string> string slist
-%type <list> slist0
-%type <value> value
-%type <pvalue> tag vallist
-%type <list> values list vlist
-%type <ident> ident
-%type <nodelist> stmtlist
-%type <pnode> stmt simple block
-
-%%
-
-input   : stmtlist
-	  {
-	    parse_node_list = $1;
-	  }
-	;
-
-stmtlist: stmt
-	  {
-	    mu_cfg_create_node_list (&$$);
-	    mu_list_append ($$, $1);
-	  }
-	| stmtlist stmt
-	  {
-	    mu_list_append ($1, $2);
-	    $$ = $1;
-	    debug_print_node ($2);
-	  }
-	;
-
-stmt    : simple
-	| block
-	;
-
-simple  : ident vallist ';'
-	  {
-	    $$ = mu_cfg_alloc_node (mu_cfg_node_param, &$1.locus,
-				    $1.name, $2,
-				    NULL);
-	  }
-	;
-
-block   : ident tag '{' '}' opt_sc
-	  {
-	    $$ = mu_cfg_alloc_node (mu_cfg_node_statement, &$1.locus,
-				    $1.name, $2,
-				    NULL);
-
-	  }
-	| ident tag '{' stmtlist '}' opt_sc
-	  {
-	    $$ = mu_cfg_alloc_node (mu_cfg_node_statement, &$1.locus,
-				    $1.name, $2, $4);
-
-	  }
-	;
-
-ident   : MU_TOK_IDENT
-	  {
-	    $$.name = $1;
-	    $$.locus = mu_cfg_locus;
-	  }
-	;
-
-tag     : /* empty */
-	  {
-	    $$ = NULL;
-	  }
-	| vallist
-	;
-
-vallist : vlist
-	  {
-	    size_t n = 0;
-	    mu_list_count($1, &n);
-	    if (n == 1)
-	      {
-		mu_list_get ($1, 0, (void**) &$$);
-	      }
-	    else
-	      {
-		size_t i;
-		mu_config_value_t val;
-
-		val.type = MU_CFG_ARRAY;
-		val.v.arg.c = n;
-		/* FIXME: Use mu_opool_alloc */
-		val.v.arg.v = mu_alloc (n * sizeof (val.v.arg.v[0]));
-		if (!val.v.arg.v)
-		  {
-		    mu_cfg_parse_error (_("not enough memory"));
-		    abort();
-		  }
-
-		for (i = 0; i < n; i++)
-		  {
-		    mu_config_value_t *v;
-		    mu_list_get ($1, i, (void **) &v);
-		    val.v.arg.v[i] = *v;
-		  }
-		$$ = config_value_dup (&val);
-	      }
-	    mu_list_destroy (&$1);
-	  }
-	;
-
-vlist   : value
-	    {
-	      int rc = mu_list_create (&$$);
-	      if (rc)
-		{
-		  mu_cfg_parse_error (_("cannot create list: %s"),
-				      mu_strerror (rc));
-		  abort ();
-		}
-	      mu_list_append ($$, config_value_dup (&$1)); /* FIXME */
-	  }
-	| vlist value
-	  {
-	    mu_list_append ($1, config_value_dup (&$2));
-	  }
-	;
-
-value   : string
-	  {
-	      $$.type = MU_CFG_STRING;
-	      $$.v.string = $1;
-	  }
-	| list
-	  {
-	      $$.type = MU_CFG_LIST;
-	      $$.v.list = $1;
-	  }
-	| MU_TOK_MSTRING
-	  {
-	      $$.type = MU_CFG_STRING;
-	      $$.v.string = $1;
-	  }
-	;
-
-string  : MU_TOK_STRING
-	| MU_TOK_IDENT
-	| slist
-	;
-
-slist   : slist0
-	  {
-	    mu_iterator_t itr;
-	    mu_list_get_iterator ($1, &itr);
-
-	    _mu_line_begin ();
-	    for (mu_iterator_first (itr);
-		 !mu_iterator_is_done (itr); mu_iterator_next (itr))
-	      {
-		char *p;
-		mu_iterator_current (itr, (void**)&p);
-		_mu_line_add (p, strlen (p));
-	      }
-	    $$ = _mu_line_finish ();
-	    mu_iterator_destroy (&itr);
-	    mu_list_destroy(&$1);
-	  }
-	;
-
-slist0  : MU_TOK_QSTRING
-	  {
-	    mu_list_create (&$$);
-	    mu_list_append ($$, $1);
-	  }
-	| slist0 MU_TOK_QSTRING
-	  {
-	    mu_list_append ($1, $2);
-	    $$ = $1;
-	  }
-	;
-
-list    : '(' values ')'
-	  {
-	      $$ = $2;
-	  }
-	| '(' values ',' ')'
-	  {
-	      $$ = $2;
-	  }
-	;
-
-values  : value
-	  {
-	    mu_list_create (&$$);
-	    mu_list_append ($$, config_value_dup (&$1));
-	  }
-	| values ',' value
-	  {
-	    mu_list_append ($1, config_value_dup (&$3));
-	    $$ = $1;
-	  }
-	;
-
-opt_sc  : /* empty */
-	| ';'
-	;
-
-
-%%
-
-static int
-_cfg_default_printer (void *unused, mu_log_level_t level, const char *str)
-{
-  fprintf (stderr, "%s", str);
-  return 0;
-}
-
-mu_debug_t
-mu_cfg_get_debug ()
-{
-  if (!_mu_cfg_debug)
-    {
-      mu_debug_create (&_mu_cfg_debug, NULL);
-      mu_debug_set_print (_mu_cfg_debug, _cfg_default_printer, NULL);
-      mu_debug_set_level (_mu_cfg_debug, mu_global_debug_level ("config"));
-    }
-  return _mu_cfg_debug;
-}
-
-void
-mu_cfg_set_debug ()
-{
-  if (mu_debug_check_level (mu_cfg_get_debug (), MU_DEBUG_TRACE7))
-    yydebug = 1;
-}
-
-int
-mu_cfg_parse (mu_cfg_tree_t **ptree)
-{
-  int rc;
-  mu_cfg_tree_t *tree;
-  mu_opool_t pool;
-  
-  mu_cfg_set_debug ();
-  _mu_cfg_errcnt = 0;
-
-  rc = yyparse ();
-  pool = mu_cfg_lexer_pool ();
-  if (rc == 0 && _mu_cfg_errcnt)
-    {
-      mu_opool_destroy (&pool);
-      rc = 1;
-    }
-  else
-    {
-      tree = mu_alloc (sizeof (*tree));
-      tree->debug = _mu_cfg_debug;
-      _mu_cfg_debug = NULL;
-      tree->nodes = parse_node_list;
-      tree->pool = pool;
-      parse_node_list = NULL;
-      *ptree = tree;
-    }
-  return rc;
-}
-
-int
-mu_cfg_tree_union (mu_cfg_tree_t **pa, mu_cfg_tree_t **pb)
-{
-  mu_cfg_tree_t *a, *b;
-  int rc;
-  
-  if (!pb)
-    return EINVAL;
-  if (!*pb)
-    return 0;
-  b = *pb;
-  if (!pa)
-    return EINVAL;
-  if (!*pa)
-    {
-      *pa = b;
-      *pb = NULL;
-      return 0;
-    }
-  else
-    a = *pa;
-  
-  /* Merge opools */
-  rc = mu_opool_union (&b->pool, &a->pool);
-  if (rc)
-    return rc;
-    
-  /* Link node lists */
-  if (b->nodes)
-    {
-      mu_list_append_list (a->nodes, b->nodes);
-      mu_list_destroy (&b->nodes);
-    }
-  
-  mu_debug_destroy (&b->debug, mu_debug_get_owner (b->debug));
-  free (b);
-  *pb = NULL;
-  return 0;
-}
-
-static mu_cfg_tree_t *
-do_include (const char *name, int flags, mu_cfg_locus_t *loc)
-{
-  struct stat sb;
-  char *tmpname = NULL;
-  mu_cfg_tree_t *tree = NULL;
-  
-  if (name[0] != '/')
-    {
-      name = tmpname = mu_make_file_name (SYSCONFDIR, name);
-      if (!name)
-        {
-          mu_error ("%s", mu_strerror (errno));
-          return NULL;
-        }
-    }
-  if (stat (name, &sb) == 0)
-    {
-      int rc = 0;
-
-      if (S_ISDIR (sb.st_mode))
-	{
-	  if (flags & MU_PARSE_CONFIG_GLOBAL)
-	    {
-	      char *file = mu_make_file_name (name, mu_program_name);
-	      rc = mu_cfg_parse_file (&tree, file, flags);
-	      free (file);
-	    }
-	}
-      else
-	rc = mu_cfg_parse_file (&tree, name, flags);
-	      
-      if (rc == 0 && tree)
-	mu_cfg_tree_postprocess (tree, flags & ~MU_PARSE_CONFIG_GLOBAL);
-    }
-  else if (errno == ENOENT)
-    mu_cfg_perror (tree->debug, loc,
-		   _("include file or directory does not exist"));
-  else
-    mu_cfg_perror (tree->debug, loc,
-		   _("cannot stat include file or directory: %s"),
-		   mu_strerror (errno));
-  free (tmpname);
-  return tree;
-}
-    
-int
-mu_cfg_tree_postprocess (mu_cfg_tree_t *tree, int flags)
-{
-  int rc;
-  mu_iterator_t itr;
-
-  if (!tree->nodes)
-    return 0;
-  rc = mu_list_get_iterator (tree->nodes, &itr);
-  if (rc)
-    return rc;
-  for (mu_iterator_first (itr); !mu_iterator_is_done (itr);
-       mu_iterator_next (itr))
-    {
-      mu_cfg_node_t *node;
-
-      mu_iterator_current (itr, (void**) &node);
-
-      if (node->type == mu_cfg_node_statement)
-	{
-	  if ((flags & MU_PARSE_CONFIG_GLOBAL) &&
-	      strcmp (node->tag, "program") == 0)
-	    {
-	      if (node->label->type == MU_CFG_STRING)
-		{
-		  if (strcmp (node->label->v.string, mu_program_name) == 0)
-		    {
-		      /* Move all nodes from this block to the topmost
-			 level */
-		      mu_iterator_ctl (itr, mu_itrctl_insert_list,
-				       node->nodes);
-		      mu_iterator_ctl (itr, mu_itrctl_delete, NULL);
-		      /*FIXME:mu_cfg_free_node (node);*/
-		    }
-		}
-	      else
-		{
-		  mu_cfg_perror (tree->debug, &node->locus,
-				 _("argument to `program' is not a string"));
-		  mu_iterator_ctl (itr, mu_itrctl_delete, NULL);
-		}
-	    }
-	}
-      else if (node->type == mu_cfg_node_param &&
-	       strcmp (node->tag, "include") == 0)
-	{
-	  if (node->label->type == MU_CFG_STRING)
-	    {
-	      mu_cfg_tree_t *t = do_include (node->label->v.string, flags,
-					     &node->locus);
-	      if (t)
-		{
-		  /* Merge the new tree into the current point and
-		     destroy the rest of it */
-		  mu_iterator_ctl (itr, mu_itrctl_insert_list, t->nodes);
-		  mu_opool_union (&tree->pool, &t->pool);
-		  mu_cfg_destroy_tree (&t);
-		}		      
-	    }
-	  else
-	    mu_cfg_perror (tree->debug, &node->locus,
-			   _("argument to `include' is not a string"));
-	  /* Remove node from the list */
-	  mu_iterator_ctl (itr, mu_itrctl_delete, NULL);
-	}
-    }
-  return 0;
-}
-
-static int
-_mu_cfg_preorder_recursive (void *item, void *cbdata)
-{
-  mu_cfg_node_t *node = item;
-  struct mu_cfg_iter_closure *clos = cbdata;
-
-  switch (node->type)
-    {
-    case mu_cfg_node_undefined:
-      abort ();
-
-    case mu_cfg_node_statement:
-      switch (clos->beg (node, clos->data))
-	{
-	case MU_CFG_ITER_OK:
-	  if (mu_cfg_preorder (node->nodes, clos))
-	    return 1;
-	  if (clos->end && clos->end (node, clos->data) == MU_CFG_ITER_STOP)
-	    return 1;
-	  break;
-
-	case MU_CFG_ITER_SKIP:
-	  break;
-
-	case MU_CFG_ITER_STOP:
-	  return 1;
-	}
-      break;
-
-    case mu_cfg_node_param:
-      return clos->beg (node, clos->data) == MU_CFG_ITER_STOP;
-    }
-  return 0;
-}
-
-int
-mu_cfg_preorder (mu_list_t nodelist, struct mu_cfg_iter_closure *clos)
-{
-  return mu_list_do (nodelist, _mu_cfg_preorder_recursive, clos);
-}
-
-
-
-void
-mu_cfg_destroy_tree (mu_cfg_tree_t **ptree)
-{
-  if (ptree && *ptree)
-    {
-      mu_cfg_tree_t *tree = *ptree;
-      mu_list_destroy (&tree->nodes);
-      mu_opool_destroy (&tree->pool);
-      *ptree = NULL;
-    }
-}
-
-
-
-struct mu_cfg_section_list
-{
-  struct mu_cfg_section_list *next;
-  struct mu_cfg_section *sec;
-};
-
-struct scan_tree_data
-{
-  struct mu_cfg_section_list *list;
-  void *target;
-  void *call_data;
-  mu_cfg_tree_t *tree;
-  int error;
-};
-
-static struct mu_cfg_cont *
-find_container (mu_list_t list, enum mu_cfg_cont_type type,
-		const char *ident, size_t len)
-{
-  mu_iterator_t iter;
-  struct mu_cfg_cont *ret = NULL;
-
-  if (len == 0)
-    len = strlen (ident);
-
-  mu_list_get_iterator (list, &iter);
-  for (mu_iterator_first (iter); !mu_iterator_is_done (iter);
-       mu_iterator_next (iter))
-    {
-      struct mu_cfg_cont *cont;
-      mu_iterator_current (iter, (void**) &cont);
-
-      if (cont->type == type
-	  && strlen (cont->v.ident) == len
-	  && memcmp (cont->v.ident, ident, len) == 0)
-	{
-	  ret = cont;
-	  break;
-	}
-    }
-  mu_iterator_destroy (&iter);
-  return ret;
-}
-
-static struct mu_cfg_section *
-find_subsection (struct mu_cfg_section *sec, const char *ident, size_t len)
-{
-  if (sec)
-    {
-      if (sec->children)
-	{
-	  struct mu_cfg_cont *cont = find_container (sec->children,
-						     mu_cfg_cont_section,
-						     ident, len);
-	  if (cont)
-	    return &cont->v.section;
-	}
-    }
-  return NULL;
-}
-
-static struct mu_cfg_param *
-find_param (struct mu_cfg_section *sec, const char *ident, size_t len)
-{
-  if (sec)
-    {
-      if (sec->children)
-	{
-	  struct mu_cfg_cont *cont = find_container (sec->children,
-						     mu_cfg_cont_param,
-						     ident, len);
-	  if (cont)
-	    return &cont->v.param;
-	}
-    }
-  return NULL;
-}
-
-static int
-push_section (struct scan_tree_data *dat, struct mu_cfg_section *sec)
-{
-  struct mu_cfg_section_list *p = mu_alloc (sizeof *p);
-  if (!p)
-    {
-      mu_cfg_perror (dat->tree->debug, NULL, _("not enough memory"));
-      return 1;
-    }
-  p->sec = sec;
-  p->next = dat->list;
-  dat->list = p;
-  return 0;
-}
-
-static struct mu_cfg_section *
-pop_section (struct scan_tree_data *dat)
-{
-  struct mu_cfg_section_list *p = dat->list;
-  struct mu_cfg_section *sec = p->sec;
-  dat->list = p->next;
-  free (p);
-  return sec;
-}
-
-#define STRTONUM(s, type, base, res, limit, d, loc)			\
-  {									\
-    type sum = 0;							\
-									\
-    for (; *s; s++)							\
-      {									\
-	type x;								\
-									\
-	if ('0' <= *s && *s <= '9')					\
-	  x = sum * base + *s - '0';					\
-	else if (base == 16 && 'a' <= *s && *s <= 'f')			\
-	  x = sum * base + *s - 'a';					\
-	else if (base == 16 && 'A' <= *s && *s <= 'F')			\
-	  x = sum * base + *s - 'A';					\
-	else								\
-	  break;							\
-	if (x <= sum)							\
-	  {								\
-	    mu_cfg_perror (d, loc, _("numeric overflow"));		\
-	    return 1;							\
-	  }								\
-	else if (limit && x > limit)					\
-	  {								\
-	    mu_cfg_perror (d, loc,					\
-			    _("value out of allowed range"));		\
-	    return 1;							\
-	  }								\
-	sum = x;							\
-      }									\
-    res = sum;								\
-  }
-
-#define STRxTONUM(s, type, res, limit, d, loc)				\
-  {									\
-    int base;								\
-    if (*s == '0')							\
-      {									\
-	s++;								\
-	if (*s == 0)							\
-	  base = 10;							\
-	else if (*s == 'x' || *s == 'X')				\
-	  {								\
-	    s++;							\
-	    base = 16;							\
-	  }								\
-	else								\
-	  base = 8;							\
-      } else								\
-      base = 10;							\
-    STRTONUM (s, type, base, res, limit, d, loc);			\
-  }
-
-#define GETUNUM(str, type, res, d, loc)					\
-  {									\
-    type tmpres;							\
-    const char *s = str;						\
-    STRxTONUM (s, type, tmpres, 0, d, loc);				\
-    if (*s)								\
-      {									\
-	mu_cfg_perror (d, loc,						\
-		       _("not a number (stopped near `%s')"),		\
-		       s);						\
-	return 1;							\
-      }									\
-    res = tmpres;							\
-  }
-
-#define GETSNUM(str, type, res, d, loc)					\
-  {									\
-    unsigned type tmpres;						\
-    const char *s = str;						\
-    int sign;								\
-    unsigned type limit;						\
-									\
-    if (*s == '-')							\
-      {									\
-	sign++;								\
-	s++;								\
-	limit = TYPE_MINIMUM (type);					\
-	limit = - limit;						\
-      }									\
-    else								\
-      {									\
-	sign = 0;							\
-	limit = TYPE_MAXIMUM (type);					\
-      }									\
-									\
-    STRxTONUM (s, unsigned type, tmpres, limit, d, loc);		\
-    if (*s)								\
-      {									\
-	mu_cfg_perror (d, loc,						\
-		       _("not a number (stopped near `%s')"),		\
-		       s);						\
-	return 1;							\
-      }									\
-    res = sign ? - tmpres : tmpres;					\
-  }
-
-static int
-parse_ipv4 (struct scan_tree_data *sdata, const mu_cfg_locus_t *locus,
-	    const char *str, struct in_addr *res)
-{
-  struct in_addr addr;
-  if (inet_aton (str, &addr) == 0)
-    {
-      mu_cfg_perror (sdata->tree->debug, locus, _("not an IPv4"));
-      return 1;
-    }
-  addr.s_addr = ntohl (addr.s_addr);
-  *res = addr;
-  return 0;
-}
-
-static int
-parse_host (struct scan_tree_data *sdata, const mu_cfg_locus_t *locus,
-	    const char *str, struct in_addr *res)
-{
-  struct in_addr addr;
-  struct hostent *hp = gethostbyname (str);
-  if (hp)
-    {
-      addr.s_addr = *(unsigned long *)hp->h_addr;
-    }
-  else if (inet_aton (str, &addr) == 0)
-    {
-      mu_cfg_perror (sdata->tree->debug, locus,
-		     _("cannot resolve hostname `%s'"),
-		     str);
-      return 1;
-    }
-  addr.s_addr = ntohl (addr.s_addr);
-  *res = addr;
-  return 0;
-}
-
-static int
-parse_cidr (struct scan_tree_data *sdata, const mu_cfg_locus_t *locus,
-	    const char *str, mu_cfg_cidr_t *res)
-{
-  struct in_addr addr;
-  unsigned long mask;
-  char astr[16];
-  const char *p, *s;
-
-  p = strchr (str, '/');
-  if (p)
-    {
-      int len = p - str;
-      if (len > sizeof astr - 1) {
-	mu_cfg_perror (sdata->tree->debug, locus,
-		       _("not a valid IPv4 address in CIDR"));
-	return 1;
-      }
-      memcpy (astr, str, len);
-      astr[len] = 0;
-      if (inet_aton (astr, &addr) == 0)
-	{
-	  mu_cfg_perror (sdata->tree->debug, locus,
-			 _("not a valid IPv4 address in CIDR"));
-	  return 1;
-	}
-      addr.s_addr = ntohl (addr.s_addr);
-
-      p++;
-      s = p;
-      STRxTONUM (s, unsigned long, mask, 0, sdata->tree->debug, locus);
-      if (*s == '.')
-	{
-	  struct in_addr a;
-	  if (inet_aton (p, &a) == 0)
-	    {
-	      mu_cfg_perror (sdata->tree->debug, locus,
-			     _("not a valid network in CIDR"));
-	      return 1;
-	    }
-	  a.s_addr = ntohl (a.s_addr);
-	  for (mask = 0; (a.s_addr & 1) == 0 && mask < 32; )
-	    {
-	      mask++;
-	      a.s_addr >>= 1;
-	    }
-	  mask = 32 - mask;
-	}
-      else if (mask > 32)
-	{
-	  mu_cfg_perror (sdata->tree->debug, locus,
-			 _("not a valid network mask in CIDR"));
-	  return 1;
-	}
-    }
-  else
-    {
-      int i;
-      unsigned short x;
-      addr.s_addr = 0;
-
-      p = str;
-      for (i = 0; i < 3; i++)
-	{
-	  STRxTONUM (p, unsigned short, x, 255, sdata->tree->debug, locus);
-	  if (*p != '.')
-	    break;
-	  addr.s_addr = (addr.s_addr << 8) + x;
-	}
-
-      if (*p)
-	{
-	  mu_cfg_perror (sdata->tree->debug, locus,
-			 _("not a CIDR (stopped near `%s')"),
-			 p);
-	  return 1;
-	}
-
-      mask = i * 8;
-
-      addr.s_addr <<= (4 - i) * 8;
-    }
-
-  res->addr = addr;
-  res->mask = mask;
-  return 0;
-}
-
-int
-mu_cfg_parse_boolean (const char *str, int *res)
-{
-  if (strcmp (str, "yes") == 0
-      || strcmp (str, "on") == 0
-      || strcmp (str, "t") == 0
-      || strcmp (str, "true") == 0
-      || strcmp (str, "1") == 0)
-    *res = 1;
-  else if (strcmp (str, "no") == 0
-	   || strcmp (str, "off") == 0
-	   || strcmp (str, "nil") == 0
-	   || strcmp (str, "false") == 0
-	   || strcmp (str, "0") == 0)
-    *res = 0;
-  else
-    return 1;
-  return 0;
-}
-
-static int
-parse_bool (struct scan_tree_data *sdata, const mu_cfg_locus_t *locus,
-	    const char *str, int *res)
-{
-  if (mu_cfg_parse_boolean (str, res))
-    {
-      mu_cfg_perror (sdata->tree->debug, locus, _("not a boolean"));
-      return 1;
-    }
-  return 0;
-}
-
-static int
-valcvt (struct scan_tree_data *sdata, const mu_cfg_locus_t *locus,
-	void *tgt,
-	enum mu_cfg_param_data_type type, mu_config_value_t *val)
-{
-  if (val->type != MU_CFG_STRING)
-    {
-      mu_cfg_perror (sdata->tree->debug, locus, _("expected string value"));
-      return 1;
-    }
-  switch (type)
-    {
-    case mu_cfg_string:
-      {
-	char *s = mu_strdup (val->v.string);
-	/* FIXME: free tgt? */
-	*(char**)tgt = s;
-	break;
-      }
-
-    case mu_cfg_short:
-      GETSNUM (val->v.string, short, *(short*)tgt, sdata->tree->debug, locus);
-      break;
-
-    case mu_cfg_ushort:
-      GETUNUM (val->v.string, unsigned short, *(unsigned short*)tgt,
-	       sdata->tree->debug, locus);
-      break;
-
-    case mu_cfg_int:
-      GETSNUM (val->v.string, int, *(int*)tgt, sdata->tree->debug, locus);
-      break;
-
-    case mu_cfg_uint:
-      GETUNUM (val->v.string, unsigned int, *(unsigned int*)tgt,
-	       sdata->tree->debug, locus);
-      break;
-
-    case mu_cfg_long:
-      GETSNUM (val->v.string, long, *(long*)tgt,
-	       sdata->tree->debug, locus);
-      break;
-
-    case mu_cfg_ulong:
-      GETUNUM (val->v.string, unsigned long, *(unsigned long*)tgt,
-	       sdata->tree->debug, locus);
-      break;
-
-    case mu_cfg_size:
-      GETUNUM (val->v.string, size_t, *(size_t*)tgt,
-	       sdata->tree->debug, locus);
-      break;
-
-    case mu_cfg_off:
-      mu_cfg_perror (sdata->tree->debug, locus, _("not implemented yet"));
-      /* GETSNUM(node->tag_label, off_t, *(off_t*)tgt); */
-      return 1;
-
-    case mu_cfg_time:
-      GETUNUM (val->v.string, time_t, *(time_t*)tgt,
-	       sdata->tree->debug, locus);
-      break;
-
-    case mu_cfg_bool:
-      if (parse_bool (sdata, locus, val->v.string, (int*) tgt))
-	return 1;
-      break;
-
-    case mu_cfg_ipv4:
-      if (parse_ipv4 (sdata, locus, val->v.string, (struct in_addr *)tgt))
-	return 1;
-      break;
-
-    case mu_cfg_cidr:
-      if (parse_cidr (sdata, locus, val->v.string, (mu_cfg_cidr_t *)tgt))
-	return 1;
-      break;
-
-    case mu_cfg_host:
-      if (parse_host (sdata, locus, val->v.string, (struct in_addr *)tgt))
-	return 1;
-      break;
-
-    default:
-      return 1;
-    }
-  return 0;
-}
-
-struct set_closure
-{
-  mu_list_t list;
-  enum mu_cfg_param_data_type type;
-  struct scan_tree_data *sdata;
-  const mu_cfg_locus_t *locus;
-};
-
-static size_t config_type_size[] = {
-  sizeof (char*),          /* mu_cfg_string */
-  sizeof (short),          /* mu_cfg_short */
-  sizeof (unsigned short), /* mu_cfg_ushort */
-  sizeof (int),            /* mu_cfg_int */
-  sizeof (unsigned),       /* mu_cfg_uint */
-  sizeof (long),           /* mu_cfg_long */
-  sizeof (unsigned long),  /* mu_cfg_ulong */
-  sizeof (size_t),         /* mu_cfg_size */
-  sizeof (mu_off_t),       /* mu_cfg_off */
-  sizeof (time_t),         /* mu_cfg_time */
-  sizeof (int),            /* mu_cfg_bool */
-  sizeof (struct in_addr), /* mu_cfg_ipv4 */
-  sizeof (mu_cfg_cidr_t),  /* mu_cfg_cidr */
-  sizeof (struct in_addr), /* mu_cfg_host */
-  0,                       /* mu_cfg_callback */
-  0,                       /* mu_cfg_section */
-}  ;
-
-static int
-_set_fun (void *item, void *data)
-{
-  mu_config_value_t *val = item;
-  struct set_closure *clos = data;
-  void *tgt;
-  size_t size;
-
-  if (clos->type >= MU_ARRAY_SIZE(config_type_size)
-      || (size = config_type_size[clos->type]) == 0)
-    {
-    mu_cfg_perror (clos->sdata->tree->debug, clos->locus,
-		   _("INTERNAL ERROR at %s:%d: unhandled data type %d"),
-		   __FILE__, __LINE__, clos->type);
-    return 1;
-    }
-
-  tgt = mu_alloc (size);
-  if (!tgt)
-    {
-      mu_cfg_perror (clos->sdata->tree->debug, clos->locus,
-		     _("not enough memory"));
-      return 1;
-    }
-
-  if (valcvt (clos->sdata, clos->locus, &tgt, clos->type, val) == 0)
-    mu_list_append (clos->list, tgt);
-  return 0;
-}
-
-static int
-parse_param (struct scan_tree_data *sdata, const mu_cfg_node_t *node)
-{
-  void *tgt;
-  struct set_closure clos;
-  struct mu_cfg_param *param = find_param (sdata->list->sec, node->tag,
-					   0);
-
-  if (!param)
-    {
-      mu_cfg_perror (sdata->tree->debug, &node->locus,
-		     _("unknown keyword `%s'"),
-		     node->tag);
-      return 1;
-    }
-
-  if (param->data)
-    tgt = param->data;
-  else if (sdata->list->sec->target)
-    tgt = (char*)sdata->list->sec->target + param->offset;
-  else if (sdata->target)
-    tgt = (char*)sdata->target + param->offset;
-  else if (param->type == mu_cfg_callback)
-    tgt = NULL;
-  else
-    {
-      mu_cfg_perror (sdata->tree->debug, &node->locus,
-		     _("INTERNAL ERROR: cannot determine target offset for "
-		       "%s"), param->ident);
-      abort ();
-    }
-
-  memset (&clos, 0, sizeof clos);
-  clos.type = MU_CFG_TYPE (param->type);
-  if (MU_CFG_IS_LIST (param->type))
-    {
-      clos.sdata = sdata;
-      clos.locus = &node->locus;
-      switch (node->label->type)
-	{
-	case MU_CFG_LIST:
-	  break;
-
-	case MU_CFG_STRING:
-	  {
-	    mu_list_t list;
-	    mu_list_create (&list);
-	    mu_list_append (list, config_value_dup (node->label));
-	    node->label->type = MU_CFG_LIST;
-	    node->label->v.list = list;
-	  }
-	  break;
-
-	case MU_CFG_ARRAY:
-	  mu_cfg_perror (sdata->tree->debug, &node->locus,
-			 _("expected list, but found array"));
-	  return 1;
-	}
-
-      mu_list_create (&clos.list);
-      mu_list_do (node->label->v.list, _set_fun, &clos);
-      *(mu_list_t*)tgt = clos.list;
-    }
-  else if (clos.type == mu_cfg_callback)
-    {
-      mu_debug_set_locus (sdata->tree->debug, node->locus.file,
-			  node->locus.line);
-      if (!param->callback)
-	{
-	  mu_cfg_perror (sdata->tree->debug, &node->locus,
-			 _("INTERNAL ERROR: %s: callback not defined"),
-			 node->tag);
-	  abort ();
-	}
-      if (param->callback (sdata->tree->debug, tgt, node->label))
-	return 1;
-
-    }
-  else
-    return valcvt (sdata, &node->locus, tgt, clos.type, node->label);
-
-  return 0;
-}
-
-
-static int
-_scan_tree_helper (const mu_cfg_node_t *node, void *data)
-{
-  struct scan_tree_data *sdata = data;
-  struct mu_cfg_section *sec;
-
-  switch (node->type)
-    {
-    case mu_cfg_node_undefined:
-      abort ();
-
-    case mu_cfg_node_statement:
-      sec = find_subsection (sdata->list->sec, node->tag, 0);
-      if (!sec)
-	{
-	  if (mu_cfg_parser_verbose)
-	    {
-	      _mu_cfg_debug_set_locus (sdata->tree->debug, &node->locus);
-	      mu_cfg_format_error (sdata->tree->debug, MU_DIAG_WARNING,
-				   _("unknown section `%s'"),
-				   node->tag);
-	    }
-	  return MU_CFG_ITER_SKIP;
-	}
-      if (!sec->children)
-	return MU_CFG_ITER_SKIP;
-      if (sdata->list->sec->target)
-	sec->target = (char*)sdata->list->sec->target + sec->offset;
-      else if (sdata->target)
-	sec->target = (char*)sdata->target + sec->offset;
-      if (sec->parser)
-	{
-	  mu_debug_set_locus (sdata->tree->debug,
-			      node->locus.file ?
-				     node->locus.file : _("unknown file"),
-			      node->locus.line);
-	  if (sec->parser (mu_cfg_section_start, node,
-			   sec->label, &sec->target,
-			   sdata->call_data, sdata->tree))
-	    {
-	      sdata->error++;
-	      return MU_CFG_ITER_SKIP;
-	    }
-	}
-      push_section(sdata, sec);
-      break;
-
-    case mu_cfg_node_param:
-      if (parse_param (sdata, node))
-	{
-	  sdata->error++;
-	  return MU_CFG_ITER_SKIP;
-	}
-      break;
-    }
-  return MU_CFG_ITER_OK;
-}
-
-static int
-_scan_tree_end_helper (const mu_cfg_node_t *node, void *data)
-{
-  struct scan_tree_data *sdata = data;
-  struct mu_cfg_section *sec;
-
-  switch (node->type)
-    {
-    default:
-      abort ();
-
-    case mu_cfg_node_statement:
-      sec = pop_section (sdata);
-      if (sec && sec->parser)
-	{
-	  if (sec->parser (mu_cfg_section_end, node,
-			   sec->label, &sec->target,
-			   sdata->call_data, sdata->tree))
-	    {
-	      sdata->error++;
-	      return MU_CFG_ITER_SKIP;
-	    }
-	}
-    }
-  return MU_CFG_ITER_OK;
-}
-
-int
-mu_cfg_scan_tree (mu_cfg_tree_t *tree, struct mu_cfg_section *sections,
-		  void *target, void *data)
-{
-  mu_debug_t debug = NULL;
-  struct scan_tree_data dat;
-  struct mu_cfg_iter_closure clos;
-    
-  dat.tree = tree;
-  dat.list = NULL;
-  dat.error = 0;
-  dat.call_data = data;
-  dat.target = target;
-  
-  if (!tree->debug)
-    {
-      mu_diag_get_debug (&debug);
-      tree->debug = debug;
-    }
-  if (push_section (&dat, sections))
-    return 1;
-  clos.beg = _scan_tree_helper;
-  clos.end = _scan_tree_end_helper;
-  clos.data = &dat;
-  mu_cfg_preorder (tree->nodes, &clos);
-  if (debug)
-    {
-      mu_debug_set_locus (debug, NULL, 0);
-      tree->debug = NULL;
-    }
-  pop_section (&dat);
-  return dat.error;
-}
-
-int
-mu_cfg_find_section (struct mu_cfg_section *root_sec,
-		     const char *path, struct mu_cfg_section **retval)
-{
-  while (path[0])
-    {
-      struct mu_cfg_section *sec;
-      size_t len;
-      const char *p;
-
-      while (*path == MU_CFG_PATH_DELIM)
-	path++;
-
-      if (*path == 0)
-	return MU_ERR_NOENT;
-
-      p = strchr (path, MU_CFG_PATH_DELIM);
-      if (p)
-	len = p - path;
-      else
-	len = strlen (path);
-
-      sec = find_subsection (root_sec, path, len);
-      if (!sec)
-	return MU_ERR_NOENT;
-      root_sec = sec;
-      path += len;
-    }
-  if (retval)
-    *retval = root_sec;
-  return 0;
-}
-
-
-int
-mu_cfg_tree_create (struct mu_cfg_tree **ptree)
-{
-  struct mu_cfg_tree *tree = calloc (1, sizeof *tree);
-  if (!tree)
-    return errno;
-  mu_opool_create (&tree->pool, 1);
-  *ptree = tree;
-  return 0;
-}
-
-void
-mu_cfg_tree_set_debug (struct mu_cfg_tree *tree, mu_debug_t debug)
-{
-  tree->debug = debug;
-}
-
-mu_cfg_node_t *
-mu_cfg_tree_create_node (struct mu_cfg_tree *tree,
-			 enum mu_cfg_node_type type,
-			 const mu_cfg_locus_t *loc,
-			 const char *tag, const char *label,
-			 mu_list_t nodelist)
-{
-  char *p;
-  mu_cfg_node_t *np;
-  size_t size = sizeof *np + strlen (tag) + 1;
-  mu_config_value_t val;
-
-  np = mu_alloc (size);
-  np->type = type;
-  if (loc)
-    np->locus = *loc;
-  else
-    memset (&np->locus, 0, sizeof np->locus);
-  p = (char*) (np + 1);
-  np->tag = p;
-  strcpy (p, tag);
-  p += strlen (p) + 1;
-  val.type = MU_CFG_STRING;
-  if (label)
-    {
-      mu_opool_clear (tree->pool);
-      mu_opool_appendz (tree->pool, label);
-      val.v.string = mu_opool_finish (tree->pool, NULL);
-      np->label = config_value_dup (&val);
-    }
-  else
-    np->label = NULL;
-  np->nodes = nodelist;
-  return np;
-}
-
-void
-mu_cfg_tree_add_node (mu_cfg_tree_t *tree, mu_cfg_node_t *node)
-{
-  if (!node)
-    return;
-  if (!tree->nodes)
-    /* FIXME: return code? */
-    mu_cfg_create_node_list (&tree->nodes);
-  mu_list_append (tree->nodes, node);
-}
-
-void
-mu_cfg_tree_add_nodelist (mu_cfg_tree_t *tree, mu_list_t nodelist)
-{
-  if (!nodelist)
-    return;
-  if (!tree->nodes)
-    /* FIXME: return code? */
-    mu_cfg_create_node_list (&tree->nodes);
-  mu_list_append_list (tree->nodes, nodelist);
-}
-
-
-/* Return 1 if configuration value A equals B */
-int
-mu_cfg_value_eq (mu_config_value_t *a, mu_config_value_t *b)
-{
-  if (a->type != b->type)
-    return 0;
-  switch (a->type)
-    {
-    case MU_CFG_STRING:
-      if (a->v.string == NULL)
-	return b->v.string == NULL;
-      return strcmp (a->v.string, b->v.string) == 0;
-		     
-    case MU_CFG_LIST:
-      {
-	int ret = 1;
-	size_t cnt;
-	size_t i;
-	mu_iterator_t aitr, bitr;
-	
-	mu_list_count (a->v.list, &cnt);
-	mu_list_count (b->v.list, &i);
-	if (i != cnt)
-	  return 1;
-
-	mu_list_get_iterator (a->v.list, &aitr);
-	mu_list_get_iterator (b->v.list, &bitr);
-	for (i = 0,
-	       mu_iterator_first (aitr),
-	       mu_iterator_first (bitr);
-	     !mu_iterator_is_done (aitr) && !mu_iterator_is_done (bitr);
-	     mu_iterator_next (aitr),
-	       mu_iterator_next (bitr),
-	       i++)
-	  {
-	    mu_config_value_t *ap, *bp;
-	    mu_iterator_current (aitr, (void**)&ap);
-	    mu_iterator_current (bitr, (void**)&bp);
-	    ret = mu_cfg_value_eq (ap, bp);
-	    if (!ret)
-	      break;
-	  }
-	mu_iterator_destroy (&aitr);
-	mu_iterator_destroy (&bitr);
-	return ret && i == cnt;
-      }
-	
-    case MU_CFG_ARRAY:
-      if (a->v.arg.c == b->v.arg.c)
-	{
-	  size_t i;
-	  for (i = 0; i < a->v.arg.c; i++)
-	    if (!mu_cfg_value_eq (&a->v.arg.v[i], &b->v.arg.v[i]))
-	      return 0;
-	  return 1;
-	}
-    }
-  return 0;
-}
-
-
-static int
-split_cfg_path (const char *path, int *pargc, char ***pargv)
-{
-  int rc;
-  int argc;
-  char **argv;
-  char *delim = MU_CFG_PATH_DELIM_STR;
-  char static_delim[2] = { 0, 0 };
-  
-  if (path[0] == '\\')
-    {
-      argv = calloc (2, sizeof (*argv));
-      if (!argv)
-	return ENOMEM;
-      argv[0] = strdup (path + 1);
-      if (!argv[0])
-	{
-	  free (argv);
-	  return ENOMEM;
-	}
-      argv[1] = NULL;
-      argc = 1;
-      rc = 0;
-    }
-  else
-    {
-      if (mu_ispunct (path[0]))
-	{
-	  delim = static_delim;
-	  delim[0] = path[0];
-	  path++;
-	}
-      rc = mu_argcv_get_np (path, strlen (path), delim, NULL, 0,
-			    &argc, &argv, NULL);
-    }
-  if (rc == 0)
-    {
-      *pargc = argc;
-      *pargv = argv;
-    }
-  return rc;
-}
-
-struct find_data
-{
-  int argc;
-  char **argv;
-  int tag;
-  mu_config_value_t *label;
-  const mu_cfg_node_t *node;
-};
-
-static void
-free_value_mem (mu_config_value_t *p)
-{
-  switch (p->type)
-    {
-    case MU_CFG_STRING:
-      free ((char*)p->v.string);
-      break;
-      
-    case MU_CFG_LIST:
-      /* FIXME */
-      break;
-      
-    case MU_CFG_ARRAY:
-      {
-	size_t i;
-	for (i = 0; i < p->v.arg.c; i++)
-	  free_value_mem (&p->v.arg.v[i]);
-      }
-    }
-}
-
-static void
-destroy_value (void *p)
-{
-  mu_config_value_t *val = p;
-  if (val)
-    {
-      free_value_mem (val);
-      free (val);
-    }
-}
-
-static mu_config_value_t *
-parse_label (const char *str)
-{
-  mu_config_value_t *val = NULL;
-  int count, i;
-  char **vect;
-  size_t len = strlen (str);
-  
-  if (len > 1 && str[0] == '(' && str[len-1] == ')')
-    {
-      mu_list_t lst;
-      mu_argcv_get_np (str + 1, len - 2,
-		       ", \t", NULL,
-		       0,
-		       &count, &vect, NULL);
-      mu_list_create (&lst);
-      mu_list_set_destroy_item (lst, destroy_value);
-      for (i = 0; i < count; i++)
-	{
-	  mu_config_value_t *p = mu_alloc (sizeof (*p));
-	  p->type = MU_CFG_STRING;
-	  p->v.string = vect[i];
-	  mu_list_append (lst, p);
-	}
-      free (vect);
-      val = mu_alloc (sizeof (*val));
-      val->type = MU_CFG_LIST;
-      val->v.list = lst;
-    }
-  else
-    {      
-      mu_argcv_get (str, NULL, NULL, &count, &vect);
-      val = mu_alloc (sizeof (*val));
-      if (count == 1)
-	{
-	  val->type = MU_CFG_STRING;
-	  val->v.string = vect[0];
-	  free (vect);
-	}
-      else
-	{
-	  val->type = MU_CFG_ARRAY;
-	  val->v.arg.c = count;
-	  val->v.arg.v = mu_alloc (count * sizeof (val->v.arg.v[0]));
-	  for (i = 0; i < count; i++)
-	    {
-	      val->v.arg.v[i].type = MU_CFG_STRING;
-	      val->v.arg.v[i].v.string = vect[i];
-	    }
-	  free (vect);
-	}
-    }
-  return val;
-}
-
-static void
-parse_tag (struct find_data *fptr)
-{
-  char *p = strchr (fptr->argv[fptr->tag], '=');
-  if (p)
-    {
-      *p++ = 0;
-      fptr->label = parse_label (p);
-    }
-  else
-    fptr->label = NULL;
-}
-
-static int
-node_finder (const mu_cfg_node_t *node, void *data)
-{
-  struct find_data *fdptr = data;
-  if (strcmp (fdptr->argv[fdptr->tag], node->tag) == 0
-      && (!fdptr->label || mu_cfg_value_eq (fdptr->label, node->label)))
-    {
-      fdptr->tag++;
-      if (fdptr->tag == fdptr->argc)
-	{
-	  fdptr->node = node;
-	  return MU_CFG_ITER_STOP;
-	}
-      parse_tag (fdptr);
-      return MU_CFG_ITER_OK;
-    }
-  
-  return node->type == mu_cfg_node_statement ?
-               MU_CFG_ITER_SKIP : MU_CFG_ITER_OK;
-}
-
-int	    
-mu_cfg_find_node (mu_cfg_tree_t *tree, const char *path, mu_cfg_node_t **pval)
-{
-  int rc;
-  struct find_data data;
-  struct mu_cfg_iter_closure clos;
-
-  rc = split_cfg_path (path, &data.argc, &data.argv);
-  if (rc)
-    return rc;
-  data.tag = 0;
-  parse_tag (&data);
-
-  clos.beg = node_finder;
-  clos.end = NULL;
-  clos.data = &data;
-  rc = mu_cfg_preorder (tree->nodes, &clos);
-  destroy_value (data.label);
-  if (rc)
-    {
-      *pval = (mu_cfg_node_t *) data.node;
-      return 0;
-    }
-  return MU_ERR_NOENT;
-}
-
-
-
-int
-mu_cfg_create_subtree (const char *path, mu_cfg_node_t **pnode)
-{
-  int rc;
-  int argc, i;
-  char **argv;
-  enum mu_cfg_node_type type;
-  mu_cfg_node_t *node = NULL;
-  mu_cfg_locus_t locus;
-
-  locus.file = "<int>";
-  locus.line = 0;
-
-  rc = split_cfg_path (path, &argc, &argv);
-  if (rc)
-    return rc;
-
-  for (i = argc - 1; i >= 0; i--)
-    {
-      mu_list_t nodelist = NULL;
-      mu_config_value_t *label = NULL;
-      char *q = argv[i], *p;
-      
-      type = mu_cfg_node_statement;
-      do
-	{
-	  p = strchr (q, '=');
-	  if (p && p > argv[i] && p[-1] != '\\')
-	    {
-	      *p++ = 0;
-	      label = parse_label (p);
-	      if (i == argc - 1)
-		type = mu_cfg_node_param;
-	      break;
-	    }
-	  else if (p)
-	    q = p + 1;
-	  else
-	    break;
-	}
-      while (*q);
-      
-      if (node)
-	{
-	  mu_cfg_create_node_list (&nodelist);
-	  mu_list_append (nodelist, node);
-	}
-      node = mu_cfg_alloc_node (type, &locus, argv[i], label, nodelist);
-    }
-
-  mu_argcv_free (argc, argv);
-  *pnode = node;
-  return 0;
-}
-
-  
-	  
-        
-  
diff --git a/libmailutils/crlfdot.c b/libmailutils/crlfdot.c
deleted file mode 100644
index eed4519..0000000
--- a/libmailutils/crlfdot.c
+++ /dev/null
@@ -1,336 +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 */
-
-/* This source implements a CRLFDOT filter, useful for data I/O in
-   such protocols as POP3 and SMTP.  When encoding, this filter
-   replaces each '\n' not following '\r' by "\r\n" and "byte-stuffs"
-   the input by outputting an additional '.' in front of any '.' appearing
-   at the beginning of a line.  Upon closing the filter in this mode, it
-   outputs additional ".\r\n".
-   
-   When decoding, the reverse is performed: each "\r\n" is replaced by a
-   '\n', and additional '.' are removed from beginning of lines.  A single
-   dot on a line by itself marks end of the stream.
- */
-
-#ifdef HAVE_CONFIG_H
-# include <config.h>
-#endif
-
-#include <stdlib.h>
-#include <string.h>
-#include <mailutils/errno.h>
-#include <mailutils/filter.h>
-#include <mailutils/stream.h>
-
-enum crlfdot_decode_state
-  {
-    crlfdot_decode_init,  /* initial state */
-    crlfdot_decode_char,  /* Any character excepting [\r\n.] */
-    crlfdot_decode_cr,    /* prev. char was \r */
-    crlfdot_decode_crlf,  /* 2 prev. char were \r\n */
-    crlfdot_decode_dot,   /* 3 prev. chars were \r\n. */
-    crlfdot_decode_dotcr, /* 4 prev. chars were \r\n.\r */
-    crlfdot_decode_end    /* final state, a \r\n.\r\n seen. */
-  };
-
-static enum crlfdot_decode_state
-new_decode_state (enum crlfdot_decode_state state, int c)
-{
-  switch (state)
-    {
-    case crlfdot_decode_init:
-      switch (c)
-	{
-	case '\r':
-	  return crlfdot_decode_cr;
-	case '.':
-	  return crlfdot_decode_dot;
-	}
-      break;
-      
-    case crlfdot_decode_char:
-      switch (c)
-	{
-	case '\r':
-	  return crlfdot_decode_cr;
-	}
-      break;
-      
-    case crlfdot_decode_cr:
-      switch (c)
-	{
-	case '\r':
-	  return crlfdot_decode_cr;
-	case '\n':
-	  return crlfdot_decode_crlf;
-	}
-      break;
-      
-    case crlfdot_decode_crlf:
-      switch (c)
-	{
-	case '\r':
-	  return crlfdot_decode_cr;
-	case '.':
-	  return crlfdot_decode_dot;
-	}
-      
-    case crlfdot_decode_dot:
-      switch (c)
-	{
-	case '\r':
-	  return crlfdot_decode_dotcr;
-	}
-      break;
-
-    case crlfdot_decode_dotcr:
-      switch (c)
-	{
-	case '\n':
-	  return crlfdot_decode_end;
-	}
-
-    case crlfdot_decode_end:
-      break;
-    }
-  return crlfdot_decode_char;
-}
-
-/* Move min(isize,osize) bytes from iptr to optr, replacing each \r\n
-   with \n. */
-static enum mu_filter_result
-_crlfdot_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 = crlfdot_decode_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 != crlfdot_decode_end && i < isize && j < osize; i++)
-    {
-      unsigned char c = *iptr++;
-
-      if (c == '\r')
-	{
-	  if (i + 1 == isize)
-	    break;
-	  *pstate = new_decode_state (*pstate, c);
-	  if (*iptr == '\n')
-	    continue;
-	}
-      else if (c == '.' &&
-	       (*pstate == crlfdot_decode_init ||
-		*pstate == crlfdot_decode_crlf))
-	{
-	  /* Make sure we have two more characters in the buffer */
-	  if (i + 2 == isize)
-	    break;
-	  *pstate = new_decode_state (*pstate, c);
-	  if (*iptr != '\r')
-	    continue;
-	}
-      else
-	*pstate = new_decode_state (*pstate, c);
-      optr[j++] = c;
-    }
-  
-  if (*pstate == crlfdot_decode_end)
-    {
-      j -= 2; /* remove the trailing .\n */
-      iobuf->eof = 1;
-    }
-  iobuf->isize = i;
-  iobuf->osize = j;
-  return mu_filter_ok;
-}
-
-enum crlfdot_encode_state
-  {
-    crlfdot_encode_init,  /* initial state */
-    crlfdot_encode_char,  /* Any character excepting [\r\n] */
-    crlfdot_encode_cr,    /* prev. char was \r */
-    crlfdot_encode_lf,    /* prev. char was \n */
-  };    
-
-static enum crlfdot_encode_state
-new_encode_state (enum crlfdot_encode_state state, int c)
-{
-  switch (c)
-    {
-    case '\r':
-      return crlfdot_encode_cr;
-      
-    case '\n':
-      return crlfdot_encode_lf;
-    }
-  return crlfdot_encode_char;
-}
-
-/* Move min(isize,osize) bytes from iptr to optr, replacing each \n
-   with \r\n.  Any input \r\n sequences remain untouched. */
-static enum mu_filter_result
-_crlfdot_encoder (void *xd,
-		  enum mu_filter_command cmd,
-		  struct mu_filter_io *iobuf)
-{
-  enum mu_filter_result result;
-  size_t i, j;
-  const unsigned char *iptr;
-  size_t isize;
-  char *optr;
-  size_t osize;
-  int *state = xd;
-  
-  switch (cmd)
-    {
-    case mu_filter_init:
-      *state = crlfdot_encode_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; i < isize && j < osize; i++, iptr++)
-    {
-      unsigned char c = *iptr;
-      int curstate = *state;
-
-      if (c == '\n')
-	{
-	  if (curstate == crlfdot_encode_cr)
-	    optr[j++] = c;
- 	  else if (j + 1 == osize)
-	    {
-	      if (i == 0)
-		{
-		  iobuf->osize = 2;
-		  return mu_filter_moreoutput;
-		}
-	      break;
-	    }
-	  else
-	    {
-	      optr[j++] = '\r';
-	      optr[j++] = '\n';
-	    }
-	}
-      else if (c == '.' &&
-	       (curstate == crlfdot_encode_init ||
-		curstate == crlfdot_encode_lf))
-	{
- 	  if (j + 2 > osize)
-	    {
-	      if (i == 0)
-		{
-		  iobuf->osize = 2;
-		  return mu_filter_moreoutput;
-		}
-	      break;
-	    }
-	  optr[j++] = '.';
-	  optr[j++] = '.';
-	}
-      else
-	optr[j++] = c;
-
-      *state = new_encode_state (curstate, c);
-    }
-
-  result = mu_filter_ok;
-  if (cmd == mu_filter_lastbuf)
-    {
-      switch (*state)
-	{
-	case crlfdot_encode_lf:
-	  if (j + 3 > osize)
-	    result = mu_filter_again;
-	  break;
-	      
-	default:
-	  if (j + 5 > osize)
-	    result = mu_filter_again;
-	  else
-	    {
-	      optr[j++] = '\r';
-	      optr[j++] = '\n';
-	    }
-	}
-
-      if (result == mu_filter_ok)
-	{
-	  optr[j++] = '.';
-	  optr[j++] = '\r';
-	  optr[j++] = '\n';
-	}
-    }
-
-  iobuf->isize = i;
-  iobuf->osize = j;
-  return result;
-}
-
-static int
-alloc_state (void **pret, int mode MU_ARG_UNUSED, void *data MU_ARG_UNUSED)
-{
-  *pret = malloc (sizeof (int));
-  if (!*pret)
-    return ENOMEM;
-  return 0;
-}
-
-static struct _mu_filter_record _crlfdot_filter = {
-  "CRLFDOT",
-  0,
-  alloc_state,
-  _crlfdot_encoder,
-  _crlfdot_decoder
-};
-
-mu_filter_record_t mu_crlfdot_filter = &_crlfdot_filter;
diff --git a/libmailutils/daemon.c b/libmailutils/daemon.c
deleted file mode 100644
index b7fca61..0000000
--- a/libmailutils/daemon.c
+++ /dev/null
@@ -1,189 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 2004, 2005, 2007, 2008, 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 <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <string.h>
-
-#include <mailutils/daemon.h>
-#include <mailutils/errno.h>
-#include <mailutils/error.h>
-#include <mailutils/nls.h>
-
-static char *pidfile;
-static pid_t current_pid;
-
-/* Return 0 if DIR is writable for EUID/EGID.
-   Otherwise, return error code. */
-static int
-ewraccess (const char *dir)
-{
-  struct stat st;
-  if (stat (dir, &st))
-    return errno;
-  if ((st.st_mode & S_IWOTH)
-      || (st.st_gid == getegid () && (st.st_mode & S_IWGRP))
-      || (st.st_uid == geteuid () && (st.st_mode & S_IWUSR)))
-    return 0;
-  else
-    return EACCES;
-}
-
-/* Return 0 if DIR is writable. If necessary and possible, raise to
-   EUID 0, in that case return prior EUID in the memory location pointed to
-   by PUID. */
-static int
-access_dir (const char *dir, uid_t *puid)
-{
-  int ec = ewraccess (dir);
-  if (ec)
-    {
-      if (ec == EACCES && access (dir, W_OK) == 0)
-	{
-	  uid_t uid = geteuid ();
-	  /* See if we can become root */
-	  if (uid && getuid () == 0 && seteuid (0) == 0)
-	    {
-	      *puid = uid;
-	      return 0;
-	    }
-	}
-    }
-  return ec;
-}
-
-int
-mu_daemon_create_pidfile (const char *filename)
-{
-  char *p;
-  int fd;
-  uid_t uid = 0;
-  int rc;
-  
-  if (filename[0] != '/')
-    return EINVAL;
-
-  if (pidfile)
-    free (pidfile);
-  pidfile = strdup (filename);
-  if (!pidfile)
-    return ENOMEM;
-
-  /* Determine the hosting directory name */
-  p = strrchr (pidfile, '/');
-  if (pidfile == p)
-    {
-      free (pidfile);
-      pidfile = NULL;
-      /* Sorry, pidfiles in root dir are not allowed */
-      return EINVAL;
-    }
-  /* Check if we have write access to the directory */
-  *p = 0;
-  rc = access_dir (pidfile, &uid);
-  if (rc)
-    {
-      /* Nope, clean up and return */
-      free (pidfile);
-      pidfile = NULL;
-      return rc;
-    }
-
-  /* Restore directory separator */
-  *p = '/';
-    
-  unlink (pidfile);
-  current_pid = getpid ();
-  
-  if ((fd = open (pidfile, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0644)) != -1)
-    {
-      FILE *fp = fdopen (fd, "w");
-      if (!fp)
-	{
-	  rc = errno;
-	  free (pidfile);
-	  close (fd);
-	}
-      else
-	{
-	  fprintf (fp, "%lu", (unsigned long) current_pid);
-	  fclose (fp);
-	  atexit (mu_daemon_remove_pidfile);
-	}
-    }
-  else
-    {
-      rc = errno;
-      free (pidfile);
-      pidfile = NULL;
-    }
-
-  /* Restore previous EUID value. */
-  if (uid)
-    seteuid (uid);
-  
-  return rc;
-}
-
-void
-mu_daemon_remove_pidfile (void)
-{
-  if (getpid () == current_pid)
-    {
-      int rc;
-      uid_t uid = 0;
-
-      /* Determine the hosting directory name */
-      char *p = strrchr (pidfile, '/');
-      if (pidfile == p)
-	{
-	  /* Should not happen */
-	  abort ();
-	}
-      /* Check if we have write access to the directory */
-      *p = 0;
-      rc = access_dir (pidfile, &uid);
-      *p = '/';
-      if (rc == 0)
-	{
-	  if (unlink (pidfile) && errno != ENOENT)
-	    rc = errno;
-	  else
-	    rc = 0;
-	}
-      
-      if (rc)
-	mu_error (_("cannot remove pidfile %s: %s"),
-		  pidfile, mu_strerror (rc));
-
-      free (pidfile);
-      pidfile = NULL;
-    }
-}
-
-
-
diff --git a/libmailutils/date.c b/libmailutils/date.c
deleted file mode 100644
index d667e34..0000000
--- a/libmailutils/date.c
+++ /dev/null
@@ -1,245 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 1999, 2000, 2001, 2002, 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 <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <mailutils/mutil.h>
-#include <mailutils/cstr.h>
-
-#define SECS_PER_DAY 86400
-#define ADJUSTMENT -719162L
-
-static time_t
-jan1st (int year)
-{
-  year--;               /* Do not consider the current year */
-  return  year*365L
-               + year/4L    /* Years divisible by 4 are leap years */
-               + year/400L  /* Years divisible by 400 are always leap years */
-               - year/100L; /* Years divisible by 100 but not 400 aren't */
-}
-
-static int  month_start[]=
-    {    0, 31, 59,  90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
-     /* Jan Feb Mar  Apr  May  Jun  Jul  Aug  Sep  Oct  Nov  Dec
-         31  28  31   30   31   30   31   31   30   31   30   31
-     */
-
-/* NOTE: ignore GCC warning. The precedence of operators is OK here */
-#define leap_year(y) ((y) % 4 == 0 && (y) % 100 != 0 || (y) % 400 == 0)
-
-static int
-dayofyear (time_t *pday, int year, int month, int day)
-{
-  int  leap, month_days;
-
-  if (year < 0 || month < 0 || month > 11)
-    return -1;
-    
-  leap = leap_year (year);
-  
-  month_days = month_start[month + 1] - month_start[month]
-               + ((month == 2) ? leap : 0);
-
-  if (day < 0 || day > month_days)
-    return -1;  /* Illegal Date */
-
-  if (month <= 2)
-    leap = 0;
-
-  *pday = month_start[month] + day + leap;
-  return 0;
-}
-
-
-/* Convert struct tm into time_t, taking into account timezone offset. */
-/* FIXME: It does not take DST into account */
-time_t
-mu_tm2time (struct tm *tm, mu_timezone *tz)
-{
-  time_t t;
-  
-  if (dayofyear (&t, tm->tm_year, tm->tm_mon, tm->tm_mday - 1))
-    return -1;
-  t = (t + ADJUSTMENT + jan1st (1900 + tm->tm_year)) * SECS_PER_DAY
-            + (tm->tm_hour * 60 + tm->tm_min) * 60 + tm->tm_sec
-      - tz->utc_offset;
-  return t;
-}
-
-/* Convert time 0 at UTC to our localtime, that tells us the offset
-   of our current timezone from UTC. */
-time_t
-mu_utc_offset (void)
-{
-  time_t t = 0;
-  struct tm *tm = gmtime (&t);
-
-  return - mktime (tm);
-}
-
-static const char *months[] =
-{
-  "Jan", "Feb", "Mar", "Apr", "May", "Jun",
-  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", NULL
-};
-
-static const char *wdays[] =
-{
-  "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL
-};
-
-int
-mu_parse_imap_date_time (const char **p, struct tm *tm, mu_timezone *tz)
-{
-  int year, mon, day, hour, min, sec;
-  char zone[6] = "+0000";	/* ( "+" / "-" ) hhmm */
-  char month[5] = "";
-  int hh = 0;
-  int mm = 0;
-  int sign = 1;
-  int scanned = 0, scanned3;
-  int i;
-  int tzoffset;
-
-  day = mon = year = hour = min = sec = 0;
-
-  memset (tm, 0, sizeof (*tm));
-
-  switch (sscanf (*p,
-		  "%2d-%3s-%4d%n %2d:%2d:%2d %5s%n",
-		  &day, month, &year, &scanned3, &hour, &min, &sec, zone,
-		  &scanned))
-    {
-    case 3:
-      scanned = scanned3;
-      break;
-    case 7:
-      break;
-    default:
-      return -1;
-    }
-
-  tm->tm_sec = sec;
-  tm->tm_min = min;
-  tm->tm_hour = hour;
-  tm->tm_mday = day;
-
-  for (i = 0; i < 12; i++)
-    {
-      if (mu_c_strncasecmp (month, months[i], 3) == 0)
-	{
-	  mon = i;
-	  break;
-	}
-    }
-  tm->tm_mon = mon;
-  tm->tm_year = (year > 1900) ? year - 1900 : year;
-  tm->tm_yday = 0;		/* unknown. */
-  tm->tm_wday = 0;		/* unknown. */
-#if HAVE_STRUCT_TM_TM_ISDST
-  tm->tm_isdst = -1;		/* unknown. */
-#endif
-
-  hh = (zone[1] - '0') * 10 + (zone[2] - '0');
-  mm = (zone[3] - '0') * 10 + (zone[4] - '0');
-  sign = (zone[0] == '-') ? -1 : +1;
-  tzoffset = sign * (hh * 60 * 60 + mm * 60);
-
-#if HAVE_STRUCT_TM_TM_GMTOFF
-  tm->tm_gmtoff = tzoffset;
-#endif
-
-  if (tz)
-    {
-      tz->utc_offset = tzoffset;
-      tz->tz_name = NULL;
-    }
-
-  *p += scanned;
-
-  return 0;
-}
-
-/* "ctime" format is: Thu Jul 01 15:58:27 1999, with no trailing \n.  */
-int
-mu_parse_ctime_date_time (const char **p, struct tm *tm, mu_timezone * tz)
-{
-  int wday = 0;
-  int year = 0;
-  int mon = 0;
-  int day = 0;
-  int hour = 0;
-  int min = 0;
-  int sec = 0;
-  int n = 0;
-  int i;
-  char weekday[5] = "";
-  char month[5] = "";
-
-  if (sscanf (*p, "%3s %3s %2d %2d:%2d:%2d %d%n\n",
-	weekday, month, &day, &hour, &min, &sec, &year, &n) != 7)
-    return -1;
-
-  *p += n;
-
-  for (i = 0; i < 7; i++)
-    {
-      if (mu_c_strncasecmp (weekday, wdays[i], 3) == 0)
-	{
-	  wday = i;
-	  break;
-	}
-    }
-
-  for (i = 0; i < 12; i++)
-    {
-      if (mu_c_strncasecmp (month, months[i], 3) == 0)
-	{
-	  mon = i;
-	  break;
-	}
-    }
-
-  if (tm)
-    {
-      memset (tm, 0, sizeof (struct tm));
-
-      tm->tm_sec = sec;
-      tm->tm_min = min;
-      tm->tm_hour = hour;
-      tm->tm_mday = day;
-      tm->tm_wday = wday;
-      tm->tm_mon = mon;
-      tm->tm_year = (year > 1900) ? year - 1900 : year;
-#ifdef HAVE_STRUCT_TM_TM_ISDST
-      tm->tm_isdst = -1;	/* unknown. */
-#endif
-    }
-
-  /* ctime has no timezone information, set tz to UTC if they ask. */
-  if (tz)
-    memset (tz, 0, sizeof (struct mu_timezone));
-
-  return 0;
-}
diff --git a/libmailutils/dbgstderr.c b/libmailutils/dbgstderr.c
deleted file mode 100644
index 9a2e164..0000000
--- a/libmailutils/dbgstderr.c
+++ /dev/null
@@ -1,36 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 1999, 2000, 2001, 2004, 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 */
-
-#ifdef HAVE_CONFIG_H
-# include <config.h>
-#endif
-
-#include <mailutils/debug.h>
-#include <stdio.h>
-
-int
-mu_debug_stderr_printer (void *unused, mu_log_level_t level, const char *str)
-{
-  fprintf (stderr, "%s: %s",
-	   (level == MU_DEBUG_ERROR) ? "ERROR" : "DEBUG",
-	   str);
-  return 0;
-}
-
-
diff --git a/libmailutils/dbgsyslog.c b/libmailutils/dbgsyslog.c
deleted file mode 100644
index f906cd3..0000000
--- a/libmailutils/dbgsyslog.c
+++ /dev/null
@@ -1,34 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 1999, 2000, 2001, 2004, 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 */
-
-#ifdef HAVE_CONFIG_H
-# include <config.h>
-#endif
-
-#include <mailutils/debug.h>
-#include <syslog.h>
-
-int
-mu_debug_syslog_printer (void *unused, mu_log_level_t level, const char *str)
-{
-  syslog ((level == MU_DEBUG_ERROR) ? LOG_ERR : LOG_DEBUG, "%s", str);
-  return 0;
-}
-
-
diff --git a/libmailutils/debug.c b/libmailutils/debug.c
deleted file mode 100644
index d738cef..0000000
--- a/libmailutils/debug.c
+++ /dev/null
@@ -1,299 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 1999, 2000, 2001, 2004, 2005, 2007, 2008, 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 <errno.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-
-#include <mailutils/errno.h>
-#include <mailutils/nls.h>
-#include <mailutils/sys/debug.h>
-
-mu_debug_printer_fp mu_debug_default_printer = mu_debug_stderr_printer;
-
-int
-mu_debug_create (mu_debug_t *pdebug, void *owner)
-{
-  mu_debug_t debug;
-  if (pdebug == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-  debug = calloc (sizeof (*debug), 1);
-  if (debug == NULL)
-    return ENOMEM;
-  debug->printer = NULL;
-  debug->owner = owner;
-  *pdebug = debug;
-  return 0;
-}
-
-void
-mu_debug_destroy (mu_debug_t *pdebug, void *owner)
-{
-  if (pdebug && *pdebug)
-    {
-      mu_debug_t debug = *pdebug;
-      if (debug->owner == owner)
-	{
-	  if (debug->stream)
-	    {
-	      mu_off_t len = 0;
-	      int rc = mu_stream_size (debug->stream, &len);
-	      if (rc == 0 && len)
-		/* Flush leftover data */
-		mu_debug_printf (debug, 0, "\n");
-
-	      mu_stream_destroy (&debug->stream);
-	    }
-	  if (debug->destroy)
-	    debug->destroy (debug->data);
-	  free (*pdebug);
-	  *pdebug = NULL;
-	}
-    }
-}
-
-void *
-mu_debug_get_owner (mu_debug_t debug)
-{
-  return (debug) ? debug->owner : NULL;
-}
-
-int
-mu_debug_set_level (mu_debug_t debug, mu_log_level_t level)
-{
-  if (debug == NULL)
-    return EINVAL;
-  debug->level = level;
-  return 0;
-}
-
-int
-mu_debug_get_level (mu_debug_t debug, mu_log_level_t *plevel)
-{
-  if (debug == NULL)
-    return EINVAL;
-  if (plevel)
-    *plevel = debug->level;
-  return 0;
-}
-
-int
-mu_debug_set_print (mu_debug_t debug, mu_debug_printer_fp printer, void *owner)
-{
-  if (debug == NULL)
-    return EINVAL;
-  if (debug->owner != owner)
-    return EACCES;
-  debug->printer = printer;
-  return 0;
-}
-
-int
-mu_debug_set_data (mu_debug_t debug, void *data, void (*destroy) (void*),
-		   void *owner)
-{
-  if (!debug)
-    return EINVAL;
-  if (debug->owner != owner)
-    return EACCES;
-  debug->data = data;
-  debug->destroy = destroy;
-  return 0;
-}
-
-static void
-debug_format_prefix (mu_debug_t debug)
-{
-  int need_space = 0;
-  if (debug->locus.file)
-    {
-      mu_stream_printf (debug->stream, "%s:%d:",
-			debug->locus.file, debug->locus.line);
-      need_space = 1;
-    }
-  
-  if (debug->function)
-    {
-      mu_stream_printf (debug->stream, "%s:", debug->function);
-      need_space = 1;
-    }
-  
-  if (need_space)
-    mu_stream_write (debug->stream, " ", 1, NULL);
-}
-
-int
-mu_debug_vprintf (mu_debug_t debug, mu_log_level_t level,
-		  const char *format, va_list ap)
-{
-  mu_debug_printer_fp printer;
-  
-  if (debug == NULL || format == NULL)
-    return EINVAL;
-
-  printer = debug->printer ? debug->printer : mu_debug_default_printer;
-  if (printer)
-    {
-      mu_off_t len;
-      mu_transport_t tbuf[2];
-      char *ptr, *start, *p;
-      size_t nseg;
-      
-      if (debug->stream == NULL)
-	{
-	  int rc = mu_memory_stream_create (&debug->stream, 0);
-	  if (rc)
-	    {
-	      fprintf (stderr,
-		       _("cannot create memory stream for debugging "
-			 "output: %s\n"),
-		       mu_strerror (rc));
-	      vfprintf (stderr, format, ap);
-	      return rc;
-	    }
-	}
-
-      if (mu_stream_size (debug->stream, &len) == 0 && len == 0)
-	debug_format_prefix (debug);
-      
-      mu_stream_vprintf (debug->stream, format, ap);
-
-      mu_stream_ioctl (debug->stream, MU_IOCTL_GET_TRANSPORT, tbuf);
-      start = (char*) tbuf[0];
-      mu_stream_size (debug->stream, &len);
-      ptr = start;
-      nseg = 0;
-      for (p = ptr = start; p < start + len; p++)
-	{
-	  if (*p == '\n')
-	    {
-	      int c = *++p;
-	      *p = 0;
-	      printer (debug->data, level, ptr);
-	      *p = c;
-	      ptr = p;
-	      nseg++;
-	    }
-	}
-
-      if (nseg)
-	{
-	  if (start[len - 1] != '\n')
-	    {
-	      size_t s = len - (ptr - start);
-	      memmove (start, ptr, s);
-	    }
-	  else
-	    len = 0;
-
-	  mu_stream_truncate (debug->stream, len);
-	  mu_stream_seek (debug->stream, len, SEEK_SET, NULL);
-	}
-    }
-  else
-    vfprintf (stderr, format, ap);
-
-  return 0;
-}
-
-int
-mu_debug_printf (mu_debug_t debug, mu_log_level_t level,
-		 const char *format, ...)
-{
-  va_list ap;
-
-  va_start (ap, format);
-  mu_debug_vprintf (debug, level, format, ap);
-  va_end (ap);
-  return 0;
-}
-  
-
-int
-mu_debug_print (mu_debug_t debug, mu_log_level_t level,
-		const char *format, ...)
-{
-  va_list ap;
-  va_start(ap, format);
-  mu_debug_printv (debug, level, format, ap);
-  va_end (ap);
-  return 0;
-}
-
-int
-mu_debug_printv (mu_debug_t debug, mu_log_level_t level, const char *format,
-		 va_list ap)
-{
-  if (debug == NULL || format == NULL)
-    return EINVAL;
-  if (debug->level & MU_DEBUG_LEVEL_MASK (level))
-    return 0;
-  return mu_debug_vprintf (debug, level, format, ap);
-}
-
-int
-mu_debug_check_level (mu_debug_t debug, mu_log_level_t level)
-{
-  if (!debug)
-    return 0;
-  return debug->level & MU_DEBUG_LEVEL_MASK (level);
-}
-
-int
-mu_debug_set_locus (mu_debug_t debug, const char *file, int line)
-{
-  if (!debug)
-    return EINVAL;
-  debug->locus.file = file;
-  debug->locus.line = line;
-  return 0;
-}
-
-int
-mu_debug_get_locus (mu_debug_t debug, struct mu_debug_locus *ploc)
-{
-  if (!debug)
-    return EINVAL;
-  *ploc = debug->locus;
-  return 0;
-}
-
-int
-mu_debug_set_function (mu_debug_t debug, const char *function)
-{
-  if (!debug)
-    return EINVAL;
-  debug->function = function;
-  return 0;
-}
-
-int
-mu_debug_get_function (mu_debug_t debug, const char **pfunction)
-{
-  if (!debug)
-    return EINVAL;
-  *pfunction = debug->function;
-  return 0;
-}
-
diff --git a/libmailutils/diag.c b/libmailutils/diag.c
deleted file mode 100644
index 7497c82..0000000
--- a/libmailutils/diag.c
+++ /dev/null
@@ -1,179 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 1999, 2000, 2001, 2005, 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 <stdlib.h>
-#include <stdio.h>
-#include <syslog.h>
-#include <string.h>
-#include <mailutils/diag.h>
-#include <mailutils/nls.h>
-#include <mailutils/errno.h>
-
-const char *mu_program_name;
-mu_debug_t mu_diag_debug;
-
-void
-mu_set_program_name (const char *name)
-{
-  const char *progname;
-
-  if (!name)
-    progname = name;
-  else
-    {
-      progname = strrchr (name, '/');
-      if (progname)
-	progname++;
-      else
-	progname = name;
-      
-      if (strlen (progname) > 3 && memcmp (progname, "lt-", 3) == 0)
-	progname += 3;
-    }
-  
-  mu_program_name = progname;
-}
-
-void
-mu_diag_init ()
-{
-  if (!mu_diag_debug)
-    {
-      int rc = mu_debug_create (&mu_diag_debug, NULL);
-      if (rc)
-	{
-	  fprintf (stderr,
-		   _("cannot initialize debug object for diagnostics: %s\n"),
-		   mu_strerror (rc));
-	  /* That's a fatal error */
-	  abort ();
-	}
-      mu_debug_set_print (mu_diag_debug, mu_diag_stderr_printer, NULL);
-    }
-}
-
-void
-mu_diag_get_debug (mu_debug_t *pdebug)
-{
-  mu_diag_init ();
-  *pdebug = mu_diag_debug;
-}
-
-void
-mu_diag_set_debug (mu_debug_t debug)
-{
-  if (mu_diag_debug)
-    mu_debug_destroy (&mu_diag_debug, NULL);
-  mu_diag_debug = debug;
-}
-
-void
-mu_diag_vprintf (mu_log_level_t level, const char *fmt, va_list ap)
-{
-  mu_diag_init ();  
-  mu_debug_vprintf (mu_diag_debug, level, fmt, ap);
-}
-
-void
-mu_diag_printf (mu_log_level_t level, const char *fmt, ...)
-{
-  va_list ap;
-  va_start (ap, fmt);
-  mu_diag_vprintf (level, fmt, ap);
-  va_end (ap);
-}
-
-void
-mu_diag_voutput (mu_log_level_t level, const char *fmt, va_list ap)
-{
-  mu_diag_init ();  
-  mu_debug_vprintf (mu_diag_debug, level, fmt, ap);
-  mu_debug_printf (mu_diag_debug, level, "\n");
-}
-
-void
-mu_diag_output (mu_log_level_t level, const char *fmt, ...)
-{
-  va_list ap;
-  va_start (ap, fmt);
-  mu_diag_voutput (level, fmt, ap);
-  va_end (ap);
-}
-
-const char *
-mu_diag_level_to_string (mu_log_level_t level)
-{
-  switch (level)
-    {
-    case MU_DIAG_EMERG:
-      return _("emergency");
-      
-    case MU_DIAG_ALERT:
-      return _("alert");
-	
-    case MU_DIAG_CRIT:
-      return _("critical");
-      
-    case MU_DIAG_ERROR:
-      return _("error");
-      
-    case MU_DIAG_WARNING:
-      return _("warning");
-      
-    case MU_DIAG_NOTICE:
-      return _("notice");
-      
-    case MU_DIAG_INFO:
-      return _("info");
-      
-    case MU_DIAG_DEBUG:
-      return _("debug");
-    }
-  return _("unknown");
-}
-
-int
-mu_diag_stderr_printer (void *data, mu_log_level_t level, const char *buf)
-{
-  if (mu_program_name)
-    fprintf (stderr, "%s: ", mu_program_name);
-  if (level != MU_DIAG_ERROR)
-    fprintf (stderr, "%s: ", mu_diag_level_to_string (level));
-  fputs (buf, stderr);
-  return 0;
-}
-
-void
-mu_diag_funcall (mu_log_level_t level, const char *func,
-		 const char *arg, int err)
-{
-  if (err)
-    /* TRANSLATORS: First %s stands for function name, second for its
-       arguments, third one for the actual error message. */
-    mu_diag_output (level, _("%s(%s) failed: %s"), func, arg ? arg : "",
-		    mu_strerror (err));
-  else
-    /* TRANSLATORS: First %s stands for function name, second for its
-       arguments. */
-    mu_diag_output (level, _("%s(%s) failed"), func, arg ? arg : "");
-}
diff --git a/libmailutils/diag/.gitignore b/libmailutils/diag/.gitignore
new file mode 100644
index 0000000..f200331
--- /dev/null
+++ b/libmailutils/diag/.gitignore
@@ -0,0 +1 @@
+muerrno.c
diff --git a/libmailutils/diag/Makefile.am b/libmailutils/diag/Makefile.am
new file mode 100644
index 0000000..f2dae72
--- /dev/null
+++ b/libmailutils/diag/Makefile.am
@@ -0,0 +1,42 @@
+# GNU Mailutils -- a suite of utilities for electronic mail
+# Copyright (C) 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, see
+# <http://www.gnu.org/licenses/>.
+
+noinst_LTLIBRARIES = libdiag.la
+
+libdiag_la_SOURCES = \
+ debug.c\
+ diag.c\
+ gdebug.c\
+ muerror.c\
+ muerrno.c\
+ syslog.c\
+ dbgstderr.c\
+ dbgsyslog.c
+
+INCLUDES = @MU_LIB_COMMON_INCLUDES@ -I/libmailutils
+
+EXTRA_DIST = \
+ errors\
+ muerrno.cin
+
+BUILT_SOURCES = muerrno.c
+
+muerrno.c: errors muerrno.cin
+	$(AWK) -f $(mu_aux_dir)/generr.awk $^ > $@
+
+
+
diff --git a/libmailutils/diag/dbgstderr.c b/libmailutils/diag/dbgstderr.c
new file mode 100644
index 0000000..caaf248
--- /dev/null
+++ b/libmailutils/diag/dbgstderr.c
@@ -0,0 +1,35 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 2004, 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, see
+   <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <mailutils/debug.h>
+#include <stdio.h>
+
+int
+mu_debug_stderr_printer (void *unused, mu_log_level_t level, const char *str)
+{
+  fprintf (stderr, "%s: %s",
+	   (level == MU_DEBUG_ERROR) ? "ERROR" : "DEBUG",
+	   str);
+  return 0;
+}
+
+
diff --git a/libmailutils/diag/dbgsyslog.c b/libmailutils/diag/dbgsyslog.c
new file mode 100644
index 0000000..af6f2fe
--- /dev/null
+++ b/libmailutils/diag/dbgsyslog.c
@@ -0,0 +1,33 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 2004, 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, see 
+   <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <mailutils/debug.h>
+#include <syslog.h>
+
+int
+mu_debug_syslog_printer (void *unused, mu_log_level_t level, const char *str)
+{
+  syslog ((level == MU_DEBUG_ERROR) ? LOG_ERR : LOG_DEBUG, "%s", str);
+  return 0;
+}
+
+
diff --git a/libmailutils/diag/debug.c b/libmailutils/diag/debug.c
new file mode 100644
index 0000000..749a04b
--- /dev/null
+++ b/libmailutils/diag/debug.c
@@ -0,0 +1,298 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 2004, 2005, 2007, 2008, 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, see 
+   <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <errno.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+#include <mailutils/errno.h>
+#include <mailutils/nls.h>
+#include <mailutils/sys/debug.h>
+
+mu_debug_printer_fp mu_debug_default_printer = mu_debug_stderr_printer;
+
+int
+mu_debug_create (mu_debug_t *pdebug, void *owner)
+{
+  mu_debug_t debug;
+  if (pdebug == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+  debug = calloc (sizeof (*debug), 1);
+  if (debug == NULL)
+    return ENOMEM;
+  debug->printer = NULL;
+  debug->owner = owner;
+  *pdebug = debug;
+  return 0;
+}
+
+void
+mu_debug_destroy (mu_debug_t *pdebug, void *owner)
+{
+  if (pdebug && *pdebug)
+    {
+      mu_debug_t debug = *pdebug;
+      if (debug->owner == owner)
+	{
+	  if (debug->stream)
+	    {
+	      mu_off_t len = 0;
+	      int rc = mu_stream_size (debug->stream, &len);
+	      if (rc == 0 && len)
+		/* Flush leftover data */
+		mu_debug_printf (debug, 0, "\n");
+
+	      mu_stream_destroy (&debug->stream);
+	    }
+	  if (debug->destroy)
+	    debug->destroy (debug->data);
+	  free (*pdebug);
+	  *pdebug = NULL;
+	}
+    }
+}
+
+void *
+mu_debug_get_owner (mu_debug_t debug)
+{
+  return (debug) ? debug->owner : NULL;
+}
+
+int
+mu_debug_set_level (mu_debug_t debug, mu_log_level_t level)
+{
+  if (debug == NULL)
+    return EINVAL;
+  debug->level = level;
+  return 0;
+}
+
+int
+mu_debug_get_level (mu_debug_t debug, mu_log_level_t *plevel)
+{
+  if (debug == NULL)
+    return EINVAL;
+  if (plevel)
+    *plevel = debug->level;
+  return 0;
+}
+
+int
+mu_debug_set_print (mu_debug_t debug, mu_debug_printer_fp printer, void *owner)
+{
+  if (debug == NULL)
+    return EINVAL;
+  if (debug->owner != owner)
+    return EACCES;
+  debug->printer = printer;
+  return 0;
+}
+
+int
+mu_debug_set_data (mu_debug_t debug, void *data, void (*destroy) (void*),
+		   void *owner)
+{
+  if (!debug)
+    return EINVAL;
+  if (debug->owner != owner)
+    return EACCES;
+  debug->data = data;
+  debug->destroy = destroy;
+  return 0;
+}
+
+static void
+debug_format_prefix (mu_debug_t debug)
+{
+  int need_space = 0;
+  if (debug->locus.file)
+    {
+      mu_stream_printf (debug->stream, "%s:%d:",
+			debug->locus.file, debug->locus.line);
+      need_space = 1;
+    }
+  
+  if (debug->function)
+    {
+      mu_stream_printf (debug->stream, "%s:", debug->function);
+      need_space = 1;
+    }
+  
+  if (need_space)
+    mu_stream_write (debug->stream, " ", 1, NULL);
+}
+
+int
+mu_debug_vprintf (mu_debug_t debug, mu_log_level_t level,
+		  const char *format, va_list ap)
+{
+  mu_debug_printer_fp printer;
+  
+  if (debug == NULL || format == NULL)
+    return EINVAL;
+
+  printer = debug->printer ? debug->printer : mu_debug_default_printer;
+  if (printer)
+    {
+      mu_off_t len;
+      mu_transport_t tbuf[2];
+      char *ptr, *start, *p;
+      size_t nseg;
+      
+      if (debug->stream == NULL)
+	{
+	  int rc = mu_memory_stream_create (&debug->stream, 0);
+	  if (rc)
+	    {
+	      fprintf (stderr,
+		       _("cannot create memory stream for debugging "
+			 "output: %s\n"),
+		       mu_strerror (rc));
+	      vfprintf (stderr, format, ap);
+	      return rc;
+	    }
+	}
+
+      if (mu_stream_size (debug->stream, &len) == 0 && len == 0)
+	debug_format_prefix (debug);
+      
+      mu_stream_vprintf (debug->stream, format, ap);
+
+      mu_stream_ioctl (debug->stream, MU_IOCTL_GET_TRANSPORT, tbuf);
+      start = (char*) tbuf[0];
+      mu_stream_size (debug->stream, &len);
+      ptr = start;
+      nseg = 0;
+      for (p = ptr = start; p < start + len; p++)
+	{
+	  if (*p == '\n')
+	    {
+	      int c = *++p;
+	      *p = 0;
+	      printer (debug->data, level, ptr);
+	      *p = c;
+	      ptr = p;
+	      nseg++;
+	    }
+	}
+
+      if (nseg)
+	{
+	  if (start[len - 1] != '\n')
+	    {
+	      size_t s = len - (ptr - start);
+	      memmove (start, ptr, s);
+	    }
+	  else
+	    len = 0;
+
+	  mu_stream_truncate (debug->stream, len);
+	  mu_stream_seek (debug->stream, len, SEEK_SET, NULL);
+	}
+    }
+  else
+    vfprintf (stderr, format, ap);
+
+  return 0;
+}
+
+int
+mu_debug_printf (mu_debug_t debug, mu_log_level_t level,
+		 const char *format, ...)
+{
+  va_list ap;
+
+  va_start (ap, format);
+  mu_debug_vprintf (debug, level, format, ap);
+  va_end (ap);
+  return 0;
+}
+  
+
+int
+mu_debug_print (mu_debug_t debug, mu_log_level_t level,
+		const char *format, ...)
+{
+  va_list ap;
+  va_start(ap, format);
+  mu_debug_printv (debug, level, format, ap);
+  va_end (ap);
+  return 0;
+}
+
+int
+mu_debug_printv (mu_debug_t debug, mu_log_level_t level, const char *format,
+		 va_list ap)
+{
+  if (debug == NULL || format == NULL)
+    return EINVAL;
+  if (debug->level & MU_DEBUG_LEVEL_MASK (level))
+    return 0;
+  return mu_debug_vprintf (debug, level, format, ap);
+}
+
+int
+mu_debug_check_level (mu_debug_t debug, mu_log_level_t level)
+{
+  if (!debug)
+    return 0;
+  return debug->level & MU_DEBUG_LEVEL_MASK (level);
+}
+
+int
+mu_debug_set_locus (mu_debug_t debug, const char *file, int line)
+{
+  if (!debug)
+    return EINVAL;
+  debug->locus.file = file;
+  debug->locus.line = line;
+  return 0;
+}
+
+int
+mu_debug_get_locus (mu_debug_t debug, struct mu_debug_locus *ploc)
+{
+  if (!debug)
+    return EINVAL;
+  *ploc = debug->locus;
+  return 0;
+}
+
+int
+mu_debug_set_function (mu_debug_t debug, const char *function)
+{
+  if (!debug)
+    return EINVAL;
+  debug->function = function;
+  return 0;
+}
+
+int
+mu_debug_get_function (mu_debug_t debug, const char **pfunction)
+{
+  if (!debug)
+    return EINVAL;
+  *pfunction = debug->function;
+  return 0;
+}
+
diff --git a/libmailutils/diag/diag.c b/libmailutils/diag/diag.c
new file mode 100644
index 0000000..55372af
--- /dev/null
+++ b/libmailutils/diag/diag.c
@@ -0,0 +1,178 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 2005, 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, see 
+   <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <syslog.h>
+#include <string.h>
+#include <mailutils/diag.h>
+#include <mailutils/nls.h>
+#include <mailutils/errno.h>
+
+const char *mu_program_name;
+mu_debug_t mu_diag_debug;
+
+void
+mu_set_program_name (const char *name)
+{
+  const char *progname;
+
+  if (!name)
+    progname = name;
+  else
+    {
+      progname = strrchr (name, '/');
+      if (progname)
+	progname++;
+      else
+	progname = name;
+      
+      if (strlen (progname) > 3 && memcmp (progname, "lt-", 3) == 0)
+	progname += 3;
+    }
+  
+  mu_program_name = progname;
+}
+
+void
+mu_diag_init ()
+{
+  if (!mu_diag_debug)
+    {
+      int rc = mu_debug_create (&mu_diag_debug, NULL);
+      if (rc)
+	{
+	  fprintf (stderr,
+		   _("cannot initialize debug object for diagnostics: %s\n"),
+		   mu_strerror (rc));
+	  /* That's a fatal error */
+	  abort ();
+	}
+      mu_debug_set_print (mu_diag_debug, mu_diag_stderr_printer, NULL);
+    }
+}
+
+void
+mu_diag_get_debug (mu_debug_t *pdebug)
+{
+  mu_diag_init ();
+  *pdebug = mu_diag_debug;
+}
+
+void
+mu_diag_set_debug (mu_debug_t debug)
+{
+  if (mu_diag_debug)
+    mu_debug_destroy (&mu_diag_debug, NULL);
+  mu_diag_debug = debug;
+}
+
+void
+mu_diag_vprintf (mu_log_level_t level, const char *fmt, va_list ap)
+{
+  mu_diag_init ();  
+  mu_debug_vprintf (mu_diag_debug, level, fmt, ap);
+}
+
+void
+mu_diag_printf (mu_log_level_t level, const char *fmt, ...)
+{
+  va_list ap;
+  va_start (ap, fmt);
+  mu_diag_vprintf (level, fmt, ap);
+  va_end (ap);
+}
+
+void
+mu_diag_voutput (mu_log_level_t level, const char *fmt, va_list ap)
+{
+  mu_diag_init ();  
+  mu_debug_vprintf (mu_diag_debug, level, fmt, ap);
+  mu_debug_printf (mu_diag_debug, level, "\n");
+}
+
+void
+mu_diag_output (mu_log_level_t level, const char *fmt, ...)
+{
+  va_list ap;
+  va_start (ap, fmt);
+  mu_diag_voutput (level, fmt, ap);
+  va_end (ap);
+}
+
+const char *
+mu_diag_level_to_string (mu_log_level_t level)
+{
+  switch (level)
+    {
+    case MU_DIAG_EMERG:
+      return _("emergency");
+      
+    case MU_DIAG_ALERT:
+      return _("alert");
+	
+    case MU_DIAG_CRIT:
+      return _("critical");
+      
+    case MU_DIAG_ERROR:
+      return _("error");
+      
+    case MU_DIAG_WARNING:
+      return _("warning");
+      
+    case MU_DIAG_NOTICE:
+      return _("notice");
+      
+    case MU_DIAG_INFO:
+      return _("info");
+      
+    case MU_DIAG_DEBUG:
+      return _("debug");
+    }
+  return _("unknown");
+}
+
+int
+mu_diag_stderr_printer (void *data, mu_log_level_t level, const char *buf)
+{
+  if (mu_program_name)
+    fprintf (stderr, "%s: ", mu_program_name);
+  if (level != MU_DIAG_ERROR)
+    fprintf (stderr, "%s: ", mu_diag_level_to_string (level));
+  fputs (buf, stderr);
+  return 0;
+}
+
+void
+mu_diag_funcall (mu_log_level_t level, const char *func,
+		 const char *arg, int err)
+{
+  if (err)
+    /* TRANSLATORS: First %s stands for function name, second for its
+       arguments, third one for the actual error message. */
+    mu_diag_output (level, _("%s(%s) failed: %s"), func, arg ? arg : "",
+		    mu_strerror (err));
+  else
+    /* TRANSLATORS: First %s stands for function name, second for its
+       arguments. */
+    mu_diag_output (level, _("%s(%s) failed"), func, arg ? arg : "");
+}
diff --git a/libmailutils/diag/errors b/libmailutils/diag/errors
new file mode 100644
index 0000000..5f82f7b
--- /dev/null
+++ b/libmailutils/diag/errors
@@ -0,0 +1,94 @@
+# Error messages for GNU Mailutils
+# Copyright (C) 2005, 2006, 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, see <http://www.gnu.org/licenses/>. 
+
+MU_ERR_FAILURE              _("Operation failed")
+MU_ERR_CANCELED             _("Operation canceled")
+
+MU_ERR_NO_HANDLER           _("No registered handler")
+MU_ERR_EMPTY_VFN            _("Empty virtual function")
+
+MU_ERR_OUT_NULL             _("Pointer to output null")
+MU_ERR_OUT_PTR_NULL         _("Pointer to output pointer null")
+
+MU_ERR_MBX_NULL             _("Mailbox null")
+MU_ERR_MBX_REMOVED          _("Mailbox removed")
+
+MU_ERR_NOT_OPEN             _("Resource not open")
+MU_ERR_OPEN                 _("Resource is already open")
+
+MU_ERR_BAD_822_FORMAT       _("Format of RFC822 object is bad")
+MU_ERR_EMPTY_ADDRESS        _("Address contains no addr specs")
+
+MU_ERR_LOCKER_NULL          _("Locker null")
+MU_ERR_LOCK_CONFLICT        _("Conflict with previous locker")
+MU_ERR_LOCK_BAD_LOCK        _("Lock file check failed")
+MU_ERR_LOCK_BAD_FILE        _("File check failed")
+MU_ERR_LOCK_NOT_HELD        _("Lock not held on file")
+MU_ERR_LOCK_EXT_FAIL        _("Failed to execute external locker")
+MU_ERR_LOCK_EXT_ERR         _("External locker failed")
+MU_ERR_LOCK_EXT_KILLED      _("External locker killed")
+
+MU_ERR_NO_SUCH_USER         _("No such user name")
+
+MU_ERR_GETHOSTBYNAME        _("DNS name resolution failed")
+MU_ERR_BAD_RESUMPTION       _("State busy must resume operation")
+MU_ERR_MAILER_BAD_FROM      _("Not a valid mailer from address")
+MU_ERR_MAILER_BAD_TO        _("Not a valid mailer to address")
+MU_ERR_MAILER_NO_RCPT_TO    _("No receipt addresses found")
+MU_ERR_MAILER_BAD_URL       _("Malformed or unsupported mailer URL")
+MU_ERR_SMTP_RCPT_FAILED     _("SMTP rcpt to command failed")
+MU_ERR_TCP_NO_HOST          _("Tcp connections need a host")
+MU_ERR_TCP_NO_PORT          _("Tcp connections need a postive port")
+
+MU_ERR_BAD_2047_INPUT       _("Input string is not RFC 2047 encoded")
+MU_ERR_BAD_2047_ENCODING    _("Not a valid RFC 2047 encoding")
+
+MU_ERR_NOUSERNAME           _("User name is not supplied")
+MU_ERR_NOPASSWORD           _("User password is not supplied")
+MU_ERR_BADREPLY             _("Invalid reply from the remote host")
+
+MU_ERR_SEQ                  _("Bad command sequence")
+MU_ERR_REPLY                _("Erroneous reply received")
+
+MU_ERR_UNSAFE_PERMS         _("Unsafe file permissions. Set 0600")
+MU_ERR_BAD_AUTH_SCHEME      _("Unsupported authentication scheme")
+MU_ERR_AUTH_FAILURE         _("Authentication failed")
+
+MU_ERR_PROCESS_NOEXEC       _("Cannot execute")
+MU_ERR_PROCESS_EXITED       _("Process exited with a non-zero status")
+MU_ERR_PROCESS_SIGNALED     _("Process exited on signal")
+MU_ERR_PROCESS_UNKNOWN_FAILURE _("Unknown failure while executing subprocess")
+MU_ERR_CONN_CLOSED          _("Connection closed by remote host")
+MU_ERR_PARSE                _("Parse error")
+MU_ERR_NOENT                _("Requested item not found")
+MU_ERR_EXISTS               _("Item already exists")
+MU_ERR_BUFSPACE             _("Not enough buffer space")
+
+MU_ERR_SQL                  _("SQL error")
+MU_ERR_DB_ALREADY_CONNECTED _("Already connected to the database")
+MU_ERR_DB_NOT_CONNECTED     _("Not connected to the database")
+MU_ERR_RESULT_NOT_RELEASED  _("Result of the previous query is not released")
+MU_ERR_NO_QUERY             _("No query was yet executed")
+MU_ERR_BAD_COLUMN           _("Bad column address")
+MU_ERR_NO_RESULT            _("No result from the previous query available")
+MU_ERR_NO_INTERFACE         _("No such interface")
+
+MU_ERR_BADOP                _("Inappropriate operation for this mode")
+MU_ERR_BAD_FILENAME         _("Badly formed file or directory name")
+MU_ERR_READ                 _("Read error")
+
+MU_ERR_NO_TRANSPORT         _("Transport stream not set")
+MU_ERR_AUTH_NO_CRED         _("No credentials supplied")
diff --git a/libmailutils/diag/gdebug.c b/libmailutils/diag/gdebug.c
new file mode 100644
index 0000000..7e6f63c
--- /dev/null
+++ b/libmailutils/diag/gdebug.c
@@ -0,0 +1,262 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 2004, 2005, 2007, 2008, 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, see
+   <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <errno.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <mailutils/cctype.h>
+#include <mailutils/assoc.h>
+#include <mailutils/error.h>
+#include <mailutils/errno.h>
+#include <mailutils/argcv.h>
+#include <mailutils/debug.h>
+#include <mailutils/cfg.h>
+#include <mailutils/nls.h>
+
+int mu_debug_line_info = 0;
+
+struct debug_level
+{
+  unsigned level;
+};
+
+static mu_assoc_t debug_table;
+
+mu_log_level_t
+mu_global_debug_level (const char *object_name)
+{
+  struct debug_level *p = mu_assoc_ref (debug_table, object_name);
+  if (p)
+    return p->level;
+  return 0;
+}
+
+int
+mu_global_debug_set_level (const char *object_name, mu_log_level_t level)
+{
+  int rc;
+  struct debug_level *dbg;
+  
+  if (!debug_table)
+    {
+      rc = mu_assoc_create (&debug_table, sizeof(struct debug_level), 0);
+      if (rc)
+	return rc;
+    }
+
+  rc = mu_assoc_ref_install (debug_table, object_name, (void**) &dbg);
+  if (rc == 0 || rc == MU_ERR_EXISTS)
+    dbg->level = level;
+  return rc;
+}
+
+int
+mu_global_debug_clear_level (const char *object_name)
+{
+  int rc = 0;
+  
+  if (!object_name)
+    mu_assoc_clear (debug_table);
+  else
+    rc = mu_assoc_remove (debug_table, object_name);
+  return rc;
+}
+
+int
+decode_debug_level (const char *p, int *lev)
+{
+  if (strcmp (p, "error") == 0)
+    *lev = MU_DEBUG_ERROR;
+  else if (strncmp (p, "trace", 5) == 0 && mu_isdigit (p[5]) && p[6] == 0)
+    *lev = MU_DEBUG_TRACE0 + atoi (p + 5);
+  else if (strcmp (p, "proto") == 0)
+    *lev = MU_DEBUG_PROT;
+  else
+    return 1;
+  return 0;
+}
+
+int
+mu_debug_level_from_string (const char *string, mu_log_level_t *plev,
+			    mu_debug_t debug)
+{
+  char *q;
+  unsigned level = MU_DEBUG_INHERIT;
+  
+  if (mu_isdigit (*string))
+    {
+      level = strtoul (string, &q, 0);
+      if (*q)
+	{
+	  mu_cfg_format_error (debug, MU_DEBUG_ERROR,
+			       _("invalid debugging specification `%s': "
+				 "expected levels or number after `=', "
+				 "but found `%s'"),
+			       string, string);
+	  return MU_ERR_FAILURE;
+	}
+    }
+  else
+    {
+      char *p = strdup (string);
+      size_t len = strlen (p);
+      if (len > 0 && p[len-1] == '\n')
+	p[len-1] = 0;
+      for (q = strtok (p, ","); q; q = strtok (NULL, ","))
+	{
+	  int flag;
+	  int revert = 0;
+	  int upto = 0;
+	  
+	  if (*q == '!')
+	    {
+	      q++;
+	      revert = 1;
+	    }
+	  if (*q == '<')
+	    {
+	      q++;
+	      upto = 1;
+	    }
+	  
+	  if (decode_debug_level (q, &flag))
+	    mu_cfg_format_error (debug, MU_DEBUG_ERROR,
+				 _("invalid debugging level `%s'"),
+				 q);
+	  else if (revert)
+	    {
+	      if (upto)
+		level &= ~MU_DEBUG_LEVEL_UPTO (flag);
+	      else
+		level &= ~MU_DEBUG_LEVEL_MASK (flag);
+	    }
+	  else
+	    {
+	      if (upto)
+		level |= MU_DEBUG_LEVEL_UPTO (flag);
+	      else
+		level |= MU_DEBUG_LEVEL_MASK (flag);
+	    }
+	}
+      free (p);
+    }
+  *plev = level;
+  return 0;
+}
+
+int
+mu_global_debug_from_string (const char *string, const char *errpfx)
+{
+  int rc;
+  int argc;
+  char **argv;
+  int i;
+  
+  rc = mu_argcv_get (string, ";", NULL, &argc, &argv);
+  if (rc)
+    return rc;
+
+  for (i = 0; i < argc; i++)
+    {
+      char *p;
+      mu_log_level_t level = MU_DEBUG_INHERIT;
+      char *object_name = argv[i];
+      
+      for (p = object_name; *p && *p != '='; p++)
+	;
+
+      if (*p == '=')
+	{
+	  /* FIXME: Use mu_debug_level_from_string */
+	  char *q;
+	  
+	  *p++ = 0;
+	  if (mu_isdigit (*p))
+	    {
+	      level = strtoul (p, &q, 0);
+	      if (*q)
+		{
+		  mu_error ("%s: invalid debugging specification `%s': "
+			    "expected levels or number after `=', "
+			    "but found `%s'",
+			    errpfx, argv[i], p);
+		  break;
+		}
+	    }
+	  else
+	    {
+	      char *q;
+	      for (q = strtok (p, ","); q; q = strtok (NULL, ","))
+		{
+		  int flag;
+		  int revert = 0;
+		  int upto = 0;
+		  
+		  if (*q == '!')
+		    {
+		      q++;
+		      revert = 1;
+		    }
+		  if (*q == '<')
+		    {
+		      q++;
+		      upto = 1;
+		    }
+		  
+		  if (decode_debug_level (q, &flag))
+		    mu_error ("%s: invalid debugging level `%s'",
+			      errpfx, q);
+		  else if (revert)
+		    {
+		      if (upto)
+			level &= ~MU_DEBUG_LEVEL_UPTO (flag);
+		      else
+			level &= ~MU_DEBUG_LEVEL_MASK (flag);
+		    }
+		  else
+		    {
+		      if (upto)
+			level |= MU_DEBUG_LEVEL_UPTO (flag);
+		      else
+			level |= MU_DEBUG_LEVEL_MASK (flag);
+		    }
+		}
+	    }   
+	}	  
+      else
+	level |= MU_DEBUG_LEVEL_UPTO (MU_DEBUG_PROT);
+      
+      if (p[-1] == ':')
+	{
+	  p[-1] = 0;
+	  level &= ~MU_DEBUG_INHERIT;
+	}
+      
+      mu_global_debug_set_level (object_name, level);
+    }
+  
+  mu_argcv_free (argc, argv);
+  return 0;
+}
+
+
diff --git a/libmailutils/diag/muerrno.cin b/libmailutils/diag/muerrno.cin
new file mode 100644
index 0000000..e448159
--- /dev/null
+++ b/libmailutils/diag/muerrno.cin
@@ -0,0 +1,65 @@
+/* -*- c -*- $AUTOWARN
+   GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 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, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <stdio.h>
+#include <errno.h>
+#include <string.h>
+
+#include <mailutils/errno.h>
+#include <mailutils/nls.h>
+
+#ifndef EOK
+# define EOK 0
+#endif
+
+const char*
+mu_errname (int e)
+{
+  static char buf[128];
+  
+  switch (e)
+    {
+    case EOK:
+      return "EOK";
+
+      $MESSAGE_CODES
+    }
+
+  snprintf (buf, sizeof buf, _("Error %d"), e);
+  return buf;
+}
+
+const char *
+mu_strerror (int e)
+{
+  switch (e)
+    {
+    case EOK:
+      return _("Success");
+
+    $MESSAGE_STRINGS
+    }
+
+  return strerror (e);
+}
+
diff --git a/libmailutils/diag/muerror.c b/libmailutils/diag/muerror.c
new file mode 100644
index 0000000..3b0b38e
--- /dev/null
+++ b/libmailutils/diag/muerror.c
@@ -0,0 +1,116 @@
+/* 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, see
+   <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <syslog.h>
+#include <string.h>
+#include <mailutils/error.h>
+
+
+/* Historic shortcuts for mu_diag_ functions */
+
+int
+mu_verror (const char *fmt, va_list ap)
+{
+  mu_diag_voutput (MU_DIAG_ERROR, fmt, ap);
+  return 0;
+}
+
+int
+mu_error (const char *fmt, ...)
+{
+  va_list ap;
+  va_start (ap, fmt);
+  mu_verror (fmt, ap);
+  va_end (ap);
+  return 0;
+}
+
+
+/* Compatibility layer */
+int
+mu_default_error_printer (const char *fmt, va_list ap)
+{
+  if (mu_program_name)
+    fprintf (stderr, "%s: ", mu_program_name);
+  vfprintf (stderr, fmt, ap);
+  fputc ('\n', stderr);
+  return 0;
+}
+
+int
+mu_syslog_error_printer (const char *fmt, va_list ap)
+{
+#ifdef HAVE_VSYSLOG
+  vsyslog (LOG_CRIT, fmt, ap);
+#else
+  char buf[128];
+  vsnprintf (buf, sizeof buf, fmt, ap);
+  syslog (LOG_CRIT, "%s", buf);
+#endif
+  return 0;
+}
+
+static void
+compat_error_printer0 (mu_error_pfn_t pfn, const char *fmt, ...)
+{
+  va_list ap;
+  va_start (ap, fmt);
+  pfn (fmt, ap);
+  va_end (ap);
+}
+
+static int
+compat_error_printer (void *data, mu_log_level_t level, const char *buf)
+{
+  if (!data)
+    mu_diag_stderr_printer (NULL, level, buf);
+  else
+    {
+      int len = strlen (buf);
+      if (len > 0 && buf[len-1] == '\n')
+	len--;
+      compat_error_printer0 (data, "%-.*s", len, buf);
+    }
+  return 0;
+}
+
+void
+mu_error_set_print (mu_error_pfn_t pfn)
+{
+  mu_debug_t debug;
+  mu_diag_get_debug (&debug);
+  mu_debug_set_print (debug, compat_error_printer, NULL);
+  mu_debug_set_data (debug, pfn, NULL, NULL);
+#if 0
+ {
+   static int warned;
+   if (!warned)
+     {
+       warned = 1;
+       mu_diag_output ("this program uses mu_error_set_print, which is deprecated");
+     }
+#endif
+}
+
+
diff --git a/libmailutils/diag/syslog.c b/libmailutils/diag/syslog.c
new file mode 100644
index 0000000..da7d233
--- /dev/null
+++ b/libmailutils/diag/syslog.c
@@ -0,0 +1,145 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 2007, 2008, 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 of the License, or (at your
+   option) any later version.
+
+   GNU Mailutils 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 GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+#include <syslog.h>
+#include <string.h>
+#include <mailutils/diag.h>
+#include <mailutils/kwd.h>
+#include <mailutils/syslog.h>
+#include <mailutils/cstr.h>
+
+#ifndef LOG_AUTHPRIV
+# define LOG_AUTHPRIV
+#endif
+
+static mu_kwd_t kw_facility[] = {
+  { "USER",    LOG_USER },   
+  { "DAEMON",  LOG_DAEMON },
+  { "AUTH",    LOG_AUTH },
+  { "AUTHPRIV",LOG_AUTHPRIV },
+  { "MAIL",    LOG_MAIL },
+  { "CRON",    LOG_CRON },
+  { "LOCAL0",  LOG_LOCAL0 },
+  { "LOCAL1",  LOG_LOCAL1 },
+  { "LOCAL2",  LOG_LOCAL2 },
+  { "LOCAL3",  LOG_LOCAL3 },
+  { "LOCAL4",  LOG_LOCAL4 },
+  { "LOCAL5",  LOG_LOCAL5 },
+  { "LOCAL6",  LOG_LOCAL6 },
+  { "LOCAL7",  LOG_LOCAL7 },
+  { NULL }
+};
+
+static int
+syslog_to_n (mu_kwd_t *kw, const char *str, int *pint)
+{
+  if (mu_c_strncasecmp (str, "LOG_", 4) == 0)
+    str += 4;
+  return mu_kwd_xlat_name_ci (kw, str, pint);
+}
+
+int
+mu_string_to_syslog_facility (const char *str, int *pfacility)
+{
+  return syslog_to_n (kw_facility, str, pfacility);
+}
+
+const char *
+mu_syslog_facility_to_string (int n)
+{
+  const char *res = NULL;
+  mu_kwd_xlat_tok (kw_facility, n, &res);
+  return res;
+}
+
+static mu_kwd_t kw_prio[] = {
+  { "EMERG", LOG_EMERG },
+  { "ALERT", LOG_ALERT },
+  { "CRIT", LOG_CRIT },
+  { "ERR", LOG_ERR },
+  { "WARNING", LOG_WARNING },
+  { "NOTICE", LOG_NOTICE },
+  { "INFO", LOG_INFO },
+  { "DEBUG", LOG_DEBUG },
+  { NULL }
+};
+
+int
+mu_string_to_syslog_priority (const char *str, int *pprio)
+{
+  return syslog_to_n (kw_prio, str, pprio);
+}
+
+const char *
+mu_syslog_priority_to_string (int n)
+{
+  const char *res = NULL;
+  mu_kwd_xlat_tok (kw_prio, n, &res);
+  return res;
+}
+
+int
+mu_diag_level_to_syslog (mu_log_level_t level)
+{
+  switch (level)
+    {
+    case MU_DIAG_EMERG:
+      return LOG_EMERG;
+      
+    case MU_DIAG_ALERT:
+      return LOG_ALERT;
+	
+    case MU_DIAG_CRIT:
+      return LOG_CRIT;
+      
+    case MU_DIAG_ERROR:
+      return LOG_ERR;
+      
+    case MU_DIAG_WARNING:
+      return LOG_WARNING;
+      
+    case MU_DIAG_NOTICE:
+      return LOG_NOTICE;
+      
+    case MU_DIAG_INFO:
+      return LOG_INFO;
+      
+    case MU_DIAG_DEBUG:
+      return LOG_DEBUG;
+    }
+  return LOG_EMERG;
+}
+
+int
+mu_diag_syslog_printer (void *data, mu_log_level_t level, const char *buf)
+{
+  int len = strlen (buf);
+  if (len > 0 && buf[len-1] == '\n')
+    {
+      len--;
+      if (len > 0 && buf[len-1] == '\r')
+	len--;
+    }
+  syslog (mu_diag_level_to_syslog (level), "%-.*s", len, buf);
+  return 0;
+}
+
+
+int mu_log_facility = LOG_FACILITY;
+char *mu_log_tag = NULL;
diff --git a/libmailutils/dot.c b/libmailutils/dot.c
deleted file mode 100644
index a0d43fa..0000000
--- a/libmailutils/dot.c
+++ /dev/null
@@ -1,268 +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 */
-
-/* This file implements a DOT filter, useful for data I/O in
-   such protocols as POP3 and SMTP.  When encoding, this filter
-   "byte-stuffs" the input by outputting an additional '.' in front
-   of any '.' appearing at the beginning of a line.  Upon closing the
-   filter in this mode, it outputs additional ".\n".
-   
-   When decoding, the reverse is performed: any '.' appearing at the
-   beginning of a line is removed.  A single dot on a line by itself
-   marks end of the stream.
-
-   MU also provides a hairy version of this filter, called CRLFDOT.
-   In addition to byte-stuffing, CRLFDOT also performs CRLF/LF translation.
- */
-
-#ifdef HAVE_CONFIG_H
-# include <config.h>
-#endif
-
-#include <stdlib.h>
-#include <string.h>
-#include <mailutils/errno.h>
-#include <mailutils/filter.h>
-#include <mailutils/stream.h>
-
-enum dot_decode_state
-  {
-    dot_decode_init,  /* initial state */
-    dot_decode_char,  /* Any character excepting [\r\n.] */
-    dot_decode_lf,    /* prev. char was \n */
-    dot_decode_dot,   /* 2 prev. chars were \n. */
-    dot_decode_end   /* final state, a \n.\n seen. */
-  };
-
-static enum dot_decode_state
-new_decode_state (enum dot_decode_state state, int c)
-{
-  switch (state)
-    {
-    case dot_decode_init:
-      switch (c)
-	{
-	case '.':
-	  return dot_decode_dot;
-	}
-      break;
-      
-    case dot_decode_char:
-      switch (c)
-	{
-	case '\n':
-	  return dot_decode_lf;
-	}
-      break;
-      
-    case dot_decode_lf:
-      switch (c)
-	{
-	case '.':
-	  return dot_decode_dot;
-	}
-      
-    case dot_decode_dot:
-      switch (c)
-	{
-	case '\n':
-	  return dot_decode_end;
-	}
-      break;
-
-    case dot_decode_end:
-      return dot_decode_end;
-    }
-  return dot_decode_char;
-}
-
-/* Move min(isize,osize) bytes from iptr to optr, replacing each \r\n
-   with \n. */
-static enum mu_filter_result
-_dot_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 = dot_decode_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 != dot_decode_end && i < isize && j < osize; i++)
-    {
-      unsigned char c = *iptr++;
-      int curstate = *pstate;
-      
-      *pstate = new_decode_state (curstate, c);
-      if (!(c == '.' && (curstate == dot_decode_init ||
-			 curstate == dot_decode_lf)))
-	optr[j++] = c;
-    }
-  
-  if (*pstate == dot_decode_end)
-    iobuf->eof = 1;
-
-  iobuf->isize = i;
-  iobuf->osize = j;
-  return mu_filter_ok;
-}
-
-enum dot_encode_state
-  {
-    dot_encode_init,  /* initial state */
-    dot_encode_char,  /* Any character excepting \n */
-    dot_encode_lf,    /* prev. char was \n */
-  };    
-
-static enum dot_encode_state
-new_encode_state (int c)
-{
-  switch (c)
-    {
-    case '\n':
-      return dot_encode_lf;
-    }
-  return dot_encode_char;
-}
-
-/* Move min(isize,osize) bytes from iptr to optr, replacing each \n
-   with \r\n.  Any input \r\n sequences remain untouched. */
-static enum mu_filter_result
-_dot_encoder (void *xd,
-	      enum mu_filter_command cmd,
-	      struct mu_filter_io *iobuf)
-{
-  enum mu_filter_result result;
-  size_t i, j;
-  const unsigned char *iptr;
-  size_t isize;
-  char *optr;
-  size_t osize;
-  int *state = xd;
-  
-  switch (cmd)
-    {
-    case mu_filter_init:
-      *state = dot_encode_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; i < isize && j < osize; i++, iptr++)
-    {
-      unsigned char c = *iptr;
-      int curstate = *state;
-
-      if (c == '.' && (curstate == dot_encode_init ||
-		       curstate == dot_encode_lf))
-	{
- 	  if (j + 2 > osize)
-	    {
-	      if (i == 0)
-		{
-		  iobuf->osize = 2;
-		  return mu_filter_moreoutput;
-		}
-	      break;
-	    }
-	  optr[j++] = '.';
-	  optr[j++] = '.';
-	}
-      else
-	optr[j++] = c;
-
-      *state = new_encode_state (c);
-    }
-
-  result = mu_filter_ok;
-  if (cmd == mu_filter_lastbuf)
-    {
-      switch (*state)
-	{
-	case dot_encode_lf:
-	  if (j + 2 > osize)
-	    result = mu_filter_again;
-	  break;
-	      
-	default:
-	  if (j + 3 > osize)
-	    result = mu_filter_again;
-	  else
-	    optr[j++] = '\n';
-	}
-
-      if (result == mu_filter_ok)
-	{
-	  optr[j++] = '.';
-	  optr[j++] = '\n';
-	}
-    }
-
-  iobuf->isize = i;
-  iobuf->osize = j;
-  return result;
-}
-
-static int
-alloc_state (void **pret, int mode MU_ARG_UNUSED, void *data MU_ARG_UNUSED)
-{
-  *pret = malloc (sizeof (int));
-  if (!*pret)
-    return ENOMEM;
-  return 0;
-}
-
-static struct _mu_filter_record _dot_filter = {
-  "DOT",
-  0,
-  alloc_state,
-  _dot_encoder,
-  _dot_decoder
-};
-
-mu_filter_record_t mu_dot_filter = &_dot_filter;
diff --git a/libmailutils/envelope.c b/libmailutils/envelope.c
deleted file mode 100644
index 1e5aa4a..0000000
--- a/libmailutils/envelope.c
+++ /dev/null
@@ -1,181 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 1999, 2000, 2004, 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 */
-
-#ifdef HAVE_CONFIG_H
-#  include <config.h>
-#endif
-#include <errno.h>
-#include <stdlib.h>
-#include <string.h>
-#include <mailutils/errno.h>
-#include <mailutils/mutil.h>
-#include <mailutils/sys/envelope.h>
-
-int
-mu_envelope_create (mu_envelope_t *penvelope, void *owner)
-{
-  mu_envelope_t envelope;
-  if (penvelope == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-  envelope = calloc (1, sizeof (*envelope));
-  if (envelope == NULL)
-    return ENOMEM;
-  envelope->owner = owner;
-  *penvelope = envelope;
-  return 0;
-}
-
-void
-mu_envelope_destroy (mu_envelope_t *penvelope, void *owner)
-{
-  if (penvelope && *penvelope)
-    {
-      mu_envelope_t envelope = *penvelope;
-      if (envelope->owner == owner)
-	{
-	  if (envelope->_destroy)
-	    envelope->_destroy (envelope);
-	  free (envelope->date);
-	  free (envelope->sender);
-	  free (envelope);
-	}
-      *penvelope = NULL;
-    }
-}
-
-void *
-mu_envelope_get_owner (mu_envelope_t envelope)
-{
-  return (envelope) ? envelope->owner : NULL;
-}
-
-int
-mu_envelope_set_sender (mu_envelope_t envelope,
-			int (*_sender) (mu_envelope_t, char *, size_t,
-					size_t*),
-			void *owner)
-{
-  if (envelope == NULL)
-    return EINVAL;
-  if (envelope->owner != owner)
-    return EACCES;
-  envelope->_get_sender = _sender;
-  return 0;
-}
-
-int
-mu_envelope_set_date (mu_envelope_t envelope,
-		      int (*_date) (mu_envelope_t, char *, size_t , size_t *),
-		      void *owner)
-{
-  if (envelope == NULL)
-    return EINVAL;
-  if (envelope->owner != owner)
-    return EACCES;
-  envelope->_get_date = _date;
-  return 0;
-}
-
-
-/* General accessors: */
-#define AC2(a,b) a ## b
-#define AC4(a,b,c,d) a ## b ## c ## d
-#define ACCESSOR(action,field) AC4(mu_envelope_,action,_,field)
-
-#define DECL_SGET(field)						  \
-int									  \
-ACCESSOR(sget,field) (mu_envelope_t env, char const **sptr)               \
-{									  \
-  if (env == NULL)							  \
-    return EINVAL;							  \
-  if (!env->field)							  \
-    {                                                                     \
-      if (env->AC2(_get_,field))                                          \
-	{								  \
-	  size_t n;							  \
-	  char *buf;							  \
-          int status;                                                     \
-	  								  \
-	  status = env->AC2(_get_,field) (env, NULL, 0, &n);	          \
-	  if (status)							  \
-	    return status;						  \
-	  								  \
-	  buf = malloc (n + 1);						  \
-	  if (!buf)							  \
-	    return ENOMEM;						  \
-	  								  \
-	  status = env->AC2(_get_,field) (env, buf, n + 1, NULL);	  \
-	  if (status)		                     		          \
-            return status;						  \
-	  								  \
-          env->field = buf;                                               \
-	}								  \
-      else								  \
-        return MU_ERR_NOENT; 	                                          \
-    }									  \
-  *sptr = env->field;							  \
-  return 0;								  \
-}
-
-#define DECL_GET(field)							  \
-int									  \
-ACCESSOR(get,field) (mu_envelope_t env, char *buf, size_t len, size_t *n) \
-{									  \
-  size_t i;								  \
-  const char *str;							  \
-  int status = ACCESSOR(sget, field) (env, &str);			  \
-  									  \
-  if (status)								  \
-    return status;							  \
-									  \
-  i = mu_cpystr (buf, str, len);					  \
-  if (n)								  \
-    *n = i;								  \
-  return 0;								  \
-}
-
-#define DECL_AGET(field)						  \
-int									  \
-ACCESSOR(aget, field) (mu_envelope_t env, char **buf)	                  \
-{									  \
-  const char *str;							  \
-  int status = ACCESSOR(sget, field) (env, &str);			  \
-									  \
-  if (status)								  \
-    return status;							  \
-									  \
-  if (str)								  \
-    {									  \
-      *buf = strdup (str);						  \
-      if (!*buf)							  \
-	status = ENOMEM;						  \
-    }									  \
-  else									  \
-    *buf = NULL;							  \
-  return status;							  \
-}
-
-#define DECL_ACCESSORS(field)			                          \
-DECL_SGET(field)				                          \
-DECL_GET(field)					                          \
-DECL_AGET(field)
-
-DECL_ACCESSORS(sender)
-DECL_ACCESSORS(date)     
-
diff --git a/libmailutils/errors b/libmailutils/errors
deleted file mode 100644
index 8593c79..0000000
--- a/libmailutils/errors
+++ /dev/null
@@ -1,96 +0,0 @@
-# Error messages for GNU Mailutils
-# Copyright (C) 2005, 2006, 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  
-
-MU_ERR_FAILURE              _("Operation failed")
-MU_ERR_CANCELED             _("Operation canceled")
-
-MU_ERR_NO_HANDLER           _("No registered handler")
-MU_ERR_EMPTY_VFN            _("Empty virtual function")
-
-MU_ERR_OUT_NULL             _("Pointer to output null")
-MU_ERR_OUT_PTR_NULL         _("Pointer to output pointer null")
-
-MU_ERR_MBX_NULL             _("Mailbox null")
-MU_ERR_MBX_REMOVED          _("Mailbox removed")
-
-MU_ERR_NOT_OPEN             _("Resource not open")
-MU_ERR_OPEN                 _("Resource is already open")
-
-MU_ERR_BAD_822_FORMAT       _("Format of RFC822 object is bad")
-MU_ERR_EMPTY_ADDRESS        _("Address contains no addr specs")
-
-MU_ERR_LOCKER_NULL          _("Locker null")
-MU_ERR_LOCK_CONFLICT        _("Conflict with previous locker")
-MU_ERR_LOCK_BAD_LOCK        _("Lock file check failed")
-MU_ERR_LOCK_BAD_FILE        _("File check failed")
-MU_ERR_LOCK_NOT_HELD        _("Lock not held on file")
-MU_ERR_LOCK_EXT_FAIL        _("Failed to execute external locker")
-MU_ERR_LOCK_EXT_ERR         _("External locker failed")
-MU_ERR_LOCK_EXT_KILLED      _("External locker killed")
-
-MU_ERR_NO_SUCH_USER         _("No such user name")
-
-MU_ERR_GETHOSTBYNAME        _("DNS name resolution failed")
-MU_ERR_BAD_RESUMPTION       _("State busy must resume operation")
-MU_ERR_MAILER_BAD_FROM      _("Not a valid mailer from address")
-MU_ERR_MAILER_BAD_TO        _("Not a valid mailer to address")
-MU_ERR_MAILER_NO_RCPT_TO    _("No receipt addresses found")
-MU_ERR_MAILER_BAD_URL       _("Malformed or unsupported mailer URL")
-MU_ERR_SMTP_RCPT_FAILED     _("SMTP rcpt to command failed")
-MU_ERR_TCP_NO_HOST          _("Tcp connections need a host")
-MU_ERR_TCP_NO_PORT          _("Tcp connections need a postive port")
-
-MU_ERR_BAD_2047_INPUT       _("Input string is not RFC 2047 encoded")
-MU_ERR_BAD_2047_ENCODING    _("Not a valid RFC 2047 encoding")
-
-MU_ERR_NOUSERNAME           _("User name is not supplied")
-MU_ERR_NOPASSWORD           _("User password is not supplied")
-MU_ERR_BADREPLY             _("Invalid reply from the remote host")
-
-MU_ERR_SEQ                  _("Bad command sequence")
-MU_ERR_REPLY                _("Erroneous reply received")
-
-MU_ERR_UNSAFE_PERMS         _("Unsafe file permissions. Set 0600")
-MU_ERR_BAD_AUTH_SCHEME      _("Unsupported authentication scheme")
-MU_ERR_AUTH_FAILURE         _("Authentication failed")
-
-MU_ERR_PROCESS_NOEXEC       _("Cannot execute")
-MU_ERR_PROCESS_EXITED       _("Process exited with a non-zero status")
-MU_ERR_PROCESS_SIGNALED     _("Process exited on signal")
-MU_ERR_PROCESS_UNKNOWN_FAILURE _("Unknown failure while executing subprocess")
-MU_ERR_CONN_CLOSED          _("Connection closed by remote host")
-MU_ERR_PARSE                _("Parse error")
-MU_ERR_NOENT                _("Requested item not found")
-MU_ERR_EXISTS               _("Item already exists")
-MU_ERR_BUFSPACE             _("Not enough buffer space")
-
-MU_ERR_SQL                  _("SQL error")
-MU_ERR_DB_ALREADY_CONNECTED _("Already connected to the database")
-MU_ERR_DB_NOT_CONNECTED     _("Not connected to the database")
-MU_ERR_RESULT_NOT_RELEASED  _("Result of the previous query is not released")
-MU_ERR_NO_QUERY             _("No query was yet executed")
-MU_ERR_BAD_COLUMN           _("Bad column address")
-MU_ERR_NO_RESULT            _("No result from the previous query available")
-MU_ERR_NO_INTERFACE         _("No such interface")
-
-MU_ERR_BADOP                _("Inappropriate operation for this mode")
-MU_ERR_BAD_FILENAME         _("Badly formed file or directory name")
-MU_ERR_READ                 _("Read error")
-
-MU_ERR_NO_TRANSPORT         _("Transport stream not set")
-MU_ERR_AUTH_NO_CRED         _("No credentials supplied")
diff --git a/libmailutils/fgetpwent.c b/libmailutils/fgetpwent.c
deleted file mode 100644
index 99ec9da..0000000
--- a/libmailutils/fgetpwent.c
+++ /dev/null
@@ -1,163 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 1999, 2000, 2001, 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 <stdio.h>
-#include <pwd.h>
-#include <string.h>
-#include <stdlib.h>
-
-/*
-  Written by Alain Magloire.
-  Simple replacement for fgetpwent(), it is not :
-  - thread safe;
-  - static buffer was not use since it will limit the size
-    of the entry.  But rather memory is allocated and __never__
-    release.  The memory will grow if need be.
-  - no support for shadow
-  - no support for NIS(+)
-*/
-
-static char *buffer;
-static size_t buflen;
-static struct passwd pw;
-
-static char *
-parse_line (char *s, char **p)
-{
-  if (*s)
-    {
-      char *sep = strchr (s, ':');
-      if (sep)
-	{
-	  *sep++ = '\0';
-	  *p = sep;
-	}
-      else
-	*p = s + strlen (s);
-    }
-  else
-    *p = s;
-  return s;
-}
-
-static struct passwd *
-getentry (char *s)
-{
-  char *p;
-  pw.pw_name = parse_line (s, &p);
-  s = p;
-  pw.pw_passwd = parse_line (s, &p);
-  s = p;
-  pw.pw_uid = strtoul (parse_line (s, &p), NULL, 10);
-  s = p;
-  pw.pw_gid = strtoul (parse_line (s, &p), NULL, 10);
-  s = p;
-  pw.pw_gecos = parse_line (s, &p);
-  s = p;
-  pw.pw_dir = parse_line (s, &p);
-  s = p;
-  pw.pw_shell = parse_line (s, &p);
-  return &pw;
-}
-
-struct passwd *
-mu_fgetpwent (FILE *fp)
-{
-  size_t pos = 0;
-  int done = 0;
-  struct passwd *pw = NULL;
-
-  /* Allocate buffer if not yet available.  */
-  /* This buffer will be never free().  */
-  if (buffer == NULL)
-    {
-      buflen = 1024;
-      buffer = malloc (buflen);
-      if (buffer == NULL)
-	return NULL;
-    }
-
-  do
-    {
-      if (fgets (buffer + pos, buflen, fp) != NULL)
-	{
-	  /* Need a full line.  */
-	  if (buffer[strlen (buffer) - 1] == '\n')
-	    {
-	      /* reset marker position.  */
-	      pos = 0;
-	      /* Nuke trailing newline.  */
-	      buffer[strlen (buffer) - 1] = '\0';
-
-	      /* Skip comments.  */
-	      if (buffer[0] != '#')
-		{
-		  done = 1;
-		  pw = getentry (buffer);
-		}
-	    }
-	  else
-	    {
-	      /* Line is too long reallocate the buffer.  */
-	      char *tmp;
-	      pos = strlen (buffer);
-	      buflen *= 2;
-	      tmp = realloc (buffer, buflen);
-	      if (tmp)
-		buffer = tmp;
-	      else
-		done = 1;
-	    }
-	}
-      else
-	done = 1;
-    } while (!done);
-
-  return pw;
-
-}
-
-#ifdef STANDALONE
-int
-main ()
-{
-  FILE *fp = fopen ("/etc/passwd", "r");
-  if (fp)
-    {
-      struct passwd *pwd;
-      while ((pwd = fgetpwent (fp)))
-        {
-          printf ("--------------------------------------\n");
-          printf ("name %s\n", pwd->pw_name);
-          printf ("passwd %s\n", pwd->pw_passwd);
-          printf ("uid %d\n", pwd->pw_uid);
-          printf ("gid %d\n", pwd->pw_gid);
-          printf ("gecos %s\n", pwd->pw_gecos);
-          printf ("dir %s\n", pwd->pw_dir);
-          printf ("shell %s\n", pwd->pw_shell);
-        }
-    }
-  return 0;
-}
-
-#endif
diff --git a/libmailutils/filter.c b/libmailutils/filter.c
deleted file mode 100644
index 0008ba0..0000000
--- a/libmailutils/filter.c
+++ /dev/null
@@ -1,194 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 1999, 2000, 2001, 2004, 2005, 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 */
-
-/* Notes:
-   First draft: Alain Magloire.
-   Complete rewrite: Sergey Poznyakoff.
-*/
-
-#ifdef HAVE_CONFIG_H
-# include <config.h>
-#endif
-
-#include <errno.h>
-#include <string.h>
-#include <stdlib.h>
-
-#ifdef HAVE_STRINGS_H
-# include <strings.h>
-#endif
-
-#include <mailutils/filter.h>
-#include <mailutils/monitor.h>
-#include <mailutils/list.h>
-#include <mailutils/stream.h>
-#include <mailutils/errno.h>
-#include <mailutils/cstr.h>
-
-/* NOTE: We will leak here since the monitor of the filter will never
-   be release.  That's ok we can leave with this, it's only done once.  */
-static mu_list_t filter_list;
-struct mu_monitor filter_monitor = MU_MONITOR_INITIALIZER;
-
-static int
-filter_name_cmp (const void *item, const void *data)
-{
-  struct _mu_filter_record const *rec = item;
-  char const *name = data;
-  return mu_c_strcasecmp (rec->name, name);
-}
-
-int
-mu_filter_get_list (mu_list_t *plist)
-{
-  if (plist == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-  mu_monitor_wrlock (&filter_monitor);
-  if (filter_list == NULL)
-    {
-      int status = mu_list_create (&filter_list);
-      if (status != 0)
-	return status;
-      mu_list_set_comparator (filter_list, filter_name_cmp);
-      /* Default filters.  */
-      mu_list_append (filter_list, mu_base64_filter);
-      mu_list_append (filter_list, mu_qp_filter);
-      mu_list_append (filter_list, mu_binary_filter);
-      mu_list_append (filter_list, mu_bit8_filter);
-      mu_list_append (filter_list, mu_bit7_filter);
-      mu_list_append (filter_list, mu_rfc822_filter);
-      mu_list_append (filter_list, mu_crlf_filter);
-      mu_list_append (filter_list, mu_crlfdot_filter);
-      mu_list_append (filter_list, mu_dot_filter);
-      mu_list_append (filter_list, mu_rfc_2047_Q_filter);
-      mu_list_append (filter_list, mu_rfc_2047_B_filter);
-      mu_list_append (filter_list, mu_from_filter);
-      /* FIXME: add the default encodings?  */
-    }
-  *plist = filter_list;
-  mu_monitor_unlock (&filter_monitor);
-  return 0;
-}
-
-static int
-filter_create_rd (mu_stream_t *pstream, mu_stream_t stream,
-		  size_t max_line_length,
-		  int mode,
-		  mu_filter_xcode_t xcode, void *xdata,
-		  int flags)
-{
-  int status;
-  mu_stream_t fltstream;
-
-  flags &= ~MU_STREAM_AUTOCLOSE;
-  
-  status = mu_filter_stream_create (&fltstream, stream,
-				    mode, xcode, xdata,
-				    flags);
-  if (status == 0)
-    {
-      if (max_line_length)
-	{
-	  status = mu_linelen_filter_create (pstream, fltstream,
-					     max_line_length,
-					     flags);
-	  mu_stream_unref (fltstream);
-	  if (status)
-	    return status;
-	}
-      else
-	*pstream = fltstream;
-
-      if (flags & MU_STREAM_AUTOCLOSE)
-	mu_stream_unref (stream);
-    }
-  return status;
-}
-
-static int
-filter_create_wr (mu_stream_t *pstream, mu_stream_t stream,
-		  size_t max_line_length,
-		  int mode,
-		  mu_filter_xcode_t xcode, void *xdata,
-		  int flags)
-{
-  int status;
-  mu_stream_t fltstream, instream = NULL, tmpstr;
-
-  flags &= ~MU_STREAM_AUTOCLOSE;
-
-  if (max_line_length)
-    {
-      status = mu_linelen_filter_create (&instream, stream,
-					 max_line_length,
-					 flags);
-      if (status)
-	return status;
-      tmpstr = instream;
-    }
-  else
-    tmpstr = stream;
-  
-  status = mu_filter_stream_create (&fltstream, tmpstr,
-				    mode, xcode, xdata,
-				    flags);
-  mu_stream_unref (instream);
-  if (status)
-    return status;
-  *pstream = fltstream;
-  if (flags & MU_STREAM_AUTOCLOSE)
-    mu_stream_unref (stream);
-  return status;
-}
-
-int
-mu_filter_create (mu_stream_t *pstream, mu_stream_t stream, const char *name,
-		  int mode, int flags)
-{
-  int status;
-  mu_filter_record_t frec;
-  mu_list_t list;
-  void *xdata = NULL;
-  
-  if ((flags & MU_STREAM_RDWR) == MU_STREAM_RDWR)
-    return EINVAL;
-  
-  mu_filter_get_list (&list);
-  status = mu_list_locate (list, (void*)name, (void**)&frec);
-  if (status)
-    return status;
-
-  if (frec->newdata)
-    {
-      status = frec->newdata (&xdata, mode, NULL);
-      if (status)
-	return status;
-    }
-
-  status = ((flags & MU_STREAM_WRITE) ? filter_create_wr : filter_create_rd)
-                   (pstream, stream,
-		    frec->max_line_length,
-		    mode,
-		    mode == MU_FILTER_ENCODE ? frec->encoder : frec->decoder,
-		    xdata,
-		    flags);
-  if (status)
-    free (xdata);
-  return status;
-}
diff --git a/libmailutils/filter/Makefile.am b/libmailutils/filter/Makefile.am
new file mode 100644
index 0000000..08082bb
--- /dev/null
+++ b/libmailutils/filter/Makefile.am
@@ -0,0 +1,33 @@
+# GNU Mailutils -- a suite of utilities for electronic mail
+# Copyright (C) 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, see
+# <http://www.gnu.org/licenses/>.
+
+noinst_LTLIBRARIES = libfilter.la
+
+libfilter_la_SOURCES =\
+ base64.c\
+ binflt.c\
+ crlfdot.c\
+ crlfflt.c\
+ dot.c\
+ filter.c\
+ filter_iconv.c\
+ fromflt.c\
+ linelenflt.c\
+ qpflt.c
+
+INCLUDES = @MU_LIB_COMMON_INCLUDES@ -I${top_srcdir}/libmailutils
+
diff --git a/libmailutils/base64.c b/libmailutils/filter/base64.c
similarity index 100%
rename from libmailutils/base64.c
rename to libmailutils/filter/base64.c
diff --git a/libmailutils/binflt.c b/libmailutils/filter/binflt.c
similarity index 100%
rename from libmailutils/binflt.c
rename to libmailutils/filter/binflt.c
diff --git a/libmailutils/filter/crlfdot.c b/libmailutils/filter/crlfdot.c
new file mode 100644
index 0000000..35043a6
--- /dev/null
+++ b/libmailutils/filter/crlfdot.c
@@ -0,0 +1,335 @@
+/* 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, see 
+   <http://www.gnu.org/licenses/>. */
+
+/* This source implements a CRLFDOT filter, useful for data I/O in
+   such protocols as POP3 and SMTP.  When encoding, this filter
+   replaces each '\n' not following '\r' by "\r\n" and "byte-stuffs"
+   the input by outputting an additional '.' in front of any '.' appearing
+   at the beginning of a line.  Upon closing the filter in this mode, it
+   outputs additional ".\r\n".
+   
+   When decoding, the reverse is performed: each "\r\n" is replaced by a
+   '\n', and additional '.' are removed from beginning of lines.  A single
+   dot on a line by itself marks end of the stream.
+ */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <string.h>
+#include <mailutils/errno.h>
+#include <mailutils/filter.h>
+#include <mailutils/stream.h>
+
+enum crlfdot_decode_state
+  {
+    crlfdot_decode_init,  /* initial state */
+    crlfdot_decode_char,  /* Any character excepting [\r\n.] */
+    crlfdot_decode_cr,    /* prev. char was \r */
+    crlfdot_decode_crlf,  /* 2 prev. char were \r\n */
+    crlfdot_decode_dot,   /* 3 prev. chars were \r\n. */
+    crlfdot_decode_dotcr, /* 4 prev. chars were \r\n.\r */
+    crlfdot_decode_end    /* final state, a \r\n.\r\n seen. */
+  };
+
+static enum crlfdot_decode_state
+new_decode_state (enum crlfdot_decode_state state, int c)
+{
+  switch (state)
+    {
+    case crlfdot_decode_init:
+      switch (c)
+	{
+	case '\r':
+	  return crlfdot_decode_cr;
+	case '.':
+	  return crlfdot_decode_dot;
+	}
+      break;
+      
+    case crlfdot_decode_char:
+      switch (c)
+	{
+	case '\r':
+	  return crlfdot_decode_cr;
+	}
+      break;
+      
+    case crlfdot_decode_cr:
+      switch (c)
+	{
+	case '\r':
+	  return crlfdot_decode_cr;
+	case '\n':
+	  return crlfdot_decode_crlf;
+	}
+      break;
+      
+    case crlfdot_decode_crlf:
+      switch (c)
+	{
+	case '\r':
+	  return crlfdot_decode_cr;
+	case '.':
+	  return crlfdot_decode_dot;
+	}
+      
+    case crlfdot_decode_dot:
+      switch (c)
+	{
+	case '\r':
+	  return crlfdot_decode_dotcr;
+	}
+      break;
+
+    case crlfdot_decode_dotcr:
+      switch (c)
+	{
+	case '\n':
+	  return crlfdot_decode_end;
+	}
+
+    case crlfdot_decode_end:
+      break;
+    }
+  return crlfdot_decode_char;
+}
+
+/* Move min(isize,osize) bytes from iptr to optr, replacing each \r\n
+   with \n. */
+static enum mu_filter_result
+_crlfdot_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 = crlfdot_decode_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 != crlfdot_decode_end && i < isize && j < osize; i++)
+    {
+      unsigned char c = *iptr++;
+
+      if (c == '\r')
+	{
+	  if (i + 1 == isize)
+	    break;
+	  *pstate = new_decode_state (*pstate, c);
+	  if (*iptr == '\n')
+	    continue;
+	}
+      else if (c == '.' &&
+	       (*pstate == crlfdot_decode_init ||
+		*pstate == crlfdot_decode_crlf))
+	{
+	  /* Make sure we have two more characters in the buffer */
+	  if (i + 2 == isize)
+	    break;
+	  *pstate = new_decode_state (*pstate, c);
+	  if (*iptr != '\r')
+	    continue;
+	}
+      else
+	*pstate = new_decode_state (*pstate, c);
+      optr[j++] = c;
+    }
+  
+  if (*pstate == crlfdot_decode_end)
+    {
+      j -= 2; /* remove the trailing .\n */
+      iobuf->eof = 1;
+    }
+  iobuf->isize = i;
+  iobuf->osize = j;
+  return mu_filter_ok;
+}
+
+enum crlfdot_encode_state
+  {
+    crlfdot_encode_init,  /* initial state */
+    crlfdot_encode_char,  /* Any character excepting [\r\n] */
+    crlfdot_encode_cr,    /* prev. char was \r */
+    crlfdot_encode_lf,    /* prev. char was \n */
+  };    
+
+static enum crlfdot_encode_state
+new_encode_state (enum crlfdot_encode_state state, int c)
+{
+  switch (c)
+    {
+    case '\r':
+      return crlfdot_encode_cr;
+      
+    case '\n':
+      return crlfdot_encode_lf;
+    }
+  return crlfdot_encode_char;
+}
+
+/* Move min(isize,osize) bytes from iptr to optr, replacing each \n
+   with \r\n.  Any input \r\n sequences remain untouched. */
+static enum mu_filter_result
+_crlfdot_encoder (void *xd,
+		  enum mu_filter_command cmd,
+		  struct mu_filter_io *iobuf)
+{
+  enum mu_filter_result result;
+  size_t i, j;
+  const unsigned char *iptr;
+  size_t isize;
+  char *optr;
+  size_t osize;
+  int *state = xd;
+  
+  switch (cmd)
+    {
+    case mu_filter_init:
+      *state = crlfdot_encode_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; i < isize && j < osize; i++, iptr++)
+    {
+      unsigned char c = *iptr;
+      int curstate = *state;
+
+      if (c == '\n')
+	{
+	  if (curstate == crlfdot_encode_cr)
+	    optr[j++] = c;
+ 	  else if (j + 1 == osize)
+	    {
+	      if (i == 0)
+		{
+		  iobuf->osize = 2;
+		  return mu_filter_moreoutput;
+		}
+	      break;
+	    }
+	  else
+	    {
+	      optr[j++] = '\r';
+	      optr[j++] = '\n';
+	    }
+	}
+      else if (c == '.' &&
+	       (curstate == crlfdot_encode_init ||
+		curstate == crlfdot_encode_lf))
+	{
+ 	  if (j + 2 > osize)
+	    {
+	      if (i == 0)
+		{
+		  iobuf->osize = 2;
+		  return mu_filter_moreoutput;
+		}
+	      break;
+	    }
+	  optr[j++] = '.';
+	  optr[j++] = '.';
+	}
+      else
+	optr[j++] = c;
+
+      *state = new_encode_state (curstate, c);
+    }
+
+  result = mu_filter_ok;
+  if (cmd == mu_filter_lastbuf)
+    {
+      switch (*state)
+	{
+	case crlfdot_encode_lf:
+	  if (j + 3 > osize)
+	    result = mu_filter_again;
+	  break;
+	      
+	default:
+	  if (j + 5 > osize)
+	    result = mu_filter_again;
+	  else
+	    {
+	      optr[j++] = '\r';
+	      optr[j++] = '\n';
+	    }
+	}
+
+      if (result == mu_filter_ok)
+	{
+	  optr[j++] = '.';
+	  optr[j++] = '\r';
+	  optr[j++] = '\n';
+	}
+    }
+
+  iobuf->isize = i;
+  iobuf->osize = j;
+  return result;
+}
+
+static int
+alloc_state (void **pret, int mode MU_ARG_UNUSED, void *data MU_ARG_UNUSED)
+{
+  *pret = malloc (sizeof (int));
+  if (!*pret)
+    return ENOMEM;
+  return 0;
+}
+
+static struct _mu_filter_record _crlfdot_filter = {
+  "CRLFDOT",
+  0,
+  alloc_state,
+  _crlfdot_encoder,
+  _crlfdot_decoder
+};
+
+mu_filter_record_t mu_crlfdot_filter = &_crlfdot_filter;
diff --git a/libmailutils/crlfflt.c b/libmailutils/filter/crlfflt.c
similarity index 100%
rename from libmailutils/crlfflt.c
rename to libmailutils/filter/crlfflt.c
diff --git a/libmailutils/filter/dot.c b/libmailutils/filter/dot.c
new file mode 100644
index 0000000..872f6ba
--- /dev/null
+++ b/libmailutils/filter/dot.c
@@ -0,0 +1,267 @@
+/* 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, see 
+   <http://www.gnu.org/licenses/>. */
+
+/* This file implements a DOT filter, useful for data I/O in
+   such protocols as POP3 and SMTP.  When encoding, this filter
+   "byte-stuffs" the input by outputting an additional '.' in front
+   of any '.' appearing at the beginning of a line.  Upon closing the
+   filter in this mode, it outputs additional ".\n".
+   
+   When decoding, the reverse is performed: any '.' appearing at the
+   beginning of a line is removed.  A single dot on a line by itself
+   marks end of the stream.
+
+   MU also provides a hairy version of this filter, called CRLFDOT.
+   In addition to byte-stuffing, CRLFDOT also performs CRLF/LF translation.
+ */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <string.h>
+#include <mailutils/errno.h>
+#include <mailutils/filter.h>
+#include <mailutils/stream.h>
+
+enum dot_decode_state
+  {
+    dot_decode_init,  /* initial state */
+    dot_decode_char,  /* Any character excepting [\r\n.] */
+    dot_decode_lf,    /* prev. char was \n */
+    dot_decode_dot,   /* 2 prev. chars were \n. */
+    dot_decode_end   /* final state, a \n.\n seen. */
+  };
+
+static enum dot_decode_state
+new_decode_state (enum dot_decode_state state, int c)
+{
+  switch (state)
+    {
+    case dot_decode_init:
+      switch (c)
+	{
+	case '.':
+	  return dot_decode_dot;
+	}
+      break;
+      
+    case dot_decode_char:
+      switch (c)
+	{
+	case '\n':
+	  return dot_decode_lf;
+	}
+      break;
+      
+    case dot_decode_lf:
+      switch (c)
+	{
+	case '.':
+	  return dot_decode_dot;
+	}
+      
+    case dot_decode_dot:
+      switch (c)
+	{
+	case '\n':
+	  return dot_decode_end;
+	}
+      break;
+
+    case dot_decode_end:
+      return dot_decode_end;
+    }
+  return dot_decode_char;
+}
+
+/* Move min(isize,osize) bytes from iptr to optr, replacing each \r\n
+   with \n. */
+static enum mu_filter_result
+_dot_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 = dot_decode_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 != dot_decode_end && i < isize && j < osize; i++)
+    {
+      unsigned char c = *iptr++;
+      int curstate = *pstate;
+      
+      *pstate = new_decode_state (curstate, c);
+      if (!(c == '.' && (curstate == dot_decode_init ||
+			 curstate == dot_decode_lf)))
+	optr[j++] = c;
+    }
+  
+  if (*pstate == dot_decode_end)
+    iobuf->eof = 1;
+
+  iobuf->isize = i;
+  iobuf->osize = j;
+  return mu_filter_ok;
+}
+
+enum dot_encode_state
+  {
+    dot_encode_init,  /* initial state */
+    dot_encode_char,  /* Any character excepting \n */
+    dot_encode_lf,    /* prev. char was \n */
+  };    
+
+static enum dot_encode_state
+new_encode_state (int c)
+{
+  switch (c)
+    {
+    case '\n':
+      return dot_encode_lf;
+    }
+  return dot_encode_char;
+}
+
+/* Move min(isize,osize) bytes from iptr to optr, replacing each \n
+   with \r\n.  Any input \r\n sequences remain untouched. */
+static enum mu_filter_result
+_dot_encoder (void *xd,
+	      enum mu_filter_command cmd,
+	      struct mu_filter_io *iobuf)
+{
+  enum mu_filter_result result;
+  size_t i, j;
+  const unsigned char *iptr;
+  size_t isize;
+  char *optr;
+  size_t osize;
+  int *state = xd;
+  
+  switch (cmd)
+    {
+    case mu_filter_init:
+      *state = dot_encode_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; i < isize && j < osize; i++, iptr++)
+    {
+      unsigned char c = *iptr;
+      int curstate = *state;
+
+      if (c == '.' && (curstate == dot_encode_init ||
+		       curstate == dot_encode_lf))
+	{
+ 	  if (j + 2 > osize)
+	    {
+	      if (i == 0)
+		{
+		  iobuf->osize = 2;
+		  return mu_filter_moreoutput;
+		}
+	      break;
+	    }
+	  optr[j++] = '.';
+	  optr[j++] = '.';
+	}
+      else
+	optr[j++] = c;
+
+      *state = new_encode_state (c);
+    }
+
+  result = mu_filter_ok;
+  if (cmd == mu_filter_lastbuf)
+    {
+      switch (*state)
+	{
+	case dot_encode_lf:
+	  if (j + 2 > osize)
+	    result = mu_filter_again;
+	  break;
+	      
+	default:
+	  if (j + 3 > osize)
+	    result = mu_filter_again;
+	  else
+	    optr[j++] = '\n';
+	}
+
+      if (result == mu_filter_ok)
+	{
+	  optr[j++] = '.';
+	  optr[j++] = '\n';
+	}
+    }
+
+  iobuf->isize = i;
+  iobuf->osize = j;
+  return result;
+}
+
+static int
+alloc_state (void **pret, int mode MU_ARG_UNUSED, void *data MU_ARG_UNUSED)
+{
+  *pret = malloc (sizeof (int));
+  if (!*pret)
+    return ENOMEM;
+  return 0;
+}
+
+static struct _mu_filter_record _dot_filter = {
+  "DOT",
+  0,
+  alloc_state,
+  _dot_encoder,
+  _dot_decoder
+};
+
+mu_filter_record_t mu_dot_filter = &_dot_filter;
diff --git a/libmailutils/filter/filter.c b/libmailutils/filter/filter.c
new file mode 100644
index 0000000..a98d0a9
--- /dev/null
+++ b/libmailutils/filter/filter.c
@@ -0,0 +1,193 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 2004, 2005, 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, see 
+   <http://www.gnu.org/licenses/>. */
+
+/* Notes:
+   First draft: Alain Magloire.
+   Complete rewrite: Sergey Poznyakoff.
+*/
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <errno.h>
+#include <string.h>
+#include <stdlib.h>
+
+#ifdef HAVE_STRINGS_H
+# include <strings.h>
+#endif
+
+#include <mailutils/filter.h>
+#include <mailutils/monitor.h>
+#include <mailutils/list.h>
+#include <mailutils/stream.h>
+#include <mailutils/errno.h>
+#include <mailutils/cstr.h>
+
+/* NOTE: We will leak here since the monitor of the filter will never
+   be release.  That's ok we can leave with this, it's only done once.  */
+static mu_list_t filter_list;
+struct mu_monitor filter_monitor = MU_MONITOR_INITIALIZER;
+
+static int
+filter_name_cmp (const void *item, const void *data)
+{
+  struct _mu_filter_record const *rec = item;
+  char const *name = data;
+  return mu_c_strcasecmp (rec->name, name);
+}
+
+int
+mu_filter_get_list (mu_list_t *plist)
+{
+  if (plist == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+  mu_monitor_wrlock (&filter_monitor);
+  if (filter_list == NULL)
+    {
+      int status = mu_list_create (&filter_list);
+      if (status != 0)
+	return status;
+      mu_list_set_comparator (filter_list, filter_name_cmp);
+      /* Default filters.  */
+      mu_list_append (filter_list, mu_base64_filter);
+      mu_list_append (filter_list, mu_qp_filter);
+      mu_list_append (filter_list, mu_binary_filter);
+      mu_list_append (filter_list, mu_bit8_filter);
+      mu_list_append (filter_list, mu_bit7_filter);
+      mu_list_append (filter_list, mu_rfc822_filter);
+      mu_list_append (filter_list, mu_crlf_filter);
+      mu_list_append (filter_list, mu_crlfdot_filter);
+      mu_list_append (filter_list, mu_dot_filter);
+      mu_list_append (filter_list, mu_rfc_2047_Q_filter);
+      mu_list_append (filter_list, mu_rfc_2047_B_filter);
+      mu_list_append (filter_list, mu_from_filter);
+      /* FIXME: add the default encodings?  */
+    }
+  *plist = filter_list;
+  mu_monitor_unlock (&filter_monitor);
+  return 0;
+}
+
+static int
+filter_create_rd (mu_stream_t *pstream, mu_stream_t stream,
+		  size_t max_line_length,
+		  int mode,
+		  mu_filter_xcode_t xcode, void *xdata,
+		  int flags)
+{
+  int status;
+  mu_stream_t fltstream;
+
+  flags &= ~MU_STREAM_AUTOCLOSE;
+  
+  status = mu_filter_stream_create (&fltstream, stream,
+				    mode, xcode, xdata,
+				    flags);
+  if (status == 0)
+    {
+      if (max_line_length)
+	{
+	  status = mu_linelen_filter_create (pstream, fltstream,
+					     max_line_length,
+					     flags);
+	  mu_stream_unref (fltstream);
+	  if (status)
+	    return status;
+	}
+      else
+	*pstream = fltstream;
+
+      if (flags & MU_STREAM_AUTOCLOSE)
+	mu_stream_unref (stream);
+    }
+  return status;
+}
+
+static int
+filter_create_wr (mu_stream_t *pstream, mu_stream_t stream,
+		  size_t max_line_length,
+		  int mode,
+		  mu_filter_xcode_t xcode, void *xdata,
+		  int flags)
+{
+  int status;
+  mu_stream_t fltstream, instream = NULL, tmpstr;
+
+  flags &= ~MU_STREAM_AUTOCLOSE;
+
+  if (max_line_length)
+    {
+      status = mu_linelen_filter_create (&instream, stream,
+					 max_line_length,
+					 flags);
+      if (status)
+	return status;
+      tmpstr = instream;
+    }
+  else
+    tmpstr = stream;
+  
+  status = mu_filter_stream_create (&fltstream, tmpstr,
+				    mode, xcode, xdata,
+				    flags);
+  mu_stream_unref (instream);
+  if (status)
+    return status;
+  *pstream = fltstream;
+  if (flags & MU_STREAM_AUTOCLOSE)
+    mu_stream_unref (stream);
+  return status;
+}
+
+int
+mu_filter_create (mu_stream_t *pstream, mu_stream_t stream, const char *name,
+		  int mode, int flags)
+{
+  int status;
+  mu_filter_record_t frec;
+  mu_list_t list;
+  void *xdata = NULL;
+  
+  if ((flags & MU_STREAM_RDWR) == MU_STREAM_RDWR)
+    return EINVAL;
+  
+  mu_filter_get_list (&list);
+  status = mu_list_locate (list, (void*)name, (void**)&frec);
+  if (status)
+    return status;
+
+  if (frec->newdata)
+    {
+      status = frec->newdata (&xdata, mode, NULL);
+      if (status)
+	return status;
+    }
+
+  status = ((flags & MU_STREAM_WRITE) ? filter_create_wr : filter_create_rd)
+                   (pstream, stream,
+		    frec->max_line_length,
+		    mode,
+		    mode == MU_FILTER_ENCODE ? frec->encoder : frec->decoder,
+		    xdata,
+		    flags);
+  if (status)
+    free (xdata);
+  return status;
+}
diff --git a/libmailutils/filter/filter_iconv.c b/libmailutils/filter/filter_iconv.c
new file mode 100644
index 0000000..79835a5
--- /dev/null
+++ b/libmailutils/filter/filter_iconv.c
@@ -0,0 +1,481 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 2004, 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, see
+   <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <mailutils/stream.h>
+#include <mailutils/sys/stream.h>
+#include <mailutils/filter.h>
+#include <mailutils/errno.h>
+#include <mailutils/nls.h>
+
+#ifdef HAVE_ICONV_H
+# include <iconv.h>
+#endif
+
+#ifndef ICONV_CONST
+# define ICONV_CONST
+#endif
+
+#ifndef HAVE_ICONV
+# undef iconv_open
+# define iconv_open(tocode, fromcode) ((iconv_t) -1)
+
+# undef iconv
+# define iconv(cd, inbuf, inbytesleft, outbuf, outbytesleft) (errno = EILSEQ, (size_t) -1)
+
+# undef iconv_close
+# define iconv_close(cd) 0
+
+#endif 
+
+enum _icvt_state
+  {
+    state_closed,           /* Filter is closed */
+    state_open,             /* Filter is open and running in conversion mode */
+    state_copy_pass,        /* Filter is open and running in copy-pass mode */
+    state_copy_octal,       /* Filter is open and running in copy-octal mode */
+    state_iconv_error,      /* A fatal iconv error has occurred */
+    state_transport_error   /* A fatal transport error has occurred */
+  };
+
+struct icvt_stream
+{
+  struct _mu_stream stream;
+  mu_stream_t transport;/* I/O stream */
+  int fallback_mode;
+  iconv_t cd;           /* Conversion descriptor */
+  char *buf;            /* Conversion buffer */
+  size_t bufsize;       /* Size of buf */
+  size_t bufpos;        /* Current position in buf */
+  enum _icvt_state state;
+  int ec;               /* Error code */
+  char errbuf[128];     /* Error message buffer */
+};
+
+static int
+_icvt_open (mu_stream_t stream)
+{
+  struct icvt_stream *s = (struct icvt_stream *)stream;
+  if (s->cd == (iconv_t) -1)
+    return EINVAL;
+  s->state = state_open;
+  return 0;
+}
+
+static int
+_icvt_close (mu_stream_t stream)
+{
+  struct icvt_stream *s = (struct icvt_stream *)stream;
+  if (s->state != state_closed)
+    {
+      mu_stream_close (s->transport);
+      iconv_close (s->cd);
+      s->cd = (iconv_t) -1;
+      s->state = state_closed;
+    }
+  return 0;
+}
+
+static void
+_icvt_done (mu_stream_t stream)
+{
+  struct icvt_stream *s = (struct icvt_stream *)stream;
+
+  if (s->state != state_closed)
+    _icvt_close (stream);
+  mu_stream_destroy (&s->transport);
+  free (s->buf);
+}
+
+static int _icvt_read (mu_stream_t stream, char *optr, size_t osize,
+		       size_t *pnbytes);
+
+static int
+internal_icvt_read (mu_stream_t stream, char *optr, size_t osize,
+		    size_t *pnbytes)
+{
+  struct icvt_stream *s = (struct icvt_stream *)stream;
+  size_t nbytes = 0;
+  int rc, status = 0;
+  char *ob = optr;
+  size_t olen = osize;
+
+  if (s->bufpos == 0)
+    {
+      status = mu_stream_read (s->transport, s->buf, s->bufsize, &nbytes);
+      if (status)
+	{
+	  s->state = state_transport_error;
+	  s->ec = rc;
+	  return MU_ERR_FAILURE;
+	}
+      else if (nbytes == 0)
+	{
+	  if (pnbytes)
+	    *pnbytes = 0;
+	  return 0;
+	}
+    }
+  
+  do
+    {
+      char ICONV_CONST *ib = s->buf;
+      size_t inlen = s->bufpos + nbytes;
+      
+      rc = iconv (s->cd, &ib, &inlen, &ob, &olen);
+      if (ib > s->buf)
+	{
+	  memmove (s->buf, ib, inlen);
+	  s->bufpos = inlen;
+	}
+      else
+	s->bufpos += nbytes;
+      
+      if (rc == -1)
+	{
+	  if (errno == E2BIG)
+	    {
+	      if (ob > optr)
+		break;
+	      else
+		{
+		  s->ec = MU_ERR_BUFSPACE;
+		  return MU_ERR_BUFSPACE;
+		}
+	    }
+	  else if (errno == EILSEQ)
+	    {
+	      switch (s->fallback_mode)
+		{
+		case mu_fallback_none:
+		  s->state = state_iconv_error;
+		  s->ec = errno;
+		  if (ob == optr)
+		    return MU_ERR_FAILURE;
+		  break;
+
+		case mu_fallback_copy_pass:
+		  s->state = state_copy_pass;
+		  if (ob == optr)
+		    return _icvt_read (stream, optr, osize, pnbytes);
+		  break;
+		  
+		case mu_fallback_copy_octal:
+		  s->state = state_copy_octal;
+		  if (ob == optr)
+		    return _icvt_read (stream, optr, osize, pnbytes);
+		  break;
+		}
+	    }
+	  else if (errno == EINVAL)
+	    {
+	      if (inlen == s->bufsize)
+		{
+		  /* Try to reallocate temp buffer */
+		  char *p = realloc (s->buf, s->bufsize + 128);
+		  if (!p)
+		    return ENOMEM;
+		  s->buf = p;
+		  s->bufsize += 128;
+		}
+	      continue;
+	    }
+	  else
+	    {
+	      s->ec = errno;
+	      s->state = state_iconv_error;
+	      return MU_ERR_FAILURE;
+	    }
+	}
+    }
+  while (olen > 0 
+	 && (status = mu_stream_read (s->transport,
+				      s->buf + s->bufpos,
+				      s->bufsize - s->bufpos,
+				      &nbytes)) == 0
+	 && nbytes);
+
+  if (status)
+    {
+      s->state = state_transport_error;
+      s->ec = status;
+      if (ob == optr)
+	return MU_ERR_FAILURE;
+    }
+      
+  if (pnbytes)
+    *pnbytes = ob - optr;
+  return 0;
+}
+
+#define ISPRINT(c) (((c)>=' '&&(c)<127)||(c)=='\n')
+
+static int
+copy_octal (struct icvt_stream *s, char *optr, size_t osize, size_t *pnbytes)
+{
+  size_t i, j;
+  int status;
+  
+  if (osize == 0)
+    return MU_ERR_BUFSPACE;
+  
+  if (s->bufpos < osize)
+    {
+      size_t rdcount = osize;
+      if (s->bufsize < rdcount)
+	{
+	  /* Try to reallocate temp buffer */
+	  char *p = realloc (s->buf, rdcount);
+	  if (p)
+	    {
+	      s->bufsize = rdcount;
+	      s->buf = p;
+	    }
+	  else
+	    rdcount = s->bufsize;
+	}
+
+      status = mu_stream_read (s->transport,
+			       s->buf + s->bufpos,
+			       rdcount - s->bufpos,
+			       &rdcount);
+      if (status)
+	{
+	  s->state = state_transport_error;
+	  s->ec = status;
+	  if (s->bufpos == 0)
+	    return MU_ERR_FAILURE;
+	}
+      else
+	s->bufpos += rdcount;
+    }
+  
+  for (i = j = 0; j < osize && i < s->bufpos; i++)
+    {
+      if (ISPRINT (*(unsigned char*)(s->buf+i)))
+	optr[j++] = s->buf[i];
+      else if (j + 4 >= osize)
+	break;
+      else
+	{
+	  sprintf (optr + j, "\\%03o", *(unsigned char*)(s->buf+i));
+	  j += 4;
+	}
+    }
+  s->bufpos -= i;
+  memmove (s->buf, s->buf + i, s->bufpos);
+  if (pnbytes)
+    *pnbytes = j;
+  return 0;
+}
+
+static int
+copy_pass (struct icvt_stream *s, char *optr, size_t osize, size_t *pnbytes)
+{
+  int status;
+  size_t nbytes;
+  
+  if (s->bufpos)
+    {
+      size_t sz = s->bufpos < osize ? s->bufpos : osize;
+      memcpy (optr, s->buf, sz);
+      s->bufpos -= sz;
+      if (s->bufpos)
+	memmove (s->buf, s->buf + sz, s->bufpos);
+      if (pnbytes)
+	*pnbytes = sz;
+      return 0;
+    }
+
+  status = mu_stream_read (s->transport, optr, osize, &nbytes);
+  if (status)
+    {
+      s->state = state_transport_error;
+      s->ec = status;
+      if (s->bufpos == 0)
+	return MU_ERR_FAILURE;
+    }
+  if (pnbytes)
+    *pnbytes = nbytes;
+  return 0;
+}
+
+static int
+_icvt_read (mu_stream_t stream, char *optr, size_t osize, size_t *pnbytes)
+{
+  struct icvt_stream *s = (struct icvt_stream *)stream;
+
+  switch (s->state)
+    {
+    case state_open:
+      return internal_icvt_read (stream, optr, osize, pnbytes);
+      
+    case state_closed:
+      return EINVAL;
+      
+    case state_copy_pass:
+      return copy_pass (s, optr, osize, pnbytes);
+      
+    case state_copy_octal:
+      return copy_octal (s, optr, osize, pnbytes);
+	
+    default:
+      break;
+    }
+  return MU_ERR_FAILURE;
+}
+
+const char *
+_icvt_strerror (mu_stream_t stream, int rc)
+{
+  struct icvt_stream *s = (struct icvt_stream *)stream;
+
+  switch (s->state)
+    {
+    case state_transport_error:
+      snprintf (s->errbuf, sizeof s->errbuf,
+		_("Transport error: %s"), mu_strerror (s->ec));
+      break;
+
+    case state_iconv_error:
+      switch (s->ec)
+	{
+	case EILSEQ:
+	  snprintf (s->errbuf, sizeof s->errbuf,
+		    _("Illegal multibyte sequence near %*.*s"),
+		    (int) s->bufpos, (int) s->bufpos, s->buf);
+	  break;
+
+	default:
+	  snprintf (s->errbuf, sizeof s->errbuf,
+		    _("Iconv error: %s"), mu_strerror (s->ec));
+	}
+      break;
+
+    case state_closed:
+      return _("Stream is closed"); 
+      
+    default:
+      return mu_strerror (s->ec);
+    }
+  
+  return s->errbuf;
+}
+
+static int
+_icvt_ioctl (mu_stream_t stream, int code, void *ptr)
+{
+  struct icvt_stream *s = (struct icvt_stream *)stream;
+  mu_transport_t *ptrans;
+  
+  switch (code)
+    {
+    case MU_IOCTL_GET_TRANSPORT:
+      if (!ptr)
+	return EINVAL;
+      ptrans = ptr;
+      ptrans[0] = (mu_transport_t) s->transport;
+      ptrans[1] = NULL;
+      break;
+
+    case MU_IOCTL_GET_STREAM:
+    case MU_IOCTL_SET_STREAM:
+    case MU_IOCTL_GET_TRANSPORT_BUFFER:
+    case MU_IOCTL_SET_TRANSPORT_BUFFER:
+      return mu_stream_ioctl (s->transport, code, ptr);
+      
+    default:
+      return ENOSYS;
+    }
+  return 0;
+}
+
+int
+_icvt_wait (mu_stream_t stream, int *pflags, struct timeval *tvp)
+{
+  struct icvt_stream *s = (struct icvt_stream *)stream;
+  return mu_stream_wait (s->transport, pflags, tvp);
+}
+
+/* FIXME: Seeks in the *transport* stream. */
+int
+_icvt_seek (mu_stream_t stream, mu_off_t off, mu_off_t *presult)
+{
+  struct icvt_stream *s = (struct icvt_stream *)stream;
+  return mu_stream_seek (s->transport, off, MU_SEEK_SET, presult);
+}
+
+int
+mu_filter_iconv_create (mu_stream_t *s, mu_stream_t transport,
+			const char *fromcode, const char *tocode, int flags,
+			enum mu_iconv_fallback_mode fallback_mode)
+{
+  struct icvt_stream *iptr;
+  iconv_t cd;
+  int rc;
+  mu_stream_t stream;
+    
+  cd = iconv_open (tocode, fromcode);
+  if (cd == (iconv_t) -1)
+    return MU_ERR_FAILURE;
+
+  iptr = (struct icvt_stream *) _mu_stream_create (sizeof (*iptr), flags);
+  if (!iptr)
+    {
+      iconv_close (cd);
+      return ENOMEM;
+    }
+  
+  if (!(flags & MU_STREAM_AUTOCLOSE))
+    mu_stream_ref (transport);
+  iptr->transport = transport;
+  iptr->fallback_mode = fallback_mode;
+  iptr->cd = cd;
+  iptr->state = state_closed;
+  iptr->bufsize = 128;
+  iptr->buf = malloc (iptr->bufsize);
+  if (!iptr->buf)
+    {
+      free (iptr);
+      return ENOMEM;
+    }
+  iptr->bufpos = 0;
+  
+  iptr->stream.open = _icvt_open;
+  iptr->stream.close = _icvt_close;
+  iptr->stream.read = _icvt_read;
+  iptr->stream.done = _icvt_done;
+  iptr->stream.error_string = _icvt_strerror;
+  iptr->stream.ctl = _icvt_ioctl;
+  iptr->stream.wait = _icvt_wait;
+  iptr->stream.seek = _icvt_seek;
+  iptr->stream.flags = MU_STREAM_READ | MU_STREAM_SEEK;
+  stream = (mu_stream_t)iptr;
+  rc = mu_stream_open (stream);
+  if (rc)
+    mu_stream_destroy (&stream);
+  else
+    *s = stream;
+  return 0;
+}
diff --git a/libmailutils/filter/fromflt.c b/libmailutils/filter/fromflt.c
new file mode 100644
index 0000000..ebf4326
--- /dev/null
+++ b/libmailutils/filter/fromflt.c
@@ -0,0 +1,282 @@
+/* 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, see 
+   <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <string.h>
+#include <mailutils/errno.h>
+#include <mailutils/filter.h>
+#include <mailutils/stream.h>
+
+enum from_decode_state
+  {
+    from_decode_init,
+    from_decode_nl,
+    from_decode_char
+  };
+
+#define GT_FROM_MARK_STR ">From "
+#define GT_FROM_MARK_LEN (sizeof (GT_FROM_MARK_STR) - 1)
+
+/* Move min(isize,osize) bytes from iptr to optr, replacing each '>From '
+   at the beginning of line with 'From '. */
+static enum mu_filter_result
+_from_decoder (void *xd,
+	       enum mu_filter_command cmd,
+	       struct mu_filter_io *iobuf)
+{
+  int *pstate = xd;
+  const unsigned char *iptr;
+  size_t isize;
+  char *optr;
+  size_t osize;
+  enum from_decode_state state;
+  size_t i, j;
+
+  switch (cmd)
+    {
+    case mu_filter_init:
+      *pstate = from_decode_init;
+      return mu_filter_ok;
+      
+    case mu_filter_done:
+      return mu_filter_ok;
+      
+    default:
+      state = *pstate;
+      break;
+    }
+  
+  iptr = (const unsigned char *) iobuf->input;
+  isize = iobuf->isize;
+  optr = iobuf->output;
+  osize = iobuf->osize;
+
+  for (i = j = 0; i < isize && j < osize; i++)
+    {
+      unsigned char c = *iptr++;
+      
+      if (c == '\n')
+	state = from_decode_nl;
+      else if (state == from_decode_init || state == from_decode_nl)
+	{
+	  size_t len = isize - i;
+
+	  if (len < GT_FROM_MARK_LEN)
+	    {
+	      if (memcmp (iptr - 1, GT_FROM_MARK_STR, len) == 0)
+		{
+		  if (i == 0)
+		    {
+		      iobuf->isize = GT_FROM_MARK_LEN - len;
+		      return mu_filter_moreinput;
+		    }
+		  break;
+		}
+	      else
+		state = from_decode_char;
+	    }
+	  else if (memcmp (iptr - 1, GT_FROM_MARK_STR, GT_FROM_MARK_LEN) == 0)
+	    {
+	      /* Skip > */
+	      state = from_decode_char;
+	      continue;
+	    }
+	}	  
+      optr[j++] = c;
+    }
+
+  *pstate = state;
+  iobuf->isize = i;
+  iobuf->osize = j;
+  return mu_filter_ok;
+}
+
+#define FROM_MARK_STR "From "
+#define FROM_MARK_LEN (sizeof (FROM_MARK_STR) - 1)
+
+enum from_encode_state
+  {
+    from_encode_init,
+    from_encode_nl,
+    from_encode_char,
+    from_encode_gt,
+    from_encode_f,
+    from_encode_r,
+    from_encode_o,
+    from_encode_m,
+    from_encode_sp
+  };
+
+static int length_to_state_tab[] = {
+  from_encode_gt,
+  from_encode_f,
+  from_encode_r,
+  from_encode_o,
+  from_encode_m,
+  from_encode_sp
+};
+
+static int state_to_length_tab[] = {
+  0, 0, 0,
+  GT_FROM_MARK_LEN,
+  GT_FROM_MARK_LEN-1,
+  GT_FROM_MARK_LEN-2,
+  GT_FROM_MARK_LEN-3,
+  GT_FROM_MARK_LEN-4,
+  GT_FROM_MARK_LEN-5
+};      
+
+/* Move min(isize,osize) bytes from iptr to optr, replacing each 'From '
+   at the beginning of line with '>From '. */
+
+static enum mu_filter_result
+_from_encoder (void *xd,
+	       enum mu_filter_command cmd,
+	       struct mu_filter_io *iobuf)
+{
+  int *pstate = xd;
+  const unsigned char *iptr;
+  size_t isize;
+  char *optr;
+  size_t osize;
+  enum from_encode_state state;
+  size_t i, j;
+  
+  switch (cmd)
+    {
+    case mu_filter_init:
+      *pstate = from_encode_init;
+      return mu_filter_ok;
+      
+    case mu_filter_done:
+      return mu_filter_ok;
+      
+    default:
+      state = *pstate;
+      switch (state)
+	{
+	case from_encode_init:
+	case from_encode_nl:
+	case from_encode_char:
+	  break;
+
+	default:
+	  osize = state_to_length_tab[state];
+	  if (iobuf->osize < osize)
+	    {
+	      iobuf->osize = osize;
+	      return mu_filter_moreoutput;
+	    }
+	  memcpy (iobuf->output, GT_FROM_MARK_STR + GT_FROM_MARK_LEN - osize,
+		  osize);
+	  iobuf->osize = osize;
+	  iobuf->isize = osize;
+	  *pstate = from_encode_init;
+	  return mu_filter_ok;
+	}
+      break;
+    }
+  
+  iptr = (const unsigned char *) iobuf->input;
+  isize = iobuf->isize;
+  optr = iobuf->output;
+  osize = iobuf->osize;
+
+  for (i = j = 0; i < isize && j < osize; i++)
+    {
+      unsigned char c = *iptr++;
+      
+      if (c == '\n')
+	state = from_encode_nl;
+      else if (state == from_encode_init || state == from_encode_nl)
+	{
+	  size_t len = isize - i;
+
+	  if (len < FROM_MARK_LEN)
+	    {
+	      if (memcmp (iptr - 1, FROM_MARK_STR, len) == 0)
+		{
+		  if (i == 0)
+		    {
+		      iobuf->isize = FROM_MARK_LEN;
+		      return mu_filter_moreinput;
+		    }
+		  break;
+		}
+	      else
+		state = from_encode_char;
+	    }
+	  else if (memcmp (iptr - 1, FROM_MARK_STR, FROM_MARK_LEN) == 0)
+	    {
+	      size_t rest = osize - j;
+	      
+	      if (rest > GT_FROM_MARK_LEN)
+		rest = GT_FROM_MARK_LEN;
+	      else if (rest < 2)
+		{
+		  if (i == 0)
+		    {
+		      iobuf->osize = GT_FROM_MARK_LEN;
+		      return mu_filter_moreoutput;
+		    }
+		  break;
+		}
+	      
+	      memcpy (optr + j, GT_FROM_MARK_STR, rest);
+	      i += rest - 2;
+	      iptr += rest - 2;
+	      j += rest;
+	      if (rest < GT_FROM_MARK_LEN)
+		state = length_to_state_tab[rest];
+	      else
+		state = from_encode_char;
+	      continue;
+	    }
+	  else
+	    state = from_encode_char;
+	}
+      optr[j++] = c;
+    }
+  *pstate = state;
+  iobuf->isize = i;
+  iobuf->osize = j;
+  return mu_filter_ok;
+}
+
+static int
+_from_alloc_state (void **pret, int mode, void *data MU_ARG_UNUSED)
+{
+  *pret = malloc (sizeof (int));
+  if (!*pret)
+    return ENOMEM;
+  return 0;
+}
+
+static struct _mu_filter_record _from_filter = {
+  "FROM",
+  0,
+  _from_alloc_state,
+  _from_encoder,
+  _from_decoder
+};
+
+mu_filter_record_t mu_from_filter = &_from_filter;
+
diff --git a/libmailutils/linelenflt.c b/libmailutils/filter/linelenflt.c
similarity index 100%
rename from libmailutils/linelenflt.c
rename to libmailutils/filter/linelenflt.c
diff --git a/libmailutils/qpflt.c b/libmailutils/filter/qpflt.c
similarity index 100%
rename from libmailutils/qpflt.c
rename to libmailutils/filter/qpflt.c
diff --git a/libmailutils/filter_iconv.c b/libmailutils/filter_iconv.c
deleted file mode 100644
index 571e8b7..0000000
--- a/libmailutils/filter_iconv.c
+++ /dev/null
@@ -1,482 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 2004, 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 */
-
-#ifdef HAVE_CONFIG_H
-# include <config.h>
-#endif
-
-#include <unistd.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <errno.h>
-#include <mailutils/stream.h>
-#include <mailutils/sys/stream.h>
-#include <mailutils/filter.h>
-#include <mailutils/errno.h>
-#include <mailutils/nls.h>
-
-#ifdef HAVE_ICONV_H
-# include <iconv.h>
-#endif
-
-#ifndef ICONV_CONST
-# define ICONV_CONST
-#endif
-
-#ifndef HAVE_ICONV
-# undef iconv_open
-# define iconv_open(tocode, fromcode) ((iconv_t) -1)
-
-# undef iconv
-# define iconv(cd, inbuf, inbytesleft, outbuf, outbytesleft) (errno = EILSEQ, (size_t) -1)
-
-# undef iconv_close
-# define iconv_close(cd) 0
-
-#endif 
-
-enum _icvt_state
-  {
-    state_closed,           /* Filter is closed */
-    state_open,             /* Filter is open and running in conversion mode */
-    state_copy_pass,        /* Filter is open and running in copy-pass mode */
-    state_copy_octal,       /* Filter is open and running in copy-octal mode */
-    state_iconv_error,      /* A fatal iconv error has occurred */
-    state_transport_error   /* A fatal transport error has occurred */
-  };
-
-struct icvt_stream
-{
-  struct _mu_stream stream;
-  mu_stream_t transport;/* I/O stream */
-  int fallback_mode;
-  iconv_t cd;           /* Conversion descriptor */
-  char *buf;            /* Conversion buffer */
-  size_t bufsize;       /* Size of buf */
-  size_t bufpos;        /* Current position in buf */
-  enum _icvt_state state;
-  int ec;               /* Error code */
-  char errbuf[128];     /* Error message buffer */
-};
-
-static int
-_icvt_open (mu_stream_t stream)
-{
-  struct icvt_stream *s = (struct icvt_stream *)stream;
-  if (s->cd == (iconv_t) -1)
-    return EINVAL;
-  s->state = state_open;
-  return 0;
-}
-
-static int
-_icvt_close (mu_stream_t stream)
-{
-  struct icvt_stream *s = (struct icvt_stream *)stream;
-  if (s->state != state_closed)
-    {
-      mu_stream_close (s->transport);
-      iconv_close (s->cd);
-      s->cd = (iconv_t) -1;
-      s->state = state_closed;
-    }
-  return 0;
-}
-
-static void
-_icvt_done (mu_stream_t stream)
-{
-  struct icvt_stream *s = (struct icvt_stream *)stream;
-
-  if (s->state != state_closed)
-    _icvt_close (stream);
-  mu_stream_destroy (&s->transport);
-  free (s->buf);
-}
-
-static int _icvt_read (mu_stream_t stream, char *optr, size_t osize,
-		       size_t *pnbytes);
-
-static int
-internal_icvt_read (mu_stream_t stream, char *optr, size_t osize,
-		    size_t *pnbytes)
-{
-  struct icvt_stream *s = (struct icvt_stream *)stream;
-  size_t nbytes = 0;
-  int rc, status = 0;
-  char *ob = optr;
-  size_t olen = osize;
-
-  if (s->bufpos == 0)
-    {
-      status = mu_stream_read (s->transport, s->buf, s->bufsize, &nbytes);
-      if (status)
-	{
-	  s->state = state_transport_error;
-	  s->ec = rc;
-	  return MU_ERR_FAILURE;
-	}
-      else if (nbytes == 0)
-	{
-	  if (pnbytes)
-	    *pnbytes = 0;
-	  return 0;
-	}
-    }
-  
-  do
-    {
-      char ICONV_CONST *ib = s->buf;
-      size_t inlen = s->bufpos + nbytes;
-      
-      rc = iconv (s->cd, &ib, &inlen, &ob, &olen);
-      if (ib > s->buf)
-	{
-	  memmove (s->buf, ib, inlen);
-	  s->bufpos = inlen;
-	}
-      else
-	s->bufpos += nbytes;
-      
-      if (rc == -1)
-	{
-	  if (errno == E2BIG)
-	    {
-	      if (ob > optr)
-		break;
-	      else
-		{
-		  s->ec = MU_ERR_BUFSPACE;
-		  return MU_ERR_BUFSPACE;
-		}
-	    }
-	  else if (errno == EILSEQ)
-	    {
-	      switch (s->fallback_mode)
-		{
-		case mu_fallback_none:
-		  s->state = state_iconv_error;
-		  s->ec = errno;
-		  if (ob == optr)
-		    return MU_ERR_FAILURE;
-		  break;
-
-		case mu_fallback_copy_pass:
-		  s->state = state_copy_pass;
-		  if (ob == optr)
-		    return _icvt_read (stream, optr, osize, pnbytes);
-		  break;
-		  
-		case mu_fallback_copy_octal:
-		  s->state = state_copy_octal;
-		  if (ob == optr)
-		    return _icvt_read (stream, optr, osize, pnbytes);
-		  break;
-		}
-	    }
-	  else if (errno == EINVAL)
-	    {
-	      if (inlen == s->bufsize)
-		{
-		  /* Try to reallocate temp buffer */
-		  char *p = realloc (s->buf, s->bufsize + 128);
-		  if (!p)
-		    return ENOMEM;
-		  s->buf = p;
-		  s->bufsize += 128;
-		}
-	      continue;
-	    }
-	  else
-	    {
-	      s->ec = errno;
-	      s->state = state_iconv_error;
-	      return MU_ERR_FAILURE;
-	    }
-	}
-    }
-  while (olen > 0 
-	 && (status = mu_stream_read (s->transport,
-				      s->buf + s->bufpos,
-				      s->bufsize - s->bufpos,
-				      &nbytes)) == 0
-	 && nbytes);
-
-  if (status)
-    {
-      s->state = state_transport_error;
-      s->ec = status;
-      if (ob == optr)
-	return MU_ERR_FAILURE;
-    }
-      
-  if (pnbytes)
-    *pnbytes = ob - optr;
-  return 0;
-}
-
-#define ISPRINT(c) (((c)>=' '&&(c)<127)||(c)=='\n')
-
-static int
-copy_octal (struct icvt_stream *s, char *optr, size_t osize, size_t *pnbytes)
-{
-  size_t i, j;
-  int status;
-  
-  if (osize == 0)
-    return MU_ERR_BUFSPACE;
-  
-  if (s->bufpos < osize)
-    {
-      size_t rdcount = osize;
-      if (s->bufsize < rdcount)
-	{
-	  /* Try to reallocate temp buffer */
-	  char *p = realloc (s->buf, rdcount);
-	  if (p)
-	    {
-	      s->bufsize = rdcount;
-	      s->buf = p;
-	    }
-	  else
-	    rdcount = s->bufsize;
-	}
-
-      status = mu_stream_read (s->transport,
-			       s->buf + s->bufpos,
-			       rdcount - s->bufpos,
-			       &rdcount);
-      if (status)
-	{
-	  s->state = state_transport_error;
-	  s->ec = status;
-	  if (s->bufpos == 0)
-	    return MU_ERR_FAILURE;
-	}
-      else
-	s->bufpos += rdcount;
-    }
-  
-  for (i = j = 0; j < osize && i < s->bufpos; i++)
-    {
-      if (ISPRINT (*(unsigned char*)(s->buf+i)))
-	optr[j++] = s->buf[i];
-      else if (j + 4 >= osize)
-	break;
-      else
-	{
-	  sprintf (optr + j, "\\%03o", *(unsigned char*)(s->buf+i));
-	  j += 4;
-	}
-    }
-  s->bufpos -= i;
-  memmove (s->buf, s->buf + i, s->bufpos);
-  if (pnbytes)
-    *pnbytes = j;
-  return 0;
-}
-
-static int
-copy_pass (struct icvt_stream *s, char *optr, size_t osize, size_t *pnbytes)
-{
-  int status;
-  size_t nbytes;
-  
-  if (s->bufpos)
-    {
-      size_t sz = s->bufpos < osize ? s->bufpos : osize;
-      memcpy (optr, s->buf, sz);
-      s->bufpos -= sz;
-      if (s->bufpos)
-	memmove (s->buf, s->buf + sz, s->bufpos);
-      if (pnbytes)
-	*pnbytes = sz;
-      return 0;
-    }
-
-  status = mu_stream_read (s->transport, optr, osize, &nbytes);
-  if (status)
-    {
-      s->state = state_transport_error;
-      s->ec = status;
-      if (s->bufpos == 0)
-	return MU_ERR_FAILURE;
-    }
-  if (pnbytes)
-    *pnbytes = nbytes;
-  return 0;
-}
-
-static int
-_icvt_read (mu_stream_t stream, char *optr, size_t osize, size_t *pnbytes)
-{
-  struct icvt_stream *s = (struct icvt_stream *)stream;
-
-  switch (s->state)
-    {
-    case state_open:
-      return internal_icvt_read (stream, optr, osize, pnbytes);
-      
-    case state_closed:
-      return EINVAL;
-      
-    case state_copy_pass:
-      return copy_pass (s, optr, osize, pnbytes);
-      
-    case state_copy_octal:
-      return copy_octal (s, optr, osize, pnbytes);
-	
-    default:
-      break;
-    }
-  return MU_ERR_FAILURE;
-}
-
-const char *
-_icvt_strerror (mu_stream_t stream, int rc)
-{
-  struct icvt_stream *s = (struct icvt_stream *)stream;
-
-  switch (s->state)
-    {
-    case state_transport_error:
-      snprintf (s->errbuf, sizeof s->errbuf,
-		_("Transport error: %s"), mu_strerror (s->ec));
-      break;
-
-    case state_iconv_error:
-      switch (s->ec)
-	{
-	case EILSEQ:
-	  snprintf (s->errbuf, sizeof s->errbuf,
-		    _("Illegal multibyte sequence near %*.*s"),
-		    (int) s->bufpos, (int) s->bufpos, s->buf);
-	  break;
-
-	default:
-	  snprintf (s->errbuf, sizeof s->errbuf,
-		    _("Iconv error: %s"), mu_strerror (s->ec));
-	}
-      break;
-
-    case state_closed:
-      return _("Stream is closed"); 
-      
-    default:
-      return mu_strerror (s->ec);
-    }
-  
-  return s->errbuf;
-}
-
-static int
-_icvt_ioctl (mu_stream_t stream, int code, void *ptr)
-{
-  struct icvt_stream *s = (struct icvt_stream *)stream;
-  mu_transport_t *ptrans;
-  
-  switch (code)
-    {
-    case MU_IOCTL_GET_TRANSPORT:
-      if (!ptr)
-	return EINVAL;
-      ptrans = ptr;
-      ptrans[0] = (mu_transport_t) s->transport;
-      ptrans[1] = NULL;
-      break;
-
-    case MU_IOCTL_GET_STREAM:
-    case MU_IOCTL_SET_STREAM:
-    case MU_IOCTL_GET_TRANSPORT_BUFFER:
-    case MU_IOCTL_SET_TRANSPORT_BUFFER:
-      return mu_stream_ioctl (s->transport, code, ptr);
-      
-    default:
-      return ENOSYS;
-    }
-  return 0;
-}
-
-int
-_icvt_wait (mu_stream_t stream, int *pflags, struct timeval *tvp)
-{
-  struct icvt_stream *s = (struct icvt_stream *)stream;
-  return mu_stream_wait (s->transport, pflags, tvp);
-}
-
-/* FIXME: Seeks in the *transport* stream. */
-int
-_icvt_seek (mu_stream_t stream, mu_off_t off, mu_off_t *presult)
-{
-  struct icvt_stream *s = (struct icvt_stream *)stream;
-  return mu_stream_seek (s->transport, off, MU_SEEK_SET, presult);
-}
-
-int
-mu_filter_iconv_create (mu_stream_t *s, mu_stream_t transport,
-			const char *fromcode, const char *tocode, int flags,
-			enum mu_iconv_fallback_mode fallback_mode)
-{
-  struct icvt_stream *iptr;
-  iconv_t cd;
-  int rc;
-  mu_stream_t stream;
-    
-  cd = iconv_open (tocode, fromcode);
-  if (cd == (iconv_t) -1)
-    return MU_ERR_FAILURE;
-
-  iptr = (struct icvt_stream *) _mu_stream_create (sizeof (*iptr), flags);
-  if (!iptr)
-    {
-      iconv_close (cd);
-      return ENOMEM;
-    }
-  
-  if (!(flags & MU_STREAM_AUTOCLOSE))
-    mu_stream_ref (transport);
-  iptr->transport = transport;
-  iptr->fallback_mode = fallback_mode;
-  iptr->cd = cd;
-  iptr->state = state_closed;
-  iptr->bufsize = 128;
-  iptr->buf = malloc (iptr->bufsize);
-  if (!iptr->buf)
-    {
-      free (iptr);
-      return ENOMEM;
-    }
-  iptr->bufpos = 0;
-  
-  iptr->stream.open = _icvt_open;
-  iptr->stream.close = _icvt_close;
-  iptr->stream.read = _icvt_read;
-  iptr->stream.done = _icvt_done;
-  iptr->stream.error_string = _icvt_strerror;
-  iptr->stream.ctl = _icvt_ioctl;
-  iptr->stream.wait = _icvt_wait;
-  iptr->stream.seek = _icvt_seek;
-  iptr->stream.flags = MU_STREAM_READ | MU_STREAM_SEEK;
-  stream = (mu_stream_t)iptr;
-  rc = mu_stream_open (stream);
-  if (rc)
-    mu_stream_destroy (&stream);
-  else
-    *s = stream;
-  return 0;
-}
diff --git a/libmailutils/folder.c b/libmailutils/folder.c
deleted file mode 100644
index 3ce430a..0000000
--- a/libmailutils/folder.c
+++ /dev/null
@@ -1,511 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 1999, 2000, 2001, 2004, 2005, 2006, 2007, 2008, 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 <errno.h>
-#include <stdlib.h>
-#include <string.h>
-#include <fnmatch.h>
-
-#include <mailutils/auth.h>
-#include <mailutils/debug.h>
-#include <mailutils/iterator.h>
-#include <mailutils/list.h>
-#include <mailutils/monitor.h>
-#include <mailutils/observer.h>
-#include <mailutils/registrar.h>
-#include <mailutils/stream.h>
-#include <mailutils/url.h>
-#include <mailutils/errno.h>
-
-#include <mailutils/sys/folder.h>
-
-/* Internal folder list.  */
-static mu_list_t known_folder_list;
-static int is_known_folder (mu_url_t, mu_folder_t *);
-
-/* Static folder lock.  */
-static struct mu_monitor folder_lock = MU_MONITOR_INITIALIZER;
-
-int
-mu_folder_match (const char *name, void *pattern, int flags)
-{
-  return fnmatch (pattern, name[0] == '/' ? name + 1 : name, flags);
-}
-
-/* A folder could be remote (IMAP), or local(a spool directory) like $HOME/Mail
-   etc ..  We maintain a list of known folders to avoid creating multiple
-   folders for the same URL.  So, when mu_folder_create is called we check if
-   we already have a folder for that URL and return it, otherwise we create a
-   new one.  Downsides: the scheme to detect the same URL is very weak, and
-   there could be cases where you'll want a different folder for the same URL,
-   there is not easy way to do this.  */
-int
-mu_folder_create_from_record (mu_folder_t *pfolder, mu_url_t url,
-			      mu_record_t record)
-{
-  if (!pfolder)
-    return MU_ERR_OUT_PTR_NULL;
-
-  if (record || 
-      /* Look in the registrar list(iterator), for a possible concrete mailbox
-	 implementation that could match the URL.  */
-      mu_registrar_lookup_url (url, MU_FOLDER_ATTRIBUTE_DIRECTORY, &record,
-			       NULL) == 0)
-    {
-      int (*f_init) (mu_folder_t) = NULL;
-      
-      mu_record_get_folder (record, &f_init);
-      if (f_init)
-        {
-	  int status;
-	  mu_folder_t folder;
-	  int (*u_init) (mu_url_t) = NULL;
-
-	  mu_record_get_url (record, &u_init);
-	  if (u_init)
-	    {
-	      status = u_init (url);
-	      if (status)
-		return status;
-	    }
-	  
-	  mu_monitor_wrlock (&folder_lock);
-
-	  /* Check if we already have the same URL folder.  */
-	  if (is_known_folder (url, &folder))
-	    {
-	      folder->ref++;
-	      *pfolder = folder;
-	      mu_url_destroy (&url); /* FIXME: Hmm */
-	      mu_monitor_unlock (&folder_lock);
-	      return  0;
-	    }
-	  else
-	    mu_monitor_unlock (&folder_lock);
-	  
-	  /* Create a new folder.  */
-
-	  /* Allocate memory for the folder.  */
-	  folder = calloc (1, sizeof (*folder));
-	  if (folder != NULL)
-	    {
-	      folder->url = url;
-	      /* Initialize the internal foilder lock, now so the
-		 concrete folder could use it.  */
-	      status = mu_monitor_create (&folder->monitor, 0, folder);
-	      if (status == 0)
-		{
-		  /* Create the concrete folder type.  */
-		  status = f_init (folder);
-		  if (status == 0)
-		    {
-		      if (!folder->_match)
-			folder->_match = mu_folder_match;
-		      *pfolder = folder;
-		      folder->ref++;
-		      /* Put on the internal list of known folders.  */
-		      if (known_folder_list == NULL)
-			mu_list_create (&known_folder_list);
-		      mu_list_append (known_folder_list, folder);
-		    }
-		}
-	      /* Something went wrong, destroy the object. */
-	      if (status)
-		{
-		  if (folder->monitor)
-		    mu_monitor_destroy (&folder->monitor, folder);
-		  free (folder);
-		}
-	    }
-	  return status;
-	}
-    }
-
-    return MU_ERR_NOENT;
-}
-
-int
-mu_folder_create (mu_folder_t *pfolder, const char *name)
-{
-  int rc;
-  mu_url_t url;
-  
-  rc = mu_url_create (&url, name);
-  if (rc)
-    return rc;
-  rc = mu_url_parse (url);
-  if (rc == 0)
-    rc = mu_folder_create_from_record (pfolder, url, NULL);
-  if (rc)
-    mu_url_destroy (&url);
-  return rc;
-}
-
-/* The folder is destroy if it is the last reference.  */
-void
-mu_folder_destroy (mu_folder_t *pfolder)
-{
-  if (pfolder && *pfolder)
-    {
-      mu_folder_t folder = *pfolder;
-      int destroy_lock = 0;
-      mu_monitor_t monitor = folder->monitor;
-
-      mu_monitor_wrlock (monitor);
-
-      /* Check if this the last reference for this folder.  If yes removed
-         it from the list.  */
-      mu_monitor_wrlock (&folder_lock);
-      folder->ref--;
-      /* Remove the folder from the list of known folder.  */
-      if (folder->ref <= 0)
-	mu_list_remove (known_folder_list, folder);
-      /* If the list is empty we can safely remove it.  */
-      if (mu_list_is_empty (known_folder_list))
-	mu_list_destroy (&known_folder_list);
-
-      mu_monitor_unlock (&folder_lock);
-
-      if (folder->ref <= 0)
-	{
-	  mu_monitor_unlock (monitor);
-	  destroy_lock = 1;
-	  /* Notify the observers.  */
-	  if (folder->observable)
-	    {
-	      mu_observable_notify (folder->observable, MU_EVT_FOLDER_DESTROY,
-				    folder);
-	      mu_observable_destroy (&folder->observable, folder);
-	    }
-	  if (folder->_destroy)
-	    folder->_destroy (folder);
-	  mu_monitor_wrlock (monitor);
-	  if (folder->authority)
-	    mu_authority_destroy (&folder->authority, folder);
-	  if (folder->stream)
-	    mu_stream_destroy (&folder->stream);
-	  if (folder->url)
-	    mu_url_destroy (&folder->url);
-	  free (folder);
-	}
-      mu_monitor_unlock (monitor);
-      if (destroy_lock)
-	mu_monitor_destroy (&monitor, folder);
-      *pfolder = NULL;
-    }
-}
-
-/* Cover functions.  */
-int
-mu_folder_open (mu_folder_t folder, int flags)
-{
-  if (folder == NULL || folder->_open == NULL)
-    return ENOSYS;
-  return folder->_open (folder, flags);
-}
-
-int
-mu_folder_close (mu_folder_t folder)
-{
-  if (folder == NULL || folder->_close == NULL)
-    return ENOSYS;
-  return folder->_close (folder);
-}
-
-int
-mu_folder_set_stream (mu_folder_t folder, mu_stream_t stream)
-{
-  if (folder == NULL)
-    return EINVAL;
-  if (folder->stream)
-    mu_stream_destroy (&folder->stream);
-  folder->stream = stream;
-  return 0;
-}
-
-int
-mu_folder_get_stream (mu_folder_t folder, mu_stream_t *pstream)
-{
-  /* FIXME: Deprecation warning */
-  if (folder == NULL)
-    return EINVAL;
-  if (pstream == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-  *pstream = folder->stream;
-  return 0;
-}
-
-int
-mu_folder_get_streamref (mu_folder_t folder, mu_stream_t *pstream)
-{
-  if (folder == NULL)
-    return EINVAL;
-  if (pstream == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-  return mu_streamref_create (pstream, folder->stream);
-}
-
-int
-mu_folder_set_authority (mu_folder_t folder, mu_authority_t authority)
-{
-  if (folder == NULL)
-    return EINVAL;
-  if (folder->authority)
-    mu_authority_destroy (&folder->authority, folder);
-  folder->authority = authority;
-  return 0;
-}
-
-int
-mu_folder_get_authority (mu_folder_t folder, mu_authority_t *pauthority)
-{
-  if (folder == NULL)
-    return EINVAL;
-  if (pauthority == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-  *pauthority = folder->authority;
-  return 0;
-}
-
-int
-mu_folder_get_observable (mu_folder_t folder, mu_observable_t *pobservable)
-{
-  if (folder == NULL)
-    return EINVAL;
-  if (pobservable == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-
-  if (folder->observable == NULL)
-    {
-      int status = mu_observable_create (&folder->observable, folder);
-      if (status != 0)
-        return status;
-    }
-  *pobservable = folder->observable;
-  return 0;
-}
-
-int
-mu_folder_set_match (mu_folder_t folder, mu_folder_match_fp pmatch)
-{
-  if (folder == NULL)
-    return EINVAL;
-  folder->_match = pmatch;
-  return 0;
-}
-
-int
-mu_folder_get_match (mu_folder_t folder, mu_folder_match_fp *pmatch)
-{
-  if (folder == NULL)
-    return EINVAL;
-  if (pmatch == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-  *pmatch = folder->_match;
-  return 0;
-}
-
-int
-mu_folder_has_debug (mu_folder_t folder)
-{
-  if (folder == NULL)
-    return 0;
-
-  return folder->debug ? 1 : 0;
-}
-
-int
-mu_folder_set_debug (mu_folder_t folder, mu_debug_t debug)
-{
-  if (folder == NULL)
-    return EINVAL;
-  if (folder->debug)
-    mu_debug_destroy (&folder->debug, folder);
-  folder->debug = debug;
-  return 0;
-}
-
-int
-mu_folder_get_debug (mu_folder_t folder, mu_debug_t *pdebug)
-{
-  if (folder == NULL)
-    return EINVAL;
-  if (pdebug == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-  if (folder->debug == NULL)
-    {
-      int status = mu_debug_create (&folder->debug, folder);
-      if (status != 0)
-        return status;
-    }
-  *pdebug = folder->debug;
-  return 0;
-}
-
-void
-mu_list_response_free (void *data)
-{
-  struct mu_list_response *f = data;
-  free (f->name);
-  free (f);
-}
-
-int
-mu_folder_list (mu_folder_t folder, const char *dirname, void *pattern,
-		size_t max_level,
-		mu_list_t *pflist)
-{
-  return mu_folder_enumerate (folder, dirname, pattern, 0, max_level,
-			      pflist, NULL, NULL);
-}
-
-int
-mu_folder_enumerate (mu_folder_t folder, const char *name,
-		     void *pattern, int flags,
-		     size_t max_level,
-		     mu_list_t *pflist,
-		     mu_folder_enumerate_fp enumfun, void *enumdata)
-{
-  int status;
-  if (folder == NULL || folder->_list == NULL)
-    return EINVAL;
-  else
-    {
-      mu_list_t list = NULL;
-      
-      if (pflist)
-	{
-	  status = mu_list_create (&list);
-	  if (status)
-	    return status;
-	  *pflist = list;
-	  mu_list_set_destroy_item (list, mu_list_response_free);
-	}
-      else if (!enumfun)
-	return EINVAL;
-      
-      status = folder->_list (folder, name, pattern, flags, max_level,
-			      list, enumfun, enumdata);
-      if (status)
-	mu_list_destroy (pflist);
-    }
-  return status;
-}
-
-int
-mu_folder_lsub (mu_folder_t folder, const char *dirname, const char *basename,
-		mu_list_t *pflist)
-{
-  int status;
-  
-  if (folder == NULL || folder->_lsub == NULL)
-    return ENOSYS;
-  else
-    {
-      status = mu_list_create (pflist);
-      if (status)
-	return status;
-      mu_list_set_destroy_item (*pflist, mu_list_response_free);
-      status = folder->_lsub (folder, dirname, basename, *pflist);
-    }
-  return status;
-}
-
-int
-mu_folder_subscribe (mu_folder_t folder, const char *name)
-{
-  if (folder == NULL || folder->_subscribe == NULL)
-    return EINVAL;
-  return folder->_subscribe (folder, name);
-}
-
-int
-mu_folder_unsubscribe (mu_folder_t folder, const char *name)
-{
-  if (folder == NULL || folder->_unsubscribe == NULL)
-    return EINVAL;
-  return folder->_unsubscribe (folder, name);
-}
-
-int
-mu_folder_delete (mu_folder_t folder, const char *name)
-{
-  if (folder == NULL || folder->_delete == NULL)
-    return ENOSYS;
-  return folder->_delete (folder, name);
-}
-
-int
-mu_folder_rename (mu_folder_t folder, const char *oldname, const char *newname)
-{
-  if (folder == NULL || folder->_rename == NULL)
-    return ENOSYS;
-  return folder->_rename (folder, oldname, newname);
-}
-
-int
-mu_folder_get_url (mu_folder_t folder, mu_url_t *purl)
-{
-  if (folder == NULL)
-    return EINVAL;
-  if (purl == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-  *purl = folder->url;
-  return 0;
-}
-
-static int
-is_known_folder (mu_url_t url, mu_folder_t *pfolder)
-{
-  int ret = 0;
-  mu_folder_t folder = NULL;
-  mu_iterator_t iterator;
-
-  if (url == NULL || pfolder == NULL)
-    return ret;
-
-  if (mu_list_get_iterator (known_folder_list, &iterator) != 0)
-    return ret;
-
-  for (mu_iterator_first (iterator); !mu_iterator_is_done (iterator);
-       mu_iterator_next (iterator))
-    {
-      mu_iterator_current (iterator, (void **)&folder);
-      /* Check if the same URL type.  */
-      if (folder && folder->url
-	  && mu_url_is_same_scheme (url, folder->url)
-	  && mu_url_is_same_user (url, folder->url)
-	  && mu_url_is_same_host (url, folder->url)
-	  && mu_url_is_same_path (url, folder->url) 
-	  && mu_url_is_same_port (url, folder->url))
-	{
-	  ret = 1;
-	  break;
-	}
-    }
-  if (ret)
-    *pfolder = folder;
-  mu_iterator_destroy (&iterator);
-  return ret;
-}
-
diff --git a/libmailutils/freeitem.c b/libmailutils/freeitem.c
deleted file mode 100644
index c268ea4..0000000
--- a/libmailutils/freeitem.c
+++ /dev/null
@@ -1,29 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 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>
-
-/* Default destroy_item function. */
-void
-mu_list_free_item (void *item)
-{
-  free (item);
-}
diff --git a/libmailutils/fromflt.c b/libmailutils/fromflt.c
deleted file mode 100644
index d96d46d..0000000
--- a/libmailutils/fromflt.c
+++ /dev/null
@@ -1,283 +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 <string.h>
-#include <mailutils/errno.h>
-#include <mailutils/filter.h>
-#include <mailutils/stream.h>
-
-enum from_decode_state
-  {
-    from_decode_init,
-    from_decode_nl,
-    from_decode_char
-  };
-
-#define GT_FROM_MARK_STR ">From "
-#define GT_FROM_MARK_LEN (sizeof (GT_FROM_MARK_STR) - 1)
-
-/* Move min(isize,osize) bytes from iptr to optr, replacing each '>From '
-   at the beginning of line with 'From '. */
-static enum mu_filter_result
-_from_decoder (void *xd,
-	       enum mu_filter_command cmd,
-	       struct mu_filter_io *iobuf)
-{
-  int *pstate = xd;
-  const unsigned char *iptr;
-  size_t isize;
-  char *optr;
-  size_t osize;
-  enum from_decode_state state;
-  size_t i, j;
-
-  switch (cmd)
-    {
-    case mu_filter_init:
-      *pstate = from_decode_init;
-      return mu_filter_ok;
-      
-    case mu_filter_done:
-      return mu_filter_ok;
-      
-    default:
-      state = *pstate;
-      break;
-    }
-  
-  iptr = (const unsigned char *) iobuf->input;
-  isize = iobuf->isize;
-  optr = iobuf->output;
-  osize = iobuf->osize;
-
-  for (i = j = 0; i < isize && j < osize; i++)
-    {
-      unsigned char c = *iptr++;
-      
-      if (c == '\n')
-	state = from_decode_nl;
-      else if (state == from_decode_init || state == from_decode_nl)
-	{
-	  size_t len = isize - i;
-
-	  if (len < GT_FROM_MARK_LEN)
-	    {
-	      if (memcmp (iptr - 1, GT_FROM_MARK_STR, len) == 0)
-		{
-		  if (i == 0)
-		    {
-		      iobuf->isize = GT_FROM_MARK_LEN - len;
-		      return mu_filter_moreinput;
-		    }
-		  break;
-		}
-	      else
-		state = from_decode_char;
-	    }
-	  else if (memcmp (iptr - 1, GT_FROM_MARK_STR, GT_FROM_MARK_LEN) == 0)
-	    {
-	      /* Skip > */
-	      state = from_decode_char;
-	      continue;
-	    }
-	}	  
-      optr[j++] = c;
-    }
-
-  *pstate = state;
-  iobuf->isize = i;
-  iobuf->osize = j;
-  return mu_filter_ok;
-}
-
-#define FROM_MARK_STR "From "
-#define FROM_MARK_LEN (sizeof (FROM_MARK_STR) - 1)
-
-enum from_encode_state
-  {
-    from_encode_init,
-    from_encode_nl,
-    from_encode_char,
-    from_encode_gt,
-    from_encode_f,
-    from_encode_r,
-    from_encode_o,
-    from_encode_m,
-    from_encode_sp
-  };
-
-static int length_to_state_tab[] = {
-  from_encode_gt,
-  from_encode_f,
-  from_encode_r,
-  from_encode_o,
-  from_encode_m,
-  from_encode_sp
-};
-
-static int state_to_length_tab[] = {
-  0, 0, 0,
-  GT_FROM_MARK_LEN,
-  GT_FROM_MARK_LEN-1,
-  GT_FROM_MARK_LEN-2,
-  GT_FROM_MARK_LEN-3,
-  GT_FROM_MARK_LEN-4,
-  GT_FROM_MARK_LEN-5
-};      
-
-/* Move min(isize,osize) bytes from iptr to optr, replacing each 'From '
-   at the beginning of line with '>From '. */
-
-static enum mu_filter_result
-_from_encoder (void *xd,
-	       enum mu_filter_command cmd,
-	       struct mu_filter_io *iobuf)
-{
-  int *pstate = xd;
-  const unsigned char *iptr;
-  size_t isize;
-  char *optr;
-  size_t osize;
-  enum from_encode_state state;
-  size_t i, j;
-  
-  switch (cmd)
-    {
-    case mu_filter_init:
-      *pstate = from_encode_init;
-      return mu_filter_ok;
-      
-    case mu_filter_done:
-      return mu_filter_ok;
-      
-    default:
-      state = *pstate;
-      switch (state)
-	{
-	case from_encode_init:
-	case from_encode_nl:
-	case from_encode_char:
-	  break;
-
-	default:
-	  osize = state_to_length_tab[state];
-	  if (iobuf->osize < osize)
-	    {
-	      iobuf->osize = osize;
-	      return mu_filter_moreoutput;
-	    }
-	  memcpy (iobuf->output, GT_FROM_MARK_STR + GT_FROM_MARK_LEN - osize,
-		  osize);
-	  iobuf->osize = osize;
-	  iobuf->isize = osize;
-	  *pstate = from_encode_init;
-	  return mu_filter_ok;
-	}
-      break;
-    }
-  
-  iptr = (const unsigned char *) iobuf->input;
-  isize = iobuf->isize;
-  optr = iobuf->output;
-  osize = iobuf->osize;
-
-  for (i = j = 0; i < isize && j < osize; i++)
-    {
-      unsigned char c = *iptr++;
-      
-      if (c == '\n')
-	state = from_encode_nl;
-      else if (state == from_encode_init || state == from_encode_nl)
-	{
-	  size_t len = isize - i;
-
-	  if (len < FROM_MARK_LEN)
-	    {
-	      if (memcmp (iptr - 1, FROM_MARK_STR, len) == 0)
-		{
-		  if (i == 0)
-		    {
-		      iobuf->isize = FROM_MARK_LEN;
-		      return mu_filter_moreinput;
-		    }
-		  break;
-		}
-	      else
-		state = from_encode_char;
-	    }
-	  else if (memcmp (iptr - 1, FROM_MARK_STR, FROM_MARK_LEN) == 0)
-	    {
-	      size_t rest = osize - j;
-	      
-	      if (rest > GT_FROM_MARK_LEN)
-		rest = GT_FROM_MARK_LEN;
-	      else if (rest < 2)
-		{
-		  if (i == 0)
-		    {
-		      iobuf->osize = GT_FROM_MARK_LEN;
-		      return mu_filter_moreoutput;
-		    }
-		  break;
-		}
-	      
-	      memcpy (optr + j, GT_FROM_MARK_STR, rest);
-	      i += rest - 2;
-	      iptr += rest - 2;
-	      j += rest;
-	      if (rest < GT_FROM_MARK_LEN)
-		state = length_to_state_tab[rest];
-	      else
-		state = from_encode_char;
-	      continue;
-	    }
-	  else
-	    state = from_encode_char;
-	}
-      optr[j++] = c;
-    }
-  *pstate = state;
-  iobuf->isize = i;
-  iobuf->osize = j;
-  return mu_filter_ok;
-}
-
-static int
-_from_alloc_state (void **pret, int mode, void *data MU_ARG_UNUSED)
-{
-  *pret = malloc (sizeof (int));
-  if (!*pret)
-    return ENOMEM;
-  return 0;
-}
-
-static struct _mu_filter_record _from_filter = {
-  "FROM",
-  0,
-  _from_alloc_state,
-  _from_encoder,
-  _from_decoder
-};
-
-mu_filter_record_t mu_from_filter = &_from_filter;
-
diff --git a/libmailutils/gdebug.c b/libmailutils/gdebug.c
deleted file mode 100644
index 3d1a54c..0000000
--- a/libmailutils/gdebug.c
+++ /dev/null
@@ -1,263 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 1999, 2000, 2001, 2004, 2005, 2007, 2008, 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 <errno.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-#include <mailutils/cctype.h>
-#include <mailutils/assoc.h>
-#include <mailutils/error.h>
-#include <mailutils/errno.h>
-#include <mailutils/argcv.h>
-#include <mailutils/debug.h>
-#include <mailutils/cfg.h>
-#include <mailutils/nls.h>
-
-int mu_debug_line_info = 0;
-
-struct debug_level
-{
-  unsigned level;
-};
-
-static mu_assoc_t debug_table;
-
-mu_log_level_t
-mu_global_debug_level (const char *object_name)
-{
-  struct debug_level *p = mu_assoc_ref (debug_table, object_name);
-  if (p)
-    return p->level;
-  return 0;
-}
-
-int
-mu_global_debug_set_level (const char *object_name, mu_log_level_t level)
-{
-  int rc;
-  struct debug_level *dbg;
-  
-  if (!debug_table)
-    {
-      rc = mu_assoc_create (&debug_table, sizeof(struct debug_level), 0);
-      if (rc)
-	return rc;
-    }
-
-  rc = mu_assoc_ref_install (debug_table, object_name, (void**) &dbg);
-  if (rc == 0 || rc == MU_ERR_EXISTS)
-    dbg->level = level;
-  return rc;
-}
-
-int
-mu_global_debug_clear_level (const char *object_name)
-{
-  int rc = 0;
-  
-  if (!object_name)
-    mu_assoc_clear (debug_table);
-  else
-    rc = mu_assoc_remove (debug_table, object_name);
-  return rc;
-}
-
-int
-decode_debug_level (const char *p, int *lev)
-{
-  if (strcmp (p, "error") == 0)
-    *lev = MU_DEBUG_ERROR;
-  else if (strncmp (p, "trace", 5) == 0 && mu_isdigit (p[5]) && p[6] == 0)
-    *lev = MU_DEBUG_TRACE0 + atoi (p + 5);
-  else if (strcmp (p, "proto") == 0)
-    *lev = MU_DEBUG_PROT;
-  else
-    return 1;
-  return 0;
-}
-
-int
-mu_debug_level_from_string (const char *string, mu_log_level_t *plev,
-			    mu_debug_t debug)
-{
-  char *q;
-  unsigned level = MU_DEBUG_INHERIT;
-  
-  if (mu_isdigit (*string))
-    {
-      level = strtoul (string, &q, 0);
-      if (*q)
-	{
-	  mu_cfg_format_error (debug, MU_DEBUG_ERROR,
-			       _("invalid debugging specification `%s': "
-				 "expected levels or number after `=', "
-				 "but found `%s'"),
-			       string, string);
-	  return MU_ERR_FAILURE;
-	}
-    }
-  else
-    {
-      char *p = strdup (string);
-      size_t len = strlen (p);
-      if (len > 0 && p[len-1] == '\n')
-	p[len-1] = 0;
-      for (q = strtok (p, ","); q; q = strtok (NULL, ","))
-	{
-	  int flag;
-	  int revert = 0;
-	  int upto = 0;
-	  
-	  if (*q == '!')
-	    {
-	      q++;
-	      revert = 1;
-	    }
-	  if (*q == '<')
-	    {
-	      q++;
-	      upto = 1;
-	    }
-	  
-	  if (decode_debug_level (q, &flag))
-	    mu_cfg_format_error (debug, MU_DEBUG_ERROR,
-				 _("invalid debugging level `%s'"),
-				 q);
-	  else if (revert)
-	    {
-	      if (upto)
-		level &= ~MU_DEBUG_LEVEL_UPTO (flag);
-	      else
-		level &= ~MU_DEBUG_LEVEL_MASK (flag);
-	    }
-	  else
-	    {
-	      if (upto)
-		level |= MU_DEBUG_LEVEL_UPTO (flag);
-	      else
-		level |= MU_DEBUG_LEVEL_MASK (flag);
-	    }
-	}
-      free (p);
-    }
-  *plev = level;
-  return 0;
-}
-
-int
-mu_global_debug_from_string (const char *string, const char *errpfx)
-{
-  int rc;
-  int argc;
-  char **argv;
-  int i;
-  
-  rc = mu_argcv_get (string, ";", NULL, &argc, &argv);
-  if (rc)
-    return rc;
-
-  for (i = 0; i < argc; i++)
-    {
-      char *p;
-      mu_log_level_t level = MU_DEBUG_INHERIT;
-      char *object_name = argv[i];
-      
-      for (p = object_name; *p && *p != '='; p++)
-	;
-
-      if (*p == '=')
-	{
-	  /* FIXME: Use mu_debug_level_from_string */
-	  char *q;
-	  
-	  *p++ = 0;
-	  if (mu_isdigit (*p))
-	    {
-	      level = strtoul (p, &q, 0);
-	      if (*q)
-		{
-		  mu_error ("%s: invalid debugging specification `%s': "
-			    "expected levels or number after `=', "
-			    "but found `%s'",
-			    errpfx, argv[i], p);
-		  break;
-		}
-	    }
-	  else
-	    {
-	      char *q;
-	      for (q = strtok (p, ","); q; q = strtok (NULL, ","))
-		{
-		  int flag;
-		  int revert = 0;
-		  int upto = 0;
-		  
-		  if (*q == '!')
-		    {
-		      q++;
-		      revert = 1;
-		    }
-		  if (*q == '<')
-		    {
-		      q++;
-		      upto = 1;
-		    }
-		  
-		  if (decode_debug_level (q, &flag))
-		    mu_error ("%s: invalid debugging level `%s'",
-			      errpfx, q);
-		  else if (revert)
-		    {
-		      if (upto)
-			level &= ~MU_DEBUG_LEVEL_UPTO (flag);
-		      else
-			level &= ~MU_DEBUG_LEVEL_MASK (flag);
-		    }
-		  else
-		    {
-		      if (upto)
-			level |= MU_DEBUG_LEVEL_UPTO (flag);
-		      else
-			level |= MU_DEBUG_LEVEL_MASK (flag);
-		    }
-		}
-	    }   
-	}	  
-      else
-	level |= MU_DEBUG_LEVEL_UPTO (MU_DEBUG_PROT);
-      
-      if (p[-1] == ':')
-	{
-	  p[-1] = 0;
-	  level &= ~MU_DEBUG_INHERIT;
-	}
-      
-      mu_global_debug_set_level (object_name, level);
-    }
-  
-  mu_argcv_free (argc, argv);
-  return 0;
-}
-
-
diff --git a/libmailutils/gocs.c b/libmailutils/gocs.c
deleted file mode 100644
index 40bde9d..0000000
--- a/libmailutils/gocs.c
+++ /dev/null
@@ -1,397 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 2007, 2008, 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 <stdlib.h>
-#include <string.h>
-#include <mailutils/types.h>
-#include <mailutils/gocs.h>
-#include <mailutils/mailbox.h>
-#include <mailutils/locker.h>
-#include <mailutils/mutil.h>
-#include <mailutils/mailer.h>
-#include <mailutils/error.h>
-#include <mailutils/errno.h>
-#include <mailutils/nls.h>
-#include <mailutils/debug.h>
-#include <mailutils/syslog.h>
-#include <mailutils/registrar.h>
-#include <syslog.h>
-
-int mu_load_user_rcfile = 1;
-int mu_load_site_rcfile = 1;
-char *mu_load_rcfile = NULL;
-
-
-int
-mu_gocs_dummy (enum mu_gocs_op op, void *data)
-{
-  return 0;
-}
-
-int
-mu_gocs_mailbox_init (enum mu_gocs_op op, void *data)
-{
-  int rc;
-  struct mu_gocs_mailbox *p = data;
-
-  if (op == mu_gocs_op_set && p)
-    {
-      if (p->mail_spool)
-	{
-	  rc = mu_set_mail_directory (p->mail_spool);
-	  if (rc)
-	    mu_error (_("cannot set mail directory name to `%s': %s"),
-		      p->mail_spool, mu_strerror (rc));
-	  free (p->mail_spool);
-	  p->mail_spool = NULL;
-	}
-      if (p->mailbox_pattern)
-	{
-	  rc = mu_set_mailbox_pattern (p->mailbox_pattern);
-	  if (rc)
-	    mu_error (_("cannot set mailbox pattern to `%s': %s"),
-		      p->mailbox_pattern, mu_strerror (rc));
-	  free (p->mailbox_pattern);
-	  p->mailbox_pattern = NULL;
-	}
-      if (p->mailbox_type)
-	{
-	  if (mu_registrar_set_default_scheme (p->mailbox_type))
-	    mu_error (_("invalid mailbox type: %s"), p->mailbox_type);
-	  free (p->mailbox_type);
-	  p->mailbox_type = NULL;
-	}
-    }
-  return 0;
-}
-
-int
-mu_gocs_locking_init (enum mu_gocs_op op, void *data)
-{
-  struct mu_gocs_locking *p = data;
-  
-  if (!(op == mu_gocs_op_set && p))
-    return 0;
-
-  if (p->lock_flags)
-    {
-      int flags = 0;
-      char *s;
-      
-      for (s = p->lock_flags; *s; s++)
-	{
-	  switch (*s)
-	    {
-	    case 'E':
-	      flags |= MU_LOCKER_EXTERNAL;
-	      break;
-	      
-	    case 'R':
-	      flags |= MU_LOCKER_RETRY;
-	      break;
-	      
-	    case 'T':
-	      flags |= MU_LOCKER_TIME;
-	      break;
-	      
-	    case 'P':
-	      flags |= MU_LOCKER_PID;
-	      break;
-	      
-	    default:
-	      mu_error (_("invalid lock flag `%c'"), *s);
-	    }
-	}
-      mu_locker_set_default_flags (flags, mu_locker_assign);
-      free (p->lock_flags);
-      p->lock_flags = NULL;
-    }
-
-  if (p->lock_retry_count)
-    {
-      mu_locker_set_default_retry_count (p->lock_retry_count);
-      mu_locker_set_default_flags (MU_LOCKER_RETRY, mu_locker_set_bit);
-      p->lock_retry_count = 0;
-    }
-
-  if (p->lock_retry_timeout)
-    {
-      mu_locker_set_default_retry_timeout (p->lock_retry_timeout);
-      mu_locker_set_default_flags (MU_LOCKER_RETRY, mu_locker_set_bit);
-      p->lock_retry_timeout = 0;
-    }
-
-  if (p->lock_expire_timeout)
-    {
-      mu_locker_set_default_expire_timeout (p->lock_expire_timeout);
-      mu_locker_set_default_flags (MU_LOCKER_EXTERNAL, mu_locker_set_bit);
-      p->lock_expire_timeout = 0;
-    }
-
-  if (p->external_locker)
-    {
-      mu_locker_set_default_external_program (p->external_locker);
-      mu_locker_set_default_flags (MU_LOCKER_TIME, mu_locker_set_bit);
-      free (p->external_locker);
-      p->external_locker = NULL;
-    }
-  return 0;
-}
-
-int
-mu_gocs_source_email_init (enum mu_gocs_op op, void *data)
-{
-  struct mu_gocs_source_email *p = data;
-  int rc;
-
-  if (!(op == mu_gocs_op_set && p))
-    return 0;
-  
-  if (p->address)
-    {
-      if ((rc = mu_set_user_email (p->address)) != 0)
-	mu_error (_("invalid email address `%s': %s"),
-		  p->address, mu_strerror (rc));
-      free (p->address);
-      p->address = NULL;
-    }
-
-  if (p->domain)
-    {
-      if ((rc = mu_set_user_email_domain (p->domain)) != 0)
-	mu_error (_("invalid email domain `%s': %s"),
-		  p->domain, mu_strerror (rc));
-
-      free (p->domain);
-      p->domain = NULL;
-    }
-  return 0;
-}
-
-int
-mu_gocs_mailer_init (enum mu_gocs_op op, void *data)
-{
-  struct mu_gocs_mailer *p = data;
-  int rc;
-
-  if (!(op == mu_gocs_op_set && p))
-    return 0;
-  
-  if (p->mailer)
-    {
-      if ((rc = mu_mailer_set_url_default (p->mailer)) != 0)
-	mu_error (_("invalid mailer URL `%s': %s"),
-		  p->mailer, mu_strerror (rc));
-      free (p->mailer);
-      p->mailer = NULL;
-    }
-  return 0;
-}
-
-int
-mu_gocs_logging_init (enum mu_gocs_op op, void *data)
-{
-  struct mu_gocs_logging *p = data;
-
-  if (op == mu_gocs_op_set)
-    {
-      if (!p)
-	{
-	  static struct mu_gocs_logging default_gocs_logging = { LOG_FACILITY };
-	  p = &default_gocs_logging;
-	}
-  
-      if (p->facility)
-	{
-	  mu_log_facility = p->facility;
-	  mu_debug_default_printer = mu_debug_syslog_printer;
-	}
-      else
-	mu_debug_default_printer = mu_debug_stderr_printer;
-
-      if (p->tag)
-	mu_log_tag = strdup (p->tag);
-    }
-  return 0;
-}
-
-int
-mu_gocs_debug_init (enum mu_gocs_op op, void *data)
-{
-  if (op == mu_gocs_op_set && data)
-    {
-      struct mu_gocs_debug *p = data;
-      if (p->string && p->errpfx)
-	{
-	  mu_global_debug_from_string (p->string, p->errpfx);
-	  free (p->errpfx);
-	}
-      if (p->line_info >= 0)
-	mu_debug_line_info = p->line_info;
-    }
-  return 0;
-}
-
-
-struct mu_gocs_entry
-{
-  const char *name;
-  mu_gocs_init_fp init;
-};
-
-#define MAX_GOCS 512
-
-static struct mu_gocs_entry _gocs_table[MAX_GOCS];
-
-void
-mu_gocs_register (const char *capa, mu_gocs_init_fp init)
-{
-  int i;
-  for (i = 0; _gocs_table[i].name; i++)
-    if (i == MAX_GOCS-1)
-      {
-	mu_error (_("gocs table overflow"));
-	abort ();
-      }
-  _gocs_table[i].name = capa;
-  _gocs_table[i].init = init;
-}
-
-int
-mu_gocs_enumerate (mu_list_action_t action, void *data)
-{
-  int i;
-  
-  for (i = 0; _gocs_table[i].name; i++)
-    {
-      int rc = action ((void*) _gocs_table[i].name, data);
-      if (rc)
-	return rc;
-    }
-  return 0;
-}
-
-static mu_gocs_init_fp
-find_init_function (struct mu_gocs_entry *tab, const char *capa)
-{
-  for (; tab->name; tab++)
-    if (strcmp (tab->name, capa) == 0)
-      return tab->init;
-  return NULL;
-}
-
-static struct mu_gocs_entry std_gocs_table[] = {
-  { "common", mu_gocs_dummy },
-  { "license", mu_gocs_dummy },
-  { "mailbox", mu_gocs_mailbox_init },
-  { "locking", mu_gocs_locking_init },
-  { "address", mu_gocs_source_email_init },
-  { "mailer", mu_gocs_mailer_init },
-  { "logging", mu_gocs_logging_init },
-  { "debug", mu_gocs_debug_init },
-  { "auth", mu_gocs_dummy },
-  { NULL }
-};
-
-void
-mu_gocs_register_std (const char *name)
-{
-  mu_gocs_init_fp init = find_init_function (std_gocs_table, name);
-  if (!init)
-    {
-      mu_error (_("INTERNAL ERROR at %s:%d: unknown standard capability `%s'"),
-		__FILE__, __LINE__, name);
-      abort ();
-    }
-  mu_gocs_register (name, init);
-}
-
-
-struct mu_gocs_data
-{
-  char *capa;
-  void *data;
-};
-
-static mu_list_t /* of struct mu_gocs_data */ data_list;
-
-static int
-_gocs_comp (const void *a, const void *b)
-{
-  const struct mu_gocs_data *da = a, *db = b;
-  return !(strcmp (da->capa, db->capa) == 0 && da->data == db->data);
-}
-
-void
-mu_gocs_store (char *capa, void *data)
-{
-  struct mu_gocs_data *s;
-  if (!data_list)
-    {
-      mu_list_create (&data_list);
-      mu_list_set_destroy_item (data_list, mu_list_free_item);
-      mu_list_set_comparator (data_list, _gocs_comp);
-    }
-  s = malloc (sizeof *s);
-  if (!s)
-    {
-      mu_error ("%s", mu_strerror (ENOMEM));
-      exit (1);
-    }
-  s->capa = capa;
-  s->data = data;
-  if (mu_list_locate (data_list, s, NULL) == 0)
-    free (s);
-  else
-    mu_list_prepend (data_list, s);
-}
-
-int
-_gocs_flush (void *item, void *data)
-{
-  struct mu_gocs_data *s = item;
-  mu_gocs_init_fp initfun = find_init_function (_gocs_table, s->capa);
-
-  if (!initfun)
-    {
-      mu_error (_("INTERNAL ERROR at %s:%d: unknown capability `%s'"),
-		__FILE__, __LINE__, s->capa);
-      abort ();
-    }
-
-  if (initfun (mu_gocs_op_set, s->data))
-    {
-      mu_error (_("initialization of GOCS `%s' failed"), s->capa);
-      return 1;
-    }
-  
-  return 0;
-}
-
-void
-mu_gocs_flush ()
-{
-  int i;
-  mu_list_do (data_list, _gocs_flush, NULL);
-
-  for (i = 0; _gocs_table[i].name; i++)
-    _gocs_table[i].init (mu_gocs_op_flush, NULL);
-}
diff --git a/libmailutils/hdritr.c b/libmailutils/hdritr.c
deleted file mode 100644
index 595735f..0000000
--- a/libmailutils/hdritr.c
+++ /dev/null
@@ -1,162 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 2008, 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 */
-
-/* Mail header iterators. */
-
-#ifdef HAVE_CONFIG_H
-# include <config.h>
-#endif
-#include <stdlib.h>
-#include <string.h>
-
-#include <mailutils/sys/header.h>
-#include <mailutils/errno.h>
-
-struct header_iterator
-{
-  mu_header_t header;
-  size_t index;
-};
-
-static int
-hdr_first (void *owner)
-{
-  struct header_iterator *itr = owner;
-  itr->index = 1;
-  return 0;
-}
-
-static int
-hdr_next (void *owner)
-{
-  struct header_iterator *itr = owner;
-  itr->index++;
-  return 0;
-}
-
-static int
-hdr_getitem (void *owner, void **pret, const void **pkey)
-{
-  struct header_iterator *itr = owner;
-  int rc;
-  size_t count;
-
-  rc = mu_header_get_field_count (itr->header, &count);
-  if (rc)
-    return rc;
-  if (itr->index > count)
-    return MU_ERR_NOENT;
-  
-  rc = mu_header_sget_field_name (itr->header, itr->index,
-				  (const char**) pkey);
-  if (rc == 0)
-    {
-      if (pkey)
-	rc = mu_header_sget_field_value (itr->header, itr->index,
-					 (const char**) pret);
-    }
-  return rc;
-}
-
-static int
-hdr_finished_p (void *owner)
-{
-  struct header_iterator *itr = owner;
-  size_t count;
-
-  if (mu_header_get_field_count (itr->header, &count))
-    return 1;
-  return itr->index > count;
-}
-
-static int
-hdr_destroy (mu_iterator_t iterator, void *data)
-{
-  struct header_iterator *itr = data;
-  mu_iterator_detach (&itr->header->itr, iterator);
-  free (data);
-  return 0;
-}
-
-static int
-hdr_curitem_p (void *owner, void *item)
-{
-  void *ptr;
-
-  if (hdr_getitem (owner, &ptr, NULL))
-    return 0;
-  return ptr == item;
-}
-
-static int
-hdr_data_dup (void **ptr, void *owner)
-{
-  struct header_iterator *itr = owner;
-
-  *ptr = malloc (sizeof (struct header_iterator));
-  if (*ptr == NULL)
-    return ENOMEM;
-  memcpy (*ptr, owner, sizeof (struct header_iterator));
-  mu_iterator_attach (&itr->header->itr, *ptr);
-  return 0;
-}
-
-int
-mu_header_get_iterator (mu_header_t hdr, mu_iterator_t *piterator)
-{
-  mu_iterator_t iterator;
-  int status;
-  struct header_iterator *itr;
-
-  if (!hdr)
-    return EINVAL;
-
-  itr = calloc (1, sizeof *itr);
-  if (!itr)
-    return ENOMEM;
-  itr->header = hdr;
-  itr->index = 1;
-
-  status = mu_iterator_create (&iterator, itr);
-  if (status)
-    {
-      free (itr);
-      return status;
-    }
-
-  mu_iterator_set_first (iterator, hdr_first);
-  mu_iterator_set_next (iterator, hdr_next);
-  mu_iterator_set_getitem (iterator, hdr_getitem);
-  mu_iterator_set_finished_p (iterator, hdr_finished_p);
-  mu_iterator_set_curitem_p (iterator, hdr_curitem_p);
-  mu_iterator_set_destroy (iterator, hdr_destroy);
-  mu_iterator_set_dup (iterator, hdr_data_dup);
-
-  mu_iterator_attach (&hdr->itr, iterator);
-
-  *piterator = iterator;
-  return 0;
-}
-
-
-
-
-
-
-
-
diff --git a/libmailutils/header.c b/libmailutils/header.c
deleted file mode 100644
index 7c95e08..0000000
--- a/libmailutils/header.c
+++ /dev/null
@@ -1,1222 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 1999, 2000, 2001, 2004, 2005, 2007, 2008, 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 */
-
-/*  This all header business needs a good rewrite.
- *          -- Alain Magloire, 2000-07-03 (rev. 1.21)
- *
- *  It's the job that's never started as takes longest to finish.
- *          -- Hamfast Gamgee, some time in the Third Age
- *
- *  It took almost 7 years to gather the courage to start the job,
- *  and only one day to finish it.
- *          -- Sergey Poznyakoff, 2007-06-24
- */
-
-#ifdef HAVE_CONFIG_H
-# include <config.h>
-#endif
-
-#include <string.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <errno.h>
-
-#ifdef HAVE_STRINGS_H
-# include <strings.h>
-#endif
-
-#include <mailutils/stream.h>
-#include <mailutils/address.h>
-#include <mailutils/mutil.h>
-#include <mailutils/errno.h>
-#include <mailutils/cstr.h>
-#include <mailutils/sys/header_stream.h>
-#include <mailutils/sys/header.h>
-
-#define HEADER_MODIFIED   0x01
-#define HEADER_INVALIDATE 0x02
-
-#define HEADER_SET_MODIFIED(h) \
-  ((h)->flags |= (HEADER_MODIFIED|HEADER_INVALIDATE))
-
-
-/* mu_hdrent manipulation */
-
-#define MU_HDRENT_NAME(hp,ep) ((hp)->spool + (ep)->fn)
-#define MU_HDRENT_VALUE(hp,ep) ((hp)->spool + (ep)->fv)
-#define MU_STR_SIZE(nlen,vlen) ((nlen) + 2 + (vlen) + 1)
-
-static struct mu_hdrent *
-mu_hdrent_nth (struct _mu_header *hdr, int n)
-{
-  struct mu_hdrent *p;
-  for (p = hdr->head; p; p = p->next)
-    if (n-- == 1)
-      break;
-  return p;
-}
-
-static struct mu_hdrent *
-mu_hdrent_find (struct _mu_header *hdr, const char *name, int pos)
-{
-  struct mu_hdrent *p;
-
-  if (pos > 0)
-    {
-      for (p = hdr->head; p; p = p->next)
-	if (mu_c_strcasecmp (MU_HDRENT_NAME (hdr,p), name) == 0 && pos-- == 1)
-	  break;
-    }
-  else if (pos < 0)
-    {
-      for (p = hdr->tail; p; p = p->prev)
-	if (mu_c_strcasecmp (MU_HDRENT_NAME (hdr,p), name) == 0 && ++pos == 0)
-	  break;
-    }
-  else
-    p = NULL;
-  return p;
-}
-
-static int
-mu_hdrent_find_stream_pos (struct _mu_header *hdr, mu_off_t pos,
-			   struct mu_hdrent **pent, size_t *poff)
-{
-  mu_off_t x;
-  struct mu_hdrent *p;
-
-  for (p = hdr->head, x = 0; p; p = p->next)
-    {
-      size_t strsize = MU_STR_SIZE (p->nlen, p->vlen);
-      if (x <= pos && pos < x + strsize)
-	{
-	  *poff = pos - x;
-	  *pent = p;
-	  return 0;
-	}
-      x += strsize;
-    }
-  if (x == pos && hdr->tail)
-    {
-      /* To supply the trailing '\n' */
-      p = hdr->tail;
-      *pent = p;
-      *poff = MU_STR_SIZE (p->nlen, p->vlen) - 1;
-      return 0;
-    }
-  return 1;
-}
-     
-static void
-mu_hdrent_count (struct _mu_header *hdr, size_t *pcount, size_t *psize,
-		 size_t *plines)
-{
-  if (hdr->flags & HEADER_INVALIDATE)
-    {
-      size_t size = 0;
-      size_t count = 0;
-      size_t lines = 0;
-      struct mu_hdrent *p;
-      for (p = hdr->head; p; p = p->next)
-	{
-	  count++;
-	  size += MU_STR_SIZE (p->nlen, p->vlen);
-	  lines += p->nlines;
-	}
-
-      hdr->numhdr = count;
-      hdr->numlines = lines;
-      hdr->size = size;
-      hdr->flags &= ~HEADER_INVALIDATE;
-    }
-      
-  *pcount = hdr->numhdr;
-  *psize = hdr->size;
-  *plines = hdr->numlines;
-}
-
-static void
-mu_hdrent_remove (struct _mu_header *hdr, struct mu_hdrent *ent)
-{
-  struct mu_hdrent *p = ent->prev;
-  if (p)
-    p->next = ent->next;
-  else
-    hdr->head = ent->next;
-
-  p = ent->next;
-  if (p)
-    p->prev = ent->prev;
-  else
-    hdr->tail = ent->prev;
-}
-
-static void
-mu_hdrent_prepend (struct _mu_header *hdr, struct mu_hdrent *ent)
-{
-  struct mu_hdrent *p = hdr->head;
-  ent->prev = NULL;
-  ent->next = p;
-  if (p)
-    p->prev = ent;
-  else
-    hdr->tail = ent;
-  hdr->head = ent;
-}
-
-static void
-mu_hdrent_append (struct _mu_header *hdr, struct mu_hdrent *ent)
-{
-  struct mu_hdrent *p = hdr->tail;
-  ent->next = NULL;
-  ent->prev = p;
-  if (p)
-    p->next = ent;
-  else
-    hdr->head = ent;
-  hdr->tail = ent;
-}
-
-static int
-mu_hdrent_insert (struct _mu_header *hdr, struct mu_hdrent *ent,
-		  const char *name, int pos, int before)
-{
-  struct mu_hdrent *p;
-  struct mu_hdrent *ref = mu_hdrent_find (hdr, name, pos);
-  if (!ref)
-    return MU_ERR_NOENT;
-
-  if (before)
-    {
-      ref = ref->prev;
-      if (!ref)
-	{
-	  mu_hdrent_prepend (hdr, ent);
-	  return 0;
-	}
-    }
-
-  p = ref->next;
-  if (!p)
-    {
-      mu_hdrent_append (hdr, ent);
-      return 0;
-    }
-
-  ent->next = p;
-  p->prev = ent;
-  ent->prev = ref;
-  ref->next = ent;
-  
-  return 0;
-}
-
-#define SPOOLBLKSIZ 1024
-
-static struct mu_hdrent *
-mu_hdrent_create (struct _mu_header *ph,
-		  struct mu_hdrent *ent,
-		  const char *name, size_t nsize,
-		  const char *value, size_t vsize)
-{
-  size_t strsize;
-  size_t sizeleft;
-  const char *p;
-  
-  if (!ent)
-    {
-      ent = calloc (1, sizeof (*ent));
-      if (!ent)
-	return NULL;
-    }
-  
-  strsize = MU_STR_SIZE (nsize, vsize);
-  sizeleft = ph->spool_size - ph->spool_used;
-
-  /* Ensure there is enough space in spool */
-  if (sizeleft < strsize)
-    {
-      char *newp;
-      size_t delta = (strsize - sizeleft + SPOOLBLKSIZ - 1) / SPOOLBLKSIZ;
-      delta *= SPOOLBLKSIZ;
-      newp = realloc (ph->spool, ph->spool_size + delta);
-      if (!newp)
-	return 0;
-      ph->spool = newp;
-      ph->spool_size += delta;
-    }
-
-  /* Copy header name */
-  ent->fn = ph->spool_used;
-  ent->nlen = nsize;
-  memcpy (ph->spool + ph->spool_used, name, nsize);
-  ph->spool_used += nsize;
-  ph->spool[ph->spool_used++] = 0;
-  ph->spool[ph->spool_used++] = ' ';
-
-  /* Copy header value */
-  ent->fv = ph->spool_used;
-  ent->vlen = vsize;
-  memcpy (ph->spool + ph->spool_used, value, vsize);
-  ph->spool_used += vsize;
-  ph->spool[ph->spool_used++] = 0;
-  
-  ent->nlines = 1;
-  for (p = value; p < value + vsize; p++)
-    if (*p == '\n')
-      ent->nlines++;
-  
-  return ent;
-}
-
-static void
-mu_hdrent_free_list (struct _mu_header *hdr)
-{
-  struct mu_hdrent *p;
-  for (p = hdr->head; p; )
-    {
-      struct mu_hdrent *next = p->next;
-      free (p);
-      p = next;
-    }
-  hdr->head = hdr->tail = NULL;
-  hdr->spool_used = 0;
-}
-  
-
-
-#define ISLWSP(c) (((c) == ' ' || (c) == '\t'))
-
-/* Parsing is done in a rather simple fashion, meaning we just consider an
-   entry to be a field-name an a field-value.  So they maybe duplicate of
-   field-name like "Received" they are just put in the array, see _get_value()
-   on how to handle the case. in the case of error .i.e a bad header construct
-   we do a full stop and return what we have so far.  */
-
-static int
-header_parse (mu_header_t header, const char *blurb, int len)
-{
-  const char *header_end;
-  const char *header_start;
-  const char *header_start2;
-  
-  /* Nothing to parse.  */
-  if (blurb == NULL)
-    return 0;
-
-  header->flags |= HEADER_INVALIDATE;
-  mu_hdrent_free_list (header);
-
-  /* Get a header, a header is:
-     field-name LWSP ':'
-       LWSP field-value '\r' '\n'
-       *[ (' ' | '\t') field-value '\r' '\n' ]
-  */
-  /* First loop goes through the blurb */
-  for (header_start = blurb; len > 0; header_start = ++header_end)
-    {
-      const char *fn, *fn_end, *fv, *fv_end;
-      struct mu_hdrent *ent;
-
-      if (header_start[0] == ' '
-	  || header_start[0] == '\t'
-	  || header_start[0] == '\n')
-	break;
-
-      /* Second loop extract one header field. */
-      for (header_start2 = header_start; len; header_start2 = ++header_end)
-	{
-	  header_end = memchr (header_start2, '\n', len);
-	  if (header_end == NULL)
-	    {
-	      header_end = header_start2 + len;
-	      len = 0;
-	      break;
-	    }
-	  else
-	    {
-	      len -= (header_end - header_start2 + 1);
-	      if (!len
-		  || (header_end[1] != ' '
-		      && header_end[1] != '\t'))
-		break; /* New header break the inner for. */
-	    }
-	  /* *header_end = ' ';  smash LF ? NO */
-	}
-
-      /* Now save the header in the data structure.  */
-
-      /* Treats unix "From " specially.  FIXME: Should we? */
-      if ((header_end - header_start >= 5)
-      	  && strncmp (header_start, "From ", 5) == 0)
-	{
-	  fn = header_start;
-	  fn_end = header_start + 5;
-	  fv = header_start + 5;
-	  fv_end = header_end;
-	}
-      else /* Break the header in key: value */
-	{
-	  char *colon = memchr (header_start, ':', header_end - header_start);
-
-	  /* Houston we have a problem.  */
-	  if (colon == NULL)
-	    break; /* FIXME: Disregard the rest and bailout.  */
-
-	  fn = header_start;
-	  fn_end = colon;
-	  /* Shrink any LWSP after the field name -- CRITICAL for 
-	     later name comparisons to work correctly! */
-	  while (ISLWSP (fn_end[-1]))
-	    fn_end--;
-
-	  fv = colon + 1;
-	  fv_end = header_end;
-
-	  /* Skip any LWSP before the field value -- unnecessary, but
-	     might make some field values look a little tidier. */
-	  while (ISLWSP (fv[0]))
-	    fv++;
-	}
-
-      /* Register this header */
-      ent = mu_hdrent_create (header, NULL, fn, fn_end - fn, fv, fv_end - fv);
-      if (!ent)
-	return ENOMEM;
-      mu_hdrent_append (header, ent);
-    } /* for (header_start ...) */
-
-  return 0;
-}
-
-
-static int 
-mu_header_fill (mu_header_t header)
-{
-  int status;
-  size_t blurb_len = 0;
-  char *blurb = NULL;
-  
-  if (header->spool_used)
-    return 0;
-  
-  if (header->_fill == NULL)
-    return 0; /* FIXME: Really? */
-
-  /* Bring in the entire header.  */
-  status = header->_fill (header->data, &blurb, &blurb_len);
-  if (status)
-    return status;
-  status = header_parse (header, blurb, blurb_len);
-  free (blurb);
-  return status;
-}
-
-
-
-int
-mu_header_create (mu_header_t *ph, const char *blurb, size_t len)
-{
-  mu_header_t header;
-  int status = 0;
-
-  header = calloc (1, sizeof (*header));
-  if (header == NULL)
-    return ENOMEM;
-  
-  status = header_parse (header, blurb, len);
-
-  *ph = header;
-  return status;
-}
-
-void
-mu_header_destroy (mu_header_t *ph)
-{
-  if (ph && *ph)
-    {  
-      mu_header_t header = *ph;
-
-      mu_stream_destroy (&header->stream);
-      mu_hdrent_free_list (header);
-      free (header->spool);
-      free (header);
-      *ph = NULL;
-    }
-}
-
-
-int
-mu_header_set_value (mu_header_t header, const char *fn, const char *fv,
-		     int replace)
-{
-  int status;
-  struct mu_hdrent *ent = NULL;
-  
-  if (header == NULL || fn == NULL)
-    return EINVAL;
-
-  status = mu_header_fill (header);
-  if (status)
-    return status;
-  
-  /* An fv of NULL means delete the field, but only do it if replace
-     was also set to true! */
-  if (fv == NULL && !replace)
-    return EINVAL;
-
-  if (replace)
-    {
-      ent = mu_hdrent_find (header, fn, 1);
-      if (ent)
-	{
-	  if (fv == NULL)
-	    {
-	      /* Delete the header */
-	      mu_hdrent_remove (header, ent);
-	      free (ent);
-	      return 0;
-	    }
-	  mu_hdrent_create (header, ent, fn, strlen (fn), fv, strlen (fv));
-	  HEADER_SET_MODIFIED (header);
-	  return 0;
-	}
-      else if (fv == NULL)
-	return 0;
-    }
-
-  ent = mu_hdrent_create (header, NULL,
-			  fn, strlen (fn), fv, strlen (fv));
-  if (!ent)
-    return ENOMEM;
-  mu_hdrent_prepend (header, ent);
-  HEADER_SET_MODIFIED (header);
-  return 0;
-}
-
-int
-mu_header_remove (mu_header_t header, const char *fn, int n)
-{
-  int status;
-  struct mu_hdrent *ent;
-  
-  if (header == NULL || fn == NULL)
-    return EINVAL;
-
-  status = mu_header_fill (header);
-  if (status)
-    return status;
-
-  ent = mu_hdrent_find (header, fn, n);
-  if (!ent)
-    return MU_ERR_NOENT;
-
-  mu_hdrent_remove (header, ent);
-  HEADER_SET_MODIFIED (header);
-  free (ent);
-  return 0;
-}
-
-int
-mu_header_append (mu_header_t header, const char *fn, const char *fv)
-{
-  int status;
-  struct mu_hdrent *ent;
-
-  if (header == NULL || fn == NULL || fv == NULL)
-    return EINVAL;
-
-  status = mu_header_fill (header);
-  if (status)
-    return status;
-
-  ent = mu_hdrent_create (header, NULL, fn, strlen (fn), fv, strlen (fv));
-  if (!ent)
-    return ENOMEM;
-  mu_hdrent_append (header, ent);
-  HEADER_SET_MODIFIED (header);
-  return 0;
-}
-
-int
-mu_header_prepend (mu_header_t header, const char *fn, const char *fv)
-{
-  int status;
-  struct mu_hdrent *ent;
-
-  if (header == NULL || fn == NULL || fv == NULL)
-    return EINVAL;
-
-  status = mu_header_fill (header);
-  if (status)
-    return status;
-
-  ent = mu_hdrent_create (header, NULL, fn, strlen (fn), fv, strlen (fv));
-  if (!ent)
-    return ENOMEM;
-  mu_hdrent_prepend (header, ent);
-  HEADER_SET_MODIFIED (header);
-  return 0;
-}
-
-int
-mu_header_insert (mu_header_t header,
-		  const char *fn, const char *fv, 
-		  const char *ref, int n, int flags)
-{
-  int status;
-  struct mu_hdrent *ent;
-
-  if (header == NULL || fn == NULL || fv == NULL)
-    return EINVAL;
-
-  status = mu_header_fill (header);
-  if (status)
-    return status;
-    
-  if (flags & MU_HEADER_REPLACE)
-    {
-      if (!ref)
-	ref = fn;
-      ent = mu_hdrent_find (header, ref, n);
-      mu_hdrent_create (header, ent, fn, strlen (fn), fv, strlen (fv));
-    }
-  else
-    {
-      ent = mu_hdrent_create (header, NULL,
-			      fn, strlen (fn), fv, strlen (fv));
-      if (!ent)
-	return ENOMEM;
-      if (ref)
-	return mu_hdrent_insert (header, ent, ref, n,
-				 flags & MU_HEADER_BEFORE);
-      else
-	mu_hdrent_prepend (header, ent);
-    }
-  HEADER_SET_MODIFIED (header);
-  return 0;
-}
-  
-
-int
-mu_header_sget_value_n (mu_header_t header,
-			const char *name, int n,
-			const char **pval)
-{
-  int status;
-  struct mu_hdrent *ent;
-
-  if (header == NULL || name == NULL)
-    return EINVAL;
-  status = mu_header_fill (header);
-  if (status)
-    return status;
-
-  ent = mu_hdrent_find (header, name, n);
-  if (!ent)
-    return MU_ERR_NOENT;
-
-  *pval = MU_HDRENT_VALUE (header, ent);
-  return 0;
-}
-
-int
-mu_header_aget_value_n (mu_header_t header,
-		      const char *name, int n,
-		      char **pval)
-{
-  const char *s;
-  int status = mu_header_sget_value_n (header, name, n, &s);
-  if (status == 0)
-    {
-      *pval = strdup (s);
-      if (!*pval)
-	status = ENOMEM;
-    }
-  return status;
-}
-
-int
-mu_header_get_value_n (mu_header_t header, const char *name, int n,
-		       char *buffer, size_t buflen, size_t *pn)
-{
-  const char *s;
-  int status = mu_header_sget_value_n (header, name, n, &s);
-  if (status == 0)
-    {
-      size_t slen = strlen (s);
-
-      if (buffer)
-	{
-	  if (slen > buflen)
-	    slen = buflen;
-	  memcpy (buffer, s, slen);
-	  buffer[slen] = 0;
-	}
-      if (pn)
-	*pn = slen;
-    }
-  return status;
-}
-
-
-/* Unfolding functions */
-int
-mu_header_get_value_unfold_n (mu_header_t header,
-			      const char *name, int n, char *buffer,
-			      size_t buflen, size_t *pn)
-{
-  int rc = mu_header_get_value_n (header, name, n, buffer, buflen, pn);
-
-  if (rc == 0)
-    mu_string_unfold (buffer, pn);
-  return rc;
-}
-
-int
-mu_header_aget_value_unfold_n (mu_header_t header, const char *name, int n,
-			       char **pvalue)
-{
-  int rc = mu_header_aget_value_n (header, name, n, pvalue);
-  if (rc == 0)
-    mu_string_unfold (*pvalue, NULL);
-  return rc;
-}
-
-
-int
-mu_header_get_address_n (mu_header_t header, const char *name, int n,
-			 mu_address_t *addr)
-{
-  const char *value = NULL;
-  int status = mu_header_sget_value_n (header, name, n, &value);
-
-  if (status)
-    return status;
-
-  return mu_address_create (addr, value);
-}
-
-
-int
-mu_header_get_field_count (mu_header_t header, size_t *pcount)
-{
-  size_t count;
-  size_t size;
-  size_t lines;
-  int status;
-  
-  if (header == NULL)
-    return EINVAL;
-
-  status = mu_header_fill (header);
-  if (status == 0)
-    {
-      mu_hdrent_count (header, &count, &size, &lines);
-      
-      if (pcount)
-	*pcount = count;
-    }
-  
-  return status;
-}
-
-int
-mu_header_sget_field_name (mu_header_t header, size_t num, const char **sptr)
-{
-  int status;
-  
-  if (header == NULL)
-    return EINVAL;
-
-  status = mu_header_fill (header);
-  if (status == 0)
-    {
-      struct mu_hdrent *ent = mu_hdrent_nth (header, num);
-      if (ent)
-	*sptr = MU_HDRENT_NAME (header, ent);
-      else
-	status = MU_ERR_NOENT;
-    }
-  return status;
-}
-
-int
-mu_header_get_field_name (mu_header_t header, size_t num, char *buffer,
-			  size_t buflen, size_t *pn)
-{
-  const char *s;
-  int status = mu_header_sget_field_name (header, num, &s);
-  if (status == 0)
-    {
-      size_t slen = strlen (s);
-
-      if (buffer)
-	{
-	  if (slen > buflen)
-	    slen = buflen;
-	  memcpy (buffer, s, slen);
-	  buffer[slen] = 0;
-	}
-      if (pn)
-	*pn = slen;
-    }
-  return status;
-}
-
-int
-mu_header_aget_field_name (mu_header_t header, size_t num, char **pvalue)
-{
-  const char *s;
-  int status = mu_header_sget_field_name (header, num, &s);
-  if (status == 0)
-    {
-      if ((*pvalue = strdup (s)) == NULL)
-	status = ENOMEM;
-    }
-  return status;
-}
-
-
-int
-mu_header_sget_field_value (mu_header_t header, size_t num, const char **sptr)
-{
-  int status;
-  
-  if (header == NULL)
-    return EINVAL;
-
-  status = mu_header_fill (header);
-  if (status == 0)
-    {
-      struct mu_hdrent *ent = mu_hdrent_nth (header, num);
-      if (ent)
-	*sptr = MU_HDRENT_VALUE (header, ent);
-      else
-	status = MU_ERR_NOENT;
-    }
-  return status;
-}
-
-int
-mu_header_get_field_value (mu_header_t header, size_t num, char *buffer,
-			   size_t buflen, size_t *pn)
-{
-  const char *s;
-  int status = mu_header_sget_field_value (header, num, &s);
-  if (status == 0)
-    {
-      size_t slen = strlen (s);
-
-      if (buffer)
-	{
-	  if (slen > buflen)
-	    slen = buflen;
-	  memcpy (buffer, s, slen);
-	  buffer[slen] = 0;
-	}
-      if (pn)
-	*pn = slen;
-    }
-  return status;
-}
-
-int
-mu_header_aget_field_value (mu_header_t header, size_t num, char **pvalue)
-{
-  const char *s;
-  int status = mu_header_sget_field_value (header, num, &s);
-  if (status == 0)
-    {
-      if ((*pvalue = strdup (s)) == NULL)
-	status = ENOMEM;
-    }
-  return status;
-}
-
-int
-mu_header_get_field_value_unfold (mu_header_t header, size_t num, char *buf,
-				  size_t buflen, size_t *nwritten)
-{
-  int rc = mu_header_get_field_value (header, num, buf, buflen, nwritten);
-  if (rc == 0)
-    mu_string_unfold (buf, nwritten);
-  return rc;
-}
-
-int
-mu_header_aget_field_value_unfold (mu_header_t header, size_t num,
-				   char **pvalue)
-{
-  int rc = mu_header_aget_field_value (header, num, pvalue);
-  if (rc == 0)
-    mu_string_unfold (*pvalue, NULL);
-  return rc;
-}
-
-
-int
-mu_header_lines (mu_header_t header, size_t *plines)
-{
-  int status;
-
-  if (header == NULL)
-    return EINVAL;
-  if (plines == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-
-  status = mu_header_fill (header);
-  if (status == 0)
-    {
-      size_t count;
-      size_t size;
-      size_t lines;
-      mu_hdrent_count (header, &count, &size, &lines);
-      *plines = lines + 1;
-    }
-  return status;
-}
-
-int
-mu_header_size (mu_header_t header, size_t *psize)
-{
-  int status;
-
-  if (header == NULL)
-    return EINVAL;
-  if (psize == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-
-  status = mu_header_fill (header);
-  if (status == 0)
-    {
-      size_t count;
-      size_t size;
-      size_t lines;
-      mu_hdrent_count (header, &count, &size, &lines);
-      *psize = size + 1;
-    }
-  return status;
-}
-
-int
-mu_header_invalidate (mu_header_t hdr)
-{
-  if (hdr == NULL)
-    return EINVAL;
-  mu_hdrent_free_list (hdr);
-  return 0;
-}
-
-
-static void
-mu_hdrent_fixup (mu_header_t hdr, struct mu_hdrent *ent)
-{
-  char *s = MU_HDRENT_NAME (hdr, ent);
-  s[ent->nlen] = ':';
-  s = MU_HDRENT_VALUE (hdr, ent);
-  s[ent->vlen] = '\n';
-}
-
-static void
-mu_hdrent_unroll_fixup (mu_header_t hdr, struct mu_hdrent *ent)
-{
-  char *s = MU_HDRENT_NAME (hdr, ent);
-  s[ent->nlen] = 0;
-  s = MU_HDRENT_VALUE (hdr, ent);
-  s[ent->vlen] = 0;
-}
-
-int
-header_seek (mu_stream_t str, mu_off_t off, mu_off_t *presult)
-{ 
-  struct _mu_header_stream *hstr = (struct _mu_header_stream *) str;
-  size_t size;
-  int status;
-    
-  status = mu_header_size (hstr->hdr, &size);
-  if (status)
-    return status;
-  
-  if (off < 0 || off > size)
-    return ESPIPE;
-  hstr->off = off;
-  *presult = off;
-  return 0;
-}
-
-static int
-header_read (mu_stream_t is, char *buffer, size_t buflen, size_t *pnread)
-{
-  struct _mu_header_stream *hstr = (struct _mu_header_stream *) is;
-  mu_header_t header;
-  struct mu_hdrent *ent;
-  size_t ent_off;
-  int status;
-  size_t nread;
-  
-  if (is == NULL)
-    return EINVAL;
-
-  header = hstr->hdr;
-  status = mu_header_fill (header);
-  if (status)
-    return status;
-  
-  if (mu_hdrent_find_stream_pos (header, hstr->off, &ent, &ent_off))
-    {
-      if (pnread)
-	*pnread = 0;
-      return 0;
-    }
-
-  for (nread = 0; nread < buflen && ent; ent = ent->next)
-    {
-      size_t rest = buflen - nread;
-      size_t strsize = MU_STR_SIZE (ent->nlen, ent->vlen) - ent_off;
-      if (rest > strsize)
-	rest = strsize;
-      mu_hdrent_fixup (header, ent);
-      memcpy (buffer + nread, MU_HDRENT_NAME (header, ent) + ent_off, rest);
-      mu_hdrent_unroll_fixup (header, ent);
-      nread += rest;
-      hstr->off += rest;
-      ent_off = 0;
-    }
-  if (pnread)
-    *pnread = nread;
-  return 0;
-}
-
-#if 0
-/* FIXME: Implement header_readdelim based on this: */
-static int
-_header_readline (mu_stream_t is, char *buffer, size_t buflen, size_t *pnread)
-{
-  struct _mu_header_stream *hstr = (struct _mu_header_stream *) is;
-  mu_header_t header = hstr->hdr;
-  struct mu_hdrent *ent;
-  size_t ent_off;
-  int status;
-  size_t strsize;
-  char *start, *end;
-
-  if (buflen == 0)
-    return EINVAL;
-
-  header = mu_stream_get_owner (is);
-  status = mu_header_fill (header);
-  if (status)
-    return status;
-  if (mu_hdrent_find_stream_pos (header, hstr->off, &ent, &ent_off))
-    {
-      if (pnread)
-	*pnread = 0;
-      return 0;
-    }
-
-  buflen--; /* Account for the terminating nul */
-
-  mu_hdrent_fixup (header, ent);
-  strsize = MU_STR_SIZE (ent->nlen, ent->vlen) - ent_off;
-  start = MU_HDRENT_NAME (header, ent) + ent_off;
-  end = strchr (start, '\n');
-  if (end)
-    {
-      size_t len = end - start + 1;
-      if (len < strsize)
-	strsize = len;
-    }
-
-  if (strsize < buflen)
-    buflen = strsize;
-
-  memcpy (buffer, start, buflen);
-  buffer[buflen] = 0;
-  hstr->off += buflen;
-  mu_hdrent_unroll_fixup (header, ent);
-  if (pnread)
-    *pnread = buflen;
-  return 0;
-}
-#endif
-
-static int
-header_write (mu_stream_t os, const char *buf, size_t buflen, size_t *pnwrite)
-{
-  struct _mu_header_stream *hstr;
-  mu_header_t header;
-  int status;
-  mu_off_t mstream_size;
-  
-  if (!os || !buf)
-    return EINVAL;
-
-  hstr = (struct _mu_header_stream *) os;
-  header = hstr->hdr;
-  if (header == NULL)
-    return EINVAL;
-  
-  /* Skip the obvious.  */
-  if (*buf == '\0' || buflen == 0)
-    {
-      if (pnwrite)
-        *pnwrite = 0;
-      return 0;
-    }
-
-  if (!header->mstream)
-    {
-      status = mu_memory_stream_create (&header->mstream, MU_STREAM_RDWR);
-      if (status)
-	return status;
-    }
-
-  status = mu_stream_write (header->mstream, buf, buflen, NULL);
-  if (status)
-    {
-      mu_stream_destroy (&header->mstream);
-      return status;
-    }
-
-  status = mu_stream_size (header->mstream, &mstream_size);
-  if (status == 0 && mstream_size > 1)
-    {
-      char nlbuf[2];
-
-      status = mu_stream_seek (header->mstream, -2, MU_SEEK_END, NULL);
-      if (status == 0)
-	status = mu_stream_read (header->mstream, nlbuf, 2, NULL);
-      if (status == 0 && memcmp (nlbuf, "\n\n", 2) == 0)
-	{
-	  char *blurb;
-
-	  blurb = calloc (1, mstream_size + 1);
-	  if (blurb)
-	    {
-	      mu_stream_read (header->mstream, blurb, mstream_size, NULL);
-	      status = header_parse (header, blurb, mstream_size);
-	    }
-	  free (blurb);
-	  mu_stream_destroy (&header->mstream);
-	}
-    }
-  
-  if (pnwrite)
-    *pnwrite = buflen;
-  
-  return status;
-}
-
-static int
-header_size (mu_stream_t str, mu_off_t *psize)
-{
-  mu_header_t header;
-  int status;
-  size_t size;
-  
-  if (str == NULL)
-    return EINVAL;
-  if (psize == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-  
-  header = ((struct _mu_header_stream *) str)->hdr;
-  status = mu_header_fill (header);
-  if (status)
-    return status;
-  status = mu_header_size (header, &size);
-  if (status == 0)
-    *psize = size;
-  return status;
-}
-
-static int
-_header_get_stream (mu_header_t header, mu_stream_t *pstream, int ref)
-{
-  if (header == NULL)
-    return EINVAL;
-
-  if (pstream == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-
-  if (header->stream == NULL)
-    {
-      struct _mu_header_stream *str = 
-	(struct _mu_header_stream *) _mu_stream_create (sizeof (*str),
-							MU_STREAM_RDWR|MU_STREAM_SEEK);
-      if (!str)
-	return ENOMEM;
-      str->stream.read = header_read;
-      /*str->stream.rdelim? */
-      str->stream.write = header_write;
-      str->stream.seek = header_seek;
-      str->stream.size = header_size;
-      str->hdr = header;
-      header->stream = (mu_stream_t) str;
-    }
-  if (!ref)
-    {
-      *pstream = header->stream;
-      return 0;
-    }
-  return mu_streamref_create (pstream, header->stream);
-}
-
-int
-mu_header_get_stream (mu_header_t header, mu_stream_t *pstream)
-{
-  /* FIXME: Deprecation warning */
-  return _header_get_stream (header, pstream, 0);
-}
-
-int
-mu_header_get_streamref (mu_header_t header, mu_stream_t *pstream)
-{
-  return _header_get_stream (header, pstream, 1);
-}
-
-
-int
-mu_header_set_fill (mu_header_t header, int
-		    (*_fill) (void *data, char **, size_t *),
-		    void *data)
-{
-  if (header == NULL)
-    return EINVAL;
-  header->_fill = _fill;
-  header->data = data;
-  return 0;
-}
-
-
-int
-mu_header_is_modified (mu_header_t header)
-{
-  return header ? (header->flags & HEADER_MODIFIED) : 0;
-}
-
-int
-mu_header_clear_modified (mu_header_t header)
-{
-  if (header)
-    header->flags &= ~HEADER_MODIFIED;
-  return 0;
-}
-
-
diff --git a/libmailutils/iterator.c b/libmailutils/iterator.c
deleted file mode 100644
index 3897925..0000000
--- a/libmailutils/iterator.c
+++ /dev/null
@@ -1,284 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 1999, 2000, 2004, 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 */
-
-#ifdef HAVE_CONFIG_H
-# include <config.h>
-#endif
-
-#include <errno.h>
-#include <stdlib.h>
-
-#include <mailutils/sys/list.h>
-#include <mailutils/sys/iterator.h>
-#include <mailutils/errno.h>
-
-int
-mu_iterator_create (mu_iterator_t *piterator, void *owner)
-{
-  mu_iterator_t iterator;
-  if (piterator == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-  if (owner == NULL)
-    return EINVAL;
-  iterator = calloc (1, sizeof (*iterator));
-  if (iterator == NULL)
-    return ENOMEM;
-  iterator->owner = owner;
-  *piterator = iterator;
-  return 0;
-}
-
-int
-mu_iterator_set_first (mu_iterator_t itr, int (*first) (void *))
-{
-  if (!itr)
-    return EINVAL;
-  itr->first = first;
-  return 0;
-}
-
-int
-mu_iterator_set_next (mu_iterator_t itr, int (*next) (void *))
-{
-  if (!itr)
-    return EINVAL;
-  itr->next = next;
-  return 0;
-}
-
-int
-mu_iterator_set_getitem (mu_iterator_t itr,
-                         int (*getitem) (void *, void **, const void **))
-{
-  if (!itr)
-    return EINVAL;
-  itr->getitem = getitem;
-  return 0;
-}
-
-int
-mu_iterator_set_finished_p (mu_iterator_t itr, int (*finished_p) (void *))
-{
-  if (!itr)
-    return EINVAL;
-  itr->finished_p = finished_p;
-  return 0;
-}
-
-int
-mu_iterator_set_curitem_p (mu_iterator_t itr,
-			int (*curitem_p) (void *, void *))
-{
-  if (!itr)
-    return EINVAL;
-  itr->curitem_p = curitem_p;
-  return 0;
-}
-
-int
-mu_iterator_set_itrctl (mu_iterator_t itr,
-			int (*itrctl) (void *,
-				       enum mu_itrctl_req,
-				       void *))
-{
-  if (!itr)
-    return EINVAL;
-  itr->itrctl = itrctl;
-  return 0;
-}
-
-int
-mu_iterator_set_destroy (mu_iterator_t itr, int (*destroy) (mu_iterator_t, void *))
-{
-  if (!itr)
-    return EINVAL;
-  itr->destroy = destroy;
-  return 0;
-}
-
-int
-mu_iterator_set_dup (mu_iterator_t itr, int (*dup) (void **ptr, void *data))
-{
-  if (!itr)
-    return EINVAL;
-  itr->dup = dup;
-  return 0;
-}
-
-
-
-int
-mu_iterator_dup (mu_iterator_t *piterator, mu_iterator_t orig)
-{
-  mu_iterator_t iterator;
-  int status;
-  
-  if (piterator == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-  if (orig == NULL)
-    return EINVAL;
-
-  status = mu_iterator_create (&iterator, orig->owner);
-  if (status)
-    return status;
-
-  status = orig->dup(&iterator->owner, orig->owner);
-  if (status)
-    {
-      free (iterator);
-      return status;
-    }
-  iterator->is_advanced = orig->is_advanced;   
-  iterator->dup = orig->dup;
-  iterator->destroy = orig->destroy;
-  iterator->first = orig->first;
-  iterator->next = orig->next;
-  iterator->getitem = orig->getitem;
-  iterator->curitem_p = orig->curitem_p;
-  iterator->finished_p = orig->finished_p;
-  iterator->itrctl = orig->itrctl;
-  
-  *piterator = iterator;
-  return 0;
-}
-
-void
-mu_iterator_destroy (mu_iterator_t *piterator)
-{
-  if (!piterator || !*piterator)
-    return;
-
-  if ((*piterator)->destroy)
-    (*piterator)->destroy (*piterator, (*piterator)->owner);
-  
-  free (*piterator);
-  *piterator = NULL;
-}
-
-int
-mu_iterator_first (mu_iterator_t iterator)
-{
-  iterator->is_advanced = 0;
-  return iterator->first (iterator->owner);
-}
-
-int
-mu_iterator_next (mu_iterator_t iterator)
-{
-  int status = 0;
-  if (!iterator->is_advanced)
-    status = iterator->next (iterator->owner);
-  iterator->is_advanced = 0;
-  return status;
-}
-
-int
-mu_iterator_skip (mu_iterator_t iterator, ssize_t count)
-{
-  int status;
-  if (count < 0)
-    return ENOSYS; /* Need prev method */
-  while (count--)
-    if ((status = mu_iterator_next (iterator)))
-      break;
-  return status;
-}
-
-int
-mu_iterator_current (mu_iterator_t iterator, void **pitem)
-{
-  return iterator->getitem (iterator->owner, pitem, NULL);
-}
-
-int
-mu_iterator_current_kv (mu_iterator_t iterator, 
-                        const void **pkey, void **pitem)
-{
-  return iterator->getitem (iterator->owner, (void**)pitem, pkey);
-}
-
-int
-mu_iterator_is_done (mu_iterator_t iterator)
-{
-  if (iterator == NULL)
-    return 1;
-  return iterator->finished_p (iterator->owner);
-}
-
-int
-iterator_get_owner (mu_iterator_t iterator, void **powner)
-{
-  if (!iterator)
-    return EINVAL;
-  if (!powner)
-    return MU_ERR_OUT_PTR_NULL;
-  *powner = iterator->owner;
-  return 0;
-}
-
-void
-mu_iterator_advance (mu_iterator_t iterator, void *e)
-{
-  for (; iterator; iterator = iterator->next_itr)
-    {
-      if (iterator->curitem_p (iterator->owner, e))
-	{
-	  iterator->next (iterator->owner);
-	  iterator->is_advanced++;
-	}
-    }
-}
-
-int
-mu_iterator_attach (mu_iterator_t *root, mu_iterator_t iterator)
-{
-  iterator->next_itr = *root;
-  *root = iterator;
-  return 0;
-}
-
-int
-mu_iterator_detach (mu_iterator_t *root, mu_iterator_t iterator)
-{
-  mu_iterator_t itr, prev;
-  
-  for (itr = *root, prev = NULL; itr; prev = itr, itr = itr->next_itr)
-    if (iterator == itr)
-      break;
-
-  if (itr)
-    {
-      if (prev)
-	prev->next_itr = itr->next_itr;
-      else
-	*root = itr->next_itr;
-    }
-  
-  return 0;
-}
-
-int
-mu_iterator_ctl (mu_iterator_t iterator, enum mu_itrctl_req req, void *arg)
-{
-  if (!iterator)
-    return EINVAL;
-  if (!iterator->itrctl)
-    return ENOSYS;
-  return iterator->itrctl (iterator->owner, req, arg);
-}
diff --git a/libmailutils/list.c b/libmailutils/list.c
deleted file mode 100644
index 0045eba..0000000
--- a/libmailutils/list.c
+++ /dev/null
@@ -1,693 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 1999, 2000, 2001, 2004, 2005, 2007, 2008, 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 <errno.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include <mailutils/sys/list.h>
-#include <mailutils/sys/iterator.h>
-#include <mailutils/errno.h>
-
-#define DESTROY_ITEM(list, elt)			\
-  do						\
-    {						\
-       if ((list)->destroy_item)		\
-	 (list)->destroy_item ((elt)->item);	\
-    }						\
-  while (0)
-
-int
-mu_list_create (mu_list_t *plist)
-{
-  mu_list_t list;
-  int status;
-
-  if (plist == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-  list = calloc (sizeof (*list), 1);
-  if (list == NULL)
-    return ENOMEM;
-  status = mu_monitor_create (&list->monitor, 0,  list);
-  if (status != 0)
-    {
-      free (list);
-      return status;
-    }
-  list->head.next = &list->head;
-  list->head.prev = &list->head;
-  *plist = list;
-  return 0;
-}
-
-void
-mu_list_clear (mu_list_t list)
-{
-  struct list_data *current;
-  struct list_data *previous;
-  
-  mu_monitor_wrlock (list->monitor);
-  for (current = list->head.next; current != &list->head;)
-    {
-      previous = current;
-      current = current->next;
-      DESTROY_ITEM (list, previous);
-      free (previous);
-    }
-  list->head.next = list->head.prev = &list->head;
-  mu_monitor_unlock (list->monitor);
-}
-
-void
-mu_list_destroy (mu_list_t *plist)
-{
-  if (plist && *plist)
-    {
-      mu_list_t list = *plist;
-      mu_list_clear (list);
-      mu_monitor_destroy (&list->monitor, list);
-      free (list);
-      *plist = NULL;
-    }
-}
-
-int
-mu_list_append (mu_list_t list, void *item)
-{
-  struct list_data *ldata;
-  struct list_data *last;
-
-  if (list == NULL)
-    return EINVAL;
-  last = list->head.prev;
-  ldata = calloc (sizeof (*ldata), 1);
-  if (ldata == NULL)
-    return ENOMEM;
-  ldata->item = item;
-  mu_monitor_wrlock (list->monitor);
-  ldata->next = &list->head;
-  ldata->prev = list->head.prev;
-  last->next = ldata;
-  list->head.prev = ldata;
-  list->count++;
-  mu_monitor_unlock (list->monitor);
-  return 0;
-}
-
-int
-mu_list_prepend (mu_list_t list, void *item)
-{
-  struct list_data *ldata;
-  struct list_data *first;
-
-  if (list == NULL)
-    return EINVAL;
-  first = list->head.next;
-  ldata = calloc (sizeof (*ldata), 1);
-  if (ldata == NULL)
-    return ENOMEM;
-  ldata->item = item;
-  mu_monitor_wrlock (list->monitor);
-  ldata->prev = &list->head;
-  ldata->next = list->head.next;
-  first->prev = ldata;
-  list->head.next = ldata;
-  list->count++;
-  mu_monitor_unlock (list->monitor);
-  return 0;
-}
-
-int
-mu_list_is_empty (mu_list_t list)
-{
-  size_t n = 0;
-
-  mu_list_count (list, &n);
-  return (n == 0);
-}
-
-int
-mu_list_count (mu_list_t list, size_t *pcount)
-{
-  if (list == NULL)
-    return EINVAL;
-  if (pcount == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-  *pcount = list->count;
-  return 0;
-}
-
-mu_list_comparator_t
-mu_list_set_comparator (mu_list_t list, mu_list_comparator_t comp)
-{
-  mu_list_comparator_t old_comp;
-
-  if (list == NULL)
-    return NULL;
-  old_comp = list->comp;
-  list->comp = comp;
-  return old_comp;
-}
-
-int
-mu_list_get_comparator (mu_list_t list, mu_list_comparator_t *comp)
-{
-  if (!list)
-    return EINVAL;
-  *comp = list->comp;
-  return 0;
-}
-
-int
-_mu_list_ptr_comparator (const void *item, const void *value)
-{
-  return item != value;
-}
-
-int
-mu_list_locate (mu_list_t list, void *item, void **ret_item)
-{
-  struct list_data *current, *previous;
-  mu_list_comparator_t comp;
-  int status = MU_ERR_NOENT;
-
-  if (list == NULL)
-    return EINVAL;
-  comp = list->comp ? list->comp : _mu_list_ptr_comparator;
-  mu_monitor_wrlock (list->monitor);
-  for (previous = &list->head, current = list->head.next;
-       current != &list->head; previous = current, current = current->next)
-    {
-      if (comp (current->item, item) == 0)
-	{
-	  if (ret_item)
-	    *ret_item = current->item;
-	  status = 0;
-	  break;
-	}
-    }
-  mu_monitor_unlock (list->monitor);
-  return status;
-}
-
-static int
-_insert_item (mu_list_t list, struct list_data *current, void *new_item,
-	      int insert_before)
-{
-  int status;
-  struct list_data *ldata = calloc (sizeof (*ldata), 1);
-  if (ldata == NULL)
-    status = ENOMEM;
-  else
-    {
-      ldata->item = new_item;
-      _mu_list_insert_sublist (list, current,
-			       ldata, ldata,
-			       1,
-			       insert_before);
-      status = 0;
-    }
-  return status;
-}
-
-int
-mu_list_insert (mu_list_t list, void *item, void *new_item, int insert_before)
-{
-  struct list_data *current;
-  mu_list_comparator_t comp;
-  int status = MU_ERR_NOENT;
-
-  if (list == NULL)
-    return EINVAL;
-  comp = list->comp ? list->comp : _mu_list_ptr_comparator;
-
-  mu_monitor_wrlock (list->monitor);
-  for (current = list->head.next;
-       current != &list->head;
-       current = current->next)
-    {
-      if (comp (current->item, item) == 0)
-	{
-	  status = _insert_item (list, current, new_item, insert_before);
-	  break;
-	}
-    }
-  mu_monitor_unlock (list->monitor);
-  return status;
-}
-
-int
-mu_list_remove (mu_list_t list, void *item)
-{
-  struct list_data *current;
-  mu_list_comparator_t comp;
-  int status = MU_ERR_NOENT;
-
-  if (list == NULL)
-    return EINVAL;
-  comp = list->comp ? list->comp : _mu_list_ptr_comparator;
-  mu_monitor_wrlock (list->monitor);
-  for (current = list->head.next;
-       current != &list->head; current = current->next)
-    {
-      if (comp (current->item, item) == 0)
-	{
-	  struct list_data *previous = current->prev;
-	  
-	  mu_iterator_advance (list->itr, current);
-	  previous->next = current->next;
-	  current->next->prev = previous;
-	  DESTROY_ITEM (list, current);
-	  free (current);
-	  list->count--;
-	  status = 0;
-	  break;
-	}
-    }
-  mu_monitor_unlock (list->monitor);
-  return status;
-}
-
-int
-mu_list_remove_nd (mu_list_t list, void *item)
-{
-  mu_list_destroy_item_t dptr = mu_list_set_destroy_item (list, NULL);
-  int rc = mu_list_remove (list, item);
-  mu_list_set_destroy_item (list, dptr);
-  return rc;
-}
-
-int
-mu_list_replace (mu_list_t list, void *old_item, void *new_item)
-{
-  struct list_data *current, *previous;
-  mu_list_comparator_t comp;
-  int status = MU_ERR_NOENT;
-
-  if (list == NULL)
-    return EINVAL;
-  comp = list->comp ? list->comp : _mu_list_ptr_comparator;
-  mu_monitor_wrlock (list->monitor);
-  for (previous = &list->head, current = list->head.next;
-       current != &list->head; previous = current, current = current->next)
-    {
-      if (comp (current->item, old_item) == 0)
-	{
-	  DESTROY_ITEM (list, current);
-	  current->item = new_item;
-	  status = 0;
-	  break;
-	}
-    }
-  mu_monitor_unlock (list->monitor);
-  return status;
-}
-
-int
-mu_list_replace_nd (mu_list_t list, void *item, void *new_item)
-{
-  mu_list_destroy_item_t dptr = mu_list_set_destroy_item (list, NULL);
-  int rc = mu_list_replace (list, item, new_item);
-  mu_list_set_destroy_item (list, dptr);
-  return rc;
-}
-
-int
-mu_list_get (mu_list_t list, size_t indx, void **pitem)
-{
-  struct list_data *current;
-  size_t count;
-  int status = MU_ERR_NOENT;
-
-  if (list == NULL)
-    return EINVAL;
-  if (pitem == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-  mu_monitor_rdlock (list->monitor);
-  for (current = list->head.next, count = 0; current != &list->head;
-       current = current->next, count++)
-    {
-      if (count == indx)
-        {
-          *pitem = current->item;
-	  status = 0;
-	  break;
-        }
-    }
-  mu_monitor_unlock (list->monitor);
-  return status;
-}
-
-int
-mu_list_do (mu_list_t list, mu_list_action_t *action, void *cbdata)
-{
-  mu_iterator_t itr;
-  int status = 0;
-
-  if (list == NULL || action == NULL)
-    return EINVAL;
-  status = mu_list_get_iterator (list, &itr);
-  if (status)
-    return status;
-  for (mu_iterator_first (itr); !mu_iterator_is_done (itr);
-       mu_iterator_next (itr))
-    {
-      void *item;
-      mu_iterator_current (itr, &item);
-      if ((status = action (item, cbdata)))
-	break;
-    }
-  mu_iterator_destroy (&itr);
-  return status;
-}
-
-mu_list_destroy_item_t
-mu_list_set_destroy_item (mu_list_t list, void (*destroy_item)(void *item))
-{
-  mu_list_destroy_item_t ret = list->destroy_item;
-  list->destroy_item = destroy_item;
-  return ret;
-}
-
-int
-mu_list_to_array (mu_list_t list, void **array, size_t count, size_t *pcount)
-{
-  size_t total = 0;
-
-  if (!list)
-    return EINVAL;
-
-  total = (count < list->count) ? count : list->count;
-
-  if (array)
-    {
-      size_t i;
-      struct list_data *current;
-
-      for (i = 0, current = list->head.next;
-	   i < total && current != &list->head; current = current->next)
-	array[i++] = current->item;
-    }
-  if (pcount)
-    *pcount = total;
-  return 0;
-}
-
-/* Computes an intersection of two lists and returns it in PDEST.
-   The resulting list contains elements from A that are
-   also encountered in B (as per comparison function of
-   the latter).
-
-   If DUP_ITEM is not NULL, it is used to create copies of
-   items to be stored in PDEST.  In this case, the destroy_item
-   function of B is also attached to PDEST.  Otherwise, if
-   DUP_ITEM is NULL, pointers to elements are stored and
-   no destroy_item function is assigned. */
-int
-mu_list_intersect_dup (mu_list_t *pdest, mu_list_t a, mu_list_t b,
-		       int (*dup_item) (void **, void *, void *),
-		       void *dup_closure)
-{
-  mu_list_t dest;
-  int rc;
-  mu_iterator_t itr;
-  
-  rc = mu_list_create (&dest);
-  if (rc)
-    return rc;
-
-  mu_list_set_comparator (dest, b->comp);
-  if (dup_item)
-    mu_list_set_destroy_item (dest, b->destroy_item);
-  
-  rc = mu_list_get_iterator (a, &itr);
-  if (rc)
-    {
-      mu_list_destroy (&dest);
-      return rc;
-    }
-
-  rc = 0;
-  for (mu_iterator_first (itr); !mu_iterator_is_done (itr);
-       mu_iterator_next (itr))
-    {
-      void *data;
-      mu_iterator_current (itr, &data);
-      if (mu_list_locate (b, data, NULL) == 0)
-	{
-	  void *new_data;
-	  if (dup_item && data)
-	    {
-	      rc = dup_item (&new_data, data, dup_closure);
-	      if (rc)
-		break;
-	    }
-	  else
-	    new_data = data;
-	
-	  mu_list_append (dest, new_data); /* FIXME: Check return, and? */
-	}
-    }
-  mu_iterator_destroy (&itr);
-  *pdest = dest;
-  return rc;
-}
-
-int
-mu_list_intersect (mu_list_t *pdest, mu_list_t a, mu_list_t b)
-{
-  return mu_list_intersect_dup (pdest, a, b, NULL, NULL);
-}
-
-
-/* Iterator interface */
-
-struct list_iterator
-{
-  mu_list_t list;
-  struct list_data *cur;
-  int backwards; /* true if iterating backwards */
-};
-
-static int
-first (void *owner)
-{
-  struct list_iterator *itr = owner;
-  if (itr->backwards)
-    itr->cur = itr->list->head.prev;
-  else
-    itr->cur = itr->list->head.next;
-  return 0;
-}
-
-static int
-next (void *owner)
-{
-  struct list_iterator *itr = owner;
-  if (itr->backwards)
-    itr->cur = itr->cur->prev;
-  else
-    itr->cur = itr->cur->next;
-  return 0;
-}
-
-static int
-getitem (void *owner, void **pret, const void **pkey)
-{
-  struct list_iterator *itr = owner;
-  *pret = itr->cur->item;
-  if (pkey)
-    *pkey = NULL;
-  return 0;
-}
-
-static int
-finished_p (void *owner)
-{
-  struct list_iterator *itr = owner;
-  return itr->cur == &itr->list->head;
-}
-
-static int
-destroy (mu_iterator_t iterator, void *data)
-{
-  struct list_iterator *itr = data;
-  mu_iterator_detach (&itr->list->itr, iterator);
-  free (data);
-  return 0;
-}
-
-static int
-curitem_p (void *owner, void *item)
-{
-  struct list_iterator *itr = owner;
-  return itr->cur == item;
-}
-
-static int
-list_data_dup (void **ptr, void *owner)
-{
-  *ptr = malloc (sizeof (struct list_iterator));
-  if (*ptr == NULL)
-    return ENOMEM;
-  memcpy (*ptr, owner, sizeof (struct list_iterator));
-  return 0;
-}
-
-static int
-list_itrctl (void *owner, enum mu_itrctl_req req, void *arg)
-{
-  struct list_iterator *itr = owner;
-  mu_list_t list = itr->list;
-  struct list_data *ptr;
-  
-  if (itr->cur == NULL)
-    return MU_ERR_NOENT;
-  switch (req)
-    {
-    case mu_itrctl_tell:
-      /* Return current position in the object */
-      {
-	size_t count;
-
-	for (count = 0, ptr = list->head.next; ptr != &list->head;
-	     ptr = ptr->next, count++)
-	  {
-	    if (ptr == itr->cur)
-	      {
-		*(size_t*)arg = count;
-		return 0;
-	      }
-	  }
-	return MU_ERR_NOENT;
-      }
-	
-    case mu_itrctl_delete:
-    case mu_itrctl_delete_nd:
-      /* Delete current element */
-      {
-	struct list_data *prev;
-	
-	ptr = itr->cur;
-	prev = ptr->prev;
-	
-	mu_iterator_advance (list->itr, ptr);
-	prev->next = ptr->next;
-	ptr->next->prev = prev;
-	if (req == mu_itrctl_delete)
-	  DESTROY_ITEM (list, ptr);
-	free (ptr);
-	list->count--;
-      }
-      break;
-      
-    case mu_itrctl_replace:
-    case mu_itrctl_replace_nd:
-      /* Replace current element */
-      if (!arg)
-	return EINVAL;
-      ptr = itr->cur;
-      if (req == mu_itrctl_replace)
-	  DESTROY_ITEM (list, ptr);
-      ptr = itr->cur;
-      ptr->item = arg;
-      break;
-      
-    case mu_itrctl_insert:
-      /* Insert new element in the current position */
-      if (!arg)
-	return EINVAL;
-      return _insert_item (list, itr->cur, arg, 0);
-
-    case mu_itrctl_insert_list:
-      /* Insert a list of elements */
-      if (!arg)
-	return EINVAL;
-      else
-	{
-	  mu_list_t new_list = arg;
-	  _mu_list_insert_sublist (list, itr->cur,
-				   new_list->head.next, new_list->head.prev,
-				   new_list->count,
-				   0);
-	  _mu_list_clear (new_list);
-	}
-      break;
-
-    case mu_itrctl_qry_direction:
-      if (!arg)
-	return EINVAL;
-      else
-	*(int*)arg = itr->backwards;
-      break;
-
-    case mu_itrctl_set_direction:
-      if (!arg)
-	return EINVAL;
-      else
-	itr->backwards = !!*(int*)arg;
-      break;
-      
-    default:
-      return ENOSYS;
-    }
-  return 0;
-}
-
-int
-mu_list_get_iterator (mu_list_t list, mu_iterator_t *piterator)
-{
-  mu_iterator_t iterator;
-  int status;
-  struct list_iterator *itr;
-
-  if (!list)
-    return EINVAL;
-
-  itr = calloc (1, sizeof *itr);
-  if (!itr)
-    return ENOMEM;
-  itr->list = list;
-  itr->cur = NULL;
-
-  status = mu_iterator_create (&iterator, itr);
-  if (status)
-    {
-      free (itr);
-      return status;
-    }
-
-  mu_iterator_set_first (iterator, first);
-  mu_iterator_set_next (iterator, next);
-  mu_iterator_set_getitem (iterator, getitem);
-  mu_iterator_set_finished_p (iterator, finished_p);
-  mu_iterator_set_curitem_p (iterator, curitem_p);
-  mu_iterator_set_destroy (iterator, destroy);
-  mu_iterator_set_dup (iterator, list_data_dup);
-  mu_iterator_set_itrctl (iterator, list_itrctl);
-  
-  mu_iterator_attach (&list->itr, iterator);
-
-  *piterator = iterator;
-  return 0;
-}
diff --git a/libmailutils/listlist.c b/libmailutils/listlist.c
deleted file mode 100644
index 94172ac..0000000
--- a/libmailutils/listlist.c
+++ /dev/null
@@ -1,139 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 1999, 2000, 2001, 2004, 2005, 2007, 2008, 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 <errno.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include <mailutils/sys/list.h>
-#include <mailutils/sys/iterator.h>
-#include <mailutils/errno.h>
-
-void
-_mu_list_insert_sublist (mu_list_t list,
-			 struct list_data *current,
-			 struct list_data *head,
-			 struct list_data *tail,
-			 size_t count,
-			 int insert_before)
-{
-  if (insert_before)
-    {
-      head->prev = current->prev;
-      tail->next = current;
-      if (current->prev != &list->head)
-	current->prev->next = head;
-      else
-	list->head.next = head;
-
-      current->prev = tail;
-    }
-  else
-    {
-      tail->next = current->next;
-      head->prev = current;
-      if (current->next != &list->head)
-	current->next->prev = tail;
-      else
-	list->head.prev = tail;
-
-      current->next = head;
-    }
-  list->count += count;
-}
-
-void
-_mu_list_clear (mu_list_t list)
-{
-  list->head.next = list->head.prev = &list->head;
-  list->count = 0;
-}
-
-int
-mu_list_insert_list (mu_list_t list, void *item, mu_list_t new_list,
-		     int insert_before)
-{
-  struct list_data *current;
-  mu_list_comparator_t comp;
-  int status = MU_ERR_NOENT;
-
-  if (list == NULL)
-    return EINVAL;
-  comp = list->comp ? list->comp : _mu_list_ptr_comparator;
-
-  mu_monitor_wrlock (list->monitor);
-  for (current = list->head.next;
-       current != &list->head;
-       current = current->next)
-    {
-      if (comp (current->item, item) == 0)
-	{
-	  _mu_list_insert_sublist (list, current,
-				   new_list->head.next, new_list->head.prev,
-				   new_list->count,
-				   insert_before);
-	  _mu_list_clear (new_list);
-	  status = 0;
-	  break;
-	}
-    }
-  mu_monitor_unlock (list->monitor);
-  return status;
-}
-
-void
-mu_list_append_list (mu_list_t list, mu_list_t new_list)
-{
-  if (list->count == 0)
-    {
-      list->head = new_list->head;
-      list->head.next->prev = list->head.prev->next = &list->head;
-      list->count = new_list->count;
-    }
-  else
-    _mu_list_insert_sublist (list, list->head.prev,
-			     new_list->head.next, new_list->head.prev,
-			     new_list->count,
-			     0);
-  _mu_list_clear (new_list);
-}
-
-void
-mu_list_prepend_list (mu_list_t list, mu_list_t new_list)
-{
-  if (list->count == 0)
-    {
-      list->head = new_list->head;
-      list->head.next->prev = list->head.prev->next = &list->head;
-      list->count = new_list->count;
-    }
-  else
-    _mu_list_insert_sublist (list, list->head.next,
-			     new_list->head.next, new_list->head.prev,
-			     new_list->count,
-			     1);
-  _mu_list_clear (new_list);
-}
-
-
-
diff --git a/libmailutils/locale.c b/libmailutils/locale.c
deleted file mode 100644
index b6bdc08..0000000
--- a/libmailutils/locale.c
+++ /dev/null
@@ -1,242 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 2003, 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 <stdlib.h>
-#include <string.h>
-#include <mailutils/mutil.h>
-#include <mailutils/cstr.h>
-
-struct langtab
-{
-  char *lang;        /* Language code */
-  char *terr;        /* Territory code */
-  char *charset;     /* Corresponding charset */
-};
-
-/* The list of language codes defined in ISO 639 with the corresponding
-   default character sets.
-
-   NOTES:
-
-   1) The list must be ordered by:
-        a) lang field in ascending order
-	b) terr field in descending order.
-      NULL fields are considered less than non-null ones.
-   2) Many entries have NULL charset fields. Please help fill them!
-   3) The "default" character set for a given language is a matter
-      of preference. Possibly the table should contain a *list* of
-      possible character sets.
-   4) LC_ALL "modifier" field is not taken into account */
-
-static struct langtab langtab[] = {
-  { "C",     NULL, "ASCII"},
-  { "POSIX", NULL, "ASCII" },
-  { "aa",    NULL, NULL},              /* Afar */
-  { "ab",    NULL, NULL},              /* Abkhazian */
-  { "ae",    NULL, NULL},              /* Avestan */
-  { "af",    NULL, "iso-8859-1"},      /* Afrikaans */
-  { "am",    NULL, "UTF-8"},           /* Amharic */
-  { "ar",    NULL, "iso-8859-6"},      /* Arabic */
-  { "as",    NULL, NULL},              /* Assamese */
-  { "ay",    NULL, "iso-8859-1"},      /* Aymara */
-  { "az",    NULL, NULL},              /* Azerbaijani */
-  { "ba",    NULL, NULL},              /* Bashkir */
-  { "be",    NULL, "UTF-8"},           /* Byelorussian; Belarusian */
-  { "bg",    NULL, "iso-8859-5"},      /* Bulgarian */
-  { "bh",    NULL, NULL},              /* Bihari */
-  { "bi",    NULL, NULL},              /* Bislama */
-  { "bn",    NULL, NULL},              /* Bengali; Bangla */
-  { "bo",    NULL, NULL},              /* Tibetan */
-  { "br",    NULL, "iso-8859-1"},      /* Breton: 1,5,8,9 */
-  { "bs",    NULL, NULL},              /* Bosnian */
-  { "ca",    NULL, "iso-8859-1"},      /* Catalan: 1,5,8,9 */
-  { "ce",    NULL, NULL},              /* Chechen */
-  { "ch",    NULL, NULL},              /* Chamorro */
-  { "co",    NULL, "iso-8859-1"},      /* Corsican */
-  { "cs",    NULL, "iso-8859-2"},      /* Czech */
-  { "cu",    NULL, NULL },             /* Church Slavic */
-  { "cv",    NULL, NULL},              /* Chuvash */
-  { "cy",    NULL, "iso-8859-1"},      /* Welsh */
-  { "da",    NULL, "iso-8859-1"},      /* Danish: 4-9 */
-  { "de",    NULL, "iso-8859-1"},      /* German */
-  { "dz",    NULL, NULL },             /* Dzongkha; Bhutani */
-  { "el",    NULL, "iso-8859-7"},      /* Greek */
-  { "en",    NULL, "iso-8859-1"},      /* English */
-  { "eo",    NULL, "iso-8859-3"},      /* Esperanto */
-  { "es",    NULL, "iso-8859-1"},      /* Spanish */
-  { "et",    NULL, "iso-8859-15"},     /* Estonian: 6,7,9 */
-  { "eu",    NULL, "iso-8859-1"},      /* Basque: 5,8,9 */
-  { "fa",    NULL, "UTF-8"},           /* Persian */
-  { "fi",    NULL, "iso-8859-15"},     /* Finnish */
-  { "fj",    NULL, NULL },             /* Fijian; Fiji */
-  { "fo",    NULL, "iso-8859-1"},      /* Faroese: 6,9 */
-  { "fr",    NULL, "iso-8859-1"},      /* French */
-  { "fy",    NULL, "iso-8859-1"},      /* Frisian */
-  { "ga",    NULL, "iso-8859-14"},     /* Irish */
-  { "gd",    NULL, "iso-8859-14" },    /* Scots; Gaelic */
-  { "gl",    NULL, NULL },             /* Gallegan; Galician */
-  { "gn",    NULL, NULL},              /* Guarani */
-  { "gu",    NULL, NULL},              /* Gujarati */
-  { "gv",    NULL, "iso-8859-14"},     /* Manx */
-  { "ha",    NULL, NULL },             /* Hausa (?) */
-  { "he",    NULL, "iso-8859-8" },     /* Hebrew */
-  { "hi",    NULL, NULL},              /* Hindi */
-  { "ho",    NULL, NULL},              /* Hiri Motu */
-  { "hr",    NULL, "iso-8859-2"},      /* Croatian: 10 */
-  { "hu",    NULL, "iso-8859-2"},      /* Hungarian */
-  { "hy",    NULL, NULL},              /* Armenian */
-  { "hz",    NULL, NULL},              /* Herero */
-  { "id",    NULL, "iso-8859-1"},      /* Indonesian (formerly in) */
-  { "ia",    NULL, NULL},              /* Interlingua */
-  { "ie",    NULL, NULL},              /* Interlingue */
-  { "ik",    NULL, NULL},              /* Inupiak */
-  { "io",    NULL, NULL},              /* Ido */
-  { "is",    NULL, "iso-8859-1"},      /* Icelandic */
-  { "it",    NULL, "iso-8859-1"},      /* Italian */
-  { "iu",    NULL, NULL},              /* Inuktitut */
-  { "ja",    NULL, "EUC-JP"},          /* Japanese */
-  { "jv",    NULL, NULL},              /* Javanese */
-  { "ka",    NULL, NULL},              /* Georgian */
-  { "ki",    NULL, NULL},              /* Kikuyu */
-  { "kj",    NULL, NULL},              /* Kuanyama */
-  { "kk",    NULL, NULL},              /* Kazakh */
-  { "kl",    NULL, "iso-8859-1"},      /* Kalaallisut; Greenlandic */
-  { "km",    NULL, NULL},              /* Khmer; Cambodian */
-  { "kn",    NULL, NULL},              /* Kannada */
-  { "ko",    NULL, "EUC-KR"},          /* Korean */
-  { "ks",    NULL, NULL},              /* Kashmiri */
-  { "ku",    NULL, NULL},              /* Kurdish */
-  { "kv",    NULL, NULL},              /* Komi */
-  { "kw",    NULL, "iso-8859-14"},     /* Cornish: 1,5,8 */
-  { "ky",    NULL, NULL},              /* Kirghiz */
-  { "la",    NULL, "iso-8859-1"},      /* Latin */
-  { "lb",    NULL, "iso-8859-1"},      /* Letzeburgesch */
-  { "ln",    NULL, NULL},              /* Lingala */
-  { "lo",    NULL, NULL},              /* Lao; Laotian */
-  { "lt",    NULL, "iso-8859-4"},      /* Lithuanian */
-  { "lv",    NULL, "iso-8859-4"},      /* Latvian; Lettish */
-  { "mg",    NULL, NULL},              /* Malagasy */
-  { "mh",    NULL, NULL},              /* Marshall */
-  { "mi",    NULL, NULL},              /* Maori */
-  { "mk",    NULL, NULL},              /* Macedonian */
-  { "ml",    NULL, NULL},              /* Malayalam */
-  { "mn",    NULL, NULL},              /* Mongolian */
-  { "mo",    NULL, "iso-8859-2"},      /* Moldavian */
-  { "mr",    NULL, NULL},              /* Marathi */
-  { "ms",    NULL, NULL},              /* Malay */
-  { "mt",    NULL, "iso-8859-3"},      /* Maltese */
-  { "my",    NULL, NULL},              /* Burmese */
-  { "na",    NULL, NULL},              /* Nauru */
-  { "nb",    NULL, "iso-8859-1"},      /* Norwegian Bokmål; Bokm@aa{}l  */
-  { "nd",    NULL, NULL},              /* Ndebele, North */
-  { "ne",    NULL, NULL},              /* Nepali */
-  { "ng",    NULL, NULL},              /* Ndonga */
-  { "nl",    NULL, "iso-8859-1"},      /* Dutch: 5,9 */
-  { "nn",    NULL, "iso-8859-1"},      /* Norwegian Nynorsk */
-  { "no",    NULL, "iso-8859-1"},      /* Norwegian */
-  { "nr",    NULL, NULL},              /* Ndebele, South */
-  { "nv",    NULL, NULL},              /* Navajo */
-  { "ny",    NULL, NULL},              /* Chichewa; Nyanja */
-  { "oc",    NULL, NULL},              /* Occitan; Provençal; Proven@,{c}al */
-  { "om",    NULL, NULL},              /* (Afan) Oromo */
-  { "or",    NULL, NULL},              /* Oriya */
-  { "os",    NULL, NULL},              /* Ossetian; Ossetic */
-  { "pa",    NULL, NULL},              /* Panjabi; Punjabi */
-  { "pi",    NULL, NULL},              /* Pali */
-  { "pl",    NULL, "iso-8859-2"},      /* Polish */
-  { "ps",    NULL, NULL},              /* Pashto, Pushto */
-  { "pt",    NULL, "iso-8859-1"},      /* Portuguese */
-  { "qu",    NULL, "iso-8859-1"},      /* Quechua */
-  { "rm",    NULL, "iso-8859-1"},      /* Rhaeto-Romance */
-  { "rn",    NULL, NULL },             /* Rundi; Kirundi */
-  { "ro",    NULL, "iso-8859-2"},      /* Romanian */
-  { "ru",    NULL, "koi8-r"},          /* Russian */
-  { "rw",    NULL, NULL},              /* Kinyarwanda */
-  { "sa",    NULL, NULL},              /* Sanskrit */
-  { "sc",    NULL, "iso-8859-1"},      /* Sardinian */
-  { "sd",    NULL, NULL},              /* Sindhi */
-  { "se",    NULL, "iso-8859-10"},     /* Northern Sami */
-  { "sg",    NULL, NULL},              /* Sango; Sangro */
-  { "si",    NULL, NULL},              /* Sinhalese */
-  { "sk",    NULL, "iso-8859-2"},      /* Slovak */
-  { "sl",    NULL, "iso-8859-1"},      /* Slovenian */
-  { "sm",    NULL, NULL},              /* Samoan */
-  { "sn",    NULL, NULL},              /* Shona */
-  { "so",    NULL, NULL},              /* Somali */
-  { "sq",    NULL, "iso-8859-1"},      /* Albanian: 2,5,8,9,10 */
-  { "sr",    NULL, "iso-8859-2"},      /* Serbian */
-  { "ss",    NULL, NULL},              /* Swati; Siswati */
-  { "st",    NULL, NULL},              /* Sesotho; Sotho, Southern */
-  { "su",    NULL, NULL},              /* Sundanese */
-  { "sv",    NULL, "iso-8859-1"},      /* Swedish */
-  { "sw",    NULL, NULL},              /* Swahili */
-  { "ta",    NULL, NULL},              /* Tamil */
-  { "te",    NULL, NULL},              /* Telugu */
-  { "tg",    NULL, NULL},              /* Tajik */
-  { "th",    NULL, "iso-8859-11"},     /* Thai */
-  { "ti",    NULL, NULL},              /* Tigrinya */
-  { "tk",    NULL, NULL},              /* Turkmen */
-  { "tl",    NULL, "iso-8859-1"},      /* Tagalog */
-  { "tn",    NULL, NULL},              /* Tswana; Setswana */
-  { "to",    NULL, NULL},              /* Tonga (?) */
-  { "tr",    NULL, "iso-8859-9"},      /* Turkish */
-  { "ts",    NULL, NULL},              /* Tsonga */
-  { "tt",    NULL, NULL},              /* Tatar */
-  { "tw",    NULL, NULL},              /* Twi */
-  { "ty",    NULL, NULL},              /* Tahitian */
-  { "ug",    NULL, NULL},              /* Uighur */
-  { "uk",    NULL, "koi8-u"},          /* Ukrainian */
-  { "ur",    NULL, NULL},              /* Urdu */
-  { "uz",    NULL, NULL},              /* Uzbek */
-  { "vi",    NULL, NULL},              /* Vietnamese */
-  { "vo",    NULL, NULL},              /* Volapük; Volap@"{u}k; Volapuk */
-  { "wa",    NULL, "iso-8859-1"},      /* Walloon */
-  { "wo",    NULL, NULL},              /* Wolof */
-  { "xh",    NULL, NULL},              /* Xhosa */
-  { "yi",    NULL, "iso-8859-8"},      /* Yiddish (formerly ji) */
-  { "yo",    NULL, NULL},              /* Yoruba */
-  { "za",    NULL, NULL},              /* Zhuang */
-  { "zh",    "TW", "big5"},            /* Chinese */
-  { "zh",    NULL, "gb2312"},          /* Chinese */
-  { "zu",    NULL, NULL},              /* Zulu */
-  { NULL }
-};
-
-/* Given the language and (optionally) territory code, return the
-   default character set for that language. See notes above. */
-
-const char *
-mu_charset_lookup (char *lang, char *terr)
-{
-  static struct langtab *p;
-
-  if (!lang)
-    return NULL;
-  for (p = langtab; p->lang; p++)
-    if (mu_c_strcasecmp (p->lang, lang) == 0
-	&& (terr == NULL 
-	    || p->terr == NULL
-	    || !mu_c_strcasecmp (p->terr, terr) == 0))
-      return p->charset;
-  return NULL;
-}
-
diff --git a/libmailutils/locker.c b/libmailutils/locker.c
deleted file mode 100644
index 4f0dcf2..0000000
--- a/libmailutils/locker.c
+++ /dev/null
@@ -1,1027 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 1999, 2000, 2001, 2005, 2006, 2007, 2008, 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 <assert.h>
-#include <errno.h>
-#include <fcntl.h>
-#include <limits.h>
-#include <signal.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <time.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <utime.h>
-
-#include <sys/param.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-#include <sys/wait.h>
-
-#include <mailutils/errno.h>
-#include <mailutils/locker.h>
-#include <mailutils/mutil.h>
-
-#define LOCKFILE_ATTR           0644
-
-/* First draft by Brian Edmond. */
-/* For subsequent modifications, see the GNU mailutils ChangeLog. */
-
-struct _mu_locker
-{
-  unsigned refcnt;             /* Number of times mu_locker_lock was called */
-  enum mu_locker_mode mode;    /* Current locking mode (if refcnt > 0) */
-
-  char *file;
-  int flags;
-  int expire_time;
-  int retries;
-  int retry_sleep;
-
-  union lock_data {
-    struct {
-      char *dotlock;
-      char *nfslock;
-    } dot;
-    
-    struct {
-      char *name;
-    } external;
-    
-    struct {
-      int fd;
-    } kernel;
-  } data;
-};
-
-#define MU_LOCKER_TYPE(l) MU_LOCKER_FLAG_TO_TYPE((l)->flags)
-
-struct locker_tab
-{
-  int (*init) (mu_locker_t);
-  void (*destroy) (mu_locker_t);
-  int (*prelock) (mu_locker_t); 
-  int (*lock) (mu_locker_t, enum mu_locker_mode);
-  int (*unlock) (mu_locker_t);
-};
-
-static int init_dotlock (mu_locker_t);
-static void destroy_dotlock (mu_locker_t);
-static int lock_dotlock (mu_locker_t, enum mu_locker_mode);
-static int unlock_dotlock (mu_locker_t);
-
-static int init_external (mu_locker_t);
-static void destroy_external (mu_locker_t);
-static int lock_external (mu_locker_t, enum mu_locker_mode);
-static int unlock_external (mu_locker_t);
-
-static int init_kernel (mu_locker_t);
-static int lock_kernel (mu_locker_t, enum mu_locker_mode);
-static int unlock_kernel (mu_locker_t);
-
-static int prelock_common (mu_locker_t);
-
-static struct locker_tab locker_tab[] = {
-  /* MU_LOCKER_TYPE_DOTLOCK */
-  { init_dotlock, destroy_dotlock, prelock_common,
-    lock_dotlock, unlock_dotlock },
-  /* MU_LOCKER_TYPE_EXTERNAL */
-  { init_external, destroy_external, prelock_common,
-    lock_external, unlock_external },
-  /* MU_LOCKER_TYPE_KERNEL */
-  { init_kernel, NULL, NULL, lock_kernel, unlock_kernel },
-  /* MU_LOCKER_TYPE_NULL */
-  { NULL, NULL, NULL, NULL, NULL }
-};
-
-#define MU_LOCKER_NTYPES (sizeof (locker_tab) / sizeof (locker_tab[0]))
-
-
-static int
-stat_check (const char *file, int fd, int links)
-{
-  struct stat fn_stat;
-  struct stat fd_stat;
-  int err = 0;
-  int localfd = -1;
-
-  if (fd == -1)
-    {
-      localfd = open (file, O_RDONLY);
-      
-      if (localfd == -1)
-	return errno;
-      fd = localfd;
-    }
-
-  /* We should always be able to stat a valid fd, so this
-     is an error condition. */
-  if (lstat (file, &fn_stat) || fstat (fd, &fd_stat))
-    err = errno;
-  else
-    {
-      /* If the link and stat don't report the same info, or the
-         file is a symlink, fail the locking. */
-#define CHK(X) if(X) err = EINVAL
-
-      CHK (!S_ISREG (fn_stat.st_mode));
-      CHK (!S_ISREG (fd_stat.st_mode));
-      CHK (fn_stat.st_nlink != links);
-      CHK (fn_stat.st_dev != fd_stat.st_dev);
-      CHK (fn_stat.st_ino != fd_stat.st_ino);
-      CHK (fn_stat.st_mode != fd_stat.st_mode);
-      CHK (fn_stat.st_nlink != fd_stat.st_nlink);
-      CHK (fn_stat.st_uid != fd_stat.st_uid);
-      CHK (fn_stat.st_gid != fd_stat.st_gid);
-      CHK (fn_stat.st_rdev != fd_stat.st_rdev);
-
-#undef CHK
-    }
-  if (localfd != -1)
-    close (localfd);
-
-  return err;
-}
-
-static int
-check_file_permissions (const char *file)
-{
-  int fd = -1;
-  int err = 0;
-
-  if ((fd = open (file, O_RDONLY)) == -1)
-    return errno == ENOENT ? 0 : errno;
-
-  err = stat_check (file, fd, 1);
-  close (fd);
-  fd = -1;
-  if (err)
-    {
-      if (err == EINVAL)
-	err = MU_ERR_LOCK_BAD_FILE;
-      return err;
-    }
-
-  return 0;
-}
-
-static int
-prelock_common (mu_locker_t locker)
-{
-  /* Check if we are trying to lock a regular file, with a link count
-     of 1, that we have permission to read, etc., or don't lock it. */
-  return check_file_permissions (locker->file);
-}
-
-
-static int mu_locker_default_flags = MU_LOCKER_DEFAULT;
-static time_t mu_locker_retry_timeout = MU_LOCKER_RETRY_SLEEP;
-static size_t mu_locker_retry_count = MU_LOCKER_RETRIES;
-static time_t mu_locker_expire_timeout = MU_LOCKER_EXPIRE_TIME;
-static char *mu_locker_external_program = NULL;
-
-int
-mu_locker_set_default_flags (int flags, enum mu_locker_set_mode mode)
-{
-  switch (mode)
-    {
-    case mu_locker_assign:
-      mu_locker_default_flags = flags;
-      break;
-
-    case mu_locker_set_bit:
-      mu_locker_default_flags |= flags;
-      break;
-
-    case mu_locker_clear_bit:
-      mu_locker_default_flags &= ~flags;
-      break;
-
-    default:
-      return EINVAL;
-    }
-  return 0;
-}
-
-void
-mu_locker_set_default_retry_timeout (time_t to)
-{
-  mu_locker_retry_timeout = to;
-}
-
-void
-mu_locker_set_default_retry_count (size_t n)
-{
-  mu_locker_retry_count = n;
-}
-
-void
-mu_locker_set_default_expire_timeout (time_t t)
-{
-  mu_locker_expire_timeout = t;
-}
-
-void
-mu_locker_set_default_external_program (char *path)
-{
-  free (mu_locker_external_program);
-  mu_locker_external_program = strdup (path);
-}
-
-int
-mu_locker_mod_flags (mu_locker_t locker, int flags,
-		     enum mu_locker_set_mode mode)
-{
-  unsigned otype, ntype;
-  int new_flags;
-  
-  if (!locker)
-    return MU_ERR_LOCKER_NULL;
-
-  switch (mode)
-    {
-    case mu_locker_assign:
-      new_flags = flags;
-      break;
-
-    case mu_locker_set_bit:
-      new_flags = locker->flags | flags;
-      break;
-
-    case mu_locker_clear_bit:
-      new_flags = locker->flags & ~flags;
-      break;
-
-    default:
-      return EINVAL;
-    }
-
-  otype = MU_LOCKER_TYPE (locker);
-  if (otype >= MU_LOCKER_NTYPES)
-    return EINVAL;
-  ntype = MU_LOCKER_FLAG_TO_TYPE (new_flags);
-  if (ntype >= MU_LOCKER_NTYPES)
-    return EINVAL;
-
-  if (ntype != otype)
-    {
-      int rc;
-      
-      if (locker_tab[otype].destroy)
-	locker_tab[otype].destroy (locker);
-      locker->flags = new_flags;
-      if (locker_tab[ntype].init)
-	{
-	  rc = locker_tab[ntype].init (locker);
-	  if (rc)
-	    locker->flags = MU_LOCKER_NULL;
-	  return rc;
-	}
-    }
-  else
-    locker->flags = new_flags;
-
-  return 0;
-}
-
-int
-mu_locker_set_flags (mu_locker_t locker, int flags)
-{
-  return mu_locker_mod_flags (locker, flags, mu_locker_assign);
-}
-
-int
-mu_locker_set_expire_time (mu_locker_t locker, int etime)
-{
-  if (!locker)
-    return MU_ERR_LOCKER_NULL;
-
-  if (etime <= 0)
-    return EINVAL;
-
-  locker->expire_time = etime;
-
-  return 0;
-}
-
-int
-mu_locker_set_retries (mu_locker_t locker, int retries)
-{
-  if (!locker)
-    return MU_ERR_LOCKER_NULL;
-
-  if (retries <= 0)
-    return EINVAL;
-
-  locker->retries = retries;
-
-  return 0;
-}
-
-int
-mu_locker_set_retry_sleep (mu_locker_t locker, int retry_sleep)
-{
-  if (!locker)
-    return MU_ERR_LOCKER_NULL;
-
-  if (retry_sleep <= 0)
-    return EINVAL;
-
-  locker->retry_sleep = retry_sleep;
-
-  return 0;
-}
-
-int
-mu_locker_set_external (mu_locker_t locker, const char* program)
-{
-  char* p = NULL;
-
-  if (!locker)
-    return MU_ERR_LOCKER_NULL;
-  if (MU_LOCKER_TYPE (locker) != MU_LOCKER_TYPE_EXTERNAL)
-    return EINVAL;
-    
-  /* program can be NULL */
-  if (program != 0)
-    {
-      p = strdup (program);
-      if (!p)
-	return ENOMEM;
-  }
-
-  free (locker->data.external.name);
-  locker->data.external.name = p;
-
-  return 0;
-}
-
-int
-mu_locker_get_flags (mu_locker_t locker, int *flags)
-{
-  if (!locker)
-    return MU_ERR_LOCKER_NULL;
-
-  if (!flags)
-    return EINVAL;
-
-  *flags = locker->flags;
-
-  return 0;
-}
-
-int
-mu_locker_get_expire_time (mu_locker_t locker, int *ptime)
-{
-  if (!locker)
-    return MU_ERR_LOCKER_NULL;
-
-  if (!ptime)
-    return EINVAL;
-
-  *ptime = locker->expire_time;
-
-  return 0;
-}
-
-int
-mu_locker_get_retries (mu_locker_t locker, int *retries)
-{
-  if (!locker)
-    return MU_ERR_LOCKER_NULL;
-
-  if (!retries)
-    return EINVAL;
-
-  *retries = locker->retries;
-
-  return 0;
-}
-
-int
-mu_locker_get_retry_sleep (mu_locker_t locker, int *retry_sleep)
-{
-  if (!locker)
-    return MU_ERR_LOCKER_NULL;
-
-  if (!retry_sleep)
-    return EINVAL;
-
-  *retry_sleep = locker->retry_sleep;
-
-  return 0;
-}
-
-int
-mu_locker_create (mu_locker_t *plocker, const char *fname, int flags)
-{
-  unsigned type;
-  mu_locker_t l;
-  char filename[_POSIX_PATH_MAX];
-  int err = 0;
-
-  if (plocker == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-
-  if (fname == NULL)
-    return EINVAL;
-
-  if ((err = mu_unroll_symlink (filename, sizeof (filename), fname)))
-    return err;
-
-  l = calloc (1, sizeof (*l));
-
-  if (l == NULL)
-    return ENOMEM;
-
-  l->file = strdup (filename);
-  
-  if (l->file == NULL)
-    {
-      free (l);
-      return ENOMEM;
-    }
-
-  if (strcmp (filename, "/dev/null") == 0)
-    l->flags = MU_LOCKER_NULL;
-  else if (flags)
-    l->flags = flags;
-  else
-    l->flags = mu_locker_default_flags;
-
-  l->expire_time = mu_locker_expire_timeout;
-  l->retries = mu_locker_retry_count;
-  l->retry_sleep = mu_locker_retry_timeout;
-
-  type = MU_LOCKER_TYPE (l);
-
-  if (type >= MU_LOCKER_NTYPES)
-    {
-      free (l->file);
-      return EINVAL;
-    }
-
-  /* Initialize locker-type-specific data */
-  err = locker_tab[type].init ? locker_tab[type].init (l) : 0;
-  if (err)
-    {
-      mu_locker_destroy (&l);
-      return err;
-    }
-
-  *plocker = l;
-
-  return 0;
-}
-
-void
-mu_locker_destroy (mu_locker_t *plocker)
-{
-  if (plocker && *plocker)
-    {
-      unsigned type = MU_LOCKER_TYPE (*plocker);
-      if (type < MU_LOCKER_NTYPES)
-	{
-	  if (locker_tab[type].destroy)
-	    locker_tab[type].destroy (*plocker);
-	  free ((*plocker)->file);
-	  free (*plocker);
-	  *plocker = NULL;
-	}
-    }
-}
-
-int
-_mu_locker_lock (mu_locker_t lock, enum mu_locker_mode mode)
-{
-  int rc;
-  unsigned type;
-  unsigned retries = 1;
-  
-  if (lock == NULL || (type = MU_LOCKER_TYPE (lock)) >= MU_LOCKER_NTYPES)
-    return EINVAL;
-
-  if (locker_tab[type].prelock && (rc = locker_tab[type].prelock (lock)))
-    return rc;
-  
-  /* Is the lock already applied? */
-  if (lock->refcnt > 0)
-    {
-      lock->refcnt++;
-      if (mode == lock->mode)
-	return 0;
-    }
-
-  lock->mode = mode;
-
-  if (lock->flags & MU_LOCKER_RETRY)
-    retries = lock->retries;
-
-  if (locker_tab[type].lock)
-    {
-      while (retries--)
-	{
-	  rc = locker_tab[type].lock (lock, mode);
-	  if (rc == EAGAIN && retries)
-	    {
-	      sleep (lock->retry_sleep);
-	      continue;
-	    }
-
-	  if (rc == 0)
-	    lock->refcnt++;
-
-	  break;
-	}
-    }
-  else
-    rc = 0;
-  
-  return rc;
-}
-
-int
-mu_locker_lock (mu_locker_t lock)
-{
-  return _mu_locker_lock (lock, mu_lck_exc);
-}
-
-int
-mu_locker_unlock (mu_locker_t lock)
-{
-  int rc = 0;
-  unsigned type;
-  
-  if (!lock)
-    return MU_ERR_LOCKER_NULL;
-
-  if (lock->refcnt == 0)
-    return MU_ERR_LOCK_NOT_HELD;
-
-  if ((rc = check_file_permissions (lock->file)))
-    return rc;
-
-  if (--lock->refcnt > 0)
-    return 0;
-
-  type = MU_LOCKER_TYPE (lock);
-  if (locker_tab[type].unlock)
-    rc = locker_tab[type].unlock (lock);
-  else
-    rc = 0;
-  
-  return rc;
-}
-
-int
-mu_locker_remove_lock (mu_locker_t lock)
-{
-  if (!lock)
-    return MU_ERR_LOCKER_NULL;
-
-  /* Force the reference count to 1 to unlock the file. */
-  lock->refcnt = 1;
-  return mu_locker_unlock (lock);
-}
-
-
-#define DOTLOCK_SUFFIX ".lock"
-
-/* expire a stale lock (if MU_LOCKER_PID or MU_LOCKER_TIME) */
-static void
-expire_stale_lock (mu_locker_t lock)
-{
-  int stale = 0;
-  int fd = open (lock->data.dot.dotlock, O_RDONLY);
-  if (fd == -1)
-    return;
-
-  /* Check to see if this process is still running.  */
-  if (lock->flags & MU_LOCKER_PID)
-    {
-      char buf[16];
-      pid_t pid;
-      int nread = read (fd, buf, sizeof (buf) - 1);
-      if (nread > 0)
-	{
-	  buf[nread] = '\0';
-	  pid = strtol (buf, NULL, 10);
-	  if (pid > 0)
-	    {
-	      /* Process is gone so we try to remove the lock. */
-	      if (kill (pid, 0) == -1)
-		stale = 1;
-	    }
-	  else
-	    stale = 1;		/* Corrupted file, remove the lock. */
-	}
-    }
-  
-  /* Check to see if the lock expired.  */
-  if (lock->flags & MU_LOCKER_TIME)
-    {
-      struct stat stbuf;
-
-      fstat (fd, &stbuf);
-      /* The lock has expired. */
-      if ((time (NULL) - stbuf.st_mtime) > lock->expire_time)
-	stale = 1;
-    }
-
-  close (fd);
-  if (stale)
-    unlink (lock->data.dot.dotlock);
-}
-
-static int
-init_dotlock (mu_locker_t locker)
-{
-  char *tmp, *p;
-
-  /* Make sure the spool directory is writable */
-  tmp = strdup (locker->file);
-  if (!tmp)
-    return ENOMEM;
-
-  strcpy (tmp, locker->file);
-  p = strrchr (tmp, '/');
-  if (!p)
-    {
-      free (tmp);
-      tmp = strdup (".");
-      if (!tmp)
-	return ENOMEM;
-    }
-  else
-    *p = 0; 
-
-  if (access (tmp, W_OK))
-    {
-      /* Fallback to kernel locking */
-      free (tmp);
-      return mu_locker_set_flags (locker,
-			   MU_LOCKER_KERNEL|MU_LOCKER_OPTIONS(locker->flags));
-    }
-  
-  free (tmp);
-  
-  locker->data.dot.dotlock = malloc (strlen (locker->file)
-				     + sizeof (DOTLOCK_SUFFIX));
-  
-  if (!locker->data.dot.dotlock)
-    return ENOMEM;
-  strcpy (locker->data.dot.dotlock, locker->file);
-  strcat (locker->data.dot.dotlock, DOTLOCK_SUFFIX);
-
-  return 0;
-}
-
-static void
-destroy_dotlock (mu_locker_t locker)
-{
-  free (locker->data.dot.dotlock);
-  free (locker->data.dot.nfslock);
-}
-
-static int
-lock_dotlock (mu_locker_t locker, enum mu_locker_mode mode)
-{
-  int rc;
-  char *host = NULL;
-  char pid[11];		/* 10 is strlen(2^32 = 4294967296) */
-  char now[11];
-  size_t sz = 0;
-  int err = 0;
-  int fd;
-    
-  if (locker->data.dot.nfslock)
-    {
-      unlink (locker->data.dot.nfslock);
-      free (locker->data.dot.nfslock);
-      locker->data.dot.nfslock = NULL;
-    }
-
-  expire_stale_lock (locker);
-
-  /* build the NFS hitching-post to the lock file */
-
-  rc = mu_get_host_name (&host);
-  if (rc)
-    return rc;
-
-  snprintf (now, sizeof (now), "%lu", (unsigned long) time (0));
-  now[sizeof (now) - 1] = 0;
-
-  snprintf (pid, sizeof (pid), "%lu", (unsigned long) getpid ());
-  pid[sizeof (pid) - 1] = 0;
-		  
-  sz = strlen (locker->file) + 1 /* "." */
-    + strlen (pid) + 1 /* "." */
-    + strlen (now) + 1 /* "." */
-    + strlen (host) + 1;
-  
-  locker->data.dot.nfslock = malloc (sz);
-  
-  if (!locker->data.dot.nfslock)
-    {
-      free (host);
-      return ENOMEM;
-    }
-
-  snprintf (locker->data.dot.nfslock, sz, "%s.%s.%s.%s",
-	    locker->file, pid, now, host);
-  free (host);
-  
-  fd = open (locker->data.dot.nfslock,
-	     O_WRONLY | O_CREAT | O_EXCL, LOCKFILE_ATTR);
-  if (fd == -1)
-    {
-      if (errno == EEXIST)
-	return EAGAIN;
-      else
-	return errno;
-    }
-  close (fd);
-  
-  /* Try to link to the lockfile. */
-  if (link (locker->data.dot.nfslock, locker->data.dot.dotlock) == -1)
-    {
-      unlink (locker->data.dot.nfslock);
-      if (errno == EEXIST)
-	return MU_ERR_LOCK_CONFLICT;
-      return errno;
-    }
-
-  if ((fd = open (locker->data.dot.dotlock, O_RDWR)) == -1)
-    {
-      unlink (locker->data.dot.nfslock);
-      return errno;
-    }
-  
-  err = stat_check (locker->data.dot.nfslock, fd, 2);
-  if (err)
-    {
-      unlink (locker->data.dot.nfslock);
-      if (err == EINVAL)
-	return MU_ERR_LOCK_BAD_LOCK;
-      return errno;
-    }
-
-  unlink (locker->data.dot.nfslock);
-
-  /* FIXME: If no errors, we have the lock. */
-  assert (locker->refcnt == 0);
-
-  if (locker->flags & MU_LOCKER_PID)
-    {
-      char buf[16];
-      sprintf (buf, "%ld", (long) getpid ());
-      write (fd, buf, strlen (buf));
-    }
-  close (fd);
-  return 0;
-}
-
-static int
-unlock_dotlock (mu_locker_t locker)
-{
-  if (unlink (locker->data.dot.dotlock) == -1)
-    {
-      int err = errno;
-      if (err == ENOENT)
-	{
-	  locker->refcnt = 0; /*FIXME?*/
-	  err = MU_ERR_LOCK_NOT_HELD;
-	  return err;
-	}
-      return err;
-    }
-  return 0;
-}
-
-int
-mu_locker_touchlock (mu_locker_t lock)
-{
-  if (!lock)
-    return MU_ERR_LOCKER_NULL;
-
-  if (MU_LOCKER_TYPE (lock) != MU_LOCKER_TYPE_DOTLOCK)
-    return 0;
-  
-  if (lock->refcnt > 0)
-    return utime (lock->data.dot.dotlock, NULL);
-
-  return MU_ERR_LOCK_NOT_HELD;
-}
-
-
-/* Kernel locking */
-static int
-init_kernel (mu_locker_t locker)
-{
-  return 0;
-}
-
-static int
-lock_kernel (mu_locker_t locker, enum mu_locker_mode mode)
-{
-  int fd;
-  struct flock fl;
-
-  switch (mode)
-    {
-    case mu_lck_shr:
-    case mu_lck_opt:
-      mode = O_RDONLY;
-      fl.l_type = F_RDLCK;
-      break;
-
-    case mu_lck_exc:
-      mode = O_RDWR;
-      fl.l_type = F_WRLCK;
-      break;
-
-    default:
-      return EINVAL;
-    }
-  
-  fd = open (locker->file, O_RDWR);
-  if (fd == -1)
-    return errno;
-  locker->data.kernel.fd = fd;
-  
-  fl.l_whence = SEEK_SET;
-  fl.l_start = 0;
-  fl.l_len = 0; /* Lock entire file */
-  if (fcntl (fd, F_SETLK, &fl))
-    {
-#ifdef EACCESS      
-      if (errno == EACCESS)
-	return EAGAIN;
-#endif
-      if (errno == EAGAIN)
-	return EAGAIN;
-      return errno;
-    }
-  return 0;
-}
-
-static int
-unlock_kernel (mu_locker_t locker)
-{
-  struct flock fl;
-
-  fl.l_type = F_UNLCK;
-  fl.l_whence = SEEK_SET;
-  fl.l_start = 0;
-  fl.l_len = 0; /* Unlock entire file */
-  if (fcntl (locker->data.kernel.fd, F_SETLK, &fl))
-    {
-#ifdef EACCESS
-      if (errno == EACCESS)
-	return EAGAIN;
-#endif
-      if (errno == EAGAIN)
-	return EAGAIN;
-      return errno;
-    }
-  close (locker->data.kernel.fd);
-  return 0;
-}
-
-static int
-init_external (mu_locker_t locker)
-{
-  if (!(locker->data.external.name = strdup (mu_locker_external_program ?
-					     mu_locker_external_program :
-					     MU_LOCKER_EXTERNAL_PROGRAM)))
-    return ENOMEM;
-  return 0;
-}
-
-static void
-destroy_external (mu_locker_t locker)
-{
-  free (locker->data.external.name);
-}
-
-/*
-  Estimate 1 decimal digit per 3 bits, + 1 for round off.
-*/
-#define DEC_DIGS_PER_INT (sizeof(int) * 8 / 3 + 1)
-
-static int
-external_locker (mu_locker_t l, int lock)
-{
-  int err = 0;
-  char *av[6];
-  int ac = 0;
-  char aforce[3 + DEC_DIGS_PER_INT + 1];
-  char aretry[3 + DEC_DIGS_PER_INT + 1];
-  int status = 0;
-
-  assert (l);
-  assert (l->flags & MU_LOCKER_EXTERNAL);
-  /* FIXME */
-  assert (lock == !l->refcnt);
-  /* lock is true, refcnt is 0 or lock is false and refcnt is 1 */
-
-  av[ac++] = l->data.external.name ?
-                   l->data.external.name : MU_LOCKER_EXTERNAL_PROGRAM;
-
-  if (l->flags & MU_LOCKER_TIME)
-    {
-      snprintf (aforce, sizeof (aforce), "-f%d", l->expire_time);
-      aforce[sizeof (aforce) - 1] = 0;
-      av[ac++] = aforce;
-    }
-  
-  if (l->flags & MU_LOCKER_RETRY)
-    {
-      snprintf (aretry, sizeof (aretry), "-r%d", l->retries);
-      aretry[sizeof (aretry) - 1] = 0;
-      av[ac++] = aretry;
-    }
-
-  if (!lock)
-    av[ac++] = "-u";
-
-  av[ac++] = l->file;
-
-  av[ac++] = NULL;
-
-  if ((err = mu_spawnvp (av[0], av, &status)))
-    return err;
-
-  if (!WIFEXITED (status))
-    {
-      err = MU_ERR_LOCK_EXT_KILLED;
-    }
-  else
-    {
-      switch (WEXITSTATUS (status))
-	{
-	case 127:
-	  err = MU_ERR_LOCK_EXT_FAIL;
-	  break;
-	  
-	case MU_DL_EX_OK:
-	  err = 0;
-	  l->refcnt = lock;
-	  break;
-	  
-	case MU_DL_EX_NEXIST:
-	  err = MU_ERR_LOCK_NOT_HELD;
-	  break;
-	  
-	case MU_DL_EX_EXIST:
-	  err = MU_ERR_LOCK_CONFLICT;
-	  break;
-	  
-	case MU_DL_EX_PERM:
-	  err = EPERM;
-	  break;
-	  
-	default:
-	case MU_DL_EX_ERROR:
-	  err = MU_ERR_LOCK_EXT_ERR;
-	  break;
-	}
-    }
-
-  return err;
-}
-
-static int
-lock_external (mu_locker_t locker, enum mu_locker_mode mode)
-{
-  return external_locker (locker, 1);
-}
-
-static int
-unlock_external (mu_locker_t locker)
-{
-  return external_locker (locker, 0);
-}
-
diff --git a/libmailutils/mailbox.c b/libmailutils/mailbox.c
deleted file mode 100644
index 56503f5..0000000
--- a/libmailutils/mailbox.c
+++ /dev/null
@@ -1,819 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 1999, 2000, 2001, 2004, 2005, 2006, 2007, 2008, 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 <stdlib.h>
-#include <errno.h>
-#include <string.h>
-
-#include <mailutils/debug.h>
-#include <mailutils/errno.h>
-#include <mailutils/error.h>
-#include <mailutils/folder.h>
-#include <mailutils/iterator.h>
-#include <mailutils/list.h>
-#include <mailutils/locker.h>
-#include <mailutils/observer.h>
-#include <mailutils/property.h>
-#include <mailutils/registrar.h>
-#include <mailutils/stream.h>
-#include <mailutils/url.h>
-#include <mailutils/attribute.h>
-#include <mailutils/message.h>
-#include <mailutils/mutil.h>
-
-#include <mailutils/sys/mailbox.h>
-#include <mailutils/sys/url.h>
-
-/* Mailbox-specific flags */
-#define _MU_MAILBOX_OPEN    0x10000000
-#define _MU_MAILBOX_REMOVED 0x20000000
-#define _MU_MAILBOX_MASK    0xF0000000
-
-static int
-mailbox_folder_create (mu_mailbox_t mbox, const char *name,
-		       mu_record_t record)
-{
-  int rc;
-  mu_url_t url;
-  
-  if ((rc = mu_url_uplevel (mbox->url, &url)))
-    {
-      if (rc == MU_ERR_NOENT)
-	{
-	  rc = mu_url_dup (mbox->url, &url);
-	  if (rc)
-	    return rc;
-	}
-      else
-	return rc;
-    }
-
-  rc = mu_folder_create_from_record (&mbox->folder, url, record);
-  if (rc)
-    mu_url_destroy (&url);
-  return rc;
-}
-
-int
-_mailbox_create_from_record (mu_mailbox_t *pmbox,
-			     mu_record_t record,
-			     mu_url_t url, 
-			     const char *name)
-{
-  mu_log_level_t level;
-  int (*m_init) (mu_mailbox_t) = NULL;
-      
-  mu_record_get_mailbox (record, &m_init);
-  if (m_init)
-    {
-      int status;
-      int (*u_init) (mu_url_t) = NULL;
-      mu_mailbox_t mbox;
-      
-      /* Allocate memory for mbox.  */
-      mbox = calloc (1, sizeof (*mbox));
-      if (mbox == NULL)
-	return ENOMEM;
-      
-      /* Initialize the internal lock now, so the concrete mailbox
-	 could use it. */
-      status = mu_monitor_create (&mbox->monitor, 0, mbox);
-      if (status != 0)
-	{
-	  mu_mailbox_destroy (&mbox);
-	  return status;
-	}
-      
-      /* Make sure scheme contains actual mailbox scheme */
-      /* FIXME: It is appropriate not for all record types.  For now we
-	 assume that if the record scheme ends with a plus sign, this
-	 should not be done.  Probably it requires some flag in struct
-	 _mu_record? */
-      if (strcmp (url->scheme, record->scheme))
-	{
-	  char *p = strdup (record->scheme);
-	  if (!p)
-	    {
-	      mu_mailbox_destroy (&mbox);
-	      return errno;
-	    }
-	  free (url->scheme);
-	  url->scheme = p;
-	}
-      
-      mu_record_get_url (record, &u_init);
-      if (u_init && (status = u_init (url)) != 0)
-	{
-	  mu_mailbox_destroy (&mbox);
-	  return status;
-	}
-      
-      mbox->url = url;
-      
-      /* Create the folder before initializing the concrete mailbox.
-	 The mailbox needs it's back pointer. */
-      status = mailbox_folder_create (mbox, name, record);
-      
-      if (status == 0)
-	status = m_init (mbox);   /* Create the concrete mailbox type.  */
-      
-      if (status != 0)
-	{
-	  /* Take care not to destroy url.  Leave it to caller. */
-	  mbox->url = NULL;
-	  mu_mailbox_destroy (&mbox);
-	}
-      else
-	{
-	  *pmbox = mbox;
-	  
-	  level = mu_global_debug_level ("mailbox");
-	  if (level)
-	    {
-	      int status = mu_debug_create (&mbox->debug, mbox);
-	      if (status)
-		return 0; /* FIXME: don't want to bail out just because I
-			     failed to create a *debug* object. But I may
-			     be wrong... */
-	      mu_debug_set_level (mbox->debug, level);
-	      if (level & MU_DEBUG_INHERIT)
-		mu_folder_set_debug (mbox->folder, mbox->debug);
-	    }
-	}
-      
-      return status;
-    }
-  return MU_ERR_NO_HANDLER;
-}
-
-static int
-_create_mailbox0 (mu_mailbox_t *pmbox, mu_url_t url, const char *name)
-{
-  mu_record_t record = NULL;
-
-  if (mu_registrar_lookup_url (url, MU_FOLDER_ATTRIBUTE_FILE, &record, NULL)
-      == 0)
-    return _mailbox_create_from_record (pmbox, record, url, name);
-  return MU_ERR_NO_HANDLER;
-}
-
-static int
-_create_mailbox (mu_mailbox_t *pmbox, const char *name)
-{
-  int status;
-  mu_url_t url;
-
-  status = mu_url_create (&url, name);
-  if (status)
-    return status;
-  status = mu_url_parse (url);
-  if (status == 0)
-    status = _create_mailbox0 (pmbox, url, name);
-  if (status)
-    mu_url_destroy (&url);
-  return status;
-}
-
-/* The Mailbox Factory.
-   Create an iterator for registrar and see if any url scheme match,
-   Then we call the mailbox's mu_url_create() to parse the URL. Last
-   initialize the concrete mailbox and folder.  */
-int
-mu_mailbox_create (mu_mailbox_t *pmbox, const char *name)
-{
-  if (pmbox == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-
-  return _create_mailbox (pmbox, name);
-}
-
-int
-mu_mailbox_create_from_url (mu_mailbox_t *pmbox, mu_url_t url)
-{
-  if (pmbox == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-  return _create_mailbox0 (pmbox, url, mu_url_to_string (url));
-}
-
-int
-mu_mailbox_create_from_record (mu_mailbox_t *pmbox, mu_record_t record,
-			       const char *name)
-{
-  mu_url_t url;
-  int rc;
-  
-  rc = mu_url_create (&url, name);
-  if (rc)
-    return rc;
-  rc = mu_url_parse (url);
-  if (rc == 0)
-    rc = _mailbox_create_from_record (pmbox, record, url, name);
-  if (rc)
-    mu_url_destroy (&url);
-  return rc;
-}
-
-void
-mu_mailbox_destroy (mu_mailbox_t *pmbox)
-{
-  if (pmbox && *pmbox)
-    {
-      mu_mailbox_t mbox = *pmbox;
-      mu_monitor_t monitor = mbox->monitor;
-
-      /* Notify the observers.  */
-      if (mbox->observable)
-	{
-	  mu_observable_notify (mbox->observable, MU_EVT_MAILBOX_DESTROY,
-				mbox);
-	  mu_observable_destroy (&mbox->observable, mbox);
-	}
-
-      /* Call the concrete mailbox _destroy method. So it can clean itself.  */
-      if (mbox->_destroy)
-	mbox->_destroy (mbox);
-
-      mu_monitor_wrlock (monitor);
-
-      /* Close the stream and nuke it */
-      if (mbox->stream)
-	{
-	  /* FIXME: Is this right, should the client be responsible
-	     for closing the stream?  */
-	  /* mu_stream_close (mbox->stream); */
-	  mu_stream_destroy (&mbox->stream);
-	}
-
-      if (mbox->url)
-        mu_url_destroy (&mbox->url);
-
-      if (mbox->locker)
-	mu_locker_destroy (&mbox->locker);
-
-      if (mbox->debug)
-	mu_debug_destroy (&mbox->debug, mbox);
-
-      if (mbox->folder)
-	mu_folder_destroy (&mbox->folder);
-
-      if (mbox->property)
-	mu_property_destroy (&mbox->property, mbox);
-
-      free (mbox);
-      *pmbox = NULL;
-      mu_monitor_unlock (monitor);
-      mu_monitor_destroy (&monitor, mbox);
-    }
-}
-
-
-/* -------------- stub functions ------------------- */
-
-int
-mu_mailbox_open (mu_mailbox_t mbox, int flag)
-{
-  int rc;
-  
-  if (!mbox)
-    return MU_ERR_MBX_NULL;
-  if (mbox->_open == NULL)
-    return MU_ERR_EMPTY_VFN;
-  if (mbox->flags & _MU_MAILBOX_OPEN)
-    return MU_ERR_OPEN;
-  if (flag & MU_STREAM_QACCESS)
-    {
-      /* Quick access mailboxes are read-only */
-      if (flag & (MU_STREAM_WRITE | MU_STREAM_RDWR
-		  | MU_STREAM_APPEND | MU_STREAM_CREAT))
-	return EINVAL; /* FIXME: Better error code, please? */
-    }
-  rc = mbox->_open (mbox, flag);
-  if (rc == 0)
-    mbox->flags |= _MU_MAILBOX_OPEN;
-  return rc;
-}
-
-int
-mu_mailbox_close (mu_mailbox_t mbox)
-{
-  int rc;
-
-  if (!mbox)
-    return MU_ERR_MBX_NULL;
-  if (!(mbox->flags & _MU_MAILBOX_OPEN))
-    return MU_ERR_NOT_OPEN;
-  if (mbox == NULL || mbox->_close == NULL)
-    return MU_ERR_EMPTY_VFN;
-
-  rc = mbox->_close (mbox);
-  if (rc == 0)
-    mbox->flags &= ~_MU_MAILBOX_OPEN;
-  return rc;
-}
-
-int
-mu_mailbox_remove (mu_mailbox_t mbox)
-{
-  if (!mbox)
-    return MU_ERR_MBX_NULL;
-  if (mbox->flags & _MU_MAILBOX_OPEN)
-    return MU_ERR_OPEN;
-  if (mbox->flags & _MU_MAILBOX_REMOVED)
-    return MU_ERR_MBX_REMOVED;
-  if (!mbox->_remove)
-    return MU_ERR_EMPTY_VFN;
-  return mbox->_remove (mbox);
-}
-
-int
-mu_mailbox_flush (mu_mailbox_t mbox, int expunge)
-{
-  size_t i, total = 0;
-  int status = 0;
-  
-  if (!mbox)
-    return MU_ERR_MBX_NULL;
-  if (mbox->flags & _MU_MAILBOX_REMOVED)
-    return MU_ERR_MBX_REMOVED;
-  if (!(mbox->flags & _MU_MAILBOX_OPEN))
-    return _MU_MAILBOX_OPEN;
-  if (!(mbox->flags & (MU_STREAM_RDWR|MU_STREAM_WRITE|MU_STREAM_APPEND)))
-    return 0;
-
-  mu_mailbox_messages_count (mbox, &total);
-  if (!(mbox->flags & MU_STREAM_APPEND))
-    for (i = 1; i <= total; i++)
-      {
-	mu_message_t msg = NULL;
-	mu_attribute_t attr = NULL;
-	mu_mailbox_get_message (mbox, i, &msg);
-	mu_message_get_attribute (msg, &attr);
-	mu_attribute_set_seen (attr);
-      }
-
-  if (expunge)
-    status = mu_mailbox_expunge (mbox);
-  else
-    status = mu_mailbox_sync (mbox);
-
-  return status;
-}
-
-#define _MBOX_CHECK_FLAGS(mbox)			\
-  if (mbox == NULL)				\
-    return MU_ERR_MBX_NULL;			\
-  if (mbox->flags & _MU_MAILBOX_REMOVED)	\
-    return MU_ERR_MBX_REMOVED;			\
-  if (!(mbox->flags & _MU_MAILBOX_OPEN))	\
-    return _MU_MAILBOX_OPEN
-
-#define _MBOX_CHECK(mbox,method)		\
-  _MBOX_CHECK_FLAGS(mbox);			\
-  if (mbox->method == NULL)			\
-    return MU_ERR_EMPTY_VFN
-
-#define _MBOX_CHECK_Q(mbox,method)		\
-  _MBOX_CHECK(mbox,method);			\
-  if (mbox->flags & MU_STREAM_QACCESS)		\
-    return MU_ERR_BADOP
-
-/* messages */
-int
-mu_mailbox_append_message (mu_mailbox_t mbox, mu_message_t msg)
-{
-  _MBOX_CHECK_Q (mbox, _append_message);
-  if (!(mbox->flags & (MU_STREAM_RDWR|MU_STREAM_WRITE|MU_STREAM_APPEND)))
-    return EACCES;
-  return mbox->_append_message (mbox, msg);
-}
-
-int
-mu_mailbox_get_message (mu_mailbox_t mbox, size_t msgno,  mu_message_t *pmsg)
-{
-  _MBOX_CHECK_Q (mbox, _get_message);
-  return mbox->_get_message (mbox, msgno, pmsg);
-}
-
-int
-mu_mailbox_quick_get_message (mu_mailbox_t mbox, mu_message_qid_t qid,
-			      mu_message_t *pmsg)
-{
-  _MBOX_CHECK (mbox, _quick_get_message);
-  if (!(mbox->flags & MU_STREAM_QACCESS))
-    return MU_ERR_BADOP;
-  return mbox->_quick_get_message (mbox, qid, pmsg);
-}
-
-int
-mu_mailbox_messages_count (mu_mailbox_t mbox, size_t *num)
-{
-  _MBOX_CHECK_Q (mbox, _messages_count);
-  return mbox->_messages_count (mbox, num);
-}
-
-int
-mu_mailbox_messages_recent (mu_mailbox_t mbox, size_t *num)
-{
-  _MBOX_CHECK_Q (mbox, _messages_recent);
-  return mbox->_messages_recent (mbox, num);
-}
-
-int
-mu_mailbox_message_unseen (mu_mailbox_t mbox, size_t *num)
-{
-  _MBOX_CHECK_Q (mbox, _message_unseen);
-  return mbox->_message_unseen (mbox, num);
-}
-
-int
-mu_mailbox_sync (mu_mailbox_t mbox)
-{
-  _MBOX_CHECK_Q (mbox, _sync);
-  if (!(mbox->flags & (MU_STREAM_RDWR|MU_STREAM_WRITE|MU_STREAM_APPEND)))
-    return 0;
-  return mbox->_sync (mbox);
-}
-
-/* Historic alias: */
-int
-mu_mailbox_save_attributes (mu_mailbox_t mbox)
-{
-  return mu_mailbox_sync (mbox);
-}
-
-int
-mu_mailbox_expunge (mu_mailbox_t mbox)
-{
-  _MBOX_CHECK_Q (mbox, _expunge);
-  if (!(mbox->flags & (MU_STREAM_RDWR|MU_STREAM_WRITE|MU_STREAM_APPEND)))
-    return EACCES;
-  return mbox->_expunge (mbox);
-}
-
-int
-mu_mailbox_is_updated (mu_mailbox_t mbox)
-{
-  if (mbox == NULL ||
-      !(mbox->flags & _MU_MAILBOX_OPEN) ||
-      (mbox->flags & _MU_MAILBOX_REMOVED) ||
-      mbox->_is_updated == NULL)
-    return 1;
-  if (mbox->flags & MU_STREAM_QACCESS)
-    return 1;
-  return mbox->_is_updated (mbox);
-}
-
-int
-mu_mailbox_scan (mu_mailbox_t mbox, size_t msgno, size_t *pcount)
-{
-  _MBOX_CHECK_Q (mbox, _scan);
-  return mbox->_scan (mbox, msgno, pcount);
-}
-
-int
-mu_mailbox_get_size (mu_mailbox_t mbox, mu_off_t *psize)
-{
-  int status;
-
-  _MBOX_CHECK_FLAGS (mbox);
-  if (mbox->flags & MU_STREAM_QACCESS)
-    return MU_ERR_BADOP;
-  if (mbox->_get_size == NULL
-      || (status = mbox->_get_size (mbox, psize)) == ENOSYS)
-    {
-      /* Fall back to brute-force method */
-      size_t i, total;
-      mu_off_t size = 0;
-      
-      status = mu_mailbox_messages_count (mbox, &total);
-      if (status)
-	return status;
-      for (i = 1; i <= total; i++)
-	{
-	  mu_message_t msg;
-	  size_t msgsize;
-	  status = mu_mailbox_get_message (mbox, i, &msg);
-	  if (status)
-	    return status;
-	  status = mu_message_size (msg, &msgsize);
-	  if (status)
-	    return status;
-	  size += msgsize;
-	}
-      *psize = size;
-    }
-  return status;
-}
-
-int
-mu_mailbox_uidvalidity (mu_mailbox_t mbox, unsigned long *pvalid)
-{
-  _MBOX_CHECK_Q (mbox, _uidvalidity);
-  return mbox->_uidvalidity (mbox, pvalid);
-}
-
-int
-mu_mailbox_uidnext (mu_mailbox_t mbox, size_t *puidnext)
-{
-  _MBOX_CHECK_Q (mbox, _uidnext);
-  return mbox->_uidnext (mbox, puidnext);
-}
-
-/* locking */
-int
-mu_mailbox_set_locker (mu_mailbox_t mbox, mu_locker_t locker)
-{
-  if (mbox == NULL)
-    return MU_ERR_MBX_NULL;
-  if (mbox->locker)
-    mu_locker_destroy (&mbox->locker);
-  mbox->locker = locker;
-  return 0;
-}
-
-int
-mu_mailbox_get_locker (mu_mailbox_t mbox, mu_locker_t *plocker)
-{
-  if (mbox == NULL)
-    return MU_ERR_MBX_NULL;
-  if (plocker == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-  *plocker = mbox->locker;
-  return 0;
-}
-
-int
-mu_mailbox_get_flags (mu_mailbox_t mbox, int *flags)
-{
-  if (mbox == NULL)
-    return MU_ERR_MBX_NULL;
-  if (!*flags)
-    return MU_ERR_OUT_NULL;
-  *flags = mbox->flags & ~_MU_MAILBOX_MASK;
-  return 0;
-}
-
-int
-mu_mailbox_set_stream (mu_mailbox_t mbox, mu_stream_t stream)
-{
-  if (mbox == NULL)
-    return MU_ERR_MBX_NULL;
-  if (mbox->flags & MU_STREAM_QACCESS)
-    return MU_ERR_BADOP;
-  if (mbox->stream)
-    mu_stream_destroy (&mbox->stream);
-  mbox->stream = stream;
-  return 0;
-}
-
-/* FIXME: This is a problem.  We provide a mu_mailbox_get_stream ()
-   and this stream is special it should, in theory, represent
-   a "view" of a flow of messages.  But providing this perspective
-   may make sense for local mailboxes but downright impossible
-   for a remote mailbox, short on downloading the entire mailbox
-   locally.
-   The question is : should this function be removed?
-   So far it as been used on local mailboxes to get offsets.  */
-int
-mu_mailbox_get_stream (mu_mailbox_t mbox, mu_stream_t *pstream)
-{
-  /* FIXME: Deprecation warning */
-  if (mbox == NULL)
-    return MU_ERR_MBX_NULL;
-  if (pstream == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-
-  /* If null two cases:
-     - it is no open yet.
-     - it a remote stream and the socket stream is on the folder.  */
-  if (mbox->stream == NULL)
-    {
-      if (mbox->folder)
-	return mu_folder_get_stream (mbox->folder, pstream);
-    }
-  *pstream = mbox->stream;
-  return 0;
-}
-
-int
-mu_mailbox_get_streamref (mu_mailbox_t mbox, mu_stream_t *pstream)
-{
-  if (mbox == NULL)
-    return MU_ERR_MBX_NULL;
-  if (pstream == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-
-  /* If null two cases:
-     - it is no open yet.
-     - it a remote stream and the socket stream is on the folder.  */
-  if (mbox->stream == NULL)
-    {
-      if (mbox->folder)
-	return mu_folder_get_streamref (mbox->folder, pstream);
-    }
-  return mu_streamref_create (pstream, mbox->stream);
-}
-
-int
-mu_mailbox_get_observable (mu_mailbox_t mbox, mu_observable_t *pobservable)
-{
-  if (mbox == NULL)
-    return MU_ERR_MBX_NULL;
-  if (pobservable == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-
-  if (mbox->observable == NULL)
-    {
-      int status = mu_observable_create (&mbox->observable, mbox);
-      if (status != 0)
-	return status;
-    }
-  *pobservable = mbox->observable;
-  return 0;
-}
-
-int
-mu_mailbox_get_property (mu_mailbox_t mbox, mu_property_t *pproperty)
-{
-  if (mbox == NULL)
-    return MU_ERR_MBX_NULL;
-  if (pproperty == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-  
-  if (mbox->property == NULL)
-    {
-      int status = mu_property_create (&mbox->property, mbox);
-      if (status != 0)
-	return status;
-    }
-  *pproperty = mbox->property;
-  return 0;
-}
-
-int
-mu_mailbox_has_debug (mu_mailbox_t mailbox)
-{
-  if (mailbox == NULL)
-    return 0;
-
-  return mailbox->debug ? 1 : 0;
-}
-
-int
-mu_mailbox_set_debug (mu_mailbox_t mbox, mu_debug_t debug)
-{
-  if (mbox == NULL)
-    return MU_ERR_MBX_NULL;
-  if (mbox->debug)
-    mu_debug_destroy (&mbox->debug, mbox);
-  mbox->debug = debug;
-  /* FIXME: Honor MU_DEBUG_INHERIT */
-  if (!mu_folder_has_debug (mbox->folder))
-    mu_folder_set_debug (mbox->folder, debug);
-  return 0;
-}
-
-int
-mu_mailbox_get_debug (mu_mailbox_t mbox, mu_debug_t *pdebug)
-{
-  if (mbox == NULL)
-    return MU_ERR_MBX_NULL;
-  if (pdebug == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-  if (mbox->debug == NULL)
-    {
-      int status = mu_debug_create (&mbox->debug, mbox);
-      if (status != 0)
-	return status;
-      /* FIXME: MU_DEBUG_INHERIT?? */
-      if (!mu_folder_has_debug (mbox->folder))
-	mu_folder_set_debug (mbox->folder, mbox->debug);
-    }
-  *pdebug = mbox->debug;
-  return 0;
-}
-
-int
-mu_mailbox_get_url (mu_mailbox_t mbox, mu_url_t *purl)
-{
-  if (mbox == NULL)
-    return MU_ERR_MBX_NULL;
-  if (purl == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-  *purl = mbox->url;
-  return 0;
-}
-
-int
-mu_mailbox_get_folder (mu_mailbox_t mbox, mu_folder_t *pfolder)
-{
-  if (mbox == NULL)
-    return MU_ERR_MBX_NULL;
-  if (pfolder == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-  *pfolder = mbox->folder;
-  return 0;
-}
-
-int
-mu_mailbox_set_folder (mu_mailbox_t mbox, mu_folder_t folder)
-{
-  if (mbox == NULL)
-    return MU_ERR_MBX_NULL;
-   mbox->folder = folder;
-  return 0;
-}
-
-int
-mu_mailbox_lock (mu_mailbox_t mbox)
-{
-  mu_locker_t lock = NULL;
-  mu_mailbox_get_locker (mbox, &lock);
-  return mu_locker_lock (lock);
-}
-
-int
-mu_mailbox_unlock (mu_mailbox_t mbox)
-{
-  mu_locker_t lock = NULL;
-  mu_mailbox_get_locker (mbox, &lock);
-  return mu_locker_unlock (lock);
-}
-
-int
-mu_mailbox_get_uidls (mu_mailbox_t mbox, mu_list_t *plist)
-{
-  mu_list_t list;
-  int status;
-
-  if (mbox == NULL)
-    return MU_ERR_MBX_NULL;
-  if (plist == NULL)
-    return EINVAL;
-  status = mu_list_create (&list);
-  if (status)
-    return status;
-  mu_list_set_destroy_item (list, mu_list_free_item);
-  if (mbox->_get_uidls)
-    status = mbox->_get_uidls (mbox, list);
-  else
-    {
-      size_t i, total;
-
-      status = mu_mailbox_messages_count (mbox, &total);
-      if (status)
-	return status;
-      for (i = 1; i <= total; i++)
-	{
-	  mu_message_t msg = NULL;
-	  char buf[MU_UIDL_BUFFER_SIZE];
-	  size_t n;
-	  struct mu_uidl *uidl;
-	  
-	  status = mu_mailbox_get_message (mbox, i, &msg);
-	  if (status)
-	    break;
-	  status = mu_message_get_uidl (msg, buf, sizeof (buf), &n);
-	  if (status)
-	    break;
-	  uidl = malloc (sizeof (uidl[0]));
-	  if (!uidl)
-	    {
-	      status = ENOMEM;
-	      break;
-	    }
-	  uidl->msgno = i;
-	  strncpy (uidl->uidl, strdup (buf), MU_UIDL_BUFFER_SIZE);
-	  status = mu_list_append (list, uidl);
-	  if (status)
-	    {
-	      free (uidl);
-	      break;
-	    }
-	}
-    }
-  *plist = list;
-  return status;
-}
-
-	  
diff --git a/libmailutils/mailbox/Makefile.am b/libmailutils/mailbox/Makefile.am
new file mode 100644
index 0000000..5fffd57
--- /dev/null
+++ b/libmailutils/mailbox/Makefile.am
@@ -0,0 +1,33 @@
+# GNU Mailutils -- a suite of utilities for electronic mail
+# Copyright (C) 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, see
+# <http://www.gnu.org/licenses/>.
+
+noinst_LTLIBRARIES = libmailbox.la
+
+libmailbox_la_SOURCES =  \
+ mailbox.c\
+ mbx_default.c\
+ mbxitr.c\
+ attribute.c\
+ body.c\
+ envelope.c\
+ folder.c\
+ hdritr.c\
+ header.c\
+ message.c\
+ msgscan.c
+
+INCLUDES = @MU_LIB_COMMON_INCLUDES@ -I/libmailutils
diff --git a/libmailutils/mailbox/attribute.c b/libmailutils/mailbox/attribute.c
new file mode 100644
index 0000000..37e479a
--- /dev/null
+++ b/libmailutils/mailbox/attribute.c
@@ -0,0 +1,469 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 2004, 2005, 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, see
+   <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <sys/types.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+
+#ifdef HAVE_STRINGS_H
+# include <strings.h>
+#endif
+
+#include <mailutils/errno.h>
+#include <mailutils/mutil.h>
+#include <mailutils/cstr.h>
+#include <mailutils/sys/attribute.h>
+
+int
+mu_attribute_create (mu_attribute_t *pattr, void *owner)
+{
+  mu_attribute_t attr;
+  if (pattr == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+  attr = calloc (1, sizeof(*attr));
+  if (attr == NULL)
+    return ENOMEM;
+  attr->owner = owner;
+  *pattr = attr;
+  return 0;
+}
+
+void
+mu_attribute_destroy (mu_attribute_t *pattr, void *owner)
+{
+  if (pattr && *pattr)
+    {
+      mu_attribute_t attr = *pattr;
+      if (attr->owner == owner)
+	free (*pattr);
+      /* Loose the link */
+      *pattr = NULL;
+    }
+}
+
+void *
+mu_attribute_get_owner (mu_attribute_t attr)
+{
+  return (attr) ? attr->owner : NULL;
+}
+
+int
+mu_attribute_is_modified (mu_attribute_t attr)
+{
+  return (attr) ? attr->flags & MU_ATTRIBUTE_MODIFIED : 0;
+}
+
+int
+mu_attribute_clear_modified (mu_attribute_t attr)
+{
+  if (attr)
+    attr->flags &= ~MU_ATTRIBUTE_MODIFIED;
+  return 0;
+}
+
+int
+mu_attribute_set_modified (mu_attribute_t attr)
+{
+  if (attr)
+    attr->flags |= MU_ATTRIBUTE_MODIFIED;
+  return 0;
+}
+
+int
+mu_attribute_get_flags (mu_attribute_t attr, int *pflags)
+{
+  if (attr == NULL)
+    return EINVAL;
+  if (pflags == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+  if (attr->_get_flags)
+    return attr->_get_flags (attr, pflags);
+  *pflags = attr->flags;
+  return 0;
+}
+
+int
+mu_attribute_set_flags (mu_attribute_t attr, int flags)
+{
+  int status = 0;
+  int oflags = 0;
+  
+  if (attr == NULL)
+    return EINVAL;
+
+  /* If the required bits are already set, do not modify anything */
+  mu_attribute_get_flags (attr, &oflags);
+  if ((oflags & flags) == flags)
+    return 0;
+  
+  if (attr->_set_flags)
+    status = attr->_set_flags (attr, flags);
+  else
+    attr->flags |= flags;
+  if (status == 0)
+    mu_attribute_set_modified (attr);
+  return 0;
+}
+
+int
+mu_attribute_unset_flags (mu_attribute_t attr, int flags)
+{
+  int status = 0;
+  int oflags = 0;
+
+  if (attr == NULL)
+    return EINVAL;
+
+  /* If the required bits are already cleared, do not modify anything */
+  mu_attribute_get_flags (attr, &oflags);
+  if ((oflags & flags) == 0)
+    return 0;
+
+  if (attr->_unset_flags)
+    status = attr->_unset_flags (attr, flags);
+  else
+    attr->flags &= ~flags;
+  if (status == 0)
+    mu_attribute_set_modified (attr);
+  return 0;
+}
+
+int
+mu_attribute_set_get_flags (mu_attribute_t attr, int (*_get_flags)
+			 (mu_attribute_t, int *), void *owner)
+{
+  if (attr == NULL)
+    return EINVAL;
+  if (attr->owner != owner)
+    return EACCES;
+  attr->_get_flags = _get_flags;
+  return 0;
+}
+
+int
+mu_attribute_set_set_flags (mu_attribute_t attr, int (*_set_flags)
+			 (mu_attribute_t, int), void *owner)
+{
+  if (attr == NULL)
+    return EINVAL;
+  if (attr->owner != owner)
+    return EACCES;
+  attr->_set_flags = _set_flags;
+  return 0;
+}
+
+int
+mu_attribute_set_unset_flags (mu_attribute_t attr, int (*_unset_flags)
+			 (mu_attribute_t, int), void *owner)
+{
+  if (attr == NULL)
+    return EINVAL;
+  if (attr->owner != owner)
+    return EACCES;
+  attr->_unset_flags = _unset_flags;
+  return 0;
+}
+
+/* We add support for "USER" flag, it is a way for external objects
+   Not being the owner to add custom flags.  */
+int
+mu_attribute_set_userflag (mu_attribute_t attr, int flag)
+{
+  if (attr == NULL)
+    return EINVAL;
+  attr->user_flags |= flag;
+  return 0;
+}
+
+int
+mu_attribute_set_seen (mu_attribute_t attr)
+{
+  return mu_attribute_set_flags (attr, MU_ATTRIBUTE_SEEN);
+}
+
+int
+mu_attribute_set_answered (mu_attribute_t attr)
+{
+  return mu_attribute_set_flags (attr, MU_ATTRIBUTE_ANSWERED);
+}
+
+int
+mu_attribute_set_flagged (mu_attribute_t attr)
+{
+  return mu_attribute_set_flags (attr, MU_ATTRIBUTE_FLAGGED);
+}
+
+int
+mu_attribute_set_read (mu_attribute_t attr)
+{
+  return mu_attribute_set_flags (attr, MU_ATTRIBUTE_READ);
+}
+
+int
+mu_attribute_set_deleted (mu_attribute_t attr)
+{
+  return mu_attribute_set_flags (attr, MU_ATTRIBUTE_DELETED);
+}
+
+int
+mu_attribute_set_draft (mu_attribute_t attr)
+{
+  return mu_attribute_set_flags (attr, MU_ATTRIBUTE_DRAFT);
+}
+
+int
+mu_attribute_set_recent (mu_attribute_t attr)
+{
+  int status = mu_attribute_unset_flags (attr, MU_ATTRIBUTE_READ);
+  if (status == 0)
+    status = mu_attribute_unset_flags (attr, MU_ATTRIBUTE_SEEN);
+  return status;
+}
+
+int
+mu_attribute_is_userflag (mu_attribute_t attr, int flag)
+{
+  if (attr == NULL)
+    return 0;
+  return attr->user_flags & flag;
+}
+
+int
+mu_attribute_is_seen (mu_attribute_t attr)
+{
+  int flags = 0;
+  if (mu_attribute_get_flags (attr, &flags) == 0)
+    return flags & MU_ATTRIBUTE_SEEN;
+  return 0;
+}
+
+int
+mu_attribute_is_answered (mu_attribute_t attr)
+{
+  int flags = 0;
+  if (mu_attribute_get_flags (attr, &flags) == 0)
+    return flags & MU_ATTRIBUTE_ANSWERED;
+  return 0;
+}
+
+int
+mu_attribute_is_flagged (mu_attribute_t attr)
+{
+  int flags = 0;
+  if (mu_attribute_get_flags (attr, &flags) == 0)
+    return flags & MU_ATTRIBUTE_FLAGGED;
+  return 0;
+}
+
+int
+mu_attribute_is_read (mu_attribute_t attr)
+{
+  int flags = 0;
+  if (mu_attribute_get_flags (attr, &flags) == 0)
+    return flags & MU_ATTRIBUTE_READ;
+  return 0;
+}
+
+int
+mu_attribute_is_deleted (mu_attribute_t attr)
+{
+  int flags = 0;
+  if (mu_attribute_get_flags (attr, &flags) == 0)
+    return flags & MU_ATTRIBUTE_DELETED;
+  return 0;
+}
+
+int
+mu_attribute_is_draft (mu_attribute_t attr)
+{
+  int flags = 0;
+  if (mu_attribute_get_flags (attr, &flags) == 0)
+    return flags & MU_ATTRIBUTE_DRAFT;
+  return 0;
+}
+
+int
+mu_attribute_is_recent (mu_attribute_t attr)
+{
+  int flags = 0;
+  if (mu_attribute_get_flags (attr, &flags) == 0)
+    return MU_ATTRIBUTE_IS_UNSEEN(flags);
+  return 0;
+}
+
+int
+mu_attribute_unset_userflag (mu_attribute_t attr, int flag)
+{
+  if (attr == NULL)
+    return 0;
+  attr->user_flags &= ~flag;
+  return 0;
+}
+
+int
+mu_attribute_unset_seen (mu_attribute_t attr)
+{
+  return mu_attribute_unset_flags (attr, MU_ATTRIBUTE_SEEN);
+}
+
+int
+mu_attribute_unset_answered (mu_attribute_t attr)
+{
+  return mu_attribute_unset_flags (attr, MU_ATTRIBUTE_ANSWERED);
+}
+
+int
+mu_attribute_unset_flagged (mu_attribute_t attr)
+{
+  return mu_attribute_unset_flags (attr, MU_ATTRIBUTE_FLAGGED);
+}
+
+int
+mu_attribute_unset_read (mu_attribute_t attr)
+{
+  return mu_attribute_unset_flags (attr, MU_ATTRIBUTE_READ);
+}
+
+int
+mu_attribute_unset_deleted (mu_attribute_t attr)
+{
+  return mu_attribute_unset_flags (attr, MU_ATTRIBUTE_DELETED);
+}
+
+int
+mu_attribute_unset_draft (mu_attribute_t attr)
+{
+  return mu_attribute_unset_flags (attr, MU_ATTRIBUTE_DRAFT);
+}
+
+int
+mu_attribute_unset_recent (mu_attribute_t attr)
+{
+  return mu_attribute_unset_flags (attr, MU_ATTRIBUTE_SEEN);
+}
+
+int
+mu_attribute_is_equal (mu_attribute_t attr, mu_attribute_t attr2)
+{
+  int flags2 = 0, flags = 0;
+  mu_attribute_get_flags (attr, &flags);
+  mu_attribute_get_flags (attr2, &flags2);
+  return flags == flags;
+}
+
+/*   Miscellaneous.  */
+int
+mu_attribute_copy (mu_attribute_t dest, mu_attribute_t src)
+{
+  if (dest == NULL || src == NULL)
+    return EINVAL;
+  /* Can not be a deep copy.  */
+  /* memcpy (dest, src, sizeof (*dest)); */
+  dest->flags = src->flags;
+  return 0;
+}
+
+struct flagtrans
+{
+  int flag;
+  char letter;
+};
+
+/* The two macros below are taken from gnulib module verify.h */
+#define mu_verify_true(R) \
+  (!!sizeof \
+   (struct { unsigned int verify_error_if_negative_size__: (R) ? 1 : -1; }))
+#define mu_verify(R) extern int (* verify_function__ (void)) [mu_verify_true (R)]
+
+static struct flagtrans flagtrans[] = {
+  { MU_ATTRIBUTE_SEEN, 'O' },
+  { MU_ATTRIBUTE_ANSWERED, 'A' },
+  { MU_ATTRIBUTE_FLAGGED, 'F' },
+  { MU_ATTRIBUTE_READ, 'R' },
+  { MU_ATTRIBUTE_DELETED, 'd' },
+  { 0 }
+};
+
+/* If cc reports an error in this statement, fix the MU_STATUS_BUF_SIZE
+   declaration in include/mailutils/attribute.h */
+mu_verify (MU_ARRAY_SIZE (flagtrans) == MU_STATUS_BUF_SIZE);
+
+int
+mu_string_to_flags (const char *buffer, int *pflags)
+{
+  const char *sep;
+
+  if (pflags == NULL)
+    return EINVAL;
+
+  /* Set the attribute */
+  if (mu_c_strncasecmp (buffer, "Status:", 7) == 0)
+    {
+      sep = strchr(buffer, ':'); /* pass the ':' */
+      sep++;
+    }
+  else
+    sep = buffer;
+
+  for (; *sep; sep++)
+    {
+      struct flagtrans *ft;
+
+      for (ft = flagtrans; ft->flag; ft++)
+	if (ft->letter == *sep)
+	  {
+	    *pflags |= ft->flag;
+	    break;
+	  }
+    }
+  return 0;
+}
+
+/* NOTE: When adding/removing flags, make sure to update the
+   MU_STATUS_BUF_SIZE define in include/mailutils/attribute.h */
+int
+mu_attribute_to_string (mu_attribute_t attr, char *buffer, size_t len,
+			size_t *pn)
+{
+  int flags = 0;
+  char buf[MU_STATUS_BUF_SIZE];
+  int i;
+  int rc;
+  struct flagtrans *ft;
+  
+  rc = mu_attribute_get_flags (attr, &flags);
+  if (rc)
+    return rc;
+
+  i = 0;
+  for (ft = flagtrans; ft->flag; ft++)
+    if (ft->flag & flags)
+      buf[i++] = ft->letter;
+  buf[i++] = 0;
+
+  i = mu_cpystr (buffer, buf, i);
+  if (pn)
+    *pn = i;
+  return 0;
+}
+
diff --git a/libmailutils/mailbox/body.c b/libmailutils/mailbox/body.c
new file mode 100644
index 0000000..b4cf22b
--- /dev/null
+++ b/libmailutils/mailbox/body.c
@@ -0,0 +1,416 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 2004, 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, see 
+   <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+#include <mailutils/stream.h>
+#include <mailutils/mutil.h>
+#include <mailutils/errno.h>
+#include <mailutils/sys/stream.h>
+#include <mailutils/sys/body.h>
+
+#define BODY_MODIFIED 0x10000
+
+static int _body_flush    (mu_stream_t);
+static int _body_read     (mu_stream_t, char *, size_t, size_t *);
+static int _body_truncate (mu_stream_t, mu_off_t);
+static int _body_size     (mu_stream_t, mu_off_t *);
+static int _body_write    (mu_stream_t, const char *, size_t, size_t *);
+static int _body_ioctl    (mu_stream_t, int, void *);
+static int _body_seek     (mu_stream_t, mu_off_t, mu_off_t *);
+static const char *_body_error_string (mu_stream_t, int);
+
+/* Our own defaults for the body.  */
+static int _body_get_size   (mu_body_t, size_t *);
+static int _body_get_lines  (mu_body_t, size_t *);
+static int _body_get_size0  (mu_stream_t, size_t *);
+static int _body_get_lines0 (mu_stream_t, size_t *);
+
+int
+mu_body_create (mu_body_t *pbody, void *owner)
+{
+  mu_body_t body;
+
+  if (pbody == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+  if (owner == NULL)
+    return EINVAL;
+
+  body = calloc (1, sizeof (*body));
+  if (body == NULL)
+    return ENOMEM;
+
+  body->owner = owner;
+  *pbody = body;
+  return 0;
+}
+
+void
+mu_body_destroy (mu_body_t *pbody, void *owner)
+{
+  if (pbody && *pbody)
+    {
+      mu_body_t body = *pbody;
+      if (body->owner == owner)
+	{
+	  if (body->filename)
+	    {
+	      /* FIXME: should we do this?  */
+	      remove (body->filename);
+	      free (body->filename);
+	    }
+
+	  if (body->stream)
+	    mu_stream_destroy (&body->stream);
+
+	  if (body->fstream)
+	    {
+	      mu_stream_close (body->fstream);
+	      mu_stream_destroy (&body->fstream);
+	    }
+
+	  free (body);
+	}
+      *pbody = NULL;
+    }
+}
+
+void *
+mu_body_get_owner (mu_body_t body)
+{
+  return (body) ? body->owner : NULL;
+}
+
+/* FIXME: not implemented.  */
+int
+mu_body_is_modified (mu_body_t body)
+{
+  return (body) ? (body->flags & BODY_MODIFIED) : 0;
+}
+
+/* FIXME: not implemented.  */
+int
+mu_body_clear_modified (mu_body_t body)
+{
+  if (body)
+    body->flags &= ~BODY_MODIFIED;
+  return 0;
+}
+
+int
+mu_body_get_filename (mu_body_t body, char *filename, size_t len, size_t *pn)
+{
+  int n = 0;
+  if (body == NULL)
+    return EINVAL;
+  if (body->filename)
+    {
+      n = strlen (body->filename);
+      if (filename && len > 0)
+	{
+	  len--; /* Space for the null.  */
+	  strncpy (filename, body->filename, len)[len] = '\0';
+	}
+    }
+  if (pn)
+    *pn = n;
+  return 0;
+}
+
+
+struct _mu_body_stream
+{
+  struct _mu_stream stream;
+  mu_body_t body;
+};
+
+static int
+_body_get_stream (mu_body_t body, mu_stream_t *pstream, int ref)
+{
+  if (body == NULL)
+    return EINVAL;
+  if (pstream == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+
+  if (body->stream == NULL)
+    {
+      if (body->_get_stream)
+	{
+	  int status = body->_get_stream (body, &body->stream);
+	  if (status)
+	    return status;
+	}
+      else
+	{
+	  int status;
+	  struct _mu_body_stream *str =
+	    (struct _mu_body_stream *)
+	    _mu_stream_create (sizeof (*str),
+			       MU_STREAM_RDWR|MU_STREAM_SEEK|_MU_STR_OPEN);
+	  if (!str)
+	    return ENOMEM;
+	  
+	  /* Create the temporary file.  */
+	  body->filename = mu_tempname (NULL);
+	  status = mu_file_stream_create (&body->fstream, 
+					  body->filename, MU_STREAM_RDWR);
+	  if (status != 0)
+	    return status;
+	  mu_stream_set_buffer (body->fstream, mu_buffer_full, 0);
+	  str->stream.ctl = _body_ioctl;
+	  str->stream.read = _body_read;
+	  str->stream.write = _body_write;
+	  str->stream.truncate = _body_truncate;
+	  str->stream.size = _body_size;
+	  str->stream.seek = _body_seek;
+	  str->stream.flush = _body_flush;
+	  str->body = body;
+	  body->stream = (mu_stream_t) str;
+	  /* Override the defaults.  */
+	  body->_lines = _body_get_lines;
+	  body->_size = _body_get_size;
+	}
+    }
+  
+  if (!ref)
+    {
+      *pstream = body->stream;
+      return 0;
+    }
+  return mu_streamref_create (pstream, body->stream);
+}
+
+int
+mu_body_get_stream (mu_body_t body, mu_stream_t *pstream)
+{
+  /* FIXME: Deprecation warning */
+  return _body_get_stream (body, pstream, 0);
+}
+
+int
+mu_body_get_streamref (mu_body_t body, mu_stream_t *pstream)
+{
+  return _body_get_stream (body, pstream, 1);
+}
+
+int
+mu_body_set_stream (mu_body_t body, mu_stream_t stream, void *owner)
+{
+  if (body == NULL)
+   return EINVAL;
+  if (body->owner != owner)
+    return EACCES;
+  /* make sure we destroy the old one if it is owned by the body */
+  mu_stream_destroy (&body->stream);
+  body->stream = stream;
+  body->flags |= BODY_MODIFIED;
+  return 0;
+}
+
+int
+mu_body_set_get_stream (mu_body_t body,
+			int (*_getstr) (mu_body_t, mu_stream_t *),
+			void *owner)
+{
+  if (body == NULL)
+    return EINVAL;
+  if (body->owner != owner)
+    return EACCES;
+  body->_get_stream = _getstr;
+  return 0;
+}
+
+int
+mu_body_set_lines (mu_body_t body, int (*_lines) (mu_body_t, size_t *),
+		   void *owner)
+{
+  if (body == NULL)
+    return EINVAL;
+  if (body->owner != owner)
+    return EACCES;
+  body->_lines = _lines;
+  return 0;
+}
+
+int
+mu_body_lines (mu_body_t body, size_t *plines)
+{
+  if (body == NULL)
+    return EINVAL;
+  if (body->_lines)
+    return body->_lines (body, plines);
+  /* Fall on the stream.  */
+  if (body->stream)
+    return _body_get_lines0 (body->stream, plines);
+  if (plines)
+    *plines = 0;
+  return 0;
+}
+
+int
+mu_body_size (mu_body_t body, size_t *psize)
+{
+  if (body == NULL)
+    return EINVAL;
+  if (body->_size)
+    return body->_size (body, psize);
+  /* Fall on the stream.  */
+  if (body->stream)
+    return _body_get_size0 (body->stream, psize);
+  if (psize)
+    *psize = 0;
+  return 0;
+}
+
+int
+mu_body_set_size (mu_body_t body, int (*_size)(mu_body_t, size_t*) , void *owner)
+{
+  if (body == NULL)
+    return EINVAL;
+  if (body->owner != owner)
+    return EACCES;
+  body->_size = _size;
+  return 0;
+}
+
+/* Stub function for the body stream.  */
+
+static int
+_body_seek (mu_stream_t stream, mu_off_t off, mu_off_t *presult)
+{
+  struct _mu_body_stream *str = (struct _mu_body_stream*) stream;
+  mu_body_t body = str->body;
+  return mu_stream_seek (body->fstream, off, MU_SEEK_SET, presult);
+}
+
+static const char *
+_body_error_string (mu_stream_t stream, int rc)
+{
+  /* FIXME: How to know if rc was returned by a body->stream? */
+  return NULL;
+}
+
+static int
+_body_ioctl (mu_stream_t stream, int code, void *ptr)
+{
+  struct _mu_body_stream *str = (struct _mu_body_stream*) stream;
+  mu_body_t body = str->body;
+  return mu_stream_ioctl (body->fstream, code, ptr);
+}
+
+static int
+_body_read (mu_stream_t stream, char *buf, size_t size, size_t *pret)
+{
+  struct _mu_body_stream *str = (struct _mu_body_stream*) stream;
+  mu_body_t body = str->body;
+  return mu_stream_read (body->fstream, buf, size, pret);
+}
+
+static int
+_body_write (mu_stream_t stream, const char *buf, size_t size, size_t *pret)
+{
+  struct _mu_body_stream *str = (struct _mu_body_stream*) stream;
+  mu_body_t body = str->body;
+  return mu_stream_write (body->fstream, buf, size, pret);
+}
+
+static int
+_body_truncate (mu_stream_t stream, mu_off_t n)
+{
+  struct _mu_body_stream *str = (struct _mu_body_stream*) stream;
+  mu_body_t body = str->body;
+  return mu_stream_truncate (body->fstream, n);
+}
+
+static int
+_body_size (mu_stream_t stream, mu_off_t *size)
+{
+  struct _mu_body_stream *str = (struct _mu_body_stream*) stream;
+  mu_body_t body = str->body;
+  return mu_stream_size (body->fstream, size);
+}
+
+static int
+_body_flush (mu_stream_t stream)
+{
+  struct _mu_body_stream *str = (struct _mu_body_stream*) stream;
+  mu_body_t body = str->body;
+  return mu_stream_flush (body->fstream);
+}
+
+/* Default function for the body.  */
+static int
+_body_get_lines (mu_body_t body, size_t *plines)
+{
+  return _body_get_lines0 (body->fstream, plines);
+}
+
+static int
+_body_get_size (mu_body_t body, size_t *psize)
+{
+  return _body_get_size0 (body->fstream, psize);
+}
+
+static int
+_body_get_size0 (mu_stream_t stream, size_t *psize)
+{
+  mu_off_t off = 0;
+  int status = mu_stream_size (stream, &off);
+  if (psize)
+    *psize = off;
+  return status;
+}
+
+static int
+_body_get_lines0 (mu_stream_t stream, size_t *plines)
+{
+  int status =  mu_stream_flush (stream);
+  size_t lines = 0;
+  
+  if (status == 0)
+    {
+      char buf[128];
+      size_t n = 0;
+
+      status = mu_stream_seek (stream, 0, MU_SEEK_SET, NULL);
+      if (status)
+	return status;
+      while ((status = mu_stream_readline (stream, buf, sizeof buf,
+					   &n)) == 0 && n > 0)
+	{
+	  if (buf[n - 1] == '\n')
+	    lines++;
+	}
+    }
+  if (plines)
+    *plines = lines;
+  return status;
+}
+
+
diff --git a/libmailutils/mailbox/envelope.c b/libmailutils/mailbox/envelope.c
new file mode 100644
index 0000000..9546885
--- /dev/null
+++ b/libmailutils/mailbox/envelope.c
@@ -0,0 +1,180 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2004, 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, see 
+   <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+#  include <config.h>
+#endif
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#include <mailutils/errno.h>
+#include <mailutils/mutil.h>
+#include <mailutils/sys/envelope.h>
+
+int
+mu_envelope_create (mu_envelope_t *penvelope, void *owner)
+{
+  mu_envelope_t envelope;
+  if (penvelope == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+  envelope = calloc (1, sizeof (*envelope));
+  if (envelope == NULL)
+    return ENOMEM;
+  envelope->owner = owner;
+  *penvelope = envelope;
+  return 0;
+}
+
+void
+mu_envelope_destroy (mu_envelope_t *penvelope, void *owner)
+{
+  if (penvelope && *penvelope)
+    {
+      mu_envelope_t envelope = *penvelope;
+      if (envelope->owner == owner)
+	{
+	  if (envelope->_destroy)
+	    envelope->_destroy (envelope);
+	  free (envelope->date);
+	  free (envelope->sender);
+	  free (envelope);
+	}
+      *penvelope = NULL;
+    }
+}
+
+void *
+mu_envelope_get_owner (mu_envelope_t envelope)
+{
+  return (envelope) ? envelope->owner : NULL;
+}
+
+int
+mu_envelope_set_sender (mu_envelope_t envelope,
+			int (*_sender) (mu_envelope_t, char *, size_t,
+					size_t*),
+			void *owner)
+{
+  if (envelope == NULL)
+    return EINVAL;
+  if (envelope->owner != owner)
+    return EACCES;
+  envelope->_get_sender = _sender;
+  return 0;
+}
+
+int
+mu_envelope_set_date (mu_envelope_t envelope,
+		      int (*_date) (mu_envelope_t, char *, size_t , size_t *),
+		      void *owner)
+{
+  if (envelope == NULL)
+    return EINVAL;
+  if (envelope->owner != owner)
+    return EACCES;
+  envelope->_get_date = _date;
+  return 0;
+}
+
+
+/* General accessors: */
+#define AC2(a,b) a ## b
+#define AC4(a,b,c,d) a ## b ## c ## d
+#define ACCESSOR(action,field) AC4(mu_envelope_,action,_,field)
+
+#define DECL_SGET(field)						  \
+int									  \
+ACCESSOR(sget,field) (mu_envelope_t env, char const **sptr)               \
+{									  \
+  if (env == NULL)							  \
+    return EINVAL;							  \
+  if (!env->field)							  \
+    {                                                                     \
+      if (env->AC2(_get_,field))                                          \
+	{								  \
+	  size_t n;							  \
+	  char *buf;							  \
+          int status;                                                     \
+	  								  \
+	  status = env->AC2(_get_,field) (env, NULL, 0, &n);	          \
+	  if (status)							  \
+	    return status;						  \
+	  								  \
+	  buf = malloc (n + 1);						  \
+	  if (!buf)							  \
+	    return ENOMEM;						  \
+	  								  \
+	  status = env->AC2(_get_,field) (env, buf, n + 1, NULL);	  \
+	  if (status)		                     		          \
+            return status;						  \
+	  								  \
+          env->field = buf;                                               \
+	}								  \
+      else								  \
+        return MU_ERR_NOENT; 	                                          \
+    }									  \
+  *sptr = env->field;							  \
+  return 0;								  \
+}
+
+#define DECL_GET(field)							  \
+int									  \
+ACCESSOR(get,field) (mu_envelope_t env, char *buf, size_t len, size_t *n) \
+{									  \
+  size_t i;								  \
+  const char *str;							  \
+  int status = ACCESSOR(sget, field) (env, &str);			  \
+  									  \
+  if (status)								  \
+    return status;							  \
+									  \
+  i = mu_cpystr (buf, str, len);					  \
+  if (n)								  \
+    *n = i;								  \
+  return 0;								  \
+}
+
+#define DECL_AGET(field)						  \
+int									  \
+ACCESSOR(aget, field) (mu_envelope_t env, char **buf)	                  \
+{									  \
+  const char *str;							  \
+  int status = ACCESSOR(sget, field) (env, &str);			  \
+									  \
+  if (status)								  \
+    return status;							  \
+									  \
+  if (str)								  \
+    {									  \
+      *buf = strdup (str);						  \
+      if (!*buf)							  \
+	status = ENOMEM;						  \
+    }									  \
+  else									  \
+    *buf = NULL;							  \
+  return status;							  \
+}
+
+#define DECL_ACCESSORS(field)			                          \
+DECL_SGET(field)				                          \
+DECL_GET(field)					                          \
+DECL_AGET(field)
+
+DECL_ACCESSORS(sender)
+DECL_ACCESSORS(date)     
+
diff --git a/libmailutils/mailbox/folder.c b/libmailutils/mailbox/folder.c
new file mode 100644
index 0000000..2b85601
--- /dev/null
+++ b/libmailutils/mailbox/folder.c
@@ -0,0 +1,510 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 2004, 2005, 2006, 2007, 2008, 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, see 
+   <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#include <fnmatch.h>
+
+#include <mailutils/auth.h>
+#include <mailutils/debug.h>
+#include <mailutils/iterator.h>
+#include <mailutils/list.h>
+#include <mailutils/monitor.h>
+#include <mailutils/observer.h>
+#include <mailutils/registrar.h>
+#include <mailutils/stream.h>
+#include <mailutils/url.h>
+#include <mailutils/errno.h>
+
+#include <mailutils/sys/folder.h>
+
+/* Internal folder list.  */
+static mu_list_t known_folder_list;
+static int is_known_folder (mu_url_t, mu_folder_t *);
+
+/* Static folder lock.  */
+static struct mu_monitor folder_lock = MU_MONITOR_INITIALIZER;
+
+int
+mu_folder_match (const char *name, void *pattern, int flags)
+{
+  return fnmatch (pattern, name[0] == '/' ? name + 1 : name, flags);
+}
+
+/* A folder could be remote (IMAP), or local(a spool directory) like $HOME/Mail
+   etc ..  We maintain a list of known folders to avoid creating multiple
+   folders for the same URL.  So, when mu_folder_create is called we check if
+   we already have a folder for that URL and return it, otherwise we create a
+   new one.  Downsides: the scheme to detect the same URL is very weak, and
+   there could be cases where you'll want a different folder for the same URL,
+   there is not easy way to do this.  */
+int
+mu_folder_create_from_record (mu_folder_t *pfolder, mu_url_t url,
+			      mu_record_t record)
+{
+  if (!pfolder)
+    return MU_ERR_OUT_PTR_NULL;
+
+  if (record || 
+      /* Look in the registrar list(iterator), for a possible concrete mailbox
+	 implementation that could match the URL.  */
+      mu_registrar_lookup_url (url, MU_FOLDER_ATTRIBUTE_DIRECTORY, &record,
+			       NULL) == 0)
+    {
+      int (*f_init) (mu_folder_t) = NULL;
+      
+      mu_record_get_folder (record, &f_init);
+      if (f_init)
+        {
+	  int status;
+	  mu_folder_t folder;
+	  int (*u_init) (mu_url_t) = NULL;
+
+	  mu_record_get_url (record, &u_init);
+	  if (u_init)
+	    {
+	      status = u_init (url);
+	      if (status)
+		return status;
+	    }
+	  
+	  mu_monitor_wrlock (&folder_lock);
+
+	  /* Check if we already have the same URL folder.  */
+	  if (is_known_folder (url, &folder))
+	    {
+	      folder->ref++;
+	      *pfolder = folder;
+	      mu_url_destroy (&url); /* FIXME: Hmm */
+	      mu_monitor_unlock (&folder_lock);
+	      return  0;
+	    }
+	  else
+	    mu_monitor_unlock (&folder_lock);
+	  
+	  /* Create a new folder.  */
+
+	  /* Allocate memory for the folder.  */
+	  folder = calloc (1, sizeof (*folder));
+	  if (folder != NULL)
+	    {
+	      folder->url = url;
+	      /* Initialize the internal foilder lock, now so the
+		 concrete folder could use it.  */
+	      status = mu_monitor_create (&folder->monitor, 0, folder);
+	      if (status == 0)
+		{
+		  /* Create the concrete folder type.  */
+		  status = f_init (folder);
+		  if (status == 0)
+		    {
+		      if (!folder->_match)
+			folder->_match = mu_folder_match;
+		      *pfolder = folder;
+		      folder->ref++;
+		      /* Put on the internal list of known folders.  */
+		      if (known_folder_list == NULL)
+			mu_list_create (&known_folder_list);
+		      mu_list_append (known_folder_list, folder);
+		    }
+		}
+	      /* Something went wrong, destroy the object. */
+	      if (status)
+		{
+		  if (folder->monitor)
+		    mu_monitor_destroy (&folder->monitor, folder);
+		  free (folder);
+		}
+	    }
+	  return status;
+	}
+    }
+
+    return MU_ERR_NOENT;
+}
+
+int
+mu_folder_create (mu_folder_t *pfolder, const char *name)
+{
+  int rc;
+  mu_url_t url;
+  
+  rc = mu_url_create (&url, name);
+  if (rc)
+    return rc;
+  rc = mu_url_parse (url);
+  if (rc == 0)
+    rc = mu_folder_create_from_record (pfolder, url, NULL);
+  if (rc)
+    mu_url_destroy (&url);
+  return rc;
+}
+
+/* The folder is destroy if it is the last reference.  */
+void
+mu_folder_destroy (mu_folder_t *pfolder)
+{
+  if (pfolder && *pfolder)
+    {
+      mu_folder_t folder = *pfolder;
+      int destroy_lock = 0;
+      mu_monitor_t monitor = folder->monitor;
+
+      mu_monitor_wrlock (monitor);
+
+      /* Check if this the last reference for this folder.  If yes removed
+         it from the list.  */
+      mu_monitor_wrlock (&folder_lock);
+      folder->ref--;
+      /* Remove the folder from the list of known folder.  */
+      if (folder->ref <= 0)
+	mu_list_remove (known_folder_list, folder);
+      /* If the list is empty we can safely remove it.  */
+      if (mu_list_is_empty (known_folder_list))
+	mu_list_destroy (&known_folder_list);
+
+      mu_monitor_unlock (&folder_lock);
+
+      if (folder->ref <= 0)
+	{
+	  mu_monitor_unlock (monitor);
+	  destroy_lock = 1;
+	  /* Notify the observers.  */
+	  if (folder->observable)
+	    {
+	      mu_observable_notify (folder->observable, MU_EVT_FOLDER_DESTROY,
+				    folder);
+	      mu_observable_destroy (&folder->observable, folder);
+	    }
+	  if (folder->_destroy)
+	    folder->_destroy (folder);
+	  mu_monitor_wrlock (monitor);
+	  if (folder->authority)
+	    mu_authority_destroy (&folder->authority, folder);
+	  if (folder->stream)
+	    mu_stream_destroy (&folder->stream);
+	  if (folder->url)
+	    mu_url_destroy (&folder->url);
+	  free (folder);
+	}
+      mu_monitor_unlock (monitor);
+      if (destroy_lock)
+	mu_monitor_destroy (&monitor, folder);
+      *pfolder = NULL;
+    }
+}
+
+/* Cover functions.  */
+int
+mu_folder_open (mu_folder_t folder, int flags)
+{
+  if (folder == NULL || folder->_open == NULL)
+    return ENOSYS;
+  return folder->_open (folder, flags);
+}
+
+int
+mu_folder_close (mu_folder_t folder)
+{
+  if (folder == NULL || folder->_close == NULL)
+    return ENOSYS;
+  return folder->_close (folder);
+}
+
+int
+mu_folder_set_stream (mu_folder_t folder, mu_stream_t stream)
+{
+  if (folder == NULL)
+    return EINVAL;
+  if (folder->stream)
+    mu_stream_destroy (&folder->stream);
+  folder->stream = stream;
+  return 0;
+}
+
+int
+mu_folder_get_stream (mu_folder_t folder, mu_stream_t *pstream)
+{
+  /* FIXME: Deprecation warning */
+  if (folder == NULL)
+    return EINVAL;
+  if (pstream == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+  *pstream = folder->stream;
+  return 0;
+}
+
+int
+mu_folder_get_streamref (mu_folder_t folder, mu_stream_t *pstream)
+{
+  if (folder == NULL)
+    return EINVAL;
+  if (pstream == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+  return mu_streamref_create (pstream, folder->stream);
+}
+
+int
+mu_folder_set_authority (mu_folder_t folder, mu_authority_t authority)
+{
+  if (folder == NULL)
+    return EINVAL;
+  if (folder->authority)
+    mu_authority_destroy (&folder->authority, folder);
+  folder->authority = authority;
+  return 0;
+}
+
+int
+mu_folder_get_authority (mu_folder_t folder, mu_authority_t *pauthority)
+{
+  if (folder == NULL)
+    return EINVAL;
+  if (pauthority == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+  *pauthority = folder->authority;
+  return 0;
+}
+
+int
+mu_folder_get_observable (mu_folder_t folder, mu_observable_t *pobservable)
+{
+  if (folder == NULL)
+    return EINVAL;
+  if (pobservable == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+
+  if (folder->observable == NULL)
+    {
+      int status = mu_observable_create (&folder->observable, folder);
+      if (status != 0)
+        return status;
+    }
+  *pobservable = folder->observable;
+  return 0;
+}
+
+int
+mu_folder_set_match (mu_folder_t folder, mu_folder_match_fp pmatch)
+{
+  if (folder == NULL)
+    return EINVAL;
+  folder->_match = pmatch;
+  return 0;
+}
+
+int
+mu_folder_get_match (mu_folder_t folder, mu_folder_match_fp *pmatch)
+{
+  if (folder == NULL)
+    return EINVAL;
+  if (pmatch == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+  *pmatch = folder->_match;
+  return 0;
+}
+
+int
+mu_folder_has_debug (mu_folder_t folder)
+{
+  if (folder == NULL)
+    return 0;
+
+  return folder->debug ? 1 : 0;
+}
+
+int
+mu_folder_set_debug (mu_folder_t folder, mu_debug_t debug)
+{
+  if (folder == NULL)
+    return EINVAL;
+  if (folder->debug)
+    mu_debug_destroy (&folder->debug, folder);
+  folder->debug = debug;
+  return 0;
+}
+
+int
+mu_folder_get_debug (mu_folder_t folder, mu_debug_t *pdebug)
+{
+  if (folder == NULL)
+    return EINVAL;
+  if (pdebug == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+  if (folder->debug == NULL)
+    {
+      int status = mu_debug_create (&folder->debug, folder);
+      if (status != 0)
+        return status;
+    }
+  *pdebug = folder->debug;
+  return 0;
+}
+
+void
+mu_list_response_free (void *data)
+{
+  struct mu_list_response *f = data;
+  free (f->name);
+  free (f);
+}
+
+int
+mu_folder_list (mu_folder_t folder, const char *dirname, void *pattern,
+		size_t max_level,
+		mu_list_t *pflist)
+{
+  return mu_folder_enumerate (folder, dirname, pattern, 0, max_level,
+			      pflist, NULL, NULL);
+}
+
+int
+mu_folder_enumerate (mu_folder_t folder, const char *name,
+		     void *pattern, int flags,
+		     size_t max_level,
+		     mu_list_t *pflist,
+		     mu_folder_enumerate_fp enumfun, void *enumdata)
+{
+  int status;
+  if (folder == NULL || folder->_list == NULL)
+    return EINVAL;
+  else
+    {
+      mu_list_t list = NULL;
+      
+      if (pflist)
+	{
+	  status = mu_list_create (&list);
+	  if (status)
+	    return status;
+	  *pflist = list;
+	  mu_list_set_destroy_item (list, mu_list_response_free);
+	}
+      else if (!enumfun)
+	return EINVAL;
+      
+      status = folder->_list (folder, name, pattern, flags, max_level,
+			      list, enumfun, enumdata);
+      if (status)
+	mu_list_destroy (pflist);
+    }
+  return status;
+}
+
+int
+mu_folder_lsub (mu_folder_t folder, const char *dirname, const char *basename,
+		mu_list_t *pflist)
+{
+  int status;
+  
+  if (folder == NULL || folder->_lsub == NULL)
+    return ENOSYS;
+  else
+    {
+      status = mu_list_create (pflist);
+      if (status)
+	return status;
+      mu_list_set_destroy_item (*pflist, mu_list_response_free);
+      status = folder->_lsub (folder, dirname, basename, *pflist);
+    }
+  return status;
+}
+
+int
+mu_folder_subscribe (mu_folder_t folder, const char *name)
+{
+  if (folder == NULL || folder->_subscribe == NULL)
+    return EINVAL;
+  return folder->_subscribe (folder, name);
+}
+
+int
+mu_folder_unsubscribe (mu_folder_t folder, const char *name)
+{
+  if (folder == NULL || folder->_unsubscribe == NULL)
+    return EINVAL;
+  return folder->_unsubscribe (folder, name);
+}
+
+int
+mu_folder_delete (mu_folder_t folder, const char *name)
+{
+  if (folder == NULL || folder->_delete == NULL)
+    return ENOSYS;
+  return folder->_delete (folder, name);
+}
+
+int
+mu_folder_rename (mu_folder_t folder, const char *oldname, const char *newname)
+{
+  if (folder == NULL || folder->_rename == NULL)
+    return ENOSYS;
+  return folder->_rename (folder, oldname, newname);
+}
+
+int
+mu_folder_get_url (mu_folder_t folder, mu_url_t *purl)
+{
+  if (folder == NULL)
+    return EINVAL;
+  if (purl == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+  *purl = folder->url;
+  return 0;
+}
+
+static int
+is_known_folder (mu_url_t url, mu_folder_t *pfolder)
+{
+  int ret = 0;
+  mu_folder_t folder = NULL;
+  mu_iterator_t iterator;
+
+  if (url == NULL || pfolder == NULL)
+    return ret;
+
+  if (mu_list_get_iterator (known_folder_list, &iterator) != 0)
+    return ret;
+
+  for (mu_iterator_first (iterator); !mu_iterator_is_done (iterator);
+       mu_iterator_next (iterator))
+    {
+      mu_iterator_current (iterator, (void **)&folder);
+      /* Check if the same URL type.  */
+      if (folder && folder->url
+	  && mu_url_is_same_scheme (url, folder->url)
+	  && mu_url_is_same_user (url, folder->url)
+	  && mu_url_is_same_host (url, folder->url)
+	  && mu_url_is_same_path (url, folder->url) 
+	  && mu_url_is_same_port (url, folder->url))
+	{
+	  ret = 1;
+	  break;
+	}
+    }
+  if (ret)
+    *pfolder = folder;
+  mu_iterator_destroy (&iterator);
+  return ret;
+}
+
diff --git a/libmailutils/mailbox/hdritr.c b/libmailutils/mailbox/hdritr.c
new file mode 100644
index 0000000..1efd18b
--- /dev/null
+++ b/libmailutils/mailbox/hdritr.c
@@ -0,0 +1,161 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 2008, 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, see 
+   <http://www.gnu.org/licenses/>. */
+
+/* Mail header iterators. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+#include <stdlib.h>
+#include <string.h>
+
+#include <mailutils/sys/header.h>
+#include <mailutils/errno.h>
+
+struct header_iterator
+{
+  mu_header_t header;
+  size_t index;
+};
+
+static int
+hdr_first (void *owner)
+{
+  struct header_iterator *itr = owner;
+  itr->index = 1;
+  return 0;
+}
+
+static int
+hdr_next (void *owner)
+{
+  struct header_iterator *itr = owner;
+  itr->index++;
+  return 0;
+}
+
+static int
+hdr_getitem (void *owner, void **pret, const void **pkey)
+{
+  struct header_iterator *itr = owner;
+  int rc;
+  size_t count;
+
+  rc = mu_header_get_field_count (itr->header, &count);
+  if (rc)
+    return rc;
+  if (itr->index > count)
+    return MU_ERR_NOENT;
+  
+  rc = mu_header_sget_field_name (itr->header, itr->index,
+				  (const char**) pkey);
+  if (rc == 0)
+    {
+      if (pkey)
+	rc = mu_header_sget_field_value (itr->header, itr->index,
+					 (const char**) pret);
+    }
+  return rc;
+}
+
+static int
+hdr_finished_p (void *owner)
+{
+  struct header_iterator *itr = owner;
+  size_t count;
+
+  if (mu_header_get_field_count (itr->header, &count))
+    return 1;
+  return itr->index > count;
+}
+
+static int
+hdr_destroy (mu_iterator_t iterator, void *data)
+{
+  struct header_iterator *itr = data;
+  mu_iterator_detach (&itr->header->itr, iterator);
+  free (data);
+  return 0;
+}
+
+static int
+hdr_curitem_p (void *owner, void *item)
+{
+  void *ptr;
+
+  if (hdr_getitem (owner, &ptr, NULL))
+    return 0;
+  return ptr == item;
+}
+
+static int
+hdr_data_dup (void **ptr, void *owner)
+{
+  struct header_iterator *itr = owner;
+
+  *ptr = malloc (sizeof (struct header_iterator));
+  if (*ptr == NULL)
+    return ENOMEM;
+  memcpy (*ptr, owner, sizeof (struct header_iterator));
+  mu_iterator_attach (&itr->header->itr, *ptr);
+  return 0;
+}
+
+int
+mu_header_get_iterator (mu_header_t hdr, mu_iterator_t *piterator)
+{
+  mu_iterator_t iterator;
+  int status;
+  struct header_iterator *itr;
+
+  if (!hdr)
+    return EINVAL;
+
+  itr = calloc (1, sizeof *itr);
+  if (!itr)
+    return ENOMEM;
+  itr->header = hdr;
+  itr->index = 1;
+
+  status = mu_iterator_create (&iterator, itr);
+  if (status)
+    {
+      free (itr);
+      return status;
+    }
+
+  mu_iterator_set_first (iterator, hdr_first);
+  mu_iterator_set_next (iterator, hdr_next);
+  mu_iterator_set_getitem (iterator, hdr_getitem);
+  mu_iterator_set_finished_p (iterator, hdr_finished_p);
+  mu_iterator_set_curitem_p (iterator, hdr_curitem_p);
+  mu_iterator_set_destroy (iterator, hdr_destroy);
+  mu_iterator_set_dup (iterator, hdr_data_dup);
+
+  mu_iterator_attach (&hdr->itr, iterator);
+
+  *piterator = iterator;
+  return 0;
+}
+
+
+
+
+
+
+
+
diff --git a/libmailutils/mailbox/header.c b/libmailutils/mailbox/header.c
new file mode 100644
index 0000000..6de32d2
--- /dev/null
+++ b/libmailutils/mailbox/header.c
@@ -0,0 +1,1221 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 2004, 2005, 2007, 2008, 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, see 
+   <http://www.gnu.org/licenses/>. */
+
+/*  This all header business needs a good rewrite.
+ *          -- Alain Magloire, 2000-07-03 (rev. 1.21)
+ *
+ *  It's the job that's never started as takes longest to finish.
+ *          -- Hamfast Gamgee, some time in the Third Age
+ *
+ *  It took almost 7 years to gather the courage to start the job,
+ *  and only one day to finish it.
+ *          -- Sergey Poznyakoff, 2007-06-24
+ */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <errno.h>
+
+#ifdef HAVE_STRINGS_H
+# include <strings.h>
+#endif
+
+#include <mailutils/stream.h>
+#include <mailutils/address.h>
+#include <mailutils/mutil.h>
+#include <mailutils/errno.h>
+#include <mailutils/cstr.h>
+#include <mailutils/sys/header_stream.h>
+#include <mailutils/sys/header.h>
+
+#define HEADER_MODIFIED   0x01
+#define HEADER_INVALIDATE 0x02
+
+#define HEADER_SET_MODIFIED(h) \
+  ((h)->flags |= (HEADER_MODIFIED|HEADER_INVALIDATE))
+
+
+/* mu_hdrent manipulation */
+
+#define MU_HDRENT_NAME(hp,ep) ((hp)->spool + (ep)->fn)
+#define MU_HDRENT_VALUE(hp,ep) ((hp)->spool + (ep)->fv)
+#define MU_STR_SIZE(nlen,vlen) ((nlen) + 2 + (vlen) + 1)
+
+static struct mu_hdrent *
+mu_hdrent_nth (struct _mu_header *hdr, int n)
+{
+  struct mu_hdrent *p;
+  for (p = hdr->head; p; p = p->next)
+    if (n-- == 1)
+      break;
+  return p;
+}
+
+static struct mu_hdrent *
+mu_hdrent_find (struct _mu_header *hdr, const char *name, int pos)
+{
+  struct mu_hdrent *p;
+
+  if (pos > 0)
+    {
+      for (p = hdr->head; p; p = p->next)
+	if (mu_c_strcasecmp (MU_HDRENT_NAME (hdr,p), name) == 0 && pos-- == 1)
+	  break;
+    }
+  else if (pos < 0)
+    {
+      for (p = hdr->tail; p; p = p->prev)
+	if (mu_c_strcasecmp (MU_HDRENT_NAME (hdr,p), name) == 0 && ++pos == 0)
+	  break;
+    }
+  else
+    p = NULL;
+  return p;
+}
+
+static int
+mu_hdrent_find_stream_pos (struct _mu_header *hdr, mu_off_t pos,
+			   struct mu_hdrent **pent, size_t *poff)
+{
+  mu_off_t x;
+  struct mu_hdrent *p;
+
+  for (p = hdr->head, x = 0; p; p = p->next)
+    {
+      size_t strsize = MU_STR_SIZE (p->nlen, p->vlen);
+      if (x <= pos && pos < x + strsize)
+	{
+	  *poff = pos - x;
+	  *pent = p;
+	  return 0;
+	}
+      x += strsize;
+    }
+  if (x == pos && hdr->tail)
+    {
+      /* To supply the trailing '\n' */
+      p = hdr->tail;
+      *pent = p;
+      *poff = MU_STR_SIZE (p->nlen, p->vlen) - 1;
+      return 0;
+    }
+  return 1;
+}
+     
+static void
+mu_hdrent_count (struct _mu_header *hdr, size_t *pcount, size_t *psize,
+		 size_t *plines)
+{
+  if (hdr->flags & HEADER_INVALIDATE)
+    {
+      size_t size = 0;
+      size_t count = 0;
+      size_t lines = 0;
+      struct mu_hdrent *p;
+      for (p = hdr->head; p; p = p->next)
+	{
+	  count++;
+	  size += MU_STR_SIZE (p->nlen, p->vlen);
+	  lines += p->nlines;
+	}
+
+      hdr->numhdr = count;
+      hdr->numlines = lines;
+      hdr->size = size;
+      hdr->flags &= ~HEADER_INVALIDATE;
+    }
+      
+  *pcount = hdr->numhdr;
+  *psize = hdr->size;
+  *plines = hdr->numlines;
+}
+
+static void
+mu_hdrent_remove (struct _mu_header *hdr, struct mu_hdrent *ent)
+{
+  struct mu_hdrent *p = ent->prev;
+  if (p)
+    p->next = ent->next;
+  else
+    hdr->head = ent->next;
+
+  p = ent->next;
+  if (p)
+    p->prev = ent->prev;
+  else
+    hdr->tail = ent->prev;
+}
+
+static void
+mu_hdrent_prepend (struct _mu_header *hdr, struct mu_hdrent *ent)
+{
+  struct mu_hdrent *p = hdr->head;
+  ent->prev = NULL;
+  ent->next = p;
+  if (p)
+    p->prev = ent;
+  else
+    hdr->tail = ent;
+  hdr->head = ent;
+}
+
+static void
+mu_hdrent_append (struct _mu_header *hdr, struct mu_hdrent *ent)
+{
+  struct mu_hdrent *p = hdr->tail;
+  ent->next = NULL;
+  ent->prev = p;
+  if (p)
+    p->next = ent;
+  else
+    hdr->head = ent;
+  hdr->tail = ent;
+}
+
+static int
+mu_hdrent_insert (struct _mu_header *hdr, struct mu_hdrent *ent,
+		  const char *name, int pos, int before)
+{
+  struct mu_hdrent *p;
+  struct mu_hdrent *ref = mu_hdrent_find (hdr, name, pos);
+  if (!ref)
+    return MU_ERR_NOENT;
+
+  if (before)
+    {
+      ref = ref->prev;
+      if (!ref)
+	{
+	  mu_hdrent_prepend (hdr, ent);
+	  return 0;
+	}
+    }
+
+  p = ref->next;
+  if (!p)
+    {
+      mu_hdrent_append (hdr, ent);
+      return 0;
+    }
+
+  ent->next = p;
+  p->prev = ent;
+  ent->prev = ref;
+  ref->next = ent;
+  
+  return 0;
+}
+
+#define SPOOLBLKSIZ 1024
+
+static struct mu_hdrent *
+mu_hdrent_create (struct _mu_header *ph,
+		  struct mu_hdrent *ent,
+		  const char *name, size_t nsize,
+		  const char *value, size_t vsize)
+{
+  size_t strsize;
+  size_t sizeleft;
+  const char *p;
+  
+  if (!ent)
+    {
+      ent = calloc (1, sizeof (*ent));
+      if (!ent)
+	return NULL;
+    }
+  
+  strsize = MU_STR_SIZE (nsize, vsize);
+  sizeleft = ph->spool_size - ph->spool_used;
+
+  /* Ensure there is enough space in spool */
+  if (sizeleft < strsize)
+    {
+      char *newp;
+      size_t delta = (strsize - sizeleft + SPOOLBLKSIZ - 1) / SPOOLBLKSIZ;
+      delta *= SPOOLBLKSIZ;
+      newp = realloc (ph->spool, ph->spool_size + delta);
+      if (!newp)
+	return 0;
+      ph->spool = newp;
+      ph->spool_size += delta;
+    }
+
+  /* Copy header name */
+  ent->fn = ph->spool_used;
+  ent->nlen = nsize;
+  memcpy (ph->spool + ph->spool_used, name, nsize);
+  ph->spool_used += nsize;
+  ph->spool[ph->spool_used++] = 0;
+  ph->spool[ph->spool_used++] = ' ';
+
+  /* Copy header value */
+  ent->fv = ph->spool_used;
+  ent->vlen = vsize;
+  memcpy (ph->spool + ph->spool_used, value, vsize);
+  ph->spool_used += vsize;
+  ph->spool[ph->spool_used++] = 0;
+  
+  ent->nlines = 1;
+  for (p = value; p < value + vsize; p++)
+    if (*p == '\n')
+      ent->nlines++;
+  
+  return ent;
+}
+
+static void
+mu_hdrent_free_list (struct _mu_header *hdr)
+{
+  struct mu_hdrent *p;
+  for (p = hdr->head; p; )
+    {
+      struct mu_hdrent *next = p->next;
+      free (p);
+      p = next;
+    }
+  hdr->head = hdr->tail = NULL;
+  hdr->spool_used = 0;
+}
+  
+
+
+#define ISLWSP(c) (((c) == ' ' || (c) == '\t'))
+
+/* Parsing is done in a rather simple fashion, meaning we just consider an
+   entry to be a field-name an a field-value.  So they maybe duplicate of
+   field-name like "Received" they are just put in the array, see _get_value()
+   on how to handle the case. in the case of error .i.e a bad header construct
+   we do a full stop and return what we have so far.  */
+
+static int
+header_parse (mu_header_t header, const char *blurb, int len)
+{
+  const char *header_end;
+  const char *header_start;
+  const char *header_start2;
+  
+  /* Nothing to parse.  */
+  if (blurb == NULL)
+    return 0;
+
+  header->flags |= HEADER_INVALIDATE;
+  mu_hdrent_free_list (header);
+
+  /* Get a header, a header is:
+     field-name LWSP ':'
+       LWSP field-value '\r' '\n'
+       *[ (' ' | '\t') field-value '\r' '\n' ]
+  */
+  /* First loop goes through the blurb */
+  for (header_start = blurb; len > 0; header_start = ++header_end)
+    {
+      const char *fn, *fn_end, *fv, *fv_end;
+      struct mu_hdrent *ent;
+
+      if (header_start[0] == ' '
+	  || header_start[0] == '\t'
+	  || header_start[0] == '\n')
+	break;
+
+      /* Second loop extract one header field. */
+      for (header_start2 = header_start; len; header_start2 = ++header_end)
+	{
+	  header_end = memchr (header_start2, '\n', len);
+	  if (header_end == NULL)
+	    {
+	      header_end = header_start2 + len;
+	      len = 0;
+	      break;
+	    }
+	  else
+	    {
+	      len -= (header_end - header_start2 + 1);
+	      if (!len
+		  || (header_end[1] != ' '
+		      && header_end[1] != '\t'))
+		break; /* New header break the inner for. */
+	    }
+	  /* *header_end = ' ';  smash LF ? NO */
+	}
+
+      /* Now save the header in the data structure.  */
+
+      /* Treats unix "From " specially.  FIXME: Should we? */
+      if ((header_end - header_start >= 5)
+      	  && strncmp (header_start, "From ", 5) == 0)
+	{
+	  fn = header_start;
+	  fn_end = header_start + 5;
+	  fv = header_start + 5;
+	  fv_end = header_end;
+	}
+      else /* Break the header in key: value */
+	{
+	  char *colon = memchr (header_start, ':', header_end - header_start);
+
+	  /* Houston we have a problem.  */
+	  if (colon == NULL)
+	    break; /* FIXME: Disregard the rest and bailout.  */
+
+	  fn = header_start;
+	  fn_end = colon;
+	  /* Shrink any LWSP after the field name -- CRITICAL for 
+	     later name comparisons to work correctly! */
+	  while (ISLWSP (fn_end[-1]))
+	    fn_end--;
+
+	  fv = colon + 1;
+	  fv_end = header_end;
+
+	  /* Skip any LWSP before the field value -- unnecessary, but
+	     might make some field values look a little tidier. */
+	  while (ISLWSP (fv[0]))
+	    fv++;
+	}
+
+      /* Register this header */
+      ent = mu_hdrent_create (header, NULL, fn, fn_end - fn, fv, fv_end - fv);
+      if (!ent)
+	return ENOMEM;
+      mu_hdrent_append (header, ent);
+    } /* for (header_start ...) */
+
+  return 0;
+}
+
+
+static int 
+mu_header_fill (mu_header_t header)
+{
+  int status;
+  size_t blurb_len = 0;
+  char *blurb = NULL;
+  
+  if (header->spool_used)
+    return 0;
+  
+  if (header->_fill == NULL)
+    return 0; /* FIXME: Really? */
+
+  /* Bring in the entire header.  */
+  status = header->_fill (header->data, &blurb, &blurb_len);
+  if (status)
+    return status;
+  status = header_parse (header, blurb, blurb_len);
+  free (blurb);
+  return status;
+}
+
+
+
+int
+mu_header_create (mu_header_t *ph, const char *blurb, size_t len)
+{
+  mu_header_t header;
+  int status = 0;
+
+  header = calloc (1, sizeof (*header));
+  if (header == NULL)
+    return ENOMEM;
+  
+  status = header_parse (header, blurb, len);
+
+  *ph = header;
+  return status;
+}
+
+void
+mu_header_destroy (mu_header_t *ph)
+{
+  if (ph && *ph)
+    {  
+      mu_header_t header = *ph;
+
+      mu_stream_destroy (&header->stream);
+      mu_hdrent_free_list (header);
+      free (header->spool);
+      free (header);
+      *ph = NULL;
+    }
+}
+
+
+int
+mu_header_set_value (mu_header_t header, const char *fn, const char *fv,
+		     int replace)
+{
+  int status;
+  struct mu_hdrent *ent = NULL;
+  
+  if (header == NULL || fn == NULL)
+    return EINVAL;
+
+  status = mu_header_fill (header);
+  if (status)
+    return status;
+  
+  /* An fv of NULL means delete the field, but only do it if replace
+     was also set to true! */
+  if (fv == NULL && !replace)
+    return EINVAL;
+
+  if (replace)
+    {
+      ent = mu_hdrent_find (header, fn, 1);
+      if (ent)
+	{
+	  if (fv == NULL)
+	    {
+	      /* Delete the header */
+	      mu_hdrent_remove (header, ent);
+	      free (ent);
+	      return 0;
+	    }
+	  mu_hdrent_create (header, ent, fn, strlen (fn), fv, strlen (fv));
+	  HEADER_SET_MODIFIED (header);
+	  return 0;
+	}
+      else if (fv == NULL)
+	return 0;
+    }
+
+  ent = mu_hdrent_create (header, NULL,
+			  fn, strlen (fn), fv, strlen (fv));
+  if (!ent)
+    return ENOMEM;
+  mu_hdrent_prepend (header, ent);
+  HEADER_SET_MODIFIED (header);
+  return 0;
+}
+
+int
+mu_header_remove (mu_header_t header, const char *fn, int n)
+{
+  int status;
+  struct mu_hdrent *ent;
+  
+  if (header == NULL || fn == NULL)
+    return EINVAL;
+
+  status = mu_header_fill (header);
+  if (status)
+    return status;
+
+  ent = mu_hdrent_find (header, fn, n);
+  if (!ent)
+    return MU_ERR_NOENT;
+
+  mu_hdrent_remove (header, ent);
+  HEADER_SET_MODIFIED (header);
+  free (ent);
+  return 0;
+}
+
+int
+mu_header_append (mu_header_t header, const char *fn, const char *fv)
+{
+  int status;
+  struct mu_hdrent *ent;
+
+  if (header == NULL || fn == NULL || fv == NULL)
+    return EINVAL;
+
+  status = mu_header_fill (header);
+  if (status)
+    return status;
+
+  ent = mu_hdrent_create (header, NULL, fn, strlen (fn), fv, strlen (fv));
+  if (!ent)
+    return ENOMEM;
+  mu_hdrent_append (header, ent);
+  HEADER_SET_MODIFIED (header);
+  return 0;
+}
+
+int
+mu_header_prepend (mu_header_t header, const char *fn, const char *fv)
+{
+  int status;
+  struct mu_hdrent *ent;
+
+  if (header == NULL || fn == NULL || fv == NULL)
+    return EINVAL;
+
+  status = mu_header_fill (header);
+  if (status)
+    return status;
+
+  ent = mu_hdrent_create (header, NULL, fn, strlen (fn), fv, strlen (fv));
+  if (!ent)
+    return ENOMEM;
+  mu_hdrent_prepend (header, ent);
+  HEADER_SET_MODIFIED (header);
+  return 0;
+}
+
+int
+mu_header_insert (mu_header_t header,
+		  const char *fn, const char *fv, 
+		  const char *ref, int n, int flags)
+{
+  int status;
+  struct mu_hdrent *ent;
+
+  if (header == NULL || fn == NULL || fv == NULL)
+    return EINVAL;
+
+  status = mu_header_fill (header);
+  if (status)
+    return status;
+    
+  if (flags & MU_HEADER_REPLACE)
+    {
+      if (!ref)
+	ref = fn;
+      ent = mu_hdrent_find (header, ref, n);
+      mu_hdrent_create (header, ent, fn, strlen (fn), fv, strlen (fv));
+    }
+  else
+    {
+      ent = mu_hdrent_create (header, NULL,
+			      fn, strlen (fn), fv, strlen (fv));
+      if (!ent)
+	return ENOMEM;
+      if (ref)
+	return mu_hdrent_insert (header, ent, ref, n,
+				 flags & MU_HEADER_BEFORE);
+      else
+	mu_hdrent_prepend (header, ent);
+    }
+  HEADER_SET_MODIFIED (header);
+  return 0;
+}
+  
+
+int
+mu_header_sget_value_n (mu_header_t header,
+			const char *name, int n,
+			const char **pval)
+{
+  int status;
+  struct mu_hdrent *ent;
+
+  if (header == NULL || name == NULL)
+    return EINVAL;
+  status = mu_header_fill (header);
+  if (status)
+    return status;
+
+  ent = mu_hdrent_find (header, name, n);
+  if (!ent)
+    return MU_ERR_NOENT;
+
+  *pval = MU_HDRENT_VALUE (header, ent);
+  return 0;
+}
+
+int
+mu_header_aget_value_n (mu_header_t header,
+		      const char *name, int n,
+		      char **pval)
+{
+  const char *s;
+  int status = mu_header_sget_value_n (header, name, n, &s);
+  if (status == 0)
+    {
+      *pval = strdup (s);
+      if (!*pval)
+	status = ENOMEM;
+    }
+  return status;
+}
+
+int
+mu_header_get_value_n (mu_header_t header, const char *name, int n,
+		       char *buffer, size_t buflen, size_t *pn)
+{
+  const char *s;
+  int status = mu_header_sget_value_n (header, name, n, &s);
+  if (status == 0)
+    {
+      size_t slen = strlen (s);
+
+      if (buffer)
+	{
+	  if (slen > buflen)
+	    slen = buflen;
+	  memcpy (buffer, s, slen);
+	  buffer[slen] = 0;
+	}
+      if (pn)
+	*pn = slen;
+    }
+  return status;
+}
+
+
+/* Unfolding functions */
+int
+mu_header_get_value_unfold_n (mu_header_t header,
+			      const char *name, int n, char *buffer,
+			      size_t buflen, size_t *pn)
+{
+  int rc = mu_header_get_value_n (header, name, n, buffer, buflen, pn);
+
+  if (rc == 0)
+    mu_string_unfold (buffer, pn);
+  return rc;
+}
+
+int
+mu_header_aget_value_unfold_n (mu_header_t header, const char *name, int n,
+			       char **pvalue)
+{
+  int rc = mu_header_aget_value_n (header, name, n, pvalue);
+  if (rc == 0)
+    mu_string_unfold (*pvalue, NULL);
+  return rc;
+}
+
+
+int
+mu_header_get_address_n (mu_header_t header, const char *name, int n,
+			 mu_address_t *addr)
+{
+  const char *value = NULL;
+  int status = mu_header_sget_value_n (header, name, n, &value);
+
+  if (status)
+    return status;
+
+  return mu_address_create (addr, value);
+}
+
+
+int
+mu_header_get_field_count (mu_header_t header, size_t *pcount)
+{
+  size_t count;
+  size_t size;
+  size_t lines;
+  int status;
+  
+  if (header == NULL)
+    return EINVAL;
+
+  status = mu_header_fill (header);
+  if (status == 0)
+    {
+      mu_hdrent_count (header, &count, &size, &lines);
+      
+      if (pcount)
+	*pcount = count;
+    }
+  
+  return status;
+}
+
+int
+mu_header_sget_field_name (mu_header_t header, size_t num, const char **sptr)
+{
+  int status;
+  
+  if (header == NULL)
+    return EINVAL;
+
+  status = mu_header_fill (header);
+  if (status == 0)
+    {
+      struct mu_hdrent *ent = mu_hdrent_nth (header, num);
+      if (ent)
+	*sptr = MU_HDRENT_NAME (header, ent);
+      else
+	status = MU_ERR_NOENT;
+    }
+  return status;
+}
+
+int
+mu_header_get_field_name (mu_header_t header, size_t num, char *buffer,
+			  size_t buflen, size_t *pn)
+{
+  const char *s;
+  int status = mu_header_sget_field_name (header, num, &s);
+  if (status == 0)
+    {
+      size_t slen = strlen (s);
+
+      if (buffer)
+	{
+	  if (slen > buflen)
+	    slen = buflen;
+	  memcpy (buffer, s, slen);
+	  buffer[slen] = 0;
+	}
+      if (pn)
+	*pn = slen;
+    }
+  return status;
+}
+
+int
+mu_header_aget_field_name (mu_header_t header, size_t num, char **pvalue)
+{
+  const char *s;
+  int status = mu_header_sget_field_name (header, num, &s);
+  if (status == 0)
+    {
+      if ((*pvalue = strdup (s)) == NULL)
+	status = ENOMEM;
+    }
+  return status;
+}
+
+
+int
+mu_header_sget_field_value (mu_header_t header, size_t num, const char **sptr)
+{
+  int status;
+  
+  if (header == NULL)
+    return EINVAL;
+
+  status = mu_header_fill (header);
+  if (status == 0)
+    {
+      struct mu_hdrent *ent = mu_hdrent_nth (header, num);
+      if (ent)
+	*sptr = MU_HDRENT_VALUE (header, ent);
+      else
+	status = MU_ERR_NOENT;
+    }
+  return status;
+}
+
+int
+mu_header_get_field_value (mu_header_t header, size_t num, char *buffer,
+			   size_t buflen, size_t *pn)
+{
+  const char *s;
+  int status = mu_header_sget_field_value (header, num, &s);
+  if (status == 0)
+    {
+      size_t slen = strlen (s);
+
+      if (buffer)
+	{
+	  if (slen > buflen)
+	    slen = buflen;
+	  memcpy (buffer, s, slen);
+	  buffer[slen] = 0;
+	}
+      if (pn)
+	*pn = slen;
+    }
+  return status;
+}
+
+int
+mu_header_aget_field_value (mu_header_t header, size_t num, char **pvalue)
+{
+  const char *s;
+  int status = mu_header_sget_field_value (header, num, &s);
+  if (status == 0)
+    {
+      if ((*pvalue = strdup (s)) == NULL)
+	status = ENOMEM;
+    }
+  return status;
+}
+
+int
+mu_header_get_field_value_unfold (mu_header_t header, size_t num, char *buf,
+				  size_t buflen, size_t *nwritten)
+{
+  int rc = mu_header_get_field_value (header, num, buf, buflen, nwritten);
+  if (rc == 0)
+    mu_string_unfold (buf, nwritten);
+  return rc;
+}
+
+int
+mu_header_aget_field_value_unfold (mu_header_t header, size_t num,
+				   char **pvalue)
+{
+  int rc = mu_header_aget_field_value (header, num, pvalue);
+  if (rc == 0)
+    mu_string_unfold (*pvalue, NULL);
+  return rc;
+}
+
+
+int
+mu_header_lines (mu_header_t header, size_t *plines)
+{
+  int status;
+
+  if (header == NULL)
+    return EINVAL;
+  if (plines == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+
+  status = mu_header_fill (header);
+  if (status == 0)
+    {
+      size_t count;
+      size_t size;
+      size_t lines;
+      mu_hdrent_count (header, &count, &size, &lines);
+      *plines = lines + 1;
+    }
+  return status;
+}
+
+int
+mu_header_size (mu_header_t header, size_t *psize)
+{
+  int status;
+
+  if (header == NULL)
+    return EINVAL;
+  if (psize == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+
+  status = mu_header_fill (header);
+  if (status == 0)
+    {
+      size_t count;
+      size_t size;
+      size_t lines;
+      mu_hdrent_count (header, &count, &size, &lines);
+      *psize = size + 1;
+    }
+  return status;
+}
+
+int
+mu_header_invalidate (mu_header_t hdr)
+{
+  if (hdr == NULL)
+    return EINVAL;
+  mu_hdrent_free_list (hdr);
+  return 0;
+}
+
+
+static void
+mu_hdrent_fixup (mu_header_t hdr, struct mu_hdrent *ent)
+{
+  char *s = MU_HDRENT_NAME (hdr, ent);
+  s[ent->nlen] = ':';
+  s = MU_HDRENT_VALUE (hdr, ent);
+  s[ent->vlen] = '\n';
+}
+
+static void
+mu_hdrent_unroll_fixup (mu_header_t hdr, struct mu_hdrent *ent)
+{
+  char *s = MU_HDRENT_NAME (hdr, ent);
+  s[ent->nlen] = 0;
+  s = MU_HDRENT_VALUE (hdr, ent);
+  s[ent->vlen] = 0;
+}
+
+int
+header_seek (mu_stream_t str, mu_off_t off, mu_off_t *presult)
+{ 
+  struct _mu_header_stream *hstr = (struct _mu_header_stream *) str;
+  size_t size;
+  int status;
+    
+  status = mu_header_size (hstr->hdr, &size);
+  if (status)
+    return status;
+  
+  if (off < 0 || off > size)
+    return ESPIPE;
+  hstr->off = off;
+  *presult = off;
+  return 0;
+}
+
+static int
+header_read (mu_stream_t is, char *buffer, size_t buflen, size_t *pnread)
+{
+  struct _mu_header_stream *hstr = (struct _mu_header_stream *) is;
+  mu_header_t header;
+  struct mu_hdrent *ent;
+  size_t ent_off;
+  int status;
+  size_t nread;
+  
+  if (is == NULL)
+    return EINVAL;
+
+  header = hstr->hdr;
+  status = mu_header_fill (header);
+  if (status)
+    return status;
+  
+  if (mu_hdrent_find_stream_pos (header, hstr->off, &ent, &ent_off))
+    {
+      if (pnread)
+	*pnread = 0;
+      return 0;
+    }
+
+  for (nread = 0; nread < buflen && ent; ent = ent->next)
+    {
+      size_t rest = buflen - nread;
+      size_t strsize = MU_STR_SIZE (ent->nlen, ent->vlen) - ent_off;
+      if (rest > strsize)
+	rest = strsize;
+      mu_hdrent_fixup (header, ent);
+      memcpy (buffer + nread, MU_HDRENT_NAME (header, ent) + ent_off, rest);
+      mu_hdrent_unroll_fixup (header, ent);
+      nread += rest;
+      hstr->off += rest;
+      ent_off = 0;
+    }
+  if (pnread)
+    *pnread = nread;
+  return 0;
+}
+
+#if 0
+/* FIXME: Implement header_readdelim based on this: */
+static int
+_header_readline (mu_stream_t is, char *buffer, size_t buflen, size_t *pnread)
+{
+  struct _mu_header_stream *hstr = (struct _mu_header_stream *) is;
+  mu_header_t header = hstr->hdr;
+  struct mu_hdrent *ent;
+  size_t ent_off;
+  int status;
+  size_t strsize;
+  char *start, *end;
+
+  if (buflen == 0)
+    return EINVAL;
+
+  header = mu_stream_get_owner (is);
+  status = mu_header_fill (header);
+  if (status)
+    return status;
+  if (mu_hdrent_find_stream_pos (header, hstr->off, &ent, &ent_off))
+    {
+      if (pnread)
+	*pnread = 0;
+      return 0;
+    }
+
+  buflen--; /* Account for the terminating nul */
+
+  mu_hdrent_fixup (header, ent);
+  strsize = MU_STR_SIZE (ent->nlen, ent->vlen) - ent_off;
+  start = MU_HDRENT_NAME (header, ent) + ent_off;
+  end = strchr (start, '\n');
+  if (end)
+    {
+      size_t len = end - start + 1;
+      if (len < strsize)
+	strsize = len;
+    }
+
+  if (strsize < buflen)
+    buflen = strsize;
+
+  memcpy (buffer, start, buflen);
+  buffer[buflen] = 0;
+  hstr->off += buflen;
+  mu_hdrent_unroll_fixup (header, ent);
+  if (pnread)
+    *pnread = buflen;
+  return 0;
+}
+#endif
+
+static int
+header_write (mu_stream_t os, const char *buf, size_t buflen, size_t *pnwrite)
+{
+  struct _mu_header_stream *hstr;
+  mu_header_t header;
+  int status;
+  mu_off_t mstream_size;
+  
+  if (!os || !buf)
+    return EINVAL;
+
+  hstr = (struct _mu_header_stream *) os;
+  header = hstr->hdr;
+  if (header == NULL)
+    return EINVAL;
+  
+  /* Skip the obvious.  */
+  if (*buf == '\0' || buflen == 0)
+    {
+      if (pnwrite)
+        *pnwrite = 0;
+      return 0;
+    }
+
+  if (!header->mstream)
+    {
+      status = mu_memory_stream_create (&header->mstream, MU_STREAM_RDWR);
+      if (status)
+	return status;
+    }
+
+  status = mu_stream_write (header->mstream, buf, buflen, NULL);
+  if (status)
+    {
+      mu_stream_destroy (&header->mstream);
+      return status;
+    }
+
+  status = mu_stream_size (header->mstream, &mstream_size);
+  if (status == 0 && mstream_size > 1)
+    {
+      char nlbuf[2];
+
+      status = mu_stream_seek (header->mstream, -2, MU_SEEK_END, NULL);
+      if (status == 0)
+	status = mu_stream_read (header->mstream, nlbuf, 2, NULL);
+      if (status == 0 && memcmp (nlbuf, "\n\n", 2) == 0)
+	{
+	  char *blurb;
+
+	  blurb = calloc (1, mstream_size + 1);
+	  if (blurb)
+	    {
+	      mu_stream_read (header->mstream, blurb, mstream_size, NULL);
+	      status = header_parse (header, blurb, mstream_size);
+	    }
+	  free (blurb);
+	  mu_stream_destroy (&header->mstream);
+	}
+    }
+  
+  if (pnwrite)
+    *pnwrite = buflen;
+  
+  return status;
+}
+
+static int
+header_size (mu_stream_t str, mu_off_t *psize)
+{
+  mu_header_t header;
+  int status;
+  size_t size;
+  
+  if (str == NULL)
+    return EINVAL;
+  if (psize == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+  
+  header = ((struct _mu_header_stream *) str)->hdr;
+  status = mu_header_fill (header);
+  if (status)
+    return status;
+  status = mu_header_size (header, &size);
+  if (status == 0)
+    *psize = size;
+  return status;
+}
+
+static int
+_header_get_stream (mu_header_t header, mu_stream_t *pstream, int ref)
+{
+  if (header == NULL)
+    return EINVAL;
+
+  if (pstream == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+
+  if (header->stream == NULL)
+    {
+      struct _mu_header_stream *str = 
+	(struct _mu_header_stream *) _mu_stream_create (sizeof (*str),
+							MU_STREAM_RDWR|MU_STREAM_SEEK);
+      if (!str)
+	return ENOMEM;
+      str->stream.read = header_read;
+      /*str->stream.rdelim? */
+      str->stream.write = header_write;
+      str->stream.seek = header_seek;
+      str->stream.size = header_size;
+      str->hdr = header;
+      header->stream = (mu_stream_t) str;
+    }
+  if (!ref)
+    {
+      *pstream = header->stream;
+      return 0;
+    }
+  return mu_streamref_create (pstream, header->stream);
+}
+
+int
+mu_header_get_stream (mu_header_t header, mu_stream_t *pstream)
+{
+  /* FIXME: Deprecation warning */
+  return _header_get_stream (header, pstream, 0);
+}
+
+int
+mu_header_get_streamref (mu_header_t header, mu_stream_t *pstream)
+{
+  return _header_get_stream (header, pstream, 1);
+}
+
+
+int
+mu_header_set_fill (mu_header_t header, int
+		    (*_fill) (void *data, char **, size_t *),
+		    void *data)
+{
+  if (header == NULL)
+    return EINVAL;
+  header->_fill = _fill;
+  header->data = data;
+  return 0;
+}
+
+
+int
+mu_header_is_modified (mu_header_t header)
+{
+  return header ? (header->flags & HEADER_MODIFIED) : 0;
+}
+
+int
+mu_header_clear_modified (mu_header_t header)
+{
+  if (header)
+    header->flags &= ~HEADER_MODIFIED;
+  return 0;
+}
+
+
diff --git a/libmailutils/mailbox/mailbox.c b/libmailutils/mailbox/mailbox.c
new file mode 100644
index 0000000..fcf2243
--- /dev/null
+++ b/libmailutils/mailbox/mailbox.c
@@ -0,0 +1,818 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 2004, 2005, 2006, 2007, 2008, 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, see 
+   <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <errno.h>
+#include <string.h>
+
+#include <mailutils/debug.h>
+#include <mailutils/errno.h>
+#include <mailutils/error.h>
+#include <mailutils/folder.h>
+#include <mailutils/iterator.h>
+#include <mailutils/list.h>
+#include <mailutils/locker.h>
+#include <mailutils/observer.h>
+#include <mailutils/property.h>
+#include <mailutils/registrar.h>
+#include <mailutils/stream.h>
+#include <mailutils/url.h>
+#include <mailutils/attribute.h>
+#include <mailutils/message.h>
+#include <mailutils/mutil.h>
+
+#include <mailutils/sys/mailbox.h>
+#include <mailutils/sys/url.h>
+
+/* Mailbox-specific flags */
+#define _MU_MAILBOX_OPEN    0x10000000
+#define _MU_MAILBOX_REMOVED 0x20000000
+#define _MU_MAILBOX_MASK    0xF0000000
+
+static int
+mailbox_folder_create (mu_mailbox_t mbox, const char *name,
+		       mu_record_t record)
+{
+  int rc;
+  mu_url_t url;
+  
+  if ((rc = mu_url_uplevel (mbox->url, &url)))
+    {
+      if (rc == MU_ERR_NOENT)
+	{
+	  rc = mu_url_dup (mbox->url, &url);
+	  if (rc)
+	    return rc;
+	}
+      else
+	return rc;
+    }
+
+  rc = mu_folder_create_from_record (&mbox->folder, url, record);
+  if (rc)
+    mu_url_destroy (&url);
+  return rc;
+}
+
+int
+_mailbox_create_from_record (mu_mailbox_t *pmbox,
+			     mu_record_t record,
+			     mu_url_t url, 
+			     const char *name)
+{
+  mu_log_level_t level;
+  int (*m_init) (mu_mailbox_t) = NULL;
+      
+  mu_record_get_mailbox (record, &m_init);
+  if (m_init)
+    {
+      int status;
+      int (*u_init) (mu_url_t) = NULL;
+      mu_mailbox_t mbox;
+      
+      /* Allocate memory for mbox.  */
+      mbox = calloc (1, sizeof (*mbox));
+      if (mbox == NULL)
+	return ENOMEM;
+      
+      /* Initialize the internal lock now, so the concrete mailbox
+	 could use it. */
+      status = mu_monitor_create (&mbox->monitor, 0, mbox);
+      if (status != 0)
+	{
+	  mu_mailbox_destroy (&mbox);
+	  return status;
+	}
+      
+      /* Make sure scheme contains actual mailbox scheme */
+      /* FIXME: It is appropriate not for all record types.  For now we
+	 assume that if the record scheme ends with a plus sign, this
+	 should not be done.  Probably it requires some flag in struct
+	 _mu_record? */
+      if (strcmp (url->scheme, record->scheme))
+	{
+	  char *p = strdup (record->scheme);
+	  if (!p)
+	    {
+	      mu_mailbox_destroy (&mbox);
+	      return errno;
+	    }
+	  free (url->scheme);
+	  url->scheme = p;
+	}
+      
+      mu_record_get_url (record, &u_init);
+      if (u_init && (status = u_init (url)) != 0)
+	{
+	  mu_mailbox_destroy (&mbox);
+	  return status;
+	}
+      
+      mbox->url = url;
+      
+      /* Create the folder before initializing the concrete mailbox.
+	 The mailbox needs it's back pointer. */
+      status = mailbox_folder_create (mbox, name, record);
+      
+      if (status == 0)
+	status = m_init (mbox);   /* Create the concrete mailbox type.  */
+      
+      if (status != 0)
+	{
+	  /* Take care not to destroy url.  Leave it to caller. */
+	  mbox->url = NULL;
+	  mu_mailbox_destroy (&mbox);
+	}
+      else
+	{
+	  *pmbox = mbox;
+	  
+	  level = mu_global_debug_level ("mailbox");
+	  if (level)
+	    {
+	      int status = mu_debug_create (&mbox->debug, mbox);
+	      if (status)
+		return 0; /* FIXME: don't want to bail out just because I
+			     failed to create a *debug* object. But I may
+			     be wrong... */
+	      mu_debug_set_level (mbox->debug, level);
+	      if (level & MU_DEBUG_INHERIT)
+		mu_folder_set_debug (mbox->folder, mbox->debug);
+	    }
+	}
+      
+      return status;
+    }
+  return MU_ERR_NO_HANDLER;
+}
+
+static int
+_create_mailbox0 (mu_mailbox_t *pmbox, mu_url_t url, const char *name)
+{
+  mu_record_t record = NULL;
+
+  if (mu_registrar_lookup_url (url, MU_FOLDER_ATTRIBUTE_FILE, &record, NULL)
+      == 0)
+    return _mailbox_create_from_record (pmbox, record, url, name);
+  return MU_ERR_NO_HANDLER;
+}
+
+static int
+_create_mailbox (mu_mailbox_t *pmbox, const char *name)
+{
+  int status;
+  mu_url_t url;
+
+  status = mu_url_create (&url, name);
+  if (status)
+    return status;
+  status = mu_url_parse (url);
+  if (status == 0)
+    status = _create_mailbox0 (pmbox, url, name);
+  if (status)
+    mu_url_destroy (&url);
+  return status;
+}
+
+/* The Mailbox Factory.
+   Create an iterator for registrar and see if any url scheme match,
+   Then we call the mailbox's mu_url_create() to parse the URL. Last
+   initialize the concrete mailbox and folder.  */
+int
+mu_mailbox_create (mu_mailbox_t *pmbox, const char *name)
+{
+  if (pmbox == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+
+  return _create_mailbox (pmbox, name);
+}
+
+int
+mu_mailbox_create_from_url (mu_mailbox_t *pmbox, mu_url_t url)
+{
+  if (pmbox == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+  return _create_mailbox0 (pmbox, url, mu_url_to_string (url));
+}
+
+int
+mu_mailbox_create_from_record (mu_mailbox_t *pmbox, mu_record_t record,
+			       const char *name)
+{
+  mu_url_t url;
+  int rc;
+  
+  rc = mu_url_create (&url, name);
+  if (rc)
+    return rc;
+  rc = mu_url_parse (url);
+  if (rc == 0)
+    rc = _mailbox_create_from_record (pmbox, record, url, name);
+  if (rc)
+    mu_url_destroy (&url);
+  return rc;
+}
+
+void
+mu_mailbox_destroy (mu_mailbox_t *pmbox)
+{
+  if (pmbox && *pmbox)
+    {
+      mu_mailbox_t mbox = *pmbox;
+      mu_monitor_t monitor = mbox->monitor;
+
+      /* Notify the observers.  */
+      if (mbox->observable)
+	{
+	  mu_observable_notify (mbox->observable, MU_EVT_MAILBOX_DESTROY,
+				mbox);
+	  mu_observable_destroy (&mbox->observable, mbox);
+	}
+
+      /* Call the concrete mailbox _destroy method. So it can clean itself.  */
+      if (mbox->_destroy)
+	mbox->_destroy (mbox);
+
+      mu_monitor_wrlock (monitor);
+
+      /* Close the stream and nuke it */
+      if (mbox->stream)
+	{
+	  /* FIXME: Is this right, should the client be responsible
+	     for closing the stream?  */
+	  /* mu_stream_close (mbox->stream); */
+	  mu_stream_destroy (&mbox->stream);
+	}
+
+      if (mbox->url)
+        mu_url_destroy (&mbox->url);
+
+      if (mbox->locker)
+	mu_locker_destroy (&mbox->locker);
+
+      if (mbox->debug)
+	mu_debug_destroy (&mbox->debug, mbox);
+
+      if (mbox->folder)
+	mu_folder_destroy (&mbox->folder);
+
+      if (mbox->property)
+	mu_property_destroy (&mbox->property, mbox);
+
+      free (mbox);
+      *pmbox = NULL;
+      mu_monitor_unlock (monitor);
+      mu_monitor_destroy (&monitor, mbox);
+    }
+}
+
+
+/* -------------- stub functions ------------------- */
+
+int
+mu_mailbox_open (mu_mailbox_t mbox, int flag)
+{
+  int rc;
+  
+  if (!mbox)
+    return MU_ERR_MBX_NULL;
+  if (mbox->_open == NULL)
+    return MU_ERR_EMPTY_VFN;
+  if (mbox->flags & _MU_MAILBOX_OPEN)
+    return MU_ERR_OPEN;
+  if (flag & MU_STREAM_QACCESS)
+    {
+      /* Quick access mailboxes are read-only */
+      if (flag & (MU_STREAM_WRITE | MU_STREAM_RDWR
+		  | MU_STREAM_APPEND | MU_STREAM_CREAT))
+	return EINVAL; /* FIXME: Better error code, please? */
+    }
+  rc = mbox->_open (mbox, flag);
+  if (rc == 0)
+    mbox->flags |= _MU_MAILBOX_OPEN;
+  return rc;
+}
+
+int
+mu_mailbox_close (mu_mailbox_t mbox)
+{
+  int rc;
+
+  if (!mbox)
+    return MU_ERR_MBX_NULL;
+  if (!(mbox->flags & _MU_MAILBOX_OPEN))
+    return MU_ERR_NOT_OPEN;
+  if (mbox == NULL || mbox->_close == NULL)
+    return MU_ERR_EMPTY_VFN;
+
+  rc = mbox->_close (mbox);
+  if (rc == 0)
+    mbox->flags &= ~_MU_MAILBOX_OPEN;
+  return rc;
+}
+
+int
+mu_mailbox_remove (mu_mailbox_t mbox)
+{
+  if (!mbox)
+    return MU_ERR_MBX_NULL;
+  if (mbox->flags & _MU_MAILBOX_OPEN)
+    return MU_ERR_OPEN;
+  if (mbox->flags & _MU_MAILBOX_REMOVED)
+    return MU_ERR_MBX_REMOVED;
+  if (!mbox->_remove)
+    return MU_ERR_EMPTY_VFN;
+  return mbox->_remove (mbox);
+}
+
+int
+mu_mailbox_flush (mu_mailbox_t mbox, int expunge)
+{
+  size_t i, total = 0;
+  int status = 0;
+  
+  if (!mbox)
+    return MU_ERR_MBX_NULL;
+  if (mbox->flags & _MU_MAILBOX_REMOVED)
+    return MU_ERR_MBX_REMOVED;
+  if (!(mbox->flags & _MU_MAILBOX_OPEN))
+    return _MU_MAILBOX_OPEN;
+  if (!(mbox->flags & (MU_STREAM_RDWR|MU_STREAM_WRITE|MU_STREAM_APPEND)))
+    return 0;
+
+  mu_mailbox_messages_count (mbox, &total);
+  if (!(mbox->flags & MU_STREAM_APPEND))
+    for (i = 1; i <= total; i++)
+      {
+	mu_message_t msg = NULL;
+	mu_attribute_t attr = NULL;
+	mu_mailbox_get_message (mbox, i, &msg);
+	mu_message_get_attribute (msg, &attr);
+	mu_attribute_set_seen (attr);
+      }
+
+  if (expunge)
+    status = mu_mailbox_expunge (mbox);
+  else
+    status = mu_mailbox_sync (mbox);
+
+  return status;
+}
+
+#define _MBOX_CHECK_FLAGS(mbox)			\
+  if (mbox == NULL)				\
+    return MU_ERR_MBX_NULL;			\
+  if (mbox->flags & _MU_MAILBOX_REMOVED)	\
+    return MU_ERR_MBX_REMOVED;			\
+  if (!(mbox->flags & _MU_MAILBOX_OPEN))	\
+    return _MU_MAILBOX_OPEN
+
+#define _MBOX_CHECK(mbox,method)		\
+  _MBOX_CHECK_FLAGS(mbox);			\
+  if (mbox->method == NULL)			\
+    return MU_ERR_EMPTY_VFN
+
+#define _MBOX_CHECK_Q(mbox,method)		\
+  _MBOX_CHECK(mbox,method);			\
+  if (mbox->flags & MU_STREAM_QACCESS)		\
+    return MU_ERR_BADOP
+
+/* messages */
+int
+mu_mailbox_append_message (mu_mailbox_t mbox, mu_message_t msg)
+{
+  _MBOX_CHECK_Q (mbox, _append_message);
+  if (!(mbox->flags & (MU_STREAM_RDWR|MU_STREAM_WRITE|MU_STREAM_APPEND)))
+    return EACCES;
+  return mbox->_append_message (mbox, msg);
+}
+
+int
+mu_mailbox_get_message (mu_mailbox_t mbox, size_t msgno,  mu_message_t *pmsg)
+{
+  _MBOX_CHECK_Q (mbox, _get_message);
+  return mbox->_get_message (mbox, msgno, pmsg);
+}
+
+int
+mu_mailbox_quick_get_message (mu_mailbox_t mbox, mu_message_qid_t qid,
+			      mu_message_t *pmsg)
+{
+  _MBOX_CHECK (mbox, _quick_get_message);
+  if (!(mbox->flags & MU_STREAM_QACCESS))
+    return MU_ERR_BADOP;
+  return mbox->_quick_get_message (mbox, qid, pmsg);
+}
+
+int
+mu_mailbox_messages_count (mu_mailbox_t mbox, size_t *num)
+{
+  _MBOX_CHECK_Q (mbox, _messages_count);
+  return mbox->_messages_count (mbox, num);
+}
+
+int
+mu_mailbox_messages_recent (mu_mailbox_t mbox, size_t *num)
+{
+  _MBOX_CHECK_Q (mbox, _messages_recent);
+  return mbox->_messages_recent (mbox, num);
+}
+
+int
+mu_mailbox_message_unseen (mu_mailbox_t mbox, size_t *num)
+{
+  _MBOX_CHECK_Q (mbox, _message_unseen);
+  return mbox->_message_unseen (mbox, num);
+}
+
+int
+mu_mailbox_sync (mu_mailbox_t mbox)
+{
+  _MBOX_CHECK_Q (mbox, _sync);
+  if (!(mbox->flags & (MU_STREAM_RDWR|MU_STREAM_WRITE|MU_STREAM_APPEND)))
+    return 0;
+  return mbox->_sync (mbox);
+}
+
+/* Historic alias: */
+int
+mu_mailbox_save_attributes (mu_mailbox_t mbox)
+{
+  return mu_mailbox_sync (mbox);
+}
+
+int
+mu_mailbox_expunge (mu_mailbox_t mbox)
+{
+  _MBOX_CHECK_Q (mbox, _expunge);
+  if (!(mbox->flags & (MU_STREAM_RDWR|MU_STREAM_WRITE|MU_STREAM_APPEND)))
+    return EACCES;
+  return mbox->_expunge (mbox);
+}
+
+int
+mu_mailbox_is_updated (mu_mailbox_t mbox)
+{
+  if (mbox == NULL ||
+      !(mbox->flags & _MU_MAILBOX_OPEN) ||
+      (mbox->flags & _MU_MAILBOX_REMOVED) ||
+      mbox->_is_updated == NULL)
+    return 1;
+  if (mbox->flags & MU_STREAM_QACCESS)
+    return 1;
+  return mbox->_is_updated (mbox);
+}
+
+int
+mu_mailbox_scan (mu_mailbox_t mbox, size_t msgno, size_t *pcount)
+{
+  _MBOX_CHECK_Q (mbox, _scan);
+  return mbox->_scan (mbox, msgno, pcount);
+}
+
+int
+mu_mailbox_get_size (mu_mailbox_t mbox, mu_off_t *psize)
+{
+  int status;
+
+  _MBOX_CHECK_FLAGS (mbox);
+  if (mbox->flags & MU_STREAM_QACCESS)
+    return MU_ERR_BADOP;
+  if (mbox->_get_size == NULL
+      || (status = mbox->_get_size (mbox, psize)) == ENOSYS)
+    {
+      /* Fall back to brute-force method */
+      size_t i, total;
+      mu_off_t size = 0;
+      
+      status = mu_mailbox_messages_count (mbox, &total);
+      if (status)
+	return status;
+      for (i = 1; i <= total; i++)
+	{
+	  mu_message_t msg;
+	  size_t msgsize;
+	  status = mu_mailbox_get_message (mbox, i, &msg);
+	  if (status)
+	    return status;
+	  status = mu_message_size (msg, &msgsize);
+	  if (status)
+	    return status;
+	  size += msgsize;
+	}
+      *psize = size;
+    }
+  return status;
+}
+
+int
+mu_mailbox_uidvalidity (mu_mailbox_t mbox, unsigned long *pvalid)
+{
+  _MBOX_CHECK_Q (mbox, _uidvalidity);
+  return mbox->_uidvalidity (mbox, pvalid);
+}
+
+int
+mu_mailbox_uidnext (mu_mailbox_t mbox, size_t *puidnext)
+{
+  _MBOX_CHECK_Q (mbox, _uidnext);
+  return mbox->_uidnext (mbox, puidnext);
+}
+
+/* locking */
+int
+mu_mailbox_set_locker (mu_mailbox_t mbox, mu_locker_t locker)
+{
+  if (mbox == NULL)
+    return MU_ERR_MBX_NULL;
+  if (mbox->locker)
+    mu_locker_destroy (&mbox->locker);
+  mbox->locker = locker;
+  return 0;
+}
+
+int
+mu_mailbox_get_locker (mu_mailbox_t mbox, mu_locker_t *plocker)
+{
+  if (mbox == NULL)
+    return MU_ERR_MBX_NULL;
+  if (plocker == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+  *plocker = mbox->locker;
+  return 0;
+}
+
+int
+mu_mailbox_get_flags (mu_mailbox_t mbox, int *flags)
+{
+  if (mbox == NULL)
+    return MU_ERR_MBX_NULL;
+  if (!*flags)
+    return MU_ERR_OUT_NULL;
+  *flags = mbox->flags & ~_MU_MAILBOX_MASK;
+  return 0;
+}
+
+int
+mu_mailbox_set_stream (mu_mailbox_t mbox, mu_stream_t stream)
+{
+  if (mbox == NULL)
+    return MU_ERR_MBX_NULL;
+  if (mbox->flags & MU_STREAM_QACCESS)
+    return MU_ERR_BADOP;
+  if (mbox->stream)
+    mu_stream_destroy (&mbox->stream);
+  mbox->stream = stream;
+  return 0;
+}
+
+/* FIXME: This is a problem.  We provide a mu_mailbox_get_stream ()
+   and this stream is special it should, in theory, represent
+   a "view" of a flow of messages.  But providing this perspective
+   may make sense for local mailboxes but downright impossible
+   for a remote mailbox, short on downloading the entire mailbox
+   locally.
+   The question is : should this function be removed?
+   So far it as been used on local mailboxes to get offsets.  */
+int
+mu_mailbox_get_stream (mu_mailbox_t mbox, mu_stream_t *pstream)
+{
+  /* FIXME: Deprecation warning */
+  if (mbox == NULL)
+    return MU_ERR_MBX_NULL;
+  if (pstream == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+
+  /* If null two cases:
+     - it is no open yet.
+     - it a remote stream and the socket stream is on the folder.  */
+  if (mbox->stream == NULL)
+    {
+      if (mbox->folder)
+	return mu_folder_get_stream (mbox->folder, pstream);
+    }
+  *pstream = mbox->stream;
+  return 0;
+}
+
+int
+mu_mailbox_get_streamref (mu_mailbox_t mbox, mu_stream_t *pstream)
+{
+  if (mbox == NULL)
+    return MU_ERR_MBX_NULL;
+  if (pstream == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+
+  /* If null two cases:
+     - it is no open yet.
+     - it a remote stream and the socket stream is on the folder.  */
+  if (mbox->stream == NULL)
+    {
+      if (mbox->folder)
+	return mu_folder_get_streamref (mbox->folder, pstream);
+    }
+  return mu_streamref_create (pstream, mbox->stream);
+}
+
+int
+mu_mailbox_get_observable (mu_mailbox_t mbox, mu_observable_t *pobservable)
+{
+  if (mbox == NULL)
+    return MU_ERR_MBX_NULL;
+  if (pobservable == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+
+  if (mbox->observable == NULL)
+    {
+      int status = mu_observable_create (&mbox->observable, mbox);
+      if (status != 0)
+	return status;
+    }
+  *pobservable = mbox->observable;
+  return 0;
+}
+
+int
+mu_mailbox_get_property (mu_mailbox_t mbox, mu_property_t *pproperty)
+{
+  if (mbox == NULL)
+    return MU_ERR_MBX_NULL;
+  if (pproperty == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+  
+  if (mbox->property == NULL)
+    {
+      int status = mu_property_create (&mbox->property, mbox);
+      if (status != 0)
+	return status;
+    }
+  *pproperty = mbox->property;
+  return 0;
+}
+
+int
+mu_mailbox_has_debug (mu_mailbox_t mailbox)
+{
+  if (mailbox == NULL)
+    return 0;
+
+  return mailbox->debug ? 1 : 0;
+}
+
+int
+mu_mailbox_set_debug (mu_mailbox_t mbox, mu_debug_t debug)
+{
+  if (mbox == NULL)
+    return MU_ERR_MBX_NULL;
+  if (mbox->debug)
+    mu_debug_destroy (&mbox->debug, mbox);
+  mbox->debug = debug;
+  /* FIXME: Honor MU_DEBUG_INHERIT */
+  if (!mu_folder_has_debug (mbox->folder))
+    mu_folder_set_debug (mbox->folder, debug);
+  return 0;
+}
+
+int
+mu_mailbox_get_debug (mu_mailbox_t mbox, mu_debug_t *pdebug)
+{
+  if (mbox == NULL)
+    return MU_ERR_MBX_NULL;
+  if (pdebug == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+  if (mbox->debug == NULL)
+    {
+      int status = mu_debug_create (&mbox->debug, mbox);
+      if (status != 0)
+	return status;
+      /* FIXME: MU_DEBUG_INHERIT?? */
+      if (!mu_folder_has_debug (mbox->folder))
+	mu_folder_set_debug (mbox->folder, mbox->debug);
+    }
+  *pdebug = mbox->debug;
+  return 0;
+}
+
+int
+mu_mailbox_get_url (mu_mailbox_t mbox, mu_url_t *purl)
+{
+  if (mbox == NULL)
+    return MU_ERR_MBX_NULL;
+  if (purl == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+  *purl = mbox->url;
+  return 0;
+}
+
+int
+mu_mailbox_get_folder (mu_mailbox_t mbox, mu_folder_t *pfolder)
+{
+  if (mbox == NULL)
+    return MU_ERR_MBX_NULL;
+  if (pfolder == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+  *pfolder = mbox->folder;
+  return 0;
+}
+
+int
+mu_mailbox_set_folder (mu_mailbox_t mbox, mu_folder_t folder)
+{
+  if (mbox == NULL)
+    return MU_ERR_MBX_NULL;
+   mbox->folder = folder;
+  return 0;
+}
+
+int
+mu_mailbox_lock (mu_mailbox_t mbox)
+{
+  mu_locker_t lock = NULL;
+  mu_mailbox_get_locker (mbox, &lock);
+  return mu_locker_lock (lock);
+}
+
+int
+mu_mailbox_unlock (mu_mailbox_t mbox)
+{
+  mu_locker_t lock = NULL;
+  mu_mailbox_get_locker (mbox, &lock);
+  return mu_locker_unlock (lock);
+}
+
+int
+mu_mailbox_get_uidls (mu_mailbox_t mbox, mu_list_t *plist)
+{
+  mu_list_t list;
+  int status;
+
+  if (mbox == NULL)
+    return MU_ERR_MBX_NULL;
+  if (plist == NULL)
+    return EINVAL;
+  status = mu_list_create (&list);
+  if (status)
+    return status;
+  mu_list_set_destroy_item (list, mu_list_free_item);
+  if (mbox->_get_uidls)
+    status = mbox->_get_uidls (mbox, list);
+  else
+    {
+      size_t i, total;
+
+      status = mu_mailbox_messages_count (mbox, &total);
+      if (status)
+	return status;
+      for (i = 1; i <= total; i++)
+	{
+	  mu_message_t msg = NULL;
+	  char buf[MU_UIDL_BUFFER_SIZE];
+	  size_t n;
+	  struct mu_uidl *uidl;
+	  
+	  status = mu_mailbox_get_message (mbox, i, &msg);
+	  if (status)
+	    break;
+	  status = mu_message_get_uidl (msg, buf, sizeof (buf), &n);
+	  if (status)
+	    break;
+	  uidl = malloc (sizeof (uidl[0]));
+	  if (!uidl)
+	    {
+	      status = ENOMEM;
+	      break;
+	    }
+	  uidl->msgno = i;
+	  strncpy (uidl->uidl, strdup (buf), MU_UIDL_BUFFER_SIZE);
+	  status = mu_list_append (list, uidl);
+	  if (status)
+	    {
+	      free (uidl);
+	      break;
+	    }
+	}
+    }
+  *plist = list;
+  return status;
+}
+
+	  
diff --git a/libmailutils/mailbox/mbx_default.c b/libmailutils/mailbox/mbx_default.c
new file mode 100644
index 0000000..9069c6f
--- /dev/null
+++ b/libmailutils/mailbox/mbx_default.c
@@ -0,0 +1,453 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 2003, 2004, 2005, 2006, 2007, 2008,
+   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, see
+   <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <string.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <stdio.h>
+#include <pwd.h>
+#include <unistd.h>
+
+#include <confpaths.h>
+
+#include <mailutils/mailbox.h>
+#include <mailutils/mutil.h>
+#include <mailutils/debug.h>
+#include <mailutils/error.h>
+#include <mailutils/errno.h>
+#include <mailutils/mu_auth.h>
+#include <mailutils/vartab.h>
+#include <mailutils/folder.h>
+#include <mailutils/auth.h>
+
+#include <mailutils/sys/mailbox.h>
+
+char *mu_ticket_file = "~/.mu-tickets";
+
+static char *_mu_mailbox_pattern;
+
+static char *_default_folder_dir = "Mail";
+static char *_mu_folder_dir;
+
+static int
+mu_normalize_mailbox_url (char **pout, const char *dir)
+{
+  int len;
+  int addslash = 0;
+#define USERSUFFIX "${user}"
+  
+  if (!pout)
+    return MU_ERR_OUT_PTR_NULL;
+      
+  len = strlen (dir);
+  if (dir[len-1] == '=')
+    {
+      if (len > 5 && strcmp (dir + len - 5, "user=") == 0)
+	*pout = strdup (dir);
+      else
+	return MU_ERR_BAD_FILENAME;
+    }
+  else if (dir[len-1] != '/')
+    addslash = 1;
+
+  *pout = malloc (strlen (dir) + (addslash ? 1 : 0) + sizeof USERSUFFIX);
+  if (!*pout)
+    return ENOMEM;
+
+  strcpy (*pout, dir);
+  if (addslash)
+    strcat (*pout, "/");
+  strcat (*pout, USERSUFFIX);
+#undef USERSUFFIX
+  return 0;
+}
+
+int
+mu_set_mail_directory (const char *p)
+{
+  if (_mu_mailbox_pattern)
+    free (_mu_mailbox_pattern);
+  if (!p)
+    {
+      _mu_mailbox_pattern = NULL;
+      return 0;
+    }
+  return mu_normalize_mailbox_url (&_mu_mailbox_pattern, p);
+}
+
+int
+mu_set_mailbox_pattern (const char *pat)
+{
+  if (_mu_mailbox_pattern)
+    free (_mu_mailbox_pattern);
+  if (!pat)
+    {
+      _mu_mailbox_pattern = NULL;
+      return 0;
+    }
+  _mu_mailbox_pattern = strdup (pat);
+  return _mu_mailbox_pattern ? 0 : ENOMEM;
+}
+
+void
+mu_set_folder_directory (const char *p)
+{
+  if (_mu_folder_dir != _default_folder_dir)
+    free (_mu_folder_dir);
+  _mu_folder_dir = strdup (p);
+}
+
+const char *
+mu_mailbox_url ()
+{
+  if (!_mu_mailbox_pattern)
+    mu_set_mail_directory (MU_PATH_MAILDIR);
+  return _mu_mailbox_pattern;
+}
+
+const char *
+mu_folder_directory ()
+{
+  if (!_mu_folder_dir)
+    _mu_folder_dir = _default_folder_dir;
+  return _mu_folder_dir;
+}
+
+int
+mu_construct_user_mailbox_url (char **pout, const char *name)
+{
+  int rc;
+  const char *pat = mu_mailbox_url ();
+  mu_vartab_t vtab;
+
+  mu_vartab_create (&vtab);
+  mu_vartab_define (vtab, "user", name, 1);
+  rc = mu_vartab_expand (vtab, pat, pout);
+  mu_vartab_destroy (&vtab);
+  return rc;
+}
+
+/* Is this a security risk?  */
+#define USE_ENVIRON 1
+
+static int
+split_shortcut (const char *file, const char pfx[], char **user, char **rest)
+{
+  *user = NULL;
+  *rest = NULL;
+
+  if (!strchr (pfx, file[0]))
+    return 0;
+
+  if (*++file == 0)
+    return 0;
+  else
+    {
+      char *p = strchr (file, '/');
+      int len;
+      if (p)
+        len = p - file + 1;
+      else
+        len = strlen (file) + 1;
+
+      if (len == 1)
+	*user = NULL;
+      else
+	{
+	  *user = calloc (1, len);
+	  if (!*user)
+	    return ENOMEM;
+
+	  memcpy (*user, file, len);
+	  (*user)[len-1] = 0;
+	}
+      file += len-1;
+      if (file[0] == '/')
+        file++;
+    }
+
+  if (file[0])
+    {
+      *rest = strdup (file);
+      if (!*rest)
+        {
+          free (*user);
+          return ENOMEM;
+        }
+    }
+  
+  return 0;
+}
+
+static char *
+get_homedir (const char *user)
+{
+  char *homedir = NULL;
+  struct mu_auth_data *auth = NULL;
+  
+  if (user)
+    {
+      auth = mu_get_auth_by_name (user);
+      if (auth)
+        homedir = auth->dir;
+    }
+  else
+    {
+#ifdef USE_ENVIRON
+      /* NOTE: Should we honor ${HOME}?  */
+      homedir = getenv ("HOME");
+      if (homedir == NULL)
+        {
+	  auth = mu_get_auth_by_name (user);
+	  if (auth)
+	    homedir = auth->dir;
+        }
+#else
+      auth = mu_get_auth_by_name (user);
+      if (auth)
+	homedir = auth->dir;
+#endif
+    }
+
+  if (homedir)
+    homedir = strdup (homedir);
+  mu_auth_data_free (auth);
+  return homedir;
+}
+
+static int
+user_mailbox_name (const char *user, char **mailbox_name)
+{
+#ifdef USE_ENVIRON
+  if (!user)
+    user = (getenv ("LOGNAME")) ? getenv ("LOGNAME") : getenv ("USER");
+#endif
+
+  if (user)
+    {
+      int rc = mu_construct_user_mailbox_url (mailbox_name, user);
+      if (rc)
+	return rc;
+    }
+  else
+    {
+      struct mu_auth_data *auth = mu_get_auth_by_uid (getuid ());
+
+      if (!auth)
+        {
+          mu_error ("Who am I?");
+          return EINVAL;
+        }
+      *mailbox_name = strdup (auth->mailbox);
+      mu_auth_data_free (auth);
+    }
+
+  return 0;
+}
+
+static int
+plus_expand (const char *file, char **buf)
+{
+  char *home;
+  const char *folder_dir = mu_folder_directory ();
+  int len;
+
+  home = get_homedir (NULL);
+  if (!home)
+    return ENOENT;
+  
+  file++;
+  
+  if (folder_dir[0] == '/' || mu_is_proto (folder_dir))
+    {
+      len = strlen (folder_dir) + strlen (file) + 2;
+      *buf = malloc (len);
+      sprintf (*buf, "%s/%s", folder_dir, file);
+    }
+  else
+    {
+      len = strlen (home) + strlen (folder_dir) + strlen (file) + 3;
+      *buf = malloc (len);
+      sprintf (*buf, "%s/%s/%s", home, folder_dir, file);
+    }
+  (*buf)[len-1] = 0;
+  
+  free (home);
+  return 0;
+}
+
+static int
+percent_expand (const char *file, char **mbox)
+{
+  char *user = NULL;
+  char *path = NULL;
+  int status;
+  
+  if ((status = split_shortcut (file, "%", &user, &path)))
+    return status;
+
+  if (path)
+    {
+      free (user);
+      free (path);
+      return ENOENT;
+    }
+
+  status = user_mailbox_name (user, mbox);
+  free (user);
+  return status;
+}
+
+static void
+attach_auth_ticket (mu_mailbox_t mbox)
+{
+  mu_folder_t folder = NULL;
+  mu_authority_t auth = NULL;
+
+  if (mu_mailbox_get_folder (mbox, &folder) == 0
+      && mu_folder_get_authority (folder, &auth) == 0
+      && auth)
+    {
+      char *filename = mu_tilde_expansion (mu_ticket_file, "/", NULL);
+      mu_wicket_t wicket;
+      int rc;
+  
+      MU_DEBUG1 (mbox->debug, MU_DEBUG_TRACE1,
+		 "Reading user ticket file %s\n", filename);
+      if ((rc = mu_file_wicket_create (&wicket, filename)) == 0)
+	{
+	  mu_ticket_t ticket;
+      
+	  if ((rc = mu_wicket_get_ticket (wicket, NULL, &ticket)) == 0)
+	    {
+	      rc = mu_authority_set_ticket (auth, ticket);
+	      MU_DEBUG1 (mbox->debug, MU_DEBUG_TRACE1,
+			 "Retrieved and set ticket: %d\n", rc);
+	    }
+	  else
+	    MU_DEBUG1 (mbox->debug, MU_DEBUG_ERROR,
+		       "Error retrieving ticket: %s\n",
+		       mu_strerror (rc));
+	  mu_wicket_destroy (&wicket);
+	}
+      else
+	MU_DEBUG1 (mbox->debug, MU_DEBUG_ERROR,
+		   "Error creating wicket: %s\n", mu_strerror (rc));
+      free (filename);
+    }
+}
+
+/* We are trying to be smart about the location of the mail.
+   mu_mailbox_create() is not doing this.
+   %           --> system mailbox for the real uid
+   %user       --> system mailbox for the given user
+   ~/file      --> /home/user/file
+   ~user/file  --> /home/user/file
+   +file       --> /home/user/Mail/file
+   =file       --> /home/user/Mail/file
+*/
+int
+mu_mailbox_create_default (mu_mailbox_t *pmbox, const char *mail)
+{
+  char *mbox = NULL;
+  char *tmp_mbox = NULL;
+  char *p;
+  int status = 0;
+
+  /* Sanity.  */
+  if (pmbox == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+
+  if (mail && *mail == 0)
+    mail = NULL;
+  
+  if (mail == NULL)
+    {
+      if (!_mu_mailbox_pattern)
+	{
+	  /* Other utilities may not understand GNU mailutils url namespace, so
+	     use FOLDER instead, to not confuse others by using MAIL.  */
+	  mail = getenv ("FOLDER");
+	  if (!mail)
+	    {
+	      /* Fallback to well-known environment.  */
+	      mail = getenv ("MAIL");
+	    }
+	}
+
+      if (!mail)
+	{
+	  if ((status = user_mailbox_name (NULL, &tmp_mbox)))
+	    return status;
+	  mail = tmp_mbox;
+	}
+    }
+
+  p = mu_tilde_expansion (mail, "/", NULL);
+  if (tmp_mbox)
+    free (tmp_mbox);
+  tmp_mbox = p;
+  mail = tmp_mbox;
+  if (!mail)
+    return ENOMEM;
+  
+  switch (mail[0])
+    {
+    case '%':
+      status = percent_expand (mail, &mbox);
+      break;
+      
+    case '+':
+    case '=':
+      status = plus_expand (mail, &mbox);
+      break;
+
+    case '/':
+      mbox = strdup (mail);
+      break;
+      
+    default:
+      if (!mu_is_proto (mail))
+	{
+	  p = mu_getcwd();
+	  mbox = malloc (strlen (p) + strlen (mail) + 2);
+	  sprintf (mbox, "%s/%s", p, mail);
+	  free (p);  
+	}
+      else
+	mbox = strdup (mail);
+      break;
+    }
+
+  if (tmp_mbox)
+    free (tmp_mbox);
+
+  if (status)
+    return status;
+  
+  status = mu_mailbox_create (pmbox, mbox);
+  free (mbox);
+  if (status == 0)
+    attach_auth_ticket (*pmbox);
+      
+  return status;
+}
diff --git a/libmailutils/mailbox/mbxitr.c b/libmailutils/mailbox/mbxitr.c
new file mode 100644
index 0000000..c8ae139
--- /dev/null
+++ b/libmailutils/mailbox/mbxitr.c
@@ -0,0 +1,159 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 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, see
+   <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <errno.h>
+#include <string.h>
+
+#include <mailutils/debug.h>
+#include <mailutils/errno.h>
+#include <mailutils/error.h>
+#include <mailutils/iterator.h>
+
+#include <mailutils/sys/mailbox.h>
+
+struct mailbox_iterator
+{
+  mu_mailbox_t mbx;
+  size_t idx;
+};
+
+static int
+mbx_first (void *owner)
+{
+  struct mailbox_iterator *itr = owner;
+  itr->idx = 1;
+  return 0;
+}
+
+static int
+mbx_next (void *owner)
+{
+  struct mailbox_iterator *itr = owner;
+  itr->idx++;
+  return 0;
+}
+
+static int
+mbx_getitem (void *owner, void **pret, const void **pkey)
+{
+  struct mailbox_iterator *itr = owner;
+  size_t count;
+  int rc;
+  
+  rc = mu_mailbox_messages_count (itr->mbx, &count);
+  if (rc)
+    return rc;
+  if (itr->idx > count)
+    return MU_ERR_NOENT;
+  rc = mu_mailbox_get_message (itr->mbx, itr->idx, (mu_message_t*)pret);
+  if (rc == 0 && pkey)
+    *pkey = NULL; /* FIXME: Perhaps return UIDL or other unique id? */
+  return rc;
+}
+
+static int
+mbx_finished_p (void *owner)
+{
+  struct mailbox_iterator *itr = owner;
+  size_t count;
+
+  if (mu_mailbox_messages_count (itr->mbx, &count))
+    return 1;
+  return itr->idx > count;
+}
+
+static int
+mbx_destroy (mu_iterator_t iterator, void *data)
+{
+  struct mailbox_iterator *itr = data;
+
+  mu_iterator_detach (&itr->mbx->iterator, iterator);
+  free (data);
+  return 0;
+}
+
+static int
+mbx_curitem_p (void *owner, void *item)
+{
+  void *ptr;
+
+  if (mbx_getitem (owner, &ptr, NULL))
+    return 0;
+  return ptr == item;/* FIXME: Is it ok? */
+}
+
+static int
+mbx_data_dup (void **ptr, void *owner)
+{
+  struct mailbox_iterator *itr = owner;
+
+  *ptr = malloc (sizeof (struct mailbox_iterator));
+  if (*ptr == NULL)
+    return ENOMEM;
+  memcpy (*ptr, owner, sizeof (struct mailbox_iterator));
+  mu_iterator_attach (&itr->mbx->iterator, *ptr);
+  return 0;
+}
+
+int
+mu_mailbox_get_iterator (mu_mailbox_t mbx, mu_iterator_t *piterator)
+{
+  mu_iterator_t iterator;
+  int status;
+  struct mailbox_iterator *itr;
+
+  if (!mbx)
+    return EINVAL;
+
+  itr = calloc (1, sizeof *itr);
+  if (!itr)
+    return ENOMEM;
+  itr->mbx = mbx;
+  itr->idx = 1;
+
+  status = mu_iterator_create (&iterator, itr);
+  if (status)
+    {
+      free (itr);
+      return status;
+    }
+
+  mu_iterator_set_first (iterator, mbx_first);
+  mu_iterator_set_next (iterator, mbx_next);
+  mu_iterator_set_getitem (iterator, mbx_getitem);
+  mu_iterator_set_finished_p (iterator, mbx_finished_p);
+  mu_iterator_set_curitem_p (iterator, mbx_curitem_p);
+  mu_iterator_set_destroy (iterator, mbx_destroy);
+  mu_iterator_set_dup (iterator, mbx_data_dup);
+
+  mu_iterator_attach (&mbx->iterator, iterator);
+
+  *piterator = iterator;
+  return 0;
+}
+
+
+
+
+
+  
+
diff --git a/libmailutils/mailbox/message.c b/libmailutils/mailbox/message.c
new file mode 100644
index 0000000..b6f81b7
--- /dev/null
+++ b/libmailutils/mailbox/message.c
@@ -0,0 +1,1293 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005, 2006, 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, see
+   <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <time.h>
+#include <unistd.h>
+#include <string.h>
+#include <pwd.h>
+
+#include <mailutils/cctype.h>
+#include <mailutils/address.h>
+#include <mailutils/attribute.h>
+#include <mailutils/auth.h>
+#include <mailutils/body.h>
+#include <mailutils/debug.h>
+#include <mailutils/envelope.h>
+#include <mailutils/errno.h>
+#include <mailutils/folder.h>
+#include <mailutils/header.h>
+#include <mailutils/mailbox.h>
+#include <mailutils/mutil.h>
+#include <mailutils/observer.h>
+#include <mailutils/stream.h>
+#include <mailutils/mu_auth.h>
+#include <mailutils/nls.h>
+#include <mailutils/md5.h>
+#include <mailutils/io.h>
+
+#include <mailutils/sys/message.h>
+#include <mailutils/sys/stream.h>
+
+#define MESSAGE_MODIFIED        0x10000
+#define MESSAGE_INTERNAL_STREAM 0x20000
+
+
+/* Message stream */
+
+enum _message_stream_state
+  {
+    _mss_init,
+    _mss_header,
+    _mss_body,
+    _mss_eof
+  };
+
+struct _mu_message_stream
+{
+  struct _mu_stream stream;
+  mu_message_t msg;
+  enum _message_stream_state state;
+  mu_stream_t transport;
+  mu_off_t limit;
+};
+
+static int
+_check_stream_state (struct _mu_message_stream *str)
+{
+  int rc = 0;
+  
+  if (str->transport && mu_stream_eof (str->transport))
+    mu_stream_destroy (&str->transport);
+  
+  switch (str->state)
+    {
+    case _mss_init:
+      if (!str->transport)
+	{
+	  rc = mu_header_get_streamref (str->msg->header, &str->transport);
+	  if (rc == 0)
+	    {
+	      str->state = _mss_header;
+	      rc = mu_stream_seek (str->transport, 0, MU_SEEK_SET, NULL);
+	    }
+	}
+      break;
+      
+    case _mss_header:
+      if (!str->transport)
+	{
+	  rc = mu_body_get_streamref (str->msg->body, &str->transport);
+	  if (rc == 0)
+	    {
+	      str->state = _mss_body;
+	      rc = mu_stream_seek (str->transport, 0, MU_SEEK_SET, NULL);
+	    }
+	}
+      break;
+      
+    case _mss_body:
+      if (!str->transport)
+	str->state = _mss_eof;
+    case _mss_eof:
+      break;
+    }
+  return rc;
+}
+
+static void
+_message_stream_done (struct _mu_stream *str)
+{
+  struct _mu_message_stream *sp = (struct _mu_message_stream *)str;
+  mu_stream_destroy (&sp->transport);
+}
+
+static int
+_message_stream_flush (struct _mu_stream *str)
+{
+  struct _mu_message_stream *sp = (struct _mu_message_stream *)str;
+  int rc = _check_stream_state (sp);
+  if (rc)
+    return rc;
+  return mu_stream_flush (sp->transport);
+}
+  
+static int
+_message_stream_size (struct _mu_stream *str, mu_off_t *psize)
+{
+  struct _mu_message_stream *sp = (struct _mu_message_stream *)str;
+  size_t hsize, bsize;
+  mu_header_size (sp->msg->header, &hsize);
+  mu_body_size (sp->msg->body, &bsize);
+  if (psize)
+    *psize = hsize + bsize;
+  return 0;
+}
+
+static int
+_message_stream_seek (struct _mu_stream *str, mu_off_t off, mu_off_t *ppos)
+{
+  struct _mu_message_stream *sp = (struct _mu_message_stream *)str;
+  size_t hsize, size;
+  int rc;
+  
+  rc = _check_stream_state (sp);
+  if (rc)
+    return rc;
+  mu_header_size (sp->msg->header, &hsize);
+  mu_body_size (sp->msg->body, &size);
+  size += hsize;
+  
+  if (off < 0 || off >= size)
+    return ESPIPE;
+
+  switch (sp->state)
+    {
+    case _mss_eof:
+      sp->state = _mss_init;
+      rc = _check_stream_state (sp);
+      if (rc)
+	return rc;
+      /* fall through */
+    case _mss_header:
+      if (off < hsize)
+	break;
+      mu_stream_destroy (&sp->transport);
+      rc = _check_stream_state (sp);
+      if (rc)
+	return rc;
+      /* fall through */
+    case _mss_body:
+      if (off > hsize)
+	off -= hsize;	
+      else
+	{
+	  mu_stream_destroy (&sp->transport);
+	  sp->state = _mss_init;
+	  rc = _check_stream_state (sp);
+	  if (rc)
+	    return rc;
+	}
+
+      break;
+
+    default:
+      break;
+    }
+  rc = mu_stream_seek (sp->transport, off, MU_SEEK_SET, &off);
+  if (rc == 0)
+    {
+      if (sp->state == _mss_body)
+	off += hsize;
+      *ppos = off;
+    }
+  return rc;
+}
+
+static int
+_message_stream_read (struct _mu_stream *str, char *buf, size_t bufsize,
+		      size_t *pnread)
+{
+  struct _mu_message_stream *sp = (struct _mu_message_stream *)str;
+  size_t nread = 0;
+  int rc;
+  
+  while (bufsize)
+    {
+      size_t n;
+      rc = _check_stream_state (sp);
+      if (rc)
+	break;
+      if (sp->state == _mss_eof)
+	break;
+      rc = mu_stream_read (sp->transport, buf, bufsize, &n);
+      nread += n;
+      buf += n;
+      bufsize -= n;
+    }
+  *pnread = nread;
+  return rc;
+}
+
+static int
+_message_stream_readdelim (struct _mu_stream *str, char *buf, size_t bufsize,
+			   int delim, size_t *pnread)
+{
+  struct _mu_message_stream *sp = (struct _mu_message_stream *)str;
+  size_t nread = 0;
+  int rc;
+  
+  while (bufsize)
+    {
+      size_t n;
+      rc = _check_stream_state (sp);
+      if (rc)
+	break;
+      if (sp->state == _mss_eof)
+	break;
+      rc = mu_stream_readdelim (sp->transport, buf, bufsize, delim, &n);
+      if (rc || n == 0)
+	break;
+      nread += n;
+      buf += n;
+      bufsize -= n;
+    }
+  *pnread = nread;
+  return rc;
+}  
+
+#if 0
+static int
+_message_stream_write (struct _mu_stream *str,
+		       const char *buf, size_t bufsize,
+		       size_t *pnwritten)
+{
+  struct _mu_message_stream *sp = (struct _mu_message_stream *)str;
+  
+  /* FIXME */
+}
+#endif
+
+static int
+_message_stream_create (mu_stream_t *pmsg, mu_message_t msg, int flags)
+{
+  struct _mu_message_stream *sp;
+
+  sp = (struct _mu_message_stream *) _mu_stream_create (sizeof (*sp),
+							flags |
+							MU_STREAM_SEEK |
+							_MU_STR_OPEN);
+  if (!sp)
+    return ENOMEM;
+
+  sp->stream.read = _message_stream_read;
+  sp->stream.readdelim = _message_stream_readdelim;
+  /* FIXME: Write is not defined */
+  /*  sp->stream.write = _message_stream_write;*/
+  sp->stream.done = _message_stream_done;
+  sp->stream.flush = _message_stream_flush;
+  sp->stream.seek = _message_stream_seek; 
+  sp->stream.size = _message_stream_size;
+  sp->state = _mss_init;
+  sp->msg = msg;
+  *pmsg = (mu_stream_t) sp;
+  return 0;
+}
+
+
+enum eoh_state
+  {
+    eoh_no,
+    eoh_maybe,
+    eoh_yes
+  };
+
+/* Message header stuff */
+static enum eoh_state
+string_find_eoh (enum eoh_state eoh, const char *str, size_t len,
+		 size_t *ppos)
+{
+  size_t pos;
+
+  if (eoh == eoh_maybe && *str == '\n')
+    {
+      *ppos = 0;
+      return eoh_yes;
+    }
+  
+  for (pos = 0; pos < len - 1; pos++)
+    if (str[pos] == '\n' && str[pos + 1] == '\n')
+      {
+	*ppos = pos + 1;
+	return eoh_yes;
+      }
+  
+  *ppos = pos + 1;
+  return str[pos] == '\n' ? eoh_maybe : eoh_no;
+}
+
+#define MIN_HEADER_BUF_SIZE 2048
+
+static int
+_header_fill (mu_stream_t stream, char **pbuf, size_t *plen)
+{
+  int status = 0;
+  char *buffer = NULL;
+  size_t bufsize = 0;
+  char inbuf[MIN_HEADER_BUF_SIZE];
+  size_t nread;
+  enum eoh_state eoh = eoh_no;
+  
+  status = mu_stream_seek (stream, 0, MU_SEEK_SET, NULL);
+  if (status)
+    return status;
+      
+  while (eoh != eoh_yes
+	 && (status = mu_stream_read (stream, inbuf, sizeof (inbuf), &nread))
+	    == 0
+	 && nread)
+    {
+      char *nbuf;
+      size_t len;
+
+      eoh = string_find_eoh (eoh, inbuf, nread, &len);
+      
+      nbuf = realloc (buffer, bufsize + len);
+      if (!nbuf)
+	{
+	  status = ENOMEM;
+	  break;
+	}
+      memcpy (nbuf + bufsize, inbuf, len);
+      buffer = nbuf;
+      bufsize += len;
+    }
+
+  if (status)
+    free (buffer);
+  else
+    {
+      *pbuf = buffer;
+      *plen = bufsize;
+    }
+  return status;
+}
+    
+static int
+message_header_fill (void *data, char **pbuf, size_t *plen)
+{
+  int status = 0;
+  mu_message_t msg = data;
+  mu_stream_t stream;
+
+  status = mu_message_get_streamref (msg, &stream);
+  if (status == 0)
+    {
+      status = _header_fill (stream, pbuf, plen);
+      mu_stream_destroy (&stream);
+    }
+  return status;
+}
+
+
+/* Message envelope */
+static int
+message_envelope_date (mu_envelope_t envelope, char *buf, size_t len,
+		       size_t *pnwrite)
+{
+  mu_message_t msg = mu_envelope_get_owner (envelope);
+  time_t t;
+  size_t n;
+
+  if (msg == NULL)
+    return EINVAL;
+
+  /* FIXME: extract the time from "Date:".  */
+
+  if (buf == NULL || len == 0)
+    {
+      n = MU_ENVELOPE_DATE_LENGTH;
+    }
+  else
+    {
+      char tmpbuf[MU_ENVELOPE_DATE_LENGTH+1];
+      t = time (NULL);
+      n = mu_strftime (tmpbuf, sizeof tmpbuf, 
+                       MU_ENVELOPE_DATE_FORMAT, localtime (&t));
+      n = mu_cpystr (buf, tmpbuf, len);
+    }
+  if (pnwrite)
+    *pnwrite = n;
+  return 0;
+}
+
+static int
+message_envelope_sender (mu_envelope_t envelope, char *buf, size_t len,
+			 size_t *pnwrite)
+{
+  mu_message_t msg = mu_envelope_get_owner (envelope);
+  mu_header_t header = NULL;
+  size_t n = 0;
+  int status;
+
+  if (msg == NULL)
+    return EINVAL;
+
+  /* Can it be extracted from the From:  */
+  mu_message_get_header (msg, &header);
+  status = mu_header_get_value (header, MU_HEADER_FROM, NULL, 0, &n);
+  if (status == 0 && n != 0)
+    {
+      char *sender;
+      mu_address_t address = NULL;
+      sender = calloc (1, n + 1);
+      if (sender == NULL)
+	return ENOMEM;
+      mu_header_get_value (header, MU_HEADER_FROM, sender, n + 1, NULL);
+      if (mu_address_create (&address, sender) == 0)
+	mu_address_get_email (address, 1, buf, n + 1, pnwrite);
+      free (sender);
+      mu_address_destroy (&address);
+      return 0;
+    }
+  else if (status == EAGAIN)
+    return status;
+
+  /* oops! We are still here */
+  {
+    struct mu_auth_data *auth = mu_get_auth_by_uid (getuid ());
+    const char *sender = auth ? auth->name : "unknown";
+    n = strlen (sender);
+    if (buf && len > 0)
+      {
+	len--; /* One for the null.  */
+	n = (n < len) ? n : len;
+	memcpy (buf, auth->name, n);
+	buf[n] = '\0';
+      }
+    if (auth)
+      mu_auth_data_free (auth);
+  }
+
+  if (pnwrite)
+    *pnwrite = n;
+  return 0;
+}
+
+
+
+/*  Allocate ressources for the mu_message_t.  */
+int
+mu_message_create (mu_message_t *pmsg, void *owner)
+{
+  mu_message_t msg;
+  int status;
+
+  if (pmsg == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+  msg = calloc (1, sizeof (*msg));
+  if (msg == NULL)
+    return ENOMEM;
+  status = mu_monitor_create (&msg->monitor, 0, msg);
+  if (status != 0)
+    {
+      free (msg);
+      return status;
+    }
+  msg->owner = owner;
+  msg->ref = 1;
+  *pmsg = msg;
+  return 0;
+}
+
+void
+mu_message_destroy (mu_message_t *pmsg, void *owner)
+{
+  if (pmsg && *pmsg)
+    {
+      mu_message_t msg = *pmsg;
+      mu_monitor_t monitor = msg->monitor;
+      int destroy_lock = 0;
+
+      mu_monitor_wrlock (monitor);
+      /* Note: msg->ref may be incremented by mu_message_ref without
+	 additional checking for its owner, therefore decrementing
+	 it must also occur independently of the owner checking. Due
+	 to this inconsistency ref may reach negative values, which
+	 is very unfortunate.
+
+	 The `owner' stuff is a leftover from older mailutils versions.
+	 There is an ongoing attempt to remove it in the stream-cleanup
+	 branch. When it is ready, it will be merged to the HEAD and this
+	 will finally resolve this issue. */
+      if (msg->ref > 0)
+	msg->ref--;
+      if ((msg->owner && msg->owner == owner)
+	  || (msg->owner == NULL && msg->ref <= 0))
+	{
+	  destroy_lock =  1;
+	  /* Notify the listeners.  */
+	  /* FIXME: to be removed since we do not support this event.  */
+	  if (msg->observable)
+	    {
+	      mu_observable_notify (msg->observable, MU_EVT_MESSAGE_DESTROY,
+				    msg);
+	      mu_observable_destroy (&msg->observable, msg);
+	    }
+
+	  /* Envelope.  */
+	  if (msg->envelope)
+	    mu_envelope_destroy (&msg->envelope, msg);
+
+	  /* Header.  */
+	  if (msg->header)
+	    mu_header_destroy (&msg->header);
+
+	  /* Body.  */
+	  if (msg->body)
+	    mu_body_destroy (&msg->body, msg);
+
+	  /* Attribute.  */
+	  if (msg->attribute)
+	    mu_attribute_destroy (&msg->attribute, msg);
+
+	  /* Stream.  */
+	  if (msg->stream)
+	    mu_stream_destroy (&msg->stream);
+
+	  /*  Mime.  */
+	  if (msg->mime)
+	    mu_mime_destroy (&msg->mime);
+
+	  /* Loose the owner.  */
+	  msg->owner = NULL;
+
+	  free (msg);
+	}
+      mu_monitor_unlock (monitor);
+      if (destroy_lock)
+	mu_monitor_destroy (&monitor, msg);
+      /* Loose the link */
+      *pmsg = NULL;
+    }
+}
+
+int
+mu_message_create_copy (mu_message_t *to, mu_message_t from)
+{
+  int status = 0;
+  mu_stream_t fromstr = NULL;
+  mu_stream_t tmp = NULL;
+
+  if (!to)
+    return MU_ERR_OUT_PTR_NULL;
+  if (!from)
+    return EINVAL;
+
+  status = mu_memory_stream_create (&tmp, MU_STREAM_RDWR|MU_STREAM_SEEK);
+  if (status)
+    return status;
+
+  status = mu_message_get_streamref (from, &fromstr);
+  if (status)
+    {
+      mu_stream_destroy (&tmp);
+      return status;
+    }
+
+  status = mu_stream_copy (tmp, fromstr, 0, NULL);
+  if (status == 0)
+    {
+      status = mu_message_create (to, NULL);
+      if (status == 0)
+	mu_message_set_stream (*to, tmp, NULL);
+    }
+
+  if (status)
+    mu_stream_destroy (&tmp);
+  mu_stream_destroy (&fromstr);
+
+  return status;
+}
+
+int
+mu_message_ref (mu_message_t msg)
+{
+  if (msg)
+    {
+      mu_monitor_wrlock (msg->monitor);
+      msg->ref++;
+      mu_monitor_unlock (msg->monitor);
+    }
+  return 0;
+}
+
+void *
+mu_message_get_owner (mu_message_t msg)
+{
+  return (msg == NULL) ? NULL : msg->owner;
+}
+
+int
+mu_message_is_modified (mu_message_t msg)
+{
+  int mod = 0;
+  if (msg)
+    {
+      mod |= mu_header_is_modified (msg->header);
+      mod |= mu_attribute_is_modified (msg->attribute);
+      mod |= mu_body_is_modified (msg->body);
+      mod |= msg->flags;
+    }
+  return mod;
+}
+
+int
+mu_message_clear_modified (mu_message_t msg)
+{
+  if (msg)
+    {
+      if (msg->header)
+	mu_header_clear_modified (msg->header);
+      if (msg->attribute)
+	mu_attribute_clear_modified (msg->attribute);
+      if (msg->body)
+	mu_body_clear_modified (msg->body);
+      msg->flags &= ~MESSAGE_MODIFIED;
+    }
+  return 0;
+}
+
+int
+mu_message_get_mailbox (mu_message_t msg, mu_mailbox_t *pmailbox)
+{
+  if (msg == NULL)
+    return EINVAL;
+  if (pmailbox == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+  *pmailbox = msg->mailbox;
+  return 0;
+}
+
+int
+mu_message_set_mailbox (mu_message_t msg, mu_mailbox_t mailbox, void *owner)
+{
+  if (msg == NULL)
+    return EINVAL;
+  if (msg->owner != owner)
+    return EACCES;
+  msg->mailbox = mailbox;
+  return 0;
+}
+
+int
+mu_message_get_header (mu_message_t msg, mu_header_t *phdr)
+{
+  if (msg == NULL)
+    return EINVAL;
+  if (phdr == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+
+  if (msg->header == NULL)
+    {
+      mu_header_t header;
+      int status = mu_header_create (&header, NULL, 0);
+      if (status != 0)
+	return status;
+      if (msg->stream)
+	mu_header_set_fill (header, message_header_fill, msg);
+      status = mu_header_size (header, &msg->orig_header_size);
+      if (status)
+	return status;
+      msg->header = header;
+    }
+  *phdr = msg->header;
+  return 0;
+}
+
+/* Note: mu_message_set_header steals the reference to hdr */
+int
+mu_message_set_header (mu_message_t msg, mu_header_t hdr, void *owner)
+{
+  if (msg == NULL )
+    return EINVAL;
+  if (msg->owner != owner)
+     return EACCES;
+  if (msg->header)
+    mu_header_destroy (&msg->header);
+  msg->header = hdr;
+  msg->flags |= MESSAGE_MODIFIED;
+  return 0;
+}
+
+int
+mu_message_get_body (mu_message_t msg, mu_body_t *pbody)
+{
+  if (msg == NULL)
+    return EINVAL;
+  if (pbody == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+
+  /* Is it a floating mesg.  */
+  if (msg->body == NULL)
+    {
+      mu_body_t body;
+      int status = mu_body_create (&body, msg);
+      if (status != 0)
+	return status;
+      /* If a stream is already set, use it to create the body stream.  */
+      /* FIXME: I'm not sure if the second condition is really needed */
+      if (msg->stream/* && (msg->flags & MESSAGE_INTERNAL_STREAM)*/)
+	{
+	  mu_stream_t stream;
+	  int flags = 0;
+
+	  /* FIXME: The actual mu_header_size cannot be used as offset,
+	     because the headers might have been modified in between. */
+	  
+	  mu_stream_get_flags (msg->stream, &flags);
+	  status = mu_streamref_create_abridged (&stream, msg->stream,
+						 msg->orig_header_size, 0);
+	  if (status)
+	    {
+	      mu_body_destroy (&body, msg);
+	      return status;
+	    }
+	  mu_body_set_stream (body, stream, msg);
+	}
+      msg->body = body;
+    }
+  *pbody = msg->body;
+  return 0;
+}
+
+int
+mu_message_set_body (mu_message_t msg, mu_body_t body, void *owner)
+{
+  if (msg == NULL )
+    return EINVAL;
+  if (msg->owner != owner)
+    return EACCES;
+  /* Make sure we destroy the old if it was owned by the mesg.  */
+  /* FIXME:  I do not know if somebody has already a ref on this ? */
+  if (msg->body)
+    mu_body_destroy (&msg->body, msg);
+  msg->body = body;
+  msg->flags |= MESSAGE_MODIFIED;
+  return 0;
+}
+
+int
+mu_message_set_stream (mu_message_t msg, mu_stream_t stream, void *owner)
+{
+  if (msg == NULL)
+    return EINVAL;
+  if (msg->owner != owner)
+    return EACCES;
+  if (msg->stream)
+    mu_stream_destroy (&msg->stream);
+  msg->stream = stream;
+  msg->flags |= MESSAGE_MODIFIED;
+  msg->flags &= ~MESSAGE_INTERNAL_STREAM;
+  return 0;
+}
+
+static int
+_message_get_stream (mu_message_t msg, mu_stream_t *pstream, int ref)
+{
+  int status;
+
+  if (msg == NULL)
+    return EINVAL;
+  if (pstream == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+
+  if (msg->stream == NULL)
+    {
+      if (msg->_get_stream)
+	{
+	  status = msg->_get_stream (msg, &msg->stream);
+	  if (status)
+	    return status;
+	}
+      else
+	{
+	  mu_header_t hdr;
+	  mu_body_t body;
+
+	  /* FIXME: Kind of a kludge: make sure the message has header
+	     and body initialized. */
+	  status = mu_message_get_header (msg, &hdr);
+	  if (status)
+	    return status;
+	  status = mu_message_get_body (msg, &body);
+	  if (status)
+	    return status;
+	  
+	  status = _message_stream_create (&msg->stream, msg, MU_STREAM_RDWR);
+	  if (status)
+	    return status;
+	  msg->flags |= MESSAGE_INTERNAL_STREAM;
+	}
+    }
+  
+  if (!ref)
+    {
+      *pstream = msg->stream;
+      return 0;
+    }
+  return mu_streamref_create (pstream, msg->stream);
+}
+
+int
+mu_message_get_stream (mu_message_t msg, mu_stream_t *pstream)
+{
+  /* FIXME: Deprecation warning */
+  return _message_get_stream (msg, pstream, 0);
+}
+
+int
+mu_message_get_streamref (mu_message_t msg, mu_stream_t *pstream)
+{
+  return _message_get_stream (msg, pstream, 1);
+}
+
+int
+mu_message_set_get_stream (mu_message_t msg,
+			   int (*_getstr) (mu_message_t, mu_stream_t *),
+			   void *owner)
+{
+  if (msg == NULL)
+    return EINVAL;
+  if (msg->owner != owner)
+    return EACCES;
+  msg->_get_stream = _getstr;
+  return 0;
+}
+
+int
+mu_message_set_lines (mu_message_t msg, int (*_lines)
+		   (mu_message_t, size_t *), void *owner)
+{
+  if (msg == NULL)
+    return EINVAL;
+  if (msg->owner != owner)
+    return EACCES;
+  msg->_lines = _lines;
+  return 0;
+}
+
+int
+mu_message_lines (mu_message_t msg, size_t *plines)
+{
+  size_t hlines, blines;
+  int ret = 0;
+
+  if (msg == NULL)
+    return EINVAL;
+  /* Overload.  */
+  if (msg->_lines)
+    return msg->_lines (msg, plines);
+  if (plines)
+    {
+      hlines = blines = 0;
+      if ( ( ret = mu_header_lines (msg->header, &hlines) ) == 0 )
+	      ret = mu_body_lines (msg->body, &blines);
+      *plines = hlines + blines;
+    }
+  return ret;
+}
+
+int
+mu_message_set_size (mu_message_t msg, int (*_size)
+		  (mu_message_t, size_t *), void *owner)
+{
+  if (msg == NULL)
+    return EINVAL;
+  if (msg->owner != owner)
+    return EACCES;
+  msg->_size = _size;
+  return 0;
+}
+
+int
+mu_message_size (mu_message_t msg, size_t *psize)
+{
+  size_t hsize, bsize;
+  int ret = 0;
+
+  if (msg == NULL)
+    return EINVAL;
+  /* Overload ? */
+  if (msg->_size)
+    return msg->_size (msg, psize);
+  if (psize)
+    {
+      mu_header_t hdr = NULL;
+      mu_body_t body = NULL;
+      
+      hsize = bsize = 0;
+      mu_message_get_header (msg, &hdr);
+      mu_message_get_body (msg, &body);
+      if ( ( ret = mu_header_size (hdr, &hsize) ) == 0 )
+	ret = mu_body_size (body, &bsize);
+      *psize = hsize + bsize;
+    }
+  return ret;
+}
+
+int
+mu_message_get_envelope (mu_message_t msg, mu_envelope_t *penvelope)
+{
+  if (msg == NULL)
+    return EINVAL;
+  if (penvelope == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+
+  if (msg->envelope == NULL)
+    {
+      mu_envelope_t envelope;
+      int status = mu_envelope_create (&envelope, msg);
+      if (status != 0)
+	return status;
+      mu_envelope_set_sender (envelope, message_envelope_sender, msg);
+      mu_envelope_set_date (envelope, message_envelope_date, msg);
+      msg->envelope = envelope;
+    }
+  *penvelope = msg->envelope;
+  return 0;
+}
+
+int
+mu_message_set_envelope (mu_message_t msg, mu_envelope_t envelope, void *owner)
+{
+  if (msg == NULL)
+    return EINVAL;
+  if (msg->owner != owner)
+    return EACCES;
+  if (msg->envelope)
+    mu_envelope_destroy (&msg->envelope, msg);
+  msg->envelope = envelope;
+  msg->flags |= MESSAGE_MODIFIED;
+  return 0;
+}
+
+int
+mu_message_get_attribute (mu_message_t msg, mu_attribute_t *pattribute)
+{
+  if (msg == NULL)
+    return EINVAL;
+  if (pattribute == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+  if (msg->attribute == NULL)
+    {
+      mu_attribute_t attribute;
+      int status = mu_attribute_create (&attribute, msg);
+      if (status != 0)
+	return status;
+      msg->attribute = attribute;
+    }
+  *pattribute = msg->attribute;
+  return 0;
+}
+
+int
+mu_message_set_attribute (mu_message_t msg, mu_attribute_t attribute, void *owner)
+{
+  if (msg == NULL)
+   return EINVAL;
+  if (msg->owner != owner)
+    return EACCES;
+  if (msg->attribute)
+    mu_attribute_destroy (&msg->attribute, owner);
+  msg->attribute = attribute;
+  msg->flags |= MESSAGE_MODIFIED;
+  return 0;
+}
+
+int
+mu_message_get_uid (mu_message_t msg, size_t *puid)
+{
+  if (msg == NULL)
+    return EINVAL;
+  if (msg->_get_uid)
+    return msg->_get_uid (msg, puid);
+  *puid = 0;
+  return 0;
+}
+
+int
+mu_message_get_uidl (mu_message_t msg, char *buffer, size_t buflen,
+		     size_t *pwriten)
+{
+  mu_header_t header = NULL;
+  size_t n = 0;
+  int status;
+
+  if (msg == NULL || buffer == NULL || buflen == 0)
+    return EINVAL;
+
+  buffer[0] = '\0';
+  /* Try the function overload if error fallback.  */
+  if (msg->_get_uidl)
+    {
+      status = msg->_get_uidl (msg, buffer, buflen, pwriten);
+      if (status == 0)
+	return status;
+    }
+
+  /* Be compatible with Qpopper ? qppoper saves the UIDL in "X-UIDL".
+     We generate a chksum and save it in the header.  */
+  mu_message_get_header (msg, &header);
+  status = mu_header_get_value_unfold (header, "X-UIDL", buffer, buflen, &n);
+  if (status != 0 || n == 0)
+    {
+      size_t uid = 0;
+      struct mu_md5_ctx md5context;
+      mu_stream_t stream = NULL;
+      char buf[1024];
+      unsigned char md5digest[16];
+      char *tmp;
+      n = 0;
+      mu_message_get_uid (msg, &uid);
+      mu_message_get_streamref (msg, &stream);
+      mu_md5_init_ctx (&md5context);
+      status = mu_stream_seek (stream, 0, MU_SEEK_SET, NULL);
+      if (status == 0)
+	{
+	  while (mu_stream_read (stream, buf, sizeof (buf), &n) == 0
+		 && n > 0)
+	    mu_md5_process_bytes (buf, n, &md5context);
+	  mu_md5_finish_ctx (&md5context, md5digest);
+	  tmp = buf;
+	  for (n = 0; n < 16; n++, tmp += 2)
+	    sprintf (tmp, "%02x", md5digest[n]);
+	  *tmp = '\0';
+	  /* POP3 rfc says that an UID should not be longer than 70.  */
+	  snprintf (buf + 32, 70, ".%lu.%lu", (unsigned long)time (NULL), 
+		    (unsigned long) uid);
+
+	  mu_header_set_value (header, "X-UIDL", buf, 1);
+	  buflen--; /* leave space for the NULL.  */
+	  strncpy (buffer, buf, buflen)[buflen] = '\0';
+	}
+      mu_stream_destroy (&stream);
+    }
+  return status;
+}
+
+int
+mu_message_get_qid (mu_message_t msg, mu_message_qid_t *pqid)
+{
+  if (msg == NULL)
+    return EINVAL;
+  if (!msg->_get_qid)
+    return ENOSYS;
+  return msg->_get_qid (msg, pqid);
+}
+    
+int
+mu_message_set_qid (mu_message_t msg,
+		    int (*_get_qid) (mu_message_t, mu_message_qid_t *),
+		    void *owner)
+{
+  if (msg == NULL)
+    return EINVAL;
+  if (msg->owner != owner)
+    return EACCES;
+  msg->_get_qid = _get_qid;
+  return 0;
+}
+
+int
+mu_message_set_uid (mu_message_t msg, int (*_get_uid) (mu_message_t, size_t *),
+		    void *owner)
+{
+  if (msg == NULL)
+    return EINVAL;
+  if (msg->owner != owner)
+    return EACCES;
+  msg->_get_uid = _get_uid;
+  return 0;
+}
+
+int
+mu_message_set_uidl (mu_message_t msg,
+		  int (* _get_uidl) (mu_message_t, char *, size_t, size_t *),
+		  void *owner)
+{
+  if (msg == NULL)
+    return EINVAL;
+  if (msg->owner != owner)
+    return EACCES;
+  msg->_get_uidl = _get_uidl;
+  return 0;
+}
+
+int
+mu_message_set_is_multipart (mu_message_t msg,
+			  int (*_is_multipart) (mu_message_t, int *),
+			  void *owner)
+{
+  if (msg == NULL)
+    return EINVAL;
+  if (msg->owner != owner)
+    return EACCES;
+  msg->_is_multipart = _is_multipart;
+  return 0;
+}
+
+int
+mu_message_is_multipart (mu_message_t msg, int *pmulti)
+{
+  if (msg && pmulti)
+    {
+      if (msg->_is_multipart)
+	return msg->_is_multipart (msg, pmulti);
+      if (msg->mime == NULL)
+	{
+	  int status = mu_mime_create (&msg->mime, msg, 0);
+	  if (status != 0)
+	    return 0;
+	}
+      *pmulti = mu_mime_is_multipart(msg->mime);
+    }
+  return 0;
+}
+
+int
+mu_message_get_num_parts (mu_message_t msg, size_t *pparts)
+{
+  if (msg == NULL || pparts == NULL)
+    return EINVAL;
+
+  if (msg->_get_num_parts)
+    return msg->_get_num_parts (msg, pparts);
+
+  if (msg->mime == NULL)
+    {
+      int status = mu_mime_create (&msg->mime, msg, 0);
+      if (status != 0)
+	return status;
+    }
+  return mu_mime_get_num_parts (msg->mime, pparts);
+}
+
+int
+mu_message_set_get_num_parts (mu_message_t msg,
+			   int (*_get_num_parts) (mu_message_t, size_t *),
+			   void *owner)
+{
+  if (msg == NULL)
+    return EINVAL;
+  if (msg->owner != owner)
+    return EACCES;
+  msg->_get_num_parts = _get_num_parts;
+  return 0;
+}
+
+int
+mu_message_get_part (mu_message_t msg, size_t part, mu_message_t *pmsg)
+{
+  if (msg == NULL || pmsg == NULL)
+    return EINVAL;
+
+  /* Overload.  */
+  if (msg->_get_part)
+    return msg->_get_part (msg, part, pmsg);
+
+  if (msg->mime == NULL)
+    {
+      int status = mu_mime_create (&msg->mime, msg, 0);
+      if (status != 0)
+	return status;
+    }
+  return mu_mime_get_part (msg->mime, part, pmsg);
+}
+
+int
+mu_message_set_get_part (mu_message_t msg, int (*_get_part)
+		      (mu_message_t, size_t, mu_message_t *),
+		      void *owner)
+{
+  if (msg == NULL)
+    return EINVAL;
+  if (msg->owner != owner)
+    return EACCES;
+  msg->_get_part = _get_part;
+  return 0;
+}
+
+int
+mu_message_get_observable (mu_message_t msg, mu_observable_t *pobservable)
+{
+  if (msg == NULL || pobservable == NULL)
+    return EINVAL;
+
+  if (msg->observable == NULL)
+    {
+      int status = mu_observable_create (&msg->observable, msg);
+      if (status != 0)
+	return status;
+    }
+  *pobservable = msg->observable;
+  return 0;
+}
+
+int
+mu_message_save_to_mailbox (mu_message_t msg, 
+                            mu_debug_t debug,
+			    const char *toname, int perms)
+{
+  int rc = 0;
+  mu_mailbox_t to = 0;
+
+  if ((rc = mu_mailbox_create_default (&to, toname)))
+    {
+      MU_DEBUG2 (debug, MU_DEBUG_ERROR,
+		 "mu_mailbox_create_default (%s) failed: %s\n", toname,
+		 mu_strerror (rc));
+      goto end;
+    }
+
+  if (debug && (rc = mu_mailbox_set_debug (to, debug)))
+	goto end;
+
+  if ((rc = mu_mailbox_open (to,
+			     MU_STREAM_WRITE | MU_STREAM_CREAT
+			     | (perms & MU_STREAM_IMASK))))
+    {
+      MU_DEBUG2 (debug, MU_DEBUG_ERROR,
+		 "mu_mailbox_open (%s) failed: %s\n", toname,
+		 mu_strerror (rc));
+      goto end;
+    }
+
+  if ((rc = mu_mailbox_append_message (to, msg)))
+    {
+      MU_DEBUG2 (debug, MU_DEBUG_ERROR,
+		 "mu_mailbox_append_message (%s) failed: %s\n", toname,
+		 mu_strerror (rc));
+      goto end;
+    }
+
+end:
+
+  if (!rc)
+    {
+      if ((rc = mu_mailbox_close (to)))
+	MU_DEBUG2 (debug, MU_DEBUG_ERROR,
+		   "mu_mailbox_close (%s) failed: %s\n", toname,
+		   mu_strerror (rc));
+    }
+  else
+    mu_mailbox_close (to);
+
+  mu_mailbox_destroy (&to);
+
+  return rc;
+}
+
diff --git a/libmailutils/msgscan.c b/libmailutils/mailbox/msgscan.c
similarity index 100%
rename from libmailutils/msgscan.c
rename to libmailutils/mailbox/msgscan.c
diff --git a/libmailutils/mailcap.c b/libmailutils/mailcap.c
deleted file mode 100644
index 8d8c700..0000000
--- a/libmailutils/mailcap.c
+++ /dev/null
@@ -1,740 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 1999, 2000, 2003, 2004, 2005, 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 <stdlib.h>
-#include <string.h>
-#include <errno.h>
-
-#include <mailutils/cctype.h>
-#include <mailutils/mailcap.h>
-#include <mailutils/stream.h>
-#include <mailutils/mutil.h>
-#include <mailutils/cstr.h>
-
-/* Definition of the structure, this should be in mailutils/sys/mailcap.h.  */
-struct _mu_mailcap_entry
-{
-  char * typefield;
-  char * viewcommand;
-  char ** fields;
-  size_t fields_count;
-};
-
-/* Definition of the structure, this should be in mailutils/sys/mailcap.h.  */
-struct _mu_mailcap
-{
-  mu_mailcap_entry_t *entries;
-  size_t entries_count;
-};
-
-
-static int mu_mailcap_parse (mu_mailcap_t mailcap, mu_stream_t stream);
-static int mu_mailcap_parse_entry (mu_mailcap_entry_t entry, char *buffer);
-static char * tokenize (char *s, char **save_ptr);
-
-int
-mu_mailcap_create (mu_mailcap_t * pmailcap, mu_stream_t stream)
-{
-  mu_mailcap_t mailcap;
-  int status = 0;
-
-  if (stream == NULL)
-    return EINVAL;
-  if (pmailcap == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-
-  mailcap = calloc (1, sizeof (*mailcap));
-  if (mailcap != NULL)
-    {
-      status = mu_mailcap_parse (mailcap, stream);
-      if (status != 0)
-	{
-	  mu_mailcap_destroy (&mailcap);
-	}
-      else
-	{
-	  *pmailcap = mailcap;
-	}
-    }
-  else
-    {
-      status = ENOMEM;
-    }
-  return status;
-}
-
-void
-mu_mailcap_destroy (mu_mailcap_t * pmailcap)
-{
-  if (pmailcap != NULL && *pmailcap != NULL)
-    {
-      int i;
-      mu_mailcap_t mailcap = *pmailcap;
-
-      for (i = 0; i < mailcap->entries_count; i++)
-	{
-	  int j;
-	  mu_mailcap_entry_t entry = mailcap->entries[i];
-	  free (entry->typefield);
-	  free (entry->viewcommand);
-	  for (j = 0; j < entry->fields_count; j++)
-	    {
-	      free (entry->fields[j]);
-	    }
-	}
-    }
-}
-
-int
-mu_mailcap_entries_count (mu_mailcap_t mailcap, size_t *pcount)
-{
-  int status = 0;
-
-  if (mailcap == NULL)
-    status = EINVAL;
-  if (pcount != NULL)
-    {
-      *pcount = mailcap->entries_count;
-    }
-  return status;
-}
-
-int
-mu_mailcap_get_entry (mu_mailcap_t mailcap, size_t no,
-		      mu_mailcap_entry_t *pentry)
-{
-  int status = 0;
-  if (mailcap == NULL)
-    status = EINVAL;
-  else if (pentry == NULL)
-    status = MU_ERR_OUT_PTR_NULL;
-  else if (no == 0 || no > mailcap->entries_count)
-    {
-      status = MU_ERR_NOENT;
-    }
-  else
-    {
-      *pentry = mailcap->entries[no - 1];
-    }
-  return status;
-}
-
-int
-mu_mailcap_entry_get_typefield (mu_mailcap_entry_t entry, char *buffer,
-				size_t buflen, size_t *pn)
-{
-  int status = 0;
-  int len = 0;
-
-  if (entry == NULL)
-    {
-      status = EINVAL;
-    }
-  else
-    {
-      len = strlen(entry->typefield);
-      if (buffer != NULL && buflen > 0)
-	{
-	  buflen--;
-	  len = (len < buflen) ? len : buflen;
-	  memcpy (buffer, entry->typefield, len);
-	  buffer[len] = '\0';
-	}
-    }
-  if (pn)
-    {
-      *pn = len;
-    }
-  return status;
-}
-
-int
-mu_mailcap_entry_get_viewcommand (mu_mailcap_entry_t entry, char *buffer,
-				  size_t buflen, size_t *pn)
-{
-  int status = 0;
-  int len = 0;
-
-  if (entry == NULL)
-    {
-      status = EINVAL;
-    }
-  else
-    {
-      len = strlen(entry->viewcommand);
-      if (buffer != NULL && buflen > 0)
-	{
-	  buflen--;
-	  len = (len < buflen) ? len : buflen;
-	  memcpy (buffer, entry->viewcommand, len);
-	  buffer[len] = '\0';
-	}
-    }
-  if (pn)
-    {
-      *pn = len;
-    }
-  return status;
-}
-
-int
-mu_mailcap_entry_fields_count (mu_mailcap_entry_t entry, size_t *pcount)
-{
-  int status = 0;
-  if (entry == NULL)
-    {
-      status = EINVAL;
-    }
-  if (pcount != NULL)
-    {
-      *pcount = entry->fields_count;
-    }
-  return status;
-}
-
-int
-mu_mailcap_entry_get_field (mu_mailcap_entry_t entry, size_t no,
-			    char *buffer, size_t buflen, size_t *pn)
-{
-  int status = 0;
-  int len = 0;
-
-  if (entry == NULL)
-    {
-      status = EINVAL;
-    }
-  else if ( no == 0 || no > entry->fields_count)
-    {
-      status = MU_ERR_NOENT;
-    }
-  else
-    {
-      len = strlen(entry->fields[no - 1]);
-      if (buffer != NULL && buflen > 0)
-	{
-	  buflen--;
-	  len = (len < buflen) ? len : buflen;
-	  memcpy (buffer, entry->fields[no - 1], len);
-	  buffer[len] = '\0';
-	}
-    }
-  if (pn)
-    {
-      *pn = len;
-    }
-  return status;
-}
-
-int
-mu_mailcap_entry_get_compose (mu_mailcap_entry_t entry,
-			      char *buffer, size_t buflen, size_t *pn)
-{
-  return mu_mailcap_entry_get_value (entry, "compose", buffer, buflen, pn);
-}
-
-int
-mu_mailcap_entry_get_composetyped (mu_mailcap_entry_t entry, char *buffer,
-				   size_t buflen, size_t *pn)
-{
-  return mu_mailcap_entry_get_value (entry, "composetyped", buffer, buflen, pn);
-}
-
-int
-mu_mailcap_entry_get_edit (mu_mailcap_entry_t entry, char *buffer,
-			   size_t buflen, size_t *pn)
-{
-  return mu_mailcap_entry_get_value (entry, "edit", buffer, buflen, pn);
-}
-
-int
-mu_mailcap_entry_get_test (mu_mailcap_entry_t entry,
-			   char *buffer, size_t buflen, size_t *pn)
-{
-  return mu_mailcap_entry_get_value (entry, "test", buffer, buflen, pn);
-}
-
-int
-mu_mailcap_entry_get_x11bitmap (mu_mailcap_entry_t entry,
-				char *buffer, size_t buflen, size_t *pn)
-{
-  return mu_mailcap_entry_get_value (entry, "x11-bitmap", buffer, buflen, pn);
-}
-
-int
-mu_mailcap_entry_get_description (mu_mailcap_entry_t entry,
-				  char *buffer, size_t buflen, size_t *pn)
-{
-  return mu_mailcap_entry_get_value (entry, "description", buffer, buflen, pn);
-}
-
-int
-mu_mailcap_entry_needsterminal (mu_mailcap_entry_t entry, int *on)
-{
-  int status = 0;
-  int found = 0;
-  if (entry == NULL)
-    {
-      status = EINVAL;
-    }
-  else
-    {
-      int i;
-      for (i = 0; i < entry->fields_count; i++)
-	{
-	  int n = mu_c_strcasecmp (entry->fields[i], "needsterminal");
-	  if (n == 0)
-	    {
-	      found = 1;
-	      break;
-	    }
-	}
-    }
-  if (on)
-    *on = found;
-  return status;
-}
-
-int
-mu_mailcap_entry_copiousoutput (mu_mailcap_entry_t entry, int *on)
-{
-  int status = 0;
-  int found = 0;
-  if (entry == NULL)
-    {
-      status = EINVAL;
-    }
-  else
-    {
-      int i;
-      for (i = 0; i < entry->fields_count; i++)
-	{
-	  int n = mu_c_strcasecmp (entry->fields[i], "copiousoutput");
-	  if (n == 0)
-	    {
-	      found = 1;
-	      break;
-	    }
-	}
-    }
-  if (on)
-    *on = found;
-  return status;
-}
-
-int
-mu_mailcap_entry_get_value (mu_mailcap_entry_t entry, const char *key,
-			    char *buffer, size_t buflen, size_t *pn)
-{
-  int len = 0;
-  int status = ENOENT;
-
-  if (!entry)
-    status = EINVAL;
-  else
-    {
-      int i;
-      int key_len = strlen (key);
-      for (i = 0; i < entry->fields_count; i++)
-	{
-	  int n = mu_c_strncasecmp (entry->fields[i], key, key_len);
-	  if (n == 0)
-	    {
-	      int field_len = strlen(entry->fields[i]);
-	      status = 0;
-	      if (field_len > key_len)
-		{
-		  int c = entry->fields[i][key_len];
-		  if (mu_isspace (c) || c == '=')
-		    {
-		      char *value = strchr (entry->fields[i], '=');
-		      if (value != NULL)
-			{
-			  value++; /* Pass the equal.  */
-			  /* Remove leading space.  */
-			  for (; mu_isspace (*value); value++)
-			    ;
-			  len = strlen (value);
-			  /* Strip surrounding double quotes */
-			  if (len > 1 && value[0] == '"' && value[len - 1] == '"')
-			    {
-			      value++;
-			      len -= 2;
-			    }
-			  if (buffer != NULL && buflen > 0)
-			    {
-			      buflen--;
-			      len = (len < buflen) ? len : buflen;
-			      memcpy (buffer, value, len);
-			      buffer[len] = '\0';
-			    }
-			  break;
-			}
-		    }
-		}
-	    }
-	}
-    }
-  if (pn)
-    *pn = len;
-  return status;
-}
-
-/*
- * break the line on ';'.  Same as strtok() but
- * check for escaped "\;"
- */
-static char *
-tokenize (char *s, char **save_ptr)
-{
-  int c;
-  char *token;
-
-  if (s == NULL)
-    {
-      s = *save_ptr;
-    }
-
-  if (*s == '\0')
-    {
-      *save_ptr = s;
-      return NULL;
-    }
-
-  for (token = s, c = 0; *s; s++)
-    {
-      if (*s == ';' && c != '\\')
-	{
-	  break;
-	}
-      c = *s;
-    }
-
-  if (*s == '\0')
-    {
-      *save_ptr = s;
-    }
-  else
-    {
-      *s = '\0';
-      *save_ptr = s + 1;
-    }
-  return token;
-}
-
-/**
- * parse the mailcap line, fields are separated by ';'
- */
-static int
-mu_mailcap_parse_entry (mu_mailcap_entry_t entry, char *buffer)
-{
-  char *token = NULL;
-  char *s = NULL;
-  int i;
-  for (i = 0, token = tokenize (buffer, &s);
-       token != NULL; token = tokenize (NULL, &s), i++)
-    {
-      switch (i)
-	{
-	  /* The first entry in a mailcap line is the typefield.  */
-	case 0:
-	  entry->typefield = strdup (mu_str_stripws (token));
-	  break;
-
-	  /* The second entry in a mailcap line is the view-command.  */
-	case 1:
-	  entry->viewcommand = strdup (mu_str_stripws (token));
-	  break;
-
-	  /* The rest are the optional fields.  */
-	default:
-	  {
-	    char **fields = realloc (entry->fields,
-				     (entry->fields_count + 1) *
-				     sizeof (*fields));
-	    if (fields != NULL)
-	      {
-		entry->fields = fields;
-		entry->fields[entry->fields_count] =
-		  strdup (mu_str_stripws (token));
-		entry->fields_count++;
-	      }
-	  }
-	}
-    }
-  /* Make sure typefield and viewcommand are not null.  */
-  if (entry->typefield == NULL)
-    {
-      entry->typefield = strdup ("");
-    }
-  if (entry->viewcommand == NULL)
-    {
-      entry->viewcommand = strdup ("");
-    }
-  return 0;
-}
-
-/*
- * parse a mailcap file or stream,
- * - ignore empty line.
- * - ignore line starting with '#'
- * - multiline is done with the '\' as continuation
- * example:
-# comment
-application/pgp; gpg < %s | metamail; needsterminal; \
- test=test %{encapsulation}=entity ; copiousoutput
- */
-static int
-mu_mailcap_parse (mu_mailcap_t mailcap, mu_stream_t stream)
-{
-  int status;
-  size_t n;
-  char *previous;
-  char *buffer;
-  size_t buflen = 512;
-
-  buffer = malloc (buflen);
-  if (buffer == NULL)
-    {
-      return ENOMEM;
-    }
-
-  /*
-   * We are doing this a little more complex then expected, because we do not
-   * want to seek() back in the stream:
-   * - we have to take care of continuation line i.e. line ending with '\'
-   * - we have to take to account that the line may be bigger then the buffer
-   *   and reallocate
-   * - check the return of malloc/realloc
-   * The old continuation line is saved in the "previous" pointer and
-   * prepended to the buffer.
-   */
-  status = mu_stream_seek (stream, 0, MU_SEEK_SET, NULL);
-  if (status)
-    return status;
-  previous = NULL;
-  while ((status = mu_stream_readline (stream, buffer, buflen, &n)) == 0
-	 && n > 0)
-    {
-      int len;
-
-      /* If there is no trailing newline, that means the buffer was too small,
-       * make room for the buffer and continue reading  */
-      if (buffer[n - 1] != '\n')
-        {
-	  char *b = realloc (buffer, buflen * sizeof (*buffer));
-          buflen *= 2;
-          if (b == NULL)
-	    {
-	      status = ENOMEM;
-	      break;
-	    }
-	  buffer = b;
-	  /*
-	   * Fake this as a continuation line, for simplicity.
-	   */
-	  strcat (buffer, "\\");
-        }
-      else
-	{
-	  /* Nuke the trailing newline.  */
-	  buffer[n - 1] = '\0';
-	}
-
-      /* recalculate the len.  */
-      len = strlen (buffer);
-
-      /* Ending with a '\' means continuation line.  */
-      if (len && buffer[len - 1] == '\\')
-	{
-	  buffer[len - 1] = '\0';
-	  /*
-	   * Check for any previous line:
-	   * - if yes append the buffer to the previous line.
-	   * - if not set the buffer as the previous line and continue.
-	   */
-	  if (previous == NULL)
-	    {
-	      previous = strdup (buffer);
-	      if (previous == NULL)
-		{
-		  status = ENOMEM;
-		  break;
-		}
-	    }
-	  else
-	    {
-	      char *b = realloc (previous, strlen (previous) + len + 1);
-	      if (b == NULL)
-		{
-		  status = ENOMEM;
-		  break;
-		}
-	      previous = b;
-	      strcat(previous, buffer);
-	    }
-	}
-      else
-	{
-	  /* Did we have a previous incomplete line?
-	   * if yes make one line from the previous and the buffer.
-	   */
-	  if (previous != NULL)
-	    {
-	      int prev_len = strlen (previous);
-	      int total =  prev_len + len + 1;
-	      if (total > buflen)
-		{
-		  char *b = realloc (buffer, total * sizeof (*buffer));
-		  if (b == NULL)
-		    {
-		      status = ENOMEM;
-		      break;
-		    }
-		  buffer = b;
-		  buflen = total;
-		}
-	      memmove (buffer + prev_len, buffer, len + 1);
-	      memcpy (buffer, previous, prev_len);
-	      free (previous);
-	      previous = NULL;
-	    }
-	}
-
-      /* Parse the well-form mailcap line entry.  */
-      if (previous == NULL) {
-	/* Nuke the trailing/prepend spaces.  */
-	char *line = mu_str_stripws (buffer);
-	/* Ignore comments or empty lines  */
-	if (*line != '#' && *line != '\0')
-	  {
-	    mu_mailcap_entry_t *entries;
-	    entries = realloc (mailcap->entries,
-			       (mailcap->entries_count + 1) *
-			       sizeof (*entries));
-	    if (entries != NULL)
-	      {
-		mailcap->entries = entries;
-		mailcap->entries[mailcap->entries_count] = calloc (1,
-							   sizeof(**entries));
-		if (mailcap->entries[mailcap->entries_count] != NULL)
-		  {
-		    mu_mailcap_parse_entry (mailcap->entries[mailcap->entries_count], line);
-		  }
-		mailcap->entries_count++;
-	      }
-	    else
-	      {
-		status = ENOMEM;
-		break;
-	      }
-	  }
-      }
-    }
-
-  if (buffer != NULL)
-    {
-      free (buffer);
-    }
-  if (previous != NULL)
-    {
-      free (previous);
-    }
-  return status;
-}
-
-#ifdef STANDALONE_TEST
-int main()
-{
-  mu_stream_t stream = NULL;
-  int status = 0;
-
-  status = mu_file_stream_create (&stream, "/home/alain/mailcap",
-				  MU_STREAM_READ);
-  if (status == 0)
-    {
-      mu_mailcap_t mailcap;
-      status = mu_mailcap_create (&mailcap, stream);
-      if (status == 0)
-	{
-	  int i, n;
-	  size_t count = 0;
-	  char buffer[256];
-	  
-	  mu_mailcap_entries_count (mailcap, &count);
-	  for (i = 1; i <= count; i++)
-	    {
-	      int j;
-	      mu_mailcap_entry_t entry = NULL;
-	      int fields_count = 0;
-	      
-	      printf ("entry[%d]\n", i);
-#if 1
-	      
-	      mu_mailcap_get_entry (mailcap, i, &entry);
-	      /* Print typefield.  */
-	      mu_mailcap_entry_get_typefield (entry, buffer,
-					      sizeof (buffer), NULL);
-	      printf ("\ttypefield: %s\n", buffer);
-	      
-	      /* Print view-command.  */
-	      mu_mailcap_entry_get_viewcommand (entry, buffer,
-						sizeof (buffer), NULL);
-	      printf ("\tview-command: %s\n", buffer);
-	      
-	      /* Print fields.  */
-	      mu_mailcap_entry_fields_count (entry, &fields_count);
-	      for (j = 1; j <= fields_count; j++)
-		{
-		  mu_mailcap_entry_get_field (entry, j, buffer,
-					      sizeof (buffer), NULL);
-		  printf("\tfields[%d]: %s\n", j, buffer);
-		}
-	      n = 0;
-	      mu_mailcap_entry_get_compose (entry, buffer,
-					    sizeof (buffer), &n);
-	      if (n > 0)
-		{
-		  printf("\tcompose[%s]\n", buffer);
-		}
-	      printf("\n");
-	    }
-#else
-	  for (i = 0; i < mailcap->entries_count; i++)
-	    {
-	      int j;
-	      mu_mailcap_entry_t entry = mailcap->entries[i];
-	      printf("[%s];[%s]", entry->typefield, entry->viewcommand);
-	      for (j = 0; j < entry->fields_count; j++)
-		{
-		  printf(";[%s]", entry->fields[j]);
-		}
-	      printf("\n");
-	    }
-#endif
-	  mu_mailcap_destroy (&mailcap);
-	}
-    }
-  return 0;
-}
-
-#endif
diff --git a/libmailutils/mailer.c b/libmailutils/mailer.c
deleted file mode 100644
index 8faf778..0000000
--- a/libmailutils/mailer.c
+++ /dev/null
@@ -1,698 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 1999, 2000, 2001, 2004, 2005, 2006, 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 <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <stdarg.h>
-#include <unistd.h>
-#include <errno.h>
-
-#include <sys/time.h>
-
-#include <mailutils/cstr.h>
-#include <mailutils/address.h>
-#include <mailutils/debug.h>
-#include <mailutils/errno.h>
-#include <mailutils/iterator.h>
-#include <mailutils/list.h>
-#include <mailutils/observer.h>
-#include <mailutils/property.h>
-#include <mailutils/registrar.h>
-#include <mailutils/stream.h>
-#include <mailutils/url.h>
-#include <mailutils/header.h>
-#include <mailutils/body.h>
-#include <mailutils/mailbox.h>
-#include <mailutils/message.h>
-#include <mailutils/argcv.h>
-#include <mailutils/mutil.h>
-#include <mailutils/mime.h>
-#include <mailutils/io.h>
-
-#include <mailutils/sys/mailer.h>
-
-static char *mailer_url_default;
-
-/* FIXME: I'd like to check that the URL is valid, but that requires that the
-   mailers already be registered! */
-int
-mu_mailer_set_url_default (const char *url)
-{
-  char *n = NULL;
-
-  if (!url)
-    return EINVAL;
-
-  if ((n = strdup (url)) == NULL)
-    return ENOMEM;
-
-  if (mailer_url_default)
-    free (mailer_url_default);
-
-  mailer_url_default = n;
-
-  return 0;
-}
-
-int
-mu_mailer_get_url_default (const char **url)
-{
-  if (!url)
-    return EINVAL;
-
-  if (mailer_url_default)
-    *url = mailer_url_default;
-  else
-    *url = MAILER_URL_DEFAULT;
-
-  return 0;
-}
-
-static void
-set_default_debug (mu_mailer_t mailer)
-{
-  mu_log_level_t level = mu_global_debug_level ("mailer");
-  if (level)
-    {
-      mu_debug_t debug;
-      if (mu_mailer_get_debug (mailer, &debug))
-	return;
-      mu_debug_set_level (debug, level);
-    }
-}
-
-int
-mu_mailer_create_from_url (mu_mailer_t *pmailer, mu_url_t url)
-{
-  mu_record_t record;
-
-  if (mu_registrar_lookup_url (url, MU_FOLDER_ATTRIBUTE_FILE, &record,
-			       NULL) == 0)
-    {
-      int (*m_init) (mu_mailer_t) = NULL;
-
-      mu_record_get_mailer (record, &m_init);
-      if (m_init)
-        {
-	  int status;
-	  mu_mailer_t mailer;
-	  int (*u_init) (mu_url_t) = NULL;
-	  
-	  /* Allocate memory for mailer.  */
-	  mailer = calloc (1, sizeof (*mailer));
-	  if (mailer == NULL)
-	    return ENOMEM;
-
-	  status = mu_monitor_create (&mailer->monitor, 0, mailer);
-	  if (status)
-	    {
-	      mu_mailer_destroy (&mailer);
-	      return status;
-	    }
-
-	  status = m_init (mailer);
-	  if (status)
-	    {
-	      mu_mailer_destroy (&mailer);
-	      return status;
-	    }
-
-	  mu_record_get_url (record, &u_init);
-	  if (u_init && (status = u_init (url)) != 0)
-	    {
-	      mu_mailer_destroy (&mailer);
-	      return status;
-	    }
-	  
-	  mailer->url = url;
-	  *pmailer = mailer;
-
-	  set_default_debug (mailer);
-	  return status;
-	}
-    }
-  
-    return MU_ERR_MAILER_BAD_URL;
-}
-
-int
-mu_mailer_create (mu_mailer_t * pmailer, const char *name)
-{
-  int status;
-  mu_url_t url;
-
-  if (name == NULL)
-    mu_mailer_get_url_default (&name);
-
-  status = mu_url_create (&url, name);
-  if (status)
-    return status;
-  status = mu_url_parse (url);
-  if (status == 0)
-    status = mu_mailer_create_from_url (pmailer, url);
-  if (status)
-    mu_url_destroy (&url);
-  return status;
-}
-
-void
-mu_mailer_destroy (mu_mailer_t * pmailer)
-{
-  if (pmailer && *pmailer)
-    {
-      mu_mailer_t mailer = *pmailer;
-      mu_monitor_t monitor = mailer->monitor;
-
-      if (mailer->observable)
-	{
-	  mu_observable_notify (mailer->observable, MU_EVT_MAILER_DESTROY,
-				mailer);
-	  mu_observable_destroy (&mailer->observable, mailer);
-	}
-
-      /* Call the object destructor.  */
-      if (mailer->_destroy)
-	mailer->_destroy (mailer);
-
-      mu_monitor_wrlock (monitor);
-
-      if (mailer->stream)
-	{
-	  /* FIXME: Should be the client responsability to close this?  */
-	  /* mu_stream_close (mailer->stream); */
-	  mu_stream_destroy (&mailer->stream);
-	}
-
-      if (mailer->url)
-	mu_url_destroy (&(mailer->url));
-
-      if (mailer->debug)
-	mu_debug_destroy (&(mailer->debug), mailer);
-
-      if (mailer->property)
-	mu_property_destroy (&(mailer->property), mailer);
-
-      free (mailer);
-      *pmailer = NULL;
-      mu_monitor_unlock (monitor);
-      mu_monitor_destroy (&monitor, mailer);
-    }
-}
-
-
-/* -------------- stub functions ------------------- */
-
-int
-mu_mailer_open (mu_mailer_t mailer, int flag)
-{
-  if (mailer == NULL || mailer->_open == NULL)
-    return ENOSYS;
-  return mailer->_open (mailer, flag);
-}
-
-int
-mu_mailer_close (mu_mailer_t mailer)
-{
-  if (mailer == NULL || mailer->_close == NULL)
-    return ENOSYS;
-  return mailer->_close (mailer);
-}
-
-
-int
-mu_mailer_check_from (mu_address_t from)
-{
-  size_t n = 0;
-
-  if (!from)
-    return EINVAL;
-
-  if (mu_address_get_count (from, &n) || n != 1)
-    return MU_ERR_MAILER_BAD_FROM;
-
-  if (mu_address_get_email_count (from, &n) || n == 0)
-    return MU_ERR_MAILER_BAD_FROM;
-
-  return 0;
-}
-
-int
-mu_mailer_check_to (mu_address_t to)
-{
-  size_t count = 0;
-  size_t emails = 0;
-  size_t groups = 0;
-
-  if (!to)
-    return EINVAL;
-
-  if (mu_address_get_count (to, &count))
-    return MU_ERR_MAILER_BAD_TO;
-
-  if (mu_address_get_email_count (to, &emails))
-    return MU_ERR_MAILER_BAD_TO;
-
-  if (emails == 0)
-    return MU_ERR_MAILER_NO_RCPT_TO;
-
-  if (mu_address_get_group_count (to, &groups))
-    return MU_ERR_MAILER_BAD_TO;
-
-  if (count - emails - groups != 0)
-    /* then not everything is a group or an email address */
-    return MU_ERR_MAILER_BAD_TO;
-
-  return 0;
-}
-
-static void
-save_fcc (mu_message_t msg)
-{
-  mu_header_t hdr;
-  size_t count = 0, i;
-  char buf[512], *fcc;
-  
-  if (mu_message_get_header (msg, &hdr))
-    return;
-
-  if (mu_header_get_value (hdr, MU_HEADER_FCC, NULL, 0, NULL))
-    return;
-  
-  mu_header_get_field_count (hdr, &count);
-  for (i = 1; i <= count; i++)
-    {
-      mu_mailbox_t mbox;
-      
-      mu_header_get_field_name (hdr, i, buf, sizeof buf, NULL);
-      if (mu_c_strcasecmp (buf, MU_HEADER_FCC) == 0
-	  && mu_header_aget_field_value (hdr, i, &fcc) == 0)
-	{
-	  int i, argc;
-	  char **argv;
-	  
-	  mu_argcv_get (fcc, ",", NULL, &argc, &argv);
-	  for (i = 0; i < argc; i += 2)
-	    {
-	      if (mu_mailbox_create_default (&mbox, argv[i]))
-		continue; /*FIXME: error message?? */
-	      if (mu_mailbox_open (mbox,
-				   MU_STREAM_RDWR | MU_STREAM_CREAT
-				   | MU_STREAM_APPEND) == 0)
-		{
-		  mu_mailbox_append_message (mbox, msg);
-		  mu_mailbox_flush (mbox, 0);
-		}
-	      mu_mailbox_close (mbox);
-	      mu_mailbox_destroy (&mbox);
-	    }
-	  mu_argcv_free (argc, argv);
-	  free (fcc);
-	}
-    }
-}
-
-static int
-_set_from (mu_address_t *pfrom, mu_message_t msg, mu_address_t from,
-	   mu_mailer_t mailer)
-{
-  int status = 0;
-  char *mail_from;
-  mu_header_t header = NULL;
-
-  *pfrom = NULL;
-  
-  /* Get MAIL_FROM from FROM, the message, or the environment. */
-  if (!from)
-    {
-      const char *type;
-      
-      if ((status = mu_message_get_header (msg, &header)) != 0)
-	return status;
-      
-      status = mu_header_aget_value (header, MU_HEADER_FROM, &mail_from);
-      
-      switch (status)
-	{
-	default:
-	  return status;
-
-	  /* Use the From: header. */
-	case 0:
-	  MU_DEBUG1 (mailer->debug, MU_DEBUG_TRACE,
-		     "mu_mailer_send_message(): using From: %s\n",
-		     mail_from);
-	    
-	  status = mu_address_create (pfrom, mail_from);
-	  free (mail_from);
-	  break;
-
-	case MU_ERR_NOENT:
-	  if (mu_property_sget_value (mailer->property, "TYPE", &type) == 0
-	      && strcmp (type, "SENDMAIL") == 0)
-	    return 0;
-	  
-	  /* Use the environment. */
-	  mail_from = mu_get_user_email (NULL);
-
-	  if (mail_from)
-            MU_DEBUG1 (mailer->debug, MU_DEBUG_TRACE,
-		       "mu_mailer_send_message(): using user's address: %s\n",
-		       mail_from);
-	  else
-            MU_DEBUG (mailer->debug, MU_DEBUG_ERROR,
-		      "mu_mailer_send_message(): no user's address, failing\n");
-
-	  if (!mail_from)
-	    return errno;
-
-	  status = mu_address_create (pfrom, mail_from);
-	  /* FIXME: should we add the From: header? */
-	  break;
-	}
-    }
-
-  return status;
-}
-
-static int
-create_part (mu_mime_t mime, mu_stream_t istr, 
-	     size_t fragsize, size_t n, size_t nparts, char *msgid)
-{
-  int status = 0;
-  mu_message_t newmsg;
-  mu_header_t newhdr;
-  mu_body_t body;
-  mu_stream_t ostr;
-  char buffer[512], *str;
-  size_t slen;
-  
-  mu_message_create (&newmsg, NULL);
-  mu_message_get_header (newmsg, &newhdr); 
-
-  str = NULL;
-  slen = 0;
-  mu_asnprintf (&str, &slen,
-		"message/partial; id=\"%s\"; number=%lu; total=%lu",
-		msgid, (unsigned long)n, (unsigned long)nparts);
-  mu_header_append (newhdr, MU_HEADER_CONTENT_TYPE, str);
-  mu_asnprintf (&str, &slen, "part %lu of %lu",
-		(unsigned long)n, (unsigned long)nparts);
-  mu_header_append (newhdr, MU_HEADER_CONTENT_DESCRIPTION, str);
-  free (str);
-  
-  mu_message_get_body (newmsg, &body);
-  mu_body_get_streamref (body, &ostr);
-
-  status = mu_stream_seek (ostr, 0, SEEK_SET, NULL);
-  if (status == 0)
-    while (fragsize)
-      {
-	size_t rds = fragsize;
-	if (rds > sizeof buffer)
-	  rds = sizeof buffer;
-	
-	status = mu_stream_read (istr, buffer, rds, &rds);
-	if (status || rds == 0)
-	  break;
-	status = mu_stream_write (ostr, buffer, rds, NULL);
-	if (status)
-	  break;
-	fragsize -= rds;
-      }
-  mu_stream_destroy (&ostr);
-  
-  if (status == 0)
-    {
-      mu_mime_add_part (mime, newmsg);
-      mu_message_unref (newmsg);
-    }
-  return status;
-}
-
-static void
-merge_headers (mu_message_t newmsg, mu_header_t hdr)
-{
-  size_t i, count;
-  mu_header_t newhdr;
-  
-  mu_message_get_header (newmsg, &newhdr);
-  mu_header_get_field_count (hdr, &count);
-  for (i = 1; i <= count; i++)
-    {
-      const char *fn, *fv;
-
-      mu_header_sget_field_name (hdr, i, &fn);
-      mu_header_sget_field_value (hdr, i, &fv);
-      if (mu_c_strcasecmp (fn, MU_HEADER_MESSAGE_ID) == 0)
-	continue;
-      else if (mu_c_strcasecmp (fn, MU_HEADER_MIME_VERSION) == 0)
-	mu_header_append (newhdr, "X-Orig-" MU_HEADER_MIME_VERSION,
-			  fv);
-      else if (mu_c_strcasecmp (fn, MU_HEADER_CONTENT_TYPE) == 0)
-	mu_header_append (newhdr, "X-Orig-" MU_HEADER_CONTENT_TYPE,
-			  fv);
-      else if (mu_c_strcasecmp (fn, MU_HEADER_CONTENT_DESCRIPTION) == 0)
-	mu_header_append (newhdr, "X-Orig-" MU_HEADER_CONTENT_DESCRIPTION,
-			  fv);
-      else
-	mu_header_append (newhdr, fn, fv);
-    }
-}
-  
-
-int
-send_fragments (mu_mailer_t mailer,
-		mu_header_t hdr,
-		mu_stream_t str,
-		size_t nparts, size_t fragsize,
-		struct timeval *delay,
-		mu_address_t from, mu_address_t to)
-{
-  int status = 0;
-  size_t i;
-  char *msgid = NULL;
-  
-  if (mu_header_aget_value (hdr, MU_HEADER_MESSAGE_ID, &msgid))
-    mu_rfc2822_msg_id (0, &msgid);
-  
-  for (i = 1; i <= nparts; i++)
-    {
-      mu_message_t newmsg;
-      mu_mime_t mime;
-		  
-      mu_mime_create (&mime, NULL, 0);
-      status = create_part (mime, str, fragsize, i, nparts, msgid);
-      if (status)
-	break;
-
-      mu_mime_get_message (mime, &newmsg);
-      merge_headers (newmsg, hdr);
-      
-      status = mailer->_send_message (mailer, newmsg, from, to);
-      mu_mime_destroy (&mime);
-      if (status)
-	break;
-      if (delay)
-	{
-	  struct timeval t = *delay;
-	  select (0, NULL, NULL, NULL, &t);
-	}
-    }
-  free (msgid);
-  return status;
-}
-
-int
-mu_mailer_send_fragments (mu_mailer_t mailer,
-			  mu_message_t msg,
-			  size_t fragsize, struct timeval *delay,
-			  mu_address_t from, mu_address_t to)
-{
-  int status;
-  mu_address_t sender_addr = NULL;
-  
-  if (mailer == NULL)
-    return EINVAL;
-  if (mailer->_send_message == NULL)
-    return ENOSYS;
-
-  status = _set_from (&sender_addr, msg, from, mailer);
-  if (status)
-    return status;
-  if (sender_addr)
-    from = sender_addr;
-  
-  if ((!from || (status = mu_mailer_check_from (from)) == 0)
-      && (!to || (status = mu_mailer_check_to (to)) == 0))
-    {
-      save_fcc (msg);
-      if (fragsize == 0)
-	status = mailer->_send_message (mailer, msg, from, to);
-      else
-	{
-	  mu_header_t hdr;
-	  mu_body_t body;
-	  size_t bsize;
-	  size_t nparts;
-	  
-	  /* Estimate the number of messages to be sent. */
-	  mu_message_get_header (msg, &hdr);
-
-	  mu_message_get_body (msg, &body);
-	  mu_body_size (body, &bsize);
-
-	  nparts = bsize + fragsize - 1;
-	  if (nparts < bsize) /* overflow */
-	    return EINVAL;
-	  nparts /= fragsize;
-
-	  if (nparts == 1)
-	    status = mailer->_send_message (mailer, msg, from, to);
-	  else
-	    {
-	      mu_stream_t str;
-	      status = mu_body_get_streamref (body, &str);
-	      if (status)
-		{
-		  status = send_fragments (mailer, hdr, str, nparts,
-					   fragsize, delay, from, to);
-		  mu_stream_destroy (&str);
-		}
-	    }
-	}
-    }
-  mu_address_destroy (&sender_addr);
-  return status;
-}
-
-int
-mu_mailer_send_message (mu_mailer_t mailer, mu_message_t msg,
-			mu_address_t from, mu_address_t to)
-{
-  return mu_mailer_send_fragments (mailer, msg, 0, NULL, from, to);
-}
-
-int
-mu_mailer_set_stream (mu_mailer_t mailer, mu_stream_t stream)
-{
-  if (mailer == NULL)
-    return EINVAL;
-  mailer->stream = stream;
-  return 0;
-}
-
-int
-mu_mailer_get_stream (mu_mailer_t mailer, mu_stream_t * pstream)
-{
-  /* FIXME: Deprecation warning */
-  if (mailer == NULL)
-    return EINVAL;
-  if (pstream == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-  *pstream = mailer->stream;
-  return 0;
-}
-
-int
-mu_mailer_get_streamref (mu_mailer_t mailer, mu_stream_t * pstream)
-{
-  if (mailer == NULL)
-    return EINVAL;
-  if (pstream == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-  return mu_streamref_create (pstream, mailer->stream);
-}
-
-int
-mu_mailer_get_observable (mu_mailer_t mailer, mu_observable_t * pobservable)
-{
-  /* FIXME: I should check for invalid types */
-  if (mailer == NULL)
-    return EINVAL;
-  if (pobservable == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-  if (mailer->observable == NULL)
-    {
-      int status = mu_observable_create (&(mailer->observable), mailer);
-      if (status != 0)
-	return status;
-    }
-  *pobservable = mailer->observable;
-  return 0;
-}
-
-int
-mu_mailer_get_property (mu_mailer_t mailer, mu_property_t * pproperty)
-{
-  if (mailer == NULL)
-    return EINVAL;
-  if (pproperty == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-  if (mailer->property == NULL)
-    {
-      int status = mu_property_create (&(mailer->property), mailer);
-      if (status != 0)
-	return status;
-    }
-  *pproperty = mailer->property;
-  return 0;
-}
-
-int
-mu_mailer_set_debug (mu_mailer_t mailer, mu_debug_t debug)
-{
-  if (mailer == NULL)
-    return EINVAL;
-  mu_debug_destroy (&(mailer->debug), mailer);
-  mailer->debug = debug;
-  return 0;
-}
-
-int
-mu_mailer_get_debug (mu_mailer_t mailer, mu_debug_t * pdebug)
-{
-  if (mailer == NULL)
-    return EINVAL;
-  if (pdebug == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-  if (mailer->debug == NULL)
-    {
-      int status = mu_debug_create (&(mailer->debug), mailer);
-      if (status != 0)
-	return status;
-    }
-  *pdebug = mailer->debug;
-  return 0;
-}
-
-int
-mu_mailer_get_url (mu_mailer_t mailer, mu_url_t * purl)
-{
-  if (!mailer)
-    return EINVAL;
-  if (!purl)
-    return MU_ERR_OUT_PTR_NULL;
-  *purl = mailer->url;
-  return 0;
-}
diff --git a/libmailutils/mailer/Makefile.am b/libmailutils/mailer/Makefile.am
new file mode 100644
index 0000000..1d5b242
--- /dev/null
+++ b/libmailutils/mailer/Makefile.am
@@ -0,0 +1,24 @@
+# GNU Mailutils -- a suite of utilities for electronic mail
+# Copyright (C) 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, see
+# <http://www.gnu.org/licenses/>.
+
+noinst_LTLIBRARIES = libmailer.la
+
+libmailer_la_SOURCES = \
+ mailer.c\
+ progmailer.c
+
+INCLUDES = @MU_LIB_COMMON_INCLUDES@ -I/libmailutils
diff --git a/libmailutils/mailer/mailer.c b/libmailutils/mailer/mailer.c
new file mode 100644
index 0000000..35c9862
--- /dev/null
+++ b/libmailutils/mailer/mailer.c
@@ -0,0 +1,697 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 2004, 2005, 2006, 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, see
+   <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdarg.h>
+#include <unistd.h>
+#include <errno.h>
+
+#include <sys/time.h>
+
+#include <mailutils/cstr.h>
+#include <mailutils/address.h>
+#include <mailutils/debug.h>
+#include <mailutils/errno.h>
+#include <mailutils/iterator.h>
+#include <mailutils/list.h>
+#include <mailutils/observer.h>
+#include <mailutils/property.h>
+#include <mailutils/registrar.h>
+#include <mailutils/stream.h>
+#include <mailutils/url.h>
+#include <mailutils/header.h>
+#include <mailutils/body.h>
+#include <mailutils/mailbox.h>
+#include <mailutils/message.h>
+#include <mailutils/argcv.h>
+#include <mailutils/mutil.h>
+#include <mailutils/mime.h>
+#include <mailutils/io.h>
+
+#include <mailutils/sys/mailer.h>
+
+static char *mailer_url_default;
+
+/* FIXME: I'd like to check that the URL is valid, but that requires that the
+   mailers already be registered! */
+int
+mu_mailer_set_url_default (const char *url)
+{
+  char *n = NULL;
+
+  if (!url)
+    return EINVAL;
+
+  if ((n = strdup (url)) == NULL)
+    return ENOMEM;
+
+  if (mailer_url_default)
+    free (mailer_url_default);
+
+  mailer_url_default = n;
+
+  return 0;
+}
+
+int
+mu_mailer_get_url_default (const char **url)
+{
+  if (!url)
+    return EINVAL;
+
+  if (mailer_url_default)
+    *url = mailer_url_default;
+  else
+    *url = MAILER_URL_DEFAULT;
+
+  return 0;
+}
+
+static void
+set_default_debug (mu_mailer_t mailer)
+{
+  mu_log_level_t level = mu_global_debug_level ("mailer");
+  if (level)
+    {
+      mu_debug_t debug;
+      if (mu_mailer_get_debug (mailer, &debug))
+	return;
+      mu_debug_set_level (debug, level);
+    }
+}
+
+int
+mu_mailer_create_from_url (mu_mailer_t *pmailer, mu_url_t url)
+{
+  mu_record_t record;
+
+  if (mu_registrar_lookup_url (url, MU_FOLDER_ATTRIBUTE_FILE, &record,
+			       NULL) == 0)
+    {
+      int (*m_init) (mu_mailer_t) = NULL;
+
+      mu_record_get_mailer (record, &m_init);
+      if (m_init)
+        {
+	  int status;
+	  mu_mailer_t mailer;
+	  int (*u_init) (mu_url_t) = NULL;
+	  
+	  /* Allocate memory for mailer.  */
+	  mailer = calloc (1, sizeof (*mailer));
+	  if (mailer == NULL)
+	    return ENOMEM;
+
+	  status = mu_monitor_create (&mailer->monitor, 0, mailer);
+	  if (status)
+	    {
+	      mu_mailer_destroy (&mailer);
+	      return status;
+	    }
+
+	  status = m_init (mailer);
+	  if (status)
+	    {
+	      mu_mailer_destroy (&mailer);
+	      return status;
+	    }
+
+	  mu_record_get_url (record, &u_init);
+	  if (u_init && (status = u_init (url)) != 0)
+	    {
+	      mu_mailer_destroy (&mailer);
+	      return status;
+	    }
+	  
+	  mailer->url = url;
+	  *pmailer = mailer;
+
+	  set_default_debug (mailer);
+	  return status;
+	}
+    }
+  
+    return MU_ERR_MAILER_BAD_URL;
+}
+
+int
+mu_mailer_create (mu_mailer_t * pmailer, const char *name)
+{
+  int status;
+  mu_url_t url;
+
+  if (name == NULL)
+    mu_mailer_get_url_default (&name);
+
+  status = mu_url_create (&url, name);
+  if (status)
+    return status;
+  status = mu_url_parse (url);
+  if (status == 0)
+    status = mu_mailer_create_from_url (pmailer, url);
+  if (status)
+    mu_url_destroy (&url);
+  return status;
+}
+
+void
+mu_mailer_destroy (mu_mailer_t * pmailer)
+{
+  if (pmailer && *pmailer)
+    {
+      mu_mailer_t mailer = *pmailer;
+      mu_monitor_t monitor = mailer->monitor;
+
+      if (mailer->observable)
+	{
+	  mu_observable_notify (mailer->observable, MU_EVT_MAILER_DESTROY,
+				mailer);
+	  mu_observable_destroy (&mailer->observable, mailer);
+	}
+
+      /* Call the object destructor.  */
+      if (mailer->_destroy)
+	mailer->_destroy (mailer);
+
+      mu_monitor_wrlock (monitor);
+
+      if (mailer->stream)
+	{
+	  /* FIXME: Should be the client responsability to close this?  */
+	  /* mu_stream_close (mailer->stream); */
+	  mu_stream_destroy (&mailer->stream);
+	}
+
+      if (mailer->url)
+	mu_url_destroy (&(mailer->url));
+
+      if (mailer->debug)
+	mu_debug_destroy (&(mailer->debug), mailer);
+
+      if (mailer->property)
+	mu_property_destroy (&(mailer->property), mailer);
+
+      free (mailer);
+      *pmailer = NULL;
+      mu_monitor_unlock (monitor);
+      mu_monitor_destroy (&monitor, mailer);
+    }
+}
+
+
+/* -------------- stub functions ------------------- */
+
+int
+mu_mailer_open (mu_mailer_t mailer, int flag)
+{
+  if (mailer == NULL || mailer->_open == NULL)
+    return ENOSYS;
+  return mailer->_open (mailer, flag);
+}
+
+int
+mu_mailer_close (mu_mailer_t mailer)
+{
+  if (mailer == NULL || mailer->_close == NULL)
+    return ENOSYS;
+  return mailer->_close (mailer);
+}
+
+
+int
+mu_mailer_check_from (mu_address_t from)
+{
+  size_t n = 0;
+
+  if (!from)
+    return EINVAL;
+
+  if (mu_address_get_count (from, &n) || n != 1)
+    return MU_ERR_MAILER_BAD_FROM;
+
+  if (mu_address_get_email_count (from, &n) || n == 0)
+    return MU_ERR_MAILER_BAD_FROM;
+
+  return 0;
+}
+
+int
+mu_mailer_check_to (mu_address_t to)
+{
+  size_t count = 0;
+  size_t emails = 0;
+  size_t groups = 0;
+
+  if (!to)
+    return EINVAL;
+
+  if (mu_address_get_count (to, &count))
+    return MU_ERR_MAILER_BAD_TO;
+
+  if (mu_address_get_email_count (to, &emails))
+    return MU_ERR_MAILER_BAD_TO;
+
+  if (emails == 0)
+    return MU_ERR_MAILER_NO_RCPT_TO;
+
+  if (mu_address_get_group_count (to, &groups))
+    return MU_ERR_MAILER_BAD_TO;
+
+  if (count - emails - groups != 0)
+    /* then not everything is a group or an email address */
+    return MU_ERR_MAILER_BAD_TO;
+
+  return 0;
+}
+
+static void
+save_fcc (mu_message_t msg)
+{
+  mu_header_t hdr;
+  size_t count = 0, i;
+  char buf[512], *fcc;
+  
+  if (mu_message_get_header (msg, &hdr))
+    return;
+
+  if (mu_header_get_value (hdr, MU_HEADER_FCC, NULL, 0, NULL))
+    return;
+  
+  mu_header_get_field_count (hdr, &count);
+  for (i = 1; i <= count; i++)
+    {
+      mu_mailbox_t mbox;
+      
+      mu_header_get_field_name (hdr, i, buf, sizeof buf, NULL);
+      if (mu_c_strcasecmp (buf, MU_HEADER_FCC) == 0
+	  && mu_header_aget_field_value (hdr, i, &fcc) == 0)
+	{
+	  int i, argc;
+	  char **argv;
+	  
+	  mu_argcv_get (fcc, ",", NULL, &argc, &argv);
+	  for (i = 0; i < argc; i += 2)
+	    {
+	      if (mu_mailbox_create_default (&mbox, argv[i]))
+		continue; /*FIXME: error message?? */
+	      if (mu_mailbox_open (mbox,
+				   MU_STREAM_RDWR | MU_STREAM_CREAT
+				   | MU_STREAM_APPEND) == 0)
+		{
+		  mu_mailbox_append_message (mbox, msg);
+		  mu_mailbox_flush (mbox, 0);
+		}
+	      mu_mailbox_close (mbox);
+	      mu_mailbox_destroy (&mbox);
+	    }
+	  mu_argcv_free (argc, argv);
+	  free (fcc);
+	}
+    }
+}
+
+static int
+_set_from (mu_address_t *pfrom, mu_message_t msg, mu_address_t from,
+	   mu_mailer_t mailer)
+{
+  int status = 0;
+  char *mail_from;
+  mu_header_t header = NULL;
+
+  *pfrom = NULL;
+  
+  /* Get MAIL_FROM from FROM, the message, or the environment. */
+  if (!from)
+    {
+      const char *type;
+      
+      if ((status = mu_message_get_header (msg, &header)) != 0)
+	return status;
+      
+      status = mu_header_aget_value (header, MU_HEADER_FROM, &mail_from);
+      
+      switch (status)
+	{
+	default:
+	  return status;
+
+	  /* Use the From: header. */
+	case 0:
+	  MU_DEBUG1 (mailer->debug, MU_DEBUG_TRACE,
+		     "mu_mailer_send_message(): using From: %s\n",
+		     mail_from);
+	    
+	  status = mu_address_create (pfrom, mail_from);
+	  free (mail_from);
+	  break;
+
+	case MU_ERR_NOENT:
+	  if (mu_property_sget_value (mailer->property, "TYPE", &type) == 0
+	      && strcmp (type, "SENDMAIL") == 0)
+	    return 0;
+	  
+	  /* Use the environment. */
+	  mail_from = mu_get_user_email (NULL);
+
+	  if (mail_from)
+            MU_DEBUG1 (mailer->debug, MU_DEBUG_TRACE,
+		       "mu_mailer_send_message(): using user's address: %s\n",
+		       mail_from);
+	  else
+            MU_DEBUG (mailer->debug, MU_DEBUG_ERROR,
+		      "mu_mailer_send_message(): no user's address, failing\n");
+
+	  if (!mail_from)
+	    return errno;
+
+	  status = mu_address_create (pfrom, mail_from);
+	  /* FIXME: should we add the From: header? */
+	  break;
+	}
+    }
+
+  return status;
+}
+
+static int
+create_part (mu_mime_t mime, mu_stream_t istr, 
+	     size_t fragsize, size_t n, size_t nparts, char *msgid)
+{
+  int status = 0;
+  mu_message_t newmsg;
+  mu_header_t newhdr;
+  mu_body_t body;
+  mu_stream_t ostr;
+  char buffer[512], *str;
+  size_t slen;
+  
+  mu_message_create (&newmsg, NULL);
+  mu_message_get_header (newmsg, &newhdr); 
+
+  str = NULL;
+  slen = 0;
+  mu_asnprintf (&str, &slen,
+		"message/partial; id=\"%s\"; number=%lu; total=%lu",
+		msgid, (unsigned long)n, (unsigned long)nparts);
+  mu_header_append (newhdr, MU_HEADER_CONTENT_TYPE, str);
+  mu_asnprintf (&str, &slen, "part %lu of %lu",
+		(unsigned long)n, (unsigned long)nparts);
+  mu_header_append (newhdr, MU_HEADER_CONTENT_DESCRIPTION, str);
+  free (str);
+  
+  mu_message_get_body (newmsg, &body);
+  mu_body_get_streamref (body, &ostr);
+
+  status = mu_stream_seek (ostr, 0, SEEK_SET, NULL);
+  if (status == 0)
+    while (fragsize)
+      {
+	size_t rds = fragsize;
+	if (rds > sizeof buffer)
+	  rds = sizeof buffer;
+	
+	status = mu_stream_read (istr, buffer, rds, &rds);
+	if (status || rds == 0)
+	  break;
+	status = mu_stream_write (ostr, buffer, rds, NULL);
+	if (status)
+	  break;
+	fragsize -= rds;
+      }
+  mu_stream_destroy (&ostr);
+  
+  if (status == 0)
+    {
+      mu_mime_add_part (mime, newmsg);
+      mu_message_unref (newmsg);
+    }
+  return status;
+}
+
+static void
+merge_headers (mu_message_t newmsg, mu_header_t hdr)
+{
+  size_t i, count;
+  mu_header_t newhdr;
+  
+  mu_message_get_header (newmsg, &newhdr);
+  mu_header_get_field_count (hdr, &count);
+  for (i = 1; i <= count; i++)
+    {
+      const char *fn, *fv;
+
+      mu_header_sget_field_name (hdr, i, &fn);
+      mu_header_sget_field_value (hdr, i, &fv);
+      if (mu_c_strcasecmp (fn, MU_HEADER_MESSAGE_ID) == 0)
+	continue;
+      else if (mu_c_strcasecmp (fn, MU_HEADER_MIME_VERSION) == 0)
+	mu_header_append (newhdr, "X-Orig-" MU_HEADER_MIME_VERSION,
+			  fv);
+      else if (mu_c_strcasecmp (fn, MU_HEADER_CONTENT_TYPE) == 0)
+	mu_header_append (newhdr, "X-Orig-" MU_HEADER_CONTENT_TYPE,
+			  fv);
+      else if (mu_c_strcasecmp (fn, MU_HEADER_CONTENT_DESCRIPTION) == 0)
+	mu_header_append (newhdr, "X-Orig-" MU_HEADER_CONTENT_DESCRIPTION,
+			  fv);
+      else
+	mu_header_append (newhdr, fn, fv);
+    }
+}
+  
+
+int
+send_fragments (mu_mailer_t mailer,
+		mu_header_t hdr,
+		mu_stream_t str,
+		size_t nparts, size_t fragsize,
+		struct timeval *delay,
+		mu_address_t from, mu_address_t to)
+{
+  int status = 0;
+  size_t i;
+  char *msgid = NULL;
+  
+  if (mu_header_aget_value (hdr, MU_HEADER_MESSAGE_ID, &msgid))
+    mu_rfc2822_msg_id (0, &msgid);
+  
+  for (i = 1; i <= nparts; i++)
+    {
+      mu_message_t newmsg;
+      mu_mime_t mime;
+		  
+      mu_mime_create (&mime, NULL, 0);
+      status = create_part (mime, str, fragsize, i, nparts, msgid);
+      if (status)
+	break;
+
+      mu_mime_get_message (mime, &newmsg);
+      merge_headers (newmsg, hdr);
+      
+      status = mailer->_send_message (mailer, newmsg, from, to);
+      mu_mime_destroy (&mime);
+      if (status)
+	break;
+      if (delay)
+	{
+	  struct timeval t = *delay;
+	  select (0, NULL, NULL, NULL, &t);
+	}
+    }
+  free (msgid);
+  return status;
+}
+
+int
+mu_mailer_send_fragments (mu_mailer_t mailer,
+			  mu_message_t msg,
+			  size_t fragsize, struct timeval *delay,
+			  mu_address_t from, mu_address_t to)
+{
+  int status;
+  mu_address_t sender_addr = NULL;
+  
+  if (mailer == NULL)
+    return EINVAL;
+  if (mailer->_send_message == NULL)
+    return ENOSYS;
+
+  status = _set_from (&sender_addr, msg, from, mailer);
+  if (status)
+    return status;
+  if (sender_addr)
+    from = sender_addr;
+  
+  if ((!from || (status = mu_mailer_check_from (from)) == 0)
+      && (!to || (status = mu_mailer_check_to (to)) == 0))
+    {
+      save_fcc (msg);
+      if (fragsize == 0)
+	status = mailer->_send_message (mailer, msg, from, to);
+      else
+	{
+	  mu_header_t hdr;
+	  mu_body_t body;
+	  size_t bsize;
+	  size_t nparts;
+	  
+	  /* Estimate the number of messages to be sent. */
+	  mu_message_get_header (msg, &hdr);
+
+	  mu_message_get_body (msg, &body);
+	  mu_body_size (body, &bsize);
+
+	  nparts = bsize + fragsize - 1;
+	  if (nparts < bsize) /* overflow */
+	    return EINVAL;
+	  nparts /= fragsize;
+
+	  if (nparts == 1)
+	    status = mailer->_send_message (mailer, msg, from, to);
+	  else
+	    {
+	      mu_stream_t str;
+	      status = mu_body_get_streamref (body, &str);
+	      if (status)
+		{
+		  status = send_fragments (mailer, hdr, str, nparts,
+					   fragsize, delay, from, to);
+		  mu_stream_destroy (&str);
+		}
+	    }
+	}
+    }
+  mu_address_destroy (&sender_addr);
+  return status;
+}
+
+int
+mu_mailer_send_message (mu_mailer_t mailer, mu_message_t msg,
+			mu_address_t from, mu_address_t to)
+{
+  return mu_mailer_send_fragments (mailer, msg, 0, NULL, from, to);
+}
+
+int
+mu_mailer_set_stream (mu_mailer_t mailer, mu_stream_t stream)
+{
+  if (mailer == NULL)
+    return EINVAL;
+  mailer->stream = stream;
+  return 0;
+}
+
+int
+mu_mailer_get_stream (mu_mailer_t mailer, mu_stream_t * pstream)
+{
+  /* FIXME: Deprecation warning */
+  if (mailer == NULL)
+    return EINVAL;
+  if (pstream == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+  *pstream = mailer->stream;
+  return 0;
+}
+
+int
+mu_mailer_get_streamref (mu_mailer_t mailer, mu_stream_t * pstream)
+{
+  if (mailer == NULL)
+    return EINVAL;
+  if (pstream == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+  return mu_streamref_create (pstream, mailer->stream);
+}
+
+int
+mu_mailer_get_observable (mu_mailer_t mailer, mu_observable_t * pobservable)
+{
+  /* FIXME: I should check for invalid types */
+  if (mailer == NULL)
+    return EINVAL;
+  if (pobservable == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+  if (mailer->observable == NULL)
+    {
+      int status = mu_observable_create (&(mailer->observable), mailer);
+      if (status != 0)
+	return status;
+    }
+  *pobservable = mailer->observable;
+  return 0;
+}
+
+int
+mu_mailer_get_property (mu_mailer_t mailer, mu_property_t * pproperty)
+{
+  if (mailer == NULL)
+    return EINVAL;
+  if (pproperty == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+  if (mailer->property == NULL)
+    {
+      int status = mu_property_create (&(mailer->property), mailer);
+      if (status != 0)
+	return status;
+    }
+  *pproperty = mailer->property;
+  return 0;
+}
+
+int
+mu_mailer_set_debug (mu_mailer_t mailer, mu_debug_t debug)
+{
+  if (mailer == NULL)
+    return EINVAL;
+  mu_debug_destroy (&(mailer->debug), mailer);
+  mailer->debug = debug;
+  return 0;
+}
+
+int
+mu_mailer_get_debug (mu_mailer_t mailer, mu_debug_t * pdebug)
+{
+  if (mailer == NULL)
+    return EINVAL;
+  if (pdebug == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+  if (mailer->debug == NULL)
+    {
+      int status = mu_debug_create (&(mailer->debug), mailer);
+      if (status != 0)
+	return status;
+    }
+  *pdebug = mailer->debug;
+  return 0;
+}
+
+int
+mu_mailer_get_url (mu_mailer_t mailer, mu_url_t * purl)
+{
+  if (!mailer)
+    return EINVAL;
+  if (!purl)
+    return MU_ERR_OUT_PTR_NULL;
+  *purl = mailer->url;
+  return 0;
+}
diff --git a/libmailutils/mailer/progmailer.c b/libmailutils/mailer/progmailer.c
new file mode 100644
index 0000000..6411cf0
--- /dev/null
+++ b/libmailutils/mailer/progmailer.c
@@ -0,0 +1,313 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 2004, 2005, 2006, 2007, 2008, 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, see 
+   <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <signal.h>
+#include <sysexits.h>
+
+#include <sys/types.h>
+#include <sys/wait.h>
+
+#include <mailutils/debug.h>
+#include <mailutils/error.h>
+#include <mailutils/errno.h>
+#include <mailutils/stream.h>
+#include <mailutils/header.h>
+#include <mailutils/body.h>
+#include <mailutils/message.h>
+#include <mailutils/progmailer.h>
+#include <mailutils/mutil.h>
+#include <mailutils/cstr.h>
+
+struct _mu_progmailer
+{
+  int fd;
+  pid_t pid;
+  RETSIGTYPE (*sighandler)();
+  mu_debug_t debug;
+  char *command;
+};
+
+/* Close FD unless it is part of pipe P */
+#define SCLOSE(fd,p) if (p[0]!=fd&&p[1]!=fd) close(fd)
+
+int
+mu_progmailer_create (struct _mu_progmailer **ppm)
+{
+  struct _mu_progmailer *pm = malloc (sizeof (*pm));
+  if (!pm)
+    return ENOMEM;
+  pm->fd = -1;
+  pm->pid = -1;
+  pm->sighandler = SIG_ERR;
+  pm->debug = NULL;
+  pm->command = NULL;
+  *ppm = pm;
+  return 0;
+}
+
+int
+mu_progmailer_set_command (mu_progmailer_t pm, const char *command)
+{
+  if (!pm)
+    return EINVAL;
+  free (pm->command);
+  if (command)
+    {
+      pm->command = strdup (command);
+      if (!pm->command)
+	return EINVAL;
+    }
+  else
+    pm->command = NULL;
+  return 0;
+}
+
+int
+mu_progmailer_sget_command (mu_progmailer_t pm, const char **command)
+{
+  if (!pm)
+    return EINVAL;
+  *command = pm->command;
+  return 0;
+}
+
+int
+mu_progmailer_set_debug (mu_progmailer_t pm, mu_debug_t debug)
+{
+  if (!pm)
+    return EINVAL;
+  pm->debug = debug;
+  return 0;
+}
+
+void
+mu_progmailer_destroy (struct _mu_progmailer **ppm)
+{
+  if (*ppm)
+    {
+      free ((*ppm)->command);
+      free (*ppm);
+      *ppm = NULL;
+    }
+}
+
+int
+mu_progmailer_open (struct _mu_progmailer *pm, char **argv)
+{
+  int tunnel[2];
+  int status = 0;
+  int i;
+
+  if (!pm || !argv)
+    return EINVAL;
+  
+  if ((pm->sighandler = signal (SIGCHLD, SIG_DFL)) == SIG_ERR)
+    {
+      status = errno;
+      MU_DEBUG1 (pm->debug, MU_DEBUG_ERROR,
+		 "setting SIGCHLD failed: %s\n", mu_strerror (status));
+      return status;
+    }
+      
+  if (pipe (tunnel) == 0)
+    {
+      pm->fd = tunnel[1];
+      pm->pid = fork ();
+      if (pm->pid == 0)	/* Child.  */
+	{
+	  SCLOSE (STDIN_FILENO, tunnel);
+	  SCLOSE (STDOUT_FILENO, tunnel);
+	  SCLOSE (STDERR_FILENO, tunnel);
+	  close (tunnel[1]);
+	  dup2 (tunnel[0], STDIN_FILENO);
+	  execv (pm->command ? pm->command : argv[0], argv);
+	  exit (errno ? EX_CANTCREAT : 0);
+	}
+      else if (pm->pid == -1)
+	{
+	  status = errno;
+	  MU_DEBUG1 (pm->debug, MU_DEBUG_ERROR,
+		     "fork failed: %s\n", mu_strerror (status));
+	}
+    }
+  else
+    {
+      status = errno;
+      MU_DEBUG1 (pm->debug, MU_DEBUG_ERROR,
+		 "pipe() failed: %s\n", mu_strerror (status));
+    }
+  MU_DEBUG1 (pm->debug, MU_DEBUG_TRACE, "exec %s argv:", pm->command);
+  for (i = 0; argv[i]; i++)
+    MU_DEBUG1 (pm->debug, MU_DEBUG_TRACE, " %s", argv[i]);
+  MU_DEBUG (pm->debug, MU_DEBUG_TRACE, "\n");
+  close (tunnel[0]);
+
+  if (status != 0)
+    close (pm->fd);
+  return status;
+}
+
+int
+mu_progmailer_send (struct _mu_progmailer *pm, mu_message_t msg)
+{
+  int status;
+  mu_stream_t stream = NULL;
+  char buffer[512];
+  size_t len = 0;
+  int rc;
+  mu_header_t hdr;
+  mu_body_t body;
+  int found_nl = 0;
+  int exit_status;
+	
+  if (!pm || !msg)
+    return EINVAL;
+  mu_message_get_header (msg, &hdr);
+  status = mu_header_get_streamref (hdr, &stream);
+  if (status)
+    {
+      MU_DEBUG1 (pm->debug, MU_DEBUG_ERROR,
+		 "cannot get header stream: %s\n", mu_strerror (status));
+      return status;
+    }
+  MU_DEBUG (pm->debug, MU_DEBUG_TRACE, "Sending headers...\n");
+  mu_stream_seek (stream, 0, MU_SEEK_SET, NULL);
+  while ((status = mu_stream_readline (stream, buffer, sizeof (buffer),
+				       &len)) == 0
+	 && len != 0)
+    {
+      if (mu_c_strncasecmp (buffer, MU_HEADER_FCC, sizeof (MU_HEADER_FCC) - 1))
+	{
+	  MU_DEBUG1 (pm->debug, MU_DEBUG_PROT, "Header: %s", buffer);
+	  if (write (pm->fd, buffer, len) == -1)
+	    {
+	      status = errno;
+	      
+	      MU_DEBUG1 (pm->debug, MU_DEBUG_TRACE,
+			 "write failed: %s\n", strerror (status));
+	      break;
+	    }
+	}
+      found_nl = (len == 1 && buffer[0] == '\n');
+    }
+
+  if (!found_nl)
+    {
+      if (write (pm->fd, "\n", 1) == -1)
+	{
+	  status = errno;
+		
+	  MU_DEBUG1 (pm->debug, MU_DEBUG_TRACE,
+		     "write failed: %s\n", strerror (status));
+	}
+    }
+  mu_stream_destroy (&stream);
+  
+  MU_DEBUG (pm->debug, MU_DEBUG_TRACE, "Sending body...\n");
+  mu_message_get_body (msg, &body);
+  status = mu_body_get_streamref (body, &stream);
+  if (status)
+    {
+      MU_DEBUG1 (pm->debug, MU_DEBUG_ERROR,
+		 "cannot get body stream: %s\n", mu_strerror (status));
+      return status;
+    }
+
+  mu_stream_seek (stream, 0, MU_SEEK_SET, NULL);
+  while ((status = mu_stream_read (stream, buffer, sizeof (buffer),
+				   &len)) == 0
+	 && len != 0)
+    {
+      if (write (pm->fd, buffer, len) == -1)
+	{
+	  status = errno;
+	  
+	  MU_DEBUG1 (pm->debug, MU_DEBUG_TRACE,
+		     "write failed: %s\n", strerror (status));
+	  break;
+	}
+    }
+  mu_body_get_streamref (body, &stream);
+
+  close (pm->fd);
+
+  rc = waitpid (pm->pid, &exit_status, 0);
+  if (status == 0)
+    {
+      if (rc < 0)
+	{
+	  if (errno == ECHILD)
+	    status = 0;
+	  else
+	    { 
+	      status = errno;
+	      MU_DEBUG2 (pm->debug, MU_DEBUG_TRACE,
+			 "waitpid(%lu) failed: %s\n",
+			 (unsigned long) pm->pid, strerror (status));
+	    }
+	}
+      else if (WIFEXITED (exit_status))
+	{
+	  exit_status = WEXITSTATUS (exit_status);
+	  MU_DEBUG2 (pm->debug, MU_DEBUG_TRACE,
+		     "%s exited with: %d\n",
+		     pm->command, exit_status);
+	  status = (exit_status == 0) ? 0 : MU_ERR_PROCESS_EXITED;
+	}
+      else if (WIFSIGNALED (exit_status))
+	status = MU_ERR_PROCESS_SIGNALED;
+      else
+	status = MU_ERR_PROCESS_UNKNOWN_FAILURE;
+    }
+  pm->pid = -1;
+  return status;
+}
+
+int
+mu_progmailer_close (struct _mu_progmailer *pm)
+{
+  int status = 0;
+
+  if (!pm)
+    return EINVAL;
+  
+  if (pm->pid > 0)
+    {
+      kill (SIGTERM, pm->pid);
+      pm->pid = -1;
+    }
+
+  if (pm->sighandler != SIG_ERR
+      && signal (SIGCHLD, pm->sighandler) == SIG_ERR)
+    {
+      status = errno;
+      MU_DEBUG1 (pm->debug, MU_DEBUG_ERROR,
+		 "resetting SIGCHLD failed: %s\n", mu_strerror (status));
+    }
+  pm->sighandler = SIG_ERR;
+  return status;
+}
diff --git a/libmailutils/mbx_default.c b/libmailutils/mbx_default.c
deleted file mode 100644
index 348e7f7..0000000
--- a/libmailutils/mbx_default.c
+++ /dev/null
@@ -1,454 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 1999, 2000, 2001, 2003, 2004, 2005, 2006, 2007, 2008,
-   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 <stdlib.h>
-#include <errno.h>
-#include <stdio.h>
-#include <pwd.h>
-#include <unistd.h>
-
-#include <confpaths.h>
-
-#include <mailutils/mailbox.h>
-#include <mailutils/mutil.h>
-#include <mailutils/debug.h>
-#include <mailutils/error.h>
-#include <mailutils/errno.h>
-#include <mailutils/mu_auth.h>
-#include <mailutils/vartab.h>
-#include <mailutils/folder.h>
-#include <mailutils/auth.h>
-
-#include <mailutils/sys/mailbox.h>
-
-char *mu_ticket_file = "~/.mu-tickets";
-
-static char *_mu_mailbox_pattern;
-
-static char *_default_folder_dir = "Mail";
-static char *_mu_folder_dir;
-
-static int
-mu_normalize_mailbox_url (char **pout, const char *dir)
-{
-  int len;
-  int addslash = 0;
-#define USERSUFFIX "${user}"
-  
-  if (!pout)
-    return MU_ERR_OUT_PTR_NULL;
-      
-  len = strlen (dir);
-  if (dir[len-1] == '=')
-    {
-      if (len > 5 && strcmp (dir + len - 5, "user=") == 0)
-	*pout = strdup (dir);
-      else
-	return MU_ERR_BAD_FILENAME;
-    }
-  else if (dir[len-1] != '/')
-    addslash = 1;
-
-  *pout = malloc (strlen (dir) + (addslash ? 1 : 0) + sizeof USERSUFFIX);
-  if (!*pout)
-    return ENOMEM;
-
-  strcpy (*pout, dir);
-  if (addslash)
-    strcat (*pout, "/");
-  strcat (*pout, USERSUFFIX);
-#undef USERSUFFIX
-  return 0;
-}
-
-int
-mu_set_mail_directory (const char *p)
-{
-  if (_mu_mailbox_pattern)
-    free (_mu_mailbox_pattern);
-  if (!p)
-    {
-      _mu_mailbox_pattern = NULL;
-      return 0;
-    }
-  return mu_normalize_mailbox_url (&_mu_mailbox_pattern, p);
-}
-
-int
-mu_set_mailbox_pattern (const char *pat)
-{
-  if (_mu_mailbox_pattern)
-    free (_mu_mailbox_pattern);
-  if (!pat)
-    {
-      _mu_mailbox_pattern = NULL;
-      return 0;
-    }
-  _mu_mailbox_pattern = strdup (pat);
-  return _mu_mailbox_pattern ? 0 : ENOMEM;
-}
-
-void
-mu_set_folder_directory (const char *p)
-{
-  if (_mu_folder_dir != _default_folder_dir)
-    free (_mu_folder_dir);
-  _mu_folder_dir = strdup (p);
-}
-
-const char *
-mu_mailbox_url ()
-{
-  if (!_mu_mailbox_pattern)
-    mu_set_mail_directory (MU_PATH_MAILDIR);
-  return _mu_mailbox_pattern;
-}
-
-const char *
-mu_folder_directory ()
-{
-  if (!_mu_folder_dir)
-    _mu_folder_dir = _default_folder_dir;
-  return _mu_folder_dir;
-}
-
-int
-mu_construct_user_mailbox_url (char **pout, const char *name)
-{
-  int rc;
-  const char *pat = mu_mailbox_url ();
-  mu_vartab_t vtab;
-
-  mu_vartab_create (&vtab);
-  mu_vartab_define (vtab, "user", name, 1);
-  rc = mu_vartab_expand (vtab, pat, pout);
-  mu_vartab_destroy (&vtab);
-  return rc;
-}
-
-/* Is this a security risk?  */
-#define USE_ENVIRON 1
-
-static int
-split_shortcut (const char *file, const char pfx[], char **user, char **rest)
-{
-  *user = NULL;
-  *rest = NULL;
-
-  if (!strchr (pfx, file[0]))
-    return 0;
-
-  if (*++file == 0)
-    return 0;
-  else
-    {
-      char *p = strchr (file, '/');
-      int len;
-      if (p)
-        len = p - file + 1;
-      else
-        len = strlen (file) + 1;
-
-      if (len == 1)
-	*user = NULL;
-      else
-	{
-	  *user = calloc (1, len);
-	  if (!*user)
-	    return ENOMEM;
-
-	  memcpy (*user, file, len);
-	  (*user)[len-1] = 0;
-	}
-      file += len-1;
-      if (file[0] == '/')
-        file++;
-    }
-
-  if (file[0])
-    {
-      *rest = strdup (file);
-      if (!*rest)
-        {
-          free (*user);
-          return ENOMEM;
-        }
-    }
-  
-  return 0;
-}
-
-static char *
-get_homedir (const char *user)
-{
-  char *homedir = NULL;
-  struct mu_auth_data *auth = NULL;
-  
-  if (user)
-    {
-      auth = mu_get_auth_by_name (user);
-      if (auth)
-        homedir = auth->dir;
-    }
-  else
-    {
-#ifdef USE_ENVIRON
-      /* NOTE: Should we honor ${HOME}?  */
-      homedir = getenv ("HOME");
-      if (homedir == NULL)
-        {
-	  auth = mu_get_auth_by_name (user);
-	  if (auth)
-	    homedir = auth->dir;
-        }
-#else
-      auth = mu_get_auth_by_name (user);
-      if (auth)
-	homedir = auth->dir;
-#endif
-    }
-
-  if (homedir)
-    homedir = strdup (homedir);
-  mu_auth_data_free (auth);
-  return homedir;
-}
-
-static int
-user_mailbox_name (const char *user, char **mailbox_name)
-{
-#ifdef USE_ENVIRON
-  if (!user)
-    user = (getenv ("LOGNAME")) ? getenv ("LOGNAME") : getenv ("USER");
-#endif
-
-  if (user)
-    {
-      int rc = mu_construct_user_mailbox_url (mailbox_name, user);
-      if (rc)
-	return rc;
-    }
-  else
-    {
-      struct mu_auth_data *auth = mu_get_auth_by_uid (getuid ());
-
-      if (!auth)
-        {
-          mu_error ("Who am I?");
-          return EINVAL;
-        }
-      *mailbox_name = strdup (auth->mailbox);
-      mu_auth_data_free (auth);
-    }
-
-  return 0;
-}
-
-static int
-plus_expand (const char *file, char **buf)
-{
-  char *home;
-  const char *folder_dir = mu_folder_directory ();
-  int len;
-
-  home = get_homedir (NULL);
-  if (!home)
-    return ENOENT;
-  
-  file++;
-  
-  if (folder_dir[0] == '/' || mu_is_proto (folder_dir))
-    {
-      len = strlen (folder_dir) + strlen (file) + 2;
-      *buf = malloc (len);
-      sprintf (*buf, "%s/%s", folder_dir, file);
-    }
-  else
-    {
-      len = strlen (home) + strlen (folder_dir) + strlen (file) + 3;
-      *buf = malloc (len);
-      sprintf (*buf, "%s/%s/%s", home, folder_dir, file);
-    }
-  (*buf)[len-1] = 0;
-  
-  free (home);
-  return 0;
-}
-
-static int
-percent_expand (const char *file, char **mbox)
-{
-  char *user = NULL;
-  char *path = NULL;
-  int status;
-  
-  if ((status = split_shortcut (file, "%", &user, &path)))
-    return status;
-
-  if (path)
-    {
-      free (user);
-      free (path);
-      return ENOENT;
-    }
-
-  status = user_mailbox_name (user, mbox);
-  free (user);
-  return status;
-}
-
-static void
-attach_auth_ticket (mu_mailbox_t mbox)
-{
-  mu_folder_t folder = NULL;
-  mu_authority_t auth = NULL;
-
-  if (mu_mailbox_get_folder (mbox, &folder) == 0
-      && mu_folder_get_authority (folder, &auth) == 0
-      && auth)
-    {
-      char *filename = mu_tilde_expansion (mu_ticket_file, "/", NULL);
-      mu_wicket_t wicket;
-      int rc;
-  
-      MU_DEBUG1 (mbox->debug, MU_DEBUG_TRACE1,
-		 "Reading user ticket file %s\n", filename);
-      if ((rc = mu_file_wicket_create (&wicket, filename)) == 0)
-	{
-	  mu_ticket_t ticket;
-      
-	  if ((rc = mu_wicket_get_ticket (wicket, NULL, &ticket)) == 0)
-	    {
-	      rc = mu_authority_set_ticket (auth, ticket);
-	      MU_DEBUG1 (mbox->debug, MU_DEBUG_TRACE1,
-			 "Retrieved and set ticket: %d\n", rc);
-	    }
-	  else
-	    MU_DEBUG1 (mbox->debug, MU_DEBUG_ERROR,
-		       "Error retrieving ticket: %s\n",
-		       mu_strerror (rc));
-	  mu_wicket_destroy (&wicket);
-	}
-      else
-	MU_DEBUG1 (mbox->debug, MU_DEBUG_ERROR,
-		   "Error creating wicket: %s\n", mu_strerror (rc));
-      free (filename);
-    }
-}
-
-/* We are trying to be smart about the location of the mail.
-   mu_mailbox_create() is not doing this.
-   %           --> system mailbox for the real uid
-   %user       --> system mailbox for the given user
-   ~/file      --> /home/user/file
-   ~user/file  --> /home/user/file
-   +file       --> /home/user/Mail/file
-   =file       --> /home/user/Mail/file
-*/
-int
-mu_mailbox_create_default (mu_mailbox_t *pmbox, const char *mail)
-{
-  char *mbox = NULL;
-  char *tmp_mbox = NULL;
-  char *p;
-  int status = 0;
-
-  /* Sanity.  */
-  if (pmbox == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-
-  if (mail && *mail == 0)
-    mail = NULL;
-  
-  if (mail == NULL)
-    {
-      if (!_mu_mailbox_pattern)
-	{
-	  /* Other utilities may not understand GNU mailutils url namespace, so
-	     use FOLDER instead, to not confuse others by using MAIL.  */
-	  mail = getenv ("FOLDER");
-	  if (!mail)
-	    {
-	      /* Fallback to well-known environment.  */
-	      mail = getenv ("MAIL");
-	    }
-	}
-
-      if (!mail)
-	{
-	  if ((status = user_mailbox_name (NULL, &tmp_mbox)))
-	    return status;
-	  mail = tmp_mbox;
-	}
-    }
-
-  p = mu_tilde_expansion (mail, "/", NULL);
-  if (tmp_mbox)
-    free (tmp_mbox);
-  tmp_mbox = p;
-  mail = tmp_mbox;
-  if (!mail)
-    return ENOMEM;
-  
-  switch (mail[0])
-    {
-    case '%':
-      status = percent_expand (mail, &mbox);
-      break;
-      
-    case '+':
-    case '=':
-      status = plus_expand (mail, &mbox);
-      break;
-
-    case '/':
-      mbox = strdup (mail);
-      break;
-      
-    default:
-      if (!mu_is_proto (mail))
-	{
-	  p = mu_getcwd();
-	  mbox = malloc (strlen (p) + strlen (mail) + 2);
-	  sprintf (mbox, "%s/%s", p, mail);
-	  free (p);  
-	}
-      else
-	mbox = strdup (mail);
-      break;
-    }
-
-  if (tmp_mbox)
-    free (tmp_mbox);
-
-  if (status)
-    return status;
-  
-  status = mu_mailbox_create (pmbox, mbox);
-  free (mbox);
-  if (status == 0)
-    attach_auth_ticket (*pmbox);
-      
-  return status;
-}
diff --git a/libmailutils/mbxitr.c b/libmailutils/mbxitr.c
deleted file mode 100644
index 8463201..0000000
--- a/libmailutils/mbxitr.c
+++ /dev/null
@@ -1,160 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 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 <string.h>
-
-#include <mailutils/debug.h>
-#include <mailutils/errno.h>
-#include <mailutils/error.h>
-#include <mailutils/iterator.h>
-
-#include <mailutils/sys/mailbox.h>
-
-struct mailbox_iterator
-{
-  mu_mailbox_t mbx;
-  size_t idx;
-};
-
-static int
-mbx_first (void *owner)
-{
-  struct mailbox_iterator *itr = owner;
-  itr->idx = 1;
-  return 0;
-}
-
-static int
-mbx_next (void *owner)
-{
-  struct mailbox_iterator *itr = owner;
-  itr->idx++;
-  return 0;
-}
-
-static int
-mbx_getitem (void *owner, void **pret, const void **pkey)
-{
-  struct mailbox_iterator *itr = owner;
-  size_t count;
-  int rc;
-  
-  rc = mu_mailbox_messages_count (itr->mbx, &count);
-  if (rc)
-    return rc;
-  if (itr->idx > count)
-    return MU_ERR_NOENT;
-  rc = mu_mailbox_get_message (itr->mbx, itr->idx, (mu_message_t*)pret);
-  if (rc == 0 && pkey)
-    *pkey = NULL; /* FIXME: Perhaps return UIDL or other unique id? */
-  return rc;
-}
-
-static int
-mbx_finished_p (void *owner)
-{
-  struct mailbox_iterator *itr = owner;
-  size_t count;
-
-  if (mu_mailbox_messages_count (itr->mbx, &count))
-    return 1;
-  return itr->idx > count;
-}
-
-static int
-mbx_destroy (mu_iterator_t iterator, void *data)
-{
-  struct mailbox_iterator *itr = data;
-
-  mu_iterator_detach (&itr->mbx->iterator, iterator);
-  free (data);
-  return 0;
-}
-
-static int
-mbx_curitem_p (void *owner, void *item)
-{
-  void *ptr;
-
-  if (mbx_getitem (owner, &ptr, NULL))
-    return 0;
-  return ptr == item;/* FIXME: Is it ok? */
-}
-
-static int
-mbx_data_dup (void **ptr, void *owner)
-{
-  struct mailbox_iterator *itr = owner;
-
-  *ptr = malloc (sizeof (struct mailbox_iterator));
-  if (*ptr == NULL)
-    return ENOMEM;
-  memcpy (*ptr, owner, sizeof (struct mailbox_iterator));
-  mu_iterator_attach (&itr->mbx->iterator, *ptr);
-  return 0;
-}
-
-int
-mu_mailbox_get_iterator (mu_mailbox_t mbx, mu_iterator_t *piterator)
-{
-  mu_iterator_t iterator;
-  int status;
-  struct mailbox_iterator *itr;
-
-  if (!mbx)
-    return EINVAL;
-
-  itr = calloc (1, sizeof *itr);
-  if (!itr)
-    return ENOMEM;
-  itr->mbx = mbx;
-  itr->idx = 1;
-
-  status = mu_iterator_create (&iterator, itr);
-  if (status)
-    {
-      free (itr);
-      return status;
-    }
-
-  mu_iterator_set_first (iterator, mbx_first);
-  mu_iterator_set_next (iterator, mbx_next);
-  mu_iterator_set_getitem (iterator, mbx_getitem);
-  mu_iterator_set_finished_p (iterator, mbx_finished_p);
-  mu_iterator_set_curitem_p (iterator, mbx_curitem_p);
-  mu_iterator_set_destroy (iterator, mbx_destroy);
-  mu_iterator_set_dup (iterator, mbx_data_dup);
-
-  mu_iterator_attach (&mbx->iterator, iterator);
-
-  *piterator = iterator;
-  return 0;
-}
-
-
-
-
-
-  
-
diff --git a/libmailutils/md5.c b/libmailutils/md5.c
deleted file mode 100644
index a6d3247..0000000
--- a/libmailutils/md5.c
+++ /dev/null
@@ -1,454 +0,0 @@
-/* Functions to compute MD5 message digest of files or memory blocks.
-   according to the definition of MD5 in RFC 1321 from April 1992.
-   Copyright (C) 1995,1996,1997,1999,2000,2001,2005,2006,2010
-	Free Software Foundation, Inc.
-   This file is part of the GNU C Library.
-
-   This program 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 2, 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.  */
-
-/* Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.  */
-
-#ifdef HAVE_CONFIG_H
-# include <config.h>
-#endif
-
-#include "mailutils/md5.h"
-
-#include <stddef.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/types.h>
-
-#if USE_UNLOCKED_IO
-# include "unlocked-io.h"
-#endif
-
-#ifdef _LIBC
-# include <endian.h>
-# if __BYTE_ORDER == __BIG_ENDIAN
-#  define WORDS_BIGENDIAN 1
-# endif
-#endif
-
-/* We need to keep the namespace clean so define the MD5 function
-   protected using leading __ .  */
-# define md5_init_ctx __md5_init_ctx
-# define md5_process_block __md5_process_block
-# define md5_process_bytes __md5_process_bytes
-# define md5_finish_ctx __md5_finish_ctx
-# define md5_read_ctx __md5_read_ctx
-# define md5_stream __md5_stream
-# define md5_buffer __md5_buffer
-
-#ifdef WORDS_BIGENDIAN
-# define SWAP(n)							\
-    (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24))
-#else
-# define SWAP(n) (n)
-#endif
-
-#define BLOCKSIZE 4096
-#if BLOCKSIZE % 64 != 0
-# error "invalid BLOCKSIZE"
-#endif
-
-/* This array contains the bytes used to pad the buffer to the next
-   64-byte boundary.  (RFC 1321, 3.1: Step 1)  */
-static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ...  */ };
-
-
-/* Initialize structure containing state of computation.
-   (RFC 1321, 3.3: Step 3)  */
-void
-md5_init_ctx (struct md5_ctx *ctx)
-{
-  ctx->A = 0x67452301;
-  ctx->B = 0xefcdab89;
-  ctx->C = 0x98badcfe;
-  ctx->D = 0x10325476;
-
-  ctx->total[0] = ctx->total[1] = 0;
-  ctx->buflen = 0;
-}
-
-/* Put result from CTX in first 16 bytes following RESBUF.  The result
-   must be in little endian byte order.
-
-   IMPORTANT: On some systems it is required that RESBUF is correctly
-   aligned for a 32-bit value.  */
-void *
-md5_read_ctx (const struct md5_ctx *ctx, void *resbuf)
-{
-  ((uint32_t *) resbuf)[0] = SWAP (ctx->A);
-  ((uint32_t *) resbuf)[1] = SWAP (ctx->B);
-  ((uint32_t *) resbuf)[2] = SWAP (ctx->C);
-  ((uint32_t *) resbuf)[3] = SWAP (ctx->D);
-
-  return resbuf;
-}
-
-/* Process the remaining bytes in the internal buffer and the usual
-   prolog according to the standard and write the result to RESBUF.
-
-   IMPORTANT: On some systems it is required that RESBUF is correctly
-   aligned for a 32-bit value.  */
-void *
-md5_finish_ctx (struct md5_ctx *ctx, void *resbuf)
-{
-  /* Take yet unprocessed bytes into account.  */
-  uint32_t bytes = ctx->buflen;
-  size_t size = (bytes < 56) ? 64 / 4 : 64 * 2 / 4;
-
-  /* Now count remaining bytes.  */
-  ctx->total[0] += bytes;
-  if (ctx->total[0] < bytes)
-    ++ctx->total[1];
-
-  /* Put the 64-bit file length in *bits* at the end of the buffer.  */
-  ctx->buffer[size - 2] = SWAP (ctx->total[0] << 3);
-  ctx->buffer[size - 1] = SWAP ((ctx->total[1] << 3) | (ctx->total[0] >> 29));
-
-  memcpy (&((char *) ctx->buffer)[bytes], fillbuf, (size - 2) * 4 - bytes);
-
-  /* Process last bytes.  */
-  md5_process_block (ctx->buffer, size * 4, ctx);
-
-  return md5_read_ctx (ctx, resbuf);
-}
-
-/* Compute MD5 message digest for bytes read from STREAM.  The
-   resulting message digest number will be written into the 16 bytes
-   beginning at RESBLOCK.  */
-int
-md5_stream (FILE *stream, void *resblock)
-{
-  struct md5_ctx ctx;
-  char buffer[BLOCKSIZE + 72];
-  size_t sum;
-
-  /* Initialize the computation context.  */
-  md5_init_ctx (&ctx);
-
-  /* Iterate over full file contents.  */
-  while (1)
-    {
-      /* We read the file in blocks of BLOCKSIZE bytes.  One call of the
-         computation function processes the whole buffer so that with the
-         next round of the loop another block can be read.  */
-      size_t n;
-      sum = 0;
-
-      /* Read block.  Take care for partial reads.  */
-      while (1)
-	{
-	  n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
-
-	  sum += n;
-
-	  if (sum == BLOCKSIZE)
-	    break;
-
-	  if (n == 0)
-	    {
-	      /* Check for the error flag IFF N == 0, so that we don't
-	         exit the loop after a partial read due to e.g., EAGAIN
-	         or EWOULDBLOCK.  */
-	      if (ferror (stream))
-		return 1;
-	      goto process_partial_block;
-	    }
-
-	  /* We've read at least one byte, so ignore errors.  But always
-	     check for EOF, since feof may be true even though N > 0.
-	     Otherwise, we could end up calling fread after EOF.  */
-	  if (feof (stream))
-	    goto process_partial_block;
-	}
-
-      /* Process buffer with BLOCKSIZE bytes.  Note that
-         BLOCKSIZE % 64 == 0
-       */
-      md5_process_block (buffer, BLOCKSIZE, &ctx);
-    }
-
-process_partial_block:
-
-  /* Process any remaining bytes.  */
-  if (sum > 0)
-    md5_process_bytes (buffer, sum, &ctx);
-
-  /* Construct result in desired memory.  */
-  md5_finish_ctx (&ctx, resblock);
-  return 0;
-}
-
-/* Compute MD5 message digest for LEN bytes beginning at BUFFER.  The
-   result is always in little endian byte order, so that a byte-wise
-   output yields to the wanted ASCII representation of the message
-   digest.  */
-void *
-md5_buffer (const char *buffer, size_t len, void *resblock)
-{
-  struct md5_ctx ctx;
-
-  /* Initialize the computation context.  */
-  md5_init_ctx (&ctx);
-
-  /* Process whole buffer but last len % 64 bytes.  */
-  md5_process_bytes (buffer, len, &ctx);
-
-  /* Put result in desired memory area.  */
-  return md5_finish_ctx (&ctx, resblock);
-}
-
-
-void
-md5_process_bytes (const void *buffer, size_t len, struct md5_ctx *ctx)
-{
-  /* When we already have some bits in our internal buffer concatenate
-     both inputs first.  */
-  if (ctx->buflen != 0)
-    {
-      size_t left_over = ctx->buflen;
-      size_t add = 128 - left_over > len ? len : 128 - left_over;
-
-      memcpy (&((char *) ctx->buffer)[left_over], buffer, add);
-      ctx->buflen += add;
-
-      if (ctx->buflen > 64)
-	{
-	  md5_process_block (ctx->buffer, ctx->buflen & ~63, ctx);
-
-	  ctx->buflen &= 63;
-	  /* The regions in the following copy operation cannot overlap.  */
-	  memcpy (ctx->buffer,
-		  &((char *) ctx->buffer)[(left_over + add) & ~63],
-		  ctx->buflen);
-	}
-
-      buffer = (const char *) buffer + add;
-      len -= add;
-    }
-
-  /* Process available complete blocks.  */
-  if (len >= 64)
-    {
-#if !_STRING_ARCH_unaligned
-# define alignof(type) offsetof (struct { char c; type x; }, x)
-# define UNALIGNED_P(p) (((size_t) p) % alignof (uint32_t) != 0)
-      if (UNALIGNED_P (buffer))
-	while (len > 64)
-	  {
-	    md5_process_block (memcpy (ctx->buffer, buffer, 64), 64, ctx);
-	    buffer = (const char *) buffer + 64;
-	    len -= 64;
-	  }
-      else
-#endif
-	{
-	  md5_process_block (buffer, len & ~63, ctx);
-	  buffer = (const char *) buffer + (len & ~63);
-	  len &= 63;
-	}
-    }
-
-  /* Move remaining bytes in internal buffer.  */
-  if (len > 0)
-    {
-      size_t left_over = ctx->buflen;
-
-      memcpy (&((char *) ctx->buffer)[left_over], buffer, len);
-      left_over += len;
-      if (left_over >= 64)
-	{
-	  md5_process_block (ctx->buffer, 64, ctx);
-	  left_over -= 64;
-	  memcpy (ctx->buffer, &ctx->buffer[16], left_over);
-	}
-      ctx->buflen = left_over;
-    }
-}
-
-
-/* These are the four functions used in the four steps of the MD5 algorithm
-   and defined in the RFC 1321.  The first function is a little bit optimized
-   (as found in Colin Plumbs public domain implementation).  */
-/* #define FF(b, c, d) ((b & c) | (~b & d)) */
-#define FF(b, c, d) (d ^ (b & (c ^ d)))
-#define FG(b, c, d) FF (d, b, c)
-#define FH(b, c, d) (b ^ c ^ d)
-#define FI(b, c, d) (c ^ (b | ~d))
-
-/* Process LEN bytes of BUFFER, accumulating context into CTX.
-   It is assumed that LEN % 64 == 0.  */
-
-void
-md5_process_block (const void *buffer, size_t len, struct md5_ctx *ctx)
-{
-  uint32_t correct_words[16];
-  const uint32_t *words = buffer;
-  size_t nwords = len / sizeof (uint32_t);
-  const uint32_t *endp = words + nwords;
-  uint32_t A = ctx->A;
-  uint32_t B = ctx->B;
-  uint32_t C = ctx->C;
-  uint32_t D = ctx->D;
-
-  /* First increment the byte count.  RFC 1321 specifies the possible
-     length of the file up to 2^64 bits.  Here we only compute the
-     number of bytes.  Do a double word increment.  */
-  ctx->total[0] += len;
-  if (ctx->total[0] < len)
-    ++ctx->total[1];
-
-  /* Process all bytes in the buffer with 64 bytes in each round of
-     the loop.  */
-  while (words < endp)
-    {
-      uint32_t *cwp = correct_words;
-      uint32_t A_save = A;
-      uint32_t B_save = B;
-      uint32_t C_save = C;
-      uint32_t D_save = D;
-
-      /* First round: using the given function, the context and a constant
-         the next context is computed.  Because the algorithms processing
-         unit is a 32-bit word and it is determined to work on words in
-         little endian byte order we perhaps have to change the byte order
-         before the computation.  To reduce the work for the next steps
-         we store the swapped words in the array CORRECT_WORDS.  */
-
-#define OP(a, b, c, d, s, T)						\
-      do								\
-        {								\
-	  a += FF (b, c, d) + (*cwp++ = SWAP (*words)) + T;		\
-	  ++words;							\
-	  CYCLIC (a, s);						\
-	  a += b;							\
-        }								\
-      while (0)
-
-      /* It is unfortunate that C does not provide an operator for
-         cyclic rotation.  Hope the C compiler is smart enough.  */
-#define CYCLIC(w, s) (w = (w << s) | (w >> (32 - s)))
-
-      /* Before we start, one word to the strange constants.
-         They are defined in RFC 1321 as
-
-         T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64
-
-         Here is an equivalent invocation using Perl:
-
-         perl -e 'foreach(1..64){printf "0x%08x\n", int (4294967296 * abs (sin $_))}'
-       */
-
-      /* Round 1.  */
-      OP (A, B, C, D, 7, 0xd76aa478);
-      OP (D, A, B, C, 12, 0xe8c7b756);
-      OP (C, D, A, B, 17, 0x242070db);
-      OP (B, C, D, A, 22, 0xc1bdceee);
-      OP (A, B, C, D, 7, 0xf57c0faf);
-      OP (D, A, B, C, 12, 0x4787c62a);
-      OP (C, D, A, B, 17, 0xa8304613);
-      OP (B, C, D, A, 22, 0xfd469501);
-      OP (A, B, C, D, 7, 0x698098d8);
-      OP (D, A, B, C, 12, 0x8b44f7af);
-      OP (C, D, A, B, 17, 0xffff5bb1);
-      OP (B, C, D, A, 22, 0x895cd7be);
-      OP (A, B, C, D, 7, 0x6b901122);
-      OP (D, A, B, C, 12, 0xfd987193);
-      OP (C, D, A, B, 17, 0xa679438e);
-      OP (B, C, D, A, 22, 0x49b40821);
-
-      /* For the second to fourth round we have the possibly swapped words
-         in CORRECT_WORDS.  Redefine the macro to take an additional first
-         argument specifying the function to use.  */
-#undef OP
-#define OP(f, a, b, c, d, k, s, T)					\
-      do								\
-	{								\
-	  a += f (b, c, d) + correct_words[k] + T;			\
-	  CYCLIC (a, s);						\
-	  a += b;							\
-	}								\
-      while (0)
-
-      /* Round 2.  */
-      OP (FG, A, B, C, D, 1, 5, 0xf61e2562);
-      OP (FG, D, A, B, C, 6, 9, 0xc040b340);
-      OP (FG, C, D, A, B, 11, 14, 0x265e5a51);
-      OP (FG, B, C, D, A, 0, 20, 0xe9b6c7aa);
-      OP (FG, A, B, C, D, 5, 5, 0xd62f105d);
-      OP (FG, D, A, B, C, 10, 9, 0x02441453);
-      OP (FG, C, D, A, B, 15, 14, 0xd8a1e681);
-      OP (FG, B, C, D, A, 4, 20, 0xe7d3fbc8);
-      OP (FG, A, B, C, D, 9, 5, 0x21e1cde6);
-      OP (FG, D, A, B, C, 14, 9, 0xc33707d6);
-      OP (FG, C, D, A, B, 3, 14, 0xf4d50d87);
-      OP (FG, B, C, D, A, 8, 20, 0x455a14ed);
-      OP (FG, A, B, C, D, 13, 5, 0xa9e3e905);
-      OP (FG, D, A, B, C, 2, 9, 0xfcefa3f8);
-      OP (FG, C, D, A, B, 7, 14, 0x676f02d9);
-      OP (FG, B, C, D, A, 12, 20, 0x8d2a4c8a);
-
-      /* Round 3.  */
-      OP (FH, A, B, C, D, 5, 4, 0xfffa3942);
-      OP (FH, D, A, B, C, 8, 11, 0x8771f681);
-      OP (FH, C, D, A, B, 11, 16, 0x6d9d6122);
-      OP (FH, B, C, D, A, 14, 23, 0xfde5380c);
-      OP (FH, A, B, C, D, 1, 4, 0xa4beea44);
-      OP (FH, D, A, B, C, 4, 11, 0x4bdecfa9);
-      OP (FH, C, D, A, B, 7, 16, 0xf6bb4b60);
-      OP (FH, B, C, D, A, 10, 23, 0xbebfbc70);
-      OP (FH, A, B, C, D, 13, 4, 0x289b7ec6);
-      OP (FH, D, A, B, C, 0, 11, 0xeaa127fa);
-      OP (FH, C, D, A, B, 3, 16, 0xd4ef3085);
-      OP (FH, B, C, D, A, 6, 23, 0x04881d05);
-      OP (FH, A, B, C, D, 9, 4, 0xd9d4d039);
-      OP (FH, D, A, B, C, 12, 11, 0xe6db99e5);
-      OP (FH, C, D, A, B, 15, 16, 0x1fa27cf8);
-      OP (FH, B, C, D, A, 2, 23, 0xc4ac5665);
-
-      /* Round 4.  */
-      OP (FI, A, B, C, D, 0, 6, 0xf4292244);
-      OP (FI, D, A, B, C, 7, 10, 0x432aff97);
-      OP (FI, C, D, A, B, 14, 15, 0xab9423a7);
-      OP (FI, B, C, D, A, 5, 21, 0xfc93a039);
-      OP (FI, A, B, C, D, 12, 6, 0x655b59c3);
-      OP (FI, D, A, B, C, 3, 10, 0x8f0ccc92);
-      OP (FI, C, D, A, B, 10, 15, 0xffeff47d);
-      OP (FI, B, C, D, A, 1, 21, 0x85845dd1);
-      OP (FI, A, B, C, D, 8, 6, 0x6fa87e4f);
-      OP (FI, D, A, B, C, 15, 10, 0xfe2ce6e0);
-      OP (FI, C, D, A, B, 6, 15, 0xa3014314);
-      OP (FI, B, C, D, A, 13, 21, 0x4e0811a1);
-      OP (FI, A, B, C, D, 4, 6, 0xf7537e82);
-      OP (FI, D, A, B, C, 11, 10, 0xbd3af235);
-      OP (FI, C, D, A, B, 2, 15, 0x2ad7d2bb);
-      OP (FI, B, C, D, A, 9, 21, 0xeb86d391);
-
-      /* Add the starting values of the context.  */
-      A += A_save;
-      B += B_save;
-      C += C_save;
-      D += D_save;
-    }
-
-  /* Put checksum in context given as argument.  */
-  ctx->A = A;
-  ctx->B = B;
-  ctx->C = C;
-  ctx->D = D;
-}
diff --git a/libmailutils/message.c b/libmailutils/message.c
deleted file mode 100644
index caeba8f..0000000
--- a/libmailutils/message.c
+++ /dev/null
@@ -1,1294 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005, 2006, 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 <errno.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <time.h>
-#include <unistd.h>
-#include <string.h>
-#include <pwd.h>
-
-#include <mailutils/cctype.h>
-#include <mailutils/address.h>
-#include <mailutils/attribute.h>
-#include <mailutils/auth.h>
-#include <mailutils/body.h>
-#include <mailutils/debug.h>
-#include <mailutils/envelope.h>
-#include <mailutils/errno.h>
-#include <mailutils/folder.h>
-#include <mailutils/header.h>
-#include <mailutils/mailbox.h>
-#include <mailutils/mutil.h>
-#include <mailutils/observer.h>
-#include <mailutils/stream.h>
-#include <mailutils/mu_auth.h>
-#include <mailutils/nls.h>
-#include <mailutils/md5.h>
-#include <mailutils/io.h>
-
-#include <mailutils/sys/message.h>
-#include <mailutils/sys/stream.h>
-
-#define MESSAGE_MODIFIED        0x10000
-#define MESSAGE_INTERNAL_STREAM 0x20000
-
-
-/* Message stream */
-
-enum _message_stream_state
-  {
-    _mss_init,
-    _mss_header,
-    _mss_body,
-    _mss_eof
-  };
-
-struct _mu_message_stream
-{
-  struct _mu_stream stream;
-  mu_message_t msg;
-  enum _message_stream_state state;
-  mu_stream_t transport;
-  mu_off_t limit;
-};
-
-static int
-_check_stream_state (struct _mu_message_stream *str)
-{
-  int rc = 0;
-  
-  if (str->transport && mu_stream_eof (str->transport))
-    mu_stream_destroy (&str->transport);
-  
-  switch (str->state)
-    {
-    case _mss_init:
-      if (!str->transport)
-	{
-	  rc = mu_header_get_streamref (str->msg->header, &str->transport);
-	  if (rc == 0)
-	    {
-	      str->state = _mss_header;
-	      rc = mu_stream_seek (str->transport, 0, MU_SEEK_SET, NULL);
-	    }
-	}
-      break;
-      
-    case _mss_header:
-      if (!str->transport)
-	{
-	  rc = mu_body_get_streamref (str->msg->body, &str->transport);
-	  if (rc == 0)
-	    {
-	      str->state = _mss_body;
-	      rc = mu_stream_seek (str->transport, 0, MU_SEEK_SET, NULL);
-	    }
-	}
-      break;
-      
-    case _mss_body:
-      if (!str->transport)
-	str->state = _mss_eof;
-    case _mss_eof:
-      break;
-    }
-  return rc;
-}
-
-static void
-_message_stream_done (struct _mu_stream *str)
-{
-  struct _mu_message_stream *sp = (struct _mu_message_stream *)str;
-  mu_stream_destroy (&sp->transport);
-}
-
-static int
-_message_stream_flush (struct _mu_stream *str)
-{
-  struct _mu_message_stream *sp = (struct _mu_message_stream *)str;
-  int rc = _check_stream_state (sp);
-  if (rc)
-    return rc;
-  return mu_stream_flush (sp->transport);
-}
-  
-static int
-_message_stream_size (struct _mu_stream *str, mu_off_t *psize)
-{
-  struct _mu_message_stream *sp = (struct _mu_message_stream *)str;
-  size_t hsize, bsize;
-  mu_header_size (sp->msg->header, &hsize);
-  mu_body_size (sp->msg->body, &bsize);
-  if (psize)
-    *psize = hsize + bsize;
-  return 0;
-}
-
-static int
-_message_stream_seek (struct _mu_stream *str, mu_off_t off, mu_off_t *ppos)
-{
-  struct _mu_message_stream *sp = (struct _mu_message_stream *)str;
-  size_t hsize, size;
-  int rc;
-  
-  rc = _check_stream_state (sp);
-  if (rc)
-    return rc;
-  mu_header_size (sp->msg->header, &hsize);
-  mu_body_size (sp->msg->body, &size);
-  size += hsize;
-  
-  if (off < 0 || off >= size)
-    return ESPIPE;
-
-  switch (sp->state)
-    {
-    case _mss_eof:
-      sp->state = _mss_init;
-      rc = _check_stream_state (sp);
-      if (rc)
-	return rc;
-      /* fall through */
-    case _mss_header:
-      if (off < hsize)
-	break;
-      mu_stream_destroy (&sp->transport);
-      rc = _check_stream_state (sp);
-      if (rc)
-	return rc;
-      /* fall through */
-    case _mss_body:
-      if (off > hsize)
-	off -= hsize;	
-      else
-	{
-	  mu_stream_destroy (&sp->transport);
-	  sp->state = _mss_init;
-	  rc = _check_stream_state (sp);
-	  if (rc)
-	    return rc;
-	}
-
-      break;
-
-    default:
-      break;
-    }
-  rc = mu_stream_seek (sp->transport, off, MU_SEEK_SET, &off);
-  if (rc == 0)
-    {
-      if (sp->state == _mss_body)
-	off += hsize;
-      *ppos = off;
-    }
-  return rc;
-}
-
-static int
-_message_stream_read (struct _mu_stream *str, char *buf, size_t bufsize,
-		      size_t *pnread)
-{
-  struct _mu_message_stream *sp = (struct _mu_message_stream *)str;
-  size_t nread = 0;
-  int rc;
-  
-  while (bufsize)
-    {
-      size_t n;
-      rc = _check_stream_state (sp);
-      if (rc)
-	break;
-      if (sp->state == _mss_eof)
-	break;
-      rc = mu_stream_read (sp->transport, buf, bufsize, &n);
-      nread += n;
-      buf += n;
-      bufsize -= n;
-    }
-  *pnread = nread;
-  return rc;
-}
-
-static int
-_message_stream_readdelim (struct _mu_stream *str, char *buf, size_t bufsize,
-			   int delim, size_t *pnread)
-{
-  struct _mu_message_stream *sp = (struct _mu_message_stream *)str;
-  size_t nread = 0;
-  int rc;
-  
-  while (bufsize)
-    {
-      size_t n;
-      rc = _check_stream_state (sp);
-      if (rc)
-	break;
-      if (sp->state == _mss_eof)
-	break;
-      rc = mu_stream_readdelim (sp->transport, buf, bufsize, delim, &n);
-      if (rc || n == 0)
-	break;
-      nread += n;
-      buf += n;
-      bufsize -= n;
-    }
-  *pnread = nread;
-  return rc;
-}  
-
-#if 0
-static int
-_message_stream_write (struct _mu_stream *str,
-		       const char *buf, size_t bufsize,
-		       size_t *pnwritten)
-{
-  struct _mu_message_stream *sp = (struct _mu_message_stream *)str;
-  
-  /* FIXME */
-}
-#endif
-
-static int
-_message_stream_create (mu_stream_t *pmsg, mu_message_t msg, int flags)
-{
-  struct _mu_message_stream *sp;
-
-  sp = (struct _mu_message_stream *) _mu_stream_create (sizeof (*sp),
-							flags |
-							MU_STREAM_SEEK |
-							_MU_STR_OPEN);
-  if (!sp)
-    return ENOMEM;
-
-  sp->stream.read = _message_stream_read;
-  sp->stream.readdelim = _message_stream_readdelim;
-  /* FIXME: Write is not defined */
-  /*  sp->stream.write = _message_stream_write;*/
-  sp->stream.done = _message_stream_done;
-  sp->stream.flush = _message_stream_flush;
-  sp->stream.seek = _message_stream_seek; 
-  sp->stream.size = _message_stream_size;
-  sp->state = _mss_init;
-  sp->msg = msg;
-  *pmsg = (mu_stream_t) sp;
-  return 0;
-}
-
-
-enum eoh_state
-  {
-    eoh_no,
-    eoh_maybe,
-    eoh_yes
-  };
-
-/* Message header stuff */
-static enum eoh_state
-string_find_eoh (enum eoh_state eoh, const char *str, size_t len,
-		 size_t *ppos)
-{
-  size_t pos;
-
-  if (eoh == eoh_maybe && *str == '\n')
-    {
-      *ppos = 0;
-      return eoh_yes;
-    }
-  
-  for (pos = 0; pos < len - 1; pos++)
-    if (str[pos] == '\n' && str[pos + 1] == '\n')
-      {
-	*ppos = pos + 1;
-	return eoh_yes;
-      }
-  
-  *ppos = pos + 1;
-  return str[pos] == '\n' ? eoh_maybe : eoh_no;
-}
-
-#define MIN_HEADER_BUF_SIZE 2048
-
-static int
-_header_fill (mu_stream_t stream, char **pbuf, size_t *plen)
-{
-  int status = 0;
-  char *buffer = NULL;
-  size_t bufsize = 0;
-  char inbuf[MIN_HEADER_BUF_SIZE];
-  size_t nread;
-  enum eoh_state eoh = eoh_no;
-  
-  status = mu_stream_seek (stream, 0, MU_SEEK_SET, NULL);
-  if (status)
-    return status;
-      
-  while (eoh != eoh_yes
-	 && (status = mu_stream_read (stream, inbuf, sizeof (inbuf), &nread))
-	    == 0
-	 && nread)
-    {
-      char *nbuf;
-      size_t len;
-
-      eoh = string_find_eoh (eoh, inbuf, nread, &len);
-      
-      nbuf = realloc (buffer, bufsize + len);
-      if (!nbuf)
-	{
-	  status = ENOMEM;
-	  break;
-	}
-      memcpy (nbuf + bufsize, inbuf, len);
-      buffer = nbuf;
-      bufsize += len;
-    }
-
-  if (status)
-    free (buffer);
-  else
-    {
-      *pbuf = buffer;
-      *plen = bufsize;
-    }
-  return status;
-}
-    
-static int
-message_header_fill (void *data, char **pbuf, size_t *plen)
-{
-  int status = 0;
-  mu_message_t msg = data;
-  mu_stream_t stream;
-
-  status = mu_message_get_streamref (msg, &stream);
-  if (status == 0)
-    {
-      status = _header_fill (stream, pbuf, plen);
-      mu_stream_destroy (&stream);
-    }
-  return status;
-}
-
-
-/* Message envelope */
-static int
-message_envelope_date (mu_envelope_t envelope, char *buf, size_t len,
-		       size_t *pnwrite)
-{
-  mu_message_t msg = mu_envelope_get_owner (envelope);
-  time_t t;
-  size_t n;
-
-  if (msg == NULL)
-    return EINVAL;
-
-  /* FIXME: extract the time from "Date:".  */
-
-  if (buf == NULL || len == 0)
-    {
-      n = MU_ENVELOPE_DATE_LENGTH;
-    }
-  else
-    {
-      char tmpbuf[MU_ENVELOPE_DATE_LENGTH+1];
-      t = time (NULL);
-      n = mu_strftime (tmpbuf, sizeof tmpbuf, 
-                       MU_ENVELOPE_DATE_FORMAT, localtime (&t));
-      n = mu_cpystr (buf, tmpbuf, len);
-    }
-  if (pnwrite)
-    *pnwrite = n;
-  return 0;
-}
-
-static int
-message_envelope_sender (mu_envelope_t envelope, char *buf, size_t len,
-			 size_t *pnwrite)
-{
-  mu_message_t msg = mu_envelope_get_owner (envelope);
-  mu_header_t header = NULL;
-  size_t n = 0;
-  int status;
-
-  if (msg == NULL)
-    return EINVAL;
-
-  /* Can it be extracted from the From:  */
-  mu_message_get_header (msg, &header);
-  status = mu_header_get_value (header, MU_HEADER_FROM, NULL, 0, &n);
-  if (status == 0 && n != 0)
-    {
-      char *sender;
-      mu_address_t address = NULL;
-      sender = calloc (1, n + 1);
-      if (sender == NULL)
-	return ENOMEM;
-      mu_header_get_value (header, MU_HEADER_FROM, sender, n + 1, NULL);
-      if (mu_address_create (&address, sender) == 0)
-	mu_address_get_email (address, 1, buf, n + 1, pnwrite);
-      free (sender);
-      mu_address_destroy (&address);
-      return 0;
-    }
-  else if (status == EAGAIN)
-    return status;
-
-  /* oops! We are still here */
-  {
-    struct mu_auth_data *auth = mu_get_auth_by_uid (getuid ());
-    const char *sender = auth ? auth->name : "unknown";
-    n = strlen (sender);
-    if (buf && len > 0)
-      {
-	len--; /* One for the null.  */
-	n = (n < len) ? n : len;
-	memcpy (buf, auth->name, n);
-	buf[n] = '\0';
-      }
-    if (auth)
-      mu_auth_data_free (auth);
-  }
-
-  if (pnwrite)
-    *pnwrite = n;
-  return 0;
-}
-
-
-
-/*  Allocate ressources for the mu_message_t.  */
-int
-mu_message_create (mu_message_t *pmsg, void *owner)
-{
-  mu_message_t msg;
-  int status;
-
-  if (pmsg == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-  msg = calloc (1, sizeof (*msg));
-  if (msg == NULL)
-    return ENOMEM;
-  status = mu_monitor_create (&msg->monitor, 0, msg);
-  if (status != 0)
-    {
-      free (msg);
-      return status;
-    }
-  msg->owner = owner;
-  msg->ref = 1;
-  *pmsg = msg;
-  return 0;
-}
-
-void
-mu_message_destroy (mu_message_t *pmsg, void *owner)
-{
-  if (pmsg && *pmsg)
-    {
-      mu_message_t msg = *pmsg;
-      mu_monitor_t monitor = msg->monitor;
-      int destroy_lock = 0;
-
-      mu_monitor_wrlock (monitor);
-      /* Note: msg->ref may be incremented by mu_message_ref without
-	 additional checking for its owner, therefore decrementing
-	 it must also occur independently of the owner checking. Due
-	 to this inconsistency ref may reach negative values, which
-	 is very unfortunate.
-
-	 The `owner' stuff is a leftover from older mailutils versions.
-	 There is an ongoing attempt to remove it in the stream-cleanup
-	 branch. When it is ready, it will be merged to the HEAD and this
-	 will finally resolve this issue. */
-      if (msg->ref > 0)
-	msg->ref--;
-      if ((msg->owner && msg->owner == owner)
-	  || (msg->owner == NULL && msg->ref <= 0))
-	{
-	  destroy_lock =  1;
-	  /* Notify the listeners.  */
-	  /* FIXME: to be removed since we do not support this event.  */
-	  if (msg->observable)
-	    {
-	      mu_observable_notify (msg->observable, MU_EVT_MESSAGE_DESTROY,
-				    msg);
-	      mu_observable_destroy (&msg->observable, msg);
-	    }
-
-	  /* Envelope.  */
-	  if (msg->envelope)
-	    mu_envelope_destroy (&msg->envelope, msg);
-
-	  /* Header.  */
-	  if (msg->header)
-	    mu_header_destroy (&msg->header);
-
-	  /* Body.  */
-	  if (msg->body)
-	    mu_body_destroy (&msg->body, msg);
-
-	  /* Attribute.  */
-	  if (msg->attribute)
-	    mu_attribute_destroy (&msg->attribute, msg);
-
-	  /* Stream.  */
-	  if (msg->stream)
-	    mu_stream_destroy (&msg->stream);
-
-	  /*  Mime.  */
-	  if (msg->mime)
-	    mu_mime_destroy (&msg->mime);
-
-	  /* Loose the owner.  */
-	  msg->owner = NULL;
-
-	  free (msg);
-	}
-      mu_monitor_unlock (monitor);
-      if (destroy_lock)
-	mu_monitor_destroy (&monitor, msg);
-      /* Loose the link */
-      *pmsg = NULL;
-    }
-}
-
-int
-mu_message_create_copy (mu_message_t *to, mu_message_t from)
-{
-  int status = 0;
-  mu_stream_t fromstr = NULL;
-  mu_stream_t tmp = NULL;
-
-  if (!to)
-    return MU_ERR_OUT_PTR_NULL;
-  if (!from)
-    return EINVAL;
-
-  status = mu_memory_stream_create (&tmp, MU_STREAM_RDWR|MU_STREAM_SEEK);
-  if (status)
-    return status;
-
-  status = mu_message_get_streamref (from, &fromstr);
-  if (status)
-    {
-      mu_stream_destroy (&tmp);
-      return status;
-    }
-
-  status = mu_stream_copy (tmp, fromstr, 0, NULL);
-  if (status == 0)
-    {
-      status = mu_message_create (to, NULL);
-      if (status == 0)
-	mu_message_set_stream (*to, tmp, NULL);
-    }
-
-  if (status)
-    mu_stream_destroy (&tmp);
-  mu_stream_destroy (&fromstr);
-
-  return status;
-}
-
-int
-mu_message_ref (mu_message_t msg)
-{
-  if (msg)
-    {
-      mu_monitor_wrlock (msg->monitor);
-      msg->ref++;
-      mu_monitor_unlock (msg->monitor);
-    }
-  return 0;
-}
-
-void *
-mu_message_get_owner (mu_message_t msg)
-{
-  return (msg == NULL) ? NULL : msg->owner;
-}
-
-int
-mu_message_is_modified (mu_message_t msg)
-{
-  int mod = 0;
-  if (msg)
-    {
-      mod |= mu_header_is_modified (msg->header);
-      mod |= mu_attribute_is_modified (msg->attribute);
-      mod |= mu_body_is_modified (msg->body);
-      mod |= msg->flags;
-    }
-  return mod;
-}
-
-int
-mu_message_clear_modified (mu_message_t msg)
-{
-  if (msg)
-    {
-      if (msg->header)
-	mu_header_clear_modified (msg->header);
-      if (msg->attribute)
-	mu_attribute_clear_modified (msg->attribute);
-      if (msg->body)
-	mu_body_clear_modified (msg->body);
-      msg->flags &= ~MESSAGE_MODIFIED;
-    }
-  return 0;
-}
-
-int
-mu_message_get_mailbox (mu_message_t msg, mu_mailbox_t *pmailbox)
-{
-  if (msg == NULL)
-    return EINVAL;
-  if (pmailbox == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-  *pmailbox = msg->mailbox;
-  return 0;
-}
-
-int
-mu_message_set_mailbox (mu_message_t msg, mu_mailbox_t mailbox, void *owner)
-{
-  if (msg == NULL)
-    return EINVAL;
-  if (msg->owner != owner)
-    return EACCES;
-  msg->mailbox = mailbox;
-  return 0;
-}
-
-int
-mu_message_get_header (mu_message_t msg, mu_header_t *phdr)
-{
-  if (msg == NULL)
-    return EINVAL;
-  if (phdr == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-
-  if (msg->header == NULL)
-    {
-      mu_header_t header;
-      int status = mu_header_create (&header, NULL, 0);
-      if (status != 0)
-	return status;
-      if (msg->stream)
-	mu_header_set_fill (header, message_header_fill, msg);
-      status = mu_header_size (header, &msg->orig_header_size);
-      if (status)
-	return status;
-      msg->header = header;
-    }
-  *phdr = msg->header;
-  return 0;
-}
-
-/* Note: mu_message_set_header steals the reference to hdr */
-int
-mu_message_set_header (mu_message_t msg, mu_header_t hdr, void *owner)
-{
-  if (msg == NULL )
-    return EINVAL;
-  if (msg->owner != owner)
-     return EACCES;
-  if (msg->header)
-    mu_header_destroy (&msg->header);
-  msg->header = hdr;
-  msg->flags |= MESSAGE_MODIFIED;
-  return 0;
-}
-
-int
-mu_message_get_body (mu_message_t msg, mu_body_t *pbody)
-{
-  if (msg == NULL)
-    return EINVAL;
-  if (pbody == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-
-  /* Is it a floating mesg.  */
-  if (msg->body == NULL)
-    {
-      mu_body_t body;
-      int status = mu_body_create (&body, msg);
-      if (status != 0)
-	return status;
-      /* If a stream is already set, use it to create the body stream.  */
-      /* FIXME: I'm not sure if the second condition is really needed */
-      if (msg->stream/* && (msg->flags & MESSAGE_INTERNAL_STREAM)*/)
-	{
-	  mu_stream_t stream;
-	  int flags = 0;
-
-	  /* FIXME: The actual mu_header_size cannot be used as offset,
-	     because the headers might have been modified in between. */
-	  
-	  mu_stream_get_flags (msg->stream, &flags);
-	  status = mu_streamref_create_abridged (&stream, msg->stream,
-						 msg->orig_header_size, 0);
-	  if (status)
-	    {
-	      mu_body_destroy (&body, msg);
-	      return status;
-	    }
-	  mu_body_set_stream (body, stream, msg);
-	}
-      msg->body = body;
-    }
-  *pbody = msg->body;
-  return 0;
-}
-
-int
-mu_message_set_body (mu_message_t msg, mu_body_t body, void *owner)
-{
-  if (msg == NULL )
-    return EINVAL;
-  if (msg->owner != owner)
-    return EACCES;
-  /* Make sure we destroy the old if it was owned by the mesg.  */
-  /* FIXME:  I do not know if somebody has already a ref on this ? */
-  if (msg->body)
-    mu_body_destroy (&msg->body, msg);
-  msg->body = body;
-  msg->flags |= MESSAGE_MODIFIED;
-  return 0;
-}
-
-int
-mu_message_set_stream (mu_message_t msg, mu_stream_t stream, void *owner)
-{
-  if (msg == NULL)
-    return EINVAL;
-  if (msg->owner != owner)
-    return EACCES;
-  if (msg->stream)
-    mu_stream_destroy (&msg->stream);
-  msg->stream = stream;
-  msg->flags |= MESSAGE_MODIFIED;
-  msg->flags &= ~MESSAGE_INTERNAL_STREAM;
-  return 0;
-}
-
-static int
-_message_get_stream (mu_message_t msg, mu_stream_t *pstream, int ref)
-{
-  int status;
-
-  if (msg == NULL)
-    return EINVAL;
-  if (pstream == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-
-  if (msg->stream == NULL)
-    {
-      if (msg->_get_stream)
-	{
-	  status = msg->_get_stream (msg, &msg->stream);
-	  if (status)
-	    return status;
-	}
-      else
-	{
-	  mu_header_t hdr;
-	  mu_body_t body;
-
-	  /* FIXME: Kind of a kludge: make sure the message has header
-	     and body initialized. */
-	  status = mu_message_get_header (msg, &hdr);
-	  if (status)
-	    return status;
-	  status = mu_message_get_body (msg, &body);
-	  if (status)
-	    return status;
-	  
-	  status = _message_stream_create (&msg->stream, msg, MU_STREAM_RDWR);
-	  if (status)
-	    return status;
-	  msg->flags |= MESSAGE_INTERNAL_STREAM;
-	}
-    }
-  
-  if (!ref)
-    {
-      *pstream = msg->stream;
-      return 0;
-    }
-  return mu_streamref_create (pstream, msg->stream);
-}
-
-int
-mu_message_get_stream (mu_message_t msg, mu_stream_t *pstream)
-{
-  /* FIXME: Deprecation warning */
-  return _message_get_stream (msg, pstream, 0);
-}
-
-int
-mu_message_get_streamref (mu_message_t msg, mu_stream_t *pstream)
-{
-  return _message_get_stream (msg, pstream, 1);
-}
-
-int
-mu_message_set_get_stream (mu_message_t msg,
-			   int (*_getstr) (mu_message_t, mu_stream_t *),
-			   void *owner)
-{
-  if (msg == NULL)
-    return EINVAL;
-  if (msg->owner != owner)
-    return EACCES;
-  msg->_get_stream = _getstr;
-  return 0;
-}
-
-int
-mu_message_set_lines (mu_message_t msg, int (*_lines)
-		   (mu_message_t, size_t *), void *owner)
-{
-  if (msg == NULL)
-    return EINVAL;
-  if (msg->owner != owner)
-    return EACCES;
-  msg->_lines = _lines;
-  return 0;
-}
-
-int
-mu_message_lines (mu_message_t msg, size_t *plines)
-{
-  size_t hlines, blines;
-  int ret = 0;
-
-  if (msg == NULL)
-    return EINVAL;
-  /* Overload.  */
-  if (msg->_lines)
-    return msg->_lines (msg, plines);
-  if (plines)
-    {
-      hlines = blines = 0;
-      if ( ( ret = mu_header_lines (msg->header, &hlines) ) == 0 )
-	      ret = mu_body_lines (msg->body, &blines);
-      *plines = hlines + blines;
-    }
-  return ret;
-}
-
-int
-mu_message_set_size (mu_message_t msg, int (*_size)
-		  (mu_message_t, size_t *), void *owner)
-{
-  if (msg == NULL)
-    return EINVAL;
-  if (msg->owner != owner)
-    return EACCES;
-  msg->_size = _size;
-  return 0;
-}
-
-int
-mu_message_size (mu_message_t msg, size_t *psize)
-{
-  size_t hsize, bsize;
-  int ret = 0;
-
-  if (msg == NULL)
-    return EINVAL;
-  /* Overload ? */
-  if (msg->_size)
-    return msg->_size (msg, psize);
-  if (psize)
-    {
-      mu_header_t hdr = NULL;
-      mu_body_t body = NULL;
-      
-      hsize = bsize = 0;
-      mu_message_get_header (msg, &hdr);
-      mu_message_get_body (msg, &body);
-      if ( ( ret = mu_header_size (hdr, &hsize) ) == 0 )
-	ret = mu_body_size (body, &bsize);
-      *psize = hsize + bsize;
-    }
-  return ret;
-}
-
-int
-mu_message_get_envelope (mu_message_t msg, mu_envelope_t *penvelope)
-{
-  if (msg == NULL)
-    return EINVAL;
-  if (penvelope == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-
-  if (msg->envelope == NULL)
-    {
-      mu_envelope_t envelope;
-      int status = mu_envelope_create (&envelope, msg);
-      if (status != 0)
-	return status;
-      mu_envelope_set_sender (envelope, message_envelope_sender, msg);
-      mu_envelope_set_date (envelope, message_envelope_date, msg);
-      msg->envelope = envelope;
-    }
-  *penvelope = msg->envelope;
-  return 0;
-}
-
-int
-mu_message_set_envelope (mu_message_t msg, mu_envelope_t envelope, void *owner)
-{
-  if (msg == NULL)
-    return EINVAL;
-  if (msg->owner != owner)
-    return EACCES;
-  if (msg->envelope)
-    mu_envelope_destroy (&msg->envelope, msg);
-  msg->envelope = envelope;
-  msg->flags |= MESSAGE_MODIFIED;
-  return 0;
-}
-
-int
-mu_message_get_attribute (mu_message_t msg, mu_attribute_t *pattribute)
-{
-  if (msg == NULL)
-    return EINVAL;
-  if (pattribute == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-  if (msg->attribute == NULL)
-    {
-      mu_attribute_t attribute;
-      int status = mu_attribute_create (&attribute, msg);
-      if (status != 0)
-	return status;
-      msg->attribute = attribute;
-    }
-  *pattribute = msg->attribute;
-  return 0;
-}
-
-int
-mu_message_set_attribute (mu_message_t msg, mu_attribute_t attribute, void *owner)
-{
-  if (msg == NULL)
-   return EINVAL;
-  if (msg->owner != owner)
-    return EACCES;
-  if (msg->attribute)
-    mu_attribute_destroy (&msg->attribute, owner);
-  msg->attribute = attribute;
-  msg->flags |= MESSAGE_MODIFIED;
-  return 0;
-}
-
-int
-mu_message_get_uid (mu_message_t msg, size_t *puid)
-{
-  if (msg == NULL)
-    return EINVAL;
-  if (msg->_get_uid)
-    return msg->_get_uid (msg, puid);
-  *puid = 0;
-  return 0;
-}
-
-int
-mu_message_get_uidl (mu_message_t msg, char *buffer, size_t buflen,
-		     size_t *pwriten)
-{
-  mu_header_t header = NULL;
-  size_t n = 0;
-  int status;
-
-  if (msg == NULL || buffer == NULL || buflen == 0)
-    return EINVAL;
-
-  buffer[0] = '\0';
-  /* Try the function overload if error fallback.  */
-  if (msg->_get_uidl)
-    {
-      status = msg->_get_uidl (msg, buffer, buflen, pwriten);
-      if (status == 0)
-	return status;
-    }
-
-  /* Be compatible with Qpopper ? qppoper saves the UIDL in "X-UIDL".
-     We generate a chksum and save it in the header.  */
-  mu_message_get_header (msg, &header);
-  status = mu_header_get_value_unfold (header, "X-UIDL", buffer, buflen, &n);
-  if (status != 0 || n == 0)
-    {
-      size_t uid = 0;
-      struct mu_md5_ctx md5context;
-      mu_stream_t stream = NULL;
-      char buf[1024];
-      unsigned char md5digest[16];
-      char *tmp;
-      n = 0;
-      mu_message_get_uid (msg, &uid);
-      mu_message_get_streamref (msg, &stream);
-      mu_md5_init_ctx (&md5context);
-      status = mu_stream_seek (stream, 0, MU_SEEK_SET, NULL);
-      if (status == 0)
-	{
-	  while (mu_stream_read (stream, buf, sizeof (buf), &n) == 0
-		 && n > 0)
-	    mu_md5_process_bytes (buf, n, &md5context);
-	  mu_md5_finish_ctx (&md5context, md5digest);
-	  tmp = buf;
-	  for (n = 0; n < 16; n++, tmp += 2)
-	    sprintf (tmp, "%02x", md5digest[n]);
-	  *tmp = '\0';
-	  /* POP3 rfc says that an UID should not be longer than 70.  */
-	  snprintf (buf + 32, 70, ".%lu.%lu", (unsigned long)time (NULL), 
-		    (unsigned long) uid);
-
-	  mu_header_set_value (header, "X-UIDL", buf, 1);
-	  buflen--; /* leave space for the NULL.  */
-	  strncpy (buffer, buf, buflen)[buflen] = '\0';
-	}
-      mu_stream_destroy (&stream);
-    }
-  return status;
-}
-
-int
-mu_message_get_qid (mu_message_t msg, mu_message_qid_t *pqid)
-{
-  if (msg == NULL)
-    return EINVAL;
-  if (!msg->_get_qid)
-    return ENOSYS;
-  return msg->_get_qid (msg, pqid);
-}
-    
-int
-mu_message_set_qid (mu_message_t msg,
-		    int (*_get_qid) (mu_message_t, mu_message_qid_t *),
-		    void *owner)
-{
-  if (msg == NULL)
-    return EINVAL;
-  if (msg->owner != owner)
-    return EACCES;
-  msg->_get_qid = _get_qid;
-  return 0;
-}
-
-int
-mu_message_set_uid (mu_message_t msg, int (*_get_uid) (mu_message_t, size_t *),
-		    void *owner)
-{
-  if (msg == NULL)
-    return EINVAL;
-  if (msg->owner != owner)
-    return EACCES;
-  msg->_get_uid = _get_uid;
-  return 0;
-}
-
-int
-mu_message_set_uidl (mu_message_t msg,
-		  int (* _get_uidl) (mu_message_t, char *, size_t, size_t *),
-		  void *owner)
-{
-  if (msg == NULL)
-    return EINVAL;
-  if (msg->owner != owner)
-    return EACCES;
-  msg->_get_uidl = _get_uidl;
-  return 0;
-}
-
-int
-mu_message_set_is_multipart (mu_message_t msg,
-			  int (*_is_multipart) (mu_message_t, int *),
-			  void *owner)
-{
-  if (msg == NULL)
-    return EINVAL;
-  if (msg->owner != owner)
-    return EACCES;
-  msg->_is_multipart = _is_multipart;
-  return 0;
-}
-
-int
-mu_message_is_multipart (mu_message_t msg, int *pmulti)
-{
-  if (msg && pmulti)
-    {
-      if (msg->_is_multipart)
-	return msg->_is_multipart (msg, pmulti);
-      if (msg->mime == NULL)
-	{
-	  int status = mu_mime_create (&msg->mime, msg, 0);
-	  if (status != 0)
-	    return 0;
-	}
-      *pmulti = mu_mime_is_multipart(msg->mime);
-    }
-  return 0;
-}
-
-int
-mu_message_get_num_parts (mu_message_t msg, size_t *pparts)
-{
-  if (msg == NULL || pparts == NULL)
-    return EINVAL;
-
-  if (msg->_get_num_parts)
-    return msg->_get_num_parts (msg, pparts);
-
-  if (msg->mime == NULL)
-    {
-      int status = mu_mime_create (&msg->mime, msg, 0);
-      if (status != 0)
-	return status;
-    }
-  return mu_mime_get_num_parts (msg->mime, pparts);
-}
-
-int
-mu_message_set_get_num_parts (mu_message_t msg,
-			   int (*_get_num_parts) (mu_message_t, size_t *),
-			   void *owner)
-{
-  if (msg == NULL)
-    return EINVAL;
-  if (msg->owner != owner)
-    return EACCES;
-  msg->_get_num_parts = _get_num_parts;
-  return 0;
-}
-
-int
-mu_message_get_part (mu_message_t msg, size_t part, mu_message_t *pmsg)
-{
-  if (msg == NULL || pmsg == NULL)
-    return EINVAL;
-
-  /* Overload.  */
-  if (msg->_get_part)
-    return msg->_get_part (msg, part, pmsg);
-
-  if (msg->mime == NULL)
-    {
-      int status = mu_mime_create (&msg->mime, msg, 0);
-      if (status != 0)
-	return status;
-    }
-  return mu_mime_get_part (msg->mime, part, pmsg);
-}
-
-int
-mu_message_set_get_part (mu_message_t msg, int (*_get_part)
-		      (mu_message_t, size_t, mu_message_t *),
-		      void *owner)
-{
-  if (msg == NULL)
-    return EINVAL;
-  if (msg->owner != owner)
-    return EACCES;
-  msg->_get_part = _get_part;
-  return 0;
-}
-
-int
-mu_message_get_observable (mu_message_t msg, mu_observable_t *pobservable)
-{
-  if (msg == NULL || pobservable == NULL)
-    return EINVAL;
-
-  if (msg->observable == NULL)
-    {
-      int status = mu_observable_create (&msg->observable, msg);
-      if (status != 0)
-	return status;
-    }
-  *pobservable = msg->observable;
-  return 0;
-}
-
-int
-mu_message_save_to_mailbox (mu_message_t msg, 
-                            mu_debug_t debug,
-			    const char *toname, int perms)
-{
-  int rc = 0;
-  mu_mailbox_t to = 0;
-
-  if ((rc = mu_mailbox_create_default (&to, toname)))
-    {
-      MU_DEBUG2 (debug, MU_DEBUG_ERROR,
-		 "mu_mailbox_create_default (%s) failed: %s\n", toname,
-		 mu_strerror (rc));
-      goto end;
-    }
-
-  if (debug && (rc = mu_mailbox_set_debug (to, debug)))
-	goto end;
-
-  if ((rc = mu_mailbox_open (to,
-			     MU_STREAM_WRITE | MU_STREAM_CREAT
-			     | (perms & MU_STREAM_IMASK))))
-    {
-      MU_DEBUG2 (debug, MU_DEBUG_ERROR,
-		 "mu_mailbox_open (%s) failed: %s\n", toname,
-		 mu_strerror (rc));
-      goto end;
-    }
-
-  if ((rc = mu_mailbox_append_message (to, msg)))
-    {
-      MU_DEBUG2 (debug, MU_DEBUG_ERROR,
-		 "mu_mailbox_append_message (%s) failed: %s\n", toname,
-		 mu_strerror (rc));
-      goto end;
-    }
-
-end:
-
-  if (!rc)
-    {
-      if ((rc = mu_mailbox_close (to)))
-	MU_DEBUG2 (debug, MU_DEBUG_ERROR,
-		   "mu_mailbox_close (%s) failed: %s\n", toname,
-		   mu_strerror (rc));
-    }
-  else
-    mu_mailbox_close (to);
-
-  mu_mailbox_destroy (&to);
-
-  return rc;
-}
-
diff --git a/libmailutils/message_stream.c b/libmailutils/message_stream.c
deleted file mode 100644
index 9f590d9..0000000
--- a/libmailutils/message_stream.c
+++ /dev/null
@@ -1,440 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 1999, 2000, 2001, 2002, 2005, 2006, 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.
-
-   GNU Mailutils 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 GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
-
-/* This file implements an MH draftfile stream: a read-only stream used
-   to transparently pass MH draftfiles to mailers. The only difference
-   between the usual RFC822 and MH draft is that the latter allows to use
-   a string of dashes to separate the headers from the body. */
-
-#ifdef HAVE_CONFIG_H
-# include <config.h>
-#endif
-#include <stdlib.h>
-#include <ctype.h>
-#include <string.h>
-
-#include <mailutils/types.h>
-#include <mailutils/address.h>
-#include <mailutils/alloc.h>
-#include <mailutils/envelope.h>
-#include <mailutils/message.h>
-#include <mailutils/header.h>
-#include <mailutils/body.h>
-#include <mailutils/stream.h>
-#include <mailutils/mutil.h>
-#include <mailutils/errno.h>
-#include <mailutils/error.h>
-#include <mailutils/cctype.h>
-#include <mailutils/cstr.h>
-#include <mailutils/sys/message_stream.h>
-
-
-static int
-_env_msg_date (mu_envelope_t envelope, char *buf, size_t len, size_t *pnwrite)
-{
-  struct _mu_message_stream *str = mu_envelope_get_owner (envelope);
-  
-  if (!str || !str->date)
-    return EINVAL;
-  if (buf)
-    {
-      strncpy (buf, str->date, len);
-      buf[len-1] = 0;
-      if (pnwrite)
-	*pnwrite = len;
-    }
-  else if (!pnwrite)
-    return EINVAL;
-  else
-    *pnwrite = strlen (str->date);
-  return 0;
-}
-
-static int
-_env_msg_sender (mu_envelope_t envelope, char *buf, size_t len,
-		 size_t *pnwrite)
-{
-  struct _mu_message_stream *str = mu_envelope_get_owner (envelope);
-  
-  if (!str || !str->from)
-    return EINVAL;
-  if (buf)
-    {
-      strncpy (buf, str->from, len);
-      buf[len-1] = 0;
-      if (pnwrite)
-	*pnwrite = len;
-    }
-  else if (!pnwrite)
-    return EINVAL;
-  else
-    *pnwrite = strlen (str->from);
-    
-  return 0;
-}
-
-
-static int
-_message_read (mu_stream_t stream, char *optr, size_t osize, size_t *nbytes)
-{
-  int rc;
-  struct _mu_message_stream *s = (struct _mu_message_stream*) stream;
-  mu_off_t offset = s->offset + s->envelope_length;
-  size_t rsize;
-  
-  if (offset < s->mark_offset)
-    {
-      if (offset + osize >= s->mark_offset)
-	osize = s->mark_offset - offset;
-    }
-  else
-    offset += s->mark_length;
-  /* FIXME: Seeking each time before read is awkward. The streamref
-     should be modified to take care of it */
-  rc = mu_stream_seek (s->transport, offset, MU_SEEK_SET, NULL);
-  if (rc == 0)
-    rc = mu_stream_read (s->transport, optr, osize, &rsize);
-  if (rc == 0)
-    {
-      s->offset += rsize;
-      if (nbytes)
-	*nbytes = rsize;
-    }
-  else
-    s->stream.last_err = rc;
-  return rc;
-}
-  
-static int
-_message_size (mu_stream_t stream, mu_off_t *psize)
-{
-  struct _mu_message_stream *s = (struct _mu_message_stream*) stream;
-  int rc = mu_stream_size (s->transport, psize);
-  
-  if (rc == 0)
-    *psize -= s->envelope_length + s->mark_length;
-  return rc;
-}
-  
-static int
-scan_stream (struct _mu_message_stream *str)
-{
-  char *from = NULL;
-  char *env_from = NULL;
-  char *env_date = NULL;
-  int rc;
-  char *buffer = NULL;
-  size_t bufsize = 0;
-  size_t len;
-  mu_off_t body_start, body_end;
-  mu_stream_t stream = str->transport;
-
-  if (str->envelope)
-    {
-      char *s = str->envelope + 5;
-      char *p = strchr (s, ' ');
-      size_t len;
-
-      if (p)
-	{
-	  len = p - s;
-	  env_from = mu_alloc (len + 1);
-	  if (!env_from)
-	    return ENOMEM;
-	  memcpy (env_from, s, len);
-	  env_from[len] = 0;
-	  env_date = mu_strdup (p + 1);
-	  if (!env_date)
-	    {
-	      free (env_from);
-	      return ENOMEM;
-	    }
-	}
-    }
-
-  rc = mu_stream_seek (stream, 0, MU_SEEK_SET, NULL);
-  if (rc)
-    return rc;
-  while ((rc = mu_stream_getline (stream, &buffer, &bufsize, &len)) == 0
-	 && len > 0)
-    {
-      if (buffer[0] == '\n')
-	break;
-
-      if (!env_from || !env_date)
-	{
-      	  if (!from && mu_c_strncasecmp (buffer, MU_HEADER_FROM,
-				         sizeof (MU_HEADER_FROM) - 1) == 0)
-	    
-	    from = mu_strdup (mu_str_skip_class (buffer +
-						 sizeof (MU_HEADER_FROM),
-						 MU_CTYPE_SPACE));
-	  else if (!env_from
-		   && mu_c_strncasecmp (buffer, MU_HEADER_ENV_SENDER,
-				        sizeof (MU_HEADER_ENV_SENDER) - 1) == 0)
-	    env_from = mu_strdup (mu_str_skip_class (buffer +
-					sizeof (MU_HEADER_ENV_SENDER),
-						     MU_CTYPE_SPACE));
-	  else if (!env_date
-		   && mu_c_strncasecmp (buffer, MU_HEADER_ENV_DATE,
-				        sizeof (MU_HEADER_ENV_DATE) - 1) == 0)
-	    env_date = mu_strdup (mu_str_skip_class (buffer +
-						  sizeof (MU_HEADER_ENV_DATE),
-						     MU_CTYPE_SPACE));
-	}
-    }
-
-  free (buffer);
-
-  rc = mu_stream_seek (stream, 0, MU_SEEK_CUR, &body_start);
-  if (rc)
-    return rc;
-  rc = mu_stream_size (stream, &body_end);
-  if (rc)
-    return rc;
-  
-  if (!env_from)
-    {
-      if (from)
-	{
-	  mu_address_t addr;
-	  
-	  mu_address_create (&addr, from);
-	  if (!addr
-	      || mu_address_aget_email (addr, 1, &env_from))
-	    env_from = mu_strdup ("GNU-Mailutils");
-	  mu_address_destroy (&addr);
-	}
-      else
-	env_from = mu_strdup ("GNU-MH");
-    }
-  free (from);
-  
-  if (!env_date)
-    {
-      struct tm *tm;
-      time_t t;
-      char date[80]; /* FIXME: This size is way too big */
-
-      time(&t);
-      tm = gmtime(&t);
-      mu_strftime (date, sizeof (date), "%a %b %e %H:%M:%S %Y", tm);
-      env_date = strdup (date);
-    }
-
-  str->from = env_from;
-  str->date = env_date;
-
-  str->body_start = body_start;
-  str->body_end = body_end - 1;
-  
-  return 0;
-}
-
-static int
-_message_open (mu_stream_t stream)
-{
-  struct _mu_message_stream *s = (struct _mu_message_stream*) stream;
-  size_t offset, len;
-  char *buffer = NULL;
-  size_t bufsize = 0;
-  int rc;
-
-  offset = 0;
-  mu_stream_seek (s->transport, 0, MU_SEEK_SET, NULL);
-  while ((rc = mu_stream_getline (s->transport, &buffer, &bufsize,
-				  &len)) == 0
-	 && len > 0)
-    {
-      if (offset == 0 && memcmp (buffer, "From ", 5) == 0)
-	{
-	  s->envelope_length = len;
-	  s->envelope = mu_strdup (buffer);
-	  if (!s->envelope)
-	    return ENOMEM;
-	  s->envelope[len - 1] = 0;
-	}
-      else if (mu_mh_delim (buffer))
-	{
-	  s->mark_offset = offset;
-	  s->mark_length = len - 1; /* do not count the terminating newline */
-	  break;
-	}
-
-      offset += len;
-    }
-  free (buffer);
-
-  return scan_stream (s);
-}
-
-static int
-_message_close (mu_stream_t stream)
-{
-  struct _mu_message_stream *s = (struct _mu_message_stream*) stream;
-  return s->stream.last_err = mu_stream_close (s->transport);
-}
-
-static void
-_message_done (mu_stream_t stream)
-{
-  struct _mu_message_stream *s = (struct _mu_message_stream*) stream;
-
-  free (s->envelope);
-  free (s->date);
-  free (s->from);
-  mu_stream_destroy (&s->transport);
-}
-
-static int
-_message_seek (struct _mu_stream *stream, mu_off_t off, mu_off_t *presult)
-{ 
-  struct _mu_message_stream *s = (struct _mu_message_stream*) stream;
-  mu_off_t size;
-
-  mu_stream_size (stream, &size);
-  if (off < 0 || off >= size)
-    return ESPIPE;
-  s->offset = off;
-  *presult = off;
-  return 0;
-}
-
-const char *
-_message_error_string (struct _mu_stream *stream, int rc)
-{
-  struct _mu_message_stream *str = (struct _mu_message_stream*) stream;
-  return mu_stream_strerror (str->transport, rc);
-}
-
-int
-mu_message_stream_create (mu_stream_t *pstream, mu_stream_t src, int flags)
-{
-  struct _mu_message_stream *s;
-  int sflag;
-  int rc;
-  mu_stream_t stream;
-  
-  mu_stream_get_flags (src, &sflag);
-  sflag &= MU_STREAM_SEEK;
-  
-  if (!flags)
-    flags = MU_STREAM_READ;
-  if (flags & (MU_STREAM_WRITE|MU_STREAM_CREAT|MU_STREAM_APPEND))
-    return EINVAL;
-  s = (struct _mu_message_stream *) _mu_stream_create (sizeof (*s),
-						       flags | sflag);
-  if (!s)
-    return ENOMEM;
-
-  rc = mu_streamref_create (&s->transport, src);
-  if (rc)
-    {
-      free (s);
-      return rc;
-    }
-  s->stream.open = _message_open;
-  s->stream.close = _message_close;
-  s->stream.done = _message_done;
-  s->stream.read = _message_read;
-  s->stream.size = _message_size;
-  s->stream.seek = _message_seek;
-  s->stream.error_string = _message_error_string;
-
-  stream = (mu_stream_t)s;
-  rc = mu_stream_open (stream);
-  if (rc)
-    mu_stream_destroy (&stream);
-  else
-    *pstream = stream;
-  return rc;
-}
-
-
-/* *************************** MH draft message **************************** */
-
-
-
-static int
-_body_obj_size (mu_body_t body, size_t *size)
-{
-  mu_message_t msg = mu_body_get_owner (body);
-  struct _mu_message_stream *str = mu_message_get_owner (msg);
-
-  if (size)
-    *size = str->body_end - str->body_start + 1;
-  return 0;
-}
-
-
-
-int
-mu_stream_to_message (mu_stream_t instream, mu_message_t *pmsg)
-{
-  mu_envelope_t env;
-  mu_message_t msg;
-  mu_body_t body;
-  mu_stream_t bstream;
-  mu_stream_t draftstream;
-  int rc;
-  struct _mu_message_stream *sp;
-  
-  /* FIXME: Perhaps MU_STREAM_NO_CLOSE is needed */
-  if ((rc = mu_message_stream_create (&draftstream, instream, 0)))
-    return rc;
-
-  if ((rc = mu_message_create (&msg, draftstream)))
-    {
-      mu_stream_destroy (&draftstream);
-      return rc;
-    }
-  
-  mu_message_set_stream (msg, draftstream, draftstream);
-  
-  if ((rc = mu_envelope_create (&env, draftstream)))
-    {
-      mu_message_destroy (&msg, draftstream);
-      mu_stream_destroy (&draftstream);
-      return rc;
-    }
-  
-  mu_envelope_set_date (env, _env_msg_date, draftstream);
-  mu_envelope_set_sender (env, _env_msg_sender, draftstream);
-  mu_message_set_envelope (msg, env, draftstream);
-
-  mu_body_create (&body, msg);
-  /* FIXME: It would be cleaner to use ioctl here */
-  sp = (struct _mu_message_stream *) draftstream;
-  rc = mu_streamref_create_abridged (&bstream, instream,
-				     sp->body_start, sp->body_end);
-  if (rc)
-    {
-      mu_body_destroy (&body, msg);
-      mu_message_destroy (&msg, draftstream);
-      mu_stream_destroy (&draftstream);
-      return rc;
-    }
-  
-  mu_body_set_stream (body, bstream, msg);
-  mu_body_set_size (body, _body_obj_size, msg);
-  mu_message_set_body (msg, body, draftstream);
-
-  *pmsg = msg;
-  return 0;
-}
diff --git a/libmailutils/mime.c b/libmailutils/mime.c
deleted file mode 100644
index 2c923cc..0000000
--- a/libmailutils/mime.c
+++ /dev/null
@@ -1,1071 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 1999, 2000, 2001, 2003, 2004, 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 <errno.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <time.h>
-#include <unistd.h>
-#ifdef HAVE_STRINGS_H
-# include <strings.h>
-#endif
-
-#include <mailutils/cctype.h>
-#include <mailutils/cstr.h>
-#include <mailutils/message.h>
-#include <mailutils/stream.h>
-#include <mailutils/body.h>
-#include <mailutils/header.h>
-#include <mailutils/errno.h>
-#include <mailutils/mutil.h>
-
-#include <mailutils/sys/mime.h>
-#include <mailutils/sys/stream.h>
-
-#ifndef TRUE
-#define TRUE (1)
-#define FALSE (0)
-#endif
-
-#define CT_MULTIPART_DIGEST "multipart/digest"
-#define CT_MULTIPART_DIGEST_LEN (sizeof (CT_MULTIPART_DIGEST) - 1)
-
-/* TODO:
- *  Need to prevent re-entry into mime lib, but allow non-blocking re-entry
- *  into lib.
- */
-
-static int
-_mime_is_multipart_digest (mu_mime_t mime)
-{
-  if (mime->content_type)
-    return mu_c_strncasecmp (CT_MULTIPART_DIGEST, mime->content_type,
-			     CT_MULTIPART_DIGEST_LEN) == 0;
-  return 0;
-}
-
-static int
-_mime_append_part (mu_mime_t mime, mu_message_t msg, int offset, int len, int lines)
-{
-  struct _mime_part *mime_part, **part_arr;
-  int             ret;
-  size_t          size;
-  mu_header_t        hdr;
-
-  if ((mime_part = calloc (1, sizeof (*mime_part))) == NULL)
-    return ENOMEM;
-
-  if (mime->nmtp_parts >= mime->tparts)
-    {
-      if ((part_arr =
-	   realloc (mime->mtp_parts,
-		    (mime->tparts + 5) * sizeof (mime_part))) == NULL)
-	{
-	  free (mime_part);
-	  return ENOMEM;
-	}
-      mime->mtp_parts = part_arr;
-      mime->tparts += 5;
-    }
-  mime->mtp_parts[mime->nmtp_parts++] = mime_part;
-  if (msg == NULL)
-    {
-      if ((ret = mu_message_create (&(mime_part->msg), mime_part)) == 0)
-	{
-	  if ((ret =
-	       mu_header_create (&hdr, mime->header_buf,
-				 mime->header_length)) != 0)
-	    {
-	      mu_message_destroy (&mime_part->msg, mime_part);
-	      free (mime_part);
-	      return ret;
-	    }
-	  mu_message_set_header (mime_part->msg, hdr, mime_part);
-	}
-      else
-	{
-	  free (mime_part);
-	  return ret;
-	}
-      mime->header_length = 0;
-      if ((ret =
-	   mu_header_get_value (hdr, MU_HEADER_CONTENT_TYPE, NULL,
-				0, &size)) != 0 || size == 0)
-	{
-	  if (_mime_is_multipart_digest (mime))
-	    mu_header_set_value (hdr,
-				 MU_HEADER_CONTENT_TYPE, "message/rfc822", 0);
-	  else
-	    mu_header_set_value (hdr, MU_HEADER_CONTENT_TYPE, "text/plain",
-				 0);
-	}
-      mime_part->len = len;
-      mime_part->lines = lines;
-      mime_part->offset = offset;
-    }
-  else
-    {
-      mu_message_ref (msg);
-      mu_message_size (msg, &mime_part->len);
-      mu_message_lines (msg, &mime_part->lines);
-      if (mime->nmtp_parts > 1)
-	mime_part->offset = mime->mtp_parts[mime->nmtp_parts - 2]->len;
-      mime_part->msg = msg;
-    }
-  mime_part->mime = mime;
-  return 0;
-}
-
-#define _ISSPECIAL(c) (						 \
-    ((c) == '(') || ((c) == ')') || ((c) == '<') || ((c) == '>') \
-    || ((c) == '@') || ((c) == ',') || ((c) == ';') || ((c) == ':') \
-    || ((c) == '\\') || ((c) == '.') || ((c) == '[') \
-    || ((c) == ']') )
-
-static void
-_mime_munge_content_header (char *field_body)
-{
-  char           *p, *e, *str = field_body;
-  int             quoted = 0;
-
-  mu_str_stripws (field_body);
-
-  if ((e = strchr (str, ';')) == NULL)
-    return;
-  while (*e == ';')
-    {
-      p = e;
-      e++;
-      while (*e && mu_isspace (*e)) /* remove space up to param */
-	e++;
-      memmove (p + 1, e, strlen (e) + 1);
-      e = p + 1;
-
-      while (*e && *e != '=')	/* find end of value */
-	e++;
-      e = p = e + 1;
-      while (*e
-	     && (quoted
-		 || (!_ISSPECIAL (*e) && !mu_isspace (*e))))
-	{
-	  if (*e == '\\')
-	    {			/* escaped */
-	      memmove (e, e + 1, strlen (e));
-	    }
-	  else if (*e == '\"')
-	    quoted = ~quoted;
-	  e++;
-	}
-    }
-}
-
-static char    *
-_mime_get_param (char *field_body, const char *param, int *len)
-{
-  char           *str, *p, *v, *e;
-  int             quoted = 0, was_quoted;
-
-  if (len == NULL || (str = field_body) == NULL)
-    return NULL;
-
-  p = strchr (str, ';');
-  while (p)
-    {
-      p++;
-      if ((v = strchr (p, '=')) == NULL)
-	break;
-      *len = 0;
-      v = e = v + 1;
-      was_quoted = 0;
-      while (*e
-	     && (quoted
-		 || (!_ISSPECIAL (*e) && !mu_isspace (*e))))
-	{			/* skip pass value and calc len */
-	  if (*e == '\"')
-	    quoted = ~quoted, was_quoted = 1;
-	  else
-	    (*len)++;
-	  e++;
-	}
-      if (mu_c_strncasecmp (p, param, strlen (param)))
-	{			/* no match jump to next */
-	  p = strchr (e, ';');
-	  continue;
-	}
-      else
-	return was_quoted ? v + 1 : v;	/* return unquoted value */
-    }
-  return NULL;
-}
-
-static int
-_mime_setup_buffers (mu_mime_t mime)
-{
-  if (mime->cur_buf == NULL
-      && (mime->cur_buf = malloc (mime->buf_size)) == NULL)
-    {
-      return ENOMEM;
-    }
-  if (mime->cur_line == NULL
-      && (mime->cur_line = calloc (mime->line_size, 1)) == NULL)
-    {
-      free (mime->cur_buf);
-      return ENOMEM;
-    }
-  return 0;
-}
-
-static void
-_mime_append_header_line (mu_mime_t mime)
-{
-  if (mime->header_length + mime->line_ndx > mime->header_buf_size)
-    {
-      char           *nhb;
-
-      if ((nhb =
-	   realloc (mime->header_buf,
-		    mime->header_length + mime->line_ndx + 128)) == NULL)
-	return;
-      mime->header_buf = nhb;
-      mime->header_buf_size = mime->header_length + mime->line_ndx + 128;
-    }
-  memcpy (mime->header_buf + mime->header_length, mime->cur_line,
-	  mime->line_ndx);
-  mime->header_length += mime->line_ndx;
-}
-
-static int
-_mime_parse_mpart_message (mu_mime_t mime)
-{
-  char           *cp, *cp2;
-  int             blength, mb_length, mb_offset, mb_lines, ret;
-  size_t          nbytes;
-
-  if (!(mime->flags & MIME_PARSER_ACTIVE))
-    {
-      char           *boundary;
-      int             len;
-
-      if ((ret = _mime_setup_buffers (mime)) != 0)
-	return ret;
-      if ((boundary =
-	   _mime_get_param (mime->content_type, "boundary", &len)) == NULL)
-	return EINVAL;
-      if ((mime->boundary = calloc (1, len + 1)) == NULL)
-	return ENOMEM;
-      strncpy (mime->boundary, boundary, len);
-
-      mime->cur_offset = 0;
-      mime->line_ndx = 0;
-      mime->parser_state = MIME_STATE_SCAN_BOUNDARY;
-      mime->flags |= MIME_PARSER_ACTIVE;
-    }
-  mb_length = mime->body_length;
-  mb_offset = mime->body_offset;
-  mb_lines = mime->body_lines;
-  blength = strlen (mime->boundary);
-
-  mu_stream_seek (mime->stream, mime->cur_offset, MU_SEEK_SET, NULL);
-  while ((ret =
-	  mu_stream_read (mime->stream, mime->cur_buf, mime->buf_size,
-			  &nbytes)) == 0 && nbytes)
-    {
-      cp = mime->cur_buf;
-      while (nbytes)
-	{
-	  mime->cur_line[mime->line_ndx] = *cp;
-	  if (*cp == '\n')
-	    {
-	      switch (mime->parser_state)
-		{
-		case MIME_STATE_BEGIN_LINE:
-		  mime->cur_line[0] = *cp;
-		  mime->line_ndx = 0;
-		  mime->parser_state = MIME_STATE_SCAN_BOUNDARY;
-		  break;
-
-		case MIME_STATE_SCAN_BOUNDARY:
-		  cp2 =
-		    mime->cur_line[0] ==
-		    '\n' ? mime->cur_line + 1 : mime->cur_line;
-		  if (mime->line_ndx >= blength)
-		    {
-		      if ((!strncmp (cp2, "--", 2)
-			   && !mu_c_strncasecmp (cp2 + 2, mime->boundary,
-				  	         blength))
-			  || !mu_c_strncasecmp (cp2, mime->boundary, blength))
-			{
-			  mime->parser_state = MIME_STATE_HEADERS;
-			  mime->flags &= ~MIME_PARSER_HAVE_CR;
-			  mb_length = mime->cur_offset 
-			                   - mb_offset
-			                   - mime->line_ndx;
-			  if (mime->header_length)
-			    /* this skips the preamble */
-			    {
-			      /* RFC 1521 [Page 30]:
-				 NOTE: The CRLF preceding the encapsulation
-				 line is conceptually attached to the boundary
-				 so that it is possible to have a part that
-				 does not end with a CRLF (line break). Body
-				 parts that must be considered to end with line
-				 breaks, therefore, must have two CRLFs
-				 preceding the encapsulation line, the first
-				 of which is part of the preceding body part,
-				 and the second of which is part of the
-				 encapsulation boundary. */
-			      
-			      if (mb_lines)
-				/* to prevent negative values in case of a
-				   malformed message */
-				mb_lines--;
-				  
-			      _mime_append_part (mime, NULL,
-						 mb_offset, mb_length,
-						 mb_lines);
-			    }
-
-			  if ((&mime->cur_line[mime->line_ndx] - cp2 - 1 >
-			       blength
-			       && !strncmp (cp2 + blength + 2, "--", 2))
-			      || (&mime->cur_line[mime->line_ndx] - cp2 - 1 ==
-				  blength
-				  && !strncmp (cp2 + blength, "--", 2)))
-			    {	/* last boundary */
-			      mime->parser_state = MIME_STATE_BEGIN_LINE;
-			      mime->header_length = 0;
-			    }
-			  else
-			    mime->line_ndx = -1; /* headers parsing requires
-						    empty line */
-			  break;
-			}
-		    }
-
-		  if (mime->header_length)
-		    mb_lines++;
-
-		  mime->line_ndx = 0;
-		  mime->cur_line[0] = *cp;	/* stay in this state but
-						   leave '\n' at begining */
-		  break;
-
-		case MIME_STATE_HEADERS:
-		  mime->line_ndx++;
-		  _mime_append_header_line (mime);
-		  if (mime->line_ndx == 1 || mime->cur_line[0] == '\r')
-		    {
-		      mime->parser_state = MIME_STATE_SCAN_BOUNDARY;
-		      mb_offset = mime->cur_offset + 1;
-		      mb_lines = 0;
-		    }
-		  mime->line_ndx = -1;
-		  break;
-		}
-	    }
-	  mime->line_ndx++;
-	  if (mime->line_ndx >= mime->line_size)
-	    {
-	      size_t newsize = mime->line_size + MIME_MAX_HDR_LEN;
-	      char *p = realloc (mime->cur_line, newsize);
-	      if (!p)
-		{
-		  ret = ENOMEM;
-		  break;
-		}
-	      mime->cur_line = p;
-	      mime->line_size = newsize;
-	    }
-	  mime->cur_offset++;
-	  nbytes--;
-	  cp++;
-	}
-    }
-  mime->body_lines = mb_lines;
-  mime->body_length = mb_length;
-  mime->body_offset = mb_offset;
-  if (ret != EAGAIN)
-    {				/* finished cleanup */
-      if (mime->header_length)	/* this skips the preamble */
-	_mime_append_part (mime, NULL, mb_offset, mb_length, mb_lines);
-      mime->flags &= ~MIME_PARSER_ACTIVE;
-      mime->body_offset = mime->body_length =
-	mime->header_length = mime->body_lines = 0;
-    }
-  return ret;
-}
-
-/*------ Mime message functions for READING a multipart message -----*/
-
-static int
-_mimepart_body_size (mu_body_t body, size_t *psize)
-{
-  mu_message_t       msg = mu_body_get_owner (body);
-  struct _mime_part *mime_part = mu_message_get_owner (msg);
-
-  if (mime_part == NULL)
-    return EINVAL;
-  if (psize)
-    *psize = mime_part->len;
-  return 0;
-}
-
-static int
-_mimepart_body_lines (mu_body_t body, size_t *plines)
-{
-  mu_message_t       msg = mu_body_get_owner (body);
-  struct _mime_part *mime_part = mu_message_get_owner (msg);
-
-  if (mime_part == NULL)
-    return EINVAL;
-  if (plines)
-    *plines = mime_part->lines;
-  return 0;
-}
-
-/*------ Mime message/header functions for CREATING multipart message -----*/
-static int
-_mime_set_content_type (mu_mime_t mime)
-{
-  const char  *content_type;
-  mu_header_t     hdr = NULL;
-  size_t          size;
-  int             ret;
-
-  /* Delayed the creation of the header 'til they create the final message via
-     mu_mime_get_message()  */
-  if (mime->hdrs == NULL)
-    return 0;
-  if (mime->nmtp_parts > 1)
-    {
-      char *cstr;
-      
-      if (mime->flags & MIME_ADDED_MULTIPART_CT)
-	return 0;
-      if (mime->flags & MU_MIME_MULTIPART_MIXED)
-	content_type = "multipart/mixed; boundary=";
-      else
-	content_type = "multipart/alternative; boundary=";
-      if (mime->boundary == NULL)
-	{
-	  char boundary[128];
-	  
-	  snprintf (boundary, sizeof boundary, "%ld-%ld=:%ld",
-		    (long) random (), (long) time (0), (long) getpid ());
-	  if ((mime->boundary = strdup (boundary)) == NULL)
-	    return ENOMEM;
-	}
-      size = strlen (content_type) + 2 + strlen (mime->boundary) + 1;
-      cstr = malloc (size);
-      if (!cstr)
-	return ENOMEM;
-      strcpy (cstr, content_type);
-      strcat (cstr, "\"");
-      strcat (cstr, mime->boundary);
-      strcat (cstr, "\"");
-      mime->flags |= MIME_ADDED_MULTIPART_CT;
-
-      ret = mu_header_set_value (mime->hdrs, MU_HEADER_CONTENT_TYPE, cstr, 1);
-      free (cstr);
-    }
-  else
-    {
-      if ((mime->flags & (MIME_ADDED_CT | MIME_ADDED_MULTIPART_CT))
-	  == MIME_ADDED_CT)
-	return 0;
-      mime->flags &= ~MIME_ADDED_MULTIPART_CT;
-      if (mime->nmtp_parts)
-	mu_message_get_header (mime->mtp_parts[0]->msg, &hdr);
-
-      if (hdr == NULL
-	  || mu_header_sget_value (hdr, MU_HEADER_CONTENT_TYPE,
-				   &content_type))
-	content_type = "text/plain; charset=us-ascii";
-
-      ret = mu_header_set_value (mime->hdrs, MU_HEADER_CONTENT_TYPE,
-				 content_type, 1);
-      if (ret)
-	return ret;
-
-      if (hdr)
-	{
-	  const char *content_te;
-	  
-	  /* if the only part contains a transfer-encoding
-	     field, set it on the message header too */
-	  if (mu_header_sget_value (hdr,
-				MU_HEADER_CONTENT_TRANSFER_ENCODING,
-				&content_te) == 0)
-	    ret = mu_header_set_value (mime->hdrs,
-				       MU_HEADER_CONTENT_TRANSFER_ENCODING,
-				       content_te, 1);
-
-	  if (ret == 0
-	      && mu_header_sget_value (hdr,
-				       MU_HEADER_CONTENT_DESCRIPTION,
-				       &content_te) == 0)
-	    ret = mu_header_set_value (mime->hdrs,
-				       MU_HEADER_CONTENT_DESCRIPTION,
-				       content_te, 1);
-
-	}
-    }
-  mime->flags |= MIME_ADDED_CT;
-  return ret;
-}
-
-
-static int
-_mime_part_size (mu_mime_t mime, size_t *psize)
-{
-  int i, ret;
-  size_t size, total = 0;
-
-  if (mime->nmtp_parts == 0)
-    return EINVAL;
-
-  if ((ret = _mime_set_content_type (mime)) != 0)
-    return ret;
-  for (i = 0; i < mime->nmtp_parts; i++)
-    {
-      mu_message_size (mime->mtp_parts[i]->msg, &size);
-      total += size;
-      if (mime->nmtp_parts > 1)	/* boundary line */
-	total += strlen (mime->boundary) + 3;
-    }
-  if (mime->nmtp_parts > 1)	/* ending boundary line */
-    total += 2;
-  *psize = total;
-  return 0;
-}
-
-
-struct _mime_body_stream
-{
-  struct _mu_stream stream;
-  mu_mime_t mime;
-};
-
-static int
-_mime_body_stream_size (mu_stream_t stream, mu_off_t *psize)
-{
-  struct _mime_body_stream *mstr = (struct _mime_body_stream *)stream;
-  mu_mime_t mime = mstr->mime;
-  size_t sz;
-  int rc = _mime_part_size (mime, &sz);
-  if (rc == 0)
-    *psize = sz;
-  return rc;
-}
-
-static void
-mime_reset_state (mu_mime_t mime)
-{				/* reset message */
-  mime->cur_offset = 0;
-  mime->cur_part = 0;
-  mime->part_offset = 0;
-  
-  if (mime->nmtp_parts > 1)
-    mime->flags |= MIME_INSERT_BOUNDARY;
-}
-
-/* FIXME: The seek method is defective */
-static int
-_mime_body_stream_seek (mu_stream_t stream, mu_off_t off, mu_off_t *presult)
-{
-  struct _mime_body_stream *mstr = (struct _mime_body_stream *)stream;
-  mu_mime_t mime = mstr->mime;
-
-  if (off == 0)
-    mime_reset_state (mime);
-
-  if (off != mime->cur_offset)
-    return ESPIPE;
-  *presult = off;
-  return 0;
-}
-
-#define ADD_CHAR(buf, c, offset, buflen, total, nbytes)	\
-  do							\
-    {							\
-      *(buf)++ = c;					\
-      (offset)++;					\
-      (total)++;				        \
-      if (--(buflen) == 0)			        \
-	{						\
-	  *(nbytes) = total;				\
-	  return 0;					\
-	}						\
-    }							\
-  while (0)
-
-static int
-_mime_body_stream_read (mu_stream_t stream, char *buf, size_t buflen, size_t *nbytes)
-{
-  struct _mime_body_stream *mstr = (struct _mime_body_stream *)stream;
-  mu_mime_t mime = mstr->mime;
-  int                ret = 0;
-  size_t             total = 0;
-  
-  if (mime->nmtp_parts == 0)
-    return EINVAL;
-  
-  if ((ret = _mime_set_content_type (mime)) == 0)
-    {
-      do
-	{
-	  size_t             part_nbytes = 0;
-
-	  if (buflen == 0)
-	    break;
-	  if (mime->nmtp_parts > 1)
-	    {
-	      size_t len;
-	      
-	      if (mime->flags & MIME_INSERT_BOUNDARY)
-		{
-		  if ((mime->flags & MIME_ADDING_BOUNDARY) == 0)
-		    {
-		      mime->boundary_len = strlen (mime->boundary);
-		      mime->preamble = 2;
-		      if (mime->cur_part == mime->nmtp_parts)
-			mime->postamble = 2;
-		      mime->flags |= MIME_ADDING_BOUNDARY;
-		    }
-		  while (mime->preamble)
-		    {
-		      mime->preamble--;
-		      ADD_CHAR (buf, '-', mime->cur_offset, buflen,
-				total, nbytes);
-		    }
-		  len = strlen (mime->boundary) - mime->boundary_len;
-		  while (mime->boundary_len)
-		    {
-		      mime->boundary_len--;
-		      ADD_CHAR (buf,
-				mime->boundary[len++],
-				mime->cur_offset, buflen,
-				total, nbytes);
-		    }
-		  while (mime->postamble)
-		    {
-		      mime->postamble--;
-		      ADD_CHAR (buf, '-', mime->cur_offset, buflen,
-				total, nbytes);
-		    }
-		  mime->flags &=
-		    ~(MIME_INSERT_BOUNDARY | MIME_ADDING_BOUNDARY);
-		  mime->part_offset = 0;
-		  ADD_CHAR (buf, '\n', mime->cur_offset, buflen,
-			    total, nbytes);
-		}
-
-	      if (!mime->part_stream)
-		{
-		  if (mime->cur_part >= mime->nmtp_parts)
-		    {
-		      *nbytes = total;
-		      return 0;
-		    }
-		  ret = mu_message_get_streamref (mime->mtp_parts[mime->cur_part]->msg,
-						  &mime->part_stream);
-		}
-	    }
-	  else if (!mime->part_stream)
-	    {
-	      mu_body_t part_body;
-
-	      if (mime->cur_part >= mime->nmtp_parts)
-		{
-		  *nbytes = total;
-		  return 0;
-		}
-	      mu_message_get_body (mime->mtp_parts[mime->cur_part]->msg,
-				   &part_body);
-	      ret = mu_body_get_streamref (part_body, &mime->part_stream);
-	    }
-	  if (ret)
-	    break;
-	  ret = mu_stream_seek (mime->part_stream, mime->part_offset,
-				MU_SEEK_SET, NULL);
-	  if (ret)
-	    {
-	      mu_stream_destroy (&mime->part_stream);
-	      break;
-	    }
-	  while (buflen > 0 &&
-		 (ret = mu_stream_read (mime->part_stream, buf, buflen,
-					&part_nbytes)) == 0)
-	    {
-	      if (part_nbytes)
-		{
-		  mime->part_offset += part_nbytes;
-		  mime->cur_offset += part_nbytes;
-		  total += part_nbytes;
-		  buflen -= part_nbytes;
-		  buf += part_nbytes;
-		}
-	      else 
-		{
-		  mu_stream_destroy (&mime->part_stream);
-		  mime->flags |= MIME_INSERT_BOUNDARY;
-		  mime->cur_part++;
-		  ADD_CHAR (buf, '\n', mime->cur_offset, buflen,
-			    total, nbytes);
-		  break;
-		}
-	    }
-	}
-      while (ret == 0 && mime->cur_part <= mime->nmtp_parts);
-    }
-  if (ret)
-    mu_stream_destroy (&mime->part_stream);
-  
-  *nbytes = total;
-  return ret;
-}
-
-static int
-_mime_body_stream_ioctl (mu_stream_t stream, int code, void *arg)
-{
-  struct _mime_body_stream *mstr = (struct _mime_body_stream *)stream;
-  mu_mime_t mime = mstr->mime;
-  mu_stream_t msg_stream;
-  int rc;
-  
-  switch (code)
-    {
-    case MU_IOCTL_GET_TRANSPORT:
-      if (!arg)
-	return EINVAL;
-      
-      if (mime->nmtp_parts == 0 || mime->cur_offset == 0)
-	return EINVAL;
-      rc = mu_message_get_streamref (mime->mtp_parts[mime->cur_part]->msg,
-				     &msg_stream);
-      if (rc)
-	break;
-      rc = mu_stream_ioctl (msg_stream, code, arg);
-      mu_stream_destroy (&msg_stream);
-      break;
-
-    default:
-      rc = ENOSYS;
-    }
-  return rc;
-}
-
-static int
-create_mime_body_stream (mu_stream_t *pstr, mu_mime_t mime)
-{
-  struct _mime_body_stream *sp =
-    (struct _mime_body_stream *)_mu_stream_create (sizeof (*sp),
-						   MU_STREAM_READ | MU_STREAM_SEEK);
-  if (!sp)
-    return ENOMEM;
-  sp->stream.read = _mime_body_stream_read;
-  sp->stream.seek = _mime_body_stream_seek;
-  sp->stream.ctl = _mime_body_stream_ioctl;
-  sp->stream.size = _mime_body_stream_size;
-  sp->mime = mime;
-  mime_reset_state (mime);
-  *pstr = (mu_stream_t) sp;
-  return 0;
-}
-
-
-static int
-_mime_body_size (mu_body_t body, size_t *psize)
-{
-  mu_message_t       msg = mu_body_get_owner (body);
-  mu_mime_t          mime = mu_message_get_owner (msg);
-  return _mime_part_size (mime, psize);
-}
-
-static int
-_mime_body_lines (mu_body_t body, size_t *plines)
-{
-  mu_message_t       msg = mu_body_get_owner (body);
-  mu_mime_t          mime = mu_message_get_owner (msg);
-  int             i, ret;
-  size_t          lines;
-
-  if (mime->nmtp_parts == 0)
-    return EINVAL;
-
-  if ((ret = _mime_set_content_type (mime)) != 0)
-    return ret;
-  for (i = 0; i < mime->nmtp_parts; i++)
-    {
-      mu_message_lines (mime->mtp_parts[i]->msg, &lines);
-      plines += lines;
-      if (mime->nmtp_parts > 1)	/* boundary line */
-	plines++;
-    }
-  return 0;
-}
-
-int
-mu_mime_create (mu_mime_t *pmime, mu_message_t msg, int flags)
-{
-  mu_mime_t          mime = NULL;
-  int             ret = 0;
-  size_t          size;
-  mu_body_t          body;
-
-  if (pmime == NULL)
-    return EINVAL;
-  *pmime = NULL;
-  if ((mime = calloc (1, sizeof (*mime))) == NULL)
-    return ENOMEM;
-  if (msg)
-    {
-      if ((ret = mu_message_get_header (msg, &mime->hdrs)) == 0)
-	{
-	  if ((ret =
-	       mu_header_get_value (mime->hdrs,
-				    MU_HEADER_CONTENT_TYPE,
-				    NULL, 0, &size)) == 0 && size)
-	    {
-	      if ((mime->content_type = malloc (size + 1)) == NULL)
-		ret = ENOMEM;
-	      else if ((ret = mu_header_get_value (mime->hdrs,
-						   MU_HEADER_CONTENT_TYPE,
-						   mime->content_type,
-						   size + 1, 0)) == 0)
-		_mime_munge_content_header (mime->content_type);
-	    }
-	  else
-	    {
-	      if (ret == MU_ERR_NOENT)
-		{
-		  ret = 0;
-		  if ((mime->content_type =
-		       strdup ("text/plain; charset=us-ascii")) == NULL)
-		    /* default as per spec. */
-		    ret = ENOMEM;
-		}
-	    }
-	  if (ret == 0)
-	    {
-	      mime->msg = msg;
-	      mime->buf_size = MIME_DFLT_BUF_SIZE;
-	      mime->line_size = MIME_MAX_HDR_LEN;
-	      mu_message_get_body (msg, &body);
-	      mu_body_get_streamref (body, &mime->stream);
-	    }
-	}
-    }
-  else
-    {
-      mime->flags |= MIME_NEW_MESSAGE | MU_MIME_MULTIPART_MIXED;
-    }
-  if (ret != 0)
-    {
-      if (mime->content_type)
-	free (mime->content_type);
-      free (mime);
-    }
-  else
-    {
-      mime->flags |= (flags & MIME_FLAG_MASK);
-      *pmime = mime;
-    }
-  return ret;
-}
-
-void
-mu_mime_destroy (mu_mime_t *pmime)
-{
-  mu_mime_t          mime;
-  struct _mime_part *mime_part;
-  int             i;
-
-  if (pmime && *pmime)
-    {
-      mime = *pmime;
-      if (mime->mtp_parts != NULL)
-	{
-	  for (i = 0; i < mime->nmtp_parts; i++)
-	    {
-	      mime_part = mime->mtp_parts[i];
-	      if (mime_part->msg && mime->flags & MIME_NEW_MESSAGE)
-		mu_message_unref (mime_part->msg);
-	      else
-		mu_message_destroy (&mime_part->msg, mime_part);
-	      free (mime_part);
-	    }
-	  free (mime->mtp_parts);
-	}
-      mu_stream_destroy (&mime->stream);
-      mu_stream_destroy (&mime->part_stream);
-      if (mime->msg && mime->flags & MIME_NEW_MESSAGE)
-	mu_message_destroy (&mime->msg, mime);
-      if (mime->content_type)
-	free (mime->content_type);
-      if (mime->cur_buf)
-	free (mime->cur_buf);
-      if (mime->cur_line)
-	free (mime->cur_line);
-      if (mime->boundary)
-	free (mime->boundary);
-      if (mime->header_buf)
-	free (mime->header_buf);
-      free (mime);
-      *pmime = NULL;
-    }
-}
-
-int
-mu_mime_get_part (mu_mime_t mime, size_t part, mu_message_t *msg)
-{
-  size_t          nmtp_parts;
-  int             ret = 0, flags = 0;
-  mu_stream_t        stream;
-  mu_body_t          body;
-  struct _mime_part *mime_part;
-
-  if ((ret = mu_mime_get_num_parts (mime, &nmtp_parts)) == 0)
-    {
-      if (part < 1 || part > nmtp_parts)
-	return MU_ERR_NOENT;
-      if (nmtp_parts == 1 && mime->mtp_parts == NULL)
-	*msg = mime->msg;
-      else
-	{
-	  mime_part = mime->mtp_parts[part - 1];
-	  if (!mime_part->body_created
-	      && (ret = mu_body_create (&body, mime_part->msg)) == 0)
-	    {
-	      mu_body_set_size (body, _mimepart_body_size, mime_part->msg);
-	      mu_body_set_lines (body, _mimepart_body_lines, mime_part->msg);
-	      mu_stream_get_flags (mime->stream, &flags);
-	      ret = mu_streamref_create_abridged (&stream, mime->stream,
-						  mime_part->offset,
-						  mime_part->offset +
-						    mime_part->len - 1);
-	      if (ret == 0)
-		{
-		  mu_stream_set_flags (stream,
-				       MU_STREAM_READ | (flags &
-							 (MU_STREAM_SEEK
-							  | MU_STREAM_NONBLOCK)));
-		  mu_body_set_stream (body, stream, mime_part->msg);
-		  mu_message_set_body (mime_part->msg, body, mime_part);
-		  mime_part->body_created = 1;
-		}
-	    }
-	  *msg = mime_part->msg;
-	}
-    }
-  return ret;
-}
-
-int
-mu_mime_get_num_parts (mu_mime_t mime, size_t *nmtp_parts)
-{
-  int             ret = 0;
-
-  if ((mime->nmtp_parts == 0 && !mime->boundary)
-      || mime->flags & MIME_PARSER_ACTIVE)
-    {
-      if (mu_mime_is_multipart (mime))
-	{
-	  if ((ret = _mime_parse_mpart_message (mime)) != 0)
-	    return (ret);
-	}
-      else
-	mime->nmtp_parts = 1;
-    }
-  *nmtp_parts = mime->nmtp_parts;
-  return (ret);
-
-}
-
-int
-mu_mime_add_part (mu_mime_t mime, mu_message_t msg)
-{
-  int             ret;
-
-  if (mime == NULL || msg == NULL || (mime->flags & MIME_NEW_MESSAGE) == 0)
-    return EINVAL;
-  if ((ret = _mime_append_part (mime, msg, 0, 0, 0)) == 0)
-    ret = _mime_set_content_type (mime);
-  return ret;
-}
-
-int
-mu_mime_get_message (mu_mime_t mime, mu_message_t *msg)
-{
-  mu_stream_t        body_stream;
-  mu_body_t          body;
-  int             ret = 0;
-
-  if (mime == NULL || msg == NULL)
-    return EINVAL;
-  if (mime->msg == NULL)
-    {
-      if ((mime->flags & MIME_NEW_MESSAGE) == 0)
-	return EINVAL;
-      if ((ret = mu_message_create (&mime->msg, mime)) == 0)
-	{
-	  if ((ret = mu_header_create (&mime->hdrs, NULL, 0)) == 0)
-	    {
-	      mu_message_set_header (mime->msg, mime->hdrs, mime);
-	      mu_header_set_value (mime->hdrs, MU_HEADER_MIME_VERSION, "1.0",
-				   0);
-	      if ((ret = _mime_set_content_type (mime)) == 0)
-		{
-		  if ((ret = mu_body_create (&body, mime->msg)) == 0)
-		    {
-		      mu_message_set_body (mime->msg, body, mime);
-		      mu_body_set_size (body, _mime_body_size, mime->msg);
-		      mu_body_set_lines (body, _mime_body_lines, mime->msg);
-		      ret = create_mime_body_stream (&body_stream, mime);
-		      if (ret == 0)
-			{
-			  mu_body_set_stream (body, body_stream, mime->msg);
-			  *msg = mime->msg;
-			  return 0;
-			}
-		    }
-		}
-	    }
-	  mu_message_destroy (&mime->msg, mime);
-	  mime->msg = NULL;
-	}
-    }
-  if (ret == 0)
-    *msg = mime->msg;
-  return ret;
-}
-
-int
-mu_mime_is_multipart (mu_mime_t mime)
-{
-  if (mime->content_type)
-    return (mu_c_strncasecmp ("multipart", mime->content_type,
-			      strlen ("multipart")) ? 0 : 1);
-  return 0;
-}
diff --git a/libmailutils/mime/Makefile.am b/libmailutils/mime/Makefile.am
new file mode 100644
index 0000000..70a3ebc
--- /dev/null
+++ b/libmailutils/mime/Makefile.am
@@ -0,0 +1,25 @@
+# GNU Mailutils -- a suite of utilities for electronic mail
+# Copyright (C) 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, see
+# <http://www.gnu.org/licenses/>.
+
+noinst_LTLIBRARIES = libmime.la
+
+libmime_la_SOURCES = \
+ attachment.c\
+ mime.c\
+ mimehdr.c
+
+INCLUDES = @MU_LIB_COMMON_INCLUDES@ -I/libmailutils
diff --git a/libmailutils/mime/attachment.c b/libmailutils/mime/attachment.c
new file mode 100644
index 0000000..e877cb7
--- /dev/null
+++ b/libmailutils/mime/attachment.c
@@ -0,0 +1,460 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 2004, 2005, 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, see 
+   <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#ifdef HAVE_LIBGEN_H
+# include <libgen.h>
+#endif
+
+#ifdef HAVE_STRINGS_H
+# include <strings.h>
+#endif
+
+#include <mailutils/cctype.h>
+#include <mailutils/cstr.h>
+#include <mailutils/body.h>
+#include <mailutils/filter.h>
+#include <mailutils/header.h>
+#include <mailutils/message.h>
+#include <mailutils/stream.h>
+#include <mailutils/errno.h>
+#include <mailutils/mutil.h>
+
+#define BUF_SIZE	2048
+
+struct _mu_mime_io_buffer
+{
+  unsigned int refcnt;
+  char *buf;
+  size_t bufsize;
+  char *charset;
+  mu_header_t hdr;
+  mu_message_t msg;
+  mu_stream_t stream;	/* output file/decoding stream for saving attachment */
+  mu_stream_t fstream;	/* output file stream for saving attachment */
+};
+
+#define MSG_HDR "Content-Type: %s; name=%s\nContent-Transfer-Encoding: %s\nContent-Disposition: attachment; filename=%s\n\n"
+
+int
+mu_message_create_attachment (const char *content_type, const char *encoding,
+			      const char *filename, mu_message_t *newmsg)
+{
+  mu_header_t hdr;
+  mu_body_t body;
+  mu_stream_t fstream = NULL, tstream = NULL;
+  char *header = NULL, *name = NULL, *fname = NULL;
+  int ret;
+
+  if (newmsg == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+  if (filename == NULL)
+    return EINVAL;
+
+  if ((ret = mu_message_create (newmsg, NULL)) == 0)
+    {
+      if (content_type == NULL)
+	content_type = "text/plain";
+      if (encoding == NULL)
+	encoding = "7bit";
+      if ((fname = strdup (filename)) != NULL)
+	{
+	  name = strrchr (fname, '/');
+	  if (name)
+	    name++;
+	  else
+	    name = fname;
+	  if ((header =
+	       malloc (strlen (MSG_HDR) + strlen (content_type) +
+		       strlen (name) * 2 + strlen (encoding) + 1)) == NULL)
+	    ret = ENOMEM;
+	  else
+	    {
+	      sprintf (header, MSG_HDR, content_type, name, encoding, name);
+	      if ((ret = mu_header_create (&hdr, header, strlen (header)))
+		  == 0)
+		{
+		  mu_message_get_body (*newmsg, &body);
+		  if ((ret =
+		       mu_file_stream_create (&fstream, filename,
+					      MU_STREAM_READ)) == 0)
+		    {
+		      if ((ret =
+			   mu_filter_create (&tstream, fstream, encoding,
+					     MU_FILTER_ENCODE,
+					     MU_STREAM_READ)) == 0)
+			{
+			  mu_body_set_stream (body, tstream, *newmsg);
+			  mu_message_set_header (*newmsg, hdr, NULL);
+			}
+		    }
+		}
+	      free (header);
+	    }
+	}
+    }
+  if (ret)
+    {
+      if (*newmsg)
+	mu_message_destroy (newmsg, NULL);
+      if (hdr)
+	mu_header_destroy (&hdr);
+      if (fstream)
+	mu_stream_destroy (&fstream);
+      if (fname)
+	free (fname);
+    }
+  return ret;
+}
+
+int
+mu_mime_io_buffer_create (mu_mime_io_buffer_t *pinfo)
+{
+  mu_mime_io_buffer_t info;
+  
+  if ((info = calloc (1, sizeof (*info))) == NULL)
+    return ENOMEM;
+  info->refcnt = 1;
+  info->bufsize = BUF_SIZE;
+  *pinfo = info;
+  return 0;
+}
+
+void
+mu_mime_io_buffer_set_size (mu_mime_io_buffer_t info, size_t size)
+{
+  info->bufsize = size;
+}
+
+void
+mu_mime_io_buffer_get_size (mu_mime_io_buffer_t info, size_t *psize)
+{
+  *psize = info->bufsize;
+}
+
+int
+mu_mime_io_buffer_set_charset (mu_mime_io_buffer_t info, const char *charset)
+{
+  char *cp = strdup (charset);
+  if (!cp)
+    return ENOMEM;
+  free (info->charset);
+  info->charset = cp;
+  return 0;
+}
+
+void
+mu_mime_io_buffer_sget_charset (mu_mime_io_buffer_t info, const char **charset)
+{
+  *charset = info->charset;
+}
+
+int
+mu_mime_io_buffer_aget_charset (mu_mime_io_buffer_t info, const char **charset)
+{
+  *charset = strdup (info->charset);
+  if (!charset)
+    return ENOMEM;
+  return 0;
+}
+
+void
+mu_mime_io_buffer_destroy (mu_mime_io_buffer_t *pinfo)
+{
+  if (pinfo && *pinfo)
+    {
+      mu_mime_io_buffer_t info = *pinfo;
+      free (info->charset);
+      free (info->buf);
+      free (info);
+      *pinfo = NULL;
+    }
+}
+
+static void
+_attachment_free (struct _mu_mime_io_buffer *info, int free_message)
+{
+  if (free_message)
+    {
+      if (info->msg)
+	mu_message_destroy (&info->msg, NULL);
+      else if (info->hdr)
+	mu_header_destroy (&info->hdr);
+    }
+  info->msg = NULL;
+  info->hdr = NULL;
+  info->stream = NULL;
+  info->fstream = NULL;
+  if (--info->refcnt == 0)
+    {
+      free (info->charset);
+      free (info->buf);
+      free (info);
+    }
+}
+
+static int
+_attachment_setup (mu_mime_io_buffer_t *pinfo, mu_message_t msg,
+		   mu_stream_t *pstream)
+{
+  int ret;
+  mu_body_t body;
+  mu_mime_io_buffer_t info;
+  mu_stream_t stream;
+    
+  if ((ret = mu_message_get_body (msg, &body)) != 0 ||
+      (ret = mu_body_get_streamref (body, &stream)) != 0)
+    return ret;
+  ret = mu_stream_seek (stream, 0, SEEK_SET, NULL);
+  if (ret)
+    return ret;
+  *pstream = stream;
+  if (*pinfo)
+    {
+      info = *pinfo;
+      info->refcnt++;
+    }
+  else
+    {
+      ret = mu_mime_io_buffer_create (&info);
+      if (ret)
+	return ret;
+    }
+  
+  if (!info->buf && ((info->buf = malloc (info->bufsize)) == NULL))
+    {
+      _attachment_free (info, 0);
+      return ENOMEM;
+    }
+  info->msg = msg;
+  *pinfo = info;
+  return 0;
+}
+
+int
+mu_message_save_attachment (mu_message_t msg, const char *filename,
+			    mu_mime_io_buffer_t info)
+{
+  mu_stream_t istream;
+  int ret;
+  size_t size;
+  size_t nbytes;
+  mu_header_t hdr;
+  const char *fname = NULL;
+  char *partname = NULL;
+
+  if (msg == NULL)
+    return EINVAL;
+
+  if ((ret = _attachment_setup (&info, msg, &istream)) != 0)
+    return ret;
+
+  if (ret == 0 && (ret = mu_message_get_header (msg, &hdr)) == 0)
+    {
+      if (filename == NULL)
+	{
+	  ret = mu_message_aget_decoded_attachment_name (msg, info->charset,
+							 &partname, NULL);
+	  if (partname)
+	    fname = partname;
+	}
+      else
+	fname = filename;
+      if (fname
+	  && (ret =
+	      mu_file_stream_create (&info->fstream, fname,
+				     MU_STREAM_WRITE | MU_STREAM_CREAT)) == 0)
+	{
+	  char *content_encoding;
+	  char *content_encoding_mem = NULL;
+	  
+	  mu_header_get_value (hdr, "Content-Transfer-Encoding", NULL, 0,
+			       &size);
+	  if (size)
+	    {
+	      content_encoding_mem = malloc (size + 1);
+	      if (content_encoding_mem == NULL)
+		ret = ENOMEM;
+	      content_encoding = content_encoding_mem;
+	      mu_header_get_value (hdr, "Content-Transfer-Encoding",
+				   content_encoding, size + 1, 0);
+	    }
+	  else
+	    content_encoding = "7bit";
+	  ret =
+	    mu_filter_create (&info->stream, istream, content_encoding,
+			      MU_FILTER_DECODE,
+			      MU_STREAM_READ);
+	  free (content_encoding_mem);
+	}
+    }
+  if (info->stream && istream)
+    {
+      while (((ret =
+	       mu_stream_read (info->stream, info->buf, BUF_SIZE, 
+			       &nbytes)) == 0 && nbytes))
+	{
+	  if ((ret =
+	       mu_stream_write (info->fstream, info->buf, nbytes, NULL)) != 0)
+	    break;
+	}
+    }
+  if (ret != EAGAIN && info)
+    {
+      mu_stream_close (info->fstream);
+      mu_stream_destroy (&info->stream);
+      mu_stream_destroy (&info->fstream);
+    }
+
+  mu_stream_destroy (&istream);
+  _attachment_free (info, ret); /* FIXME: or 0? */
+  
+  /* Free fname if we allocated it. */
+  if (partname)
+    free (partname);
+
+  return ret;
+}
+
+int
+mu_message_encapsulate (mu_message_t msg, mu_message_t *newmsg,
+			mu_mime_io_buffer_t info)
+{
+  mu_stream_t istream, ostream;
+  const char *header;
+  int ret = 0;
+  size_t nbytes;
+  mu_message_t tmsg = NULL;
+  
+  if (newmsg == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+
+  if (msg == NULL)
+    {
+      mu_header_t hdr;
+      
+      ret = mu_message_create (&tmsg, NULL);
+      if (ret)
+	return ret;
+      msg = tmsg;
+      header =
+	"Content-Type: message/rfc822\nContent-Transfer-Encoding: 7bit\n\n";
+      if ((ret =
+	   mu_header_create (&hdr, header, strlen (header))) == 0)
+	ret = mu_message_set_header (msg, hdr, NULL);
+      if (ret)
+	{
+	  mu_message_destroy (&msg, NULL);
+	  return ret;
+	}
+    }
+      
+  if ((ret = _attachment_setup (&info, msg, &ostream)) != 0)
+    {
+      mu_message_destroy (&tmsg, NULL);
+      return ret;
+    }
+
+  if (ret == 0 && (ret = mu_message_get_streamref (msg, &istream)) == 0)
+    {
+      mu_stream_seek (istream, 0, MU_SEEK_SET, NULL);
+      while (((ret = mu_stream_read (istream, info->buf, BUF_SIZE, 
+				     &nbytes)) == 0 && nbytes))
+	{
+	  if ((ret =
+	       mu_stream_write (ostream, info->buf, nbytes, NULL)) != 0)
+	    break;
+	}
+      mu_stream_destroy (&istream);
+    }
+  if (ret == 0)
+    *newmsg = info->msg;
+  mu_stream_destroy (&ostream);
+  _attachment_free (info, ret && ret != EAGAIN);
+  return ret;
+}
+
+#define MESSAGE_RFC822_STR "message/rfc822"
+
+int
+mu_message_unencapsulate (mu_message_t msg, mu_message_t *newmsg,
+			  mu_mime_io_buffer_t info)
+{
+  size_t size, nbytes;
+  int ret = 0;
+  mu_header_t hdr;
+  mu_stream_t istream, ostream;
+
+  if (msg == NULL)
+    return EINVAL;
+  if (newmsg == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+
+  if (info == NULL /* FIXME: not needed? */
+      && (ret = mu_message_get_header (msg, &hdr)) == 0)
+    {
+      mu_header_get_value (hdr, "Content-Type", NULL, 0, &size);
+      if (size)
+	{
+	  char *content_type;
+	  if ((content_type = malloc (size + 1)) == NULL)
+	    return ENOMEM;
+	  mu_header_get_value (hdr, "Content-Type", content_type, size + 1,
+			       0);
+	  ret = mu_c_strncasecmp (content_type, MESSAGE_RFC822_STR,
+				  sizeof (MESSAGE_RFC822_STR) - 1);
+	  free (content_type);
+	  if (ret != 0)
+	    return EINVAL;
+	}
+      else
+	return EINVAL;
+    }
+  if ((ret = _attachment_setup (&info, msg, &istream)) != 0)
+    return ret;
+  if (info->msg == NULL)
+    ret = mu_message_create (&info->msg, NULL);
+  if (ret == 0)
+    {
+      mu_message_get_streamref (info->msg, &ostream);
+      mu_stream_seek (ostream, 0, MU_SEEK_SET, NULL);
+      while (((ret =
+	       mu_stream_read (istream, info->buf, BUF_SIZE, 
+			       &nbytes)) == 0 && nbytes))
+	{
+	  if ((ret =
+	       mu_stream_write (ostream, info->buf, nbytes, NULL)) != 0)
+	    break;
+	}
+      mu_stream_destroy (&ostream);
+    }
+  if (ret == 0)
+    *newmsg = info->msg;
+  mu_stream_destroy (&istream);
+  _attachment_free (info, ret && ret != EAGAIN);
+  return ret;
+}
diff --git a/libmailutils/mime/mime.c b/libmailutils/mime/mime.c
new file mode 100644
index 0000000..17c5441
--- /dev/null
+++ b/libmailutils/mime/mime.c
@@ -0,0 +1,1070 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 2003, 2004, 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, see
+   <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+#ifdef HAVE_STRINGS_H
+# include <strings.h>
+#endif
+
+#include <mailutils/cctype.h>
+#include <mailutils/cstr.h>
+#include <mailutils/message.h>
+#include <mailutils/stream.h>
+#include <mailutils/body.h>
+#include <mailutils/header.h>
+#include <mailutils/errno.h>
+#include <mailutils/mutil.h>
+
+#include <mailutils/sys/mime.h>
+#include <mailutils/sys/stream.h>
+
+#ifndef TRUE
+#define TRUE (1)
+#define FALSE (0)
+#endif
+
+#define CT_MULTIPART_DIGEST "multipart/digest"
+#define CT_MULTIPART_DIGEST_LEN (sizeof (CT_MULTIPART_DIGEST) - 1)
+
+/* TODO:
+ *  Need to prevent re-entry into mime lib, but allow non-blocking re-entry
+ *  into lib.
+ */
+
+static int
+_mime_is_multipart_digest (mu_mime_t mime)
+{
+  if (mime->content_type)
+    return mu_c_strncasecmp (CT_MULTIPART_DIGEST, mime->content_type,
+			     CT_MULTIPART_DIGEST_LEN) == 0;
+  return 0;
+}
+
+static int
+_mime_append_part (mu_mime_t mime, mu_message_t msg, int offset, int len, int lines)
+{
+  struct _mime_part *mime_part, **part_arr;
+  int             ret;
+  size_t          size;
+  mu_header_t        hdr;
+
+  if ((mime_part = calloc (1, sizeof (*mime_part))) == NULL)
+    return ENOMEM;
+
+  if (mime->nmtp_parts >= mime->tparts)
+    {
+      if ((part_arr =
+	   realloc (mime->mtp_parts,
+		    (mime->tparts + 5) * sizeof (mime_part))) == NULL)
+	{
+	  free (mime_part);
+	  return ENOMEM;
+	}
+      mime->mtp_parts = part_arr;
+      mime->tparts += 5;
+    }
+  mime->mtp_parts[mime->nmtp_parts++] = mime_part;
+  if (msg == NULL)
+    {
+      if ((ret = mu_message_create (&(mime_part->msg), mime_part)) == 0)
+	{
+	  if ((ret =
+	       mu_header_create (&hdr, mime->header_buf,
+				 mime->header_length)) != 0)
+	    {
+	      mu_message_destroy (&mime_part->msg, mime_part);
+	      free (mime_part);
+	      return ret;
+	    }
+	  mu_message_set_header (mime_part->msg, hdr, mime_part);
+	}
+      else
+	{
+	  free (mime_part);
+	  return ret;
+	}
+      mime->header_length = 0;
+      if ((ret =
+	   mu_header_get_value (hdr, MU_HEADER_CONTENT_TYPE, NULL,
+				0, &size)) != 0 || size == 0)
+	{
+	  if (_mime_is_multipart_digest (mime))
+	    mu_header_set_value (hdr,
+				 MU_HEADER_CONTENT_TYPE, "message/rfc822", 0);
+	  else
+	    mu_header_set_value (hdr, MU_HEADER_CONTENT_TYPE, "text/plain",
+				 0);
+	}
+      mime_part->len = len;
+      mime_part->lines = lines;
+      mime_part->offset = offset;
+    }
+  else
+    {
+      mu_message_ref (msg);
+      mu_message_size (msg, &mime_part->len);
+      mu_message_lines (msg, &mime_part->lines);
+      if (mime->nmtp_parts > 1)
+	mime_part->offset = mime->mtp_parts[mime->nmtp_parts - 2]->len;
+      mime_part->msg = msg;
+    }
+  mime_part->mime = mime;
+  return 0;
+}
+
+#define _ISSPECIAL(c) (						 \
+    ((c) == '(') || ((c) == ')') || ((c) == '<') || ((c) == '>') \
+    || ((c) == '@') || ((c) == ',') || ((c) == ';') || ((c) == ':') \
+    || ((c) == '\\') || ((c) == '.') || ((c) == '[') \
+    || ((c) == ']') )
+
+static void
+_mime_munge_content_header (char *field_body)
+{
+  char           *p, *e, *str = field_body;
+  int             quoted = 0;
+
+  mu_str_stripws (field_body);
+
+  if ((e = strchr (str, ';')) == NULL)
+    return;
+  while (*e == ';')
+    {
+      p = e;
+      e++;
+      while (*e && mu_isspace (*e)) /* remove space up to param */
+	e++;
+      memmove (p + 1, e, strlen (e) + 1);
+      e = p + 1;
+
+      while (*e && *e != '=')	/* find end of value */
+	e++;
+      e = p = e + 1;
+      while (*e
+	     && (quoted
+		 || (!_ISSPECIAL (*e) && !mu_isspace (*e))))
+	{
+	  if (*e == '\\')
+	    {			/* escaped */
+	      memmove (e, e + 1, strlen (e));
+	    }
+	  else if (*e == '\"')
+	    quoted = ~quoted;
+	  e++;
+	}
+    }
+}
+
+static char    *
+_mime_get_param (char *field_body, const char *param, int *len)
+{
+  char           *str, *p, *v, *e;
+  int             quoted = 0, was_quoted;
+
+  if (len == NULL || (str = field_body) == NULL)
+    return NULL;
+
+  p = strchr (str, ';');
+  while (p)
+    {
+      p++;
+      if ((v = strchr (p, '=')) == NULL)
+	break;
+      *len = 0;
+      v = e = v + 1;
+      was_quoted = 0;
+      while (*e
+	     && (quoted
+		 || (!_ISSPECIAL (*e) && !mu_isspace (*e))))
+	{			/* skip pass value and calc len */
+	  if (*e == '\"')
+	    quoted = ~quoted, was_quoted = 1;
+	  else
+	    (*len)++;
+	  e++;
+	}
+      if (mu_c_strncasecmp (p, param, strlen (param)))
+	{			/* no match jump to next */
+	  p = strchr (e, ';');
+	  continue;
+	}
+      else
+	return was_quoted ? v + 1 : v;	/* return unquoted value */
+    }
+  return NULL;
+}
+
+static int
+_mime_setup_buffers (mu_mime_t mime)
+{
+  if (mime->cur_buf == NULL
+      && (mime->cur_buf = malloc (mime->buf_size)) == NULL)
+    {
+      return ENOMEM;
+    }
+  if (mime->cur_line == NULL
+      && (mime->cur_line = calloc (mime->line_size, 1)) == NULL)
+    {
+      free (mime->cur_buf);
+      return ENOMEM;
+    }
+  return 0;
+}
+
+static void
+_mime_append_header_line (mu_mime_t mime)
+{
+  if (mime->header_length + mime->line_ndx > mime->header_buf_size)
+    {
+      char           *nhb;
+
+      if ((nhb =
+	   realloc (mime->header_buf,
+		    mime->header_length + mime->line_ndx + 128)) == NULL)
+	return;
+      mime->header_buf = nhb;
+      mime->header_buf_size = mime->header_length + mime->line_ndx + 128;
+    }
+  memcpy (mime->header_buf + mime->header_length, mime->cur_line,
+	  mime->line_ndx);
+  mime->header_length += mime->line_ndx;
+}
+
+static int
+_mime_parse_mpart_message (mu_mime_t mime)
+{
+  char           *cp, *cp2;
+  int             blength, mb_length, mb_offset, mb_lines, ret;
+  size_t          nbytes;
+
+  if (!(mime->flags & MIME_PARSER_ACTIVE))
+    {
+      char           *boundary;
+      int             len;
+
+      if ((ret = _mime_setup_buffers (mime)) != 0)
+	return ret;
+      if ((boundary =
+	   _mime_get_param (mime->content_type, "boundary", &len)) == NULL)
+	return EINVAL;
+      if ((mime->boundary = calloc (1, len + 1)) == NULL)
+	return ENOMEM;
+      strncpy (mime->boundary, boundary, len);
+
+      mime->cur_offset = 0;
+      mime->line_ndx = 0;
+      mime->parser_state = MIME_STATE_SCAN_BOUNDARY;
+      mime->flags |= MIME_PARSER_ACTIVE;
+    }
+  mb_length = mime->body_length;
+  mb_offset = mime->body_offset;
+  mb_lines = mime->body_lines;
+  blength = strlen (mime->boundary);
+
+  mu_stream_seek (mime->stream, mime->cur_offset, MU_SEEK_SET, NULL);
+  while ((ret =
+	  mu_stream_read (mime->stream, mime->cur_buf, mime->buf_size,
+			  &nbytes)) == 0 && nbytes)
+    {
+      cp = mime->cur_buf;
+      while (nbytes)
+	{
+	  mime->cur_line[mime->line_ndx] = *cp;
+	  if (*cp == '\n')
+	    {
+	      switch (mime->parser_state)
+		{
+		case MIME_STATE_BEGIN_LINE:
+		  mime->cur_line[0] = *cp;
+		  mime->line_ndx = 0;
+		  mime->parser_state = MIME_STATE_SCAN_BOUNDARY;
+		  break;
+
+		case MIME_STATE_SCAN_BOUNDARY:
+		  cp2 =
+		    mime->cur_line[0] ==
+		    '\n' ? mime->cur_line + 1 : mime->cur_line;
+		  if (mime->line_ndx >= blength)
+		    {
+		      if ((!strncmp (cp2, "--", 2)
+			   && !mu_c_strncasecmp (cp2 + 2, mime->boundary,
+				  	         blength))
+			  || !mu_c_strncasecmp (cp2, mime->boundary, blength))
+			{
+			  mime->parser_state = MIME_STATE_HEADERS;
+			  mime->flags &= ~MIME_PARSER_HAVE_CR;
+			  mb_length = mime->cur_offset 
+			                   - mb_offset
+			                   - mime->line_ndx;
+			  if (mime->header_length)
+			    /* this skips the preamble */
+			    {
+			      /* RFC 1521 [Page 30]:
+				 NOTE: The CRLF preceding the encapsulation
+				 line is conceptually attached to the boundary
+				 so that it is possible to have a part that
+				 does not end with a CRLF (line break). Body
+				 parts that must be considered to end with line
+				 breaks, therefore, must have two CRLFs
+				 preceding the encapsulation line, the first
+				 of which is part of the preceding body part,
+				 and the second of which is part of the
+				 encapsulation boundary. */
+			      
+			      if (mb_lines)
+				/* to prevent negative values in case of a
+				   malformed message */
+				mb_lines--;
+				  
+			      _mime_append_part (mime, NULL,
+						 mb_offset, mb_length,
+						 mb_lines);
+			    }
+
+			  if ((&mime->cur_line[mime->line_ndx] - cp2 - 1 >
+			       blength
+			       && !strncmp (cp2 + blength + 2, "--", 2))
+			      || (&mime->cur_line[mime->line_ndx] - cp2 - 1 ==
+				  blength
+				  && !strncmp (cp2 + blength, "--", 2)))
+			    {	/* last boundary */
+			      mime->parser_state = MIME_STATE_BEGIN_LINE;
+			      mime->header_length = 0;
+			    }
+			  else
+			    mime->line_ndx = -1; /* headers parsing requires
+						    empty line */
+			  break;
+			}
+		    }
+
+		  if (mime->header_length)
+		    mb_lines++;
+
+		  mime->line_ndx = 0;
+		  mime->cur_line[0] = *cp;	/* stay in this state but
+						   leave '\n' at begining */
+		  break;
+
+		case MIME_STATE_HEADERS:
+		  mime->line_ndx++;
+		  _mime_append_header_line (mime);
+		  if (mime->line_ndx == 1 || mime->cur_line[0] == '\r')
+		    {
+		      mime->parser_state = MIME_STATE_SCAN_BOUNDARY;
+		      mb_offset = mime->cur_offset + 1;
+		      mb_lines = 0;
+		    }
+		  mime->line_ndx = -1;
+		  break;
+		}
+	    }
+	  mime->line_ndx++;
+	  if (mime->line_ndx >= mime->line_size)
+	    {
+	      size_t newsize = mime->line_size + MIME_MAX_HDR_LEN;
+	      char *p = realloc (mime->cur_line, newsize);
+	      if (!p)
+		{
+		  ret = ENOMEM;
+		  break;
+		}
+	      mime->cur_line = p;
+	      mime->line_size = newsize;
+	    }
+	  mime->cur_offset++;
+	  nbytes--;
+	  cp++;
+	}
+    }
+  mime->body_lines = mb_lines;
+  mime->body_length = mb_length;
+  mime->body_offset = mb_offset;
+  if (ret != EAGAIN)
+    {				/* finished cleanup */
+      if (mime->header_length)	/* this skips the preamble */
+	_mime_append_part (mime, NULL, mb_offset, mb_length, mb_lines);
+      mime->flags &= ~MIME_PARSER_ACTIVE;
+      mime->body_offset = mime->body_length =
+	mime->header_length = mime->body_lines = 0;
+    }
+  return ret;
+}
+
+/*------ Mime message functions for READING a multipart message -----*/
+
+static int
+_mimepart_body_size (mu_body_t body, size_t *psize)
+{
+  mu_message_t       msg = mu_body_get_owner (body);
+  struct _mime_part *mime_part = mu_message_get_owner (msg);
+
+  if (mime_part == NULL)
+    return EINVAL;
+  if (psize)
+    *psize = mime_part->len;
+  return 0;
+}
+
+static int
+_mimepart_body_lines (mu_body_t body, size_t *plines)
+{
+  mu_message_t       msg = mu_body_get_owner (body);
+  struct _mime_part *mime_part = mu_message_get_owner (msg);
+
+  if (mime_part == NULL)
+    return EINVAL;
+  if (plines)
+    *plines = mime_part->lines;
+  return 0;
+}
+
+/*------ Mime message/header functions for CREATING multipart message -----*/
+static int
+_mime_set_content_type (mu_mime_t mime)
+{
+  const char  *content_type;
+  mu_header_t     hdr = NULL;
+  size_t          size;
+  int             ret;
+
+  /* Delayed the creation of the header 'til they create the final message via
+     mu_mime_get_message()  */
+  if (mime->hdrs == NULL)
+    return 0;
+  if (mime->nmtp_parts > 1)
+    {
+      char *cstr;
+      
+      if (mime->flags & MIME_ADDED_MULTIPART_CT)
+	return 0;
+      if (mime->flags & MU_MIME_MULTIPART_MIXED)
+	content_type = "multipart/mixed; boundary=";
+      else
+	content_type = "multipart/alternative; boundary=";
+      if (mime->boundary == NULL)
+	{
+	  char boundary[128];
+	  
+	  snprintf (boundary, sizeof boundary, "%ld-%ld=:%ld",
+		    (long) random (), (long) time (0), (long) getpid ());
+	  if ((mime->boundary = strdup (boundary)) == NULL)
+	    return ENOMEM;
+	}
+      size = strlen (content_type) + 2 + strlen (mime->boundary) + 1;
+      cstr = malloc (size);
+      if (!cstr)
+	return ENOMEM;
+      strcpy (cstr, content_type);
+      strcat (cstr, "\"");
+      strcat (cstr, mime->boundary);
+      strcat (cstr, "\"");
+      mime->flags |= MIME_ADDED_MULTIPART_CT;
+
+      ret = mu_header_set_value (mime->hdrs, MU_HEADER_CONTENT_TYPE, cstr, 1);
+      free (cstr);
+    }
+  else
+    {
+      if ((mime->flags & (MIME_ADDED_CT | MIME_ADDED_MULTIPART_CT))
+	  == MIME_ADDED_CT)
+	return 0;
+      mime->flags &= ~MIME_ADDED_MULTIPART_CT;
+      if (mime->nmtp_parts)
+	mu_message_get_header (mime->mtp_parts[0]->msg, &hdr);
+
+      if (hdr == NULL
+	  || mu_header_sget_value (hdr, MU_HEADER_CONTENT_TYPE,
+				   &content_type))
+	content_type = "text/plain; charset=us-ascii";
+
+      ret = mu_header_set_value (mime->hdrs, MU_HEADER_CONTENT_TYPE,
+				 content_type, 1);
+      if (ret)
+	return ret;
+
+      if (hdr)
+	{
+	  const char *content_te;
+	  
+	  /* if the only part contains a transfer-encoding
+	     field, set it on the message header too */
+	  if (mu_header_sget_value (hdr,
+				MU_HEADER_CONTENT_TRANSFER_ENCODING,
+				&content_te) == 0)
+	    ret = mu_header_set_value (mime->hdrs,
+				       MU_HEADER_CONTENT_TRANSFER_ENCODING,
+				       content_te, 1);
+
+	  if (ret == 0
+	      && mu_header_sget_value (hdr,
+				       MU_HEADER_CONTENT_DESCRIPTION,
+				       &content_te) == 0)
+	    ret = mu_header_set_value (mime->hdrs,
+				       MU_HEADER_CONTENT_DESCRIPTION,
+				       content_te, 1);
+
+	}
+    }
+  mime->flags |= MIME_ADDED_CT;
+  return ret;
+}
+
+
+static int
+_mime_part_size (mu_mime_t mime, size_t *psize)
+{
+  int i, ret;
+  size_t size, total = 0;
+
+  if (mime->nmtp_parts == 0)
+    return EINVAL;
+
+  if ((ret = _mime_set_content_type (mime)) != 0)
+    return ret;
+  for (i = 0; i < mime->nmtp_parts; i++)
+    {
+      mu_message_size (mime->mtp_parts[i]->msg, &size);
+      total += size;
+      if (mime->nmtp_parts > 1)	/* boundary line */
+	total += strlen (mime->boundary) + 3;
+    }
+  if (mime->nmtp_parts > 1)	/* ending boundary line */
+    total += 2;
+  *psize = total;
+  return 0;
+}
+
+
+struct _mime_body_stream
+{
+  struct _mu_stream stream;
+  mu_mime_t mime;
+};
+
+static int
+_mime_body_stream_size (mu_stream_t stream, mu_off_t *psize)
+{
+  struct _mime_body_stream *mstr = (struct _mime_body_stream *)stream;
+  mu_mime_t mime = mstr->mime;
+  size_t sz;
+  int rc = _mime_part_size (mime, &sz);
+  if (rc == 0)
+    *psize = sz;
+  return rc;
+}
+
+static void
+mime_reset_state (mu_mime_t mime)
+{				/* reset message */
+  mime->cur_offset = 0;
+  mime->cur_part = 0;
+  mime->part_offset = 0;
+  
+  if (mime->nmtp_parts > 1)
+    mime->flags |= MIME_INSERT_BOUNDARY;
+}
+
+/* FIXME: The seek method is defective */
+static int
+_mime_body_stream_seek (mu_stream_t stream, mu_off_t off, mu_off_t *presult)
+{
+  struct _mime_body_stream *mstr = (struct _mime_body_stream *)stream;
+  mu_mime_t mime = mstr->mime;
+
+  if (off == 0)
+    mime_reset_state (mime);
+
+  if (off != mime->cur_offset)
+    return ESPIPE;
+  *presult = off;
+  return 0;
+}
+
+#define ADD_CHAR(buf, c, offset, buflen, total, nbytes)	\
+  do							\
+    {							\
+      *(buf)++ = c;					\
+      (offset)++;					\
+      (total)++;				        \
+      if (--(buflen) == 0)			        \
+	{						\
+	  *(nbytes) = total;				\
+	  return 0;					\
+	}						\
+    }							\
+  while (0)
+
+static int
+_mime_body_stream_read (mu_stream_t stream, char *buf, size_t buflen, size_t *nbytes)
+{
+  struct _mime_body_stream *mstr = (struct _mime_body_stream *)stream;
+  mu_mime_t mime = mstr->mime;
+  int                ret = 0;
+  size_t             total = 0;
+  
+  if (mime->nmtp_parts == 0)
+    return EINVAL;
+  
+  if ((ret = _mime_set_content_type (mime)) == 0)
+    {
+      do
+	{
+	  size_t             part_nbytes = 0;
+
+	  if (buflen == 0)
+	    break;
+	  if (mime->nmtp_parts > 1)
+	    {
+	      size_t len;
+	      
+	      if (mime->flags & MIME_INSERT_BOUNDARY)
+		{
+		  if ((mime->flags & MIME_ADDING_BOUNDARY) == 0)
+		    {
+		      mime->boundary_len = strlen (mime->boundary);
+		      mime->preamble = 2;
+		      if (mime->cur_part == mime->nmtp_parts)
+			mime->postamble = 2;
+		      mime->flags |= MIME_ADDING_BOUNDARY;
+		    }
+		  while (mime->preamble)
+		    {
+		      mime->preamble--;
+		      ADD_CHAR (buf, '-', mime->cur_offset, buflen,
+				total, nbytes);
+		    }
+		  len = strlen (mime->boundary) - mime->boundary_len;
+		  while (mime->boundary_len)
+		    {
+		      mime->boundary_len--;
+		      ADD_CHAR (buf,
+				mime->boundary[len++],
+				mime->cur_offset, buflen,
+				total, nbytes);
+		    }
+		  while (mime->postamble)
+		    {
+		      mime->postamble--;
+		      ADD_CHAR (buf, '-', mime->cur_offset, buflen,
+				total, nbytes);
+		    }
+		  mime->flags &=
+		    ~(MIME_INSERT_BOUNDARY | MIME_ADDING_BOUNDARY);
+		  mime->part_offset = 0;
+		  ADD_CHAR (buf, '\n', mime->cur_offset, buflen,
+			    total, nbytes);
+		}
+
+	      if (!mime->part_stream)
+		{
+		  if (mime->cur_part >= mime->nmtp_parts)
+		    {
+		      *nbytes = total;
+		      return 0;
+		    }
+		  ret = mu_message_get_streamref (mime->mtp_parts[mime->cur_part]->msg,
+						  &mime->part_stream);
+		}
+	    }
+	  else if (!mime->part_stream)
+	    {
+	      mu_body_t part_body;
+
+	      if (mime->cur_part >= mime->nmtp_parts)
+		{
+		  *nbytes = total;
+		  return 0;
+		}
+	      mu_message_get_body (mime->mtp_parts[mime->cur_part]->msg,
+				   &part_body);
+	      ret = mu_body_get_streamref (part_body, &mime->part_stream);
+	    }
+	  if (ret)
+	    break;
+	  ret = mu_stream_seek (mime->part_stream, mime->part_offset,
+				MU_SEEK_SET, NULL);
+	  if (ret)
+	    {
+	      mu_stream_destroy (&mime->part_stream);
+	      break;
+	    }
+	  while (buflen > 0 &&
+		 (ret = mu_stream_read (mime->part_stream, buf, buflen,
+					&part_nbytes)) == 0)
+	    {
+	      if (part_nbytes)
+		{
+		  mime->part_offset += part_nbytes;
+		  mime->cur_offset += part_nbytes;
+		  total += part_nbytes;
+		  buflen -= part_nbytes;
+		  buf += part_nbytes;
+		}
+	      else 
+		{
+		  mu_stream_destroy (&mime->part_stream);
+		  mime->flags |= MIME_INSERT_BOUNDARY;
+		  mime->cur_part++;
+		  ADD_CHAR (buf, '\n', mime->cur_offset, buflen,
+			    total, nbytes);
+		  break;
+		}
+	    }
+	}
+      while (ret == 0 && mime->cur_part <= mime->nmtp_parts);
+    }
+  if (ret)
+    mu_stream_destroy (&mime->part_stream);
+  
+  *nbytes = total;
+  return ret;
+}
+
+static int
+_mime_body_stream_ioctl (mu_stream_t stream, int code, void *arg)
+{
+  struct _mime_body_stream *mstr = (struct _mime_body_stream *)stream;
+  mu_mime_t mime = mstr->mime;
+  mu_stream_t msg_stream;
+  int rc;
+  
+  switch (code)
+    {
+    case MU_IOCTL_GET_TRANSPORT:
+      if (!arg)
+	return EINVAL;
+      
+      if (mime->nmtp_parts == 0 || mime->cur_offset == 0)
+	return EINVAL;
+      rc = mu_message_get_streamref (mime->mtp_parts[mime->cur_part]->msg,
+				     &msg_stream);
+      if (rc)
+	break;
+      rc = mu_stream_ioctl (msg_stream, code, arg);
+      mu_stream_destroy (&msg_stream);
+      break;
+
+    default:
+      rc = ENOSYS;
+    }
+  return rc;
+}
+
+static int
+create_mime_body_stream (mu_stream_t *pstr, mu_mime_t mime)
+{
+  struct _mime_body_stream *sp =
+    (struct _mime_body_stream *)_mu_stream_create (sizeof (*sp),
+						   MU_STREAM_READ | MU_STREAM_SEEK);
+  if (!sp)
+    return ENOMEM;
+  sp->stream.read = _mime_body_stream_read;
+  sp->stream.seek = _mime_body_stream_seek;
+  sp->stream.ctl = _mime_body_stream_ioctl;
+  sp->stream.size = _mime_body_stream_size;
+  sp->mime = mime;
+  mime_reset_state (mime);
+  *pstr = (mu_stream_t) sp;
+  return 0;
+}
+
+
+static int
+_mime_body_size (mu_body_t body, size_t *psize)
+{
+  mu_message_t       msg = mu_body_get_owner (body);
+  mu_mime_t          mime = mu_message_get_owner (msg);
+  return _mime_part_size (mime, psize);
+}
+
+static int
+_mime_body_lines (mu_body_t body, size_t *plines)
+{
+  mu_message_t       msg = mu_body_get_owner (body);
+  mu_mime_t          mime = mu_message_get_owner (msg);
+  int             i, ret;
+  size_t          lines;
+
+  if (mime->nmtp_parts == 0)
+    return EINVAL;
+
+  if ((ret = _mime_set_content_type (mime)) != 0)
+    return ret;
+  for (i = 0; i < mime->nmtp_parts; i++)
+    {
+      mu_message_lines (mime->mtp_parts[i]->msg, &lines);
+      plines += lines;
+      if (mime->nmtp_parts > 1)	/* boundary line */
+	plines++;
+    }
+  return 0;
+}
+
+int
+mu_mime_create (mu_mime_t *pmime, mu_message_t msg, int flags)
+{
+  mu_mime_t          mime = NULL;
+  int             ret = 0;
+  size_t          size;
+  mu_body_t          body;
+
+  if (pmime == NULL)
+    return EINVAL;
+  *pmime = NULL;
+  if ((mime = calloc (1, sizeof (*mime))) == NULL)
+    return ENOMEM;
+  if (msg)
+    {
+      if ((ret = mu_message_get_header (msg, &mime->hdrs)) == 0)
+	{
+	  if ((ret =
+	       mu_header_get_value (mime->hdrs,
+				    MU_HEADER_CONTENT_TYPE,
+				    NULL, 0, &size)) == 0 && size)
+	    {
+	      if ((mime->content_type = malloc (size + 1)) == NULL)
+		ret = ENOMEM;
+	      else if ((ret = mu_header_get_value (mime->hdrs,
+						   MU_HEADER_CONTENT_TYPE,
+						   mime->content_type,
+						   size + 1, 0)) == 0)
+		_mime_munge_content_header (mime->content_type);
+	    }
+	  else
+	    {
+	      if (ret == MU_ERR_NOENT)
+		{
+		  ret = 0;
+		  if ((mime->content_type =
+		       strdup ("text/plain; charset=us-ascii")) == NULL)
+		    /* default as per spec. */
+		    ret = ENOMEM;
+		}
+	    }
+	  if (ret == 0)
+	    {
+	      mime->msg = msg;
+	      mime->buf_size = MIME_DFLT_BUF_SIZE;
+	      mime->line_size = MIME_MAX_HDR_LEN;
+	      mu_message_get_body (msg, &body);
+	      mu_body_get_streamref (body, &mime->stream);
+	    }
+	}
+    }
+  else
+    {
+      mime->flags |= MIME_NEW_MESSAGE | MU_MIME_MULTIPART_MIXED;
+    }
+  if (ret != 0)
+    {
+      if (mime->content_type)
+	free (mime->content_type);
+      free (mime);
+    }
+  else
+    {
+      mime->flags |= (flags & MIME_FLAG_MASK);
+      *pmime = mime;
+    }
+  return ret;
+}
+
+void
+mu_mime_destroy (mu_mime_t *pmime)
+{
+  mu_mime_t          mime;
+  struct _mime_part *mime_part;
+  int             i;
+
+  if (pmime && *pmime)
+    {
+      mime = *pmime;
+      if (mime->mtp_parts != NULL)
+	{
+	  for (i = 0; i < mime->nmtp_parts; i++)
+	    {
+	      mime_part = mime->mtp_parts[i];
+	      if (mime_part->msg && mime->flags & MIME_NEW_MESSAGE)
+		mu_message_unref (mime_part->msg);
+	      else
+		mu_message_destroy (&mime_part->msg, mime_part);
+	      free (mime_part);
+	    }
+	  free (mime->mtp_parts);
+	}
+      mu_stream_destroy (&mime->stream);
+      mu_stream_destroy (&mime->part_stream);
+      if (mime->msg && mime->flags & MIME_NEW_MESSAGE)
+	mu_message_destroy (&mime->msg, mime);
+      if (mime->content_type)
+	free (mime->content_type);
+      if (mime->cur_buf)
+	free (mime->cur_buf);
+      if (mime->cur_line)
+	free (mime->cur_line);
+      if (mime->boundary)
+	free (mime->boundary);
+      if (mime->header_buf)
+	free (mime->header_buf);
+      free (mime);
+      *pmime = NULL;
+    }
+}
+
+int
+mu_mime_get_part (mu_mime_t mime, size_t part, mu_message_t *msg)
+{
+  size_t          nmtp_parts;
+  int             ret = 0, flags = 0;
+  mu_stream_t        stream;
+  mu_body_t          body;
+  struct _mime_part *mime_part;
+
+  if ((ret = mu_mime_get_num_parts (mime, &nmtp_parts)) == 0)
+    {
+      if (part < 1 || part > nmtp_parts)
+	return MU_ERR_NOENT;
+      if (nmtp_parts == 1 && mime->mtp_parts == NULL)
+	*msg = mime->msg;
+      else
+	{
+	  mime_part = mime->mtp_parts[part - 1];
+	  if (!mime_part->body_created
+	      && (ret = mu_body_create (&body, mime_part->msg)) == 0)
+	    {
+	      mu_body_set_size (body, _mimepart_body_size, mime_part->msg);
+	      mu_body_set_lines (body, _mimepart_body_lines, mime_part->msg);
+	      mu_stream_get_flags (mime->stream, &flags);
+	      ret = mu_streamref_create_abridged (&stream, mime->stream,
+						  mime_part->offset,
+						  mime_part->offset +
+						    mime_part->len - 1);
+	      if (ret == 0)
+		{
+		  mu_stream_set_flags (stream,
+				       MU_STREAM_READ | (flags &
+							 (MU_STREAM_SEEK
+							  | MU_STREAM_NONBLOCK)));
+		  mu_body_set_stream (body, stream, mime_part->msg);
+		  mu_message_set_body (mime_part->msg, body, mime_part);
+		  mime_part->body_created = 1;
+		}
+	    }
+	  *msg = mime_part->msg;
+	}
+    }
+  return ret;
+}
+
+int
+mu_mime_get_num_parts (mu_mime_t mime, size_t *nmtp_parts)
+{
+  int             ret = 0;
+
+  if ((mime->nmtp_parts == 0 && !mime->boundary)
+      || mime->flags & MIME_PARSER_ACTIVE)
+    {
+      if (mu_mime_is_multipart (mime))
+	{
+	  if ((ret = _mime_parse_mpart_message (mime)) != 0)
+	    return (ret);
+	}
+      else
+	mime->nmtp_parts = 1;
+    }
+  *nmtp_parts = mime->nmtp_parts;
+  return (ret);
+
+}
+
+int
+mu_mime_add_part (mu_mime_t mime, mu_message_t msg)
+{
+  int             ret;
+
+  if (mime == NULL || msg == NULL || (mime->flags & MIME_NEW_MESSAGE) == 0)
+    return EINVAL;
+  if ((ret = _mime_append_part (mime, msg, 0, 0, 0)) == 0)
+    ret = _mime_set_content_type (mime);
+  return ret;
+}
+
+int
+mu_mime_get_message (mu_mime_t mime, mu_message_t *msg)
+{
+  mu_stream_t        body_stream;
+  mu_body_t          body;
+  int             ret = 0;
+
+  if (mime == NULL || msg == NULL)
+    return EINVAL;
+  if (mime->msg == NULL)
+    {
+      if ((mime->flags & MIME_NEW_MESSAGE) == 0)
+	return EINVAL;
+      if ((ret = mu_message_create (&mime->msg, mime)) == 0)
+	{
+	  if ((ret = mu_header_create (&mime->hdrs, NULL, 0)) == 0)
+	    {
+	      mu_message_set_header (mime->msg, mime->hdrs, mime);
+	      mu_header_set_value (mime->hdrs, MU_HEADER_MIME_VERSION, "1.0",
+				   0);
+	      if ((ret = _mime_set_content_type (mime)) == 0)
+		{
+		  if ((ret = mu_body_create (&body, mime->msg)) == 0)
+		    {
+		      mu_message_set_body (mime->msg, body, mime);
+		      mu_body_set_size (body, _mime_body_size, mime->msg);
+		      mu_body_set_lines (body, _mime_body_lines, mime->msg);
+		      ret = create_mime_body_stream (&body_stream, mime);
+		      if (ret == 0)
+			{
+			  mu_body_set_stream (body, body_stream, mime->msg);
+			  *msg = mime->msg;
+			  return 0;
+			}
+		    }
+		}
+	    }
+	  mu_message_destroy (&mime->msg, mime);
+	  mime->msg = NULL;
+	}
+    }
+  if (ret == 0)
+    *msg = mime->msg;
+  return ret;
+}
+
+int
+mu_mime_is_multipart (mu_mime_t mime)
+{
+  if (mime->content_type)
+    return (mu_c_strncasecmp ("multipart", mime->content_type,
+			      strlen ("multipart")) ? 0 : 1);
+  return 0;
+}
diff --git a/libmailutils/mimehdr.c b/libmailutils/mime/mimehdr.c
similarity index 100%
rename from libmailutils/mimehdr.c
rename to libmailutils/mime/mimehdr.c
diff --git a/libmailutils/monitor.c b/libmailutils/monitor.c
deleted file mode 100644
index 45e7dc9..0000000
--- a/libmailutils/monitor.c
+++ /dev/null
@@ -1,282 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 1999, 2000, 2001, 2004, 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 */
-
-/* Tell GLIBC that we want UNIX98 pthread_rwlock_xx() functions.  */
-#define _XOPEN_SOURCE   500
-/* Tell QNX/Neutrino to define pthread_rwlock_xx() functions.  */
-#define _QNX_SOURCE
-#define _POSIX_C_SOURCE 199506
-#ifdef HAVE_CONFIG_H
-#  include <config.h>
-#endif
-
-#ifdef WITH_PTHREAD
-#  ifdef HAVE_PTHREAD_H
-#    include <pthread.h>
-#  endif
-#endif
-#include <errno.h>
-#include <stdlib.h>
-
-#include <mailutils/sys/monitor.h>
-#include <mailutils/errno.h>
-
-#ifdef WITH_PTHREAD
-pthread_mutex_t monitor_lock = PTHREAD_MUTEX_INITIALIZER;
-#  define STATIC_LOCK(m) pthread_mutex_lock(m)
-#  define STATIC_UNLOCK(m) pthread_mutex_unlock(m)
-#else
-#  define STATIC_LOCK(m) 0
-#  define STATIC_UNLOCK(m)
-int monitor_lock;
-#endif
-
-union _p_lock
-{
-#ifdef WITH_PTHREAD
-#  ifdef HAVE_PTHREAD_RWLOCK_INIT
-     pthread_rwlock_t mutex;
-#  else
-     pthread_mutex_t mutex;
-#  endif
-#else
-  int dummy;
-#endif
-};
-
-typedef union _p_lock *p_lock_t;
-static int  monitor_pthread_create (p_lock_t *);
-static void monitor_pthread_destroy (p_lock_t *);
-static int monitor_pthread_rdlock (p_lock_t);
-static int monitor_pthread_wrlock (p_lock_t);
-static int monitor_pthread_unlock (p_lock_t);
-
-/* The idea was to have a general/portable object mu_monitor_t.
-   The mu_monitor_t object could have different implementation (on the fly ?)
-   of locking.  Also the rest of the library would not have to know about
-   different threading implementation.  So far we've pretty much hardcoded
-   the concrete implementation of monitor on pthread and read/write locks,
-   but changing to a different concrete implementation will not be hard if
-   the need arise.
-   For static initializers we take a small penality and since we have
-   a global static lock.
- */
-
-int
-mu_monitor_create (mu_monitor_t *pmonitor, int flags, void *owner)
-{
-  mu_monitor_t monitor;
-
-  if (pmonitor == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-
-  monitor = calloc (1, sizeof (*monitor));
-  if (monitor == NULL)
-    return ENOMEM;
-
-  if (flags == MU_MONITOR_PTHREAD)
-    {
-      int status = monitor_pthread_create ((p_lock_t *)&(monitor->data));
-      if (status != 0)
-	{
-	  free (monitor);
-	  return status;
-	}
-    }
-  monitor->owner = owner;
-  monitor->allocated = 1;
-  monitor->flags = flags;
-  *pmonitor = monitor;
-  return 0;
-}
-
-void *
-mu_monitor_get_owner (mu_monitor_t monitor)
-{
-  return (monitor == NULL) ? NULL : monitor->owner;
-}
-
-void
-mu_monitor_destroy (mu_monitor_t *pmonitor, void *owner)
-{
-  if (pmonitor && *pmonitor)
-    {
-      mu_monitor_t monitor = *pmonitor;
-      if (monitor->owner == owner)
-	{
-	  if (monitor->flags == MU_MONITOR_PTHREAD)
-	    monitor_pthread_destroy ((p_lock_t *)&(monitor->data));
-	}
-      free (monitor);
-      *pmonitor = NULL;
-    }
-}
-
-int
-mu_monitor_rdlock (mu_monitor_t monitor)
-{
-  if (monitor)
-    {
-      if (!monitor->allocated)
-	{
-	  int status = STATIC_LOCK (&monitor_lock);
-	  if (monitor->data == NULL)
-	    {
-	      if (monitor->flags == MU_MONITOR_PTHREAD)
-		status = monitor_pthread_create ((p_lock_t*)&(monitor->data));
-	      if (status != 0)
-		{
-		  STATIC_UNLOCK (&monitor_lock);
-		  return status;
-		}
-	    }
-	  monitor->allocated = 1;
-	  STATIC_UNLOCK (&monitor_lock);
-	}
-      if (monitor->flags == MU_MONITOR_PTHREAD)
-	return monitor_pthread_rdlock ((p_lock_t)monitor->data);
-    }
-  return 0;
-}
-
-int
-mu_monitor_wrlock  (mu_monitor_t monitor)
-{
-  if (monitor)
-    {
-      if (!monitor->allocated)
-	{
-	  int status = STATIC_LOCK (&monitor_lock);
-	  if (monitor->data == NULL)
-	    {
-	      if (monitor->flags == MU_MONITOR_PTHREAD)
-		status = monitor_pthread_create ((p_lock_t *)&(monitor->data));
-	      if (status != 0)
-		{
-		  STATIC_UNLOCK (&monitor_lock);
-		  return status;
-		}
-	    }
-	  monitor->allocated = 1;
-	  STATIC_UNLOCK (&monitor_lock);
-	}
-      if (monitor->flags == MU_MONITOR_PTHREAD)
-	return monitor_pthread_wrlock ((p_lock_t)monitor->data);
-    }
-  return 0;
-}
-
-int
-mu_monitor_unlock (mu_monitor_t monitor)
-{
-  if (monitor)
-    {
-      if (monitor->flags == MU_MONITOR_PTHREAD)
-	return monitor_pthread_unlock ((p_lock_t)monitor->data);
-    }
-  return 0;
-}
-
-int
-mu_monitor_wait (mu_monitor_t monitor MU_ARG_UNUSED)
-{
-  return ENOSYS;
-}
-
-int
-mu_monitor_notify (mu_monitor_t monitor MU_ARG_UNUSED)
-{
-  return ENOSYS;
-}
-
-
-/* Concrete Implementation of pthread base on rwlocks.  */
-
-#ifdef WITH_PTHREAD
-#  ifdef HAVE_PTHREAD_RWLOCK_INIT
-#    define RWLOCK_INIT(rwl, attr)  pthread_rwlock_init (rwl, attr)
-#    define RWLOCK_DESTROY(rwl)     pthread_rwlock_destroy (rwl)
-#    define RWLOCK_RDLOCK(rwl)      pthread_rwlock_rdlock (rwl)
-#    define RWLOCK_WRLOCK(rwl)      pthread_rwlock_wrlock (rwl)
-#    define RWLOCK_UNLOCK(rwl)      pthread_rwlock_unlock (rwl)
-#  else
-#    define RWLOCK_INIT(rwl, attr)  pthread_mutex_init (rwl, attr)
-#    define RWLOCK_DESTROY(rwl)     pthread_mutex_destroy (rwl)
-#    define RWLOCK_RDLOCK(rwl)      pthread_mutex_lock (rwl)
-#    define RWLOCK_WRLOCK(rwl)      pthread_mutex_lock (rwl)
-#    define RWLOCK_UNLOCK(rwl)      pthread_mutex_unlock (rwl)
-#  endif
-#else
-#  define RWLOCK_INIT(rwl, attr)    0
-#  define RWLOCK_DESTROY(rwl)
-#  define RWLOCK_RDLOCK(rwl)        0
-#  define RWLOCK_WRLOCK(rwl)        0
-#  define RWLOCK_UNLOCK(rwl)        0
-#  define flockfile(arg)
-#  define funlockfile(arg)
-#endif
-
-
-
-static int
-monitor_pthread_create (p_lock_t *plock)
-{
-  int status;
-  p_lock_t lock = calloc (1, sizeof (*lock));
-  if (lock == NULL)
-    return ENOMEM;
-  status = RWLOCK_INIT (&(lock->mutex), NULL);
-  if (status != 0)
-    {
-      free (lock);
-      return status;
-    }
-  *plock = lock;
-  return 0;
-}
-
-static void
-monitor_pthread_destroy (p_lock_t *plock)
-{
-  p_lock_t lock = *plock;
-  if (lock)
-    {
-      RWLOCK_DESTROY (&(lock->mutex));
-      free (lock);
-    }
-  *plock = NULL;
-}
-
-static int
-monitor_pthread_rdlock (p_lock_t lock)
-{
-  return RWLOCK_RDLOCK (&(lock->mutex));
-}
-
-static int
-monitor_pthread_wrlock (p_lock_t lock)
-{
-  return RWLOCK_WRLOCK (&(lock->mutex));
-}
-
-static int
-monitor_pthread_unlock (p_lock_t lock)
-{
-  return RWLOCK_UNLOCK (&(lock->mutex));
-}
diff --git a/libmailutils/mu_auth.c b/libmailutils/mu_auth.c
deleted file mode 100644
index bba6b45..0000000
--- a/libmailutils/mu_auth.c
+++ /dev/null
@@ -1,539 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 2002, 2004, 2005, 2006, 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 <unistd.h>
-#include <sys/types.h>
-#include <pwd.h>
-#ifdef HAVE_SHADOW_H
-# include <shadow.h>
-#endif
-#include <errno.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#ifdef HAVE_STRINGS_H
-# include <strings.h>
-#endif
-#ifdef HAVE_SECURITY_PAM_APPL_H
-# include <security/pam_appl.h>
-#endif
-#ifdef HAVE_CRYPT_H
-# include <crypt.h>
-#endif
-
-#include <mailutils/argcv.h>
-#include <mailutils/list.h>
-#include <mailutils/iterator.h>
-#include <mailutils/mailbox.h>
-#include <mailutils/mu_auth.h>
-#include <mailutils/error.h>
-#include <mailutils/errno.h>
-#include <mailutils/nls.h>
-#include <mailutils/debug.h>
-
-static mu_debug_t mu_auth_debug;
-
-#define S(s) ((s) ? (s) : "(none)")
-#define DEBUG_AUTH(a)                                                         \
-     MU_DEBUG11 (mu_auth_debug, MU_DEBUG_TRACE,                               \
-                       "source=%s, name=%s, passwd=%s, uid=%lu, gid=%lu, "    \
-                       "gecos=%s, dir=%s, shell=%s, mailbox=%s, quota=%lu, "  \
-                       "change_uid=%d\n",                                     \
-                       S ((a)->source),                                       \
-                       S ((a)->name),                                         \
-                       S ((a)->passwd),                                       \
-                       (unsigned long) (a)->uid,                              \
-                       (unsigned long) (a)->gid,                              \
-                       S ((a)->gecos),                                        \
-                       S ((a)->dir),                                          \
-                       S ((a)->shell),                                        \
-                       S ((a)->mailbox),                                      \
-                       (unsigned long) (a)->quota,                            \
-                       (a)->change_uid)
-     
-mu_debug_t
-mu_auth_set_debug (mu_debug_t debug)
-{
-  mu_debug_t prev = mu_auth_debug;
-  mu_auth_debug = debug;
-  return prev;
-}
-
-
-/* memory allocation */
-int 
-mu_auth_data_alloc (struct mu_auth_data **ptr,
-                    const char *name,
-                    const char *passwd,
-                    uid_t uid,
-                    gid_t gid,
-                    const char *gecos,
-                    const char *dir,
-                    const char *shell,
-                    const char *mailbox,
-                    int change_uid)
-{
-  size_t size;
-  char *p;
-  char *tmp_mailbox_name = NULL;
-  
-  if (!name)
-    return EINVAL;
-  if (!passwd)
-    passwd = "x";
-  if (!gecos)
-    gecos = "";
-  if (!dir)
-    dir = "/nonexisting";
-  if (!shell)
-    shell = "/dev/null";
-  if (!mailbox)
-    {
-      int rc = mu_construct_user_mailbox_url (&tmp_mailbox_name, name);
-      if (rc)
-	return rc;
-      mailbox = tmp_mailbox_name;
-    }
-
-  size = sizeof (**ptr) +
-         strlen (name) + 1 +
-         strlen (passwd) + 1 +
-         strlen (gecos) + 1 +
-         strlen (dir) + 1 +
-         strlen (shell) + 1 +
-         strlen (mailbox) + 1;
-  
-  *ptr = calloc (1, size);
-  if (!*ptr)
-    return ENOMEM;
-
-  p = (char *)(*ptr + 1);
-  
-#define COPY(f) \
-  (*ptr)->f = p; \
-  strcpy (p, f); \
-  p += strlen (f) + 1
-  
-  COPY (name);
-  COPY (passwd);
-  COPY (gecos);
-  COPY (dir);
-  COPY (shell);
-  COPY (mailbox);
-  (*ptr)->uid = uid;
-  (*ptr)->gid = gid;
-  (*ptr)->change_uid = change_uid;
-
-  free (tmp_mailbox_name);
-  return 0;
-}
-
-void
-mu_auth_data_set_quota (struct mu_auth_data *ptr, mu_off_t q)
-{
-  ptr->flags |= MU_AF_QUOTA;
-  ptr->quota = q;
-}
-
-void
-mu_auth_data_free (struct mu_auth_data *ptr)
-{
-  free (ptr);
-}
-
-void
-mu_auth_data_destroy (struct mu_auth_data **pptr)
-{
-  if (pptr)
-    {
-      mu_auth_data_free (*pptr);
-      *pptr = NULL;
-    }
-}
-
-/* Generic functions */
-
-struct auth_stack_entry {
-  char *name;        /* for diagnostics purposes */
-  mu_auth_fp fun;
-  void *func_data;
-};
-
-void
-mu_insert_stack_entry (mu_list_t *pflist, struct auth_stack_entry *entry)
-{
-  if (!*pflist && mu_list_create (pflist))
-    return;
-  mu_list_append (*pflist, entry);
-}
-
-int
-mu_auth_runlist (mu_list_t flist, struct mu_auth_data **return_data,
-                 const void *key, void *data)
-{
-  int status = MU_ERR_AUTH_FAILURE;
-  int rc;
-  mu_iterator_t itr;
-
-  if (mu_list_get_iterator (flist, &itr) == 0)
-    {
-      struct auth_stack_entry *ep;
-      
-      for (mu_iterator_first (itr); !mu_iterator_is_done (itr);
-           mu_iterator_next (itr))
-        {
-          mu_iterator_current (itr, (void **)&ep);
-          MU_DEBUG1 (mu_auth_debug, MU_DEBUG_TRACE, "Trying %s...", ep->name);
-          rc = ep->fun (return_data, key, ep->func_data, data);
-          MU_DEBUG2 (mu_auth_debug, MU_DEBUG_TRACE, 
-                     "result: %d=%s\n", rc, mu_strerror (rc));
-          if (rc == 0)
-            {
-              if (return_data)
-                {
-                  struct mu_auth_data *auth = *return_data;
-                  if (auth->source == NULL)
-                    auth->source = ep->name;
-                  DEBUG_AUTH (auth);
-                }
-              status = rc;
-              break;
-            }
-          else if (rc == ENOSYS && status != 0)
-	    /* nothing: do not overwrite last meaningful return code */;
-	  else if (status != EAGAIN)
-            status = rc;
-        }
-
-      mu_iterator_destroy (&itr);
-    }
-  return status;
-}
-
-int
-mu_auth_nosupport (struct mu_auth_data **return_data MU_ARG_UNUSED,
-                   const void *key MU_ARG_UNUSED,
-                   void *func_data MU_ARG_UNUSED,
-                   void *call_data MU_ARG_UNUSED)
-{
-  return ENOSYS;
-}
-
-/* II. Authorization: retrieving information about user */
-
-static mu_list_t mu_auth_by_name_list, _tmp_auth_by_name_list;
-static mu_list_t mu_auth_by_uid_list, _tmp_auth_by_uid_list;
-
-int
-mu_get_auth (struct mu_auth_data **auth, enum mu_auth_key_type type,
-             const void *key)
-{
-  mu_list_t list;
-  
-  if (!mu_auth_by_name_list)
-    mu_auth_begin_setup ();
-  switch (type)
-    {
-    case mu_auth_key_name:
-      MU_DEBUG1 (mu_auth_debug, MU_DEBUG_TRACE, 
-                 "Getting auth info for user %s\n", 
-                 (char*) key);
-      list = mu_auth_by_name_list;
-      break;
-
-    case mu_auth_key_uid:
-      MU_DEBUG1 (mu_auth_debug, MU_DEBUG_TRACE, 
-                 "Getting auth info for UID %lu\n",
-	         (unsigned long) *(uid_t*) key);
-      list = mu_auth_by_uid_list;
-      break;
-
-    default:
-      MU_DEBUG1 (mu_auth_debug, MU_DEBUG_ERROR, 
-                 "Unknown mu_auth_key_type: %d\n", type);
-      return EINVAL;
-    }
-  return mu_auth_runlist (list, auth, key, NULL);
-}
-
-struct mu_auth_data *
-mu_get_auth_by_name (const char *username)
-{
-  struct mu_auth_data *auth = NULL;
-  mu_get_auth (&auth, mu_auth_key_name, username);
-  return auth;
-}
-
-struct mu_auth_data *
-mu_get_auth_by_uid (uid_t uid)
-{
-  struct mu_auth_data *auth = NULL;
-  mu_get_auth (&auth, mu_auth_key_uid, &uid);
-  return auth;
-}
-
-/* III. Authentication: determining the authenticity of a user */
-
-static mu_list_t mu_authenticate_list, _tmp_authenticate_list;
-
-int
-mu_authenticate (struct mu_auth_data *auth_data, const char *pass)
-{
-  if (!auth_data)
-    return EINVAL;
-  MU_DEBUG2 (mu_auth_debug, MU_DEBUG_TRACE, 
-             "mu_authenticate, user %s, source %s\n", 
-             auth_data->name, auth_data->source);
-  if (!mu_authenticate_list)
-    mu_auth_begin_setup ();
-  return mu_auth_runlist (mu_authenticate_list, NULL, auth_data, (void*) pass);
-}
-
-
-/* ************************************************************************* */
-
-struct _module_handler {
-  struct auth_stack_entry authenticate;
-  struct auth_stack_entry auth_by_name;
-  struct auth_stack_entry auth_by_uid;
-};
-
-static mu_list_t module_handler_list;
-
-void
-mu_auth_register_module (struct mu_auth_module *mod)
-{
-  struct _module_handler *entry;
-  
-  if (mod->init)
-    mu_gocs_register (mod->name, mod->init);
-  
-  if (!module_handler_list && mu_list_create (&module_handler_list))
-    abort ();
-
-  entry = malloc (sizeof (*entry));
-  if (!entry)
-    {
-      mu_error ("not enough memory");
-      exit (1);
-    }
-  entry->authenticate.name = mod->name;
-  entry->authenticate.fun  = mod->authenticate;
-  entry->authenticate.func_data = mod->authenticate_data;
-  entry->auth_by_name.name = mod->name;
-  entry->auth_by_name.fun = mod->auth_by_name;
-  entry->auth_by_name.func_data = mod->auth_by_name_data;
-  entry->auth_by_uid.name = mod->name;
-  entry->auth_by_uid.fun = mod->auth_by_uid;
-  entry->auth_by_uid.func_data = mod->auth_by_uid_data;
-    
-  mu_list_append (module_handler_list, entry);
-  
-}
-
-static struct _module_handler *
-_locate (const char *name)
-{
-  struct _module_handler *rp = NULL;
-  mu_iterator_t itr;
-
-  if (mu_list_get_iterator (module_handler_list, &itr) == 0)
-    {
-      struct _module_handler *p;
-      
-      for (mu_iterator_first (itr); !rp && !mu_iterator_is_done (itr);
-           mu_iterator_next (itr))
-        {
-          mu_iterator_current (itr, (void **)&p);
-          if (strcmp (p->authenticate.name, name) == 0)
-            rp = p;
-        }
-
-      mu_iterator_destroy (&itr);
-    }
-  return rp;
-}
-
-static void
-_add_module_list (const char *modlist, int (*fun)(const char *name))
-{
-  int argc;
-  char **argv;
-  int rc, i;
-  
-  rc = mu_argcv_get (modlist, ":", NULL, &argc, &argv);
-  if (rc)
-    {
-      mu_error (_("cannot split line `%s': %s"), modlist, mu_strerror (rc));
-      exit (1);
-    }
-
-  for (i = 0; i < argc; i += 2)
-    {
-      if (fun (argv[i]))
-        {
-          /* Historically,auth functions used ENOENT. We support this
-             return value for backward compatibility. */
-          if (errno == ENOENT || errno == MU_ERR_NOENT)
-            mu_error (_("no such module: %s"), argv[i]);
-          else
-            mu_error (_("failed to add module %s: %s"),
-                      argv[i], strerror (errno));
-          exit (1);
-        }
-    }
-  mu_argcv_free (argc, argv);
-}
-
-
-int
-mu_authorization_add_module (const char *name)
-{
-  struct _module_handler *mod = _locate (name);
-  
-  if (!mod)
-    {
-      errno = MU_ERR_NOENT;
-      return 1;
-    }
-  mu_insert_stack_entry (&_tmp_auth_by_name_list, &mod->auth_by_name);
-  mu_insert_stack_entry (&_tmp_auth_by_uid_list, &mod->auth_by_uid);
-  return 0;
-}
-
-void
-mu_authorization_add_module_list (const char *modlist)
-{
-  _add_module_list (modlist, mu_authorization_add_module);
-}
-
-void
-mu_authorization_clear_list ()
-{
-  mu_list_destroy (&_tmp_auth_by_name_list);
-  mu_list_destroy (&_tmp_auth_by_uid_list);
-}
-
-
-int
-mu_authentication_add_module (const char *name)
-{
-  struct _module_handler *mod = _locate (name);
-
-  if (!mod)
-    {
-      errno = MU_ERR_NOENT;
-      return 1;
-    }
-  mu_insert_stack_entry (&_tmp_authenticate_list, &mod->authenticate);
-  return 0;
-}
-
-void
-mu_authentication_add_module_list (const char *modlist)
-{
-  _add_module_list (modlist, mu_authentication_add_module);
-}
-
-void
-mu_authentication_clear_list ()
-{
-  mu_list_destroy (&_tmp_authenticate_list);
-}
-
-
-/* ************************************************************************ */
-
-/* Setup functions. Note that:
-   1) If libmuath is not linked in, then "generic" and "system" modules
-      are used unconditionally. This provides compatibility with the
-      standard getpw.* functions.
-   2) --authentication and --authorization modify only temporary lists,
-      which get flushed to the main ones when mu_auth_finish_setup() is
-      run. Thus, the default "generic:system" remain in force until
-      argp_parse() exits. */
-   
-void
-mu_auth_begin_setup ()
-{
-  mu_iterator_t itr;
-
-  if (!module_handler_list)
-    {
-      mu_auth_register_module (&mu_auth_generic_module); 
-      mu_auth_register_module (&mu_auth_system_module);
-    }
-  
-  if (!mu_authenticate_list)
-    {
-      if (mu_list_get_iterator (module_handler_list, &itr) == 0)
-        {
-          struct _module_handler *mod;
-          
-          for (mu_iterator_first (itr); !mu_iterator_is_done (itr);
-               mu_iterator_next (itr))
-            {
-              mu_iterator_current (itr, (void **)&mod);
-              mu_insert_stack_entry (&mu_authenticate_list,
-                                     &mod->authenticate);
-            }
-          mu_iterator_destroy (&itr);
-        }
-    }
-
-  if (!mu_auth_by_name_list)
-    {
-      if (mu_list_get_iterator (module_handler_list, &itr) == 0)
-        {
-          struct _module_handler *mod;
-          
-          for (mu_iterator_first (itr); !mu_iterator_is_done (itr);
-               mu_iterator_next (itr))
-            {
-              mu_iterator_current (itr, (void **)&mod);
-              mu_insert_stack_entry (&mu_auth_by_name_list, &mod->auth_by_name);
-              mu_insert_stack_entry (&mu_auth_by_uid_list, &mod->auth_by_uid);
-            }
-          mu_iterator_destroy (&itr);
-        }
-    }
-}
-
-void
-mu_auth_finish_setup ()
-{
-  mu_list_destroy (&mu_authenticate_list);
-  mu_list_destroy (&mu_auth_by_name_list);
-  mu_list_destroy (&mu_auth_by_uid_list);
-  
-  mu_authenticate_list = _tmp_authenticate_list;
-  _tmp_authenticate_list = NULL;
-  mu_auth_by_name_list = _tmp_auth_by_name_list;
-  _tmp_auth_by_name_list = NULL;
-  mu_auth_by_uid_list = _tmp_auth_by_uid_list;
-  _tmp_auth_by_uid_list = NULL;
-  
-  mu_auth_begin_setup ();
-}
-
-
diff --git a/libmailutils/muerrno.cin b/libmailutils/muerrno.cin
deleted file mode 100644
index f00fb94..0000000
--- a/libmailutils/muerrno.cin
+++ /dev/null
@@ -1,65 +0,0 @@
-/* -*- c -*- $AUTOWARN
-   GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 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  */
-
-#ifdef HAVE_CONFIG_H
-# include <config.h>
-#endif
-
-#include <stdio.h>
-#include <errno.h>
-#include <string.h>
-
-#include <mailutils/errno.h>
-#include <mailutils/nls.h>
-
-#ifndef EOK
-# define EOK 0
-#endif
-
-const char*
-mu_errname (int e)
-{
-  static char buf[128];
-  
-  switch (e)
-    {
-    case EOK:
-      return "EOK";
-
-      $MESSAGE_CODES
-    }
-
-  snprintf (buf, sizeof buf, _("Error %d"), e);
-  return buf;
-}
-
-const char *
-mu_strerror (int e)
-{
-  switch (e)
-    {
-    case EOK:
-      return _("Success");
-
-    $MESSAGE_STRINGS
-    }
-
-  return strerror (e);
-}
-
diff --git a/libmailutils/muerror.c b/libmailutils/muerror.c
deleted file mode 100644
index e18f0fe..0000000
--- a/libmailutils/muerror.c
+++ /dev/null
@@ -1,117 +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 */
-
-#ifdef HAVE_CONFIG_H
-# include <config.h>
-#endif
-
-#include <stdlib.h>
-#include <stdio.h>
-#include <syslog.h>
-#include <string.h>
-#include <mailutils/error.h>
-
-
-/* Historic shortcuts for mu_diag_ functions */
-
-int
-mu_verror (const char *fmt, va_list ap)
-{
-  mu_diag_voutput (MU_DIAG_ERROR, fmt, ap);
-  return 0;
-}
-
-int
-mu_error (const char *fmt, ...)
-{
-  va_list ap;
-  va_start (ap, fmt);
-  mu_verror (fmt, ap);
-  va_end (ap);
-  return 0;
-}
-
-
-/* Compatibility layer */
-int
-mu_default_error_printer (const char *fmt, va_list ap)
-{
-  if (mu_program_name)
-    fprintf (stderr, "%s: ", mu_program_name);
-  vfprintf (stderr, fmt, ap);
-  fputc ('\n', stderr);
-  return 0;
-}
-
-int
-mu_syslog_error_printer (const char *fmt, va_list ap)
-{
-#ifdef HAVE_VSYSLOG
-  vsyslog (LOG_CRIT, fmt, ap);
-#else
-  char buf[128];
-  vsnprintf (buf, sizeof buf, fmt, ap);
-  syslog (LOG_CRIT, "%s", buf);
-#endif
-  return 0;
-}
-
-static void
-compat_error_printer0 (mu_error_pfn_t pfn, const char *fmt, ...)
-{
-  va_list ap;
-  va_start (ap, fmt);
-  pfn (fmt, ap);
-  va_end (ap);
-}
-
-static int
-compat_error_printer (void *data, mu_log_level_t level, const char *buf)
-{
-  if (!data)
-    mu_diag_stderr_printer (NULL, level, buf);
-  else
-    {
-      int len = strlen (buf);
-      if (len > 0 && buf[len-1] == '\n')
-	len--;
-      compat_error_printer0 (data, "%-.*s", len, buf);
-    }
-  return 0;
-}
-
-void
-mu_error_set_print (mu_error_pfn_t pfn)
-{
-  mu_debug_t debug;
-  mu_diag_get_debug (&debug);
-  mu_debug_set_print (debug, compat_error_printer, NULL);
-  mu_debug_set_data (debug, pfn, NULL, NULL);
-#if 0
- {
-   static int warned;
-   if (!warned)
-     {
-       warned = 1;
-       mu_diag_output ("this program uses mu_error_set_print, which is deprecated");
-     }
-#endif
-}
-
-
diff --git a/libmailutils/munre.c b/libmailutils/munre.c
deleted file mode 100644
index ca66cad..0000000
--- a/libmailutils/munre.c
+++ /dev/null
@@ -1,93 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 1999, 2000, 2001, 2002, 2003, 2006, 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 */
-
-#if HAVE_CONFIG_H
-# include <config.h>
-#endif
-
-#include <errno.h>
-#include <stdlib.h>
-
-#include <mailutils/error.h>
-#include <mailutils/errno.h>
-
-#include <regex.h>
-
-static regex_t *re_prefix;
-
-int
-mu_unre_set_regex (const char *str, int caseflag, char **errp)
-{
-  int rc;
-  int flags = REG_EXTENDED;
-
-  if (errp)
-    *errp = NULL;
-
-  if (!str)
-    str = "^re: *";
-  if (re_prefix)
-    regfree (re_prefix);
-  else
-    {
-      re_prefix = malloc (sizeof (*re_prefix));
-      if (!re_prefix)
-	return ENOMEM;
-    }
-  if (!caseflag)
-    flags |= REG_ICASE;
-  rc = regcomp (re_prefix, str, flags);
-  if (rc)
-    {
-      if (errp)
-	{
-	  size_t s = regerror (rc, re_prefix, NULL, 0);
-	  s++;
-	  *errp = malloc (s);
-	  if (*errp)
-	    regerror (rc, re_prefix, *errp, s);
-	}
-      regfree (re_prefix);
-      free (re_prefix);
-      return MU_ERR_FAILURE;
-    }
-  return 0;
-}
-
-int
-mu_unre_subject (const char *subject, const char **new_subject)
-{
-  int rc;
-  regmatch_t rm;
-
-  if (!subject)
-    return EINVAL;
-  
-  if (!re_prefix)
-    {
-      rc = mu_unre_set_regex (NULL, 0, NULL);
-      if (rc)
-	return rc;
-    }
-
-  rc = regexec (re_prefix, subject, 1, &rm, 0);
-  if (rc == 0 && rm.rm_eo != -1 && new_subject)
-    *new_subject = subject + rm.rm_eo;
-  return rc;
-}
diff --git a/libmailutils/mutil.c b/libmailutils/mutil.c
deleted file mode 100644
index 2b091ce..0000000
--- a/libmailutils/mutil.c
+++ /dev/null
@@ -1,1536 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 1999, 2000, 2001, 2002, 2003, 2005, 2006, 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 */
-
-#if HAVE_CONFIG_H
-# include <config.h>
-#endif
-
-#include <errno.h>
-#include <limits.h>
-#include <netdb.h>
-#include <pwd.h>
-#include <signal.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <time.h>
-#include <unistd.h>
-#include <ctype.h>
-#include <sys/param.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <sys/time.h>
-#include <sys/wait.h>
-#include <sys/select.h>
-
-#ifdef HAVE_STRINGS_H
-#include <strings.h>
-#endif
-
-#include <mailutils/address.h>
-#include <mailutils/assoc.h>
-#include <mailutils/argcv.h>
-#include <mailutils/error.h>
-#include <mailutils/errno.h>
-#include <mailutils/iterator.h>
-#include <mailutils/mutil.h>
-#include <mailutils/parse822.h>
-#include <mailutils/mu_auth.h>
-#include <mailutils/header.h>
-#include <mailutils/message.h>
-#include <mailutils/envelope.h>
-#include <mailutils/nls.h>
-#include <mailutils/stream.h>
-#include <mailutils/filter.h>
-#include <mailutils/sql.h>
-#include <mailutils/url.h>
-#include <mailutils/io.h>
-#include <mailutils/cctype.h>
-#include <mailutils/cstr.h>
-
-#include <mailutils/sys/registrar.h>
-
-/* convert a sequence of hex characters into an integer */
-
-unsigned long
-mu_hex2ul (char hex)
-{
-  if (hex >= '0' && hex <= '9')
-   return hex - '0';
-
-  if (hex >= 'a' && hex <= 'z')
-    return hex - 'a' + 10;
-
-  if (hex >= 'A' && hex <= 'Z')
-    return hex - 'A' + 10;
-
-  return -1;
-}
-
-size_t
-mu_hexstr2ul (unsigned long *ul, const char *hex, size_t len)
-{
-  size_t r;
-
-  *ul = 0;
-
-  for (r = 0; r < len; r++)
-    {
-      unsigned long v = mu_hex2ul (hex[r]);
-
-      if (v == (unsigned long)-1)
-	return r;
-
-      *ul = *ul * 16 + v;
-    }
-  return r;
-}
-
-
-char *
-mu_get_homedir (void)
-{
-  char *homedir = getenv ("HOME");
-  if (homedir)
-    homedir = strdup (homedir);
-  else
-    {
-      struct mu_auth_data *auth = mu_get_auth_by_uid (geteuid ());
-      if (!auth)
-	return NULL;
-      homedir = strdup (auth->dir);
-      mu_auth_data_free (auth);
-    }
-  return homedir;
-}
-
-char *
-mu_getcwd ()
-{
-  char *ret;
-  unsigned path_max;
-  char buf[128];
-
-  errno = 0;
-  ret = getcwd (buf, sizeof (buf));
-  if (ret != NULL)
-    return strdup (buf);
-
-  if (errno != ERANGE)
-    return NULL;
-
-  path_max = 128;
-  path_max += 2;                /* The getcwd docs say to do this. */
-
-  for (;;)
-    {
-      char *cwd = (char *) malloc (path_max);
-
-      errno = 0;
-      ret = getcwd (cwd, path_max);
-      if (ret != NULL)
-        return ret;
-      if (errno != ERANGE)
-        {
-          int save_errno = errno;
-          free (cwd);
-          errno = save_errno;
-          return NULL;
-        }
-
-      free (cwd);
-
-      path_max += path_max / 16;
-      path_max += 32;
-    }
-  /* oops?  */
-  return NULL;
-}
-
-char *
-mu_get_full_path (const char *file)
-{
-  char *p = NULL;
-
-  if (!file)
-    p = mu_getcwd ();
-  else if (*file != '/')
-    {
-      char *cwd = mu_getcwd ();
-      if (cwd)
-	{
-	  p = calloc (strlen (cwd) + 1 + strlen (file) + 1, 1);
-	  if (p)
-	    sprintf (p, "%s/%s", cwd, file);
-	  free (cwd);
-	}
-    }
-
-  if (!p)
-    p = strdup (file);
-  return p;
-}
-
-/* NOTE: Allocates Memory.  */
-/* Expand: ~ --> /home/user and to ~guest --> /home/guest.  */
-char *
-mu_tilde_expansion (const char *ref, const char *delim, const char *homedir)
-{
-  char *base = strdup (ref);
-  char *home = NULL;
-  char *proto = NULL;
-  size_t proto_len = 0;
-  char *p;
-
-  for (p = base; *p && isascii (*p) && isalnum (*p); p++)
-    ;
-  
-  if (*p == ':')
-    {
-      p++;
-      proto_len = p - base;
-      proto = malloc (proto_len + 1);
-      if (!proto)
-	return NULL;
-      memcpy (proto, base, proto_len);
-      proto[proto_len] = 0;
-      /* Allow for extra pair of slashes after the protocol specifier */
-      if (*p == delim[0])
-	p++;
-      if (*p == delim[0])
-	p++;
-    }
-  else
-    p = base;
-  
-  if (*p == '~')
-    {
-      p++;
-      if (*p == delim[0] || *p == '\0')
-        {
-	  char *s;
-	  if (!homedir)
-	    {
-	      home = mu_get_homedir ();
-	      if (!home)
-		return base;
-	      homedir = home;
-	    }
-	  s = calloc (proto_len + strlen (homedir) + strlen (p) + 1, 1);
-	  if (proto_len)
-	    strcpy (s, proto);
-	  else
-	    s[0] = 0;
-          strcat (s, homedir);
-          strcat (s, p);
-          free (base);
-          base = s;
-        }
-      else
-        {
-          struct mu_auth_data *auth;
-          char *s = p;
-          char *name;
-          while (*s && *s != delim[0])
-            s++;
-          name = calloc (s - p + 1, 1);
-          memcpy (name, p, s - p);
-          name[s - p] = '\0';
-	  
-          auth = mu_get_auth_by_name (name);
-          free (name);
-          if (auth)
-            {
-              char *buf = calloc (proto_len + strlen (auth->dir)
-				  + strlen (s) + 1, 1);
-	      if (proto_len)
-		strcpy (buf, proto);
-	      else
-		buf[0] = 0;
-	      strcat (buf, auth->dir);
-              strcat (buf, s);
-              free (base);
-              base = buf;
-	      mu_auth_data_free (auth);
-            }
-        }
-    }
-  if (home)
-    free (home);
-  return base;
-}
-
-/* Smart strncpy that always add the null and returns the number of bytes
-   written.  */
-size_t
-mu_cpystr (char *dst, const char *src, size_t size)
-{
-  size_t len = src ? strlen (src) : 0 ;
-  if (dst == NULL || size == 0)
-    return len;
-  if (len >= size)
-    len = size - 1;
-  memcpy (dst, src, len);
-  dst[len] = '\0';
-  return len;
-}
-
-#ifndef MAXHOSTNAMELEN
-# define MAXHOSTNAMELEN 64
-#endif
-
-int
-mu_get_host_name (char **host)
-{
-  char *hostname = NULL;
-  size_t size = 0;
-  char *p;
-
-  while (1)
-    {
-      if (size == 0)
-	{
-	  size = MAXHOSTNAMELEN;
-	  p = malloc (size);
-	}
-      else
-	{
-	  size_t ns = size * 2;
-	  if (ns < size)
-	    {
-	      free (hostname);
-	      return ENOMEM;
-	    }
-	  size = ns;
-	  p = realloc (hostname, size);
-	}
-      if (!p)
-	{
-	  free (hostname);
-	  return ENOMEM;
-	}
-      hostname = p;
-      hostname[size - 1] = 0;
-      if (gethostname (hostname, size - 1) == 0)
-	{
-	  if (!hostname[size - 1])
-	    break;
-	}
-      else if (errno != 0 && errno != ENAMETOOLONG && errno != EINVAL
-	       && errno != ENOMEM)
-	{
-	  int rc = errno;
-	  free (hostname);
-	  return rc;
-	}
-    }
-
-  /* Try to return fully qualified host name */
-  if (!strchr (hostname, '.'))
-    {
-      struct hostent *hp = gethostbyname (hostname);
-      if (hp)
-	{
-	  size_t len = strlen (hp->h_name);
-	  if (size < len + 1)
-	    {
-	      p = realloc (hostname, len + 1);
-	      if (!p)
-		{
-		  free (hostname);
-		  return ENOMEM;
-		}
-	      hostname = p;
-	    }
-	  strcpy (hostname, hp->h_name);
-	}
-    }
-  
-  *host = hostname;
-  return 0;
-}  
-
-/*
- * Functions used to convert unix mailbox/user names into RFC822 addr-specs.
- */
-
-static char *mu_user_email = 0;
-
-int
-mu_set_user_email (const char *candidate)
-{
-  int err = 0;
-  mu_address_t addr = NULL;
-  size_t emailno = 0;
-  char *email = NULL;
-  const char *domain = NULL;
-  
-  if ((err = mu_address_create (&addr, candidate)) != 0)
-    return err;
-
-  if ((err = mu_address_get_email_count (addr, &emailno)) != 0)
-    goto cleanup;
-
-  if (emailno != 1)
-    {
-      errno = EINVAL;
-      goto cleanup;
-    }
-
-  if ((err = mu_address_aget_email (addr, 1, &email)) != 0)
-    goto cleanup;
-
-  if (mu_user_email)
-    free (mu_user_email);
-
-  mu_user_email = email;
-
-  if ((err = mu_address_sget_domain (addr, 1, &domain)) == 0)
-    mu_set_user_email_domain (domain);
-  
-cleanup:
-  mu_address_destroy (&addr);
-
-  return err;
-}
-
-static char *mu_user_email_domain = 0;
-
-int
-mu_set_user_email_domain (const char *domain)
-{
-  char *d = NULL;
-  
-  if (!domain)
-    return EINVAL;
-  
-  d = strdup (domain);
-
-  if (!d)
-    return ENOMEM;
-
-  if (mu_user_email_domain)
-    free (mu_user_email_domain);
-
-  mu_user_email_domain = d;
-
-  return 0;
-}
-
-/* FIXME: must be called _sget_ */
-int
-mu_get_user_email_domain (const char **domain)
-{
-  int err = 0;
-
-  if (!mu_user_email_domain)
-    {
-      if ((err = mu_get_host_name (&mu_user_email_domain)))
-	return err;
-    }
-
-  *domain = mu_user_email_domain;
-
-  return 0;
-}
-
-int
-mu_aget_user_email_domain (char **pdomain)
-{
-  const char *domain;
-  int status = mu_get_user_email_domain (&domain);
-  if (status)
-    return status;
-  if (domain == NULL)
-    *pdomain = NULL;
-  else
-    {
-      *pdomain = strdup (domain);
-      if (*pdomain == NULL)
-	return ENOMEM;
-    }
-  return 0;
-}
-
-/* Note: allocates memory */
-char *
-mu_get_user_email (const char *name)
-{
-  int status = 0;
-  char *localpart = NULL;
-  const char *domainpart = NULL;
-  char *email = NULL;
-  char *tmpname = NULL;
-
-  if (!name && mu_user_email)
-    {
-      email = strdup (mu_user_email);
-      if (!email)
-	errno = ENOMEM;
-      return email;
-    }
-
-  if (!name)
-    {
-      struct mu_auth_data *auth = mu_get_auth_by_uid (getuid ());
-      if (!auth)
-	{
-	  errno = EINVAL;
-	  return NULL;
-	}
-      name = tmpname = strdup(auth->name);
-      if (auth)
-	mu_auth_data_free (auth);
-    }
-
-  status = mu_get_user_email_domain (&domainpart);
-
-  if (status)
-    {
-      free(tmpname);
-      errno = status;
-      return NULL;
-    }
-
-  if ((status = mu_parse822_quote_local_part (&localpart, name)))
-    {
-      free(tmpname);
-      errno = status;
-      return NULL;
-    }
-
-
-  email = malloc (strlen (localpart) + 1
-		  + strlen (domainpart) + 1);
-  if (!email)
-    errno = ENOMEM;
-  else
-    sprintf (email, "%s@%s", localpart, domainpart);
-
-  free(tmpname);
-  free (localpart);
-
-  return email;
-}
-
-/* mu_normalize_path: convert pathname containig relative paths specs (../)
-   into an equivalent absolute path. Strip trailing delimiter if present,
-   unless it is the only character left. E.g.:
-
-         /home/user/../smith   -->   /home/smith
-	 /home/user/../..      -->   /
-
-*/
-char *
-mu_normalize_path (char *path)
-{
-  int len;
-  char *p;
-
-  if (!path)
-    return path;
-
-  len = strlen (path);
-
-  /* Empty string is returned as is */
-  if (len == 0)
-    return path;
-
-  /* delete trailing delimiter if any */
-  if (len && path[len-1] == '/')
-    path[len-1] = 0;
-
-  /* Eliminate any /../ */
-  for (p = strchr (path, '.'); p; p = strchr (p, '.'))
-    {
-      if (p > path && p[-1] == '/')
-	{
-	  if (p[1] == '.' && (p[2] == 0 || p[2] == '/'))
-	    /* found */
-	    {
-	      char *q, *s;
-
-	      /* Find previous delimiter */
-	      for (q = p-2; *q != '/' && q >= path; q--)
-		;
-
-	      if (q < path)
-		break;
-	      /* Copy stuff */
-	      s = p + 2;
-	      p = q;
-	      while ((*q++ = *s++))
-		;
-	      continue;
-	    }
-	}
-
-      p++;
-    }
-
-  if (path[0] == 0)
-    {
-      path[0] = '/';
-      path[1] = 0;
-    }
-
-  return path;
-}
-
-/* Create and open a temporary file. Be very careful about it, since we
-   may be running with extra privilege i.e setgid().
-   Returns file descriptor of the open file.
-   If namep is not NULL, the pointer to the malloced file name will
-   be stored there. Otherwise, the file is unlinked right after open,
-   i.e. it will disappear after close(fd). */
-
-#ifndef P_tmpdir
-# define P_tmpdir "/tmp"
-#endif
-
-int
-mu_tempfile (const char *tmpdir, char **namep)
-{
-  char *filename;
-  int fd;
-
-  if (!tmpdir)
-    tmpdir = (getenv ("TMPDIR")) ? getenv ("TMPDIR") : P_tmpdir;
-
-  filename = malloc (strlen (tmpdir) + /*'/'*/1 + /* "muXXXXXX" */8 + 1);
-  if (!filename)
-    return -1;
-  sprintf (filename, "%s/muXXXXXX", tmpdir);
-
-#ifdef HAVE_MKSTEMP
-  {
-    int save_mask = umask (077);
-    fd = mkstemp (filename);
-    umask (save_mask);
-  }
-#else
-  if (mktemp (filename))
-    fd = open (filename, O_CREAT|O_EXCL|O_RDWR, 0600);
-  else
-    fd = -1;
-#endif
-
-  if (fd == -1)
-    {
-      mu_error (_("cannot open temporary file: %s"), mu_strerror (errno));
-      free (filename);
-      return -1;
-    }
-
-  if (namep)
-    *namep = filename;
-  else
-    {
-      unlink (filename);
-      free (filename);
-    }
-
-  return fd;
-}
-
-/* Create a unique temporary file name in tmpdir. The function
-   creates an empty file with this name to avoid possible race
-   conditions. Returns a pointer to the malloc'ed file name.
-   If tmpdir is NULL, the value of the environment variable
-   TMPDIR or the hardcoded P_tmpdir is used, whichever is defined. */
-
-char *
-mu_tempname (const char *tmpdir)
-{
-  char *filename = NULL;
-  int fd = mu_tempfile (tmpdir, &filename);
-  close (fd);
-  return filename;
-}
-
-/* See Advanced Programming in the UNIX Environment, Stevens,
- * program  10.20 for the rational for the signal handling. I
- * had to look it up, so if somebody else is curious, thats where
- * to find it.
- */
-int 
-mu_spawnvp (const char *prog, char *av[], int *stat)
-{
-  pid_t pid;
-  int err = 0;
-  int progstat;
-  struct sigaction ignore;
-  struct sigaction saveintr;
-  struct sigaction savequit;
-  sigset_t chldmask;
-  sigset_t savemask;
-
-  if (!prog || !av)
-    return EINVAL;
-
-  ignore.sa_handler = SIG_IGN;	/* ignore SIGINT and SIGQUIT */
-  ignore.sa_flags = 0;
-  sigemptyset (&ignore.sa_mask);
-
-  if (sigaction (SIGINT, &ignore, &saveintr) < 0)
-    return errno;
-  if (sigaction (SIGQUIT, &ignore, &savequit) < 0)
-    {
-      sigaction (SIGINT, &saveintr, NULL);
-      return errno;
-    }
-
-  sigemptyset (&chldmask);	/* now block SIGCHLD */
-  sigaddset (&chldmask, SIGCHLD);
-
-  if (sigprocmask (SIG_BLOCK, &chldmask, &savemask) < 0)
-    {
-      sigaction (SIGINT, &saveintr, NULL);
-      sigaction (SIGQUIT, &savequit, NULL);
-      return errno;
-    }
-
-#ifdef HAVE_VFORK
-  pid = vfork ();
-#else
-  pid = fork ();
-#endif
-
-  if (pid < 0)
-    {
-      err = errno;
-    }
-  else if (pid == 0)
-    {				/* child */
-      /* restore previous signal actions & reset signal mask */
-      sigaction (SIGINT, &saveintr, NULL);
-      sigaction (SIGQUIT, &savequit, NULL);
-      sigprocmask (SIG_SETMASK, &savemask, NULL);
-
-      execvp (prog, av);
-#ifdef HAVE__EXIT      
-      _exit (127);		/* exec error */
-#else
-      exit (127);
-#endif
-    }
-  else
-    {				/* parent */
-      while (waitpid (pid, &progstat, 0) < 0)
-	if (errno != EINTR)
-	  {
-	    err = errno;	/* error other than EINTR from waitpid() */
-	    break;
-	  }
-      if (err == 0 && stat)
-	*stat = progstat;
-    }
-
-  /* restore previous signal actions & reset signal mask */
-  /* preserve first error number, but still try and reset the signals */
-  if (sigaction (SIGINT, &saveintr, NULL) < 0)
-    err = err ? err : errno;
-  if (sigaction (SIGQUIT, &savequit, NULL) < 0)
-    err = err ? err : errno;
-  if (sigprocmask (SIG_SETMASK, &savemask, NULL) < 0)
-    err = err ? err : errno;
-
-  return err;
-}
-
-/* The result of readlink() may be a path relative to that link, 
- * qualify it if necessary.
- */
-static void
-mu_qualify_link (const char *path, const char *link, char *qualified)
-{
-  const char *lb = NULL;
-  size_t len;
-
-  /* link is full path */
-  if (*link == '/')
-    {
-      mu_cpystr (qualified, link, _POSIX_PATH_MAX);
-      return;
-    }
-
-  if ((lb = strrchr (path, '/')) == NULL)
-    {
-      /* no path in link */
-      mu_cpystr (qualified, link, _POSIX_PATH_MAX);
-      return;
-    }
-
-  len = lb - path + 1;
-  memcpy (qualified, path, len);
-  mu_cpystr (qualified + len, link, _POSIX_PATH_MAX - len);
-}
-
-#ifndef _POSIX_SYMLOOP_MAX
-# define _POSIX_SYMLOOP_MAX 255
-#endif
-
-int
-mu_unroll_symlink (char *out, size_t outsz, const char *in)
-{
-  char path[_POSIX_PATH_MAX];
-  int symloops = 0;
-
-  while (symloops++ < _POSIX_SYMLOOP_MAX)
-    {
-      struct stat s;
-      char link[_POSIX_PATH_MAX];
-      char qualified[_POSIX_PATH_MAX];
-      int len;
-
-      if (lstat (in, &s) == -1)
-	return errno;
-
-      if (!S_ISLNK (s.st_mode))
-	{
-	  mu_cpystr (path, in, sizeof (path));
-	  break;
-	}
-
-      if ((len = readlink (in, link, sizeof (link))) == -1)
-	return errno;
-
-      link[(len >= sizeof (link)) ? (sizeof (link) - 1) : len] = '\0';
-
-      mu_qualify_link (in, link, qualified);
-
-      mu_cpystr (path, qualified, sizeof (path));
-
-      in = path;
-    }
-
-  mu_cpystr (out, path, outsz);
-
-  return 0;
-}
-
-/* Expand a PATTERN to the pathname. PATTERN may contain the following
-   macro-notations:
-   ---------+------------ 
-   notation |  expands to
-   ---------+------------
-   %u         user name
-   %h         user's home dir
-   ~          Likewise
-   ---------+------------
-
-   Allocates memory. 
-*/
-char *
-mu_expand_path_pattern (const char *pattern, const char *username)
-{
-  const char *p;
-  char *q;
-  char *path;
-  size_t len = 0;
-  struct mu_auth_data *auth = NULL;
-  
-  for (p = pattern; *p; p++)
-    {
-      if (*p == '~')
-        {
-          if (!auth)
-            {
-              auth = mu_get_auth_by_name (username);
-              if (!auth)
-                return NULL;
-            }
-          len += strlen (auth->dir);
-        }
-      else if (*p == '%')
-	switch (*++p)
-	  {
-	  case 'u':
-	    len += strlen (username);
-	    break;
-	    
-	  case 'h':
-	    if (!auth)
-	      {
-		auth = mu_get_auth_by_name (username);
-		if (!auth)
-		  return NULL;
-	      }
-	    len += strlen (auth->dir);
-	    break;
-	    
-	  case '%':
-	    len++;
-	    break;
-	    
-	  default:
-	    len += 2;
-	  }
-      else
-	len++;
-    }
-  
-  path = malloc (len + 1);
-  if (!path)
-    return NULL;
-
-  p = pattern;
-  q = path;
-  while (*p)
-    {
-      size_t off = strcspn (p, "~%");
-      memcpy (q, p, off);
-      q += off;
-      p += off;
-      if (*p == '~')
-	{
-	  strcpy (q, auth->dir);
-	  q += strlen (auth->dir);
-	  p++;
-	}
-      else if (*p == '%')
-	{
-	  switch (*++p)
-	    {
-	    case 'u':
-	      strcpy (q, username);
-	      q += strlen (username);
-	      break;
-	    
-	    case 'h':
-	      strcpy (q, auth->dir);
-	      q += strlen (auth->dir);
-	      break;
-	  
-	    case '%':
-	      *q++ = '%';
-	      break;
-	  
-	    default:
-	      *q++ = '%';
-	      *q++ = *p;
-	    }
-	  p++;
-	}
-    }
-
-  *q = 0;
-  if (auth)
-    mu_auth_data_free (auth);
-  return path;
-}
-
-#define ST_INIT  0
-#define ST_MSGID 1
-
-static int
-strip_message_id (char *msgid, char **pval)
-{
-  char *p, *q;
-  int state;
-  
-  *pval = strdup (msgid);
-  if (!*pval)
-    return ENOMEM;
-  state = ST_INIT;
-  for (p = q = *pval; *p; p++)
-    {
-      switch (state)
-	{
-	case ST_INIT:
-	  if (*p == '<')
-	    {
-	      *q++ = *p;
-	      state = ST_MSGID;
-	    }
-	  else if (isspace (*p))
-	    *q++ = *p;
-	  break;
-
-	case ST_MSGID:
-	  *q++ = *p;
-	  if (*p == '>')
-	    state = ST_INIT;
-	  break;
-	}
-    }
-  *q = 0;
-  return 0;
-}
-
-int
-get_msgid_header (mu_header_t hdr, const char *name, char **val)
-{
-  char *p;
-  int status = mu_header_aget_value (hdr, name, &p);
-  if (status)
-    return status;
-  status = strip_message_id (p, val);
-  free (p);
-  return status;
-}
-
-static char *
-concat (const char *s1, const char *s2)
-{
-  int len = (s1 ? strlen (s1) : 0) + (s2 ? strlen (s2) : 0) + 2;
-  char *s = malloc (len);
-  if (s)
-    {
-      char *p = s;
-      
-      if (s1)
-	{
-	  strcpy (p, s1);
-	  p += strlen (s1);
-	  *p++ = ' ';
-	}
-      if (s2)
-	strcpy (p, s2);
-    }
-  return s;
-}
-
-/* rfc2822:
-   
-   The "References:" field will contain the contents of the parent's
-   "References:" field (if any) followed by the contents of the parent's
-   "Message-ID:" field (if any).  If the parent message does not contain
-   a "References:" field but does have an "In-Reply-To:" field
-   containing a single message identifier, then the "References:" field
-   will contain the contents of the parent's "In-Reply-To:" field
-   followed by the contents of the parent's "Message-ID:" field (if
-   any).  If the parent has none of the "References:", "In-Reply-To:",
-   or "Message-ID:" fields, then the new message will have no
-   References:" field. */
-
-int
-mu_rfc2822_references (mu_message_t msg, char **pstr)
-{
-  char *ref = NULL, *msgid = NULL;
-  mu_header_t hdr;
-  int rc;
-  
-  rc = mu_message_get_header (msg, &hdr);
-  if (rc)
-    return rc;
-  get_msgid_header (hdr, MU_HEADER_MESSAGE_ID, &msgid);
-  if (get_msgid_header (hdr, MU_HEADER_REFERENCES, &ref))
-    get_msgid_header (hdr, MU_HEADER_IN_REPLY_TO, &ref);
-
-  if (ref || msgid)
-    {
-      *pstr = concat (ref, msgid);
-      free (ref);
-      free (msgid);
-      return 0;
-    }
-  return MU_ERR_FAILURE;
-}
-
-int
-mu_rfc2822_msg_id (int subpart, char **pval)
-{
-  char date[4+2+2+2+2+2+1];
-  time_t t = time (NULL);
-  struct tm *tm = localtime (&t);
-  char *host;
-  char *p;
-	  
-  mu_strftime (date, sizeof date, "%Y%m%d%H%M%S", tm);
-  mu_get_host_name (&host);
-
-  if (subpart)
-    {
-      struct timeval tv;
-      gettimeofday (&tv, NULL);
-      mu_asprintf (&p, "<%s.%lu.%d@%s>",
-		   date,
-		   (unsigned long) getpid (),
-		   subpart,
-		   host);
-    }
-  else
-    mu_asprintf (&p, "<%s.%lu@%s>", date, (unsigned long) getpid (), host);
-  free (host);
-  *pval = p;
-  return 0;
-}
-
-#define DATEBUFSIZE 128
-#define COMMENT "Your message of "
-
-/*
-   The "In-Reply-To:" field will contain the contents of the "Message-
-   ID:" field of the message to which this one is a reply (the "parent
-   message").  If there is more than one parent message, then the "In-
-   Reply-To:" field will contain the contents of all of the parents'
-   "Message-ID:" fields.  If there is no "Message-ID:" field in any of
-   the parent messages, then the new message will have no "In-Reply-To:"
-   field.
-*/
-int
-mu_rfc2822_in_reply_to (mu_message_t msg, char **pstr)
-{
-  const char *value = NULL;
-  char *s1 = NULL, *s2 = NULL;
-  mu_header_t hdr;
-  int rc;
-  
-  rc = mu_message_get_header (msg, &hdr);
-  if (rc)
-    return rc;
-  
-  if (mu_header_sget_value (hdr, MU_HEADER_DATE, &value))
-    {
-      mu_envelope_t envelope = NULL;
-      mu_message_get_envelope (msg, &envelope);
-      mu_envelope_sget_date (envelope, &value);
-    }
-
-  if (value)
-    {
-      s1 = malloc (sizeof (COMMENT) + strlen (value));
-      if (!s1)
-	return ENOMEM;
-      strcat (strcpy (s1, COMMENT), value);
-    }
-  
-  if (mu_header_sget_value (hdr, MU_HEADER_MESSAGE_ID, &value) == 0)
-    {
-      s2 = malloc (strlen (value) + 3);
-      if (!s2)
-	{
-	  free (s1);
-	  return ENOMEM;
-	}
-      strcat (strcpy (s2, "\n\t"), value);
-    }
-
-  if (s1 || s2)
-    {
-      *pstr = concat (s1, s2);
-      free (s1);
-      free (s2);
-      return 0;
-    }
-  return MU_ERR_FAILURE;
-}
-
-/* Based on strstr from GNU libc (Stephen R. van den Berg,
-   berg@pool.informatik.rwth-aachen.de) */
-
-char *
-mu_strcasestr (const char *a_haystack, const char *a_needle)
-{
-  register const unsigned char *haystack = (unsigned char*) a_haystack,
-    *needle = (unsigned char*) a_needle;
-  register unsigned int b, c;
-
-#define U(c) mu_toupper (c)
-  if ((b = U (*needle)))
-    {
-      haystack--;		
-      do
-	{
-	  if (!(c = *++haystack))
-	    goto ret0;
-	}
-      while (U (c) != b);
-
-      if (!(c = *++needle))
-	goto foundneedle;
-
-      c = U (c);
-      ++needle;
-      goto jin;
-
-      for (;;)
-        { 
-          register unsigned int a;
-	  register const unsigned char *rhaystack, *rneedle;
-
-	  do
-	    {
-	      if (!(a = *++haystack))
-		goto ret0;
-	      if (U (a) == b)
-		break;
-	      if (!(a = *++haystack))
-		goto ret0;
-shloop:       ;
-            }
-          while (U (a) != b);
-	  
-jin:     if (!(a = *++haystack))
-	    goto ret0;
-
-	  if (U (a) != c)
-	    goto shloop;
-
-	  if (U (*(rhaystack = haystack-- + 1)) ==
-	      (a = U (*(rneedle = needle))))
-	    do
-	      {
-		if (!a)
-		  goto foundneedle;
-		if (U (*++rhaystack) != (a = U (*++needle)))
-		  break;
-		if (!a)
-		  goto foundneedle;
-	      }
-	    while (U (*++rhaystack) == (a = U (*++needle)));
-
-	  needle = rneedle;
-
-	  if (!a)
-	    break;
-        }
-    }
-foundneedle:
-  return (char*)haystack;
-ret0:
-  return NULL;
-
-#undef U
-}  
-
-int
-mu_string_unfold (char *text, size_t *plen)
-{
-  char *p, *q;
-  enum uf_state { uf_init, uf_nl, uf_fold } state = uf_init;
-#define ISSPACE(c) (c == '\r' || c == ' ' || c == '\t')
-  
-  if (!text)
-    return EINVAL;
-  
-  for (p = q = text; *q; q++)
-    {
-      switch (state)
-	{
-	case uf_init:
-	  if (*q == '\n')
-	    state = uf_nl;
-	  else
-	    *p++ = *q;
-	  break;
-
-	case uf_nl:
-	  if (ISSPACE (*q))
-	    state = uf_fold;
-	  else
-	    {
-	      state = uf_init;
-	      *p++ = *q;
-	    }
-	  break;
-
-	case uf_fold:
-	  if (!ISSPACE (*q))
-	    {
-	      *p++ = ' ';
-	      *p++ = *q;
-	      state = uf_init;
-	    }
-	  break;
-	}
-    }
-  
-  *p++ = 0;
-  if (plen)
-    *plen = p - text;
-  return 0;
-}
-
-int
-mu_true_answer_p (const char *p)
-{
-  if (!p)
-    return -1;
-
-  while (*p && isspace (*p))
-    p++;
-
-  if (*p)
-    {
-      /* TRANSLATORS: This is a list of characters which start
-	 an affirmative answer. Whenever possible, please preserve
-	 'yY' in your translation, e.g., for Euskara:
-
-	 msgstr "yYbB";
-      */
-      if (strchr (_("yY"), *p))
-	return 1;
-
-      /* TRANSLATORS: This is a list of characters which start
-	 a negative answer. Whenever possible, please preserve
-	 'nN' in your translation, e.g., for Euskara:
-
-	 msgstr "nNeE";
-      */
-      else if (strchr (_("nN"), *p))
-	return 0;
-    }
-  return -1;
-}
-
-/* Returns true if SCHEME represents a local (autodetect) mail folder.  */
-int
-mu_scheme_autodetect_p (mu_url_t url)
-{
-  if (mu_url_is_scheme (url, "file"))
-    {
-      mu_url_expand_path (url);
-      return 1;
-    }
-  return 0;
-}
-    
-int
-mu_fd_wait (int fd, int *pflags, struct timeval *tvp)
-{
-  fd_set rdset, wrset, exset;
-  int rc;
-
-  FD_ZERO (&rdset);
-  FD_ZERO (&wrset);
-  FD_ZERO (&exset);
-  if ((*pflags) & MU_STREAM_READY_RD)
-    FD_SET (fd, &rdset);
-  if ((*pflags) & MU_STREAM_READY_WR)
-    FD_SET (fd, &wrset);
-  if ((*pflags) & MU_STREAM_READY_EX)
-    FD_SET (fd, &exset);
-  
-  do
-    {
-      if (tvp)
-	{
-	  struct timeval tv = *tvp; 
-	  rc = select (fd + 1, &rdset, &wrset, &exset, &tv);
-	}
-      else
-	rc = select (fd + 1, &rdset, &wrset, &exset, NULL);
-    }
-  while (rc == -1 && errno == EINTR);
-
-  if (rc < 0)
-    return errno;
-  
-  *pflags = 0;
-  if (rc > 0)
-    {
-      if (FD_ISSET (fd, &rdset))
-	*pflags |= MU_STREAM_READY_RD;
-      if (FD_ISSET (fd, &wrset))
-	*pflags |= MU_STREAM_READY_WR;
-      if (FD_ISSET (fd, &exset))
-	*pflags |= MU_STREAM_READY_EX;
-    }
-  return 0;
-}
-
-enum mu_iconv_fallback_mode mu_default_fallback_mode = mu_fallback_copy_octal;
-
-int
-mu_set_default_fallback (const char *str)
-{
-  if (strcmp (str, "none") == 0)
-    mu_default_fallback_mode = mu_fallback_none;
-  else if (strcmp (str, "copy-pass") == 0)
-    mu_default_fallback_mode = mu_fallback_copy_pass;
-  else if (strcmp (str, "copy-octal") == 0)
-    mu_default_fallback_mode = mu_fallback_copy_octal;
-  else
-    return EINVAL;
-  return 0;
-}
-
-int
-mu_decode_filter (mu_stream_t *pfilter, mu_stream_t input,
-		  const char *filter_type,
-		  const char *fromcode, const char *tocode)
-{
-  mu_stream_t filter;
-  
-  int status = mu_filter_create (&filter, input, filter_type,
-				 MU_FILTER_DECODE, MU_STREAM_READ);
-  if (status)
-    return status;
-
-  if (fromcode && tocode && mu_c_strcasecmp (fromcode, tocode))
-    {
-      mu_stream_t cvt;
-
-      status = mu_filter_iconv_create (&cvt, filter, fromcode, tocode,
-				       0, mu_default_fallback_mode);
-      if (status == 0)
-	{
-          mu_stream_unref (filter);
-          filter = cvt;
-	}
-    }
-  *pfilter = filter;
-  return 0;
-}
-
-int
-mu_is_proto (const char *p)
-{
-  if (*p == '|')
-    return 1;
-  for (; *p && *p != '/'; p++)
-    if (*p == ':')
-      return 1;
-  return 0;
-}
-
-int
-mu_mh_delim (const char *str)
-{
-  if (str[0] == '-')
-    {
-      for (; *str == '-'; str++)
-	;
-      for (; *str == ' ' || *str == '\t'; str++)
-	;
-    }
-  return str[0] == '\n';
-}
-
-char *
-__argp_base_name (const char *arg)
-{
-  char *p = strrchr (arg, '/');
-  return (char *)(p ? p + 1 : arg);
-}
-
-/* A locale-independent version of strftime */
-size_t
-mu_strftime (char *s, size_t max, const char *format, const struct tm *tm)
-{
-  size_t size;
-  mu_set_locale ("C");
-  size = strftime(s, max, format, tm);
-  mu_restore_locale ();
-  return size;
-}
-  
-
-static void
-assoc_str_free (void *data)
-{
-  free (data);
-}
-
-int
-mutil_parse_field_map (const char *map, mu_assoc_t *passoc_tab, int *perr)
-{
-  int rc;
-  int i;
-  int argc;
-  char **argv;
-  mu_assoc_t assoc_tab = NULL;
-
-  rc = mu_argcv_get (map, ":", NULL, &argc, &argv);
-  if (rc)
-    {
-      mu_error (_("cannot split line `%s': %s"), map, mu_strerror (rc));
-      return rc;
-    }
-
-  for (i = 0; i < argc; i += 2)
-    {
-      char *tok = argv[i];
-      char *p = strchr (tok, '=');
-      char *pptr;
-      
-      if (!p)
-	{
-	  rc = EINVAL;
-	  break;
-	}
-      if (!assoc_tab)
-	{
-	  rc = mu_assoc_create (&assoc_tab, sizeof(char*), 0);
-	  if (rc)
-	    break;
-	  mu_assoc_set_free (assoc_tab, assoc_str_free);
-	  *passoc_tab = assoc_tab;
-	}
-      *p++ = 0;
-      pptr = strdup (p);
-      if (!pptr)
-	{
-	  rc = errno;
-	  break;
-	}
-      rc = mu_assoc_install (assoc_tab, tok, &pptr);
-      if (rc)
-	{
-	  free (p);
-	  break;
-	}
-    }
-
-  mu_argcv_free (argc, argv);
-  if (rc && perr)
-    *perr = i;
-  return rc;
-}
-
-/* FIXME: should it be here? */
-int
-mu_sql_decode_password_type (const char *arg, enum mu_password_type *t)
-{
-  if (strcmp (arg, "plain") == 0)
-    *t = password_plaintext;
-  else if (strcmp (arg, "hash") == 0)
-    *t = password_hash;
-  else if (strcmp (arg, "scrambled") == 0)
-    *t = password_scrambled;
-  else
-    return 1;
-  return 0;
-}
-
-int
-mu_stream_flags_to_mode (int flags, int isdir)
-{
-  int mode = 0;
-  if (flags & MU_STREAM_IRGRP)
-    mode |= S_IRGRP;
-  if (flags & MU_STREAM_IWGRP)
-    mode |= S_IWGRP;
-  if (flags & MU_STREAM_IROTH)
-    mode |= S_IROTH;
-  if (flags & MU_STREAM_IWOTH)
-    mode |= S_IWOTH;
-
-  if (isdir)
-    {
-      if (mode & (S_IRGRP|S_IWGRP))
-	mode |= S_IXGRP;
-      if (mode & (S_IROTH|S_IWOTH))
-	mode |= S_IXOTH;
-    }
-  
-  return mode;
-}
diff --git a/libmailutils/nls.c b/libmailutils/nls.c
deleted file mode 100644
index 4b9a8fe..0000000
--- a/libmailutils/nls.c
+++ /dev/null
@@ -1,62 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 2002, 2006, 2007, 2008, 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 */
-
-#if HAVE_CONFIG_H
-# include <config.h>
-#endif
-
-#include <stdlib.h>
-#include <mailutils/nls.h>
-#include <locale.h>
-
-/* Initialize Native Language Support */
-
-char *mu_locale_set;
-
-#ifndef HAVE_SETLOCALE
-# define setlocale(c,s) NULL
-#endif
-
-/* Set locale via LC_ALL.  */
-char *
-mu_set_locale (const char *locale)
-{
-#if defined HAVE_SETLOCALE
-  return setlocale (LC_ALL, locale);
-#else
-  return NULL;
-#endif
-}
-
-void
-mu_restore_locale (void)
-{
-  if (mu_locale_set)
-    mu_set_locale (mu_locale_set);
-}
-
-void
-mu_init_nls (void)
-{
-#ifdef ENABLE_NLS
-  mu_locale_set = mu_set_locale ("");
-  bindtextdomain (PACKAGE, LOCALEDIR);
-#endif /* ENABLE_NLS */
-}
-
diff --git a/libmailutils/observer.c b/libmailutils/observer.c
deleted file mode 100644
index 51b0260..0000000
--- a/libmailutils/observer.c
+++ /dev/null
@@ -1,239 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 1999, 2000, 2004, 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 */
-
-#ifdef HAVE_CONFIG_H
-# include <config.h>
-#endif
-
-#include <errno.h>
-#include <stdlib.h>
-
-#include <mailutils/list.h>
-#include <mailutils/iterator.h>
-#include <mailutils/errno.h>
-#include <mailutils/sys/observer.h>
-
-int
-mu_observer_create (mu_observer_t *pobserver, void *owner)
-{
-  mu_observer_t observer;
-  observer = calloc (sizeof (*observer), 1);
-  if (observer == NULL)
-    return ENOMEM;
-  observer->owner = owner;
-  *pobserver = observer;
-  return 0;
-}
-
-void
-mu_observer_destroy (mu_observer_t *pobserver, void *owner)
-{
-  if (pobserver && *pobserver)
-    {
-      mu_observer_t observer = *pobserver;
-      if (observer->owner == owner || observer->flags & MU_OBSERVER_NO_CHECK)
-	{
-	  if (observer->_destroy)
-	    observer->_destroy (observer, observer->_action_data);
-	  free (observer);
-	}
-      *pobserver = NULL;
-    }
-}
-
-void *
-mu_observer_get_owner (mu_observer_t observer)
-{
-  return (observer) ? observer->owner : NULL;
-}
-
-int
-mu_observer_action (mu_observer_t observer, size_t type, void *data)
-{
-  if (observer == NULL)
-    return EINVAL;
-  if (observer->_action)
-    return observer->_action (observer, type, data, observer->_action_data);
-  return 0;
-}
-
-int
-mu_observer_set_action (mu_observer_t observer,
-			int (*_action) (mu_observer_t, size_t, void *, void *),
-			void *owner)
-{
-  if (observer == NULL)
-    return EINVAL;
-  if (observer->owner != owner)
-    return EACCES;
-  observer->_action = _action;
-  return 0;
-}
-
-int
-mu_observer_set_action_data  (mu_observer_t observer, void *data, void *owner)
-{
-  if (observer == NULL)
-    return EINVAL;
-  if (observer->owner != owner)
-    return EACCES;
-  observer->_action_data = data;
-  return 0;
-}
-
-int
-mu_observer_set_destroy (mu_observer_t observer,
-			 int (*_destroy) (mu_observer_t, void *),
-			 void *owner)
-{
-  if (observer == NULL)
-    return EINVAL;
-  if (observer->owner != owner)
-    return EACCES;
-  observer->_destroy = _destroy;
-  return 0;
-}
-
-int
-mu_observer_set_flags (mu_observer_t observer, int flags)
-{
-  if (observer == NULL)
-    return EINVAL;
-  observer->flags |= flags;
-  return 0;
-}
-
-static void
-_free_event (void *ptr)
-{
-  event_t event = ptr;
-  mu_observer_destroy (&event->observer, NULL);
-  free (event);
-}
-
-int
-mu_observable_create (mu_observable_t *pobservable, void *owner)
-{
-  mu_observable_t observable;
-  int status;
-  if (pobservable == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-  observable = calloc (sizeof (*observable), 1);
-  if (observable == NULL)
-    return ENOMEM;
-  status = mu_list_create (&observable->list);
-  if (status != 0 )
-    {
-      free (observable);
-      return status;
-    }
-  mu_list_set_destroy_item (observable->list, _free_event);
-  observable->owner = owner;
-  *pobservable = observable;
-  return 0;
-}
-
-void
-mu_observable_destroy (mu_observable_t *pobservable, void *owner)
-{
-  if (pobservable && *pobservable)
-    {
-      mu_observable_t observable = *pobservable;
-      if (observable->owner == owner)
-	{
-	  mu_list_destroy (&observable->list);
-	  free (observable);
-	}
-      *pobservable = NULL;
-    }
-}
-
-void *
-mu_observable_get_owner (mu_observable_t observable)
-{
-  return (observable) ? observable->owner : NULL;
-}
-
-int
-mu_observable_attach (mu_observable_t observable, size_t type,  mu_observer_t observer)
-{
-  event_t event;
-  if (observable == NULL || observer == NULL)
-    return EINVAL;
-  event = calloc (1, sizeof (*event));
-  if (event == NULL)
-    return ENOMEM;
-  event->type = type;
-  event->observer = observer;
-  return mu_list_append (observable->list, event);
-}
-
-int
-mu_observable_detach (mu_observable_t observable, mu_observer_t observer)
-{
-  mu_iterator_t iterator;
-  int status;
-  event_t event = NULL;
-
-  if (observable == NULL || observer == NULL)
-    return EINVAL;
-  status = mu_list_get_iterator (observable->list, &iterator);
-  if (status != 0)
-    return status;
-  status = MU_ERR_NOENT;
-  for (mu_iterator_first (iterator); !mu_iterator_is_done (iterator);
-       mu_iterator_next (iterator))
-    {
-      event = NULL;
-      mu_iterator_current (iterator, (void **)&event);
-      if (event && event->observer == observer)
-        {
-          mu_iterator_ctl (iterator, mu_itrctl_delete, NULL);
-	  status = 0;
-          break;
-        }
-    }
-  mu_iterator_destroy (&iterator);
-  return status;
-}
-
-int
-mu_observable_notify (mu_observable_t observable, int type, void *data)
-{
-  mu_iterator_t iterator;
-  event_t event = NULL;
-  int status = 0;
-  if (observable == NULL)
-    return EINVAL;
-  status = mu_list_get_iterator (observable->list, &iterator);
-  if (status != 0)
-    return status;
-  for (mu_iterator_first (iterator); !mu_iterator_is_done (iterator);
-       mu_iterator_next (iterator))
-    {
-      event = NULL;
-      mu_iterator_current (iterator, (void **)&event);
-      if (event && event->type & type)
-        {
-	  status |= mu_observer_action (event->observer, type, data);
-        }
-    }
-  mu_iterator_destroy (&iterator);
-  return status;
-}
diff --git a/libmailutils/opool.c b/libmailutils/opool.c
deleted file mode 100644
index 991b527..0000000
--- a/libmailutils/opool.c
+++ /dev/null
@@ -1,446 +0,0 @@
-/* String-list functions for GNU Mailutils.
-   Copyright (C) 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
-
-   Based on slist module from GNU Radius.  Written by Sergey Poznyakoff.
-   
-   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, see <http://www.gnu.org/licenses/>.
-*/
-
-#ifdef HAVE_CONFIG_H
-# include <config.h>
-#endif
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <mailutils/types.h>
-#include <mailutils/alloc.h>
-#include <mailutils/opool.h>
-#include <mailutils/errno.h>
-#include <mailutils/error.h>
-#include <mailutils/nls.h>
-#include <mailutils/iterator.h>
-
-struct mu_opool_bucket
-{
-  struct mu_opool_bucket *next;
-  char *buf;
-  size_t level;
-  size_t size;
-};
-
-struct _mu_opool
-{
-  int memerr;
-  size_t bucket_size;
-  size_t itr_count;
-  struct mu_opool_bucket *head, *tail;
-  struct mu_opool_bucket *free;
-};
-
-static struct mu_opool_bucket *
-alloc_bucket (struct _mu_opool *opool, size_t size)
-{
-  struct mu_opool_bucket *p = malloc (sizeof (*p) + size);
-  if (!p)
-    {
-      if (opool->memerr)
-	mu_alloc_die ();
-    }
-  else
-    {
-      p->buf = (char*)(p + 1);
-      p->level = 0;
-      p->size = size;
-      p->next = NULL;
-    }
-  return p;
-}
-
-static int
-alloc_pool (mu_opool_t opool, size_t size)
-{
-  struct mu_opool_bucket *p = alloc_bucket (opool, opool->bucket_size);
-  if (!p)
-    return ENOMEM;
-  if (opool->tail)
-    opool->tail->next = p;
-  else
-    opool->head = p;
-  opool->tail = p;
-  return 0;
-}
-
-static int
-copy_chars (mu_opool_t opool, const char *str, size_t n, size_t *psize)
-{
-  size_t rest;
-
-  if (!opool->head || opool->tail->level == opool->tail->size)
-    if (alloc_pool (opool, opool->bucket_size))
-      return ENOMEM;
-  rest = opool->tail->size - opool->tail->level;
-  if (n > rest)
-    n = rest;
-  memcpy (opool->tail->buf + opool->tail->level, str, n);
-  opool->tail->level += n;
-  *psize = n;
-  return 0;
-}
-
-int
-mu_opool_create (mu_opool_t *pret, int memerr)
-{
-  struct _mu_opool *x = malloc (sizeof (x[0]));
-  if (!x)
-    {
-      if (memerr)
-	mu_alloc_die ();
-      return ENOMEM;
-    }
-  x->memerr = memerr;
-  x->bucket_size = MU_OPOOL_BUCKET_SIZE;
-  x->itr_count = 0;
-  x->head = x->tail = x->free = 0;
-  *pret = x;
-  return 0;
-}
-
-int
-mu_opool_set_bucket_size (mu_opool_t opool, size_t size)
-{
-  if (!opool)
-    return EINVAL;
-  opool->bucket_size = size;
-  return 0;
-}
-
-int
-mu_opool_get_bucket_size (mu_opool_t opool, size_t *psize)
-{
-  if (!opool || !psize)
-    return EINVAL;
-  *psize = opool->bucket_size;
-  return 0;
-}
-
-void
-mu_opool_clear (mu_opool_t opool)
-{
-  if (!opool)
-    return;
-  
-  if (opool->tail)
-    {
-      opool->tail->next = opool->free;
-      opool->free = opool->head;
-      opool->head = opool->tail = NULL;
-    }
-}	
-
-void
-mu_opool_destroy (mu_opool_t *popool)
-{
-  struct mu_opool_bucket *p;
-  if (popool && *popool)
-    {
-      mu_opool_t opool = *popool;
-      mu_opool_clear (opool);
-      for (p = opool->free; p; )
-	{
-	  struct mu_opool_bucket *next = p->next;
-	  free (p);
-	  p = next;
-	}
-      free (opool);
-    }
-  *popool = NULL;
-}
-
-int
-mu_opool_append (mu_opool_t opool, const void *str, size_t n)
-{
-  const char *ptr = str;
-  while (n)
-    {
-      size_t s;
-      if (copy_chars (opool, ptr, n, &s))
-	return ENOMEM;
-      ptr += s;
-      n -= s;
-    }
-  return 0;
-}
-
-int
-mu_opool_append_char (mu_opool_t opool, char c)
-{
-  return mu_opool_append (opool, &c, 1);
-}	
-
-int
-mu_opool_appendz (mu_opool_t opool, const char *str)
-{
-  return mu_opool_append (opool, str, strlen (str))
-         || mu_opool_append_char (opool, 0);
-}
-
-size_t
-mu_opool_size (mu_opool_t opool)
-{
-  size_t size = 0;
-  struct mu_opool_bucket *p;
-  for (p = opool->head; p; p = p->next)
-    size += p->level;
-  return size;
-}
-
-size_t
-mu_opool_copy (mu_opool_t opool, void *buf, size_t size)
-{
-  char *cp = buf;
-  size_t total = 0;
-  struct mu_opool_bucket *p;
-  
-  for (p = opool->head; p && total < size; p = p->next)
-    {
-      size_t cpsize = size - total;
-      if (cpsize > p->level)
-	cpsize = p->level;
-      memcpy (cp, p->buf, cpsize);
-      cp += cpsize;
-      total += cpsize;
-    }
-  return total;
-}
-
-int
-mu_opool_coalesce (mu_opool_t opool, size_t *psize)
-{
-  size_t size;
-
-  if (opool->itr_count)
-    return MU_ERR_FAILURE;
-  if (opool->head && opool->head->next == NULL)
-    size = opool->head->level;
-  else {
-    struct mu_opool_bucket *bucket;
-    struct mu_opool_bucket *p;
-
-    size = mu_opool_size (opool);
-	
-    bucket = alloc_bucket (opool, size);
-    if (!bucket)
-      return ENOMEM;
-    for (p = opool->head; p; )
-      {
-	struct mu_opool_bucket *next = p->next;
-	memcpy (bucket->buf + bucket->level, p->buf, p->level);
-	bucket->level += p->level;
-	free (p);
-	p = next;
-      }
-    opool->head = opool->tail = bucket;
-  }
-  if (psize)
-    *psize = size;
-  return 0;
-}
-
-void *
-mu_opool_head (mu_opool_t opool, size_t *psize)
-{
-  if (*psize) 
-    *psize = opool->head ? opool->head->level : 0;
-  return opool->head ? opool->head->buf : NULL;
-}
-
-void *
-mu_opool_finish (mu_opool_t opool, size_t *psize)
-{
-  if (mu_opool_coalesce (opool, psize))
-    return NULL;
-  mu_opool_clear (opool);
-  return opool->free->buf;
-}
-
-int
-mu_opool_union (mu_opool_t *pdst, mu_opool_t *psrc)
-{
-  mu_opool_t src, dst;
-  
-  if (!psrc)
-    return EINVAL;
-  if (!*psrc)
-    return 0;
-  src = *psrc;
-  
-  if (!pdst)
-    return EINVAL;
-  if (!*pdst)
-    {
-      *pdst = src;
-      *psrc = NULL;
-      return 0;
-    }
-  else
-    dst = *pdst;
-
-  if (dst->tail)
-    dst->tail->next = src->head;
-  else
-    dst->head = src->head;
-  dst->tail = src->tail;
-
-  if (src->free)
-    {
-      struct mu_opool_bucket *p;
-
-      for (p = src->free; p->next; p = p->next)
-	;
-      p->next = dst->free;
-      dst->free = src->free;
-    }
-
-  free (src);
-  *psrc = NULL;
-  return 0;
-}
-
-
-/* Iterator support */
-struct opool_iterator
-{
-  mu_opool_t opool;
-  struct mu_opool_bucket *cur;
-};
-
-static int
-opitr_first (void *owner)
-{
-  struct opool_iterator *itr = owner;
-  itr->cur = itr->opool->head;
-  return 0;
-}
-
-static int
-opitr_next (void *owner)
-{
-  struct opool_iterator *itr = owner;
-  if (itr->cur)
-    {
-      itr->cur = itr->cur->next;
-      return 0;
-    }
-  return EINVAL;
-}
-
-static int
-opitr_getitem (void *owner, void **pret, const void **pkey)
-{
-  struct opool_iterator *itr = owner;
-  if (!itr->cur)
-    return MU_ERR_NOENT;
-
-  *pret = itr->cur->buf;
-  if (pkey)
-    *(size_t*) pkey = itr->cur->level;
-  return 0;
-}
-
-static int
-opitr_finished_p (void *owner)
-{
-  struct opool_iterator *itr = owner;
-  return itr->cur == NULL;
-}
-
-static int
-opitr_curitem_p (void *owner, void *item)
-{
-  struct opool_iterator *itr = owner;
-  return itr->cur && itr->cur->buf == item;
-}
-
-static int
-opitr_destroy (mu_iterator_t iterator, void *data)
-{
-  struct opool_iterator *itr = data;
-  if (itr->opool->itr_count == 0)
-    {
-      /* oops! */
-      mu_error (_("%s: INTERNAL ERROR: zero reference count"),
-		"opool_destroy");
-    }
-  else
-    itr->opool->itr_count--;
-  free (data);
-  return 0;
-}
-
-static int
-opitr_data_dup (void **ptr, void *owner)
-{
-  struct opool_iterator *itr = owner;
-
-  *ptr = malloc (sizeof (struct opool_iterator));
-  if (*ptr == NULL)
-    return ENOMEM;
-  memcpy (*ptr, owner, sizeof (struct opool_iterator));
-  itr->opool->itr_count++;
-  return 0;
-}
-
-int
-mu_opool_get_iterator (mu_opool_t opool, mu_iterator_t *piterator)
-{
-  mu_iterator_t iterator;
-  int status;
-  struct opool_iterator *itr;
-
-  if (!opool)
-    return EINVAL;
-
-  itr = calloc (1, sizeof *itr);
-  if (!itr)
-    return ENOMEM;
-  itr->opool = opool;
-  itr->cur = opool->head;
-  
-  status = mu_iterator_create (&iterator, itr);
-  if (status)
-    {
-      free (itr);
-      return status;
-    }
-
-  mu_iterator_set_first (iterator, opitr_first);
-  mu_iterator_set_next (iterator, opitr_next);
-  mu_iterator_set_getitem (iterator, opitr_getitem);
-  mu_iterator_set_finished_p (iterator, opitr_finished_p);
-  mu_iterator_set_curitem_p (iterator, opitr_curitem_p);
-  mu_iterator_set_destroy (iterator, opitr_destroy);
-  mu_iterator_set_dup (iterator, opitr_data_dup);
-
-  opool->itr_count++;
-
-  *piterator = iterator;
-  return 0;
-}
-
-
-
-
-  
-  
- 
diff --git a/libmailutils/parse822.c b/libmailutils/parse822.c
deleted file mode 100644
index b4d66bc..0000000
--- a/libmailutils/parse822.c
+++ /dev/null
@@ -1,2040 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 1999, 2000, 2001, 2005, 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 */
-
-/*
-
-FIXME: what is the status of this TODO list?
-
-Things to consider:
-
-  - When parsing phrase, should I ignore non-ascii, or replace with a
-    '?' character? Right now parsing fails.
-
-    --> Should ignore non-ascii, it is unicode or iso8892-1.
-
-  - Are comments allowed in domain-literals?
-
-  - Need a way to mark the *end* of a group. Maybe add a field to _mu_address,
-    int group_end;, so if you care, you can search for the end of
-    a group with address_is_group_end();
-
-    --> Groups no longer show up in the mu_address_t list.
-
-  - Need a way to parse ",,,", it's a valid address-list, it just doesn't
-    have any addresses.
-
-  - The personal for ""Sam"" <sam@here> is "Sam", and for "'s@b'" <s@b>
-    is 's@b', should I strip those outside parentheses, or is that
-    too intrusive? Maybe an apps business if it wants to?
-
-  - Should we do best effort parsing, so parsing "sam@locahost, foo@"
-    gets one address, or just say it is or it isn't in RFC format?
-    Right now we're strict, we'll see how it goes.
-
-  - parse Received: field?
-
-  - test for memory leaks on malloc failure
-
-  - fix the realloc, try a struct _string { char* b, size_t sz };
-
-The lexer finds consecutive sequences of characters, so it should
-define:
-
-struct parse822_token_t {
-    const char* b;  // beginning of token
-    const char* e;  // one past end of token
-}
-typedef struct parse822_token_t TOK;
-
-Then I can have str_append_token(), and the lexer functions can
-look like:
-
-int mu_parse822_atom(const char** p, const char* e, TOK* atom);
-
-Just a quick thought, I'll have to see how many functions that will
-actually help.
-
-  - get example addresses from rfc2822, and from the perl code.
-*/
-
-#ifdef HAVE_CONFIG_H
-# include <config.h>
-#endif
-
-#include <assert.h>
-#include <errno.h>
-#include <string.h>
-#include <stdlib.h>
-#include <time.h>
-
-#ifdef HAVE_STRINGS_H
-# include <strings.h>
-#endif
-
-#include <mailutils/cctype.h>
-#include <mailutils/cstr.h>
-#include <mailutils/errno.h>
-#include <mailutils/parse822.h>
-#include <mailutils/address.h>
-
-#ifdef EOK
-# undef EOK
-#endif
-
-#define EOK	0
-#define EPARSE	MU_ERR_BAD_822_FORMAT
-
-/*
- * Some convenience functions for dealing with dynamically re-sized
- * strings.
- */
-
-static int
-str_append_n (char **to, const char *from, size_t n)
-{
-  size_t l = 0;
-
-  /* if not to, then silently discard data */
-  if (!to)
-    {
-      return EOK;
-    }
-
-  if (*to)
-    {
-      char *bigger;
-
-      l = strlen (*to);
-
-      bigger = realloc (*to, l + n + 1);
-
-      if (!bigger)
-	{
-	  return ENOMEM;
-	}
-
-      *to = bigger;
-    }
-  else
-    {
-      *to = malloc (n + 1);
-    }
-
-  strncpy (&to[0][l], from, n);
-
-  /* strncpy is lame, nul terminate our buffer */
-
-  to[0][l + n] = 0;
-
-  return EOK;
-}
-
-static int
-str_append (char **to, const char *from)
-{
-  if (!from)
-    return 0;
-  return str_append_n (to, from, strlen (from));
-}
-
-static int
-str_append_char (char **to, char c)
-{
-  return str_append_n (to, &c, 1);
-}
-
-static int
-str_append_range (char **to, const char *b, const char *e)
-{
-  return str_append_n (to, b, e - b);
-}
-
-static void
-str_free (char **s)
-{
-  if (s && *s)
-    {
-      free (*s);
-      *s = 0;
-    }
-}
-
-/*
- * Character Classification - could be rewritten in a C library
- * independent way, my system's C library matches the RFC
- * definitions. I don't know if that's guaranteed.
- *  
- * Note that all return values are:
- *   1 -> TRUE
- *   0 -> FALSE
- * This may be appear different than the 0 == success return
- * values of the other functions, but I was getting lost in
- * boolean arithmetic.
- */
-int
-mu_parse822_is_char (char c)
-{
-  return mu_isascii (c);
-}
-
-int
-mu_parse822_is_digit (char c)
-{
-  /* digit = <any ASCII decimal digit> */
-
-  return mu_isdigit ((unsigned) c);
-}
-
-int
-mu_parse822_is_ctl (char c)
-{
-  return mu_iscntrl ((unsigned) c) || c == 127 /* DEL */ ;
-}
-
-int
-mu_parse822_is_space (char c)
-{
-  return c == ' ';
-}
-
-int
-mu_parse822_is_htab (char c)
-{
-  return c == '\t';
-}
-
-int
-mu_parse822_is_lwsp_char (char c)
-{
-  return mu_parse822_is_space (c) || mu_parse822_is_htab (c);
-}
-
-int
-mu_parse822_is_special (char c)
-{
-  return strchr ("()<>@,;:\\\".[]", c) ? 1 : 0;
-}
-
-int
-parse822_is_atom_char_ex (char c)
-{
-  return !mu_parse822_is_special (c)
-    && !mu_parse822_is_space (c)
-    && !mu_parse822_is_ctl (c);
-}
-
-int
-mu_parse822_is_atom_char (char c)
-{
-  return mu_parse822_is_char (c) && parse822_is_atom_char_ex (c);
-}
-
-int
-mu_parse822_is_q_text (char c)
-{
-  return
-    mu_parse822_is_char (c) &&
-    c != '"' &&
-    c != '\\' &&
-    c != '\r';
-}
-
-int
-mu_parse822_is_d_text (char c)
-{
-  return
-    mu_parse822_is_char (c) &&
-    c != '[' &&
-    c != ']' &&
-    c != '\\' &&
-    c != '\r';
-}
-/*
- * SMTP's version of qtext, called <q> in the RFC 821 syntax,
- * also excludes <LF>.
- */
-int
-mu_parse822_is_smtp_q (char c)
-{
-  return
-    mu_parse822_is_q_text (c) &&
-    c != '\n';
-}
-
-/***** From RFC 822, 3.3 Lexical Tokens *****/
-
-int
-mu_parse822_skip_nl (const char **p, const char *e)
-{
-  /* Here we consider a new-line (NL) to be either a bare LF, or
-   * a CRLF pair as required by the RFC.
-   */
-  const char *s = *p;
-
-  if ((&s[1] < e) && s[0] == '\r' && s[1] == '\n')
-    {
-      *p += 2;
-
-      return EOK;
-    }
-
-  if ((&s[0] < e) && s[0] == '\n')
-    {
-      *p += 1;
-
-      return EOK;
-    }
-
-  return EPARSE;
-}
-
-int
-mu_parse822_skip_lwsp_char (const char **p, const char *e)
-{
-  if (*p < e && mu_parse822_is_lwsp_char (**p))
-    {
-      *p += 1;
-      return EOK;
-    }
-  return EPARSE;
-}
-
-int
-mu_parse822_skip_lwsp (const char **p, const char *e)
-{
-  /*
-   * linear-white-space = 1*([[CR]LF] LWSP-char)
-   *
-   *   We interpret a bare LF as identical to the canonical CRLF
-   *   line ending, I don't know another way since on a Unix system
-   *   all CRLF will be translated to the local convention, a bare
-   *   LF, and thus we can not deal with bare NLs in the message.
-   */
-  int space = 0;
-
-  while (*p != e)
-    {
-      const char *save = *p;
-
-      if (mu_parse822_skip_lwsp_char (p, e) == EOK)
-	{
-	  space = 1;
-	  continue;
-	}
-      if (mu_parse822_skip_nl (p, e) == EOK)
-	{
-	  if (mu_parse822_skip_lwsp_char (p, e) == EOK)
-	    {
-	      continue;
-	    }
-	  *p = save;
-	  return EPARSE;
-	}
-      break;
-    }
-  return space ? EOK : EPARSE;
-}
-
-int
-mu_parse822_skip_comments (const char **p, const char *e)
-{
-  int status;
-
-  while ((status = mu_parse822_comment (p, e, 0)) == EOK)
-    ;
-
-  return EOK;
-}
-
-int
-mu_parse822_digits (const char **p, const char *e, int min, int max, int *digits)
-{
-  const char *save = *p;
-
-  int i = 0;
-
-  assert (digits);
-
-  *digits = 0;
-
-  while (*p < e && mu_parse822_is_digit (**p))
-    {
-      *digits = *digits * 10 + **p - '0';
-      *p += 1;
-      ++i;
-      if (max != 0 && i == max)
-	{
-	  break;
-	}
-    }
-  if (i < min)
-    {
-      *p = save;
-      return EPARSE;
-    }
-
-  return EOK;
-}
-
-int
-mu_parse822_special (const char **p, const char *e, char c)
-{
-  mu_parse822_skip_lwsp (p, e);	/* not comments, they start with a special... */
-
-  if ((*p != e) && **p == c)
-    {
-      *p += 1;
-      return EOK;
-    }
-  return EPARSE;
-}
-
-int
-mu_parse822_comment (const char **p, const char *e, char **comment)
-{
-  /* comment = "(" *(ctext / quoted-pair / comment) ")"
-   * ctext = <any char except "(", ")", "\", & CR, including lwsp>
-   */
-  const char *save = *p;
-  int rc;
-
-  if ((rc = mu_parse822_special (p, e, '(')))
-    {
-      return rc;
-    }
-
-  while (*p != e)
-    {
-      char c = **p;
-
-      if (c == ')')
-	{
-	  *p += 1;
-	  return EOK;		/* found end-of-comment */
-	}
-      else if (c == '(')
-	{
-	  rc = mu_parse822_comment (p, e, comment);
-	}
-      else if (c == '\\')
-	{
-	  rc = mu_parse822_quoted_pair (p, e, comment);
-	}
-      else if (c == '\r')
-	{
-	  /* invalid character... */
-	  *p += 1;
-	}
-      else if (mu_parse822_is_char (c))
-	{
-	  rc = str_append_char (comment, c);
-	  *p += 1;
-	}
-      else
-	{
-	  /* invalid character... */
-	  *p += 1;
-	}
-      if (rc != EOK)
-	break;
-    }
-
-  if (*p == e)
-    {
-      rc = EPARSE;		/* end-of-comment not found */
-    }
-
-  *p = save;
-
-  assert (rc != EOK);
-
-  return rc;
-}
-
-int
-mu_parse822_atom (const char **p, const char *e, char **atom)
-{
-  /* atom = 1*<an atom char> */
-
-  const char *save = *p;
-  int rc = EPARSE;
-
-  mu_parse822_skip_comments (p, e);
-
-  save = *p;
-
-  while ((*p != e) && (**p == '.' || mu_parse822_is_atom_char (**p)))
-    {
-      rc = str_append_char (atom, **p);
-      *p += 1;
-      if (rc != EOK)
-	{
-	  *p = save;
-	  break;
-	}
-    }
-  return rc;
-}
-
-int
-parse822_atom_ex (const char **p, const char *e, char **atom)
-{
-  /* atom = 1*<an atom char> */
-  const char *ptr;
-  int rc;
-
-  mu_parse822_skip_comments (p, e);
-
-  for (ptr = *p; (ptr != e) && parse822_is_atom_char_ex (*ptr); ptr++)
-    ;
-  if (ptr - *p == 0)
-    return EPARSE;
-  rc = str_append_n (atom, *p, ptr - *p);
-  if (rc == 0)
-    *p = ptr;
-  return rc;
-}
-
-int
-mu_parse822_quoted_pair (const char **p, const char *e, char **qpair)
-{
-  /* quoted-pair = "\" char */
-
-  int rc;
-
-  /* need TWO characters to be available */
-  if ((e - *p) < 2)
-    return EPARSE;
-
-  if (**p != '\\')
-    return EPARSE;
-
-  if ((rc = str_append_char (qpair, *(*p + 1))))
-    return rc;
-
-  *p += 2;
-
-  return EOK;
-}
-
-int
-mu_parse822_quoted_string (const char **p, const char *e, char **qstr)
-{
-  /* quoted-string = <"> *(qtext/quoted-pair) <">
-   * qtext = char except <">, "\", & CR, including lwsp-char
-   */
-
-  const char *save = *p;
-  int rc;
-
-  mu_parse822_skip_comments (p, e);
-
-  save = *p;
-
-  if ((rc = mu_parse822_special (p, e, '"')))
-    return rc;
-
-  while (*p != e)
-    {
-      char c = **p;
-
-      if (c == '"')
-	{
-	  *p += 1;
-	  return EOK;		/* found end-of-qstr */
-	}
-      else if (c == '\\')
-	{
-	  rc = mu_parse822_quoted_pair (p, e, qstr);
-	}
-      else if (c == '\r')
-	{
-	  /* invalid character... */
-	  *p += 1;
-	}
-      else if (mu_parse822_is_char (c))
-	{
-	  rc = str_append_char (qstr, c);
-	  *p += 1;
-	}
-      else
-	{
-	  /* invalid character... */
-	  *p += 1;
-	}
-      if (rc)
-	{
-	  *p = save;
-	  str_free (qstr);
-	  return rc;
-	}
-    }
-  *p = save;
-  str_free (qstr);
-  return EPARSE;		/* end-of-qstr not found */
-}
-
-int
-mu_parse822_word (const char **p, const char *e, char **word)
-{
-  /* word = atom / quoted-string */
-
-  const char *save = *p;
-  int rc = EOK;
-
-  mu_parse822_skip_comments (p, e);
-
-  save = *p;
-
-  {
-    char *qstr = 0;
-    if ((rc = mu_parse822_quoted_string (p, e, &qstr)) == EOK && qstr)
-      {
-	rc = str_append (word, qstr);
-
-	str_free (&qstr);
-
-	if (rc != EOK)
-	  *p = save;
-
-	return rc;
-      }
-    assert (qstr == NULL);
-  }
-
-  if (rc != EPARSE)
-    {
-      /* it's fatal */
-      return rc;
-    }
-
-  /* Necessary because the quoted string could have found
-   * a partial string (invalid syntax). Thus reset, the atom
-   * will fail to if the syntax is invalid.
-   * We use parse822_atom_ex to allow for non-rfc-compliant atoms:
-   *
-   * "Be liberal in what you accept, and conservative in what you send."
-   */
-
-  {
-    char *atom = 0;
-    if (parse822_atom_ex (p, e, &atom) == EOK)
-      {
-	rc = str_append (word, atom);
-
-	str_free (&atom);
-
-	if (rc != EOK)
-	  *p = save;
-
-	return rc;
-      }
-    assert (atom == NULL);
-  }
-
-  return EPARSE;
-}
-
-/* Some mailers do not quote personal part even if it contains dot.
-   Try to be smart about it.
-*/
-	
-int
-parse822_word_dot (const char **p, const char *e, char **word)
-{
-  int rc = mu_parse822_word (p, e, word);
-  for (;rc == 0 && (*p != e) && **p == '.'; ++*p)
-    rc = str_append (word, ".");
-  return rc;
-}
-
-int
-mu_parse822_phrase (const char **p, const char *e, char **phrase)
-{
-  /* phrase = 1*word */
-
-  const char *save = *p;
-  int rc;
-
-  if ((rc = parse822_word_dot (p, e, phrase)))
-    return rc;
-
-  /* ok, got the 1 word, now append all the others we can */
-  {
-    char *word = 0;
-
-    while ((rc = parse822_word_dot (p, e, &word)) == EOK)
-      {
-	rc = str_append_char (phrase, ' ');
-
-	if (rc == EOK)
-	  rc = str_append (phrase, word);
-
-	str_free (&word);
-
-	if (rc != EOK)
-	  break;
-      }
-    assert (word == NULL);
-    if (rc == EPARSE)
-      rc = EOK;			/* its not an error to find no more words */
-  }
-  if (rc)
-    *p = save;
-
-  return rc;
-}
-
-/***** From RFC 822, 6.1 Address Specification Syntax *****/
-
-static mu_address_t
-new_mb (void)
-{
-  return calloc (1, sizeof (struct mu_address));
-}
-
-static char *
-addr_field_by_mask (mu_address_t addr, int mask)
-{
-  switch (mask)						
-    {
-    case MU_ADDR_HINT_ADDR:
-      return addr->addr;
-	  
-    case MU_ADDR_HINT_COMMENTS:				
-      return addr->comments;					
-	  
-    case MU_ADDR_HINT_PERSONAL:				
-      return addr->personal;					
-
-    case MU_ADDR_HINT_EMAIL:
-      return addr->email;
-
-    case MU_ADDR_HINT_LOCAL:
-      return addr->local_part;
-      
-    case MU_ADDR_HINT_DOMAIN:				
-      return addr->domain;					
-
-    case MU_ADDR_HINT_ROUTE:
-      return addr->route;
-    }
-  return NULL;
-}							
-
-static char *
-get_val (mu_address_t hint, int hflags, char *value, int mask, int *memflag)
-{
-  if (!value && hint && (hflags & mask))
-    {
-      char *p = addr_field_by_mask (hint, mask);
-      if (p)							
-	{
-	  if (memflag)
-	    *memflag |= mask;
-	  value = strdup (p);
-	}
-    }
-  return value;
-}
-
-static void
-addr_free_fields (mu_address_t a, int memflag)
-{
-  char *p;
-  
-  if ((p = addr_field_by_mask (a, memflag & MU_ADDR_HINT_ADDR)))
-    free (p);
-  if ((p = addr_field_by_mask (a, memflag & MU_ADDR_HINT_COMMENTS)))
-    free (p);
-  if ((p = addr_field_by_mask (a, memflag & MU_ADDR_HINT_PERSONAL)))
-    free (p);
-  if ((p = addr_field_by_mask (a, memflag & MU_ADDR_HINT_EMAIL)))
-    free (p);
-  if ((p = addr_field_by_mask (a, memflag & MU_ADDR_HINT_LOCAL)))
-    free (p);
-  if ((p = addr_field_by_mask (a, memflag & MU_ADDR_HINT_DOMAIN)))
-    free (p);
-  if ((p = addr_field_by_mask (a, memflag & MU_ADDR_HINT_ROUTE)))
-    free (p);
-}
-
-static int
-fill_mb (mu_address_t *pa,
-	 char *comments, char *personal, char *local, char *domain,
-	 mu_address_t hint, int hflags)
-{
-  int rc = EOK;
-  mu_address_t a;
-  int memflag = 0;
-
-  a = new_mb ();
-
-  if (!a)
-    return ENOMEM;
-
-  a->comments = get_val (hint, hflags, comments, MU_ADDR_HINT_COMMENTS,
-			 &memflag);
-  a->personal = get_val (hint, hflags, personal, MU_ADDR_HINT_PERSONAL,
-			 &memflag);
-
-  domain = get_val (hint, hflags, domain, MU_ADDR_HINT_DOMAIN,
-		    &memflag);
-  local = get_val (hint, hflags, local, MU_ADDR_HINT_LOCAL,
-		   &memflag);
-  do
-    {
-      /* loop exists only to break out of */
-      if (!local)
-	/* no email to construct */
-	break;
-
-      if ((rc = mu_parse822_quote_local_part (&a->email, local)))
-	break;
-      if (domain)
-	{
-	  if ((rc = str_append (&a->email, "@")))
-	    break;
-	  if ((rc = str_append (&a->email, domain)))
-	    break;
-	}
-    }
-  while (0);
-
-  a->local_part = local;
-  a->domain = domain;
-
-  if (rc != EOK)
-    {
-      addr_free_fields (a, memflag);
-      /* note that the arguments have NOT been freed, we only own
-       * them on success. */
-      free (a);
-    }
-  else
-    *pa = a;
-
-  return rc;
-}
-
-int
-mu_parse822_address_list (mu_address_t *a, const char *s,
-			  mu_address_t hint, int hflags)
-{
-  /* address-list = #(address) */
-
-  const char **p = &s;
-  const char *e = &s[strlen (s)];
-  int rc = EOK;
-  mu_address_t *n = a;		/* the next address we'll be creating */
-
-  rc = mu_parse822_address (p, e, n, hint, hflags);
-
-  /* A list may start with a leading <,>, we'll find out if
-   * that's not the case at the top of the while, but give
-   * this a conditional OK unless there was some other kind
-   * of error.
-   */
-  if (rc != EOK && rc != EPARSE)
-    {
-      return rc;
-    }
-  while (*p < e)
-    {
-      mu_parse822_skip_comments (p, e);
-
-      /* An address can contain a group, so an entire
-       * list of addresses may have been appended, or no
-       * addresses at all. Walk to the end.
-       */
-      while (*n)
-	{
-	  n = &(*n)->next;
-	}
-
-      /* Remember that ',,a@b' is a valid list! So, we must find
-       * the <,>, but the address after it can be empty.
-       */
-      if ((rc = mu_parse822_special (p, e, ',')))
-	{
-	  break;
-	}
-      mu_parse822_skip_comments (p, e);
-
-      rc = mu_parse822_address (p, e, n, hint, hflags);
-
-      if (rc == EOK || rc == EPARSE)
-	{
-	  /* that's cool, it may be a <,>, we'll find out if it isn't
-	   * at the top of the loop
-	   */
-	  rc = EOK;
-	}
-      else
-	{
-	  /* anything else is a fatal error, break out */
-	  break;
-	}
-    }
-
-  if (rc)
-    {
-      mu_address_destroy (a);
-    }
-
-  return rc;
-}
-
-int
-mu_parse822_address (const char **p, const char *e, mu_address_t *a,
-		     mu_address_t hint, int hflags)
-{
-  /* address = mailbox / group / unix-mbox */
-
-  int rc;
-
-  if ((rc = mu_parse822_mail_box (p, e, a, hint, hflags)) == EPARSE)
-    {
-      if ((rc = mu_parse822_group (p, e, a, hint, hflags)) == EPARSE)
-	{
-	  rc = mu_parse822_unix_mbox (p, e, a, hint, hflags);
-	}
-    }
-
-  if (rc == 0 && *a && !(*a)->route)
-    (*a)->route = get_val (hint, hflags, NULL, MU_ADDR_HINT_ROUTE, NULL);
-
-  return rc;
-}
-
-/* No longer put groups into an address node, it wasn't useful, was
- * troublesome, and since there wasn't an end-group marker, wasn't
- * even conceivably useful.
- */
-#undef ADD_GROUPS
-int
-mu_parse822_group (const char **p, const char *e, mu_address_t *a,
-		   mu_address_t hint, int hflags)
-{
-  /* group = phrase ":" [#mailbox] ";" */
-
-  const char *save = *p;
-  mu_address_t *asave = a;		/* so we can destroy these if parsing fails */
-  int rc;
-  char *phrase = 0;
-
-  mu_parse822_skip_comments (p, e);
-
-  *p = save;
-
-  if ((rc = mu_parse822_phrase (p, e, &phrase)))
-    {
-      return rc;
-    }
-
-  mu_parse822_skip_comments (p, e);
-
-  if ((rc = mu_parse822_special (p, e, ':')))
-    {
-      *p = save;
-      str_free (&phrase);
-      return rc;
-    }
-#ifdef ADD_GROUPS
-  /* fake up an address node for the group's descriptive phrase, if
-   * it fails, clean-up will happen after the loop
-   */
-  if ((rc = fill_mb (a, 0, phrase, 0, 0, hint, hflags)) == EOK)
-    {
-      a = &(*a)->next;
-    }
-  else
-    {
-      str_free (&phrase);
-    }
-#else
-  str_free (&phrase);
-#endif
-
-  /* Basically, on each loop, we may find a mailbox, but we must find
-   * a comma after the mailbox, otherwise we've popped off the end
-   * of the list.
-   */
-  while (!rc)
-    {
-      mu_parse822_skip_comments (p, e);
-
-      /* it's ok not be a mailbox, but other errors are fatal */
-      rc = mu_parse822_mail_box (p, e, a, hint, hflags);
-      if (rc == EOK)
-	{
-	  a = &(*a)->next;
-
-	  mu_parse822_skip_comments (p, e);
-	}
-      else if (rc != EPARSE)
-	{
-	  break;
-	}
-
-      if ((rc = mu_parse822_special (p, e, ',')))
-	{
-	  /* the commas aren't optional */
-	  break;
-	}
-    }
-  if (rc == EPARSE)
-    {
-      rc = EOK;			/* ok, as long as we find the ";" next */
-    }
-
-  if (rc || (rc = mu_parse822_special (p, e, ';')))
-    {
-      *p = save;
-
-      mu_address_destroy (asave);
-    }
-
-  return rc;
-}
-
-int
-mu_parse822_mail_box (const char **p, const char *e, mu_address_t *a,
-		      mu_address_t hint, int hflags)
-{
-  /* mailbox =
-   *     addr-spec [ "(" comment ")" ] /
-   *     [phrase] route-addr
-   *
-   *  Note: we parse the ancient comment on the right since
-   *    it's such "common practice". :-(
-   *  Note: phrase is called display-name in drums.
-   *  Note: phrase is optional in drums, though not in RFC 822.
-   */
-  const char *save = *p;
-  int rc;
-
-  /* -> addr-spec */
-  if ((rc = mu_parse822_addr_spec (p, e, a, hint, hflags)) == EOK)
-    {
-      mu_parse822_skip_lwsp (p, e);
-
-      /* yuck. */
-      if ((rc = mu_parse822_comment (p, e, &(*a)->personal)) == EPARSE)
-	{
-	  rc = EOK;
-	  /* cool if there's no comment, */
-	}
-      /* but if something else is wrong, destroy the address */
-      if (rc)
-	{
-	  mu_address_destroy (a);
-	  *p = save;
-	}
-
-      return rc;
-    }
-
-  /* -> phrase route-addr */
-  {
-    char *phrase = 0;
-
-    rc = mu_parse822_phrase (p, e, &phrase);
-
-    if (rc != EPARSE && rc != EOK)
-      {
-	return rc;
-      }
-
-    if ((rc = mu_parse822_route_addr (p, e, a, hint, hflags)) == EOK)
-      {
-	/* add the phrase */
-	(*a)->personal = phrase;
-
-	return EOK;
-      }
-    /* some internal error, fail out */
-    str_free (&phrase);
-    *p = save;
-
-    return rc;
-  }
-
-  return rc;
-}
-
-int
-mu_parse822_route_addr (const char **p, const char *e, mu_address_t *a,
-			mu_address_t hint, int hflags)
-{
-  /* route-addr = "<" [route] addr-spec ">" */
-
-  const char *save = *p;
-  char *route = NULL;
-  int rc;
-  int memflag = 0;
-  
-  mu_parse822_skip_comments (p, e);
-
-  if ((rc = mu_parse822_special (p, e, '<')))
-    {
-      *p = save;
-
-      return rc;
-    }
-  if (!(rc = mu_parse822_special (p, e, '>')))
-    {
-      if ((rc = fill_mb (a, 0, 0, 0, 0, hint, hflags)) == EOK)
-	rc = str_append (&(*a)->email, "");
-       
-      return rc;
-    }
-
-  mu_parse822_route (p, e, &route);
-
-  if ((rc = mu_parse822_addr_spec (p, e, a, hint, hflags)))
-    {
-      *p = save;
-
-      str_free (&route);
-
-      return rc;
-    }
-
-  (*a)->route = get_val (hint, hflags, route, MU_ADDR_HINT_ROUTE,
-			 &memflag);
-
-  mu_parse822_skip_comments (p, e);
-
-  if ((rc = mu_parse822_special (p, e, '>')))
-    {
-      *p = save;
-
-      mu_address_destroy (a);
-
-      return rc;
-    }
-
-  return EOK;
-}
-
-int
-mu_parse822_route (const char **p, const char *e, char **route)
-{
-  /* route = 1#("@" domain ) ":" */
-
-  const char *save = *p;
-  char *accumulator = 0;
-  int rc = EOK;
-
-  for (;;)
-    {
-      mu_parse822_skip_comments (p, e);
-
-      if ((rc = mu_parse822_special (p, e, '@')))
-	{
-	  break;
-	}
-
-      if ((rc = str_append (&accumulator, "@")))
-	{
-	  break;
-	}
-
-      mu_parse822_skip_comments (p, e);
-
-      if ((rc = mu_parse822_domain (p, e, &accumulator)))
-	{
-	  /* it looked like a route, but there's no domain! */
-	  break;
-	}
-
-      mu_parse822_skip_comments (p, e);
-
-      if ((rc = mu_parse822_special (p, e, ',')) == EPARSE)
-	{
-	  /* no more routes, but we got one so its ok */
-	  rc = EOK;
-	  break;
-	}
-      if ((rc = str_append (&accumulator, ", ")))
-	{
-	  break;
-	}
-    }
-
-  mu_parse822_skip_comments (p, e);
-
-  if (!rc)
-    {
-      rc = mu_parse822_special (p, e, ':');
-    }
-
-  if (!rc)
-    {
-      rc = str_append (route, accumulator);
-    }
-  if (rc)
-    {
-      *p = save;
-    }
-  str_free (&accumulator);
-  return rc;
-}
-
-int
-mu_parse822_addr_spec (const char **p, const char *e, mu_address_t *a,
-		       mu_address_t hint, int hflags)
-{
-  /* addr-spec = local-part "@" domain */
-
-  const char *save = *p;
-  char *local_part = 0;
-  char *domain = 0;
-  int rc;
-
-  rc = mu_parse822_local_part (p, e, &local_part);
-
-  mu_parse822_skip_comments (p, e);
-
-  if (!rc)
-    {
-      rc = mu_parse822_special (p, e, '@');
-      
-      if (!rc)
-	{
-	  rc = mu_parse822_domain (p, e, &domain);
-
-	  if (!rc)
-	    rc = fill_mb (a, 0, 0, local_part, domain, hint, hflags);
-	}
-    }
-  
-  if (rc)
-    {
-      *p = save;
-      str_free (&local_part);
-      str_free (&domain);
-    }
-  return rc;
-}
-
-int
-mu_parse822_unix_mbox (const char **p, const char *e, mu_address_t *a,
-		       mu_address_t hint, int hflags)
-{
-  /* unix-mbox = atom */
-
-  const char *save = *p;
-  char *mbox = 0;
-  int rc;
-
-  mu_parse822_skip_comments (p, e);
-
-  rc = mu_parse822_atom (p, e, &mbox);
-
-  if (!rc)
-    rc = fill_mb (a, 0, 0, mbox, 0, hint, hflags);
-
-  if (rc)
-    {
-      *p = save;
-      str_free (&mbox);
-    }
-  return rc;
-}
-
-int
-mu_parse822_local_part (const char **p, const char *e, char **local_part)
-{
-  /* local-part = word *("." word)
-   *
-   * Note: rewrite as ->  word ["." local-part]
-   */
-
-  const char *save = *p;
-  const char *save2 = *p;
-  int rc;
-
-  mu_parse822_skip_comments (p, e);
-
-  if ((rc = mu_parse822_word (p, e, local_part)))
-    {
-      *p = save;
-      return rc;
-    }
-  /* We've got a local-part, but keep looking for more. */
-
-  mu_parse822_skip_comments (p, e);
-
-  /* If we get a parse error, we roll back to save2, but if
-   * something else failed, we have to roll back to save.
-   */
-  save2 = *p;
-
-  rc = mu_parse822_special (p, e, '.');
-
-  if (!rc)
-    {
-      char *more = 0;
-      if ((rc = mu_parse822_local_part (p, e, &more)) == EOK)
-	{
-	  /* append more */
-	  if ((rc = str_append (local_part, ".")) == EOK)
-	    {
-	      rc = str_append (local_part, more);
-	    }
-	}
-      str_free (&more);
-    }
-
-  if (rc == EPARSE)
-    {
-      /* if we didn't get more ("." word) pairs, that's ok */
-      *p = save2;
-      rc = EOK;
-    }
-  if (rc)
-    {
-      /* if anything else failed, that's real */
-      *p = save;
-      str_free (local_part);
-    }
-  return rc;
-}
-
-int
-mu_parse822_domain (const char **p, const char *e, char **domain)
-{
-  /* domain = sub-domain *("." sub-domain)
-   *
-   * Note: rewrite as -> sub-domain ("." domain)
-   */
-
-  const char *save = *p;
-  const char *save2 = 0;
-  int rc;
-
-  mu_parse822_skip_comments (p, e);
-
-  if ((rc = mu_parse822_sub_domain (p, e, domain)))
-    {
-      *p = save;
-      return rc;
-    }
-
-
-  /* We save before skipping comments to preserve the comment
-   * at the end of a domain, the addr-spec may want to abuse it
-   * for a personal name.
-   */
-  save2 = *p;
-
-  /* we've got the 1, keep looking for more */
-
-  mu_parse822_skip_comments (p, e);
-
-  rc = mu_parse822_special (p, e, '.');
-
-  if (!rc)
-    {
-      char *more = 0;
-      if ((rc = mu_parse822_domain (p, e, &more)) == EOK)
-	{
-	  if ((rc = str_append (domain, ".")) == EOK)
-	    {
-	      rc = str_append (domain, more);
-	    }
-	}
-      str_free (&more);
-    }
-  if (rc == EPARSE)
-    {
-      /* we didn't parse more ("." sub-domain) pairs, that's ok */
-      *p = save2;
-      rc = EOK;
-    }
-
-  if (rc)
-    {
-      /* something else failed, roll it all back */
-      *p = save;
-      str_free (domain);
-    }
-  return rc;
-}
-
-int
-mu_parse822_sub_domain (const char **p, const char *e, char **sub_domain)
-{
-  /* sub-domain = domain-ref / domain-literal
-   */
-
-  int rc;
-
-  if ((rc = mu_parse822_domain_ref (p, e, sub_domain)) == EPARSE)
-    rc = mu_parse822_domain_literal (p, e, sub_domain);
-
-  return rc;
-}
-
-int
-mu_parse822_domain_ref (const char **p, const char *e, char **domain_ref)
-{
-  /* domain-ref = atom */
-
-  return mu_parse822_atom (p, e, domain_ref);
-}
-
-int
-mu_parse822_d_text (const char **p, const char *e, char **dtext)
-{
-  /* d-text = 1*dtext
-   *
-   * Note: dtext is only defined as a character class in
-   *  RFC822, but this definition is more useful for
-   *  slurping domain literals.
-   */
-
-  const char *start = *p;
-  int rc = EOK;
-
-  while (*p < e && mu_parse822_is_d_text (**p))
-    {
-      *p += 1;
-    }
-
-  if (start == *p)
-    {
-      return EPARSE;
-    }
-
-  if ((rc = str_append_range (dtext, start, *p)))
-    {
-      *p = start;
-    }
-
-  return rc;
-}
-
-int
-mu_parse822_domain_literal (const char **p, const char *e, char **domain_literal)
-{
-  /* domain-literal = "[" *(dtext / quoted-pair) "]" */
-
-  const char *save = *p;
-  char *literal = 0;
-  int rc;
-
-  if ((rc = mu_parse822_special (p, e, '[')))
-    {
-      return rc;
-    }
-  if ((rc = str_append_char (&literal, '[')))
-    {
-      *p = save;
-      return rc;
-    }
-
-  while ((rc = mu_parse822_d_text (p, e, &literal)) == EOK ||
-	 (rc = mu_parse822_quoted_pair (p, e, &literal)) == EOK)
-    {
-      /* Eat all of this we can get! */
-    }
-  if (rc == EPARSE)
-    {
-      rc = EOK;
-    }
-  if (!rc)
-    {
-      rc = mu_parse822_special (p, e, ']');
-    }
-  if (!rc)
-    {
-      rc = str_append_char (&literal, ']');
-    }
-  if (!rc)
-    {
-      rc = str_append (domain_literal, literal);
-    }
-
-  str_free (&literal);
-
-  if (rc)
-    {
-      *p = save;
-    }
-  return rc;
-}
-
-/***** From RFC 822, 5.1 Date and Time Specification Syntax *****/
-
-int
-mu_parse822_day (const char **p, const char *e, int *day)
-{
-  /* day = "Mon" / "Tue" / "Wed" / "Thu" / "Fri" / "Sat" / "Sun" */
-
-  const char *days[] = {
-    "Sun",
-    "Mon",
-    "Tue",
-    "Wed",
-    "Thu",
-    "Fri",
-    "Sat",
-    NULL
-  };
-
-  int d;
-
-  mu_parse822_skip_comments (p, e);
-
-  if ((e - *p) < 3)
-    return EPARSE;
-
-  for (d = 0; days[d]; d++)
-    {
-      if (mu_c_strncasecmp (*p, days[d], 3) == 0)
-	{
-	  *p += 3;
-	  if (day)
-	    *day = d;
-	  return EOK;
-	}
-    }
-  return EPARSE;
-}
-
-int
-mu_parse822_date (const char **p, const char *e, int *day, int *mon, int *year)
-{
-  /* date = 1*2DIGIT month 2*4DIGIT
-   * month =  "Jan"  /  "Feb" /  "Mar"  /  "Apr"
-   *       /  "May"  /  "Jun" /  "Jul"  /  "Aug"
-   *       /  "Sep"  /  "Oct" /  "Nov"  /  "Dec"
-   */
-
-  const char *mons[] = {
-    "Jan",
-    "Feb",
-    "Mar",
-    "Apr",
-    "May",
-    "Jun",
-    "Jul",
-    "Aug",
-    "Sep",
-    "Oct",
-    "Nov",
-    "Dec",
-    NULL
-  };
-
-  const char *save = *p;
-  int rc = EOK;
-  int m = 0;
-  int yr = 0;
-  const char *yrbeg = 0;
-
-  mu_parse822_skip_comments (p, e);
-
-  if ((rc = mu_parse822_digits (p, e, 1, 2, day)))
-    {
-      *p = save;
-      return rc;
-    }
-
-  mu_parse822_skip_comments (p, e);
-
-  if ((e - *p) < 3)
-    return EPARSE;
-
-  for (m = 0; mons[m]; m++)
-    {
-      if (mu_c_strncasecmp (*p, mons[m], 3) == 0)
-	{
-	  *p += 3;
-	  if (mon)
-	    *mon = m;
-	  break;
-	}
-    }
-
-  if (!mons[m])
-    {
-      *p = save;
-      return EPARSE;
-    }
-
-  mu_parse822_skip_comments (p, e);
-
-  /* We need to count how many digits their were, and adjust the
-   * interpretation of the year accordingly. This is from RFC 2822,
-   * Section 4.3, Obsolete Date and Time. */
-  yrbeg = *p;
-
-  if ((rc = mu_parse822_digits (p, e, 2, 4, &yr)))
-    {
-      *p = save;
-      return rc;
-    }
-
-  /* rationalize year to four digit, then adjust to tz notation */
-  switch (*p - yrbeg)
-    {
-    case 2:
-      if (yr >= 0 && yr <= 49)
-	{
-	  yr += 2000;
-	  break;
-	}
-    case 3:
-      yr += 1900;
-      break;
-    }
-
-  if (year)
-    *year = yr - 1900;
-
-  return EOK;
-}
-
-int
-mu_parse822_time (const char **p, const char *e,
-	       int *hour, int *min, int *sec, int *tz, const char **tz_name)
-{
-  /* time        =  hour zone
-   * hour        =  2DIGIT ":" 2DIGIT [":" 2DIGIT] ; 00:00:00 - 23:59:59
-   * zone        =  "UT"  / "GMT"           ; Universal Time
-   *                                        ; North American : UT
-   *             /  "EST" / "EDT"           ;  Eastern:  - 5/ - 4
-   *             /  "CST" / "CDT"           ;  Central:  - 6/ - 5
-   *             /  "MST" / "MDT"           ;  Mountain: - 7/ - 6
-   *             /  "PST" / "PDT"           ;  Pacific:  - 8/ - 7
-   *             /  1ALPHA                  ; RFC 822 was wrong, RFC 2822
-   *                                        ; says treat these all as -0000.
-   *             / ( ("+" / "-") 4DIGIT )   ; Local differential
-   *                                        ;  hours+min. (HHMM)
-   */
-
-  struct
-  {
-    const char *tzname;
-    int tz;
-  }
-  tzs[] =
-  {
-    { "UT",   0 * 60 * 60 },
-    { "UTC",  0 * 60 * 60 },
-    { "GMT",  0 * 60 * 60 },
-    { "EST", -5 * 60 * 60 },
-    { "EDT", -4 * 60 * 60 },
-    { "CST", -6 * 60 * 60 },
-    { "CDT", -5 * 60 * 60 },
-    { "MST", -7 * 60 * 60 },
-    { "MDT", -6 * 60 * 60 },
-    { "PST", -8 * 60 * 60 },
-    { "PDT", -7 * 60 * 60 },
-    { NULL, 0}
-  };
-
-  const char *save = *p;
-  int rc = EOK;
-  int z = 0;
-  char *zone = NULL;
-
-  mu_parse822_skip_comments (p, e);
-
-  if ((rc = mu_parse822_digits (p, e, 1, 2, hour)))
-    {
-      *p = save;
-      return rc;
-    }
-
-  if ((rc = mu_parse822_special (p, e, ':')))
-    {
-      *p = save;
-      return rc;
-    }
-
-  if ((rc = mu_parse822_digits (p, e, 1, 2, min)))
-    {
-      *p = save;
-      return rc;
-    }
-
-  if ((rc = mu_parse822_special (p, e, ':')))
-    {
-      *sec = 0;
-    }
-  else if ((rc = mu_parse822_digits (p, e, 1, 2, sec)))
-    {
-      *p = save;
-      return rc;
-    }
-
-  mu_parse822_skip_comments (p, e);
-
-  if ((rc = mu_parse822_atom (p, e, &zone)))
-    {
-      /* zone is optional */
-      if (tz)
-	*tz = 0;
-      return EOK;
-    }
-
-  /* see if it's a timezone */
-  for (; tzs[z].tzname; z++)
-    {
-      if (mu_c_strcasecmp (zone, tzs[z].tzname) == 0)
-	break;
-    }
-  if (tzs[z].tzname)
-    {
-      if (tz_name)
-	*tz_name = tzs[z].tzname;
-
-      if (tz)
-	*tz = tzs[z].tz;
-    }
-  else if (strlen (zone) > 5 || strlen (zone) < 4)
-    {
-      if (*tz)
-	*tz = 0; /* Assume UTC */
-    }
-  else
-    {
-      /* zone = ( + / - ) hhmm */
-      int hh;
-      int mm;
-      int sign;
-      char *zp = zone;
-
-      switch (zp[0])
-	{
-	case '-':
-	  sign = -1;
-	  zp++;
-	  break;
-	case '+':
-	  sign = +1;
-	  zp++;
-	  break;
-	default:
-	  sign = 1;
-	  break;
-	}
-
-      if (strspn (zp, "0123456789") == 4)
-	{
-      /* convert to seconds from UTC */
-      hh = (zone[1] - '0') * 10 + (zone[2] - '0');
-      mm = (zone[3] - '0') * 10 + (zone[4] - '0');
-	}
-      else
-	{
-	  hh = mm = 0; /* Consider equivalent to -0000 */
-	}
-      if (tz)
-	*tz = sign * (hh * 60 * 60 + mm * 60);
-    }
-
-  str_free (&zone);
-
-  return EOK;
-}
-
-#if 0
-For reference, especially the for the required range and values of the
-integer fields.
-
-struct tm
-{
-  int tm_sec;			/* Seconds.	[0-60] (1 leap second) */
-  int tm_min;			/* Minutes.	[0-59] */
-  int tm_hour;			/* Hours.	[0-23] */
-  int tm_mday;			/* Day.		[1-31] */
-  int tm_mon;			/* Month.	[0-11] */
-  int tm_year;			/* Year	- 1900.  */
-  int tm_wday;			/* Day of week.	[0-6] */
-  int tm_yday;			/* Days in year.[0-365]	*/
-  int tm_isdst;			/* DST.		[-1/0/1]*/
-
-  int tm_gmtoff;        /* Seconds east of UTC. */
-  const char *tm_zone;	/* Timezone abbreviation.  */
-};
-#endif
-
-int
-mu_parse822_date_time (const char **p, const char *e, struct tm *tm,
-		       struct mu_timezone *tz)
-{
-  /* date-time = [ day "," ] date time */
-
-  const char *save = *p;
-  int rc = 0;
-
-  int wday = 0;
-
-  int mday = 0;
-  int mon = 0;
-  int year = 0;
-
-  int hour = 0;
-  int min = 0;
-  int sec = 0;
-
-  int tzoffset = 0;
-  const char *tz_name = 0;
-
-  if ((rc = mu_parse822_day (p, e, &wday)))
-    {
-      if (rc != EPARSE)
-	return rc;
-    }
-  else
-    {
-      /* If we got a day, we MUST have a ','. */
-      mu_parse822_skip_comments (p, e);
-
-      if ((rc = mu_parse822_special (p, e, ',')))
-	{
-	  *p = save;
-	  return rc;
-	}
-    }
-
-  if ((rc = mu_parse822_date (p, e, &mday, &mon, &year)))
-    {
-      *p = save;
-      return rc;
-    }
-  if ((rc = mu_parse822_time (p, e, &hour, &min, &sec, &tzoffset, &tz_name)))
-    {
-      *p = save;
-      return rc;
-    }
-
-  if (tm)
-    {
-      memset (tm, 0, sizeof (*tm));
-
-      tm->tm_wday = wday;
-
-      tm->tm_mday = mday;
-      tm->tm_mon = mon;
-      tm->tm_year = year;
-
-      tm->tm_hour = hour;
-      tm->tm_min = min;
-      tm->tm_sec = sec;
-
-#ifdef HAVE_STRUCT_TM_TM_ISDST
-      tm->tm_isdst = -1;	/* unknown whether it's dst or not */
-#endif
-#ifdef HAVE_STRUCT_TM_TM_GMTOFF
-      tm->tm_gmtoff = tzoffset;
-#endif
-#ifdef HAVE_STRUCT_TM_TM_ZONE
-      tm->tm_zone = (char*) tz_name;
-#endif
-    }
-
-  if (tz)
-    {
-      tz->utc_offset = tzoffset;
-      tz->tz_name = tz_name;
-    }
-
-  return EOK;
-}
-
-/***** From RFC 822, 3.2 Header Field Definitions *****/
-
-int
-mu_parse822_field_name (const char **p, const char *e, char **fieldname)
-{
-  /* field-name = 1*<any char, excluding ctls, space, and ":"> ":" */
-
-  const char *save = *p;
-
-  char *fn = NULL;
-
-  while (*p != e)
-    {
-      char c = **p;
-
-      if (!mu_parse822_is_char (c))
-	break;
-
-      if (mu_parse822_is_ctl (c))
-	break;
-      if (mu_parse822_is_space (c))
-	break;
-      if (c == ':')
-	break;
-
-      str_append_char (&fn, c);
-      *p += 1;
-    }
-  /* must be at least one char in the field name */
-  if (!fn)
-    {
-      *p = save;
-      return EPARSE;
-    }
-  mu_parse822_skip_comments (p, e);
-
-  if (!mu_parse822_special (p, e, ':'))
-    {
-      *p = save;
-      if (fn)
-	free (fn);
-      return EPARSE;
-    }
-
-  *fieldname = fn;
-
-  return EOK;
-}
-
-int
-mu_parse822_field_body (const char **p, const char *e, char **fieldbody)
-{
-  /* field-body = *text [CRLF lwsp-char field-body] */
-
-  /*const char *save = *p; */
-
-  char *fb = NULL;
-
-  for (;;)
-    {
-      const char *eol = *p;
-      while (eol != e)
-	{
-	  /*char c = *eol; */
-	  if (eol[0] == '\r' && (eol + 1) != e && eol[1] == '\n')
-	    break;
-	  ++eol;
-	}
-      str_append_range (&fb, *p, eol);
-      *p = eol;
-      if (eol == e)
-	break;			/* no more, so we're done */
-
-      /*assert(p[0] == '\r'); */
-      /*assert(p[1] == '\n'); */
-
-      *p += 2;
-
-      if (*p == e)
-	break;			/* no more, so we're done */
-
-      /* check if next line is a continuation line */
-      if (**p != ' ' && **p != '\t')
-	break;
-    }
-
-  *fieldbody = fb;
-
-  return EOK;
-}
-
-/***** RFC 822 Quoting Functions *****/
-
-int
-mu_parse822_quote_string (char **quoted, const char *raw)
-{
-  /* quoted-string = <"> *(qtext/quoted-pair) <">
-   *
-   * So double quote the string, and back quote anything that
-   * isn't qtext.
-   */
-
-  int rc = EOK;
-  const char *s;
-
-  if (!raw || !quoted || *quoted)
-    {
-      return EINVAL;
-    }
-
-  s = raw;
-
-  rc = str_append_char (quoted, '"');
-
-  while (!rc && *s)
-    {
-      if (!mu_parse822_is_q_text (*s))
-	{
-	  rc = str_append_char (quoted, '\\');
-	}
-
-      if (!rc)
-	{
-	  rc = str_append_char (quoted, *s);
-	}
-      ++s;
-    }
-
-  if (!rc)
-    {
-      rc = str_append_char (quoted, '"');
-    }
-
-  if (rc)
-    {
-      str_free (quoted);
-    }
-  return rc;
-}
-
-int
-mu_parse822_quote_local_part (char **quoted, const char *raw)
-{
-  /* local-part = word * ("." word)
-   * word = atom / quoted-string
-   *
-   * So, if any character isn't a "." or an atom character, we quote
-   * the whole thing as a string, for simplicity, otherwise just
-   * copy it.
-   */
-
-  const char *s = 0;
-
-  if (!raw || !quoted || *quoted)
-    {
-      return EINVAL;
-    }
-  s = raw;
-
-  while (*s)
-    {
-      if (*s != '.' && !mu_parse822_is_atom_char (*s))
-	{
-	  return mu_parse822_quote_string (quoted, raw);
-	}
-      ++s;
-    }
-
-  /* if we don't have to quote it, just copy it over */
-
-  return str_append (quoted, raw);
-}
-
diff --git a/libmailutils/parsedate.y b/libmailutils/parsedate.y
deleted file mode 100644
index 79eda8b..0000000
--- a/libmailutils/parsedate.y
+++ /dev/null
@@ -1,1206 +0,0 @@
-%{
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 2003, 2007, 2008, 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 2, or (at your option)
-   any later version.
-   
-   GNU Mailutils 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 GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA  */
-  
-/* A heavily modified version of the well-known public domain getdate.y.
-   It was originally written by Steven M. Bellovin <smb@research.att.com>
-   while at the University of North Carolina at Chapel Hill.  Later tweaked
-   by a couple of people on Usenet.  Completely overhauled by Rich $alz
-   <rsalz@bbn.com> and Jim Berets <jberets@bbn.com> in August, 1990.
-   Rewritten using a proper union by Sergey Poznyakoff <gray@gnu.org> */
-
-#ifdef HAVE_CONFIG_H
-# include <config.h>
-#endif
-
-#include <stdio.h>
-#include <ctype.h>
-
-#if HAVE_STDLIB_H
-# include <stdlib.h> /* for `free'; used by Bison 1.27 */
-#endif
-
-#if defined (STDC_HEADERS) || (!defined (isascii) && !defined (HAVE_ISASCII))
-# define IN_CTYPE_DOMAIN(c) 1
-#else
-# define IN_CTYPE_DOMAIN(c) isascii(c)
-#endif
-
-#define ISSPACE(c) (IN_CTYPE_DOMAIN (c) && isspace (c))
-#define ISALPHA(c) (IN_CTYPE_DOMAIN (c) && isalpha (c))
-#define ISUPPER(c) (IN_CTYPE_DOMAIN (c) && isupper (c))
-#define ISDIGIT_LOCALE(c) (IN_CTYPE_DOMAIN (c) && isdigit (c))
-
-/* ISDIGIT differs from ISDIGIT_LOCALE, as follows:
-   - Its arg may be any int or unsigned int; it need not be an unsigned char.
-   - It's guaranteed to evaluate its argument exactly once.
-   - It's typically faster.
-   Posix 1003.2-1992 section 2.5.2.1 page 50 lines 1556-1558 says that
-   only '0' through '9' are digits.  Prefer ISDIGIT to ISDIGIT_LOCALE unless
-   it's important to use the locale's definition of `digit' even when the
-   host does not conform to Posix.  */
-#define ISDIGIT(c) ((unsigned) (c) - '0' <= 9)
-
-#if defined (STDC_HEADERS) || defined (USG)
-# include <string.h>
-#endif
-
-/* Some old versions of bison generate parsers that use bcopy.
-   That loses on systems that don't provide the function, so we have
-   to redefine it here.  */
-#if !defined (HAVE_BCOPY) && defined (HAVE_MEMCPY) && !defined (bcopy)
-# define bcopy(from, to, len) memcpy ((to), (from), (len))
-#endif
-
-static int yylex ();
-static int yyerror ();
-
-#define EPOCH		1970
-#define HOUR(x)		((x) * 60)
-
-#define MAX_BUFF_LEN    128   /* size of buffer to read the date into */
-
-/*
-**  An entry in the lexical lookup table.
-*/
-typedef struct _lex_tab {
-  const char	*name;
-  int		type;
-  int		value;
-} SYMBOL;
-
-
-/*
-**  Meridian:  am, pm, or 24-hour style.
-*/
-typedef enum meridian {
-  MERam,
-  MERpm,
-  MER24
-} MERIDIAN;
-
-#define PD_MASK_SECOND   00001
-#define PD_MASK_MINUTE   00002
-#define PD_MASK_HOUR     00004
-#define PD_MASK_DAY      00010
-#define PD_MASK_MONTH    00020
-#define PD_MASK_YEAR     00040
-#define PD_MASK_TZ       00100 
-#define PD_MASK_MERIDIAN 00200
-#define PD_MASK_ORDINAL  00400
-#define PD_MASK_NUMBER   01000
-
-#define PD_MASK_TIME PD_MASK_SECOND|PD_MASK_MINUTE|PD_MASK_HOUR
-#define PD_MASK_DATE PD_MASK_DAY|PD_MASK_MONTH|PD_MASK_YEAR
-#define PD_MASK_DOW PD_MASK_NUMBER
- 
-#define MASK_IS_SET(f,m) (((f)&(m))==(m))
-#define MASK_TEST(f,m)   ((f)&(m)) 
-struct pd_date
-{
-  int mask;
-  int day;
-  int hour;
-  int minute;
-  int month;
-  int second;
-  int year;
-  int tz;
-  enum meridian meridian;
-  int number;
-  int ordinal;
-};
-
-#define DATE_INIT(date) memset(&(date), 0, sizeof(date))
-#define DATE_SET(date, memb, m, val, lim, onerror)                        \
- do                                                                       \
-   {                                                                      \
-     int __x = val;                                                       \
-     if (((m) != PD_MASK_TZ && __x < 0) || (lim && __x > lim)) onerror;   \
-     date . memb = __x; date.mask |= m;                                   \
-   }                                                                      \
- while (0)
-   
-#define __SET_SECOND(d,v,a)   DATE_SET(d,second,PD_MASK_SECOND,v,59,a)
-#define __SET_MINUTE(d,v,a)   DATE_SET(d,minute,PD_MASK_MINUTE,v,59,a)  
-#define __SET_HOUR(d,v,a)     DATE_SET(d,hour,PD_MASK_HOUR,v,23,a)
-#define __SET_DAY(d,v,a)      DATE_SET(d,day,PD_MASK_DAY,v,31,a)   
-#define __SET_MONTH(d,v,a)    DATE_SET(d,month,PD_MASK_MONTH,v,12,a)
-#define __SET_YEAR(d,v,a)     DATE_SET(d,year,PD_MASK_YEAR,v,0,a)  
-#define __SET_TZ(d,v,a)       DATE_SET(d,tz,PD_MASK_TZ,v,0,a)
-#define __SET_MERIDIAN(d,v,a) DATE_SET(d,meridian,PD_MASK_MERIDIAN,v,MER24,a)
-#define __SET_ORDINAL(d,v,a)  DATE_SET(d,ordinal,PD_MASK_ORDINAL,v,0,a)
-#define __SET_NUMBER(d,v,a)   DATE_SET(d,number,PD_MASK_NUMBER,v,0,a) 
- 
-#define SET_SECOND(d,v)   __SET_SECOND(d,v,YYERROR)   
-#define SET_MINUTE(d,v)   __SET_MINUTE(d,v,YYERROR)   
-#define SET_HOUR(d,v)     __SET_HOUR(d,v,YYERROR)     
-#define SET_DAY(d,v)      __SET_DAY(d,v,YYERROR)      
-#define SET_MONTH(d,v)    __SET_MONTH(d,v,YYERROR)    
-#define SET_YEAR(d,v)     __SET_YEAR(d,v,YYERROR)     
-#define SET_TZ(d,v)       __SET_TZ(d,v,YYERROR)       
-#define SET_MERIDIAN(d,v) __SET_MERIDIAN(d,v,YYERROR) 
-#define SET_ORDINAL(d,v)  __SET_ORDINAL(d,v,YYERROR)  
-#define SET_NUMBER(d,v)   __SET_NUMBER(d,v,YYERROR)   
-
-int
-pd_date_union (struct pd_date *a, struct pd_date *b)
-{
-  int diff = (~a->mask) & b->mask;
-  if (!diff)
-    return 1;
-
-  a->mask |= diff;
-  
-  if (diff & PD_MASK_SECOND)
-    a->second = b->second;
-  
-  if (diff & PD_MASK_MINUTE)
-    a->minute = b->minute;
-
-  if (diff & PD_MASK_HOUR)
-    a->hour = b->hour;
-
-  if (diff & PD_MASK_DAY)
-    a->day = b->day;
-
-  if (diff & PD_MASK_MONTH)
-    a->month = b->month;
-
-  if (diff & PD_MASK_YEAR)
-    a->year = b->year;
-
-  if (diff & PD_MASK_TZ)
-    a->tz = b->tz;
-
-  if (diff & PD_MASK_MERIDIAN)
-    a->meridian = b->meridian;
-
-  if (diff & PD_MASK_ORDINAL)
-    a->ordinal = b->ordinal;
-
-  if (diff & PD_MASK_NUMBER)
-    a->number = b->number;
-
-  return 0;
-}
-
-struct pd_datespec
-{
-  struct pd_date date;
-  struct pd_date rel;
-};
-
-static struct pd_datespec pd;
- 
-static const char	*yyinput;
-
-%}
-
-%union {
-  int number;
-  enum meridian meridian;
-  struct pd_date date;
-  struct pd_datespec datespec;
-}
-
-/*FIXME: do we need T_ID? */
-%token  T_AGO T_DST  T_ID
-
-%token <number> T_DAY T_DAY_UNIT T_DAYZONE T_HOUR_UNIT T_MINUTE_UNIT
-%token <number> T_MONTH T_MONTH_UNIT
-%token <number>	T_SEC_UNIT T_SNUMBER T_UNUMBER T_YEAR_UNIT T_ZONE
-%token <meridian> T_MERIDIAN
-
-%type <meridian> o_merid
-%type <date> item time zone date day rel relspec relunit
-%type <datespec> spec
-
-%%
-
-input   : spec
-          {
-	    pd = $1;
-	  }
-        ;
-
-spec	: /* NULL */
-          {
-	    DATE_INIT ($$.date);
-	    DATE_INIT ($$.rel);
-	  }
-	| spec item
-          {
-	    if (pd_date_union (&$1.date, &$2))
-	      YYERROR;
-	    $$ = $1;
-	  }
-        | spec rel
-          {
-	    if (pd_date_union (&$1.rel, &$2))
-	      YYERROR;
-	    $$ = $1;
-	  }
-        | spec T_UNUMBER
-          {
-	    if (MASK_IS_SET ($1.date.mask, (PD_MASK_TIME|PD_MASK_DATE))
-		&& !$1.rel.mask)
-	      SET_YEAR ($1.date, $2);
-	    else
-	      {
-		if ($2 > 10000)
-		  {
-		    SET_DAY ($1.date, $2 % 100);
-		    SET_MONTH ($1.date, ($2 / 100) %100);
-		    SET_YEAR ($1.date, $2 / 10000);
-		  }
-		else
-		  {
-		    if ($2 < 100)
-		      {
-			SET_YEAR ($1.date, $2);
-		      }
-		    else
-		      {
-		    	SET_HOUR ($1.date, $2 / 100);
-		    	SET_MINUTE ($1.date, $2 % 100);
-		      }
-		    SET_MERIDIAN ($1.date, MER24);
-		  }
-	      }
-	    $$ = $1;
-	  }
-	;
-
-item	: time
-        | zone
-	| date
-	| day
-	;
-
-time	: T_UNUMBER T_MERIDIAN
-          {
-	    DATE_INIT ($$);
-	    SET_HOUR ($$, $1);
-	    SET_MERIDIAN ($$, $2);
-	  }
-	| T_UNUMBER ':' T_UNUMBER o_merid
-          {
-	    DATE_INIT ($$);
-	    SET_HOUR ($$, $1);
-	    SET_MINUTE ($$, $3);
-	    SET_MERIDIAN ($$, $4);
-	  }
-	| T_UNUMBER ':' T_UNUMBER T_SNUMBER
-          {
-	    DATE_INIT ($$);
-	    SET_HOUR ($$, $1);
-	    SET_MINUTE ($$, $3);
-	    SET_MERIDIAN ($$, MER24);
-	    SET_TZ ($$, ($4 < 0
-			   ? -$4 % 100 + (-$4 / 100) * 60
-			   : - ($4 % 100 + ($4 / 100) * 60)));
-
-	  }
-	| T_UNUMBER ':' T_UNUMBER ':' T_UNUMBER o_merid
-          {
-	    DATE_INIT ($$);
-	    SET_HOUR ($$, $1);
-	    SET_MINUTE ($$, $3);
-	    SET_SECOND ($$, $5);
-	    SET_MERIDIAN ($$, $6);
-	  }
-	| T_UNUMBER ':' T_UNUMBER ':' T_UNUMBER T_SNUMBER
-          {
-	    DATE_INIT ($$);
-	    SET_HOUR ($$, $1);
-	    SET_MINUTE ($$, $3);
-	    SET_SECOND ($$, $5);
-	    SET_MERIDIAN ($$, MER24);
-	    SET_TZ ($$, ($6 < 0
-			 ? -$6 % 100 + (-$6 / 100) * 60
-			 : - ($6 % 100 + ($6 / 100) * 60)));
-	  }
-	;
-
-zone	: T_ZONE
-          {
-	    DATE_INIT ($$);
-	    SET_TZ ($$, $1);
-	  }
-	| T_DAYZONE
-          {
-	    DATE_INIT ($$);
-	    SET_TZ ($$, $1 - 60);
-	  }
-	| T_ZONE T_DST
-          {
-	    DATE_INIT ($$);
-	    SET_TZ ($$, $1 - 60);
-	  }
-	;
-
-day	: T_DAY
-          {
-	    DATE_INIT ($$);
-	    SET_ORDINAL ($$, 1);
-	    SET_NUMBER ($$, $1);
-	  }
-	| T_DAY ','
-          {
-	    DATE_INIT ($$);
-	    SET_ORDINAL ($$, 1);
-	    SET_NUMBER ($$, $1);
-	  }
-	| T_UNUMBER T_DAY
-          {
-	    DATE_INIT ($$);
-	    SET_ORDINAL ($$, $1);
-	    SET_NUMBER ($$, $2);
-	  }
-	;
-
-date	: T_UNUMBER '/' T_UNUMBER
-          {
-	    DATE_INIT ($$);
-	    SET_MONTH ($$, $1);
-	    SET_DAY ($$, $3);
-	  }
-	| T_UNUMBER '/' T_UNUMBER '/' T_UNUMBER
-          {
-	    DATE_INIT ($$);
-	    /* Interpret as YYYY/MM/DD if $1 >= 1000, otherwise as MM/DD/YY.
-	       The goal in recognizing YYYY/MM/DD is solely to support legacy
-	       machine-generated dates like those in an RCS log listing.  If
-	       you want portability, use the ISO 8601 format.  */
-	    if ($1 >= 1000)
-	      {
-		SET_YEAR ($$, $1);
-		SET_MONTH ($$, $3);
-		SET_DAY ($$, $5);
-	      }
-	    else
-	      {
-		SET_MONTH ($$, $1);
-		SET_DAY ($$, $3);
-		SET_YEAR ($$, $5);
-	      }
-	  }
-	| T_UNUMBER T_SNUMBER T_SNUMBER
-          {
-	    DATE_INIT ($$);
-	    /* ISO 8601 format.  yyyy-mm-dd.  */
-	    SET_YEAR ($$, $1);
-	    SET_MONTH ($$, -$2);
-	    SET_DAY ($$, -$3);
-	  }
-	| T_UNUMBER T_MONTH T_SNUMBER
-          {
-	    /* either 17-JUN-1992 or 1992-JUN-17 */
-	    DATE_INIT ($$);
-	    if ($1 < 32)
-	      {
-		SET_DAY ($$, $1);
-		SET_MONTH ($$, $2);
-		SET_YEAR ($$, -$3);
-	      }
-	    else
-	      {
-		SET_DAY ($$, -$3);
-		SET_MONTH ($$, $2);
-		SET_YEAR ($$, $1);
-	      }
-	  }
-	| T_MONTH T_UNUMBER
-          {
-	    DATE_INIT ($$);
-	    SET_MONTH ($$, $1);
-	    SET_DAY ($$, $2);
-	  }
-	| T_MONTH T_UNUMBER ',' T_UNUMBER
-          {
-	    DATE_INIT ($$);
-	    SET_MONTH ($$, $1);
-	    SET_DAY ($$, $2);
-	    SET_YEAR ($$, $4);
-	  }
-	| T_UNUMBER T_MONTH
-          {
-	    DATE_INIT ($$);
-	    SET_MONTH ($$, $2);
-	    SET_DAY ($$, $1);
-	  }
-	| T_UNUMBER T_MONTH T_UNUMBER
-          {
-	    DATE_INIT ($$);
-	    SET_MONTH ($$, $2);
-	    SET_DAY ($$, $1);
-	    SET_YEAR ($$, $3);
-	  }
-        | T_DAY T_MONTH T_UNUMBER time T_UNUMBER
-          {
-	    DATE_INIT ($$);
-
-	    SET_ORDINAL ($$, 1);
-	    SET_NUMBER ($$, $1);
-
-	    SET_MONTH ($$, $2);
-	    SET_DAY ($$, $3);
-	    SET_YEAR ($$, $5);
-	    if (pd_date_union (&$$, &$4))
-	      YYERROR;
-	  }
-	;
-
-rel	: relspec T_AGO
-          {
-	    $1.second = - $1.second;
-	    $1.minute = - $1.minute;
-	    $1.hour = - $1.hour;
-	    $1.day = - $1.day;
-	    $1.month = - $1.month;
-	    $1.year = - $1.year;
-	    $$ = $1;
-	  }
-	| relspec
-	;
-
-
-relspec : relunit
-          {
-	    DATE_INIT ($$);
-	    if (pd_date_union (&$$, &$1))
-	      YYERROR;
-	  }
-        | relspec relunit
-          {
-	    if (pd_date_union (&$1, &$2))
-	      YYERROR;
-	    $$ = $1;
-	  }
-        ;
-
-relunit	: T_UNUMBER T_YEAR_UNIT
-          {
-	    DATE_INIT ($$);
-	    SET_YEAR ($$, $1 * $2);
-	  }
-	| T_SNUMBER T_YEAR_UNIT
-          {
-	    DATE_INIT ($$);
-	    SET_YEAR ($$, $1 * $2);
-	  }
-	| T_YEAR_UNIT
-          {
-	    DATE_INIT ($$);
-	    SET_YEAR ($$, $1);
-	  }
-	| T_UNUMBER T_MONTH_UNIT
-          {
-	    DATE_INIT ($$);
-	    SET_MONTH ($$, $1 * $2);
-	  }
-	| T_SNUMBER T_MONTH_UNIT
-          {
-	    DATE_INIT ($$);
-	    SET_MONTH ($$, $1 * $2);
-	  }
-	| T_MONTH_UNIT
-          {
-	    DATE_INIT ($$);
-	    SET_MONTH ($$, $1);
-	  }
-	| T_UNUMBER T_DAY_UNIT
-          {
-	    DATE_INIT ($$);
-	    SET_DAY ($$, $1 * $2);
-	  }
-	| T_SNUMBER T_DAY_UNIT
-          {
-	    DATE_INIT ($$);
-	    SET_DAY ($$, $1 * $2);
-	  }
-	| T_DAY_UNIT
-          {
-	    DATE_INIT ($$);
-	    SET_DAY ($$, $1);
-	  }
-	| T_UNUMBER T_HOUR_UNIT
-          {
-	    DATE_INIT ($$);
-	    SET_HOUR ($$, $1 * $2);
-	  }
-	| T_SNUMBER T_HOUR_UNIT
-          {
-	    DATE_INIT ($$);
-	    SET_HOUR ($$, $1 * $2);
-	  }
-	| T_HOUR_UNIT
-          {
-	    DATE_INIT ($$);
-	    SET_HOUR ($$, $1);
-	  }
-	| T_UNUMBER T_MINUTE_UNIT
-          {
-	    DATE_INIT ($$);
-	    SET_MINUTE ($$, $1 * $2);
-	  }
-	| T_SNUMBER T_MINUTE_UNIT
-          {
-	    DATE_INIT ($$);
-	    SET_MINUTE ($$, $1 * $2);
-	  }
-	| T_MINUTE_UNIT
-          {
-	    DATE_INIT ($$);
-	    SET_MINUTE ($$, $1);
-	  }
-	| T_UNUMBER T_SEC_UNIT
-          {
-	    DATE_INIT ($$);
-	    SET_SECOND ($$, $1 * $2);
-	  }
-	| T_SNUMBER T_SEC_UNIT
-          {
-	    DATE_INIT ($$);
-	    SET_SECOND ($$, $1 * $2);
-	  }
-	| T_SEC_UNIT
-          {
-	    DATE_INIT ($$);
-	    SET_SECOND ($$, $1);
-	  }
-	;
-
-o_merid	: /* empty */
-          {
-	    $$ = MER24;
-	  }
-	| T_MERIDIAN
-	  {
-	    $$ = $1;
-	  }
-	;
-
-%%
-
-#include <mailutils/types.h>
-
-#if TIME_WITH_SYS_TIME
-# include <sys/time.h>
-# include <time.h>
-#else
-# if HAVE_SYS_TIME_H
-#  include <sys/time.h>
-# else
-#  include <time.h>
-# endif
-#endif
-
-#include <mailutils/mutil.h>
-
-/* Month and day table. */
-static SYMBOL const month_day_tab[] = {
-  { "january",	T_MONTH,  1 },
-  { "february",	T_MONTH,  2 },
-  { "march",	T_MONTH,  3 },
-  { "april",	T_MONTH,  4 },
-  { "may",	T_MONTH,  5 },
-  { "june",	T_MONTH,  6 },
-  { "july",	T_MONTH,  7 },
-  { "august",	T_MONTH,  8 },
-  { "september",T_MONTH,  9 },
-  { "sept",	T_MONTH,  9 },
-  { "october",	T_MONTH, 10 },
-  { "november",	T_MONTH, 11 },
-  { "december",	T_MONTH, 12 },
-  { "sunday",	T_DAY,   0 },
-  { "monday",	T_DAY,   1 },
-  { "tuesday",	T_DAY,   2 },
-  { "tues",	T_DAY,   2 },
-  { "wednesday",T_DAY,   3 },
-  { "wednes",	T_DAY,   3 },
-  { "thursday",	T_DAY,   4 },
-  { "thur",	T_DAY,   4 },
-  { "thurs",	T_DAY,   4 },
-  { "friday",	T_DAY,   5 },
-  { "saturday",	T_DAY,   6 },
-  { NULL, 0, 0 }
-};
-
-/* Time units table. */
-static SYMBOL const units_tab[] = {
-  { "year",	T_YEAR_UNIT,	1 },
-  { "month",	T_MONTH_UNIT,	1 },
-  { "fortnight",T_DAY_UNIT,	14 },
-  { "week",	T_DAY_UNIT,	7 },
-  { "day",	T_DAY_UNIT,	1 },
-  { "hour",	T_HOUR_UNIT,	1 },
-  { "minute",	T_MINUTE_UNIT,	1 },
-  { "min",	T_MINUTE_UNIT,	1 },
-  { "second",	T_SEC_UNIT,	1 },
-  { "sec",	T_SEC_UNIT,	1 },
-  { NULL, 0, 0 }
-};
-
-/* Assorted relative-time words. */
-static SYMBOL const other_tab[] = {
-  { "tomorrow",	T_MINUTE_UNIT,	1 * 24 * 60 },
-  { "yesterday",T_MINUTE_UNIT,	-1 * 24 * 60 },
-  { "today",	T_MINUTE_UNIT,	0 },
-  { "now",	T_MINUTE_UNIT,	0 },
-  { "last",	T_UNUMBER,	-1 },
-  { "this",	T_MINUTE_UNIT,	0 },
-  { "next",	T_UNUMBER,	1 },
-  { "first",	T_UNUMBER,	1 },
-/*  { "second",	T_UNUMBER,	2 }, */
-  { "third",	T_UNUMBER,	3 },
-  { "fourth",	T_UNUMBER,	4 },
-  { "fifth",	T_UNUMBER,	5 },
-  { "sixth",	T_UNUMBER,	6 },
-  { "seventh",	T_UNUMBER,	7 },
-  { "eighth",	T_UNUMBER,	8 },
-  { "ninth",	T_UNUMBER,	9 },
-  { "tenth",	T_UNUMBER,	10 },
-  { "eleventh",	T_UNUMBER,	11 },
-  { "twelfth",	T_UNUMBER,	12 },
-  { "ago",	T_AGO,	        1 },
-  { NULL, 0, 0 }
-};
-
-/* The timezone table. */
-static SYMBOL const tz_tab[] = {
-  { "gmt",	T_ZONE,     HOUR ( 0) },	/* Greenwich Mean */
-  { "ut",	T_ZONE,     HOUR ( 0) },	/* Universal (Coordinated) */
-  { "utc",	T_ZONE,     HOUR ( 0) },
-  { "wet",	T_ZONE,     HOUR ( 0) },	/* Western European */
-  { "bst",	T_DAYZONE,  HOUR ( 0) },	/* British Summer */
-  { "wat",	T_ZONE,     HOUR ( 1) },	/* West Africa */
-  { "at",	T_ZONE,     HOUR ( 2) },	/* Azores */
-#if	0
-  /* For completeness.  BST is also British Summer, and GST is
-   * also Guam Standard. */
-  { "bst",	T_ZONE,     HOUR ( 3) },	/* Brazil Standard */
-  { "gst",	T_ZONE,     HOUR ( 3) },	/* Greenland Standard */
-#endif
-#if 0
-  { "nft",	T_ZONE,     HOUR (3.5) },	/* Newfoundland */
-  { "nst",	T_ZONE,     HOUR (3.5) },	/* Newfoundland Standard */
-  { "ndt",	T_DAYZONE,  HOUR (3.5) },	/* Newfoundland Daylight */
-#endif
-  { "ast",	T_ZONE,     HOUR ( 4) },	/* Atlantic Standard */
-  { "adt",	T_DAYZONE,  HOUR ( 4) },	/* Atlantic Daylight */
-  { "est",	T_ZONE,     HOUR ( 5) },	/* Eastern Standard */
-  { "edt",	T_DAYZONE,  HOUR ( 5) },	/* Eastern Daylight */
-  { "cst",	T_ZONE,     HOUR ( 6) },	/* Central Standard */
-  { "cdt",	T_DAYZONE,  HOUR ( 6) },	/* Central Daylight */
-  { "mst",	T_ZONE,     HOUR ( 7) },	/* Mountain Standard */
-  { "mdt",	T_DAYZONE,  HOUR ( 7) },	/* Mountain Daylight */
-  { "pst",	T_ZONE,     HOUR ( 8) },	/* Pacific Standard */
-  { "pdt",	T_DAYZONE,  HOUR ( 8) },	/* Pacific Daylight */
-  { "yst",	T_ZONE,     HOUR ( 9) },	/* Yukon Standard */
-  { "ydt",	T_DAYZONE,  HOUR ( 9) },	/* Yukon Daylight */
-  { "hst",	T_ZONE,     HOUR (10) },	/* Hawaii Standard */
-  { "hdt",	T_DAYZONE,  HOUR (10) },	/* Hawaii Daylight */
-  { "cat",	T_ZONE,     HOUR (10) },	/* Central Alaska */
-  { "ahst",	T_ZONE,     HOUR (10) },	/* Alaska-Hawaii Standard */
-  { "nt",	T_ZONE,     HOUR (11) },	/* Nome */
-  { "idlw",	T_ZONE,     HOUR (12) },	/* International Date Line West */
-  { "cet",	T_ZONE,     -HOUR (1) },	/* Central European */
-  { "met",	T_ZONE,     -HOUR (1) },	/* Middle European */
-  { "mewt",	T_ZONE,     -HOUR (1) },	/* Middle European Winter */
-  { "mest",	T_DAYZONE,  -HOUR (1) },	/* Middle European Summer */
-  { "mesz",	T_DAYZONE,  -HOUR (1) },	/* Middle European Summer */
-  { "swt",	T_ZONE,     -HOUR (1) },	/* Swedish Winter */
-  { "sst",	T_DAYZONE,  -HOUR (1) },	/* Swedish Summer */
-  { "fwt",	T_ZONE,     -HOUR (1) },	/* French Winter */
-  { "fst",	T_DAYZONE,  -HOUR (1) },	/* French Summer */
-  { "eet",	T_ZONE,     -HOUR (2) },	/* Eastern Europe, USSR Zone 1 */
-  { "bt",	T_ZONE,     -HOUR (3) },	/* Baghdad, USSR Zone 2 */
-#if 0
-  { "it",	T_ZONE,     -HOUR (3.5) },/* Iran */
-#endif
-  { "zp4",	T_ZONE,     -HOUR (4) },	/* USSR Zone 3 */
-  { "zp5",	T_ZONE,     -HOUR (5) },	/* USSR Zone 4 */
-#if 0
-  { "ist",	T_ZONE,     -HOUR (5.5) },/* Indian Standard */
-#endif
-  { "zp6",	T_ZONE,     -HOUR (6) },	/* USSR Zone 5 */
-#if	0
-  /* For completeness.  NST is also Newfoundland Standard, and SST is
-   * also Swedish Summer. */
-  { "nst",	T_ZONE,     -HOUR (6.5) },/* North Sumatra */
-  { "sst",	T_ZONE,     -HOUR (7) },	/* South Sumatra, USSR Zone 6 */
-#endif	/* 0 */
-  { "wast",	T_ZONE,     -HOUR (7) },	/* West Australian Standard */
-  { "wadt",	T_DAYZONE,  -HOUR (7) },	/* West Australian Daylight */
-#if 0
-  { "jt",	T_ZONE,     -HOUR (7.5) },/* Java (3pm in Cronusland!) */
-#endif
-  { "cct",	T_ZONE,     -HOUR (8) },	/* China Coast, USSR Zone 7 */
-  { "jst",	T_ZONE,     -HOUR (9) },	/* Japan Standard, USSR Zone 8 */
-#if 0
-  { "cast",	T_ZONE,     -HOUR (9.5) },/* Central Australian Standard */
-  { "cadt",	T_DAYZONE,  -HOUR (9.5) },/* Central Australian Daylight */
-#endif
-  { "east",	T_ZONE,     -HOUR (10) },	/* Eastern Australian Standard */
-  { "eadt",	T_DAYZONE,  -HOUR (10) },	/* Eastern Australian Daylight */
-  { "gst",	T_ZONE,     -HOUR (10) },	/* Guam Standard, USSR Zone 9 */
-  { "nzt",	T_ZONE,     -HOUR (12) },	/* New Zealand */
-  { "nzst",	T_ZONE,     -HOUR (12) },	/* New Zealand Standard */
-  { "nzdt",	T_DAYZONE,  -HOUR (12) },	/* New Zealand Daylight */
-  { "idle",	T_ZONE,     -HOUR (12) },	/* International Date Line
-						   East */
-  {  NULL, 0, 0  }
-};
-
-/* Military timezone table. */
-static SYMBOL const mil_tz_tab[] = {
-  { "a",	T_ZONE,	HOUR (  1) },
-  { "b",	T_ZONE,	HOUR (  2) },
-  { "c",	T_ZONE,	HOUR (  3) },
-  { "d",	T_ZONE,	HOUR (  4) },
-  { "e",	T_ZONE,	HOUR (  5) },
-  { "f",	T_ZONE,	HOUR (  6) },
-  { "g",	T_ZONE,	HOUR (  7) },
-  { "h",	T_ZONE,	HOUR (  8) },
-  { "i",	T_ZONE,	HOUR (  9) },
-  { "k",	T_ZONE,	HOUR ( 10) },
-  { "l",	T_ZONE,	HOUR ( 11) },
-  { "m",	T_ZONE,	HOUR ( 12) },
-  { "n",	T_ZONE,	HOUR (- 1) },
-  { "o",	T_ZONE,	HOUR (- 2) },
-  { "p",	T_ZONE,	HOUR (- 3) },
-  { "q",	T_ZONE,	HOUR (- 4) },
-  { "r",	T_ZONE,	HOUR (- 5) },
-  { "s",	T_ZONE,	HOUR (- 6) },
-  { "t",	T_ZONE,	HOUR (- 7) },
-  { "u",	T_ZONE,	HOUR (- 8) },
-  { "v",	T_ZONE,	HOUR (- 9) },
-  { "w",	T_ZONE,	HOUR (-10) },
-  { "x",	T_ZONE,	HOUR (-11) },
-  { "y",	T_ZONE,	HOUR (-12) },
-  { "z",	T_ZONE,	HOUR (  0) },
-  { NULL, 0, 0 }
-};
-
-
-
-
-/* ARGSUSED */
-static int
-yyerror (char *s MU_ARG_UNUSED)
-{
-  return 0;
-}
-
-static int
-norm_hour (int hours, MERIDIAN meridian)
-{
-  switch (meridian)
-    {
-    case MER24:
-      if (hours < 0 || hours > 23)
-	return -1;
-      return hours;
-      
-    case MERam:
-      if (hours < 1 || hours > 12)
-	return -1;
-      if (hours == 12)
-	hours = 0;
-      return hours;
-      
-    case MERpm:
-      if (hours < 1 || hours > 12)
-	return -1;
-      if (hours == 12)
-	hours = 0;
-      return hours + 12;
-      
-    default:
-      abort ();
-    }
-  /* NOTREACHED */
-}
-
-static int
-norm_year (int year)
-{
-  if (year < 0)
-    year = -year;
-  
-  /* XPG4 suggests that years 00-68 map to 2000-2068, and
-     years 69-99 map to 1969-1999.  */
-  if (year < 69)
-    year += 2000;
-  else if (year < 100)
-    year += 1900;
-
-  return year;
-}
-
-static int
-sym_lookup (char *buff)
-{
-  register char *p;
-  register char *q;
-  register const SYMBOL *tp;
-  int i;
-  int abbrev;
-  
-  /* Make it lowercase. */
-  for (p = buff; *p; p++)
-    if (ISUPPER ((unsigned char) *p))
-      *p = tolower (*p);
-  
-  if (strcmp (buff, "am") == 0 || strcmp (buff, "a.m.") == 0)
-    {
-      yylval.meridian = MERam;
-      return T_MERIDIAN;
-    }
-  if (strcmp (buff, "pm") == 0 || strcmp (buff, "p.m.") == 0)
-    {
-      yylval.meridian = MERpm;
-      return T_MERIDIAN;
-    }
-  
-  /* See if we have an abbreviation for a month. */
-  if (strlen (buff) == 3)
-    abbrev = 1;
-  else if (strlen (buff) == 4 && buff[3] == '.')
-    {
-      abbrev = 1;
-      buff[3] = '\0';
-    }
-  else
-    abbrev = 0;
-
-  for (tp = month_day_tab; tp->name; tp++)
-    {
-      if (abbrev)
-	{
-	  if (strncmp (buff, tp->name, 3) == 0)
-	    {
-	      yylval.number = tp->value;
-	      return tp->type;
-	    }
-	}
-      else if (strcmp (buff, tp->name) == 0)
-	{
-	  yylval.number = tp->value;
-	  return tp->type;
-	}
-    }
-
-  for (tp = tz_tab; tp->name; tp++)
-    if (strcmp (buff, tp->name) == 0)
-      {
-	yylval.number = tp->value;
-	return tp->type;
-      }
-
-  if (strcmp (buff, "dst") == 0)
-    return T_DST;
-
-  for (tp = units_tab; tp->name; tp++)
-    if (strcmp (buff, tp->name) == 0)
-      {
-	yylval.number = tp->value;
-	return tp->type;
-      }
-
-  /* Strip off any plural and try the units table again. */
-  i = strlen (buff) - 1;
-  if (buff[i] == 's')
-    {
-      buff[i] = '\0';
-      for (tp = units_tab; tp->name; tp++)
-	if (strcmp (buff, tp->name) == 0)
-	  {
-	    yylval.number = tp->value;
-	    return tp->type;
-	  }
-      buff[i] = 's';		/* Put back for "this" in other_tab. */
-    }
-
-  for (tp = other_tab; tp->name; tp++)
-    if (strcmp (buff, tp->name) == 0)
-      {
-	yylval.number = tp->value;
-	return tp->type;
-      }
-
-  /* Military timezones. */
-  if (buff[1] == '\0' && ISALPHA ((unsigned char) *buff))
-    {
-      for (tp = mil_tz_tab; tp->name; tp++)
-	if (strcmp (buff, tp->name) == 0)
-	  {
-	    yylval.number = tp->value;
-	    return tp->type;
-	  }
-    }
-
-  /* Drop out any periods and try the timezone table again. */
-  for (i = 0, p = q = buff; *q; q++)
-    if (*q != '.')
-      *p++ = *q;
-    else
-      i++;
-  *p = '\0';
-  if (i)
-    for (tp = tz_tab; tp->name; tp++)
-      if (strcmp (buff, tp->name) == 0)
-	{
-	  yylval.number = tp->value;
-	  return tp->type;
-	}
-
-  return T_ID;
-}
-
-static int
-yylex ()
-{
-  register unsigned char c;
-  register char *p;
-  char buff[20];
-  int count;
-  int sign;
-
-  for (;;)
-    {
-      while (ISSPACE ((unsigned char) *yyinput))
-	yyinput++;
-
-      if (ISDIGIT (c = *yyinput) || c == '-' || c == '+')
-	{
-	  if (c == '-' || c == '+')
-	    {
-	      sign = c == '-' ? -1 : 1;
-	      if (!ISDIGIT (*++yyinput))
-		/* skip the '-' sign */
-		continue;
-	    }
-	  else
-	    sign = 0;
-	  for (yylval.number = 0; ISDIGIT (c = *yyinput++);)
-	    yylval.number = 10 * yylval.number + c - '0';
-	  yyinput--;
-	  if (sign < 0)
-	    yylval.number = -yylval.number;
-	  return sign ? T_SNUMBER : T_UNUMBER;
-	}
-      if (ISALPHA (c))
-	{
-	  for (p = buff; (c = *yyinput++, ISALPHA (c)) || c == '.';)
-	    if (p < &buff[sizeof buff - 1])
-	      *p++ = c;
-	  *p = '\0';
-	  yyinput--;
-	  return sym_lookup (buff);
-	}
-      if (c != '(')
-	return *yyinput++;
-      count = 0;
-      do
-	{
-	  c = *yyinput++;
-	  if (c == '\0')
-	    return c;
-	  if (c == '(')
-	    count++;
-	  else if (c == ')')
-	    count--;
-	}
-      while (count > 0);
-    }
-}
-
-#define TM_YEAR_ORIGIN 1900
-
-/* Yield A - B, measured in seconds.  */
-static long
-difftm (struct tm *a, struct tm *b)
-{
-  int ay = a->tm_year + (TM_YEAR_ORIGIN - 1);
-  int by = b->tm_year + (TM_YEAR_ORIGIN - 1);
-  long days = (
-  /* difference in day of year */
-		a->tm_yday - b->tm_yday
-  /* + intervening leap days */
-		+ ((ay >> 2) - (by >> 2))
-		- (ay / 100 - by / 100)
-		+ ((ay / 100 >> 2) - (by / 100 >> 2))
-  /* + difference in years * 365 */
-		+ (long) (ay - by) * 365
-  );
-  return (60 * (60 * (24 * days + (a->tm_hour - b->tm_hour))
-		+ (a->tm_min - b->tm_min))
-	  + (a->tm_sec - b->tm_sec));
-}
-
-int
-mu_parse_date (const char *p, time_t *rettime, const time_t *now)
-{
-  struct tm tm, tm0, *tmp;
-  time_t start;
-
-  yyinput = p;
-  start = now ? *now : time ((time_t *) NULL);
-  tmp = localtime (&start);
-  if (!tmp)
-    return -1;
-
-  memset (&tm, 0, sizeof tm);
-  tm.tm_isdst = tmp->tm_isdst;
-
-  if (yyparse ())
-    return -1;
-  
-  if (!MASK_IS_SET (pd.date.mask, PD_MASK_YEAR))
-    __SET_YEAR (pd.date, tmp->tm_year + TM_YEAR_ORIGIN, return -1);
-  if (!MASK_IS_SET (pd.date.mask, PD_MASK_MONTH))
-    __SET_MONTH (pd.date, tmp->tm_mon + 1, return -1);
-  if (!MASK_IS_SET (pd.date.mask, PD_MASK_DAY))
-    __SET_DAY (pd.date, tmp->tm_mday, return -1);
-  if (!MASK_IS_SET (pd.date.mask, PD_MASK_HOUR))
-    __SET_HOUR (pd.date, tmp->tm_hour, return -1);
-  if (!MASK_IS_SET (pd.date.mask, PD_MASK_MERIDIAN))
-    __SET_MERIDIAN (pd.date, MER24, return -1);
-  if (!MASK_IS_SET (pd.date.mask, PD_MASK_MINUTE))
-    __SET_MINUTE (pd.date, tmp->tm_min, return -1);
-  if (!MASK_IS_SET (pd.date.mask, PD_MASK_SECOND))
-    __SET_SECOND (pd.date, tmp->tm_sec, return -1);
-  
-  tm.tm_year = norm_year (pd.date.year) - TM_YEAR_ORIGIN + pd.rel.year;
-  tm.tm_mon = pd.date.month - 1 + pd.rel.month;
-  tm.tm_mday = pd.date.day + pd.rel.day;
-  if (MASK_TEST (pd.date.mask, PD_MASK_TIME)
-      || (pd.rel.mask && !MASK_TEST (pd.date.mask, PD_MASK_DATE)
-	  && !MASK_TEST (pd.date.mask, PD_MASK_DOW)))
-    {
-      tm.tm_hour = norm_hour (pd.date.hour, pd.date.meridian);
-      if (tm.tm_hour < 0)
-	return -1;
-      tm.tm_min = pd.date.minute;
-      tm.tm_sec = pd.date.second;
-    }
-  else
-    {
-      tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
-    }
-  tm.tm_hour += pd.rel.hour;
-  tm.tm_min += pd.rel.minute;
-  tm.tm_sec += pd.rel.second;
-
-  /* Let mktime deduce tm_isdst if we have an absolute timestamp,
-     or if the relative timestamp mentions days, months, or years.  */
-  if (MASK_TEST (pd.date.mask, PD_MASK_DATE | PD_MASK_DOW | PD_MASK_TIME)
-      || MASK_TEST (pd.rel.mask, PD_MASK_DOW | PD_MASK_MONTH | PD_MASK_YEAR))
-    tm.tm_isdst = -1;
-
-  tm0 = tm;
-
-  start = mktime (&tm);
-
-  if (start == (time_t) -1)
-    {
-
-      /* Guard against falsely reporting errors near the time_t boundaries
-         when parsing times in other time zones.  For example, if the min
-         time_t value is 1970-01-01 00:00:00 UTC and we are 8 hours ahead
-         of UTC, then the min localtime value is 1970-01-01 08:00:00; if
-         we apply mktime to 1970-01-01 00:00:00 we will get an error, so
-         we apply mktime to 1970-01-02 08:00:00 instead and adjust the time
-         zone by 24 hours to compensate.  This algorithm assumes that
-         there is no DST transition within a day of the time_t boundaries.  */
-      if (MASK_TEST (pd.date.mask, PD_MASK_TZ))
-	{
-	  tm = tm0;
-	  if (tm.tm_year <= EPOCH - TM_YEAR_ORIGIN)
-	    {
-	      tm.tm_mday++;
-	      pd.date.tz -= 24 * 60;
-	    }
-	  else
-	    {
-	      tm.tm_mday--;
-	      pd.date.tz += 24 * 60;
-	    }
-	  start = mktime (&tm);
-	}
-
-      if (start == (time_t) -1)
-	return -1;
-    }
-
-  if (MASK_TEST (pd.date.mask, PD_MASK_DOW)
-      && !MASK_TEST (pd.date.mask, PD_MASK_DATE))
-    {
-      tm.tm_mday += ((pd.date.number - tm.tm_wday + 7) % 7
-		     + 7 * (pd.date.ordinal - (0 < pd.date.ordinal)));
-      start = mktime (&tm);
-      if (start == (time_t) -1)
-	return -1;
-    }
-  
-  if (MASK_TEST (pd.date.mask, PD_MASK_TZ))
-    {
-      long delta;
-      struct tm *gmt = gmtime (&start);
-      if (gmt)
-	{
-	  delta = pd.date.tz * 60L + difftm (&tm, gmt);
-	  if ((start + delta < start) != (delta < 0))
-	    return -1;		/* time_t overflow */
-	  start += delta;
-	}
-    }
-  
-  *rettime = start;
-  return 0;
-}
-
-#ifdef STANDALONE
-int
-main (int argc, char *argv[])
-{
-  char buff[MAX_BUFF_LEN + 1];
-  time_t d;
-
-  if (argc > 1 && strcmp (argv[1], "-d") == 0)
-    yydebug++;
-  printf ("Enter date, or blank line to exit.\n\t> ");
-  fflush (stdout);
-
-  buff[MAX_BUFF_LEN] = 0;
-  while (fgets (buff, MAX_BUFF_LEN, stdin) && buff[0])
-    {
-      if (mu_parse_date (buff, &d, NULL))
-	printf ("Bad format - couldn't convert.\n");
-      else
-	printf ("%s", ctime (&d));
-      printf ("\t> ");
-      fflush (stdout);
-    }
-  exit (0);
-  /* NOTREACHED */
-}
-
-#endif
diff --git a/libmailutils/permstr.c b/libmailutils/permstr.c
deleted file mode 100644
index d3d184a..0000000
--- a/libmailutils/permstr.c
+++ /dev/null
@@ -1,128 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 2008, 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 */
-
-#if HAVE_CONFIG_H
-# include <config.h>
-#endif
-#include <mailutils/stream.h>
-#include <mailutils/mutil.h>
-#include <mailutils/errno.h>
-
-#define FILE_PERM_READ 0x1
-#define FILE_PERM_WRITE 0x2
-
-static int
-parse_perm_bits (int *pmode, const char *str, const char **endp)
-{
-  switch (*str)
-    {
-    case '+':
-    case '=':
-      str++;
-      break;
-
-    default:
-      if (endp)
-	*endp = str;
-      return 1;
-    }
-
-  for (; *str; str++)
-    {
-      switch (*str)
-	{
-	case 'r':
-	  *pmode |= FILE_PERM_READ;
-	  break;
-
-	case 'w':
-	  *pmode |= FILE_PERM_WRITE;
-	  break;
-
-	case ',':
-	  if (endp)
-	    *endp = str;
-	  return 0;
-	  
-	default:
-	  if (endp)
-	    *endp = str;
-	  return 1;
-	}
-    }
-  if (endp)
-    *endp = str;
-  return 0;
-}
-
-/* Parse a MU stream permission specification in form:
-
-      g(+|=)[wr]+,o(+|=)[wr]+
-
-   Return 0 on success.
-   On failure, return MU_ERR_FAILURE and point endp to the offending character.
-*/
-int
-mu_parse_stream_perm_string (int *pmode, const char *str, const char **endp)
-{
-  int mode = 0;
-  int f;
-  while (*str)
-    {
-      switch (*str)
-	{
-	case 'g':
-	  if (parse_perm_bits (&f, str + 1, &str))
-	    {
-	      if (endp)
-		*endp = str;
-	      return MU_ERR_FAILURE;
-	    }
-	  if (f & FILE_PERM_READ)
-	    mode |= MU_STREAM_IRGRP;
-	  if (f & FILE_PERM_WRITE)
-	    mode |= MU_STREAM_IWGRP;
-	  break;
-	  
-	case 'o':
-	  if (parse_perm_bits (&f, str + 1, &str))
-	    {
-	      if (endp)
-		*endp = str;
-	      return MU_ERR_FAILURE;
-	    }
-	  if (f & FILE_PERM_READ)
-	    mode |= MU_STREAM_IROTH;
-	  if (f & FILE_PERM_WRITE)
-	    mode |= MU_STREAM_IWOTH;
-	  break;
-	  
-	default:
-	  if (endp)
-	    *endp = str;
-	  return MU_ERR_FAILURE;
-	}
-      if (*str == ',')
-	str++;
-    }
-  *pmode = mode;
-  if (endp)
-    *endp = str;
-  return 0;
-}
-      
diff --git a/libmailutils/progmailer.c b/libmailutils/progmailer.c
deleted file mode 100644
index f39440f..0000000
--- a/libmailutils/progmailer.c
+++ /dev/null
@@ -1,314 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 1999, 2000, 2001, 2004, 2005, 2006, 2007, 2008, 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 <errno.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-#include <signal.h>
-#include <sysexits.h>
-
-#include <sys/types.h>
-#include <sys/wait.h>
-
-#include <mailutils/debug.h>
-#include <mailutils/error.h>
-#include <mailutils/errno.h>
-#include <mailutils/stream.h>
-#include <mailutils/header.h>
-#include <mailutils/body.h>
-#include <mailutils/message.h>
-#include <mailutils/progmailer.h>
-#include <mailutils/mutil.h>
-#include <mailutils/cstr.h>
-
-struct _mu_progmailer
-{
-  int fd;
-  pid_t pid;
-  RETSIGTYPE (*sighandler)();
-  mu_debug_t debug;
-  char *command;
-};
-
-/* Close FD unless it is part of pipe P */
-#define SCLOSE(fd,p) if (p[0]!=fd&&p[1]!=fd) close(fd)
-
-int
-mu_progmailer_create (struct _mu_progmailer **ppm)
-{
-  struct _mu_progmailer *pm = malloc (sizeof (*pm));
-  if (!pm)
-    return ENOMEM;
-  pm->fd = -1;
-  pm->pid = -1;
-  pm->sighandler = SIG_ERR;
-  pm->debug = NULL;
-  pm->command = NULL;
-  *ppm = pm;
-  return 0;
-}
-
-int
-mu_progmailer_set_command (mu_progmailer_t pm, const char *command)
-{
-  if (!pm)
-    return EINVAL;
-  free (pm->command);
-  if (command)
-    {
-      pm->command = strdup (command);
-      if (!pm->command)
-	return EINVAL;
-    }
-  else
-    pm->command = NULL;
-  return 0;
-}
-
-int
-mu_progmailer_sget_command (mu_progmailer_t pm, const char **command)
-{
-  if (!pm)
-    return EINVAL;
-  *command = pm->command;
-  return 0;
-}
-
-int
-mu_progmailer_set_debug (mu_progmailer_t pm, mu_debug_t debug)
-{
-  if (!pm)
-    return EINVAL;
-  pm->debug = debug;
-  return 0;
-}
-
-void
-mu_progmailer_destroy (struct _mu_progmailer **ppm)
-{
-  if (*ppm)
-    {
-      free ((*ppm)->command);
-      free (*ppm);
-      *ppm = NULL;
-    }
-}
-
-int
-mu_progmailer_open (struct _mu_progmailer *pm, char **argv)
-{
-  int tunnel[2];
-  int status = 0;
-  int i;
-
-  if (!pm || !argv)
-    return EINVAL;
-  
-  if ((pm->sighandler = signal (SIGCHLD, SIG_DFL)) == SIG_ERR)
-    {
-      status = errno;
-      MU_DEBUG1 (pm->debug, MU_DEBUG_ERROR,
-		 "setting SIGCHLD failed: %s\n", mu_strerror (status));
-      return status;
-    }
-      
-  if (pipe (tunnel) == 0)
-    {
-      pm->fd = tunnel[1];
-      pm->pid = fork ();
-      if (pm->pid == 0)	/* Child.  */
-	{
-	  SCLOSE (STDIN_FILENO, tunnel);
-	  SCLOSE (STDOUT_FILENO, tunnel);
-	  SCLOSE (STDERR_FILENO, tunnel);
-	  close (tunnel[1]);
-	  dup2 (tunnel[0], STDIN_FILENO);
-	  execv (pm->command ? pm->command : argv[0], argv);
-	  exit (errno ? EX_CANTCREAT : 0);
-	}
-      else if (pm->pid == -1)
-	{
-	  status = errno;
-	  MU_DEBUG1 (pm->debug, MU_DEBUG_ERROR,
-		     "fork failed: %s\n", mu_strerror (status));
-	}
-    }
-  else
-    {
-      status = errno;
-      MU_DEBUG1 (pm->debug, MU_DEBUG_ERROR,
-		 "pipe() failed: %s\n", mu_strerror (status));
-    }
-  MU_DEBUG1 (pm->debug, MU_DEBUG_TRACE, "exec %s argv:", pm->command);
-  for (i = 0; argv[i]; i++)
-    MU_DEBUG1 (pm->debug, MU_DEBUG_TRACE, " %s", argv[i]);
-  MU_DEBUG (pm->debug, MU_DEBUG_TRACE, "\n");
-  close (tunnel[0]);
-
-  if (status != 0)
-    close (pm->fd);
-  return status;
-}
-
-int
-mu_progmailer_send (struct _mu_progmailer *pm, mu_message_t msg)
-{
-  int status;
-  mu_stream_t stream = NULL;
-  char buffer[512];
-  size_t len = 0;
-  int rc;
-  mu_header_t hdr;
-  mu_body_t body;
-  int found_nl = 0;
-  int exit_status;
-	
-  if (!pm || !msg)
-    return EINVAL;
-  mu_message_get_header (msg, &hdr);
-  status = mu_header_get_streamref (hdr, &stream);
-  if (status)
-    {
-      MU_DEBUG1 (pm->debug, MU_DEBUG_ERROR,
-		 "cannot get header stream: %s\n", mu_strerror (status));
-      return status;
-    }
-  MU_DEBUG (pm->debug, MU_DEBUG_TRACE, "Sending headers...\n");
-  mu_stream_seek (stream, 0, MU_SEEK_SET, NULL);
-  while ((status = mu_stream_readline (stream, buffer, sizeof (buffer),
-				       &len)) == 0
-	 && len != 0)
-    {
-      if (mu_c_strncasecmp (buffer, MU_HEADER_FCC, sizeof (MU_HEADER_FCC) - 1))
-	{
-	  MU_DEBUG1 (pm->debug, MU_DEBUG_PROT, "Header: %s", buffer);
-	  if (write (pm->fd, buffer, len) == -1)
-	    {
-	      status = errno;
-	      
-	      MU_DEBUG1 (pm->debug, MU_DEBUG_TRACE,
-			 "write failed: %s\n", strerror (status));
-	      break;
-	    }
-	}
-      found_nl = (len == 1 && buffer[0] == '\n');
-    }
-
-  if (!found_nl)
-    {
-      if (write (pm->fd, "\n", 1) == -1)
-	{
-	  status = errno;
-		
-	  MU_DEBUG1 (pm->debug, MU_DEBUG_TRACE,
-		     "write failed: %s\n", strerror (status));
-	}
-    }
-  mu_stream_destroy (&stream);
-  
-  MU_DEBUG (pm->debug, MU_DEBUG_TRACE, "Sending body...\n");
-  mu_message_get_body (msg, &body);
-  status = mu_body_get_streamref (body, &stream);
-  if (status)
-    {
-      MU_DEBUG1 (pm->debug, MU_DEBUG_ERROR,
-		 "cannot get body stream: %s\n", mu_strerror (status));
-      return status;
-    }
-
-  mu_stream_seek (stream, 0, MU_SEEK_SET, NULL);
-  while ((status = mu_stream_read (stream, buffer, sizeof (buffer),
-				   &len)) == 0
-	 && len != 0)
-    {
-      if (write (pm->fd, buffer, len) == -1)
-	{
-	  status = errno;
-	  
-	  MU_DEBUG1 (pm->debug, MU_DEBUG_TRACE,
-		     "write failed: %s\n", strerror (status));
-	  break;
-	}
-    }
-  mu_body_get_streamref (body, &stream);
-
-  close (pm->fd);
-
-  rc = waitpid (pm->pid, &exit_status, 0);
-  if (status == 0)
-    {
-      if (rc < 0)
-	{
-	  if (errno == ECHILD)
-	    status = 0;
-	  else
-	    { 
-	      status = errno;
-	      MU_DEBUG2 (pm->debug, MU_DEBUG_TRACE,
-			 "waitpid(%lu) failed: %s\n",
-			 (unsigned long) pm->pid, strerror (status));
-	    }
-	}
-      else if (WIFEXITED (exit_status))
-	{
-	  exit_status = WEXITSTATUS (exit_status);
-	  MU_DEBUG2 (pm->debug, MU_DEBUG_TRACE,
-		     "%s exited with: %d\n",
-		     pm->command, exit_status);
-	  status = (exit_status == 0) ? 0 : MU_ERR_PROCESS_EXITED;
-	}
-      else if (WIFSIGNALED (exit_status))
-	status = MU_ERR_PROCESS_SIGNALED;
-      else
-	status = MU_ERR_PROCESS_UNKNOWN_FAILURE;
-    }
-  pm->pid = -1;
-  return status;
-}
-
-int
-mu_progmailer_close (struct _mu_progmailer *pm)
-{
-  int status = 0;
-
-  if (!pm)
-    return EINVAL;
-  
-  if (pm->pid > 0)
-    {
-      kill (SIGTERM, pm->pid);
-      pm->pid = -1;
-    }
-
-  if (pm->sighandler != SIG_ERR
-      && signal (SIGCHLD, pm->sighandler) == SIG_ERR)
-    {
-      status = errno;
-      MU_DEBUG1 (pm->debug, MU_DEBUG_ERROR,
-		 "resetting SIGCHLD failed: %s\n", mu_strerror (status));
-    }
-  pm->sighandler = SIG_ERR;
-  return status;
-}
diff --git a/libmailutils/property.c b/libmailutils/property.c
deleted file mode 100644
index 435d8e2..0000000
--- a/libmailutils/property.c
+++ /dev/null
@@ -1,192 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 1999, 2000, 2001, 2004, 2005, 2007, 2008, 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
-#ifdef HAVE_STRINGS_H
-# include <strings.h>
-#endif
-
-#include <mailutils/sys/property.h>
-#include <mailutils/errno.h>
-#include <mailutils/assoc.h>
-#include <stdlib.h>
-
-static void
-prop_free_value (void *data)
-{
-  struct property_item *item = data;
-  free (item->value);
-}
-
-int
-mu_property_create (mu_property_t *pp, void *owner)
-{
-  int rc;
-  mu_property_t prop;
-  if (pp == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-  prop = calloc (1, sizeof *prop);
-  if (prop == NULL)
-    return ENOMEM;
-  rc = mu_assoc_create (&prop->assoc, sizeof (struct property_item), 0);
-  if (rc)
-    {
-      free (prop);
-      return rc;
-    }
-  mu_assoc_set_free (prop->assoc, prop_free_value);
-  mu_monitor_create (&prop->lock, 0, prop);
-  prop->owner = owner;
-  *pp = prop;
-  return 0;
-}
-
-void
-mu_property_destroy (mu_property_t *pp, void *owner)
-{
-  if (pp && *pp)
-    {
-      mu_property_t prop = *pp;
-      if (prop->owner == owner)
-	{
-	  /* Destroy the list and is properties.  */
-	  mu_monitor_wrlock (prop->lock);
-	  mu_assoc_destroy (&prop->assoc);
-	  mu_monitor_unlock (prop->lock);
-	  mu_monitor_destroy (&prop->lock, prop);
-	  free (prop);
-	}
-      *pp = NULL;
-    }
-}
-
-void *
-mu_property_get_owner (mu_property_t prop)
-{
-  return (prop == NULL) ? NULL : prop->owner;
-}
-
-int
-mu_property_set_value (mu_property_t prop, const char *key, const char *value,
-		       int overwrite)
-{
-  struct property_item *item;
-  int rc;
-
-  if (!prop)
-    return EINVAL;
-  rc = mu_assoc_ref_install (prop->assoc, key, (void **)&item);
-  if (rc == MU_ERR_NOENT)
-    {
-      item->value = strdup (value);
-    }
-  else if (overwrite)
-    {
-      free (item->value);
-      item->value = strdup (value);
-    }
-  else
-    return 0;
-  
-  if (!item->value)
-    {
-      mu_assoc_remove (prop->assoc, key);
-      return ENOMEM;
-    }
-
-  return 0;
-}
-  
-int
-mu_property_sget_value (mu_property_t prop, const char *key,
-			const char **buffer)
-{
-  struct property_item *item;
-
-  if (!prop)
-    return EINVAL;
-  item = mu_assoc_ref (prop->assoc, key);
-  if (item == NULL)
-    return MU_ERR_NOENT;
-  if (buffer)
-    *buffer = item->value;
-  return 0;
-}
-
-int
-mu_property_aget_value (mu_property_t prop, const char *key,
-			char **buffer)
-{
-  const char *value;
-  int rc = mu_property_sget_value (prop, key, &value);
-  if (rc == 0)
-    {
-      if ((*buffer = strdup (value)) == NULL)
-	return ENOMEM;
-    }
-  return rc;
-}
-
-int
-mu_property_get_value (mu_property_t prop, const char *key, char *buffer,
-		       size_t buflen, size_t *n)
-{
-  size_t len = 0;
-  const char *value;
-  int rc = mu_property_sget_value (prop, key, &value);
-  if (rc == 0)
-    {
-      len = strlen (value) + 1;
-      if (buffer && buflen)
-	{
-	  if (buflen < len)
-	    len = buflen;
-	  len--;
-	  memcpy (buffer, value, len);
-	  buffer[len] = 0;
-	}
-    }
-  if (n)
-    *n = len;
-  return rc;
-}
-
-int
-mu_property_is_set (mu_property_t prop, const char *key)
-{
-  struct property_item *item = mu_assoc_ref (prop->assoc, key);
-  return (item == NULL) ? 0 : 1;
-}
-
-int
-mu_property_set (mu_property_t prop, const char *key)
-{
-  return mu_property_set_value (prop, key, "", 1);
-}
-
-int
-mu_property_unset (mu_property_t prop, const char *key)
-{
-  if (!prop)
-    return EINVAL;
-  return mu_assoc_remove (prop->assoc, key);
-}
-  
diff --git a/libmailutils/refcount.c b/libmailutils/refcount.c
deleted file mode 100644
index 1be1fc6..0000000
--- a/libmailutils/refcount.c
+++ /dev/null
@@ -1,107 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 1999, 2000, 2001, 2003, 2004, 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA  */
-
-#ifdef HAVE_CONFIG_H
-# include <config.h>
-#endif
-
-#include <stdlib.h>
-#include <errno.h>
-
-#include <mailutils/errno.h>
-#include <mailutils/monitor.h>
-#include <mailutils/refcount.h>
-
-struct _mu_refcount
-{
-  unsigned int ref;
-  mu_monitor_t lock;
-};
-
-int
-mu_refcount_create (mu_refcount_t *prefcount)
-{
-  int status = 0;
-  mu_refcount_t refcount;
-  if (prefcount == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-  refcount = calloc (1, sizeof *refcount);
-  if (refcount != NULL)
-    {
-      refcount->ref = 1;
-      status = mu_monitor_create (&refcount->lock, 0, refcount);
-      if (status == 0)
-	{
-	  *prefcount = refcount;
-	}
-      else
-	{
-	  free (refcount);
-	}
-    }
-  else
-    {
-      status = ENOMEM;
-    }
-  return status;
-}
-
-unsigned
-mu_refcount_value (mu_refcount_t refcount)
-{
-  return refcount ? refcount->ref : 0;
-}
-
-void
-mu_refcount_destroy (mu_refcount_t *prefcount)
-{
-  if (prefcount && *prefcount)
-    {
-      mu_refcount_t refcount = *prefcount;
-      mu_monitor_destroy (&refcount->lock, refcount);
-      free (refcount);
-      *prefcount = NULL;
-    }
-}
-
-unsigned
-mu_refcount_inc (mu_refcount_t refcount)
-{
-  int count = 0;
-  if (refcount)
-    {
-      mu_monitor_wrlock (refcount->lock);
-      count = ++refcount->ref;
-      mu_monitor_unlock (refcount->lock);
-    }
-  return count;
-}
-
-unsigned
-mu_refcount_dec (mu_refcount_t refcount)
-{
-  int count = 0;
-  if (refcount)
-    {
-      mu_monitor_wrlock (refcount->lock);
-      if (refcount->ref)
-	count = --refcount->ref;
-      mu_monitor_unlock (refcount->lock);
-    }
-  return count;
-}
diff --git a/libmailutils/registrar.c b/libmailutils/registrar.c
deleted file mode 100644
index a1a2b4f..0000000
--- a/libmailutils/registrar.c
+++ /dev/null
@@ -1,443 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 1999, 2000, 2001, 2004, 2005, 2006, 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 <stdlib.h>
-#include <string.h>
-#include <errno.h>
-#ifdef HAVE_STRINGS_H
-# include <strings.h>
-#endif
-
-#include <mailutils/iterator.h>
-#include <mailutils/list.h>
-#include <mailutils/monitor.h>
-#include <mailutils/errno.h>
-#include <mailutils/nls.h>
-#include <mailutils/error.h>
-#include <mailutils/url.h>
-#include <mailutils/mutil.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.  */
-static mu_list_t registrar_list;
-struct mu_monitor registrar_monitor = MU_MONITOR_INITIALIZER;
-
-static mu_record_t mu_default_record;
-
-void
-mu_registrar_set_default_record (mu_record_t record)
-{
-  mu_default_record = record;
-}
-
-int
-mu_registrar_get_default_record (mu_record_t *prec)
-{
-  if (mu_default_record)
-    {
-      if (prec)
-	*prec = mu_default_record;
-      return 0;
-    }
-  return MU_ERR_NOENT;
-}
-
-int
-mu_registrar_set_default_scheme (const char *scheme)
-{
-  int status;
-  mu_record_t rec;
-  
-  status = mu_registrar_lookup_scheme (scheme, &rec);
-  if (status == 0)
-    mu_default_record = rec;
-  return status;
-}
-
-const char *
-mu_registrar_get_default_scheme ()
-{
-  return mu_default_record ? mu_default_record->scheme : NULL;
-}
-
-static int
-_registrar_get_list (mu_list_t *plist)
-{
-  int status = 0;
-
-  if (plist == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-  mu_monitor_wrlock (&registrar_monitor);
-  if (registrar_list == NULL)
-    status = mu_list_create (&registrar_list);
-  *plist = registrar_list;
-  mu_monitor_unlock (&registrar_monitor);
-  return status;
-}
-
-/* Provided for backward compatibility */
-int
-mu_registrar_get_list (mu_list_t *plist)
-{
-  static int warned;
-
-  if (!warned)
-    {
-      mu_error (_("program uses mu_registrar_get_list(), which is deprecated"));
-      warned = 1;
-    }
-  return _registrar_get_list (plist);
-}
-
-int
-mu_registrar_get_iterator (mu_iterator_t *pitr)
-{
-  int status = 0;
-  if (pitr == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-  mu_monitor_wrlock (&registrar_monitor);
-  if (registrar_list == NULL)
-    {
-      status = mu_list_create (&registrar_list);
-      if (status)
-	return status;
-    }
-  status = mu_list_get_iterator (registrar_list, pitr);
-  mu_monitor_unlock (&registrar_monitor);
-  return status;
-}
-
-int
-mu_registrar_lookup_scheme (const char *scheme, mu_record_t *precord)
-{
-  size_t len;
-  mu_iterator_t iterator;
-  int status = mu_registrar_get_iterator (&iterator);
-  if (status != 0)
-    return status;
-  status = MU_ERR_NOENT;
-  len = strcspn (scheme, ":");
-  for (mu_iterator_first (iterator); !mu_iterator_is_done (iterator);
-       mu_iterator_next (iterator))
-    {
-      mu_record_t record;
-      mu_iterator_current (iterator, (void **)&record);
-      if (strlen (record->scheme) == len
-	  && memcmp (record->scheme, scheme, len) == 0)
-	{
-	  if (precord)
-	    *precord = record;
-	  status = 0;
-	  break;
-	}
-    }
-  mu_iterator_destroy (&iterator);
-  return status;
-}
-
-int
-mu_registrar_lookup_url (mu_url_t url, int flags,
-			 mu_record_t *precord, int *pflags)
-{
-  mu_iterator_t iterator;
-  mu_record_t last_record = NULL;
-  int last_flags = 0;
-  int status = mu_registrar_get_iterator (&iterator);
-  if (status != 0)
-    return status;
-  status = MU_ERR_NOENT;
-  for (mu_iterator_first (iterator); !mu_iterator_is_done (iterator);
-       mu_iterator_next (iterator))
-    {
-      int rc;
-      mu_record_t record;
-      mu_iterator_current (iterator, (void **)&record);
-      if ((rc = mu_record_is_scheme (record, url, flags)))
-	{
-	  if (rc == flags)
-	    {
-	      status = 0;
-	      last_record = record;
-	      last_flags = rc;
-	      break;
-	    }
-	  else if (rc > last_flags)
-	    {
-	      status = 0;
-	      last_record = record;
-	      last_flags = rc;
-	    }
-	}
-    }
-  mu_iterator_destroy (&iterator);
-
-  if (status == 0)
-    {
-      if (precord)
-	*precord = last_record;
-      if (pflags)
-	*pflags = last_flags;
-    }
-  else if (!mu_is_proto (mu_url_to_string (url)) /* FIXME: This check is not
-						    enough. */
-	   && mu_registrar_get_default_record (precord) == 0)
-    {
-      status = 0;
-      if (pflags)
-	*pflags = flags & MU_FOLDER_ATTRIBUTE_FILE; /* FIXME? */
-    }
-  
-  return status;
-}
-
-int
-mu_registrar_lookup (const char *name, int flags,
-		     mu_record_t *precord, int *pflags)
-{
-  int rc;
-  mu_url_t url;
-  
-  rc = mu_url_create (&url, name);
-  if (rc)
-    return rc;
-  rc = mu_url_parse (url);
-  if (rc == 0)
-    rc = mu_registrar_lookup_url (url, flags, precord, pflags);
-  mu_url_destroy (&url);
-  return rc;
-}
-
-static int
-_compare_prio (const void *item, const void *value)
-{
-  const mu_record_t a = (const mu_record_t) item;
-  const mu_record_t b = (const mu_record_t) value;
-  if (a->priority > b->priority)
-    return 0;
-  return -1;
-}
-
-int
-mu_registrar_record (mu_record_t record)
-{
-  int status;
-  mu_list_t list;
-  mu_list_comparator_t comp;
-  
-  if (!record)
-    return 0;
-  _registrar_get_list (&list);
-  comp = mu_list_set_comparator (list, _compare_prio);
-  status = mu_list_insert (list, record, record, 1);
-  if (status == MU_ERR_NOENT)
-    status = mu_list_append (list, record);
-  mu_list_set_comparator (list, comp);
-  return status;
-}
-
-int
-mu_unregistrar_record (mu_record_t record)
-{
-  mu_list_t list;
-  _registrar_get_list (&list);
-  mu_list_remove (list, record);
-  return 0;
-}
-
-int
-mu_record_is_scheme (mu_record_t record, mu_url_t url, int flags)
-{
-  if (record == NULL)
-    return 0;
-
-  /* Overload.  */
-  if (record->_is_scheme)
-    return record->_is_scheme (record, url, flags);
-
-  if (mu_url_is_scheme (url, record->scheme))
-    return MU_FOLDER_ATTRIBUTE_ALL;
-
-  return 0;
-}
-
-int
-mu_record_set_scheme (mu_record_t record, const char *scheme)
-{
-  if (record == NULL)
-    return EINVAL;
-  record->scheme = scheme;
-  return 0;
-}
-
-int
-mu_record_set_is_scheme (mu_record_t record,
-			 int (*_is_scheme) (mu_record_t, mu_url_t, int))
-{
-  if (record == NULL)
-    return EINVAL;
-  record->_is_scheme = _is_scheme;
-  return 0;
-}
-
-int
-mu_record_get_url (mu_record_t record, int (*(*_purl)) (mu_url_t))
-{
-  if (record == NULL)
-    return EINVAL;
-  if (_purl == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-  /* Overload.  */
-  if (record->_get_url)
-    return record->_get_url (record, _purl);
-  *_purl = record->_url;
-  return 0;
-}
-
-int
-mu_record_set_url (mu_record_t record, int (*_mu_url) (mu_url_t))
-{
-  if (record == NULL)
-    return EINVAL;
-  record->_url = _mu_url;
-  return 0;
-}
-
-int
-mu_record_set_get_url (mu_record_t record, int (*_get_url)
-		    (mu_record_t, int (*(*)) (mu_url_t)))
-{
-  if (record == NULL)
-    return EINVAL;
-  record->_get_url = _get_url;
-  return 0;
-}
-
-int
-mu_record_get_mailbox (mu_record_t record, int (*(*_pmailbox)) (mu_mailbox_t))
-{
-  if (record == NULL)
-    return EINVAL;
-  if (_pmailbox == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-  /* Overload.  */
-  if (record->_get_mailbox)
-    return record->_get_mailbox (record, _pmailbox);
-  *_pmailbox = record->_mailbox;
-  return 0;
-}
-
-int
-mu_record_set_mailbox (mu_record_t record, int (*_mu_mailbox) (mu_mailbox_t))
-{
-  if (record)
-    return EINVAL;
-  record->_mailbox = _mu_mailbox;
-  return 0;
-}
-
-int
-mu_record_set_get_mailbox (mu_record_t record, 
-     int (*_get_mailbox) (mu_record_t, int (*(*)) (mu_mailbox_t)))
-{
-  if (record)
-    return EINVAL;
-  record->_get_mailbox = _get_mailbox;
-  return 0;
-}
-
-int
-mu_record_get_mailer (mu_record_t record, int (*(*_pmailer)) (mu_mailer_t))
-{
-  if (record == NULL)
-    return EINVAL;
-  if (_pmailer == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-  /* Overload.  */
-  if (record->_get_mailer)
-    return record->_get_mailer (record, _pmailer);
-  *_pmailer = record->_mailer;
-  return 0;
-}
-
-int
-mu_record_set_mailer (mu_record_t record, int (*_mu_mailer) (mu_mailer_t))
-{
-  if (record)
-    return EINVAL;
-  record->_mailer = _mu_mailer;
-  return 0;
-}
-
-int
-mu_record_set_get_mailer (mu_record_t record, 
-  int (*_get_mailer) (mu_record_t, int (*(*)) (mu_mailer_t)))
-{
-  if (record == NULL)
-    return EINVAL;
-  record->_get_mailer = _get_mailer;
-  return 0;
-}
-
-int
-mu_record_get_folder (mu_record_t record, int (*(*_pfolder)) (mu_folder_t))
-{
-  if (record == NULL)
-    return EINVAL;
-  if (_pfolder == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-  /* Overload.  */
-  if (record->_get_folder)
-    return record->_get_folder (record, _pfolder);
-  *_pfolder = record->_folder;
-  return 0;
-}
-
-int
-mu_record_set_folder (mu_record_t record, int (*_mu_folder) (mu_folder_t))
-{
-  if (record == NULL)
-    return EINVAL;
-  record->_folder = _mu_folder;
-  return 0;
-}
-
-int
-mu_record_set_get_folder (mu_record_t record, 
-   int (*_get_folder) (mu_record_t, int (*(*)) (mu_folder_t)))
-{
-  if (record == NULL)
-    return EINVAL;
-  record->_get_folder = _get_folder;
-  return 0;
-}
-
-int
-mu_record_list_p (mu_record_t record, const char *name, int flags)
-{
-  if (record == NULL)
-    return EINVAL;
-  return record == NULL
-          || !record->_list_p
-          || record->_list_p (record, name, flags);
-}
diff --git a/libmailutils/rfc2047.c b/libmailutils/rfc2047.c
deleted file mode 100644
index c30ba2f..0000000
--- a/libmailutils/rfc2047.c
+++ /dev/null
@@ -1,314 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 2003, 2004, 2005, 2006, 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 <stdio.h>
-#include <stdlib.h>
-#include <errno.h>
-#include <ctype.h>
-#include <mailutils/stream.h>
-#include <mailutils/filter.h>
-#include <mailutils/errno.h>
-#include <mailutils/mutil.h>
-
-static int
-realloc_buffer (char **bufp, size_t *bufsizep, size_t incr)
-{
-  size_t newsize = *bufsizep + incr;
-  char *newp = realloc (*bufp, newsize);
-  if (newp == NULL)
-    return 1;
-  *bufp = newp;
-  *bufsizep = newsize;
-  return 0;
-}
-
-int
-getword (char **pret, const char **pstr, int delim)
-{
-  size_t len;
-  char *ret;
-  const char *start = *pstr;
-  const char *end = strchr (start, delim);
-
-  free (*pret);
-  *pret = NULL;
-  if (!end)
-    return MU_ERR_BAD_2047_INPUT;
-  len = end - start;
-  ret = malloc (len + 1);
-  if (!ret)
-    return ENOMEM;
-  memcpy (ret, start, len);
-  ret[len] = 0;
-  *pstr = end + 1;
-  *pret = ret;
-  return 0;
-}
-    
-int
-mu_rfc2047_decode (const char *tocode, const char *input, char **ptostr)
-{
-  int status = 0;
-  const char *fromstr;
-  char *buffer;
-  size_t bufsize;
-  size_t bufpos;
-  size_t run_count = 0;
-  char *fromcode = NULL;
-  char *encoding_type = NULL;
-  char *encoded_text = NULL;
-
-#define BUFINC 128  
-#define CHKBUF(count) do {                       \
-  if (bufpos+count >= bufsize)                   \
-    {                                            \
-      size_t s = bufpos + count - bufsize;       \
-      if (s < BUFINC)                            \
-        s = BUFINC;                              \
-      if (realloc_buffer (&buffer, &bufsize, s)) \
-	{                                        \
-	  free (buffer);                         \
-          free (fromcode);                       \
-          free (encoding_type);                  \
-          free (encoded_text);                   \
-	  return ENOMEM;                         \
-	}                                        \
-     }                                           \
- } while (0) 
-  
-  if (!input)
-    return EINVAL;
-  if (!ptostr)
-    return MU_ERR_OUT_PTR_NULL;
-
-  fromstr = input;
-
-  /* Allocate the buffer. It is assumed that encoded string is always
-     longer than it's decoded variant, so it's safe to use its length
-     as the first estimate */
-  bufsize = strlen (fromstr) + 1;
-  buffer = malloc (bufsize);
-  if (buffer == NULL)
-    return ENOMEM;
-  bufpos = 0;
-  
-  while (*fromstr)
-    {
-      if (strncmp (fromstr, "=?", 2) == 0)
-	{
-	  mu_stream_t filter = NULL;
-	  mu_stream_t in_stream = NULL;
-	  const char *filter_type = NULL;
-	  size_t nbytes = 0, size;
-	  const char *sp = fromstr + 2;
-	  char tmp[128];
-	  
-	  status = getword (&fromcode, &sp, '?');
-	  if (status)
-	    break;
-	  status = getword (&encoding_type, &sp, '?');
-	  if (status)
-	    break;
-	  status = getword (&encoded_text, &sp, '?');
-	  if (status)
-	    break;
-	  if (sp == NULL || sp[0] != '=')
-	    {
-	      status = MU_ERR_BAD_2047_INPUT;
-	      break;
-	    }
-      
-	  size = strlen (encoded_text);
-
-	  switch (encoding_type[0])
-	    {
-            case 'b':
-	    case 'B':
-	      filter_type = "base64";
-	      break;
-	     
-            case 'q': 
-	    case 'Q':
-	      filter_type = "Q";
-	      break;
-
-	    default:
-	      status = MU_ERR_BAD_2047_INPUT;
-	      break;
-	    }
-	  
-	  if (status != 0)
-	    break;
-
-	  mu_memory_stream_create (&in_stream, 0);
-	  mu_stream_write (in_stream, encoded_text, size, NULL);
-	  mu_stream_seek (in_stream, 0, MU_SEEK_SET, NULL);
-	  status = mu_decode_filter (&filter, in_stream, filter_type, fromcode,
-				     tocode);
-	  mu_stream_unref (in_stream);
-	  if (status != 0)
-	    break;
-
-	  while (mu_stream_read (filter, tmp, sizeof (tmp), &nbytes) == 0
-		 && nbytes)
-	    {
-	      CHKBUF (nbytes);
-	      memcpy (buffer + bufpos, tmp, nbytes);
-	      bufpos += nbytes;
-	    }
-
-	  mu_stream_close (filter);
-	  mu_stream_destroy (&filter);
-	  
-	  fromstr = sp + 1;
-	  run_count = 1;
-	}
-      else if (run_count)
-	{
-	  if (*fromstr == ' ' || *fromstr == '\t')
-	    {
-	      run_count++;
-	      fromstr++;
-	      continue;
-	    }
-	  else
-	    {
-	      if (--run_count)
-		{
-		  CHKBUF (run_count);
-		  memcpy (buffer + bufpos, fromstr - run_count, run_count);
-		  bufpos += run_count;
-		  run_count = 0;
-		}
-	      CHKBUF (1);
-	      buffer[bufpos++] = *fromstr++;
-	    }
-	}
-      else
-	{
-	  CHKBUF (1);
-	  buffer[bufpos++] = *fromstr++;
-	}
-    }
-  
-  if (*fromstr)
-    {
-      size_t len = strlen (fromstr);
-      CHKBUF (len);
-      memcpy (buffer + bufpos, fromstr, len);
-      bufpos += len;
-    }
-
-  CHKBUF (1);
-  buffer[bufpos++] = 0;
-  
-  free (fromcode);
-  free (encoding_type);
-  free (encoded_text);
-
-  if (status)
-    free (buffer);
-  else
-    *ptostr = realloc (buffer, bufpos);
-  return status;
-}
-
-
-/**
-   Encode a header according to RFC 2047
-   
-   @param charset
-     Charset of the text to encode
-   @param encoding
-     Requested encoding (must be "base64" or "quoted-printable")
-   @param text
-     Actual text to encode
-   @param result [OUT]
-     Encoded string
-
-   @return 0 on success
-*/
-int
-mu_rfc2047_encode (const char *charset, const char *encoding,
-		   const char *text, char **result)
-{
-  mu_stream_t input_stream;
-  mu_stream_t output_stream;
-  int rc;
-  
-  if (charset == NULL || encoding == NULL || text == NULL)
-    return EINVAL;
-
-  if (strcmp (encoding, "base64") == 0)
-    encoding = "B";
-  else if (strcmp (encoding, "quoted-printable") == 0)
-    encoding = "Q";
-  else if (encoding[1] || !strchr ("BQ", encoding[0]))
-    return MU_ERR_BAD_2047_ENCODING;
-
-  rc = mu_memory_stream_create (&input_stream, 0);
-  if (rc)
-    return rc;
-  
-  mu_stream_write (input_stream, text, strlen (text), NULL);
-  mu_stream_seek (input_stream, 0, MU_SEEK_SET, NULL);
-  rc = mu_filter_create (&output_stream, input_stream,
-			 encoding, MU_FILTER_ENCODE,
-			 MU_STREAM_READ | MU_STREAM_AUTOCLOSE);
-  if (rc == 0)
-    {
-      /* Assume strlen(qp_encoded_text) <= strlen(text) * 3 */
-      /* malloced length is composed of:
-	 "=?"  
-	 charset 
-	 "?"
-	 B or Q
-	 "?" 
-	 encoded_text
-	 "?="
-	 zero terminator */
-      
-      *result = malloc (2 + strlen (charset) + 3 + strlen (text) * 3 + 3);
-      if (*result)
-	{
-	  char *p = *result;
-	  size_t s;
-	  
-	  p += sprintf (p, "=?%s?%s?", charset, encoding);
-	  
-	  rc = mu_stream_read (output_stream,
-			       p,
-			       strlen (text) * 3, &s);
-
-	  strcpy (p + s, "?=");
-	}
-      else
-	rc = ENOMEM;
-      mu_stream_destroy (&output_stream);
-    }
-  else
-    mu_stream_destroy (&input_stream);
-
-  return rc;
-}
diff --git a/libmailutils/secret.c b/libmailutils/secret.c
deleted file mode 100644
index e472839..0000000
--- a/libmailutils/secret.c
+++ /dev/null
@@ -1,135 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 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 <stdlib.h>
-#include <string.h>
-#include <mailutils/types.h>
-#include <mailutils/secret.h>
-#include <mailutils/errno.h>
-
-struct _mu_secret
-{
-  unsigned int refcnt;      /* Number of references to this object */
-  size_t length;            /* Secret length */
-  unsigned char *obptr;     /* Obfuscated data */
-  unsigned char *clptr;     /* Cleartext data */
-  unsigned int clref;       /* Number of references to clptr returned
-			       this far */
-};
-
-static unsigned char xchar;
-
-static void
-obfuscate (const unsigned char *input, unsigned char *output, size_t len)
-{
-  if (!xchar)
-    xchar = random () % 255;
-  while (len--)
-    *output++ = *input++ ^ xchar;
-}
-  
-int
-mu_secret_create (mu_secret_t *psec, const char *value, size_t len)
-{
-  mu_secret_t sec;
-  sec = calloc (1, sizeof (sec[0]) + 2 * (len + 1));
-  if (!sec)
-    return ENOMEM;
-  sec->obptr = (unsigned char*)(sec + 1);
-  sec->clptr = sec->obptr + len + 1;
-  obfuscate ((unsigned char *) value, sec->obptr, len);
-  sec->length = len;
-  *psec = sec;
-  mu_secret_ref (sec);
-  return 0;
-}
-
-int
-mu_secret_dup (mu_secret_t sec, mu_secret_t *newsec)
-{
-  const char *pass = mu_secret_password (sec);
-  int rc = mu_secret_create (newsec, pass, strlen (pass));
-  mu_secret_password_unref (sec);
-  return rc;
-}
-
-void
-mu_secret_ref (mu_secret_t sec)
-{
-  if (sec)
-    sec->refcnt++;
-}
-
-/* Decrement reference counter in SEC. If it falls to 0, free memory
-   allocated for SEC and return 0. Otherwise, return MU_ERR_EXISTS,
-   indicating that someone else is still holding it.
-   Return EINVAL if sec==NULL. */
-int
-mu_secret_unref (mu_secret_t sec)
-{
-  if (sec)
-    {
-      if (sec->refcnt)
-	sec->refcnt--;
-      if (sec->refcnt == 0)
-	{
-	  memset (sec->clptr, 0, sec->length);
-	  memset (sec->obptr, 0, sec->length);
-	  free (sec);
-	  return 0;
-	}
-      return MU_ERR_EXISTS;
-    }
-  return EINVAL;
-}
-
-void
-mu_secret_destroy (mu_secret_t *psec)
-{
-  if (psec && *psec && mu_secret_unref (*psec) == 0)
-    *psec = NULL;
-}
-
-const char *
-mu_secret_password (mu_secret_t sec)
-{
-  if (!sec)
-    return 0;
-  if (sec->clref++ == 0)
-    obfuscate (sec->obptr, sec->clptr, sec->length);
-  return (const char*) sec->clptr;
-}
-
-size_t
-mu_secret_length (mu_secret_t sec)
-{
-  if (!sec)
-    return 0;
-  return sec->length;
-}
-
-void
-mu_secret_password_unref (mu_secret_t sec)
-{
-  if (--sec->clref == 0)
-    memset (sec->clptr, 0, sec->length);
-}
-
diff --git a/libmailutils/server/Makefile.am b/libmailutils/server/Makefile.am
new file mode 100644
index 0000000..77aa154
--- /dev/null
+++ b/libmailutils/server/Makefile.am
@@ -0,0 +1,26 @@
+# GNU Mailutils -- a suite of utilities for electronic mail
+# Copyright (C) 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, see
+# <http://www.gnu.org/licenses/>.
+
+noinst_LTLIBRARIES = libserver.la
+
+libserver_la_SOURCES = \
+ acl.c\
+ server.c\
+ msrv.c\
+ ipsrv.c
+
+INCLUDES = @MU_LIB_COMMON_INCLUDES@ -I/libmailutils
diff --git a/libmailutils/acl.c b/libmailutils/server/acl.c
similarity index 100%
rename from libmailutils/acl.c
rename to libmailutils/server/acl.c
diff --git a/libmailutils/ipsrv.c b/libmailutils/server/ipsrv.c
similarity index 100%
rename from libmailutils/ipsrv.c
rename to libmailutils/server/ipsrv.c
diff --git a/libmailutils/msrv.c b/libmailutils/server/msrv.c
similarity index 100%
rename from libmailutils/msrv.c
rename to libmailutils/server/msrv.c
diff --git a/libmailutils/server.c b/libmailutils/server/server.c
similarity index 100%
rename from libmailutils/server.c
rename to libmailutils/server/server.c
diff --git a/libmailutils/sha1.c b/libmailutils/sha1.c
deleted file mode 100644
index 57d6377..0000000
--- a/libmailutils/sha1.c
+++ /dev/null
@@ -1,418 +0,0 @@
-/* sha1.c - Functions to compute SHA1 message digest of files or
-   memory blocks according to the NIST specification FIPS-180-1.
-
-   Copyright (C) 2000, 2001, 2003, 2004, 2005, 2006, 2010 Free Software
-   Foundation, Inc.
-
-   This program 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 2, 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.  */
-
-/* Written by Scott G. Miller
-   Credits:
-      Robert Klep <robert@ilse.nl>  -- Expansion function fix
-*/
-
-#ifdef HAVE_CONFIG_H
-# include <config.h>
-#endif
-
-#include <mailutils/sha1.h>
-
-#include <stddef.h>
-#include <string.h>
-
-#if USE_UNLOCKED_IO
-# include "unlocked-io.h"
-#endif
-
-#ifdef WORDS_BIGENDIAN
-# define SWAP(n) (n)
-#else
-# define SWAP(n) \
-    (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24))
-#endif
-
-#define BLOCKSIZE 4096
-#if BLOCKSIZE % 64 != 0
-# error "invalid BLOCKSIZE"
-#endif
-
-/* This array contains the bytes used to pad the buffer to the next
-   64-byte boundary.  (RFC 1321, 3.1: Step 1)  */
-static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ...  */ };
-
-
-/* Take a pointer to a 160 bit block of data (five 32 bit ints) and
-   initialize it to the start constants of the SHA1 algorithm.  This
-   must be called before using hash in the call to sha1_hash.  */
-void
-sha1_init_ctx (struct sha1_ctx *ctx)
-{
-  ctx->A = 0x67452301;
-  ctx->B = 0xefcdab89;
-  ctx->C = 0x98badcfe;
-  ctx->D = 0x10325476;
-  ctx->E = 0xc3d2e1f0;
-
-  ctx->total[0] = ctx->total[1] = 0;
-  ctx->buflen = 0;
-}
-
-/* Put result from CTX in first 20 bytes following RESBUF.  The result
-   must be in little endian byte order.
-
-   IMPORTANT: On some systems it is required that RESBUF is correctly
-   aligned for a 32-bit value.  */
-void *
-sha1_read_ctx (const struct sha1_ctx *ctx, void *resbuf)
-{
-  ((uint32_t *) resbuf)[0] = SWAP (ctx->A);
-  ((uint32_t *) resbuf)[1] = SWAP (ctx->B);
-  ((uint32_t *) resbuf)[2] = SWAP (ctx->C);
-  ((uint32_t *) resbuf)[3] = SWAP (ctx->D);
-  ((uint32_t *) resbuf)[4] = SWAP (ctx->E);
-
-  return resbuf;
-}
-
-/* Process the remaining bytes in the internal buffer and the usual
-   prolog according to the standard and write the result to RESBUF.
-
-   IMPORTANT: On some systems it is required that RESBUF is correctly
-   aligned for a 32-bit value.  */
-void *
-sha1_finish_ctx (struct sha1_ctx *ctx, void *resbuf)
-{
-  /* Take yet unprocessed bytes into account.  */
-  uint32_t bytes = ctx->buflen;
-  size_t size = (bytes < 56) ? 64 / 4 : 64 * 2 / 4;
-
-  /* Now count remaining bytes.  */
-  ctx->total[0] += bytes;
-  if (ctx->total[0] < bytes)
-    ++ctx->total[1];
-
-  /* Put the 64-bit file length in *bits* at the end of the buffer.  */
-  ctx->buffer[size - 2] = SWAP ((ctx->total[1] << 3) | (ctx->total[0] >> 29));
-  ctx->buffer[size - 1] = SWAP (ctx->total[0] << 3);
-
-  memcpy (&((char *) ctx->buffer)[bytes], fillbuf, (size - 2) * 4 - bytes);
-
-  /* Process last bytes.  */
-  sha1_process_block (ctx->buffer, size * 4, ctx);
-
-  return sha1_read_ctx (ctx, resbuf);
-}
-
-/* Compute SHA1 message digest for bytes read from STREAM.  The
-   resulting message digest number will be written into the 16 bytes
-   beginning at RESBLOCK.  */
-int
-sha1_stream (FILE *stream, void *resblock)
-{
-  struct sha1_ctx ctx;
-  char buffer[BLOCKSIZE + 72];
-  size_t sum;
-
-  /* Initialize the computation context.  */
-  sha1_init_ctx (&ctx);
-
-  /* Iterate over full file contents.  */
-  while (1)
-    {
-      /* We read the file in blocks of BLOCKSIZE bytes.  One call of the
-	 computation function processes the whole buffer so that with the
-	 next round of the loop another block can be read.  */
-      size_t n;
-      sum = 0;
-
-      /* Read block.  Take care for partial reads.  */
-      while (1)
-	{
-	  n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
-
-	  sum += n;
-
-	  if (sum == BLOCKSIZE)
-	    break;
-
-	  if (n == 0)
-	    {
-	      /* Check for the error flag IFF N == 0, so that we don't
-		 exit the loop after a partial read due to e.g., EAGAIN
-		 or EWOULDBLOCK.  */
-	      if (ferror (stream))
-		return 1;
-	      goto process_partial_block;
-	    }
-
-	  /* We've read at least one byte, so ignore errors.  But always
-	     check for EOF, since feof may be true even though N > 0.
-	     Otherwise, we could end up calling fread after EOF.  */
-	  if (feof (stream))
-	    goto process_partial_block;
-	}
-
-      /* Process buffer with BLOCKSIZE bytes.  Note that
-			BLOCKSIZE % 64 == 0
-       */
-      sha1_process_block (buffer, BLOCKSIZE, &ctx);
-    }
-
- process_partial_block:;
-
-  /* Process any remaining bytes.  */
-  if (sum > 0)
-    sha1_process_bytes (buffer, sum, &ctx);
-
-  /* Construct result in desired memory.  */
-  sha1_finish_ctx (&ctx, resblock);
-  return 0;
-}
-
-/* Compute SHA1 message digest for LEN bytes beginning at BUFFER.  The
-   result is always in little endian byte order, so that a byte-wise
-   output yields to the wanted ASCII representation of the message
-   digest.  */
-void *
-sha1_buffer (const char *buffer, size_t len, void *resblock)
-{
-  struct sha1_ctx ctx;
-
-  /* Initialize the computation context.  */
-  sha1_init_ctx (&ctx);
-
-  /* Process whole buffer but last len % 64 bytes.  */
-  sha1_process_bytes (buffer, len, &ctx);
-
-  /* Put result in desired memory area.  */
-  return sha1_finish_ctx (&ctx, resblock);
-}
-
-void
-sha1_process_bytes (const void *buffer, size_t len, struct sha1_ctx *ctx)
-{
-  /* When we already have some bits in our internal buffer concatenate
-     both inputs first.  */
-  if (ctx->buflen != 0)
-    {
-      size_t left_over = ctx->buflen;
-      size_t add = 128 - left_over > len ? len : 128 - left_over;
-
-      memcpy (&((char *) ctx->buffer)[left_over], buffer, add);
-      ctx->buflen += add;
-
-      if (ctx->buflen > 64)
-	{
-	  sha1_process_block (ctx->buffer, ctx->buflen & ~63, ctx);
-
-	  ctx->buflen &= 63;
-	  /* The regions in the following copy operation cannot overlap.  */
-	  memcpy (ctx->buffer,
-		  &((char *) ctx->buffer)[(left_over + add) & ~63],
-		  ctx->buflen);
-	}
-
-      buffer = (const char *) buffer + add;
-      len -= add;
-    }
-
-  /* Process available complete blocks.  */
-  if (len >= 64)
-    {
-#if !_STRING_ARCH_unaligned
-# define alignof(type) offsetof (struct { char c; type x; }, x)
-# define UNALIGNED_P(p) (((size_t) p) % alignof (uint32_t) != 0)
-      if (UNALIGNED_P (buffer))
-	while (len > 64)
-	  {
-	    sha1_process_block (memcpy (ctx->buffer, buffer, 64), 64, ctx);
-	    buffer = (const char *) buffer + 64;
-	    len -= 64;
-	  }
-      else
-#endif
-	{
-	  sha1_process_block (buffer, len & ~63, ctx);
-	  buffer = (const char *) buffer + (len & ~63);
-	  len &= 63;
-	}
-    }
-
-  /* Move remaining bytes in internal buffer.  */
-  if (len > 0)
-    {
-      size_t left_over = ctx->buflen;
-
-      memcpy (&((char *) ctx->buffer)[left_over], buffer, len);
-      left_over += len;
-      if (left_over >= 64)
-	{
-	  sha1_process_block (ctx->buffer, 64, ctx);
-	  left_over -= 64;
-	  memcpy (ctx->buffer, &ctx->buffer[16], left_over);
-	}
-      ctx->buflen = left_over;
-    }
-}
-
-/* --- Code below is the primary difference between md5.c and sha1.c --- */
-
-/* SHA1 round constants */
-#define K1 0x5a827999
-#define K2 0x6ed9eba1
-#define K3 0x8f1bbcdc
-#define K4 0xca62c1d6
-
-/* Round functions.  Note that F2 is the same as F4.  */
-#define F1(B,C,D) ( D ^ ( B & ( C ^ D ) ) )
-#define F2(B,C,D) (B ^ C ^ D)
-#define F3(B,C,D) ( ( B & C ) | ( D & ( B | C ) ) )
-#define F4(B,C,D) (B ^ C ^ D)
-
-/* Process LEN bytes of BUFFER, accumulating context into CTX.
-   It is assumed that LEN % 64 == 0.
-   Most of this code comes from GnuPG's cipher/sha1.c.  */
-
-void
-sha1_process_block (const void *buffer, size_t len, struct sha1_ctx *ctx)
-{
-  const uint32_t *words = buffer;
-  size_t nwords = len / sizeof (uint32_t);
-  const uint32_t *endp = words + nwords;
-  uint32_t x[16];
-  uint32_t a = ctx->A;
-  uint32_t b = ctx->B;
-  uint32_t c = ctx->C;
-  uint32_t d = ctx->D;
-  uint32_t e = ctx->E;
-
-  /* First increment the byte count.  RFC 1321 specifies the possible
-     length of the file up to 2^64 bits.  Here we only compute the
-     number of bytes.  Do a double word increment.  */
-  ctx->total[0] += len;
-  if (ctx->total[0] < len)
-    ++ctx->total[1];
-
-#define rol(x, n) (((x) << (n)) | ((uint32_t) (x) >> (32 - (n))))
-
-#define M(I) ( tm =   x[I&0x0f] ^ x[(I-14)&0x0f] \
-		    ^ x[(I-8)&0x0f] ^ x[(I-3)&0x0f] \
-	       , (x[I&0x0f] = rol(tm, 1)) )
-
-#define R(A,B,C,D,E,F,K,M)  do { E += rol( A, 5 )     \
-				      + F( B, C, D )  \
-				      + K	      \
-				      + M;	      \
-				 B = rol( B, 30 );    \
-			       } while(0)
-
-  while (words < endp)
-    {
-      uint32_t tm;
-      int t;
-      for (t = 0; t < 16; t++)
-	{
-	  x[t] = SWAP (*words);
-	  words++;
-	}
-
-      R( a, b, c, d, e, F1, K1, x[ 0] );
-      R( e, a, b, c, d, F1, K1, x[ 1] );
-      R( d, e, a, b, c, F1, K1, x[ 2] );
-      R( c, d, e, a, b, F1, K1, x[ 3] );
-      R( b, c, d, e, a, F1, K1, x[ 4] );
-      R( a, b, c, d, e, F1, K1, x[ 5] );
-      R( e, a, b, c, d, F1, K1, x[ 6] );
-      R( d, e, a, b, c, F1, K1, x[ 7] );
-      R( c, d, e, a, b, F1, K1, x[ 8] );
-      R( b, c, d, e, a, F1, K1, x[ 9] );
-      R( a, b, c, d, e, F1, K1, x[10] );
-      R( e, a, b, c, d, F1, K1, x[11] );
-      R( d, e, a, b, c, F1, K1, x[12] );
-      R( c, d, e, a, b, F1, K1, x[13] );
-      R( b, c, d, e, a, F1, K1, x[14] );
-      R( a, b, c, d, e, F1, K1, x[15] );
-      R( e, a, b, c, d, F1, K1, M(16) );
-      R( d, e, a, b, c, F1, K1, M(17) );
-      R( c, d, e, a, b, F1, K1, M(18) );
-      R( b, c, d, e, a, F1, K1, M(19) );
-      R( a, b, c, d, e, F2, K2, M(20) );
-      R( e, a, b, c, d, F2, K2, M(21) );
-      R( d, e, a, b, c, F2, K2, M(22) );
-      R( c, d, e, a, b, F2, K2, M(23) );
-      R( b, c, d, e, a, F2, K2, M(24) );
-      R( a, b, c, d, e, F2, K2, M(25) );
-      R( e, a, b, c, d, F2, K2, M(26) );
-      R( d, e, a, b, c, F2, K2, M(27) );
-      R( c, d, e, a, b, F2, K2, M(28) );
-      R( b, c, d, e, a, F2, K2, M(29) );
-      R( a, b, c, d, e, F2, K2, M(30) );
-      R( e, a, b, c, d, F2, K2, M(31) );
-      R( d, e, a, b, c, F2, K2, M(32) );
-      R( c, d, e, a, b, F2, K2, M(33) );
-      R( b, c, d, e, a, F2, K2, M(34) );
-      R( a, b, c, d, e, F2, K2, M(35) );
-      R( e, a, b, c, d, F2, K2, M(36) );
-      R( d, e, a, b, c, F2, K2, M(37) );
-      R( c, d, e, a, b, F2, K2, M(38) );
-      R( b, c, d, e, a, F2, K2, M(39) );
-      R( a, b, c, d, e, F3, K3, M(40) );
-      R( e, a, b, c, d, F3, K3, M(41) );
-      R( d, e, a, b, c, F3, K3, M(42) );
-      R( c, d, e, a, b, F3, K3, M(43) );
-      R( b, c, d, e, a, F3, K3, M(44) );
-      R( a, b, c, d, e, F3, K3, M(45) );
-      R( e, a, b, c, d, F3, K3, M(46) );
-      R( d, e, a, b, c, F3, K3, M(47) );
-      R( c, d, e, a, b, F3, K3, M(48) );
-      R( b, c, d, e, a, F3, K3, M(49) );
-      R( a, b, c, d, e, F3, K3, M(50) );
-      R( e, a, b, c, d, F3, K3, M(51) );
-      R( d, e, a, b, c, F3, K3, M(52) );
-      R( c, d, e, a, b, F3, K3, M(53) );
-      R( b, c, d, e, a, F3, K3, M(54) );
-      R( a, b, c, d, e, F3, K3, M(55) );
-      R( e, a, b, c, d, F3, K3, M(56) );
-      R( d, e, a, b, c, F3, K3, M(57) );
-      R( c, d, e, a, b, F3, K3, M(58) );
-      R( b, c, d, e, a, F3, K3, M(59) );
-      R( a, b, c, d, e, F4, K4, M(60) );
-      R( e, a, b, c, d, F4, K4, M(61) );
-      R( d, e, a, b, c, F4, K4, M(62) );
-      R( c, d, e, a, b, F4, K4, M(63) );
-      R( b, c, d, e, a, F4, K4, M(64) );
-      R( a, b, c, d, e, F4, K4, M(65) );
-      R( e, a, b, c, d, F4, K4, M(66) );
-      R( d, e, a, b, c, F4, K4, M(67) );
-      R( c, d, e, a, b, F4, K4, M(68) );
-      R( b, c, d, e, a, F4, K4, M(69) );
-      R( a, b, c, d, e, F4, K4, M(70) );
-      R( e, a, b, c, d, F4, K4, M(71) );
-      R( d, e, a, b, c, F4, K4, M(72) );
-      R( c, d, e, a, b, F4, K4, M(73) );
-      R( b, c, d, e, a, F4, K4, M(74) );
-      R( a, b, c, d, e, F4, K4, M(75) );
-      R( e, a, b, c, d, F4, K4, M(76) );
-      R( d, e, a, b, c, F4, K4, M(77) );
-      R( c, d, e, a, b, F4, K4, M(78) );
-      R( b, c, d, e, a, F4, K4, M(79) );
-
-      a = ctx->A += a;
-      b = ctx->B += b;
-      c = ctx->C += c;
-      d = ctx->D += d;
-      e = ctx->E += e;
-    }
-}
diff --git a/libmailutils/size_max.h b/libmailutils/size_max.h
deleted file mode 100644
index ee3fc25..0000000
--- a/libmailutils/size_max.h
+++ /dev/null
@@ -1,31 +0,0 @@
-/* size_max.h -- declare SIZE_MAX through system headers
-   Copyright (C) 2005, 2006, 2010 Free Software Foundation, Inc.
-   Written by Simon Josefsson.
-
-   This program 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 2, 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.  */
-
-#ifndef GNULIB_SIZE_MAX_H
-#define GNULIB_SIZE_MAX_H
-
-/* Get SIZE_MAX declaration on systems like Solaris 7/8/9.  */
-# include <limits.h>
-/* Get SIZE_MAX declaration on systems like glibc 2.  */
-# if HAVE_STDINT_H
-#  include <stdint.h>
-# endif
-/* On systems where these include files don't define it, SIZE_MAX is defined
-   in config.h.  */
-
-#endif /* GNULIB_SIZE_MAX_H */
diff --git a/libmailutils/stream/Makefile.am b/libmailutils/stream/Makefile.am
new file mode 100644
index 0000000..c68e83a
--- /dev/null
+++ b/libmailutils/stream/Makefile.am
@@ -0,0 +1,41 @@
+# GNU Mailutils -- a suite of utilities for electronic mail
+# Copyright (C) 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, see
+# <http://www.gnu.org/licenses/>.
+
+noinst_LTLIBRARIES = libstream.la
+
+libstream_la_SOURCES = \
+ dbgstream.c\
+ file_stream.c\
+ fltstream.c\
+ iostream.c\
+ mapfile_stream.c\
+ memory_stream.c\
+ message_stream.c\
+ prog_stream.c\
+ rdcache_stream.c\
+ socket_stream.c\
+ stdio_stream.c\
+ stream.c\
+ stream_printf.c\
+ stream_vprintf.c\
+ streamcpy.c\
+ streamref.c\
+ tcp.c\
+ temp_file_stream.c\
+ xscript-stream.c
+
+INCLUDES = @MU_LIB_COMMON_INCLUDES@ -I/libmailutils
diff --git a/libmailutils/dbgstream.c b/libmailutils/stream/dbgstream.c
similarity index 100%
rename from libmailutils/dbgstream.c
rename to libmailutils/stream/dbgstream.c
diff --git a/libmailutils/file_stream.c b/libmailutils/stream/file_stream.c
similarity index 100%
rename from libmailutils/file_stream.c
rename to libmailutils/stream/file_stream.c
diff --git a/libmailutils/fltstream.c b/libmailutils/stream/fltstream.c
similarity index 100%
rename from libmailutils/fltstream.c
rename to libmailutils/stream/fltstream.c
diff --git a/libmailutils/iostream.c b/libmailutils/stream/iostream.c
similarity index 100%
rename from libmailutils/iostream.c
rename to libmailutils/stream/iostream.c
diff --git a/libmailutils/mapfile_stream.c b/libmailutils/stream/mapfile_stream.c
similarity index 100%
rename from libmailutils/mapfile_stream.c
rename to libmailutils/stream/mapfile_stream.c
diff --git a/libmailutils/memory_stream.c b/libmailutils/stream/memory_stream.c
similarity index 100%
rename from libmailutils/memory_stream.c
rename to libmailutils/stream/memory_stream.c
diff --git a/libmailutils/stream/message_stream.c b/libmailutils/stream/message_stream.c
new file mode 100644
index 0000000..91bef83
--- /dev/null
+++ b/libmailutils/stream/message_stream.c
@@ -0,0 +1,438 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 2002, 2005, 2006, 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.
+
+   GNU Mailutils 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 GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
+
+/* This file implements an MH draftfile stream: a read-only stream used
+   to transparently pass MH draftfiles to mailers. The only difference
+   between the usual RFC822 and MH draft is that the latter allows to use
+   a string of dashes to separate the headers from the body. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+#include <stdlib.h>
+#include <ctype.h>
+#include <string.h>
+
+#include <mailutils/types.h>
+#include <mailutils/address.h>
+#include <mailutils/alloc.h>
+#include <mailutils/envelope.h>
+#include <mailutils/message.h>
+#include <mailutils/header.h>
+#include <mailutils/body.h>
+#include <mailutils/stream.h>
+#include <mailutils/mutil.h>
+#include <mailutils/errno.h>
+#include <mailutils/error.h>
+#include <mailutils/cctype.h>
+#include <mailutils/cstr.h>
+#include <mailutils/sys/message_stream.h>
+
+
+static int
+_env_msg_date (mu_envelope_t envelope, char *buf, size_t len, size_t *pnwrite)
+{
+  struct _mu_message_stream *str = mu_envelope_get_owner (envelope);
+  
+  if (!str || !str->date)
+    return EINVAL;
+  if (buf)
+    {
+      strncpy (buf, str->date, len);
+      buf[len-1] = 0;
+      if (pnwrite)
+	*pnwrite = len;
+    }
+  else if (!pnwrite)
+    return EINVAL;
+  else
+    *pnwrite = strlen (str->date);
+  return 0;
+}
+
+static int
+_env_msg_sender (mu_envelope_t envelope, char *buf, size_t len,
+		 size_t *pnwrite)
+{
+  struct _mu_message_stream *str = mu_envelope_get_owner (envelope);
+  
+  if (!str || !str->from)
+    return EINVAL;
+  if (buf)
+    {
+      strncpy (buf, str->from, len);
+      buf[len-1] = 0;
+      if (pnwrite)
+	*pnwrite = len;
+    }
+  else if (!pnwrite)
+    return EINVAL;
+  else
+    *pnwrite = strlen (str->from);
+    
+  return 0;
+}
+
+
+static int
+_message_read (mu_stream_t stream, char *optr, size_t osize, size_t *nbytes)
+{
+  int rc;
+  struct _mu_message_stream *s = (struct _mu_message_stream*) stream;
+  mu_off_t offset = s->offset + s->envelope_length;
+  size_t rsize;
+  
+  if (offset < s->mark_offset)
+    {
+      if (offset + osize >= s->mark_offset)
+	osize = s->mark_offset - offset;
+    }
+  else
+    offset += s->mark_length;
+  /* FIXME: Seeking each time before read is awkward. The streamref
+     should be modified to take care of it */
+  rc = mu_stream_seek (s->transport, offset, MU_SEEK_SET, NULL);
+  if (rc == 0)
+    rc = mu_stream_read (s->transport, optr, osize, &rsize);
+  if (rc == 0)
+    {
+      s->offset += rsize;
+      if (nbytes)
+	*nbytes = rsize;
+    }
+  else
+    s->stream.last_err = rc;
+  return rc;
+}
+  
+static int
+_message_size (mu_stream_t stream, mu_off_t *psize)
+{
+  struct _mu_message_stream *s = (struct _mu_message_stream*) stream;
+  int rc = mu_stream_size (s->transport, psize);
+  
+  if (rc == 0)
+    *psize -= s->envelope_length + s->mark_length;
+  return rc;
+}
+  
+static int
+scan_stream (struct _mu_message_stream *str)
+{
+  char *from = NULL;
+  char *env_from = NULL;
+  char *env_date = NULL;
+  int rc;
+  char *buffer = NULL;
+  size_t bufsize = 0;
+  size_t len;
+  mu_off_t body_start, body_end;
+  mu_stream_t stream = str->transport;
+
+  if (str->envelope)
+    {
+      char *s = str->envelope + 5;
+      char *p = strchr (s, ' ');
+      size_t len;
+
+      if (p)
+	{
+	  len = p - s;
+	  env_from = mu_alloc (len + 1);
+	  if (!env_from)
+	    return ENOMEM;
+	  memcpy (env_from, s, len);
+	  env_from[len] = 0;
+	  env_date = mu_strdup (p + 1);
+	  if (!env_date)
+	    {
+	      free (env_from);
+	      return ENOMEM;
+	    }
+	}
+    }
+
+  rc = mu_stream_seek (stream, 0, MU_SEEK_SET, NULL);
+  if (rc)
+    return rc;
+  while ((rc = mu_stream_getline (stream, &buffer, &bufsize, &len)) == 0
+	 && len > 0)
+    {
+      if (buffer[0] == '\n')
+	break;
+
+      if (!env_from || !env_date)
+	{
+      	  if (!from && mu_c_strncasecmp (buffer, MU_HEADER_FROM,
+				         sizeof (MU_HEADER_FROM) - 1) == 0)
+	    
+	    from = mu_strdup (mu_str_skip_class (buffer +
+						 sizeof (MU_HEADER_FROM),
+						 MU_CTYPE_SPACE));
+	  else if (!env_from
+		   && mu_c_strncasecmp (buffer, MU_HEADER_ENV_SENDER,
+				        sizeof (MU_HEADER_ENV_SENDER) - 1) == 0)
+	    env_from = mu_strdup (mu_str_skip_class (buffer +
+					sizeof (MU_HEADER_ENV_SENDER),
+						     MU_CTYPE_SPACE));
+	  else if (!env_date
+		   && mu_c_strncasecmp (buffer, MU_HEADER_ENV_DATE,
+				        sizeof (MU_HEADER_ENV_DATE) - 1) == 0)
+	    env_date = mu_strdup (mu_str_skip_class (buffer +
+						  sizeof (MU_HEADER_ENV_DATE),
+						     MU_CTYPE_SPACE));
+	}
+    }
+
+  free (buffer);
+
+  rc = mu_stream_seek (stream, 0, MU_SEEK_CUR, &body_start);
+  if (rc)
+    return rc;
+  rc = mu_stream_size (stream, &body_end);
+  if (rc)
+    return rc;
+  
+  if (!env_from)
+    {
+      if (from)
+	{
+	  mu_address_t addr;
+	  
+	  mu_address_create (&addr, from);
+	  if (!addr
+	      || mu_address_aget_email (addr, 1, &env_from))
+	    env_from = mu_strdup ("GNU-Mailutils");
+	  mu_address_destroy (&addr);
+	}
+      else
+	env_from = mu_strdup ("GNU-MH");
+    }
+  free (from);
+  
+  if (!env_date)
+    {
+      struct tm *tm;
+      time_t t;
+      char date[80]; /* FIXME: This size is way too big */
+
+      time(&t);
+      tm = gmtime(&t);
+      mu_strftime (date, sizeof (date), "%a %b %e %H:%M:%S %Y", tm);
+      env_date = strdup (date);
+    }
+
+  str->from = env_from;
+  str->date = env_date;
+
+  str->body_start = body_start;
+  str->body_end = body_end - 1;
+  
+  return 0;
+}
+
+static int
+_message_open (mu_stream_t stream)
+{
+  struct _mu_message_stream *s = (struct _mu_message_stream*) stream;
+  size_t offset, len;
+  char *buffer = NULL;
+  size_t bufsize = 0;
+  int rc;
+
+  offset = 0;
+  mu_stream_seek (s->transport, 0, MU_SEEK_SET, NULL);
+  while ((rc = mu_stream_getline (s->transport, &buffer, &bufsize,
+				  &len)) == 0
+	 && len > 0)
+    {
+      if (offset == 0 && memcmp (buffer, "From ", 5) == 0)
+	{
+	  s->envelope_length = len;
+	  s->envelope = mu_strdup (buffer);
+	  if (!s->envelope)
+	    return ENOMEM;
+	  s->envelope[len - 1] = 0;
+	}
+      else if (mu_mh_delim (buffer))
+	{
+	  s->mark_offset = offset;
+	  s->mark_length = len - 1; /* do not count the terminating newline */
+	  break;
+	}
+
+      offset += len;
+    }
+  free (buffer);
+
+  return scan_stream (s);
+}
+
+static int
+_message_close (mu_stream_t stream)
+{
+  struct _mu_message_stream *s = (struct _mu_message_stream*) stream;
+  return s->stream.last_err = mu_stream_close (s->transport);
+}
+
+static void
+_message_done (mu_stream_t stream)
+{
+  struct _mu_message_stream *s = (struct _mu_message_stream*) stream;
+
+  free (s->envelope);
+  free (s->date);
+  free (s->from);
+  mu_stream_destroy (&s->transport);
+}
+
+static int
+_message_seek (struct _mu_stream *stream, mu_off_t off, mu_off_t *presult)
+{ 
+  struct _mu_message_stream *s = (struct _mu_message_stream*) stream;
+  mu_off_t size;
+
+  mu_stream_size (stream, &size);
+  if (off < 0 || off >= size)
+    return ESPIPE;
+  s->offset = off;
+  *presult = off;
+  return 0;
+}
+
+const char *
+_message_error_string (struct _mu_stream *stream, int rc)
+{
+  struct _mu_message_stream *str = (struct _mu_message_stream*) stream;
+  return mu_stream_strerror (str->transport, rc);
+}
+
+int
+mu_message_stream_create (mu_stream_t *pstream, mu_stream_t src, int flags)
+{
+  struct _mu_message_stream *s;
+  int sflag;
+  int rc;
+  mu_stream_t stream;
+  
+  mu_stream_get_flags (src, &sflag);
+  sflag &= MU_STREAM_SEEK;
+  
+  if (!flags)
+    flags = MU_STREAM_READ;
+  if (flags & (MU_STREAM_WRITE|MU_STREAM_CREAT|MU_STREAM_APPEND))
+    return EINVAL;
+  s = (struct _mu_message_stream *) _mu_stream_create (sizeof (*s),
+						       flags | sflag);
+  if (!s)
+    return ENOMEM;
+
+  rc = mu_streamref_create (&s->transport, src);
+  if (rc)
+    {
+      free (s);
+      return rc;
+    }
+  s->stream.open = _message_open;
+  s->stream.close = _message_close;
+  s->stream.done = _message_done;
+  s->stream.read = _message_read;
+  s->stream.size = _message_size;
+  s->stream.seek = _message_seek;
+  s->stream.error_string = _message_error_string;
+
+  stream = (mu_stream_t)s;
+  rc = mu_stream_open (stream);
+  if (rc)
+    mu_stream_destroy (&stream);
+  else
+    *pstream = stream;
+  return rc;
+}
+
+
+/* *************************** MH draft message **************************** */
+
+
+
+static int
+_body_obj_size (mu_body_t body, size_t *size)
+{
+  mu_message_t msg = mu_body_get_owner (body);
+  struct _mu_message_stream *str = mu_message_get_owner (msg);
+
+  if (size)
+    *size = str->body_end - str->body_start + 1;
+  return 0;
+}
+
+
+
+int
+mu_stream_to_message (mu_stream_t instream, mu_message_t *pmsg)
+{
+  mu_envelope_t env;
+  mu_message_t msg;
+  mu_body_t body;
+  mu_stream_t bstream;
+  mu_stream_t draftstream;
+  int rc;
+  struct _mu_message_stream *sp;
+  
+  /* FIXME: Perhaps MU_STREAM_NO_CLOSE is needed */
+  if ((rc = mu_message_stream_create (&draftstream, instream, 0)))
+    return rc;
+
+  if ((rc = mu_message_create (&msg, draftstream)))
+    {
+      mu_stream_destroy (&draftstream);
+      return rc;
+    }
+  
+  mu_message_set_stream (msg, draftstream, draftstream);
+  
+  if ((rc = mu_envelope_create (&env, draftstream)))
+    {
+      mu_message_destroy (&msg, draftstream);
+      mu_stream_destroy (&draftstream);
+      return rc;
+    }
+  
+  mu_envelope_set_date (env, _env_msg_date, draftstream);
+  mu_envelope_set_sender (env, _env_msg_sender, draftstream);
+  mu_message_set_envelope (msg, env, draftstream);
+
+  mu_body_create (&body, msg);
+  /* FIXME: It would be cleaner to use ioctl here */
+  sp = (struct _mu_message_stream *) draftstream;
+  rc = mu_streamref_create_abridged (&bstream, instream,
+				     sp->body_start, sp->body_end);
+  if (rc)
+    {
+      mu_body_destroy (&body, msg);
+      mu_message_destroy (&msg, draftstream);
+      mu_stream_destroy (&draftstream);
+      return rc;
+    }
+  
+  mu_body_set_stream (body, bstream, msg);
+  mu_body_set_size (body, _body_obj_size, msg);
+  mu_message_set_body (msg, body, draftstream);
+
+  *pmsg = msg;
+  return 0;
+}
diff --git a/libmailutils/prog_stream.c b/libmailutils/stream/prog_stream.c
similarity index 100%
rename from libmailutils/prog_stream.c
rename to libmailutils/stream/prog_stream.c
diff --git a/libmailutils/rdcache_stream.c b/libmailutils/stream/rdcache_stream.c
similarity index 100%
rename from libmailutils/rdcache_stream.c
rename to libmailutils/stream/rdcache_stream.c
diff --git a/libmailutils/socket_stream.c b/libmailutils/stream/socket_stream.c
similarity index 100%
rename from libmailutils/socket_stream.c
rename to libmailutils/stream/socket_stream.c
diff --git a/libmailutils/stdio_stream.c b/libmailutils/stream/stdio_stream.c
similarity index 100%
rename from libmailutils/stdio_stream.c
rename to libmailutils/stream/stdio_stream.c
diff --git a/libmailutils/stream.c b/libmailutils/stream/stream.c
similarity index 100%
rename from libmailutils/stream.c
rename to libmailutils/stream/stream.c
diff --git a/libmailutils/stream_printf.c b/libmailutils/stream/stream_printf.c
similarity index 100%
rename from libmailutils/stream_printf.c
rename to libmailutils/stream/stream_printf.c
diff --git a/libmailutils/stream_vprintf.c b/libmailutils/stream/stream_vprintf.c
similarity index 100%
rename from libmailutils/stream_vprintf.c
rename to libmailutils/stream/stream_vprintf.c
diff --git a/libmailutils/streamcpy.c b/libmailutils/stream/streamcpy.c
similarity index 100%
rename from libmailutils/streamcpy.c
rename to libmailutils/stream/streamcpy.c
diff --git a/libmailutils/streamref.c b/libmailutils/stream/streamref.c
similarity index 100%
rename from libmailutils/streamref.c
rename to libmailutils/stream/streamref.c
diff --git a/libmailutils/stream/tcp.c b/libmailutils/stream/tcp.c
new file mode 100644
index 0000000..eafd40b
--- /dev/null
+++ b/libmailutils/stream/tcp.c
@@ -0,0 +1,362 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2004, 2006, 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, see 
+   <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <errno.h>
+#include <fcntl.h>
+#include <netdb.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <sys/socket.h>
+#include <sys/types.h>
+
+#include <netinet/in.h>
+
+#include <arpa/inet.h>
+
+#include <mailutils/errno.h>
+#include <mailutils/stream.h>
+#include <mailutils/mutil.h>
+
+#include <mailutils/sys/stream.h>
+
+#define TCP_STATE_INIT 		1
+#define TCP_STATE_RESOLVE	2
+#define TCP_STATE_RESOLVING	3
+#define TCP_STATE_CONNECTING 	4
+#define TCP_STATE_CONNECTED	5
+
+struct _tcp_instance
+{
+  struct _mu_stream stream;
+  int 		fd;
+  char 		*host;
+  int 		port;
+  int		state;
+  unsigned long	address;
+  unsigned long source_addr;
+};
+
+/* On solaris inet_addr() return -1.  */
+#ifndef INADDR_NONE
+# define INADDR_NONE (unsigned long)-1
+#endif
+
+static int
+_tcp_close (mu_stream_t stream)
+{
+  struct _tcp_instance *tcp = (struct _tcp_instance *)stream;
+  int err = 0;
+
+  if (tcp->fd != -1)
+    {
+      if (close (tcp->fd) != 0)
+	{
+	  err = errno;
+	}
+    }
+  tcp->fd = -1;
+  tcp->state = TCP_STATE_INIT;
+  return err;
+}
+
+static int
+resolve_hostname (const char *host, unsigned long *ip)
+{
+  unsigned long address = inet_addr (host);
+  if (address == INADDR_NONE)
+    {
+      struct hostent *phe = gethostbyname (host);
+      if (!phe)
+	return MU_ERR_GETHOSTBYNAME;
+      address = *(((unsigned long **) phe->h_addr_list)[0]);
+    }
+  *ip = address;
+  return 0;
+}
+
+static int
+_tcp_open (mu_stream_t stream)
+{
+  struct _tcp_instance *tcp = (struct _tcp_instance *)stream;
+  int flgs, ret;
+  socklen_t namelen;
+  struct sockaddr_in peer_addr;
+  struct sockaddr_in soc_addr;
+  int flags;
+
+  mu_stream_get_flags (stream, &flags);
+
+  switch (tcp->state)
+    {
+    case TCP_STATE_INIT:
+      if (tcp->fd == -1)
+	{
+	  if ((tcp->fd = socket (PF_INET, SOCK_STREAM, 0)) == -1)
+	    return errno;
+	}
+      if (flags & MU_STREAM_NONBLOCK)
+	{
+	  flgs = fcntl (tcp->fd, F_GETFL);
+	  flgs |= O_NONBLOCK;
+	  fcntl (tcp->fd, F_SETFL, flgs);
+	  mu_stream_set_flags (stream, MU_STREAM_NONBLOCK);
+	}
+      if (tcp->source_addr != INADDR_ANY)
+	{
+	  struct sockaddr_in s;
+	  s.sin_family = AF_INET;
+	  s.sin_addr.s_addr = tcp->source_addr;
+	  s.sin_port = 0;
+	  if (bind (tcp->fd, (struct sockaddr*) &s, sizeof(s)) < 0)
+	    {
+	      int e = errno;
+	      close (tcp->fd);
+	      tcp->fd = -1;
+	      return e;
+	    }
+	}
+      
+      tcp->state = TCP_STATE_RESOLVING;
+      
+    case TCP_STATE_RESOLVING:
+      if (!(tcp->host != NULL && tcp->port > 0))
+	{
+	  _tcp_close (stream);
+	  return EINVAL;
+	}
+      
+      if ((ret = resolve_hostname (tcp->host, &tcp->address)))
+	{
+	  _tcp_close (stream);
+	  return ret;
+	}
+      tcp->state = TCP_STATE_RESOLVE;
+      
+    case TCP_STATE_RESOLVE:
+      memset (&soc_addr, 0, sizeof (soc_addr));
+      soc_addr.sin_family = AF_INET;
+      soc_addr.sin_port = htons (tcp->port);
+      soc_addr.sin_addr.s_addr = tcp->address;
+
+      if ((connect (tcp->fd,
+		    (struct sockaddr *) &soc_addr, sizeof (soc_addr))) == -1)
+	{
+	  ret = errno;
+	  if (ret == EINPROGRESS || ret == EAGAIN)
+	    {
+	      tcp->state = TCP_STATE_CONNECTING;
+	      ret = EAGAIN;
+	    }
+	  else
+	    _tcp_close (stream);
+	  return ret;
+	}
+      tcp->state = TCP_STATE_CONNECTING;
+      
+    case TCP_STATE_CONNECTING:
+      namelen = sizeof (peer_addr);
+      if (getpeername (tcp->fd,
+		       (struct sockaddr *) &peer_addr, &namelen) == 0)
+	tcp->state = TCP_STATE_CONNECTED;
+      else
+	{
+	  ret = errno;
+	  _tcp_close (stream);
+	  return ret;
+	}
+      break;
+    }
+  return 0;
+}
+
+static int
+_tcp_ioctl (mu_stream_t stream, int code, void *ptr)
+{
+  struct _tcp_instance *tcp = (struct _tcp_instance *)stream;
+  mu_transport_t *ptrans;
+
+  switch (code)
+    {
+    case MU_IOCTL_GET_TRANSPORT:
+      if (!ptr)
+	return EINVAL;
+      ptrans = ptr;
+      ptrans[0] = (mu_transport_t) tcp->fd;
+      ptrans[1] = NULL;
+      break;
+
+    default:
+      return ENOSYS;
+    }
+  return 0;
+}
+
+static int
+_tcp_read (mu_stream_t stream, char *buf, size_t size, size_t *pret)
+{
+  struct _tcp_instance *tcp = (struct _tcp_instance *)stream;
+  ssize_t bytes;
+
+  if ((bytes = recv (tcp->fd, buf, size, 0)) == -1)
+    return errno;
+  *pret = bytes;
+  return 0;
+}
+
+static int
+_tcp_write (mu_stream_t stream, const char *buf, size_t size, size_t *pret)
+{
+  struct _tcp_instance *tcp = (struct _tcp_instance *)stream;
+  ssize_t bytes;
+
+  if ((bytes = send (tcp->fd, buf, size, 0)) == -1)
+    return errno;
+  *pret = bytes;
+  return 0;
+}
+
+static void
+_tcp_done (mu_stream_t stream)
+{
+  struct _tcp_instance *tcp = (struct _tcp_instance *)stream;
+
+  if (tcp->host)
+    free (tcp->host);
+  if (tcp->fd != -1)
+    close (tcp->fd);
+}
+
+int
+_tcp_wait (mu_stream_t stream, int *pflags, struct timeval *tvp)
+{
+  struct _tcp_instance *tcp = (struct _tcp_instance *)stream;
+  if (tcp->fd == -1)
+    return EINVAL;
+  return mu_fd_wait (tcp->fd, pflags, tvp);
+}
+
+int
+_tcp_shutdown (mu_stream_t stream, int how)
+{
+  struct _tcp_instance *tcp = (struct _tcp_instance *)stream;
+  int flag;
+  if (tcp->fd == -1)
+    return EINVAL;
+
+  switch (how)
+    {
+    case MU_STREAM_READ:
+      flag = SHUT_RD;
+      break;
+      
+    case MU_STREAM_WRITE:
+      flag = SHUT_WR;
+    }
+
+  if (shutdown (tcp->fd, flag))
+    return errno;
+  return 0;
+}
+
+static struct _tcp_instance *
+_create_tcp_stream (int flags)
+{
+  struct _tcp_instance *tcp =
+    (struct _tcp_instance *)_mu_stream_create (sizeof (*tcp), flags);
+
+  if (tcp)
+    {
+      tcp->stream.open = _tcp_open;
+      tcp->stream.close = _tcp_close;
+      tcp->stream.read = _tcp_read;
+      tcp->stream.write = _tcp_write;
+      tcp->stream.ctl = _tcp_ioctl;
+      tcp->stream.done = _tcp_done;
+      tcp->stream.wait = _tcp_wait;
+      tcp->stream.shutdown = _tcp_shutdown;
+      tcp->fd = -1;
+      tcp->state = TCP_STATE_INIT;
+    }
+  return tcp;
+}
+
+int
+mu_tcp_stream_create_with_source_ip (mu_stream_t *pstream,
+				     const char *host, int port,
+				     unsigned long source_ip,
+				     int flags)
+{
+  int rc;
+  mu_stream_t stream;
+  struct _tcp_instance *tcp;
+
+  if (host == NULL)
+    return MU_ERR_TCP_NO_HOST;
+
+  if (port < 1)
+    return MU_ERR_TCP_NO_PORT;
+
+  tcp = _create_tcp_stream (flags | MU_STREAM_RDWR);
+  if (!tcp)
+    return ENOMEM;
+  tcp->host = strdup (host);
+  if (!tcp->host)
+    {
+      free (tcp);
+      return ENOMEM;
+    }
+  tcp->port = port;
+  tcp->state = TCP_STATE_INIT;
+  tcp->source_addr = source_ip;
+  stream = (mu_stream_t) tcp;
+  rc = mu_stream_open (stream);
+  if (rc == 0 || rc == EAGAIN || rc == EINPROGRESS)
+    *pstream = stream;
+  else
+    mu_stream_destroy (&stream);
+  return rc;
+}
+
+int
+mu_tcp_stream_create_with_source_host (mu_stream_t *stream,
+				       const char *host, int port,
+				       const char *source_host,
+				       int flags)
+{
+  unsigned long source_addr;
+  int ret = resolve_hostname (source_host, &source_addr);
+  if (ret == 0)
+    ret = mu_tcp_stream_create_with_source_ip (stream, host, port,
+					       source_addr, flags);
+  return ret;
+}
+       
+int
+mu_tcp_stream_create (mu_stream_t *stream, const char *host, int port,
+		      int flags)
+{
+  return mu_tcp_stream_create_with_source_ip (stream, host, port,
+					      INADDR_ANY, flags);
+}
diff --git a/libmailutils/temp_file_stream.c b/libmailutils/stream/temp_file_stream.c
similarity index 100%
rename from libmailutils/temp_file_stream.c
rename to libmailutils/stream/temp_file_stream.c
diff --git a/libmailutils/xscript-stream.c b/libmailutils/stream/xscript-stream.c
similarity index 100%
rename from libmailutils/xscript-stream.c
rename to libmailutils/stream/xscript-stream.c
diff --git a/libmailutils/string/Makefile.am b/libmailutils/string/Makefile.am
new file mode 100644
index 0000000..52452b9
--- /dev/null
+++ b/libmailutils/string/Makefile.am
@@ -0,0 +1,34 @@
+# GNU Mailutils -- a suite of utilities for electronic mail
+# Copyright (C) 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, see
+# <http://www.gnu.org/licenses/>.
+
+noinst_LTLIBRARIES = libstring.la
+
+libstring_la_SOURCES = \
+ cstrcasecmp.c\
+ cstrlower.c\
+ cstrupper.c\
+ strltrim.c\
+ strskip.c\
+ stripws.c\
+ strrtrim.c\
+ asnprintf.c\
+ asprintf.c\
+ muctype.c\
+ vasnprintf.c\
+ mkfilename.c
+
+INCLUDES = @MU_LIB_COMMON_INCLUDES@ -I/libmailutils
diff --git a/libmailutils/string/asnprintf.c b/libmailutils/string/asnprintf.c
new file mode 100644
index 0000000..2d74ec9
--- /dev/null
+++ b/libmailutils/string/asnprintf.c
@@ -0,0 +1,39 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 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, see <http://www.gnu.org/licenses/>. */
+
+#if HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <stdarg.h>
+#include <stdio.h>
+
+#include <mailutils/io.h>
+
+int
+mu_asnprintf (char **pbuf, size_t *psize, const char *fmt, ...)
+{
+  int rc;
+  va_list ap;
+
+  va_start (ap, fmt);
+  rc = mu_vasnprintf (pbuf, psize, fmt, ap);
+  va_end (ap);
+  return rc;
+}
+
diff --git a/libmailutils/string/asprintf.c b/libmailutils/string/asprintf.c
new file mode 100644
index 0000000..3d11df8
--- /dev/null
+++ b/libmailutils/string/asprintf.c
@@ -0,0 +1,40 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 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, see <http://www.gnu.org/licenses/>. */
+
+#if HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <stdarg.h>
+#include <stdio.h>
+
+#include <mailutils/io.h>
+
+int
+mu_asprintf (char **pbuf, const char *fmt, ...)
+{
+  va_list ap;
+  size_t size;
+  int rc;
+  
+  va_start (ap, fmt);
+  *pbuf = NULL;
+  size = 0;
+  rc = mu_vasnprintf (pbuf, &size, fmt, ap);
+  va_end (ap);
+  return rc;
+}
diff --git a/libmailutils/cstrcasecmp.c b/libmailutils/string/cstrcasecmp.c
similarity index 100%
rename from libmailutils/cstrcasecmp.c
rename to libmailutils/string/cstrcasecmp.c
diff --git a/libmailutils/cstrlower.c b/libmailutils/string/cstrlower.c
similarity index 100%
rename from libmailutils/cstrlower.c
rename to libmailutils/string/cstrlower.c
diff --git a/libmailutils/cstrupper.c b/libmailutils/string/cstrupper.c
similarity index 100%
rename from libmailutils/cstrupper.c
rename to libmailutils/string/cstrupper.c
diff --git a/libmailutils/mkfilename.c b/libmailutils/string/mkfilename.c
similarity index 100%
rename from libmailutils/mkfilename.c
rename to libmailutils/string/mkfilename.c
diff --git a/libmailutils/muctype.c b/libmailutils/string/muctype.c
similarity index 100%
rename from libmailutils/muctype.c
rename to libmailutils/string/muctype.c
diff --git a/libmailutils/stripws.c b/libmailutils/string/stripws.c
similarity index 100%
rename from libmailutils/stripws.c
rename to libmailutils/string/stripws.c
diff --git a/libmailutils/strltrim.c b/libmailutils/string/strltrim.c
similarity index 100%
rename from libmailutils/strltrim.c
rename to libmailutils/string/strltrim.c
diff --git a/libmailutils/strrtrim.c b/libmailutils/string/strrtrim.c
similarity index 100%
rename from libmailutils/strrtrim.c
rename to libmailutils/string/strrtrim.c
diff --git a/libmailutils/strskip.c b/libmailutils/string/strskip.c
similarity index 100%
rename from libmailutils/strskip.c
rename to libmailutils/string/strskip.c
diff --git a/libmailutils/string/vasnprintf.c b/libmailutils/string/vasnprintf.c
new file mode 100644
index 0000000..6c788d5
--- /dev/null
+++ b/libmailutils/string/vasnprintf.c
@@ -0,0 +1,86 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 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, see 
+   <http://www.gnu.org/licenses/>. */
+
+#if HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <errno.h>
+#include <string.h>
+
+#include <mailutils/io.h>
+
+int
+mu_vasnprintf (char **pbuf, size_t *psize, const char *fmt, va_list ap)
+{
+  char *buf = *pbuf;
+  size_t buflen = *psize;
+  int rc = 0;
+  
+  if (!buf)
+    {
+      if (buflen == 0)
+	buflen = 512; /* Initial allocation */
+      
+      buf = calloc (1, buflen);
+      if (buf == NULL)
+	return ENOMEM;
+    }
+  
+  for (;;)
+    {
+      ssize_t n = vsnprintf (buf, buflen, fmt, ap);
+      if (n < 0 || n >= buflen || !memchr (buf, '\0', n + 1))
+	{
+	  char *newbuf;
+	  size_t newlen = buflen * 2;
+	  if (newlen < buflen)
+	    {
+	      rc = ENOMEM;
+	      break;
+	    }
+	  newbuf = realloc (buf, newlen);
+	  if (newbuf == NULL)
+	    {
+	      rc = ENOMEM;
+	      break;
+	    }
+	  buflen = newlen;
+	  buf = newbuf;
+	}
+      else
+	break;
+    }
+
+  if (rc)
+    {
+      if (!*pbuf)
+	{
+	  /* We made first allocation, now free it */
+	  free (buf);
+	  buf = NULL;
+	  buflen = 0;
+	}
+    }
+
+  *pbuf = buf;
+  *psize = buflen;
+  return rc;
+}
diff --git a/libmailutils/syslog.c b/libmailutils/syslog.c
deleted file mode 100644
index c97da9b..0000000
--- a/libmailutils/syslog.c
+++ /dev/null
@@ -1,145 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
-
-   This program 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 of the License, 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, see <http://www.gnu.org/licenses/>. */
-
-#ifdef HAVE_CONFIG_H
-# include <config.h>
-#endif
-#include <syslog.h>
-#include <string.h>
-#include <mailutils/diag.h>
-#include <mailutils/kwd.h>
-#include <mailutils/syslog.h>
-#include <mailutils/cstr.h>
-
-#ifndef LOG_AUTHPRIV
-# define LOG_AUTHPRIV
-#endif
-
-static mu_kwd_t kw_facility[] = {
-  { "USER",    LOG_USER },   
-  { "DAEMON",  LOG_DAEMON },
-  { "AUTH",    LOG_AUTH },
-  { "AUTHPRIV",LOG_AUTHPRIV },
-  { "MAIL",    LOG_MAIL },
-  { "CRON",    LOG_CRON },
-  { "LOCAL0",  LOG_LOCAL0 },
-  { "LOCAL1",  LOG_LOCAL1 },
-  { "LOCAL2",  LOG_LOCAL2 },
-  { "LOCAL3",  LOG_LOCAL3 },
-  { "LOCAL4",  LOG_LOCAL4 },
-  { "LOCAL5",  LOG_LOCAL5 },
-  { "LOCAL6",  LOG_LOCAL6 },
-  { "LOCAL7",  LOG_LOCAL7 },
-  { NULL }
-};
-
-static int
-syslog_to_n (mu_kwd_t *kw, const char *str, int *pint)
-{
-  if (mu_c_strncasecmp (str, "LOG_", 4) == 0)
-    str += 4;
-  return mu_kwd_xlat_name_ci (kw, str, pint);
-}
-
-int
-mu_string_to_syslog_facility (const char *str, int *pfacility)
-{
-  return syslog_to_n (kw_facility, str, pfacility);
-}
-
-const char *
-mu_syslog_facility_to_string (int n)
-{
-  const char *res = NULL;
-  mu_kwd_xlat_tok (kw_facility, n, &res);
-  return res;
-}
-
-static mu_kwd_t kw_prio[] = {
-  { "EMERG", LOG_EMERG },
-  { "ALERT", LOG_ALERT },
-  { "CRIT", LOG_CRIT },
-  { "ERR", LOG_ERR },
-  { "WARNING", LOG_WARNING },
-  { "NOTICE", LOG_NOTICE },
-  { "INFO", LOG_INFO },
-  { "DEBUG", LOG_DEBUG },
-  { NULL }
-};
-
-int
-mu_string_to_syslog_priority (const char *str, int *pprio)
-{
-  return syslog_to_n (kw_prio, str, pprio);
-}
-
-const char *
-mu_syslog_priority_to_string (int n)
-{
-  const char *res = NULL;
-  mu_kwd_xlat_tok (kw_prio, n, &res);
-  return res;
-}
-
-int
-mu_diag_level_to_syslog (mu_log_level_t level)
-{
-  switch (level)
-    {
-    case MU_DIAG_EMERG:
-      return LOG_EMERG;
-      
-    case MU_DIAG_ALERT:
-      return LOG_ALERT;
-	
-    case MU_DIAG_CRIT:
-      return LOG_CRIT;
-      
-    case MU_DIAG_ERROR:
-      return LOG_ERR;
-      
-    case MU_DIAG_WARNING:
-      return LOG_WARNING;
-      
-    case MU_DIAG_NOTICE:
-      return LOG_NOTICE;
-      
-    case MU_DIAG_INFO:
-      return LOG_INFO;
-      
-    case MU_DIAG_DEBUG:
-      return LOG_DEBUG;
-    }
-  return LOG_EMERG;
-}
-
-int
-mu_diag_syslog_printer (void *data, mu_log_level_t level, const char *buf)
-{
-  int len = strlen (buf);
-  if (len > 0 && buf[len-1] == '\n')
-    {
-      len--;
-      if (len > 0 && buf[len-1] == '\r')
-	len--;
-    }
-  syslog (mu_diag_level_to_syslog (level), "%-.*s", len, buf);
-  return 0;
-}
-
-
-int mu_log_facility = LOG_FACILITY;
-char *mu_log_tag = NULL;
diff --git a/libmailutils/system.c b/libmailutils/system.c
deleted file mode 100644
index 8e28caa..0000000
--- a/libmailutils/system.c
+++ /dev/null
@@ -1,160 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 2002, 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 <unistd.h>
-#include <sys/types.h>
-#include <pwd.h>
-#ifdef HAVE_SHADOW_H
-# include <shadow.h>
-#endif
-#include <errno.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#ifdef HAVE_STRINGS_H
-# include <strings.h>
-#endif
-#ifdef HAVE_CRYPT_H
-# include <crypt.h>
-#endif
-
-#include <mailutils/list.h>
-#include <mailutils/iterator.h>
-#include <mailutils/mailbox.h>
-#include <mailutils/mu_auth.h>
-#include <mailutils/errno.h>
-
-/* System database */
-static int
-mu_auth_system (struct mu_auth_data **return_data, const struct passwd *pw)
-{
-  char *mailbox_name;
-  int rc;
-  
-  if (!pw)
-    return MU_ERR_AUTH_FAILURE;
-
-  rc = mu_construct_user_mailbox_url (&mailbox_name, pw->pw_name);
-  if (rc)
-    return rc; /* FIXME: Return code is lost */
-  
-  rc = mu_auth_data_alloc (return_data,
-			   pw->pw_name,
-			   pw->pw_passwd,
-			   pw->pw_uid,
-			   pw->pw_gid,
-			   pw->pw_gecos,
-			   pw->pw_dir,
-			   pw->pw_shell,
-			   mailbox_name,
-			   1);
-  free (mailbox_name);
-  return rc;
-}
-
-int
-mu_auth_system_by_name (struct mu_auth_data **return_data,
-			const void *key,
-			void *func_data MU_ARG_UNUSED,
-			void *call_data MU_ARG_UNUSED)
-{
-  if (!key)
-    return EINVAL;
-  return mu_auth_system (return_data, getpwnam (key));
-}
-
-static int
-mu_auth_system_by_uid (struct mu_auth_data **return_data,
-		       const void *key,
-		       void *func_data MU_ARG_UNUSED,
-		       void *call_data MU_ARG_UNUSED)
-{
-  if (!key)
-    return EINVAL;
-  return mu_auth_system (return_data, getpwuid (*(uid_t*) key));
-}
-
-static int
-mu_authenticate_generic (struct mu_auth_data **return_data MU_ARG_UNUSED,
-			 const void *key,
-			 void *func_data MU_ARG_UNUSED,
-			 void *call_data)
-{
-  const struct mu_auth_data *auth_data = key;
-  char *pass = call_data;
-
-  if (!auth_data || !pass)
-    return EINVAL;
-  
-  return auth_data->passwd
-         && strcmp (auth_data->passwd, crypt (pass, auth_data->passwd)) == 0 ?
-          0 : MU_ERR_AUTH_FAILURE;
-}
-
-/* Called only if generic fails */
-static int
-mu_authenticate_system (struct mu_auth_data **return_data MU_ARG_UNUSED,
-			const void *key,
-			void *func_data MU_ARG_UNUSED,
-			void *call_data)
-{
-  const struct mu_auth_data *auth_data = key;
-
-#ifdef HAVE_SHADOW_H
-  char *pass = call_data;
-  
-  if (auth_data)
-    {
-      struct spwd *spw;
-      spw = getspnam (auth_data->name);
-      if (spw)
-	return strcmp (spw->sp_pwdp, crypt (pass, spw->sp_pwdp)) == 0 ?
-	        0 : MU_ERR_AUTH_FAILURE;
-    }
-#endif
-  return MU_ERR_AUTH_FAILURE;
-}
-
-
-struct mu_auth_module mu_auth_system_module = {
-  "system",
-  NULL,
-  mu_authenticate_system,
-  NULL,
-  mu_auth_system_by_name,
-  NULL,
-  mu_auth_system_by_uid,
-  NULL
-};
-
-
-struct mu_auth_module mu_auth_generic_module = {
-  "generic",
-  NULL,
-  mu_authenticate_generic,
-  NULL,
-  mu_auth_nosupport,
-  NULL,
-  mu_auth_nosupport,
-  NULL
-};
-
diff --git a/libmailutils/tcp.c b/libmailutils/tcp.c
deleted file mode 100644
index aea7809..0000000
--- a/libmailutils/tcp.c
+++ /dev/null
@@ -1,363 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 1999, 2000, 2004, 2006, 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 <errno.h>
-#include <fcntl.h>
-#include <netdb.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-
-#include <sys/socket.h>
-#include <sys/types.h>
-
-#include <netinet/in.h>
-
-#include <arpa/inet.h>
-
-#include <mailutils/errno.h>
-#include <mailutils/stream.h>
-#include <mailutils/mutil.h>
-
-#include <mailutils/sys/stream.h>
-
-#define TCP_STATE_INIT 		1
-#define TCP_STATE_RESOLVE	2
-#define TCP_STATE_RESOLVING	3
-#define TCP_STATE_CONNECTING 	4
-#define TCP_STATE_CONNECTED	5
-
-struct _tcp_instance
-{
-  struct _mu_stream stream;
-  int 		fd;
-  char 		*host;
-  int 		port;
-  int		state;
-  unsigned long	address;
-  unsigned long source_addr;
-};
-
-/* On solaris inet_addr() return -1.  */
-#ifndef INADDR_NONE
-# define INADDR_NONE (unsigned long)-1
-#endif
-
-static int
-_tcp_close (mu_stream_t stream)
-{
-  struct _tcp_instance *tcp = (struct _tcp_instance *)stream;
-  int err = 0;
-
-  if (tcp->fd != -1)
-    {
-      if (close (tcp->fd) != 0)
-	{
-	  err = errno;
-	}
-    }
-  tcp->fd = -1;
-  tcp->state = TCP_STATE_INIT;
-  return err;
-}
-
-static int
-resolve_hostname (const char *host, unsigned long *ip)
-{
-  unsigned long address = inet_addr (host);
-  if (address == INADDR_NONE)
-    {
-      struct hostent *phe = gethostbyname (host);
-      if (!phe)
-	return MU_ERR_GETHOSTBYNAME;
-      address = *(((unsigned long **) phe->h_addr_list)[0]);
-    }
-  *ip = address;
-  return 0;
-}
-
-static int
-_tcp_open (mu_stream_t stream)
-{
-  struct _tcp_instance *tcp = (struct _tcp_instance *)stream;
-  int flgs, ret;
-  socklen_t namelen;
-  struct sockaddr_in peer_addr;
-  struct sockaddr_in soc_addr;
-  int flags;
-
-  mu_stream_get_flags (stream, &flags);
-
-  switch (tcp->state)
-    {
-    case TCP_STATE_INIT:
-      if (tcp->fd == -1)
-	{
-	  if ((tcp->fd = socket (PF_INET, SOCK_STREAM, 0)) == -1)
-	    return errno;
-	}
-      if (flags & MU_STREAM_NONBLOCK)
-	{
-	  flgs = fcntl (tcp->fd, F_GETFL);
-	  flgs |= O_NONBLOCK;
-	  fcntl (tcp->fd, F_SETFL, flgs);
-	  mu_stream_set_flags (stream, MU_STREAM_NONBLOCK);
-	}
-      if (tcp->source_addr != INADDR_ANY)
-	{
-	  struct sockaddr_in s;
-	  s.sin_family = AF_INET;
-	  s.sin_addr.s_addr = tcp->source_addr;
-	  s.sin_port = 0;
-	  if (bind (tcp->fd, (struct sockaddr*) &s, sizeof(s)) < 0)
-	    {
-	      int e = errno;
-	      close (tcp->fd);
-	      tcp->fd = -1;
-	      return e;
-	    }
-	}
-      
-      tcp->state = TCP_STATE_RESOLVING;
-      
-    case TCP_STATE_RESOLVING:
-      if (!(tcp->host != NULL && tcp->port > 0))
-	{
-	  _tcp_close (stream);
-	  return EINVAL;
-	}
-      
-      if ((ret = resolve_hostname (tcp->host, &tcp->address)))
-	{
-	  _tcp_close (stream);
-	  return ret;
-	}
-      tcp->state = TCP_STATE_RESOLVE;
-      
-    case TCP_STATE_RESOLVE:
-      memset (&soc_addr, 0, sizeof (soc_addr));
-      soc_addr.sin_family = AF_INET;
-      soc_addr.sin_port = htons (tcp->port);
-      soc_addr.sin_addr.s_addr = tcp->address;
-
-      if ((connect (tcp->fd,
-		    (struct sockaddr *) &soc_addr, sizeof (soc_addr))) == -1)
-	{
-	  ret = errno;
-	  if (ret == EINPROGRESS || ret == EAGAIN)
-	    {
-	      tcp->state = TCP_STATE_CONNECTING;
-	      ret = EAGAIN;
-	    }
-	  else
-	    _tcp_close (stream);
-	  return ret;
-	}
-      tcp->state = TCP_STATE_CONNECTING;
-      
-    case TCP_STATE_CONNECTING:
-      namelen = sizeof (peer_addr);
-      if (getpeername (tcp->fd,
-		       (struct sockaddr *) &peer_addr, &namelen) == 0)
-	tcp->state = TCP_STATE_CONNECTED;
-      else
-	{
-	  ret = errno;
-	  _tcp_close (stream);
-	  return ret;
-	}
-      break;
-    }
-  return 0;
-}
-
-static int
-_tcp_ioctl (mu_stream_t stream, int code, void *ptr)
-{
-  struct _tcp_instance *tcp = (struct _tcp_instance *)stream;
-  mu_transport_t *ptrans;
-
-  switch (code)
-    {
-    case MU_IOCTL_GET_TRANSPORT:
-      if (!ptr)
-	return EINVAL;
-      ptrans = ptr;
-      ptrans[0] = (mu_transport_t) tcp->fd;
-      ptrans[1] = NULL;
-      break;
-
-    default:
-      return ENOSYS;
-    }
-  return 0;
-}
-
-static int
-_tcp_read (mu_stream_t stream, char *buf, size_t size, size_t *pret)
-{
-  struct _tcp_instance *tcp = (struct _tcp_instance *)stream;
-  ssize_t bytes;
-
-  if ((bytes = recv (tcp->fd, buf, size, 0)) == -1)
-    return errno;
-  *pret = bytes;
-  return 0;
-}
-
-static int
-_tcp_write (mu_stream_t stream, const char *buf, size_t size, size_t *pret)
-{
-  struct _tcp_instance *tcp = (struct _tcp_instance *)stream;
-  ssize_t bytes;
-
-  if ((bytes = send (tcp->fd, buf, size, 0)) == -1)
-    return errno;
-  *pret = bytes;
-  return 0;
-}
-
-static void
-_tcp_done (mu_stream_t stream)
-{
-  struct _tcp_instance *tcp = (struct _tcp_instance *)stream;
-
-  if (tcp->host)
-    free (tcp->host);
-  if (tcp->fd != -1)
-    close (tcp->fd);
-}
-
-int
-_tcp_wait (mu_stream_t stream, int *pflags, struct timeval *tvp)
-{
-  struct _tcp_instance *tcp = (struct _tcp_instance *)stream;
-  if (tcp->fd == -1)
-    return EINVAL;
-  return mu_fd_wait (tcp->fd, pflags, tvp);
-}
-
-int
-_tcp_shutdown (mu_stream_t stream, int how)
-{
-  struct _tcp_instance *tcp = (struct _tcp_instance *)stream;
-  int flag;
-  if (tcp->fd == -1)
-    return EINVAL;
-
-  switch (how)
-    {
-    case MU_STREAM_READ:
-      flag = SHUT_RD;
-      break;
-      
-    case MU_STREAM_WRITE:
-      flag = SHUT_WR;
-    }
-
-  if (shutdown (tcp->fd, flag))
-    return errno;
-  return 0;
-}
-
-static struct _tcp_instance *
-_create_tcp_stream (int flags)
-{
-  struct _tcp_instance *tcp =
-    (struct _tcp_instance *)_mu_stream_create (sizeof (*tcp), flags);
-
-  if (tcp)
-    {
-      tcp->stream.open = _tcp_open;
-      tcp->stream.close = _tcp_close;
-      tcp->stream.read = _tcp_read;
-      tcp->stream.write = _tcp_write;
-      tcp->stream.ctl = _tcp_ioctl;
-      tcp->stream.done = _tcp_done;
-      tcp->stream.wait = _tcp_wait;
-      tcp->stream.shutdown = _tcp_shutdown;
-      tcp->fd = -1;
-      tcp->state = TCP_STATE_INIT;
-    }
-  return tcp;
-}
-
-int
-mu_tcp_stream_create_with_source_ip (mu_stream_t *pstream,
-				     const char *host, int port,
-				     unsigned long source_ip,
-				     int flags)
-{
-  int rc;
-  mu_stream_t stream;
-  struct _tcp_instance *tcp;
-
-  if (host == NULL)
-    return MU_ERR_TCP_NO_HOST;
-
-  if (port < 1)
-    return MU_ERR_TCP_NO_PORT;
-
-  tcp = _create_tcp_stream (flags | MU_STREAM_RDWR);
-  if (!tcp)
-    return ENOMEM;
-  tcp->host = strdup (host);
-  if (!tcp->host)
-    {
-      free (tcp);
-      return ENOMEM;
-    }
-  tcp->port = port;
-  tcp->state = TCP_STATE_INIT;
-  tcp->source_addr = source_ip;
-  stream = (mu_stream_t) tcp;
-  rc = mu_stream_open (stream);
-  if (rc == 0 || rc == EAGAIN || rc == EINPROGRESS)
-    *pstream = stream;
-  else
-    mu_stream_destroy (&stream);
-  return rc;
-}
-
-int
-mu_tcp_stream_create_with_source_host (mu_stream_t *stream,
-				       const char *host, int port,
-				       const char *source_host,
-				       int flags)
-{
-  unsigned long source_addr;
-  int ret = resolve_hostname (source_host, &source_addr);
-  if (ret == 0)
-    ret = mu_tcp_stream_create_with_source_ip (stream, host, port,
-					       source_addr, flags);
-  return ret;
-}
-       
-int
-mu_tcp_stream_create (mu_stream_t *stream, const char *host, int port,
-		      int flags)
-{
-  return mu_tcp_stream_create_with_source_ip (stream, host, port,
-					      INADDR_ANY, flags);
-}
diff --git a/libmailutils/tests/Makefile.am b/libmailutils/tests/Makefile.am
index a8215fb..a0b9574 100644
--- a/libmailutils/tests/Makefile.am
+++ b/libmailutils/tests/Makefile.am
@@ -6,7 +6,7 @@
 # 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
+# GNU Mailutils 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.
diff --git a/libmailutils/tests/addr.c b/libmailutils/tests/addr.c
index b6bdaa8..64eb884 100644
--- a/libmailutils/tests/addr.c
+++ b/libmailutils/tests/addr.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libmailutils/tests/address.at b/libmailutils/tests/address.at
index 0a2bd26..5de3fcd 100644
--- a/libmailutils/tests/address.at
+++ b/libmailutils/tests/address.at
@@ -6,7 +6,7 @@
 # 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
+# GNU Mailutils 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.
diff --git a/libmailutils/tests/argcv.at b/libmailutils/tests/argcv.at
index ca932e5..857f500 100644
--- a/libmailutils/tests/argcv.at
+++ b/libmailutils/tests/argcv.at
@@ -6,7 +6,7 @@
 # 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
+# GNU Mailutils 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.
diff --git a/libmailutils/tests/argcv.c b/libmailutils/tests/argcv.c
index 3253feb..88c54a2 100644
--- a/libmailutils/tests/argcv.c
+++ b/libmailutils/tests/argcv.c
@@ -12,9 +12,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libmailutils/tests/base64d.at b/libmailutils/tests/base64d.at
index 9b68e3e..9983f58 100644
--- a/libmailutils/tests/base64d.at
+++ b/libmailutils/tests/base64d.at
@@ -6,7 +6,7 @@
 # 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
+# GNU Mailutils 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.
diff --git a/libmailutils/tests/base64e.at b/libmailutils/tests/base64e.at
index 0799fcf..c0e0563 100644
--- a/libmailutils/tests/base64e.at
+++ b/libmailutils/tests/base64e.at
@@ -6,7 +6,7 @@
 # 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
+# GNU Mailutils 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.
diff --git a/libmailutils/tests/decode2047.at b/libmailutils/tests/decode2047.at
index 2a5fa38..69e7b22 100644
--- a/libmailutils/tests/decode2047.at
+++ b/libmailutils/tests/decode2047.at
@@ -6,7 +6,7 @@
 # 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
+# GNU Mailutils 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.
diff --git a/libmailutils/tests/decode2047.c b/libmailutils/tests/decode2047.c
index f7c36a9..22120b2 100644
--- a/libmailutils/tests/decode2047.c
+++ b/libmailutils/tests/decode2047.c
@@ -12,9 +12,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libmailutils/tests/encode2047.at b/libmailutils/tests/encode2047.at
index ed59672..2a47c37 100644
--- a/libmailutils/tests/encode2047.at
+++ b/libmailutils/tests/encode2047.at
@@ -6,7 +6,7 @@
 # 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
+# GNU Mailutils 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.
diff --git a/libmailutils/tests/encode2047.c b/libmailutils/tests/encode2047.c
index dbf3620..208ef3f 100644
--- a/libmailutils/tests/encode2047.c
+++ b/libmailutils/tests/encode2047.c
@@ -12,9 +12,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libmailutils/tests/fltst.c b/libmailutils/tests/fltst.c
index d62a02b..e5c0ae1 100644
--- a/libmailutils/tests/fltst.c
+++ b/libmailutils/tests/fltst.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libmailutils/tests/fromflt.at b/libmailutils/tests/fromflt.at
index e03bf11..0451451 100644
--- a/libmailutils/tests/fromflt.at
+++ b/libmailutils/tests/fromflt.at
@@ -6,7 +6,7 @@
 # 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
+# GNU Mailutils 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.
diff --git a/libmailutils/tests/list.at b/libmailutils/tests/list.at
index 093a9d4..b1b1c74 100644
--- a/libmailutils/tests/list.at
+++ b/libmailutils/tests/list.at
@@ -6,7 +6,7 @@
 # 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
+# GNU Mailutils 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.
diff --git a/libmailutils/tests/listop.c b/libmailutils/tests/listop.c
index 176dd20..564e1d3 100644
--- a/libmailutils/tests/listop.c
+++ b/libmailutils/tests/listop.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libmailutils/tests/mailcap.at b/libmailutils/tests/mailcap.at
index e3673db..4a602e6 100644
--- a/libmailutils/tests/mailcap.at
+++ b/libmailutils/tests/mailcap.at
@@ -6,7 +6,7 @@
 # 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
+# GNU Mailutils 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.
diff --git a/libmailutils/tests/mailcap.c b/libmailutils/tests/mailcap.c
index 136b971..7f50b27 100644
--- a/libmailutils/tests/mailcap.c
+++ b/libmailutils/tests/mailcap.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libmailutils/tests/testsuite.at b/libmailutils/tests/testsuite.at
index 46e04b3..ed6e530 100644
--- a/libmailutils/tests/testsuite.at
+++ b/libmailutils/tests/testsuite.at
@@ -6,7 +6,7 @@
 # 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
+# GNU Mailutils 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.
diff --git a/libmailutils/tests/url-parse.c b/libmailutils/tests/url-parse.c
index 3eb3d1c..92fd4f1 100644
--- a/libmailutils/tests/url-parse.c
+++ b/libmailutils/tests/url-parse.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libmailutils/tests/url.at b/libmailutils/tests/url.at
index 010d328..9f933ea 100644
--- a/libmailutils/tests/url.at
+++ b/libmailutils/tests/url.at
@@ -6,7 +6,7 @@
 # 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
+# GNU Mailutils 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.
diff --git a/libmailutils/ticket.c b/libmailutils/ticket.c
deleted file mode 100644
index ec7f609..0000000
--- a/libmailutils/ticket.c
+++ /dev/null
@@ -1,245 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 1999, 2000, 2001, 2004, 2005, 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 <errno.h>
-#include <sys/types.h>
-#include <unistd.h>
-#include <string.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <termios.h>
-
-#include <mailutils/mutil.h>
-#include <mailutils/errno.h>
-#include <mailutils/secret.h>
-#include <mailutils/sys/auth.h>
-
-static void
-echo_off (struct termios *stored_settings)
-{
-  struct termios new_settings;
-  tcgetattr (0, stored_settings);
-  new_settings = *stored_settings;
-  new_settings.c_lflag &= (~ECHO);
-  tcsetattr (0, TCSANOW, &new_settings);
-}
-
-static void
-echo_on (struct termios *stored_settings)
-{
-  tcsetattr (0, TCSANOW, stored_settings);
-}
-
-int
-mu_ticket_create (mu_ticket_t *pticket, void *owner)
-{
-  mu_ticket_t ticket;
-  if (pticket == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-  ticket = calloc (1, sizeof (*ticket));
-  if (ticket == NULL)
-    return ENOMEM;
-  ticket->owner = owner;
-  mu_ticket_ref (ticket);
-  *pticket = ticket;
-  return 0;
-}
-
-int
-mu_ticket_ref (mu_ticket_t ticket)
-{
-  if (!ticket)
-    return EINVAL;
-  ticket->refcnt++;
-  return 0;
-}
-  
-int
-mu_ticket_unref (mu_ticket_t ticket)
-{
-  if (!ticket)
-    return EINVAL;
-  if (ticket->refcnt)
-    ticket->refcnt--;
-  if (ticket->refcnt == 0)
-    {
-      if (ticket->plain)
-	free (ticket->plain);
-      if (ticket->secret)
-	mu_secret_destroy (&ticket->secret);
-      if (ticket->_destroy)
-	ticket->_destroy (ticket);
-      free (ticket);
-      return 0;
-    }
-  return MU_ERR_EXISTS;
-}
-      
-void
-mu_ticket_destroy (mu_ticket_t *pticket)
-{
-  if (pticket && *pticket && mu_ticket_unref (*pticket) == 0)
-    *pticket = NULL;
-}
-
-int
-mu_ticket_set_destroy (mu_ticket_t ticket,
-		       void (*_destroy) (mu_ticket_t), void *owner)
-{
-  if (ticket == NULL)
-    return EINVAL;
-  if (ticket->owner != owner)
-    return EACCES;
-  ticket->_destroy = _destroy;
-  return 0;
-}
-
-void *
-mu_ticket_get_owner (mu_ticket_t ticket)
-{
-  return (ticket) ? ticket->owner : NULL;
-}
-
-int
-mu_ticket_set_get_cred (mu_ticket_t ticket,
-			int  (*_get_cred) (mu_ticket_t, mu_url_t,
-					   const char *,
-					   char **, mu_secret_t *),
-			void *owner)
-{
-  if (ticket == NULL)
-    return EINVAL;
-  if (ticket->owner != owner)
-    return EACCES;
-  ticket->_get_cred = _get_cred;
-  return 0;
-}
-
-int
-mu_ticket_set_secret (mu_ticket_t ticket, mu_secret_t secret)
-{
-  if (ticket == NULL)
-    return EINVAL;
-  if (ticket->secret)
-    mu_secret_unref (ticket->secret);
-  mu_secret_ref (secret);
-  ticket->secret = secret;
-  return 0;
-}
-
-int
-mu_ticket_set_plain (mu_ticket_t ticket, const char *text)
-{
-  if (ticket == NULL)
-    return EINVAL;
-  if (ticket->plain)
-    free (ticket->plain);
-  ticket->plain = strdup (text);
-  if (!ticket->plain)
-    return ENOMEM;
-  return 0;
-}
-
-int
-mu_ticket_get_cred (mu_ticket_t ticket, mu_url_t url, const char *challenge,
-		    char **pplain, mu_secret_t *psec)
-{
-  int rc = 0;
-  char arg[256];
-  
-  if (ticket == NULL || (pplain && psec))
-    return EINVAL;
-  if (pplain == NULL && psec == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-
-  if (ticket->_get_cred)
-    {
-      int res = ticket->_get_cred (ticket, url, challenge, pplain, psec);
-      if (res == 0)
-	return res;
-    }
-
-  if (pplain && ticket->plain)
-    {
-      *pplain = strdup (ticket->plain);
-      if (!*pplain)
-	return ENOMEM;
-    }
-  
-  if (psec && ticket->secret)
-    {
-      mu_secret_ref (ticket->secret);
-      *psec = ticket->secret;
-      return 0;
-    }
-  
-  if (isatty (fileno (stdin)))
-    {
-      struct termios stored_settings;
-      int echo = pplain != NULL;
-
-      printf ("%s", challenge);
-      fflush (stdout);
-      if (!echo)
-	echo_off (&stored_settings);
-      fgets (arg, sizeof (arg), stdin);
-      if (!echo)
-	{
-	  echo_on (&stored_settings);
-	  putchar ('\n');
-	  fflush (stdout);
-	}
-      arg [strlen (arg) - 1] = '\0'; /* nuke the trailing line.  */
-    }
-  else
-    return MU_ERR_FAILURE;
-  
-  if (pplain)
-    {
-      *pplain = strdup (arg);
-      if (!*pplain)
-	return ENOMEM;
-    }
-  else
-    rc = mu_secret_create (psec, arg, strlen (arg));
-  return rc;
-}
-
-void *
-mu_ticket_get_data (mu_ticket_t ticket)
-{
-  if (!ticket)
-    return NULL;
-  return ticket->data;
-}
-
-int
-mu_ticket_set_data (mu_ticket_t ticket, void *data, void *owner)
-{
-  if (ticket == NULL)
-    return EINVAL;
-  if (ticket->owner != owner)
-    return EACCES;
-  ticket->data = data;
-  return 0;
-}
diff --git a/libmailutils/url.c b/libmailutils/url.c
deleted file mode 100644
index d635157..0000000
--- a/libmailutils/url.c
+++ /dev/null
@@ -1,1140 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 1999, 2000, 2001, 2007, 2008, 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 <ctype.h>
-#include <errno.h>
-#include <stdlib.h>
-#include <string.h>
-#ifdef HAVE_STRINGS_H
-# include <strings.h>
-#endif
-
-#include <mailutils/mutil.h>
-#include <mailutils/errno.h>
-#include <mailutils/argcv.h>
-#include <mailutils/secret.h>
-#include <mailutils/cctype.h>
-#include <mailutils/cstr.h>
-#include <mailutils/sys/url.h>
-
-#define AC2(a,b) a ## b
-#define AC4(a,b,c,d) a ## b ## c ## d
-
-static int url_parse0 (mu_url_t, char *, size_t *poff, int *decode);
-
-static int
-parse_query (const char *query,
-	     char *delim,
-	     int *pargc, char ***pargv, const char **pend)
-{
-  size_t count, i;
-  char **v;
-  const char *p;
-
-  for (p = query, count = 0; ; count++)
-    {
-      size_t len = strcspn (p, delim);
-      p += len;
-      if (!*p || *p == delim[1])
-	break;
-      p++;
-    }
-
-  if (pend)
-    *pend = p;
-  if (p == query)
-    return 0;
-  count++;
-
-  v = calloc (count + 1, sizeof (v[0]));
-  for (i = 0, p = query; i < count; i++)
-    {
-      size_t len = strcspn (p, delim);
-      v[i] = mu_url_decode_len (p, len);
-      if (v[i] == NULL)
-	{
-	  mu_argcv_free (i, v);
-	  return 1;
-	}
-      p += len + 1;
-    }
-  v[i] = NULL;
-
-  *pargc = count;
-  *pargv = v;
-  return 0;
-}
-
-int
-mu_url_create (mu_url_t *purl, const char *name)
-{
-  mu_url_t url = calloc (1, sizeof (*url));
-  if (url == NULL)
-    return ENOMEM;
-
-  url->name = strdup (name);
-  if (url->name == NULL)
-    {
-      free (url);
-      return ENOMEM;
-    }
-  *purl = url;
-  return 0;
-}
-
-static char **
-argcv_copy (size_t argc, char **argv)
-{
-  size_t i;
-  char **nv = calloc (argc + 1, sizeof (nv[0]));
-  if (!nv)
-    return NULL;
-  for (i = 0; i < argc; i++)
-    if ((nv[i] = strdup (argv[i])) == NULL)
-      {
-	mu_argcv_free (i, nv);
-	free (nv);
-	return NULL;
-      }
-  return nv;
-}
-
-static int
-mu_url_copy0 (mu_url_t old_url, mu_url_t new_url)
-{
-  const char *str;
-  size_t argc;
-  char **argv;
-  int rc;
-  mu_secret_t sec;
-
-#define URLCOPY(what)						\
-  do								\
-    {								\
-      rc = AC2(mu_url_sget_,what) (old_url, &str);		\
-      if (rc == 0)						\
-	{							\
-	  if ((new_url->what = strdup (str)) == NULL)		\
-	    return ENOMEM;					\
-	}							\
-      else if (rc != MU_ERR_NOENT)				\
-	return rc;						\
-    }								\
-  while (0);
-
-  URLCOPY (scheme);
-  URLCOPY (user);
-
-  rc = mu_url_get_secret (old_url, &sec);
-  if (rc == MU_ERR_NOENT)
-    new_url->secret = NULL;
-  else if (rc)
-    return rc;
-  else
-    {
-      rc = mu_secret_dup (sec, &new_url->secret);
-      if (rc)
-	return rc;
-    }
-  
-  URLCOPY (auth);
-  URLCOPY (host);
-  new_url->port = old_url->port;
-  URLCOPY (path);
-
-  rc = mu_url_sget_fvpairs (old_url, &argc, &argv);
-  if (rc == 0 && argc)
-    {
-      if ((new_url->fvpairs = argcv_copy (argc, argv)) == NULL)
-	return ENOMEM;
-      new_url->fvcount = argc;
-    }
-
-  rc = mu_url_sget_query (old_url, &argc, &argv);
-  if (rc == 0 && argc)
-    {
-      if ((new_url->qargv = argcv_copy (argc, argv)) == NULL)
-	return ENOMEM;
-      new_url->qargc = argc;
-    }
-  return 0;
-#undef URLCOPY
-}
-
-int
-mu_url_dup (mu_url_t old_url, mu_url_t *new_url)
-{
-  mu_url_t url;
-  int rc = mu_url_create (&url, mu_url_to_string (old_url));
-
-  if (rc)
-    return rc;
-
-  rc = mu_url_copy0 (old_url, url);
-  if (rc == 0)
-    *new_url = url;
-  else
-    mu_url_destroy (&url);
-  return rc;
-}
-
-int
-mu_url_uplevel (mu_url_t url, mu_url_t *upurl)
-{
-  int rc;
-  char *p;
-  mu_url_t new_url;
-
-  if (url->_uplevel)
-    return url->_uplevel (url, upurl);
-
-  if (!url->path)
-    return MU_ERR_NOENT;
-  p = strrchr (url->path, '/');
-
-  rc = mu_url_dup (url, &new_url);
-  if (rc == 0)
-    {
-      if (!p || p == url->path)
-	{
-	  free (new_url->path);
-	  new_url->path = NULL;
-	}
-      else
-	{
-	  size_t size = p - url->path;
-	  new_url->path = realloc (new_url->path, size + 1);
-	  if (!new_url->path)
-	    {
-	      mu_url_destroy (&new_url);
-	      return ENOMEM;
-	    }
-	  memcpy (new_url->path, url->path, size);
-	  new_url->path[size] = 0;
-	}
-      *upurl = new_url;
-    }
-  return rc;
-}
-
-void
-mu_url_destroy (mu_url_t * purl)
-{
-  if (purl && *purl)
-    {
-      mu_url_t url = (*purl);
-
-      if (url->_destroy)
-	url->_destroy (url);
-
-      if (url->name)
-	free (url->name);
-
-      if (url->scheme)
-	free (url->scheme);
-
-      if (url->user)
-	free (url->user);
-
-      mu_secret_destroy (&url->secret);
-
-      if (url->auth)
-	free (url->auth);
-
-      if (url->host)
-	free (url->host);
-
-      if (url->path)
-	free (url->path);
-
-      if (url->fvcount)
-	mu_argcv_free (url->fvcount, url->fvpairs);
-
-      mu_argcv_free (url->qargc, url->qargv);
-
-      free (url);
-
-      *purl = NULL;
-    }
-}
-
-int
-mu_url_parse (mu_url_t url)
-{
-  int err = 0;
-  char *n = NULL;
-  struct _mu_url u;
-  size_t pstart;
-  mu_secret_t newsec;
-  int want_decode;
-  
-  if (!url || !url->name)
-    return EINVAL;
-
-  memset (&u, 0, sizeof u);
-  /* can't have been parsed already */
-  if (url->scheme || url->user || url->secret || url->auth ||
-      url->host || url->path || url->qargc)
-    return EINVAL;
-
-  n = strdup (url->name);
-
-  if (!n)
-    return ENOMEM;
-
-  err = url_parse0 (&u, n, &pstart, &want_decode);
-
-  if (!err)
-    {
-      if (u.secret)
-	{
-	  /* Obfuscate the password */
-#define PASS_REPL "***"
-#define PASS_REPL_LEN (sizeof (PASS_REPL) - 1)
-	  size_t plen = mu_secret_length (u.secret);
-	  size_t nlen = strlen (url->name);
-	  size_t len = nlen - plen + PASS_REPL_LEN + 1;
-	  char *newname;
-
-	  memset (url->name + pstart, 0, plen);
-	  newname = realloc (url->name, len);
-	  if (!newname)
-	    goto CLEANUP;
-	  memmove (newname + pstart + PASS_REPL_LEN, newname + pstart + plen,
-		   nlen - (pstart + plen) + 1);
-	  memcpy (newname + pstart, PASS_REPL, PASS_REPL_LEN);
-	  url->name = newname;
-	}
-
-      /* Dup the strings we found. We wouldn't have to do this
-	 if we did a single alloc of the source url name, and
-	 kept it around. It's also a good time to do hex decoding,
-	 though.
-       */
-
-#define UALLOC(X)							\
-      if (u.X && u.X[0] &&						\
-	  !(url->X = (want_decode ? mu_url_decode (u.X) : strdup (u.X)))) \
-	{								\
-	  err = ENOMEM;							\
-	  goto CLEANUP;							\
-	}								\
-      else								\
-	{								\
-	  /* Set zero-length strings to NULL. */			\
-	  u.X = NULL;							\
-	}
-
-      UALLOC (scheme);
-      UALLOC (user);
-
-      if (u.secret)
-	{
-	  char *pass = mu_url_decode (mu_secret_password (u.secret));
-	  err = mu_secret_create (&newsec, pass, strlen (pass));
-	  memset (pass, 0, strlen (pass));
-	  mu_secret_destroy (&u.secret);
-	  if (err)
-	    goto CLEANUP;
-
-	  url->secret = newsec;
-	}
-
-      UALLOC (auth);
-      UALLOC (host);
-      UALLOC (path);
-
-#undef UALLOC
-      url->fvcount = u.fvcount;
-      url->fvpairs = u.fvpairs;
-
-      url->qargc = u.qargc;
-      url->qargv = u.qargv;
-
-      url->port = u.port;
-    }
-
-CLEANUP:
-  memset (n, 0, strlen (n));
-  free (n);
-
-  if (err)
-    {
-#define UFREE(X) if (X) { free(X); X = 0; }
-
-      UFREE (url->scheme);
-      UFREE (url->user);
-      mu_secret_destroy (&u.secret);
-      UFREE (url->auth);
-      UFREE (url->host);
-      UFREE (url->path);
-      mu_argcv_free (url->fvcount, url->fvpairs);
-      mu_argcv_free (url->qargc, url->qargv);
-#undef UFREE
-    }
-
-  return err;
-}
-
-/*
-
-Syntax, condensed from RFC 1738, and extended with the ;auth=
-of RFC 2384 (for POP) and RFC 2192 (for IMAP):
-
-url =
-    scheme ":" [ "//"
-
-    [ user [ ( ":" password ) | ( ";auth=" auth ) ] "@" ]
-
-    host [ ":" port ]
-
-    [ ( "/" urlpath ) | ( "?" query ) ] ]
-
-All hell will break loose in this parser if the user/pass/auth
-portion is missing, and the urlpath has any @ or : characters
-in it. A imap mailbox, say, named after the email address of
-the person the mail is from:
-
-  imap://imap.uniserve.com/alain@qnx.com
-
-Is this required to be % quoted, though? I hope so!
-
-*/
-
-static int
-url_parse0 (mu_url_t u, char *name, size_t *poff, int *decode)
-{
-  char *start = name;
-  char *p;			/* pointer into name */
-
-  /* reject the obvious */
-  if (name == NULL)
-    return EINVAL;
-
-  if (name[0] == '/')
-    {
-      u->scheme = "file";
-      *decode = 0;
-    }
-  else if (name[0] == '|')
-    {
-      int rc;
-      u->scheme = "prog";
-      *decode = 0;
-      rc = mu_argcv_get (name + 1, NULL, NULL, &u->qargc, &u->qargv);
-      if (rc == 0)
-	{
-	  u->path = strdup (u->qargv[0]);
-	  if (!u->path)
-	    rc = ENOMEM;
-	}
-      return rc;
-    }
-  else
-    {
-      *decode = 1;
-      /* Parse out the SCHEME. */
-      p = strchr (name, ':');
-      if (p == NULL)
-	return MU_ERR_PARSE;
-
-      *p++ = 0;
-
-      u->scheme = name;
-
-      /* RFC 1738, section 2.1, lower the scheme case */
-      for (; name < p; name++)
-	*name = mu_tolower (*name);
-
-      name = p;
-    }
-
-  /* Check for nothing following the scheme. */
-  if (!*name)
-    return 0;
-
-  if (strncmp (name, "//", 2) == 0)
-    {
-      name += 2;
-
-      if (name[0] == '/')
-	{
-	  u->path = name;
-	  p = u->path + strcspn (u->path, ";?");
-	}
-      else
-	{
-	  /* Split into LHS and RHS of the '@', and then parse each side. */
-	  u->host = strchr (name, '@');
-	  if (u->host == NULL)
-	    u->host = name;
-	  else
-	    {
-	      char *pass = NULL;
-
-	      /* Parse the LHS into an identification/authentication pair. */
-	      *u->host++ = 0;
-
-	      u->user = name;
-
-	      /* Try to split the user into a:
-		 <user>:<password>
-		 or
-		 <user>:<password>;AUTH=<auth>
-	      */
-
-	      for (; *name; name++)
-		{
-		  if (*name == ':')
-		    {
-		      *name++ = 0;
-		      pass = name;
-		      *poff = pass - start;
-		    }
-		  else if (*name == ';')
-		    {
-		      /* Make sure it's the auth token. */
-		      if (mu_c_strncasecmp (name + 1, "auth=", 5) == 0)
-			{
-			  *name++ = 0;
-			  name += 5;
-			  u->auth = name;
-			  break;
-			}
-		    }
-		}
-
-	      if (pass)
-		{
-		  if (mu_secret_create (&u->secret, pass, strlen (pass)))
-		    return ENOMEM;
-		  else
-		    /* Obfuscate password */
-		    memset (pass, 0, strlen (pass));
-		}
-	    }
-
-	  /* Parse the host and port from the RHS. */
-	  p = strchr (u->host, ':');
-	  if (p)
-	    {
-	      *p++ = 0;
-	      u->port = strtol (p, &p, 10);
-
-	      /* Check for garbage after the port: we should be on the start
-		 of a path, a query, or at the end of the string. */
-	      if (*p && strcspn (p, "/?") != 0)
-		return MU_ERR_PARSE;
-	    }
-	  else
-	    p = u->host + strcspn (u->host, ";/?");
-	}
-    }
-  else
-    {
-      u->path = name;
-      p = u->path + strcspn (u->path, ";?");
-    }
-
-  /* Either way, if we're not at a nul, we're at a path or query. */
-  if (u->path == NULL && *p == '/')
-    {
-      /* found a path */
-      *p++ = 0;
-      u->path = p;
-      p = u->path + strcspn (u->path, ";?");
-    }
-
-  if (*p == ';')
-    {
-      *p++ = 0;
-      if (parse_query (p, ";?", &u->fvcount, &u->fvpairs, (const char **)&p))
-	return ENOMEM;
-    }
-
-  if (*p == '?')
-    {
-      /* found a query */
-      *p++ = 0;
-      if (parse_query (p, "&", &u->qargc, &u->qargv, NULL))
-	return ENOMEM;
-    }
-
-  return 0;
-}
-
-
-/* General accessors: */
-#define ACCESSOR(action,field) AC4(mu_url_,action,_,field)
-
-#define DECL_SGET(field)						  \
-int									  \
-ACCESSOR(sget,field) (mu_url_t url, char const **sptr)	                  \
-{									  \
-  if (url == NULL)							  \
-    return EINVAL;							  \
-  if (!url->field)							  \
-    {									  \
-      if (url->AC2(_get_,field))					  \
-	{								  \
-	  size_t n;							  \
-	  char *buf;							  \
-									  \
-	  int status = url->AC2(_get_,field) (url, NULL, 0, &n);	  \
-	  if (status)							  \
-	    return status;						  \
-									  \
-	  buf = malloc (n + 1);						  \
-	  if (!buf)							  \
-	    return ENOMEM;						  \
-									  \
-	  status = url->AC2(_get_,field) (url, buf, n + 1, NULL);	  \
-	  if (status)				          \
-	    return status;						  \
-									  \
-	  if (buf[0])                                                     \
-	    {                                                             \
-	       url->field = mu_url_decode (buf);			  \
-	       free (buf);						  \
-	    }                                                             \
-	  else                                                            \
-	    url->field = buf;                                             \
-	  if (!url->field)						  \
-	    return ENOMEM;						  \
-	}								  \
-      else								  \
-	return MU_ERR_NOENT;			                          \
-    }									  \
-  *sptr = url->field;							  \
-  return 0;								  \
-}
-
-#define DECL_GET(field)							  \
-int									  \
-ACCESSOR(get,field) (mu_url_t url, char *buf, size_t len, size_t *n)      \
-{									  \
-  size_t i;								  \
-  const char *str;							  \
-  int status = ACCESSOR(sget, field) (url, &str);			  \
-									  \
-  if (status)								  \
-    return status;							  \
-									  \
-  i = mu_cpystr (buf, str, len);					  \
-  if (n)								  \
-    *n = i;								  \
-  return 0;								  \
-}
-
-#define DECL_AGET(field)						  \
-int									  \
-ACCESSOR(aget, field) (mu_url_t url, char **buf)	                  \
-{									  \
-  const char *str;							  \
-  int status = ACCESSOR(sget, field) (url, &str);			  \
-									  \
-  if (status)								  \
-    return status;							  \
-									  \
-  if (str)								  \
-    {									  \
-      *buf = strdup (str);						  \
-      if (!*buf)							  \
-	status = ENOMEM;						  \
-    }									  \
-  else									  \
-    *buf = NULL;							  \
-  return status;							  \
-}
-
-#define DECL_CMP(field)							  \
-int									  \
-ACCESSOR(is_same,field) (mu_url_t url1, mu_url_t url2)		          \
-{									  \
-  const char *s1, *s2;							  \
-  int status1, status2;							  \
-									  \
-  status1 = ACCESSOR(sget, field) (url1, &s1);				  \
-  if (status1 && status1 != MU_ERR_NOENT)				  \
-    return 0;								  \
-  status2 = ACCESSOR(sget, field) (url2, &s2);				  \
-  if (status2 && status2 != MU_ERR_NOENT)				  \
-    return 0;								  \
-									  \
-  if (status1 || status2)						  \
-    return status1 == status2; /* Both fields are missing */		  \
-  return mu_c_strcasecmp (s1, s2) == 0;					  \
-}
-
-#define DECL_ACCESSORS(field)			                          \
-DECL_SGET(field)				                          \
-DECL_GET(field)					                          \
-DECL_AGET(field)                                                          \
-DECL_CMP(field)
-
-
-/* Declare particular accessors */
-DECL_ACCESSORS (scheme)
-DECL_ACCESSORS (user)
-DECL_ACCESSORS (auth)
-DECL_ACCESSORS (host)
-DECL_ACCESSORS (path)
-
-int
-mu_url_get_secret (const mu_url_t url, mu_secret_t *psecret)
-{
-  if (url->_get_secret)
-    return url->_get_secret (url, psecret);
-  if (url->secret == NULL)
-    return MU_ERR_NOENT;
-  mu_secret_ref (url->secret);
-  *psecret = url->secret;
-  return 0;
-}
-
-int
-mu_url_sget_query (const mu_url_t url, size_t *qc, char ***qv)
-{
-  if (url == NULL)
-    return EINVAL;
-  /* See FIXME below */
-  *qc = url->qargc;
-  *qv = url->qargv;
-  return 0;
-}
-
-int
-mu_url_aget_query (const mu_url_t url, size_t *qc, char ***qv)
-{
-  size_t qargc, i;
-  char **qargv;
-  char **qcopy;
-
-  int rc = mu_url_sget_fvpairs (url, &qargc, &qargv);
-  if (rc)
-    return rc;
-
-  qcopy = calloc (qargc + 1, sizeof (qcopy[0]));
-  if (!qcopy)
-    return errno;
-  for (i = 0; i < qargc; i++)
-    {
-      if (!(qcopy[i] = strdup (qargv[i])))
-	{
-	  mu_argcv_free (i, qcopy);
-	  return errno;
-	}
-    }
-  qcopy[i] = NULL;
-  *qc = qargc;
-  *qv = qcopy;
-  return 0;
-}
-
-/* field-value pairs accessors */
-int
-mu_url_sget_fvpairs (const mu_url_t url, size_t *fvc, char ***fvp)
-{
-  if (url == NULL)
-    return EINVAL;
-  /* FIXME: no _get_fvpairs method, but the method stuff needs to be rewritten
-     anyway */
-  *fvc = url->fvcount;
-  *fvp = url->fvpairs;
-  return 0;
-}
-
-int
-mu_url_aget_fvpairs (const mu_url_t url, size_t *pfvc, char ***pfvp)
-{
-  size_t fvc, i;
-  char **fvp;
-  char **fvcopy;
-
-  int rc = mu_url_sget_fvpairs (url, &fvc, &fvp);
-  if (rc)
-    return rc;
-
-  fvcopy = calloc (fvc + 1, sizeof (fvcopy[0]));
-  if (!fvcopy)
-    return errno;
-  for (i = 0; i < fvc; i++)
-    {
-      if (!(fvcopy[i] = strdup (fvp[i])))
-	{
-	  mu_argcv_free (i, fvcopy);
-	  return errno;
-	}
-    }
-  fvcopy[i] = NULL;
-  *pfvc = fvc;
-  *pfvp = fvcopy;
-  return 0;
-}
-
-int
-mu_url_get_port (const mu_url_t url, long *pport)
-{
-  if (url == NULL)
-    return EINVAL;
-  if (url->_get_port)
-    return url->_get_port (url, pport);
-  *pport = url->port;
-  return 0;
-}
-
-const char *
-mu_url_to_string (const mu_url_t url)
-{
-  if (url == NULL || url->name == NULL)
-    return "";
-  return url->name;
-}
-
-int
-mu_url_set_scheme (mu_url_t url, const char *scheme)
-{
-  char *p;
-  if (!url || !scheme)
-    return EINVAL;
-  p = realloc (url->scheme, strlen (scheme) + 1);
-  if (!p)
-    return ENOMEM;
-  strcpy (url->scheme, scheme);
-  return 0;
-}
-
-int
-mu_url_is_scheme (mu_url_t url, const char *scheme)
-{
-  if (url && scheme && url->scheme 
-      && mu_c_strcasecmp (url->scheme, scheme) == 0)
-    return 1;
-
-  return 0;
-}
-
-int
-mu_url_is_same_port (mu_url_t url1, mu_url_t url2)
-{
-  long p1 = 0, p2 = 0;
-
-  mu_url_get_port (url1, &p1);
-  mu_url_get_port (url2, &p2);
-  return (p1 == p2);
-}
-
-/* From RFC 1738, section 2.2 */
-char *
-mu_url_decode_len (const char *s, size_t len)
-{
-  char *d;
-  const char *eos = s + len;
-  int i;
-
-  d = malloc (len + 1);
-  if (!d)
-    return NULL;
-
-  for (i = 0; s < eos; i++)
-    {
-      if (*s != '%')
-	{
-	  d[i] = *s;
-	  s++;
-	}
-      else
-	{
-	  unsigned long ul = 0;
-
-	  s++;
-
-	  /* don't check return value, it's correctly coded, or it's not,
-	     in which case we just skip the garbage, this is a decoder,
-	     not an AI project */
-
-	  mu_hexstr2ul (&ul, s, 2);
-
-	  s += 2;
-
-	  d[i] = (char) ul;
-	}
-    }
-
-  d[i] = 0;
-
-  return d;
-}
-
-char *
-mu_url_decode (const char *s)
-{
-  return mu_url_decode_len (s, strlen (s));
-}
-
-static int
-defined (const char *s)
-{
-  if (s && strcmp ("*", s) != 0)
-    return 1;
-  return 0;
-}
-
-int
-mu_url_is_ticket (mu_url_t ticket, mu_url_t url)
-{
-  if (!ticket || !url)
-    return 0;
-
-  /* If ticket has a scheme, host, port, or path, then the queries
-     equivalent must be defined and match. */
-  if (defined (ticket->scheme))
-    {
-      if (!url->scheme || mu_c_strcasecmp (ticket->scheme, url->scheme) != 0)
-	return 0;
-    }
-  if (defined (ticket->host))
-    {
-      if (!url->host || mu_c_strcasecmp (ticket->host, url->host) != 0)
-	return 0;
-    }
-  if (ticket->port && ticket->port != url->port)
-    return 0;
-  /* If ticket has a user or pass, but url doesn't, that's OK, we were
-     urling for this info. But if url does have a user/pass, it
-     must match the ticket. */
-  if (url->user)
-    {
-      if (defined (ticket->user) && strcmp (ticket->user, url->user) != 0)
-	return 0;
-    }
-
-  /* Guess it matches. */
-  return 1;
-}
-
-int
-mu_url_init (mu_url_t url, int port, const char *scheme)
-{
-  int status = 0;
-
-  url->_destroy = NULL;
-
-  status = mu_url_parse (url);
-  if (status)
-    return status;
-
-  if (!mu_url_is_scheme (url, scheme))
-    return EINVAL;
-
-  if (url->port == 0)
-    url->port = port;
-
-  return status;
-}
-
-/* Default mailbox path generator */
-static char *
-_url_path_default (const char *spooldir, const char *user, int unused)
-{
-  char *mbox = malloc (strlen (spooldir) + strlen (user) + 2);
-  if (!mbox)
-    errno = ENOMEM;
-  else
-    sprintf (mbox, "%s/%s", spooldir, user);
-  return mbox;
-}
-
-/* Hashed indexing */
-static char *
-_url_path_hashed (const char *spooldir, const char *user, int param)
-{
-  int i;
-  int ulen = strlen (user);
-  char *mbox;
-  unsigned hash;
-
-  if (param > ulen)
-    param = ulen;
-  for (i = 0, hash = 0; i < param; i++)
-    hash += user[i];
-
-  mbox = malloc (ulen + strlen (spooldir) + 5);
-  sprintf (mbox, "%s/%02X/%s", spooldir, hash % 256, user);
-  return mbox;
-}
-
-static int transtab[] = {
-  'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
-  'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
-  'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
-  'y', 'z', 'a', 'b', 'c', 'd', 'e', 'f',
-  'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
-  'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
-  'w', 'x', 'y', 'z', 'a', 'b', 'c', 'd',
-  'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
-  'm', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
-  'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
-  'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
-  'x', 'y', 'z', 'b', 'c', 'd', 'e', 'f',
-  'g', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
-  'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
-  'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
-  'x', 'y', 'z', 'b', 'c', 'd', 'e', 'f',
-  'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
-  'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
-  'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
-  'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
-  'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
-  'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
-  'x', 'y', 'z', 'a', 'b', 'c', 'd', 'e',
-  'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
-  'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
-  'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
-  'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
-  'y', 'z', 'b', 'c', 'd', 'e', 'f', 'g',
-  'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
-  'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
-  'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
-  'y', 'z', 'b', 'c', 'd', 'e', 'f', 'g'
-};
-
-/* Forward Indexing */
-static char *
-_url_path_index (const char *spooldir, const char *iuser, int index_depth)
-{
-  const unsigned char* user = (const unsigned char*) iuser;
-  int i, ulen = strlen (iuser);
-  char *mbox, *p;
-
-  if (ulen == 0)
-    return NULL;
-
-  mbox = malloc (ulen + strlen (spooldir) + 2*index_depth + 2);
-  strcpy (mbox, spooldir);
-  p = mbox + strlen (mbox);
-  for (i = 0; i < index_depth && i < ulen; i++)
-    {
-      *p++ = '/';
-      *p++ = transtab[ user[i] ];
-    }
-  for (; i < index_depth; i++)
-    {
-      *p++ = '/';
-      *p++ = transtab[ user[ulen-1] ];
-    }
-  *p++ = '/';
-  strcpy (p, iuser);
-  return mbox;
-}
-
-/* Reverse Indexing */
-static char *
-_url_path_rev_index (const char *spooldir, const char *iuser, int index_depth)
-{
-  const unsigned char* user = (const unsigned char*) iuser;
-  int i, ulen = strlen (iuser);
-  char *mbox, *p;
-
-  if (ulen == 0)
-    return NULL;
-
-  mbox = malloc (ulen + strlen (spooldir) + 2*index_depth + 1);
-  strcpy (mbox, spooldir);
-  p = mbox + strlen (mbox);
-  for (i = 0; i < index_depth && i < ulen; i++)
-    {
-      *p++ = '/';
-      *p++ = transtab[ user[ulen - i - 1] ];
-    }
-  for (; i < index_depth; i++)
-    {
-      *p++ = '/';
-      *p++ = transtab[ user[0] ];
-    }
-  *p++ = '/';
-  strcpy (p, iuser);
-  return mbox;
-}
-
-static int
-rmselector (const char *p, void *data MU_ARG_UNUSED)
-{
-  return strncmp (p, "type=", 5) == 0
-	 || strncmp (p, "user=", 5) == 0
-	 || strncmp (p, "param=", 6) == 0;
-}
-
-int
-mu_url_expand_path (mu_url_t url)
-{
-  size_t i;
-  char *user = NULL;
-  int param = 0;
-  char *p;
-  char *(*fun) (const char *, const char *, int) = _url_path_default;
-
-  if (url->fvcount == 0)
-    return 0;
-
-  for (i = 0; i < url->fvcount; i++)
-    {
-      p = url->fvpairs[i];
-      if (strncmp (p, "type=", 5) == 0)
-	{
-	  char *type = p + 5;
-
-	  if (strcmp (type, "hash") == 0)
-	    fun = _url_path_hashed;
-	  else if (strcmp (type, "index") == 0)
-	    fun = _url_path_index;
-	  else if (strcmp (type, "rev-index") == 0)
-	    fun = _url_path_rev_index;
-	  else
-	    return MU_ERR_NOENT;
-	}
-      else if (strncmp (p, "user=", 5) == 0)
-	{
-	  user = p + 5;
-	}
-      else if (strncmp (p, "param=", 6) == 0)
-	{
-	  param = strtoul (p + 6, NULL, 0);
-	}
-    }
-
-  if (user)
-    {
-      char *p = fun (url->path, user, param);
-      if (p)
-	{
-	  free (url->path);
-	  url->path = p;
-	}
-      mu_argcv_remove (&url->fvcount, &url->fvpairs, rmselector, NULL);
-    }
-  else
-    return MU_ERR_NOENT;
-
-  return 0;
-}
diff --git a/libmailutils/vasnprintf.c b/libmailutils/vasnprintf.c
deleted file mode 100644
index 0f4350e..0000000
--- a/libmailutils/vasnprintf.c
+++ /dev/null
@@ -1,87 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 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 */
-
-#if HAVE_CONFIG_H
-# include <config.h>
-#endif
-
-#include <stdlib.h>
-#include <stdarg.h>
-#include <stdio.h>
-#include <errno.h>
-#include <string.h>
-
-#include <mailutils/io.h>
-
-int
-mu_vasnprintf (char **pbuf, size_t *psize, const char *fmt, va_list ap)
-{
-  char *buf = *pbuf;
-  size_t buflen = *psize;
-  int rc = 0;
-  
-  if (!buf)
-    {
-      if (buflen == 0)
-	buflen = 512; /* Initial allocation */
-      
-      buf = calloc (1, buflen);
-      if (buf == NULL)
-	return ENOMEM;
-    }
-  
-  for (;;)
-    {
-      ssize_t n = vsnprintf (buf, buflen, fmt, ap);
-      if (n < 0 || n >= buflen || !memchr (buf, '\0', n + 1))
-	{
-	  char *newbuf;
-	  size_t newlen = buflen * 2;
-	  if (newlen < buflen)
-	    {
-	      rc = ENOMEM;
-	      break;
-	    }
-	  newbuf = realloc (buf, newlen);
-	  if (newbuf == NULL)
-	    {
-	      rc = ENOMEM;
-	      break;
-	    }
-	  buflen = newlen;
-	  buf = newbuf;
-	}
-      else
-	break;
-    }
-
-  if (rc)
-    {
-      if (!*pbuf)
-	{
-	  /* We made first allocation, now free it */
-	  free (buf);
-	  buf = NULL;
-	  buflen = 0;
-	}
-    }
-
-  *pbuf = buf;
-  *psize = buflen;
-  return rc;
-}
diff --git a/libmailutils/version.c b/libmailutils/version.c
deleted file mode 100644
index 1967dab..0000000
--- a/libmailutils/version.c
+++ /dev/null
@@ -1,189 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 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 <mailutils/nls.h>
-#include <mailutils/version.h>
-#include <mailutils/cstr.h>
-#include <stdio.h>
-#include <string.h>
-
-#include <confpaths.h>
-
-char *mu_license_text =
- N_("   GNU Mailutils is free software; you can redistribute it and/or modify\n"
-    "   it under the terms of the GNU General Public License as published by\n"
-    "   the Free Software Foundation; either version 3 of the License, or\n"
-    "   (at your option) any later version.\n"
-    "\n"
-    "   GNU Mailutils is distributed in the hope that it will be useful,\n"
-    "   but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
-    "   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
-    "   GNU General Public License for more details.\n"
-    "\n"
-    "   You should have received a copy of the GNU General Public License along\n"
-    "   with GNU Mailutils; if not, write to the Free Software Foundation,\n"
-    "   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n"
-    "\n"
-    "\n"
-);
-
-static struct mu_conf_option mu_conf_option[] = {
-  { "VERSION=" VERSION, N_("Version of this package") },
-  { "SYSCONFDIR=" SYSCONFDIR, N_("System configuration directory") },
-  { "MAILSPOOLDIR=" MU_PATH_MAILDIR, N_("Default mail spool directory") },
-  { "SCHEME=" MU_DEFAULT_SCHEME, N_("Default mailbox type") },
-  { "LOG_FACILITY=" LOG_FACILITY_STRING, N_("Default syslog facility") },
-#ifdef USE_LIBPAM
-  { "USE_LIBPAM", N_("PAM support") },
-#endif
-#ifdef HAVE_LIBLTDL
-  { "HAVE_LIBLTDL", N_("a portable `dlopen' wrapper library") },
-#endif
-#ifdef WITH_BDB2
-  { "WITH_BDB2", N_("Berkeley DB v. 2") },
-#endif
-#ifdef WITH_NDBM
-  { "WITH_NDBM", },
-#endif
-#ifdef WITH_OLD_DBM
-  { "WITH_OLD_DBM", N_("Old DBM support") },
-#endif
-#ifdef WITH_GDBM
-  { "WITH_GDBM", N_("GNU DBM") },
-#endif
-#ifdef WITH_TOKYOCABINET
-  { "WITH_TOKYOCABINET", N_("Tokyo Cabinet DBM") },
-#endif
-#ifdef WITH_GNUTLS
-  { "WITH_GNUTLS", N_("TLS support using GNU TLS") },
-#endif
-#ifdef WITH_GSASL
-  { "WITH_GSASL", N_("SASL support using GNU SASL") },
-#endif
-#ifdef WITH_GSSAPI
-  { "WITH_GSSAPI", N_("GSSAPI support") },
-#endif
-#ifdef WITH_GUILE
-  { "WITH_GUILE", N_("Support for Guile as extension language") },
-#endif
-#ifdef WITH_PYTHON
-  { "WITH_PYTHON", N_("Support for Python as extension language") },
-#endif
-#ifdef WITH_PTHREAD
-  { "WITH_PTHREAD", N_("Support for POSIX threads") },
-#endif
-#ifdef WITH_READLINE
-  { "WITH_READLINE", N_("GNU Readline") },
-#endif
-#ifdef HAVE_MYSQL
-  { "HAVE_MYSQL", N_("MySQL") },
-#endif
-#ifdef HAVE_PGSQL
-  { "HAVE_PGSQL", N_("PostgreSQL") },
-#endif
-#ifdef WITH_LDAP
-  { "WITH_LDAP", },
-#endif
-#ifdef WITH_LIBWRAP
-  { "WITH_LIBWRAP", N_("Support for TCP wrappers") },
-#endif
-#ifdef ENABLE_VIRTUAL_DOMAINS
-  { "ENABLE_VIRTUAL_DOMAINS", N_("Support for virtual mail domains") },
-#endif
-#ifdef ENABLE_IMAP
-  { "ENABLE_IMAP", N_("IMAP4 protocol support") },
-#endif
-#ifdef ENABLE_POP
-  { "ENABLE_POP",  N_("POP3 protocol support") },
-#endif
-#ifdef ENABLE_MH
-  { "ENABLE_MH", N_("MH mail storage support") },
-#endif
-#ifdef ENABLE_MAILDIR
-  { "ENABLE_MAILDIR", N_("Maildir mail storage support") },
-#endif
-#ifdef ENABLE_SMTP
-  { "ENABLE_SMTP", N_("SMTP protocol support") },
-#endif
-#ifdef ENABLE_SENDMAIL
-  { "ENABLE_SENDMAIL", N_("Sendmail command line interface support")},
-#endif
-#ifdef ENABLE_NNTP
-  { "ENABLE_NNTP", N_("NNTP protocol support") },
-#endif
-#ifdef ENABLE_RADIUS
-  { "ENABLE_RADIUS", N_("RADIUS protocol support") },
-#endif
-#ifdef WITH_INCLUDED_LIBINTL
-  { "WITH_INCLUDED_LIBINTL", N_("GNU libintl compiled in") },
-#endif
-  { NULL }
-};
-
-void
-mu_fprint_conf_option (FILE *fp, const struct mu_conf_option *opt, int verbose)
-{
-  fprintf (fp, "%s", opt->name);
-  if (verbose && opt->descr)
-    fprintf (fp, " \t- %s", _(opt->descr));
-  fputc('\n', fp);
-}
-
-void
-mu_fprint_options (FILE *fp, int verbose)
-{
-  int i;
-  
-  for (i = 0; mu_conf_option[i].name; i++)
-    mu_fprint_conf_option (fp, mu_conf_option + i, verbose);
-}
-
-void
-mu_print_options ()
-{
-  mu_fprint_options (stdout, 1);
-}
-
-const struct mu_conf_option *
-mu_check_option (char *name)
-{
-  int i;
-  
-  for (i = 0; mu_conf_option[i].name; i++)
-    {
-      int len;
-      char *q, *p = strchr (mu_conf_option[i].name, '=');
-      if (p)
-	len = p - mu_conf_option[i].name;
-      else
-	len = strlen (mu_conf_option[i].name);
-
-      if (mu_c_strncasecmp (mu_conf_option[i].name, name, len) == 0)
-	return &mu_conf_option[i];
-      else if ((q = strchr (mu_conf_option[i].name, '_')) != NULL
-	       && mu_c_strncasecmp (q + 1, name,
-			       len - (q - mu_conf_option[i].name) - 1) == 0)
-	return &mu_conf_option[i];
-    }
-  return NULL;
-}  
-
diff --git a/libmailutils/wicket.c b/libmailutils/wicket.c
deleted file mode 100644
index 8b0fbbb..0000000
--- a/libmailutils/wicket.c
+++ /dev/null
@@ -1,357 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 1999, 2000, 2001, 2003, 2004, 2005, 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 <errno.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <pwd.h>
-#include <string.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <unistd.h>
-#include <ctype.h>
-
-#include <mailutils/errno.h>
-#include <mailutils/mutil.h>
-#include <mailutils/mu_auth.h>
-#include <mailutils/stream.h>
-
-#include <mailutils/sys/auth.h>
-#include <mailutils/sys/url.h>
-
-int
-mu_wicket_create (mu_wicket_t *pwicket)
-{
-  mu_wicket_t wicket = calloc (1, sizeof (*wicket));
-  if (!wicket)
-    return ENOMEM;
-  wicket->refcnt = 1;
-  *pwicket = wicket;
-  return 0;
-}
-
-int
-mu_wicket_get_ticket (mu_wicket_t wicket, const char *user, mu_ticket_t *pticket)
-{
-  if (!wicket)
-    return EINVAL;
-  if (!pticket)
-    return EINVAL;
-  if (!wicket->_get_ticket)
-    return ENOSYS;
-  return wicket->_get_ticket (wicket, wicket->data, user, pticket);
-}
-
-int
-mu_wicket_ref (mu_wicket_t wicket)
-{
-  if (!wicket)
-    return EINVAL;
-  wicket->refcnt++;
-  return 0;
-}
-
-int
-mu_wicket_unref (mu_wicket_t wicket)
-{
-  if (!wicket)
-    return EINVAL;
-  if (wicket->refcnt)
-    wicket->refcnt--;
-  if (wicket->refcnt == 0)
-    {
-      if (wicket->_destroy)
-	wicket->_destroy (wicket);
-      free (wicket);
-      return 0;
-    }
-  return MU_ERR_EXISTS;
-}
-
-
-void
-mu_wicket_destroy (mu_wicket_t *pwicket)
-{
-  if (pwicket && *pwicket && mu_wicket_unref (*pwicket) == 0)
-    *pwicket = NULL;
-}
-
-int
-mu_wicket_set_destroy (mu_wicket_t wicket, void (*_destroy) (mu_wicket_t))
-{
-  if (!wicket)
-    return EINVAL;
-  wicket->_destroy = _destroy;
-  return 0;
-}
-
-int
-mu_wicket_set_data (mu_wicket_t wicket, void *data)
-{
-  if (!wicket)
-    return EINVAL;
-  wicket->data = data;
-  return 0;
-}
-
-void *
-mu_wicket_get_data (mu_wicket_t wicket)
-{
-  if (!wicket)
-    return NULL;
-  return wicket->data;
-}
-
-int
-mu_wicket_set_get_ticket (mu_wicket_t wicket,
-			  int (*_get_ticket) (mu_wicket_t, void *,
-					      const char *, mu_ticket_t *))
-{
-  if (!wicket)
-    return EINVAL;
-  wicket->_get_ticket = _get_ticket;
-  return 0;
-}
-
-
-/* A "file wicket" implementation */
-
-struct file_wicket
-{
-  char *filename;
-};
-
-static void
-_file_wicket_destroy (mu_wicket_t wicket)
-{
-  struct file_wicket *fw = mu_wicket_get_data (wicket);
-  free (fw->filename);
-  free (fw);
-}
-
-struct file_ticket
-{
-  char *filename;
-  char *user;
-  mu_url_t tickurl;
-};
-
-static void
-file_ticket_destroy (mu_ticket_t ticket)
-{
-  struct file_ticket *ft = mu_ticket_get_data (ticket);
-  if (ft)
-    {
-      free (ft->filename);
-      free (ft->user);
-      mu_url_destroy (&ft->tickurl);
-      free (ft);
-    }
-}
-
-static int get_ticket_url (mu_ticket_t ticket, mu_url_t url, mu_url_t *pticket_url);
-
-int
-file_ticket_get_cred (mu_ticket_t ticket, mu_url_t url, const char *challenge,
-		      char **pplain, mu_secret_t *psec)
-{
-  struct file_ticket *ft = mu_ticket_get_data (ticket);
-
-  if (!ft->tickurl)
-    {
-      int rc = get_ticket_url (ticket, url, &ft->tickurl);
-      if (rc)
-	return rc;
-    }
-  if (pplain)
-    {
-      if (ft->user)
-	{
-	  *pplain = strdup (ft->user);
-	  if (!*pplain)
-	    return ENOMEM;
-	}
-      else
-	return mu_url_aget_user (ft->tickurl, pplain);
-    }
-  else
-    return mu_url_get_secret (ft->tickurl, psec);
-}
-
-static int
-_file_wicket_get_ticket (mu_wicket_t wicket, void *data,
-			 const char *user, mu_ticket_t *pticket)
-{
-  int rc;
-  mu_ticket_t ticket;
-  struct file_wicket *fw = data;
-  struct file_ticket *ft = calloc (1, sizeof (*ft));
-  ft->filename = strdup (fw->filename);
-  if (!ft->filename)
-    {
-      free (ft);
-      return ENOMEM;
-    }
-  if (user)
-    {
-      ft->user = strdup (user);
-      if (!ft->user)
-	{
-	  free (ft->filename);
-	  free (ft);
-	  return ENOMEM;
-	}
-    }
-  else
-    ft->user = NULL;
-
-  rc = mu_ticket_create (&ticket, NULL);
-  if (rc)
-    {
-      free (ft->filename);
-      free (ft->user);
-      free (ft);
-      return rc;
-    }
-	
-  mu_ticket_set_destroy (ticket, file_ticket_destroy, NULL);
-  mu_ticket_set_data (ticket, ft, NULL);
-  mu_ticket_set_get_cred (ticket, file_ticket_get_cred, NULL);
-
-  *pticket = ticket;
-  return 0;
-}
-  
-static int
-get_ticket_url (mu_ticket_t ticket, mu_url_t url, mu_url_t *pticket_url)
-{
-  mu_stream_t stream;
-  struct file_ticket *ft = mu_ticket_get_data (ticket);
-  int rc;
-  mu_url_t u = NULL;
-  char *buf = NULL;
-  size_t bufsize = 0;
-  size_t len;
-  
-  rc = mu_file_stream_create (&stream, ft->filename, MU_STREAM_READ);
-  if (rc)
-    return rc;
-    
-  while ((rc = mu_stream_getline (stream, &buf, &bufsize, &len)) == 0
-	 && len > 0)
-    {
-      char *p;
-      int err;
-      
-      /* Truncate a trailing newline. */
-      if (len && buf[len - 1] == '\n')
-	buf[--len] = 0;
-      
-      /* Skip leading spaces  */
-      for (p = buf; *p == ' ' || *p == '\t'; p++)
-	;
-      /* Skip trailing spaces */
-      for (; len > 0 && (p[len-1] == ' ' || p[len-1] == '\t'); )
-	p[--len] = 0;
-      
-      /* Skip empty lines and comments. */
-      if (*p == 0 || *p == '#')
-	continue;
-      
-      if ((err = mu_url_create (&u, p)) != 0)
-	{
-	  /* Skip erroneous entry */
-	  /* FIXME: Error message */
-	  continue;
-	}
-      if ((err = mu_url_parse (u)) != 0)
-	{
-	  /* FIXME: See above */
-	  mu_url_destroy (&u);
-	  continue;
-	}
-      
-      if (!mu_url_is_ticket (u, url))
-	{
-	  mu_url_destroy (&u);
-	  continue;
-	}
-      
-      if (ft->user)
-	{
-	  if (u->name && strcmp (u->name, "*") != 0
-	      && strcmp (ft->user, u->name) != 0)
-	    {
-	      mu_url_destroy (&u);
-	      continue;
-	    }
-	}
-      
-      break;
-    }
-  mu_stream_close (stream);
-  free (buf);
-    
-  mu_stream_destroy (&stream);
-
-  if (rc == 0)
-    {
-      if (u)
-	*pticket_url = u;
-      else
-	rc = MU_ERR_NOENT;
-    }
-  
-  return rc;
-}
-
-int
-mu_file_wicket_create (mu_wicket_t *pwicket, const char *filename)
-{
-  mu_wicket_t wicket;
-  int rc;
-  struct file_wicket *fw = calloc (1, sizeof (*fw));
-
-  if (!fw)
-    return ENOMEM;
-  fw->filename = strdup (filename);
-  if (!fw->filename)
-    {
-      free (fw);
-      return ENOMEM;
-    }
-  
-  rc = mu_wicket_create (&wicket);
-  if (rc)
-    {
-      free (fw->filename);
-      free (fw);
-      return rc;
-    }
-  mu_wicket_set_data (wicket, fw);
-  mu_wicket_set_destroy (wicket, _file_wicket_destroy);
-  mu_wicket_set_get_ticket (wicket, _file_wicket_get_ticket);
-  *pwicket = wicket;
-  return 0;
-}
-
diff --git a/libmu_argp/Makefile.am b/libmu_argp/Makefile.am
index 93c26d2..1045562 100644
--- a/libmu_argp/Makefile.am
+++ b/libmu_argp/Makefile.am
@@ -12,9 +12,7 @@
 # General Public License for more details.
 #
 # You should have received a copy of the GNU General Public License
-# along with GNU Mailutils; if not, write to the Free Software
-# Foundation, Inc.  51 Franklin Street, Fifth Floor, Boston, MA
-# 02110-1301 USA
+# along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. 
 
 # This library uses gnulib calls and is always linked with gnulib, therefore
 # we use application includes here.
diff --git a/libmu_argp/auth.c b/libmu_argp/auth.c
index 929912a..86d48cf 100644
--- a/libmu_argp/auth.c
+++ b/libmu_argp/auth.c
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libmu_argp/cmdline.c b/libmu_argp/cmdline.c
index 61b217f..26f84fa 100644
--- a/libmu_argp/cmdline.c
+++ b/libmu_argp/cmdline.c
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libmu_argp/cmdline.h b/libmu_argp/cmdline.h
index fecb043..651ff0f 100644
--- a/libmu_argp/cmdline.h
+++ b/libmu_argp/cmdline.h
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #include "mailutils/libcfg.h"
 #include "mailutils/libargp.h"
diff --git a/libmu_argp/common.c b/libmu_argp/common.c
index aa481de..a76e378 100644
--- a/libmu_argp/common.c
+++ b/libmu_argp/common.c
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libmu_argp/mu_argp.c b/libmu_argp/mu_argp.c
index 75618f6..ec854e9 100644
--- a/libmu_argp/mu_argp.c
+++ b/libmu_argp/mu_argp.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libmu_argp/muinit.c b/libmu_argp/muinit.c
index 85b65ed..413389b 100644
--- a/libmu_argp/muinit.c
+++ b/libmu_argp/muinit.c
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libmu_argp/sieve.c b/libmu_argp/sieve.c
index 2bff3d3..5a39c31 100644
--- a/libmu_argp/sieve.c
+++ b/libmu_argp/sieve.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libmu_argp/tls.c b/libmu_argp/tls.c
index b516062..408ec8b 100644
--- a/libmu_argp/tls.c
+++ b/libmu_argp/tls.c
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libmu_auth/Makefile.am b/libmu_auth/Makefile.am
index 2533ffb..a239cd2 100644
--- a/libmu_auth/Makefile.am
+++ b/libmu_auth/Makefile.am
@@ -1,22 +1,19 @@
-## Process this file with GNU Automake to create Makefile.in
-
-##   Copyright (C) 2002, 2003, 2005, 2006, 2010 Free Software
-##   Foundation, Inc.
+## This file is part of GNU Mailutils.
+## Copyright (C) 2002, 2003, 2005, 2006, 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.
+## 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.
 ##
-##   GNU Mailutils 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.
+## GNU Mailutils 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 GNU Mailutils; if not, write to the Free Software
-##   Foundation, Inc.  51 Franklin Street, Fifth Floor, Boston, MA
-##   02110-1301 USA
+## You should have received a copy of the GNU General Public License
+## along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
 INCLUDES = @MU_LIB_COMMON_INCLUDES@ 
 AM_CFLAGS = -DSITE_VIRTUAL_PWDDIR=\"@SITE_VIRTUAL_PWDDIR@\" \
diff --git a/libmu_auth/gsasl.c b/libmu_auth/gsasl.c
index c4e98f5..10b14bf 100644
--- a/libmu_auth/gsasl.c
+++ b/libmu_auth/gsasl.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #if HAVE_CONFIG_H
 # include <config.h>
diff --git a/libmu_auth/ldap.c b/libmu_auth/ldap.c
index 419ea6c..f29472e 100644
--- a/libmu_auth/ldap.c
+++ b/libmu_auth/ldap.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libmu_auth/pam.c b/libmu_auth/pam.c
index ff66c43..5ea88a3 100644
--- a/libmu_auth/pam.c
+++ b/libmu_auth/pam.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libmu_auth/radius.c b/libmu_auth/radius.c
index b848b42..048a9c7 100644
--- a/libmu_auth/radius.c
+++ b/libmu_auth/radius.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libmu_auth/sql.c b/libmu_auth/sql.c
index 87e3f60..e84911a 100644
--- a/libmu_auth/sql.c
+++ b/libmu_auth/sql.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libmu_auth/sql.h b/libmu_auth/sql.h
index 3a6696d..985375a 100644
--- a/libmu_auth/sql.h
+++ b/libmu_auth/sql.h
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #include <sys/types.h>
 #include <stdlib.h>
diff --git a/libmu_auth/tls.c b/libmu_auth/tls.c
index e6e4551..01633c5 100644
--- a/libmu_auth/tls.c
+++ b/libmu_auth/tls.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #if HAVE_CONFIG_H
 # include <config.h>
diff --git a/libmu_auth/virtual.c b/libmu_auth/virtual.c
index 1932ea4..ad15352 100644
--- a/libmu_auth/virtual.c
+++ b/libmu_auth/virtual.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library. If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libmu_cfg/Makefile.am b/libmu_cfg/Makefile.am
index 2baa59f..d3ddabb 100644
--- a/libmu_cfg/Makefile.am
+++ b/libmu_cfg/Makefile.am
@@ -12,9 +12,7 @@
 # General Public License for more details.
 #
 # You should have received a copy of the GNU General Public License
-# along with GNU Mailutils; if not, write to the Free Software
-# Foundation, Inc.  51 Franklin Street, Fifth Floor, Boston, MA
-# 02110-1301 USA
+# along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
 INCLUDES = @MU_LIB_COMMON_INCLUDES@ 
 AM_CFLAGS = -DSITE_VIRTUAL_PWDDIR=\"@SITE_VIRTUAL_PWDDIR@\" \
diff --git a/libmu_cfg/acl.c b/libmu_cfg/acl.c
index d9b769c..0ad60e4 100644
--- a/libmu_cfg/acl.c
+++ b/libmu_cfg/acl.c
@@ -6,13 +6,13 @@
    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
+   GNU Mailutils 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, see <http://www.gnu.org/licenses/>.
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #ifdef HAVE_CONFIG_H
diff --git a/libmu_cfg/auth.c b/libmu_cfg/auth.c
index dde04f6..d19c8e3 100644
--- a/libmu_cfg/auth.c
+++ b/libmu_cfg/auth.c
@@ -6,13 +6,13 @@
    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
+   GNU Mailutils 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, see <http://www.gnu.org/licenses/>.
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #ifdef HAVE_CONFIG_H
diff --git a/libmu_cfg/common.c b/libmu_cfg/common.c
index 17c39c4..ec59b60 100644
--- a/libmu_cfg/common.c
+++ b/libmu_cfg/common.c
@@ -6,13 +6,13 @@
    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
+   GNU Mailutils 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, see <http://www.gnu.org/licenses/>.
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #ifdef HAVE_CONFIG_H
diff --git a/libmu_cfg/gsasl.c b/libmu_cfg/gsasl.c
index cd37d46..7d6690b 100644
--- a/libmu_cfg/gsasl.c
+++ b/libmu_cfg/gsasl.c
@@ -6,13 +6,13 @@
    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
+   GNU Mailutils 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, see <http://www.gnu.org/licenses/>.
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #ifdef HAVE_CONFIG_H
diff --git a/libmu_cfg/init.c b/libmu_cfg/init.c
index d9f84d6..1dcaa77 100644
--- a/libmu_cfg/init.c
+++ b/libmu_cfg/init.c
@@ -6,13 +6,13 @@
    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
+   GNU Mailutils 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, see <http://www.gnu.org/licenses/>.
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #ifdef HAVE_CONFIG_H
diff --git a/libmu_cfg/ldap.c b/libmu_cfg/ldap.c
index ea59a49..953fa3e 100644
--- a/libmu_cfg/ldap.c
+++ b/libmu_cfg/ldap.c
@@ -6,13 +6,13 @@
    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
+   GNU Mailutils 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, see <http://www.gnu.org/licenses/>.
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #ifdef HAVE_CONFIG_H
diff --git a/libmu_cfg/pam.c b/libmu_cfg/pam.c
index 502e306..4cd2fab 100644
--- a/libmu_cfg/pam.c
+++ b/libmu_cfg/pam.c
@@ -6,13 +6,13 @@
    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
+   GNU Mailutils 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, see <http://www.gnu.org/licenses/>.
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #ifdef HAVE_CONFIG_H
diff --git a/libmu_cfg/radius.c b/libmu_cfg/radius.c
index 62f6960..4f81efd 100644
--- a/libmu_cfg/radius.c
+++ b/libmu_cfg/radius.c
@@ -6,13 +6,13 @@
    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
+   GNU Mailutils 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, see <http://www.gnu.org/licenses/>.
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #ifdef HAVE_CONFIG_H
diff --git a/libmu_cfg/sieve.c b/libmu_cfg/sieve.c
index 5b4a5ed..911cedc 100644
--- a/libmu_cfg/sieve.c
+++ b/libmu_cfg/sieve.c
@@ -6,13 +6,13 @@
    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
+   GNU Mailutils 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, see <http://www.gnu.org/licenses/>.
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #ifdef HAVE_CONFIG_H
diff --git a/libmu_cfg/sql.c b/libmu_cfg/sql.c
index 360448d..daf91e9 100644
--- a/libmu_cfg/sql.c
+++ b/libmu_cfg/sql.c
@@ -6,13 +6,13 @@
    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
+   GNU Mailutils 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, see <http://www.gnu.org/licenses/>.
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #ifdef HAVE_CONFIG_H
diff --git a/libmu_cfg/tls.c b/libmu_cfg/tls.c
index 3e7cf38..16e80d3 100644
--- a/libmu_cfg/tls.c
+++ b/libmu_cfg/tls.c
@@ -6,13 +6,13 @@
    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
+   GNU Mailutils 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, see <http://www.gnu.org/licenses/>.
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #ifdef HAVE_CONFIG_H
diff --git a/libmu_cfg/virtdomain.c b/libmu_cfg/virtdomain.c
index 7c3217d..b114d9b 100644
--- a/libmu_cfg/virtdomain.c
+++ b/libmu_cfg/virtdomain.c
@@ -6,13 +6,13 @@
    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
+   GNU Mailutils 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, see <http://www.gnu.org/licenses/>.
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #ifdef HAVE_CONFIG_H
diff --git a/libmu_cpp/Makefile.am b/libmu_cpp/Makefile.am
index e6154de..b89cd22 100644
--- a/libmu_cpp/Makefile.am
+++ b/libmu_cpp/Makefile.am
@@ -1,22 +1,19 @@
-##   Process this file with GNU Automake to create Makefile.in
+## This file is part of GNU Mailutils.
+## Copyright (C) 2004, 2006, 2007, 2009, 2010 Free Software
+## Foundation, Inc.
 ##
-##   Copyright (C) 2004, 2006, 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.
 ##
-##   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.
+## GNU Mailutils 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.
 ##
-##   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
+## You should have received a copy of the GNU General Public License
+## along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
 INCLUDES = @MU_LIB_COMMON_INCLUDES@ 
 
diff --git a/libmu_cpp/address.cc b/libmu_cpp/address.cc
index 7ed7dff..757cd28 100644
--- a/libmu_cpp/address.cc
+++ b/libmu_cpp/address.cc
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2004, 2006, 2007, 2009, 2010 Free Software Foundation,
    Inc.
 
@@ -14,10 +13,8 @@
    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
-*/
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #include <mailutils/cpp/address.h>
 
diff --git a/libmu_cpp/attribute.cc b/libmu_cpp/attribute.cc
index 247c117..570cfcd 100644
--- a/libmu_cpp/attribute.cc
+++ b/libmu_cpp/attribute.cc
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 
    This library is free software; you can redistribute it and/or
@@ -13,10 +12,8 @@
    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
-*/
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #include <mailutils/cpp/attribute.h>
 
diff --git a/libmu_cpp/body.cc b/libmu_cpp/body.cc
index 93370a2..311013f 100644
--- a/libmu_cpp/body.cc
+++ b/libmu_cpp/body.cc
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 
    This library is free software; you can redistribute it and/or
@@ -13,10 +12,8 @@
    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
-*/
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #include <mailutils/cpp/body.h>
 
diff --git a/libmu_cpp/debug.cc b/libmu_cpp/debug.cc
index 4042137..95407c4 100644
--- a/libmu_cpp/debug.cc
+++ b/libmu_cpp/debug.cc
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 
    This library is free software; you can redistribute it and/or
@@ -13,10 +12,8 @@
    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
-*/
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #include <mailutils/cpp/debug.h>
 
diff --git a/libmu_cpp/envelope.cc b/libmu_cpp/envelope.cc
index e1fff64..7259b29 100644
--- a/libmu_cpp/envelope.cc
+++ b/libmu_cpp/envelope.cc
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 
    This library is free software; you can redistribute it and/or
@@ -13,10 +12,8 @@
    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
-*/
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #include <mailutils/cpp/envelope.h>
 
diff --git a/libmu_cpp/filter.cc b/libmu_cpp/filter.cc
index cf53e1b..1d392dc 100644
--- a/libmu_cpp/filter.cc
+++ b/libmu_cpp/filter.cc
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2004, 2006, 2007, 2009, 2010 Free Software Foundation,
    Inc.
 
@@ -14,10 +13,8 @@
    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
-*/
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #include <mailutils/cpp/filter.h>
 
diff --git a/libmu_cpp/folder.cc b/libmu_cpp/folder.cc
index aa53941..85cf083 100644
--- a/libmu_cpp/folder.cc
+++ b/libmu_cpp/folder.cc
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 
    This library is free software; you can redistribute it and/or
@@ -13,10 +12,8 @@
    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
-*/
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #include <mailutils/cpp/folder.h>
 
diff --git a/libmu_cpp/header.cc b/libmu_cpp/header.cc
index cc44067..4ba2daa 100644
--- a/libmu_cpp/header.cc
+++ b/libmu_cpp/header.cc
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2004, 2006, 2007, 2009, 2010 Free Software Foundation,
    Inc.
 
@@ -14,10 +13,8 @@
    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
-*/
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #include <mailutils/cpp/header.h>
 
diff --git a/libmu_cpp/iterator.cc b/libmu_cpp/iterator.cc
index b0aa161..2306923 100644
--- a/libmu_cpp/iterator.cc
+++ b/libmu_cpp/iterator.cc
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2004, 2006, 2007, 2009, 2010 Free Software Foundation,
    Inc.
 
@@ -14,10 +13,8 @@
    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
-*/
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #include <mailutils/cpp/iterator.h>
 
diff --git a/libmu_cpp/list.cc b/libmu_cpp/list.cc
index 75dd945..9066c0c 100644
--- a/libmu_cpp/list.cc
+++ b/libmu_cpp/list.cc
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2004, 2006, 2007, 2009, 2010 Free Software Foundation,
    Inc.
 
@@ -14,10 +13,8 @@
    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
-*/
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #include <mailutils/cpp/list.h>
 
diff --git a/libmu_cpp/mailbox.cc b/libmu_cpp/mailbox.cc
index b0e2d56..56761fc 100644
--- a/libmu_cpp/mailbox.cc
+++ b/libmu_cpp/mailbox.cc
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2004, 2006, 2007, 2009, 2010 Free Software Foundation,
    Inc.
 
@@ -14,10 +13,8 @@
    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
-*/
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #include <mailutils/cpp/mailbox.h>
 
diff --git a/libmu_cpp/mailcap.cc b/libmu_cpp/mailcap.cc
index 14cb01c..00ff72e 100644
--- a/libmu_cpp/mailcap.cc
+++ b/libmu_cpp/mailcap.cc
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2004, 2006, 2007, 2009, 2010 Free Software Foundation,
    Inc.
 
@@ -14,10 +13,8 @@
    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
-*/
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #include <mailutils/cpp/mailcap.h>
 
diff --git a/libmu_cpp/mailer.cc b/libmu_cpp/mailer.cc
index 8090fd4..cde47ee 100644
--- a/libmu_cpp/mailer.cc
+++ b/libmu_cpp/mailer.cc
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2004, 2006, 2007, 2009, 2010 Free Software Foundation,
    Inc.
 
@@ -14,10 +13,8 @@
    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
-*/
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #include <mailutils/cpp/mailer.h>
 
diff --git a/libmu_cpp/message.cc b/libmu_cpp/message.cc
index a023600..eb4f7ff 100644
--- a/libmu_cpp/message.cc
+++ b/libmu_cpp/message.cc
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2004, 2006, 2007, 2009, 2010 Free Software Foundation,
    Inc.
 
@@ -14,10 +13,8 @@
    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
-*/
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #include <cstdlib>
 #include <mailutils/cpp/message.h>
diff --git a/libmu_cpp/mime.cc b/libmu_cpp/mime.cc
index 483c211..81cd9e4 100644
--- a/libmu_cpp/mime.cc
+++ b/libmu_cpp/mime.cc
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 
    This library is free software; you can redistribute it and/or
@@ -13,10 +12,8 @@
    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
-*/
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #include <mailutils/cpp/mime.h>
 
diff --git a/libmu_cpp/mutil.cc b/libmu_cpp/mutil.cc
index e4ecceb..165cfa4 100644
--- a/libmu_cpp/mutil.cc
+++ b/libmu_cpp/mutil.cc
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 
    This library is free software; you can redistribute it and/or
@@ -13,10 +12,8 @@
    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
-*/
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #include <cstdlib>
 #include <mailutils/cpp/mutil.h>
diff --git a/libmu_cpp/pop3.cc b/libmu_cpp/pop3.cc
index 8ca7c5d..aa8c8e2 100644
--- a/libmu_cpp/pop3.cc
+++ b/libmu_cpp/pop3.cc
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2004, 2006, 2007, 2009, 2010 Free Software Foundation,
    Inc.
 
@@ -14,10 +13,8 @@
    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
-*/
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #include <cstdlib>
 #include <mailutils/cpp/pop3.h>
diff --git a/libmu_cpp/registrar.cc b/libmu_cpp/registrar.cc
index 26057bb..a4b6828 100644
--- a/libmu_cpp/registrar.cc
+++ b/libmu_cpp/registrar.cc
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 
    This library is free software; you can redistribute it and/or
@@ -13,10 +12,8 @@
    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
-*/
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #include <mailutils/cpp/registrar.h>
 
diff --git a/libmu_cpp/secret.cc b/libmu_cpp/secret.cc
index 4b5582e..59d352d 100644
--- a/libmu_cpp/secret.cc
+++ b/libmu_cpp/secret.cc
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 
    This library is free software; you can redistribute it and/or
@@ -13,10 +12,8 @@
    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
-*/
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #include <mailutils/cpp/secret.h>
 
diff --git a/libmu_cpp/sieve.cc b/libmu_cpp/sieve.cc
index c3be1b4..115840a 100644
--- a/libmu_cpp/sieve.cc
+++ b/libmu_cpp/sieve.cc
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 
    This library is free software; you can redistribute it and/or
@@ -13,10 +12,8 @@
    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
-*/
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #include <mailutils/cpp/sieve.h>
 
diff --git a/libmu_cpp/stream.cc b/libmu_cpp/stream.cc
index eb95740..fe1b98a 100644
--- a/libmu_cpp/stream.cc
+++ b/libmu_cpp/stream.cc
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2004, 2006, 2007, 2009, 2010 Free Software Foundation,
    Inc.
 
@@ -14,10 +13,8 @@
    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
-*/
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #include <mailutils/cpp/stream.h>
 
diff --git a/libmu_cpp/url.cc b/libmu_cpp/url.cc
index 1ae8c00..9c9353c 100644
--- a/libmu_cpp/url.cc
+++ b/libmu_cpp/url.cc
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2004, 2006, 2007, 2008, 2009, 2010 Free Software
    Foundation, Inc.
 
@@ -14,10 +13,8 @@
    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
-*/
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #include <mailutils/cpp/url.h>
 
diff --git a/libmu_scm/Makefile.am b/libmu_scm/Makefile.am
index 3a2ecb9..68b82b5 100644
--- a/libmu_scm/Makefile.am
+++ b/libmu_scm/Makefile.am
@@ -1,22 +1,19 @@
-## Process this file with GNU Automake to create Makefile.in
-
-##   Copyright (C) 2001, 2002, 2006, 2007, 2009, 2010 Free Software
-##   Foundation, Inc.
+## This file is part of GNU Mailutils.
+## Copyright (C) 2001, 2002, 2006, 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.
+## 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.
+## GNU Mailutils 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
+## You should have received a copy of the GNU General Public License
+## along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
 INCLUDES = -I. @MU_LIB_COMMON_INCLUDES@ @GUILE_INCLUDES@
 
diff --git a/libmu_scm/mailutils.scm.in b/libmu_scm/mailutils.scm.in
index d78a753..285a964 100644
--- a/libmu_scm/mailutils.scm.in
+++ b/libmu_scm/mailutils.scm.in
@@ -14,8 +14,7 @@
 ;;;; GNU General Public License for more details.
 ;;;; 
 ;;;; You should have received a copy of the GNU General Public License along
-;;;; with GNU Mailutils; if not, write to the Free Software Foundation,
-;;;; Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+;;;; with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 ;;;;
 changequote([,])dnl
 
diff --git a/libmu_scm/mu_address.c b/libmu_scm/mu_address.c
index 2df7dcb..bd20a32 100644
--- a/libmu_scm/mu_address.c
+++ b/libmu_scm/mu_address.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #include "mu_scm.h"
 
diff --git a/libmu_scm/mu_body.c b/libmu_scm/mu_body.c
index a5c5cf5..7f3d85c 100644
--- a/libmu_scm/mu_body.c
+++ b/libmu_scm/mu_body.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #include "mu_scm.h"
 
diff --git a/libmu_scm/mu_dbgport.c b/libmu_scm/mu_dbgport.c
index f118cb4..a847381 100644
--- a/libmu_scm/mu_dbgport.c
+++ b/libmu_scm/mu_dbgport.c
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #include "mu_scm.h"
 
diff --git a/libmu_scm/mu_guile.c b/libmu_scm/mu_guile.c
index 39e1916..f912495 100644
--- a/libmu_scm/mu_guile.c
+++ b/libmu_scm/mu_guile.c
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #include "mu_scm.h"
 #include <setjmp.h>
diff --git a/libmu_scm/mu_logger.c b/libmu_scm/mu_logger.c
index 01ebe00..5385fe2 100644
--- a/libmu_scm/mu_logger.c
+++ b/libmu_scm/mu_logger.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #include "mu_scm.h"
 
diff --git a/libmu_scm/mu_mailbox.c b/libmu_scm/mu_mailbox.c
index 4252e65..4b488b8 100644
--- a/libmu_scm/mu_mailbox.c
+++ b/libmu_scm/mu_mailbox.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #include "mu_scm.h"
 #include <mailutils/iterator.h>
diff --git a/libmu_scm/mu_message.c b/libmu_scm/mu_message.c
index 1cb06c0..f00dd4c 100644
--- a/libmu_scm/mu_message.c
+++ b/libmu_scm/mu_message.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #include "mu_scm.h"
 
diff --git a/libmu_scm/mu_mime.c b/libmu_scm/mu_mime.c
index 2c90037..dea08b7 100644
--- a/libmu_scm/mu_mime.c
+++ b/libmu_scm/mu_mime.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #include "mu_scm.h"
 
diff --git a/libmu_scm/mu_port.c b/libmu_scm/mu_port.c
index 0efa5e7..b090424 100644
--- a/libmu_scm/mu_port.c
+++ b/libmu_scm/mu_port.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #include "mu_scm.h"
 #include <mailutils/io.h>
diff --git a/libmu_scm/mu_scm.c b/libmu_scm/mu_scm.c
index f5d3f4c..5f085b1 100644
--- a/libmu_scm/mu_scm.c
+++ b/libmu_scm/mu_scm.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #include "mu_scm.h"
 
diff --git a/libmu_scm/mu_scm.h b/libmu_scm/mu_scm.h
index 1f5f70b..22956cf 100644
--- a/libmu_scm/mu_scm.h
+++ b/libmu_scm/mu_scm.h
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #if defined(HAVE_CONFIG_H)
 # include <config.h>
diff --git a/libmu_scm/mu_util.c b/libmu_scm/mu_util.c
index 8f3b232..0950b96 100644
--- a/libmu_scm/mu_util.c
+++ b/libmu_scm/mu_util.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #include "mu_scm.h"
 
diff --git a/libmu_sieve/Makefile.am b/libmu_sieve/Makefile.am
index 1dc3027..8275a03 100644
--- a/libmu_sieve/Makefile.am
+++ b/libmu_sieve/Makefile.am
@@ -1,22 +1,19 @@
-## Process this file with GNU Automake to create Makefile.in
-
-##   Copyright (C) 2002, 2003, 2004, 2005, 2007, 2009, 2010 Free
-##   Software Foundation, Inc.
+## This file is part of GNU Mailutils.
+## Copyright (C) 2002, 2003, 2004, 2005, 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.
+## 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.
+## GNU Mailutils 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
+## You should have received a copy of the GNU General Public License
+## along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. 
 
 INCLUDES = @MU_LIB_COMMON_INCLUDES@ 
 
diff --git a/libmu_sieve/actions.c b/libmu_sieve/actions.c
index 952626a..98c00d1 100644
--- a/libmu_sieve/actions.c
+++ b/libmu_sieve/actions.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libmu_sieve/comparator.c b/libmu_sieve/comparator.c
index 50fb672..b23ce8a 100644
--- a/libmu_sieve/comparator.c
+++ b/libmu_sieve/comparator.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libmu_sieve/conf.c b/libmu_sieve/conf.c
index 9566a0b..dc9c050 100644
--- a/libmu_sieve/conf.c
+++ b/libmu_sieve/conf.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libmu_sieve/extensions/Makefile.am b/libmu_sieve/extensions/Makefile.am
index cdbdcee..391d191 100644
--- a/libmu_sieve/extensions/Makefile.am
+++ b/libmu_sieve/extensions/Makefile.am
@@ -1,22 +1,19 @@
-## Process this file with GNU Automake to create Makefile.in
-
-##   Copyright (C) 2004, 2005, 2006, 2007, 2009, 2010 Free Software
-##   Foundation, Inc.
+## This file is part of GNU Mailutils.
+## Copyright (C) 2004, 2005, 2006, 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.
+## 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.
+## GNU Mailutils 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
+## You should have received a copy of the GNU General Public License
+## along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. 
 
 moddir=@MU_SIEVE_MODDIR@
 mod_LTLIBRARIES = \
diff --git a/libmu_sieve/extensions/list.c b/libmu_sieve/extensions/list.c
index 184caad..452228e 100644
--- a/libmu_sieve/extensions/list.c
+++ b/libmu_sieve/extensions/list.c
@@ -13,9 +13,8 @@
    GNU Lesser General Public License for more details.
 
    You should have received a copy of the GNU Lesser General Public
-   License along with GNU Mailutils; if not, write to the Free
-   Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
-   Boston, MA 02110-1301 USA */
+   License along with GNU Mailutils.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 /* Implements "list" sieve extension test. See "Syntax:" below for the
    description */
diff --git a/libmu_sieve/extensions/moderator.c b/libmu_sieve/extensions/moderator.c
index f3dc8c4..b4508e9 100644
--- a/libmu_sieve/extensions/moderator.c
+++ b/libmu_sieve/extensions/moderator.c
@@ -12,9 +12,8 @@
    GNU Lesser General Public License for more details.
 
    You should have received a copy of the GNU Lesser General Public
-   License along with GNU Mailutils; if not, write to the Free
-   Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
-   Boston, MA 02110-1301 USA */
+   License along with GNU Mailutils.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 /* Moderator robot for Mailman-driven mail archives.
    Mailman moderation request is a MIME message consisting of the three parts:
diff --git a/libmu_sieve/extensions/pipe.c b/libmu_sieve/extensions/pipe.c
index 664997d..e049cac 100644
--- a/libmu_sieve/extensions/pipe.c
+++ b/libmu_sieve/extensions/pipe.c
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 /* Syntax: pipe [:envelope] <program: string>
 
diff --git a/libmu_sieve/extensions/spamd.c b/libmu_sieve/extensions/spamd.c
index b679cd3..c94ef74 100644
--- a/libmu_sieve/extensions/spamd.c
+++ b/libmu_sieve/extensions/spamd.c
@@ -13,9 +13,8 @@
    GNU Lesser General Public License for more details.
 
    You should have received a copy of the GNU Lesser General Public
-   License along with GNU Mailutils; if not, write to the Free
-   Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
-   Boston, MA 02110-1301 USA */
+   License along with GNU Mailutils.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 /* This module implements sieve extension test "spamd": an interface to
    the SpamAssassin spamd daemon. See "Usage:" below for the description */
diff --git a/libmu_sieve/extensions/timestamp.c b/libmu_sieve/extensions/timestamp.c
index 7cbcda8..8f03489 100644
--- a/libmu_sieve/extensions/timestamp.c
+++ b/libmu_sieve/extensions/timestamp.c
@@ -12,9 +12,8 @@
    GNU Lesser General Public License for more details.
 
    You should have received a copy of the GNU Lesser General Public
-   License along with GNU Mailutils; if not, write to the Free
-   Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
-   Boston, MA 02110-1301 USA */
+   License along with GNU Mailutils.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 /*  Syntax:   timestamp [":before"/":after"] <header-name: string>
               <date: datestring>
diff --git a/libmu_sieve/extensions/vacation.c b/libmu_sieve/extensions/vacation.c
index a41103a..323da50 100644
--- a/libmu_sieve/extensions/vacation.c
+++ b/libmu_sieve/extensions/vacation.c
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 /* Syntax: vacation [:days <ndays: number>]
                     [:subject <subject: string>]
diff --git a/libmu_sieve/load.c b/libmu_sieve/load.c
index 8c7f121..c35ae8b 100644
--- a/libmu_sieve/load.c
+++ b/libmu_sieve/load.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libmu_sieve/prog.c b/libmu_sieve/prog.c
index bafdcfa..58ca94d 100644
--- a/libmu_sieve/prog.c
+++ b/libmu_sieve/prog.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libmu_sieve/register.c b/libmu_sieve/register.c
index 251a088..a2e6c3b 100644
--- a/libmu_sieve/register.c
+++ b/libmu_sieve/register.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libmu_sieve/relational.c b/libmu_sieve/relational.c
index 8f1c06c..e24d0ff 100644
--- a/libmu_sieve/relational.c
+++ b/libmu_sieve/relational.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libmu_sieve/require.c b/libmu_sieve/require.c
index 21e072e..b7a770f 100644
--- a/libmu_sieve/require.c
+++ b/libmu_sieve/require.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libmu_sieve/runtime.c b/libmu_sieve/runtime.c
index c2cb658..60bbc74 100644
--- a/libmu_sieve/runtime.c
+++ b/libmu_sieve/runtime.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libmu_sieve/sieve-priv.h b/libmu_sieve/sieve-priv.h
index 0bea174..0a9e84b 100644
--- a/libmu_sieve/sieve-priv.h
+++ b/libmu_sieve/sieve-priv.h
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #include <mailutils/sieve.h>
 #include <setjmp.h>
diff --git a/libmu_sieve/sieve.l b/libmu_sieve/sieve.l
index ffaaef3..87a5dc6 100644
--- a/libmu_sieve/sieve.l
+++ b/libmu_sieve/sieve.l
@@ -14,9 +14,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libmu_sieve/sieve.y b/libmu_sieve/sieve.y
index c3ca2dc..c09dd37 100644
--- a/libmu_sieve/sieve.y
+++ b/libmu_sieve/sieve.y
@@ -14,9 +14,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libmu_sieve/tests.c b/libmu_sieve/tests.c
index bbcbb6c..83aa7aa 100644
--- a/libmu_sieve/tests.c
+++ b/libmu_sieve/tests.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libmu_sieve/util.c b/libmu_sieve/util.c
index 2966950..05a8494 100644
--- a/libmu_sieve/util.c
+++ b/libmu_sieve/util.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/Makefile.am b/libproto/Makefile.am
index 3ba7d19..009985a 100644
--- a/libproto/Makefile.am
+++ b/libproto/Makefile.am
@@ -1,21 +1,18 @@
-## Process this file with GNU Automake to create Makefile.in
-
-##   Copyright (C) 2006, 2007, 2009, 2010 Free Software Foundation, Inc.
+## This file is part of GNU Mailutils.
+## Copyright (C) 2006, 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.
+## 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.
+## GNU Mailutils 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
+## You should have received a copy of the GNU General Public License
+## along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. 
 
 if MU_COND_SUPPORT_POP
   POP_DIR = pop
diff --git a/libproto/imap/Makefile.am b/libproto/imap/Makefile.am
index dd37326..7c01741 100644
--- a/libproto/imap/Makefile.am
+++ b/libproto/imap/Makefile.am
@@ -1,22 +1,19 @@
-## Process this file with GNU Automake to create Makefile.in
-
-##   Copyright (C) 2003, 2005, 2006, 2007, 2010 Free Software
-##   Foundation, Inc.
+## This file is part of GNU Mailutils.
+## Copyright (C) 2003, 2005, 2006, 2007, 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.
+## 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.
+## GNU Mailutils 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
+## You should have received a copy of the GNU General Public License
+## along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. 
 
 INCLUDES = @MU_LIB_COMMON_INCLUDES@ 
 
diff --git a/libproto/imap/folder.c b/libproto/imap/folder.c
index e555c12..278294c 100644
--- a/libproto/imap/folder.c
+++ b/libproto/imap/folder.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/imap/mbox.c b/libproto/imap/mbox.c
index 5163eeb..00e8859 100644
--- a/libproto/imap/mbox.c
+++ b/libproto/imap/mbox.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/imap/url.c b/libproto/imap/url.c
index 8e3ae1a..051f45f 100644
--- a/libproto/imap/url.c
+++ b/libproto/imap/url.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/maildir/Makefile.am b/libproto/maildir/Makefile.am
index 939cd03..a43719f 100644
--- a/libproto/maildir/Makefile.am
+++ b/libproto/maildir/Makefile.am
@@ -1,22 +1,19 @@
-## Process this file with GNU Automake to create Makefile.in
-
-##   Copyright (C) 2003, 2005, 2006, 2007, 2010 Free Software
-##   Foundation, Inc.
+## This file is part of GNU Mailutils.
+## Copyright (C) 2003, 2005, 2006, 2007, 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.
+## 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.
+## GNU Mailutils 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
+## You should have received a copy of the GNU General Public License
+## along with GNU Mailutils  If not, see <http://www.gnu.org/licenses/>. 
 
 INCLUDES = @MU_LIB_COMMON_INCLUDES@ 
 
diff --git a/libproto/maildir/folder.c b/libproto/maildir/folder.c
index f6431a8..19ff4b3 100644
--- a/libproto/maildir/folder.c
+++ b/libproto/maildir/folder.c
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/maildir/maildir.h b/libproto/maildir/maildir.h
index 14a95e4..7796740 100644
--- a/libproto/maildir/maildir.h
+++ b/libproto/maildir/maildir.h
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #define PERMS 0700
 #define TMPSUF "tmp"
diff --git a/libproto/maildir/mbox.c b/libproto/maildir/mbox.c
index 8a8b197..c182958 100644
--- a/libproto/maildir/mbox.c
+++ b/libproto/maildir/mbox.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 /* First draft by Sergey Poznyakoff */
 
diff --git a/libproto/mailer/Makefile.am b/libproto/mailer/Makefile.am
index fbbb481..156dc1f 100644
--- a/libproto/mailer/Makefile.am
+++ b/libproto/mailer/Makefile.am
@@ -1,21 +1,18 @@
-## Process this file with GNU Automake to create Makefile.in
-
-##   Copyright (C) 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
+## This file is part of GNU Mailutils.
+## Copyright (C) 2007, 2008, 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.
+## 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.
+## GNU Mailutils 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
+## You should have received a copy of the GNU General Public License
+## along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. 
 
 INCLUDES = @MU_LIB_COMMON_INCLUDES@
 
diff --git a/libproto/mailer/mbox.c b/libproto/mailer/mbox.c
index ab6f905..30130a7 100644
--- a/libproto/mailer/mbox.c
+++ b/libproto/mailer/mbox.c
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 #include <config.h>
diff --git a/libproto/mailer/sendmail.c b/libproto/mailer/sendmail.c
index bc944d2..0a991a3 100644
--- a/libproto/mailer/sendmail.c
+++ b/libproto/mailer/sendmail.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/mailer/smtp.c b/libproto/mailer/smtp.c
index ae78cae..4db4398 100644
--- a/libproto/mailer/smtp.c
+++ b/libproto/mailer/smtp.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/mbox/Makefile.am b/libproto/mbox/Makefile.am
index b65cf1e..7cf8b0a 100644
--- a/libproto/mbox/Makefile.am
+++ b/libproto/mbox/Makefile.am
@@ -1,22 +1,19 @@
-## Process this file with GNU Automake to create Makefile.in
-
-##   Copyright (C) 2003, 2005, 2006, 2007, 2010 Free Software
-##   Foundation, Inc.
+## This file is part of GNU Mailutils.
+## Copyright (C) 2003, 2005, 2006, 2007, 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.
+## 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.
+## GNU Mailutils 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
+## You should have received a copy of the GNU General Public License
+## along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. 
 
 INCLUDES = @MU_LIB_COMMON_INCLUDES@ 
 
diff --git a/libproto/mbox/folder.c b/libproto/mbox/folder.c
index 4ae658b..84633d0 100644
--- a/libproto/mbox/folder.c
+++ b/libproto/mbox/folder.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/mbox/mbox.c b/libproto/mbox/mbox.c
index cc412ff..0411c23 100644
--- a/libproto/mbox/mbox.c
+++ b/libproto/mbox/mbox.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 /* First draft by Alain Magloire.
  * Completely rewritten by Sergey Poznyakoff.
diff --git a/libproto/mbox/mbox0.h b/libproto/mbox/mbox0.h
index 85e8408..382294f 100644
--- a/libproto/mbox/mbox0.h
+++ b/libproto/mbox/mbox0.h
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #include <stdlib.h>
 #include <stdio.h>
diff --git a/libproto/mbox/mboxscan.c b/libproto/mbox/mboxscan.c
index cb804c7..3a35e6f 100644
--- a/libproto/mbox/mboxscan.c
+++ b/libproto/mbox/mboxscan.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 /* Mailbox Parsing. */
 
diff --git a/libproto/mh/Makefile.am b/libproto/mh/Makefile.am
index a5be198..322d657 100644
--- a/libproto/mh/Makefile.am
+++ b/libproto/mh/Makefile.am
@@ -1,22 +1,19 @@
-## Process this file with GNU Automake to create Makefile.in
-
-##   Copyright (C) 2003, 2005, 2006, 2007, 2010 Free Software
-##   Foundation, Inc.
+## This file is part of GNU Mailutils.
+## Copyright (C) 2003, 2005, 2006, 2007, 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.
+## 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.
+## GNU Mailutils 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
+## You should have received a copy of the GNU General Public License
+## along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. 
 
 INCLUDES = @MU_LIB_COMMON_INCLUDES@
 
diff --git a/libproto/mh/folder.c b/libproto/mh/folder.c
index 1b2c033..074273f 100644
--- a/libproto/mh/folder.c
+++ b/libproto/mh/folder.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/mh/mbox.c b/libproto/mh/mbox.c
index 5995c5b..bb412b7 100644
--- a/libproto/mh/mbox.c
+++ b/libproto/mh/mbox.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 /* First draft by Sergey Poznyakoff */
 
diff --git a/libproto/nntp/Makefile.am b/libproto/nntp/Makefile.am
index aecc0c7..39a0cb3 100644
--- a/libproto/nntp/Makefile.am
+++ b/libproto/nntp/Makefile.am
@@ -1,22 +1,19 @@
-## Process this file with GNU Automake to create Makefile.in
-
-##   Copyright (C) 2004, 2005, 2006, 2007, 2010 Free Software
-##   Foundation, Inc.
+## This file is part of GNU Mailutils.
+## Copyright (C) 2004, 2005, 2006, 2007, 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.
+## 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.
+## GNU Mailutils 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
+## You should have received a copy of the GNU General Public License
+## along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. 
 
 INCLUDES = @MU_LIB_COMMON_INCLUDES@
 
diff --git a/libproto/nntp/folder.c b/libproto/nntp/folder.c
index 0e3b43d..4573559 100644
--- a/libproto/nntp/folder.c
+++ b/libproto/nntp/folder.c
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/nntp/mbox.c b/libproto/nntp/mbox.c
index 2d09be2..8ea6d67 100644
--- a/libproto/nntp/mbox.c
+++ b/libproto/nntp/mbox.c
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/nntp/nntp0.h b/libproto/nntp/nntp0.h
index 5610b24..e667cdd 100644
--- a/libproto/nntp/nntp0.h
+++ b/libproto/nntp/nntp0.h
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/nntp/nntp_article.c b/libproto/nntp/nntp_article.c
index 9c3c58a..e9bb1cd 100644
--- a/libproto/nntp/nntp_article.c
+++ b/libproto/nntp/nntp_article.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/nntp/nntp_body.c b/libproto/nntp/nntp_body.c
index f06d034..9d9b878 100644
--- a/libproto/nntp/nntp_body.c
+++ b/libproto/nntp/nntp_body.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/nntp/nntp_carrier.c b/libproto/nntp/nntp_carrier.c
index 1209a67..69c66d7 100644
--- a/libproto/nntp/nntp_carrier.c
+++ b/libproto/nntp/nntp_carrier.c
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/nntp/nntp_connect.c b/libproto/nntp/nntp_connect.c
index 959f0b4..123b629 100644
--- a/libproto/nntp/nntp_connect.c
+++ b/libproto/nntp/nntp_connect.c
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/nntp/nntp_create.c b/libproto/nntp/nntp_create.c
index 005256c..90cd10a 100644
--- a/libproto/nntp/nntp_create.c
+++ b/libproto/nntp/nntp_create.c
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/nntp/nntp_date.c b/libproto/nntp/nntp_date.c
index e80459c..cf8371a 100644
--- a/libproto/nntp/nntp_date.c
+++ b/libproto/nntp/nntp_date.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/nntp/nntp_debug.c b/libproto/nntp/nntp_debug.c
index 54a8d6f..08f77db 100644
--- a/libproto/nntp/nntp_debug.c
+++ b/libproto/nntp/nntp_debug.c
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/nntp/nntp_destroy.c b/libproto/nntp/nntp_destroy.c
index 3725bec..ceb5b87 100644
--- a/libproto/nntp/nntp_destroy.c
+++ b/libproto/nntp/nntp_destroy.c
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/nntp/nntp_disconnect.c b/libproto/nntp/nntp_disconnect.c
index 7b8eabb..118945e 100644
--- a/libproto/nntp/nntp_disconnect.c
+++ b/libproto/nntp/nntp_disconnect.c
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/nntp/nntp_group.c b/libproto/nntp/nntp_group.c
index 7518efa..6d248d5 100644
--- a/libproto/nntp/nntp_group.c
+++ b/libproto/nntp/nntp_group.c
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/nntp/nntp_head.c b/libproto/nntp/nntp_head.c
index 6f1783f..0e5fd1d 100644
--- a/libproto/nntp/nntp_head.c
+++ b/libproto/nntp/nntp_head.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/nntp/nntp_help.c b/libproto/nntp/nntp_help.c
index 9930e75..0b312e1 100644
--- a/libproto/nntp/nntp_help.c
+++ b/libproto/nntp/nntp_help.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/nntp/nntp_ihave.c b/libproto/nntp/nntp_ihave.c
index a166754..81767b8 100644
--- a/libproto/nntp/nntp_ihave.c
+++ b/libproto/nntp/nntp_ihave.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/nntp/nntp_iterator.c b/libproto/nntp/nntp_iterator.c
index f147901..db8e49f 100644
--- a/libproto/nntp/nntp_iterator.c
+++ b/libproto/nntp/nntp_iterator.c
@@ -1,19 +1,18 @@
 /* GNU mailutils - a suite of utilities for electronic mail
    Copyright (C) 2004, 2005, 2007, 2010 Free Software Foundation, Inc.
 
-   This program is free software; you can redistribute it and/or modify
+   GNU Mailutils is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Library 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,
+   GNU Mailutils 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 Library General Public License for more details.
 
    You should have received a copy of the GNU Library General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/nntp/nntp_last.c b/libproto/nntp/nntp_last.c
index e30e0b1..a332868 100644
--- a/libproto/nntp/nntp_last.c
+++ b/libproto/nntp/nntp_last.c
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/nntp/nntp_list_active.c b/libproto/nntp/nntp_list_active.c
index c45c11a..c7a5fe6 100644
--- a/libproto/nntp/nntp_list_active.c
+++ b/libproto/nntp/nntp_list_active.c
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/nntp/nntp_list_distribpats.c b/libproto/nntp/nntp_list_distribpats.c
index 511f1ee..065e7df 100644
--- a/libproto/nntp/nntp_list_distribpats.c
+++ b/libproto/nntp/nntp_list_distribpats.c
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/nntp/nntp_list_distributions.c b/libproto/nntp/nntp_list_distributions.c
index 9b990c1..eb0168d 100644
--- a/libproto/nntp/nntp_list_distributions.c
+++ b/libproto/nntp/nntp_list_distributions.c
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/nntp/nntp_list_extensions.c b/libproto/nntp/nntp_list_extensions.c
index d6a18ec..6bfda73 100644
--- a/libproto/nntp/nntp_list_extensions.c
+++ b/libproto/nntp/nntp_list_extensions.c
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/nntp/nntp_list_newsgroups.c b/libproto/nntp/nntp_list_newsgroups.c
index 5b5da42..eb778e9 100644
--- a/libproto/nntp/nntp_list_newsgroups.c
+++ b/libproto/nntp/nntp_list_newsgroups.c
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/nntp/nntp_list_times.c b/libproto/nntp/nntp_list_times.c
index e770d47..25f6713 100644
--- a/libproto/nntp/nntp_list_times.c
+++ b/libproto/nntp/nntp_list_times.c
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/nntp/nntp_mode_reader.c b/libproto/nntp/nntp_mode_reader.c
index 04854c8..12b0bad 100644
--- a/libproto/nntp/nntp_mode_reader.c
+++ b/libproto/nntp/nntp_mode_reader.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/nntp/nntp_newgroups.c b/libproto/nntp/nntp_newgroups.c
index b406423..ff3ee69 100644
--- a/libproto/nntp/nntp_newgroups.c
+++ b/libproto/nntp/nntp_newgroups.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/nntp/nntp_newnews.c b/libproto/nntp/nntp_newnews.c
index 4175205..07e4078 100644
--- a/libproto/nntp/nntp_newnews.c
+++ b/libproto/nntp/nntp_newnews.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/nntp/nntp_next.c b/libproto/nntp/nntp_next.c
index 510f074..61f8d25 100644
--- a/libproto/nntp/nntp_next.c
+++ b/libproto/nntp/nntp_next.c
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/nntp/nntp_post.c b/libproto/nntp/nntp_post.c
index 22eb37e..2758c7a 100644
--- a/libproto/nntp/nntp_post.c
+++ b/libproto/nntp/nntp_post.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/nntp/nntp_quit.c b/libproto/nntp/nntp_quit.c
index 802e009..5fcd091 100644
--- a/libproto/nntp/nntp_quit.c
+++ b/libproto/nntp/nntp_quit.c
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/nntp/nntp_readline.c b/libproto/nntp/nntp_readline.c
index b369f38..5551df0 100644
--- a/libproto/nntp/nntp_readline.c
+++ b/libproto/nntp/nntp_readline.c
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/nntp/nntp_response.c b/libproto/nntp/nntp_response.c
index fea1286..f4b7528 100644
--- a/libproto/nntp/nntp_response.c
+++ b/libproto/nntp/nntp_response.c
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/nntp/nntp_sendline.c b/libproto/nntp/nntp_sendline.c
index f1e1a10..4908829 100644
--- a/libproto/nntp/nntp_sendline.c
+++ b/libproto/nntp/nntp_sendline.c
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/nntp/nntp_stat.c b/libproto/nntp/nntp_stat.c
index 9b5c6f1..21e98bc 100644
--- a/libproto/nntp/nntp_stat.c
+++ b/libproto/nntp/nntp_stat.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/nntp/nntp_stream.c b/libproto/nntp/nntp_stream.c
index f2f50ac..de7cca6 100644
--- a/libproto/nntp/nntp_stream.c
+++ b/libproto/nntp/nntp_stream.c
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/nntp/nntp_timeout.c b/libproto/nntp/nntp_timeout.c
index 3682f58..8b35ed0 100644
--- a/libproto/nntp/nntp_timeout.c
+++ b/libproto/nntp/nntp_timeout.c
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/nntp/url.c b/libproto/nntp/url.c
index 73a66eb..5354750 100644
--- a/libproto/nntp/url.c
+++ b/libproto/nntp/url.c
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/pop/Makefile.am b/libproto/pop/Makefile.am
index 5865e4e..749e515 100644
--- a/libproto/pop/Makefile.am
+++ b/libproto/pop/Makefile.am
@@ -1,22 +1,19 @@
-## Process this file with GNU Automake to create Makefile.in
-
-##   Copyright (C) 2003, 2004, 2005, 2006, 2007, 2010 Free Software
-##   Foundation, Inc.
+## This file is part of GNU Mailutils.
+## Copyright (C) 2003, 2004, 2005, 2006, 2007, 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.
+## 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.
+## GNU Mailutils 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
+## You should have received a copy of the GNU General Public License
+## along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. 
 
 INCLUDES = @MU_LIB_COMMON_INCLUDES@
 
diff --git a/libproto/pop/folder.c b/libproto/pop/folder.c
index b1ab364..82f8197 100644
--- a/libproto/pop/folder.c
+++ b/libproto/pop/folder.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/pop/mbox.c b/libproto/pop/mbox.c
index eb25bcc..5bd14ca 100644
--- a/libproto/pop/mbox.c
+++ b/libproto/pop/mbox.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/pop/pop3_apop.c b/libproto/pop/pop3_apop.c
index c358b93..22f69d0 100644
--- a/libproto/pop/pop3_apop.c
+++ b/libproto/pop/pop3_apop.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/pop/pop3_capa.c b/libproto/pop/pop3_capa.c
index e6402af..ffca6aa 100644
--- a/libproto/pop/pop3_capa.c
+++ b/libproto/pop/pop3_capa.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/pop/pop3_capatst.c b/libproto/pop/pop3_capatst.c
index 37d740b..4fd4466 100644
--- a/libproto/pop/pop3_capatst.c
+++ b/libproto/pop/pop3_capatst.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/pop/pop3_carrier.c b/libproto/pop/pop3_carrier.c
index 485790f..265669d 100644
--- a/libproto/pop/pop3_carrier.c
+++ b/libproto/pop/pop3_carrier.c
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/pop/pop3_connect.c b/libproto/pop/pop3_connect.c
index 5b9ffab..f115e11 100644
--- a/libproto/pop/pop3_connect.c
+++ b/libproto/pop/pop3_connect.c
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/pop/pop3_create.c b/libproto/pop/pop3_create.c
index dc92360..dbb7a4b 100644
--- a/libproto/pop/pop3_create.c
+++ b/libproto/pop/pop3_create.c
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/pop/pop3_dele.c b/libproto/pop/pop3_dele.c
index e4b06c4..0782d7d 100644
--- a/libproto/pop/pop3_dele.c
+++ b/libproto/pop/pop3_dele.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/pop/pop3_destroy.c b/libproto/pop/pop3_destroy.c
index 010a063..de70748 100644
--- a/libproto/pop/pop3_destroy.c
+++ b/libproto/pop/pop3_destroy.c
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/pop/pop3_disconnect.c b/libproto/pop/pop3_disconnect.c
index 1c3d231..5f9e5ef 100644
--- a/libproto/pop/pop3_disconnect.c
+++ b/libproto/pop/pop3_disconnect.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/pop/pop3_iterator.c b/libproto/pop/pop3_iterator.c
index d7a2313..7712b20 100644
--- a/libproto/pop/pop3_iterator.c
+++ b/libproto/pop/pop3_iterator.c
@@ -1,19 +1,18 @@
 /* GNU mailutils - a suite of utilities for electronic mail
    Copyright (C) 2004, 2005, 2007, 2010 Free Software Foundation, Inc.
 
-   This program is free software; you can redistribute it and/or modify
+   GNU Mailutils is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Library 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,
+   GNU Mailutils 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 Library General Public License for more details.
 
    You should have received a copy of the GNU Library General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/pop/pop3_list.c b/libproto/pop/pop3_list.c
index e6bccb5..663c8c8 100644
--- a/libproto/pop/pop3_list.c
+++ b/libproto/pop/pop3_list.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/pop/pop3_list_cmd.c b/libproto/pop/pop3_list_cmd.c
index 2917e21..ee6df29 100644
--- a/libproto/pop/pop3_list_cmd.c
+++ b/libproto/pop/pop3_list_cmd.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/pop/pop3_lista.c b/libproto/pop/pop3_lista.c
index 4ce2ff1..5b5a345 100644
--- a/libproto/pop/pop3_lista.c
+++ b/libproto/pop/pop3_lista.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/pop/pop3_listas.c b/libproto/pop/pop3_listas.c
index 0227650..59e1ebe 100644
--- a/libproto/pop/pop3_listas.c
+++ b/libproto/pop/pop3_listas.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/pop/pop3_noop.c b/libproto/pop/pop3_noop.c
index beed039..82801b7 100644
--- a/libproto/pop/pop3_noop.c
+++ b/libproto/pop/pop3_noop.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/pop/pop3_pass.c b/libproto/pop/pop3_pass.c
index d72c511..3870e9d 100644
--- a/libproto/pop/pop3_pass.c
+++ b/libproto/pop/pop3_pass.c
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/pop/pop3_quit.c b/libproto/pop/pop3_quit.c
index 8ec8178..f38c948 100644
--- a/libproto/pop/pop3_quit.c
+++ b/libproto/pop/pop3_quit.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/pop/pop3_rdlist.c b/libproto/pop/pop3_rdlist.c
index 3544a31..915d7ce 100644
--- a/libproto/pop/pop3_rdlist.c
+++ b/libproto/pop/pop3_rdlist.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/pop/pop3_readline.c b/libproto/pop/pop3_readline.c
index 566735b..aeb5f94 100644
--- a/libproto/pop/pop3_readline.c
+++ b/libproto/pop/pop3_readline.c
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/pop/pop3_response.c b/libproto/pop/pop3_response.c
index 2a106ab..63ecc7a 100644
--- a/libproto/pop/pop3_response.c
+++ b/libproto/pop/pop3_response.c
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/pop/pop3_retr.c b/libproto/pop/pop3_retr.c
index 7cd8706..ca22c97 100644
--- a/libproto/pop/pop3_retr.c
+++ b/libproto/pop/pop3_retr.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/pop/pop3_rset.c b/libproto/pop/pop3_rset.c
index d233ec4..41aca1a 100644
--- a/libproto/pop/pop3_rset.c
+++ b/libproto/pop/pop3_rset.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/pop/pop3_sendline.c b/libproto/pop/pop3_sendline.c
index 0de5e6a..c7e5004 100644
--- a/libproto/pop/pop3_sendline.c
+++ b/libproto/pop/pop3_sendline.c
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/pop/pop3_stat.c b/libproto/pop/pop3_stat.c
index 707fe7d..38f36b6 100644
--- a/libproto/pop/pop3_stat.c
+++ b/libproto/pop/pop3_stat.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/pop/pop3_stls.c b/libproto/pop/pop3_stls.c
index 0cf617d..2f5f197 100644
--- a/libproto/pop/pop3_stls.c
+++ b/libproto/pop/pop3_stls.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/pop/pop3_stream.c b/libproto/pop/pop3_stream.c
index 6df096c..046dfed 100644
--- a/libproto/pop/pop3_stream.c
+++ b/libproto/pop/pop3_stream.c
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/pop/pop3_timeout.c b/libproto/pop/pop3_timeout.c
index 32b01f0..9e22265 100644
--- a/libproto/pop/pop3_timeout.c
+++ b/libproto/pop/pop3_timeout.c
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/pop/pop3_top.c b/libproto/pop/pop3_top.c
index c060736..c43c6b3 100644
--- a/libproto/pop/pop3_top.c
+++ b/libproto/pop/pop3_top.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/pop/pop3_trace.c b/libproto/pop/pop3_trace.c
index 5fa3d95..8ed7eab 100644
--- a/libproto/pop/pop3_trace.c
+++ b/libproto/pop/pop3_trace.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/pop/pop3_uidl.c b/libproto/pop/pop3_uidl.c
index c97cc32..c2fcdd4 100644
--- a/libproto/pop/pop3_uidl.c
+++ b/libproto/pop/pop3_uidl.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/pop/pop3_uidl_cmd.c b/libproto/pop/pop3_uidl_cmd.c
index 23d7866..fe7ff85 100644
--- a/libproto/pop/pop3_uidl_cmd.c
+++ b/libproto/pop/pop3_uidl_cmd.c
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/pop/pop3_uidla.c b/libproto/pop/pop3_uidla.c
index 78ff0ff..9418ae9 100644
--- a/libproto/pop/pop3_uidla.c
+++ b/libproto/pop/pop3_uidla.c
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/pop/pop3_uidlas.c b/libproto/pop/pop3_uidlas.c
index 82771fd..ba671d2 100644
--- a/libproto/pop/pop3_uidlas.c
+++ b/libproto/pop/pop3_uidlas.c
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/pop/pop3_user.c b/libproto/pop/pop3_user.c
index 5298a42..ee0ef85 100644
--- a/libproto/pop/pop3_user.c
+++ b/libproto/pop/pop3_user.c
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/libproto/pop/url.c b/libproto/pop/url.c
index e98b3a6..86992b6 100644
--- a/libproto/pop/url.c
+++ b/libproto/pop/url.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/maidag/Makefile.am b/maidag/Makefile.am
index 50fb709..b1a22f5 100644
--- a/maidag/Makefile.am
+++ b/maidag/Makefile.am
@@ -5,15 +5,13 @@
 # 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
+# GNU Mailutils 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
+# along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
 SUBDIRS = . tests
 
diff --git a/maidag/deliver.c b/maidag/deliver.c
index 7bdf29a..57684b9 100644
--- a/maidag/deliver.c
+++ b/maidag/deliver.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "maidag.h"
 
diff --git a/maidag/forward.c b/maidag/forward.c
index 3924eef..2bedd80 100644
--- a/maidag/forward.c
+++ b/maidag/forward.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 /* ".forward" support for GNU Maidag */
 
diff --git a/maidag/guile.c b/maidag/guile.c
index ca85618..e9af5ce 100644
--- a/maidag/guile.c
+++ b/maidag/guile.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "maidag.h"
 
diff --git a/maidag/lmtp.c b/maidag/lmtp.c
index 0595f30..77a54a4 100644
--- a/maidag/lmtp.c
+++ b/maidag/lmtp.c
@@ -12,9 +12,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "maidag.h"
 #include <netinet/in.h>
diff --git a/maidag/maidag.c b/maidag/maidag.c
index af9abf8..f67b581 100644
--- a/maidag/maidag.c
+++ b/maidag/maidag.c
@@ -12,9 +12,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "maidag.h"
 
diff --git a/maidag/maidag.h b/maidag/maidag.h
index 012230d..424df8b 100644
--- a/maidag/maidag.h
+++ b/maidag/maidag.h
@@ -12,9 +12,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #if defined(HAVE_CONFIG_H)
 # include <config.h>
diff --git a/maidag/mailquota.c b/maidag/mailquota.c
index ef0d94e..3ead0e6 100644
--- a/maidag/mailquota.c
+++ b/maidag/mailquota.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "maidag.h"
 
diff --git a/maidag/python.c b/maidag/python.c
index 71ff506..787266f 100644
--- a/maidag/python.c
+++ b/maidag/python.c
@@ -12,9 +12,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "maidag.h"
 
diff --git a/maidag/script.c b/maidag/script.c
index 280011e..96ca669 100644
--- a/maidag/script.c
+++ b/maidag/script.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "maidag.h"
 
diff --git a/maidag/sieve.c b/maidag/sieve.c
index f3cfd43..00b8a81 100644
--- a/maidag/sieve.c
+++ b/maidag/sieve.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "maidag.h"
 
diff --git a/maidag/tests/Makefile.am b/maidag/tests/Makefile.am
index eec3015..50c0e9f 100644
--- a/maidag/tests/Makefile.am
+++ b/maidag/tests/Makefile.am
@@ -6,7 +6,7 @@
 # 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
+# GNU Mailutils 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.
diff --git a/maidag/util.c b/maidag/util.c
index 068f928..1549b22 100644
--- a/maidag/util.c
+++ b/maidag/util.c
@@ -12,9 +12,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "maidag.h"
 
diff --git a/mail/Makefile.am b/mail/Makefile.am
index 3da9034..cca0beb 100644
--- a/mail/Makefile.am
+++ b/mail/Makefile.am
@@ -1,22 +1,19 @@
-## Process this file with GNU Automake to create Makefile.in
-
-##   Copyright (C) 1999, 2000, 2001, 2002, 2005, 2007, 2010 Free
-##   Software Foundation, Inc.
+## This file is part of GNU Mailutils.
+## Copyright (C) 1999, 2000, 2001, 2002, 2005, 2007, 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 2, or (at
-##   your option) any later version.
+## 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 2, 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.
+## GNU Mailutils 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
+## You should have received a copy of the GNU General Public License
+## along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. 
 
 AM_YFLAGS=-vt
 INCLUDES = @MU_APP_COMMON_INCLUDES@ @MU_AUTHINCS@ 
diff --git a/mail/alias.c b/mail/alias.c
index 31532ff..7ff2085 100644
--- a/mail/alias.c
+++ b/mail/alias.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "mail.h"
 
diff --git a/mail/alt.c b/mail/alt.c
index e38c6fb..c9545b3 100644
--- a/mail/alt.c
+++ b/mail/alt.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "mail.h"
 #include <pwd.h>
diff --git a/mail/cd.c b/mail/cd.c
index 01c199f..785c29f 100644
--- a/mail/cd.c
+++ b/mail/cd.c
@@ -12,9 +12,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "mail.h"
 
diff --git a/mail/copy.c b/mail/copy.c
index 43087db..e1775be 100644
--- a/mail/copy.c
+++ b/mail/copy.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "mail.h"
 
diff --git a/mail/decode.c b/mail/decode.c
index f508faf..09f22d1 100644
--- a/mail/decode.c
+++ b/mail/decode.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "mail.h"
 #include "mailcap.h"
diff --git a/mail/delete.c b/mail/delete.c
index 55a7978..402a648 100644
--- a/mail/delete.c
+++ b/mail/delete.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "mail.h"
 
diff --git a/mail/dp.c b/mail/dp.c
index d52ce1b..eebe354 100644
--- a/mail/dp.c
+++ b/mail/dp.c
@@ -12,9 +12,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "mail.h"
 
diff --git a/mail/echo.c b/mail/echo.c
index accafed..0579e20 100644
--- a/mail/echo.c
+++ b/mail/echo.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "mail.h"
 
diff --git a/mail/edit.c b/mail/edit.c
index 27a394d..0006a25 100644
--- a/mail/edit.c
+++ b/mail/edit.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "mail.h"
 
diff --git a/mail/eq.c b/mail/eq.c
index 99a509d..6a414d9 100644
--- a/mail/eq.c
+++ b/mail/eq.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "mail.h"
 
diff --git a/mail/escape.c b/mail/escape.c
index 0f3f57e..f35bbe1 100644
--- a/mail/escape.c
+++ b/mail/escape.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 /* Functions for handling escape variables */
 
diff --git a/mail/exit.c b/mail/exit.c
index af2d7bc..5118568 100644
--- a/mail/exit.c
+++ b/mail/exit.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "mail.h"
 
diff --git a/mail/file.c b/mail/file.c
index c0e3463..bf4acbd 100644
--- a/mail/file.c
+++ b/mail/file.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "mail.h"
 
diff --git a/mail/folders.c b/mail/folders.c
index d832da7..b21316c 100644
--- a/mail/folders.c
+++ b/mail/folders.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "mail.h"
 
diff --git a/mail/followup.c b/mail/followup.c
index 5c699d0..c8c5e26 100644
--- a/mail/followup.c
+++ b/mail/followup.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "mail.h"
 
diff --git a/mail/from.c b/mail/from.c
index 66b4c29..9515138 100644
--- a/mail/from.c
+++ b/mail/from.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "mail.h"
 #include <mu_umaxtostr.h>
diff --git a/mail/headers.c b/mail/headers.c
index 5e7e982..915e7c8 100644
--- a/mail/headers.c
+++ b/mail/headers.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "mail.h"
 
diff --git a/mail/help.c b/mail/help.c
index c6442f1..3454109 100644
--- a/mail/help.c
+++ b/mail/help.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "mail.h"
 
diff --git a/mail/hold.c b/mail/hold.c
index 8535cc9..2938125 100644
--- a/mail/hold.c
+++ b/mail/hold.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "mail.h"
 
diff --git a/mail/if.c b/mail/if.c
index 8a93618..12c6744 100644
--- a/mail/if.c
+++ b/mail/if.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "mail.h"
 
diff --git a/mail/inc.c b/mail/inc.c
index f0b6617..a9dec14 100644
--- a/mail/inc.c
+++ b/mail/inc.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "mail.h"
 
diff --git a/mail/list.c b/mail/list.c
index c6c4844..a6eb18f 100644
--- a/mail/list.c
+++ b/mail/list.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "mail.h"
 
diff --git a/mail/mail.c b/mail/mail.c
index 8a43b76..6cd68f3 100644
--- a/mail/mail.c
+++ b/mail/mail.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "mail.h"
 #include "mailutils/libargp.h"
@@ -584,8 +582,7 @@ mail_warranty (int argc MU_ARG_UNUSED, char **argv MU_ARG_UNUSED)
     "   GNU General Public License for more details.\n"
     "\n"
     "   You should have received a copy of the GNU General Public License along\n"
-    "   with GNU Mailutils; if not, write to the Free Software Foundation,\n"
-    "   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n"
+    "   with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.\n" 
     "\n"
     "\n"
 ),
diff --git a/mail/mail.h b/mail/mail.h
index 6c419d5..0efe8d3 100644
--- a/mail/mail.h
+++ b/mail/mail.h
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAIL_H
 #define _MAIL_H 1
diff --git a/mail/mailline.c b/mail/mailline.c
index cc0df6d..95e3243 100644
--- a/mail/mailline.c
+++ b/mail/mailline.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "mail.h"
 #include <sys/stat.h>
diff --git a/mail/mbox.c b/mail/mbox.c
index 2b093f2..c8a2337 100644
--- a/mail/mbox.c
+++ b/mail/mbox.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "mail.h"
 
diff --git a/mail/msgset.y b/mail/msgset.y
index f2c2a52..8f6ae9d 100644
--- a/mail/msgset.y
+++ b/mail/msgset.y
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 %{
 #include "mail.h"
diff --git a/mail/next.c b/mail/next.c
index 4b48b5f..aea6367 100644
--- a/mail/next.c
+++ b/mail/next.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "mail.h"
 
diff --git a/mail/page.c b/mail/page.c
index 760de42..952e4e2 100644
--- a/mail/page.c
+++ b/mail/page.c
@@ -12,9 +12,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "mail.h"
 
diff --git a/mail/pipe.c b/mail/pipe.c
index d057450..58fa64e 100644
--- a/mail/pipe.c
+++ b/mail/pipe.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "mail.h"
 
diff --git a/mail/previous.c b/mail/previous.c
index 25ed10d..81ca149 100644
--- a/mail/previous.c
+++ b/mail/previous.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "mail.h"
 
diff --git a/mail/print.c b/mail/print.c
index 89542f6..d851908 100644
--- a/mail/print.c
+++ b/mail/print.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "mail.h"
 
diff --git a/mail/quit.c b/mail/quit.c
index 4efeaf4..9632c72 100644
--- a/mail/quit.c
+++ b/mail/quit.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "mail.h"
 
diff --git a/mail/reply.c b/mail/reply.c
index 6a85ec7..ff5e8af 100644
--- a/mail/reply.c
+++ b/mail/reply.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "mail.h"
 
diff --git a/mail/retain.c b/mail/retain.c
index 07656aa..08d76a8 100644
--- a/mail/retain.c
+++ b/mail/retain.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "mail.h"
 
diff --git a/mail/save.c b/mail/save.c
index 0b681bd..b67a7f4 100644
--- a/mail/save.c
+++ b/mail/save.c
@@ -12,9 +12,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "mail.h"
 
diff --git a/mail/send.c b/mail/send.c
index 3f095b2..618f6a8 100644
--- a/mail/send.c
+++ b/mail/send.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "mail.h"
 #include <sys/types.h>
diff --git a/mail/set.c b/mail/set.c
index 6daf381..ae9a9c9 100644
--- a/mail/set.c
+++ b/mail/set.c
@@ -12,9 +12,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "mail.h"
 
diff --git a/mail/setenv.c b/mail/setenv.c
index ed73abf..cccb4f6 100644
--- a/mail/setenv.c
+++ b/mail/setenv.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "mail.h"
 
diff --git a/mail/shell.c b/mail/shell.c
index 3c44495..df710fa 100644
--- a/mail/shell.c
+++ b/mail/shell.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "mail.h"
 
diff --git a/mail/size.c b/mail/size.c
index c21b8bd..186d36d 100644
--- a/mail/size.c
+++ b/mail/size.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "mail.h"
 
diff --git a/mail/source.c b/mail/source.c
index c7aa43a..57a5ccb 100644
--- a/mail/source.c
+++ b/mail/source.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "mail.h"
 
diff --git a/mail/summary.c b/mail/summary.c
index 4e7721b..95ab7b5 100644
--- a/mail/summary.c
+++ b/mail/summary.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "mail.h"
 
diff --git a/mail/table.c b/mail/table.c
index c8996b8..3c2fd83 100644
--- a/mail/table.c
+++ b/mail/table.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "mail.h"
 
diff --git a/mail/tag.c b/mail/tag.c
index 198b041..5719a5e 100644
--- a/mail/tag.c
+++ b/mail/tag.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "mail.h"
 
diff --git a/mail/testsuite/Makefile.am b/mail/testsuite/Makefile.am
index bb8ddf8..0e41522 100644
--- a/mail/testsuite/Makefile.am
+++ b/mail/testsuite/Makefile.am
@@ -1,21 +1,18 @@
-## Process this file with GNU Automake to create Makefile.in
-
-##   Copyright (C) 2002, 2007, 2010 Free Software Foundation, Inc.
+## This file is part of GNU Mailutils.
+## Copyright (C) 2002, 2007, 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.
+## 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.
+## GNU Mailutils 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
+## You should have received a copy of the GNU General Public License
+## along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. 
 
 AUTOMAKE_OPTIONS = dejagnu
 DEJATOOL = mail
diff --git a/mail/testsuite/if.mail b/mail/testsuite/if.mail
index 5fa6a01..acc3d73 100644
--- a/mail/testsuite/if.mail
+++ b/mail/testsuite/if.mail
@@ -1,20 +1,18 @@
 # This file is part of Mailutils testsuite.
 # Copyright (C) 2005, 2007, 2010 Free Software Foundation, Inc.
 #  
-# This program is free software; you can redistribute it and/or modify
+# 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 of the License, or
 # (at your option) any later version.
 #   
-# This program is distributed in the hope that it will be useful,
+# GNU Mailutils 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.
+# along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. 
 
 echo "Begin test"
 
diff --git a/mail/testsuite/lib/mail.exp b/mail/testsuite/lib/mail.exp
index b0b23e9..3728bc3 100644
--- a/mail/testsuite/lib/mail.exp
+++ b/mail/testsuite/lib/mail.exp
@@ -2,20 +2,18 @@
 # This file is part of Mailutils testsuite.
 # Copyright (C) 2002, 2007, 2009, 2010 Free Software Foundation, Inc.
 #  
-# This program is free software; you can redistribute it and/or modify
+# 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 of the License, or
 # (at your option) any later version.
 #   
-# This program is distributed in the hope that it will be useful,
+# GNU Mailutils 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.
+# along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. 
 
 source $top_srcdir/testsuite/lib/mailutils.exp
 
diff --git a/mail/testsuite/mail/alias.exp b/mail/testsuite/mail/alias.exp
index 71b5dd9..9291858 100644
--- a/mail/testsuite/mail/alias.exp
+++ b/mail/testsuite/mail/alias.exp
@@ -2,20 +2,18 @@
 # This file is part of Mailutils testsuite.
 # Copyright (C) 2002, 2007, 2010 Free Software Foundation, Inc.
 #  
-# This program is free software; you can redistribute it and/or modify
+# 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 of the License, or
 # (at your option) any later version.
 #   
-# This program is distributed in the hope that it will be useful,
+# GNU Mailutils 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.
+# along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
 mail_start "--file=%mbox1"
 
diff --git a/mail/testsuite/mail/folder.exp b/mail/testsuite/mail/folder.exp
index 70a284b..4ddcf06 100644
--- a/mail/testsuite/mail/folder.exp
+++ b/mail/testsuite/mail/folder.exp
@@ -2,20 +2,18 @@
 # This file is part of Mailutils testsuite.
 # Copyright (C) 2002, 2007, 2010 Free Software Foundation, Inc.
 #  
-# This program is free software; you can redistribute it and/or modify
+# 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 of the License, or
 # (at your option) any later version.
 #   
-# This program is distributed in the hope that it will be useful,
+# GNU Mailutils 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.
+# along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. 
 
 # Test mailbox name expansion and various aspects of `file' and `folders'
 # functionality.
diff --git a/mail/testsuite/mail/if.exp b/mail/testsuite/mail/if.exp
index e93c262..877b13d 100644
--- a/mail/testsuite/mail/if.exp
+++ b/mail/testsuite/mail/if.exp
@@ -2,20 +2,18 @@
 # This file is part of Mailutils testsuite.
 # Copyright (C) 2002, 2005, 2007, 2009, 2010 Free Software Foundation, Inc.
 #  
-# This program is free software; you can redistribute it and/or modify
+# 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 of the License, or
 # (at your option) any later version.
 #   
-# This program is distributed in the hope that it will be useful,
+# GNU Mailutils 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.
+# along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. 
 
 mu_prepare_spools
 
diff --git a/mail/testsuite/mail/read.exp b/mail/testsuite/mail/read.exp
index fbb52ae..7e02fe5 100644
--- a/mail/testsuite/mail/read.exp
+++ b/mail/testsuite/mail/read.exp
@@ -2,20 +2,18 @@
 # This file is part of Mailutils testsuite.
 # Copyright (C) 2002, 2005, 2007, 2010 Free Software Foundation, Inc.
 #  
-# This program is free software; you can redistribute it and/or modify
+# 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 of the License, or
 # (at your option) any later version.
 #   
-# This program is distributed in the hope that it will be useful,
+# GNU Mailutils 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.
+# along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. 
 
 mail_start "--file=%mbox1"
 mail_command "set readonly"
diff --git a/mail/testsuite/mail/send.exp b/mail/testsuite/mail/send.exp
index 916a546..16b5ebb 100644
--- a/mail/testsuite/mail/send.exp
+++ b/mail/testsuite/mail/send.exp
@@ -2,20 +2,18 @@
 # This file is part of Mailutils testsuite.
 # Copyright (C) 2002, 2006, 2007, 2010 Free Software Foundation, Inc.
 #  
-# This program is free software; you can redistribute it and/or modify
+# 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 of the License, or
 # (at your option) any later version.
 #   
-# This program is distributed in the hope that it will be useful,
+# GNU Mailutils 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.
+# along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. 
 
 mail_version
 
diff --git a/mail/testsuite/mail/tag.exp b/mail/testsuite/mail/tag.exp
index edb2270..ac50953 100644
--- a/mail/testsuite/mail/tag.exp
+++ b/mail/testsuite/mail/tag.exp
@@ -2,20 +2,18 @@
 # This file is part of Mailutils testsuite.
 # Copyright (C) 2002, 2007, 2010 Free Software Foundation, Inc.
 #  
-# This program is free software; you can redistribute it and/or modify
+# 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 of the License, or
 # (at your option) any later version.
 #   
-# This program is distributed in the hope that it will be useful,
+# GNU Mailutils 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.
+# along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. 
 
 mail_start "--file=%teaparty.mbox"
 mail_command "set readonly"
diff --git a/mail/testsuite/mail/write.exp b/mail/testsuite/mail/write.exp
index d2cce6f..4743f95 100644
--- a/mail/testsuite/mail/write.exp
+++ b/mail/testsuite/mail/write.exp
@@ -2,20 +2,18 @@
 # This file is part of Mailutils testsuite.
 # Copyright (C) 2002, 2007, 2009, 2010 Free Software Foundation, Inc.
 #  
-# This program is free software; you can redistribute it and/or modify
+# 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 of the License, or
 # (at your option) any later version.
 #   
-# This program is distributed in the hope that it will be useful,
+# GNU Mailutils 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.
+# along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. 
 
 mail_start "--file=%mbox1"
 
diff --git a/mail/testsuite/mail/z.exp b/mail/testsuite/mail/z.exp
index 39b876a..ce4a92e 100644
--- a/mail/testsuite/mail/z.exp
+++ b/mail/testsuite/mail/z.exp
@@ -2,20 +2,18 @@
 # This file is part of Mailutils testsuite.
 # Copyright (C) 2002, 2005, 2007, 2010 Free Software Foundation, Inc.
 #  
-# This program is free software; you can redistribute it and/or modify
+# 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 of the License, or
 # (at your option) any later version.
 #   
-# This program is distributed in the hope that it will be useful,
+# GNU Mailutils 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.
+# along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
 mail_start "--file=%teaparty.mbox"
 mail_command "set readonly"
diff --git a/mail/testsuite/makespool b/mail/testsuite/makespool
index 190ba85..5a72446 100755
--- a/mail/testsuite/makespool
+++ b/mail/testsuite/makespool
@@ -13,8 +13,7 @@
 # GNU General Public License for more details.
 #
 # You should have received a copy of the GNU General Public License
-# along with GNU Mailutils; if not, write to the Free Software
-# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
+# along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. 
 
 
 if [ "$1" = "-r" ]; then
diff --git a/mail/top.c b/mail/top.c
index de030af..c70801f 100644
--- a/mail/top.c
+++ b/mail/top.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "mail.h"
 
diff --git a/mail/touch.c b/mail/touch.c
index 6f232f3..969f4da 100644
--- a/mail/touch.c
+++ b/mail/touch.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "mail.h"
 
diff --git a/mail/unalias.c b/mail/unalias.c
index 7370c24..d97c794 100644
--- a/mail/unalias.c
+++ b/mail/unalias.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "mail.h"
 
diff --git a/mail/undelete.c b/mail/undelete.c
index ca99b32..cac68e6 100644
--- a/mail/undelete.c
+++ b/mail/undelete.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "mail.h"
 
diff --git a/mail/unset.c b/mail/unset.c
index cbc6118..77e6fb4 100644
--- a/mail/unset.c
+++ b/mail/unset.c
@@ -12,9 +12,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "mail.h"
 
diff --git a/mail/util.c b/mail/util.c
index c64496d..683fdf9 100644
--- a/mail/util.c
+++ b/mail/util.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "mail.h"
 #include <mailutils/mutil.h>
diff --git a/mail/version.c b/mail/version.c
index 0136278..12c907b 100644
--- a/mail/version.c
+++ b/mail/version.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "mail.h"
 
diff --git a/mail/visual.c b/mail/visual.c
index 6103221..0f29ee1 100644
--- a/mail/visual.c
+++ b/mail/visual.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "mail.h"
 
diff --git a/mail/write.c b/mail/write.c
index 13ad960..4840873 100644
--- a/mail/write.c
+++ b/mail/write.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "mail.h"
 
diff --git a/mail/z.c b/mail/z.c
index a124cbe..52a90f5 100644
--- a/mail/z.c
+++ b/mail/z.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "mail.h"
 
diff --git a/mapi/MAPIAddress.c b/mapi/MAPIAddress.c
index 4466a02..c356bcf 100644
--- a/mapi/MAPIAddress.c
+++ b/mapi/MAPIAddress.c
@@ -13,8 +13,8 @@
    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., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA  */
+   License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #include <mailutils/mapi.h>
 
diff --git a/mapi/MAPIDeleteMail.c b/mapi/MAPIDeleteMail.c
index cf36a0b..8a5e14e 100644
--- a/mapi/MAPIDeleteMail.c
+++ b/mapi/MAPIDeleteMail.c
@@ -13,8 +13,8 @@
    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., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA  */
+   License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #include <mailutils/mapi.h>
 
diff --git a/mapi/MAPIDetails.c b/mapi/MAPIDetails.c
index c66ec84..3714487 100644
--- a/mapi/MAPIDetails.c
+++ b/mapi/MAPIDetails.c
@@ -13,8 +13,8 @@
    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., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA  */
+   License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #include <mailutils/mapi.h>
 
diff --git a/mapi/MAPIFindNext.c b/mapi/MAPIFindNext.c
index 1b87f36..88dd3d9 100644
--- a/mapi/MAPIFindNext.c
+++ b/mapi/MAPIFindNext.c
@@ -13,8 +13,8 @@
    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., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA  */
+   License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #include <mailutils/mapi.h>
 
diff --git a/mapi/MAPIFreeBuffer.c b/mapi/MAPIFreeBuffer.c
index ab1b0a9..13252a8 100644
--- a/mapi/MAPIFreeBuffer.c
+++ b/mapi/MAPIFreeBuffer.c
@@ -13,8 +13,8 @@
    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., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA  */
+   License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #include <mailutils/mapi.h>
 
diff --git a/mapi/MAPILogoff.c b/mapi/MAPILogoff.c
index 6cb0bac..0683f7e 100644
--- a/mapi/MAPILogoff.c
+++ b/mapi/MAPILogoff.c
@@ -13,8 +13,8 @@
    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., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA  */
+   License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #include <mailutils/mapi.h>
 
diff --git a/mapi/MAPILogon.c b/mapi/MAPILogon.c
index 95be7b7..1ddbd1a 100644
--- a/mapi/MAPILogon.c
+++ b/mapi/MAPILogon.c
@@ -13,8 +13,8 @@
    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., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA  */
+   License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #include <mailutils/mapi.h>
 
diff --git a/mapi/MAPIReadMail.c b/mapi/MAPIReadMail.c
index 0062355..f1a3502 100644
--- a/mapi/MAPIReadMail.c
+++ b/mapi/MAPIReadMail.c
@@ -13,8 +13,8 @@
    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., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA  */
+   License along with this library.  If not, 
+   see <http://www.gnu.org/licenses/>. */
 
 #include <mailutils/mapi.h>
 
diff --git a/mapi/MAPISaveMail.c b/mapi/MAPISaveMail.c
index 3f36067..6e43f95 100644
--- a/mapi/MAPISaveMail.c
+++ b/mapi/MAPISaveMail.c
@@ -13,8 +13,8 @@
    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., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA  */
+   License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #include <mailutils/mapi.h>
 
diff --git a/mapi/MAPISendDocuments.c b/mapi/MAPISendDocuments.c
index 80bb5e7..8c97088 100644
--- a/mapi/MAPISendDocuments.c
+++ b/mapi/MAPISendDocuments.c
@@ -13,8 +13,8 @@
    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., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA  */
+   License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #include <mailutils/mapi.h>
 
diff --git a/mapi/MAPISendMail.c b/mapi/MAPISendMail.c
index 034fc68..62dbafe 100644
--- a/mapi/MAPISendMail.c
+++ b/mapi/MAPISendMail.c
@@ -13,8 +13,8 @@
    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., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA  */
+   License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #include <mailutils/mapi.h>
 
diff --git a/mapi/Makefile.am b/mapi/Makefile.am
index fddc3bf..19efbda 100644
--- a/mapi/Makefile.am
+++ b/mapi/Makefile.am
@@ -1,21 +1,18 @@
-## Process this file with GNU Automake to create Makefile.in
-
-##   Copyright (C) 2001, 2002, 2007, 2010 Free Software Foundation, Inc.
+## This file is part of GNU Mailutils.
+## Copyright (C) 2001, 2002, 2007, 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.
+## 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.
+## GNU Mailutils 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.
-##   59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+## You should have received a copy of the GNU General Public License
+## along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. 
 
 INCLUDES = @MU_LIB_COMMON_INCLUDES@
 
diff --git a/mapi/mapi.h b/mapi/mapi.h
index 4294b6f..8f63040 100644
--- a/mapi/mapi.h
+++ b/mapi/mapi.h
@@ -13,8 +13,8 @@
    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., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA  */
+   License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifndef _MAPI_H
 #define _MAPI_H
diff --git a/messages/Makefile.am b/messages/Makefile.am
index dbbbdd7..4632cc1 100644
--- a/messages/Makefile.am
+++ b/messages/Makefile.am
@@ -1,22 +1,19 @@
-## Process this file with GNU Automake to create Makefile.in
-
-##   Copyright (C) 2001, 2002, 2003, 2004, 2007, 2010 Free Software
-##   Foundation, Inc.
+## This file is part of GNU Mailutils.
+## Copyright (C) 2001, 2002, 2003, 2004, 2007, 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.
+## 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.
+## GNU Mailutils 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
+## You should have received a copy of the GNU General Public License
+## along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. 
 
 INCLUDES = @MU_APP_COMMON_INCLUDES@ 
 
diff --git a/messages/messages.c b/messages/messages.c
index 62c38b8..e467a9b 100644
--- a/messages/messages.c
+++ b/messages/messages.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/messages/tests/Makefile.am b/messages/tests/Makefile.am
index b548c51..d587c2f 100644
--- a/messages/tests/Makefile.am
+++ b/messages/tests/Makefile.am
@@ -6,7 +6,7 @@
 # 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
+# GNU Mailutils 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.
diff --git a/messages/tests/testsuite.at b/messages/tests/testsuite.at
index 627b1bb..ee855b0 100644
--- a/messages/tests/testsuite.at
+++ b/messages/tests/testsuite.at
@@ -6,7 +6,7 @@
 # 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
+# GNU Mailutils 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.
diff --git a/mh/Makefile.am b/mh/Makefile.am
index 8cb3f09..2cceef0 100644
--- a/mh/Makefile.am
+++ b/mh/Makefile.am
@@ -1,22 +1,19 @@
-## Process this file with GNU Automake to create Makefile.in
-
-##   Copyright (C) 2001, 2002, 2003, 2007, 2009, 2010 Free Software
-##   Foundation, Inc.
+## This file is part of GNU Mailutils.
+## Copyright (C) 2001, 2002, 2003, 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.
+## 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.
+## GNU Mailutils 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
+## You should have received a copy of the GNU General Public License
+## along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. 
 
 bindir = @MH_BIN_DIR@
 mhlibdir = $(pkgdatadir)/mh
diff --git a/mh/ali.c b/mh/ali.c
index 0c357cf..fa11429 100644
--- a/mh/ali.c
+++ b/mh/ali.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 /* MH ali command */
 
diff --git a/mh/anno.c b/mh/anno.c
index efc22d3..aa16f32 100644
--- a/mh/anno.c
+++ b/mh/anno.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 /* MH annotate command */
 
diff --git a/mh/burst.c b/mh/burst.c
index b413c07..1078fda 100644
--- a/mh/burst.c
+++ b/mh/burst.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 /* MH burst command */
 
diff --git a/mh/comp.c b/mh/comp.c
index f65fab2..0f8830d 100644
--- a/mh/comp.c
+++ b/mh/comp.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 /* MH comp command */
 
diff --git a/mh/compcommon.c b/mh/compcommon.c
index e85da11..312faf3 100644
--- a/mh/compcommon.c
+++ b/mh/compcommon.c
@@ -12,9 +12,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 /* Functions shared between comp and forw utilities */
 
diff --git a/mh/fmtcheck.c b/mh/fmtcheck.c
index 33c456a..216544d 100644
--- a/mh/fmtcheck.c
+++ b/mh/fmtcheck.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 /* fmtcheck */
 
diff --git a/mh/folder.c b/mh/folder.c
index c2baf4c..5394f2b 100644
--- a/mh/folder.c
+++ b/mh/folder.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 /* MH folder command */
 
diff --git a/mh/forw.c b/mh/forw.c
index 6e63d23..3889601 100644
--- a/mh/forw.c
+++ b/mh/forw.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 /* MH forw command */
 
diff --git a/mh/inc.c b/mh/inc.c
index 184a056..d2fb0a3 100644
--- a/mh/inc.c
+++ b/mh/inc.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 /* MH inc command */
 
diff --git a/mh/install-mh.c b/mh/install-mh.c
index a563c5f..1363a65 100644
--- a/mh/install-mh.c
+++ b/mh/install-mh.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include <mh.h>
 
diff --git a/mh/mailutils-mh.eli b/mh/mailutils-mh.eli
index c3bc6a5..93330af 100644
--- a/mh/mailutils-mh.eli
+++ b/mh/mailutils-mh.eli
@@ -13,8 +13,7 @@
 ;;;; GNU General Public License for more details.
 ;;;; 
 ;;;; You should have received a copy of the GNU General Public License
-;;;; along with GNU Mailutils; if not, write to the Free Software
-;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA  
+;;;; along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. 
 
 (setq mh-progs "BINDIR")
 (setq mh-lib "MHLIBDIR")
diff --git a/mh/mark.c b/mh/mark.c
index 5f67a63..ec2e46e 100644
--- a/mh/mark.c
+++ b/mh/mark.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include <mh.h>
 
diff --git a/mh/mh.h b/mh/mh.h
index 005a533..3f4566f 100644
--- a/mh/mh.h
+++ b/mh/mh.h
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/mh/mh_alias.l b/mh/mh_alias.l
index 926348c..b002343 100644
--- a/mh/mh_alias.l
+++ b/mh/mh_alias.l
@@ -14,9 +14,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/mh/mh_alias.y b/mh/mh_alias.y
index ca27e8b..e34509c 100644
--- a/mh/mh_alias.y
+++ b/mh/mh_alias.y
@@ -14,9 +14,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include <mh.h>
 #include <pwd.h>
diff --git a/mh/mh_argp.c b/mh/mh_argp.c
index be7ed16..8978d4c 100644
--- a/mh/mh_argp.c
+++ b/mh/mh_argp.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 /* Coexistence between GNU long options, traditional UNIX-style short
    options and traditional MH long options. */
@@ -283,8 +281,7 @@ mh_license (const char *name)
     "   GNU General Public License for more details.\n"
     "\n"
     "   You should have received a copy of the GNU General Public License along\n"
-    "   with GNU Mailutils; if not, write to the Free Software Foundation,\n"
-    "   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n"
+    "   with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.\n"
     "\n"
     "\n"
 ));
diff --git a/mh/mh_ctx.c b/mh/mh_ctx.c
index 0b4795d..33a1069 100644
--- a/mh/mh_ctx.c
+++ b/mh/mh_ctx.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 /* MH context functions. */
   
diff --git a/mh/mh_fmtgram.y b/mh/mh_fmtgram.y
index 2727331..7af0d9f 100644
--- a/mh/mh_fmtgram.y
+++ b/mh/mh_fmtgram.y
@@ -14,9 +14,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include <mh.h>
 #define obstack_chunk_alloc malloc
diff --git a/mh/mh_format.c b/mh/mh_format.c
index 7d548c0..f084f31 100644
--- a/mh/mh_format.c
+++ b/mh/mh_format.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 /* This module implements execution of MH format strings. */
 
diff --git a/mh/mh_format.h b/mh/mh_format.h
index de4f30b..64c1a66 100644
--- a/mh/mh_format.h
+++ b/mh/mh_format.h
@@ -12,9 +12,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #define obstack_chunk_alloc malloc
 #define obstack_chunk_free free
diff --git a/mh/mh_getopt.c b/mh/mh_getopt.c
index 2a6c949..09b8a3e 100644
--- a/mh/mh_getopt.c
+++ b/mh/mh_getopt.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 /* Parse traditional MH options. */
 
diff --git a/mh/mh_getopt.h b/mh/mh_getopt.h
index beb6fff..9aff8c9 100644
--- a/mh/mh_getopt.h
+++ b/mh/mh_getopt.h
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include <mailutils/nls.h>
 #include "argp.h"
diff --git a/mh/mh_global.c b/mh/mh_global.c
index 7b5c054..9578a8f 100644
--- a/mh/mh_global.c
+++ b/mh/mh_global.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 /* Global MH state. */
 
diff --git a/mh/mh_init.c b/mh/mh_init.c
index 7043d1a..b1c20c9 100644
--- a/mh/mh_init.c
+++ b/mh/mh_init.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 /* Initialize MH applications. */
 
diff --git a/mh/mh_list.c b/mh/mh_list.c
index 4df743f..a414574 100644
--- a/mh/mh_list.c
+++ b/mh/mh_list.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 /* MH mhl command */
 
diff --git a/mh/mh_msgset.c b/mh/mh_msgset.c
index 4376732..f4706fd 100644
--- a/mh/mh_msgset.c
+++ b/mh/mh_msgset.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 /* MH message sets. */
 
diff --git a/mh/mh_sequence.c b/mh/mh_sequence.c
index a12407c..4e7f5cf 100644
--- a/mh/mh_sequence.c
+++ b/mh/mh_sequence.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include <mh.h>
 
diff --git a/mh/mh_stream.c b/mh/mh_stream.c
index 0e2e6fe..9878503 100644
--- a/mh/mh_stream.c
+++ b/mh/mh_stream.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 /* This file implements an MH draftfile stream: a read-only stream used
    to transparently pass MH draftfiles to mailers. The only difference
diff --git a/mh/mh_whatnow.c b/mh/mh_whatnow.c
index 3ec4533..527e73a 100644
--- a/mh/mh_whatnow.c
+++ b/mh/mh_whatnow.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include <mh.h>
 
diff --git a/mh/mh_whom.c b/mh/mh_whom.c
index df82cf8..4b70b25 100644
--- a/mh/mh_whom.c
+++ b/mh/mh_whom.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include <mh.h>
 
diff --git a/mh/mhl.c b/mh/mhl.c
index 030fe42..5f6b6fc 100644
--- a/mh/mhl.c
+++ b/mh/mhl.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 /* MH mhl command */
 
diff --git a/mh/mhn.c b/mh/mhn.c
index d25932f..23c8872 100644
--- a/mh/mhn.c
+++ b/mh/mhn.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 /* MH mhn command */
 
diff --git a/mh/mhparam.c b/mh/mhparam.c
index f9b7a23..9149c1c 100644
--- a/mh/mhparam.c
+++ b/mh/mhparam.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 /* MH mhparam command */
 
diff --git a/mh/mhpath.c b/mh/mhpath.c
index 04d58f8..5051418 100644
--- a/mh/mhpath.c
+++ b/mh/mhpath.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 /* MH mhpath command */
 
diff --git a/mh/pick.c b/mh/pick.c
index f51a386..122734c 100644
--- a/mh/pick.c
+++ b/mh/pick.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 /* MH pick command */
 
diff --git a/mh/pick.h b/mh/pick.h
index 53c3fa3..4ef9e55 100644
--- a/mh/pick.h
+++ b/mh/pick.h
@@ -12,9 +12,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 typedef enum
 {
diff --git a/mh/pick.y b/mh/pick.y
index 64248ec..b123849 100644
--- a/mh/pick.y
+++ b/mh/pick.y
@@ -14,9 +14,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include <mh.h>
 #include <regex.h>  
diff --git a/mh/refile.c b/mh/refile.c
index f244e70..ab66730 100644
--- a/mh/refile.c
+++ b/mh/refile.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 /* MH refile command */
 
diff --git a/mh/repl.c b/mh/repl.c
index afb9233..5bfccba 100644
--- a/mh/repl.c
+++ b/mh/repl.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 /* MH repl command */
 
diff --git a/mh/rmf.c b/mh/rmf.c
index 31fb44e..469f770 100644
--- a/mh/rmf.c
+++ b/mh/rmf.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 /* MH rmf command */
 
diff --git a/mh/rmm.c b/mh/rmm.c
index e82ac79..a9bbfc8 100644
--- a/mh/rmm.c
+++ b/mh/rmm.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 /* MH rmm command */
 
diff --git a/mh/scan.c b/mh/scan.c
index 308d772..3bc3816 100644
--- a/mh/scan.c
+++ b/mh/scan.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 /* MH scan command */
 
diff --git a/mh/send.c b/mh/send.c
index 6ba5c46..a642d80 100644
--- a/mh/send.c
+++ b/mh/send.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 /* MH send command */
 
diff --git a/mh/sortm.c b/mh/sortm.c
index cdea0bd..fd40de6 100644
--- a/mh/sortm.c
+++ b/mh/sortm.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 /* MH sortm command */
 
diff --git a/mh/whatnow.c b/mh/whatnow.c
index 3075d5a..decf947 100644
--- a/mh/whatnow.c
+++ b/mh/whatnow.c
@@ -12,9 +12,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 /* MH whatnow command */
 
diff --git a/mh/whom.c b/mh/whom.c
index 9db8446..b943e97 100644
--- a/mh/whom.c
+++ b/mh/whom.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include <mh.h>
 
diff --git a/mimeview/Makefile.am b/mimeview/Makefile.am
index 86bcdc8..4dc8df8 100644
--- a/mimeview/Makefile.am
+++ b/mimeview/Makefile.am
@@ -1,21 +1,18 @@
-## Process this file with GNU Automake to create Makefile.in
-
-##   Copyright (C) 2005, 2007, 2010 Free Software Foundation, Inc.
+## This file is part of GNU Mailutils.
+## Copyright (C) 2005, 2007, 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.
+## 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.
+## GNU Mailutils 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
+## You should have received a copy of the GNU General Public License
+## along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. 
 
 INCLUDES = @MU_APP_COMMON_INCLUDES@ 
 
diff --git a/mimeview/mimetypes.l b/mimeview/mimetypes.l
index f1e0b90..a4dbf20 100644
--- a/mimeview/mimetypes.l
+++ b/mimeview/mimetypes.l
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/mimeview/mimetypes.y b/mimeview/mimetypes.y
index 4f50773..50be133 100644
--- a/mimeview/mimetypes.y
+++ b/mimeview/mimetypes.y
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/mimeview/mimeview.c b/mimeview/mimeview.c
index 8b32311..0601255 100644
--- a/mimeview/mimeview.c
+++ b/mimeview/mimeview.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/mimeview/mimeview.h b/mimeview/mimeview.h
index 972f390..a77dc5a 100644
--- a/mimeview/mimeview.h
+++ b/mimeview/mimeview.h
@@ -12,9 +12,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include <stdlib.h>
 #include <stdio.h>
diff --git a/movemail/Makefile.am b/movemail/Makefile.am
index 7bc6228..9588330 100644
--- a/movemail/Makefile.am
+++ b/movemail/Makefile.am
@@ -1,22 +1,19 @@
-## Process this file with GNU Automake to create Makefile.in
-
-##   Copyright (C) 1999, 2000, 2001, 2002, 2006, 2007, 2010 Free
-##   Software Foundation, Inc.
+## This file is part of GNU Mailutils.
+## Copyright (C) 1999, 2000, 2001, 2002, 2006, 2007, 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.
+## 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.
+## GNU Mailutils 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
+## You should have received a copy of the GNU General Public License
+## along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. 
 
 INCLUDES = @MU_APP_COMMON_INCLUDES@ @MU_AUTHINCS@
 bin_PROGRAMS = movemail
diff --git a/movemail/movemail.c b/movemail/movemail.c
index ddbeb14..4218849 100644
--- a/movemail/movemail.c
+++ b/movemail/movemail.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #if defined(HAVE_CONFIG_H)
 # include <config.h>
diff --git a/mu-aux/Makefile.am b/mu-aux/Makefile.am
index 28a299b..7273a9f 100644
--- a/mu-aux/Makefile.am
+++ b/mu-aux/Makefile.am
@@ -1,22 +1,19 @@
-## Process this file with GNU Automake to create Makefile.in
-
-##   Copyright (C) 2002, 2003, 2005, 2007, 2010 Free Software
-##   Foundation, Inc.
+## This file is part of GNU Mailutils.
+## Copyright (C) 2002, 2003, 2005, 2007, 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.
+## 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.
+## GNU Mailutils 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
+## You should have received a copy of the GNU General Public License
+## along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. 
 
 EXTRA_DIST = \
  debugdef.m4\
diff --git a/mu-aux/debugdef.m4 b/mu-aux/debugdef.m4
index 34be1cd..ab9ee70 100644
--- a/mu-aux/debugdef.m4
+++ b/mu-aux/debugdef.m4
@@ -4,18 +4,18 @@ divert(-1)
 #
 # Initially written by Sergey Poznyakoff for Mailfromd project.
 #
-# This program is free software; you can redistribute it and/or modify
+# 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,
+# GNU Mailutils 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, see <http://www.gnu.org/licenses/>.
+# along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
 changecom(/*,*/)
 
diff --git a/mu-aux/generr.awk b/mu-aux/generr.awk
index a40690c..e0efc71 100644
--- a/mu-aux/generr.awk
+++ b/mu-aux/generr.awk
@@ -6,17 +6,15 @@
 # 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
+# GNU Mailutils 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
+# along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. 
 
-# This program creates error reporting source files (muerrno.c and errno.h)
+# GNU Mailutils creates error reporting source files (muerrno.c and errno.h)
 # for GNU Mailutils.
 #
 # Usage:
diff --git a/mu-aux/gylwrap b/mu-aux/gylwrap
index 66dbdd0..20d9b40 100755
--- a/mu-aux/gylwrap
+++ b/mu-aux/gylwrap
@@ -15,8 +15,7 @@
 # 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.
+# along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 
 # Usage:
 #     ylwrap PROGRAM [ARGS] INPUT [OUTPUT DESIRED]... -- [-yy repl] [ARGS]...
diff --git a/mu-aux/texify.sed b/mu-aux/texify.sed
index 468001a..e344122 100644
--- a/mu-aux/texify.sed
+++ b/mu-aux/texify.sed
@@ -5,15 +5,13 @@
 # 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
+# GNU Mailutils 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
+# along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. 
 
 1{s/\/\*/@comment  /
 b
diff --git a/mu/Makefile.am b/mu/Makefile.am
index d2a2f3d..e724b0c 100644
--- a/mu/Makefile.am
+++ b/mu/Makefile.am
@@ -7,13 +7,13 @@
 ## published by the Free Software Foundation; either version 2, or (at
 ## your option) any later version.
 ##
-## This program is distributed in the hope that it will be useful, but
+## GNU Mailutils 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, see <http://www.gnu.org/licenses/>.
+## along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
 bin_PROGRAMS = mu
 
diff --git a/paths b/paths
index 45ad28e..6efbee1 100644
--- a/paths
+++ b/paths
@@ -2,18 +2,18 @@
 #
 # Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 #
-# This program is free software; you can redistribute it and/or modify
+# 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,
+# GNU Mailutils 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, see <http://www.gnu.org/licenses/>.
+# along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
 #
 # This file describes how to set various paths used by libraries and
diff --git a/po/POTFILES.in b/po/POTFILES.in
index a189a7d..30c0f15 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -120,25 +120,25 @@ mail/unalias.c
 mail/util.c
 mail/z.c
 
-libmailutils/cfg_driver.c
-libmailutils/cfg_format.c
-libmailutils/cfg_lexer.l
-libmailutils/cfg_parser.y
-libmailutils/debug.c
-libmailutils/diag.c
-libmailutils/errors
-libmailutils/file_stream.c
-libmailutils/filter_iconv.c
-libmailutils/gdebug.c
-libmailutils/gocs.c
-libmailutils/ipsrv.c
-libmailutils/message.c
-libmailutils/msrv.c
-libmailutils/mu_auth.c
-libmailutils/mutil.c
-libmailutils/parsedate.y
-libmailutils/registrar.c
-libmailutils/version.c
+libmailutils/cfg/driver.c
+libmailutils/cfg/format.c
+libmailutils/cfg/lexer.l
+libmailutils/cfg/parser.y
+libmailutils/cfg/gocs.c
+libmailutils/diag/debug.c
+libmailutils/diag/diag.c
+libmailutils/diag/errors
+libmailutils/diag/gdebug.c
+libmailutils/stream/file_stream.c
+libmailutils/filter/filter_iconv.c
+libmailutils/server/ipsrv.c
+libmailutils/server/msrv.c
+libmailutils/mailbox/message.c
+libmailutils/auth/mu_auth.c
+libmailutils/base/mutil.c
+libmailutils/base/parsedate.y
+libmailutils/base/registrar.c
+libmailutils/base/version.c
 
 messages/messages.c
 
diff --git a/pop3d/Makefile.am b/pop3d/Makefile.am
index 171733f..38576a6 100644
--- a/pop3d/Makefile.am
+++ b/pop3d/Makefile.am
@@ -1,22 +1,19 @@
-## Process this file with GNU Automake to create Makefile.in
-
-##   Copyright (C) 1999, 2000, 2001, 2002, 2003, 2005, 2007, 2010 Free
-##   Software Foundation, Inc.
+## This file is part of GNU Mailutils.
+## Copyright (C) 1999, 2000, 2001, 2002, 2003, 2005, 2007, 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.
+## 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.
 ##
-##   GNU Mailutils 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.
+## GNU Mailutils 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 GNU Mailutils; if not, write to the Free Software
-##   Foundation, Inc.  51 Franklin Street, Fifth Floor, Boston, MA
-##   02110-1301 USA
+## You should have received a copy of the GNU General Public License
+## along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. 
 
 INCLUDES = @MU_APP_COMMON_INCLUDES@ 
 
diff --git a/pop3d/bulletin.c b/pop3d/bulletin.c
index 031ad49..2d53a46 100644
--- a/pop3d/bulletin.c
+++ b/pop3d/bulletin.c
@@ -12,9 +12,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "pop3d.h"
 
diff --git a/pop3d/expire.c b/pop3d/expire.c
index da19ef3..a43ee4e 100644
--- a/pop3d/expire.c
+++ b/pop3d/expire.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "pop3d.h"
 
diff --git a/pop3d/extra.c b/pop3d/extra.c
index a52fee2..d6f2116 100644
--- a/pop3d/extra.c
+++ b/pop3d/extra.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "pop3d.h"
 #include "mailutils/libargp.h"
diff --git a/pop3d/lock.c b/pop3d/lock.c
index 590b9e2..d3bbfe9 100644
--- a/pop3d/lock.c
+++ b/pop3d/lock.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "pop3d.h"
 
diff --git a/pop3d/logindelay.c b/pop3d/logindelay.c
index 5cab22c..7b3bf35 100644
--- a/pop3d/logindelay.c
+++ b/pop3d/logindelay.c
@@ -12,9 +12,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "pop3d.h"
 
diff --git a/pop3d/pop3d.c b/pop3d/pop3d.c
index ab5ed7c..b058147 100644
--- a/pop3d/pop3d.c
+++ b/pop3d/pop3d.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "pop3d.h"
 #include "mailutils/pam.h"
diff --git a/pop3d/pop3d.h b/pop3d/pop3d.h
index 862ce72..b93284b 100644
--- a/pop3d/pop3d.h
+++ b/pop3d/pop3d.h
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #ifndef _POP3D_H
 #define _POP3D_H	1
diff --git a/pop3d/popauth.c b/pop3d/popauth.c
index f8505f9..b22cb66 100644
--- a/pop3d/popauth.c
+++ b/pop3d/popauth.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "pop3d.h"
 #include <mailutils/argcv.h>
diff --git a/pop3d/signal.c b/pop3d/signal.c
index b12bca7..a8a6f0c 100644
--- a/pop3d/signal.c
+++ b/pop3d/signal.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "pop3d.h"
 
diff --git a/pop3d/stat.c b/pop3d/stat.c
index 9daf3ed..f108b76 100644
--- a/pop3d/stat.c
+++ b/pop3d/stat.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "pop3d.h"
 
diff --git a/pop3d/testsuite/Makefile.am b/pop3d/testsuite/Makefile.am
index 0b9982f..9e8335a 100644
--- a/pop3d/testsuite/Makefile.am
+++ b/pop3d/testsuite/Makefile.am
@@ -1,21 +1,18 @@
-## Process this file with GNU Automake to create Makefile.in
-
-##   Copyright (C) 2002, 2007, 2008, 2010 Free Software Foundation, Inc.
+## This file is part of GNU Mailutils.
+## Copyright (C) 2002, 2007, 2008, 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.
+## 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.
+## GNU Mailutils 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
+## You should have received a copy of the GNU General Public License
+## along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. 
 
 AUTOMAKE_OPTIONS = dejagnu
 DEJATOOL = pop3d
diff --git a/pop3d/testsuite/lib/pop3d.exp b/pop3d/testsuite/lib/pop3d.exp
index e75b24e..69de7d6 100644
--- a/pop3d/testsuite/lib/pop3d.exp
+++ b/pop3d/testsuite/lib/pop3d.exp
@@ -2,20 +2,18 @@
 # This file is part of Mailutils testsuite.
 # Copyright (C) 2002, 2007, 2008, 2010 Free Software Foundation, Inc.
 #  
-# This program is free software; you can redistribute it and/or modify
+# 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 of the License, or
 # (at your option) any later version.
 #   
-# This program is distributed in the hope that it will be useful,
+# GNU Mailutils 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.
+# along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. 
 
 source $top_srcdir/testsuite/lib/mailutils.exp
 
diff --git a/pop3d/testsuite/pop3d/read.exp b/pop3d/testsuite/pop3d/read.exp
index c748133..8e8cc7c 100644
--- a/pop3d/testsuite/pop3d/read.exp
+++ b/pop3d/testsuite/pop3d/read.exp
@@ -2,20 +2,18 @@
 # This file is part of Mailutils testsuite.
 # Copyright (C) 2002, 2007, 2010 Free Software Foundation, Inc.
 #  
-# This program is free software; you can redistribute it and/or modify
+# 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 of the License, or
 # (at your option) any later version.
 #   
-# This program is distributed in the hope that it will be useful,
+# GNU Mailutils 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.
+# along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. 
 
 pop3d_start 
 pop3d_auth "user!passwd" "guessme"
diff --git a/python/Makefile.am b/python/Makefile.am
index fcbb7a2..097e5e0 100644
--- a/python/Makefile.am
+++ b/python/Makefile.am
@@ -1,21 +1,18 @@
-## Process this file with GNU Automake to create Makefile.in
-
-##   Copyright (C) 2009, 2010 Free Software Foundation, Inc.
+## This file is part of GNU Mailutils.
+## Copyright (C) 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.
+## 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.
+## GNU Mailutils 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
+## You should have received a copy of the GNU General Public License
+## along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. 
 
 SUBDIRS = libmu_py mailutils
 
diff --git a/python/libmu_py/Makefile.am b/python/libmu_py/Makefile.am
index 4051143..4b70bff 100644
--- a/python/libmu_py/Makefile.am
+++ b/python/libmu_py/Makefile.am
@@ -1,21 +1,18 @@
-## Process this file with GNU Automake to create Makefile.in
-
-##   Copyright (C) 2009, 2010 Free Software Foundation, Inc.
+## This file is part of GNU Mailutils.
+## Copyright (C) 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.
+## 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.
+## GNU Mailutils 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
+## You should have received a copy of the GNU General Public License
+## along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. 
 
 INCLUDES = @MU_LIB_COMMON_INCLUDES@ $(PYTHON_INCLUDES)
 
diff --git a/python/libmu_py/address.c b/python/libmu_py/address.c
index 611de20..c0dcb5f 100644
--- a/python/libmu_py/address.c
+++ b/python/libmu_py/address.c
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 
    This library is free software; you can redistribute it and/or
@@ -13,10 +12,8 @@
    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
-*/
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #include "libmu_py.h"
 
diff --git a/python/libmu_py/attribute.c b/python/libmu_py/attribute.c
index df51fa3..46c8226 100644
--- a/python/libmu_py/attribute.c
+++ b/python/libmu_py/attribute.c
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 
    This library is free software; you can redistribute it and/or
@@ -13,10 +12,8 @@
    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
-*/
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #include "libmu_py.h"
 
diff --git a/python/libmu_py/auth.c b/python/libmu_py/auth.c
index 6f342bc..678b8b9 100644
--- a/python/libmu_py/auth.c
+++ b/python/libmu_py/auth.c
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 
    This library is free software; you can redistribute it and/or
@@ -13,10 +12,8 @@
    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
-*/
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #include "libmu_py.h"
 #include <mailutils/pam.h>
diff --git a/python/libmu_py/body.c b/python/libmu_py/body.c
index 81606ea..8ddc517 100644
--- a/python/libmu_py/body.c
+++ b/python/libmu_py/body.c
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 
    This library is free software; you can redistribute it and/or
@@ -13,10 +12,8 @@
    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
-*/
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #include "libmu_py.h"
 
diff --git a/python/libmu_py/c_api.c b/python/libmu_py/c_api.c
index 8914b20..aab2345 100644
--- a/python/libmu_py/c_api.c
+++ b/python/libmu_py/c_api.c
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 
    This library is free software; you can redistribute it and/or
@@ -13,10 +12,8 @@
    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
-*/
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #include "libmu_py.h"
 
diff --git a/python/libmu_py/debug.c b/python/libmu_py/debug.c
index ac79d35..bb9da85 100644
--- a/python/libmu_py/debug.c
+++ b/python/libmu_py/debug.c
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 
    This library is free software; you can redistribute it and/or
@@ -13,10 +12,8 @@
    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
-*/
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #include "libmu_py.h"
 
diff --git a/python/libmu_py/envelope.c b/python/libmu_py/envelope.c
index 617b398..81d52d3 100644
--- a/python/libmu_py/envelope.c
+++ b/python/libmu_py/envelope.c
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 
    This library is free software; you can redistribute it and/or
@@ -13,10 +12,8 @@
    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
-*/
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #include "libmu_py.h"
 
diff --git a/python/libmu_py/error.c b/python/libmu_py/error.c
index 77013c9..028a8b5 100644
--- a/python/libmu_py/error.c
+++ b/python/libmu_py/error.c
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 
    This library is free software; you can redistribute it and/or
@@ -13,10 +12,8 @@
    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
-*/
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #include "libmu_py.h"
 
diff --git a/python/libmu_py/filter.c b/python/libmu_py/filter.c
index 01c53b3..8e983e4 100644
--- a/python/libmu_py/filter.c
+++ b/python/libmu_py/filter.c
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 
    This library is free software; you can redistribute it and/or
@@ -13,10 +12,8 @@
    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
-*/
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #include "libmu_py.h"
 
diff --git a/python/libmu_py/folder.c b/python/libmu_py/folder.c
index 4ba9ddf..e476efa 100644
--- a/python/libmu_py/folder.c
+++ b/python/libmu_py/folder.c
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 
    This library is free software; you can redistribute it and/or
@@ -13,10 +12,8 @@
    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
-*/
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #include "libmu_py.h"
 
diff --git a/python/libmu_py/header.c b/python/libmu_py/header.c
index 3af10e0..8962d83 100644
--- a/python/libmu_py/header.c
+++ b/python/libmu_py/header.c
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 
    This library is free software; you can redistribute it and/or
@@ -13,10 +12,8 @@
    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
-*/
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #include "libmu_py.h"
 
diff --git a/python/libmu_py/libmu_py.c b/python/libmu_py/libmu_py.c
index bf6da0c..e9792a2 100644
--- a/python/libmu_py/libmu_py.c
+++ b/python/libmu_py/libmu_py.c
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 
    This library is free software; you can redistribute it and/or
@@ -13,10 +12,8 @@
    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
-*/
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #include "libmu_py.h"
 
diff --git a/python/libmu_py/libmu_py.h b/python/libmu_py/libmu_py.h
index 21dc560..b637858 100644
--- a/python/libmu_py/libmu_py.h
+++ b/python/libmu_py/libmu_py.h
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 
    This library is free software; you can redistribute it and/or
@@ -13,10 +12,8 @@
    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
-*/
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/python/libmu_py/list.c b/python/libmu_py/list.c
index a7a9818..eb0f2a4 100644
--- a/python/libmu_py/list.c
+++ b/python/libmu_py/list.c
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 
    This library is free software; you can redistribute it and/or
@@ -13,10 +12,8 @@
    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
-*/
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #include "libmu_py.h"
 
diff --git a/python/libmu_py/mailbox.c b/python/libmu_py/mailbox.c
index b6eea4f..5fc79cc 100644
--- a/python/libmu_py/mailbox.c
+++ b/python/libmu_py/mailbox.c
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 
    This library is free software; you can redistribute it and/or
@@ -13,10 +12,8 @@
    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
-*/
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #include "libmu_py.h"
 
diff --git a/python/libmu_py/mailcap.c b/python/libmu_py/mailcap.c
index 8f65cbe..6bafd42 100644
--- a/python/libmu_py/mailcap.c
+++ b/python/libmu_py/mailcap.c
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 
    This library is free software; you can redistribute it and/or
@@ -13,10 +12,8 @@
    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
-*/
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #include "libmu_py.h"
 
diff --git a/python/libmu_py/mailer.c b/python/libmu_py/mailer.c
index 968ea02..b0e08e2 100644
--- a/python/libmu_py/mailer.c
+++ b/python/libmu_py/mailer.c
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 
    This library is free software; you can redistribute it and/or
@@ -13,10 +12,8 @@
    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
-*/
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #include "libmu_py.h"
 
diff --git a/python/libmu_py/message.c b/python/libmu_py/message.c
index 58f9c5b..78600cf 100644
--- a/python/libmu_py/message.c
+++ b/python/libmu_py/message.c
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 
    This library is free software; you can redistribute it and/or
@@ -13,10 +12,8 @@
    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
-*/
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #include "libmu_py.h"
 
diff --git a/python/libmu_py/mime.c b/python/libmu_py/mime.c
index fd9e1a0..85d872d 100644
--- a/python/libmu_py/mime.c
+++ b/python/libmu_py/mime.c
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 
    This library is free software; you can redistribute it and/or
@@ -13,10 +12,8 @@
    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
-*/
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #include "libmu_py.h"
 
diff --git a/python/libmu_py/nls.c b/python/libmu_py/nls.c
index 2d54c5c..cd811eb 100644
--- a/python/libmu_py/nls.c
+++ b/python/libmu_py/nls.c
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 
    This library is free software; you can redistribute it and/or
@@ -13,10 +12,8 @@
    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
-*/
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #include "libmu_py.h"
 
diff --git a/python/libmu_py/registrar.c b/python/libmu_py/registrar.c
index 88ef162..7878ac1 100644
--- a/python/libmu_py/registrar.c
+++ b/python/libmu_py/registrar.c
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 
    This library is free software; you can redistribute it and/or
@@ -13,10 +12,8 @@
    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
-*/
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #include "libmu_py.h"
 
diff --git a/python/libmu_py/script.c b/python/libmu_py/script.c
index da49146..7aca9ee 100644
--- a/python/libmu_py/script.c
+++ b/python/libmu_py/script.c
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 
    This library is free software; you can redistribute it and/or
@@ -13,10 +12,8 @@
    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
-*/
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #include "libmu_py.h"
 
diff --git a/python/libmu_py/secret.c b/python/libmu_py/secret.c
index 0033d02..c3ca0e3 100644
--- a/python/libmu_py/secret.c
+++ b/python/libmu_py/secret.c
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 
    This library is free software; you can redistribute it and/or
@@ -13,10 +12,8 @@
    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
-*/
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #include "libmu_py.h"
 
diff --git a/python/libmu_py/sieve.c b/python/libmu_py/sieve.c
index 9c38fa2..43200a4 100644
--- a/python/libmu_py/sieve.c
+++ b/python/libmu_py/sieve.c
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 
    This library is free software; you can redistribute it and/or
@@ -13,10 +12,8 @@
    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
-*/
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #include "libmu_py.h"
 
diff --git a/python/libmu_py/stream.c b/python/libmu_py/stream.c
index 0ef7385..4767376 100644
--- a/python/libmu_py/stream.c
+++ b/python/libmu_py/stream.c
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 
    This library is free software; you can redistribute it and/or
@@ -13,10 +12,8 @@
    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
-*/
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #include "libmu_py.h"
 
diff --git a/python/libmu_py/url.c b/python/libmu_py/url.c
index 2b8a7dc..f1b7eee 100644
--- a/python/libmu_py/url.c
+++ b/python/libmu_py/url.c
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 
    This library is free software; you can redistribute it and/or
@@ -13,10 +12,8 @@
    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
-*/
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #include "libmu_py.h"
 
diff --git a/python/libmu_py/util.c b/python/libmu_py/util.c
index 0741c25..929c74c 100644
--- a/python/libmu_py/util.c
+++ b/python/libmu_py/util.c
@@ -1,5 +1,4 @@
-/*
-   GNU Mailutils -- a suite of utilities for electronic mail
+/* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 
    This library is free software; you can redistribute it and/or
@@ -13,10 +12,8 @@
    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
-*/
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #include "libmu_py.h"
 
diff --git a/python/mailutils/Makefile.am b/python/mailutils/Makefile.am
index 5cf261d..d994cf3 100644
--- a/python/mailutils/Makefile.am
+++ b/python/mailutils/Makefile.am
@@ -1,21 +1,18 @@
-## Process this file with GNU Automake to create Makefile.in
-
-##   Copyright (C) 2009, 2010 Free Software Foundation, Inc.
+## This file is part of GNU Mailutils.
+## Copyright (C) 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.
+## 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.
+## GNU Mailutils 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
+## You should have received a copy of the GNU General Public License
+## along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. 
 
 pythonsitedir=@PYTHON_SITE_DIR@
 pythonsite_PYTHON=\
diff --git a/python/mailutils/__init__.py b/python/mailutils/__init__.py
index bea528a..423a09d 100644
--- a/python/mailutils/__init__.py
+++ b/python/mailutils/__init__.py
@@ -12,9 +12,7 @@
 #  GNU General Public License for more details.
 #
 #  You should have received a copy of the GNU General Public License
-#  along with GNU Mailutils; if not, write to the Free Software
-#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-#  MA 02110-1301 USA
+#  along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. 
 
 """
 A Python interface to Mailutils framework.
diff --git a/python/mailutils/address.py b/python/mailutils/address.py
index 759b03c..4f43232 100644
--- a/python/mailutils/address.py
+++ b/python/mailutils/address.py
@@ -1,4 +1,3 @@
-#
 #  GNU Mailutils -- a suite of utilities for electronic mail
 #  Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 #
@@ -13,10 +12,8 @@
 #  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
-#
+#  Public License along with this library.  If not, see 
+#  <http://www.gnu.org/licenses/>. 
 
 import types
 from mailutils.c_api import address
diff --git a/python/mailutils/attribute.py b/python/mailutils/attribute.py
index 45fd710..7f22b29 100644
--- a/python/mailutils/attribute.py
+++ b/python/mailutils/attribute.py
@@ -1,4 +1,3 @@
-#
 #  GNU Mailutils -- a suite of utilities for electronic mail
 #  Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 #
@@ -13,10 +12,8 @@
 #  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
-#
+#  Public License along with this library.  If not, see 
+#  <http://www.gnu.org/licenses/>. 
 
 import types
 from mailutils.c_api import attribute
diff --git a/python/mailutils/auth.py b/python/mailutils/auth.py
index 8b68edc..36ee8de 100644
--- a/python/mailutils/auth.py
+++ b/python/mailutils/auth.py
@@ -1,4 +1,3 @@
-#
 #  GNU Mailutils -- a suite of utilities for electronic mail
 #  Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 #
@@ -13,10 +12,8 @@
 #  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
-#
+#  Public License along with this library.  If not, see 
+#  <http://www.gnu.org/licenses/>. 
 
 import types
 from mailutils.c_api import auth
diff --git a/python/mailutils/body.py b/python/mailutils/body.py
index 02af50b..44de502 100644
--- a/python/mailutils/body.py
+++ b/python/mailutils/body.py
@@ -1,4 +1,3 @@
-#
 #  GNU Mailutils -- a suite of utilities for electronic mail
 #  Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 #
@@ -13,10 +12,8 @@
 #  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
-#
+#  Public License along with this library.  If not, see 
+#  <http://www.gnu.org/licenses/>. 
 
 from mailutils.c_api import body
 from mailutils import stream
diff --git a/python/mailutils/debug.py b/python/mailutils/debug.py
index 141dd8a..feafe78 100644
--- a/python/mailutils/debug.py
+++ b/python/mailutils/debug.py
@@ -1,4 +1,3 @@
-#
 #  GNU Mailutils -- a suite of utilities for electronic mail
 #  Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 #
@@ -13,10 +12,8 @@
 #  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
-#
+#  Public License along with this library.  If not, see 
+#  <http://www.gnu.org/licenses/>.
 
 from mailutils.c_api import debug
 from mailutils.error import DebugError
diff --git a/python/mailutils/envelope.py b/python/mailutils/envelope.py
index 86926e3..7be7bdd 100644
--- a/python/mailutils/envelope.py
+++ b/python/mailutils/envelope.py
@@ -1,4 +1,3 @@
-#
 #  GNU Mailutils -- a suite of utilities for electronic mail
 #  Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 #
@@ -13,10 +12,8 @@
 #  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
-#
+#  Public License along with this library.  If not, see 
+#  <http://www.gnu.org/licenses/>.
 
 from mailutils.c_api import envelope
 from mailutils.error import EnvelopeError
diff --git a/python/mailutils/error.py b/python/mailutils/error.py
index 4ef0b47..c6444dc 100644
--- a/python/mailutils/error.py
+++ b/python/mailutils/error.py
@@ -1,4 +1,3 @@
-#
 #  GNU Mailutils -- a suite of utilities for electronic mail
 #  Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 #
@@ -13,10 +12,8 @@
 #  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
-#
+#  Public License along with this library.  If not, see 
+#  <http://www.gnu.org/licenses/>.
 
 from mailutils.c_api import error
 
diff --git a/python/mailutils/filter.py b/python/mailutils/filter.py
index e3242aa..7844fb5 100644
--- a/python/mailutils/filter.py
+++ b/python/mailutils/filter.py
@@ -1,4 +1,3 @@
-#
 #  GNU Mailutils -- a suite of utilities for electronic mail
 #  Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 #
@@ -13,10 +12,8 @@
 #  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
-#
+#  Public License along with this library.  If not, see 
+#  <http://www.gnu.org/licenses/>.
 
 from mailutils.c_api import filter
 from mailutils.stream import *
diff --git a/python/mailutils/folder.py b/python/mailutils/folder.py
index 738f1ff..ba503f7 100644
--- a/python/mailutils/folder.py
+++ b/python/mailutils/folder.py
@@ -1,4 +1,3 @@
-#
 #  GNU Mailutils -- a suite of utilities for electronic mail
 #  Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 #
@@ -13,10 +12,8 @@
 #  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
-#
+#  Public License along with this library.  If not, see 
+#  <http://www.gnu.org/licenses/>.
 
 from mailutils.c_api import folder
 from mailutils import stream
diff --git a/python/mailutils/header.py b/python/mailutils/header.py
index 78bfb2d..ea875cf 100644
--- a/python/mailutils/header.py
+++ b/python/mailutils/header.py
@@ -1,4 +1,3 @@
-#
 #  GNU Mailutils -- a suite of utilities for electronic mail
 #  Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 #
@@ -13,10 +12,8 @@
 #  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
-#
+#  Public License along with this library.  If not, see
+#  <http://www.gnu.org/licenses/>.
 
 import types
 from mailutils.c_api import header
diff --git a/python/mailutils/mailbox.py b/python/mailutils/mailbox.py
index 1bde2c9..517da49 100644
--- a/python/mailutils/mailbox.py
+++ b/python/mailutils/mailbox.py
@@ -1,4 +1,3 @@
-#
 #  GNU Mailutils -- a suite of utilities for electronic mail
 #  Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 #
@@ -13,10 +12,8 @@
 #  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
-#
+#  Public License along with this library.  If not, see
+#  <http://www.gnu.org/licenses/>.
 
 import types
 from mailutils.c_api import mailbox
diff --git a/python/mailutils/mailcap.py b/python/mailutils/mailcap.py
index a555364..5c10dfa 100644
--- a/python/mailutils/mailcap.py
+++ b/python/mailutils/mailcap.py
@@ -1,4 +1,3 @@
-#
 #  GNU Mailutils -- a suite of utilities for electronic mail
 #  Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 #
@@ -13,10 +12,8 @@
 #  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
-#
+#  Public License along with this library.  If not, see 
+#  <http://www.gnu.org/licenses/>.
 
 from mailutils.c_api import mailcap
 from mailutils.error import MailcapError
diff --git a/python/mailutils/mailer.py b/python/mailutils/mailer.py
index 19cf665..0bbbd71 100644
--- a/python/mailutils/mailer.py
+++ b/python/mailutils/mailer.py
@@ -1,4 +1,3 @@
-#
 #  GNU Mailutils -- a suite of utilities for electronic mail
 #  Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 #
@@ -13,10 +12,8 @@
 #  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
-#
+#  Public License along with this library.  If not, see
+#  <http://www.gnu.org/licenses/>.
 
 from mailutils.c_api import mailer
 from mailutils import address
diff --git a/python/mailutils/message.py b/python/mailutils/message.py
index 89aa984..85ad25e 100644
--- a/python/mailutils/message.py
+++ b/python/mailutils/message.py
@@ -1,4 +1,3 @@
-#
 #  GNU Mailutils -- a suite of utilities for electronic mail
 #  Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 #
@@ -13,10 +12,8 @@
 #  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
-#
+#  Public License along with this library.  If not, see 
+#  <http://www.gnu.org/licenses/>.
 
 from mailutils.c_api import message
 from mailutils import envelope
diff --git a/python/mailutils/mime.py b/python/mailutils/mime.py
index 9937f66..3c28a89 100644
--- a/python/mailutils/mime.py
+++ b/python/mailutils/mime.py
@@ -1,4 +1,3 @@
-#
 #  GNU Mailutils -- a suite of utilities for electronic mail
 #  Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 #
@@ -13,10 +12,8 @@
 #  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
-#
+#  Public License along with this library.  If not, see
+#  <http://www.gnu.org/licenses/>.
 
 from mailutils.c_api import mime
 from mailutils import message
diff --git a/python/mailutils/nls.py b/python/mailutils/nls.py
index 90eacb2..334b95f 100644
--- a/python/mailutils/nls.py
+++ b/python/mailutils/nls.py
@@ -1,4 +1,3 @@
-#
 #  GNU Mailutils -- a suite of utilities for electronic mail
 #  Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 #
@@ -13,10 +12,8 @@
 #  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
-#
+#  Public License along with this library.  If not, see
+#  <http://www.gnu.org/licenses/>.
 
 from mailutils.c_api import nls
 
diff --git a/python/mailutils/registrar.py b/python/mailutils/registrar.py
index bdfa270..af90ad6 100644
--- a/python/mailutils/registrar.py
+++ b/python/mailutils/registrar.py
@@ -1,4 +1,3 @@
-#
 #  GNU Mailutils -- a suite of utilities for electronic mail
 #  Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 #
@@ -13,10 +12,8 @@
 #  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
-#
+#  Public License along with this library.  If not, see
+#  <http://www.gnu.org/licenses/>.
 
 import types
 from mailutils.c_api import registrar
diff --git a/python/mailutils/secret.py b/python/mailutils/secret.py
index 51e91ba..f334d98 100644
--- a/python/mailutils/secret.py
+++ b/python/mailutils/secret.py
@@ -1,4 +1,3 @@
-#
 #  GNU Mailutils -- a suite of utilities for electronic mail
 #  Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 #
@@ -13,10 +12,8 @@
 #  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
-#
+#  Public License along with this library.  If not, see 
+#  <http://www.gnu.org/licenses/>.
 
 from mailutils.c_api import secret
 from mailutils.error import SecretError
diff --git a/python/mailutils/sieve.py b/python/mailutils/sieve.py
index 36c5263..36b7e4a 100644
--- a/python/mailutils/sieve.py
+++ b/python/mailutils/sieve.py
@@ -1,4 +1,3 @@
-#
 #  GNU Mailutils -- a suite of utilities for electronic mail
 #  Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 #
@@ -13,10 +12,8 @@
 #  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
-#
+#  Public License along with this library.  If not, see 
+#  <http://www.gnu.org/licenses/>.
 
 from mailutils.c_api import sieve
 from mailutils.error import SieveMachineError
diff --git a/python/mailutils/stream.py b/python/mailutils/stream.py
index 8a2fef2..dc909cb 100644
--- a/python/mailutils/stream.py
+++ b/python/mailutils/stream.py
@@ -1,4 +1,3 @@
-#
 #  GNU Mailutils -- a suite of utilities for electronic mail
 #  Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 #
@@ -13,10 +12,8 @@
 #  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
-#
+#  Public License along with this library.  If not, see
+#  <http://www.gnu.org/licenses/>.
 
 from mailutils.c_api import stream
 from mailutils.error import StreamError
diff --git a/python/mailutils/url.py b/python/mailutils/url.py
index 3efeba0..a1b4d46 100644
--- a/python/mailutils/url.py
+++ b/python/mailutils/url.py
@@ -1,4 +1,3 @@
-#
 #  GNU Mailutils -- a suite of utilities for electronic mail
 #  Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 #
@@ -13,10 +12,8 @@
 #  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
-#
+#  Public License along with this library.  If not, see 
+#  <http://www.gnu.org/licenses/>.
 
 from mailutils.c_api import url
 from mailutils import secret
diff --git a/python/mailutils/util.py b/python/mailutils/util.py
index 1bf0dc7..9aa1795 100644
--- a/python/mailutils/util.py
+++ b/python/mailutils/util.py
@@ -1,4 +1,3 @@
-#
 #  GNU Mailutils -- a suite of utilities for electronic mail
 #  Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 #
@@ -13,10 +12,8 @@
 #  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
-#
+#  Public License along with this library.  If not, see
+#  <http://www.gnu.org/licenses/>.
 
 from mailutils.c_api import util
 
diff --git a/readmsg/Makefile.am b/readmsg/Makefile.am
index 0522a9e..05bc58f 100644
--- a/readmsg/Makefile.am
+++ b/readmsg/Makefile.am
@@ -1,22 +1,19 @@
-## Process this file with GNU Automake to create Makefile.in
-
-##   Copyright (C) 2001, 2002, 2003, 2004, 2007, 2010 Free Software
-##   Foundation, Inc.
+## This file is part of GNU Mailutils.
+## Copyright (C) 2001, 2002, 2003, 2004, 2007, 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.
+## 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.
+## GNU Mailutils 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
+## You should have received a copy of the GNU General Public License
+## along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
 INCLUDES = @MU_APP_COMMON_INCLUDES@ 
 
diff --git a/readmsg/msglist.c b/readmsg/msglist.c
index b42e325..0e9b38c 100644
--- a/readmsg/msglist.c
+++ b/readmsg/msglist.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "readmsg.h"
 
diff --git a/readmsg/readmsg.c b/readmsg/readmsg.c
index 7fdc63b..db60329 100644
--- a/readmsg/readmsg.c
+++ b/readmsg/readmsg.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/readmsg/readmsg.h b/readmsg/readmsg.h
index f1bd87c..9b12c23 100644
--- a/readmsg/readmsg.h
+++ b/readmsg/readmsg.h
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #ifndef _READMSG_H
 #define _READMSG_H
diff --git a/readmsg/tests/Makefile.am b/readmsg/tests/Makefile.am
index 662a627..8c6e849 100644
--- a/readmsg/tests/Makefile.am
+++ b/readmsg/tests/Makefile.am
@@ -6,7 +6,7 @@
 # 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
+# GNU Mailutils 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.
diff --git a/readmsg/tests/all.at b/readmsg/tests/all.at
index 67ab085..fd54991 100644
--- a/readmsg/tests/all.at
+++ b/readmsg/tests/all.at
@@ -6,7 +6,7 @@
 # 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
+# GNU Mailutils 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.
diff --git a/readmsg/tests/hdr.at b/readmsg/tests/hdr.at
index 08395c7..78cc482 100644
--- a/readmsg/tests/hdr.at
+++ b/readmsg/tests/hdr.at
@@ -6,7 +6,7 @@
 # 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
+# GNU Mailutils 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.
diff --git a/readmsg/tests/nohdr.at b/readmsg/tests/nohdr.at
index 6ddf5e9..402588b 100644
--- a/readmsg/tests/nohdr.at
+++ b/readmsg/tests/nohdr.at
@@ -6,7 +6,7 @@
 # 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
+# GNU Mailutils 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.
diff --git a/readmsg/tests/testsuite.at b/readmsg/tests/testsuite.at
index 49305aa..59b6839 100644
--- a/readmsg/tests/testsuite.at
+++ b/readmsg/tests/testsuite.at
@@ -6,7 +6,7 @@
 # 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
+# GNU Mailutils 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.
diff --git a/readmsg/tests/twomsg.at b/readmsg/tests/twomsg.at
index 577af51..5de48b1 100644
--- a/readmsg/tests/twomsg.at
+++ b/readmsg/tests/twomsg.at
@@ -6,7 +6,7 @@
 # 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
+# GNU Mailutils 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.
diff --git a/readmsg/tests/weed.at b/readmsg/tests/weed.at
index bf2c1fc..ca8519b 100644
--- a/readmsg/tests/weed.at
+++ b/readmsg/tests/weed.at
@@ -6,7 +6,7 @@
 # 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
+# GNU Mailutils 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.
diff --git a/sieve/Makefile.am b/sieve/Makefile.am
index 1d9a0bb..669a689 100644
--- a/sieve/Makefile.am
+++ b/sieve/Makefile.am
@@ -1,21 +1,18 @@
-## Process this file with GNU Automake to create Makefile.in
-
-##   Copyright (C) 2001, 2002, 2007, 2010 Free Software Foundation, Inc.
+## This file is part of GNU Mailutils.
+## Copyright (C) 2001, 2002, 2007, 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.
+## 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.
+## GNU Mailutils 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
+## You should have received a copy of the GNU General Public License
+## along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
 INCLUDES = @MU_APP_COMMON_INCLUDES@ 
 SUBDIRS  = . tests
diff --git a/sieve/sieve.c b/sieve/sieve.c
index 22374da..91adfbc 100644
--- a/sieve/sieve.c
+++ b/sieve/sieve.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/sieve/tests/Makefile.am b/sieve/tests/Makefile.am
index fc83481..967b60e 100644
--- a/sieve/tests/Makefile.am
+++ b/sieve/tests/Makefile.am
@@ -6,7 +6,7 @@
 # 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
+# GNU Mailutils 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.
diff --git a/sieve/tests/action.at b/sieve/tests/action.at
index 2da7723..ef98b97 100644
--- a/sieve/tests/action.at
+++ b/sieve/tests/action.at
@@ -6,7 +6,7 @@
 # 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
+# GNU Mailutils 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.
diff --git a/sieve/tests/address.at b/sieve/tests/address.at
index 88499dd..541b5c0 100644
--- a/sieve/tests/address.at
+++ b/sieve/tests/address.at
@@ -6,7 +6,7 @@
 # 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
+# GNU Mailutils 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.
diff --git a/sieve/tests/allof.at b/sieve/tests/allof.at
index 369d32a..44688b2 100644
--- a/sieve/tests/allof.at
+++ b/sieve/tests/allof.at
@@ -6,7 +6,7 @@
 # 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
+# GNU Mailutils 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.
diff --git a/sieve/tests/anyof.at b/sieve/tests/anyof.at
index ba4e9a1..0f22ec5 100644
--- a/sieve/tests/anyof.at
+++ b/sieve/tests/anyof.at
@@ -6,7 +6,7 @@
 # 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
+# GNU Mailutils 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.
diff --git a/sieve/tests/compile.at b/sieve/tests/compile.at
index 8bdab9c..ccb06dd 100644
--- a/sieve/tests/compile.at
+++ b/sieve/tests/compile.at
@@ -6,7 +6,7 @@
 # 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
+# GNU Mailutils 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.
diff --git a/sieve/tests/envelope.at b/sieve/tests/envelope.at
index 375accc..fc8baf6 100644
--- a/sieve/tests/envelope.at
+++ b/sieve/tests/envelope.at
@@ -6,7 +6,7 @@
 # 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
+# GNU Mailutils 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.
diff --git a/sieve/tests/exists.at b/sieve/tests/exists.at
index 41095d9..24fb066 100644
--- a/sieve/tests/exists.at
+++ b/sieve/tests/exists.at
@@ -6,7 +6,7 @@
 # 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
+# GNU Mailutils 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.
diff --git a/sieve/tests/ext.at b/sieve/tests/ext.at
index ab9fc92..bd06379 100644
--- a/sieve/tests/ext.at
+++ b/sieve/tests/ext.at
@@ -6,7 +6,7 @@
 # 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
+# GNU Mailutils 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.
diff --git a/sieve/tests/false.at b/sieve/tests/false.at
index 5dae588..ec53582 100644
--- a/sieve/tests/false.at
+++ b/sieve/tests/false.at
@@ -6,7 +6,7 @@
 # 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
+# GNU Mailutils 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.
diff --git a/sieve/tests/header.at b/sieve/tests/header.at
index ecb99be..134d23f 100644
--- a/sieve/tests/header.at
+++ b/sieve/tests/header.at
@@ -6,7 +6,7 @@
 # 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
+# GNU Mailutils 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.
diff --git a/sieve/tests/i-casemap.at b/sieve/tests/i-casemap.at
index 3b34194..9d55b38 100644
--- a/sieve/tests/i-casemap.at
+++ b/sieve/tests/i-casemap.at
@@ -6,7 +6,7 @@
 # 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
+# GNU Mailutils 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.
diff --git a/sieve/tests/i-numeric.at b/sieve/tests/i-numeric.at
index a62ff28..fcf1213 100644
--- a/sieve/tests/i-numeric.at
+++ b/sieve/tests/i-numeric.at
@@ -6,7 +6,7 @@
 # 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
+# GNU Mailutils 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.
diff --git a/sieve/tests/i-octet.at b/sieve/tests/i-octet.at
index 13049ad..a2186c3 100644
--- a/sieve/tests/i-octet.at
+++ b/sieve/tests/i-octet.at
@@ -6,7 +6,7 @@
 # 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
+# GNU Mailutils 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.
diff --git a/sieve/tests/mul-addr.at b/sieve/tests/mul-addr.at
index b11f4f5..e005c6a 100644
--- a/sieve/tests/mul-addr.at
+++ b/sieve/tests/mul-addr.at
@@ -6,7 +6,7 @@
 # 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
+# GNU Mailutils 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.
diff --git a/sieve/tests/not.at b/sieve/tests/not.at
index cf4e4c5..3828604 100644
--- a/sieve/tests/not.at
+++ b/sieve/tests/not.at
@@ -6,7 +6,7 @@
 # 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
+# GNU Mailutils 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.
diff --git a/sieve/tests/redirect.at b/sieve/tests/redirect.at
index 0449447..6a2f8ff 100644
--- a/sieve/tests/redirect.at
+++ b/sieve/tests/redirect.at
@@ -6,7 +6,7 @@
 # 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
+# GNU Mailutils 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.
diff --git a/sieve/tests/reject.at b/sieve/tests/reject.at
index d7acfc0..91a6cfb 100644
--- a/sieve/tests/reject.at
+++ b/sieve/tests/reject.at
@@ -6,7 +6,7 @@
 # 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
+# GNU Mailutils 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.
diff --git a/sieve/tests/relational.at b/sieve/tests/relational.at
index c68ab22..e9588e1 100644
--- a/sieve/tests/relational.at
+++ b/sieve/tests/relational.at
@@ -6,7 +6,7 @@
 # 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
+# GNU Mailutils 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.
diff --git a/sieve/tests/size.at b/sieve/tests/size.at
index e809e82..5edf122 100644
--- a/sieve/tests/size.at
+++ b/sieve/tests/size.at
@@ -6,7 +6,7 @@
 # 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
+# GNU Mailutils 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.
diff --git a/sieve/tests/testsuite.at b/sieve/tests/testsuite.at
index a9ee7ce..7f6a161 100644
--- a/sieve/tests/testsuite.at
+++ b/sieve/tests/testsuite.at
@@ -6,7 +6,7 @@
 # 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
+# GNU Mailutils 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.
diff --git a/sieve/tests/true.at b/sieve/tests/true.at
index d1065de..5e5cf05 100644
--- a/sieve/tests/true.at
+++ b/sieve/tests/true.at
@@ -6,7 +6,7 @@
 # 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
+# GNU Mailutils 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.
diff --git a/sieve/tests/version.at b/sieve/tests/version.at
index ce951a9..f03eda4 100644
--- a/sieve/tests/version.at
+++ b/sieve/tests/version.at
@@ -6,7 +6,7 @@
 # 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
+# GNU Mailutils 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.
diff --git a/sql/Makefile.am b/sql/Makefile.am
index 2ad125f..9c6274c 100644
--- a/sql/Makefile.am
+++ b/sql/Makefile.am
@@ -1,21 +1,18 @@
 ## This file is part of GNU Mailutils
-
-##   Copyright (C) 2004, 2005, 2007, 2010 Free Software Foundation, Inc.
+## Copyright (C) 2004, 2005, 2007, 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.
+## 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.
+## GNU Mailutils 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
+## You should have received a copy of the GNU General Public License
+## along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. 
 
 noinst_LTLIBRARIES=@BUILD_SQL@
 EXTRA_LTLIBRARIES=libsql.la mysql.la postgres.la odbc.la
diff --git a/sql/mysql.c b/sql/mysql.c
index c573303..b3cb8ba 100644
--- a/sql/mysql.c
+++ b/sql/mysql.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/sql/odbc.c b/sql/odbc.c
index bb9e345..e2f1725 100644
--- a/sql/odbc.c
+++ b/sql/odbc.c
@@ -12,9 +12,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/sql/postgres.c b/sql/postgres.c
index e4e4c87..d7b5ba5 100644
--- a/sql/postgres.c
+++ b/sql/postgres.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/sql/sql.c b/sql/sql.c
index b256beb..d0bb950 100644
--- a/sql/sql.c
+++ b/sql/sql.c
@@ -13,9 +13,8 @@
    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 */
+   Public License along with this library.  If not, see 
+   <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/testsuite/Makefile.am b/testsuite/Makefile.am
index f845683..5c243b1 100644
--- a/testsuite/Makefile.am
+++ b/testsuite/Makefile.am
@@ -6,7 +6,7 @@
 # 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
+# GNU Mailutils 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.
diff --git a/testsuite/lib/mailutils.exp b/testsuite/lib/mailutils.exp
index 827f0d3..b2b2408 100644
--- a/testsuite/lib/mailutils.exp
+++ b/testsuite/lib/mailutils.exp
@@ -3,20 +3,18 @@
 # Copyright (C) 2002, 2005, 2007, 2008, 2009, 2010 Free Software 
 # Foundation, Inc.
 #  
-# This program is free software; you can redistribute it and/or modify
+# 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 of the License, or
 # (at your option) any later version.
 #   
-# This program is distributed in the hope that it will be useful,
+# GNU Mailutils 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.
+# along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. 
 
 verbose "STARTED" 1
 
diff --git a/testsuite/mbdel.at b/testsuite/mbdel.at
index 57dca63..99137e5 100644
--- a/testsuite/mbdel.at
+++ b/testsuite/mbdel.at
@@ -6,7 +6,7 @@
 # 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
+# GNU Mailutils 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.
diff --git a/testsuite/mbdel.c b/testsuite/mbdel.c
index 802444b..e1c7339 100644
--- a/testsuite/mbdel.c
+++ b/testsuite/mbdel.c
@@ -12,7 +12,7 @@
    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, see <http://www.gnu.org/licenses/>. */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/testsuite/mime.at b/testsuite/mime.at
index ff39da6..cb023f7 100644
--- a/testsuite/mime.at
+++ b/testsuite/mime.at
@@ -6,7 +6,7 @@
 # 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
+# GNU Mailutils 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.
diff --git a/testsuite/mimetest.c b/testsuite/mimetest.c
index 581eae7..245cf3a 100644
--- a/testsuite/mimetest.c
+++ b/testsuite/mimetest.c
@@ -13,9 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */ 
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/testsuite/smtpsend.c b/testsuite/smtpsend.c
index 1b843e3..c93dcc0 100644
--- a/testsuite/smtpsend.c
+++ b/testsuite/smtpsend.c
@@ -12,7 +12,7 @@
    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, see <http://www.gnu.org/licenses/>. */
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
diff --git a/testsuite/testsuite.at b/testsuite/testsuite.at
index 5686cfa..7bb3e05 100644
--- a/testsuite/testsuite.at
+++ b/testsuite/testsuite.at
@@ -6,7 +6,7 @@
 # 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
+# GNU Mailutils 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.
diff --git a/testsuite/testsuite.inc b/testsuite/testsuite.inc
index 56f4538..363913f 100644
--- a/testsuite/testsuite.inc
+++ b/testsuite/testsuite.inc
@@ -6,7 +6,7 @@
 # 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
+# GNU Mailutils 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.


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Sun Oct 10 08:46:03 2010
Received: from mailman by lists.gnu.org with archive (Exim 4.43)
	id 1P4vHv-0005Ga-LJ
	for mharc-commit-mailutils@gnu.org; Sun, 10 Oct 2010 08:46:03 -0400
Received: from [140.186.70.92] (port=44964 helo=eggs.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.43) id 1P4vHl-00056N-Dy
	for commit-mailutils@gnu.org; Sun, 10 Oct 2010 08:46:01 -0400
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P4vHd-0000z3-Ou
	for commit-mailutils@gnu.org; Sun, 10 Oct 2010 08:45:53 -0400
Received: from colonialone.fsf.org ([140.186.70.51]:36904
	helo=internal.in.savannah.gnu.org)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P4vHd-0000yz-JE
	for commit-mailutils@gnu.org; Sun, 10 Oct 2010 08:45:45 -0400
Received: from [10.1.0.108] (helo=vcs-noshell.in.savannah.gnu.org)
	by internal.in.savannah.gnu.org with esmtp (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P4vHb-0003fn-DN
	for commit-mailutils@gnu.org; Sun, 10 Oct 2010 12:45:44 +0000
Received: from gray by vcs-noshell.in.savannah.gnu.org with local (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P4vHa-0003pa-8F
	for commit-mailutils@gnu.org; Sun, 10 Oct 2010 12:45:43 +0000
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: bf07c0b891aad29dd8eb45ff3c1b1ea8c23e7dce
X-Git-Newrev: 4f9d95ae16167f61c74af38b564b34c2715bba1d
Message-Id: <E1P4vHa-0003pa-8F@vcs-noshell.in.savannah.gnu.org>
Date: Sun, 10 Oct 2010 12:45:42 +0000
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
Subject: [SCM] GNU Mailutils branch, master,
	updated. release-2.2-143-g4f9d95a
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.5
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=unsubscribe>
List-Archive: <http://lists.gnu.org/archive/html/commit-mailutils>
List-Post: <mailto:commit-mailutils@gnu.org>
List-Help: <mailto:commit-mailutils-request@gnu.org?subject=help>
List-Subscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Sun, 10 Oct 2010 12:46:02 -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=4f9d95ae16167f61c74af38b564b34c2715bba1d

The branch, master has been updated
       via  4f9d95ae16167f61c74af38b564b34c2715bba1d (commit)
       via  8e96b44ffde18c9135dc5e694aea0e4e1a6a86bb (commit)
       via  1c4cec9be5a0f9dbc389b15f6effab641748e382 (commit)
      from  bf07c0b891aad29dd8eb45ff3c1b1ea8c23e7dce (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 4f9d95ae16167f61c74af38b564b34c2715bba1d
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Sun Oct 10 15:35:58 2010 +0300

    Minor fix
    
    * libmailutils/base/getcwd.c: Add missing include file.

commit 8e96b44ffde18c9135dc5e694aea0e4e1a6a86bb
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Sun Oct 10 15:29:10 2010 +0300

    Rename mu_strcasestr to mu_c_strcasestr.
    
    * include/mailutils/cstr.h (mu_strcasestr): Rename to mu_c_strcasestr.
    * libmailutils/string/cstrcasestr.c: Likewise. All uses changed.

commit 1c4cec9be5a0f9dbc389b15f6effab641748e382
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Sun Oct 10 13:07:35 2010 +0300

    Split the mess in mutil.c into logically separated units.
    
    * libmailutils/base/mutil.c (mu_hex2ul, mu_hexstr2ul): Move to string/hexstr.c.
    (mu_get_homedir, mu_get_full_path)
    (mu_expand_path_pattern): Move to base/filename.c.
    (mu_getcwd): Move to base/getcwd.c.
    (mu_tilde_expansion): Move to base/tilde.c
    (mu_cpystr): Move to string/cpystr.c.
    (mu_get_host_name): Move to base/hostname.c.
    (mu_set_user_email,mu_set_user_email_domain)
    (mu_get_user_email_domain,mu_aget_user_email_domain)
    (mu_get_user_email,mu_normalize_path): Move to base/usremail.c.
    (mu_tempfile,mu_tempname): Move to base/tempfile.c.
    (mu_spawnvp): Move to base/spawnvp.c.
    (mu_qualify_link,mu_unroll_symlink): Move to base/symlink.c.
    (mu_rfc2822_references,mu_rfc2822_msg_id)
    (mu_rfc2822_in_reply_to): Move to base/msgid.c.
    (mu_strcasestr): Move to string/cstrcasestr.c.
    (mu_string_unfold): Move to string/unfold.c.
    (mu_true_answer_p): Move to string/trueans.c.
    (mu_scheme_autodetect_p): Move to base/schemeauto.c.
    (mu_fd_wait): Move to base/fdwait.c.
    (mu_set_default_fallback,mu_decode_filter): Move to filter/decode.c.
    (__argp_base_name): Move to lib/argp_base.c.
    
    * libmailutils/base/fdwait.c: New file.
    * libmailutils/base/filename.c: New file.
    * libmailutils/base/getcwd.c: New file.
    * libmailutils/base/hostname.c: New file.
    * libmailutils/base/msgid.c: New file.
    * libmailutils/base/schemeauto.c: New file.
    * libmailutils/base/spawnvp.c: New file.
    * libmailutils/base/symlink.c: New file.
    * libmailutils/base/tempfile.c: New file.
    * libmailutils/base/tilde.c: New file.
    * libmailutils/base/usremail.c: New file.
    * libmailutils/base/Makefile.am: Add new files.
    
    * libmailutils/filter/decode.c: New file.
    * libmailutils/filter/Makefile.am: Add new files.
    
    * libmailutils/string/cpystr.c: New file.
    * libmailutils/string/cstrcasestr.c: New file.
    * libmailutils/string/hexstr.c: New file.
    * libmailutils/string/trueans.c: New file.
    * libmailutils/string/unfold.c: New file.
    * libmailutils/string/Makefile.am: Add new files.
    
    * lib/argp_base.c: New file.
    * lib/Makefile.am: Add new files.
    
    * include/mailutils/io.h: Include stdarg.h
    * include/mailutils/mutil.h: Rename to util.h.  Reorder prototypes
    and declarations in logical groups.  All uses changed.
    
    * include/mailutils/cstr.h (mu_strcasestr): New proto, moved from
    include/mailutils/mutil.h.

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

Summary of changes:
 comsat/comsat.h                                    |    2 +-
 examples/addr.c                                    |    2 +-
 examples/muemail.c                                 |    2 +-
 examples/nntpclient.c                              |    2 +-
 examples/pop3client.c                              |    2 +-
 frm/frm.h                                          |    2 +-
 guimb/guimb.h                                      |    2 +-
 imap4d/imap4d.h                                    |    2 +-
 imap4d/util.c                                      |    2 +-
 include/mailutils/Makefile.am                      |    2 +-
 include/mailutils/cpp/Makefile.am                  |    2 +-
 include/mailutils/cpp/mailutils.h                  |    2 +-
 include/mailutils/cpp/{mutil.h => util.h}          |    8 +-
 include/mailutils/cstr.h                           |    1 +
 include/mailutils/io.h                             |    2 +
 include/mailutils/mailutils.h                      |    2 +-
 include/mailutils/mutil.h                          |  167 ---
 include/mailutils/parse822.h                       |    2 +-
 include/mailutils/util.h                           |  174 +++
 lib/Makefile.am                                    |    1 +
 python/libmu_py/c_api.c => lib/argp_base.c         |   17 +-
 libmailutils/address/address.c                     |    2 +-
 libmailutils/base/Makefile.am                      |   11 +
 libmailutils/base/amd.c                            |    2 +-
 libmailutils/base/assoc.c                          |    2 +-
 libmailutils/base/date.c                           |    2 +-
 libmailutils/base/fdwait.c                         |   72 +
 libmailutils/base/filename.c                       |  247 ++++
 libmailutils/base/getcwd.c                         |   70 +
 libmailutils/base/getpass.c                        |    2 +-
 libmailutils/base/hostname.c                       |  102 ++
 libmailutils/base/kwd.c                            |    2 +-
 libmailutils/base/locale.c                         |    2 +-
 libmailutils/base/locker.c                         |    2 +-
 libmailutils/base/mailcap.c                        |    2 +-
 libmailutils/base/msgid.c                          |  230 ++++
 libmailutils/base/mutil.c                          | 1372 +-------------------
 libmailutils/base/parsedate.y                      |    2 +-
 libmailutils/base/permstr.c                        |    2 +-
 libmailutils/base/registrar.c                      |    2 +-
 libmailutils/base/rfc2047.c                        |    2 +-
 .../{diag/dbgsyslog.c => base/schemeauto.c}        |   18 +-
 libmailutils/base/spawnvp.c                        |  120 ++
 libmailutils/base/symlink.c                        |  103 ++
 libmailutils/base/tempfile.c                       |  104 ++
 libmailutils/base/ticket.c                         |    2 +-
 libmailutils/base/tilde.c                          |  119 ++
 libmailutils/base/url.c                            |    2 +-
 libmailutils/base/usremail.c                       |  196 +++
 libmailutils/base/wicket.c                         |    2 +-
 libmailutils/cfg/driver.c                          |    2 +-
 libmailutils/cfg/gocs.c                            |    2 +-
 libmailutils/cfg/lexer.l                           |    2 +-
 libmailutils/cfg/parser.y                          |    2 +-
 libmailutils/filter/Makefile.am                    |    1 +
 libmailutils/filter/decode.c                       |   73 +
 libmailutils/mailbox/attribute.c                   |    2 +-
 libmailutils/mailbox/body.c                        |    2 +-
 libmailutils/mailbox/envelope.c                    |    2 +-
 libmailutils/mailbox/header.c                      |    2 +-
 libmailutils/mailbox/mailbox.c                     |    2 +-
 libmailutils/mailbox/mbx_default.c                 |    2 +-
 libmailutils/mailbox/message.c                     |    2 +-
 libmailutils/mailer/mailer.c                       |    2 +-
 libmailutils/mailer/progmailer.c                   |    2 +-
 libmailutils/mime/attachment.c                     |    2 +-
 libmailutils/mime/mime.c                           |    2 +-
 libmailutils/mime/mimehdr.c                        |    2 +-
 libmailutils/stream/file_stream.c                  |    2 +-
 libmailutils/stream/memory_stream.c                |    2 +-
 libmailutils/stream/message_stream.c               |    2 +-
 libmailutils/stream/prog_stream.c                  |    2 +-
 libmailutils/stream/tcp.c                          |    2 +-
 libmailutils/stream/temp_file_stream.c             |    2 +-
 libmailutils/string/Makefile.am                    |    5 +
 libmailutils/{diag/dbgsyslog.c => string/cpystr.c} |   27 +-
 libmailutils/string/cstrcasestr.c                  |  101 ++
 libmailutils/{diag/dbgsyslog.c => string/hexstr.c} |   44 +-
 libmailutils/string/mkfilename.c                   |    2 +-
 libmailutils/string/trueans.c                      |   58 +
 .../cpp/secret.h => libmailutils/string/unfold.c   |   71 +-
 libmailutils/tests/addr.c                          |    2 +-
 libmu_auth/ldap.c                                  |    2 +-
 libmu_auth/sql.c                                   |    2 +-
 libmu_cfg/ldap.c                                   |    2 +-
 libmu_cfg/sql.c                                    |    2 +-
 libmu_cpp/mutil.cc                                 |    2 +-
 libmu_scm/mu_scm.h                                 |    2 +-
 libmu_sieve/comparator.c                           |    2 +-
 libproto/imap/folder.c                             |    2 +-
 libproto/imap/mbox.c                               |    2 +-
 libproto/maildir/folder.c                          |    2 +-
 libproto/maildir/mbox.c                            |    2 +-
 libproto/mailer/mbox.c                             |    2 +-
 libproto/mailer/smtp.c                             |    2 +-
 libproto/mailer/smtp_auth.c                        |    2 +-
 libproto/mailer/smtp_ehlo.c                        |    2 +-
 libproto/mailer/smtp_gsasl.c                       |    2 +-
 libproto/mailer/smtp_mail.c                        |    2 +-
 libproto/mailer/smtp_mech.c                        |    2 +-
 libproto/mailer/smtp_rcpt.c                        |    2 +-
 libproto/mbox/folder.c                             |    2 +-
 libproto/mbox/mbox0.h                              |    2 +-
 libproto/mh/folder.c                               |    2 +-
 libproto/mh/mbox.c                                 |    2 +-
 libproto/pop/mbox.c                                |    2 +-
 maidag/maidag.h                                    |    2 +-
 mail/mail.h                                        |    2 +-
 mail/util.c                                        |    2 +-
 mh/mh.h                                            |    2 +-
 po/POTFILES.in                                     |   10 +
 pop3d/pop3d.h                                      |    2 +-
 python/libmu_py/libmu_py.h                         |    2 +-
 readmsg/readmsg.h                                  |    2 +-
 sieve/sieve.c                                      |    2 +-
 115 files changed, 2014 insertions(+), 1682 deletions(-)
 rename include/mailutils/cpp/{mutil.h => util.h} (90%)
 delete mode 100644 include/mailutils/mutil.h
 create mode 100644 include/mailutils/util.h
 copy python/libmu_py/c_api.c => lib/argp_base.c (73%)
 create mode 100644 libmailutils/base/fdwait.c
 create mode 100644 libmailutils/base/filename.c
 create mode 100644 libmailutils/base/getcwd.c
 create mode 100644 libmailutils/base/hostname.c
 create mode 100644 libmailutils/base/msgid.c
 copy libmailutils/{diag/dbgsyslog.c => base/schemeauto.c} (68%)
 create mode 100644 libmailutils/base/spawnvp.c
 create mode 100644 libmailutils/base/symlink.c
 create mode 100644 libmailutils/base/tempfile.c
 create mode 100644 libmailutils/base/tilde.c
 create mode 100644 libmailutils/base/usremail.c
 create mode 100644 libmailutils/filter/decode.c
 copy libmailutils/{diag/dbgsyslog.c => string/cpystr.c} (60%)
 create mode 100644 libmailutils/string/cstrcasestr.c
 copy libmailutils/{diag/dbgsyslog.c => string/hexstr.c} (52%)
 create mode 100644 libmailutils/string/trueans.c
 copy include/mailutils/cpp/secret.h => libmailutils/string/unfold.c (52%)

diff --git a/comsat/comsat.h b/comsat/comsat.h
index df4a19c..5148fa8 100644
--- a/comsat/comsat.h
+++ b/comsat/comsat.h
@@ -49,7 +49,7 @@
 #include <mailutils/list.h>
 #include <mailutils/mailbox.h>
 #include <mailutils/message.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 #include <mailutils/registrar.h>
 #include <mailutils/stream.h>
 #include <mailutils/mu_auth.h>
diff --git a/examples/addr.c b/examples/addr.c
index 009e13e..96a5855 100644
--- a/examples/addr.c
+++ b/examples/addr.c
@@ -25,7 +25,7 @@
 #include <mailutils/address.h>
 #include <mailutils/errno.h>
 #include <mailutils/kwd.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 
 #define EPARSE MU_ERR_NOENT
 
diff --git a/examples/muemail.c b/examples/muemail.c
index b2635cb..0e57119 100644
--- a/examples/muemail.c
+++ b/examples/muemail.c
@@ -20,7 +20,7 @@
 #endif
 #include <stdlib.h>
 #include <stdio.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 #include "mailutils/libargp.h"
 
 const char *capa[] = {
diff --git a/examples/nntpclient.c b/examples/nntpclient.c
index 19fec3d..7fb28f3 100644
--- a/examples/nntpclient.c
+++ b/examples/nntpclient.c
@@ -41,7 +41,7 @@
 #include <mailutils/iterator.h>
 #include <mailutils/error.h>
 #include <mailutils/errno.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 #include <mailutils/cctype.h>
 #include <mailutils/cstr.h>
 
diff --git a/examples/pop3client.c b/examples/pop3client.c
index f2d81a3..7b1f864 100644
--- a/examples/pop3client.c
+++ b/examples/pop3client.c
@@ -48,7 +48,7 @@
 #include <mailutils/cctype.h>
 #include <mailutils/cstr.h>
 #include <mailutils/tls.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 
 /* A structure which contains information on the commands this program
    can understand. */
diff --git a/frm/frm.h b/frm/frm.h
index 24d59ef..b45333e 100644
--- a/frm/frm.h
+++ b/frm/frm.h
@@ -59,7 +59,7 @@
 #include <mailutils/nls.h>
 #include <mailutils/tls.h>
 #include <mailutils/error.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 #include <mailutils/mime.h>
 
 #include "mailutils/libargp.h"
diff --git a/guimb/guimb.h b/guimb/guimb.h
index d1fc759..621a3ec 100644
--- a/guimb/guimb.h
+++ b/guimb/guimb.h
@@ -39,7 +39,7 @@
 #include <mailutils/guile.h>
 #include <mailutils/nls.h>
 #include <mailutils/list.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 #include <mailutils/attribute.h>
 #include <mailutils/envelope.h>
 #include <mu_asprintf.h>
diff --git a/imap4d/imap4d.h b/imap4d/imap4d.h
index 2e54f66..6cf6f58 100644
--- a/imap4d/imap4d.h
+++ b/imap4d/imap4d.h
@@ -86,7 +86,7 @@
 #include <mailutils/mailbox.h>
 #include <mailutils/message.h>
 #include <mailutils/mime.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 #include <mailutils/mu_auth.h>
 #include <mailutils/nls.h>
 #include <mailutils/parse822.h>
diff --git a/imap4d/util.c b/imap4d/util.c
index 956ddc4..6d5a2c7 100644
--- a/imap4d/util.c
+++ b/imap4d/util.c
@@ -388,7 +388,7 @@ util_parse_ctime_date (const char *date, time_t * timep)
 char *
 util_strcasestr (const char *haystack, const char *needle)
 {
-  return mu_strcasestr (haystack, needle);
+  return mu_c_strcasestr (haystack, needle);
 }
 
 struct
diff --git a/include/mailutils/Makefile.am b/include/mailutils/Makefile.am
index 5b94709..6f0a67f 100644
--- a/include/mailutils/Makefile.am
+++ b/include/mailutils/Makefile.am
@@ -69,7 +69,7 @@ pkginclude_HEADERS = \
  mime.h\
  monitor.h\
  mu_auth.h\
- mutil.h\
+ util.h\
  nls.h\
  nntp.h\
  observer.h\
diff --git a/include/mailutils/cpp/Makefile.am b/include/mailutils/cpp/Makefile.am
index 0f9ec82..ad52dc3 100644
--- a/include/mailutils/cpp/Makefile.am
+++ b/include/mailutils/cpp/Makefile.am
@@ -33,7 +33,7 @@ cppinclude_HEADERS = \
  mailutils.h\
  message.h\
  mime.h\
- mutil.h\
+ util.h\
  pop3.h\
  registrar.h\
  secret.h\
diff --git a/include/mailutils/cpp/mailutils.h b/include/mailutils/cpp/mailutils.h
index d5e820d..a307e33 100644
--- a/include/mailutils/cpp/mailutils.h
+++ b/include/mailutils/cpp/mailutils.h
@@ -30,7 +30,7 @@
 #include <mailutils/cpp/mailer.h>
 #include <mailutils/cpp/message.h>
 #include <mailutils/cpp/mime.h>
-#include <mailutils/cpp/mutil.h>
+#include <mailutils/cpp/util.h>
 #include <mailutils/cpp/pop3.h>
 #include <mailutils/cpp/registrar.h>
 #include <mailutils/cpp/secret.h>
diff --git a/include/mailutils/cpp/mutil.h b/include/mailutils/cpp/util.h
similarity index 90%
rename from include/mailutils/cpp/mutil.h
rename to include/mailutils/cpp/util.h
index c64d9a7..dd2e203 100644
--- a/include/mailutils/cpp/mutil.h
+++ b/include/mailutils/cpp/util.h
@@ -15,12 +15,12 @@
    Public License along with this library.  If not, see 
    <http://www.gnu.org/licenses/>. */
 
-#ifndef _MUCPP_MUTIL_H
-#define _MUCPP_MUTIL_H
+#ifndef _MUCPP_UTIL_H
+#define _MUCPP_UTIL_H
 
 #include <string>
 #include <mailutils/filter.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 
 namespace mailutils
 {
@@ -32,5 +32,5 @@ std::string tempname (const std::string& tmpdir);
 
 }
 
-#endif // not _MUCPP_MUTIL_H
+#endif // not _MUCPP_UTIL_H
 
diff --git a/include/mailutils/cstr.h b/include/mailutils/cstr.h
index 787855a..0914df7 100644
--- a/include/mailutils/cstr.h
+++ b/include/mailutils/cstr.h
@@ -27,6 +27,7 @@ int mu_strupper (char *);
 
 int mu_c_strcasecmp (const char *a, const char *b);
 int mu_c_strncasecmp (const char *a, const char *b, size_t n);
+char *mu_c_strcasestr (const char *haystack, const char *needle);
 
 size_t mu_rtrim_class (char *str, int __class);
 size_t mu_rtrim_cset (char *str, const char *cset);
diff --git a/include/mailutils/io.h b/include/mailutils/io.h
index e9c862d..a80eb4b 100644
--- a/include/mailutils/io.h
+++ b/include/mailutils/io.h
@@ -18,6 +18,8 @@
 #ifndef _MAILUTILS_IO_H
 #define _MAILUTILS_IO_H
 
+#include <stdarg.h>
+
 #ifdef __cplusplus
 extern "C" {
 #endif
diff --git a/include/mailutils/mailutils.h b/include/mailutils/mailutils.h
index 18aa097..cad14b6 100644
--- a/include/mailutils/mailutils.h
+++ b/include/mailutils/mailutils.h
@@ -44,7 +44,7 @@
 #include <mailutils/mime.h>
 #include <mailutils/monitor.h>
 #include <mailutils/mu_auth.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 #include <mailutils/nls.h>
 #include <mailutils/observer.h>
 #include <mailutils/opool.h>
diff --git a/include/mailutils/mutil.h b/include/mailutils/mutil.h
deleted file mode 100644
index 1953971..0000000
--- a/include/mailutils/mutil.h
+++ /dev/null
@@ -1,167 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 1999, 2000, 2001, 2004, 2005, 2006, 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, see
-   <http://www.gnu.org/licenses/>. */
-
-#ifndef _MAILUTILS_MUTIL_H
-#define _MAILUTILS_MUTIL_H
-
-/*
-   Collection of useful utility routines that are worth sharing,
-   but don't have a natural home somewhere else.
-*/
-
-#include <time.h>
-
-#include <mailutils/list.h>
-#include <mailutils/types.h>
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-extern unsigned long mu_hex2ul (char hex);
-extern size_t mu_hexstr2ul (unsigned long* ul, const char* hex, size_t len);
-
-struct mu_timezone
-{
-  int utc_offset;
-    /* Seconds east of UTC. */
-
-  const char *tz_name;
-    /* Nickname for this timezone, if known. It is always considered
-     * to be a pointer to static string, so will never be freed. */
-};
-
-typedef struct mu_timezone mu_timezone;
-
-int mu_parse_date (const char *p, time_t *rettime, const time_t *now);
-extern int mu_parse_imap_date_time (const char **p, struct tm * tm,
-				    mu_timezone * tz);
-extern int mu_parse_ctime_date_time (const char **p, struct tm * tm,
-				     mu_timezone * tz);
-
-extern time_t mu_utc_offset (void);
-extern time_t mu_tm2time (struct tm * timeptr, mu_timezone * tz);
-extern char * mu_get_homedir (void);
-extern char * mu_tilde_expansion (const char *ref, const char *delim, const char *homedir);
-
-extern size_t mu_cpystr (char *dst, const char *src, size_t size);
-
-/* Get the host name, doing a gethostbyname() if possible.
- *  
- * It is the caller's responsibility to free host.
- */
-extern int mu_get_host_name (char **host);
-
-/* Set the default user email address.
- *  
- * Subsequent calls to mu_get_user_email() with a NULL name will return this
- * email address.  email is parsed to determine that it consists of a a valid
- * rfc822 address, with one valid addr-spec, i.e, the address must be
- * qualified.
- */
-extern int mu_set_user_email (const char *email);
-
-/* Set the default user email address domain.
- *  
- * Subsequent calls to mu_get_user_email() with a non-null name will return
- * email addresses in this domain (name@domain). It should be fully
- * qualified, but this isn't (and can't) be enforced.
- */
-extern int mu_set_user_email_domain (const char *domain);
-
-/* Return the currently set user email domain, or NULL if not set. */
-extern int mu_get_user_email_domain (const char** domain);
-
-/* Same, but allocates memory */
-extern int mu_aget_user_email_domain (char **pdomain);
-
-/*
- * Get the default email address for user name. A NULL name is taken
- * to mean the current user.
- *  
- * The result must be freed by the caller after use.
- */
-extern char *mu_get_user_email (const char *name);
-
-extern char *mu_make_file_name (const char *dir, const char *file);  
-extern char *mu_normalize_path (char *path);
-extern int mu_tempfile (const char *tmpdir, char **namep);
-extern char *mu_tempname (const char *tmpdir);
-
-extern char * mu_get_full_path (const char *file);
-extern char * mu_getcwd (void);
-  
-extern int mu_spawnvp(const char *prog, char *av[], int *stat);
-
-extern int mu_unroll_symlink (char *out, size_t outsz, const char *in);
-
-extern char * mu_expand_path_pattern (const char *pattern, const char *username);
-
-extern int mu_rfc2822_msg_id (int subpart, char **pstr);
-extern int mu_rfc2822_references (mu_message_t msg, char **pstr);
-extern int mu_rfc2822_in_reply_to (mu_message_t msg, char **pstr);
-
-/* Find NEEDLE in the HAYSTACK. Case insensitive comparison */
-extern char *mu_strcasestr (const char *haystack, const char *needle);
-
-extern int mu_string_unfold (char *text, size_t *plen);
-
-extern int mu_unre_set_regex (const char *str, int caseflag, char **errp);
-extern int mu_unre_subject  (const char *subject, const char **new_subject);
-
-extern const char *mu_charset_lookup (char *lang, char *terr);
-
-extern int mu_true_answer_p (const char *p);
-extern int mu_scheme_autodetect_p (mu_url_t);
-
-struct timeval; 
-  
-extern int mu_fd_wait (int fd, int *pflags, struct timeval *tvp);
-
-extern int mu_decode_filter (mu_stream_t *pfilter, mu_stream_t input,
-			     const char *filter_type,
-			     const char *fromcode, const char *tocode);
-
-extern enum mu_iconv_fallback_mode mu_default_fallback_mode;
-extern int mu_set_default_fallback (const char *str);
-
-extern int mu_is_proto (const char *p);
-
-extern int mu_mh_delim (const char *str);
-
-extern size_t mu_strftime (char *s, size_t max, const char *format,
-			   const struct tm *tm);
-  
-
-extern int mutil_parse_field_map (const char *map, mu_assoc_t *passoc_tab,
-				  int *perr);
-
-extern int mu_stream_flags_to_mode (int flags, int isdir);
-
-extern int mu_parse_stream_perm_string (int *pmode, const char *str,
-					const char **endp);
-  
-extern int mu_getpass (mu_stream_t in, mu_stream_t out, const char *prompt,
-		       char **passptr);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif
-
diff --git a/include/mailutils/parse822.h b/include/mailutils/parse822.h
index b212d18..fcfb352 100644
--- a/include/mailutils/parse822.h
+++ b/include/mailutils/parse822.h
@@ -24,7 +24,7 @@
 #define _MAILUTILS_PARSE822_H
 
 #include <mailutils/types.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 
 #ifdef __cplusplus
 extern "C" {
diff --git a/include/mailutils/util.h b/include/mailutils/util.h
new file mode 100644
index 0000000..57d11f0
--- /dev/null
+++ b/include/mailutils/util.h
@@ -0,0 +1,174 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 2004, 2005, 2006, 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, see
+   <http://www.gnu.org/licenses/>. */
+
+#ifndef _MAILUTILS_UTIL_H
+#define _MAILUTILS_UTIL_H
+
+/* A collection of utility routines that don't belong somewhere else. */
+
+#include <time.h>
+
+#include <mailutils/list.h>
+#include <mailutils/types.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+  /* ----------------------- */
+  /* String manipulation     */
+  /* ----------------------- */
+unsigned long mu_hex2ul (char hex);
+size_t mu_hexstr2ul (unsigned long* ul, const char* hex, size_t len);
+size_t mu_cpystr (char *dst, const char *src, size_t size);
+int mu_string_unfold (char *text, size_t *plen);
+int mu_true_answer_p (const char *p);
+int mu_unre_set_regex (const char *str, int caseflag, char **errp);
+int mu_unre_subject  (const char *subject, const char **new_subject);
+int mu_is_proto (const char *p);
+int mu_mh_delim (const char *str);
+
+
+  /* ----------------------- */
+  /* Date & time functions   */
+  /* ----------------------- */
+struct mu_timezone
+{
+  int utc_offset;  /* Seconds east of UTC. */
+
+  const char *tz_name;
+    /* Nickname for this timezone, if known. It is always considered
+       to be a pointer to static string, so will never be freed. */
+};
+
+typedef struct mu_timezone mu_timezone;
+
+int mu_parse_date (const char *p, time_t *rettime, const time_t *now);
+int mu_parse_imap_date_time (const char **p, struct tm *tm,
+			     mu_timezone *tz);
+int mu_parse_ctime_date_time (const char **p, struct tm *tm,
+			      mu_timezone *tz);
+
+time_t mu_utc_offset (void);
+time_t mu_tm2time (struct tm *timeptr, mu_timezone *tz);
+size_t mu_strftime (char *s, size_t max, const char *format,
+		    const struct tm *tm);
+  
+  /* ----------------------- */
+  /* File & path names.      */
+  /* ----------------------- */
+char *mu_get_homedir (void);
+char *mu_get_full_path (const char *file);
+char *mu_normalize_path (char *path);
+char *mu_expand_path_pattern (const char *pattern, const char *username);
+char *mu_tilde_expansion (const char *ref, const char *delim,
+			  const char *homedir);
+int mu_unroll_symlink (char *out, size_t outsz, const char *in);
+char *mu_getcwd (void);
+int mu_tempfile (const char *tmpdir, char **namep);
+char *mu_tempname (const char *tmpdir);
+char *mu_make_file_name (const char *dir, const char *file);
+  
+  /* ----------------------- */
+  /* Current user email.     */
+  /* ----------------------- */
+/* Set the default user email address.
+ *  
+ * Subsequent calls to mu_get_user_email() with a NULL name will return this
+ * email address.  email is parsed to determine that it consists of a a valid
+ * rfc822 address, with one valid addr-spec, i.e, the address must be
+ * qualified.
+ */
+int mu_set_user_email (const char *email);
+
+/* Set the default user email address domain.
+ *  
+ * Subsequent calls to mu_get_user_email() with a non-null name will return
+ * email addresses in this domain (name@domain). It should be fully
+ * qualified, but this isn't (and can't) be enforced.
+ */
+int mu_set_user_email_domain (const char *domain);
+
+/* Return the currently set user email domain, or NULL if not set. */
+int mu_get_user_email_domain (const char** domain);
+
+/* Same, but allocates memory */
+int mu_aget_user_email_domain (char **pdomain);
+
+/*
+ * Get the default email address for user name. A NULL name is taken
+ * to mean the current user.
+ *  
+ * The result must be freed by the caller after use.
+ */
+char *mu_get_user_email (const char *name);
+
+  /* ----------------------- */
+  /* Message ID support.     */
+  /* ----------------------- */
+
+int mu_rfc2822_msg_id (int subpart, char **pstr);
+int mu_rfc2822_references (mu_message_t msg, char **pstr);
+int mu_rfc2822_in_reply_to (mu_message_t msg, char **pstr);
+
+  /* ----------------------- */
+  /* Filter+iconv            */
+  /* ----------------------- */
+int mu_decode_filter (mu_stream_t *pfilter, mu_stream_t input,
+		      const char *filter_type,
+		      const char *fromcode, const char *tocode);
+
+extern enum mu_iconv_fallback_mode mu_default_fallback_mode;
+int mu_set_default_fallback (const char *str);
+
+  /* ----------------------- */
+  /* Stream flags conversion */
+  /* ----------------------- */
+int mu_stream_flags_to_mode (int flags, int isdir);
+int mu_parse_stream_perm_string (int *pmode, const char *str,
+				 const char **endp);
+  
+  
+  /* ----------------------- */
+  /* Stream-based getpass    */
+  /* ----------------------- */
+int mu_getpass (mu_stream_t in, mu_stream_t out, const char *prompt,
+		char **passptr);
+
+  /* ----------------------- */
+  /* Assorted functions.     */
+  /* ----------------------- */
+/* Get the host name, doing a gethostbyname() if possible. */
+int mu_get_host_name (char **host);
+int mu_spawnvp(const char *prog, char *av[], int *stat);
+const char *mu_charset_lookup (char *lang, char *terr);
+int mu_scheme_autodetect_p (mu_url_t);
+
+struct timeval; 
+int mu_fd_wait (int fd, int *pflags, struct timeval *tvp);
+
+
+int mutil_parse_field_map (const char *map, mu_assoc_t *passoc_tab,
+			   int *perr);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
+
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 1e584d8..ed88cd0 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -20,6 +20,7 @@ include gnulib.mk
 INCLUDES = @MU_LIB_COMMON_INCLUDES@ 
 
 libmuaux_a_SOURCES += \
+ argp_base.c\
  daemon.c\
  mailcap.c\
  mu_dbm.c\
diff --git a/python/libmu_py/c_api.c b/lib/argp_base.c
similarity index 73%
copy from python/libmu_py/c_api.c
copy to lib/argp_base.c
index aab2345..36f4c88 100644
--- a/python/libmu_py/c_api.c
+++ b/lib/argp_base.c
@@ -1,5 +1,5 @@
 /* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 2009, 2010 Free Software Foundation, Inc.
+   Copyright (C) 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
@@ -15,11 +15,16 @@
    Public License along with this library.  If not, see
    <http://www.gnu.org/licenses/>. */
 
-#include "libmu_py.h"
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+#include <unistd.h>
+#include <string.h>
 
-PyMODINIT_FUNC
-initc_api (void)
+char *
+__argp_base_name (const char *arg)
 {
-  mu_py_init ();
-  mu_py_attach_modules ();
+  char *p = strrchr (arg, '/');
+  return (char *)(p ? p + 1 : arg);
 }
+
diff --git a/libmailutils/address/address.c b/libmailutils/address/address.c
index 0bdd93e..8518ada 100644
--- a/libmailutils/address/address.c
+++ b/libmailutils/address/address.c
@@ -31,7 +31,7 @@
 #include <sys/types.h>
 
 #include <mailutils/errno.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 #include <mailutils/parse822.h>
 #include <mailutils/address.h>
 #include <mailutils/cstr.h>
diff --git a/libmailutils/base/Makefile.am b/libmailutils/base/Makefile.am
index 4bb7acb..196344b 100644
--- a/libmailutils/base/Makefile.am
+++ b/libmailutils/base/Makefile.am
@@ -24,9 +24,13 @@ libbase_la_SOURCES = \
  assoc.c\
  daemon.c\
  date.c\
+ fdwait.c\
  fgetpwent.c\
+ filename.c\
  freeitem.c\
+ getcwd.c\
  getpass.c\
+ hostname.c\
  iterator.c\
  kwd.c\
  list.c\
@@ -36,6 +40,7 @@ libbase_la_SOURCES = \
  mailcap.c\
  md5.c\
  monitor.c\
+ msgid.c\
  munre.c\
  mutil.c\
  nls.c\
@@ -48,10 +53,16 @@ libbase_la_SOURCES = \
  registrar.c\
  refcount.c\
  rfc2047.c\
+ schemeauto.c\
  sha1.c\
  secret.c\
+ spawnvp.c\
+ symlink.c\
+ tempfile.c\
  ticket.c\
+ tilde.c\
  url.c\
+ usremail.c\
  vartab.c\
  version.c\
  wicket.c
diff --git a/libmailutils/base/amd.c b/libmailutils/base/amd.c
index 1a32228..c2f3b34 100644
--- a/libmailutils/base/amd.c
+++ b/libmailutils/base/amd.c
@@ -62,7 +62,7 @@
 #include <mailutils/header.h>
 #include <mailutils/locker.h>
 #include <mailutils/message.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 #include <mailutils/property.h>
 #include <mailutils/stream.h>
 #include <mailutils/url.h>
diff --git a/libmailutils/base/assoc.c b/libmailutils/base/assoc.c
index 4d27cb7..b2e3f60 100644
--- a/libmailutils/base/assoc.c
+++ b/libmailutils/base/assoc.c
@@ -25,7 +25,7 @@
 #include <mailutils/errno.h>
 #include <mailutils/error.h>
 #include <mailutils/iterator.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 #include <mailutils/cstr.h>
 #include <mailutils/sys/iterator.h>
 
diff --git a/libmailutils/base/date.c b/libmailutils/base/date.c
index efb187b..1f9353c 100644
--- a/libmailutils/base/date.c
+++ b/libmailutils/base/date.c
@@ -22,7 +22,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 #include <mailutils/cstr.h>
 
 #define SECS_PER_DAY 86400
diff --git a/libmailutils/base/fdwait.c b/libmailutils/base/fdwait.c
new file mode 100644
index 0000000..1a489e4
--- /dev/null
+++ b/libmailutils/base/fdwait.c
@@ -0,0 +1,72 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 2002, 2003, 2005, 2006, 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, see 
+   <http://www.gnu.org/licenses/>. */
+
+#if HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <sys/select.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <errno.h>
+#include <mailutils/stream.h>
+
+int
+mu_fd_wait (int fd, int *pflags, struct timeval *tvp)
+{
+  fd_set rdset, wrset, exset;
+  int rc;
+
+  FD_ZERO (&rdset);
+  FD_ZERO (&wrset);
+  FD_ZERO (&exset);
+  if ((*pflags) & MU_STREAM_READY_RD)
+    FD_SET (fd, &rdset);
+  if ((*pflags) & MU_STREAM_READY_WR)
+    FD_SET (fd, &wrset);
+  if ((*pflags) & MU_STREAM_READY_EX)
+    FD_SET (fd, &exset);
+  
+  do
+    {
+      if (tvp)
+	{
+	  struct timeval tv = *tvp; 
+	  rc = select (fd + 1, &rdset, &wrset, &exset, &tv);
+	}
+      else
+	rc = select (fd + 1, &rdset, &wrset, &exset, NULL);
+    }
+  while (rc == -1 && errno == EINTR);
+
+  if (rc < 0)
+    return errno;
+  
+  *pflags = 0;
+  if (rc > 0)
+    {
+      if (FD_ISSET (fd, &rdset))
+	*pflags |= MU_STREAM_READY_RD;
+      if (FD_ISSET (fd, &wrset))
+	*pflags |= MU_STREAM_READY_WR;
+      if (FD_ISSET (fd, &exset))
+	*pflags |= MU_STREAM_READY_EX;
+    }
+  return 0;
+}
diff --git a/libmailutils/base/filename.c b/libmailutils/base/filename.c
new file mode 100644
index 0000000..97c4813
--- /dev/null
+++ b/libmailutils/base/filename.c
@@ -0,0 +1,247 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 2002, 2003, 2005, 2006, 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, see 
+   <http://www.gnu.org/licenses/>. */
+
+#if HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <stdio.h>
+#include <mailutils/types.h>
+#include <mailutils/util.h>
+#include <mailutils/mu_auth.h>
+
+char *
+mu_get_homedir (void)
+{
+  char *homedir = getenv ("HOME");
+  if (homedir)
+    homedir = strdup (homedir);
+  else
+    {
+      struct mu_auth_data *auth = mu_get_auth_by_uid (geteuid ());
+      if (!auth)
+	return NULL;
+      homedir = strdup (auth->dir);
+      mu_auth_data_free (auth);
+    }
+  return homedir;
+}
+
+char *
+mu_get_full_path (const char *file)
+{
+  char *p = NULL;
+
+  if (!file)
+    p = mu_getcwd ();
+  else if (*file != '/')
+    {
+      char *cwd = mu_getcwd ();
+      if (cwd)
+	{
+	  p = calloc (strlen (cwd) + 1 + strlen (file) + 1, 1);
+	  if (p)
+	    sprintf (p, "%s/%s", cwd, file);
+	  free (cwd);
+	}
+    }
+
+  if (!p)
+    p = strdup (file);
+  return p;
+}
+
+/* mu_normalize_path: convert pathname containig relative paths specs (../)
+   into an equivalent absolute path. Strip trailing delimiter if present,
+   unless it is the only character left. E.g.:
+
+         /home/user/../smith   -->   /home/smith
+	 /home/user/../..      -->   /
+
+*/
+char *
+mu_normalize_path (char *path)
+{
+  int len;
+  char *p;
+
+  if (!path)
+    return path;
+
+  len = strlen (path);
+
+  /* Empty string is returned as is */
+  if (len == 0)
+    return path;
+
+  /* delete trailing delimiter if any */
+  if (len && path[len-1] == '/')
+    path[len-1] = 0;
+
+  /* Eliminate any /../ */
+  for (p = strchr (path, '.'); p; p = strchr (p, '.'))
+    {
+      if (p > path && p[-1] == '/')
+	{
+	  if (p[1] == '.' && (p[2] == 0 || p[2] == '/'))
+	    /* found */
+	    {
+	      char *q, *s;
+
+	      /* Find previous delimiter */
+	      for (q = p-2; *q != '/' && q >= path; q--)
+		;
+
+	      if (q < path)
+		break;
+	      /* Copy stuff */
+	      s = p + 2;
+	      p = q;
+	      while ((*q++ = *s++))
+		;
+	      continue;
+	    }
+	}
+
+      p++;
+    }
+
+  if (path[0] == 0)
+    {
+      path[0] = '/';
+      path[1] = 0;
+    }
+
+  return path;
+}
+
+/* Expand a PATTERN to the pathname. PATTERN may contain the following
+   macro-notations:
+   ---------+------------ 
+   notation |  expands to
+   ---------+------------
+   %u         user name
+   %h         user's home dir
+   ~          Likewise
+   ---------+------------
+
+   Allocates memory. 
+*/
+char *
+mu_expand_path_pattern (const char *pattern, const char *username)
+{
+  const char *p;
+  char *q;
+  char *path;
+  size_t len = 0;
+  struct mu_auth_data *auth = NULL;
+  
+  for (p = pattern; *p; p++)
+    {
+      if (*p == '~')
+        {
+          if (!auth)
+            {
+              auth = mu_get_auth_by_name (username);
+              if (!auth)
+                return NULL;
+            }
+          len += strlen (auth->dir);
+        }
+      else if (*p == '%')
+	switch (*++p)
+	  {
+	  case 'u':
+	    len += strlen (username);
+	    break;
+	    
+	  case 'h':
+	    if (!auth)
+	      {
+		auth = mu_get_auth_by_name (username);
+		if (!auth)
+		  return NULL;
+	      }
+	    len += strlen (auth->dir);
+	    break;
+	    
+	  case '%':
+	    len++;
+	    break;
+	    
+	  default:
+	    len += 2;
+	  }
+      else
+	len++;
+    }
+  
+  path = malloc (len + 1);
+  if (!path)
+    return NULL;
+
+  p = pattern;
+  q = path;
+  while (*p)
+    {
+      size_t off = strcspn (p, "~%");
+      memcpy (q, p, off);
+      q += off;
+      p += off;
+      if (*p == '~')
+	{
+	  strcpy (q, auth->dir);
+	  q += strlen (auth->dir);
+	  p++;
+	}
+      else if (*p == '%')
+	{
+	  switch (*++p)
+	    {
+	    case 'u':
+	      strcpy (q, username);
+	      q += strlen (username);
+	      break;
+	    
+	    case 'h':
+	      strcpy (q, auth->dir);
+	      q += strlen (auth->dir);
+	      break;
+	  
+	    case '%':
+	      *q++ = '%';
+	      break;
+	  
+	    default:
+	      *q++ = '%';
+	      *q++ = *p;
+	    }
+	  p++;
+	}
+    }
+
+  *q = 0;
+  if (auth)
+    mu_auth_data_free (auth);
+  return path;
+}
diff --git a/libmailutils/base/getcwd.c b/libmailutils/base/getcwd.c
new file mode 100644
index 0000000..4f7ac60
--- /dev/null
+++ b/libmailutils/base/getcwd.c
@@ -0,0 +1,70 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 2002, 2003, 2005, 2006, 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, see 
+   <http://www.gnu.org/licenses/>. */
+
+#if HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <errno.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <string.h>
+
+char *
+mu_getcwd ()
+{
+  char *ret;
+  unsigned path_max;
+  char buf[128];
+
+  errno = 0;
+  ret = getcwd (buf, sizeof (buf));
+  if (ret != NULL)
+    return strdup (buf);
+
+  if (errno != ERANGE)
+    return NULL;
+
+  path_max = 128;
+  path_max += 2;                /* The getcwd docs say to do this. */
+
+  for (;;)
+    {
+      char *cwd = (char *) malloc (path_max);
+
+      errno = 0;
+      ret = getcwd (cwd, path_max);
+      if (ret != NULL)
+        return ret;
+      if (errno != ERANGE)
+        {
+          int save_errno = errno;
+          free (cwd);
+          errno = save_errno;
+          return NULL;
+        }
+
+      free (cwd);
+
+      path_max += path_max / 16;
+      path_max += 32;
+    }
+  /* oops?  */
+  return NULL;
+}
+
diff --git a/libmailutils/base/getpass.c b/libmailutils/base/getpass.c
index 8a37c52..ec88b0e 100644
--- a/libmailutils/base/getpass.c
+++ b/libmailutils/base/getpass.c
@@ -24,7 +24,7 @@
 
 #include <mailutils/types.h>
 #include <mailutils/stream.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 #include <mailutils/cstr.h>
 
 int
diff --git a/libmailutils/base/hostname.c b/libmailutils/base/hostname.c
new file mode 100644
index 0000000..4113dc2
--- /dev/null
+++ b/libmailutils/base/hostname.c
@@ -0,0 +1,102 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 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, see 
+   <http://www.gnu.org/licenses/>. */
+
+#if HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <unistd.h>
+#include <errno.h>
+#include <string.h>
+#include <netdb.h>
+
+#ifndef MAXHOSTNAMELEN
+# define MAXHOSTNAMELEN 64
+#endif
+
+int
+mu_get_host_name (char **host)
+{
+  char *hostname = NULL;
+  size_t size = 0;
+  char *p;
+
+  while (1)
+    {
+      if (size == 0)
+	{
+	  size = MAXHOSTNAMELEN;
+	  p = malloc (size);
+	}
+      else
+	{
+	  size_t ns = size * 2;
+	  if (ns < size)
+	    {
+	      free (hostname);
+	      return ENOMEM;
+	    }
+	  size = ns;
+	  p = realloc (hostname, size);
+	}
+      if (!p)
+	{
+	  free (hostname);
+	  return ENOMEM;
+	}
+      hostname = p;
+      hostname[size - 1] = 0;
+      if (gethostname (hostname, size - 1) == 0)
+	{
+	  if (!hostname[size - 1])
+	    break;
+	}
+      else if (errno != 0 && errno != ENAMETOOLONG && errno != EINVAL
+	       && errno != ENOMEM)
+	{
+	  int rc = errno;
+	  free (hostname);
+	  return rc;
+	}
+    }
+
+  /* Try to return fully qualified host name */
+  if (!strchr (hostname, '.'))
+    {
+      struct hostent *hp = gethostbyname (hostname);
+      if (hp)
+	{
+	  size_t len = strlen (hp->h_name);
+	  if (size < len + 1)
+	    {
+	      p = realloc (hostname, len + 1);
+	      if (!p)
+		{
+		  free (hostname);
+		  return ENOMEM;
+		}
+	      hostname = p;
+	    }
+	  strcpy (hostname, hp->h_name);
+	}
+    }
+  
+  *host = hostname;
+  return 0;
+}  
+
diff --git a/libmailutils/base/kwd.c b/libmailutils/base/kwd.c
index 9135f9f..3e50e93 100644
--- a/libmailutils/base/kwd.c
+++ b/libmailutils/base/kwd.c
@@ -22,7 +22,7 @@
 #include <string.h>
 #include <mailutils/kwd.h>
 #include <mailutils/errno.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 #include <mailutils/cstr.h>
 
 int
diff --git a/libmailutils/base/locale.c b/libmailutils/base/locale.c
index 4234ad4..c871cee 100644
--- a/libmailutils/base/locale.c
+++ b/libmailutils/base/locale.c
@@ -21,7 +21,7 @@
 
 #include <stdlib.h>
 #include <string.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 #include <mailutils/cstr.h>
 
 struct langtab
diff --git a/libmailutils/base/locker.c b/libmailutils/base/locker.c
index b720e20..99a1a4e 100644
--- a/libmailutils/base/locker.c
+++ b/libmailutils/base/locker.c
@@ -40,7 +40,7 @@
 
 #include <mailutils/errno.h>
 #include <mailutils/locker.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 
 #define LOCKFILE_ATTR           0644
 
diff --git a/libmailutils/base/mailcap.c b/libmailutils/base/mailcap.c
index ede3f67..7b0af63 100644
--- a/libmailutils/base/mailcap.c
+++ b/libmailutils/base/mailcap.c
@@ -27,7 +27,7 @@
 #include <mailutils/cctype.h>
 #include <mailutils/mailcap.h>
 #include <mailutils/stream.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 #include <mailutils/cstr.h>
 
 /* Definition of the structure, this should be in mailutils/sys/mailcap.h.  */
diff --git a/libmailutils/base/msgid.c b/libmailutils/base/msgid.c
new file mode 100644
index 0000000..d7905b4
--- /dev/null
+++ b/libmailutils/base/msgid.c
@@ -0,0 +1,230 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 2002, 2003, 2005, 2006, 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, see 
+   <http://www.gnu.org/licenses/>. */
+
+#if HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <sys/time.h>
+#include <errno.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <string.h>
+#include <mailutils/cctype.h>
+#include <mailutils/message.h>
+#include <mailutils/header.h>
+#include <mailutils/util.h>
+#include <mailutils/io.h>
+#include <mailutils/envelope.h>
+#include <mailutils/errno.h>
+
+#define ST_INIT  0
+#define ST_MSGID 1
+
+static int
+strip_message_id (const char *msgid, char **pval)
+{
+  const char *p;
+  char *q;
+  int state;
+  
+  *pval = malloc (strlen (msgid) + 1);
+  if (!*pval)
+    return ENOMEM;
+  state = ST_INIT;
+  for (p = msgid, q = *pval; *p; p++)
+    {
+      switch (state)
+	{
+	case ST_INIT:
+	  if (*p == '<')
+	    {
+	      *q++ = *p;
+	      state = ST_MSGID;
+	    }
+	  else if (mu_isspace (*p))
+	    *q++ = *p;
+	  break;
+
+	case ST_MSGID:
+	  *q++ = *p;
+	  if (*p == '>')
+	    state = ST_INIT;
+	  break;
+	}
+    }
+  *q = 0;
+  return 0;
+}
+
+static int
+get_msgid_header (mu_header_t hdr, const char *name, char **val)
+{
+  const char *p;
+  int status = mu_header_sget_value (hdr, name, &p);
+  if (status)
+    return status;
+  return strip_message_id (p, val);
+}
+
+static char *
+concat (const char *s1, const char *s2)
+{
+  int len = (s1 ? strlen (s1) : 0) + (s2 ? strlen (s2) : 0) + 2;
+  char *s = malloc (len);
+  if (s)
+    {
+      char *p = s;
+      
+      if (s1)
+	{
+	  strcpy (p, s1);
+	  p += strlen (s1);
+	  *p++ = ' ';
+	}
+      if (s2)
+	strcpy (p, s2);
+    }
+  return s;
+}
+
+/* rfc2822:
+   
+   The "References:" field will contain the contents of the parent's
+   "References:" field (if any) followed by the contents of the parent's
+   "Message-ID:" field (if any).  If the parent message does not contain
+   a "References:" field but does have an "In-Reply-To:" field
+   containing a single message identifier, then the "References:" field
+   will contain the contents of the parent's "In-Reply-To:" field
+   followed by the contents of the parent's "Message-ID:" field (if
+   any).  If the parent has none of the "References:", "In-Reply-To:",
+   or "Message-ID:" fields, then the new message will have no
+   References:" field. */
+
+int
+mu_rfc2822_references (mu_message_t msg, char **pstr)
+{
+  char *ref = NULL, *msgid = NULL;
+  mu_header_t hdr;
+  int rc;
+  
+  rc = mu_message_get_header (msg, &hdr);
+  if (rc)
+    return rc;
+  get_msgid_header (hdr, MU_HEADER_MESSAGE_ID, &msgid);
+  if (get_msgid_header (hdr, MU_HEADER_REFERENCES, &ref))
+    get_msgid_header (hdr, MU_HEADER_IN_REPLY_TO, &ref);
+
+  if (ref || msgid)
+    {
+      *pstr = concat (ref, msgid);
+      free (ref);
+      free (msgid);
+      return 0;
+    }
+  return MU_ERR_FAILURE;
+}
+
+int
+mu_rfc2822_msg_id (int subpart, char **pval)
+{
+  char date[4+2+2+2+2+2+1];
+  time_t t = time (NULL);
+  struct tm *tm = localtime (&t);
+  char *host;
+  char *p;
+	  
+  mu_strftime (date, sizeof date, "%Y%m%d%H%M%S", tm);
+  mu_get_host_name (&host);
+
+  if (subpart)
+    {
+      struct timeval tv;
+      gettimeofday (&tv, NULL);
+      mu_asprintf (&p, "<%s.%lu.%d@%s>",
+		   date,
+		   (unsigned long) getpid (),
+		   subpart,
+		   host);
+    }
+  else
+    mu_asprintf (&p, "<%s.%lu@%s>", date, (unsigned long) getpid (), host);
+  free (host);
+  *pval = p;
+  return 0;
+}
+
+#define DATEBUFSIZE 128
+#define COMMENT "Your message of "
+
+/*
+   The "In-Reply-To:" field will contain the contents of the "Message-
+   ID:" field of the message to which this one is a reply (the "parent
+   message").  If there is more than one parent message, then the "In-
+   Reply-To:" field will contain the contents of all of the parents'
+   "Message-ID:" fields.  If there is no "Message-ID:" field in any of
+   the parent messages, then the new message will have no "In-Reply-To:"
+   field.
+*/
+int
+mu_rfc2822_in_reply_to (mu_message_t msg, char **pstr)
+{
+  const char *value = NULL;
+  char *s1 = NULL, *s2 = NULL;
+  mu_header_t hdr;
+  int rc;
+  
+  rc = mu_message_get_header (msg, &hdr);
+  if (rc)
+    return rc;
+  
+  if (mu_header_sget_value (hdr, MU_HEADER_DATE, &value))
+    {
+      mu_envelope_t envelope = NULL;
+      mu_message_get_envelope (msg, &envelope);
+      mu_envelope_sget_date (envelope, &value);
+    }
+
+  if (value)
+    {
+      s1 = malloc (sizeof (COMMENT) + strlen (value));
+      if (!s1)
+	return ENOMEM;
+      strcat (strcpy (s1, COMMENT), value);
+    }
+  
+  if (mu_header_sget_value (hdr, MU_HEADER_MESSAGE_ID, &value) == 0)
+    {
+      s2 = malloc (strlen (value) + 3);
+      if (!s2)
+	{
+	  free (s1);
+	  return ENOMEM;
+	}
+      strcat (strcpy (s2, "\n\t"), value);
+    }
+
+  if (s1 || s2)
+    {
+      *pstr = concat (s1, s2);
+      free (s1);
+      free (s2);
+      return 0;
+    }
+  return MU_ERR_FAILURE;
+}
diff --git a/libmailutils/base/mutil.c b/libmailutils/base/mutil.c
index 8612106..ffa04dd 100644
--- a/libmailutils/base/mutil.c
+++ b/libmailutils/base/mutil.c
@@ -20,1375 +20,16 @@
 # include <config.h>
 #endif
 
-#include <errno.h>
-#include <limits.h>
-#include <netdb.h>
-#include <pwd.h>
-#include <signal.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <time.h>
-#include <unistd.h>
-#include <ctype.h>
-#include <sys/param.h>
 #include <sys/types.h>
 #include <sys/stat.h>
-#include <sys/time.h>
-#include <sys/wait.h>
-#include <sys/select.h>
-
-#ifdef HAVE_STRINGS_H
-#include <strings.h>
-#endif
-
-#include <mailutils/address.h>
-#include <mailutils/assoc.h>
+#include <time.h>
 #include <mailutils/argcv.h>
 #include <mailutils/error.h>
 #include <mailutils/errno.h>
-#include <mailutils/iterator.h>
-#include <mailutils/mutil.h>
-#include <mailutils/parse822.h>
-#include <mailutils/mu_auth.h>
-#include <mailutils/header.h>
-#include <mailutils/message.h>
-#include <mailutils/envelope.h>
 #include <mailutils/nls.h>
+#include <mailutils/assoc.h>
 #include <mailutils/stream.h>
-#include <mailutils/filter.h>
 #include <mailutils/sql.h>
-#include <mailutils/url.h>
-#include <mailutils/io.h>
-#include <mailutils/cctype.h>
-#include <mailutils/cstr.h>
-
-#include <mailutils/sys/registrar.h>
-
-/* convert a sequence of hex characters into an integer */
-
-unsigned long
-mu_hex2ul (char hex)
-{
-  if (hex >= '0' && hex <= '9')
-   return hex - '0';
-
-  if (hex >= 'a' && hex <= 'z')
-    return hex - 'a' + 10;
-
-  if (hex >= 'A' && hex <= 'Z')
-    return hex - 'A' + 10;
-
-  return -1;
-}
-
-size_t
-mu_hexstr2ul (unsigned long *ul, const char *hex, size_t len)
-{
-  size_t r;
-
-  *ul = 0;
-
-  for (r = 0; r < len; r++)
-    {
-      unsigned long v = mu_hex2ul (hex[r]);
-
-      if (v == (unsigned long)-1)
-	return r;
-
-      *ul = *ul * 16 + v;
-    }
-  return r;
-}
-
-
-char *
-mu_get_homedir (void)
-{
-  char *homedir = getenv ("HOME");
-  if (homedir)
-    homedir = strdup (homedir);
-  else
-    {
-      struct mu_auth_data *auth = mu_get_auth_by_uid (geteuid ());
-      if (!auth)
-	return NULL;
-      homedir = strdup (auth->dir);
-      mu_auth_data_free (auth);
-    }
-  return homedir;
-}
-
-char *
-mu_getcwd ()
-{
-  char *ret;
-  unsigned path_max;
-  char buf[128];
-
-  errno = 0;
-  ret = getcwd (buf, sizeof (buf));
-  if (ret != NULL)
-    return strdup (buf);
-
-  if (errno != ERANGE)
-    return NULL;
-
-  path_max = 128;
-  path_max += 2;                /* The getcwd docs say to do this. */
-
-  for (;;)
-    {
-      char *cwd = (char *) malloc (path_max);
-
-      errno = 0;
-      ret = getcwd (cwd, path_max);
-      if (ret != NULL)
-        return ret;
-      if (errno != ERANGE)
-        {
-          int save_errno = errno;
-          free (cwd);
-          errno = save_errno;
-          return NULL;
-        }
-
-      free (cwd);
-
-      path_max += path_max / 16;
-      path_max += 32;
-    }
-  /* oops?  */
-  return NULL;
-}
-
-char *
-mu_get_full_path (const char *file)
-{
-  char *p = NULL;
-
-  if (!file)
-    p = mu_getcwd ();
-  else if (*file != '/')
-    {
-      char *cwd = mu_getcwd ();
-      if (cwd)
-	{
-	  p = calloc (strlen (cwd) + 1 + strlen (file) + 1, 1);
-	  if (p)
-	    sprintf (p, "%s/%s", cwd, file);
-	  free (cwd);
-	}
-    }
-
-  if (!p)
-    p = strdup (file);
-  return p;
-}
-
-/* NOTE: Allocates Memory.  */
-/* Expand: ~ --> /home/user and to ~guest --> /home/guest.  */
-char *
-mu_tilde_expansion (const char *ref, const char *delim, const char *homedir)
-{
-  char *base = strdup (ref);
-  char *home = NULL;
-  char *proto = NULL;
-  size_t proto_len = 0;
-  char *p;
-
-  for (p = base; *p && isascii (*p) && isalnum (*p); p++)
-    ;
-  
-  if (*p == ':')
-    {
-      p++;
-      proto_len = p - base;
-      proto = malloc (proto_len + 1);
-      if (!proto)
-	return NULL;
-      memcpy (proto, base, proto_len);
-      proto[proto_len] = 0;
-      /* Allow for extra pair of slashes after the protocol specifier */
-      if (*p == delim[0])
-	p++;
-      if (*p == delim[0])
-	p++;
-    }
-  else
-    p = base;
-  
-  if (*p == '~')
-    {
-      p++;
-      if (*p == delim[0] || *p == '\0')
-        {
-	  char *s;
-	  if (!homedir)
-	    {
-	      home = mu_get_homedir ();
-	      if (!home)
-		return base;
-	      homedir = home;
-	    }
-	  s = calloc (proto_len + strlen (homedir) + strlen (p) + 1, 1);
-	  if (proto_len)
-	    strcpy (s, proto);
-	  else
-	    s[0] = 0;
-          strcat (s, homedir);
-          strcat (s, p);
-          free (base);
-          base = s;
-        }
-      else
-        {
-          struct mu_auth_data *auth;
-          char *s = p;
-          char *name;
-          while (*s && *s != delim[0])
-            s++;
-          name = calloc (s - p + 1, 1);
-          memcpy (name, p, s - p);
-          name[s - p] = '\0';
-	  
-          auth = mu_get_auth_by_name (name);
-          free (name);
-          if (auth)
-            {
-              char *buf = calloc (proto_len + strlen (auth->dir)
-				  + strlen (s) + 1, 1);
-	      if (proto_len)
-		strcpy (buf, proto);
-	      else
-		buf[0] = 0;
-	      strcat (buf, auth->dir);
-              strcat (buf, s);
-              free (base);
-              base = buf;
-	      mu_auth_data_free (auth);
-            }
-        }
-    }
-  if (home)
-    free (home);
-  return base;
-}
-
-/* Smart strncpy that always add the null and returns the number of bytes
-   written.  */
-size_t
-mu_cpystr (char *dst, const char *src, size_t size)
-{
-  size_t len = src ? strlen (src) : 0 ;
-  if (dst == NULL || size == 0)
-    return len;
-  if (len >= size)
-    len = size - 1;
-  memcpy (dst, src, len);
-  dst[len] = '\0';
-  return len;
-}
-
-#ifndef MAXHOSTNAMELEN
-# define MAXHOSTNAMELEN 64
-#endif
-
-int
-mu_get_host_name (char **host)
-{
-  char *hostname = NULL;
-  size_t size = 0;
-  char *p;
-
-  while (1)
-    {
-      if (size == 0)
-	{
-	  size = MAXHOSTNAMELEN;
-	  p = malloc (size);
-	}
-      else
-	{
-	  size_t ns = size * 2;
-	  if (ns < size)
-	    {
-	      free (hostname);
-	      return ENOMEM;
-	    }
-	  size = ns;
-	  p = realloc (hostname, size);
-	}
-      if (!p)
-	{
-	  free (hostname);
-	  return ENOMEM;
-	}
-      hostname = p;
-      hostname[size - 1] = 0;
-      if (gethostname (hostname, size - 1) == 0)
-	{
-	  if (!hostname[size - 1])
-	    break;
-	}
-      else if (errno != 0 && errno != ENAMETOOLONG && errno != EINVAL
-	       && errno != ENOMEM)
-	{
-	  int rc = errno;
-	  free (hostname);
-	  return rc;
-	}
-    }
-
-  /* Try to return fully qualified host name */
-  if (!strchr (hostname, '.'))
-    {
-      struct hostent *hp = gethostbyname (hostname);
-      if (hp)
-	{
-	  size_t len = strlen (hp->h_name);
-	  if (size < len + 1)
-	    {
-	      p = realloc (hostname, len + 1);
-	      if (!p)
-		{
-		  free (hostname);
-		  return ENOMEM;
-		}
-	      hostname = p;
-	    }
-	  strcpy (hostname, hp->h_name);
-	}
-    }
-  
-  *host = hostname;
-  return 0;
-}  
-
-/*
- * Functions used to convert unix mailbox/user names into RFC822 addr-specs.
- */
-
-static char *mu_user_email = 0;
-
-int
-mu_set_user_email (const char *candidate)
-{
-  int err = 0;
-  mu_address_t addr = NULL;
-  size_t emailno = 0;
-  char *email = NULL;
-  const char *domain = NULL;
-  
-  if ((err = mu_address_create (&addr, candidate)) != 0)
-    return err;
-
-  if ((err = mu_address_get_email_count (addr, &emailno)) != 0)
-    goto cleanup;
-
-  if (emailno != 1)
-    {
-      errno = EINVAL;
-      goto cleanup;
-    }
-
-  if ((err = mu_address_aget_email (addr, 1, &email)) != 0)
-    goto cleanup;
-
-  if (mu_user_email)
-    free (mu_user_email);
-
-  mu_user_email = email;
-
-  if ((err = mu_address_sget_domain (addr, 1, &domain)) == 0)
-    mu_set_user_email_domain (domain);
-  
-cleanup:
-  mu_address_destroy (&addr);
-
-  return err;
-}
-
-static char *mu_user_email_domain = 0;
-
-int
-mu_set_user_email_domain (const char *domain)
-{
-  char *d = NULL;
-  
-  if (!domain)
-    return EINVAL;
-  
-  d = strdup (domain);
-
-  if (!d)
-    return ENOMEM;
-
-  if (mu_user_email_domain)
-    free (mu_user_email_domain);
-
-  mu_user_email_domain = d;
-
-  return 0;
-}
-
-/* FIXME: must be called _sget_ */
-int
-mu_get_user_email_domain (const char **domain)
-{
-  int err = 0;
-
-  if (!mu_user_email_domain)
-    {
-      if ((err = mu_get_host_name (&mu_user_email_domain)))
-	return err;
-    }
-
-  *domain = mu_user_email_domain;
-
-  return 0;
-}
-
-int
-mu_aget_user_email_domain (char **pdomain)
-{
-  const char *domain;
-  int status = mu_get_user_email_domain (&domain);
-  if (status)
-    return status;
-  if (domain == NULL)
-    *pdomain = NULL;
-  else
-    {
-      *pdomain = strdup (domain);
-      if (*pdomain == NULL)
-	return ENOMEM;
-    }
-  return 0;
-}
-
-/* Note: allocates memory */
-char *
-mu_get_user_email (const char *name)
-{
-  int status = 0;
-  char *localpart = NULL;
-  const char *domainpart = NULL;
-  char *email = NULL;
-  char *tmpname = NULL;
-
-  if (!name && mu_user_email)
-    {
-      email = strdup (mu_user_email);
-      if (!email)
-	errno = ENOMEM;
-      return email;
-    }
-
-  if (!name)
-    {
-      struct mu_auth_data *auth = mu_get_auth_by_uid (getuid ());
-      if (!auth)
-	{
-	  errno = EINVAL;
-	  return NULL;
-	}
-      name = tmpname = strdup(auth->name);
-      if (auth)
-	mu_auth_data_free (auth);
-    }
-
-  status = mu_get_user_email_domain (&domainpart);
-
-  if (status)
-    {
-      free(tmpname);
-      errno = status;
-      return NULL;
-    }
-
-  if ((status = mu_parse822_quote_local_part (&localpart, name)))
-    {
-      free(tmpname);
-      errno = status;
-      return NULL;
-    }
-
-
-  email = malloc (strlen (localpart) + 1
-		  + strlen (domainpart) + 1);
-  if (!email)
-    errno = ENOMEM;
-  else
-    sprintf (email, "%s@%s", localpart, domainpart);
-
-  free(tmpname);
-  free (localpart);
-
-  return email;
-}
-
-/* mu_normalize_path: convert pathname containig relative paths specs (../)
-   into an equivalent absolute path. Strip trailing delimiter if present,
-   unless it is the only character left. E.g.:
-
-         /home/user/../smith   -->   /home/smith
-	 /home/user/../..      -->   /
-
-*/
-char *
-mu_normalize_path (char *path)
-{
-  int len;
-  char *p;
-
-  if (!path)
-    return path;
-
-  len = strlen (path);
-
-  /* Empty string is returned as is */
-  if (len == 0)
-    return path;
-
-  /* delete trailing delimiter if any */
-  if (len && path[len-1] == '/')
-    path[len-1] = 0;
-
-  /* Eliminate any /../ */
-  for (p = strchr (path, '.'); p; p = strchr (p, '.'))
-    {
-      if (p > path && p[-1] == '/')
-	{
-	  if (p[1] == '.' && (p[2] == 0 || p[2] == '/'))
-	    /* found */
-	    {
-	      char *q, *s;
-
-	      /* Find previous delimiter */
-	      for (q = p-2; *q != '/' && q >= path; q--)
-		;
-
-	      if (q < path)
-		break;
-	      /* Copy stuff */
-	      s = p + 2;
-	      p = q;
-	      while ((*q++ = *s++))
-		;
-	      continue;
-	    }
-	}
-
-      p++;
-    }
-
-  if (path[0] == 0)
-    {
-      path[0] = '/';
-      path[1] = 0;
-    }
-
-  return path;
-}
-
-/* Create and open a temporary file. Be very careful about it, since we
-   may be running with extra privilege i.e setgid().
-   Returns file descriptor of the open file.
-   If namep is not NULL, the pointer to the malloced file name will
-   be stored there. Otherwise, the file is unlinked right after open,
-   i.e. it will disappear after close(fd). */
-
-#ifndef P_tmpdir
-# define P_tmpdir "/tmp"
-#endif
-
-int
-mu_tempfile (const char *tmpdir, char **namep)
-{
-  char *filename;
-  int fd;
-
-  if (!tmpdir)
-    tmpdir = (getenv ("TMPDIR")) ? getenv ("TMPDIR") : P_tmpdir;
-
-  filename = malloc (strlen (tmpdir) + /*'/'*/1 + /* "muXXXXXX" */8 + 1);
-  if (!filename)
-    return -1;
-  sprintf (filename, "%s/muXXXXXX", tmpdir);
-
-#ifdef HAVE_MKSTEMP
-  {
-    int save_mask = umask (077);
-    fd = mkstemp (filename);
-    umask (save_mask);
-  }
-#else
-  if (mktemp (filename))
-    fd = open (filename, O_CREAT|O_EXCL|O_RDWR, 0600);
-  else
-    fd = -1;
-#endif
-
-  if (fd == -1)
-    {
-      mu_error (_("cannot open temporary file: %s"), mu_strerror (errno));
-      free (filename);
-      return -1;
-    }
-
-  if (namep)
-    *namep = filename;
-  else
-    {
-      unlink (filename);
-      free (filename);
-    }
-
-  return fd;
-}
-
-/* Create a unique temporary file name in tmpdir. The function
-   creates an empty file with this name to avoid possible race
-   conditions. Returns a pointer to the malloc'ed file name.
-   If tmpdir is NULL, the value of the environment variable
-   TMPDIR or the hardcoded P_tmpdir is used, whichever is defined. */
-
-char *
-mu_tempname (const char *tmpdir)
-{
-  char *filename = NULL;
-  int fd = mu_tempfile (tmpdir, &filename);
-  close (fd);
-  return filename;
-}
-
-/* See Advanced Programming in the UNIX Environment, Stevens,
- * program  10.20 for the rational for the signal handling. I
- * had to look it up, so if somebody else is curious, thats where
- * to find it.
- */
-int 
-mu_spawnvp (const char *prog, char *av[], int *stat)
-{
-  pid_t pid;
-  int err = 0;
-  int progstat;
-  struct sigaction ignore;
-  struct sigaction saveintr;
-  struct sigaction savequit;
-  sigset_t chldmask;
-  sigset_t savemask;
-
-  if (!prog || !av)
-    return EINVAL;
-
-  ignore.sa_handler = SIG_IGN;	/* ignore SIGINT and SIGQUIT */
-  ignore.sa_flags = 0;
-  sigemptyset (&ignore.sa_mask);
-
-  if (sigaction (SIGINT, &ignore, &saveintr) < 0)
-    return errno;
-  if (sigaction (SIGQUIT, &ignore, &savequit) < 0)
-    {
-      sigaction (SIGINT, &saveintr, NULL);
-      return errno;
-    }
-
-  sigemptyset (&chldmask);	/* now block SIGCHLD */
-  sigaddset (&chldmask, SIGCHLD);
-
-  if (sigprocmask (SIG_BLOCK, &chldmask, &savemask) < 0)
-    {
-      sigaction (SIGINT, &saveintr, NULL);
-      sigaction (SIGQUIT, &savequit, NULL);
-      return errno;
-    }
-
-#ifdef HAVE_VFORK
-  pid = vfork ();
-#else
-  pid = fork ();
-#endif
-
-  if (pid < 0)
-    {
-      err = errno;
-    }
-  else if (pid == 0)
-    {				/* child */
-      /* restore previous signal actions & reset signal mask */
-      sigaction (SIGINT, &saveintr, NULL);
-      sigaction (SIGQUIT, &savequit, NULL);
-      sigprocmask (SIG_SETMASK, &savemask, NULL);
-
-      execvp (prog, av);
-#ifdef HAVE__EXIT      
-      _exit (127);		/* exec error */
-#else
-      exit (127);
-#endif
-    }
-  else
-    {				/* parent */
-      while (waitpid (pid, &progstat, 0) < 0)
-	if (errno != EINTR)
-	  {
-	    err = errno;	/* error other than EINTR from waitpid() */
-	    break;
-	  }
-      if (err == 0 && stat)
-	*stat = progstat;
-    }
-
-  /* restore previous signal actions & reset signal mask */
-  /* preserve first error number, but still try and reset the signals */
-  if (sigaction (SIGINT, &saveintr, NULL) < 0)
-    err = err ? err : errno;
-  if (sigaction (SIGQUIT, &savequit, NULL) < 0)
-    err = err ? err : errno;
-  if (sigprocmask (SIG_SETMASK, &savemask, NULL) < 0)
-    err = err ? err : errno;
-
-  return err;
-}
-
-/* The result of readlink() may be a path relative to that link, 
- * qualify it if necessary.
- */
-static void
-mu_qualify_link (const char *path, const char *link, char *qualified)
-{
-  const char *lb = NULL;
-  size_t len;
-
-  /* link is full path */
-  if (*link == '/')
-    {
-      mu_cpystr (qualified, link, _POSIX_PATH_MAX);
-      return;
-    }
-
-  if ((lb = strrchr (path, '/')) == NULL)
-    {
-      /* no path in link */
-      mu_cpystr (qualified, link, _POSIX_PATH_MAX);
-      return;
-    }
-
-  len = lb - path + 1;
-  memcpy (qualified, path, len);
-  mu_cpystr (qualified + len, link, _POSIX_PATH_MAX - len);
-}
-
-#ifndef _POSIX_SYMLOOP_MAX
-# define _POSIX_SYMLOOP_MAX 255
-#endif
-
-int
-mu_unroll_symlink (char *out, size_t outsz, const char *in)
-{
-  char path[_POSIX_PATH_MAX];
-  int symloops = 0;
-
-  while (symloops++ < _POSIX_SYMLOOP_MAX)
-    {
-      struct stat s;
-      char link[_POSIX_PATH_MAX];
-      char qualified[_POSIX_PATH_MAX];
-      int len;
-
-      if (lstat (in, &s) == -1)
-	return errno;
-
-      if (!S_ISLNK (s.st_mode))
-	{
-	  mu_cpystr (path, in, sizeof (path));
-	  break;
-	}
-
-      if ((len = readlink (in, link, sizeof (link))) == -1)
-	return errno;
-
-      link[(len >= sizeof (link)) ? (sizeof (link) - 1) : len] = '\0';
-
-      mu_qualify_link (in, link, qualified);
-
-      mu_cpystr (path, qualified, sizeof (path));
-
-      in = path;
-    }
-
-  mu_cpystr (out, path, outsz);
-
-  return 0;
-}
-
-/* Expand a PATTERN to the pathname. PATTERN may contain the following
-   macro-notations:
-   ---------+------------ 
-   notation |  expands to
-   ---------+------------
-   %u         user name
-   %h         user's home dir
-   ~          Likewise
-   ---------+------------
-
-   Allocates memory. 
-*/
-char *
-mu_expand_path_pattern (const char *pattern, const char *username)
-{
-  const char *p;
-  char *q;
-  char *path;
-  size_t len = 0;
-  struct mu_auth_data *auth = NULL;
-  
-  for (p = pattern; *p; p++)
-    {
-      if (*p == '~')
-        {
-          if (!auth)
-            {
-              auth = mu_get_auth_by_name (username);
-              if (!auth)
-                return NULL;
-            }
-          len += strlen (auth->dir);
-        }
-      else if (*p == '%')
-	switch (*++p)
-	  {
-	  case 'u':
-	    len += strlen (username);
-	    break;
-	    
-	  case 'h':
-	    if (!auth)
-	      {
-		auth = mu_get_auth_by_name (username);
-		if (!auth)
-		  return NULL;
-	      }
-	    len += strlen (auth->dir);
-	    break;
-	    
-	  case '%':
-	    len++;
-	    break;
-	    
-	  default:
-	    len += 2;
-	  }
-      else
-	len++;
-    }
-  
-  path = malloc (len + 1);
-  if (!path)
-    return NULL;
-
-  p = pattern;
-  q = path;
-  while (*p)
-    {
-      size_t off = strcspn (p, "~%");
-      memcpy (q, p, off);
-      q += off;
-      p += off;
-      if (*p == '~')
-	{
-	  strcpy (q, auth->dir);
-	  q += strlen (auth->dir);
-	  p++;
-	}
-      else if (*p == '%')
-	{
-	  switch (*++p)
-	    {
-	    case 'u':
-	      strcpy (q, username);
-	      q += strlen (username);
-	      break;
-	    
-	    case 'h':
-	      strcpy (q, auth->dir);
-	      q += strlen (auth->dir);
-	      break;
-	  
-	    case '%':
-	      *q++ = '%';
-	      break;
-	  
-	    default:
-	      *q++ = '%';
-	      *q++ = *p;
-	    }
-	  p++;
-	}
-    }
-
-  *q = 0;
-  if (auth)
-    mu_auth_data_free (auth);
-  return path;
-}
-
-#define ST_INIT  0
-#define ST_MSGID 1
-
-static int
-strip_message_id (char *msgid, char **pval)
-{
-  char *p, *q;
-  int state;
-  
-  *pval = strdup (msgid);
-  if (!*pval)
-    return ENOMEM;
-  state = ST_INIT;
-  for (p = q = *pval; *p; p++)
-    {
-      switch (state)
-	{
-	case ST_INIT:
-	  if (*p == '<')
-	    {
-	      *q++ = *p;
-	      state = ST_MSGID;
-	    }
-	  else if (isspace (*p))
-	    *q++ = *p;
-	  break;
-
-	case ST_MSGID:
-	  *q++ = *p;
-	  if (*p == '>')
-	    state = ST_INIT;
-	  break;
-	}
-    }
-  *q = 0;
-  return 0;
-}
-
-int
-get_msgid_header (mu_header_t hdr, const char *name, char **val)
-{
-  char *p;
-  int status = mu_header_aget_value (hdr, name, &p);
-  if (status)
-    return status;
-  status = strip_message_id (p, val);
-  free (p);
-  return status;
-}
-
-static char *
-concat (const char *s1, const char *s2)
-{
-  int len = (s1 ? strlen (s1) : 0) + (s2 ? strlen (s2) : 0) + 2;
-  char *s = malloc (len);
-  if (s)
-    {
-      char *p = s;
-      
-      if (s1)
-	{
-	  strcpy (p, s1);
-	  p += strlen (s1);
-	  *p++ = ' ';
-	}
-      if (s2)
-	strcpy (p, s2);
-    }
-  return s;
-}
-
-/* rfc2822:
-   
-   The "References:" field will contain the contents of the parent's
-   "References:" field (if any) followed by the contents of the parent's
-   "Message-ID:" field (if any).  If the parent message does not contain
-   a "References:" field but does have an "In-Reply-To:" field
-   containing a single message identifier, then the "References:" field
-   will contain the contents of the parent's "In-Reply-To:" field
-   followed by the contents of the parent's "Message-ID:" field (if
-   any).  If the parent has none of the "References:", "In-Reply-To:",
-   or "Message-ID:" fields, then the new message will have no
-   References:" field. */
-
-int
-mu_rfc2822_references (mu_message_t msg, char **pstr)
-{
-  char *ref = NULL, *msgid = NULL;
-  mu_header_t hdr;
-  int rc;
-  
-  rc = mu_message_get_header (msg, &hdr);
-  if (rc)
-    return rc;
-  get_msgid_header (hdr, MU_HEADER_MESSAGE_ID, &msgid);
-  if (get_msgid_header (hdr, MU_HEADER_REFERENCES, &ref))
-    get_msgid_header (hdr, MU_HEADER_IN_REPLY_TO, &ref);
-
-  if (ref || msgid)
-    {
-      *pstr = concat (ref, msgid);
-      free (ref);
-      free (msgid);
-      return 0;
-    }
-  return MU_ERR_FAILURE;
-}
-
-int
-mu_rfc2822_msg_id (int subpart, char **pval)
-{
-  char date[4+2+2+2+2+2+1];
-  time_t t = time (NULL);
-  struct tm *tm = localtime (&t);
-  char *host;
-  char *p;
-	  
-  mu_strftime (date, sizeof date, "%Y%m%d%H%M%S", tm);
-  mu_get_host_name (&host);
-
-  if (subpart)
-    {
-      struct timeval tv;
-      gettimeofday (&tv, NULL);
-      mu_asprintf (&p, "<%s.%lu.%d@%s>",
-		   date,
-		   (unsigned long) getpid (),
-		   subpart,
-		   host);
-    }
-  else
-    mu_asprintf (&p, "<%s.%lu@%s>", date, (unsigned long) getpid (), host);
-  free (host);
-  *pval = p;
-  return 0;
-}
-
-#define DATEBUFSIZE 128
-#define COMMENT "Your message of "
-
-/*
-   The "In-Reply-To:" field will contain the contents of the "Message-
-   ID:" field of the message to which this one is a reply (the "parent
-   message").  If there is more than one parent message, then the "In-
-   Reply-To:" field will contain the contents of all of the parents'
-   "Message-ID:" fields.  If there is no "Message-ID:" field in any of
-   the parent messages, then the new message will have no "In-Reply-To:"
-   field.
-*/
-int
-mu_rfc2822_in_reply_to (mu_message_t msg, char **pstr)
-{
-  const char *value = NULL;
-  char *s1 = NULL, *s2 = NULL;
-  mu_header_t hdr;
-  int rc;
-  
-  rc = mu_message_get_header (msg, &hdr);
-  if (rc)
-    return rc;
-  
-  if (mu_header_sget_value (hdr, MU_HEADER_DATE, &value))
-    {
-      mu_envelope_t envelope = NULL;
-      mu_message_get_envelope (msg, &envelope);
-      mu_envelope_sget_date (envelope, &value);
-    }
-
-  if (value)
-    {
-      s1 = malloc (sizeof (COMMENT) + strlen (value));
-      if (!s1)
-	return ENOMEM;
-      strcat (strcpy (s1, COMMENT), value);
-    }
-  
-  if (mu_header_sget_value (hdr, MU_HEADER_MESSAGE_ID, &value) == 0)
-    {
-      s2 = malloc (strlen (value) + 3);
-      if (!s2)
-	{
-	  free (s1);
-	  return ENOMEM;
-	}
-      strcat (strcpy (s2, "\n\t"), value);
-    }
-
-  if (s1 || s2)
-    {
-      *pstr = concat (s1, s2);
-      free (s1);
-      free (s2);
-      return 0;
-    }
-  return MU_ERR_FAILURE;
-}
-
-/* Based on strstr from GNU libc (Stephen R. van den Berg,
-   berg@pool.informatik.rwth-aachen.de) */
-
-char *
-mu_strcasestr (const char *a_haystack, const char *a_needle)
-{
-  register const unsigned char *haystack = (unsigned char*) a_haystack,
-    *needle = (unsigned char*) a_needle;
-  register unsigned int b, c;
-
-#define U(c) mu_toupper (c)
-  if ((b = U (*needle)))
-    {
-      haystack--;		
-      do
-	{
-	  if (!(c = *++haystack))
-	    goto ret0;
-	}
-      while (U (c) != b);
-
-      if (!(c = *++needle))
-	goto foundneedle;
-
-      c = U (c);
-      ++needle;
-      goto jin;
-
-      for (;;)
-        { 
-          register unsigned int a;
-	  register const unsigned char *rhaystack, *rneedle;
-
-	  do
-	    {
-	      if (!(a = *++haystack))
-		goto ret0;
-	      if (U (a) == b)
-		break;
-	      if (!(a = *++haystack))
-		goto ret0;
-shloop:       ;
-            }
-          while (U (a) != b);
-	  
-jin:     if (!(a = *++haystack))
-	    goto ret0;
-
-	  if (U (a) != c)
-	    goto shloop;
-
-	  if (U (*(rhaystack = haystack-- + 1)) ==
-	      (a = U (*(rneedle = needle))))
-	    do
-	      {
-		if (!a)
-		  goto foundneedle;
-		if (U (*++rhaystack) != (a = U (*++needle)))
-		  break;
-		if (!a)
-		  goto foundneedle;
-	      }
-	    while (U (*++rhaystack) == (a = U (*++needle)));
-
-	  needle = rneedle;
-
-	  if (!a)
-	    break;
-        }
-    }
-foundneedle:
-  return (char*)haystack;
-ret0:
-  return NULL;
-
-#undef U
-}  
-
-int
-mu_string_unfold (char *text, size_t *plen)
-{
-  char *p, *q;
-  enum uf_state { uf_init, uf_nl, uf_fold } state = uf_init;
-#define ISSPACE(c) (c == '\r' || c == ' ' || c == '\t')
-  
-  if (!text)
-    return EINVAL;
-  
-  for (p = q = text; *q; q++)
-    {
-      switch (state)
-	{
-	case uf_init:
-	  if (*q == '\n')
-	    state = uf_nl;
-	  else
-	    *p++ = *q;
-	  break;
-
-	case uf_nl:
-	  if (ISSPACE (*q))
-	    state = uf_fold;
-	  else
-	    {
-	      state = uf_init;
-	      *p++ = *q;
-	    }
-	  break;
-
-	case uf_fold:
-	  if (!ISSPACE (*q))
-	    {
-	      *p++ = ' ';
-	      *p++ = *q;
-	      state = uf_init;
-	    }
-	  break;
-	}
-    }
-  
-  *p++ = 0;
-  if (plen)
-    *plen = p - text;
-  return 0;
-}
-
-int
-mu_true_answer_p (const char *p)
-{
-  if (!p)
-    return -1;
-
-  while (*p && isspace (*p))
-    p++;
-
-  if (*p)
-    {
-      /* TRANSLATORS: This is a list of characters which start
-	 an affirmative answer. Whenever possible, please preserve
-	 'yY' in your translation, e.g., for Euskara:
-
-	 msgstr "yYbB";
-      */
-      if (strchr (_("yY"), *p))
-	return 1;
-
-      /* TRANSLATORS: This is a list of characters which start
-	 a negative answer. Whenever possible, please preserve
-	 'nN' in your translation, e.g., for Euskara:
-
-	 msgstr "nNeE";
-      */
-      else if (strchr (_("nN"), *p))
-	return 0;
-    }
-  return -1;
-}
-
-/* Returns true if SCHEME represents a local (autodetect) mail folder.  */
-int
-mu_scheme_autodetect_p (mu_url_t url)
-{
-  if (mu_url_is_scheme (url, "file"))
-    {
-      mu_url_expand_path (url);
-      return 1;
-    }
-  return 0;
-}
-    
-int
-mu_fd_wait (int fd, int *pflags, struct timeval *tvp)
-{
-  fd_set rdset, wrset, exset;
-  int rc;
-
-  FD_ZERO (&rdset);
-  FD_ZERO (&wrset);
-  FD_ZERO (&exset);
-  if ((*pflags) & MU_STREAM_READY_RD)
-    FD_SET (fd, &rdset);
-  if ((*pflags) & MU_STREAM_READY_WR)
-    FD_SET (fd, &wrset);
-  if ((*pflags) & MU_STREAM_READY_EX)
-    FD_SET (fd, &exset);
-  
-  do
-    {
-      if (tvp)
-	{
-	  struct timeval tv = *tvp; 
-	  rc = select (fd + 1, &rdset, &wrset, &exset, &tv);
-	}
-      else
-	rc = select (fd + 1, &rdset, &wrset, &exset, NULL);
-    }
-  while (rc == -1 && errno == EINTR);
-
-  if (rc < 0)
-    return errno;
-  
-  *pflags = 0;
-  if (rc > 0)
-    {
-      if (FD_ISSET (fd, &rdset))
-	*pflags |= MU_STREAM_READY_RD;
-      if (FD_ISSET (fd, &wrset))
-	*pflags |= MU_STREAM_READY_WR;
-      if (FD_ISSET (fd, &exset))
-	*pflags |= MU_STREAM_READY_EX;
-    }
-  return 0;
-}
-
-enum mu_iconv_fallback_mode mu_default_fallback_mode = mu_fallback_copy_octal;
-
-int
-mu_set_default_fallback (const char *str)
-{
-  if (strcmp (str, "none") == 0)
-    mu_default_fallback_mode = mu_fallback_none;
-  else if (strcmp (str, "copy-pass") == 0)
-    mu_default_fallback_mode = mu_fallback_copy_pass;
-  else if (strcmp (str, "copy-octal") == 0)
-    mu_default_fallback_mode = mu_fallback_copy_octal;
-  else
-    return EINVAL;
-  return 0;
-}
-
-int
-mu_decode_filter (mu_stream_t *pfilter, mu_stream_t input,
-		  const char *filter_type,
-		  const char *fromcode, const char *tocode)
-{
-  mu_stream_t filter;
-  
-  int status = mu_filter_create (&filter, input, filter_type,
-				 MU_FILTER_DECODE, MU_STREAM_READ);
-  if (status)
-    return status;
-
-  if (fromcode && tocode && mu_c_strcasecmp (fromcode, tocode))
-    {
-      mu_stream_t cvt;
-
-      status = mu_filter_iconv_create (&cvt, filter, fromcode, tocode,
-				       0, mu_default_fallback_mode);
-      if (status == 0)
-	{
-          mu_stream_unref (filter);
-          filter = cvt;
-	}
-    }
-  *pfilter = filter;
-  return 0;
-}
 
 int
 mu_is_proto (const char *p)
@@ -1414,20 +55,13 @@ mu_mh_delim (const char *str)
   return str[0] == '\n';
 }
 
-char *
-__argp_base_name (const char *arg)
-{
-  char *p = strrchr (arg, '/');
-  return (char *)(p ? p + 1 : arg);
-}
-
 /* A locale-independent version of strftime */
 size_t
 mu_strftime (char *s, size_t max, const char *format, const struct tm *tm)
 {
   size_t size;
   mu_set_locale ("C");
-  size = strftime(s, max, format, tm);
+  size = strftime (s, max, format, tm);
   mu_restore_locale ();
   return size;
 }
diff --git a/libmailutils/base/parsedate.y b/libmailutils/base/parsedate.y
index 98c932b..90f0681 100644
--- a/libmailutils/base/parsedate.y
+++ b/libmailutils/base/parsedate.y
@@ -610,7 +610,7 @@ o_merid	: /* empty */
 # endif
 #endif
 
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 
 /* Month and day table. */
 static SYMBOL const month_day_tab[] = {
diff --git a/libmailutils/base/permstr.c b/libmailutils/base/permstr.c
index ae16ff6..f49b27b 100644
--- a/libmailutils/base/permstr.c
+++ b/libmailutils/base/permstr.c
@@ -19,7 +19,7 @@
 # include <config.h>
 #endif
 #include <mailutils/stream.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 #include <mailutils/errno.h>
 
 #define FILE_PERM_READ 0x1
diff --git a/libmailutils/base/registrar.c b/libmailutils/base/registrar.c
index 33782e9..d24aac2 100644
--- a/libmailutils/base/registrar.c
+++ b/libmailutils/base/registrar.c
@@ -34,7 +34,7 @@
 #include <mailutils/nls.h>
 #include <mailutils/error.h>
 #include <mailutils/url.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 #include <mailutils/sys/registrar.h>
 
 /* NOTE: We will leak here since the monitor and the registrar will never
diff --git a/libmailutils/base/rfc2047.c b/libmailutils/base/rfc2047.c
index 32b0923..29b3c4c 100644
--- a/libmailutils/base/rfc2047.c
+++ b/libmailutils/base/rfc2047.c
@@ -28,7 +28,7 @@
 #include <mailutils/stream.h>
 #include <mailutils/filter.h>
 #include <mailutils/errno.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 
 static int
 realloc_buffer (char **bufp, size_t *bufsizep, size_t incr)
diff --git a/libmailutils/diag/dbgsyslog.c b/libmailutils/base/schemeauto.c
similarity index 68%
copy from libmailutils/diag/dbgsyslog.c
copy to libmailutils/base/schemeauto.c
index af6f2fe..c82faae 100644
--- a/libmailutils/diag/dbgsyslog.c
+++ b/libmailutils/base/schemeauto.c
@@ -1,5 +1,6 @@
+/* Returns true if SCHEME represents a local (autodetect) mail folder.  */
 /* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 1999, 2000, 2001, 2004, 2005, 2007, 2010 Free Software
+   Copyright (C) 1999, 2000, 2001, 2007, 2008, 2009, 2010 Free Software
    Foundation, Inc.
 
    This library is free software; you can redistribute it and/or
@@ -20,14 +21,17 @@
 # include <config.h>
 #endif
 
-#include <mailutils/debug.h>
-#include <syslog.h>
+#include <mailutils/url.h>
 
+/* Returns true if SCHEME represents a local (autodetect) mail folder.  */
 int
-mu_debug_syslog_printer (void *unused, mu_log_level_t level, const char *str)
+mu_scheme_autodetect_p (mu_url_t url)
 {
-  syslog ((level == MU_DEBUG_ERROR) ? LOG_ERR : LOG_DEBUG, "%s", str);
+  if (mu_url_is_scheme (url, "file"))
+    {
+      mu_url_expand_path (url);
+      return 1;
+    }
   return 0;
 }
-
-
+    
diff --git a/libmailutils/base/spawnvp.c b/libmailutils/base/spawnvp.c
new file mode 100644
index 0000000..25d7059
--- /dev/null
+++ b/libmailutils/base/spawnvp.c
@@ -0,0 +1,120 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 2002, 2003, 2005, 2006, 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, see 
+   <http://www.gnu.org/licenses/>. */
+
+#if HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <signal.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+
+/* See Advanced Programming in the UNIX Environment, Stevens,
+ * program  10.20 for the rational for the signal handling. I
+ * had to look it up, so if somebody else is curious, thats where
+ * to find it.
+ */
+int 
+mu_spawnvp (const char *prog, char *av[], int *stat)
+{
+  pid_t pid;
+  int err = 0;
+  int progstat;
+  struct sigaction ignore;
+  struct sigaction saveintr;
+  struct sigaction savequit;
+  sigset_t chldmask;
+  sigset_t savemask;
+
+  if (!prog || !av)
+    return EINVAL;
+
+  ignore.sa_handler = SIG_IGN;	/* ignore SIGINT and SIGQUIT */
+  ignore.sa_flags = 0;
+  sigemptyset (&ignore.sa_mask);
+
+  if (sigaction (SIGINT, &ignore, &saveintr) < 0)
+    return errno;
+  if (sigaction (SIGQUIT, &ignore, &savequit) < 0)
+    {
+      sigaction (SIGINT, &saveintr, NULL);
+      return errno;
+    }
+
+  sigemptyset (&chldmask);	/* now block SIGCHLD */
+  sigaddset (&chldmask, SIGCHLD);
+
+  if (sigprocmask (SIG_BLOCK, &chldmask, &savemask) < 0)
+    {
+      sigaction (SIGINT, &saveintr, NULL);
+      sigaction (SIGQUIT, &savequit, NULL);
+      return errno;
+    }
+
+#ifdef HAVE_VFORK
+  pid = vfork ();
+#else
+  pid = fork ();
+#endif
+
+  if (pid < 0)
+    {
+      err = errno;
+    }
+  else if (pid == 0)
+    {				/* child */
+      /* restore previous signal actions & reset signal mask */
+      sigaction (SIGINT, &saveintr, NULL);
+      sigaction (SIGQUIT, &savequit, NULL);
+      sigprocmask (SIG_SETMASK, &savemask, NULL);
+
+      execvp (prog, av);
+#ifdef HAVE__EXIT      
+      _exit (127);		/* exec error */
+#else
+      exit (127);
+#endif
+    }
+  else
+    {				/* parent */
+      while (waitpid (pid, &progstat, 0) < 0)
+	if (errno != EINTR)
+	  {
+	    err = errno;	/* error other than EINTR from waitpid() */
+	    break;
+	  }
+      if (err == 0 && stat)
+	*stat = progstat;
+    }
+
+  /* restore previous signal actions & reset signal mask */
+  /* preserve first error number, but still try and reset the signals */
+  if (sigaction (SIGINT, &saveintr, NULL) < 0)
+    err = err ? err : errno;
+  if (sigaction (SIGQUIT, &savequit, NULL) < 0)
+    err = err ? err : errno;
+  if (sigprocmask (SIG_SETMASK, &savemask, NULL) < 0)
+    err = err ? err : errno;
+
+  return err;
+}
+
diff --git a/libmailutils/base/symlink.c b/libmailutils/base/symlink.c
new file mode 100644
index 0000000..7551a46
--- /dev/null
+++ b/libmailutils/base/symlink.c
@@ -0,0 +1,103 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 2002, 2003, 2005, 2006, 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, see 
+   <http://www.gnu.org/licenses/>. */
+
+#if HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/param.h>
+#include <sys/stat.h>
+#include <mailutils/types.h>
+#include <mailutils/util.h>
+
+/* The result of readlink() may be a path relative to that link, 
+ * qualify it if necessary.
+ */
+static void
+mu_qualify_link (const char *path, const char *link, char *qualified)
+{
+  const char *lb = NULL;
+  size_t len;
+
+  /* link is full path */
+  if (*link == '/')
+    {
+      mu_cpystr (qualified, link, _POSIX_PATH_MAX);
+      return;
+    }
+
+  if ((lb = strrchr (path, '/')) == NULL)
+    {
+      /* no path in link */
+      mu_cpystr (qualified, link, _POSIX_PATH_MAX);
+      return;
+    }
+
+  len = lb - path + 1;
+  memcpy (qualified, path, len);
+  mu_cpystr (qualified + len, link, _POSIX_PATH_MAX - len);
+}
+
+#ifndef _POSIX_SYMLOOP_MAX
+# define _POSIX_SYMLOOP_MAX 255
+#endif
+
+int
+mu_unroll_symlink (char *out, size_t outsz, const char *in)
+{
+  char path[_POSIX_PATH_MAX];
+  int symloops = 0;
+
+  while (symloops++ < _POSIX_SYMLOOP_MAX)
+    {
+      struct stat s;
+      char link[_POSIX_PATH_MAX];
+      char qualified[_POSIX_PATH_MAX];
+      int len;
+
+      if (lstat (in, &s) == -1)
+	return errno;
+
+      if (!S_ISLNK (s.st_mode))
+	{
+	  mu_cpystr (path, in, sizeof (path));
+	  break;
+	}
+
+      if ((len = readlink (in, link, sizeof (link))) == -1)
+	return errno;
+
+      link[(len >= sizeof (link)) ? (sizeof (link) - 1) : len] = '\0';
+
+      mu_qualify_link (in, link, qualified);
+
+      mu_cpystr (path, qualified, sizeof (path));
+
+      in = path;
+    }
+
+  mu_cpystr (out, path, outsz);
+
+  return 0;
+}
+
diff --git a/libmailutils/base/tempfile.c b/libmailutils/base/tempfile.c
new file mode 100644
index 0000000..cbd6d96
--- /dev/null
+++ b/libmailutils/base/tempfile.c
@@ -0,0 +1,104 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 2002, 2003, 2005, 2006, 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, see 
+   <http://www.gnu.org/licenses/>. */
+
+#if HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <errno.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <mailutils/nls.h>
+#include <mailutils/io.h>
+#include <mailutils/error.h>
+#include <mailutils/errno.h>
+
+/* Create and open a temporary file. Be very careful about it, since we
+   may be running with extra privilege i.e setgid().
+   Returns file descriptor of the open file.
+   If namep is not NULL, the pointer to the malloced file name will
+   be stored there. Otherwise, the file is unlinked right after open,
+   i.e. it will disappear after close(fd). */
+
+#ifndef P_tmpdir
+# define P_tmpdir "/tmp"
+#endif
+
+int
+mu_tempfile (const char *tmpdir, char **namep)
+{
+  char *filename;
+  int fd;
+  
+  if (!tmpdir)
+    tmpdir = (getenv ("TMPDIR")) ? getenv ("TMPDIR") : P_tmpdir;
+
+  if (mu_asprintf (&filename, "%s/muXXXXXX", tmpdir))
+    return -1;
+
+#ifdef HAVE_MKSTEMP
+  {
+    int save_mask = umask (077);
+    fd = mkstemp (filename);
+    umask (save_mask);
+  }
+#else
+  if (mktemp (filename))
+    fd = open (filename, O_CREAT|O_EXCL|O_RDWR, 0600);
+  else
+    fd = -1;
+#endif
+
+  if (fd == -1)
+    {
+      mu_error (_("cannot open temporary file: %s"), mu_strerror (errno));
+      free (filename);
+      return -1;
+    }
+
+  if (namep)
+    *namep = filename;
+  else
+    {
+      unlink (filename);
+      free (filename);
+    }
+
+  return fd;
+}
+
+/* Create a unique temporary file name in tmpdir. The function
+   creates an empty file with this name to avoid possible race
+   conditions. Returns a pointer to the malloc'ed file name.
+   If tmpdir is NULL, the value of the environment variable
+   TMPDIR or the hardcoded P_tmpdir is used, whichever is defined. */
+/* FIXME: Remove it. Use mu_tempfile instead. */
+char *
+mu_tempname (const char *tmpdir)
+{
+  char *filename = NULL;
+  int fd = mu_tempfile (tmpdir, &filename);
+  close (fd);
+  return filename;
+}
+
+
diff --git a/libmailutils/base/ticket.c b/libmailutils/base/ticket.c
index 4e12b56..1e26c26 100644
--- a/libmailutils/base/ticket.c
+++ b/libmailutils/base/ticket.c
@@ -28,7 +28,7 @@
 #include <stdio.h>
 #include <termios.h>
 
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 #include <mailutils/errno.h>
 #include <mailutils/secret.h>
 #include <mailutils/sys/auth.h>
diff --git a/libmailutils/base/tilde.c b/libmailutils/base/tilde.c
new file mode 100644
index 0000000..87576b7
--- /dev/null
+++ b/libmailutils/base/tilde.c
@@ -0,0 +1,119 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 2002, 2003, 2005, 2006, 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, see 
+   <http://www.gnu.org/licenses/>. */
+
+#if HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#include <mailutils/types.h>
+#include <mailutils/cctype.h>
+#include <mailutils/util.h>
+#include <mailutils/mu_auth.h>
+
+/* NOTE: Allocates Memory.  */
+/* Expand: ~ --> /home/user and to ~guest --> /home/guest.  */
+char *
+mu_tilde_expansion (const char *ref, const char *delim, const char *homedir)
+{
+  char *base = strdup (ref);
+  char *home = NULL;
+  char *proto = NULL;
+  size_t proto_len = 0;
+  char *p;
+
+  for (p = base; *p && mu_isascii (*p) && mu_isalnum (*p); p++)
+    ;
+  
+  if (*p == ':')
+    {
+      p++;
+      proto_len = p - base;
+      proto = malloc (proto_len + 1);
+      if (!proto)
+	return NULL;
+      memcpy (proto, base, proto_len);
+      proto[proto_len] = 0;
+      /* Allow for extra pair of slashes after the protocol specifier */
+      if (*p == delim[0])
+	p++;
+      if (*p == delim[0])
+	p++;
+    }
+  else
+    p = base;
+  
+  if (*p == '~')
+    {
+      p++;
+      if (*p == delim[0] || *p == '\0')
+        {
+	  char *s;
+	  if (!homedir)
+	    {
+	      home = mu_get_homedir ();
+	      if (!home)
+		return base;
+	      homedir = home;
+	    }
+	  s = calloc (proto_len + strlen (homedir) + strlen (p) + 1, 1);
+	  if (proto_len)
+	    strcpy (s, proto);
+	  else
+	    s[0] = 0;
+          strcat (s, homedir);
+          strcat (s, p);
+          free (base);
+          base = s;
+        }
+      else
+        {
+          struct mu_auth_data *auth;
+          char *s = p;
+          char *name;
+          while (*s && *s != delim[0])
+            s++;
+          name = calloc (s - p + 1, 1);
+          memcpy (name, p, s - p);
+          name[s - p] = '\0';
+	  
+          auth = mu_get_auth_by_name (name);
+          free (name);
+          if (auth)
+            {
+              char *buf = calloc (proto_len + strlen (auth->dir)
+				  + strlen (s) + 1, 1);
+	      if (proto_len)
+		strcpy (buf, proto);
+	      else
+		buf[0] = 0;
+	      strcat (buf, auth->dir);
+              strcat (buf, s);
+              free (base);
+              base = buf;
+	      mu_auth_data_free (auth);
+            }
+        }
+    }
+  if (home)
+    free (home);
+  return base;
+}
+
diff --git a/libmailutils/base/url.c b/libmailutils/base/url.c
index 554eb8a..d177fa7 100644
--- a/libmailutils/base/url.c
+++ b/libmailutils/base/url.c
@@ -28,7 +28,7 @@
 # include <strings.h>
 #endif
 
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 #include <mailutils/errno.h>
 #include <mailutils/argcv.h>
 #include <mailutils/secret.h>
diff --git a/libmailutils/base/usremail.c b/libmailutils/base/usremail.c
new file mode 100644
index 0000000..3af6062
--- /dev/null
+++ b/libmailutils/base/usremail.c
@@ -0,0 +1,196 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 2002, 2003, 2005, 2006, 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, see 
+   <http://www.gnu.org/licenses/>. */
+
+#if HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <stdio.h>
+#include <mailutils/types.h>
+#include <mailutils/address.h>
+#include <mailutils/mu_auth.h>
+#include <mailutils/util.h>
+#include <mailutils/parse822.h>
+
+/*
+ * Functions used to convert unix mailbox/user names into RFC822 addr-specs.
+ */
+
+static char *mu_user_email = 0;
+
+int
+mu_set_user_email (const char *candidate)
+{
+  int err = 0;
+  mu_address_t addr = NULL;
+  size_t emailno = 0;
+  char *email = NULL;
+  const char *domain = NULL;
+  
+  if ((err = mu_address_create (&addr, candidate)) != 0)
+    return err;
+
+  if ((err = mu_address_get_email_count (addr, &emailno)) != 0)
+    goto cleanup;
+
+  if (emailno != 1)
+    {
+      errno = EINVAL;
+      goto cleanup;
+    }
+
+  if ((err = mu_address_aget_email (addr, 1, &email)) != 0)
+    goto cleanup;
+
+  if (mu_user_email)
+    free (mu_user_email);
+
+  mu_user_email = email;
+
+  if ((err = mu_address_sget_domain (addr, 1, &domain)) == 0)
+    mu_set_user_email_domain (domain);
+  
+cleanup:
+  mu_address_destroy (&addr);
+
+  return err;
+}
+
+static char *mu_user_email_domain = 0;
+
+int
+mu_set_user_email_domain (const char *domain)
+{
+  char *d = NULL;
+  
+  if (!domain)
+    return EINVAL;
+  
+  d = strdup (domain);
+
+  if (!d)
+    return ENOMEM;
+
+  if (mu_user_email_domain)
+    free (mu_user_email_domain);
+
+  mu_user_email_domain = d;
+
+  return 0;
+}
+
+/* FIXME: must be called _sget_ */
+int
+mu_get_user_email_domain (const char **domain)
+{
+  int err = 0;
+
+  if (!mu_user_email_domain)
+    {
+      if ((err = mu_get_host_name (&mu_user_email_domain)))
+	return err;
+    }
+
+  *domain = mu_user_email_domain;
+
+  return 0;
+}
+
+int
+mu_aget_user_email_domain (char **pdomain)
+{
+  const char *domain;
+  int status = mu_get_user_email_domain (&domain);
+  if (status)
+    return status;
+  if (domain == NULL)
+    *pdomain = NULL;
+  else
+    {
+      *pdomain = strdup (domain);
+      if (*pdomain == NULL)
+	return ENOMEM;
+    }
+  return 0;
+}
+
+/* Note: allocates memory */
+char *
+mu_get_user_email (const char *name)
+{
+  int status = 0;
+  char *localpart = NULL;
+  const char *domainpart = NULL;
+  char *email = NULL;
+  char *tmpname = NULL;
+
+  if (!name && mu_user_email)
+    {
+      email = strdup (mu_user_email);
+      if (!email)
+	errno = ENOMEM;
+      return email;
+    }
+
+  if (!name)
+    {
+      struct mu_auth_data *auth = mu_get_auth_by_uid (getuid ());
+      if (!auth)
+	{
+	  errno = EINVAL;
+	  return NULL;
+	}
+      name = tmpname = strdup(auth->name);
+      if (auth)
+	mu_auth_data_free (auth);
+    }
+
+  status = mu_get_user_email_domain (&domainpart);
+
+  if (status)
+    {
+      free(tmpname);
+      errno = status;
+      return NULL;
+    }
+
+  if ((status = mu_parse822_quote_local_part (&localpart, name)))
+    {
+      free(tmpname);
+      errno = status;
+      return NULL;
+    }
+
+
+  email = malloc (strlen (localpart) + 1
+		  + strlen (domainpart) + 1);
+  if (!email)
+    errno = ENOMEM;
+  else
+    sprintf (email, "%s@%s", localpart, domainpart);
+
+  free(tmpname);
+  free (localpart);
+
+  return email;
+}
diff --git a/libmailutils/base/wicket.c b/libmailutils/base/wicket.c
index 1e6a657..705e69a 100644
--- a/libmailutils/base/wicket.c
+++ b/libmailutils/base/wicket.c
@@ -31,7 +31,7 @@
 #include <ctype.h>
 
 #include <mailutils/errno.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 #include <mailutils/mu_auth.h>
 #include <mailutils/stream.h>
 
diff --git a/libmailutils/cfg/driver.c b/libmailutils/cfg/driver.c
index 33683d4..de34729 100644
--- a/libmailutils/cfg/driver.c
+++ b/libmailutils/cfg/driver.c
@@ -31,7 +31,7 @@
 #include <mailutils/cfg.h>
 #include <mailutils/errno.h>
 #include <mailutils/error.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 #include <mailutils/monitor.h>
 #include <mailutils/refcount.h>
 #include <mailutils/list.h>
diff --git a/libmailutils/cfg/gocs.c b/libmailutils/cfg/gocs.c
index e6b14b4..7976aa2 100644
--- a/libmailutils/cfg/gocs.c
+++ b/libmailutils/cfg/gocs.c
@@ -24,7 +24,7 @@
 #include <mailutils/gocs.h>
 #include <mailutils/mailbox.h>
 #include <mailutils/locker.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 #include <mailutils/mailer.h>
 #include <mailutils/error.h>
 #include <mailutils/errno.h>
diff --git a/libmailutils/cfg/lexer.l b/libmailutils/cfg/lexer.l
index 83e8351..67c1199 100644
--- a/libmailutils/cfg/lexer.l
+++ b/libmailutils/cfg/lexer.l
@@ -37,7 +37,7 @@
 #include <mailutils/nls.h>
 #include <mailutils/cfg.h>
 #include <mailutils/list.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
   
 #include "parser.h"
 
diff --git a/libmailutils/cfg/parser.y b/libmailutils/cfg/parser.y
index 8c8e060..9fe5b1b 100644
--- a/libmailutils/cfg/parser.y
+++ b/libmailutils/cfg/parser.y
@@ -35,7 +35,7 @@
 #include <mailutils/list.h>
 #include <mailutils/iterator.h>
 #include <mailutils/debug.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 #include <mailutils/cctype.h>
 
 int mu_cfg_parser_verbose;
diff --git a/libmailutils/filter/Makefile.am b/libmailutils/filter/Makefile.am
index 08082bb..3d459c6 100644
--- a/libmailutils/filter/Makefile.am
+++ b/libmailutils/filter/Makefile.am
@@ -22,6 +22,7 @@ libfilter_la_SOURCES =\
  binflt.c\
  crlfdot.c\
  crlfflt.c\
+ decode.c\
  dot.c\
  filter.c\
  filter_iconv.c\
diff --git a/libmailutils/filter/decode.c b/libmailutils/filter/decode.c
new file mode 100644
index 0000000..56e7a59
--- /dev/null
+++ b/libmailutils/filter/decode.c
@@ -0,0 +1,73 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 2002, 2003, 2005, 2006, 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, see 
+   <http://www.gnu.org/licenses/>. */
+
+#if HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <unistd.h>
+#include <string.h>
+#include <errno.h>
+#include <mailutils/stream.h>
+#include <mailutils/filter.h>
+#include <mailutils/cstr.h>
+
+enum mu_iconv_fallback_mode mu_default_fallback_mode = mu_fallback_copy_octal;
+
+int
+mu_set_default_fallback (const char *str)
+{
+  if (strcmp (str, "none") == 0)
+    mu_default_fallback_mode = mu_fallback_none;
+  else if (strcmp (str, "copy-pass") == 0)
+    mu_default_fallback_mode = mu_fallback_copy_pass;
+  else if (strcmp (str, "copy-octal") == 0)
+    mu_default_fallback_mode = mu_fallback_copy_octal;
+  else
+    return EINVAL;
+  return 0;
+}
+
+int
+mu_decode_filter (mu_stream_t *pfilter, mu_stream_t input,
+		  const char *filter_type,
+		  const char *fromcode, const char *tocode)
+{
+  mu_stream_t filter;
+  
+  int status = mu_filter_create (&filter, input, filter_type,
+				 MU_FILTER_DECODE, MU_STREAM_READ);
+  if (status)
+    return status;
+
+  if (fromcode && tocode && mu_c_strcasecmp (fromcode, tocode))
+    {
+      mu_stream_t cvt;
+
+      status = mu_filter_iconv_create (&cvt, filter, fromcode, tocode,
+				       0, mu_default_fallback_mode);
+      if (status == 0)
+	{
+          mu_stream_unref (filter);
+          filter = cvt;
+	}
+    }
+  *pfilter = filter;
+  return 0;
+}
+
diff --git a/libmailutils/mailbox/attribute.c b/libmailutils/mailbox/attribute.c
index 37e479a..c4e5808 100644
--- a/libmailutils/mailbox/attribute.c
+++ b/libmailutils/mailbox/attribute.c
@@ -30,7 +30,7 @@
 #endif
 
 #include <mailutils/errno.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 #include <mailutils/cstr.h>
 #include <mailutils/sys/attribute.h>
 
diff --git a/libmailutils/mailbox/body.c b/libmailutils/mailbox/body.c
index b4cf22b..e2c2bcd 100644
--- a/libmailutils/mailbox/body.c
+++ b/libmailutils/mailbox/body.c
@@ -30,7 +30,7 @@
 #include <fcntl.h>
 
 #include <mailutils/stream.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 #include <mailutils/errno.h>
 #include <mailutils/sys/stream.h>
 #include <mailutils/sys/body.h>
diff --git a/libmailutils/mailbox/envelope.c b/libmailutils/mailbox/envelope.c
index 9546885..d50e4df 100644
--- a/libmailutils/mailbox/envelope.c
+++ b/libmailutils/mailbox/envelope.c
@@ -23,7 +23,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <mailutils/errno.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 #include <mailutils/sys/envelope.h>
 
 int
diff --git a/libmailutils/mailbox/header.c b/libmailutils/mailbox/header.c
index 6de32d2..6ce70d1 100644
--- a/libmailutils/mailbox/header.c
+++ b/libmailutils/mailbox/header.c
@@ -42,7 +42,7 @@
 
 #include <mailutils/stream.h>
 #include <mailutils/address.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 #include <mailutils/errno.h>
 #include <mailutils/cstr.h>
 #include <mailutils/sys/header_stream.h>
diff --git a/libmailutils/mailbox/mailbox.c b/libmailutils/mailbox/mailbox.c
index fcf2243..d95ce2d 100644
--- a/libmailutils/mailbox/mailbox.c
+++ b/libmailutils/mailbox/mailbox.c
@@ -38,7 +38,7 @@
 #include <mailutils/url.h>
 #include <mailutils/attribute.h>
 #include <mailutils/message.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 
 #include <mailutils/sys/mailbox.h>
 #include <mailutils/sys/url.h>
diff --git a/libmailutils/mailbox/mbx_default.c b/libmailutils/mailbox/mbx_default.c
index 9069c6f..468230a 100644
--- a/libmailutils/mailbox/mbx_default.c
+++ b/libmailutils/mailbox/mbx_default.c
@@ -30,7 +30,7 @@
 #include <confpaths.h>
 
 #include <mailutils/mailbox.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 #include <mailutils/debug.h>
 #include <mailutils/error.h>
 #include <mailutils/errno.h>
diff --git a/libmailutils/mailbox/message.c b/libmailutils/mailbox/message.c
index b6f81b7..f415583 100644
--- a/libmailutils/mailbox/message.c
+++ b/libmailutils/mailbox/message.c
@@ -41,7 +41,7 @@
 #include <mailutils/folder.h>
 #include <mailutils/header.h>
 #include <mailutils/mailbox.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 #include <mailutils/observer.h>
 #include <mailutils/stream.h>
 #include <mailutils/mu_auth.h>
diff --git a/libmailutils/mailer/mailer.c b/libmailutils/mailer/mailer.c
index 35c9862..6c0ce9a 100644
--- a/libmailutils/mailer/mailer.c
+++ b/libmailutils/mailer/mailer.c
@@ -45,7 +45,7 @@
 #include <mailutils/mailbox.h>
 #include <mailutils/message.h>
 #include <mailutils/argcv.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 #include <mailutils/mime.h>
 #include <mailutils/io.h>
 
diff --git a/libmailutils/mailer/progmailer.c b/libmailutils/mailer/progmailer.c
index 6411cf0..a433106 100644
--- a/libmailutils/mailer/progmailer.c
+++ b/libmailutils/mailer/progmailer.c
@@ -39,7 +39,7 @@
 #include <mailutils/body.h>
 #include <mailutils/message.h>
 #include <mailutils/progmailer.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 #include <mailutils/cstr.h>
 
 struct _mu_progmailer
diff --git a/libmailutils/mime/attachment.c b/libmailutils/mime/attachment.c
index e877cb7..e4a09e3 100644
--- a/libmailutils/mime/attachment.c
+++ b/libmailutils/mime/attachment.c
@@ -42,7 +42,7 @@
 #include <mailutils/message.h>
 #include <mailutils/stream.h>
 #include <mailutils/errno.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 
 #define BUF_SIZE	2048
 
diff --git a/libmailutils/mime/mime.c b/libmailutils/mime/mime.c
index 17c5441..165d337 100644
--- a/libmailutils/mime/mime.c
+++ b/libmailutils/mime/mime.c
@@ -37,7 +37,7 @@
 #include <mailutils/body.h>
 #include <mailutils/header.h>
 #include <mailutils/errno.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 
 #include <mailutils/sys/mime.h>
 #include <mailutils/sys/stream.h>
diff --git a/libmailutils/mime/mimehdr.c b/libmailutils/mime/mimehdr.c
index dd5eb34..7bc801b 100644
--- a/libmailutils/mime/mimehdr.c
+++ b/libmailutils/mime/mimehdr.c
@@ -33,7 +33,7 @@
 			      be renamed! */
 #include <mailutils/mime.h>
 #include <mailutils/filter.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 
 /* See RFC 2045, 5.1.  Syntax of the Content-Type Header Field */
 #define _ISSPECIAL(c) !!strchr ("()<>@,;:\\\"/[]?=", c)
diff --git a/libmailutils/stream/file_stream.c b/libmailutils/stream/file_stream.c
index e6a6e67..6f9723a 100644
--- a/libmailutils/stream/file_stream.c
+++ b/libmailutils/stream/file_stream.c
@@ -35,7 +35,7 @@
 #include <mailutils/stream.h>
 #include <mailutils/sys/stream.h>
 #include <mailutils/sys/file_stream.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 
 #define MU_FSTR_ERROR_NOREG (MU_ERR_LAST+1)
 
diff --git a/libmailutils/stream/memory_stream.c b/libmailutils/stream/memory_stream.c
index 67952bc..ed599dc 100644
--- a/libmailutils/stream/memory_stream.c
+++ b/libmailutils/stream/memory_stream.c
@@ -30,7 +30,7 @@
 #include <mailutils/nls.h>
 #include <mailutils/stream.h>
 #include <mailutils/sys/memory_stream.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 
 static void
 _memory_done (mu_stream_t stream)
diff --git a/libmailutils/stream/message_stream.c b/libmailutils/stream/message_stream.c
index 91bef83..a404ccd 100644
--- a/libmailutils/stream/message_stream.c
+++ b/libmailutils/stream/message_stream.c
@@ -35,7 +35,7 @@
 #include <mailutils/header.h>
 #include <mailutils/body.h>
 #include <mailutils/stream.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 #include <mailutils/errno.h>
 #include <mailutils/error.h>
 #include <mailutils/cctype.h>
diff --git a/libmailutils/stream/prog_stream.c b/libmailutils/stream/prog_stream.c
index b789def..437bf01 100644
--- a/libmailutils/stream/prog_stream.c
+++ b/libmailutils/stream/prog_stream.c
@@ -35,7 +35,7 @@
 #include <mailutils/stream.h>
 #include <mailutils/sys/stream.h>
 #include <mailutils/sys/prog_stream.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 
 static mu_list_t prog_stream_list;
 
diff --git a/libmailutils/stream/tcp.c b/libmailutils/stream/tcp.c
index eafd40b..b53511a 100644
--- a/libmailutils/stream/tcp.c
+++ b/libmailutils/stream/tcp.c
@@ -37,7 +37,7 @@
 
 #include <mailutils/errno.h>
 #include <mailutils/stream.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 
 #include <mailutils/sys/stream.h>
 
diff --git a/libmailutils/stream/temp_file_stream.c b/libmailutils/stream/temp_file_stream.c
index fb178a5..29ca359 100644
--- a/libmailutils/stream/temp_file_stream.c
+++ b/libmailutils/stream/temp_file_stream.c
@@ -30,7 +30,7 @@
 #include <mailutils/stream.h>
 #include <mailutils/sys/stream.h>
 #include <mailutils/sys/file_stream.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 
 
 static int
diff --git a/libmailutils/string/Makefile.am b/libmailutils/string/Makefile.am
index 52452b9..3f11ade 100644
--- a/libmailutils/string/Makefile.am
+++ b/libmailutils/string/Makefile.am
@@ -18,13 +18,18 @@
 noinst_LTLIBRARIES = libstring.la
 
 libstring_la_SOURCES = \
+ cpystr.c\
  cstrcasecmp.c\
+ cstrcasestr.c\
  cstrlower.c\
  cstrupper.c\
+ hexstr.c\
  strltrim.c\
  strskip.c\
  stripws.c\
  strrtrim.c\
+ trueans.c\
+ unfold.c\
  asnprintf.c\
  asprintf.c\
  muctype.c\
diff --git a/libmailutils/diag/dbgsyslog.c b/libmailutils/string/cpystr.c
similarity index 60%
copy from libmailutils/diag/dbgsyslog.c
copy to libmailutils/string/cpystr.c
index af6f2fe..9671c70 100644
--- a/libmailutils/diag/dbgsyslog.c
+++ b/libmailutils/string/cpystr.c
@@ -1,6 +1,6 @@
 /* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 1999, 2000, 2001, 2004, 2005, 2007, 2010 Free Software
-   Foundation, Inc.
+   Copyright (C) 1999, 2000, 2001, 2002, 2003, 2005, 2006, 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
@@ -16,18 +16,25 @@
    Public License along with this library.  If not, see 
    <http://www.gnu.org/licenses/>. */
 
-#ifdef HAVE_CONFIG_H
+#if HAVE_CONFIG_H
 # include <config.h>
 #endif
 
-#include <mailutils/debug.h>
-#include <syslog.h>
+#include <string.h>
+#include <mailutils/util.h>
 
-int
-mu_debug_syslog_printer (void *unused, mu_log_level_t level, const char *str)
+/* Smart strncpy that always add the null and returns the number of bytes
+   written.  */
+size_t
+mu_cpystr (char *dst, const char *src, size_t size)
 {
-  syslog ((level == MU_DEBUG_ERROR) ? LOG_ERR : LOG_DEBUG, "%s", str);
-  return 0;
+  size_t len = src ? strlen (src) : 0 ;
+  if (dst == NULL || size == 0)
+    return len;
+  if (len >= size)
+    len = size - 1;
+  memcpy (dst, src, len);
+  dst[len] = '\0';
+  return len;
 }
 
-
diff --git a/libmailutils/string/cstrcasestr.c b/libmailutils/string/cstrcasestr.c
new file mode 100644
index 0000000..7f20e08
--- /dev/null
+++ b/libmailutils/string/cstrcasestr.c
@@ -0,0 +1,101 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 2002, 2003, 2005, 2006, 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, see 
+   <http://www.gnu.org/licenses/>. */
+
+#if HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <unistd.h>
+#include <mailutils/cctype.h>
+
+/* Based on strstr from GNU libc (Stephen R. van den Berg,
+   berg@pool.informatik.rwth-aachen.de) */
+char *
+mu_c_strcasestr (const char *a_haystack, const char *a_needle)
+{
+  register const unsigned char *haystack = (unsigned char*) a_haystack,
+    *needle = (unsigned char*) a_needle;
+  register unsigned int b, c;
+
+#define U(c) mu_toupper (c)
+  if ((b = U (*needle)))
+    {
+      haystack--;		
+      do
+	{
+	  if (!(c = *++haystack))
+	    goto ret0;
+	}
+      while (U (c) != b);
+
+      if (!(c = *++needle))
+	goto foundneedle;
+
+      c = U (c);
+      ++needle;
+      goto jin;
+
+      for (;;)
+        { 
+          register unsigned int a;
+	  register const unsigned char *rhaystack, *rneedle;
+
+	  do
+	    {
+	      if (!(a = *++haystack))
+		goto ret0;
+	      if (U (a) == b)
+		break;
+	      if (!(a = *++haystack))
+		goto ret0;
+shloop:       ;
+            }
+          while (U (a) != b);
+	  
+jin:     if (!(a = *++haystack))
+	    goto ret0;
+
+	  if (U (a) != c)
+	    goto shloop;
+
+	  if (U (*(rhaystack = haystack-- + 1)) ==
+	      (a = U (*(rneedle = needle))))
+	    do
+	      {
+		if (!a)
+		  goto foundneedle;
+		if (U (*++rhaystack) != (a = U (*++needle)))
+		  break;
+		if (!a)
+		  goto foundneedle;
+	      }
+	    while (U (*++rhaystack) == (a = U (*++needle)));
+
+	  needle = rneedle;
+
+	  if (!a)
+	    break;
+        }
+    }
+foundneedle:
+  return (char*)haystack;
+ret0:
+  return NULL;
+
+#undef U
+}  
diff --git a/libmailutils/diag/dbgsyslog.c b/libmailutils/string/hexstr.c
similarity index 52%
copy from libmailutils/diag/dbgsyslog.c
copy to libmailutils/string/hexstr.c
index af6f2fe..dd22fb8 100644
--- a/libmailutils/diag/dbgsyslog.c
+++ b/libmailutils/string/hexstr.c
@@ -1,6 +1,6 @@
 /* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 1999, 2000, 2001, 2004, 2005, 2007, 2010 Free Software
-   Foundation, Inc.
+   Copyright (C) 1999, 2000, 2001, 2002, 2003, 2005, 2006, 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
@@ -16,18 +16,44 @@
    Public License along with this library.  If not, see 
    <http://www.gnu.org/licenses/>. */
 
-#ifdef HAVE_CONFIG_H
+#if HAVE_CONFIG_H
 # include <config.h>
 #endif
 
-#include <mailutils/debug.h>
-#include <syslog.h>
+#include <unistd.h>
 
-int
-mu_debug_syslog_printer (void *unused, mu_log_level_t level, const char *str)
+/* convert a sequence of hex characters into an integer */
+
+unsigned long
+mu_hex2ul (char hex)
 {
-  syslog ((level == MU_DEBUG_ERROR) ? LOG_ERR : LOG_DEBUG, "%s", str);
-  return 0;
+  if (hex >= '0' && hex <= '9')
+   return hex - '0';
+
+  if (hex >= 'a' && hex <= 'z')
+    return hex - 'a' + 10;
+
+  if (hex >= 'A' && hex <= 'Z')
+    return hex - 'A' + 10;
+
+  return -1;
 }
 
+size_t
+mu_hexstr2ul (unsigned long *ul, const char *hex, size_t len)
+{
+  size_t r;
 
+  *ul = 0;
+
+  for (r = 0; r < len; r++)
+    {
+      unsigned long v = mu_hex2ul (hex[r]);
+
+      if (v == (unsigned long)-1)
+	return r;
+
+      *ul = *ul * 16 + v;
+    }
+  return r;
+}
diff --git a/libmailutils/string/mkfilename.c b/libmailutils/string/mkfilename.c
index ecd6dee..7bf0665 100644
--- a/libmailutils/string/mkfilename.c
+++ b/libmailutils/string/mkfilename.c
@@ -22,7 +22,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <mailutils/alloc.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 
 char *
 mu_make_file_name (const char *dir, const char *file)
diff --git a/libmailutils/string/trueans.c b/libmailutils/string/trueans.c
new file mode 100644
index 0000000..b57c45c
--- /dev/null
+++ b/libmailutils/string/trueans.c
@@ -0,0 +1,58 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 2002, 2003, 2005, 2006, 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, see 
+   <http://www.gnu.org/licenses/>. */
+
+#if HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <string.h>
+#include <mailutils/cctype.h>
+#include <mailutils/nls.h>
+
+int
+mu_true_answer_p (const char *p)
+{
+  if (!p)
+    return -1;
+
+  while (*p && mu_isspace (*p))
+    p++;
+
+  if (*p)
+    {
+      /* TRANSLATORS: This is a list of characters which start
+	 an affirmative answer. Whenever possible, please preserve
+	 'yY' in your translation, e.g., for Euskara:
+
+	 msgstr "yYbB";
+      */
+      if (strchr (_("yY"), *p))
+	return 1;
+
+      /* TRANSLATORS: This is a list of characters which start
+	 a negative answer. Whenever possible, please preserve
+	 'nN' in your translation, e.g., for Euskara:
+
+	 msgstr "nNeE";
+      */
+      else if (strchr (_("nN"), *p))
+	return 0;
+    }
+  return -1;
+}
+
diff --git a/include/mailutils/cpp/secret.h b/libmailutils/string/unfold.c
similarity index 52%
copy from include/mailutils/cpp/secret.h
copy to libmailutils/string/unfold.c
index 20afdfb..4e37e28 100644
--- a/include/mailutils/cpp/secret.h
+++ b/libmailutils/string/unfold.c
@@ -15,34 +15,57 @@
    Public License along with this library.  If not, see 
    <http://www.gnu.org/licenses/>. */
 
-#ifndef _MUCPP_SECRET_H
-#define _MUCPP_SECRET_H
+#if HAVE_CONFIG_H
+# include <config.h>
+#endif
 
-#include <string>
+#include <unistd.h>
 #include <errno.h>
-#include <mailutils/secret.h>
-#include <mailutils/cpp/error.h>
 
-namespace mailutils
+int
+mu_string_unfold (char *text, size_t *plen)
 {
+  char *p, *q;
+  enum uf_state { uf_init, uf_nl, uf_fold } state = uf_init;
+#define ISSPACE(c) (c == '\r' || c == ' ' || c == '\t')
+  
+  if (!text)
+    return EINVAL;
+  
+  for (p = q = text; *q; q++)
+    {
+      switch (state)
+	{
+	case uf_init:
+	  if (*q == '\n')
+	    state = uf_nl;
+	  else
+	    *p++ = *q;
+	  break;
 
-class Secret
-{
- protected:
-  mu_secret_t secret;
-  bool owner;
-
- public:
-  Secret (const std::string&);
-  Secret (const char* str, size_t len);
-  Secret (const mu_secret_t);
-  ~Secret ();
-
-  std::string password ();
-  void password_unref ();
-};
+	case uf_nl:
+	  if (ISSPACE (*q))
+	    state = uf_fold;
+	  else
+	    {
+	      state = uf_init;
+	      *p++ = *q;
+	    }
+	  break;
 
+	case uf_fold:
+	  if (!ISSPACE (*q))
+	    {
+	      *p++ = ' ';
+	      *p++ = *q;
+	      state = uf_init;
+	    }
+	  break;
+	}
+    }
+  
+  *p++ = 0;
+  if (plen)
+    *plen = p - text;
+  return 0;
 }
-
-#endif // not _MUCPP_SECRET_H
-
diff --git a/libmailutils/tests/addr.c b/libmailutils/tests/addr.c
index 64eb884..4f397df 100644
--- a/libmailutils/tests/addr.c
+++ b/libmailutils/tests/addr.c
@@ -26,7 +26,7 @@
 #include <mailutils/address.h>
 #include <mailutils/errno.h>
 #include <mailutils/kwd.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 #include <mailutils/cstr.h>
 #include <mailutils/cctype.h>
 
diff --git a/libmu_auth/ldap.c b/libmu_auth/ldap.c
index f29472e..250aa40 100644
--- a/libmu_auth/ldap.c
+++ b/libmu_auth/ldap.c
@@ -37,7 +37,7 @@
 #include "mailutils/error.h"
 #include "mailutils/errno.h"
 #include "mailutils/nls.h"
-#include "mailutils/mutil.h"
+#include "mailutils/util.h"
 #include "mailutils/stream.h"
 #include "mailutils/filter.h"
 #include "mailutils/md5.h"
diff --git a/libmu_auth/sql.c b/libmu_auth/sql.c
index e84911a..744701f 100644
--- a/libmu_auth/sql.c
+++ b/libmu_auth/sql.c
@@ -47,7 +47,7 @@
 #include <mailutils/error.h>
 #include <mailutils/errno.h>
 #include <mailutils/nls.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 #include <mailutils/sql.h>
 #include <mailutils/vartab.h>
 #include <mailutils/cstr.h>
diff --git a/libmu_cfg/ldap.c b/libmu_cfg/ldap.c
index 953fa3e..12d239e 100644
--- a/libmu_cfg/ldap.c
+++ b/libmu_cfg/ldap.c
@@ -20,7 +20,7 @@
 #endif
 #include <stdlib.h>
 #include "mailutils/libcfg.h"
-#include "mailutils/mutil.h"
+#include "mailutils/util.h"
 #include "mailutils/ldap.h"
 
 static struct mu_ldap_module_config ldap_settings;
diff --git a/libmu_cfg/sql.c b/libmu_cfg/sql.c
index daf91e9..f29bd75 100644
--- a/libmu_cfg/sql.c
+++ b/libmu_cfg/sql.c
@@ -21,7 +21,7 @@
 #include <stdlib.h>
 #include "mailutils/libcfg.h"
 #include <mailutils/sql.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 
 static struct mu_sql_module_config sql_settings;
 
diff --git a/libmu_cpp/mutil.cc b/libmu_cpp/mutil.cc
index 165cfa4..56f76da 100644
--- a/libmu_cpp/mutil.cc
+++ b/libmu_cpp/mutil.cc
@@ -16,7 +16,7 @@
    <http://www.gnu.org/licenses/>. */
 
 #include <cstdlib>
-#include <mailutils/cpp/mutil.h>
+#include <mailutils/cpp/util.h>
 
 using namespace mailutils;
 
diff --git a/libmu_scm/mu_scm.h b/libmu_scm/mu_scm.h
index 22956cf..0dd49b6 100644
--- a/libmu_scm/mu_scm.h
+++ b/libmu_scm/mu_scm.h
@@ -37,7 +37,7 @@
 #include <mailutils/errno.h>
 #include <mailutils/address.h>
 #include <mailutils/registrar.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 #include <mailutils/stream.h>
 #include <mailutils/debug.h>
 #include <mailutils/attribute.h>
diff --git a/libmu_sieve/comparator.c b/libmu_sieve/comparator.c
index b23ce8a..82aa843 100644
--- a/libmu_sieve/comparator.c
+++ b/libmu_sieve/comparator.c
@@ -420,7 +420,7 @@ i_ascii_casemap_is (const char *pattern, const char *text)
 static int
 i_ascii_casemap_contains (const char *pattern, const char *text)
 {
-  return mu_strcasestr (text, pattern) != NULL;
+  return mu_c_strcasestr (text, pattern) != NULL;
 }
 
 static int
diff --git a/libproto/imap/folder.c b/libproto/imap/folder.c
index 278294c..c95c930 100644
--- a/libproto/imap/folder.c
+++ b/libproto/imap/folder.c
@@ -49,7 +49,7 @@
 #include <mailutils/tls.h>
 #include <mailutils/nls.h>
 #include <mailutils/secret.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 #include <mailutils/cctype.h>
 #include <mailutils/cstr.h>
 
diff --git a/libproto/imap/mbox.c b/libproto/imap/mbox.c
index 00e8859..9869339 100644
--- a/libproto/imap/mbox.c
+++ b/libproto/imap/mbox.c
@@ -40,7 +40,7 @@
 #include <mailutils/errno.h>
 #include <mailutils/header.h>
 #include <mailutils/message.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 #include <mailutils/observer.h>
 #include <mailutils/property.h>
 #include <mailutils/stream.h>
diff --git a/libproto/maildir/folder.c b/libproto/maildir/folder.c
index 19ff4b3..4fef07b 100644
--- a/libproto/maildir/folder.c
+++ b/libproto/maildir/folder.c
@@ -31,7 +31,7 @@
 #include <mailutils/sys/registrar.h>
 
 #include <maildir.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 #include <mailutils/url.h>
 #include <mailutils/sys/amd.h>
 
diff --git a/libproto/maildir/mbox.c b/libproto/maildir/mbox.c
index c182958..02b090e 100644
--- a/libproto/maildir/mbox.c
+++ b/libproto/maildir/mbox.c
@@ -58,7 +58,7 @@
 #include <mailutils/error.h>
 #include <mailutils/header.h>
 #include <mailutils/message.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 #include <mailutils/property.h>
 #include <mailutils/stream.h>
 #include <mailutils/url.h>
diff --git a/libproto/mailer/mbox.c b/libproto/mailer/mbox.c
index 30130a7..635e137 100644
--- a/libproto/mailer/mbox.c
+++ b/libproto/mailer/mbox.c
@@ -28,7 +28,7 @@
 #include <mailutils/property.h>
 #include <mailutils/mailer.h>
 #include <mailutils/url.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 #include <mailutils/sys/mailbox.h>
 #include <mailutils/sys/mailer.h>
 
diff --git a/libproto/mailer/smtp.c b/libproto/mailer/smtp.c
index 4db4398..72e7c7c 100644
--- a/libproto/mailer/smtp.c
+++ b/libproto/mailer/smtp.c
@@ -40,7 +40,7 @@
 #include <mailutils/iterator.h>
 #include <mailutils/message.h>
 #include <mailutils/mime.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 #include <mailutils/observer.h>
 #include <mailutils/property.h>
 #include <mailutils/stream.h>
diff --git a/libproto/mailer/smtp_auth.c b/libproto/mailer/smtp_auth.c
index b31943c..48b5a91 100644
--- a/libproto/mailer/smtp_auth.c
+++ b/libproto/mailer/smtp_auth.c
@@ -25,7 +25,7 @@
 #include <mailutils/smtp.h>
 #include <mailutils/sys/smtp.h>
 #include <mailutils/mailbox.h> /* Strange, mu_ticket_file is declared here */
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 #include <mailutils/auth.h>
 #include <mailutils/url.h>
 
diff --git a/libproto/mailer/smtp_ehlo.c b/libproto/mailer/smtp_ehlo.c
index 1a7caf5..6c978cf 100644
--- a/libproto/mailer/smtp_ehlo.c
+++ b/libproto/mailer/smtp_ehlo.c
@@ -24,7 +24,7 @@
 #include <mailutils/errno.h>
 #include <mailutils/cctype.h>
 #include <mailutils/list.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 #include <mailutils/smtp.h>
 #include <mailutils/sys/smtp.h>
 
diff --git a/libproto/mailer/smtp_gsasl.c b/libproto/mailer/smtp_gsasl.c
index d43631f..b8e2638 100644
--- a/libproto/mailer/smtp_gsasl.c
+++ b/libproto/mailer/smtp_gsasl.c
@@ -26,7 +26,7 @@
 #include <mailutils/errno.h>
 #include <mailutils/cctype.h>
 #include <mailutils/list.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 #include <mailutils/smtp.h>
 #include <mailutils/stream.h>
 #include <mailutils/sys/smtp.h>
diff --git a/libproto/mailer/smtp_mail.c b/libproto/mailer/smtp_mail.c
index c3b02de..e24989a 100644
--- a/libproto/mailer/smtp_mail.c
+++ b/libproto/mailer/smtp_mail.c
@@ -25,7 +25,7 @@
 #include <mailutils/errno.h>
 #include <mailutils/cctype.h>
 #include <mailutils/list.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 #include <mailutils/smtp.h>
 #include <mailutils/stream.h>
 #include <mailutils/sys/smtp.h>
diff --git a/libproto/mailer/smtp_mech.c b/libproto/mailer/smtp_mech.c
index c8707b8..0067842 100644
--- a/libproto/mailer/smtp_mech.c
+++ b/libproto/mailer/smtp_mech.c
@@ -26,7 +26,7 @@
 #include <mailutils/cstr.h>
 #include <mailutils/list.h>
 #include <mailutils/iterator.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 #include <mailutils/smtp.h>
 #include <mailutils/sys/smtp.h>
 
diff --git a/libproto/mailer/smtp_rcpt.c b/libproto/mailer/smtp_rcpt.c
index 30962fe..461275b 100644
--- a/libproto/mailer/smtp_rcpt.c
+++ b/libproto/mailer/smtp_rcpt.c
@@ -24,7 +24,7 @@
 #include <mailutils/errno.h>
 #include <mailutils/cctype.h>
 #include <mailutils/list.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 #include <mailutils/smtp.h>
 #include <mailutils/stream.h>
 #include <mailutils/sys/smtp.h>
diff --git a/libproto/mbox/folder.c b/libproto/mbox/folder.c
index 84633d0..7697825 100644
--- a/libproto/mbox/folder.c
+++ b/libproto/mbox/folder.c
@@ -38,7 +38,7 @@
 #include <mailutils/auth.h>
 #include <mailutils/url.h>
 #include <mailutils/stream.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 #include <mailutils/errno.h>
 #include <mailutils/debug.h>
 
diff --git a/libproto/mbox/mbox0.h b/libproto/mbox/mbox0.h
index 382294f..81012fb 100644
--- a/libproto/mbox/mbox0.h
+++ b/libproto/mbox/mbox0.h
@@ -54,7 +54,7 @@
 #include <mailutils/header.h>
 #include <mailutils/locker.h>
 #include <mailutils/message.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 #include <mailutils/nls.h>
 #include <mailutils/observer.h>
 #include <mailutils/property.h>
diff --git a/libproto/mh/folder.c b/libproto/mh/folder.c
index 074273f..1743ca0 100644
--- a/libproto/mh/folder.c
+++ b/libproto/mh/folder.c
@@ -34,7 +34,7 @@
 #include <mailutils/sys/folder.h>
 #include <mailutils/sys/registrar.h>
 #include <mailutils/sys/amd.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 #include <mailutils/cctype.h>
 
 static int
diff --git a/libproto/mh/mbox.c b/libproto/mh/mbox.c
index bb412b7..c3e550a 100644
--- a/libproto/mh/mbox.c
+++ b/libproto/mh/mbox.c
@@ -56,7 +56,7 @@
 #include <mailutils/header.h>
 #include <mailutils/locker.h>
 #include <mailutils/message.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 #include <mailutils/property.h>
 #include <mailutils/stream.h>
 #include <mailutils/url.h>
diff --git a/libproto/pop/mbox.c b/libproto/pop/mbox.c
index 5bd14ca..2c406de 100644
--- a/libproto/pop/mbox.c
+++ b/libproto/pop/mbox.c
@@ -46,7 +46,7 @@
 #include <mailutils/tls.h>
 #include <mailutils/md5.h>
 #include <mailutils/io.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 #include <mailutils/cstr.h>
 #include <mailutils/cctype.h>
 #include <mailutils/opool.h>
diff --git a/maidag/maidag.h b/maidag/maidag.h
index 424df8b..6c7224d 100644
--- a/maidag/maidag.h
+++ b/maidag/maidag.h
@@ -73,7 +73,7 @@
 #include <mailutils/locker.h>
 #include <mailutils/mailbox.h>
 #include <mailutils/message.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 #include <mailutils/registrar.h>
 #include <mailutils/stream.h>
 #include <mailutils/url.h>
diff --git a/mail/mail.h b/mail/mail.h
index 0efe8d3..cc2de6e 100644
--- a/mail/mail.h
+++ b/mail/mail.h
@@ -70,7 +70,7 @@
 #include <mailutils/mailbox.h>
 #include <mailutils/mailer.h>
 #include <mailutils/message.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 #include <mailutils/registrar.h>
 #include <mailutils/stream.h>
 #include <mailutils/url.h>
diff --git a/mail/util.c b/mail/util.c
index 683fdf9..ae6625f 100644
--- a/mail/util.c
+++ b/mail/util.c
@@ -16,7 +16,7 @@
    along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "mail.h"
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 #include <mailutils/mime.h>
 #include <pwd.h>
 #ifdef HAVE_TERMIOS_H
diff --git a/mh/mh.h b/mh/mh.h
index 3f4566f..4ebc589 100644
--- a/mh/mh.h
+++ b/mh/mh.h
@@ -40,7 +40,7 @@
 #include <mailutils/list.h>
 #include <mailutils/iterator.h>
 #include <mailutils/address.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 #include <mailutils/stream.h>
 #include <mailutils/filter.h>
 #include <mailutils/url.h>
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 30c0f15..f07b4c5 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -125,20 +125,30 @@ libmailutils/cfg/format.c
 libmailutils/cfg/lexer.l
 libmailutils/cfg/parser.y
 libmailutils/cfg/gocs.c
+
 libmailutils/diag/debug.c
 libmailutils/diag/diag.c
 libmailutils/diag/errors
 libmailutils/diag/gdebug.c
+
 libmailutils/stream/file_stream.c
+
 libmailutils/filter/filter_iconv.c
+
 libmailutils/server/ipsrv.c
 libmailutils/server/msrv.c
+
 libmailutils/mailbox/message.c
+
 libmailutils/auth/mu_auth.c
+
 libmailutils/base/mutil.c
 libmailutils/base/parsedate.y
 libmailutils/base/registrar.c
 libmailutils/base/version.c
+libmailutils/base/tempfile.c
+
+libmailutils/string/trueans.c
 
 messages/messages.c
 
diff --git a/pop3d/pop3d.h b/pop3d/pop3d.h
index b93284b..39ac438 100644
--- a/pop3d/pop3d.h
+++ b/pop3d/pop3d.h
@@ -107,7 +107,7 @@ extern int expire_on_exit;
 #include <mailutils/locker.h>
 #include <mailutils/mailbox.h>
 #include <mailutils/message.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 #include <mailutils/mu_auth.h>
 #include <mailutils/nls.h>
 #include <mailutils/registrar.h>
diff --git a/python/libmu_py/libmu_py.h b/python/libmu_py/libmu_py.h
index b637858..3b1d4cf 100644
--- a/python/libmu_py/libmu_py.h
+++ b/python/libmu_py/libmu_py.h
@@ -36,7 +36,7 @@
 #include <mailutils/message.h>
 #include <mailutils/mime.h>
 #include <mailutils/mu_auth.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 #include <mailutils/nls.h>
 #include <mailutils/registrar.h>
 #include <mailutils/tls.h>
diff --git a/readmsg/readmsg.h b/readmsg/readmsg.h
index 9b12c23..e0fde21 100644
--- a/readmsg/readmsg.h
+++ b/readmsg/readmsg.h
@@ -49,7 +49,7 @@
 #include <mailutils/error.h>
 #include <mailutils/envelope.h>
 #include <mailutils/argcv.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 
 int msglist (mu_mailbox_t mbox, int show_all, int argc, char **argv, int **set, int *n);
 
diff --git a/sieve/sieve.c b/sieve/sieve.c
index 91adfbc..93e467a 100644
--- a/sieve/sieve.c
+++ b/sieve/sieve.c
@@ -38,7 +38,7 @@
 #include <mailutils/folder.h>
 #include <mailutils/list.h>
 #include <mailutils/mailbox.h>
-#include <mailutils/mutil.h>
+#include <mailutils/util.h>
 #include <mailutils/registrar.h>
 #include <mailutils/stream.h>
 #include <mailutils/nls.h>


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Sun Oct 10 16:03:18 2010
Received: from mailman by lists.gnu.org with archive (Exim 4.43)
	id 1P5274-0000d0-7w
	for mharc-commit-mailutils@gnu.org; Sun, 10 Oct 2010 16:03:18 -0400
Received: from [140.186.70.92] (port=37538 helo=eggs.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.43) id 1P5270-0000cm-QJ
	for commit-mailutils@gnu.org; Sun, 10 Oct 2010 16:03:16 -0400
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P526y-00062k-Sy
	for commit-mailutils@gnu.org; Sun, 10 Oct 2010 16:03:14 -0400
Received: from colonialone.fsf.org ([140.186.70.51]:56240
	helo=internal.in.savannah.gnu.org)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P526y-00062e-R2
	for commit-mailutils@gnu.org; Sun, 10 Oct 2010 16:03:12 -0400
Received: from [10.1.0.108] (helo=vcs-noshell.in.savannah.gnu.org)
	by internal.in.savannah.gnu.org with esmtp (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P526y-0000cG-Kr
	for commit-mailutils@gnu.org; Sun, 10 Oct 2010 20:03:12 +0000
Received: from polak by vcs-noshell.in.savannah.gnu.org with local (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P526y-0004FE-Gv
	for commit-mailutils@gnu.org; Sun, 10 Oct 2010 20:03:12 +0000
From: "Wojciech Polak" <polak@gnu.org>
To: commit-mailutils@gnu.org
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: 4f9d95ae16167f61c74af38b564b34c2715bba1d
X-Git-Newrev: e3ef44efce74604bf62cb630f172226afa91b76c
Message-Id: <E1P526y-0004FE-Gv@vcs-noshell.in.savannah.gnu.org>
Date: Sun, 10 Oct 2010 20:03:12 +0000
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
Subject: [SCM] GNU Mailutils branch, master,
	updated. release-2.2-144-ge3ef44e
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.5
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=unsubscribe>
List-Archive: <http://lists.gnu.org/archive/html/commit-mailutils>
List-Post: <mailto:commit-mailutils@gnu.org>
List-Help: <mailto:commit-mailutils-request@gnu.org?subject=help>
List-Subscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Sun, 10 Oct 2010 20:03:17 -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=e3ef44efce74604bf62cb630f172226afa91b76c

The branch, master has been updated
       via  e3ef44efce74604bf62cb630f172226afa91b76c (commit)
      from  4f9d95ae16167f61c74af38b564b34c2715bba1d (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 e3ef44efce74604bf62cb630f172226afa91b76c
Author: Wojciech Polak <polak@gnu.org>
Date:   Sun Oct 10 21:56:48 2010 +0200

    Update Python API and the examples.
    
    * python/libmu_py/folder.c (api_folder_set_stream): Remove.
    * python/libmu_py/message.c (api_message_set_stream): Remove.
    * python/libmu_py/stream.c (api_stream_ref, api_stream_unref): Add.
    (api_stream_sequential_readline, api_stream_sequential_write): Remove.

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

Summary of changes:
 examples/python/iconv.py    |    6 +-
 examples/python/mimetest.py |    4 +-
 examples/python/msg-send.py |    2 +-
 python/libmu_py/body.c      |    2 +-
 python/libmu_py/folder.c    |   25 +---------
 python/libmu_py/message.c   |   25 ----------
 python/libmu_py/stream.c    |  110 ++++++++++++++++---------------------------
 python/mailutils/stream.py  |   33 +++++--------
 8 files changed, 60 insertions(+), 147 deletions(-)

diff --git a/examples/python/iconv.py b/examples/python/iconv.py
index d1d77c9..ab91ca8 100644
--- a/examples/python/iconv.py
+++ b/examples/python/iconv.py
@@ -22,19 +22,19 @@ if len (sys.argv) != 3:
     print "usage: %s from-code to-code" % sys.argv[0]
     sys.exit (0)
 
-sti = stream.StdioStream (sys.stdin)
+sti = stream.StdioStream (stream.MU_STDIN_FD)
 sti.open ()
 
 cvt = filter.FilterIconvStream (sti, sys.argv[1], sys.argv[2])
 cvt.open ()
 
-out = stream.StdioStream (sys.stdout, 0)
+out = stream.StdioStream (stream.MU_STDOUT_FD, 0)
 out.open ()
 
 total = 0
 while True:
     buf = cvt.read (total)
-    out.sequential_write (buf)
+    out.write (buf)
     total += cvt.read_count
     if not cvt.read_count:
         break
diff --git a/examples/python/mimetest.py b/examples/python/mimetest.py
index 7ec6ee2..c63c868 100644
--- a/examples/python/mimetest.py
+++ b/examples/python/mimetest.py
@@ -77,11 +77,9 @@ def message_display_parts (msg, indent):
           print "%*.*sBegin" % (indent, indent, '')
 
 	  flt = filter.FilterStream (part.body.get_stream (), encoding)
-          offset = 0
 
           while True:
-              buf = flt.readline (offset)
-              offset += flt.read_count
+              buf = flt.readline ()
               if not flt.read_count:
                   break
               print "%*.*s%s" % (indent, indent, '', buf),
diff --git a/examples/python/msg-send.py b/examples/python/msg-send.py
index cbca586..bb13097 100644
--- a/examples/python/msg-send.py
+++ b/examples/python/msg-send.py
@@ -59,7 +59,7 @@ if optfrom:
 if args:
     to = address.Address (args)
 
-sti = stream.StdioStream (sys.stdin, stream.MU_STREAM_SEEKABLE)
+sti = stream.StdioStream (stream.MU_STDIN_FD, stream.MU_STREAM_SEEKABLE)
 sti.open ()
 
 msg = message.Message ()
diff --git a/python/libmu_py/body.c b/python/libmu_py/body.c
index 8ddc517..3ac16f9 100644
--- a/python/libmu_py/body.c
+++ b/python/libmu_py/body.c
@@ -116,7 +116,7 @@ api_body_get_stream (PyObject *self, PyObject *args)
 
   Py_INCREF (py_stm);
 
-  status = mu_body_get_stream (py_body->body, &py_stm->stm);
+  status = mu_body_get_streamref (py_body->body, &py_stm->stm);
   return status_object (status, (PyObject *)py_stm);
 }
 
diff --git a/python/libmu_py/folder.c b/python/libmu_py/folder.c
index e476efa..41be5ca 100644
--- a/python/libmu_py/folder.c
+++ b/python/libmu_py/folder.c
@@ -140,31 +140,11 @@ api_folder_get_stream (PyObject *self, PyObject *args)
 
   Py_INCREF (py_stm);
 
-  status = mu_folder_get_stream (py_folder->folder, &py_stm->stm);
+  status = mu_folder_get_streamref (py_folder->folder, &py_stm->stm);
   return status_object (status, (PyObject *)py_stm);
 }
 
 static PyObject *
-api_folder_set_stream (PyObject *self, PyObject *args)
-{
-  int status;
-  PyFolder *py_folder;
-  PyStream *py_stm;
-
-  if (!PyArg_ParseTuple (args, "O!O", &PyFolderType, &py_folder, &py_stm))
-    return NULL;
-
-  if (!PyStream_Check ((PyObject *)py_stm))
-    {
-      PyErr_SetString (PyExc_TypeError, "");
-      return NULL;
-    }
-
-  status = mu_folder_set_stream (py_folder->folder, py_stm->stm);
-  return _ro (PyInt_FromLong (status));
-}
-
-static PyObject *
 api_folder_get_authority (PyObject *self, PyObject *args)
 {
   int status;
@@ -276,9 +256,6 @@ static PyMethodDef methods[] = {
   { "get_stream", (PyCFunction) api_folder_get_stream, METH_VARARGS,
     "" },
 
-  { "set_stream", (PyCFunction) api_folder_set_stream, METH_VARARGS,
-    "" },
-
   { "get_authority", (PyCFunction) api_folder_get_authority, METH_VARARGS,
     "" },
 
diff --git a/python/libmu_py/message.c b/python/libmu_py/message.c
index 78600cf..ab05639 100644
--- a/python/libmu_py/message.c
+++ b/python/libmu_py/message.c
@@ -331,28 +331,6 @@ api_message_unencapsulate (PyObject *self, PyObject *args)
   return status_object (status, (PyObject *)py_unen);
 }
 
-static PyObject *
-api_message_set_stream (PyObject *self, PyObject *args)
-{
-  int status;
-  PyMessage *py_msg;
-  PyStream *py_stm;
-
-  if (!PyArg_ParseTuple (args, "O!O", &PyMessageType, &py_msg, &py_stm))
-    return NULL;
-
-  if (!PyStream_Check ((PyObject *)py_stm))
-    {
-      PyErr_SetString (PyExc_TypeError, "");
-      return NULL;
-    }
-
-  status = mu_message_set_stream (py_msg->msg, py_stm->stm, NULL);
-  py_stm->stm = NULL;
-
-  return _ro (PyInt_FromLong (status));
-}
-
 static PyMethodDef methods[] = {
   { "create", (PyCFunction) api_message_create, METH_VARARGS,
     "Create message." },
@@ -402,9 +380,6 @@ static PyMethodDef methods[] = {
   { "unencapsulate", (PyCFunction) api_message_unencapsulate,
     METH_VARARGS, "" },
 
-  { "set_stream", (PyCFunction) api_message_set_stream, METH_VARARGS,
-    "" },
-
   { NULL, NULL, 0, NULL }
 };
 
diff --git a/python/libmu_py/stream.c b/python/libmu_py/stream.c
index 4767376..fdcbcca 100644
--- a/python/libmu_py/stream.c
+++ b/python/libmu_py/stream.c
@@ -115,20 +115,13 @@ api_file_stream_create (PyObject *self, PyObject *args)
 static PyObject *
 api_stdio_stream_create (PyObject *self, PyObject *args)
 {
-  int status, flags;
-  FILE *fp;
+  int status, fd, flags;
   PyStream *py_stm;
-  PyFileObject *py_file;
 
-  if (!PyArg_ParseTuple (args, "O!O!i",
-			 &PyStreamType, &py_stm,
-			 &PyFile_Type, &py_file,
-			 &flags))
+  if (!PyArg_ParseTuple (args, "O!ii", &PyStreamType, &py_stm, &fd, &flags))
     return NULL;
 
-  fp = PyFile_AsFile ((PyObject *)py_file);
-
-  status = mu_stdio_stream_create (&py_stm->stm, fp, flags);
+  status = mu_stdio_stream_create (&py_stm->stm, fd, flags);
   return _ro (PyInt_FromLong (status));
 }
 
@@ -166,6 +159,30 @@ api_filter_prog_stream_create (PyObject *self, PyObject *args)
 }
 
 static PyObject *
+api_stream_ref (PyObject *self, PyObject *args)
+{
+  PyStream *py_stm;
+
+  if (!PyArg_ParseTuple (args, "O!", &PyStreamType, &py_stm))
+    return NULL;
+
+  mu_stream_ref (py_stm->stm);
+  return _ro (Py_None);
+}
+
+static PyObject *
+api_stream_unref (PyObject *self, PyObject *args)
+{
+  PyStream *py_stm;
+
+  if (!PyArg_ParseTuple (args, "O!", &PyStreamType, &py_stm))
+    return NULL;
+
+  mu_stream_unref (py_stm->stm);
+  return _ro (Py_None);
+}
+
+static PyObject *
 api_stream_destroy (PyObject *self, PyObject *args)
 {
   PyStream *py_stm;
@@ -173,7 +190,7 @@ api_stream_destroy (PyObject *self, PyObject *args)
   if (!PyArg_ParseTuple (args, "O!", &PyStreamType, &py_stm))
     return NULL;
 
-  mu_stream_destroy (&py_stm->stm, NULL);
+  mu_stream_destroy (&py_stm->stm);
   return _ro (Py_None);
 }
 
@@ -233,7 +250,6 @@ static PyObject *
 api_stream_read (PyObject *self, PyObject *args)
 {
   int status;
-  size_t offset;
   size_t read_count;
   char rbuf[1024];
   PyObject *py_ret;
@@ -241,11 +257,10 @@ api_stream_read (PyObject *self, PyObject *args)
 
   memset (rbuf, 0, sizeof (rbuf));
 
-  if (!PyArg_ParseTuple (args, "O!i", &PyStreamType, &py_stm, &offset))
+  if (!PyArg_ParseTuple (args, "O!", &PyStreamType, &py_stm))
     return NULL;
 
-  status = mu_stream_read (py_stm->stm, rbuf, sizeof (rbuf), offset,
-			   &read_count);
+  status = mu_stream_read (py_stm->stm, rbuf, sizeof (rbuf), &read_count);
 
   py_ret = PyTuple_New (3);
   PyTuple_SetItem (py_ret, 0, PyInt_FromLong (status));
@@ -258,17 +273,14 @@ static PyObject *
 api_stream_write (PyObject *self, PyObject *args)
 {
   int status;
-  size_t offset;
-  size_t write_count;
   char *wbuf;
+  size_t size, write_count;
   PyStream *py_stm;
 
-  if (!PyArg_ParseTuple (args, "O!si", &PyStreamType, &py_stm,
-			 &wbuf, &offset))
+  if (!PyArg_ParseTuple (args, "O!si", &PyStreamType, &py_stm, &wbuf, &size))
     return NULL;
 
-  status = mu_stream_write (py_stm->stm, wbuf, strlen (wbuf), offset,
-			    &write_count);
+  status = mu_stream_write (py_stm->stm, wbuf, size, &write_count);
   return status_object (status, PyInt_FromLong (write_count));
 }
 
@@ -276,31 +288,6 @@ static PyObject *
 api_stream_readline (PyObject *self, PyObject *args)
 {
   int status;
-  size_t offset;
-  size_t read_count;
-  char rbuf[1024];
-  PyObject *py_ret;
-  PyStream *py_stm;
-
-  memset (rbuf, 0, sizeof (rbuf));
-
-  if (!PyArg_ParseTuple (args, "O!i", &PyStreamType, &py_stm, &offset))
-    return NULL;
-
-  status = mu_stream_readline (py_stm->stm, rbuf, sizeof (rbuf), offset,
-			       &read_count);
-
-  py_ret = PyTuple_New (3);
-  PyTuple_SetItem (py_ret, 0, PyInt_FromLong (status));
-  PyTuple_SetItem (py_ret, 1, PyString_FromString (rbuf));
-  PyTuple_SetItem (py_ret, 2, PyInt_FromLong (read_count));
-  return _ro (py_ret);
-}
-
-static PyObject *
-api_stream_sequential_readline (PyObject *self, PyObject *args)
-{
-  int status;
   size_t read_count;
   char rbuf[1024];
   PyObject *py_ret;
@@ -311,8 +298,7 @@ api_stream_sequential_readline (PyObject *self, PyObject *args)
   if (!PyArg_ParseTuple (args, "O!", &PyStreamType, &py_stm))
     return NULL;
 
-  status = mu_stream_sequential_readline (py_stm->stm, rbuf, sizeof (rbuf),
-					  &read_count);
+  status = mu_stream_readline (py_stm->stm, rbuf, sizeof (rbuf), &read_count);
 
   py_ret = PyTuple_New (3);
   PyTuple_SetItem (py_ret, 0, PyInt_FromLong (status));
@@ -321,22 +307,6 @@ api_stream_sequential_readline (PyObject *self, PyObject *args)
   return _ro (py_ret);
 }
 
-static PyObject *
-api_stream_sequential_write (PyObject *self, PyObject *args)
-{
-  int status;
-  char *wbuf;
-  size_t size;
-  PyStream *py_stm;
-
-  if (!PyArg_ParseTuple (args, "O!si", &PyStreamType, &py_stm, &wbuf,
-			 &size))
-    return NULL;
-
-  status = mu_stream_sequential_write (py_stm->stm, wbuf, size);
-  return _ro (PyInt_FromLong (status));
-}
-
 static PyMethodDef methods[] = {
   { "tcp_stream_create", (PyCFunction) api_tcp_stream_create, METH_VARARGS,
     "" },
@@ -354,6 +324,12 @@ static PyMethodDef methods[] = {
     (PyCFunction) api_filter_prog_stream_create, METH_VARARGS,
     "" },
 
+  { "ref", (PyCFunction) api_stream_ref, METH_VARARGS,
+    "" },
+
+  { "unref", (PyCFunction) api_stream_unref, METH_VARARGS,
+    "" },
+
   { "destroy", (PyCFunction) api_stream_destroy, METH_VARARGS,
     "" },
 
@@ -378,12 +354,6 @@ static PyMethodDef methods[] = {
   { "readline", (PyCFunction) api_stream_readline, METH_VARARGS,
     "" },
 
-  { "sequential_readline", (PyCFunction) api_stream_sequential_readline,
-    METH_VARARGS, "" },
-
-  { "sequential_write", (PyCFunction) api_stream_sequential_write,
-    METH_VARARGS, "" },
-
   { NULL, NULL, 0, NULL }
 };
 
diff --git a/python/mailutils/stream.py b/python/mailutils/stream.py
index dc909cb..a0edfc9 100644
--- a/python/mailutils/stream.py
+++ b/python/mailutils/stream.py
@@ -36,6 +36,10 @@ MU_STREAM_IROTH =       0x00004000
 MU_STREAM_IWOTH =       0x00008000
 MU_STREAM_IMASK =       0x0000F000
 
+MU_STDIN_FD  = 0
+MU_STDOUT_FD = 1
+MU_STDERR_FD = 2
+
 class Stream:
     __refcount = 0
 
@@ -80,36 +84,25 @@ class Stream:
         if status:
             raise StreamError (status)
 
-    def read (self, offset = 0):
-        status, rbuf, self.read_count = stream.read (self.stm, offset)
+    def read (self):
+        status, rbuf, self.read_count = stream.read (self.stm)
         if status:
             raise StreamError (status)
         return rbuf
 
-    def write (self, wbuf, offset = 0):
-        status, self.write_count = stream.write (self.stm, wbuf, offset)
-        if status:
-            raise StreamError (status)
-
-    def readline (self, offset = 0):
-        status, rbuf, self.read_count = stream.readline (self.stm, offset)
+    def write (self, wbuf, size=None):
+        if size == None:
+            size = len (wbuf)
+        status, self.write_count = stream.write (self.stm, wbuf, size)
         if status:
             raise StreamError (status)
-        return rbuf
 
-    def sequential_readline (self):
+    def readline (self):
         status, rbuf, self.read_count = stream.readline (self.stm)
         if status:
             raise StreamError (status)
         return rbuf
 
-    def sequential_write (self, wbuf, size = None):
-        if size == None:
-            size = len (wbuf)
-        status = stream.sequential_write (self.stm, wbuf, size)
-        if status:
-            raise StreamError (status)
-
 class TcpStream (Stream):
     def __init__ (self, host, port, flags = MU_STREAM_READ):
         Stream.__init__ (self)
@@ -125,9 +118,9 @@ class FileStream (Stream):
             raise StreamError (status)
 
 class StdioStream (Stream):
-    def __init__ (self, file, flags = MU_STREAM_READ):
+    def __init__ (self, fd=MU_STDIN_FD, flags=MU_STREAM_READ):
         Stream.__init__ (self)
-        status = stream.stdio_stream_create (self.stm, file, flags)
+        status = stream.stdio_stream_create (self.stm, fd, flags)
         if status:
             raise StreamError (status)
 


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Mon Oct 11 17:23:57 2010
Received: from mailman by lists.gnu.org with archive (Exim 4.43)
	id 1P5Pqf-0003s1-7C
	for mharc-commit-mailutils@gnu.org; Mon, 11 Oct 2010 17:23:57 -0400
Received: from [140.186.70.92] (port=35846 helo=eggs.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.43) id 1P5Pqa-0003oi-WE
	for commit-mailutils@gnu.org; Mon, 11 Oct 2010 17:23:55 -0400
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P5Pov-00071f-3t
	for commit-mailutils@gnu.org; Mon, 11 Oct 2010 17:22:11 -0400
Received: from colonialone.fsf.org ([140.186.70.51]:44554
	helo=internal.in.savannah.gnu.org)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P5Pov-00071b-16
	for commit-mailutils@gnu.org; Mon, 11 Oct 2010 17:22:09 -0400
Received: from [10.1.0.108] (helo=vcs-noshell.in.savannah.gnu.org)
	by internal.in.savannah.gnu.org with esmtp (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P5Pou-0003GA-E9
	for commit-mailutils@gnu.org; Mon, 11 Oct 2010 21:22:08 +0000
Received: from gray by vcs-noshell.in.savannah.gnu.org with local (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P5Pou-0003fo-1w
	for commit-mailutils@gnu.org; Mon, 11 Oct 2010 21:22:08 +0000
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: e3ef44efce74604bf62cb630f172226afa91b76c
X-Git-Newrev: 2164243df618ab718f4d30c95174eab12178fb76
Message-Id: <E1P5Pou-0003fo-1w@vcs-noshell.in.savannah.gnu.org>
Date: Mon, 11 Oct 2010 21:22:08 +0000
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
Subject: [SCM] GNU Mailutils branch, master,
	updated. release-2.2-146-g2164243
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.5
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=unsubscribe>
List-Archive: <http://lists.gnu.org/archive/html/commit-mailutils>
List-Post: <mailto:commit-mailutils@gnu.org>
List-Help: <mailto:commit-mailutils-request@gnu.org?subject=help>
List-Subscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Mon, 11 Oct 2010 21:23:55 -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=2164243df618ab718f4d30c95174eab12178fb76

The branch, master has been updated
       via  2164243df618ab718f4d30c95174eab12178fb76 (commit)
       via  d3759d840f37fa6a56fcbe57b7bce8883f210593 (commit)
      from  e3ef44efce74604bf62cb630f172226afa91b76c (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 2164243df618ab718f4d30c95174eab12178fb76
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Tue Oct 12 00:19:24 2010 +0300

    Improve mu_unroll_symlink interface.
    
    * include/mailutils/util.h (mu_readlink): New function.
    (mu_unroll_symlink): Change prototype.
    * libmailutils/base/symlink.c (mu_qualify_link): Remove.
    (mu_readlink): New function.
    (mu_unroll_symlink): Rewrite.  Don't use statically allocated
    buffers.
    * libmailutils/base/locker.c (mu_locker_create): Update calls to
    mu_unroll_symlink.

commit d3759d840f37fa6a56fcbe57b7bce8883f210593
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Sun Oct 10 22:17:08 2010 +0300

    Use mu_make_file_name where necessary.
    
    * comsat/action.c (run_user_action): Use mu_make_file_name.
    * imap4d/lsub.c (imap4d_lsub): Likewise.
    * imap4d/subscribe.c (imap4d_subscribe): Likewise.
    * imap4d/unsubscribe.c (imap4d_unsubscribe): Likewise.
    * libmailutils/base/filename.c (mu_get_full_path): Likewise.
    * libmailutils/base/tempfile.c (mu_tempfile): Likewise.
    * libmailutils/base/url.c (_url_path_default): Likewise.
    * libmu_auth/virtual.c (getpwnam_virtual)
    (mu_auth_virt_domain_by_name): Likewise.
    * libmu_sieve/extensions/vacation.c (check_db): Likewise.
    * libmu_sieve/sieve.l (_try_include): Likewise.
    * maidag/forward.c (maidag_forward): Likewise.
    
    * mh/mh.h (mh_safe_make_file_name): New proto.
    * mh/mh_init.c (mh_audit_open,mh_get_dir)
    (mh_expand_name,mh_real_install): Use mh_safe_make_file_name.
    (mh_safe_make_file_name): New function.
    * mh/folder.c (read_seq_file, _scan): Use mh_safe_make_file_name.
    * mh/mh_global.c (mh_read_profile, _mh_init_global_sequences): Likewise.
    * mh/install-mh.c (main): Likewise.
    * mh/mhn.c (normalize_path, store_handler, main): Likewise.
    * mh/repl.c (make_draft): Likewise.
    * mh/rmf.c (rmf): Likewise.
    * mh/sortm.c (swap_message): Likewise.
    * mimeview/mimetypes.l (mimetypes_open): Likewise.
    
    * lib/mailcap.c: Add error checking.

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

Summary of changes:
 comsat/action.c                   |   10 ++-
 imap4d/lsub.c                     |    8 +-
 imap4d/subscribe.c                |    4 +-
 imap4d/unsubscribe.c              |    8 ++-
 include/mailutils/util.h          |    3 +-
 lib/mailcap.c                     |    4 +-
 libmailutils/base/filename.c      |    7 +--
 libmailutils/base/locker.c        |   13 ++--
 libmailutils/base/symlink.c       |  138 +++++++++++++++++++++++++------------
 libmailutils/base/tempfile.c      |    9 ++-
 libmailutils/base/url.c           |    7 +--
 libmu_auth/virtual.c              |    8 +--
 libmu_sieve/extensions/vacation.c |    3 +-
 libmu_sieve/sieve.l               |    8 ++-
 maidag/forward.c                  |    2 +-
 mh/folder.c                       |    8 +--
 mh/install-mh.c                   |    3 +-
 mh/mh.h                           |    2 +
 mh/mh_global.c                    |    4 +-
 mh/mh_init.c                      |   41 +++++++----
 mh/mhn.c                          |    9 +--
 mh/repl.c                         |    4 +-
 mh/rmf.c                          |    2 +-
 mh/sortm.c                        |    4 +-
 mimeview/mimetypes.l              |    2 +-
 25 files changed, 194 insertions(+), 117 deletions(-)

diff --git a/comsat/action.c b/comsat/action.c
index afba30f..0e1e679 100644
--- a/comsat/action.c
+++ b/comsat/action.c
@@ -375,9 +375,15 @@ run_user_action (FILE *tty, const char *cr, mu_message_t msg)
       char *cwd = mu_getcwd ();
       char *rcname;
 
-      mu_asprintf (&rcname, "%s/%s", cwd, BIFF_RC);
+      rcname = mu_make_file_name (cwd, BIFF_RC);
       free (cwd);
-      
+      if (!rcname)
+        {
+          mu_diag_funcall (MU_DIAG_ERROR, "mu_make_file_name", NULL, ENOMEM);
+          fclose (fp);
+          return;
+        }
+        
       mu_diag_get_debug (&debug);
       
       while ((n = act_getline (fp, &stmt, &size)))
diff --git a/imap4d/lsub.c b/imap4d/lsub.c
index d743c3c..2421f6b 100644
--- a/imap4d/lsub.c
+++ b/imap4d/lsub.c
@@ -34,8 +34,8 @@ imap4d_lsub (struct imap4d_command *command, imap4d_tokbuf_t tok)
 {
   char *ref;
   char *wcard;
-  char *file = NULL;
-  char *pattern = NULL;
+  char *file;
+  char *pattern;
   const char *delim = "/";
   FILE *fp;
   
@@ -45,11 +45,11 @@ imap4d_lsub (struct imap4d_command *command, imap4d_tokbuf_t tok)
   ref = imap4d_tokbuf_getarg (tok, IMAP4_ARG_1);
   wcard = imap4d_tokbuf_getarg (tok, IMAP4_ARG_2);
 
-  asprintf (&pattern, "%s%s", ref, wcard);
+  pattern = mu_make_file_name (ref, wcard);
   if (!pattern)
     return io_completion_response (command, RESP_NO, "Not enough memory");
   
-  asprintf (&file, "%s/.mailboxlist", real_homedir);
+  file = mu_make_file_name (real_homedir, ".mailboxlist");
   if (!file)
     {
       free (pattern);
diff --git a/imap4d/subscribe.c b/imap4d/subscribe.c
index 49feb90..6c24407 100644
--- a/imap4d/subscribe.c
+++ b/imap4d/subscribe.c
@@ -40,7 +40,9 @@ imap4d_subscribe (struct imap4d_command *command, imap4d_tokbuf_t tok)
 
   name = imap4d_tokbuf_getarg (tok, IMAP4_ARG_1);
 
-  asprintf (&file, "%s/.mailboxlist", real_homedir);
+  file = mu_make_file_name (real_homedir, ".mailboxlist");
+  if (!file)
+    return io_completion_response (command, RESP_NO, "Cannot subscribe");
   fp = fopen (file, "a");
   free (file);
   if (fp)
diff --git a/imap4d/unsubscribe.c b/imap4d/unsubscribe.c
index fb17bd8..108f7f3 100644
--- a/imap4d/unsubscribe.c
+++ b/imap4d/unsubscribe.c
@@ -99,7 +99,13 @@ imap4d_unsubscribe (struct imap4d_command *command, imap4d_tokbuf_t tok)
 
   name = imap4d_tokbuf_getarg (tok, IMAP4_ARG_1);
 
-  asprintf (&file, "%s/.mailboxlist", real_homedir);
+  file = mu_make_file_name (real_homedir, ".mailboxlist");
+  if (!file)
+    {
+      mu_diag_funcall (MU_DIAG_ERROR, "mu_make_file_name", NULL, ENOMEM);
+      return io_completion_response (command, RESP_NO, "Cannot unsubscribe");
+    }
+
   sd.result = 0;
   sd.name = name;
 
diff --git a/include/mailutils/util.h b/include/mailutils/util.h
index 57d11f0..3af373c 100644
--- a/include/mailutils/util.h
+++ b/include/mailutils/util.h
@@ -78,7 +78,8 @@ char *mu_normalize_path (char *path);
 char *mu_expand_path_pattern (const char *pattern, const char *username);
 char *mu_tilde_expansion (const char *ref, const char *delim,
 			  const char *homedir);
-int mu_unroll_symlink (char *out, size_t outsz, const char *in);
+int mu_readlink (const char *name, char **pbuf, size_t *psize, size_t *plen);
+int mu_unroll_symlink (const char *name, char **pout);
 char *mu_getcwd (void);
 int mu_tempfile (const char *tmpdir, char **namep);
 char *mu_tempname (const char *tmpdir);
diff --git a/lib/mailcap.c b/lib/mailcap.c
index fd1c40a..e4f0b57 100644
--- a/lib/mailcap.c
+++ b/lib/mailcap.c
@@ -660,10 +660,12 @@ display_stream_mailcap (const char *ident, mu_stream_t stream, mu_header_t hdr,
       char *home = mu_get_homedir ();
       asprintf (&mailcap_path, "%s/.mailcap:%s", home, DEFAULT_MAILCAP);
       free (home);
+      if (!mailcap_path)
+        return 1;
     }
   else
     mailcap_path = strdup (mailcap_path);
-
+  
   obstack_init (&expand_stack);
   
   for (p = strtok_r (mailcap_path, ":", &sp); p; p = strtok_r (NULL, ":", &sp))
diff --git a/libmailutils/base/filename.c b/libmailutils/base/filename.c
index 97c4813..625fbb6 100644
--- a/libmailutils/base/filename.c
+++ b/libmailutils/base/filename.c
@@ -59,14 +59,11 @@ mu_get_full_path (const char *file)
       char *cwd = mu_getcwd ();
       if (cwd)
 	{
-	  p = calloc (strlen (cwd) + 1 + strlen (file) + 1, 1);
-	  if (p)
-	    sprintf (p, "%s/%s", cwd, file);
+	  p = mu_make_file_name (cwd, file);
 	  free (cwd);
 	}
     }
-
-  if (!p)
+  else
     p = strdup (file);
   return p;
 }
diff --git a/libmailutils/base/locker.c b/libmailutils/base/locker.c
index 99a1a4e..dc33fdc 100644
--- a/libmailutils/base/locker.c
+++ b/libmailutils/base/locker.c
@@ -437,7 +437,7 @@ mu_locker_create (mu_locker_t *plocker, const char *fname, int flags)
 {
   unsigned type;
   mu_locker_t l;
-  char filename[_POSIX_PATH_MAX];
+  char *filename;
   int err = 0;
 
   if (plocker == NULL)
@@ -446,15 +446,18 @@ mu_locker_create (mu_locker_t *plocker, const char *fname, int flags)
   if (fname == NULL)
     return EINVAL;
 
-  if ((err = mu_unroll_symlink (filename, sizeof (filename), fname)))
+  if ((err = mu_unroll_symlink (fname, &filename)))
     return err;
 
   l = calloc (1, sizeof (*l));
 
   if (l == NULL)
-    return ENOMEM;
-
-  l->file = strdup (filename);
+    {
+      free (filename);
+      return ENOMEM;
+    }
+  
+  l->file = filename;
   
   if (l->file == NULL)
     {
diff --git a/libmailutils/base/symlink.c b/libmailutils/base/symlink.c
index 7551a46..eafbd28 100644
--- a/libmailutils/base/symlink.c
+++ b/libmailutils/base/symlink.c
@@ -30,32 +30,73 @@
 #include <mailutils/types.h>
 #include <mailutils/util.h>
 
-/* The result of readlink() may be a path relative to that link, 
- * qualify it if necessary.
- */
-static void
-mu_qualify_link (const char *path, const char *link, char *qualified)
+#define INITIAL_READLINK_SIZE 128
+
+int
+mu_readlink (const char *name, char **pbuf, size_t *psize, size_t *plen)
 {
-  const char *lb = NULL;
-  size_t len;
+  size_t status = 0;
+  char *buf = *pbuf;
+  size_t size = *plen;
+  ssize_t linklen;
 
-  /* link is full path */
-  if (*link == '/')
+  while (1)
     {
-      mu_cpystr (qualified, link, _POSIX_PATH_MAX);
-      return;
+      if (!buf)
+	{
+	  size = INITIAL_READLINK_SIZE;
+	  buf = malloc (size);
+	}
+      else
+	{
+	  char *p;
+	  size_t newsize = size << 1;
+	  if (newsize < size)
+	    {
+	      status = ENAMETOOLONG;
+	      break;
+	    }
+	  size = newsize;
+	  p = realloc (buf, size);
+	  if (!p)
+	    free (buf);
+	  buf = p;
+	}
+      if (!buf)
+	{
+	  status = ENOMEM;
+	  break;
+	}
+      
+      linklen = readlink (name, buf, size);
+      if (linklen < 0 && errno != ERANGE)
+	{
+	  status = errno;
+	  break;
+	}
+
+      if ((size_t) linklen < size)
+	{
+	  buf[linklen++] = '\0';
+	  status = 0;
+	  break;
+	}
     }
 
-  if ((lb = strrchr (path, '/')) == NULL)
+  if (status)
     {
-      /* no path in link */
-      mu_cpystr (qualified, link, _POSIX_PATH_MAX);
-      return;
+      if (buf)
+	{
+	  free (buf);
+	  buf = NULL;
+	}
+      size = 0;
     }
-
-  len = lb - path + 1;
-  memcpy (qualified, path, len);
-  mu_cpystr (qualified + len, link, _POSIX_PATH_MAX - len);
+  *pbuf = buf;
+  *psize = size;
+  if (plen)
+    *plen = linklen;
+  return status;
 }
 
 #ifndef _POSIX_SYMLOOP_MAX
@@ -63,41 +104,52 @@ mu_qualify_link (const char *path, const char *link, char *qualified)
 #endif
 
 int
-mu_unroll_symlink (char *out, size_t outsz, const char *in)
+mu_unroll_symlink (const char *name, char **pout)
 {
-  char path[_POSIX_PATH_MAX];
-  int symloops = 0;
+  size_t symloops = 0;
+  struct slbuf { char *base; size_t size; } buf[2];
+  int idx = 0;
+  int status;
+  
+  buf[0].base = buf[1].base = NULL;
+  buf[0].size = buf[1].size = 0;
 
   while (symloops++ < _POSIX_SYMLOOP_MAX)
     {
-      struct stat s;
-      char link[_POSIX_PATH_MAX];
-      char qualified[_POSIX_PATH_MAX];
-      int len;
+      struct stat st;
+      size_t len;
 
-      if (lstat (in, &s) == -1)
+      if (lstat (name, &st) == -1)
 	return errno;
 
-      if (!S_ISLNK (s.st_mode))
+      if (!S_ISLNK (st.st_mode))
 	{
-	  mu_cpystr (path, in, sizeof (path));
+	  if (!buf[idx].base)
+	    {
+	      buf[idx].base = strdup (name);
+	      if (!buf[idx].base)
+		return ENOMEM;
+	    }
+	  status = 0;
 	  break;
 	}
 
-      if ((len = readlink (in, link, sizeof (link))) == -1)
-	return errno;
-
-      link[(len >= sizeof (link)) ? (sizeof (link) - 1) : len] = '\0';
-
-      mu_qualify_link (in, link, qualified);
-
-      mu_cpystr (path, qualified, sizeof (path));
-
-      in = path;
+      idx = !idx;
+      status = mu_readlink (name, &buf[idx].base, &buf[idx].size, &len);
+      if (status)
+	break;
+      name = mu_normalize_path (buf[idx].base);
     }
 
-  mu_cpystr (out, path, outsz);
-
-  return 0;
+  if (status)
+    {
+      free (buf[0].base);
+      free (buf[1].base);
+    }
+  else
+    {
+      *pout = buf[idx].base;
+      free (buf[!idx].base);
+    }
+  return status;
 }
-
diff --git a/libmailutils/base/tempfile.c b/libmailutils/base/tempfile.c
index cbd6d96..951e039 100644
--- a/libmailutils/base/tempfile.c
+++ b/libmailutils/base/tempfile.c
@@ -31,6 +31,7 @@
 #include <mailutils/io.h>
 #include <mailutils/error.h>
 #include <mailutils/errno.h>
+#include <mailutils/util.h>
 
 /* Create and open a temporary file. Be very careful about it, since we
    may be running with extra privilege i.e setgid().
@@ -52,8 +53,12 @@ mu_tempfile (const char *tmpdir, char **namep)
   if (!tmpdir)
     tmpdir = (getenv ("TMPDIR")) ? getenv ("TMPDIR") : P_tmpdir;
 
-  if (mu_asprintf (&filename, "%s/muXXXXXX", tmpdir))
-    return -1;
+  filename = mu_make_file_name (tmpdir, "muXXXXXX");
+  if (!filename)
+    {
+      mu_diag_funcall (MU_DIAG_ERROR, "mu_make_file_name", NULL, ENOMEM);
+      return -1;
+    }
 
 #ifdef HAVE_MKSTEMP
   {
diff --git a/libmailutils/base/url.c b/libmailutils/base/url.c
index d177fa7..4f51071 100644
--- a/libmailutils/base/url.c
+++ b/libmailutils/base/url.c
@@ -956,12 +956,7 @@ mu_url_init (mu_url_t url, int port, const char *scheme)
 static char *
 _url_path_default (const char *spooldir, const char *user, int unused)
 {
-  char *mbox = malloc (strlen (spooldir) + strlen (user) + 2);
-  if (!mbox)
-    errno = ENOMEM;
-  else
-    sprintf (mbox, "%s/%s", spooldir, user);
-  return mbox;
+  return mu_make_file_name (spooldir, user);
 }
 
 /* Hashed indexing */
diff --git a/libmu_auth/virtual.c b/libmu_auth/virtual.c
index ad15352..d45b909 100644
--- a/libmu_auth/virtual.c
+++ b/libmu_auth/virtual.c
@@ -52,6 +52,7 @@
 #include <mailutils/mu_auth.h>
 #include <mailutils/nls.h>
 #include <mailutils/errno.h>
+#include <mailutils/util.h>
 
 #ifdef ENABLE_VIRTUAL_DOMAINS
 
@@ -89,12 +90,10 @@ getpwnam_virtual (const char *u)
   if (delim == 0)
     return NULL;
 
-  filename = malloc (strlen (mu_virtual_module_config.pwddir) +
-		     strlen (&u[delim + 1]) + 2 /* slash and null byte */);
+  filename = mu_make_file_name (mu_virtual_module_config.pwddir, &u[delim + 1]);
   if (filename == NULL)
     return NULL;
 
-  sprintf (filename, "%s/%s", mu_virtual_module_config.pwddir, &u[delim + 1]);
   pfile = fopen (filename, "r");
   free (filename);
 
@@ -173,10 +172,9 @@ mu_auth_virt_domain_by_name (struct mu_auth_data **return_data,
 	return MU_ERR_AUTH_FAILURE;
     }
   
-  mailbox_name = calloc (strlen (pw->pw_dir) + strlen ("/INBOX") + 1, 1);
+  mailbox_name = mu_make_file_name (pw->pw_dir, "INBOX");
   if (!mailbox_name)
     return ENOMEM;
-  sprintf (mailbox_name, "%s/INBOX", pw->pw_dir);
 
   rc = mu_auth_data_alloc (return_data,
 			   pw->pw_name,
diff --git a/libmu_sieve/extensions/vacation.c b/libmu_sieve/extensions/vacation.c
index 323da50..3e3b595 100644
--- a/libmu_sieve/extensions/vacation.c
+++ b/libmu_sieve/extensions/vacation.c
@@ -300,7 +300,8 @@ check_db (mu_sieve_machine_t mach, mu_list_t tags, char *from)
 
   home = mu_get_homedir ();
 
-  if (asprintf (&file, "%s/.vacation", (home ? home : ".")) == -1)
+  file = mu_make_file_name (home ? home : ".", ".vacation");
+  if (!file)
     {
       mu_sieve_error (mach, _("%lu: cannot build db file name"),
 		      (unsigned long) mu_sieve_get_message_num (mach));
diff --git a/libmu_sieve/sieve.l b/libmu_sieve/sieve.l
index 87a5dc6..2f60e13 100644
--- a/libmu_sieve/sieve.l
+++ b/libmu_sieve/sieve.l
@@ -349,11 +349,13 @@ static int
 _try_include (void *item, void *data)
 {
   char **dir = data;
-  char *name = malloc (strlen (item) + 1 + strlen (*dir) + 1);
+  char *name = mu_make_file_name ((char*) item, *dir);
 
   if (!name)
-    return 0;
-  sprintf (name, "%s/%s", (char*) item, *dir);
+    {
+      mu_diag_funcall (MU_DIAG_ERROR, "mu_make_file_name", NULL, ENOMEM);
+      return 0;
+    }
   if (access (name, R_OK) == 0)
     {
       *(char**) data = name;
diff --git a/maidag/forward.c b/maidag/forward.c
index 2bedd80..f64a3ae 100644
--- a/maidag/forward.c
+++ b/maidag/forward.c
@@ -342,7 +342,7 @@ maidag_forward (mu_message_t msg, struct mu_auth_data *auth, char *fwfile)
 		      auth->dir, mu_strerror (errno));
 	  return maidag_forward_none;
 	}
-      asprintf (&filename, "%s/%s", auth->dir, fwfile);
+      filename = mu_make_file_name (auth->dir, fwfile);
     }
   else
     filename = strdup (fwfile);
diff --git a/mh/folder.c b/mh/folder.c
index 5394f2b..6175594 100644
--- a/mh/folder.c
+++ b/mh/folder.c
@@ -275,9 +275,7 @@ read_seq_file (struct folder_info *info, const char *prefix, const char *name)
   mh_context_t *ctx;
   const char *p;
   
-  asprintf (&pname, "%s/%s", prefix, name);
-  if (!pname)
-    abort ();
+  pname = mh_safe_make_file_name (prefix, name);
   ctx = mh_context_create (pname, 1);
   mh_context_read (ctx);
   
@@ -350,7 +348,7 @@ _scan (const char *name, size_t depth)
 	}
       else if (entry->d_name[0] != ',')
 	{
-	  asprintf (&p, "%s/%s", name, entry->d_name);
+	  p = mh_safe_make_file_name (name, entry->d_name);
 	  if (stat (p, &st) < 0)
 	    mu_diag_funcall (MU_DIAG_ERROR, "stat", p, errno);
 	  else if (S_ISDIR (st.st_mode))
@@ -378,7 +376,7 @@ _scan (const char *name, size_t depth)
   
   if (info.cur)
     {
-      asprintf (&p, "%s/%s", name, mu_umaxtostr (0, info.cur));
+      p = mh_safe_make_file_name (name, mu_umaxtostr (0, info.cur));
       if (stat (p, &st) < 0 || !S_ISREG (st.st_mode))
 	info.cur = 0;
       free (p);
diff --git a/mh/install-mh.c b/mh/install-mh.c
index 1363a65..4bf3cff 100644
--- a/mh/install-mh.c
+++ b/mh/install-mh.c
@@ -71,8 +71,7 @@ main (int argc, char **argv)
   home = mu_get_homedir ();
   if (!home)
     abort (); /* shouldn't happen */
-  asprintf (&name, "%s/%s", home, MH_USER_PROFILE);
-  
+  name = mh_safe_make_file_name (home, MH_USER_PROFILE);
   mh_install (name, automode);
   return 0;
 }
diff --git a/mh/mh.h b/mh/mh.h
index 4ebc589..ec78d01 100644
--- a/mh/mh.h
+++ b/mh/mh.h
@@ -368,3 +368,5 @@ int check_draft_disposition (struct mh_whatnow_env *wh, int use_draft);
 void ali_parse_error (const char *fmt, ...) MU_PRINTFLIKE(1,2); 
 void ali_verbatim (int enable);
 
+char *mh_safe_make_file_name (const char *dir, const char *file);
+  
diff --git a/mh/mh_global.c b/mh/mh_global.c
index 9578a8f..06b4417 100644
--- a/mh/mh_global.c
+++ b/mh/mh_global.c
@@ -61,7 +61,7 @@ mh_read_profile ()
       char *home = mu_get_homedir ();
       if (!home)
 	abort (); /* shouldn't happen */
-      asprintf (&p, "%s/%s", home, MH_USER_PROFILE);
+      p = mh_safe_make_file_name (home, MH_USER_PROFILE);
       free (home);
     }
 
@@ -152,7 +152,7 @@ _mh_init_global_sequences ()
   _mh_init_global_context ();
   name = mh_global_profile_get ("mh-sequences", MH_SEQUENCES_FILE);
   p = mh_expand_name (NULL, current_folder, 0);
-  asprintf (&seq_name, "%s/%s", p, name);
+  seq_name = mh_safe_make_file_name (p, name);
   free (p);
   sequences = mh_context_create (seq_name, 1);
   if (mh_context_read (sequences) == 0)
diff --git a/mh/mh_init.c b/mh/mh_init.c
index b1c20c9..ca24686 100644
--- a/mh/mh_init.c
+++ b/mh/mh_init.c
@@ -381,14 +381,7 @@ mh_audit_open (char *name, mu_mailbox_t mbox)
   namep = mu_tilde_expansion (name, "/", NULL);
   if (strchr (namep, '/') == NULL)
     {
-      char *p = NULL;
-
-      asprintf (&p, "%s/%s", mu_folder_directory (), namep);
-      if (!p)
-	{
-	  mu_error (_("not enough memory"));
-	  exit (1);
-	}
+      char *p = mh_safe_make_file_name (mu_folder_directory (), namep);
       free (namep);
       namep = p;
     }
@@ -467,11 +460,16 @@ mh_get_dir ()
   if (mhdir[0] != '/')
     {
       char *p = mu_get_homedir ();
-      asprintf (&mhcopy, "%s/%s", p, mhdir);
+      mhcopy = mh_safe_make_file_name (p, mhdir);
       free (p);
     }
   else 
     mhcopy = strdup (mhdir);
+  if (!mhcopy)
+    {
+      mu_error (_("not enough memory"));
+      abort ();
+    }
   return mhcopy;
 }
 
@@ -487,8 +485,7 @@ mh_expand_name (const char *base, const char *name, int is_folder)
   else if (strncmp (namep, "../", 3) == 0 || strncmp (namep, "./", 2) == 0)
     {
       char *cwd = mu_getcwd ();
-      char *tmp = NULL;
-      asprintf (&tmp, "%s/%s", cwd, namep);
+      char *tmp = mh_safe_make_file_name (cwd, namep);
       free (cwd);
       free (namep);
       namep = tmp;
@@ -698,7 +695,7 @@ mh_real_install (char *name, int automode)
   char *ctx;
   FILE *fp;
   
-  asprintf (&mhdir, "%s/%s", home, "Mail");
+  mhdir = mh_safe_make_file_name (home, "Mail");
   
   if (!automode)
     {
@@ -736,7 +733,7 @@ mh_real_install (char *name, int automode)
 	  free (mhdir);
 	  if (local)
 	    {
-	      asprintf (&mhdir, "%s/%s", home, p);
+              mhdir = mh_safe_make_file_name (home, p);
 	      free (p);
 	    }
 	  else
@@ -756,7 +753,7 @@ mh_real_install (char *name, int automode)
   fprintf (fp, "Path: %s\n", mhdir);
   fclose (fp);
 
-  asprintf (&ctx, "%s/%s", mhdir, MH_CONTEXT_FILE);
+  ctx = mh_safe_make_file_name (mhdir, MH_CONTEXT_FILE);
   fp = fopen (ctx, "w");
   if (fp)
     {
@@ -764,7 +761,7 @@ mh_real_install (char *name, int automode)
       fclose (fp);
     }
   free (ctx);
-  asprintf (&ctx, "%s/inbox", mhdir);
+  ctx = mh_safe_make_file_name (mhdir, "inbox");
   if (mh_check_folder (ctx, !automode))
     exit (1);
   free (ctx);
@@ -1029,3 +1026,17 @@ mh_draft_message (const char *name, const char *msgspec, char **pname)
   mu_mailbox_destroy (&mbox);
   return rc;
 }
+
+char *
+mh_safe_make_file_name (const char *dir, const char *file)
+{
+  file = mu_make_file_name (dir, file);
+  if (!file)
+    {
+      mu_diag_funcall (MU_DIAG_ERROR, "mu_make_file_name", NULL, ENOMEM);
+      abort ();
+    }
+  return file;
+}
+
+			  
diff --git a/mh/mhn.c b/mh/mhn.c
index 23c8872..2f95838 100644
--- a/mh/mhn.c
+++ b/mh/mhn.c
@@ -1487,10 +1487,7 @@ normalize_path (const char *cwd, char *path)
   if (!cwd)
     cwd = pcwd = mu_getcwd ();
 
-  len = strlen (cwd) + strlen (path) + 2;
-  p = xmalloc (len);
-  sprintf (p, "%s/%s", cwd, path);
-  path = p;
+  path = mh_safe_make_file_name (cwd, path);
 
   /* delete trailing delimiter if any */
   if (len && path[len-1] == '/')
@@ -1576,7 +1573,7 @@ store_handler (mu_message_t msg, msg_part_t part, char *type, char *encoding,
     {
       char *fname = mhn_store_command (msg, part, prefix);
       if (dir)
-	asprintf (&name, "%s/%s", dir, fname);
+	name = mh_safe_make_file_name (dir, fname);
       else
 	name = fname;
     }
@@ -2670,7 +2667,7 @@ main (int argc, char **argv)
     case mode_compose:
       /* Prepare filename for diagnostic purposes */
       if (input_file[0] != '/')
-	asprintf (&input_file, "%s/%s", mu_folder_directory (), input_file);
+	input_file = mh_safe_make_file_name (mu_folder_directory (), input_file);
       rc = mhn_compose ();
       break;
       
diff --git a/mh/repl.c b/mh/repl.c
index 5bfccba..920c4d6 100644
--- a/mh/repl.c
+++ b/mh/repl.c
@@ -372,8 +372,8 @@ make_draft (mu_mailbox_t mbox, int disp, struct mh_whatnow_env *wh)
     
     mu_mailbox_get_url (mbox, &url);
     mh_message_number (msg, &num);
-    asprintf (&msgname, "%s/%s",
-	      mu_url_to_string (url), mu_umaxtostr (0, num));
+    msgname = mh_safe_make_file_name (mu_url_to_string (url), 
+                                      mu_umaxtostr (0, num));
     p = strchr (msgname, ':');
     if (!p)
       wh->msg = msgname;
diff --git a/mh/rmf.c b/mh/rmf.c
index 469f770..8a2c60a 100644
--- a/mh/rmf.c
+++ b/mh/rmf.c
@@ -131,7 +131,7 @@ rmf (const char *name)
 	  || strcmp (entry->d_name, "..") == 0)
 	continue;
       
-      asprintf (&p, "%s/%s", name, entry->d_name);
+      p = mh_safe_make_file_name (name, entry->d_name);
       if (stat (p, &st) < 0)
 	{
 	  mu_diag_funcall (MU_DIAG_ERROR, "stat", p, errno);
diff --git a/mh/sortm.c b/mh/sortm.c
index fd40de6..1e5bacc 100644
--- a/mh/sortm.c
+++ b/mh/sortm.c
@@ -447,8 +447,8 @@ swap_message (size_t a, size_t b)
   char *path_a, *path_b;
   char *tmp;
   
-  asprintf (&path_a, "%s/%s", mbox_path, mu_umaxtostr (0, a));
-  asprintf (&path_b, "%s/%s", mbox_path, mu_umaxtostr (1, b));
+  path_a = mh_safe_make_file_name (mbox_path, mu_umaxtostr (0, a));
+  path_b = mh_safe_make_file_name (mbox_path, mu_umaxtostr (1, b));
   tmp = mu_tempname (mbox_path);
   rename (path_a, tmp);
   unlink (path_a);
diff --git a/mimeview/mimetypes.l b/mimeview/mimetypes.l
index a4dbf20..222f9bf 100644
--- a/mimeview/mimetypes.l
+++ b/mimeview/mimetypes.l
@@ -153,7 +153,7 @@ mimetypes_open (const char *name)
   
   if (S_ISDIR (st.st_mode))
     {
-      asprintf (&file_name, "%s/mime.types", name);
+      file_name = mu_make_file_name (name, "mime.types");
       file_name_alloc = 1;
     }
   else


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Tue Oct 12 16:19:29 2010
Received: from mailman by lists.gnu.org with archive (Exim 4.43)
	id 1P5lJp-0000Lw-MX
	for mharc-commit-mailutils@gnu.org; Tue, 12 Oct 2010 16:19:29 -0400
Received: from [140.186.70.92] (port=56340 helo=eggs.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.43) id 1P5lJL-0003ab-Sn
	for commit-mailutils@gnu.org; Tue, 12 Oct 2010 16:19:28 -0400
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P5lIj-0001Nk-OT
	for commit-mailutils@gnu.org; Tue, 12 Oct 2010 16:18:59 -0400
Received: from colonialone.fsf.org ([140.186.70.51]:36620
	helo=internal.in.savannah.gnu.org)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P5lIj-0001Ng-Mr
	for commit-mailutils@gnu.org; Tue, 12 Oct 2010 16:18:21 -0400
Received: from [10.1.0.108] (helo=vcs-noshell.in.savannah.gnu.org)
	by internal.in.savannah.gnu.org with esmtp (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P5lIj-0003LC-Ir
	for commit-mailutils@gnu.org; Tue, 12 Oct 2010 20:18:21 +0000
Received: from gray by vcs-noshell.in.savannah.gnu.org with local (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P5lIi-0006Km-88
	for commit-mailutils@gnu.org; Tue, 12 Oct 2010 20:18:21 +0000
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: 2164243df618ab718f4d30c95174eab12178fb76
X-Git-Newrev: c93d9f7a12d78e2c3c991fbb8ba49008eee074ef
Message-Id: <E1P5lIi-0006Km-88@vcs-noshell.in.savannah.gnu.org>
Date: Tue, 12 Oct 2010 20:18:20 +0000
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
Subject: [SCM] GNU Mailutils branch, master,
	updated. release-2.2-147-gc93d9f7
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.5
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=unsubscribe>
List-Archive: <http://lists.gnu.org/archive/html/commit-mailutils>
List-Post: <mailto:commit-mailutils@gnu.org>
List-Help: <mailto:commit-mailutils-request@gnu.org?subject=help>
List-Subscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Tue, 12 Oct 2010 20:19:28 -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=c93d9f7a12d78e2c3c991fbb8ba49008eee074ef

The branch, master has been updated
       via  c93d9f7a12d78e2c3c991fbb8ba49008eee074ef (commit)
      from  2164243df618ab718f4d30c95174eab12178fb76 (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 c93d9f7a12d78e2c3c991fbb8ba49008eee074ef
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Tue Oct 12 23:18:10 2010 +0300

    maidag/tests/forward.at: forgotten to add (see db1ee5e0a).

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

Summary of changes:
 maidag/tests/forward.at |   78 +++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 78 insertions(+), 0 deletions(-)
 create mode 100644 maidag/tests/forward.at

diff --git a/maidag/tests/forward.at b/maidag/tests/forward.at
new file mode 100644
index 0000000..3516aa7
--- /dev/null
+++ b/maidag/tests/forward.at
@@ -0,0 +1,78 @@
+# This file is part of GNU Mailutils. -*- Autotest -*-
+# Copyright (C) 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.
+#
+# GNU Mailutils 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 GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
+
+AT_SETUP([forwarding support])
+AT_KEYWORDS([maidag forward])
+
+m4_pushdef([FORWARD_OPTIONS],[dnl
+ --set "^mailer^url=sendmail:$abs_top_builddir/examples/mta" dnl
+ --set "^forward-file=$forward" dnl
+ --set "^forward-file-checks=none"])
+
+AT_CHECK([
+forward=`pwd`/my.forward
+namelist=`sed 's/:.*//;3,$d' /etc/passwd 2>/dev/null`
+test -n "$namelist" || AT_SKIP_TEST
+
+cat > my.forward <<EOT
+foo@bar.baz
+\\$namelist
+EOT
+
+set -- $namelist
+
+cat > experr <<EOT
+maidag: notice: skipping forward file $forward: already processed
+EOT
+
+echo ENVELOPE > expout.0
+cat $abs_top_srcdir/maidag/tests/input.msg >> expout.0
+echo "" >> expout.0
+
+echo "ENVELOPE FROM: gulliver@example.net" > expout.1
+echo "ENVELOPE TO: <foo@bar.baz>" >> expout.1
+awk '{printf("% 4d: %s\n", NR-1,$0)}' $abs_top_srcdir/maidag/tests/input.msg >> expout.1
+echo "END OF MESSAGE" >> expout.1
+
+MTA_DIAG=mta.diag
+export MTA_DIAG
+mkdir spool
+maidag MAIDAG_OPTIONS FORWARD_OPTIONS --from gulliver@example.net $1 < \
+ $abs_top_srcdir/maidag/tests/input.msg || exit $?
+
+for name in $namelist
+do
+  if test -f spool/$name; then
+    sed '1s/From gulliver@example.net.*/ENVELOPE/' spool/$name | \
+      cmp expout.0 - >/dev/null || echo "$name mailbox mismatch" >&2
+  else
+    echo "$name missed message" >&2
+  fi
+done
+
+if test -f mta.diag; then
+  cmp mta.diag expout.1 >/dev/null || echo "remote copy mismatch" >&2
+else
+  echo "remote delivery failed" >&2
+fi  
+],
+[0],
+[],
+[experr])
+
+m4_popdef([FORWARD_OPTIONS])
+
+AT_CLEANUP


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Sat Oct 16 16:08:54 2010
Received: from mailman by lists.gnu.org with archive (Exim 4.43)
	id 1P7D3m-0006VO-FA
	for mharc-commit-mailutils@gnu.org; Sat, 16 Oct 2010 16:08:54 -0400
Received: from [140.186.70.92] (port=35589 helo=eggs.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.43) id 1P7D3j-0006TZ-3h
	for commit-mailutils@gnu.org; Sat, 16 Oct 2010 16:08:52 -0400
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P7D3h-0001ST-VD
	for commit-mailutils@gnu.org; Sat, 16 Oct 2010 16:08:50 -0400
Received: from colonialone.fsf.org ([140.186.70.51]:36996
	helo=internal.in.savannah.gnu.org)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P7D3h-0001SO-Su
	for commit-mailutils@gnu.org; Sat, 16 Oct 2010 16:08:49 -0400
Received: from [10.1.0.108] (helo=vcs-noshell.in.savannah.gnu.org)
	by internal.in.savannah.gnu.org with esmtp (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P7D3h-0007RO-Ko
	for commit-mailutils@gnu.org; Sat, 16 Oct 2010 20:08:49 +0000
Received: from gray by vcs-noshell.in.savannah.gnu.org with local (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P7D3h-0008Uc-Dy
	for commit-mailutils@gnu.org; Sat, 16 Oct 2010 20:08:49 +0000
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: c93d9f7a12d78e2c3c991fbb8ba49008eee074ef
X-Git-Newrev: cea1ebcb1ef0c5d46023e24b6008adb7d04376e3
Message-Id: <E1P7D3h-0008Uc-Dy@vcs-noshell.in.savannah.gnu.org>
Date: Sat, 16 Oct 2010 20:08:49 +0000
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
Subject: [SCM] GNU Mailutils branch, master,
	updated. release-2.2-148-gcea1ebc
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.5
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=unsubscribe>
List-Archive: <http://lists.gnu.org/archive/html/commit-mailutils>
List-Post: <mailto:commit-mailutils@gnu.org>
List-Help: <mailto:commit-mailutils-request@gnu.org?subject=help>
List-Subscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Sat, 16 Oct 2010 20:08:52 -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=cea1ebcb1ef0c5d46023e24b6008adb7d04376e3

The branch, master has been updated
       via  cea1ebcb1ef0c5d46023e24b6008adb7d04376e3 (commit)
      from  c93d9f7a12d78e2c3c991fbb8ba49008eee074ef (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 cea1ebcb1ef0c5d46023e24b6008adb7d04376e3
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Sat Oct 16 22:34:02 2010 +0300

    movemail: accept "smtp://" and "sendmail://" as a destination mailbox.
    
    * movemail/Makefile.am (movemail_LDADD): Add MU_LIB_MAILER.
    * movemail/movemail.c: Register *all* mailbox formats,
    including remote ones.

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

Summary of changes:
 movemail/Makefile.am |    1 +
 movemail/movemail.c  |    6 +++---
 2 files changed, 4 insertions(+), 3 deletions(-)

diff --git a/movemail/Makefile.am b/movemail/Makefile.am
index 9588330..c018d7f 100644
--- a/movemail/Makefile.am
+++ b/movemail/Makefile.am
@@ -27,6 +27,7 @@ movemail_LDADD = \
  ${MU_LIB_MH}\
  ${MU_LIB_MAILDIR}\
  ${MU_LIB_AUTH}\
+ ${MU_LIB_MAILER}\
  @MU_AUTHLIBS@\
  ${MU_LIB_MAILUTILS}\
  @MU_COMMON_LIBRARIES@
diff --git a/movemail/movemail.c b/movemail/movemail.c
index 4218849..914bd77 100644
--- a/movemail/movemail.c
+++ b/movemail/movemail.c
@@ -735,8 +735,8 @@ main (int argc, char **argv)
   MU_APP_INIT_NLS ();
   MU_AUTH_REGISTER_ALL_MODULES ();
   
-  /* Register the desired mailbox formats.  */
-  mu_register_all_mbox_formats ();
+  /* Register the desired "mailbox" formats.  */
+  mu_register_all_formats ();
 
   /* argument parsing */
   
@@ -760,7 +760,7 @@ main (int argc, char **argv)
   if (emacs_mode)
     {
       /* Undo the effect of configuration options that may affect
-	 the interaction with Emacs. */
+	 interaction with Emacs. */
       mu_registrar_set_default_record (mu_mbox_record);
       mu_debug_default_printer = mu_debug_stderr_printer;
     }


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Sun Oct 17 04:51:48 2010
Received: from mailman by lists.gnu.org with archive (Exim 4.43)
	id 1P7Oy4-0005EO-81
	for mharc-commit-mailutils@gnu.org; Sun, 17 Oct 2010 04:51:48 -0400
Received: from [140.186.70.92] (port=56839 helo=eggs.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.43) id 1P7Oy1-0005E6-97
	for commit-mailutils@gnu.org; Sun, 17 Oct 2010 04:51:46 -0400
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P7Oxz-0005AF-US
	for commit-mailutils@gnu.org; Sun, 17 Oct 2010 04:51:45 -0400
Received: from colonialone.fsf.org ([140.186.70.51]:36223
	helo=internal.in.savannah.gnu.org)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P7Oxz-0005AA-Rx
	for commit-mailutils@gnu.org; Sun, 17 Oct 2010 04:51:43 -0400
Received: from [10.1.0.108] (helo=vcs-noshell.in.savannah.gnu.org)
	by internal.in.savannah.gnu.org with esmtp (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P7Oxz-0008G0-7t
	for commit-mailutils@gnu.org; Sun, 17 Oct 2010 08:51:43 +0000
Received: from polak by vcs-noshell.in.savannah.gnu.org with local (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P7Oxz-0001f1-3j
	for commit-mailutils@gnu.org; Sun, 17 Oct 2010 08:51:43 +0000
From: "Wojciech Polak" <polak@gnu.org>
To: commit-mailutils@gnu.org
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: cea1ebcb1ef0c5d46023e24b6008adb7d04376e3
X-Git-Newrev: 1417084b72f9a2eb6547b65982e314e07fd9b012
Message-Id: <E1P7Oxz-0001f1-3j@vcs-noshell.in.savannah.gnu.org>
Date: Sun, 17 Oct 2010 08:51:43 +0000
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
Subject: [SCM] GNU Mailutils branch, master,
	updated. release-2.2-149-g1417084
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.5
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=unsubscribe>
List-Archive: <http://lists.gnu.org/archive/html/commit-mailutils>
List-Post: <mailto:commit-mailutils@gnu.org>
List-Help: <mailto:commit-mailutils-request@gnu.org?subject=help>
List-Subscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Sun, 17 Oct 2010 08:51:47 -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=1417084b72f9a2eb6547b65982e314e07fd9b012

The branch, master has been updated
       via  1417084b72f9a2eb6547b65982e314e07fd9b012 (commit)
      from  cea1ebcb1ef0c5d46023e24b6008adb7d04376e3 (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 1417084b72f9a2eb6547b65982e314e07fd9b012
Author: Wojciech Polak <polak@gnu.org>
Date:   Sun Oct 17 10:48:30 2010 +0200

    Minor PEP 8 changes in Python API.

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

Summary of changes:
 python/mailutils/__init__.py  |    5 ++---
 python/mailutils/address.py   |    8 ++++----
 python/mailutils/auth.py      |    8 ++++----
 python/mailutils/debug.py     |    2 +-
 python/mailutils/filter.py    |    4 ++--
 python/mailutils/folder.py    |    2 +-
 python/mailutils/header.py    |    6 +++---
 python/mailutils/mailbox.py   |    6 +++---
 python/mailutils/mailer.py    |    2 +-
 python/mailutils/message.py   |    4 ++--
 python/mailutils/mime.py      |    2 +-
 python/mailutils/registrar.py |    2 +-
 python/mailutils/stream.py    |    8 ++++----
 python/mailutils/util.py      |    4 ++--
 14 files changed, 31 insertions(+), 32 deletions(-)

diff --git a/python/mailutils/__init__.py b/python/mailutils/__init__.py
index 423a09d..eb23f52 100644
--- a/python/mailutils/__init__.py
+++ b/python/mailutils/__init__.py
@@ -33,10 +33,9 @@ is licensed using the GNU GPL.
 
 See http://www.gnu.org/software/mailutils/ for more information about
 GNU Mailutils.
-
 """
 
-__all__ = [
+__all__ = (
     "error",
     "address",
     "attribute",
@@ -59,4 +58,4 @@ __all__ = [
     "stream",
     "url",
     "util",
-]
+)
diff --git a/python/mailutils/address.py b/python/mailutils/address.py
index 4f43232..73a457a 100644
--- a/python/mailutils/address.py
+++ b/python/mailutils/address.py
@@ -83,28 +83,28 @@ class Address:
 
     def get_domain (self, n):
         """Return domain part of the Nth email address."""
-        status, domain  = address.get_domain (self.addr, n)
+        status, domain = address.get_domain (self.addr, n)
         if status:
             raise AddressError (status)
         return domain
 
     def get_personal (self, n):
         """Return personal part of the Nth email address."""
-        status, personal  = address.get_personal (self.addr, n)
+        status, personal = address.get_personal (self.addr, n)
         if status:
             raise AddressError (status)
         return personal
 
     def get_comments (self, n):
         """Return comment part of the Nth email address."""
-        status, comments  = address.get_comments (self.addr, n)
+        status, comments = address.get_comments (self.addr, n)
         if status:
             raise AddressError (status)
         return comments
 
     def get_route (self, n):
         """Return the route part of the Nth email address."""
-        status, route  = address.get_route (self.addr, n)
+        status, route = address.get_route (self.addr, n)
         if status:
             raise AddressError (status)
         return route
diff --git a/python/mailutils/auth.py b/python/mailutils/auth.py
index 36ee8de..c201a69 100644
--- a/python/mailutils/auth.py
+++ b/python/mailutils/auth.py
@@ -21,7 +21,7 @@ from mailutils.error import AuthError
 
 MU_AF_QUOTA = 0x1
 
-def register_module (name = None):
+def register_module (name=None):
     if name == None:
         status = auth.register_module ()
     elif isinstance (name, types.TupleType) \
@@ -51,7 +51,7 @@ def set_pam_service (pam_service):
 class Authority:
     __owner = False
 
-    def __init__ (self, authority = None):
+    def __init__ (self, authority=None):
         if isinstance (authority, auth.AuthorityType):
             self.authority = authority
         else:
@@ -85,7 +85,7 @@ class Authority:
 class Ticket:
     __owner = False
 
-    def __init__ (self, ticket = None):
+    def __init__ (self, ticket=None):
         if isinstance (ticket, auth.TicketType):
             self.ticket = ticket
         else:
@@ -108,7 +108,7 @@ class Ticket:
 class Wicket:
     __owner = False
 
-    def __init__ (self, wicket = None):
+    def __init__ (self, wicket=None):
         if isinstance (wicket, auth.WicketType):
             self.wicket = wicket
         else:
diff --git a/python/mailutils/debug.py b/python/mailutils/debug.py
index feafe78..fda7747 100644
--- a/python/mailutils/debug.py
+++ b/python/mailutils/debug.py
@@ -37,7 +37,7 @@ class Debug:
     def __del__ (self):
         del self.dbg
 
-    def set_level (self, level = MU_DEBUG_PROT):
+    def set_level (self, level=MU_DEBUG_PROT):
         status = debug.set_level (self.dbg, level)
         if status:
             raise DebugError (status)
diff --git a/python/mailutils/filter.py b/python/mailutils/filter.py
index 7844fb5..08f9459 100644
--- a/python/mailutils/filter.py
+++ b/python/mailutils/filter.py
@@ -29,8 +29,8 @@ MU_FILTER_WRITE = MU_STREAM_WRITE
 MU_FILTER_RDWR  = MU_STREAM_RDWR
 
 class FilterStream (Stream):
-    def __init__ (self, transport, name, type = MU_FILTER_DECODE,
-                  direction = MU_FILTER_READ):
+    def __init__ (self, transport, name, type=MU_FILTER_DECODE,
+                  direction=MU_FILTER_READ):
         Stream.__init__ (self)
         status = filter.create (self.stm, transport.stm, name,
                                 type, direction)
diff --git a/python/mailutils/folder.py b/python/mailutils/folder.py
index ba503f7..b3fc69c 100644
--- a/python/mailutils/folder.py
+++ b/python/mailutils/folder.py
@@ -77,7 +77,7 @@ class Folder:
             raise FolderError (status)
         return url.Url (u)
 
-    def list (self, dirname, pattern, max_level = 0):
+    def list (self, dirname, pattern, max_level=0):
         status, lst = folder.list (self.folder, dirname, pattern, max_level)
         if status:
             raise FolderError (status)
diff --git a/python/mailutils/header.py b/python/mailutils/header.py
index ea875cf..3c39751 100644
--- a/python/mailutils/header.py
+++ b/python/mailutils/header.py
@@ -82,7 +82,7 @@ class Header:
             raise HeaderError (status)
         return lines
 
-    def get_value (self, name, default = None):
+    def get_value (self, name, default=None):
         status, value = header.get_value (self.hdr, name)
         if status == MU_ERR_NOENT:
             if default != None:
@@ -93,7 +93,7 @@ class Header:
             raise HeaderError (status)
         return value
 
-    def get_value_n (self, name, n = 1, default = None):
+    def get_value_n (self, name, n=1, default=None):
         status, value = header.get_value_n (self.hdr, name, n)
         if status == MU_ERR_NOENT:
             if default != None:
@@ -104,7 +104,7 @@ class Header:
             raise HeaderError (status)
         return value
 
-    def set_value (self, name, value, replace = True):
+    def set_value (self, name, value, replace=True):
         status = header.set_value (self.hdr, name, value, replace)
         if status:
             raise HeaderError (status)
diff --git a/python/mailutils/mailbox.py b/python/mailutils/mailbox.py
index 517da49..a077015 100644
--- a/python/mailutils/mailbox.py
+++ b/python/mailutils/mailbox.py
@@ -24,7 +24,7 @@ from mailutils import debug
 from mailutils.error import MailboxError
 
 class MailboxBase:
-    def open (self, mode = 0):
+    def open (self, mode=0):
         """Open the connection.
 
         'mode' may be a string, consisting of the characters described
@@ -65,7 +65,7 @@ class MailboxBase:
         if status:
             raise MailboxError (status)
 
-    def flush (self, expunge = False):
+    def flush (self, expunge=False):
         """Flush the mailbox."""
         status = mailbox.flush (self.mbox, expunge)
         if status:
@@ -216,7 +216,7 @@ class Mailbox (MailboxBase):
         del self.mbox
 
 class MailboxDefault (MailboxBase):
-    def __init__ (self, name = None):
+    def __init__ (self, name=None):
         """MailboxDefault creates a Mailbox object for the supplied
         mailbox 'name'. Before creating, the name is expanded using
         the rules below:
diff --git a/python/mailutils/mailer.py b/python/mailutils/mailer.py
index 0bbbd71..445f002 100644
--- a/python/mailutils/mailer.py
+++ b/python/mailutils/mailer.py
@@ -37,7 +37,7 @@ class Mailer:
         else:
             raise AttributeError, name
 
-    def open (self, flags = 0):
+    def open (self, flags=0):
         status = mailer.open (self.mlr, flags)
         if status:
             raise MailerError (status)
diff --git a/python/mailutils/message.py b/python/mailutils/message.py
index 85ad25e..aaa8e20 100644
--- a/python/mailutils/message.py
+++ b/python/mailutils/message.py
@@ -25,7 +25,7 @@ from mailutils.error import MessageError
 class Message:
     __owner = False
 
-    def __init__ (self, msg = None):
+    def __init__ (self, msg=None):
         if msg == None:
             self.msg = message.MessageType ()
             self.__owner = True
@@ -141,7 +141,7 @@ class Message:
             raise MessageError (status)
         return name, lang
 
-    def save_attachment (self, filename = ''):
+    def save_attachment (self, filename=''):
         status = message.save_attachment (self.msg, filename)
         if status:
             raise MessageError (status)
diff --git a/python/mailutils/mime.py b/python/mailutils/mime.py
index 3c28a89..be428f6 100644
--- a/python/mailutils/mime.py
+++ b/python/mailutils/mime.py
@@ -23,7 +23,7 @@ MU_MIME_MULTIPART_MIXED = 0x1
 MU_MIME_MULTIPART_ALT   = 0x2
 
 class Mime:
-    def __init__ (self, msg, flags = 0):
+    def __init__ (self, msg, flags=0):
         self.mime = mime.MimeType ()
         status = mime.create (self.mime, msg.msg, flags)
         if status:
diff --git a/python/mailutils/registrar.py b/python/mailutils/registrar.py
index af90ad6..c6d5218 100644
--- a/python/mailutils/registrar.py
+++ b/python/mailutils/registrar.py
@@ -18,7 +18,7 @@
 import types
 from mailutils.c_api import registrar
 
-def register_format (name = None):
+def register_format (name=None):
     """Register desired mailutils 'name' format.
 
     A list or tuple of strings can be given.
diff --git a/python/mailutils/stream.py b/python/mailutils/stream.py
index a0edfc9..5b7d92b 100644
--- a/python/mailutils/stream.py
+++ b/python/mailutils/stream.py
@@ -43,7 +43,7 @@ MU_STDERR_FD = 2
 class Stream:
     __refcount = 0
 
-    def __init__ (self, stm = None):
+    def __init__ (self, stm=None):
         if isinstance (stm, stream.StreamType):
             self.stm = stm
         else:
@@ -104,14 +104,14 @@ class Stream:
         return rbuf
 
 class TcpStream (Stream):
-    def __init__ (self, host, port, flags = MU_STREAM_READ):
+    def __init__ (self, host, port, flags=MU_STREAM_READ):
         Stream.__init__ (self)
         status = stream.tcp_stream_create (self.stm, host, port, flags)
         if status:
             raise StreamError (status)
 
 class FileStream (Stream):
-    def __init__ (self, filename, flags = MU_STREAM_READ):
+    def __init__ (self, filename, flags=MU_STREAM_READ):
         Stream.__init__ (self)
         status = stream.file_stream_create (self.stm, filename, flags)
         if status:
@@ -125,7 +125,7 @@ class StdioStream (Stream):
             raise StreamError (status)
 
 class ProgStream (Stream):
-    def __init__ (self, progname, flags = MU_STREAM_READ):
+    def __init__ (self, progname, flags=MU_STREAM_READ):
         Stream.__init__ (self)
         status = stream.prog_stream_create (self.stm, progname, flags)
         if status:
diff --git a/python/mailutils/util.py b/python/mailutils/util.py
index 9aa1795..fbc6a94 100644
--- a/python/mailutils/util.py
+++ b/python/mailutils/util.py
@@ -17,7 +17,7 @@
 
 from mailutils.c_api import util
 
-def get_user_email (name = None):
+def get_user_email (name=None):
     if name == None:
         return util.get_user_email ()
     else:
@@ -33,5 +33,5 @@ def get_user_email_domain ():
 def set_user_email_domain (domain):
     util.set_user_email_domain (domain)
 
-def tempname (tmpdir = None):
+def tempname (tmpdir=None):
     return util.tempname (tmpdir)


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Mon Oct 18 04:58:06 2010
Received: from mailman by lists.gnu.org with archive (Exim 4.43)
	id 1P7lXi-0000YU-3v
	for mharc-commit-mailutils@gnu.org; Mon, 18 Oct 2010 04:58:06 -0400
Received: from [140.186.70.92] (port=45761 helo=eggs.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.43) id 1P7lXe-0000Wp-Mx
	for commit-mailutils@gnu.org; Mon, 18 Oct 2010 04:58:04 -0400
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P7lXd-00006S-7Q
	for commit-mailutils@gnu.org; Mon, 18 Oct 2010 04:58:02 -0400
Received: from colonialone.fsf.org ([140.186.70.51]:34811
	helo=internal.in.savannah.gnu.org)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P7lXd-00006K-4L
	for commit-mailutils@gnu.org; Mon, 18 Oct 2010 04:58:01 -0400
Received: from [10.1.0.108] (helo=vcs-noshell.in.savannah.gnu.org)
	by internal.in.savannah.gnu.org with esmtp (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P7lXc-0000aV-JV
	for commit-mailutils@gnu.org; Mon, 18 Oct 2010 08:58:00 +0000
Received: from gray by vcs-noshell.in.savannah.gnu.org with local (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P7lXc-0007of-DC
	for commit-mailutils@gnu.org; Mon, 18 Oct 2010 08:58:00 +0000
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
X-Git-Refname: refs/heads/patches-2.2
X-Git-Reftype: branch
X-Git-Oldrev: 3edaa0f6738e29773cc4fa92dd96900c3e4f351e
X-Git-Newrev: 998888781247d0a60306cd2f6bd1a9ff7b60ee83
Message-Id: <E1P7lXc-0007of-DC@vcs-noshell.in.savannah.gnu.org>
Date: Mon, 18 Oct 2010 08:58:00 +0000
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
Subject: [SCM] GNU Mailutils branch, patches-2.2,
	updated. release-2.2-12-g9988887
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.5
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=unsubscribe>
List-Archive: <http://lists.gnu.org/archive/html/commit-mailutils>
List-Post: <mailto:commit-mailutils@gnu.org>
List-Help: <mailto:commit-mailutils-request@gnu.org?subject=help>
List-Subscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Mon, 18 Oct 2010 08:58:04 -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=998888781247d0a60306cd2f6bd1a9ff7b60ee83

The branch, patches-2.2 has been updated
       via  998888781247d0a60306cd2f6bd1a9ff7b60ee83 (commit)
       via  f0fbffbabf159f45baae3561a65e006846e0d13a (commit)
      from  3edaa0f6738e29773cc4fa92dd96900c3e4f351e (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 998888781247d0a60306cd2f6bd1a9ff7b60ee83
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Mon Oct 18 11:50:57 2010 +0300

    movemail: accept "smtp://" and "sendmail://" as a destination mailbox.
    (port cea1ebcb1e from master)
    
    * movemail/Makefile.am (movemail_LDADD): Add MU_LIB_MAILER.
    * movemail/movemail.c: Register *all* mailbox formats,
    including remote ones.

commit f0fbffbabf159f45baae3561a65e006846e0d13a
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Fri Oct 15 12:06:50 2010 +0300

    Switch to new gnulib (port of 8672f9f048 and 52d2552d23).

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

Summary of changes:
 configure.ac             |    6 +++---
 examples/Makefile.am     |   14 +++++++-------
 examples/cpp/Makefile.am |    4 ++--
 imap4d/Makefile.am       |    2 +-
 lib/Makefile.am          |    2 +-
 mapi/Makefile.am         |    2 +-
 mh/Makefile.am           |    2 +-
 movemail/Makefile.am     |    1 +
 movemail/movemail.c      |    6 +++---
 pop3d/Makefile.am        |    2 +-
 10 files changed, 21 insertions(+), 20 deletions(-)

diff --git a/configure.ac b/configure.ac
index 7d9e218..cd13ead 100644
--- a/configure.ac
+++ b/configure.ac
@@ -58,7 +58,7 @@ AC_SUBST(mu_aux_dir,'$(top_srcdir)/mu-aux')
 AC_SUBST(MU_SIEVE_MODDIR,'$(libdir)/$(PACKAGE)')
 
 AC_SUBST(MU_COMMON_LIBRARIES,'$(LTLIBINTL) $(LTLIBICONV)')
-AC_SUBST(MU_APP_LIBRARIES,'../libmu_argp/libmu_argp.a ../libmu_cfg/libmu_cfg.la ../lib/libmuaux.la')
+AC_SUBST(MU_APP_LIBRARIES,'../libmu_argp/libmu_argp.a ../libmu_cfg/libmu_cfg.la ../lib/libmuaux.a')
 
 # There are two sets of include directories: MU_LIB_COMMON_INCLUDES, used
 # by the libraries, and MU_APP_COMMON_INCLUDES, which is used by applications.
@@ -98,7 +98,7 @@ status_tcpwrap=maybe
 
 dnl Internationalization macros.
 AM_GNU_GETTEXT([external], [need-ngettext])
-AM_GNU_GETTEXT_VERSION(0.17)
+AM_GNU_GETTEXT_VERSION(0.18)
 AM_ICONV
 AC_CHECK_HEADERS(iconv.h)
 AC_CHECK_TYPE(iconv_t,:,
@@ -1077,7 +1077,7 @@ GINT_INIT([gint],[1.8 with-guile],
            MU_GUILE_SIEVE_MOD_DIR='$(GUILE_SITE)/$(PACKAGE)/sieve-modules'
            MU_GUILE_SIEVE_MOD_DATA='$(MU_GUILE_SIEVE_MOD_DATA_X)'
            GINT_INCLUDES='${MU_APP_COMMON_INCLUDES}'
-           GINT_LDADD=../lib/libmuaux.la
+           GINT_LDADD=../lib/libmuaux.a
           ],[useguile=no])
 
 AM_CONDITIONAL([MU_COND_LIBMU_SCM],[test "$useguile" = "yes"])
diff --git a/examples/Makefile.am b/examples/Makefile.am
index cb2415b..63e9a9c 100644
--- a/examples/Makefile.am
+++ b/examples/Makefile.am
@@ -55,7 +55,7 @@ mod_LTLIBRARIES = numaddr.la
 INCLUDES = @MU_LIB_COMMON_INCLUDES@ 
 
 LDADD = \
- ../lib/libmuaux.la\
+ ../lib/libmuaux.a\
  ${MU_LIB_MAILUTILS}\
  @MU_COMMON_LIBRARIES@
 
@@ -64,7 +64,7 @@ numaddr_la_LIBADD = ${MU_LIB_SIEVE}
 numaddr_la_LDFLAGS = -module -avoid-version -no-undefined -rpath '$(moddir)'
 
 mimetest_LDADD =\
- ../lib/libmuaux.la\
+ ../lib/libmuaux.a\
  ${MU_LIB_MBOX}\
  ${MU_LIB_IMAP}\
  ${MU_LIB_POP}\
@@ -73,20 +73,20 @@ mimetest_LDADD =\
  ${MU_LIB_MAILUTILS}
 
 msg_send_LDADD =\
- ../lib/libmuaux.la\
+ ../lib/libmuaux.a\
  ${MU_LIB_MAILER}\
  @MU_AUTHLIBS@\
  ${MU_LIB_MAILUTILS}
 
 mta_CPPFLAGS=-I${top_srcdir}/lib -I${top_builddir}/lib
 mta_LDADD =\
- ../lib/libmuaux.la\
+ ../lib/libmuaux.a\
  ${MU_LIB_MBOX}\
  ${MU_LIB_MAILER}\
  ${MU_LIB_MAILUTILS}
 
 sfrom_LDADD =\
- ../lib/libmuaux.la\
+ ../lib/libmuaux.a\
  ${MU_LIB_MBOX}\
  ${MU_LIB_IMAP}\
  ${MU_LIB_POP}\
@@ -98,7 +98,7 @@ sfrom_LDADD =\
  ${MU_LIB_MAILUTILS}
 
 pop3client_LDADD = \
- ../lib/libmuaux.la\
+ ../lib/libmuaux.a\
  ${MU_LIB_POP}\
  ${MU_LIB_AUTH}\
  @MU_AUTHLIBS@\
@@ -106,7 +106,7 @@ pop3client_LDADD = \
  @READLINE_LIBS@
 
 nntpclient_LDADD = \
- ../lib/libmuaux.la\
+ ../lib/libmuaux.a\
  ${MU_LIB_NNTP}\
  ${MU_LIB_AUTH}\
  @MU_AUTHLIBS@\
diff --git a/examples/cpp/Makefile.am b/examples/cpp/Makefile.am
index 32da62f..8176c6c 100644
--- a/examples/cpp/Makefile.am
+++ b/examples/cpp/Makefile.am
@@ -37,7 +37,7 @@ noinst_PROGRAMS = @MU_CXX_EXAMPLES_BUILD@
 INCLUDES = @MU_LIB_COMMON_INCLUDES@ 
 
 AM_LDFLAGS = \
- ../../lib/libmuaux.la\
+ ../../lib/libmuaux.a\
  ../../libmu_cpp/libmu_cpp.la\
  ${MU_LIB_MAILUTILS}\
  @MU_COMMON_LIBRARIES@
@@ -55,7 +55,7 @@ sfrom_SOURCES = sfrom.cc
 url_parse_SOURCES = url-parse.cc
 
 LDADD =\
- ../../lib/libmuaux.la\
+ ../../lib/libmuaux.a\
  ../../libmu_cpp/libmu_cpp.la\
  ${MU_LIB_MBOX}\
  ${MU_LIB_IMAP}\
diff --git a/imap4d/Makefile.am b/imap4d/Makefile.am
index 0d55728..3223044 100644
--- a/imap4d/Makefile.am
+++ b/imap4d/Makefile.am
@@ -82,7 +82,7 @@ imap4d_LDADD = \
 imap4d_DEPENDENCIES = \
  @IMAP_AUTHOBJS@\
  @MU_AUTHLIBS_DEPENDENCY@\
- ../lib/libmuaux.la\
+ ../lib/libmuaux.a\
  ${MU_LIB_MBOX}\
  ${MU_LIB_MAILUTILS}
 
diff --git a/lib/Makefile.am b/lib/Makefile.am
index d4c3b3c..4f7ad17 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -22,7 +22,7 @@ include gnulib.mk
 
 INCLUDES = @MU_LIB_COMMON_INCLUDES@ 
 
-libmuaux_la_SOURCES += \
+libmuaux_a_SOURCES += \
  daemon.c\
  mailcap.c\
  mu_dbm.c\
diff --git a/mapi/Makefile.am b/mapi/Makefile.am
index 9682f7a..fddc3bf 100644
--- a/mapi/Makefile.am
+++ b/mapi/Makefile.am
@@ -26,7 +26,7 @@ lib_LTLIBRARIES = libmapi.la
 /* FIXME:  This should be part of the default includes.  */
 EXTRA_DIST = mapi.h
 
-libmapi_LDADD = ../lib/libmuaux.la ${MU_LIB_MAILUTILS} 
+libmapi_LDADD = ../lib/libmuaux.a ${MU_LIB_MAILUTILS} 
 
 libmapi_la_SOURCES = \
  MAPIAddress.c \
diff --git a/mh/Makefile.am b/mh/Makefile.am
index 2fdb487..8cb3f09 100644
--- a/mh/Makefile.am
+++ b/mh/Makefile.am
@@ -96,7 +96,7 @@ INCLUDES = @MU_APP_COMMON_INCLUDES@
 AM_CPPFLAGS = -D_GNU_SOURCE -DMHLIBDIR=\"$(mhlibdir)\" -DMHBINDIR=\"$(bindir)\"
 mh_LIBS = \
  ./libmh.a\
- ../lib/libmuaux.la\
+ ../lib/libmuaux.a\
  ${MU_LIB_MBOX}\
  ${MU_LIB_IMAP}\
  ${MU_LIB_POP}\
diff --git a/movemail/Makefile.am b/movemail/Makefile.am
index 7bc6228..9752905 100644
--- a/movemail/Makefile.am
+++ b/movemail/Makefile.am
@@ -30,6 +30,7 @@ movemail_LDADD = \
  ${MU_LIB_MH}\
  ${MU_LIB_MAILDIR}\
  ${MU_LIB_AUTH}\
+ ${MU_LIB_MAILER}\
  @MU_AUTHLIBS@\
  ${MU_LIB_MAILUTILS}\
  @MU_COMMON_LIBRARIES@
diff --git a/movemail/movemail.c b/movemail/movemail.c
index 4e7bdb9..3d3cf2a 100644
--- a/movemail/movemail.c
+++ b/movemail/movemail.c
@@ -738,8 +738,8 @@ main (int argc, char **argv)
   MU_APP_INIT_NLS ();
   MU_AUTH_REGISTER_ALL_MODULES ();
   
-  /* Register the desired mailbox formats.  */
-  mu_register_all_mbox_formats ();
+  /* Register the desired "mailbox" formats.  */
+  mu_register_all_formats ();
 
   /* argument parsing */
   
@@ -763,7 +763,7 @@ main (int argc, char **argv)
   if (emacs_mode)
     {
       /* Undo the effect of configuration options that may affect
-	 the interaction with Emacs. */
+	 interaction with Emacs. */
       mu_registrar_set_default_record (mu_mbox_record);
       mu_debug_default_printer = mu_debug_stderr_printer;
     }
diff --git a/pop3d/Makefile.am b/pop3d/Makefile.am
index 8a73ede..171733f 100644
--- a/pop3d/Makefile.am
+++ b/pop3d/Makefile.am
@@ -64,7 +64,7 @@ popauth_SOURCES = popauth.c
 popauth_LDADD = ${MU_APP_LIBRARIES} ${MU_LIB_MAILUTILS} @MU_COMMON_LIBRARIES@
 pop3d_DEPENDENCIES = \
  @MU_AUTHLIBS_DEPENDENCY@ \
- ../lib/libmuaux.la \
+ ../lib/libmuaux.a \
  ${MU_LIB_MBOX}\
  ${MU_LIB_MH}\
  ${MU_LIB_MAILDIR}\


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Mon Oct 18 16:32:16 2010
Received: from mailman by lists.gnu.org with archive (Exim 4.43)
	id 1P7wNU-0000o9-84
	for mharc-commit-mailutils@gnu.org; Mon, 18 Oct 2010 16:32:16 -0400
Received: from [140.186.70.92] (port=52972 helo=eggs.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.43) id 1P7wNQ-0000mk-FQ
	for commit-mailutils@gnu.org; Mon, 18 Oct 2010 16:32:14 -0400
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P7wNN-0004qX-Lp
	for commit-mailutils@gnu.org; Mon, 18 Oct 2010 16:32:12 -0400
Received: from colonialone.fsf.org ([140.186.70.51]:50560
	helo=internal.in.savannah.gnu.org)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P7wNN-0004q2-JT
	for commit-mailutils@gnu.org; Mon, 18 Oct 2010 16:32:09 -0400
Received: from [10.1.0.108] (helo=vcs-noshell.in.savannah.gnu.org)
	by internal.in.savannah.gnu.org with esmtp (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P7wNN-0000rE-61
	for commit-mailutils@gnu.org; Mon, 18 Oct 2010 20:32:09 +0000
Received: from gray by vcs-noshell.in.savannah.gnu.org with local (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P7wNL-0004U2-Rr
	for commit-mailutils@gnu.org; Mon, 18 Oct 2010 20:32:09 +0000
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: 1417084b72f9a2eb6547b65982e314e07fd9b012
X-Git-Newrev: 2f6f7b4fc2230352e8ff742b9999672dec35236b
Message-Id: <E1P7wNL-0004U2-Rr@vcs-noshell.in.savannah.gnu.org>
Date: Mon, 18 Oct 2010 20:32:08 +0000
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
Subject: [SCM] GNU Mailutils branch, master,
	updated. release-2.2-150-g2f6f7b4
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.5
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=unsubscribe>
List-Archive: <http://lists.gnu.org/archive/html/commit-mailutils>
List-Post: <mailto:commit-mailutils@gnu.org>
List-Help: <mailto:commit-mailutils-request@gnu.org?subject=help>
List-Subscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Mon, 18 Oct 2010 20:32:14 -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=2f6f7b4fc2230352e8ff742b9999672dec35236b

The branch, master has been updated
       via  2f6f7b4fc2230352e8ff742b9999672dec35236b (commit)
      from  1417084b72f9a2eb6547b65982e314e07fd9b012 (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 2f6f7b4fc2230352e8ff742b9999672dec35236b
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Mon Oct 18 23:11:51 2010 +0300

    Improve the algorithm of obtaining sender name from messages.
    
    * include/mailutils/header.h: Remove unnecessary externs.
    (mu_url_sget_param, mu_url_aget_param): New protos.
    * libmailutils/base/url.c (mu_url_sget_param, mu_url_aget_param): New
    functions.
    
    * libmailutils/mailbox/hdrfirst.c: New file.
    * libmailutils/mailbox/Makefile.am (libmailbox_la_SOURCES): Add hdrfirst.c.
    
    * libmailutils/mailbox/message.c (message_envelope_sender): Try to restore
    the envelope sender from X-Envelope-Sender, X-Envelope-From, X-Original-Sender,
    and From headers, in that order.
    * libmailutils/mailer/mailer.c (_set_from): Try to obtain sender from
    the message envelope.  If that fails, fall back to the From header.
    * libproto/mailer/mbox.c (parse_received, guess_message_recipient): New
    static functions.
    (remote_mbox_append_message): Get recipient address from the first
    of X-Envelope-To, Delivered-To, X-Original-To headers, in that order.
    If that fails, try to deduce it from the Received header.  If that
    fails too, fall back to the To header.
    If recipient-headers URL parameter is defined, its value (a comma-separated
    list of names) overrides this list.
    The strip-domain parameter, if present, instructs the function to strip
    domain part from the recipient address before resolving it.
    The domain parameter, if present, supplies the domain name which overrides
    the domain obtained from the URL.

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

Summary of changes:
 include/mailutils/header.h                         |    3 +
 include/mailutils/url.h                            |   87 +++++-----
 libmailutils/base/url.c                            |   57 +++++++
 libmailutils/mailbox/Makefile.am                   |    1 +
 .../mailbox/hdrfirst.c                             |   46 +++---
 libmailutils/mailbox/message.c                     |   81 +++++-----
 libmailutils/mailer/mailer.c                       |   45 +++---
 libproto/mailer/mbox.c                             |  170 +++++++++++++++++++-
 8 files changed, 364 insertions(+), 126 deletions(-)
 copy libproto/pop/pop3_timeout.c => libmailutils/mailbox/hdrfirst.c (61%)

diff --git a/include/mailutils/header.h b/include/mailutils/header.h
index fe617fb..0a73d52 100644
--- a/include/mailutils/header.h
+++ b/include/mailutils/header.h
@@ -105,6 +105,9 @@ extern int mu_header_get_value_n (mu_header_t, const char *, int, char *,
 extern int mu_header_aget_value_n (mu_header_t, const char *, int, char **);
 #define mu_header_aget_value(header, name, pptr) \
   mu_header_aget_value_n (header, name, 1, pptr)
+
+int mu_header_sget_firstof (mu_header_t hdr, char **names,
+			    const char **pval, int *pidx);
   
 /* Get field values as an mu_address_t. */
 extern int mu_header_get_address_n (mu_header_t, const char *,
diff --git a/include/mailutils/url.h b/include/mailutils/url.h
index 1531d0d..449fbc4 100644
--- a/include/mailutils/url.h
+++ b/include/mailutils/url.h
@@ -25,61 +25,64 @@
 extern "C" {
 #endif
 
-extern int  mu_url_create    (mu_url_t *, const char *name);
-extern int  mu_url_dup       (mu_url_t old_url, mu_url_t *new_url);
-extern int  mu_url_uplevel   (mu_url_t url, mu_url_t *upurl);
+int  mu_url_create    (mu_url_t *, const char *name);
+int  mu_url_dup       (mu_url_t old_url, mu_url_t *new_url);
+int  mu_url_uplevel   (mu_url_t url, mu_url_t *upurl);
 
-extern void mu_url_destroy   (mu_url_t *);
-extern int  mu_url_parse     (mu_url_t);
+void mu_url_destroy   (mu_url_t *);
+int  mu_url_parse     (mu_url_t);
 
-extern int mu_url_sget_scheme  (const mu_url_t, const char **);
-extern int mu_url_aget_scheme  (const mu_url_t, char **);  
-extern int mu_url_get_scheme  (const mu_url_t, char *, size_t, size_t *);
-  
-extern int mu_url_sget_user  (const mu_url_t, const char **);
-extern int mu_url_aget_user  (const mu_url_t, char **);  
-extern int mu_url_get_user  (const mu_url_t, char *, size_t, size_t *);
+int mu_url_sget_scheme  (const mu_url_t, const char **);
+int mu_url_aget_scheme  (const mu_url_t, char **);  
+int mu_url_get_scheme  (const mu_url_t, char *, size_t, size_t *);
 
-extern int mu_url_get_secret (const mu_url_t url, mu_secret_t *psecret);
-  
-extern int mu_url_sget_auth  (const mu_url_t, const char **);
-extern int mu_url_aget_auth  (const mu_url_t, char **);  
-extern int mu_url_get_auth  (const mu_url_t, char *, size_t, size_t *);
+int mu_url_sget_user  (const mu_url_t, const char **);
+int mu_url_aget_user  (const mu_url_t, char **);  
+int mu_url_get_user  (const mu_url_t, char *, size_t, size_t *);
 
-extern int mu_url_sget_host  (const mu_url_t, const char **);
-extern int mu_url_aget_host  (const mu_url_t, char **);  
-extern int mu_url_get_host  (const mu_url_t, char *, size_t, size_t *);
-  
-extern int mu_url_sget_path  (const mu_url_t, const char **);
-extern int mu_url_aget_path  (const mu_url_t, char **);  
-extern int mu_url_get_path  (const mu_url_t, char *, size_t, size_t *);
+int mu_url_get_secret (const mu_url_t url, mu_secret_t *psecret);
 
-extern int mu_url_sget_query (const mu_url_t url, size_t *qc, char ***qv);
-extern int mu_url_aget_query (const mu_url_t url, size_t *qc, char ***qv);
-  
-extern int mu_url_get_port    (const mu_url_t, long *);
+int mu_url_sget_auth  (const mu_url_t, const char **);
+int mu_url_aget_auth  (const mu_url_t, char **);  
+int mu_url_get_auth  (const mu_url_t, char *, size_t, size_t *);
+
+int mu_url_sget_host  (const mu_url_t, const char **);
+int mu_url_aget_host  (const mu_url_t, char **);  
+int mu_url_get_host  (const mu_url_t, char *, size_t, size_t *);
+
+int mu_url_sget_path  (const mu_url_t, const char **);
+int mu_url_aget_path  (const mu_url_t, char **);  
+int mu_url_get_path  (const mu_url_t, char *, size_t, size_t *);
+
+int mu_url_sget_query (const mu_url_t url, size_t *qc, char ***qv);
+int mu_url_aget_query (const mu_url_t url, size_t *qc, char ***qv);
+
+int mu_url_get_port    (const mu_url_t, long *);
 
 int mu_url_sget_fvpairs (const mu_url_t url, size_t *fvc, char ***fvp);
 int mu_url_aget_fvpairs (const mu_url_t url, size_t *pfvc, char ***pfvp);
 
-extern int mu_url_expand_path (mu_url_t url);
-extern const char *mu_url_to_string   (const mu_url_t);
-
-extern int mu_url_set_scheme (mu_url_t url, const char *scheme);
+int mu_url_sget_param (const mu_url_t url, const char *param, const char **val);
+int mu_url_aget_param (const mu_url_t url, const char *param, char **val);
   
-extern int mu_url_is_scheme   (mu_url_t, const char *scheme);
+int mu_url_expand_path (mu_url_t url);
+const char *mu_url_to_string   (const mu_url_t);
+
+int mu_url_set_scheme (mu_url_t url, const char *scheme);
+
+int mu_url_is_scheme   (mu_url_t, const char *scheme);
 
-extern int mu_url_is_same_scheme (mu_url_t, mu_url_t);
-extern int mu_url_is_same_user   (mu_url_t, mu_url_t);
-extern int mu_url_is_same_path   (mu_url_t, mu_url_t);
-extern int mu_url_is_same_host   (mu_url_t, mu_url_t);
-extern int mu_url_is_same_port   (mu_url_t, mu_url_t);
+int mu_url_is_same_scheme (mu_url_t, mu_url_t);
+int mu_url_is_same_user   (mu_url_t, mu_url_t);
+int mu_url_is_same_path   (mu_url_t, mu_url_t);
+int mu_url_is_same_host   (mu_url_t, mu_url_t);
+int mu_url_is_same_port   (mu_url_t, mu_url_t);
 
-extern char *mu_url_decode_len (const char *s, size_t len);  
-extern char *mu_url_decode     (const char *s);
+char *mu_url_decode_len (const char *s, size_t len);  
+char *mu_url_decode     (const char *s);
 
-extern int mu_url_is_ticket   (mu_url_t ticket, mu_url_t url);
-extern int mu_url_init (mu_url_t url, int port, const char *scheme);
+int mu_url_is_ticket   (mu_url_t ticket, mu_url_t url);
+int mu_url_init (mu_url_t url, int port, const char *scheme);
   
 #ifdef __cplusplus
 }
diff --git a/libmailutils/base/url.c b/libmailutils/base/url.c
index 4f51071..82b9f3f 100644
--- a/libmailutils/base/url.c
+++ b/libmailutils/base/url.c
@@ -764,6 +764,48 @@ mu_url_sget_fvpairs (const mu_url_t url, size_t *fvc, char ***fvp)
 }
 
 int
+mu_url_sget_param (const mu_url_t url, const char *param, const char **val)
+{
+  size_t fvc;
+  char **fvp;
+  int status = mu_url_sget_fvpairs (url, &fvc, &fvp);
+
+  if (status)
+    return status;
+
+  if (fvc)
+    {
+      size_t i;
+      
+      for (i = 0; i < fvc; i++)
+	{
+	  const char *p;
+	  char *q;
+
+	  for (p = param, q = fvp[i]; *p && *q && *p == *q; p++, q++)
+	    ;
+	  if (*p == 0)
+	    {
+	      if (*q == 0)
+		{
+		  if (val)
+		    *val = q;
+		  return 0;
+		}
+	      else if (*q == '=')
+		{
+		  if (val)
+		    *val = q + 1;
+		  return 0;
+		}
+	    }
+	}
+    }
+ 
+  return MU_ERR_NOENT;
+}
+  
+int
 mu_url_aget_fvpairs (const mu_url_t url, size_t *pfvc, char ***pfvp)
 {
   size_t fvc, i;
@@ -792,6 +834,21 @@ mu_url_aget_fvpairs (const mu_url_t url, size_t *pfvc, char ***pfvp)
 }
 
 int
+mu_url_aget_param (const mu_url_t url, const char *param, char **val)
+{
+  const char *s;
+  int status = mu_url_sget_param (url, param, &s);
+
+  if (status == 0)
+    {
+      *val = strdup (s);
+      if (!*val)
+	status = ENOMEM;
+    }
+  return status;
+}
+
+int
 mu_url_get_port (const mu_url_t url, long *pport)
 {
   if (url == NULL)
diff --git a/libmailutils/mailbox/Makefile.am b/libmailutils/mailbox/Makefile.am
index 5fffd57..24658ed 100644
--- a/libmailutils/mailbox/Makefile.am
+++ b/libmailutils/mailbox/Makefile.am
@@ -25,6 +25,7 @@ libmailbox_la_SOURCES =  \
  body.c\
  envelope.c\
  folder.c\
+ hdrfirst.c\
  hdritr.c\
  header.c\
  message.c\
diff --git a/libproto/pop/pop3_timeout.c b/libmailutils/mailbox/hdrfirst.c
similarity index 61%
copy from libproto/pop/pop3_timeout.c
copy to libmailutils/mailbox/hdrfirst.c
index 9e22265..363021f 100644
--- a/libproto/pop/pop3_timeout.c
+++ b/libmailutils/mailbox/hdrfirst.c
@@ -1,5 +1,5 @@
 /* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 2003, 2007, 2010 Free Software Foundation, Inc.
+   Copyright (C) 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
@@ -20,29 +20,29 @@
 #endif
 
 #include <stdlib.h>
-#include <errno.h>
-#include <mailutils/sys/pop3.h>
+#include <mailutils/types.h>
+#include <mailutils/header.h>
+#include <mailutils/errno.h>
 
 int
-mu_pop3_set_timeout (mu_pop3_t pop3, int timeout)
+mu_header_sget_firstof (mu_header_t hdr, char **names,
+			const char **pval, int *pidx)
 {
-  /* Sanity checks.  */
-  if (pop3 == NULL)
-    return EINVAL;
-
-  pop3->timeout = timeout;
-  return 0;
-}
-
-int
-mu_pop3_get_timeout (mu_pop3_t pop3, int *ptimeout)
-{
-  /* Sanity checks.  */
-  if (pop3 == NULL)
-    return EINVAL;
-  if (ptimeout == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-
-  *ptimeout = pop3->timeout;
-  return 0;
+  int status;
+  const char *s = NULL;
+  int i;
+  
+  for (i = 0; names[i]; i++)
+    {
+      status = mu_header_sget_value (hdr, names[i], &s);
+      if (status == 0 && *s != 0)
+	{
+	  if (pval)
+	    *pval = s;
+	  if (pidx)
+	    *pidx = i;
+	  return 0;
+	}
+    }
+  return MU_ERR_NOENT;
 }
diff --git a/libmailutils/mailbox/message.c b/libmailutils/mailbox/message.c
index f415583..b1249bc 100644
--- a/libmailutils/mailbox/message.c
+++ b/libmailutils/mailbox/message.c
@@ -429,52 +429,59 @@ message_envelope_sender (mu_envelope_t envelope, char *buf, size_t len,
 			 size_t *pnwrite)
 {
   mu_message_t msg = mu_envelope_get_owner (envelope);
-  mu_header_t header = NULL;
-  size_t n = 0;
+  mu_header_t header;
   int status;
+  const char *sender;
+  struct mu_auth_data *auth = NULL;
+  static char *hdrnames[] = {
+    "X-Envelope-Sender",
+    "X-Envelope-From",
+    "X-Original-Sender",
+    "From",
+    NULL
+  };
+  mu_address_t address = NULL;
 
   if (msg == NULL)
     return EINVAL;
 
-  /* Can it be extracted from the From:  */
-  mu_message_get_header (msg, &header);
-  status = mu_header_get_value (header, MU_HEADER_FROM, NULL, 0, &n);
-  if (status == 0 && n != 0)
+  /* First, try the header  */
+  status = mu_message_get_header (msg, &header);
+  if (status)
+    return status;
+  status = mu_header_sget_firstof (header, hdrnames, &sender, NULL);
+  if (status)
     {
-      char *sender;
-      mu_address_t address = NULL;
-      sender = calloc (1, n + 1);
-      if (sender == NULL)
-	return ENOMEM;
-      mu_header_get_value (header, MU_HEADER_FROM, sender, n + 1, NULL);
-      if (mu_address_create (&address, sender) == 0)
-	mu_address_get_email (address, 1, buf, n + 1, pnwrite);
-      free (sender);
-      mu_address_destroy (&address);
-      return 0;
+      auth = mu_get_auth_by_uid (getuid ());
+      if (!auth)
+	return MU_ERR_NOENT;
+      sender = auth->name;
     }
-  else if (status == EAGAIN)
-    return status;
 
-  /* oops! We are still here */
-  {
-    struct mu_auth_data *auth = mu_get_auth_by_uid (getuid ());
-    const char *sender = auth ? auth->name : "unknown";
-    n = strlen (sender);
-    if (buf && len > 0)
-      {
-	len--; /* One for the null.  */
-	n = (n < len) ? n : len;
-	memcpy (buf, auth->name, n);
-	buf[n] = '\0';
-      }
-    if (auth)
-      mu_auth_data_free (auth);
-  }
+  status = mu_address_create (&address, sender);
+  if (status == 0)
+    {
+      status = mu_address_sget_email (address, 1, &sender);
+      if (status == 0)
+	{
+	  size_t n = strlen (sender);
+	  if (buf && len > 0)
+	    {
+	      len--; /* One for the null.  */
+	      n = (n < len) ? n : len;
+	      memcpy (buf, sender, n);
+	      buf[n] = '\0';
+	    }
+	  if (pnwrite)
+	    *pnwrite = n;
+	}
+      mu_address_destroy (&address);
+    }
+  
+  if (auth)
+    mu_auth_data_free (auth);
 
-  if (pnwrite)
-    *pnwrite = n;
-  return 0;
+  return status;
 }
 
 
diff --git a/libmailutils/mailer/mailer.c b/libmailutils/mailer/mailer.c
index 6c0ce9a..0ab690d 100644
--- a/libmailutils/mailer/mailer.c
+++ b/libmailutils/mailer/mailer.c
@@ -41,6 +41,7 @@
 #include <mailutils/stream.h>
 #include <mailutils/url.h>
 #include <mailutils/header.h>
+#include <mailutils/envelope.h>
 #include <mailutils/body.h>
 #include <mailutils/mailbox.h>
 #include <mailutils/message.h>
@@ -336,20 +337,27 @@ _set_from (mu_address_t *pfrom, mu_message_t msg, mu_address_t from,
 	   mu_mailer_t mailer)
 {
   int status = 0;
-  char *mail_from;
-  mu_header_t header = NULL;
 
-  *pfrom = NULL;
-  
   /* Get MAIL_FROM from FROM, the message, or the environment. */
   if (!from)
     {
       const char *type;
-      
-      if ((status = mu_message_get_header (msg, &header)) != 0)
+      mu_envelope_t env;
+      const char *mail_from;
+
+      status = mu_message_get_envelope (msg, &env);
+      if (status)
 	return status;
-      
-      status = mu_header_aget_value (header, MU_HEADER_FROM, &mail_from);
+
+      status = mu_envelope_sget_sender (env, &mail_from);
+      if (status)
+	{
+	  mu_header_t header;
+	  status = mu_message_get_header (msg, &header);
+	  if (status)
+	    return status;
+	  status = mu_header_sget_value (header, MU_HEADER_FROM, &mail_from);
+	}
       
       switch (status)
 	{
@@ -361,9 +369,6 @@ _set_from (mu_address_t *pfrom, mu_message_t msg, mu_address_t from,
 	  MU_DEBUG1 (mailer->debug, MU_DEBUG_TRACE,
 		     "mu_mailer_send_message(): using From: %s\n",
 		     mail_from);
-	    
-	  status = mu_address_create (pfrom, mail_from);
-	  free (mail_from);
 	  break;
 
 	case MU_ERR_NOENT:
@@ -379,18 +384,20 @@ _set_from (mu_address_t *pfrom, mu_message_t msg, mu_address_t from,
 		       "mu_mailer_send_message(): using user's address: %s\n",
 		       mail_from);
 	  else
-            MU_DEBUG (mailer->debug, MU_DEBUG_ERROR,
-		      "mu_mailer_send_message(): no user's address, failing\n");
-
-	  if (!mail_from)
-	    return errno;
-
-	  status = mu_address_create (pfrom, mail_from);
+	    {
+	      MU_DEBUG (mailer->debug, MU_DEBUG_ERROR,
+			"mu_mailer_send_message(): "
+			"no user's address, failing\n");
+	      return errno;
+	    }
 	  /* FIXME: should we add the From: header? */
 	  break;
 	}
+      status = mu_address_create (pfrom, mail_from);
     }
-
+  else
+    *pfrom = NULL;
+  
   return status;
 }
 
diff --git a/libproto/mailer/mbox.c b/libproto/mailer/mbox.c
index 635e137..527ca20 100644
--- a/libproto/mailer/mbox.c
+++ b/libproto/mailer/mbox.c
@@ -29,6 +29,10 @@
 #include <mailutils/mailer.h>
 #include <mailutils/url.h>
 #include <mailutils/util.h>
+#include <mailutils/argcv.h>
+#include <mailutils/message.h>
+#include <mailutils/envelope.h>
+#include <mailutils/header.h>
 #include <mailutils/sys/mailbox.h>
 #include <mailutils/sys/mailer.h>
 
@@ -116,6 +120,113 @@ mkaddr (mu_mailbox_t mbox, mu_property_t property,
   return 0;
 }
 
+static int
+parse_received (mu_header_t hdr, char **sptr)
+{
+  const char *recv;
+  int wc, i;
+  char **ws;
+  enum { rcv_init, rcv_from, rcv_by, rcv_for } state;
+  int status;
+  char *s;
+  size_t len;
+  
+  status = mu_header_sget_value (hdr, MU_HEADER_RECEIVED, &recv);
+  if (status)
+    return status;
+  status = mu_argcv_get (recv, NULL, NULL, &wc, &ws);
+  if (status)
+    return status;
+
+  state = rcv_init;
+  for (i = 0; i < wc && state != rcv_for; i++)
+    {
+      switch (state)
+	{
+	case rcv_init:
+	  if (strcmp (ws[i], "from") == 0)
+	    state = rcv_from;
+	  break;
+	  
+	case rcv_from:
+	  if (strcmp (ws[i], "by") == 0)
+	    state = rcv_by;
+	  break;
+	  
+	case rcv_by:
+	  if (strcmp (ws[i], "for") == 0)
+	    state = rcv_for;
+	  break;
+
+	default:
+	  break;
+	}
+    }
+
+  if (state != rcv_for || ws[i] == NULL)
+    return MU_ERR_NOENT;
+
+  s = ws[i];
+  len = strlen (s);
+  if (s[len - 1] == ';')
+    len--;
+  if (s[0] == '<' && s[len - 1] == '>')
+    {
+      s++;
+      len--;
+    }
+  *sptr = malloc (len);
+  if (!*sptr)
+    status = ENOMEM;
+  else
+    {
+      memcpy (*sptr, s, len);
+      (*sptr)[len - 1] = 0;
+    }
+  mu_argcv_free (wc, ws);
+  return status;
+} 
+  
+static int
+guess_message_recipient (mu_message_t msg, char **hdrname, char **pptr)
+{
+  mu_header_t hdr;
+  int status;
+  char *s = NULL;
+  
+  status = mu_message_get_header (msg, &hdr);
+  if (status)
+    return status;
+
+  /* First, try an easy way. */
+  if (hdrname)
+    {
+      int i;
+      for (i = 0; hdrname[i]; i++)
+	{
+	  status = mu_header_aget_value (hdr, hdrname[i], &s);
+	  if (status == 0 && *s != 0)
+	    break;
+	}
+    }
+  else
+    status = MU_ERR_NOENT;
+  
+  if (status == MU_ERR_NOENT)
+    {
+      status = parse_received (hdr, &s);
+      if (status)
+	status = mu_header_aget_value (hdr, MU_HEADER_TO, &s);
+    }
+  
+  if (status)
+    return status;
+  
+  *pptr = s;
+  
+  return 0;
+}
+  
 
 static int
 remote_mbox_append_message (mu_mailbox_t mbox, mu_message_t msg)
@@ -137,9 +248,47 @@ remote_mbox_append_message (mu_mailbox_t mbox, mu_message_t msg)
   mkaddr (mbox, property, "TO", &to);
   if (!to)
     {
-      const char *rcpt;
+      char *rcpt;
       
-      status = mu_url_sget_user (mbox->url, &rcpt);
+      status = mu_url_aget_user (mbox->url, &rcpt);
+      if (status == MU_ERR_NOENT)
+	{
+	  static char *hdrnames[] = {
+	    "X-Envelope-To",
+	    "Delivered-To",
+	    "X-Original-To",
+	    NULL
+	  };
+	  const char *hstr;
+	  int hc; 
+	  char **hv;
+	  
+	  if (mu_url_sget_param (mbox->url, "recipient-headers", &hstr) == 0)
+	    {
+	      if (*hstr == 0)
+		{
+		  hc = 0;
+		  hv = NULL;
+		}
+	      else
+		{
+		  status = mu_argcv_get_np (hstr, strlen (hstr), ",", NULL, 0,
+					    &hc, &hv, NULL);
+		  if (status)
+		    return status;
+		}
+	    }
+	  else
+	    {
+	      hc = 0;
+	      hv = hdrnames;
+	    }
+	  
+	  status = guess_message_recipient (msg, hv, &rcpt);
+	  if (hc)
+	    mu_argcv_free (hc, hv);
+	}
+
       if (status != MU_ERR_NOENT)
 	{
 	  const char *host;
@@ -148,16 +297,27 @@ remote_mbox_append_message (mu_mailbox_t mbox, mu_message_t msg)
 	  if (status)
 	    {
 	      MU_DEBUG1 (mbox->debug, MU_DEBUG_ERROR,
-			 "failed to get recipient from the url: %s\n",
+			 "failed to get recipient: %s\n",
 			 mu_strerror (status));
 	      return status;
 	    }
 
-	  mu_url_sget_host (mbox->url, &host);
+	  /* Get additional parameters */
+	  status = mu_url_sget_param (mbox->url, "strip-domain", NULL);
+	  if (status == 0)
+	    {
+	      char *q = strchr (rcpt, '@');
+	      if (q)
+		*q = 0;
+	    }
+
+	  status = mu_url_sget_param (mbox->url, "domain", &host);
+	  if (!(status == 0 && *host))
+	    mu_url_sget_host (mbox->url, &host);
 	  hint.domain = (char*) host;
 	  status = mu_address_create_hint (&to, rcpt, &hint, 
 	                                   MU_ADDR_HINT_DOMAIN);
-      
+	  free (rcpt);
 	  if (status)
 	    {
 	      MU_DEBUG3 (mbox->debug, MU_DEBUG_ERROR,


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Tue Oct 19 15:21:29 2010
Received: from mailman by lists.gnu.org with archive (Exim 4.43)
	id 1P8HkX-0006ut-6W
	for mharc-commit-mailutils@gnu.org; Tue, 19 Oct 2010 15:21:29 -0400
Received: from [140.186.70.92] (port=35932 helo=eggs.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.43) id 1P8HkT-0006sX-SE
	for commit-mailutils@gnu.org; Tue, 19 Oct 2010 15:21:27 -0400
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P8HkR-0006Jl-Ic
	for commit-mailutils@gnu.org; Tue, 19 Oct 2010 15:21:24 -0400
Received: from colonialone.fsf.org ([140.186.70.51]:60264
	helo=internal.in.savannah.gnu.org)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P8HkR-0006Ja-GI
	for commit-mailutils@gnu.org; Tue, 19 Oct 2010 15:21:23 -0400
Received: from [10.1.0.108] (helo=vcs-noshell.in.savannah.gnu.org)
	by internal.in.savannah.gnu.org with esmtp (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P8HkR-0000hS-62
	for commit-mailutils@gnu.org; Tue, 19 Oct 2010 19:21:23 +0000
Received: from gray by vcs-noshell.in.savannah.gnu.org with local (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P8HkR-0000o6-04
	for commit-mailutils@gnu.org; Tue, 19 Oct 2010 19:21:23 +0000
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: 2f6f7b4fc2230352e8ff742b9999672dec35236b
X-Git-Newrev: f50c5d11f2775f11cb450c97008f9fa4b9e39fa1
Message-Id: <E1P8HkR-0000o6-04@vcs-noshell.in.savannah.gnu.org>
Date: Tue, 19 Oct 2010 19:21:23 +0000
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
Subject: [SCM] GNU Mailutils branch, master,
	updated. release-2.2-151-gf50c5d1
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.5
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=unsubscribe>
List-Archive: <http://lists.gnu.org/archive/html/commit-mailutils>
List-Post: <mailto:commit-mailutils@gnu.org>
List-Help: <mailto:commit-mailutils-request@gnu.org?subject=help>
List-Subscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Tue, 19 Oct 2010 19:21:27 -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=f50c5d11f2775f11cb450c97008f9fa4b9e39fa1

The branch, master has been updated
       via  f50c5d11f2775f11cb450c97008f9fa4b9e39fa1 (commit)
      from  2f6f7b4fc2230352e8ff742b9999672dec35236b (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 f50c5d11f2775f11cb450c97008f9fa4b9e39fa1
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Tue Oct 19 22:13:48 2010 +0300

    Provide an easy way of setting sender/recipient addresses in smtp URLs.
    
    * libmailutils/mailer/mailer.c (_set_from): Parameter from=
    can be used to set the sender address explicitly.
    (_set_to): New function. Parameter to= sets recipient
    addresses explicitly.
    (create_part): Use _set_to.
    * libproto/mailer/mbox.c (remote_mbox_append_message): Use the
    to= parameter.

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

Summary of changes:
 libmailutils/mailer/mailer.c |   71 +++++++++++++++++++++++++++++++++++------
 libproto/mailer/mbox.c       |    2 +-
 2 files changed, 61 insertions(+), 12 deletions(-)

diff --git a/libmailutils/mailer/mailer.c b/libmailutils/mailer/mailer.c
index 0ab690d..59559eb 100644
--- a/libmailutils/mailer/mailer.c
+++ b/libmailutils/mailer/mailer.c
@@ -338,25 +338,33 @@ _set_from (mu_address_t *pfrom, mu_message_t msg, mu_address_t from,
 {
   int status = 0;
 
-  /* Get MAIL_FROM from FROM, the message, or the environment. */
+  *pfrom = NULL;
+  
+  /* Get MAIL_FROM from URL, envelope, headers, or the environment. */
   if (!from)
     {
       const char *type;
       mu_envelope_t env;
       const char *mail_from;
 
-      status = mu_message_get_envelope (msg, &env);
-      if (status)
-	return status;
+      status = mu_url_sget_param (mailer->url, "from", &mail_from);
 
-      status = mu_envelope_sget_sender (env, &mail_from);
       if (status)
 	{
-	  mu_header_t header;
-	  status = mu_message_get_header (msg, &header);
+	  status = mu_message_get_envelope (msg, &env);
 	  if (status)
 	    return status;
-	  status = mu_header_sget_value (header, MU_HEADER_FROM, &mail_from);
+
+	  status = mu_envelope_sget_sender (env, &mail_from);
+	  if (status)
+	    {
+	      mu_header_t header;
+	      status = mu_message_get_header (msg, &header);
+	      if (status)
+		return status;
+	      status = mu_header_sget_value (header, MU_HEADER_FROM,
+					     &mail_from);
+	    }
 	}
       
       switch (status)
@@ -395,13 +403,47 @@ _set_from (mu_address_t *pfrom, mu_message_t msg, mu_address_t from,
 	}
       status = mu_address_create (pfrom, mail_from);
     }
-  else
-    *pfrom = NULL;
   
   return status;
 }
 
 static int
+_set_to (mu_address_t *paddr, mu_message_t msg, mu_address_t to,
+	 mu_mailer_t mailer)
+{
+  int status = 0;
+
+  *paddr = NULL;
+  if (!to)
+    {
+      const char *rcpt_to;
+
+      status = mu_url_sget_param (mailer->url, "to", &rcpt_to);
+      switch (status)
+	{
+	case 0:
+	  break;
+
+	case MU_ERR_NOENT:
+	  /* FIXME: Get it from the message itself, at least if the
+	     mailer is not SENDMAIL. */
+	  return 0;
+
+	default:
+	  return status;
+	}
+      MU_DEBUG1 (mailer->debug, MU_DEBUG_TRACE,
+		 "mu_mailer_send_message(): using RCPT TO: %s\n",
+		 rcpt_to);
+      status = mu_address_create (paddr, rcpt_to);
+    }
+  
+  return status;
+}
+  
+
+  
+static int
 create_part (mu_mime_t mime, mu_stream_t istr, 
 	     size_t fragsize, size_t n, size_t nparts, char *msgid)
 {
@@ -536,7 +578,7 @@ mu_mailer_send_fragments (mu_mailer_t mailer,
 			  mu_address_t from, mu_address_t to)
 {
   int status;
-  mu_address_t sender_addr = NULL;
+  mu_address_t sender_addr = NULL, rcpt_addr = NULL;
   
   if (mailer == NULL)
     return EINVAL;
@@ -548,6 +590,12 @@ mu_mailer_send_fragments (mu_mailer_t mailer,
     return status;
   if (sender_addr)
     from = sender_addr;
+
+  status = _set_to (&rcpt_addr, msg, from, mailer);
+  if (status)
+    return status;
+  if (rcpt_addr)
+    to = rcpt_addr;
   
   if ((!from || (status = mu_mailer_check_from (from)) == 0)
       && (!to || (status = mu_mailer_check_to (to)) == 0))
@@ -589,6 +637,7 @@ mu_mailer_send_fragments (mu_mailer_t mailer,
 	}
     }
   mu_address_destroy (&sender_addr);
+  mu_address_destroy (&rcpt_addr);
   return status;
 }
 
diff --git a/libproto/mailer/mbox.c b/libproto/mailer/mbox.c
index 527ca20..1353d42 100644
--- a/libproto/mailer/mbox.c
+++ b/libproto/mailer/mbox.c
@@ -250,7 +250,7 @@ remote_mbox_append_message (mu_mailbox_t mbox, mu_message_t msg)
     {
       char *rcpt;
       
-      status = mu_url_aget_user (mbox->url, &rcpt);
+      status = mu_url_aget_param (mbox->url, "to", &rcpt);
       if (status == MU_ERR_NOENT)
 	{
 	  static char *hdrnames[] = {


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Wed Oct 20 15:15:42 2010
Received: from mailman by lists.gnu.org with archive (Exim 4.43)
	id 1P8e8U-0000Fg-5M
	for mharc-commit-mailutils@gnu.org; Wed, 20 Oct 2010 15:15:42 -0400
Received: from [140.186.70.92] (port=45081 helo=eggs.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.43) id 1P8e8P-0000F5-MM
	for commit-mailutils@gnu.org; Wed, 20 Oct 2010 15:15:40 -0400
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P8e8N-0002x4-0Y
	for commit-mailutils@gnu.org; Wed, 20 Oct 2010 15:15:37 -0400
Received: from colonialone.fsf.org ([140.186.70.51]:43108
	helo=internal.in.savannah.gnu.org)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P8e8M-0002wr-UY
	for commit-mailutils@gnu.org; Wed, 20 Oct 2010 15:15:34 -0400
Received: from [10.1.0.108] (helo=vcs-noshell.in.savannah.gnu.org)
	by internal.in.savannah.gnu.org with esmtp (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P8e8M-0001IE-LO
	for commit-mailutils@gnu.org; Wed, 20 Oct 2010 19:15:34 +0000
Received: from gray by vcs-noshell.in.savannah.gnu.org with local (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P8e8M-0003mm-2g
	for commit-mailutils@gnu.org; Wed, 20 Oct 2010 19:15:34 +0000
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: f50c5d11f2775f11cb450c97008f9fa4b9e39fa1
X-Git-Newrev: 1e409dd4c3369bc70cf2353935a610314228fa65
Message-Id: <E1P8e8M-0003mm-2g@vcs-noshell.in.savannah.gnu.org>
Date: Wed, 20 Oct 2010 19:15:34 +0000
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
Subject: [SCM] GNU Mailutils branch, master,
	updated. release-2.2-152-g1e409dd
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.5
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=unsubscribe>
List-Archive: <http://lists.gnu.org/archive/html/commit-mailutils>
List-Post: <mailto:commit-mailutils@gnu.org>
List-Help: <mailto:commit-mailutils-request@gnu.org?subject=help>
List-Subscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Wed, 20 Oct 2010 19:15:40 -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=1e409dd4c3369bc70cf2353935a610314228fa65

The branch, master has been updated
       via  1e409dd4c3369bc70cf2353935a610314228fa65 (commit)
      from  f50c5d11f2775f11cb450c97008f9fa4b9e39fa1 (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 1e409dd4c3369bc70cf2353935a610314228fa65
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Wed Oct 20 22:13:00 2010 +0300

    Improve registrar/url interaction.  Fix SMTP URLs to comply to draft-earhart-url-smtp-00.  Minor fix in maidag (url mode).
    
    * include/mailutils/registrar.h (MU_RECORD_DEFAULT)
    (MU_RECORD_LOCAL): New flags.
    (_mu_record) <flags,url_may_have,url_must_have>: New members.
    Remove unnecessary externs.
    (mu_record_set_.*): Remove all protos.
    (mu_record_check_url, mu_registrar_test_local_url): New protos.
    * include/mailutils/sys/url.h (_mu_url) <flags>: New member.
    (MU_URL_USER, MU_URL_SECRET, MU_URL_AUTH)
    (MU_URL_HOST, MU_URL_PORT, MU_URL_PATH)
    (MU_URL_PARAM, MU_URL_QUERY)
    (MU_URL_CRED, MU_URL_INET, MU_URL_ALL): New flags.
    (mu_url_get_flags, mu_url_has_flag): New protos.
    * libmailutils/base/registrar.c (mu_record_set_.*): Remove all.
    (mu_record_check_url): New function.
    (mu_registrar_test_local_url): New function.
    * libmailutils/base/url.c (mu_url_copy0): Copy flags.
    (mu_url_parse): Use flags intead of inspecting each
    structure member. Set flags.
    (mu_url_get_flags, mu_url_has_flag): New functions.
    * libmailutils/diag/errors (MU_ERR_URL_MISS_PARTS)
    (MU_ERR_URL_EXTRA_PARTS): New error codes.
    * libmailutils/mailbox/folder.c (mu_folder_create_from_record): Check
    the URL using mu_record_check_url.
    * libproto/imap/folder.c (_imap_record, _imaps_record): Initialize new fields.
    * libproto/maildir/folder.c (_maildir_record): Likewise.
    * libproto/mbox/folder.c (_mbox_record): Likewise.
    * libproto/mh/folder.c (_mh_record): Likewise.
    * libproto/nntp/folder.c (_nntp_record): Likewise.
    * libproto/pop/folder.c (_pop_record, _pops_record): Likewise.
    * libproto/mailer/prog.c (_prog_record): Likewise.
    (_url_prog_init): Remove extra checks, rely on mu_record_check_url.
    * libproto/mailer/remote.c (_mu_remote_smtp_record)
    (_mu_remote_sendmail_record,_mu_remote_prog_record): Initialize new fields.
    * libproto/mailer/sendmail.c (_url_sendmail_init): Remove extra checks, rely
    on mu_record_check_url.
    (_sendmail_record): Initialize new fields.
    * libproto/nntp/url.c (url_nntp_destroy): Remove.
    (_nntp_url_init): Remove extra checks.
    * libproto/pop/url.c (url_pop_destroy): Remove.
    (_url_pop_init, _url_pops_init): Remove extra checks.
    
    * libproto/mailer/smtp.c (_url_smtp_init): Remove extra checks.
    (_smtp_record): Initialize new fields.
    (smtp_mailer_add_auth_mech): New function.
    (smtp_open): Allow for auth= part before the host name.
    
    * maidag/deliver.c (is_remote_url): New function.
    (do_delivery): Do not try to switch user privileges if the
    URL refers to a remote mailbox.
    * maidag/maidag.c (main): Initialize TLS.

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

Summary of changes:
 include/mailutils/registrar.h |   68 ++++++++++-----------
 include/mailutils/sys/url.h   |    1 +
 include/mailutils/url.h       |   23 +++++++
 libmailutils/base/registrar.c |  134 ++++++++++++++---------------------------
 libmailutils/base/url.c       |   57 +++++++++++++----
 libmailutils/diag/errors      |    3 +
 libmailutils/mailbox/folder.c |    7 ++-
 libproto/imap/folder.c        |    6 ++
 libproto/maildir/folder.c     |    3 +
 libproto/mailer/prog.c        |   10 ++--
 libproto/mailer/remote.c      |    9 +++
 libproto/mailer/sendmail.c    |   11 ++--
 libproto/mailer/smtp.c        |   52 ++++++++--------
 libproto/mbox/folder.c        |    3 +
 libproto/mh/folder.c          |    3 +
 libproto/nntp/folder.c        |    3 +
 libproto/nntp/url.c           |   24 -------
 libproto/pop/folder.c         |    6 ++
 libproto/pop/url.c            |   21 -------
 maidag/deliver.c              |   14 ++++-
 maidag/maidag.c               |    3 +
 21 files changed, 238 insertions(+), 223 deletions(-)

diff --git a/include/mailutils/registrar.h b/include/mailutils/registrar.h
index 2042966..cf77925 100644
--- a/include/mailutils/registrar.h
+++ b/include/mailutils/registrar.h
@@ -25,11 +25,21 @@
 extern "C" {
 #endif
 
+#define MU_RECORD_DEFAULT 0
+#define MU_RECORD_LOCAL   0x0001
+  /* This record represents a "local entity", e.g. a UNIX or MH mailbox */
+
+  
 /* Public Interface, to allow static initialization.  */
 struct _mu_record
 {
   int priority;    /* Higher priority records are scanned first */
   const char *scheme;
+  int flags;       /* MU_RECORD_ flags */
+  int url_may_have; /* MU_URL_ flags (see url.h) describing what an URL
+		       for this record may have */
+  int url_must_have;/* MU_URL_ flags telling what such an URL must have */
+  
   int (*_url) (mu_url_t);
   int (*_mailbox) (mu_mailbox_t);
   int (*_mailer) (mu_mailer_t);
@@ -47,54 +57,40 @@ struct _mu_record
 };
 
 /* Defaults */  
-extern int mu_registrar_set_default_scheme (const char *scheme);
-extern const char *mu_registrar_get_default_scheme (void);
-extern int mu_registrar_get_default_record (mu_record_t *prec);
-extern void mu_registrar_set_default_record (mu_record_t record);
+int mu_registrar_set_default_scheme (const char *scheme);
+const char *mu_registrar_get_default_scheme (void);
+int mu_registrar_get_default_record (mu_record_t *prec);
+void mu_registrar_set_default_record (mu_record_t record);
   
 /* Registration.  */
-extern int mu_registrar_get_iterator (mu_iterator_t *);
-extern int mu_registrar_get_list (mu_list_t *) __attribute__ ((deprecated));
+int mu_registrar_get_iterator (mu_iterator_t *);
+int mu_registrar_get_list (mu_list_t *) __attribute__ ((deprecated));
 
-extern int mu_registrar_lookup_scheme (const char *scheme,
+int mu_registrar_lookup_scheme (const char *scheme,
 				       mu_record_t *precord);
-  
-extern int mu_registrar_lookup (const char *name, int flags, 
-                                mu_record_t *precord, int *pflags);
-extern int mu_registrar_lookup_url (mu_url_t url, int flags,
+
+int mu_registrar_lookup (const char *name, int flags, 
+                         mu_record_t *precord, int *pflags);
+int mu_registrar_lookup_url (mu_url_t url, int flags,
 				    mu_record_t *precord, int *pflags);
-extern int mu_registrar_record       (mu_record_t);
-extern int mu_unregistrar_record     (mu_record_t);
+int mu_registrar_record       (mu_record_t);
+int mu_unregistrar_record     (mu_record_t);
 
 /* Scheme.  */
-extern int mu_record_is_scheme       (mu_record_t, mu_url_t, int flags);
-extern int mu_record_set_scheme      (mu_record_t, const char *);
-extern int mu_record_set_is_scheme   (mu_record_t, 
-                      int (*_is_scheme) (mu_record_t, mu_url_t, int));
+int mu_record_is_scheme       (mu_record_t, mu_url_t, int flags);
 
 /* Url.  */
-extern int mu_record_get_url         (mu_record_t, int (*(*)) (mu_url_t));
-extern int mu_record_set_url         (mu_record_t, int (*) (mu_url_t));
-extern int mu_record_set_get_url     (mu_record_t, 
-                      int (*_get_url) (mu_record_t, int (*(*)) (mu_url_t)));
+int mu_record_get_url         (mu_record_t, int (*(*)) (mu_url_t));
+int mu_record_check_url (mu_record_t record, mu_url_t url, int *pmask);
+int mu_registrar_test_local_url (mu_url_t url, int *pres);
 /*  Mailbox. */
-extern int mu_record_get_mailbox     (mu_record_t, int (*(*)) (mu_mailbox_t));
-extern int mu_record_set_mailbox     (mu_record_t, int (*) (mu_mailbox_t));
-extern int mu_record_set_get_mailbox (mu_record_t, 
-                 int (*_get_mailbox) (mu_record_t, int (*(*)) (mu_mailbox_t)));
+int mu_record_get_mailbox     (mu_record_t, int (*(*)) (mu_mailbox_t));
 /* Mailer.  */
-extern int mu_record_get_mailer      (mu_record_t,
-				   int (*(*)) (mu_mailer_t));
-extern int mu_record_set_mailer      (mu_record_t, int (*) (mu_mailer_t));
-extern int mu_record_set_get_mailer  (mu_record_t, 
-        int (*_get_mailer) (mu_record_t, int (*(*)) (mu_mailer_t)));
+int mu_record_get_mailer      (mu_record_t, int (*(*)) (mu_mailer_t));
 /* Folder.  */
-extern int mu_record_get_folder      (mu_record_t, int (*(*)) (mu_folder_t));
-extern int mu_record_set_folder      (mu_record_t, int (*) (mu_folder_t));
-extern int mu_record_set_get_folder  (mu_record_t, 
-         int (*_get_folder) (mu_record_t, int (*(*)) (mu_folder_t)));
-
-extern int mu_record_list_p (mu_record_t record, const char *name, int);
+int mu_record_get_folder      (mu_record_t, int (*(*)) (mu_folder_t));
+  
+int mu_record_list_p (mu_record_t record, const char *name, int);
   
 /* Records provided by the library.  */
 
diff --git a/include/mailutils/sys/url.h b/include/mailutils/sys/url.h
index 095a111..f782bb0 100644
--- a/include/mailutils/sys/url.h
+++ b/include/mailutils/sys/url.h
@@ -27,6 +27,7 @@ extern "C" {
 
 struct _mu_url
 {
+  int flags;  /* See MU_URL_ flags in ../url.h */
   /* Data */
   char  *name;
   char  *scheme;
diff --git a/include/mailutils/url.h b/include/mailutils/url.h
index 449fbc4..231432d 100644
--- a/include/mailutils/url.h
+++ b/include/mailutils/url.h
@@ -25,10 +25,33 @@
 extern "C" {
 #endif
 
+#define MU_URL_USER   0x0001 /* Has a user part */
+#define MU_URL_SECRET 0x0002 /* Has a secret (password) part */
+#define MU_URL_AUTH   0x0004 /* Has auth part */
+#define MU_URL_HOST   0x0008 /* Has host part */
+#define MU_URL_PORT   0x0010 /* Has port part */  
+#define MU_URL_PATH   0x0020 /* Has path */
+#define MU_URL_PARAM  0x0040 /* Has parameters */
+#define MU_URL_QUERY  0x0080 /* Has query */
+  
+#define MU_URL_CRED (MU_URL_USER | MU_URL_SECRET | MU_URL_AUTH)  
+  /* Has some of authentication credentials */
+#define MU_URL_INET (MU_URL_HOST | MU_URL_PORT)
+  /* Has Inet address */
+#define MU_URL_ALL \
+  (MU_URL_CRED | \
+   MU_URL_HOST | \
+   MU_URL_PATH | \
+   MU_URL_PARAM | \
+   MU_URL_QUERY)
+
 int  mu_url_create    (mu_url_t *, const char *name);
 int  mu_url_dup       (mu_url_t old_url, mu_url_t *new_url);
 int  mu_url_uplevel   (mu_url_t url, mu_url_t *upurl);
 
+int mu_url_get_flags (mu_url_t, int *);
+int mu_url_has_flag (mu_url_t, int);  
+  
 void mu_url_destroy   (mu_url_t *);
 int  mu_url_parse     (mu_url_t);
 
diff --git a/libmailutils/base/registrar.c b/libmailutils/base/registrar.c
index d24aac2..da43bff 100644
--- a/libmailutils/base/registrar.c
+++ b/libmailutils/base/registrar.c
@@ -281,25 +281,6 @@ mu_record_is_scheme (mu_record_t record, mu_url_t url, int flags)
 }
 
 int
-mu_record_set_scheme (mu_record_t record, const char *scheme)
-{
-  if (record == NULL)
-    return EINVAL;
-  record->scheme = scheme;
-  return 0;
-}
-
-int
-mu_record_set_is_scheme (mu_record_t record,
-			 int (*_is_scheme) (mu_record_t, mu_url_t, int))
-{
-  if (record == NULL)
-    return EINVAL;
-  record->_is_scheme = _is_scheme;
-  return 0;
-}
-
-int
 mu_record_get_url (mu_record_t record, int (*(*_purl)) (mu_url_t))
 {
   if (record == NULL)
@@ -314,25 +295,6 @@ mu_record_get_url (mu_record_t record, int (*(*_purl)) (mu_url_t))
 }
 
 int
-mu_record_set_url (mu_record_t record, int (*_mu_url) (mu_url_t))
-{
-  if (record == NULL)
-    return EINVAL;
-  record->_url = _mu_url;
-  return 0;
-}
-
-int
-mu_record_set_get_url (mu_record_t record, int (*_get_url)
-		    (mu_record_t, int (*(*)) (mu_url_t)))
-{
-  if (record == NULL)
-    return EINVAL;
-  record->_get_url = _get_url;
-  return 0;
-}
-
-int
 mu_record_get_mailbox (mu_record_t record, int (*(*_pmailbox)) (mu_mailbox_t))
 {
   if (record == NULL)
@@ -347,25 +309,6 @@ mu_record_get_mailbox (mu_record_t record, int (*(*_pmailbox)) (mu_mailbox_t))
 }
 
 int
-mu_record_set_mailbox (mu_record_t record, int (*_mu_mailbox) (mu_mailbox_t))
-{
-  if (record)
-    return EINVAL;
-  record->_mailbox = _mu_mailbox;
-  return 0;
-}
-
-int
-mu_record_set_get_mailbox (mu_record_t record, 
-     int (*_get_mailbox) (mu_record_t, int (*(*)) (mu_mailbox_t)))
-{
-  if (record)
-    return EINVAL;
-  record->_get_mailbox = _get_mailbox;
-  return 0;
-}
-
-int
 mu_record_get_mailer (mu_record_t record, int (*(*_pmailer)) (mu_mailer_t))
 {
   if (record == NULL)
@@ -380,25 +323,6 @@ mu_record_get_mailer (mu_record_t record, int (*(*_pmailer)) (mu_mailer_t))
 }
 
 int
-mu_record_set_mailer (mu_record_t record, int (*_mu_mailer) (mu_mailer_t))
-{
-  if (record)
-    return EINVAL;
-  record->_mailer = _mu_mailer;
-  return 0;
-}
-
-int
-mu_record_set_get_mailer (mu_record_t record, 
-  int (*_get_mailer) (mu_record_t, int (*(*)) (mu_mailer_t)))
-{
-  if (record == NULL)
-    return EINVAL;
-  record->_get_mailer = _get_mailer;
-  return 0;
-}
-
-int
 mu_record_get_folder (mu_record_t record, int (*(*_pfolder)) (mu_folder_t))
 {
   if (record == NULL)
@@ -413,30 +337,64 @@ mu_record_get_folder (mu_record_t record, int (*(*_pfolder)) (mu_folder_t))
 }
 
 int
-mu_record_set_folder (mu_record_t record, int (*_mu_folder) (mu_folder_t))
+mu_record_list_p (mu_record_t record, const char *name, int flags)
 {
   if (record == NULL)
     return EINVAL;
-  record->_folder = _mu_folder;
-  return 0;
+  return record == NULL
+          || !record->_list_p
+          || record->_list_p (record, name, flags);
 }
 
 int
-mu_record_set_get_folder (mu_record_t record, 
-   int (*_get_folder) (mu_record_t, int (*(*)) (mu_folder_t)))
+mu_record_check_url (mu_record_t record, mu_url_t url, int *pmask)
 {
-  if (record == NULL)
+  int mask;
+  int flags;
+  int rc;
+  
+  if (!record || !url)
     return EINVAL;
-  record->_get_folder = _get_folder;
+
+  rc = mu_url_get_flags (url, &flags);
+  if (rc)
+    return rc;
+
+  mask = flags & record->url_must_have;
+  if (mask != record->url_must_have)
+    {
+      if (pmask)
+	*pmask = record->url_must_have & ~mask;
+      return MU_ERR_URL_MISS_PARTS;
+    }
+  mask = flags & ~(record->url_may_have | record->url_must_have);
+  if (mask)
+    {
+      if (pmask)
+	*pmask = mask;
+      return MU_ERR_URL_EXTRA_PARTS;
+    }
   return 0;
 }
 
+/* Test if URL corresponds to a local record.  
+   Return:
+     0            -  OK, the result is stored in *pres;
+     MU_ERR_NOENT -  don't know: there's no matching record;
+     EINVAL       -  some of the arguments is not valid;
+     other        -  URL lookup failed.
+*/
 int
-mu_record_list_p (mu_record_t record, const char *name, int flags)
+mu_registrar_test_local_url (mu_url_t url, int *pres)
 {
-  if (record == NULL)
+  int rc;
+  mu_record_t rec;
+
+  if (!url || !pres)
     return EINVAL;
-  return record == NULL
-          || !record->_list_p
-          || record->_list_p (record, name, flags);
+  rc = mu_registrar_lookup_url (url, MU_FOLDER_ATTRIBUTE_ALL, &rec, NULL);
+  if (rc)
+    return rc;
+  *pres = rec->flags & MU_RECORD_LOCAL;
+  return 0;
 }
diff --git a/libmailutils/base/url.c b/libmailutils/base/url.c
index 82b9f3f..9b4e102 100644
--- a/libmailutils/base/url.c
+++ b/libmailutils/base/url.c
@@ -176,6 +176,7 @@ mu_url_copy0 (mu_url_t old_url, mu_url_t new_url)
 	return ENOMEM;
       new_url->qargc = argc;
     }
+  new_url->flags = old_url->flags;
   return 0;
 #undef URLCOPY
 }
@@ -292,8 +293,7 @@ mu_url_parse (mu_url_t url)
 
   memset (&u, 0, sizeof u);
   /* can't have been parsed already */
-  if (url->scheme || url->user || url->secret || url->auth ||
-      url->host || url->path || url->qargc)
+  if (url->flags)
     return EINVAL;
 
   n = strdup (url->name);
@@ -331,21 +331,25 @@ mu_url_parse (mu_url_t url)
 	 though.
        */
 
-#define UALLOC(X)							\
-      if (u.X && u.X[0] &&						\
-	  !(url->X = (want_decode ? mu_url_decode (u.X) : strdup (u.X)))) \
+#define UALLOC(X,f)							\
+      if (u.X && u.X[0])						\
 	{								\
-	  err = ENOMEM;							\
-	  goto CLEANUP;							\
+	  url->X = want_decode ? mu_url_decode (u.X) : strdup (u.X);	\
+	  if (!url->X)							\
+	    {								\
+	      err = ENOMEM;						\
+	      goto CLEANUP;						\
+	    }								\
+	  url->flags |= f;						\
 	}								\
       else								\
 	{								\
 	  /* Set zero-length strings to NULL. */			\
-	  u.X = NULL;							\
+	  url->X = NULL;						\
 	}
 
-      UALLOC (scheme);
-      UALLOC (user);
+      UALLOC (scheme, 0);
+      UALLOC (user, MU_URL_USER);
 
       if (u.secret)
 	{
@@ -357,20 +361,27 @@ mu_url_parse (mu_url_t url)
 	    goto CLEANUP;
 
 	  url->secret = newsec;
+	  url->flags |= MU_URL_SECRET;
 	}
 
-      UALLOC (auth);
-      UALLOC (host);
-      UALLOC (path);
+      UALLOC (auth, MU_URL_AUTH);
+      UALLOC (host, MU_URL_HOST);
+      UALLOC (path, MU_URL_PATH);
 
 #undef UALLOC
       url->fvcount = u.fvcount;
       url->fvpairs = u.fvpairs;
-
+      if (u.fvcount)
+	url->flags |= MU_URL_PARAM;
+	  
       url->qargc = u.qargc;
       url->qargv = u.qargv;
+      if (u.qargc)
+	url->flags |= MU_URL_QUERY;
 
       url->port = u.port;
+      if (u.port)
+	url->flags |= MU_URL_PORT;
     }
 
 CLEANUP:
@@ -1189,3 +1200,21 @@ mu_url_expand_path (mu_url_t url)
 
   return 0;
 }
+
+int
+mu_url_get_flags (mu_url_t url, int *pf)
+{
+  if (!url || !pf)
+    return EINVAL;
+  *pf = url->flags;
+  return 0;
+}
+
+int
+mu_url_has_flag (mu_url_t url, int flags)
+{
+  if (!url)
+    return 0;
+  return url->flags & flags;
+}
+
diff --git a/libmailutils/diag/errors b/libmailutils/diag/errors
index 5f82f7b..3807524 100644
--- a/libmailutils/diag/errors
+++ b/libmailutils/diag/errors
@@ -92,3 +92,6 @@ MU_ERR_READ                 _("Read error")
 
 MU_ERR_NO_TRANSPORT         _("Transport stream not set")
 MU_ERR_AUTH_NO_CRED         _("No credentials supplied")
+
+MU_ERR_URL_MISS_PARTS       _("URL missing required parts")
+MU_ERR_URL_EXTRA_PARTS      _("URL has parts not allowed by its scheme")
diff --git a/libmailutils/mailbox/folder.c b/libmailutils/mailbox/folder.c
index 2b85601..f6e75c0 100644
--- a/libmailutils/mailbox/folder.c
+++ b/libmailutils/mailbox/folder.c
@@ -76,10 +76,15 @@ mu_folder_create_from_record (mu_folder_t *pfolder, mu_url_t url,
       mu_record_get_folder (record, &f_init);
       if (f_init)
         {
-	  int status;
+	  int status, mask;
 	  mu_folder_t folder;
 	  int (*u_init) (mu_url_t) = NULL;
 
+	  status = mu_record_check_url (record, url, &mask);
+	  if (status)
+	    /* FIXME: mask would provide more info */
+	    return status;
+	  
 	  mu_record_get_url (record, &u_init);
 	  if (u_init)
 	    {
diff --git a/libproto/imap/folder.c b/libproto/imap/folder.c
index c95c930..ed5ba54 100644
--- a/libproto/imap/folder.c
+++ b/libproto/imap/folder.c
@@ -66,6 +66,9 @@ static struct _mu_record _imap_record =
 {
   MU_IMAP_PRIO,
   MU_IMAP_SCHEME,
+  MU_RECORD_DEFAULT,
+  MU_URL_CRED | MU_URL_INET | MU_URL_PATH,
+  MU_URL_HOST,
   _url_imap_init,     /* url entry.  */
   _mailbox_imap_init, /* Mailbox entry.  */
   NULL,               /* Mailer entry.  */
@@ -87,6 +90,9 @@ static struct _mu_record _imaps_record =
 {
   MU_IMAP_PRIO,
   MU_IMAPS_SCHEME,
+  MU_RECORD_DEFAULT,
+  MU_URL_CRED | MU_URL_INET | MU_URL_PATH | MU_URL_PARAM,
+  MU_URL_HOST,
   _url_imaps_init,     /* url entry.  */
   _mailbox_imaps_init, /* Mailbox entry.  */
   NULL,                /* Mailer entry.  */
diff --git a/libproto/maildir/folder.c b/libproto/maildir/folder.c
index 4fef07b..657dfa5 100644
--- a/libproto/maildir/folder.c
+++ b/libproto/maildir/folder.c
@@ -98,6 +98,9 @@ static struct _mu_record _maildir_record =
 {
   MU_MAILDIR_PRIO,
   MU_MAILDIR_SCHEME,
+  MU_RECORD_LOCAL,
+  MU_URL_PATH,
+  MU_URL_PATH,
   mu_url_expand_path, /* Url init.  */
   _mailbox_maildir_init, /* Mailbox init.  */
   NULL, /* Mailer init.  */
diff --git a/libproto/mailer/prog.c b/libproto/mailer/prog.c
index 216869c..d026177 100644
--- a/libproto/mailer/prog.c
+++ b/libproto/mailer/prog.c
@@ -44,6 +44,11 @@ static struct _mu_record _prog_record =
 {
   MU_PROG_PRIO,
   MU_PROG_SCHEME,
+  MU_RECORD_DEFAULT,
+  /* FIXME: MU_URL_USER could be used to request running with this
+     user privileges. */
+  MU_URL_PATH | MU_URL_QUERY, 
+  MU_URL_PATH,
   _url_prog_init,    /* url init.  */
   _mu_mailer_mailbox_init,  /* Mailbox entry.  */
   _mu_mailer_prog_init, /* Mailer entry.  */
@@ -68,11 +73,6 @@ _url_prog_uplevel (const mu_url_t orig, mu_url_t *up)
 static int
 _url_prog_init (mu_url_t url)
 {
-  /* not valid in a prog url */
-  if (url->secret || url->auth || url->host || url->port)
-    return EINVAL;
-  if (!url->path)
-    return EINVAL;
   url->_uplevel = _url_prog_uplevel;
   return 0;
 }
diff --git a/libproto/mailer/remote.c b/libproto/mailer/remote.c
index 1781687..ae96cee 100644
--- a/libproto/mailer/remote.c
+++ b/libproto/mailer/remote.c
@@ -75,6 +75,9 @@ _url_remote_smtp_init (mu_url_t url)
 static struct _mu_record _mu_remote_smtp_record = {
   MU_SMTP_PRIO,
   "remote+smtp",
+  MU_RECORD_DEFAULT,
+  MU_URL_CRED | MU_URL_INET | MU_URL_PATH | MU_URL_PARAM,
+  MU_URL_HOST,
   _url_remote_smtp_init,	/* url init.  */
   _mu_mailer_mailbox_init,      /* Mailbox init.  */
   NULL,		                /* Mailer init.  */
@@ -104,6 +107,9 @@ static struct _mu_record _mu_remote_sendmail_record =
 {
   MU_SENDMAIL_PRIO,
   "remote+sendmail",
+  MU_RECORD_DEFAULT,
+  MU_URL_PATH,
+  MU_URL_PATH,
   _url_remote_sendmail_init,    /* url init.  */
   _mu_mailer_mailbox_init,      /* Mailbox entry.  */
   _mu_mailer_sendmail_init, /* Mailer entry.  */
@@ -130,6 +136,9 @@ static struct _mu_record _mu_remote_prog_record =
 {
   MU_PROG_PRIO,
   "remote+prog",
+  MU_RECORD_DEFAULT,
+  MU_URL_CRED | MU_URL_PATH | MU_URL_QUERY,
+  MU_URL_PATH,
   _url_remote_prog_init,    /* url init.  */
   _mu_mailer_mailbox_init,  /* Mailbox entry.  */
   _mu_mailer_prog_init, /* Mailer entry.  */
diff --git a/libproto/mailer/sendmail.c b/libproto/mailer/sendmail.c
index 0a991a3..f1ba068 100644
--- a/libproto/mailer/sendmail.c
+++ b/libproto/mailer/sendmail.c
@@ -53,12 +53,7 @@ static int sendmail_send_message (mu_mailer_t, mu_message_t, mu_address_t,
 static int
 _url_sendmail_init (mu_url_t url)
 {
-  /* not valid in a sendmail url */
-  if (url->user || url->secret || url->auth || url->qargc
-      || url->host || url->port)
-    return EINVAL;
-
-  if (url->path == 0)
+  if (url->path == NULL)
     if ((url->path = strdup (PATH_SENDMAIL)) == 0)
       return ENOMEM;
 
@@ -275,6 +270,10 @@ static struct _mu_record _sendmail_record =
 {
   MU_SENDMAIL_PRIO,
   MU_SENDMAIL_SCHEME,
+  MU_RECORD_DEFAULT,
+  MU_URL_PATH,
+  0, /* Nothing is required in the URL, except scheme. Missing path means
+	using PATH_SENDMAIL. */
   _url_sendmail_init,    /* url init.  */
   _mu_mailer_mailbox_init,     /* Mailbox entry.  */
   _mu_mailer_sendmail_init, /* Mailer entry.  */
diff --git a/libproto/mailer/smtp.c b/libproto/mailer/smtp.c
index 72e7c7c..cfcad0e 100644
--- a/libproto/mailer/smtp.c
+++ b/libproto/mailer/smtp.c
@@ -56,26 +56,17 @@ static int      _mailer_smtp_init (mu_mailer_t);
 static int
 _url_smtp_init (mu_url_t url)
 {
-  /* host isn't optional */
-  if (!url->host)
-    return EINVAL;
-
-  /* accept url->user, pass, and auth
-     for the ESMTP authentication */
-
-  /* all other fields must be NULL */
-  if (url->path || url->qargc)
-    return EINVAL;
-
   if (url->port == 0)
     url->port = MU_SMTP_PORT;
-
   return 0;
 }
 
 static struct _mu_record _smtp_record = {
   MU_SMTP_PRIO,
   MU_SMTP_SCHEME,
+  MU_RECORD_DEFAULT,
+  MU_URL_CRED | MU_URL_INET | MU_URL_PARAM,
+  MU_URL_HOST,
   _url_smtp_init,		/* url init.  */
   _mu_mailer_mailbox_init,	/* Mailbox init.  */
   _mailer_smtp_init,		/* Mailer init.  */
@@ -101,10 +92,27 @@ struct _smtp_mailer
   mu_address_t    rcpt_bcc;
 };
 
+static void
+smtp_mailer_add_auth_mech (struct _smtp_mailer *smtp_mailer, const char *str)
+{
+  int mc, i, rc;
+  char **mv;
+  
+  rc = mu_argcv_get_np (str, strlen (str),
+			",", NULL,
+			0,
+			&mc, &mv, NULL);
+  if (rc == 0)
+    for (i = 0; i < mc; i++)
+      mu_smtp_add_auth_mech (smtp_mailer->smtp, mv[i]);
+  
+  free (mv);
+}
+
 static int
 smtp_open (mu_mailer_t mailer, int flags)
 {
-  const char *host;
+  const char *host, *auth;
   long port;
   struct _smtp_mailer *smtp_mailer = mailer->data;
   int rc;
@@ -134,6 +142,9 @@ smtp_open (mu_mailer_t mailer, int flags)
   if (mu_url_get_port (mailer->url, &port))
     port = 25;
 
+  if (mu_url_sget_auth (mailer->url, &auth) == 0)
+    smtp_mailer_add_auth_mech (smtp_mailer, auth);
+  
   /* Additional information is supplied in the arguments */
   if (mu_url_sget_fvpairs (mailer->url, &parmc, &parmv) == 0)
     {
@@ -146,20 +157,7 @@ smtp_open (mu_mailer_t mailer, int flags)
 	  else if (strcmp (parmv[i], "noauth") == 0)
 	    noauth = 1;
 	  else if (strncmp (parmv[i], "auth=", 5) == 0)
-	    {
-	      int mc, j;
-	      char **mv;
-	  
-	      rc = mu_argcv_get_np (parmv[i] + 5, strlen (parmv[i] + 5),
-				    ",", NULL,
-				    0,
-				    &mc, &mv, NULL);
-	      if (rc == 0)
-		for (j = 0; j < mc; j++)
-		  mu_smtp_add_auth_mech (smtp_mailer->smtp, mv[j]);
-	      
-	      free (mv);
-	    }
+	    smtp_mailer_add_auth_mech (smtp_mailer, parmv[i] + 5);
 	  /* unrecognized arguments silently ignored */
 	}
     }
diff --git a/libproto/mbox/folder.c b/libproto/mbox/folder.c
index 7697825..c6a9d51 100644
--- a/libproto/mbox/folder.c
+++ b/libproto/mbox/folder.c
@@ -100,6 +100,9 @@ static struct _mu_record _mbox_record =
 {
   MU_MBOX_PRIO,
   MU_MBOX_SCHEME,
+  MU_RECORD_LOCAL,
+  MU_URL_PATH,
+  MU_URL_PATH,
   mu_url_expand_path, /* URL init.  */
   _mailbox_mbox_init, /* Mailbox init.  */
   NULL, /* Mailer init.  */
diff --git a/libproto/mh/folder.c b/libproto/mh/folder.c
index 1743ca0..f5feb1f 100644
--- a/libproto/mh/folder.c
+++ b/libproto/mh/folder.c
@@ -138,6 +138,9 @@ static struct _mu_record _mh_record =
 {
   MU_MH_PRIO,
   MU_MH_SCHEME,
+  MU_RECORD_LOCAL,
+  MU_URL_PATH,
+  MU_URL_PATH,
   mu_url_expand_path, /* Url init.  */
   _mailbox_mh_init, /* Mailbox init.  */
   NULL, /* Mailer init.  */
diff --git a/libproto/nntp/folder.c b/libproto/nntp/folder.c
index 4573559..071ffae 100644
--- a/libproto/nntp/folder.c
+++ b/libproto/nntp/folder.c
@@ -45,6 +45,9 @@ static struct _mu_record _nntp_record =
 {
   MU_NNTP_PRIO,
   MU_NNTP_URL_SCHEME,
+  MU_RECORD_DEFAULT,
+  MU_URL_CRED | MU_URL_INET | MU_URL_PATH,
+  MU_URL_HOST,
   _nntp_url_init, /* Url init.  */
   _nntp_mailbox_init, /* Mailbox init.  */
   NULL, /* Mailer init.  */
diff --git a/libproto/nntp/url.c b/libproto/nntp/url.c
index 5354750..3870107 100644
--- a/libproto/nntp/url.c
+++ b/libproto/nntp/url.c
@@ -33,13 +33,6 @@
 
 #include <mailutils/sys/url.h>
 
-static void url_nntp_destroy (mu_url_t url);
-
-static void
-url_nntp_destroy (mu_url_t url MU_ARG_UNUSED)
-{
-}
-
 /*
   POP URL:
   nntp://<host>:<port>/<newsgroup-name>/<article-number>
@@ -48,23 +41,6 @@ url_nntp_destroy (mu_url_t url MU_ARG_UNUSED)
 int
 _nntp_url_init (mu_url_t url)
 {
-  int status = 0;
-
-  url->_destroy = url_nntp_destroy;
-
-  status = mu_url_parse(url);
-
-  if(status)
-    return status;
-
-  /* is it nntp? */
-  if (strcmp (MU_NNTP_URL_SCHEME, url->scheme) != 0)
-    return EINVAL;
-
-  /* not valid in a nntp url */
-  if (!url->host || !url->path)
-    return EINVAL;
-
   if (url->port == 0)
     url->port = MU_NNTP_DEFAULT_PORT;
 
diff --git a/libproto/pop/folder.c b/libproto/pop/folder.c
index 82f8197..ec945f9 100644
--- a/libproto/pop/folder.c
+++ b/libproto/pop/folder.c
@@ -47,6 +47,9 @@ static struct _mu_record _pop_record =
 {
   MU_POP_PRIO,
   MU_POP_SCHEME,
+  MU_RECORD_DEFAULT,
+  MU_URL_CRED | MU_URL_INET,
+  MU_URL_HOST,
   _url_pop_init, /* Url init.  */
   _mailbox_pop_init, /* Mailbox init.  */
   NULL, /* Mailer init.  */
@@ -65,6 +68,9 @@ static struct _mu_record _pops_record =
 {
   MU_POP_PRIO,
   MU_POPS_SCHEME,
+  MU_RECORD_DEFAULT,
+  MU_URL_CRED | MU_URL_INET,
+  MU_URL_HOST,
   _url_pops_init, /* Url init.  */
   _mailbox_pops_init, /* Mailbox init.  */
   NULL, /* Mailer init.  */
diff --git a/libproto/pop/url.c b/libproto/pop/url.c
index 86992b6..f528064 100644
--- a/libproto/pop/url.c
+++ b/libproto/pop/url.c
@@ -33,13 +33,6 @@
 #include <mailutils/sys/url.h>
 #include <mailutils/sys/registrar.h>
 
-static void url_pop_destroy (mu_url_t url);
-
-static void
-url_pop_destroy (mu_url_t url MU_ARG_UNUSED)
-{
-}
-
 /*
   POP URLs:
     pop://[<user>[;AUTH=<auth>]@]<host>[:<port>]
@@ -51,13 +44,6 @@ _url_pop_init (mu_url_t url)
 {
   if (url->port == 0)
     url->port = MU_POP_PORT;
-  
-  url->_destroy = url_pop_destroy;
-
-  /* not valid in pop url */
-  if (url->path || url->qargc || !url->host)
-    return EINVAL;
-
   return 0;
 }
 
@@ -72,13 +58,6 @@ _url_pops_init (mu_url_t url)
 {
   if (url->port == 0)
     url->port = MU_POPS_PORT;
-
-  url->_destroy = url_pop_destroy;
-
-  /* not valid in pops url */
-  if (url->path || url->qargc || !url->host)
-    return EINVAL;
-
   return 0;
 }
 
diff --git a/maidag/deliver.c b/maidag/deliver.c
index 57684b9..5a65259 100644
--- a/maidag/deliver.c
+++ b/maidag/deliver.c
@@ -314,13 +314,25 @@ deliver_to_mailbox (mu_mailbox_t mbox, mu_message_t msg,
 }
 
 static int
+is_remote_url (mu_url_t url)
+{
+  int rc, res;
+
+  if (!url)
+    return 0;
+  
+  rc = mu_registrar_test_local_url (url, &res);
+  return rc == 0 && res == 0;
+}
+
+static int
 do_delivery (mu_url_t url, mu_message_t msg, const char *name, char **errp)
 {
   struct mu_auth_data *auth = NULL;
   mu_mailbox_t mbox;
   int status;
 
-  if (name)
+  if (name && !is_remote_url (url))
     {
       auth = mu_get_auth_by_name (name);
       if (!auth)
diff --git a/maidag/maidag.c b/maidag/maidag.c
index f67b581..b588ca9 100644
--- a/maidag/maidag.c
+++ b/maidag/maidag.c
@@ -564,6 +564,9 @@ main (int argc, char *argv[])
   maidag_cfg_init ();
   
   /* Parse command line */
+#ifdef WITH_TLS
+  mu_gocs_register ("tls", mu_tls_module_init);
+#endif
   mu_argp_init (NULL, NULL);
 
   mu_m_server_create (&server, program_version);


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Thu Oct 21 07:18:05 2010
Received: from mailman by lists.gnu.org with archive (Exim 4.43)
	id 1P8t9p-0005Am-If
	for mharc-commit-mailutils@gnu.org; Thu, 21 Oct 2010 07:18:05 -0400
Received: from [140.186.70.92] (port=50269 helo=eggs.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.43) id 1P8t9m-00059f-U1
	for commit-mailutils@gnu.org; Thu, 21 Oct 2010 07:18:03 -0400
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P8t9k-0005rw-UL
	for commit-mailutils@gnu.org; Thu, 21 Oct 2010 07:18:02 -0400
Received: from colonialone.fsf.org ([140.186.70.51]:50213
	helo=internal.in.savannah.gnu.org)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P8t9k-0005rs-Sj
	for commit-mailutils@gnu.org; Thu, 21 Oct 2010 07:18:00 -0400
Received: from [10.1.0.108] (helo=vcs-noshell.in.savannah.gnu.org)
	by internal.in.savannah.gnu.org with esmtp (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P8t9k-0004U8-KL
	for commit-mailutils@gnu.org; Thu, 21 Oct 2010 11:18:00 +0000
Received: from gray by vcs-noshell.in.savannah.gnu.org with local (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P8t9j-0002Na-Nv
	for commit-mailutils@gnu.org; Thu, 21 Oct 2010 11:18:00 +0000
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: 1e409dd4c3369bc70cf2353935a610314228fa65
X-Git-Newrev: d9cd8883ffbad01eb69cda7ae8332038b467aaf5
Message-Id: <E1P8t9j-0002Na-Nv@vcs-noshell.in.savannah.gnu.org>
Date: Thu, 21 Oct 2010 11:17:59 +0000
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
Subject: [SCM] GNU Mailutils branch, master,
	updated. release-2.2-153-gd9cd888
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.5
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=unsubscribe>
List-Archive: <http://lists.gnu.org/archive/html/commit-mailutils>
List-Post: <mailto:commit-mailutils@gnu.org>
List-Help: <mailto:commit-mailutils-request@gnu.org?subject=help>
List-Subscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Thu, 21 Oct 2010 11:18:04 -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=d9cd8883ffbad01eb69cda7ae8332038b467aaf5

The branch, master has been updated
       via  d9cd8883ffbad01eb69cda7ae8332038b467aaf5 (commit)
      from  1e409dd4c3369bc70cf2353935a610314228fa65 (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 d9cd8883ffbad01eb69cda7ae8332038b467aaf5
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Thu Oct 21 14:16:58 2010 +0300

    Allow for `notls' parameter in pop URLs.
    
    * libproto/pop/folder.c (_pop_record): Accept URL parameters.
    * libproto/pop/mbox.c (pop_open): Don't start TLS if
    the `notls' parameter is present in the URL.

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

Summary of changes:
 libproto/pop/folder.c |    2 +-
 libproto/pop/mbox.c   |    1 +
 2 files changed, 2 insertions(+), 1 deletions(-)

diff --git a/libproto/pop/folder.c b/libproto/pop/folder.c
index ec945f9..59ad1cc 100644
--- a/libproto/pop/folder.c
+++ b/libproto/pop/folder.c
@@ -48,7 +48,7 @@ static struct _mu_record _pop_record =
   MU_POP_PRIO,
   MU_POP_SCHEME,
   MU_RECORD_DEFAULT,
-  MU_URL_CRED | MU_URL_INET,
+  MU_URL_CRED | MU_URL_INET | MU_URL_PARAM,
   MU_URL_HOST,
   _url_pop_init, /* Url init.  */
   _mailbox_pop_init, /* Mailbox init.  */
diff --git a/libproto/pop/mbox.c b/libproto/pop/mbox.c
index 2c406de..6865f6f 100644
--- a/libproto/pop/mbox.c
+++ b/libproto/pop/mbox.c
@@ -169,6 +169,7 @@ pop_open (mu_mailbox_t mbox, int flags)
 	break;
 
       if (WITH_TLS && !mpd->pops &&
+	  mu_url_sget_param (mbox->url, "notls", NULL) == MU_ERR_NOENT &&
 	  mu_pop3_capa_test (mpd->pop3, "STLS", NULL) == 0)
 	{
 	  status = mu_pop3_stls (mpd->pop3);


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Thu Oct 21 18:45:04 2010
Received: from mailman by lists.gnu.org with archive (Exim 4.43)
	id 1P93se-0007kR-CN
	for mharc-commit-mailutils@gnu.org; Thu, 21 Oct 2010 18:45:04 -0400
Received: from [140.186.70.92] (port=35047 helo=eggs.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.43) id 1P93sc-0007kL-3U
	for commit-mailutils@gnu.org; Thu, 21 Oct 2010 18:45:03 -0400
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P93sa-0001TQ-Ca
	for commit-mailutils@gnu.org; Thu, 21 Oct 2010 18:45:01 -0400
Received: from colonialone.fsf.org ([140.186.70.51]:42296
	helo=internal.in.savannah.gnu.org)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P93sa-0001TK-9T
	for commit-mailutils@gnu.org; Thu, 21 Oct 2010 18:45:00 -0400
Received: from [10.1.0.108] (helo=vcs-noshell.in.savannah.gnu.org)
	by internal.in.savannah.gnu.org with esmtp (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P93sZ-0004Y8-NN
	for commit-mailutils@gnu.org; Thu, 21 Oct 2010 22:44:59 +0000
Received: from gray by vcs-noshell.in.savannah.gnu.org with local (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P93sY-0000O2-Cp
	for commit-mailutils@gnu.org; Thu, 21 Oct 2010 22:44:59 +0000
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: d9cd8883ffbad01eb69cda7ae8332038b467aaf5
X-Git-Newrev: d992b1b4f5d731a3e11a5caaffd22cfd068e5477
Message-Id: <E1P93sY-0000O2-Cp@vcs-noshell.in.savannah.gnu.org>
Date: Thu, 21 Oct 2010 22:44:58 +0000
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
Subject: [SCM] GNU Mailutils branch, master,
	updated. release-2.2-154-gd992b1b
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.5
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=unsubscribe>
List-Archive: <http://lists.gnu.org/archive/html/commit-mailutils>
List-Post: <mailto:commit-mailutils@gnu.org>
List-Help: <mailto:commit-mailutils-request@gnu.org?subject=help>
List-Subscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Thu, 21 Oct 2010 22:45:03 -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=d992b1b4f5d731a3e11a5caaffd22cfd068e5477

The branch, master has been updated
       via  d992b1b4f5d731a3e11a5caaffd22cfd068e5477 (commit)
      from  d9cd8883ffbad01eb69cda7ae8332038b467aaf5 (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 d992b1b4f5d731a3e11a5caaffd22cfd068e5477
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Fri Oct 22 01:36:19 2010 +0300

    Improve SMTP client.
    
    * include/mailutils/smtp.h (mu_smtp_dot): New proto.
    * libproto/mailer/smtp_dot.c: New file.
    * libproto/mailer/Makefile.am (libmu_mailer_la_SOURCES): Add
    smtp_dot.c.
    * libproto/mailer/smtp.c (smtp_send_message): Send RSET after an
    unexpected SMTP reply.
    Call mu_smtp_dot after successful sending.
    * libproto/mailer/smtp_data.c (_mu_smtp_data_end): Set
    state to MU_SMTP_DOT.
    * libproto/mailer/smtp_rset.c (mu_smtp_rset): Improve state
    selection algorithm.
    * libproto/mailer/smtp_send.c (mu_smtp_send_stream): Don't
    reset state: it is done by _mu_smtp_data_end.
    * testsuite/smtpsend.c (main): Call mu_smtp_dot after sending
    message.

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

Summary of changes:
 include/mailutils/smtp.h                    |    1 +
 libproto/mailer/Makefile.am                 |    1 +
 libproto/mailer/smtp.c                      |   47 +++++++++++++++++++-------
 libproto/mailer/smtp_data.c                 |    1 +
 libproto/mailer/{smtp_rset.c => smtp_dot.c} |   12 +++---
 libproto/mailer/smtp_rset.c                 |   17 +++++++++-
 libproto/mailer/smtp_send.c                 |    2 -
 testsuite/smtpsend.c                        |    2 +-
 8 files changed, 60 insertions(+), 23 deletions(-)
 copy libproto/mailer/{smtp_rset.c => smtp_dot.c} (85%)

diff --git a/include/mailutils/smtp.h b/include/mailutils/smtp.h
index 0d692ff..1eda800 100644
--- a/include/mailutils/smtp.h
+++ b/include/mailutils/smtp.h
@@ -65,6 +65,7 @@ int mu_smtp_rcpt_basic (mu_smtp_t smtp, const char *email,
 
 int mu_smtp_data (mu_smtp_t smtp, mu_stream_t *pstream);
 int mu_smtp_send_stream (mu_smtp_t smtp, mu_stream_t str);
+int mu_smtp_dot (mu_smtp_t smtp);
 int mu_smtp_rset (mu_smtp_t smtp);
 int mu_smtp_quit (mu_smtp_t smtp);
 
diff --git a/libproto/mailer/Makefile.am b/libproto/mailer/Makefile.am
index 156dc1f..cb7ab32 100644
--- a/libproto/mailer/Makefile.am
+++ b/libproto/mailer/Makefile.am
@@ -36,6 +36,7 @@ libmu_mailer_la_SOURCES = \
  smtp_create.c\
  smtp_data.c\
  smtp_disconnect.c\
+ smtp_dot.c\
  smtp_ehlo.c\
  $(SMTP_GSASL)\
  smtp_io.c\
diff --git a/libproto/mailer/smtp.c b/libproto/mailer/smtp.c
index cfcad0e..5b48fce 100644
--- a/libproto/mailer/smtp.c
+++ b/libproto/mailer/smtp.c
@@ -375,8 +375,8 @@ static int
 smtp_send_message (mu_mailer_t mailer, mu_message_t msg,
 		   mu_address_t argfrom, mu_address_t argto)
 {
-  struct _smtp_mailer *smp = mailer->data;
-  mu_smtp_t smtp = smp->smtp;
+  struct _smtp_mailer *smp;
+  mu_smtp_t smtp;
   int status;
   size_t size, lines, count;
   const char *mail_from, *size_str;
@@ -388,7 +388,8 @@ smtp_send_message (mu_mailer_t mailer, mu_message_t msg,
   smp = mailer->data;
   if (!smp)
     return EINVAL;
-
+  smtp = smp->smtp;
+  
   if ((status = mu_message_get_header (msg, &header)))
     return status;
   
@@ -400,7 +401,7 @@ smtp_send_message (mu_mailer_t mailer, mu_message_t msg,
   if (status)
     return status;
   
-  if (mu_smtp_capa_test (smp->smtp, "SIZE", &size_str) == 0 &&
+  if (mu_smtp_capa_test (smtp, "SIZE", &size_str) == 0 &&
       mu_message_size (msg, &size) == 0 &&
       mu_message_lines (msg, &lines) == 0)
     {
@@ -409,21 +410,33 @@ smtp_send_message (mu_mailer_t mailer, mu_message_t msg,
 
       if (msgsize && msgsize > maxsize)
 	return EFBIG;
-      status = mu_smtp_mail_basic (smp->smtp, mail_from,
+      status = mu_smtp_mail_basic (smtp, mail_from,
 				   "SIZE=%lu",
 				   (unsigned long) msgsize);
     }
   else
-    status = mu_smtp_mail_basic (smp->smtp, mail_from, NULL);
+    status = mu_smtp_mail_basic (smtp, mail_from, NULL);
   if (status)
-    return status;
+    {
+      if (status == MU_ERR_REPLY)
+	mu_smtp_rset (smtp);
+      return status;
+    }
 
   status = _rcpt_to_addr (smtp, smp->rcpt_to, &count);
   if (status && count == 0)
-    return status;
+    {
+      if (status == MU_ERR_REPLY)
+	mu_smtp_rset (smtp);
+      return status;
+    }
   status = _rcpt_to_addr (smtp, smp->rcpt_bcc, &count);
   if (status && count == 0)
-    return status;
+    {
+      if (status == MU_ERR_REPLY)
+	mu_smtp_rset (smtp);
+      return status;
+    }
 
   if (mu_header_sget_value (header, MU_HEADER_BCC, NULL))
     {
@@ -433,7 +446,11 @@ smtp_send_message (mu_mailer_t mailer, mu_message_t msg,
 
       status = mu_smtp_data (smtp, &ostr);
       if (status)
-	return status;
+	{
+	  if (status == MU_ERR_REPLY)
+	    mu_smtp_rset (smtp);
+	  return status;
+	}
       mu_header_get_iterator (header, &itr);
       for (mu_iterator_first (itr); !mu_iterator_is_done (itr);
 	   mu_iterator_next (itr))
@@ -451,7 +468,7 @@ smtp_send_message (mu_mailer_t mailer, mu_message_t msg,
       
       mu_message_get_body (msg, &body);
       mu_body_get_streamref (body, &bstr);
-      mu_stream_copy (ostr, bstr, 0, NULL);
+      status = mu_stream_copy (ostr, bstr, 0, NULL);
       mu_stream_destroy (&bstr);
       mu_stream_close (ostr);
       mu_stream_destroy (&ostr);
@@ -463,13 +480,17 @@ smtp_send_message (mu_mailer_t mailer, mu_message_t msg,
       status = mu_smtp_send_stream (smtp, str);
       mu_stream_destroy (&str);
     }
-  mu_smtp_quit (smtp);
   mu_address_destroy (&smp->rcpt_to);
   mu_address_destroy (&smp->rcpt_bcc);
+  if (status == 0)
+    {
+      status = mu_smtp_dot (smtp);
+      if (status == MU_ERR_REPLY)
+	mu_smtp_rset (smtp);
+    }
   return status;
 }
 
-
 static int
 _mailer_smtp_init (mu_mailer_t mailer)
 {
diff --git a/libproto/mailer/smtp_data.c b/libproto/mailer/smtp_data.c
index e96fa80..efe5242 100644
--- a/libproto/mailer/smtp_data.c
+++ b/libproto/mailer/smtp_data.c
@@ -76,6 +76,7 @@ _mu_smtp_data_end (mu_smtp_t smtp)
 			mu_strerror (status));
     }
   _mu_smtp_xscript_level (smtp, MU_XSCRIPT_NORMAL);
+  smtp->state = MU_SMTP_DOT;
   return status;
 }
 
diff --git a/libproto/mailer/smtp_rset.c b/libproto/mailer/smtp_dot.c
similarity index 85%
copy from libproto/mailer/smtp_rset.c
copy to libproto/mailer/smtp_dot.c
index 7a0a2e0..8c15697 100644
--- a/libproto/mailer/smtp_rset.c
+++ b/libproto/mailer/smtp_dot.c
@@ -22,14 +22,14 @@
 #include <stdlib.h>
 #include <string.h>
 #include <mailutils/errno.h>
-#include <mailutils/filter.h>
+#include <mailutils/cctype.h>
 #include <mailutils/list.h>
+#include <mailutils/util.h>
 #include <mailutils/smtp.h>
-#include <mailutils/stream.h>
 #include <mailutils/sys/smtp.h>
 
 int
-mu_smtp_rset (mu_smtp_t smtp)
+mu_smtp_dot (mu_smtp_t smtp)
 {
   int status;
   
@@ -37,12 +37,12 @@ mu_smtp_rset (mu_smtp_t smtp)
     return EINVAL;
   if (MU_SMTP_FISSET (smtp, _MU_SMTP_ERR))
     return MU_ERR_FAILURE;
-  status = mu_smtp_write (smtp, "RSET\r\n");
-  MU_SMTP_CHECK_ERROR (smtp, status);
+  if (smtp->state != MU_SMTP_DOT)
+    return MU_ERR_SEQ;
   status = mu_smtp_response (smtp);
   MU_SMTP_CHECK_ERROR (smtp, status);
   if (smtp->replcode[0] != '2')
     return MU_ERR_REPLY;
-  smtp->state = MU_SMTP_EHLO;
+  smtp->state = MU_SMTP_MAIL; /* FIXME: Force _EHLO perhaps? */
   return 0;
 }
diff --git a/libproto/mailer/smtp_rset.c b/libproto/mailer/smtp_rset.c
index 7a0a2e0..e051c7a 100644
--- a/libproto/mailer/smtp_rset.c
+++ b/libproto/mailer/smtp_rset.c
@@ -43,6 +43,21 @@ mu_smtp_rset (mu_smtp_t smtp)
   MU_SMTP_CHECK_ERROR (smtp, status);
   if (smtp->replcode[0] != '2')
     return MU_ERR_REPLY;
-  smtp->state = MU_SMTP_EHLO;
+  
+  switch (smtp->state)
+    {
+    case MU_SMTP_INIT:
+    case MU_SMTP_EHLO:
+    case MU_SMTP_DOT:
+      /* RFC 2821, 4.1.1.5 RESET (RSET):
+	[RSET] is effectively equivalent to a NOOP (i.e., if has no effect)
+	if issued immediately after EHLO, before EHLO is issued in the
+	session, after an end-of-data indicator has been sent and
+	acknowledged, or immediately before a QUIT */
+      break;
+      
+    default:
+      smtp->state = MU_SMTP_MAIL;
+    }
   return 0;
 }
diff --git a/libproto/mailer/smtp_send.c b/libproto/mailer/smtp_send.c
index 104fa12..b492573 100644
--- a/libproto/mailer/smtp_send.c
+++ b/libproto/mailer/smtp_send.c
@@ -62,7 +62,5 @@ mu_smtp_send_stream (mu_smtp_t smtp, mu_stream_t stream)
   
   status = _smtp_data_send (smtp, input);
   mu_stream_destroy (&input);
-  if (status == 0)
-    smtp->state = MU_SMTP_DOT;
   return status;
 }
diff --git a/testsuite/smtpsend.c b/testsuite/smtpsend.c
index c93dcc0..a716c3d 100644
--- a/testsuite/smtpsend.c
+++ b/testsuite/smtpsend.c
@@ -289,7 +289,7 @@ main (int argc, char **argv)
       mu_stream_close (ostr);
       mu_stream_destroy (&ostr);
     }
-  
+  MU_ASSERT (mu_smtp_dot (smtp));
   MU_ASSERT (mu_smtp_quit (smtp));
   
   mu_smtp_destroy (&smtp);


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Sun Oct 24 07:45:17 2010
Received: from mailman by lists.gnu.org with archive (Exim 4.43)
	id 1P9z0n-000244-PB
	for mharc-commit-mailutils@gnu.org; Sun, 24 Oct 2010 07:45:17 -0400
Received: from [140.186.70.92] (port=32933 helo=eggs.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.43) id 1P9z0j-00023n-O5
	for commit-mailutils@gnu.org; Sun, 24 Oct 2010 07:45:15 -0400
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P9z0h-0003L3-HL
	for commit-mailutils@gnu.org; Sun, 24 Oct 2010 07:45:13 -0400
Received: from colonialone.fsf.org ([140.186.70.51]:56792
	helo=internal.in.savannah.gnu.org)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P9z0h-0003Kz-Dy
	for commit-mailutils@gnu.org; Sun, 24 Oct 2010 07:45:11 -0400
Received: from [10.1.0.108] (helo=vcs-noshell.in.savannah.gnu.org)
	by internal.in.savannah.gnu.org with esmtp (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P9z0g-00073f-OI
	for commit-mailutils@gnu.org; Sun, 24 Oct 2010 11:45:11 +0000
Received: from gray by vcs-noshell.in.savannah.gnu.org with local (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1P9z0d-0003sE-Vn
	for commit-mailutils@gnu.org; Sun, 24 Oct 2010 11:45:10 +0000
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: d992b1b4f5d731a3e11a5caaffd22cfd068e5477
X-Git-Newrev: be1380d7aa53c031a237b5e993187733eab3fa40
Message-Id: <E1P9z0d-0003sE-Vn@vcs-noshell.in.savannah.gnu.org>
Date: Sun, 24 Oct 2010 11:45:08 +0000
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
Subject: [SCM] GNU Mailutils branch, master,
	updated. release-2.2-155-gbe1380d
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.5
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=unsubscribe>
List-Archive: <http://lists.gnu.org/archive/html/commit-mailutils>
List-Post: <mailto:commit-mailutils@gnu.org>
List-Help: <mailto:commit-mailutils-request@gnu.org?subject=help>
List-Subscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Sun, 24 Oct 2010 11:45:16 -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=be1380d7aa53c031a237b5e993187733eab3fa40

The branch, master has been updated
       via  be1380d7aa53c031a237b5e993187733eab3fa40 (commit)
      from  d992b1b4f5d731a3e11a5caaffd22cfd068e5477 (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 be1380d7aa53c031a237b5e993187733eab3fa40
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Sun Oct 24 14:26:08 2010 +0300

    Improve the algorithm for URL look up in wickets.
    
    The new algorithm always returns the best match, i.e. the
    one with the smallest number of wildcard comparisons.
    
    * include/mailutils/auth.h (mu_wicket_stream_match_url)
    (mu_wicket_file_match_url): New protos.
    Remove unnecessary 'extern' qualifiers.
    * include/mailutils/url.h (mu_url_is_ticket): Remove.
    (mu_url_matches_ticket): New proto.
    * libmailutils/base/url.c (mu_url_parse): Fix name corruption during
    password obfuscation.
    (defined): Remove static function.
    (mu_url_is_ticket): Remove function.
    (mu_url_matches_ticket): New function.
    * libmailutils/base/wicket.c (get_ticket_url): Remove.
    (mu_wicket_stream_match_url)
    (mu_wicket_file_match_url): New functions.
    (file_ticket_get_cred): Use mu_wicket_file_match_url.
    
    * libmailutils/tests/wicket.c: New file.
    * libmailutils/tests/Wicketfile: New file.
    * libmailutils/tests/wicket.at: New file.
    * libmailutils/tests/Makefile.am (noinst_PROGRAMS): Add wicket.
    (EXTRA_DIST): Add Wicketfile
    (TESTSUITE_AT): Add wicket.at
    * libmailutils/tests/testsuite.at: Include wicket.at
    * libmailutils/tests/.gitignore: Update.

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

Summary of changes:
 include/mailutils/auth.h        |   92 +++++++++++++++++---------------
 include/mailutils/url.h         |    2 +-
 libmailutils/base/url.c         |   87 ++++++++++++++++++++----------
 libmailutils/base/wicket.c      |   89 +++++++++++++++++--------------
 libmailutils/tests/.gitignore   |    1 +
 libmailutils/tests/Makefile.am  |    8 ++-
 libmailutils/tests/Wicketfile   |    7 +++
 libmailutils/tests/testsuite.at |    1 +
 libmailutils/tests/wicket.at    |   52 ++++++++++++++++++
 libmailutils/tests/wicket.c     |  111 +++++++++++++++++++++++++++++++++++++++
 10 files changed, 336 insertions(+), 114 deletions(-)
 create mode 100644 libmailutils/tests/Wicketfile
 create mode 100644 libmailutils/tests/wicket.at
 create mode 100644 libmailutils/tests/wicket.c

diff --git a/include/mailutils/auth.h b/include/mailutils/auth.h
index 72bfa51..b3b7be3 100644
--- a/include/mailutils/auth.h
+++ b/include/mailutils/auth.h
@@ -25,56 +25,64 @@
 extern "C" {
 #endif
 
-extern int  mu_ticket_create          (mu_ticket_t *, void *);
-extern int mu_ticket_ref              (mu_ticket_t);
-extern int mu_ticket_unref            (mu_ticket_t);
-extern void mu_ticket_destroy         (mu_ticket_t *);
-extern int  mu_ticket_set_destroy     (mu_ticket_t,
-				       void (*) (mu_ticket_t), void *);
-extern void *mu_ticket_get_owner      (mu_ticket_t);
-
-extern int mu_ticket_get_cred         (mu_ticket_t ticket,
-				       mu_url_t url, const char *challenge,
-				       char **pplain, mu_secret_t *psec);
-
-extern int mu_ticket_set_get_cred     (mu_ticket_t,
-				       int  (*) (mu_ticket_t, mu_url_t,
-						 const char *,
-						 char **, mu_secret_t *),
-				       void *);
-
-extern int mu_ticket_set_data         (mu_ticket_t, void *, void *owner);
-extern void *mu_ticket_get_data       (mu_ticket_t);
-
-extern int mu_ticket_set_secret (mu_ticket_t ticket, mu_secret_t secret);
+int  mu_ticket_create          (mu_ticket_t *, void *);
+int mu_ticket_ref              (mu_ticket_t);
+int mu_ticket_unref            (mu_ticket_t);
+void mu_ticket_destroy         (mu_ticket_t *);
+int  mu_ticket_set_destroy     (mu_ticket_t,
+				void (*) (mu_ticket_t), void *);
+void *mu_ticket_get_owner      (mu_ticket_t);
+
+int mu_ticket_get_cred         (mu_ticket_t ticket,
+				mu_url_t url, const char *challenge,
+				char **pplain, mu_secret_t *psec);
+
+int mu_ticket_set_get_cred     (mu_ticket_t,
+				int  (*) (mu_ticket_t, mu_url_t,
+					  const char *,
+					  char **, mu_secret_t *),
+				void *);
+
+int mu_ticket_set_data         (mu_ticket_t, void *, void *owner);
+void *mu_ticket_get_data       (mu_ticket_t);
+
+int mu_ticket_set_secret (mu_ticket_t ticket, mu_secret_t secret);
 int mu_ticket_set_plain (mu_ticket_t ticket, const char *text);
 
-extern int mu_authority_create           (mu_authority_t *, mu_ticket_t, void *);
-extern void mu_authority_destroy         (mu_authority_t *, void *);
-extern void *mu_authority_get_owner      (mu_authority_t);
-extern int mu_authority_set_ticket       (mu_authority_t, mu_ticket_t);
-extern int mu_authority_get_ticket       (mu_authority_t, mu_ticket_t *);
-extern int mu_authority_authenticate     (mu_authority_t);
-extern int mu_authority_set_authenticate (mu_authority_t,
-					    int (*_authenticate) (mu_authority_t), void *);
+int mu_authority_create           (mu_authority_t *, mu_ticket_t, void *);
+void mu_authority_destroy         (mu_authority_t *, void *);
+void *mu_authority_get_owner      (mu_authority_t);
+int mu_authority_set_ticket       (mu_authority_t, mu_ticket_t);
+int mu_authority_get_ticket       (mu_authority_t, mu_ticket_t *);
+int mu_authority_authenticate     (mu_authority_t);
+int mu_authority_set_authenticate (mu_authority_t,
+				   int (*_authenticate) (mu_authority_t),
+				   void *);
 
-extern int mu_authority_create_null      (mu_authority_t *pauthority, void *owner);
+int mu_authority_create_null      (mu_authority_t *pauthority, void *owner);
 
-extern int mu_wicket_create       (mu_wicket_t *);
-extern int mu_wicket_get_ticket (mu_wicket_t wicket, const char *user,
+int mu_wicket_create       (mu_wicket_t *);
+int mu_wicket_get_ticket (mu_wicket_t wicket, const char *user,
 				 mu_ticket_t *pticket);
-extern int mu_wicket_ref (mu_wicket_t wicket);
-extern int mu_wicket_unref (mu_wicket_t wicket);
-extern void mu_wicket_destroy (mu_wicket_t *pwicket);
-extern int mu_wicket_set_destroy (mu_wicket_t wicket,
+int mu_wicket_ref (mu_wicket_t wicket);
+int mu_wicket_unref (mu_wicket_t wicket);
+void mu_wicket_destroy (mu_wicket_t *pwicket);
+int mu_wicket_set_destroy (mu_wicket_t wicket,
 				  void (*_destroy) (mu_wicket_t));
-extern int mu_wicket_set_data (mu_wicket_t wicket, void *data);
-extern void *mu_wicket_get_data (mu_wicket_t wicket);
-extern int mu_wicket_set_get_ticket (mu_wicket_t wicket,
+int mu_wicket_set_data (mu_wicket_t wicket, void *data);
+void *mu_wicket_get_data (mu_wicket_t wicket);
+int mu_wicket_set_get_ticket (mu_wicket_t wicket,
 			      int (*_get_ticket) (mu_wicket_t, void *,
-						  const char *, mu_ticket_t *));
-extern int mu_file_wicket_create (mu_wicket_t *pwicket, const char *filename);
+						 const char *, mu_ticket_t *));
+int mu_file_wicket_create (mu_wicket_t *pwicket, const char *filename);
 
+struct mu_debug_locus;  
+int mu_wicket_stream_match_url (mu_stream_t stream, struct mu_debug_locus *loc,
+				mu_url_t url, mu_url_t *pticket_url);
+int mu_wicket_file_match_url (const char *name, mu_url_t url,
+			      mu_url_t *pticket_url);
+
+  
 #ifdef __cplusplus
 }
 #endif
diff --git a/include/mailutils/url.h b/include/mailutils/url.h
index 231432d..b8745bb 100644
--- a/include/mailutils/url.h
+++ b/include/mailutils/url.h
@@ -104,7 +104,7 @@ int mu_url_is_same_port   (mu_url_t, mu_url_t);
 char *mu_url_decode_len (const char *s, size_t len);  
 char *mu_url_decode     (const char *s);
 
-int mu_url_is_ticket   (mu_url_t ticket, mu_url_t url);
+int mu_url_matches_ticket   (mu_url_t ticket, mu_url_t url, int *wcn);  
 int mu_url_init (mu_url_t url, int port, const char *scheme);
   
 #ifdef __cplusplus
diff --git a/libmailutils/base/url.c b/libmailutils/base/url.c
index 9b4e102..49aab4b 100644
--- a/libmailutils/base/url.c
+++ b/libmailutils/base/url.c
@@ -316,13 +316,18 @@ mu_url_parse (mu_url_t url)
 	  char *newname;
 
 	  memset (url->name + pstart, 0, plen);
-	  newname = realloc (url->name, len);
-	  if (!newname)
-	    goto CLEANUP;
+	  if (len > nlen + 1)
+	    {
+	      newname = realloc (url->name, len);
+	      if (!newname)
+		goto CLEANUP;
+	      url->name = newname;
+	    }
+	  else
+	    newname = url->name;
 	  memmove (newname + pstart + PASS_REPL_LEN, newname + pstart + plen,
 		   nlen - (pstart + plen) + 1);
 	  memcpy (newname + pstart, PASS_REPL, PASS_REPL_LEN);
-	  url->name = newname;
 	}
 
       /* Dup the strings we found. We wouldn't have to do this
@@ -959,44 +964,68 @@ mu_url_decode (const char *s)
   return mu_url_decode_len (s, strlen (s));
 }
 
-static int
-defined (const char *s)
-{
-  if (s && strcmp ("*", s) != 0)
-    return 1;
-  return 0;
-}
+#define is_wildcard(s) ((s)[0] == '*' && s[1] == 0)
 
 int
-mu_url_is_ticket (mu_url_t ticket, mu_url_t url)
+mu_url_matches_ticket (mu_url_t ticket, mu_url_t url, int *pwc)
 {
-  if (!ticket || !url)
+  int wcnt = 0;
+
+  if (is_wildcard (ticket->scheme))
+    wcnt++;
+  else if (mu_c_strcasecmp (ticket->scheme, url->scheme))
     return 0;
 
-  /* If ticket has a scheme, host, port, or path, then the queries
-     equivalent must be defined and match. */
-  if (defined (ticket->scheme))
+  if (ticket->flags & MU_URL_HOST)
     {
-      if (!url->scheme || mu_c_strcasecmp (ticket->scheme, url->scheme) != 0)
+      if (is_wildcard (ticket->host))
+	wcnt++;
+      else if (url->flags & MU_URL_HOST)
+	{
+	  if (mu_c_strcasecmp (ticket->host, url->host))
+	    /* FIXME: Compare IP addresses */
+	    return 0;
+	}
+      else
 	return 0;
     }
-  if (defined (ticket->host))
+  else
+    wcnt++;
+
+  if (ticket->flags & MU_URL_PORT)
     {
-      if (!url->host || mu_c_strcasecmp (ticket->host, url->host) != 0)
-	return 0;
+      /* FIXME: No way to put a wildcard in the ticket file */
+      if (url->port & MU_URL_PORT)
+	{
+	  if (ticket->port != url->port)
+	    return 0;
+	  else
+	    wcnt++;
+	}
     }
-  if (ticket->port && ticket->port != url->port)
-    return 0;
-  /* If ticket has a user or pass, but url doesn't, that's OK, we were
-     urling for this info. But if url does have a user/pass, it
-     must match the ticket. */
-  if (url->user)
+  else
+    wcnt++;
+  
+  if (ticket->flags & MU_URL_USER)
     {
-      if (defined (ticket->user) && strcmp (ticket->user, url->user) != 0)
-	return 0;
+      if (is_wildcard (ticket->user))
+	wcnt += 2;
+      
+      /* If ticket has a user or pass, but url doesn't, that's OK, we were
+	 looking for this info. But if url does have a user/pass, it
+	 must match the ticket. */
+      else if (url->flags & MU_URL_USER)
+	{
+	  if (strcmp (ticket->user, url->user))
+	    return 0;
+	}
     }
-
+  else
+    wcnt++;
+  
   /* Guess it matches. */
+  if (pwc)
+    *pwc = wcnt;
   return 1;
 }
 
diff --git a/libmailutils/base/wicket.c b/libmailutils/base/wicket.c
index 705e69a..4d79c2c 100644
--- a/libmailutils/base/wicket.c
+++ b/libmailutils/base/wicket.c
@@ -34,6 +34,9 @@
 #include <mailutils/util.h>
 #include <mailutils/mu_auth.h>
 #include <mailutils/stream.h>
+#include <mailutils/cstr.h>
+#include <mailutils/nls.h>
+#include <mailutils/errno.h>
 
 #include <mailutils/sys/auth.h>
 #include <mailutils/sys/url.h>
@@ -50,7 +53,8 @@ mu_wicket_create (mu_wicket_t *pwicket)
 }
 
 int
-mu_wicket_get_ticket (mu_wicket_t wicket, const char *user, mu_ticket_t *pticket)
+mu_wicket_get_ticket (mu_wicket_t wicket, const char *user,
+		      mu_ticket_t *pticket)
 {
   if (!wicket)
     return EINVAL;
@@ -168,8 +172,6 @@ file_ticket_destroy (mu_ticket_t ticket)
     }
 }
 
-static int get_ticket_url (mu_ticket_t ticket, mu_url_t url, mu_url_t *pticket_url);
-
 int
 file_ticket_get_cred (mu_ticket_t ticket, mu_url_t url, const char *challenge,
 		      char **pplain, mu_secret_t *psec)
@@ -178,7 +180,7 @@ file_ticket_get_cred (mu_ticket_t ticket, mu_url_t url, const char *challenge,
 
   if (!ft->tickurl)
     {
-      int rc = get_ticket_url (ticket, url, &ft->tickurl);
+      int rc = mu_wicket_file_match_url (ft->filename, url, &ft->tickurl);
       if (rc)
 	return rc;
     }
@@ -241,37 +243,28 @@ _file_wicket_get_ticket (mu_wicket_t wicket, void *data,
   return 0;
 }
   
-static int
-get_ticket_url (mu_ticket_t ticket, mu_url_t url, mu_url_t *pticket_url)
+int
+mu_wicket_stream_match_url (mu_stream_t stream, struct mu_debug_locus *loc,
+			    mu_url_t url, mu_url_t *pticket_url)
 {
-  mu_stream_t stream;
-  struct file_ticket *ft = mu_ticket_get_data (ticket);
   int rc;
   mu_url_t u = NULL;
   char *buf = NULL;
   size_t bufsize = 0;
   size_t len;
-  
-  rc = mu_file_stream_create (&stream, ft->filename, MU_STREAM_READ);
-  if (rc)
-    return rc;
+  mu_url_t pret = NULL;
+  int weight = 0;
+  int line = loc->line;
     
   while ((rc = mu_stream_getline (stream, &buf, &bufsize, &len)) == 0
 	 && len > 0)
     {
       char *p;
       int err;
+      int n;
       
-      /* Truncate a trailing newline. */
-      if (len && buf[len - 1] == '\n')
-	buf[--len] = 0;
-      
-      /* Skip leading spaces  */
-      for (p = buf; *p == ' ' || *p == '\t'; p++)
-	;
-      /* Skip trailing spaces */
-      for (; len > 0 && (p[len-1] == ' ' || p[len-1] == '\t'); )
-	p[--len] = 0;
+      loc->line++;
+      p = mu_str_stripws (buf);
       
       /* Skip empty lines and comments. */
       if (*p == 0 || *p == '#')
@@ -280,43 +273,42 @@ get_ticket_url (mu_ticket_t ticket, mu_url_t url, mu_url_t *pticket_url)
       if ((err = mu_url_create (&u, p)) != 0)
 	{
 	  /* Skip erroneous entry */
-	  /* FIXME: Error message */
+	  mu_error (_("%s:%u: cannot create URL: %s"),
+		    loc->file, loc->line, mu_strerror (err));
 	  continue;
 	}
       if ((err = mu_url_parse (u)) != 0)
 	{
-	  /* FIXME: See above */
+	  mu_error (_("%s:%u: cannot parse URL: %s"),
+		    loc->file, loc->line, mu_strerror (err));
 	  mu_url_destroy (&u);
 	  continue;
 	}
       
-      if (!mu_url_is_ticket (u, url))
+      if (!mu_url_matches_ticket (u, url, &n))
 	{
 	  mu_url_destroy (&u);
 	  continue;
 	}
       
-      if (ft->user)
+      if (!pret || n < weight)
 	{
-	  if (u->name && strcmp (u->name, "*") != 0
-	      && strcmp (ft->user, u->name) != 0)
-	    {
-	      mu_url_destroy (&u);
-	      continue;
-	    }
+	  pret = u;
+	  weight = n;
+	  line = loc->line;
+	  if (weight == 0)
+	    break;
 	}
-      
-      break;
     }
-  mu_stream_close (stream);
   free (buf);
     
-  mu_stream_destroy (&stream);
-
   if (rc == 0)
     {
-      if (u)
-	*pticket_url = u;
+      if (pret)
+	{
+	  *pticket_url = pret;
+	  loc->line = line;
+	}
       else
 	rc = MU_ERR_NOENT;
     }
@@ -325,6 +317,25 @@ get_ticket_url (mu_ticket_t ticket, mu_url_t url, mu_url_t *pticket_url)
 }
 
 int
+mu_wicket_file_match_url (const char *name, mu_url_t url,
+			  mu_url_t *pticket_url)
+{
+  mu_stream_t stream;
+  int rc;
+  struct mu_debug_locus loc;
+  
+  rc = mu_file_stream_create (&stream, name, MU_STREAM_READ);
+  if (rc)
+    return rc;
+  loc.file = name;
+  loc.line = 0;
+  rc = mu_wicket_stream_match_url (stream, &loc, url, pticket_url);
+  mu_stream_close (stream);
+  mu_stream_destroy (&stream);
+  return rc;
+}
+  
+int
 mu_file_wicket_create (mu_wicket_t *pwicket, const char *filename)
 {
   mu_wicket_t wicket;
diff --git a/libmailutils/tests/.gitignore b/libmailutils/tests/.gitignore
index 63b7b53..ec75020 100644
--- a/libmailutils/tests/.gitignore
+++ b/libmailutils/tests/.gitignore
@@ -12,3 +12,4 @@ fltst
 listop
 mailcap
 url-parse
+wicket
diff --git a/libmailutils/tests/Makefile.am b/libmailutils/tests/Makefile.am
index a0b9574..f714b58 100644
--- a/libmailutils/tests/Makefile.am
+++ b/libmailutils/tests/Makefile.am
@@ -47,11 +47,12 @@ noinst_PROGRAMS = \
  fltst\
  listop\
  mailcap\
- url-parse
+ url-parse\
+ wicket
 
 LDADD = ${MU_LIB_MAILUTILS}
 
-EXTRA_DIST += Encode Decode
+EXTRA_DIST += Encode Decode Wicketfile
 
 ## ------------ ##
 ## Test suite.  ##
@@ -68,7 +69,8 @@ TESTSUITE_AT = \
  list.at\
  mailcap.at\
  testsuite.at\
- url.at
+ url.at\
+ wicket.at
 
 TESTSUITE = $(srcdir)/testsuite
 M4=m4
diff --git a/libmailutils/tests/Wicketfile b/libmailutils/tests/Wicketfile
new file mode 100644
index 0000000..076de05
--- /dev/null
+++ b/libmailutils/tests/Wicketfile
@@ -0,0 +1,7 @@
+# A test wicket file.
+*://foo:bar@localhost
+pop://*:foo;AUTH=+APOP@localhost
+pop://gray:test@localhost
+smtp://gray:guessme@localhost
+smtp://gray:adiviname@gnu.org
+pop://gray:secret@gnu.org
diff --git a/libmailutils/tests/testsuite.at b/libmailutils/tests/testsuite.at
index ed6e530..d5d02aa 100644
--- a/libmailutils/tests/testsuite.at
+++ b/libmailutils/tests/testsuite.at
@@ -63,4 +63,5 @@ m4_include([base64d.at])
 m4_include([decode2047.at])
 m4_include([encode2047.at])
 m4_include([fromflt.at])
+m4_include([wicket.at])
 
diff --git a/libmailutils/tests/wicket.at b/libmailutils/tests/wicket.at
new file mode 100644
index 0000000..7f5f408
--- /dev/null
+++ b/libmailutils/tests/wicket.at
@@ -0,0 +1,52 @@
+# This file is part of GNU Mailutils. -*- Autotest -*-
+# Copyright (C) 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.
+#
+# GNU Mailutils 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 GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
+
+dnl ------------------------------------------------------------
+dnl TESTWICKET(URL, [KW = `'], [STDOUT = `'],
+dnl            [STDERR = `'], [RUN-IF-FAIL], [RUN-IF-PASS])
+dnl
+m4_pushdef([TESTWICKET],[
+AT_SETUP([Wicket: [$1]])
+AT_KEYWORDS([wicket wicket-$2])
+AT_CHECK([wicket $abs_top_srcdir/libmailutils/tests/Wicketfile "$1" | \
+ sed "s|$abs_top_srcdir/libmailutils/tests/Wicketfile|Wicketfile|"],
+[0],
+[$3],[$4],[$5],[$6])
+AT_CLEANUP])
+
+TESTWICKET([pop://localhost],[00],
+           [pop://localhost matches pop://gray:***@localhost at Wicketfile:4
+])
+TESTWICKET([pop://smith@localhost],[01],
+           [pop://smith@localhost matches pop://*:***;AUTH=+APOP@localhost at Wicketfile:3
+])
+TESTWICKET([pop://foo@localhost],[02],
+           [pop://foo@localhost matches *://foo:***@localhost at Wicketfile:2
+])
+TESTWICKET([pop://gnu.org],[03],
+           [pop://gnu.org matches pop://gray:***@gnu.org at Wicketfile:7
+])
+TESTWICKET([smtp://gnu.org],[04],
+           [smtp://gnu.org matches smtp://gray:***@gnu.org at Wicketfile:6
+])
+TESTWICKET([smtp://gray@gnu.org],[05],
+           [smtp://gray@gnu.org matches smtp://gray:***@gnu.org at Wicketfile:6
+])
+TESTWICKET([smtp://localhost],[06],
+           [smtp://localhost matches smtp://gray:***@localhost at Wicketfile:5
+])
+
+	   
\ No newline at end of file
diff --git a/libmailutils/tests/wicket.c b/libmailutils/tests/wicket.c
new file mode 100644
index 0000000..b6215cb
--- /dev/null
+++ b/libmailutils/tests/wicket.c
@@ -0,0 +1,111 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 2002, 2005, 2007, 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.
+
+   GNU Mailutils 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 GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+#include <unistd.h>
+#include <stdio.h>
+#include <mailutils/mailutils.h>
+
+const char *name;
+mu_stream_t stream;
+
+void
+match_string (const char *str)
+{
+  int rc;
+  mu_url_t u, url;
+  struct mu_debug_locus loc;
+  
+  if ((rc = mu_url_create (&u, str)) != 0)
+    {
+      fprintf (stderr, "mu_url_create %s ERROR: [%d] %s",
+	       str, rc, mu_strerror (rc));
+      return;
+    }
+  if ((rc = mu_url_parse (u)) != 0)
+    {
+      fprintf (stderr, "%s\n", mu_errname (rc));
+      return;
+    }
+  MU_ASSERT (mu_stream_seek (stream, 0, MU_SEEK_SET, NULL));
+  loc.file = name;
+  loc.line = 0;
+  rc = mu_wicket_stream_match_url (stream, &loc, u, &url);
+  switch (rc)
+    {
+    case 0:
+      printf ("%s matches %s at %s:%d\n",
+	      mu_url_to_string (u), mu_url_to_string (url),
+	      loc.file, loc.line);
+      mu_url_destroy (&url);
+      break;
+
+    case MU_ERR_NOENT:
+      printf ("no matches for %s\n", mu_url_to_string (u));
+      break;
+
+    default:
+      mu_error ("mu_wicket_stream_match_url: %s", mu_strerror (rc));
+      exit (1);
+    }
+  mu_url_destroy (&u);
+}
+
+
+int
+main (int argc, char **argv)
+{
+  if (argc < 2)
+    {
+      fprintf (stderr, "usage: %s filename [url [url...]]\n", argv[0]);
+      return 1;
+    }
+
+  name = argv[1];
+  MU_ASSERT (mu_file_stream_create (&stream, name, MU_STREAM_READ));
+
+  if (argc > 2)
+    {
+      int i;
+
+      for (i = 2; i < argc; i++)
+	match_string (argv[i]);
+    }
+  else
+    {
+      mu_stream_t in;
+      char *buf = NULL;
+      size_t size = 0, n;
+      int rc;
+      
+      MU_ASSERT (mu_stdio_stream_create (&in, MU_STDIN_FD, 0));
+
+      while ((rc = mu_stream_getline (in, &buf, &size, &n)) == 0
+	     && n > 0)
+	match_string (mu_str_stripws (buf));
+      free (buf);
+      mu_stream_destroy (&in);
+    }
+  mu_stream_destroy (&stream);
+  return 0;
+}
+
+	  
+
+	


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Sun Oct 24 15:00:58 2010
Received: from mailman by lists.gnu.org with archive (Exim 4.43)
	id 1PA5oQ-0007gx-4M
	for mharc-commit-mailutils@gnu.org; Sun, 24 Oct 2010 15:00:58 -0400
Received: from [140.186.70.92] (port=52578 helo=eggs.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.43) id 1PA5no-00073B-HN
	for commit-mailutils@gnu.org; Sun, 24 Oct 2010 15:00:56 -0400
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1PA5V7-0003TJ-6O
	for commit-mailutils@gnu.org; Sun, 24 Oct 2010 14:41:03 -0400
Received: from colonialone.fsf.org ([140.186.70.51]:35737
	helo=internal.in.savannah.gnu.org)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1PA5V7-0003TE-2h
	for commit-mailutils@gnu.org; Sun, 24 Oct 2010 14:41:01 -0400
Received: from [10.1.0.108] (helo=vcs-noshell.in.savannah.gnu.org)
	by internal.in.savannah.gnu.org with esmtp (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1PA5V6-0003fj-Ng
	for commit-mailutils@gnu.org; Sun, 24 Oct 2010 18:41:00 +0000
Received: from gray by vcs-noshell.in.savannah.gnu.org with local (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1PA5V3-0004AQ-Ke
	for commit-mailutils@gnu.org; Sun, 24 Oct 2010 18:41:00 +0000
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: be1380d7aa53c031a237b5e993187733eab3fa40
X-Git-Newrev: eb3a07ec466ebc923230618d059bdb4c29b170b9
Message-Id: <E1PA5V3-0004AQ-Ke@vcs-noshell.in.savannah.gnu.org>
Date: Sun, 24 Oct 2010 18:40:57 +0000
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
Subject: [SCM] GNU Mailutils branch, master,
	updated. release-2.2-157-geb3a07e
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.5
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=unsubscribe>
List-Archive: <http://lists.gnu.org/archive/html/commit-mailutils>
List-Post: <mailto:commit-mailutils@gnu.org>
List-Help: <mailto:commit-mailutils-request@gnu.org?subject=help>
List-Subscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Sun, 24 Oct 2010 19:00:57 -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=eb3a07ec466ebc923230618d059bdb4c29b170b9

The branch, master has been updated
       via  eb3a07ec466ebc923230618d059bdb4c29b170b9 (commit)
       via  686a73448881868fbbf0a6d9d83fa3fc6fc692af (commit)
      from  be1380d7aa53c031a237b5e993187733eab3fa40 (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 eb3a07ec466ebc923230618d059bdb4c29b170b9
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Sun Oct 24 21:18:58 2010 +0300

    Move `mailutils-config' functionality to `mu'.  Retain `mailutils-config' for backward compatibility.
    
    * config/.gitignore: Remove.
    * config/Makefile.am: Remove.
    * config/mailutils-config.c: Remove.
    * config/maint.mk: Remove.
    * config/mailutils.m4: Move to mu-aux/mailutils.m4
    * mu-aux/Makefile.am (m4datadir, dist_m4data_DATA): New variables.
    
    * Makefile.am (SUBDIRS) <config>: Remove.
    * configure.ac (AC_CONFIG_FILES): Remove config.
    
    * mu/mailutils-config: New file.
    * mu/cflags.c: New file.
    * mu/ldflags.c: New file.
    * mu/Makefile.am (dist_bin_SCRIPTS, EXTRA_DIST): Add mailutils-config.
    (mu_SOURCES): Add cflags.c and ldflags.c.
    (AM_CPPFLAGS): New variable.
    * mu/mu.c: Add new modes: cflags and ldflags.
    * mu/mu.h (mutool_ldflags, mutool_cflags): New protos.
    * po/POTFILES.in: Update.

commit 686a73448881868fbbf0a6d9d83fa3fc6fc692af
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Sun Oct 24 19:09:58 2010 +0300

    New `mu' mode: wicket.
    
    * mu/wicket.c: New file.
    * mu/mu.h (mutool_wicket): New proto.
    * mu/mu.c (mutool_action_tab): Add an entry for wicket mode.
    * mu/Makefile.am (mu_SOURCES): Add wicket.c
    * .gitignore: Update.

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

Summary of changes:
 .gitignore                      |    1 +
 Makefile.am                     |    1 -
 config/.gitignore               |    5 -
 config/Makefile.am              |   39 ----
 config/mailutils-config.c       |  367 ---------------------------------------
 config/maint.mk                 |   29 ---
 configure.ac                    |    1 -
 libmailutils/cfg/.gitignore     |    6 +-
 mu-aux/Makefile.am              |    2 +
 {config => mu-aux}/mailutils.m4 |    0
 mu/Makefile.am                  |   14 ++-
 lib/signal.c => mu/cflags.c     |   43 +++--
 mu/ldflags.c                    |  193 ++++++++++++++++++++
 mu/mailutils-config             |  109 ++++++++++++
 mu/mu.c                         |    6 +
 mu/mu.h                         |    5 +-
 mu/wicket.c                     |  196 +++++++++++++++++++++
 po/POTFILES.in                  |    5 +-
 18 files changed, 555 insertions(+), 467 deletions(-)
 delete mode 100644 config/.gitignore
 delete mode 100644 config/Makefile.am
 delete mode 100644 config/mailutils-config.c
 delete mode 100644 config/maint.mk
 rename {config => mu-aux}/mailutils.m4 (100%)
 copy lib/signal.c => mu/cflags.c (59%)
 create mode 100644 mu/ldflags.c
 create mode 100755 mu/mailutils-config
 create mode 100644 mu/wicket.c

diff --git a/.gitignore b/.gitignore
index d827e24..a530f29 100644
--- a/.gitignore
+++ b/.gitignore
@@ -23,6 +23,7 @@ config.h.in
 config.log
 config.status
 configure
+core
 git-describe
 git-describe.h
 libtool
diff --git a/Makefile.am b/Makefile.am
index 3558c92..aed3f5a 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -104,7 +104,6 @@ SUBDIRS = . \
  libmu_sieve\
  $(PYTHON_DIR)\
  doc\
- config\
  examples\
  mu\
  $(FRM_DIR)\
diff --git a/config/.gitignore b/config/.gitignore
deleted file mode 100644
index 94e05b5..0000000
--- a/config/.gitignore
+++ /dev/null
@@ -1,5 +0,0 @@
-.deps
-.libs
-Makefile
-Makefile.in
-mailutils-config
diff --git a/config/Makefile.am b/config/Makefile.am
deleted file mode 100644
index b850f7f..0000000
--- a/config/Makefile.am
+++ /dev/null
@@ -1,39 +0,0 @@
-## This file is part of GNU Mailutils.
-## Copyright (C) 2005, 2006, 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.
-##
-## GNU Mailutils 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 GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
-
-INCLUDES = @MU_APP_COMMON_INCLUDES@ 
-
-bin_PROGRAMS = mailutils-config
-
-mailutils_config_SOURCES = mailutils-config.c
-mailutils_config_LDADD = \
- ${MU_APP_LIBRARIES}\
- ${MU_LIB_MAILUTILS}\
- @MU_COMMON_LIBRARIES@
-
-mailutils_config_CFLAGS = -DCOMPILE_FLAGS="\"-I$(includedir)\"" \
-  -I$(top_srcdir)/intl \
-  -I${top_srcdir}/lib \
-  -DLINK_FLAGS="\"-L$(libdir)\"" \
-  -DLINK_POSTFLAGS="\"$(MU_LINK_POSTFLAGS)\"" \
-  -DAUTHLIBS="\"$(MU_AUTHLIBS)\"" \
-  -DGUILE_LIBS="\"$(GUILE_LIBS)\"" \
-  -DPYTHON_LIBS="\"$(PYTHON_LIBS)\"" \
-  -DI18NLIBS="\"$(LIBINTL)\""
-
-m4datadir = $(datadir)/aclocal
-dist_m4data_DATA = mailutils.m4
diff --git a/config/mailutils-config.c b/config/mailutils-config.c
deleted file mode 100644
index 6a34497..0000000
--- a/config/mailutils-config.c
+++ /dev/null
@@ -1,367 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 1999, 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.
-
-   GNU Mailutils 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 GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
-
-#ifdef HAVE_CONFIG_H
-# include <config.h>
-#endif
-#include <string.h>
-#include <mailutils/mailutils.h>
-#include <mu_asprintf.h>
-#include "mailutils/libargp.h"
-
-static char doc[] = N_("GNU mailutils-config -- display compiler and loader options needed for building a program with mailutils.");
-static char args_doc[] = N_("[arg...]");
-
-static struct argp_option options[] = {
-  {"compile", 'c', NULL,   0,
-   N_("print C compiler flags to compile with"), 0},
-  {"link",    'l', NULL,   0,
-   N_("print libraries to link with; possible arguments are: auth, guile, "
-      "mbox, mh, maildir, mailer, imap, pop, sieve and all"), 0},
-  {"info", 'i', NULL, 0,
-   N_("print a list of configuration options used to build mailutils; "
-      "optional arguments are interpreted as a list of configuration "
-      "options to check for"), 0},
-  {"query", 'q', N_("FILE"), OPTION_ARG_OPTIONAL,
-   N_("query configuration values from FILE (default mailutils.rc)"),
-   0 },
-  {"verbose", 'v', NULL, 0,
-   N_("increase output verbosity"), 0},
-  {0, 0, 0, 0}
-};
-
-enum config_mode {
-  MODE_VOID,
-  MODE_COMPILE,
-  MODE_LINK,
-  MODE_INFO,
-  MODE_QUERY
-};
-
-enum config_mode mode;
-int verbose;
-char *query_config_file;
-
-static error_t
-parse_opt (int key, char *arg, struct argp_state *state)
-{
-  switch (key)
-    {
-    case 'l':
-      mode = MODE_LINK;
-      break;
-
-    case 'c':
-      mode = MODE_COMPILE;
-      break;
-
-    case 'i':
-      mode = MODE_INFO;
-      break;
-
-    case 'q':
-      if (arg)
-	query_config_file = arg;
-      mode = MODE_QUERY;
-      break;
-      
-    case 'v':
-      verbose++;
-      break;
-      
-    default: 
-      return ARGP_ERR_UNKNOWN;
-    }
-  return 0;
-}
-
-static struct argp argp = {
-  options,
-  parse_opt,
-  args_doc,
-  doc,
-  NULL,
-  NULL, NULL
-};
-
-static const char *argp_capa[] = {
-  "common",
-  "license",
-  NULL
-};
-
-#ifdef WITH_TLS
-# define NEEDAUTH 1
-#else
-# define NEEDAUTH 0
-#endif
-#define NOTALL   2
-
-struct lib_descr {
-  char *name;
-  char *libname;
-  int flags;
-} lib_descr[] = {
-  { "mbox",   "mu_mbox", 0 },
-  { "mh",     "mu_mh",   0 },
-  { "maildir","mu_maildir", 0 },
-  { "imap",   "mu_imap", NEEDAUTH },
-  { "pop",    "mu_pop",  NEEDAUTH },
-  { "nntp",   "mu_nntp", 0 },
-  { "mailer", "mu_mailer", 0 },
-  { "sieve",  "mu_sieve", NOTALL },
-  { NULL }
-};
-
-struct lib_entry {
-  int level;
-  char *ptr;
-} lib_entry[16];
-
-int nentry;
-
-void
-add_entry (int level, char *ptr)
-{
-  int i;
-  if (nentry >= sizeof(lib_entry)/sizeof(lib_entry[0]))
-    {
-      mu_error (_("too many arguments"));
-      exit (1);
-    }
-  
-  for (i = 0; i < nentry; i++)
-    if (strcmp (lib_entry[i].ptr, ptr) == 0)
-      return;
-  lib_entry[nentry].level = level;
-  lib_entry[nentry].ptr = ptr;
-  nentry++;
-}
-
-/* Sort the entries by their level. */
-void
-sort_entries ()
-{
-  int j;
-
-  for (j = 0; j < nentry; j++)
-    {
-      int i;
-	      
-      for (i = j; i < nentry; i++)
-	if (lib_entry[j].level > lib_entry[i].level)
-	  {
-	    struct lib_entry tmp;
-	    tmp = lib_entry[i];
-	    lib_entry[i] = lib_entry[j];
-	    lib_entry[j] = tmp;
-	  }
-      
-    }
-}
-
-int
-main (int argc, char **argv)
-{
-  int index;
-  int i, rc;
-  struct argp *myargp;
-  char **excapa;
-  mu_cfg_tree_t *tree = NULL;
-  mu_stream_t stream;
-  int fmtflags = 0;
-  
-  mu_argp_init (NULL, NULL);
-
-  mu_set_program_name (argv[0]);
-  mu_libargp_init ();
-  for (i = 0; argp_capa[i]; i++)
-    mu_gocs_register_std (argp_capa[i]); /*FIXME*/
-  myargp = mu_argp_build (&argp, &excapa);
-  
-  if (argp_parse (myargp, argc, argv, 0, &index, NULL))
-    {
-      argp_help (myargp, stdout, ARGP_HELP_SEE, program_invocation_short_name);
-      return 1;
-    }
-  mu_argp_done (myargp);
-  mu_set_program_name (program_invocation_name);
-  
-  argc -= index;
-  argv += index;
-  
-  switch (mode)
-    {
-    case MODE_VOID:
-      break;
-
-    case MODE_LINK:
-	{
-	  int j;
-	  char *ptr;
-	  
-	  add_entry (-100, LINK_FLAGS);
-	  add_entry (100, LINK_POSTFLAGS);
-	  add_entry (1, "-lmailutils");
-#ifdef ENABLE_NLS
-	  if (sizeof (I18NLIBS) > 1)
-	    add_entry (10, I18NLIBS);
-#endif
-
-	  for ( ; argc > 0; argc--, argv++)
-	    {
-	      if (strcmp (argv[0], "auth") == 0)
-		{
-		  add_entry (2, "-lmu_auth " AUTHLIBS);
-		}
-#ifdef WITH_GUILE	      
-	      else if (strcmp (argv[0], "guile") == 0)
-		{
-		  add_entry (-1, "-lmu_scm " GUILE_LIBS);
-		}
-#endif
-#ifdef WITH_PYTHON
-	      else if (strcmp (argv[0], "python") == 0)
-		{
-		  add_entry (-1, "-lmu_py " PYTHON_LIBS);
-		}
-#endif
-	      else if (strcmp (argv[0], "cfg") == 0)
-		add_entry (-1, "-lmu_cfg");
-	      else if (strcmp (argv[0], "argp") == 0)
-		add_entry (-2, "-lmu_argp");
-	      else if (strcmp (argv[0], "all") == 0)
-		{
-		  struct lib_descr *p;
-		  
-		  for (p = lib_descr; p->name; p++)
-		    {
-		      if (p->flags & NOTALL)
-			continue;
-		      asprintf (&ptr, "-l%s", p->libname);
-		      add_entry (0, ptr);
-		      if (p->flags & NEEDAUTH)
-			add_entry (2, "-lmu_auth " AUTHLIBS);
-		    }
-		}
-	      else
-		{
-		  struct lib_descr *p;
-		  
-		  for (p = lib_descr; p->name; p++)
-		    if (mu_c_strcasecmp (p->name, argv[0]) == 0)
-		      break;
-
-		  if (p->name)
-		    {
-		      asprintf (&ptr, "-l%s", p->libname);
-		      add_entry (0, ptr);
-		      if (p->flags & NEEDAUTH)
-			add_entry (2, "-lmu_auth " AUTHLIBS);
-		    }
-		  else
-		    {
-		      argp_help (&argp, stdout, ARGP_HELP_USAGE,
-				 program_invocation_short_name);
-		      return 1;
-		    }
-		}
-	    }
-	  
-	  sort_entries ();
-	  
-	  /* At least one entry is always present */
-	  printf ("%s", lib_entry[0].ptr);
-
-	  /* Print the rest of them separated by a space */
-	  for (j = 1; j < nentry; j++)
-	    {
-	      printf (" %s", lib_entry[j].ptr);
-	    }
-	  printf ("\n");
-	  return 0;
-	}
-	
-    case MODE_COMPILE:
-      if (argc != 0)
-	break;
-      printf ("%s\n", COMPILE_FLAGS);
-      return 0;
-
-    case MODE_INFO:
-      if (argc == 0)
-	mu_fprint_options (stdout, verbose);
-      else
-	{
-	  int i, found = 0;
-	  
-	  for (i = 0; i < argc; i++)
-	    {
-	      const struct mu_conf_option *opt = mu_check_option (argv[i]);
-	      if (opt)
-		{
-		  found++;
-		  mu_fprint_conf_option (stdout, opt, verbose);
-		}
-	    }
-	  return found == argc ? 0 : 1;
-	}
-      return 0;
-
-    case MODE_QUERY:
-      if (argc == 0)
-	{
-	  mu_error (_("not enough arguments"));
-	  return 1;
-	}
-
-      if (query_config_file)
-	{
-	  mu_load_site_rcfile = 0;
-	  mu_load_user_rcfile = 0;
-	  mu_load_rcfile = query_config_file;
-	}
-
-      if (mu_libcfg_parse_config (&tree))
-	exit (1);
-      if (!tree)
-	exit (0);
-      rc = mu_stdio_stream_create (&stream, MU_STDOUT_FD, 0);
-      if (rc)
-	{
-	  mu_error ("mu_stdio_stream_create: %s", mu_strerror (rc));
-	  exit (1);
-	}
-      if (verbose)
-	fmtflags = MU_CFG_FMT_LOCUS;
-      for ( ; argc > 0; argc--, argv++)
-	{
-	  char *path = *argv;
-	  mu_cfg_node_t *node;
-
-	  if (mu_cfg_find_node (tree, path, &node) == 0)
-	    {
-	      mu_cfg_format_node (stream, node, fmtflags);
-	    }
-	}
-      exit (0);
-    }
-  
-  argp_help (&argp, stdout, ARGP_HELP_USAGE, program_invocation_short_name);
-  return 0;
-}
-  
diff --git a/config/maint.mk b/config/maint.mk
deleted file mode 100644
index 69049bf..0000000
--- a/config/maint.mk
+++ /dev/null
@@ -1,29 +0,0 @@
-# This file is part of GNU Mailutils.
-# Copyright (C) 2009, 2010 Free Software Foundation, Inc.
-#
-# Written by Sergey Poznyakoff
-#
-# 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.
-#
-# GNU Mailutils 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 GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
-
-include Makefile
-mailutils_config_CFLOW_INPUT=$(mailutils_config_SOURCES)
-mailutils-config.cflow: $(mailutils_config_CFLOW_INPUT) Makefile
-	cflow -o$@ $(CFLOW_FLAGS) $(DEFS) \
-		$(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \
-		$(CPPFLAGS) \
-	$(mailutils_config_CFLOW_INPUT)
-
-flowgraph: mailutils-config.cflow
-maintclean:
-	rm -f mailutils-config.cflow
diff --git a/configure.ac b/configure.ac
index c7a627d..27ff3f6 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1337,7 +1337,6 @@ AC_CONFIG_FILES([
  Makefile
  sql/Makefile
  comsat/Makefile
- config/Makefile
  doc/Makefile
  doc/man/Makefile
  doc/texinfo/Makefile
diff --git a/libmailutils/cfg/.gitignore b/libmailutils/cfg/.gitignore
index 473803d..8993e60 100644
--- a/libmailutils/cfg/.gitignore
+++ b/libmailutils/cfg/.gitignore
@@ -1,3 +1,3 @@
-cfg_lexer.c
-cfg_parser.c
-cfg_parser.h
+lexer.c
+parser.c
+parser.h
diff --git a/mu-aux/Makefile.am b/mu-aux/Makefile.am
index 7273a9f..ec41589 100644
--- a/mu-aux/Makefile.am
+++ b/mu-aux/Makefile.am
@@ -24,3 +24,5 @@ EXTRA_DIST = \
  sqlmod.sh\
  generr.awk
 
+m4datadir = $(datadir)/aclocal
+dist_m4data_DATA = mailutils.m4
diff --git a/config/mailutils.m4 b/mu-aux/mailutils.m4
similarity index 100%
rename from config/mailutils.m4
rename to mu-aux/mailutils.m4
diff --git a/mu/Makefile.am b/mu/Makefile.am
index e724b0c..6fe40d9 100644
--- a/mu/Makefile.am
+++ b/mu/Makefile.am
@@ -16,6 +16,7 @@
 ## along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
 bin_PROGRAMS = mu
+dist_bin_SCRIPTS = mailutils-config
 
 if MU_COND_SUPPORT_POP
  POP_C=pop.c
@@ -25,14 +26,17 @@ EXTRA_DIST=pop.c
 
 mu_SOURCES = \
  acl.c\
+ cflags.c\
  info.c\
  mu.h\
  mu.c\
  filter.c\
  flt2047.c\
+ ldflags.c\
  $(POP_C)\
  query.c\
- shell.c
+ shell.c\
+ wicket.c
 
 mu_LDADD = \
  ${MU_APP_LIBRARIES}\
@@ -49,3 +53,11 @@ mu_LDADD = \
  @READLINE_LIBS@ @MU_COMMON_LIBRARIES@
 
 INCLUDES = @MU_APP_COMMON_INCLUDES@ @MU_AUTHINCS@
+AM_CPPFLAGS = \
+  -DCOMPILE_FLAGS="\"-I$(includedir)\"" \
+  -DLINK_FLAGS="\"-L$(libdir)\"" \
+  -DLINK_POSTFLAGS="\"$(MU_LINK_POSTFLAGS)\"" \
+  -DAUTHLIBS="\"$(MU_AUTHLIBS)\"" \
+  -DGUILE_LIBS="\"$(GUILE_LIBS)\"" \
+  -DPYTHON_LIBS="\"$(PYTHON_LIBS)\"" \
+  -DI18NLIBS="\"$(LIBINTL)\""
diff --git a/lib/signal.c b/mu/cflags.c
similarity index 59%
copy from lib/signal.c
copy to mu/cflags.c
index bd92e1c..9d177d0 100644
--- a/lib/signal.c
+++ b/mu/cflags.c
@@ -1,5 +1,5 @@
 /* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 2008, 2010 Free Software Foundation, Inc.
+   Copyright (C) 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
@@ -14,26 +14,33 @@
    You should have received a copy of the GNU General Public License
    along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
-#ifdef HAVE_CONFIG_H
+#if defined(HAVE_CONFIG_H)
 # include <config.h>
 #endif
 #include <stdlib.h>
-#include <signal.h>
+#include <mailutils/mailutils.h>
+#include <mailutils/libcfg.h>
+#include <argp.h>
 
-void
-mu_set_signals (RETSIGTYPE (*handler) (int signo), int *sigv, int sigc)
+static char cflags_doc[] = N_("mu cflags - show compiler options");
+
+static struct argp cflags_argp = {
+  NULL,
+  NULL,
+  NULL,
+  cflags_doc,
+  NULL,
+  NULL,
+  NULL
+};
+
+int
+mutool_cflags (int argc, char **argv)
 {
-  int i;
-  struct sigaction act;
-
-  act.sa_flags = 0;
-  sigemptyset (&act.sa_mask);
-  for (i = 0; i < sigc; i++)
-    sigaddset (&act.sa_mask, i);
-      
-  for (i = 0; i < sigc; i++)
-    {
-      act.sa_handler = handler;
-      sigaction (sigv[i], &act, NULL);
-    }
+  if (argp_parse (&cflags_argp, argc, argv, ARGP_IN_ORDER, NULL, NULL))
+    return 1;
+  printf ("%s\n", COMPILE_FLAGS);
+  return 0;
 }
+
+
diff --git a/mu/ldflags.c b/mu/ldflags.c
new file mode 100644
index 0000000..ba00bf8
--- /dev/null
+++ b/mu/ldflags.c
@@ -0,0 +1,193 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 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.
+
+   GNU Mailutils 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 GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
+
+#if defined(HAVE_CONFIG_H)
+# include <config.h>
+#endif
+#include <stdlib.h>
+#include <mailutils/mailutils.h>
+#include <mailutils/libcfg.h>
+#include <argp.h>
+
+static char ldflags_doc[] = N_("mu ldflags - list libraries required to link");
+static char ldflags_args_doc[] = N_("KEYWORD [KEYWORD...]");
+
+static struct argp ldflags_argp = {
+  NULL,
+  NULL,
+  ldflags_args_doc,
+  ldflags_doc,
+  NULL,
+  NULL,
+  NULL
+};
+
+
+#ifdef WITH_TLS
+# define NEEDAUTH 1
+#else
+# define NEEDAUTH 0
+#endif
+#define NOTALL   2
+
+struct lib_descr {
+  char *name;
+  char *libname;
+  int flags;
+} lib_descr[] = {
+  { "mbox",   "mu_mbox", 0 },
+  { "mh",     "mu_mh",   0 },
+  { "maildir","mu_maildir", 0 },
+  { "imap",   "mu_imap", NEEDAUTH },
+  { "pop",    "mu_pop",  NEEDAUTH },
+  { "nntp",   "mu_nntp", 0 },
+  { "mailer", "mu_mailer", 0 },
+  { "sieve",  "mu_sieve", NOTALL },
+  { NULL }
+};
+
+struct lib_entry {
+  int level;
+  char *ptr;
+} lib_entry[16];
+
+int nentry;
+
+void
+add_entry (int level, char *ptr)
+{
+  int i;
+  if (nentry >= sizeof(lib_entry)/sizeof(lib_entry[0]))
+    {
+      mu_error (_("too many arguments"));
+      exit (1);
+    }
+  
+  for (i = 0; i < nentry; i++)
+    if (strcmp (lib_entry[i].ptr, ptr) == 0)
+      return;
+  lib_entry[nentry].level = level;
+  lib_entry[nentry].ptr = ptr;
+  nentry++;
+}
+
+/* Sort the entries by their level. */
+void
+sort_entries ()
+{
+  int j;
+
+  for (j = 0; j < nentry; j++)
+    {
+      int i;
+	      
+      for (i = j; i < nentry; i++)
+	if (lib_entry[j].level > lib_entry[i].level)
+	  {
+	    struct lib_entry tmp;
+	    tmp = lib_entry[i];
+	    lib_entry[i] = lib_entry[j];
+	    lib_entry[j] = tmp;
+	  }
+    }
+}
+
+
+int
+mutool_ldflags (int argc, char **argv)
+{
+  int i, j;
+  char *ptr;
+  
+  if (argp_parse (&ldflags_argp, argc, argv, ARGP_IN_ORDER, &i, NULL))
+    return 1;
+
+  argc -= i;
+  argv += i;
+  
+  add_entry (-100, LINK_FLAGS);
+  add_entry (100, LINK_POSTFLAGS);
+  add_entry (1, "-lmailutils");
+#ifdef ENABLE_NLS
+  if (sizeof (I18NLIBS) > 1)
+    add_entry (10, I18NLIBS);
+#endif
+
+  for ( ; argc > 0; argc--, argv++)
+    {
+      if (strcmp (argv[0], "auth") == 0)
+	add_entry (2, "-lmu_auth " AUTHLIBS);
+#ifdef WITH_GUILE	      
+      else if (strcmp (argv[0], "guile") == 0)
+	add_entry (-1, "-lmu_scm " GUILE_LIBS);
+#endif
+#ifdef WITH_PYTHON
+      else if (strcmp (argv[0], "python") == 0)
+	add_entry (-1, "-lmu_py " PYTHON_LIBS);
+#endif
+      else if (strcmp (argv[0], "cfg") == 0)
+	add_entry (-1, "-lmu_cfg");
+      else if (strcmp (argv[0], "argp") == 0)
+	add_entry (-2, "-lmu_argp");
+      else if (strcmp (argv[0], "all") == 0)
+	{
+	  struct lib_descr *p;
+		  
+	  for (p = lib_descr; p->name; p++)
+	    {
+	      if (p->flags & NOTALL)
+		continue;
+	      asprintf (&ptr, "-l%s", p->libname);
+	      add_entry (0, ptr);
+	      if (p->flags & NEEDAUTH)
+		add_entry (2, "-lmu_auth " AUTHLIBS);
+	    }
+	}
+      else
+	{
+	  struct lib_descr *p;
+	  
+	  for (p = lib_descr; p->name; p++)
+	    if (mu_c_strcasecmp (p->name, argv[0]) == 0)
+	      break;
+
+	  if (p->name)
+	    {
+	      asprintf (&ptr, "-l%s", p->libname);
+	      add_entry (0, ptr);
+	      if (p->flags & NEEDAUTH)
+		add_entry (2, "-lmu_auth " AUTHLIBS);
+	    }
+	  else
+	    {
+	      mu_error (_("unknown keyword: %s"), argv[0]);
+	      return 1;
+	    }
+	}
+    }
+  
+  sort_entries ();
+	  
+  /* At least one entry is always present */
+  printf ("%s", lib_entry[0].ptr);
+
+  /* Print the rest of them separated by a space */
+  for (j = 1; j < nentry; j++)
+    printf (" %s", lib_entry[j].ptr);
+  putchar ('\n');
+  return 0;
+}
+
diff --git a/mu/mailutils-config b/mu/mailutils-config
new file mode 100755
index 0000000..2545265
--- /dev/null
+++ b/mu/mailutils-config
@@ -0,0 +1,109 @@
+#! /bin/sh
+# A deprecated interface to GNU Mailutils configuration facilities.
+# Copyright (C) 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 2, or (at
+# your option) any later version.
+#
+# GNU Mailutils 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 GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
+
+mode=
+file=
+dir=`expr "$0" : '\(.*\)/.*'`
+
+test -n "$dir" && PATH=$PATH:$dir
+
+usage() {
+    cat <<EOT
+This is a deprecated interface to GNU Mailutils configuration facilities.
+It will be removed in future versions.  Please, consider using these
+alternatives instead:
+
+       Traditional usage          |   Use instead
+    ------------------------------+-------------------
+    mailutils-config --compile    | mu cflags
+    mailutils-config --link       | mu ldflags
+    mailutils-config --info       | mu info
+    mailutils-config --query      | mu query
+    mailutils-config --query=FILE | mu query -f FILE
+
+For more information, try \`mu --help'.
+
+EOT
+    exit 0
+}
+
+while test $# -ne 0
+do
+  arg=$1
+  shift
+  case $arg in
+  -c|--c|--co|--com|--comp|--compil|--compile)
+    mode=cflags
+    break
+    ;;
+  -i|--i|--in|--inf|--info)
+    mode=info
+    break
+    ;;
+  -l|--l|--li|--lin|--link)
+    mode=ldflags
+    break
+    ;;
+  --)
+    break
+    ;;
+  -q*=*|--q*=*)
+    opt=`expr "$arg" : '\(.*\)='`
+    arg=`expr "$arg" : '.*=\(.*\)'`
+    case $opt in
+    -q|--q|--qu|--que|--quer|--query)
+      mode=query
+      file=arg
+      break
+      ;;
+    *)
+      echo >&2 "$0: invalid option: $1"
+      exit 1
+    esac
+    ;;
+  -q|--q|--qu|--que|--quer|--query)
+    mode=query
+    break
+    ;;
+  -q*)
+    mode=query
+    file=`expr "$arg" : '-q\(.*\)'`
+    break
+    ;;
+  --usage|--u|--us|--usa|--usag|--help|--hel|--he|--h)
+    usage
+    ;;
+  -V|--version|--versio|--versi|--vers|--ver|--ve|--v)
+    mu --version | sed -n '1{s/^mu/mailutils-config/;s/(\(GNU Mailutils\)) \([0-9][0-9.]*\).*/(\1 \2)/;p}'
+    exit 0
+    ;;
+  *)
+    echo >&2 "$0: unexpected argument; try \`$0 --usage' for help"
+    exit 1
+    ;;
+  esac
+done
+
+if test -z "$mode"; then
+  usage
+fi
+
+if test -n "$file"; then
+  set -- -f"$file" $*
+fi
+
+mu $mode $*
diff --git a/mu/mu.c b/mu/mu.c
index 2490805..a0900f7 100644
--- a/mu/mu.c
+++ b/mu/mu.c
@@ -33,6 +33,9 @@ Commands are:\n\
     mu filter - filter program\n\
     mu 2047   - decode/encode message headers as per RFC 2047\n\
     mu acl    - test access control lists\n\
+    mu wicket - find matching URL in wicket\n\
+    mu ldflags- list libraries required to link\n\
+    mu cflags - list compiler flags\n\
 \n\
 Try `mu COMMAND --help' to get help on a particular COMMAND.\n\
 \n\
@@ -106,6 +109,9 @@ struct mutool_action_tab mutool_action_tab[] = {
   { "2047", mutool_flt2047 },
   { "query", mutool_query },
   { "acl", mutool_acl },
+  { "wicket", mutool_wicket },
+  { "ldflags", mutool_ldflags },
+  { "cflags", mutool_cflags },
   { NULL }
 };
 
diff --git a/mu/mu.h b/mu/mu.h
index 7ab5b70..22bcc33 100644
--- a/mu/mu.h
+++ b/mu/mu.h
@@ -35,7 +35,10 @@ int mutool_flt2047 (int argc, char **argv);
 int mutool_info (int argc, char **argv);
 int mutool_query (int argc, char **argv);
 int mutool_acl (int argc, char **argv);
-
+int mutool_wicket (int argc, char **argv);
+int mutool_ldflags (int argc, char **argv);
+int mutool_cflags (int argc, char **argv);
+  
 extern char *mutool_shell_prompt;
 extern mu_vartab_t mutool_prompt_vartab;
 extern int mutool_shell_interactive;
diff --git a/mu/wicket.c b/mu/wicket.c
new file mode 100644
index 0000000..2615cdf
--- /dev/null
+++ b/mu/wicket.c
@@ -0,0 +1,196 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 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.
+
+   GNU Mailutils 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 GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
+
+#if defined(HAVE_CONFIG_H)
+# include <config.h>
+#endif
+#include <stdlib.h>
+#include <mailutils/mailutils.h>
+#include <mailutils/libcfg.h>
+#include <argp.h>
+
+static char wicket_doc[] = N_("mu wicket - find matching URL in wicket");
+static char wicket_args_doc[] = N_("NAME");
+
+static struct argp_option wicket_options[] = {
+  { "file",    'f', N_("FILE"), 0, N_("use FILE instead of ~/.mu-tickets") },
+  { "verbose", 'v', NULL,       0, N_("increase output verbosity") },
+  { "quiet",   'q', NULL,       0, N_("suppress any output") },
+  { NULL }
+};
+
+static char *wicket_file = "~/.mu-tickets";
+static int wicket_verbose = 1;
+
+static error_t
+wicket_parse_opt (int key, char *arg, struct argp_state *state)
+{
+  switch (key)
+    {
+    case 'f':
+      wicket_file = arg;
+      break;
+
+    case 'v':
+      wicket_verbose++;
+      break;
+
+    case 'q':
+      wicket_verbose = 0;
+      break;
+      
+    default:
+      return ARGP_ERR_UNKNOWN;
+    }
+  return 0;
+}
+
+static struct argp wicket_argp = {
+  wicket_options,
+  wicket_parse_opt,
+  wicket_args_doc,
+  wicket_doc,
+  NULL,
+  NULL,
+  NULL
+};
+
+
+static int
+wicket_match (mu_stream_t stream, const char *str)
+{
+  int rc, ret;
+  mu_url_t u, url;
+  struct mu_debug_locus loc;
+  
+  rc = mu_url_create (&u, str);
+  if (rc)
+    {
+      mu_diag_funcall (MU_DIAG_ERROR, "mu_url_create", str, rc);
+      return 2;
+    }
+  rc = mu_url_parse (u);
+  if (rc)
+    {
+      mu_diag_funcall (MU_DIAG_ERROR, "mu_url_parse", str, rc);
+      return 2;
+    }
+
+  rc = mu_stream_seek (stream, 0, MU_SEEK_SET, NULL);
+  if (rc)
+    {
+      mu_diag_funcall (MU_DIAG_ERROR, "mu_stream_seek", "0", rc);
+      return 2;
+    }
+  loc.file = wicket_file;
+  loc.line = 0;
+  rc = mu_wicket_stream_match_url (stream, &loc, u, &url);
+  switch (rc)
+    {
+    case 0:
+      ret = 0;
+      if (wicket_verbose)
+	{
+	  printf ("%s: %s:%d", str, loc.file, loc.line);
+	  if (wicket_verbose > 1)
+	    {
+	      printf (": %s", mu_url_to_string (url));
+	      if (wicket_verbose > 2)
+		{
+		  mu_secret_t s;
+		  rc = mu_url_get_secret (url, &s);
+		  if (rc == 0)
+		    {
+		      printf (": %s", mu_secret_password (s));
+		      mu_secret_password_unref (s);
+		      mu_secret_unref (s);
+		    }
+		  else if (rc == MU_ERR_NOENT)
+		    printf (": [%s]", _("no password"));
+		  else
+		    {
+		      printf (": [error: %s]", mu_strerror (rc));
+		      ret = 2;
+		    }
+		}
+	    }
+	  putchar ('\n');
+	}
+      break;
+
+    case MU_ERR_NOENT:
+      if (wicket_verbose)
+	printf ("%s: %s\n", str, _("not found"));
+      ret = 1;
+      break;
+
+    default:
+      mu_diag_funcall (MU_DIAG_ERROR, "mu_wicket_stream_match_url", str, rc);
+      ret = 2;
+    }
+  return ret;
+}
+
+int
+mutool_wicket (int argc, char **argv)
+{
+  mu_stream_t stream;
+  int rc, i, exit_code;
+  
+  if (argp_parse (&wicket_argp, argc, argv, ARGP_IN_ORDER, &i, NULL))
+    return 1;
+
+  argc -= i;
+  argv += i;
+
+  if (argc == 0)
+    {
+      mu_error (_("not enough arguments"));
+      return 1;
+    }
+
+  wicket_file = mu_tilde_expansion (wicket_file, "/", NULL);
+  if (!wicket_file)
+    {
+      mu_diag_funcall (MU_DIAG_ERROR, "mu_tilde_expansion", wicket_file,
+		       ENOMEM);
+      return 2;
+    }
+  
+  rc = mu_file_stream_create (&stream, wicket_file, MU_STREAM_READ);
+  if (rc)
+    {
+      mu_error (_("cannot open input file %s: %s"), wicket_file,
+		mu_strerror (rc));
+      return 1;
+    }
+
+  exit_code = 0;
+  for (i = 0; i < argc; i++)
+    {
+      rc = wicket_match (stream, argv[i]);
+      if (rc)
+	{
+	  if (exit_code < rc)
+	    exit_code = rc;
+	  if (!wicket_verbose)
+	    break;
+	}
+    }
+
+  mu_stream_destroy (&stream);
+  return exit_code;
+}
diff --git a/po/POTFILES.in b/po/POTFILES.in
index f07b4c5..fc1e8b1 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -7,8 +7,6 @@
 comsat/action.c
 comsat/comsat.c
 
-config/mailutils-config.c
-
 dotlock/dotlock.c
 
 frm/common.c
@@ -213,6 +211,9 @@ mu/mu.c
 mu/pop.c
 mu/query.c
 mu/shell.c
+mu/cflags.c
+mu/ldflags.c
+mu/wicket.c
 
 # EOF
 


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Mon Oct 25 15:52:33 2010
Received: from mailman by lists.gnu.org with archive (Exim 4.43)
	id 1PAT5t-0000OS-JJ
	for mharc-commit-mailutils@gnu.org; Mon, 25 Oct 2010 15:52:33 -0400
Received: from [140.186.70.92] (port=49133 helo=eggs.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.43) id 1PAT5q-0000MV-QZ
	for commit-mailutils@gnu.org; Mon, 25 Oct 2010 15:52:32 -0400
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1PAT5p-00024w-97
	for commit-mailutils@gnu.org; Mon, 25 Oct 2010 15:52:30 -0400
Received: from colonialone.fsf.org ([140.186.70.51]:58754
	helo=internal.in.savannah.gnu.org)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1PAT5p-00024s-6R
	for commit-mailutils@gnu.org; Mon, 25 Oct 2010 15:52:29 -0400
Received: from [10.1.0.108] (helo=vcs-noshell.in.savannah.gnu.org)
	by internal.in.savannah.gnu.org with esmtp (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1PAT5p-0005U9-2T
	for commit-mailutils@gnu.org; Mon, 25 Oct 2010 19:52:29 +0000
Received: from gray by vcs-noshell.in.savannah.gnu.org with local (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1PAT5o-0000vY-R8
	for commit-mailutils@gnu.org; Mon, 25 Oct 2010 19:52:29 +0000
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: eb3a07ec466ebc923230618d059bdb4c29b170b9
X-Git-Newrev: 6203ae65f53758a4b893e6e176be7c153a31bef0
Message-Id: <E1PAT5o-0000vY-R8@vcs-noshell.in.savannah.gnu.org>
Date: Mon, 25 Oct 2010 19:52:28 +0000
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
Subject: [SCM] GNU Mailutils branch, master,
	updated. release-2.2-159-g6203ae6
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.5
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=unsubscribe>
List-Archive: <http://lists.gnu.org/archive/html/commit-mailutils>
List-Post: <mailto:commit-mailutils@gnu.org>
List-Help: <mailto:commit-mailutils-request@gnu.org?subject=help>
List-Subscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Mon, 25 Oct 2010 19:52:32 -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=6203ae65f53758a4b893e6e176be7c153a31bef0

The branch, master has been updated
       via  6203ae65f53758a4b893e6e176be7c153a31bef0 (commit)
       via  08f69cc36908f376659369a3c5ee2f63a75f1c73 (commit)
      from  eb3a07ec466ebc923230618d059bdb4c29b170b9 (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 6203ae65f53758a4b893e6e176be7c153a31bef0
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Mon Oct 25 17:16:55 2010 +0300

    Minor fix.
    
    * mh/mh_init.c (mh_safe_make_file_name): Avoid gcc warnings.

commit 08f69cc36908f376659369a3c5ee2f63a75f1c73
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Mon Oct 25 16:55:37 2010 +0300

    Improve URL matching code.
    
    * libmailutils/base/url.c (mu_url_matches_ticket): Use different
    weight values, depending on the missing part.
    * libmailutils/base/wicket.c (mu_wicket_stream_match_url): Refuse
    URLs that lack host and password parts.

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

Summary of changes:
 libmailutils/base/url.c    |   21 +++++++++++++--------
 libmailutils/base/wicket.c |    8 ++++++++
 mh/mh_init.c               |    6 +++---
 3 files changed, 24 insertions(+), 11 deletions(-)

diff --git a/libmailutils/base/url.c b/libmailutils/base/url.c
index 49aab4b..96345f0 100644
--- a/libmailutils/base/url.c
+++ b/libmailutils/base/url.c
@@ -966,20 +966,25 @@ mu_url_decode (const char *s)
 
 #define is_wildcard(s) ((s)[0] == '*' && s[1] == 0)
 
+#define WEIGHT_SCHEME 3
+#define WEIGHT_USER   4
+#define WEIGHT_HOST   2
+#define WEIGHT_PORT   1
+
 int
 mu_url_matches_ticket (mu_url_t ticket, mu_url_t url, int *pwc)
 {
   int wcnt = 0;
-
+					      
   if (is_wildcard (ticket->scheme))
-    wcnt++;
+    wcnt += WEIGHT_SCHEME;
   else if (mu_c_strcasecmp (ticket->scheme, url->scheme))
     return 0;
 
   if (ticket->flags & MU_URL_HOST)
     {
       if (is_wildcard (ticket->host))
-	wcnt++;
+	wcnt += WEIGHT_HOST;
       else if (url->flags & MU_URL_HOST)
 	{
 	  if (mu_c_strcasecmp (ticket->host, url->host))
@@ -990,7 +995,7 @@ mu_url_matches_ticket (mu_url_t ticket, mu_url_t url, int *pwc)
 	return 0;
     }
   else
-    wcnt++;
+    wcnt += WEIGHT_HOST;
 
   if (ticket->flags & MU_URL_PORT)
     {
@@ -1000,16 +1005,16 @@ mu_url_matches_ticket (mu_url_t ticket, mu_url_t url, int *pwc)
 	  if (ticket->port != url->port)
 	    return 0;
 	  else
-	    wcnt++;
+	    wcnt += WEIGHT_PORT;
 	}
     }
   else
-    wcnt++;
+    wcnt += WEIGHT_PORT;
   
   if (ticket->flags & MU_URL_USER)
     {
       if (is_wildcard (ticket->user))
-	wcnt += 2;
+	wcnt += WEIGHT_USER;
       
       /* If ticket has a user or pass, but url doesn't, that's OK, we were
 	 looking for this info. But if url does have a user/pass, it
@@ -1021,7 +1026,7 @@ mu_url_matches_ticket (mu_url_t ticket, mu_url_t url, int *pwc)
 	}
     }
   else
-    wcnt++;
+    wcnt += WEIGHT_USER;
   
   /* Guess it matches. */
   if (pwc)
diff --git a/libmailutils/base/wicket.c b/libmailutils/base/wicket.c
index 4d79c2c..3e808c0 100644
--- a/libmailutils/base/wicket.c
+++ b/libmailutils/base/wicket.c
@@ -284,6 +284,14 @@ mu_wicket_stream_match_url (mu_stream_t stream, struct mu_debug_locus *loc,
 	  mu_url_destroy (&u);
 	  continue;
 	}
+
+      if (!mu_url_has_flag (u, MU_URL_USER|MU_URL_SECRET))
+	{
+	  mu_error (_("%s:%u: URL is missing required parts"),
+		    loc->file, loc->line);
+	  mu_url_destroy (&u);
+	  continue;
+	}
       
       if (!mu_url_matches_ticket (u, url, &n))
 	{
diff --git a/mh/mh_init.c b/mh/mh_init.c
index ca24686..9153476 100644
--- a/mh/mh_init.c
+++ b/mh/mh_init.c
@@ -1030,13 +1030,13 @@ mh_draft_message (const char *name, const char *msgspec, char **pname)
 char *
 mh_safe_make_file_name (const char *dir, const char *file)
 {
-  file = mu_make_file_name (dir, file);
-  if (!file)
+  char *name = mu_make_file_name (dir, file);
+  if (!name)
     {
       mu_diag_funcall (MU_DIAG_ERROR, "mu_make_file_name", NULL, ENOMEM);
       abort ();
     }
-  return file;
+  return name;
 }
 
 			  


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Tue Oct 26 06:39:30 2010
Received: from mailman by lists.gnu.org with archive (Exim 4.43)
	id 1PAgwE-00017H-1x
	for mharc-commit-mailutils@gnu.org; Tue, 26 Oct 2010 06:39:30 -0400
Received: from [140.186.70.92] (port=37483 helo=eggs.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.43) id 1PAgw3-000150-Vp
	for commit-mailutils@gnu.org; Tue, 26 Oct 2010 06:39:28 -0400
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1PAgvx-00088z-38
	for commit-mailutils@gnu.org; Tue, 26 Oct 2010 06:39:19 -0400
Received: from colonialone.fsf.org ([140.186.70.51]:57451
	helo=internal.in.savannah.gnu.org)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1PAgvw-00088n-VU
	for commit-mailutils@gnu.org; Tue, 26 Oct 2010 06:39:13 -0400
Received: from [10.1.0.108] (helo=vcs-noshell.in.savannah.gnu.org)
	by internal.in.savannah.gnu.org with esmtp (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1PAgvw-0007T7-C0
	for commit-mailutils@gnu.org; Tue, 26 Oct 2010 10:39:12 +0000
Received: from gray by vcs-noshell.in.savannah.gnu.org with local (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1PAgvu-0003tb-Ux
	for commit-mailutils@gnu.org; Tue, 26 Oct 2010 10:39:12 +0000
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: 6203ae65f53758a4b893e6e176be7c153a31bef0
X-Git-Newrev: 3327a23a49e532c068972a0b2d64021361540f7b
Message-Id: <E1PAgvu-0003tb-Ux@vcs-noshell.in.savannah.gnu.org>
Date: Tue, 26 Oct 2010 10:39:11 +0000
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
Subject: [SCM] GNU Mailutils branch, master,
	updated. release-2.2-160-g3327a23
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.5
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=unsubscribe>
List-Archive: <http://lists.gnu.org/archive/html/commit-mailutils>
List-Post: <mailto:commit-mailutils@gnu.org>
List-Help: <mailto:commit-mailutils-request@gnu.org?subject=help>
List-Subscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Tue, 26 Oct 2010 10:39:28 -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=3327a23a49e532c068972a0b2d64021361540f7b

The branch, master has been updated
       via  3327a23a49e532c068972a0b2d64021361540f7b (commit)
      from  6203ae65f53758a4b893e6e176be7c153a31bef0 (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 3327a23a49e532c068972a0b2d64021361540f7b
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Tue Oct 26 13:00:52 2010 +0300

    Rewrite URL support.
    
    The purpose is to make it modular and flexible.  URLs are
    parsed out as they are created.  Missing URL parts can be
    supplied via a "URL hint" at creation time (similar to
    the approach used in creating mu_address_t).  Ports
    can be specified either as numbers or as service names.
    Original port string representation can be retrieved from
    the URL, as well as its numeric value.
    
    * libmailutils/url/accessor.h: New file.
    * libmailutils/url/copy.c: New file.
    * libmailutils/url/create.c: New file.
    * libmailutils/url/decode.c: New file.
    * libmailutils/url/destroy.c: New file.
    * libmailutils/url/dup.c: New file.
    * libmailutils/url/expand.c: New file.
    * libmailutils/url/flag.c: New file.
    * libmailutils/url/get-auth.c: New file.
    * libmailutils/url/get-host.c: New file.
    * libmailutils/url/get-param.c: New file.
    * libmailutils/url/get-path.c: New file.
    * libmailutils/url/get-portstr.c: New file.
    * libmailutils/url/get-query.c: New file.
    * libmailutils/url/get-scheme.c: New file.
    * libmailutils/url/get-secret.c: New file.
    * libmailutils/url/get-user.c: New file.
    * libmailutils/url/match.c: New file.
    * libmailutils/url/port.c: New file.
    * libmailutils/url/scheme.c: New file.
    * libmailutils/url/uplevel.c: New file.
    * libmailutils/url/urlstr.c: New file.
    
    * configure.ac (AC_CONFIG_FILES): Add libmailutils/url/Makefile
    * libmailutils/Makefile.am (SUBDIRS): Add url.
    (libmailutils_la_LIBADD): Link with liburl.
    * libmailutils/base/Makefile.am (libbase_la_SOURCES): Remove url.c
    * libmailutils/base/url.c: Remove.
    
    * libmailutils/string/Makefile.am (libstring_la_SOURCES): Add xdecode.c
    * libmailutils/string/xdecode.c: New file.
    
    * include/mailutils/sys/url.h (_mu_url): Change type to short.
    <_get_port>: Change second argument to unsigned.
    <_get_portstr>: New method.
    * include/mailutils/url.h (MU_URL_SCHEME): New flag.
    (MU_URL_PARSE_HEXCODE, MU_URL_PARSE_HIDEPASS)
    (MU_URL_PARSE_PORTSRV, MU_URL_PARSE_PORTWC)
    (MU_URL_PARSE_PIPE, MU_URL_PARSE_SLASH): New flags.
    (mu_url_create_hint, mu_url_copy_hints): New prototypes.
    (mu_url_parse): Remove.
    (mu_url_get_port): Change second argument to unsigned.
    (mu_url_decode_len,mu_url_decode): Remove.
    (mu_url_decode): New proto.
    (mu_url_sget_portstr, mu_url_aget_portstr)
    (mu_url_get_portstr): New protos.
    
    * include/mailutils/util.h (mu_str_url_decode)
    (mu_str_url_decode_inline): New protos.
    * libproto/pop/mbox.c (pop_open): Port is unsigned.
    
    * libproto/imap/folder.c: Use MU_URL_SCHEME in url_may_have.
    * libproto/maildir/folder.c: Likewise.
    * libproto/mailer/prog.c: Likewise.
    * libproto/mailer/remote.c: Likewise.
    * libproto/mailer/sendmail.c: Likewise.
    * libproto/mailer/smtp.c: Likewise.
    * libproto/mbox/folder.c: Likewise.
    * libproto/mh/folder.c: Likewise.
    * libproto/nntp/folder.c: Likewise.
    * libproto/pop/folder.c: Likewise.
    
    * imap4d/imap4d.c: Remove calls to mu_url_parse.
    * libmailutils/base/registrar.c: Likewise.
    * libmailutils/base/wicket.c: Likewise.
    * libmailutils/mailbox/folder.c: Likewise.
    * libmailutils/mailbox/mailbox.c: Likewise.
    * libmailutils/mailer/mailer.c: Likewise.
    * libmailutils/tests/url-parse.c: Likewise.
    * libmailutils/tests/wicket.c: Likewise.
    * libproto/mailer/smtp_auth.c: Likewise.
    * maidag/deliver.c: Likewise.
    * mu/wicket.c: Likewise.
    
    * libmailutils/mime/mimehdr.c (mu_mimehdr_decode_param): Use
    mu_str_url_decode, instead of mu_url_decode.
    * libmailutils/stream/tcp.c (_tcp_instance)<port>: Change type
    to unsigned short. All uses updated.
    (mu_tcp_stream_create_with_source_ip)
    (mu_tcp_stream_create_with_source_host)
    (mu_tcp_stream_create): Port is unsigned.
    * include/mailutils/stream.h (mu_tcp_stream_create_with_source_ip)
    (mu_tcp_stream_create_with_source_host)
    (mu_tcp_stream_create): Port is unsigned.
    * include/mailutils/cpp/url.h (get_port): Return unsigned.
    * libmu_cpp/url.cc (get_port): Return unsigned.
    (parse): Empty function. Schedule for removal.
    * python/libmu_py/url.c (api_url_parse): Empty function.
    Schedule for removal.
    (api_url_get_port): Port is unsigned.
    
    * libmailutils/base/wicket.c (mu_wicket_file_match_url)
    (mu_wicket_file_match_url): New parameter: parse_flags.
    * mu/wicket.c (wicket_match): Use parse_flags to control
    whether or not to show the plaintext password.
    
    * doc/texinfo/url.texi: Update.

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

Summary of changes:
 configure.ac                                       |    1 +
 doc/texinfo/url.texi                               |    2 +-
 imap4d/imap4d.c                                    |    9 +-
 include/mailutils/auth.h                           |    4 +-
 include/mailutils/cpp/url.h                        |    2 +-
 include/mailutils/stream.h                         |    6 +-
 include/mailutils/sys/url.h                        |    6 +-
 include/mailutils/url.h                            |   52 +-
 include/mailutils/util.h                           |    3 +-
 libmailutils/Makefile.am                           |    5 +-
 libmailutils/base/Makefile.am                      |    1 -
 libmailutils/base/registrar.c                      |    4 +-
 libmailutils/base/url.c                            | 1254 --------------------
 libmailutils/base/wicket.c                         |   20 +-
 libmailutils/mailbox/folder.c                      |    4 +-
 libmailutils/mailbox/mailbox.c                     |    8 +-
 libmailutils/mailer/mailer.c                       |    4 +-
 libmailutils/mime/mimehdr.c                        |    8 +-
 libmailutils/stream/tcp.c                          |   11 +-
 libmailutils/string/Makefile.am                    |    3 +-
 libmailutils/string/{cpystr.c => xdecode.c}        |   57 +-
 libmailutils/tests/url-parse.c                     |    9 +-
 libmailutils/tests/wicket.c                        |    7 +-
 libmailutils/{diag => url}/Makefile.am             |   47 +-
 libmailutils/url/accessor.h                        |  143 +++
 libmailutils/url/copy.c                            |  145 +++
 libmailutils/url/create.c                          |  525 ++++++++
 libmailutils/url/decode.c                          |  118 ++
 libproto/nntp/url.c => libmailutils/url/destroy.c  |   62 +-
 libproto/nntp/url.c => libmailutils/url/dup.c      |   44 +-
 libmailutils/url/expand.c                          |  216 ++++
 libmailutils/{base/freeitem.c => url/flag.c}       |   22 +-
 libmailutils/url/get-auth.c                        |    3 +
 libmailutils/url/get-host.c                        |    2 +
 libmailutils/url/get-param.c                       |  131 ++
 libmailutils/url/get-path.c                        |    2 +
 libmailutils/url/get-portstr.c                     |    2 +
 libproto/pop/url.c => libmailutils/url/get-query.c |   60 +-
 libmailutils/url/get-scheme.c                      |    2 +
 .../nntp/url.c => libmailutils/url/get-secret.c    |   32 +-
 libmailutils/url/get-user.c                        |    2 +
 libmailutils/url/match.c                           |  102 ++
 libproto/nntp/url.c => libmailutils/url/port.c     |   35 +-
 libproto/nntp/url.c => libmailutils/url/scheme.c   |   37 +-
 libmailutils/{mailbox/hdrfirst.c => url/uplevel.c} |   57 +-
 libmailutils/{base/freeitem.c => url/urlstr.c}     |   11 +-
 libmu_cpp/url.cc                                   |   10 +-
 libproto/imap/folder.c                             |    6 +-
 libproto/maildir/folder.c                          |    2 +-
 libproto/mailer/prog.c                             |    2 +-
 libproto/mailer/remote.c                           |    6 +-
 libproto/mailer/sendmail.c                         |    6 +-
 libproto/mailer/smtp.c                             |    4 +-
 libproto/mailer/smtp_auth.c                        |    9 -
 libproto/mbox/folder.c                             |    2 +-
 libproto/mh/folder.c                               |    2 +-
 libproto/nntp/folder.c                             |    4 +-
 libproto/pop/folder.c                              |    4 +-
 libproto/pop/mbox.c                                |    2 +-
 maidag/deliver.c                                   |   15 -
 mu/wicket.c                                        |   34 +-
 python/libmu_py/url.c                              |    8 +-
 62 files changed, 1801 insertions(+), 1595 deletions(-)
 delete mode 100644 libmailutils/base/url.c
 copy libmailutils/string/{cpystr.c => xdecode.c} (57%)
 copy libmailutils/{diag => url}/Makefile.am (72%)
 create mode 100644 libmailutils/url/accessor.h
 create mode 100644 libmailutils/url/copy.c
 create mode 100644 libmailutils/url/create.c
 create mode 100644 libmailutils/url/decode.c
 copy libproto/nntp/url.c => libmailutils/url/destroy.c (51%)
 copy libproto/nntp/url.c => libmailutils/url/dup.c (61%)
 create mode 100644 libmailutils/url/expand.c
 copy libmailutils/{base/freeitem.c => url/flag.c} (75%)
 create mode 100644 libmailutils/url/get-auth.c
 create mode 100644 libmailutils/url/get-host.c
 create mode 100644 libmailutils/url/get-param.c
 create mode 100644 libmailutils/url/get-path.c
 create mode 100644 libmailutils/url/get-portstr.c
 copy libproto/pop/url.c => libmailutils/url/get-query.c (52%)
 create mode 100644 libmailutils/url/get-scheme.c
 copy libproto/nntp/url.c => libmailutils/url/get-secret.c (66%)
 create mode 100644 libmailutils/url/get-user.c
 create mode 100644 libmailutils/url/match.c
 copy libproto/nntp/url.c => libmailutils/url/port.c (62%)
 copy libproto/nntp/url.c => libmailutils/url/scheme.c (60%)
 copy libmailutils/{mailbox/hdrfirst.c => url/uplevel.c} (51%)
 copy libmailutils/{base/freeitem.c => url/urlstr.c} (82%)

diff --git a/configure.ac b/configure.ac
index 27ff3f6..690171b 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1387,6 +1387,7 @@ AC_CONFIG_FILES([
  libmailutils/server/Makefile
  libmailutils/string/Makefile
  libmailutils/stream/Makefile
+ libmailutils/url/Makefile
  libmailutils/Makefile
  messages/Makefile
  mh/Makefile
diff --git a/doc/texinfo/url.texi b/doc/texinfo/url.texi
index 267a89f..1399731 100644
--- a/doc/texinfo/url.texi
+++ b/doc/texinfo/url.texi
@@ -186,7 +186,7 @@ for any particular scheme.
 @deftypefun  int mu_url_get_host (const mu_url_t, char *, size_t, size_t *)
 @end deftypefun
 
-@deftypefun  int mu_url_get_port (const mu_url_t, long *)
+@deftypefun  int mu_url_get_port (const mu_url_t, unsigned *)
 @end deftypefun
 
 @deftypefun  int mu_url_get_path (const mu_url_t, char *, size_t, size_t *)
diff --git a/imap4d/imap4d.c b/imap4d/imap4d.c
index c80af2b..cbadb83 100644
--- a/imap4d/imap4d.c
+++ b/imap4d/imap4d.c
@@ -172,7 +172,7 @@ parse_preauth_scheme (mu_debug_t debug, const char *scheme, mu_url_t url)
   else if (strcmp (scheme, "ident") == 0)
     {
       struct servent *sp;
-      long n;
+      unsigned n;
       if (url && mu_url_get_port (url, &n) == 0)
 	ident_port = (short) n;
       else if ((sp = getservbyname ("auth", "tcp")))
@@ -219,13 +219,6 @@ cb_preauth (mu_debug_t debug, void *data, mu_config_value_t *val)
 	  mu_diag_funcall (MU_DIAG_ERROR, "mu_url_create", val->v.string, rc);
 	  return 1;
 	}
-      rc = mu_url_parse (url);
-      if (rc)
-	{
-	  mu_cfg_format_error (debug, MU_DEBUG_ERROR,
-			       "%s: %s", val->v.string, mu_strerror (rc));
-	  return 1;
-	}
 
       rc = mu_url_aget_scheme (url, &scheme);
       if (rc)
diff --git a/include/mailutils/auth.h b/include/mailutils/auth.h
index b3b7be3..3433b61 100644
--- a/include/mailutils/auth.h
+++ b/include/mailutils/auth.h
@@ -78,8 +78,10 @@ int mu_file_wicket_create (mu_wicket_t *pwicket, const char *filename);
 
 struct mu_debug_locus;  
 int mu_wicket_stream_match_url (mu_stream_t stream, struct mu_debug_locus *loc,
-				mu_url_t url, mu_url_t *pticket_url);
+				mu_url_t url, int parse_flags,
+				mu_url_t *pticket_url);
 int mu_wicket_file_match_url (const char *name, mu_url_t url,
+			      int parse_flags,
 			      mu_url_t *pticket_url);
 
   
diff --git a/include/mailutils/cpp/url.h b/include/mailutils/cpp/url.h
index dd7d065..b3b7138 100644
--- a/include/mailutils/cpp/url.h
+++ b/include/mailutils/cpp/url.h
@@ -42,7 +42,7 @@ class Url
   ~Url ();
 
   void parse ();
-  long get_port ();
+  unsigned get_port ();
   std::string get_scheme ();
   std::string get_user ();
   std::string get_auth ();
diff --git a/include/mailutils/stream.h b/include/mailutils/stream.h
index d951f34..bb14186 100644
--- a/include/mailutils/stream.h
+++ b/include/mailutils/stream.h
@@ -176,14 +176,14 @@ int mu_streamref_create_abridged (mu_stream_t *pref, mu_stream_t str,
 int mu_streamref_create (mu_stream_t *pref, mu_stream_t str);
 
 int mu_tcp_stream_create_with_source_ip (mu_stream_t *stream,
-					 const char *host, int port,
+					 const char *host, unsigned port,
 					 unsigned long source_ip,
 					 int flags);
 int mu_tcp_stream_create_with_source_host (mu_stream_t *stream,
-					   const char *host, int port,
+					   const char *host, unsigned port,
 					   const char *source_host,
 					   int flags);
-int mu_tcp_stream_create (mu_stream_t *stream, const char *host, int port,
+int mu_tcp_stream_create (mu_stream_t *stream, const char *host, unsigned port,
 			  int flags);
 
 /* Transcript output levels */
diff --git a/include/mailutils/sys/url.h b/include/mailutils/sys/url.h
index f782bb0..8e80197 100644
--- a/include/mailutils/sys/url.h
+++ b/include/mailutils/sys/url.h
@@ -35,7 +35,8 @@ struct _mu_url
   mu_secret_t secret;
   char  *auth;
   char  *host;
-  long  port;
+  short port;
+  char  *portstr;
   char  *path;
   char  **fvpairs;
   int   fvcount;
@@ -53,7 +54,8 @@ struct _mu_url
   int   (*_get_secret) (const mu_url_t, mu_secret_t *);
   int   (*_get_auth)   (const mu_url_t, char *, size_t, size_t *);
   int   (*_get_host)   (const mu_url_t, char *, size_t, size_t *);
-  int   (*_get_port)   (const mu_url_t, long *);
+  int   (*_get_port)   (const mu_url_t, unsigned *);
+  int   (*_get_portstr)(const mu_url_t, char *, size_t, size_t *);
   int   (*_get_path)   (const mu_url_t, char *, size_t, size_t *);
   int   (*_get_query)  (const mu_url_t, char *, size_t, size_t *);
   int   (*_uplevel)    (const mu_url_t, mu_url_t *);
diff --git a/include/mailutils/url.h b/include/mailutils/url.h
index b8745bb..dc4801d 100644
--- a/include/mailutils/url.h
+++ b/include/mailutils/url.h
@@ -25,14 +25,15 @@
 extern "C" {
 #endif
 
-#define MU_URL_USER   0x0001 /* Has a user part */
-#define MU_URL_SECRET 0x0002 /* Has a secret (password) part */
-#define MU_URL_AUTH   0x0004 /* Has auth part */
-#define MU_URL_HOST   0x0008 /* Has host part */
-#define MU_URL_PORT   0x0010 /* Has port part */  
-#define MU_URL_PATH   0x0020 /* Has path */
-#define MU_URL_PARAM  0x0040 /* Has parameters */
-#define MU_URL_QUERY  0x0080 /* Has query */
+#define MU_URL_SCHEME 0x0001
+#define MU_URL_USER   0x0002 /* Has a user part */
+#define MU_URL_SECRET 0x0004 /* Has a secret (password) part */
+#define MU_URL_AUTH   0x0008 /* Has auth part */
+#define MU_URL_HOST   0x0010 /* Has host part */
+#define MU_URL_PORT   0x0020 /* Has port part */  
+#define MU_URL_PATH   0x0040 /* Has path */
+#define MU_URL_PARAM  0x0080 /* Has parameters */
+#define MU_URL_QUERY  0x0100 /* Has query */
   
 #define MU_URL_CRED (MU_URL_USER | MU_URL_SECRET | MU_URL_AUTH)  
   /* Has some of authentication credentials */
@@ -45,6 +46,28 @@ extern "C" {
    MU_URL_PARAM | \
    MU_URL_QUERY)
 
+  /* Parser flags */
+#define MU_URL_PARSE_HEXCODE      0x0001  /* Decode % notations (RFC 1738,
+					     section 2.2) */
+#define MU_URL_PARSE_HIDEPASS     0x0002  /* Hide password in the URL */
+#define MU_URL_PARSE_PORTSRV      0x0004  /* Use getservbyname to determine
+					     port number */
+#define MU_URL_PARSE_PORTWC       0x0008  /* Allow wildcard (*) as a port
+					    number (for tickets) */
+#define MU_URL_PARSE_PIPE         0x0010  /* Translate "| ..." to
+					    "prog://..." */
+#define MU_URL_PARSE_SLASH        0x0020  /* Translate "/..." to
+					     "file:///..." */
+
+#define MU_URL_PARSE_DEFAULT \
+  (MU_URL_PARSE_HEXCODE|MU_URL_PARSE_HIDEPASS|MU_URL_PARSE_PORTSRV|\
+   MU_URL_PARSE_PIPE|MU_URL_PARSE_SLASH)
+#define MU_URL_PARSE_ALL (MU_URL_PARSE_DEFAULT|MU_URL_PARSE_PORTWC)  
+  
+int mu_url_create_hint (mu_url_t *purl, const char *str, int flags,
+			mu_url_t hint);
+int mu_url_copy_hints (mu_url_t url, mu_url_t hint);
+  
 int  mu_url_create    (mu_url_t *, const char *name);
 int  mu_url_dup       (mu_url_t old_url, mu_url_t *new_url);
 int  mu_url_uplevel   (mu_url_t url, mu_url_t *upurl);
@@ -53,7 +76,6 @@ int mu_url_get_flags (mu_url_t, int *);
 int mu_url_has_flag (mu_url_t, int);  
   
 void mu_url_destroy   (mu_url_t *);
-int  mu_url_parse     (mu_url_t);
 
 int mu_url_sget_scheme  (const mu_url_t, const char **);
 int mu_url_aget_scheme  (const mu_url_t, char **);  
@@ -80,7 +102,11 @@ int mu_url_get_path  (const mu_url_t, char *, size_t, size_t *);
 int mu_url_sget_query (const mu_url_t url, size_t *qc, char ***qv);
 int mu_url_aget_query (const mu_url_t url, size_t *qc, char ***qv);
 
-int mu_url_get_port    (const mu_url_t, long *);
+int mu_url_sget_portstr  (const mu_url_t, const char **);
+int mu_url_aget_portstr  (const mu_url_t, char **);  
+int mu_url_get_portstr   (const mu_url_t, char *, size_t, size_t *);
+  
+int mu_url_get_port    (const mu_url_t, unsigned *);
 
 int mu_url_sget_fvpairs (const mu_url_t url, size_t *fvc, char ***fvp);
 int mu_url_aget_fvpairs (const mu_url_t url, size_t *pfvc, char ***pfvp);
@@ -101,11 +127,9 @@ int mu_url_is_same_path   (mu_url_t, mu_url_t);
 int mu_url_is_same_host   (mu_url_t, mu_url_t);
 int mu_url_is_same_port   (mu_url_t, mu_url_t);
 
-char *mu_url_decode_len (const char *s, size_t len);  
-char *mu_url_decode     (const char *s);
-
 int mu_url_matches_ticket   (mu_url_t ticket, mu_url_t url, int *wcn);  
-int mu_url_init (mu_url_t url, int port, const char *scheme);
+
+int mu_url_decode (mu_url_t url);
   
 #ifdef __cplusplus
 }
diff --git a/include/mailutils/util.h b/include/mailutils/util.h
index 3af373c..9d3fff4 100644
--- a/include/mailutils/util.h
+++ b/include/mailutils/util.h
@@ -42,7 +42,8 @@ int mu_unre_set_regex (const char *str, int caseflag, char **errp);
 int mu_unre_subject  (const char *subject, const char **new_subject);
 int mu_is_proto (const char *p);
 int mu_mh_delim (const char *str);
-
+void mu_str_url_decode_inline (char *str);
+int mu_str_url_decode (char **ptr, const char *s);
 
   /* ----------------------- */
   /* Date & time functions   */
diff --git a/libmailutils/Makefile.am b/libmailutils/Makefile.am
index f13085f..454962b 100644
--- a/libmailutils/Makefile.am
+++ b/libmailutils/Makefile.am
@@ -17,7 +17,7 @@
 # <http://www.gnu.org/licenses/>.
 
 SUBDIRS = auth base address cfg diag filter mailbox mailer mime\
- server string stream . tests 
+ server string stream url . tests 
 
 lib_LTLIBRARIES = libmailutils.la
 
@@ -36,7 +36,8 @@ libmailutils_la_LIBADD = \
  mime/libmime.la\
  server/libserver.la\
  string/libstring.la\
- stream/libstream.la
+ stream/libstream.la\
+ url/liburl.la
 
 libmailutils_la_LDFLAGS = -version-info @VI_CURRENT@:@VI_REVISION@:@VI_AGE@
 
diff --git a/libmailutils/base/Makefile.am b/libmailutils/base/Makefile.am
index 196344b..c51c947 100644
--- a/libmailutils/base/Makefile.am
+++ b/libmailutils/base/Makefile.am
@@ -61,7 +61,6 @@ libbase_la_SOURCES = \
  tempfile.c\
  ticket.c\
  tilde.c\
- url.c\
  usremail.c\
  vartab.c\
  version.c\
diff --git a/libmailutils/base/registrar.c b/libmailutils/base/registrar.c
index da43bff..fc31b90 100644
--- a/libmailutils/base/registrar.c
+++ b/libmailutils/base/registrar.c
@@ -220,9 +220,7 @@ mu_registrar_lookup (const char *name, int flags,
   rc = mu_url_create (&url, name);
   if (rc)
     return rc;
-  rc = mu_url_parse (url);
-  if (rc == 0)
-    rc = mu_registrar_lookup_url (url, flags, precord, pflags);
+  rc = mu_registrar_lookup_url (url, flags, precord, pflags);
   mu_url_destroy (&url);
   return rc;
 }
diff --git a/libmailutils/base/url.c b/libmailutils/base/url.c
deleted file mode 100644
index 96345f0..0000000
--- a/libmailutils/base/url.c
+++ /dev/null
@@ -1,1254 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 1999, 2000, 2001, 2007, 2008, 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, see 
-   <http://www.gnu.org/licenses/>. */
-
-#ifdef HAVE_CONFIG_H
-# include <config.h>
-#endif
-
-#include <ctype.h>
-#include <errno.h>
-#include <stdlib.h>
-#include <string.h>
-#ifdef HAVE_STRINGS_H
-# include <strings.h>
-#endif
-
-#include <mailutils/util.h>
-#include <mailutils/errno.h>
-#include <mailutils/argcv.h>
-#include <mailutils/secret.h>
-#include <mailutils/cctype.h>
-#include <mailutils/cstr.h>
-#include <mailutils/sys/url.h>
-
-#define AC2(a,b) a ## b
-#define AC4(a,b,c,d) a ## b ## c ## d
-
-static int url_parse0 (mu_url_t, char *, size_t *poff, int *decode);
-
-static int
-parse_query (const char *query,
-	     char *delim,
-	     int *pargc, char ***pargv, const char **pend)
-{
-  size_t count, i;
-  char **v;
-  const char *p;
-
-  for (p = query, count = 0; ; count++)
-    {
-      size_t len = strcspn (p, delim);
-      p += len;
-      if (!*p || *p == delim[1])
-	break;
-      p++;
-    }
-
-  if (pend)
-    *pend = p;
-  if (p == query)
-    return 0;
-  count++;
-
-  v = calloc (count + 1, sizeof (v[0]));
-  for (i = 0, p = query; i < count; i++)
-    {
-      size_t len = strcspn (p, delim);
-      v[i] = mu_url_decode_len (p, len);
-      if (v[i] == NULL)
-	{
-	  mu_argcv_free (i, v);
-	  return 1;
-	}
-      p += len + 1;
-    }
-  v[i] = NULL;
-
-  *pargc = count;
-  *pargv = v;
-  return 0;
-}
-
-int
-mu_url_create (mu_url_t *purl, const char *name)
-{
-  mu_url_t url = calloc (1, sizeof (*url));
-  if (url == NULL)
-    return ENOMEM;
-
-  url->name = strdup (name);
-  if (url->name == NULL)
-    {
-      free (url);
-      return ENOMEM;
-    }
-  *purl = url;
-  return 0;
-}
-
-static char **
-argcv_copy (size_t argc, char **argv)
-{
-  size_t i;
-  char **nv = calloc (argc + 1, sizeof (nv[0]));
-  if (!nv)
-    return NULL;
-  for (i = 0; i < argc; i++)
-    if ((nv[i] = strdup (argv[i])) == NULL)
-      {
-	mu_argcv_free (i, nv);
-	free (nv);
-	return NULL;
-      }
-  return nv;
-}
-
-static int
-mu_url_copy0 (mu_url_t old_url, mu_url_t new_url)
-{
-  const char *str;
-  size_t argc;
-  char **argv;
-  int rc;
-  mu_secret_t sec;
-
-#define URLCOPY(what)						\
-  do								\
-    {								\
-      rc = AC2(mu_url_sget_,what) (old_url, &str);		\
-      if (rc == 0)						\
-	{							\
-	  if ((new_url->what = strdup (str)) == NULL)		\
-	    return ENOMEM;					\
-	}							\
-      else if (rc != MU_ERR_NOENT)				\
-	return rc;						\
-    }								\
-  while (0);
-
-  URLCOPY (scheme);
-  URLCOPY (user);
-
-  rc = mu_url_get_secret (old_url, &sec);
-  if (rc == MU_ERR_NOENT)
-    new_url->secret = NULL;
-  else if (rc)
-    return rc;
-  else
-    {
-      rc = mu_secret_dup (sec, &new_url->secret);
-      if (rc)
-	return rc;
-    }
-  
-  URLCOPY (auth);
-  URLCOPY (host);
-  new_url->port = old_url->port;
-  URLCOPY (path);
-
-  rc = mu_url_sget_fvpairs (old_url, &argc, &argv);
-  if (rc == 0 && argc)
-    {
-      if ((new_url->fvpairs = argcv_copy (argc, argv)) == NULL)
-	return ENOMEM;
-      new_url->fvcount = argc;
-    }
-
-  rc = mu_url_sget_query (old_url, &argc, &argv);
-  if (rc == 0 && argc)
-    {
-      if ((new_url->qargv = argcv_copy (argc, argv)) == NULL)
-	return ENOMEM;
-      new_url->qargc = argc;
-    }
-  new_url->flags = old_url->flags;
-  return 0;
-#undef URLCOPY
-}
-
-int
-mu_url_dup (mu_url_t old_url, mu_url_t *new_url)
-{
-  mu_url_t url;
-  int rc = mu_url_create (&url, mu_url_to_string (old_url));
-
-  if (rc)
-    return rc;
-
-  rc = mu_url_copy0 (old_url, url);
-  if (rc == 0)
-    *new_url = url;
-  else
-    mu_url_destroy (&url);
-  return rc;
-}
-
-int
-mu_url_uplevel (mu_url_t url, mu_url_t *upurl)
-{
-  int rc;
-  char *p;
-  mu_url_t new_url;
-
-  if (url->_uplevel)
-    return url->_uplevel (url, upurl);
-
-  if (!url->path)
-    return MU_ERR_NOENT;
-  p = strrchr (url->path, '/');
-
-  rc = mu_url_dup (url, &new_url);
-  if (rc == 0)
-    {
-      if (!p || p == url->path)
-	{
-	  free (new_url->path);
-	  new_url->path = NULL;
-	}
-      else
-	{
-	  size_t size = p - url->path;
-	  new_url->path = realloc (new_url->path, size + 1);
-	  if (!new_url->path)
-	    {
-	      mu_url_destroy (&new_url);
-	      return ENOMEM;
-	    }
-	  memcpy (new_url->path, url->path, size);
-	  new_url->path[size] = 0;
-	}
-      *upurl = new_url;
-    }
-  return rc;
-}
-
-void
-mu_url_destroy (mu_url_t * purl)
-{
-  if (purl && *purl)
-    {
-      mu_url_t url = (*purl);
-
-      if (url->_destroy)
-	url->_destroy (url);
-
-      if (url->name)
-	free (url->name);
-
-      if (url->scheme)
-	free (url->scheme);
-
-      if (url->user)
-	free (url->user);
-
-      mu_secret_destroy (&url->secret);
-
-      if (url->auth)
-	free (url->auth);
-
-      if (url->host)
-	free (url->host);
-
-      if (url->path)
-	free (url->path);
-
-      if (url->fvcount)
-	mu_argcv_free (url->fvcount, url->fvpairs);
-
-      mu_argcv_free (url->qargc, url->qargv);
-
-      free (url);
-
-      *purl = NULL;
-    }
-}
-
-int
-mu_url_parse (mu_url_t url)
-{
-  int err = 0;
-  char *n = NULL;
-  struct _mu_url u;
-  size_t pstart;
-  mu_secret_t newsec;
-  int want_decode;
-  
-  if (!url || !url->name)
-    return EINVAL;
-
-  memset (&u, 0, sizeof u);
-  /* can't have been parsed already */
-  if (url->flags)
-    return EINVAL;
-
-  n = strdup (url->name);
-
-  if (!n)
-    return ENOMEM;
-
-  err = url_parse0 (&u, n, &pstart, &want_decode);
-
-  if (!err)
-    {
-      if (u.secret)
-	{
-	  /* Obfuscate the password */
-#define PASS_REPL "***"
-#define PASS_REPL_LEN (sizeof (PASS_REPL) - 1)
-	  size_t plen = mu_secret_length (u.secret);
-	  size_t nlen = strlen (url->name);
-	  size_t len = nlen - plen + PASS_REPL_LEN + 1;
-	  char *newname;
-
-	  memset (url->name + pstart, 0, plen);
-	  if (len > nlen + 1)
-	    {
-	      newname = realloc (url->name, len);
-	      if (!newname)
-		goto CLEANUP;
-	      url->name = newname;
-	    }
-	  else
-	    newname = url->name;
-	  memmove (newname + pstart + PASS_REPL_LEN, newname + pstart + plen,
-		   nlen - (pstart + plen) + 1);
-	  memcpy (newname + pstart, PASS_REPL, PASS_REPL_LEN);
-	}
-
-      /* Dup the strings we found. We wouldn't have to do this
-	 if we did a single alloc of the source url name, and
-	 kept it around. It's also a good time to do hex decoding,
-	 though.
-       */
-
-#define UALLOC(X,f)							\
-      if (u.X && u.X[0])						\
-	{								\
-	  url->X = want_decode ? mu_url_decode (u.X) : strdup (u.X);	\
-	  if (!url->X)							\
-	    {								\
-	      err = ENOMEM;						\
-	      goto CLEANUP;						\
-	    }								\
-	  url->flags |= f;						\
-	}								\
-      else								\
-	{								\
-	  /* Set zero-length strings to NULL. */			\
-	  url->X = NULL;						\
-	}
-
-      UALLOC (scheme, 0);
-      UALLOC (user, MU_URL_USER);
-
-      if (u.secret)
-	{
-	  char *pass = mu_url_decode (mu_secret_password (u.secret));
-	  err = mu_secret_create (&newsec, pass, strlen (pass));
-	  memset (pass, 0, strlen (pass));
-	  mu_secret_destroy (&u.secret);
-	  if (err)
-	    goto CLEANUP;
-
-	  url->secret = newsec;
-	  url->flags |= MU_URL_SECRET;
-	}
-
-      UALLOC (auth, MU_URL_AUTH);
-      UALLOC (host, MU_URL_HOST);
-      UALLOC (path, MU_URL_PATH);
-
-#undef UALLOC
-      url->fvcount = u.fvcount;
-      url->fvpairs = u.fvpairs;
-      if (u.fvcount)
-	url->flags |= MU_URL_PARAM;
-	  
-      url->qargc = u.qargc;
-      url->qargv = u.qargv;
-      if (u.qargc)
-	url->flags |= MU_URL_QUERY;
-
-      url->port = u.port;
-      if (u.port)
-	url->flags |= MU_URL_PORT;
-    }
-
-CLEANUP:
-  memset (n, 0, strlen (n));
-  free (n);
-
-  if (err)
-    {
-#define UFREE(X) if (X) { free(X); X = 0; }
-
-      UFREE (url->scheme);
-      UFREE (url->user);
-      mu_secret_destroy (&u.secret);
-      UFREE (url->auth);
-      UFREE (url->host);
-      UFREE (url->path);
-      mu_argcv_free (url->fvcount, url->fvpairs);
-      mu_argcv_free (url->qargc, url->qargv);
-#undef UFREE
-    }
-
-  return err;
-}
-
-/*
-
-Syntax, condensed from RFC 1738, and extended with the ;auth=
-of RFC 2384 (for POP) and RFC 2192 (for IMAP):
-
-url =
-    scheme ":" [ "//"
-
-    [ user [ ( ":" password ) | ( ";auth=" auth ) ] "@" ]
-
-    host [ ":" port ]
-
-    [ ( "/" urlpath ) | ( "?" query ) ] ]
-
-All hell will break loose in this parser if the user/pass/auth
-portion is missing, and the urlpath has any @ or : characters
-in it. A imap mailbox, say, named after the email address of
-the person the mail is from:
-
-  imap://imap.uniserve.com/alain@qnx.com
-
-Is this required to be % quoted, though? I hope so!
-
-*/
-
-static int
-url_parse0 (mu_url_t u, char *name, size_t *poff, int *decode)
-{
-  char *start = name;
-  char *p;			/* pointer into name */
-
-  /* reject the obvious */
-  if (name == NULL)
-    return EINVAL;
-
-  if (name[0] == '/')
-    {
-      u->scheme = "file";
-      *decode = 0;
-    }
-  else if (name[0] == '|')
-    {
-      int rc;
-      u->scheme = "prog";
-      *decode = 0;
-      rc = mu_argcv_get (name + 1, NULL, NULL, &u->qargc, &u->qargv);
-      if (rc == 0)
-	{
-	  u->path = strdup (u->qargv[0]);
-	  if (!u->path)
-	    rc = ENOMEM;
-	}
-      return rc;
-    }
-  else
-    {
-      *decode = 1;
-      /* Parse out the SCHEME. */
-      p = strchr (name, ':');
-      if (p == NULL)
-	return MU_ERR_PARSE;
-
-      *p++ = 0;
-
-      u->scheme = name;
-
-      /* RFC 1738, section 2.1, lower the scheme case */
-      for (; name < p; name++)
-	*name = mu_tolower (*name);
-
-      name = p;
-    }
-
-  /* Check for nothing following the scheme. */
-  if (!*name)
-    return 0;
-
-  if (strncmp (name, "//", 2) == 0)
-    {
-      name += 2;
-
-      if (name[0] == '/')
-	{
-	  u->path = name;
-	  p = u->path + strcspn (u->path, ";?");
-	}
-      else
-	{
-	  /* Split into LHS and RHS of the '@', and then parse each side. */
-	  u->host = strchr (name, '@');
-	  if (u->host == NULL)
-	    u->host = name;
-	  else
-	    {
-	      char *pass = NULL;
-
-	      /* Parse the LHS into an identification/authentication pair. */
-	      *u->host++ = 0;
-
-	      u->user = name;
-
-	      /* Try to split the user into a:
-		 <user>:<password>
-		 or
-		 <user>:<password>;AUTH=<auth>
-	      */
-
-	      for (; *name; name++)
-		{
-		  if (*name == ':')
-		    {
-		      *name++ = 0;
-		      pass = name;
-		      *poff = pass - start;
-		    }
-		  else if (*name == ';')
-		    {
-		      /* Make sure it's the auth token. */
-		      if (mu_c_strncasecmp (name + 1, "auth=", 5) == 0)
-			{
-			  *name++ = 0;
-			  name += 5;
-			  u->auth = name;
-			  break;
-			}
-		    }
-		}
-
-	      if (pass)
-		{
-		  if (mu_secret_create (&u->secret, pass, strlen (pass)))
-		    return ENOMEM;
-		  else
-		    /* Obfuscate password */
-		    memset (pass, 0, strlen (pass));
-		}
-	    }
-
-	  /* Parse the host and port from the RHS. */
-	  p = strchr (u->host, ':');
-	  if (p)
-	    {
-	      *p++ = 0;
-	      u->port = strtol (p, &p, 10);
-
-	      /* Check for garbage after the port: we should be on the start
-		 of a path, a query, or at the end of the string. */
-	      if (*p && strcspn (p, "/?") != 0)
-		return MU_ERR_PARSE;
-	    }
-	  else
-	    p = u->host + strcspn (u->host, ";/?");
-	}
-    }
-  else
-    {
-      u->path = name;
-      p = u->path + strcspn (u->path, ";?");
-    }
-
-  /* Either way, if we're not at a nul, we're at a path or query. */
-  if (u->path == NULL && *p == '/')
-    {
-      /* found a path */
-      *p++ = 0;
-      u->path = p;
-      p = u->path + strcspn (u->path, ";?");
-    }
-
-  if (*p == ';')
-    {
-      *p++ = 0;
-      if (parse_query (p, ";?", &u->fvcount, &u->fvpairs, (const char **)&p))
-	return ENOMEM;
-    }
-
-  if (*p == '?')
-    {
-      /* found a query */
-      *p++ = 0;
-      if (parse_query (p, "&", &u->qargc, &u->qargv, NULL))
-	return ENOMEM;
-    }
-
-  return 0;
-}
-
-
-/* General accessors: */
-#define ACCESSOR(action,field) AC4(mu_url_,action,_,field)
-
-#define DECL_SGET(field)						  \
-int									  \
-ACCESSOR(sget,field) (mu_url_t url, char const **sptr)	                  \
-{									  \
-  if (url == NULL)							  \
-    return EINVAL;							  \
-  if (!url->field)							  \
-    {									  \
-      if (url->AC2(_get_,field))					  \
-	{								  \
-	  size_t n;							  \
-	  char *buf;							  \
-									  \
-	  int status = url->AC2(_get_,field) (url, NULL, 0, &n);	  \
-	  if (status)							  \
-	    return status;						  \
-									  \
-	  buf = malloc (n + 1);						  \
-	  if (!buf)							  \
-	    return ENOMEM;						  \
-									  \
-	  status = url->AC2(_get_,field) (url, buf, n + 1, NULL);	  \
-	  if (status)				          \
-	    return status;						  \
-									  \
-	  if (buf[0])                                                     \
-	    {                                                             \
-	       url->field = mu_url_decode (buf);			  \
-	       free (buf);						  \
-	    }                                                             \
-	  else                                                            \
-	    url->field = buf;                                             \
-	  if (!url->field)						  \
-	    return ENOMEM;						  \
-	}								  \
-      else								  \
-	return MU_ERR_NOENT;			                          \
-    }									  \
-  *sptr = url->field;							  \
-  return 0;								  \
-}
-
-#define DECL_GET(field)							  \
-int									  \
-ACCESSOR(get,field) (mu_url_t url, char *buf, size_t len, size_t *n)      \
-{									  \
-  size_t i;								  \
-  const char *str;							  \
-  int status = ACCESSOR(sget, field) (url, &str);			  \
-									  \
-  if (status)								  \
-    return status;							  \
-									  \
-  i = mu_cpystr (buf, str, len);					  \
-  if (n)								  \
-    *n = i;								  \
-  return 0;								  \
-}
-
-#define DECL_AGET(field)						  \
-int									  \
-ACCESSOR(aget, field) (mu_url_t url, char **buf)	                  \
-{									  \
-  const char *str;							  \
-  int status = ACCESSOR(sget, field) (url, &str);			  \
-									  \
-  if (status)								  \
-    return status;							  \
-									  \
-  if (str)								  \
-    {									  \
-      *buf = strdup (str);						  \
-      if (!*buf)							  \
-	status = ENOMEM;						  \
-    }									  \
-  else									  \
-    *buf = NULL;							  \
-  return status;							  \
-}
-
-#define DECL_CMP(field)							  \
-int									  \
-ACCESSOR(is_same,field) (mu_url_t url1, mu_url_t url2)		          \
-{									  \
-  const char *s1, *s2;							  \
-  int status1, status2;							  \
-									  \
-  status1 = ACCESSOR(sget, field) (url1, &s1);				  \
-  if (status1 && status1 != MU_ERR_NOENT)				  \
-    return 0;								  \
-  status2 = ACCESSOR(sget, field) (url2, &s2);				  \
-  if (status2 && status2 != MU_ERR_NOENT)				  \
-    return 0;								  \
-									  \
-  if (status1 || status2)						  \
-    return status1 == status2; /* Both fields are missing */		  \
-  return mu_c_strcasecmp (s1, s2) == 0;					  \
-}
-
-#define DECL_ACCESSORS(field)			                          \
-DECL_SGET(field)				                          \
-DECL_GET(field)					                          \
-DECL_AGET(field)                                                          \
-DECL_CMP(field)
-
-
-/* Declare particular accessors */
-DECL_ACCESSORS (scheme)
-DECL_ACCESSORS (user)
-DECL_ACCESSORS (auth)
-DECL_ACCESSORS (host)
-DECL_ACCESSORS (path)
-
-int
-mu_url_get_secret (const mu_url_t url, mu_secret_t *psecret)
-{
-  if (url->_get_secret)
-    return url->_get_secret (url, psecret);
-  if (url->secret == NULL)
-    return MU_ERR_NOENT;
-  mu_secret_ref (url->secret);
-  *psecret = url->secret;
-  return 0;
-}
-
-int
-mu_url_sget_query (const mu_url_t url, size_t *qc, char ***qv)
-{
-  if (url == NULL)
-    return EINVAL;
-  /* See FIXME below */
-  *qc = url->qargc;
-  *qv = url->qargv;
-  return 0;
-}
-
-int
-mu_url_aget_query (const mu_url_t url, size_t *qc, char ***qv)
-{
-  size_t qargc, i;
-  char **qargv;
-  char **qcopy;
-
-  int rc = mu_url_sget_fvpairs (url, &qargc, &qargv);
-  if (rc)
-    return rc;
-
-  qcopy = calloc (qargc + 1, sizeof (qcopy[0]));
-  if (!qcopy)
-    return errno;
-  for (i = 0; i < qargc; i++)
-    {
-      if (!(qcopy[i] = strdup (qargv[i])))
-	{
-	  mu_argcv_free (i, qcopy);
-	  return errno;
-	}
-    }
-  qcopy[i] = NULL;
-  *qc = qargc;
-  *qv = qcopy;
-  return 0;
-}
-
-/* field-value pairs accessors */
-int
-mu_url_sget_fvpairs (const mu_url_t url, size_t *fvc, char ***fvp)
-{
-  if (url == NULL)
-    return EINVAL;
-  /* FIXME: no _get_fvpairs method, but the method stuff needs to be rewritten
-     anyway */
-  *fvc = url->fvcount;
-  *fvp = url->fvpairs;
-  return 0;
-}
-
-int
-mu_url_sget_param (const mu_url_t url, const char *param, const char **val)
-{
-  size_t fvc;
-  char **fvp;
-  int status = mu_url_sget_fvpairs (url, &fvc, &fvp);
-
-  if (status)
-    return status;
-
-  if (fvc)
-    {
-      size_t i;
-      
-      for (i = 0; i < fvc; i++)
-	{
-	  const char *p;
-	  char *q;
-
-	  for (p = param, q = fvp[i]; *p && *q && *p == *q; p++, q++)
-	    ;
-	  if (*p == 0)
-	    {
-	      if (*q == 0)
-		{
-		  if (val)
-		    *val = q;
-		  return 0;
-		}
-	      else if (*q == '=')
-		{
-		  if (val)
-		    *val = q + 1;
-		  return 0;
-		}
-	    }
-	}
-    }
- 
-  return MU_ERR_NOENT;
-}
-  
-int
-mu_url_aget_fvpairs (const mu_url_t url, size_t *pfvc, char ***pfvp)
-{
-  size_t fvc, i;
-  char **fvp;
-  char **fvcopy;
-
-  int rc = mu_url_sget_fvpairs (url, &fvc, &fvp);
-  if (rc)
-    return rc;
-
-  fvcopy = calloc (fvc + 1, sizeof (fvcopy[0]));
-  if (!fvcopy)
-    return errno;
-  for (i = 0; i < fvc; i++)
-    {
-      if (!(fvcopy[i] = strdup (fvp[i])))
-	{
-	  mu_argcv_free (i, fvcopy);
-	  return errno;
-	}
-    }
-  fvcopy[i] = NULL;
-  *pfvc = fvc;
-  *pfvp = fvcopy;
-  return 0;
-}
-
-int
-mu_url_aget_param (const mu_url_t url, const char *param, char **val)
-{
-  const char *s;
-  int status = mu_url_sget_param (url, param, &s);
-
-  if (status == 0)
-    {
-      *val = strdup (s);
-      if (!*val)
-	status = ENOMEM;
-    }
-  return status;
-}
-
-int
-mu_url_get_port (const mu_url_t url, long *pport)
-{
-  if (url == NULL)
-    return EINVAL;
-  if (url->_get_port)
-    return url->_get_port (url, pport);
-  *pport = url->port;
-  return 0;
-}
-
-const char *
-mu_url_to_string (const mu_url_t url)
-{
-  if (url == NULL || url->name == NULL)
-    return "";
-  return url->name;
-}
-
-int
-mu_url_set_scheme (mu_url_t url, const char *scheme)
-{
-  char *p;
-  if (!url || !scheme)
-    return EINVAL;
-  p = realloc (url->scheme, strlen (scheme) + 1);
-  if (!p)
-    return ENOMEM;
-  strcpy (url->scheme, scheme);
-  return 0;
-}
-
-int
-mu_url_is_scheme (mu_url_t url, const char *scheme)
-{
-  if (url && scheme && url->scheme 
-      && mu_c_strcasecmp (url->scheme, scheme) == 0)
-    return 1;
-
-  return 0;
-}
-
-int
-mu_url_is_same_port (mu_url_t url1, mu_url_t url2)
-{
-  long p1 = 0, p2 = 0;
-
-  mu_url_get_port (url1, &p1);
-  mu_url_get_port (url2, &p2);
-  return (p1 == p2);
-}
-
-/* From RFC 1738, section 2.2 */
-char *
-mu_url_decode_len (const char *s, size_t len)
-{
-  char *d;
-  const char *eos = s + len;
-  int i;
-
-  d = malloc (len + 1);
-  if (!d)
-    return NULL;
-
-  for (i = 0; s < eos; i++)
-    {
-      if (*s != '%')
-	{
-	  d[i] = *s;
-	  s++;
-	}
-      else
-	{
-	  unsigned long ul = 0;
-
-	  s++;
-
-	  /* don't check return value, it's correctly coded, or it's not,
-	     in which case we just skip the garbage, this is a decoder,
-	     not an AI project */
-
-	  mu_hexstr2ul (&ul, s, 2);
-
-	  s += 2;
-
-	  d[i] = (char) ul;
-	}
-    }
-
-  d[i] = 0;
-
-  return d;
-}
-
-char *
-mu_url_decode (const char *s)
-{
-  return mu_url_decode_len (s, strlen (s));
-}
-
-#define is_wildcard(s) ((s)[0] == '*' && s[1] == 0)
-
-#define WEIGHT_SCHEME 3
-#define WEIGHT_USER   4
-#define WEIGHT_HOST   2
-#define WEIGHT_PORT   1
-
-int
-mu_url_matches_ticket (mu_url_t ticket, mu_url_t url, int *pwc)
-{
-  int wcnt = 0;
-					      
-  if (is_wildcard (ticket->scheme))
-    wcnt += WEIGHT_SCHEME;
-  else if (mu_c_strcasecmp (ticket->scheme, url->scheme))
-    return 0;
-
-  if (ticket->flags & MU_URL_HOST)
-    {
-      if (is_wildcard (ticket->host))
-	wcnt += WEIGHT_HOST;
-      else if (url->flags & MU_URL_HOST)
-	{
-	  if (mu_c_strcasecmp (ticket->host, url->host))
-	    /* FIXME: Compare IP addresses */
-	    return 0;
-	}
-      else
-	return 0;
-    }
-  else
-    wcnt += WEIGHT_HOST;
-
-  if (ticket->flags & MU_URL_PORT)
-    {
-      /* FIXME: No way to put a wildcard in the ticket file */
-      if (url->port & MU_URL_PORT)
-	{
-	  if (ticket->port != url->port)
-	    return 0;
-	  else
-	    wcnt += WEIGHT_PORT;
-	}
-    }
-  else
-    wcnt += WEIGHT_PORT;
-  
-  if (ticket->flags & MU_URL_USER)
-    {
-      if (is_wildcard (ticket->user))
-	wcnt += WEIGHT_USER;
-      
-      /* If ticket has a user or pass, but url doesn't, that's OK, we were
-	 looking for this info. But if url does have a user/pass, it
-	 must match the ticket. */
-      else if (url->flags & MU_URL_USER)
-	{
-	  if (strcmp (ticket->user, url->user))
-	    return 0;
-	}
-    }
-  else
-    wcnt += WEIGHT_USER;
-  
-  /* Guess it matches. */
-  if (pwc)
-    *pwc = wcnt;
-  return 1;
-}
-
-int
-mu_url_init (mu_url_t url, int port, const char *scheme)
-{
-  int status = 0;
-
-  url->_destroy = NULL;
-
-  status = mu_url_parse (url);
-  if (status)
-    return status;
-
-  if (!mu_url_is_scheme (url, scheme))
-    return EINVAL;
-
-  if (url->port == 0)
-    url->port = port;
-
-  return status;
-}
-
-/* Default mailbox path generator */
-static char *
-_url_path_default (const char *spooldir, const char *user, int unused)
-{
-  return mu_make_file_name (spooldir, user);
-}
-
-/* Hashed indexing */
-static char *
-_url_path_hashed (const char *spooldir, const char *user, int param)
-{
-  int i;
-  int ulen = strlen (user);
-  char *mbox;
-  unsigned hash;
-
-  if (param > ulen)
-    param = ulen;
-  for (i = 0, hash = 0; i < param; i++)
-    hash += user[i];
-
-  mbox = malloc (ulen + strlen (spooldir) + 5);
-  sprintf (mbox, "%s/%02X/%s", spooldir, hash % 256, user);
-  return mbox;
-}
-
-static int transtab[] = {
-  'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
-  'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
-  'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
-  'y', 'z', 'a', 'b', 'c', 'd', 'e', 'f',
-  'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
-  'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
-  'w', 'x', 'y', 'z', 'a', 'b', 'c', 'd',
-  'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
-  'm', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
-  'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
-  'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
-  'x', 'y', 'z', 'b', 'c', 'd', 'e', 'f',
-  'g', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
-  'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
-  'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
-  'x', 'y', 'z', 'b', 'c', 'd', 'e', 'f',
-  'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
-  'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
-  'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
-  'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
-  'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
-  'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
-  'x', 'y', 'z', 'a', 'b', 'c', 'd', 'e',
-  'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
-  'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
-  'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
-  'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
-  'y', 'z', 'b', 'c', 'd', 'e', 'f', 'g',
-  'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
-  'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
-  'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
-  'y', 'z', 'b', 'c', 'd', 'e', 'f', 'g'
-};
-
-/* Forward Indexing */
-static char *
-_url_path_index (const char *spooldir, const char *iuser, int index_depth)
-{
-  const unsigned char* user = (const unsigned char*) iuser;
-  int i, ulen = strlen (iuser);
-  char *mbox, *p;
-
-  if (ulen == 0)
-    return NULL;
-
-  mbox = malloc (ulen + strlen (spooldir) + 2*index_depth + 2);
-  strcpy (mbox, spooldir);
-  p = mbox + strlen (mbox);
-  for (i = 0; i < index_depth && i < ulen; i++)
-    {
-      *p++ = '/';
-      *p++ = transtab[ user[i] ];
-    }
-  for (; i < index_depth; i++)
-    {
-      *p++ = '/';
-      *p++ = transtab[ user[ulen-1] ];
-    }
-  *p++ = '/';
-  strcpy (p, iuser);
-  return mbox;
-}
-
-/* Reverse Indexing */
-static char *
-_url_path_rev_index (const char *spooldir, const char *iuser, int index_depth)
-{
-  const unsigned char* user = (const unsigned char*) iuser;
-  int i, ulen = strlen (iuser);
-  char *mbox, *p;
-
-  if (ulen == 0)
-    return NULL;
-
-  mbox = malloc (ulen + strlen (spooldir) + 2*index_depth + 1);
-  strcpy (mbox, spooldir);
-  p = mbox + strlen (mbox);
-  for (i = 0; i < index_depth && i < ulen; i++)
-    {
-      *p++ = '/';
-      *p++ = transtab[ user[ulen - i - 1] ];
-    }
-  for (; i < index_depth; i++)
-    {
-      *p++ = '/';
-      *p++ = transtab[ user[0] ];
-    }
-  *p++ = '/';
-  strcpy (p, iuser);
-  return mbox;
-}
-
-static int
-rmselector (const char *p, void *data MU_ARG_UNUSED)
-{
-  return strncmp (p, "type=", 5) == 0
-	 || strncmp (p, "user=", 5) == 0
-	 || strncmp (p, "param=", 6) == 0;
-}
-
-int
-mu_url_expand_path (mu_url_t url)
-{
-  size_t i;
-  char *user = NULL;
-  int param = 0;
-  char *p;
-  char *(*fun) (const char *, const char *, int) = _url_path_default;
-
-  if (url->fvcount == 0)
-    return 0;
-
-  for (i = 0; i < url->fvcount; i++)
-    {
-      p = url->fvpairs[i];
-      if (strncmp (p, "type=", 5) == 0)
-	{
-	  char *type = p + 5;
-
-	  if (strcmp (type, "hash") == 0)
-	    fun = _url_path_hashed;
-	  else if (strcmp (type, "index") == 0)
-	    fun = _url_path_index;
-	  else if (strcmp (type, "rev-index") == 0)
-	    fun = _url_path_rev_index;
-	  else
-	    return MU_ERR_NOENT;
-	}
-      else if (strncmp (p, "user=", 5) == 0)
-	{
-	  user = p + 5;
-	}
-      else if (strncmp (p, "param=", 6) == 0)
-	{
-	  param = strtoul (p + 6, NULL, 0);
-	}
-    }
-
-  if (user)
-    {
-      char *p = fun (url->path, user, param);
-      if (p)
-	{
-	  free (url->path);
-	  url->path = p;
-	}
-      mu_argcv_remove (&url->fvcount, &url->fvpairs, rmselector, NULL);
-    }
-  else
-    return MU_ERR_NOENT;
-
-  return 0;
-}
-
-int
-mu_url_get_flags (mu_url_t url, int *pf)
-{
-  if (!url || !pf)
-    return EINVAL;
-  *pf = url->flags;
-  return 0;
-}
-
-int
-mu_url_has_flag (mu_url_t url, int flags)
-{
-  if (!url)
-    return 0;
-  return url->flags & flags;
-}
-
diff --git a/libmailutils/base/wicket.c b/libmailutils/base/wicket.c
index 3e808c0..afdb875 100644
--- a/libmailutils/base/wicket.c
+++ b/libmailutils/base/wicket.c
@@ -180,7 +180,9 @@ file_ticket_get_cred (mu_ticket_t ticket, mu_url_t url, const char *challenge,
 
   if (!ft->tickurl)
     {
-      int rc = mu_wicket_file_match_url (ft->filename, url, &ft->tickurl);
+      int rc = mu_wicket_file_match_url (ft->filename, url,
+					 MU_URL_PARSE_ALL,
+					 &ft->tickurl);
       if (rc)
 	return rc;
     }
@@ -245,7 +247,8 @@ _file_wicket_get_ticket (mu_wicket_t wicket, void *data,
   
 int
 mu_wicket_stream_match_url (mu_stream_t stream, struct mu_debug_locus *loc,
-			    mu_url_t url, mu_url_t *pticket_url)
+			    mu_url_t url, int parse_flags,
+			    mu_url_t *pticket_url)
 {
   int rc;
   mu_url_t u = NULL;
@@ -270,20 +273,13 @@ mu_wicket_stream_match_url (mu_stream_t stream, struct mu_debug_locus *loc,
       if (*p == 0 || *p == '#')
 	continue;
       
-      if ((err = mu_url_create (&u, p)) != 0)
+      if ((err = mu_url_create_hint (&u, p, parse_flags, NULL)) != 0)
 	{
 	  /* Skip erroneous entry */
 	  mu_error (_("%s:%u: cannot create URL: %s"),
 		    loc->file, loc->line, mu_strerror (err));
 	  continue;
 	}
-      if ((err = mu_url_parse (u)) != 0)
-	{
-	  mu_error (_("%s:%u: cannot parse URL: %s"),
-		    loc->file, loc->line, mu_strerror (err));
-	  mu_url_destroy (&u);
-	  continue;
-	}
 
       if (!mu_url_has_flag (u, MU_URL_USER|MU_URL_SECRET))
 	{
@@ -326,6 +322,7 @@ mu_wicket_stream_match_url (mu_stream_t stream, struct mu_debug_locus *loc,
 
 int
 mu_wicket_file_match_url (const char *name, mu_url_t url,
+			  int parse_flags,
 			  mu_url_t *pticket_url)
 {
   mu_stream_t stream;
@@ -337,7 +334,8 @@ mu_wicket_file_match_url (const char *name, mu_url_t url,
     return rc;
   loc.file = name;
   loc.line = 0;
-  rc = mu_wicket_stream_match_url (stream, &loc, url, pticket_url);
+  rc = mu_wicket_stream_match_url (stream, &loc, url, parse_flags,
+				   pticket_url);
   mu_stream_close (stream);
   mu_stream_destroy (&stream);
   return rc;
diff --git a/libmailutils/mailbox/folder.c b/libmailutils/mailbox/folder.c
index f6e75c0..fe91823 100644
--- a/libmailutils/mailbox/folder.c
+++ b/libmailutils/mailbox/folder.c
@@ -157,9 +157,7 @@ mu_folder_create (mu_folder_t *pfolder, const char *name)
   rc = mu_url_create (&url, name);
   if (rc)
     return rc;
-  rc = mu_url_parse (url);
-  if (rc == 0)
-    rc = mu_folder_create_from_record (pfolder, url, NULL);
+  rc = mu_folder_create_from_record (pfolder, url, NULL);
   if (rc)
     mu_url_destroy (&url);
   return rc;
diff --git a/libmailutils/mailbox/mailbox.c b/libmailutils/mailbox/mailbox.c
index d95ce2d..0677e88 100644
--- a/libmailutils/mailbox/mailbox.c
+++ b/libmailutils/mailbox/mailbox.c
@@ -185,9 +185,7 @@ _create_mailbox (mu_mailbox_t *pmbox, const char *name)
   status = mu_url_create (&url, name);
   if (status)
     return status;
-  status = mu_url_parse (url);
-  if (status == 0)
-    status = _create_mailbox0 (pmbox, url, name);
+  status = _create_mailbox0 (pmbox, url, name);
   if (status)
     mu_url_destroy (&url);
   return status;
@@ -224,9 +222,7 @@ mu_mailbox_create_from_record (mu_mailbox_t *pmbox, mu_record_t record,
   rc = mu_url_create (&url, name);
   if (rc)
     return rc;
-  rc = mu_url_parse (url);
-  if (rc == 0)
-    rc = _mailbox_create_from_record (pmbox, record, url, name);
+  rc = _mailbox_create_from_record (pmbox, record, url, name);
   if (rc)
     mu_url_destroy (&url);
   return rc;
diff --git a/libmailutils/mailer/mailer.c b/libmailutils/mailer/mailer.c
index 59559eb..01fb6d3 100644
--- a/libmailutils/mailer/mailer.c
+++ b/libmailutils/mailer/mailer.c
@@ -168,9 +168,7 @@ mu_mailer_create (mu_mailer_t * pmailer, const char *name)
   status = mu_url_create (&url, name);
   if (status)
     return status;
-  status = mu_url_parse (url);
-  if (status == 0)
-    status = mu_mailer_create_from_url (pmailer, url);
+  status = mu_mailer_create_from_url (pmailer, url);
   if (status)
     mu_url_destroy (&url);
   return status;
diff --git a/libmailutils/mime/mimehdr.c b/libmailutils/mime/mimehdr.c
index 7bc801b..863566c 100644
--- a/libmailutils/mime/mimehdr.c
+++ b/libmailutils/mime/mimehdr.c
@@ -29,8 +29,6 @@
 #include <mailutils/message.h>
 #include <mailutils/header.h>
 #include <mailutils/stream.h>
-#include <mailutils/url.h> /* FIXME: for mu_url_decode, which should
-			      be renamed! */
 #include <mailutils/mime.h>
 #include <mailutils/filter.h>
 #include <mailutils/util.h>
@@ -480,9 +478,9 @@ mu_mimehdr_decode_param (const char *value, int flags,
     }
   else
     {
-      decoded = mu_url_decode (value);
-      if (!decoded)
-	return ENOMEM;
+      rc = mu_str_url_decode (&decoded, value);
+      if (rc)
+	return rc;
   
       if ((flags & MU_MIMEHDR_CSINFO)
 	  && (lang = strchr (decoded, '\''))
diff --git a/libmailutils/stream/tcp.c b/libmailutils/stream/tcp.c
index b53511a..c719be8 100644
--- a/libmailutils/stream/tcp.c
+++ b/libmailutils/stream/tcp.c
@@ -27,6 +27,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <limits.h>
 
 #include <sys/socket.h>
 #include <sys/types.h>
@@ -52,7 +53,7 @@ struct _tcp_instance
   struct _mu_stream stream;
   int 		fd;
   char 		*host;
-  int 		port;
+  unsigned short port;
   int		state;
   unsigned long	address;
   unsigned long source_addr;
@@ -304,7 +305,7 @@ _create_tcp_stream (int flags)
 
 int
 mu_tcp_stream_create_with_source_ip (mu_stream_t *pstream,
-				     const char *host, int port,
+				     const char *host, unsigned port,
 				     unsigned long source_ip,
 				     int flags)
 {
@@ -315,7 +316,7 @@ mu_tcp_stream_create_with_source_ip (mu_stream_t *pstream,
   if (host == NULL)
     return MU_ERR_TCP_NO_HOST;
 
-  if (port < 1)
+  if (port > USHRT_MAX)
     return MU_ERR_TCP_NO_PORT;
 
   tcp = _create_tcp_stream (flags | MU_STREAM_RDWR);
@@ -341,7 +342,7 @@ mu_tcp_stream_create_with_source_ip (mu_stream_t *pstream,
 
 int
 mu_tcp_stream_create_with_source_host (mu_stream_t *stream,
-				       const char *host, int port,
+				       const char *host, unsigned port,
 				       const char *source_host,
 				       int flags)
 {
@@ -354,7 +355,7 @@ mu_tcp_stream_create_with_source_host (mu_stream_t *stream,
 }
        
 int
-mu_tcp_stream_create (mu_stream_t *stream, const char *host, int port,
+mu_tcp_stream_create (mu_stream_t *stream, const char *host, unsigned port,
 		      int flags)
 {
   return mu_tcp_stream_create_with_source_ip (stream, host, port,
diff --git a/libmailutils/string/Makefile.am b/libmailutils/string/Makefile.am
index 3f11ade..485df4a 100644
--- a/libmailutils/string/Makefile.am
+++ b/libmailutils/string/Makefile.am
@@ -34,6 +34,7 @@ libstring_la_SOURCES = \
  asprintf.c\
  muctype.c\
  vasnprintf.c\
- mkfilename.c
+ mkfilename.c\
+ xdecode.c
 
 INCLUDES = @MU_LIB_COMMON_INCLUDES@ -I/libmailutils
diff --git a/libmailutils/string/cpystr.c b/libmailutils/string/xdecode.c
similarity index 57%
copy from libmailutils/string/cpystr.c
copy to libmailutils/string/xdecode.c
index 9671c70..e802bf6 100644
--- a/libmailutils/string/cpystr.c
+++ b/libmailutils/string/xdecode.c
@@ -21,20 +21,53 @@
 #endif
 
 #include <string.h>
+#include <mailutils/errno.h>
 #include <mailutils/util.h>
 
-/* Smart strncpy that always add the null and returns the number of bytes
-   written.  */
-size_t
-mu_cpystr (char *dst, const char *src, size_t size)
+/* From RFC 1738, section 2.2 */
+void
+mu_str_url_decode_inline (char *s)
 {
-  size_t len = src ? strlen (src) : 0 ;
-  if (dst == NULL || size == 0)
-    return len;
-  if (len >= size)
-    len = size - 1;
-  memcpy (dst, src, len);
-  dst[len] = '\0';
-  return len;
+  char *d;
+
+  d = strchr (s, '%');
+  if (!d)
+    return;
+
+  for (s = d; *s; )
+    {
+      if (*s != '%')
+	{
+	  *d++ = *s++;
+	}
+      else
+	{
+	  unsigned long ul = 0;
+
+	  s++;
+
+	  /* don't check return value, it's correctly coded, or it's not,
+	     in which case we just skip the garbage, this is a decoder,
+	     not an AI project */
+
+	  mu_hexstr2ul (&ul, s, 2);
+
+	  s += 2;
+
+	  *d++ = (char) ul;
+	}
+    }
+
+  *d = 0;
 }
 
+int
+mu_str_url_decode (char **ptr, const char *s)
+{
+  char *d = strdup (s);
+  if (!d)
+    return ENOMEM;
+  mu_str_url_decode_inline (d);
+  *ptr = d;
+  return 0;
+}
diff --git a/libmailutils/tests/url-parse.c b/libmailutils/tests/url-parse.c
index 92fd4f1..c87753c 100644
--- a/libmailutils/tests/url-parse.c
+++ b/libmailutils/tests/url-parse.c
@@ -77,7 +77,7 @@ int
 main ()
 {
   char str[1024];
-  long port = 0;
+  unsigned port = 0;
   mu_url_t u = NULL;
 
   while (fgets (str, sizeof (str), stdin) != NULL)
@@ -95,11 +95,6 @@ main ()
                    str, rc, mu_strerror (rc));
           exit (1);
         }
-      if ((rc = mu_url_parse (u)) != 0)
-        {
-          fprintf (stderr, "%s\n", mu_errname (rc));
-          continue;
-        }
 
       GET_AND_PRINT (scheme, u, buf, rc);
       GET_AND_PRINT (user, u, buf, rc);
@@ -127,7 +122,7 @@ main ()
 	  mu_error ("cannot get %s: %s", "port", mu_strerror (rc));	
 	  exit (1);					
         }                                               
-      printf ("port %ld\n", port);
+      printf ("port %hu\n", port);
       
       GET_AND_PRINT (path, u, buf, rc);
       print_fvpairs (u);
diff --git a/libmailutils/tests/wicket.c b/libmailutils/tests/wicket.c
index b6215cb..57857da 100644
--- a/libmailutils/tests/wicket.c
+++ b/libmailutils/tests/wicket.c
@@ -38,15 +38,10 @@ match_string (const char *str)
 	       str, rc, mu_strerror (rc));
       return;
     }
-  if ((rc = mu_url_parse (u)) != 0)
-    {
-      fprintf (stderr, "%s\n", mu_errname (rc));
-      return;
-    }
   MU_ASSERT (mu_stream_seek (stream, 0, MU_SEEK_SET, NULL));
   loc.file = name;
   loc.line = 0;
-  rc = mu_wicket_stream_match_url (stream, &loc, u, &url);
+  rc = mu_wicket_stream_match_url (stream, &loc, u, MU_URL_PARSE_ALL, &url);
   switch (rc)
     {
     case 0:
diff --git a/libmailutils/diag/Makefile.am b/libmailutils/url/Makefile.am
similarity index 72%
copy from libmailutils/diag/Makefile.am
copy to libmailutils/url/Makefile.am
index f2dae72..51e3e64 100644
--- a/libmailutils/diag/Makefile.am
+++ b/libmailutils/url/Makefile.am
@@ -15,28 +15,31 @@
 # Public License along with this library.  If not, see
 # <http://www.gnu.org/licenses/>.
 
-noinst_LTLIBRARIES = libdiag.la
-
-libdiag_la_SOURCES = \
- debug.c\
- diag.c\
- gdebug.c\
- muerror.c\
- muerrno.c\
- syslog.c\
- dbgstderr.c\
- dbgsyslog.c
+noinst_LTLIBRARIES = liburl.la
+
+liburl_la_SOURCES = \
+ accessor.h\
+ copy.c\
+ create.c\
+ decode.c\
+ destroy.c\
+ dup.c\
+ expand.c\
+ flag.c\
+ get-auth.c\
+ get-host.c\
+ get-param.c\
+ get-path.c\
+ get-portstr.c\
+ get-query.c\
+ get-scheme.c\
+ get-secret.c\
+ get-user.c\
+ match.c\
+ port.c\
+ scheme.c\
+ uplevel.c\
+ urlstr.c
 
 INCLUDES = @MU_LIB_COMMON_INCLUDES@ -I/libmailutils
 
-EXTRA_DIST = \
- errors\
- muerrno.cin
-
-BUILT_SOURCES = muerrno.c
-
-muerrno.c: errors muerrno.cin
-	$(AWK) -f $(mu_aux_dir)/generr.awk $^ > $@
-
-
-
diff --git a/libmailutils/url/accessor.h b/libmailutils/url/accessor.h
new file mode 100644
index 0000000..de38711
--- /dev/null
+++ b/libmailutils/url/accessor.h
@@ -0,0 +1,143 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 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, see 
+   <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#ifdef HAVE_STRINGS_H
+# include <strings.h>
+#endif
+
+#include <mailutils/types.h>
+#include <mailutils/errno.h>
+#include <mailutils/util.h>
+#include <mailutils/cstr.h>
+#include <mailutils/sys/url.h>
+
+/* General accessors: */
+#define AC2(a,b) a ## b
+#define METHOD(pfx,part) AC2(pfx,part)
+#define AC4(a,b,c,d) a ## b ## c ## d
+#define ACCESSOR(action,field) AC4(mu_url_,action,_,field)
+
+/* Define a `static get' accessor */
+int
+ACCESSOR(sget,URL_PART) (mu_url_t url, char const **sptr)
+{
+  if (url == NULL)
+    return EINVAL;
+  if (!url->URL_PART)
+    {
+      if (url->METHOD(_get_,URL_PART))
+	{
+	  size_t n;
+	  char *buf;
+
+	  int status = url->METHOD(_get_,URL_PART) (url, NULL, 0, &n);
+	  if (status)
+	    return status;
+
+	  buf = malloc (n + 1);
+	  if (!buf)
+	    return ENOMEM;
+
+	  status = url->METHOD(_get_,URL_PART) (url, buf, n + 1, NULL);
+	  if (status)
+	    return status;
+
+	  if (buf[0])
+	    {
+	      /* FIXME */
+	      status = mu_str_url_decode (&url->URL_PART, buf);
+	      if (status)
+		{
+		   free (buf);
+		   return status;
+		}
+	    }
+	  else
+	    url->URL_PART = buf;
+	  if (!url->URL_PART)
+	    return ENOMEM;
+	}
+      else
+	return MU_ERR_NOENT;
+    }
+  *sptr = url->URL_PART;
+  return 0;
+}
+
+/* Define a `get' accessor */
+int
+ACCESSOR(get,URL_PART) (mu_url_t url, char *buf, size_t len, size_t *n)
+{
+  size_t i;
+  const char *str;
+  int status = ACCESSOR(sget, URL_PART) (url, &str);
+
+  if (status)
+    return status;
+
+  i = mu_cpystr (buf, str, len);
+  if (n)
+    *n = i;
+  return 0;
+}
+
+/* Define an `allocated get' accessor */
+int
+ACCESSOR(aget, URL_PART) (mu_url_t url, char **buf)
+{
+  const char *str;
+  int status = ACCESSOR(sget, URL_PART) (url, &str);
+
+  if (status)
+    return status;
+
+  if (str)
+    {
+      *buf = strdup (str);
+      if (!*buf)
+	status = ENOMEM;
+    }
+  else
+    *buf = NULL;
+  return status;
+}
+
+/* Define a comparator */
+int
+ACCESSOR(is_same,URL_PART) (mu_url_t url1, mu_url_t url2)
+{
+  const char *s1, *s2;
+  int status1, status2;
+
+  status1 = ACCESSOR(sget, URL_PART) (url1, &s1);
+  if (status1 && status1 != MU_ERR_NOENT)
+    return 0;
+  status2 = ACCESSOR(sget, URL_PART) (url2, &s2);
+  if (status2 && status2 != MU_ERR_NOENT)
+    return 0;
+
+  if (status1 || status2)
+    return status1 == status2; /* Both fields are missing */
+  return mu_c_strcasecmp (s1, s2) == 0;
+}
diff --git a/libmailutils/url/copy.c b/libmailutils/url/copy.c
new file mode 100644
index 0000000..7190b42
--- /dev/null
+++ b/libmailutils/url/copy.c
@@ -0,0 +1,145 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 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, see 
+   <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <errno.h>
+#include <string.h>
+#ifdef HAVE_STRINGS_H
+# include <strings.h>
+#endif
+
+#include <mailutils/types.h>
+#include <mailutils/argcv.h>
+#include <mailutils/secret.h>
+#include <mailutils/util.h>
+#include <mailutils/sys/url.h>
+
+struct copy_tab
+{
+  int mask;
+  int (*fun) (mu_url_t, mu_url_t, size_t);
+  size_t off;
+};
+
+static int
+_url_copy_str (mu_url_t dest_url, mu_url_t src_url, size_t off)
+{
+  char **dest = (char**) ((char*) dest_url + off);
+  char *src = *(char**) ((char*) src_url + off);
+  char *p = strdup (src);
+  if (!p)
+    return ENOMEM;
+  *dest = p;
+  return 0;
+}
+  
+static int
+_url_copy_secret (mu_url_t dest, mu_url_t src, size_t off)
+{
+  return mu_secret_dup (src->secret, &dest->secret);
+}
+
+static int
+_url_copy_port (mu_url_t dest, mu_url_t src, size_t off)
+{
+  if (src->portstr)
+    {
+      dest->portstr = strdup (src->portstr);
+      if (!dest->portstr)
+	return ENOMEM;
+    }
+  dest->port = src->port;
+  return 0;
+}
+
+static char **
+argcv_copy (size_t argc, char **argv)
+{
+  size_t i;
+  char **nv = calloc (argc + 1, sizeof (nv[0]));
+  if (!nv)
+    return NULL;
+  for (i = 0; i < argc; i++)
+    if ((nv[i] = strdup (argv[i])) == NULL)
+      {
+	mu_argcv_free (i, nv);
+	free (nv);
+	return NULL;
+      }
+  return nv;
+}
+
+static int
+_url_copy_param (mu_url_t dest, mu_url_t src, size_t off)
+{
+  if ((dest->fvpairs = argcv_copy (src->fvcount, src->fvpairs)) == NULL)
+    return ENOMEM;
+  dest->fvcount = src->fvcount;
+  return 0;
+}
+
+static int
+_url_copy_query (mu_url_t dest, mu_url_t src, size_t off)
+{
+  if ((dest->qargv = argcv_copy (src->qargc, src->qargv)) == NULL)
+    return ENOMEM;
+  dest->qargc = src->qargc;
+  return 0;
+}
+
+static struct copy_tab copy_tab[] = {
+  { MU_URL_SCHEME, _url_copy_str, mu_offsetof (struct _mu_url, scheme) },
+  { MU_URL_USER,   _url_copy_str, mu_offsetof (struct _mu_url, user) },
+  { MU_URL_SECRET, _url_copy_secret, 0 },
+  { MU_URL_AUTH,   _url_copy_str, mu_offsetof (struct _mu_url, auth) },
+  { MU_URL_HOST,   _url_copy_str, mu_offsetof (struct _mu_url, host) },
+  { MU_URL_PORT,   _url_copy_port, 0 },
+  { MU_URL_PATH,   _url_copy_str, mu_offsetof (struct _mu_url, path) },
+  { MU_URL_PARAM,  _url_copy_param, 0 },
+  { MU_URL_QUERY,  _url_copy_query, 0 }
+};
+
+int
+mu_url_copy_hints (mu_url_t url, mu_url_t hint)
+{
+  int i;
+
+  if (!url)
+    return EINVAL;
+  if (!hint)
+    return 0;
+  for (i = 0; i < MU_ARRAY_SIZE (copy_tab); i++)
+    {
+      if (!(url->flags & copy_tab[i].mask) &&
+	  (hint->flags & copy_tab[i].mask))
+	{
+	  int rc = copy_tab[i].fun (url, hint, copy_tab[i].off);
+	  if (rc)
+	    return rc;
+	  url->flags |= copy_tab[i].mask;
+	}
+    }
+  return 0;
+}
+
+    
+
+
+
diff --git a/libmailutils/url/create.c b/libmailutils/url/create.c
new file mode 100644
index 0000000..674a524
--- /dev/null
+++ b/libmailutils/url/create.c
@@ -0,0 +1,525 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 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, see 
+   <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <ctype.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#ifdef HAVE_STRINGS_H
+# include <strings.h>
+#endif
+#include <netdb.h>
+#include <arpa/inet.h>
+#include <limits.h>
+
+#include <mailutils/util.h>
+#include <mailutils/errno.h>
+#include <mailutils/argcv.h>
+#include <mailutils/secret.h>
+#include <mailutils/cstr.h>
+#include <mailutils/sys/url.h>
+
+struct mu_url_ctx
+{
+  int flags;
+  const char *input;
+  const char *cur;
+  mu_url_t url;
+
+  size_t passoff;
+  
+  char *tokbuf;
+  size_t toksize;
+  size_t toklen;
+};
+
+static int
+getkn (struct mu_url_ctx *ctx, char *delim)
+{
+  size_t n;
+
+  if (*ctx->cur == 0)
+    return -1;
+  n = strcspn (ctx->cur, delim);
+  if (n > ctx->toksize)
+    {
+      char *p = realloc (ctx->tokbuf, n + 1);
+      if (!p)
+	return ENOENT;
+      ctx->toksize = n + 1;
+      ctx->tokbuf = p;
+    }
+  memcpy (ctx->tokbuf, ctx->cur, n);
+  ctx->tokbuf[n] = 0;
+  ctx->toklen = n;
+  ctx->cur += n;
+  return 0;
+}
+
+#define INIT_ARRAY_SIZE 16
+
+static int
+expand_array (size_t *pwc, char ***pwv, int incr)
+{
+  size_t wc = *pwc;
+  char **wv = *pwv;
+
+  if (!wv)
+    {
+      wv = calloc (INIT_ARRAY_SIZE, sizeof (wv[0]));
+      wc = INIT_ARRAY_SIZE;
+    }
+  else
+    {
+      if (incr)
+	wc += incr;
+      else
+	{
+	  size_t newsize = wc * 2;
+	  if (newsize < wc)
+	    return ENOMEM;
+	  wc = newsize;
+	}
+      wv = realloc (wv, sizeof (wv[0]) * wc);
+    }
+  if (!wv)
+    return ENOMEM;
+  *pwv = wv;
+  *pwc = wc;
+  return 0;
+}
+  
+static int
+parse_param (struct mu_url_ctx *ctx, char *delim, int *pargc, char ***pargv)
+{
+  int rc;
+  size_t wc = 0, wn = 0;
+  char **wv = NULL;
+
+  while ((rc = getkn (ctx, delim)) == 0)
+    {
+      if (wn == wc)
+	{
+	  rc = expand_array (&wc, &wv, 0);
+	  if (rc)
+	    break;
+	}
+      wv[wn] = strdup (ctx->tokbuf);
+      if (!wv[wn])
+	{
+	  rc = ENOMEM;
+	  break;
+	}
+      wn++;
+      if (*ctx->cur != delim[0])
+	break;
+      ctx->cur++;
+    }
+
+  if (rc == 0)
+    {
+      if (wn == wc)
+	{
+	  rc = expand_array (&wc, &wv, 1);
+	  if (rc)
+	    {
+	      mu_argcv_free (wc, wv);
+	      return ENOMEM;
+	    }
+	  wv[wn] = NULL;
+	}
+
+      *pargv = realloc (wv, sizeof (wv[0]) * (wn + 1));
+      *pargc = wn;
+    }
+  else
+    mu_argcv_free (wc, wv);
+
+  return rc;
+}
+
+static int
+_mu_url_ctx_parse_query (struct mu_url_ctx *ctx)
+{
+  int rc;
+  
+  ctx->cur++;
+  rc = parse_param (ctx, "&", &ctx->url->qargc, &ctx->url->qargv);
+  if (rc == 0 && ctx->url->qargc)
+    ctx->url->flags |= MU_URL_QUERY;
+  return rc;
+}
+
+static int
+_mu_url_ctx_parse_param (struct mu_url_ctx *ctx)
+{
+  int rc;
+  
+  ctx->cur++;
+  rc = parse_param (ctx, ";?", &ctx->url->fvcount, &ctx->url->fvpairs);
+  if (rc)
+    return rc;
+  if (ctx->url->fvcount)
+    ctx->url->flags |= MU_URL_PARAM;
+  if (*ctx->cur == '?')
+    return _mu_url_ctx_parse_query (ctx);
+  return 0;
+}
+
+static int
+str_assign (char **ptr, const char *str)
+{
+  *ptr = strdup (str);
+  if (!*ptr)
+    return ENOMEM;
+  return 0;
+}
+
+static int
+_mu_url_ctx_parse_path (struct mu_url_ctx *ctx)
+{
+  int rc;
+  mu_url_t url = ctx->url;
+  
+  rc = getkn (ctx, ";?");
+  if (rc)
+    return rc;
+  rc = str_assign (&url->path, ctx->tokbuf);
+  if (rc == 0)
+    url->flags |= MU_URL_PATH;
+  if (*ctx->cur == ';')
+    return _mu_url_ctx_parse_param (ctx);
+  if (*ctx->cur == '?')
+    return _mu_url_ctx_parse_query (ctx);
+  return 0;
+}
+
+static int
+_mu_url_ctx_parse_host (struct mu_url_ctx *ctx, int has_host)
+{
+  int rc;
+  mu_url_t url = ctx->url;
+  
+  rc = getkn (ctx, ":/;?");
+  if (rc)
+    return rc;
+
+  if (ctx->toklen)
+    {
+      rc = str_assign (&url->host, ctx->tokbuf);
+      if (rc)
+	return rc;
+      url->flags |= MU_URL_HOST;
+      has_host = 1;
+    }
+
+  if (*ctx->cur == ':')
+    {
+      ctx->cur++;
+      has_host = 1;
+
+      rc = getkn (ctx, "/;?");
+      if (rc)
+	return rc;
+      
+      rc = str_assign (&url->portstr, ctx->tokbuf);
+      if (rc)
+	return rc;
+      url->flags |= MU_URL_PORT;
+    }
+
+  if (*ctx->cur == '/')
+    {
+      if (has_host)
+	ctx->cur++;
+      return _mu_url_ctx_parse_path (ctx);
+    }
+  
+  if (*ctx->cur == ';')
+    return _mu_url_ctx_parse_param (ctx);
+
+  if (*ctx->cur == '?')
+    return _mu_url_ctx_parse_query (ctx);
+  return 0;
+}
+
+static int
+_mu_url_ctx_parse_cred (struct mu_url_ctx *ctx)
+{
+  int rc, has_cred;
+  mu_url_t url = ctx->url;
+  const char *save = ctx->cur;
+  
+  rc = getkn (ctx, "@");
+  if (rc)
+    return rc;
+  has_cred = *ctx->cur == '@';
+  /* restore the pointer */
+  ctx->cur = save;  
+  if (has_cred)
+    {
+      /* Try to split the user into a:
+	 <user>:<password>
+	 or
+	 <user>:<password>;AUTH=<auth>
+      */
+      rc = getkn (ctx, ":;@");
+      if (rc)
+	return rc;
+
+      if (ctx->toklen)
+	{
+	  rc = str_assign (&url->user, ctx->tokbuf);
+	  if (rc)
+	    return rc;
+	  url->flags |= MU_URL_USER;
+	}
+      
+      if (*ctx->cur == ':')
+	{
+	  ctx->cur++;
+	  ctx->passoff = ctx->cur - ctx->input;
+	  
+	  rc = getkn (ctx, ";@");
+	  if (rc)
+	    return rc;
+
+	  if (ctx->toklen)
+	    {
+	      if (mu_secret_create (&url->secret, ctx->tokbuf, ctx->toklen))
+		return ENOMEM;
+	      else
+		/* Clear password */
+		memset (ctx->tokbuf, 0, ctx->toklen);
+	      url->flags |= MU_URL_SECRET;
+	    }
+	}
+      if (*ctx->cur == ';')
+	{
+	  ctx->cur++;
+
+	  rc = getkn (ctx, "@");
+	  if (rc)
+	    return rc;
+	  
+	  /* Make sure it's the auth token. */
+	  if (mu_c_strncasecmp (ctx->tokbuf, "auth=", 5) == 0)
+	    {
+	      rc = str_assign (&url->auth, ctx->tokbuf + 5);
+	      if (rc)
+		return rc;
+	      url->flags |= MU_URL_AUTH;
+	    }
+	}
+
+      /* Skip @ sign */
+      ctx->cur++;
+    }
+  return _mu_url_ctx_parse_host (ctx, has_cred);
+}
+
+int
+_mu_url_ctx_parse (struct mu_url_ctx *ctx)
+{
+  int rc;
+  mu_url_t url = ctx->url;
+  
+  /* Parse the scheme part */
+  rc = getkn (ctx, ":/");
+  if (rc)
+    return rc;
+  if (*ctx->cur == ':')
+    {
+      rc = str_assign (&url->scheme, ctx->tokbuf);
+      if (rc)
+	return rc;
+      url->flags |= MU_URL_SCHEME;
+      ctx->cur++;
+    }
+
+  if (*ctx->cur == 0)
+    return 0;
+
+  if (ctx->cur[0] == '/' && ctx->cur[1] == '/')
+    {
+      ctx->cur += 2;
+      return _mu_url_ctx_parse_cred (ctx);
+    }
+
+  return _mu_url_ctx_parse_path (ctx);
+}
+
+static int
+_mu_url_create_internal (struct mu_url_ctx *ctx, mu_url_t hint)
+{
+  int rc;
+  mu_url_t url = ctx->url;
+
+  if ((ctx->flags & MU_URL_PARSE_PIPE) && ctx->input[0] == '|')
+    {
+      rc = str_assign (&url->scheme, "prog");
+      if (rc)
+	return rc;
+      url->flags |= MU_URL_SCHEME;
+      ctx->flags &= ~MU_URL_PARSE_HEXCODE;
+      rc = mu_argcv_get (ctx->input + 1, NULL, NULL, &url->qargc, &url->qargv);
+      if (rc == 0)
+	{
+	  url->flags |= MU_URL_QUERY;
+	  rc = str_assign (&url->path, url->qargv[0]);
+	  if (rc == 0)
+	    url->flags |= MU_URL_PATH;
+	}
+    }
+  else if ((ctx->flags & MU_URL_PARSE_SLASH) && ctx->input[0] == '/')
+    {
+      rc = str_assign (&url->scheme, "file");
+      if (rc)
+	return rc;
+      url->flags |= MU_URL_SCHEME;
+      ctx->flags &= ~MU_URL_PARSE_HEXCODE;
+      rc = str_assign (&url->path, ctx->input);
+      if (rc == 0)
+	url->flags |= MU_URL_PATH;
+    }
+  else
+    rc = _mu_url_ctx_parse (ctx);
+  
+  if (rc)
+    return rc;
+
+  if (hint)
+    {
+      /* Fill in missing values */
+      rc = mu_url_copy_hints (url, hint);
+      if (rc)
+	return rc;
+    }
+
+  if (!(url->flags & MU_URL_SCHEME))
+    return MU_ERR_URL_MISS_PARTS;
+  
+  /* RFC 1738, section 2.1, lower the scheme case */
+  mu_strlower (url->scheme);
+
+  if ((url->flags & MU_URL_PORT) && url->port == 0)
+    {
+      /* Convert port string to number */
+      unsigned long n;
+      char *p;
+      
+      n = strtoul (url->portstr, &p, 10);
+      if (*p)
+	{
+	  if (ctx->flags & MU_URL_PARSE_PORTSRV)
+	    {
+	      /* FIXME: Another proto? */
+	      struct servent *sp = getservbyname (url->portstr, "tcp");
+	      if (!sp)
+		return MU_ERR_TCP_NO_PORT; //FIXME: Error code?
+	      url->port = ntohs (sp->s_port);
+	    }
+	  else
+	    return MU_ERR_TCP_NO_PORT;
+	}
+      else if (n > USHRT_MAX)
+	return ERANGE;
+      else
+	url->port = n;
+    }
+
+  if (ctx->flags & MU_URL_PARSE_HEXCODE)
+    {
+      /* Decode the %XX notations */
+      rc = mu_url_decode (url);
+      if (rc)
+	return rc;
+    }
+  
+  if ((url->flags & MU_URL_SECRET) &&
+      (ctx->flags & MU_URL_PARSE_HIDEPASS))
+    {
+      /* Obfuscate the password */
+#define PASS_REPL "***"
+#define PASS_REPL_LEN (sizeof (PASS_REPL) - 1)
+      size_t plen = mu_secret_length (url->secret);
+      size_t nlen = strlen (url->name);
+      size_t len = nlen - plen + PASS_REPL_LEN + 1;
+      char *newname;
+      
+      memset (url->name + ctx->passoff, 0, plen);
+      if (len > nlen + 1)
+	{
+	  newname = realloc (url->name, len);
+	  if (!newname)
+	    return rc;
+	  url->name = newname;
+	}
+      else
+	newname = url->name;
+      memmove (newname + ctx->passoff + PASS_REPL_LEN,
+	       newname + ctx->passoff + plen,
+	       nlen - (ctx->passoff + plen) + 1);
+      memcpy (newname + ctx->passoff, PASS_REPL, PASS_REPL_LEN);
+    }
+
+  return 0;
+}
+
+int
+mu_url_create_hint (mu_url_t *purl, const char *str, int flags,
+		    mu_url_t hint)
+{
+  int rc;
+  struct mu_url_ctx ctx;
+  mu_url_t url = calloc (1, sizeof (*url));
+  if (url == NULL)
+    return ENOMEM;
+  url->name = strdup (str);
+  if (!url->name)
+    {
+      free (url);
+      return ENOMEM;
+    }
+  memset (&ctx, 0, sizeof (ctx));
+  ctx.flags = flags;
+  ctx.input = str;
+  ctx.cur = ctx.input;
+  ctx.url = url;
+  rc = _mu_url_create_internal (&ctx, hint);
+  free (ctx.tokbuf);
+  if (rc)
+    mu_url_destroy (&url);
+  else
+    *purl = url;
+  return rc;
+}
+      
+int
+mu_url_create (mu_url_t *purl, const char *str)
+{
+  return mu_url_create_hint (purl, str,
+			     MU_URL_PARSE_HEXCODE |
+			     MU_URL_PARSE_HIDEPASS |
+			     MU_URL_PARSE_PORTSRV |
+			     MU_URL_PARSE_PIPE |
+			     MU_URL_PARSE_SLASH, NULL);
+}
diff --git a/libmailutils/url/decode.c b/libmailutils/url/decode.c
new file mode 100644
index 0000000..9aca0e2
--- /dev/null
+++ b/libmailutils/url/decode.c
@@ -0,0 +1,118 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 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, see 
+   <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#ifdef HAVE_STRINGS_H
+# include <strings.h>
+#endif
+
+#include <mailutils/types.h>
+#include <mailutils/util.h>
+#include <mailutils/secret.h>
+#include <mailutils/sys/url.h>
+
+struct decode_tab
+{
+  int mask;
+  int (*fun) (mu_url_t, size_t);
+  size_t off;
+};
+
+static int
+_url_dec_str (mu_url_t url, size_t off)
+{
+  char **pptr = (char**) ((char*) url + off);
+  mu_str_url_decode_inline (*pptr);
+  return 0;
+}
+
+static int
+_url_dec_param (mu_url_t url, size_t off)
+{
+  int i;
+
+  for (i = 0; i < url->fvcount; i++)
+    mu_str_url_decode_inline (url->fvpairs[i]);
+  return 0;
+}
+
+static int
+_url_dec_query (mu_url_t url, size_t off)
+{
+  int i;
+
+  for (i = 0; i < url->qargc; i++)
+    mu_str_url_decode_inline (url->qargv[i]);
+  return 0;
+}
+
+static int
+_url_dec_secret (mu_url_t url, size_t off)
+{
+  char *pass;
+  mu_secret_t newsec;
+  int rc;
+
+  rc = mu_str_url_decode (&pass, mu_secret_password (url->secret));
+  if (rc)
+    return rc;
+  rc = mu_secret_create (&newsec, pass, strlen (pass));
+  memset (pass, 0, strlen (pass));
+  free (pass);
+  if (rc)
+    return rc;
+  mu_secret_destroy (&url->secret);
+  url->secret = newsec;
+  return 0;
+}
+
+static struct decode_tab decode_tab[] = {
+  { MU_URL_SCHEME, _url_dec_str, mu_offsetof (struct _mu_url, scheme) },
+  { MU_URL_USER,   _url_dec_str, mu_offsetof (struct _mu_url, user) },
+  { MU_URL_SECRET, _url_dec_secret },
+  { MU_URL_AUTH,   _url_dec_str, mu_offsetof (struct _mu_url, auth) },
+  { MU_URL_HOST,   _url_dec_str, mu_offsetof (struct _mu_url, host) },
+  { MU_URL_PATH,   _url_dec_str, mu_offsetof (struct _mu_url, path) },
+  { MU_URL_PARAM,  _url_dec_param, 0 },
+  { MU_URL_QUERY,  _url_dec_query, 0 }
+};
+
+int
+mu_url_decode (mu_url_t url)
+{
+  int i;
+
+  if (!url)
+    return EINVAL;
+  for (i = 0; i < MU_ARRAY_SIZE (decode_tab); i++)
+    {
+      if (url->flags & decode_tab[i].mask)
+	{
+	  int rc = decode_tab[i].fun (url, decode_tab[i].off);
+	  if (rc)
+	    return rc;
+	}
+    }
+  return 0;
+}
+
diff --git a/libproto/nntp/url.c b/libmailutils/url/destroy.c
similarity index 51%
copy from libproto/nntp/url.c
copy to libmailutils/url/destroy.c
index 3870107..a46dc31 100644
--- a/libproto/nntp/url.c
+++ b/libmailutils/url/destroy.c
@@ -1,5 +1,5 @@
 /* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 2004, 2007, 2010 Free Software Foundation, Inc.
+   Copyright (C) 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
@@ -12,39 +12,63 @@
    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, see
+   Public License along with this library.  If not, see 
    <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
 #endif
 
-#ifdef ENABLE_NNTP
-
 #include <errno.h>
 #include <stdlib.h>
 #include <string.h>
-#include <errno.h>
 #ifdef HAVE_STRINGS_H
 # include <strings.h>
 #endif
 
-#include <mailutils/nntp.h>
-
+#include <mailutils/types.h>
+#include <mailutils/argcv.h>
+#include <mailutils/secret.h>
+#include <mailutils/errno.h>
 #include <mailutils/sys/url.h>
 
-/*
-  POP URL:
-  nntp://<host>:<port>/<newsgroup-name>/<article-number>
-*/
-
-int
-_nntp_url_init (mu_url_t url)
+void
+mu_url_destroy (mu_url_t * purl)
 {
-  if (url->port == 0)
-    url->port = MU_NNTP_DEFAULT_PORT;
+  if (purl && *purl)
+    {
+      mu_url_t url = (*purl);
 
-  return status;
-}
+      if (url->_destroy)
+	url->_destroy (url);
 
-#endif
+      if (url->name)
+	free (url->name);
+
+      if (url->scheme)
+	free (url->scheme);
+
+      if (url->user)
+	free (url->user);
+
+      mu_secret_destroy (&url->secret);
+
+      if (url->auth)
+	free (url->auth);
+
+      if (url->host)
+	free (url->host);
+
+      if (url->path)
+	free (url->path);
+
+      if (url->fvcount)
+	mu_argcv_free (url->fvcount, url->fvpairs);
+
+      mu_argcv_free (url->qargc, url->qargv);
+
+      free (url);
+
+      *purl = NULL;
+    }
+}
diff --git a/libproto/nntp/url.c b/libmailutils/url/dup.c
similarity index 61%
copy from libproto/nntp/url.c
copy to libmailutils/url/dup.c
index 3870107..d9a7383 100644
--- a/libproto/nntp/url.c
+++ b/libmailutils/url/dup.c
@@ -1,5 +1,5 @@
 /* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 2004, 2007, 2010 Free Software Foundation, Inc.
+   Copyright (C) 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
@@ -12,39 +12,45 @@
    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, see
+   Public License along with this library.  If not, see 
    <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
 #endif
 
-#ifdef ENABLE_NNTP
-
 #include <errno.h>
 #include <stdlib.h>
 #include <string.h>
-#include <errno.h>
 #ifdef HAVE_STRINGS_H
 # include <strings.h>
 #endif
 
-#include <mailutils/nntp.h>
-
+#include <mailutils/types.h>
+#include <mailutils/util.h>
 #include <mailutils/sys/url.h>
 
-/*
-  POP URL:
-  nntp://<host>:<port>/<newsgroup-name>/<article-number>
-*/
-
 int
-_nntp_url_init (mu_url_t url)
+mu_url_dup (mu_url_t old_url, mu_url_t *new_url)
 {
-  if (url->port == 0)
-    url->port = MU_NNTP_DEFAULT_PORT;
-
-  return status;
+  int rc;
+  mu_url_t url = calloc (1, sizeof (*url));
+
+  if (!url)
+    return ENOMEM;
+  url->name = strdup (old_url->name);
+  if (!url->name)
+    {
+      free (url);
+      return ENOMEM;
+    }
+  
+  rc = mu_url_copy_hints (url, old_url);
+  if (rc)
+    {
+      mu_url_destroy (&url);
+      return rc;
+    }
+  *new_url = url;
+  return 0;
 }
-
-#endif
diff --git a/libmailutils/url/expand.c b/libmailutils/url/expand.c
new file mode 100644
index 0000000..bb2ef95
--- /dev/null
+++ b/libmailutils/url/expand.c
@@ -0,0 +1,216 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 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, see 
+   <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <errno.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#ifdef HAVE_STRINGS_H
+# include <strings.h>
+#endif
+
+#include <mailutils/types.h>
+#include <mailutils/util.h>
+#include <mailutils/errno.h>
+#include <mailutils/argcv.h>
+#include <mailutils/sys/url.h>
+
+/* Default mailbox path generator */
+static char *
+_url_path_default (const char *spooldir, const char *user, int unused)
+{
+  return mu_make_file_name (spooldir, user);
+}
+
+/* Hashed indexing */
+static char *
+_url_path_hashed (const char *spooldir, const char *user, int param)
+{
+  int i;
+  int ulen = strlen (user);
+  char *mbox;
+  unsigned hash;
+
+  if (param > ulen)
+    param = ulen;
+  for (i = 0, hash = 0; i < param; i++)
+    hash += user[i];
+
+  mbox = malloc (ulen + strlen (spooldir) + 5);
+  sprintf (mbox, "%s/%02X/%s", spooldir, hash % 256, user);
+  return mbox;
+}
+
+static int transtab[] = {
+  'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
+  'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
+  'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
+  'y', 'z', 'a', 'b', 'c', 'd', 'e', 'f',
+  'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
+  'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
+  'w', 'x', 'y', 'z', 'a', 'b', 'c', 'd',
+  'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
+  'm', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
+  'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
+  'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
+  'x', 'y', 'z', 'b', 'c', 'd', 'e', 'f',
+  'g', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
+  'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
+  'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
+  'x', 'y', 'z', 'b', 'c', 'd', 'e', 'f',
+  'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
+  'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
+  'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
+  'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
+  'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
+  'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
+  'x', 'y', 'z', 'a', 'b', 'c', 'd', 'e',
+  'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
+  'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
+  'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
+  'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
+  'y', 'z', 'b', 'c', 'd', 'e', 'f', 'g',
+  'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
+  'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
+  'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
+  'y', 'z', 'b', 'c', 'd', 'e', 'f', 'g'
+};
+
+/* Forward Indexing */
+static char *
+_url_path_index (const char *spooldir, const char *iuser, int index_depth)
+{
+  const unsigned char* user = (const unsigned char*) iuser;
+  int i, ulen = strlen (iuser);
+  char *mbox, *p;
+
+  if (ulen == 0)
+    return NULL;
+
+  mbox = malloc (ulen + strlen (spooldir) + 2*index_depth + 2);
+  strcpy (mbox, spooldir);
+  p = mbox + strlen (mbox);
+  for (i = 0; i < index_depth && i < ulen; i++)
+    {
+      *p++ = '/';
+      *p++ = transtab[ user[i] ];
+    }
+  for (; i < index_depth; i++)
+    {
+      *p++ = '/';
+      *p++ = transtab[ user[ulen-1] ];
+    }
+  *p++ = '/';
+  strcpy (p, iuser);
+  return mbox;
+}
+
+/* Reverse Indexing */
+static char *
+_url_path_rev_index (const char *spooldir, const char *iuser, int index_depth)
+{
+  const unsigned char* user = (const unsigned char*) iuser;
+  int i, ulen = strlen (iuser);
+  char *mbox, *p;
+
+  if (ulen == 0)
+    return NULL;
+
+  mbox = malloc (ulen + strlen (spooldir) + 2*index_depth + 1);
+  strcpy (mbox, spooldir);
+  p = mbox + strlen (mbox);
+  for (i = 0; i < index_depth && i < ulen; i++)
+    {
+      *p++ = '/';
+      *p++ = transtab[ user[ulen - i - 1] ];
+    }
+  for (; i < index_depth; i++)
+    {
+      *p++ = '/';
+      *p++ = transtab[ user[0] ];
+    }
+  *p++ = '/';
+  strcpy (p, iuser);
+  return mbox;
+}
+
+static int
+rmselector (const char *p, void *data MU_ARG_UNUSED)
+{
+  return strncmp (p, "type=", 5) == 0
+	 || strncmp (p, "user=", 5) == 0
+	 || strncmp (p, "param=", 6) == 0;
+}
+
+int
+mu_url_expand_path (mu_url_t url)
+{
+  size_t i;
+  char *user = NULL;
+  int param = 0;
+  char *p;
+  char *(*fun) (const char *, const char *, int) = _url_path_default;
+
+  if (url->fvcount == 0)
+    return 0;
+
+  for (i = 0; i < url->fvcount; i++)
+    {
+      p = url->fvpairs[i];
+      if (strncmp (p, "type=", 5) == 0)
+	{
+	  char *type = p + 5;
+
+	  if (strcmp (type, "hash") == 0)
+	    fun = _url_path_hashed;
+	  else if (strcmp (type, "index") == 0)
+	    fun = _url_path_index;
+	  else if (strcmp (type, "rev-index") == 0)
+	    fun = _url_path_rev_index;
+	  else
+	    return MU_ERR_NOENT;
+	}
+      else if (strncmp (p, "user=", 5) == 0)
+	{
+	  user = p + 5;
+	}
+      else if (strncmp (p, "param=", 6) == 0)
+	{
+	  param = strtoul (p + 6, NULL, 0);
+	}
+    }
+
+  if (user)
+    {
+      char *p = fun (url->path, user, param);
+      if (p)
+	{
+	  free (url->path);
+	  url->path = p;
+	}
+      mu_argcv_remove (&url->fvcount, &url->fvpairs, rmselector, NULL);
+    }
+  else
+    return MU_ERR_NOENT;
+
+  return 0;
+}
+
diff --git a/libmailutils/base/freeitem.c b/libmailutils/url/flag.c
similarity index 75%
copy from libmailutils/base/freeitem.c
copy to libmailutils/url/flag.c
index ad382a4..bf06f68 100644
--- a/libmailutils/base/freeitem.c
+++ b/libmailutils/url/flag.c
@@ -18,11 +18,23 @@
 #ifdef HAVE_CONFIG_H
 # include <config.h>
 #endif
-#include <stdlib.h>
 
-/* Default destroy_item function. */
-void
-mu_list_free_item (void *item)
+#include <errno.h>
+#include <mailutils/sys/url.h>
+
+int
+mu_url_get_flags (mu_url_t url, int *pf)
+{
+  if (!url || !pf)
+    return EINVAL;
+  *pf = url->flags;
+  return 0;
+}
+
+int
+mu_url_has_flag (mu_url_t url, int flags)
 {
-  free (item);
+  if (!url)
+    return 0;
+  return url->flags & flags;
 }
diff --git a/libmailutils/url/get-auth.c b/libmailutils/url/get-auth.c
new file mode 100644
index 0000000..b9650a5
--- /dev/null
+++ b/libmailutils/url/get-auth.c
@@ -0,0 +1,3 @@
+#define URL_PART auth
+#include "accessor.h"
+
diff --git a/libmailutils/url/get-host.c b/libmailutils/url/get-host.c
new file mode 100644
index 0000000..6de6b56
--- /dev/null
+++ b/libmailutils/url/get-host.c
@@ -0,0 +1,2 @@
+#define URL_PART host
+#include "accessor.h"
diff --git a/libmailutils/url/get-param.c b/libmailutils/url/get-param.c
new file mode 100644
index 0000000..c2be823
--- /dev/null
+++ b/libmailutils/url/get-param.c
@@ -0,0 +1,131 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 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, see 
+   <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#ifdef HAVE_STRINGS_H
+# include <strings.h>
+#endif
+
+#include <mailutils/types.h>
+#include <mailutils/errno.h>
+#include <mailutils/argcv.h>
+#include <mailutils/sys/url.h>
+
+/* field-value pairs accessors */
+int
+mu_url_sget_fvpairs (const mu_url_t url, size_t *fvc, char ***fvp)
+{
+  if (url == NULL)
+    return EINVAL;
+  /* FIXME: no _get_fvpairs method, but the method stuff needs to be rewritten
+     anyway */
+  *fvc = url->fvcount;
+  *fvp = url->fvpairs;
+  return 0;
+}
+
+int
+mu_url_sget_param (const mu_url_t url, const char *param, const char **val)
+{
+  size_t fvc;
+  char **fvp;
+  int status = mu_url_sget_fvpairs (url, &fvc, &fvp);
+
+  if (status)
+    return status;
+
+  if (fvc)
+    {
+      size_t i;
+      
+      for (i = 0; i < fvc; i++)
+	{
+	  const char *p;
+	  char *q;
+
+	  for (p = param, q = fvp[i]; *p && *q && *p == *q; p++, q++)
+	    ;
+	  if (*p == 0)
+	    {
+	      if (*q == 0)
+		{
+		  if (val)
+		    *val = q;
+		  return 0;
+		}
+	      else if (*q == '=')
+		{
+		  if (val)
+		    *val = q + 1;
+		  return 0;
+		}
+	    }
+	}
+    }
+ 
+  return MU_ERR_NOENT;
+}
+  
+int
+mu_url_aget_fvpairs (const mu_url_t url, size_t *pfvc, char ***pfvp)
+{
+  size_t fvc, i;
+  char **fvp;
+  char **fvcopy;
+
+  int rc = mu_url_sget_fvpairs (url, &fvc, &fvp);
+  if (rc)
+    return rc;
+
+  fvcopy = calloc (fvc + 1, sizeof (fvcopy[0]));
+  if (!fvcopy)
+    return errno;
+  for (i = 0; i < fvc; i++)
+    {
+      if (!(fvcopy[i] = strdup (fvp[i])))
+	{
+	  mu_argcv_free (i, fvcopy);
+	  return errno;
+	}
+    }
+  fvcopy[i] = NULL;
+  *pfvc = fvc;
+  *pfvp = fvcopy;
+  return 0;
+}
+
+int
+mu_url_aget_param (const mu_url_t url, const char *param, char **val)
+{
+  const char *s;
+  int status = mu_url_sget_param (url, param, &s);
+
+  if (status == 0)
+    {
+      *val = strdup (s);
+      if (!*val)
+	status = ENOMEM;
+    }
+  return status;
+}
+
diff --git a/libmailutils/url/get-path.c b/libmailutils/url/get-path.c
new file mode 100644
index 0000000..3f906b9
--- /dev/null
+++ b/libmailutils/url/get-path.c
@@ -0,0 +1,2 @@
+#define URL_PART path
+#include "accessor.h"
diff --git a/libmailutils/url/get-portstr.c b/libmailutils/url/get-portstr.c
new file mode 100644
index 0000000..9b211bf
--- /dev/null
+++ b/libmailutils/url/get-portstr.c
@@ -0,0 +1,2 @@
+#define URL_PART portstr
+#include "accessor.h"
diff --git a/libproto/pop/url.c b/libmailutils/url/get-query.c
similarity index 52%
copy from libproto/pop/url.c
copy to libmailutils/url/get-query.c
index f528064..cf74922 100644
--- a/libproto/pop/url.c
+++ b/libmailutils/url/get-query.c
@@ -1,6 +1,5 @@
 /* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 1999, 2000, 2001, 2003, 2007, 2008, 2010 Free Software
-   Foundation, Inc.
+   Copyright (C) 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
@@ -13,52 +12,61 @@
    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, see
+   Public License along with this library.  If not, see 
    <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
 #endif
 
-#ifdef ENABLE_POP
-
 #include <errno.h>
 #include <stdlib.h>
 #include <string.h>
-#include <errno.h>
 #ifdef HAVE_STRINGS_H
 # include <strings.h>
 #endif
 
+#include <mailutils/types.h>
+#include <mailutils/errno.h>
+#include <mailutils/argcv.h>
 #include <mailutils/sys/url.h>
-#include <mailutils/sys/registrar.h>
-
-/*
-  POP URLs:
-    pop://[<user>[;AUTH=<auth>]@]<host>[:<port>]
-    pop://[<user>[:pass]@]<host>[:<port>]
-*/
 
 int
-_url_pop_init (mu_url_t url)
+mu_url_sget_query (const mu_url_t url, size_t *qc, char ***qv)
 {
-  if (url->port == 0)
-    url->port = MU_POP_PORT;
+  if (url == NULL)
+    return EINVAL;
+  /* See FIXME below */
+  *qc = url->qargc;
+  *qv = url->qargv;
   return 0;
 }
 
-/*
-  POPS URLs:
-    pops://[<user>[;AUTH=<auth>]@]<host>[:<port>]
-    pops://[<user>[:pass]@]<host>[:<port>]
-*/
-
 int
-_url_pops_init (mu_url_t url)
+mu_url_aget_query (const mu_url_t url, size_t *qc, char ***qv)
 {
-  if (url->port == 0)
-    url->port = MU_POPS_PORT;
+  size_t qargc, i;
+  char **qargv;
+  char **qcopy;
+
+  int rc = mu_url_sget_fvpairs (url, &qargc, &qargv);
+  if (rc)
+    return rc;
+
+  qcopy = calloc (qargc + 1, sizeof (qcopy[0]));
+  if (!qcopy)
+    return errno;
+  for (i = 0; i < qargc; i++)
+    {
+      if (!(qcopy[i] = strdup (qargv[i])))
+	{
+	  mu_argcv_free (i, qcopy);
+	  return errno;
+	}
+    }
+  qcopy[i] = NULL;
+  *qc = qargc;
+  *qv = qcopy;
   return 0;
 }
 
-#endif /* ENABLE_POP */
diff --git a/libmailutils/url/get-scheme.c b/libmailutils/url/get-scheme.c
new file mode 100644
index 0000000..983902e
--- /dev/null
+++ b/libmailutils/url/get-scheme.c
@@ -0,0 +1,2 @@
+#define URL_PART scheme
+#include "accessor.h"
diff --git a/libproto/nntp/url.c b/libmailutils/url/get-secret.c
similarity index 66%
copy from libproto/nntp/url.c
copy to libmailutils/url/get-secret.c
index 3870107..34fb229 100644
--- a/libproto/nntp/url.c
+++ b/libmailutils/url/get-secret.c
@@ -1,5 +1,5 @@
 /* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 2004, 2007, 2010 Free Software Foundation, Inc.
+   Copyright (C) 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
@@ -12,39 +12,33 @@
    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, see
+   Public License along with this library.  If not, see 
    <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
 #endif
 
-#ifdef ENABLE_NNTP
-
 #include <errno.h>
 #include <stdlib.h>
 #include <string.h>
-#include <errno.h>
 #ifdef HAVE_STRINGS_H
 # include <strings.h>
 #endif
 
-#include <mailutils/nntp.h>
-
+#include <mailutils/types.h>
+#include <mailutils/errno.h>
+#include <mailutils/secret.h>
 #include <mailutils/sys/url.h>
 
-/*
-  POP URL:
-  nntp://<host>:<port>/<newsgroup-name>/<article-number>
-*/
-
 int
-_nntp_url_init (mu_url_t url)
+mu_url_get_secret (const mu_url_t url, mu_secret_t *psecret)
 {
-  if (url->port == 0)
-    url->port = MU_NNTP_DEFAULT_PORT;
-
-  return status;
+  if (url->_get_secret)
+    return url->_get_secret (url, psecret);
+  if (url->secret == NULL)
+    return MU_ERR_NOENT;
+  mu_secret_ref (url->secret);
+  *psecret = url->secret;
+  return 0;
 }
-
-#endif
diff --git a/libmailutils/url/get-user.c b/libmailutils/url/get-user.c
new file mode 100644
index 0000000..5a3ab79
--- /dev/null
+++ b/libmailutils/url/get-user.c
@@ -0,0 +1,2 @@
+#define URL_PART user
+#include "accessor.h"
diff --git a/libmailutils/url/match.c b/libmailutils/url/match.c
new file mode 100644
index 0000000..4932f45
--- /dev/null
+++ b/libmailutils/url/match.c
@@ -0,0 +1,102 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 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, see 
+   <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#ifdef HAVE_STRINGS_H
+# include <strings.h>
+#endif
+
+#include <mailutils/types.h>
+#include <mailutils/cstr.h>
+#include <mailutils/sys/url.h>
+
+#define is_wildcard(s) ((s)[0] == '*' && s[1] == 0)
+
+#define WEIGHT_SCHEME 3
+#define WEIGHT_USER   4
+#define WEIGHT_HOST   2
+#define WEIGHT_PORT   1
+
+int
+mu_url_matches_ticket (mu_url_t ticket, mu_url_t url, int *pwc)
+{
+  int wcnt = 0;
+					      
+  if (is_wildcard (ticket->scheme))
+    wcnt += WEIGHT_SCHEME;
+  else if (mu_c_strcasecmp (ticket->scheme, url->scheme))
+    return 0;
+
+  if (ticket->flags & MU_URL_HOST)
+    {
+      if (is_wildcard (ticket->host))
+	wcnt += WEIGHT_HOST;
+      else if (url->flags & MU_URL_HOST)
+	{
+	  if (mu_c_strcasecmp (ticket->host, url->host))
+	    /* FIXME: Compare IP addresses */
+	    return 0;
+	}
+      else
+	return 0;
+    }
+  else
+    wcnt += WEIGHT_HOST;
+
+  if (ticket->flags & MU_URL_PORT)
+    {
+      if (is_wildcard (ticket->portstr))
+	wcnt += WEIGHT_PORT;
+      else if (url->port & MU_URL_PORT)
+	{
+	  if (ticket->port != url->port)
+	    return 0;
+	  else
+	    wcnt += WEIGHT_PORT;
+	}
+    }
+  else
+    wcnt += WEIGHT_PORT;
+  
+  if (ticket->flags & MU_URL_USER)
+    {
+      if (is_wildcard (ticket->user))
+	wcnt += WEIGHT_USER;
+      
+      /* If ticket has a user or pass, but url doesn't, that's OK, we were
+	 looking for this info. But if url does have a user/pass, it
+	 must match the ticket. */
+      else if (url->flags & MU_URL_USER)
+	{
+	  if (strcmp (ticket->user, url->user))
+	    return 0;
+	}
+    }
+  else
+    wcnt += WEIGHT_USER;
+  
+  /* Guess it matches. */
+  if (pwc)
+    *pwc = wcnt;
+  return 1;
+}
diff --git a/libproto/nntp/url.c b/libmailutils/url/port.c
similarity index 62%
copy from libproto/nntp/url.c
copy to libmailutils/url/port.c
index 3870107..88b7dd1 100644
--- a/libproto/nntp/url.c
+++ b/libmailutils/url/port.c
@@ -1,5 +1,5 @@
 /* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 2004, 2007, 2010 Free Software Foundation, Inc.
+   Copyright (C) 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
@@ -12,39 +12,42 @@
    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, see
+   Public License along with this library.  If not, see 
    <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
 #endif
 
-#ifdef ENABLE_NNTP
-
 #include <errno.h>
 #include <stdlib.h>
 #include <string.h>
-#include <errno.h>
 #ifdef HAVE_STRINGS_H
 # include <strings.h>
 #endif
 
-#include <mailutils/nntp.h>
-
+#include <mailutils/types.h>
+#include <mailutils/cstr.h>
 #include <mailutils/sys/url.h>
 
-/*
-  POP URL:
-  nntp://<host>:<port>/<newsgroup-name>/<article-number>
-*/
+int
+mu_url_get_port (const mu_url_t url, unsigned *pport)
+{
+  if (url == NULL)
+    return EINVAL;
+  if (url->_get_port)
+    return url->_get_port (url, pport);
+  *pport = url->port;
+  return 0;
+}
 
 int
-_nntp_url_init (mu_url_t url)
+mu_url_is_same_port (mu_url_t url1, mu_url_t url2)
 {
-  if (url->port == 0)
-    url->port = MU_NNTP_DEFAULT_PORT;
+  unsigned p1 = 0, p2 = 0;
 
-  return status;
+  mu_url_get_port (url1, &p1);
+  mu_url_get_port (url2, &p2);
+  return (p1 == p2);
 }
 
-#endif
diff --git a/libproto/nntp/url.c b/libmailutils/url/scheme.c
similarity index 60%
copy from libproto/nntp/url.c
copy to libmailutils/url/scheme.c
index 3870107..e68fa91 100644
--- a/libproto/nntp/url.c
+++ b/libmailutils/url/scheme.c
@@ -1,5 +1,5 @@
 /* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 2004, 2007, 2010 Free Software Foundation, Inc.
+   Copyright (C) 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
@@ -12,39 +12,44 @@
    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, see
+   Public License along with this library.  If not, see 
    <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
 #endif
 
-#ifdef ENABLE_NNTP
-
 #include <errno.h>
 #include <stdlib.h>
 #include <string.h>
-#include <errno.h>
 #ifdef HAVE_STRINGS_H
 # include <strings.h>
 #endif
 
-#include <mailutils/nntp.h>
-
+#include <mailutils/types.h>
+#include <mailutils/cstr.h>
 #include <mailutils/sys/url.h>
 
-/*
-  POP URL:
-  nntp://<host>:<port>/<newsgroup-name>/<article-number>
-*/
+int
+mu_url_set_scheme (mu_url_t url, const char *scheme)
+{
+  char *p;
+  if (!url || !scheme)
+    return EINVAL;
+  p = realloc (url->scheme, strlen (scheme) + 1);
+  if (!p)
+    return ENOMEM;
+  strcpy (url->scheme, scheme);
+  return 0;
+}
 
 int
-_nntp_url_init (mu_url_t url)
+mu_url_is_scheme (mu_url_t url, const char *scheme)
 {
-  if (url->port == 0)
-    url->port = MU_NNTP_DEFAULT_PORT;
+  if (url && scheme && url->scheme 
+      && mu_c_strcasecmp (url->scheme, scheme) == 0)
+    return 1;
 
-  return status;
+  return 0;
 }
 
-#endif
diff --git a/libmailutils/mailbox/hdrfirst.c b/libmailutils/url/uplevel.c
similarity index 51%
copy from libmailutils/mailbox/hdrfirst.c
copy to libmailutils/url/uplevel.c
index 363021f..749fe1f 100644
--- a/libmailutils/mailbox/hdrfirst.c
+++ b/libmailutils/url/uplevel.c
@@ -12,37 +12,60 @@
    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, see
+   Public License along with this library.  If not, see 
    <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
 #endif
 
+#include <errno.h>
 #include <stdlib.h>
+#include <string.h>
+#ifdef HAVE_STRINGS_H
+# include <strings.h>
+#endif
+
 #include <mailutils/types.h>
-#include <mailutils/header.h>
 #include <mailutils/errno.h>
+#include <mailutils/sys/url.h>
 
 int
-mu_header_sget_firstof (mu_header_t hdr, char **names,
-			const char **pval, int *pidx)
+mu_url_uplevel (mu_url_t url, mu_url_t *upurl)
 {
-  int status;
-  const char *s = NULL;
-  int i;
-  
-  for (i = 0; names[i]; i++)
+  int rc;
+  char *p;
+  mu_url_t new_url;
+
+  if (url->_uplevel)
+    return url->_uplevel (url, upurl);
+
+  if (!url->path)
+    return MU_ERR_NOENT;
+  p = strrchr (url->path, '/');
+
+  rc = mu_url_dup (url, &new_url);
+  if (rc == 0)
     {
-      status = mu_header_sget_value (hdr, names[i], &s);
-      if (status == 0 && *s != 0)
+      if (!p || p == url->path)
+	{
+	  free (new_url->path);
+	  new_url->path = NULL;
+	}
+      else
 	{
-	  if (pval)
-	    *pval = s;
-	  if (pidx)
-	    *pidx = i;
-	  return 0;
+	  size_t size = p - url->path;
+	  new_url->path = realloc (new_url->path, size + 1);
+	  if (!new_url->path)
+	    {
+	      mu_url_destroy (&new_url);
+	      return ENOMEM;
+	    }
+	  memcpy (new_url->path, url->path, size);
+	  new_url->path[size] = 0;
 	}
+      *upurl = new_url;
     }
-  return MU_ERR_NOENT;
+  return rc;
 }
+
diff --git a/libmailutils/base/freeitem.c b/libmailutils/url/urlstr.c
similarity index 82%
copy from libmailutils/base/freeitem.c
copy to libmailutils/url/urlstr.c
index ad382a4..31e5197 100644
--- a/libmailutils/base/freeitem.c
+++ b/libmailutils/url/urlstr.c
@@ -19,10 +19,13 @@
 # include <config.h>
 #endif
 #include <stdlib.h>
+#include <mailutils/types.h>
+#include <mailutils/sys/url.h>
 
-/* Default destroy_item function. */
-void
-mu_list_free_item (void *item)
+const char *
+mu_url_to_string (const mu_url_t url)
 {
-  free (item);
+  if (url == NULL || url->name == NULL)
+    return "";
+  return url->name;
 }
diff --git a/libmu_cpp/url.cc b/libmu_cpp/url.cc
index 9c9353c..fe7aedc 100644
--- a/libmu_cpp/url.cc
+++ b/libmu_cpp/url.cc
@@ -54,19 +54,17 @@ Url :: ~Url ()
 void
 Url :: parse ()
 {
-  int status = mu_url_parse (url);
-  if (status)
-    throw Exception ("Url::parse", status);
+  /* FIXME: Remove */
 }
 
-long
+unsigned
 Url :: get_port ()
 {
-  long port;
+  unsigned port;
   int status = mu_url_get_port (url, &port);
   if (status)
     throw Exception ("Url::get_port", status);
-  return port;
+  return (unsigned short) port;
 }
 
 std::string
diff --git a/libproto/imap/folder.c b/libproto/imap/folder.c
index ed5ba54..ce15084 100644
--- a/libproto/imap/folder.c
+++ b/libproto/imap/folder.c
@@ -67,7 +67,7 @@ static struct _mu_record _imap_record =
   MU_IMAP_PRIO,
   MU_IMAP_SCHEME,
   MU_RECORD_DEFAULT,
-  MU_URL_CRED | MU_URL_INET | MU_URL_PATH,
+  MU_URL_SCHEME | MU_URL_CRED | MU_URL_INET | MU_URL_PATH,
   MU_URL_HOST,
   _url_imap_init,     /* url entry.  */
   _mailbox_imap_init, /* Mailbox entry.  */
@@ -91,7 +91,7 @@ static struct _mu_record _imaps_record =
   MU_IMAP_PRIO,
   MU_IMAPS_SCHEME,
   MU_RECORD_DEFAULT,
-  MU_URL_CRED | MU_URL_INET | MU_URL_PATH | MU_URL_PARAM,
+  MU_URL_SCHEME | MU_URL_CRED | MU_URL_INET | MU_URL_PATH | MU_URL_PARAM,
   MU_URL_HOST,
   _url_imaps_init,     /* url entry.  */
   _mailbox_imaps_init, /* Mailbox entry.  */
@@ -629,7 +629,7 @@ folder_imap_open (mu_folder_t folder, int flags)
 {
   f_imap_t f_imap = folder->data;
   const char *host;
-  long port = f_imap->imaps ? MU_IMAPS_PORT : MU_IMAP_PORT;
+  unsigned port = f_imap->imaps ? MU_IMAPS_PORT : MU_IMAP_PORT;
   int status = 0;
 
   /* If we are already open for business, noop.  */
diff --git a/libproto/maildir/folder.c b/libproto/maildir/folder.c
index 657dfa5..d53c526 100644
--- a/libproto/maildir/folder.c
+++ b/libproto/maildir/folder.c
@@ -99,7 +99,7 @@ static struct _mu_record _maildir_record =
   MU_MAILDIR_PRIO,
   MU_MAILDIR_SCHEME,
   MU_RECORD_LOCAL,
-  MU_URL_PATH,
+  MU_URL_SCHEME | MU_URL_PATH,
   MU_URL_PATH,
   mu_url_expand_path, /* Url init.  */
   _mailbox_maildir_init, /* Mailbox init.  */
diff --git a/libproto/mailer/prog.c b/libproto/mailer/prog.c
index d026177..6a3c615 100644
--- a/libproto/mailer/prog.c
+++ b/libproto/mailer/prog.c
@@ -47,7 +47,7 @@ static struct _mu_record _prog_record =
   MU_RECORD_DEFAULT,
   /* FIXME: MU_URL_USER could be used to request running with this
      user privileges. */
-  MU_URL_PATH | MU_URL_QUERY, 
+  MU_URL_SCHEME | MU_URL_PATH | MU_URL_QUERY, 
   MU_URL_PATH,
   _url_prog_init,    /* url init.  */
   _mu_mailer_mailbox_init,  /* Mailbox entry.  */
diff --git a/libproto/mailer/remote.c b/libproto/mailer/remote.c
index ae96cee..7198571 100644
--- a/libproto/mailer/remote.c
+++ b/libproto/mailer/remote.c
@@ -76,7 +76,7 @@ static struct _mu_record _mu_remote_smtp_record = {
   MU_SMTP_PRIO,
   "remote+smtp",
   MU_RECORD_DEFAULT,
-  MU_URL_CRED | MU_URL_INET | MU_URL_PATH | MU_URL_PARAM,
+  MU_URL_SCHEME | MU_URL_CRED | MU_URL_INET | MU_URL_PATH | MU_URL_PARAM,
   MU_URL_HOST,
   _url_remote_smtp_init,	/* url init.  */
   _mu_mailer_mailbox_init,      /* Mailbox init.  */
@@ -108,7 +108,7 @@ static struct _mu_record _mu_remote_sendmail_record =
   MU_SENDMAIL_PRIO,
   "remote+sendmail",
   MU_RECORD_DEFAULT,
-  MU_URL_PATH,
+  MU_URL_SCHEME | MU_URL_PATH,
   MU_URL_PATH,
   _url_remote_sendmail_init,    /* url init.  */
   _mu_mailer_mailbox_init,      /* Mailbox entry.  */
@@ -137,7 +137,7 @@ static struct _mu_record _mu_remote_prog_record =
   MU_PROG_PRIO,
   "remote+prog",
   MU_RECORD_DEFAULT,
-  MU_URL_CRED | MU_URL_PATH | MU_URL_QUERY,
+  MU_URL_SCHEME | MU_URL_CRED | MU_URL_PATH | MU_URL_QUERY,
   MU_URL_PATH,
   _url_remote_prog_init,    /* url init.  */
   _mu_mailer_mailbox_init,  /* Mailbox entry.  */
diff --git a/libproto/mailer/sendmail.c b/libproto/mailer/sendmail.c
index f1ba068..7ab01a0 100644
--- a/libproto/mailer/sendmail.c
+++ b/libproto/mailer/sendmail.c
@@ -271,9 +271,9 @@ static struct _mu_record _sendmail_record =
   MU_SENDMAIL_PRIO,
   MU_SENDMAIL_SCHEME,
   MU_RECORD_DEFAULT,
-  MU_URL_PATH,
-  0, /* Nothing is required in the URL, except scheme. Missing path means
-	using PATH_SENDMAIL. */
+  MU_URL_SCHEME | MU_URL_PATH,
+  MU_URL_SCHEME, /* Nothing is required in the URL, except scheme. 
+                    Missing path means using PATH_SENDMAIL. */
   _url_sendmail_init,    /* url init.  */
   _mu_mailer_mailbox_init,     /* Mailbox entry.  */
   _mu_mailer_sendmail_init, /* Mailer entry.  */
diff --git a/libproto/mailer/smtp.c b/libproto/mailer/smtp.c
index 5b48fce..7196288 100644
--- a/libproto/mailer/smtp.c
+++ b/libproto/mailer/smtp.c
@@ -65,7 +65,7 @@ static struct _mu_record _smtp_record = {
   MU_SMTP_PRIO,
   MU_SMTP_SCHEME,
   MU_RECORD_DEFAULT,
-  MU_URL_CRED | MU_URL_INET | MU_URL_PARAM,
+  MU_URL_SCHEME | MU_URL_CRED | MU_URL_INET | MU_URL_PARAM,
   MU_URL_HOST,
   _url_smtp_init,		/* url init.  */
   _mu_mailer_mailbox_init,	/* Mailbox init.  */
@@ -113,7 +113,7 @@ static int
 smtp_open (mu_mailer_t mailer, int flags)
 {
   const char *host, *auth;
-  long port;
+  unsigned port;
   struct _smtp_mailer *smtp_mailer = mailer->data;
   int rc;
   size_t parmc = 0;
diff --git a/libproto/mailer/smtp_auth.c b/libproto/mailer/smtp_auth.c
index 48b5a91..a0d8a87 100644
--- a/libproto/mailer/smtp_auth.c
+++ b/libproto/mailer/smtp_auth.c
@@ -77,15 +77,6 @@ _mu_smtp_fixup_params (mu_smtp_t smtp)
       return rc;
     }
 
-  rc = mu_url_parse (url);
-  if (rc)
-    {
-      mu_diag_output (MU_DIAG_ERROR, "cannot parse URL: %s",
-		      mu_strerror (rc));
-      mu_url_destroy (&url);
-      return rc;
-    }
-
   if (!(flags & _HAS_USERNAME))
     {
       rc = mu_url_sget_user (url, &str);
diff --git a/libproto/mbox/folder.c b/libproto/mbox/folder.c
index c6a9d51..95506e3 100644
--- a/libproto/mbox/folder.c
+++ b/libproto/mbox/folder.c
@@ -101,7 +101,7 @@ static struct _mu_record _mbox_record =
   MU_MBOX_PRIO,
   MU_MBOX_SCHEME,
   MU_RECORD_LOCAL,
-  MU_URL_PATH,
+  MU_URL_SCHEME | MU_URL_PATH,
   MU_URL_PATH,
   mu_url_expand_path, /* URL init.  */
   _mailbox_mbox_init, /* Mailbox init.  */
diff --git a/libproto/mh/folder.c b/libproto/mh/folder.c
index f5feb1f..90291d0 100644
--- a/libproto/mh/folder.c
+++ b/libproto/mh/folder.c
@@ -139,7 +139,7 @@ static struct _mu_record _mh_record =
   MU_MH_PRIO,
   MU_MH_SCHEME,
   MU_RECORD_LOCAL,
-  MU_URL_PATH,
+  MU_URL_SCHEME | MU_URL_PATH,
   MU_URL_PATH,
   mu_url_expand_path, /* Url init.  */
   _mailbox_mh_init, /* Mailbox init.  */
diff --git a/libproto/nntp/folder.c b/libproto/nntp/folder.c
index 071ffae..cac6fb3 100644
--- a/libproto/nntp/folder.c
+++ b/libproto/nntp/folder.c
@@ -46,7 +46,7 @@ static struct _mu_record _nntp_record =
   MU_NNTP_PRIO,
   MU_NNTP_URL_SCHEME,
   MU_RECORD_DEFAULT,
-  MU_URL_CRED | MU_URL_INET | MU_URL_PATH,
+  MU_URL_SCHEME | MU_URL_CRED | MU_URL_INET | MU_URL_PATH,
   MU_URL_HOST,
   _nntp_url_init, /* Url init.  */
   _nntp_mailbox_init, /* Mailbox init.  */
@@ -104,7 +104,7 @@ nntp_folder_open (mu_folder_t folder, int flags)
   f_nntp_t f_nntp = folder->data;
   mu_stream_t carrier = NULL;
   const char *host;
-  long port = MU_NNTP_DEFAULT_PORT; /* default nntp port.  */
+  unsigned port = MU_NNTP_DEFAULT_PORT; /* default nntp port.  */
   int status = 0;
 
   /* If we are already open for business, noop.  */
diff --git a/libproto/pop/folder.c b/libproto/pop/folder.c
index 59ad1cc..4a26dbe 100644
--- a/libproto/pop/folder.c
+++ b/libproto/pop/folder.c
@@ -48,7 +48,7 @@ static struct _mu_record _pop_record =
   MU_POP_PRIO,
   MU_POP_SCHEME,
   MU_RECORD_DEFAULT,
-  MU_URL_CRED | MU_URL_INET | MU_URL_PARAM,
+  MU_URL_SCHEME | MU_URL_CRED | MU_URL_INET | MU_URL_PARAM,
   MU_URL_HOST,
   _url_pop_init, /* Url init.  */
   _mailbox_pop_init, /* Mailbox init.  */
@@ -69,7 +69,7 @@ static struct _mu_record _pops_record =
   MU_POP_PRIO,
   MU_POPS_SCHEME,
   MU_RECORD_DEFAULT,
-  MU_URL_CRED | MU_URL_INET,
+  MU_URL_SCHEME | MU_URL_CRED | MU_URL_INET,
   MU_URL_HOST,
   _url_pops_init, /* Url init.  */
   _mailbox_pops_init, /* Mailbox init.  */
diff --git a/libproto/pop/mbox.c b/libproto/pop/mbox.c
index 6865f6f..9b658bc 100644
--- a/libproto/pop/mbox.c
+++ b/libproto/pop/mbox.c
@@ -105,7 +105,7 @@ pop_open (mu_mailbox_t mbox, int flags)
   struct _pop3_mailbox *mpd = mbox->data;
   int status;
   const char *host;
-  long port = mpd->pops ? MU_POPS_PORT : MU_POP_PORT;
+  unsigned port = mpd->pops ? MU_POPS_PORT : MU_POP_PORT;
   mu_stream_t stream;
   
   /* Sanity checks. */
diff --git a/maidag/deliver.c b/maidag/deliver.c
index 5a65259..25a7ab6 100644
--- a/maidag/deliver.c
+++ b/maidag/deliver.c
@@ -385,13 +385,6 @@ do_delivery (mu_url_t url, mu_message_t msg, const char *name, char **errp)
 			auth->mailbox, mu_strerror (status));
 	  return exit_code = EX_UNAVAILABLE;
 	}
-      status = mu_url_parse (url);
-      if (status)
-	{
-	  maidag_error (_("error parsing URL %s: %s"),
-			auth->mailbox, mu_strerror (status));
-	  return exit_code = EX_UNAVAILABLE;
-	}
     }      
 
   status = mu_mailbox_create_from_url (&mbox, url);
@@ -437,14 +430,6 @@ deliver_to_url (mu_message_t msg, char *dest_id, char **errp)
 		    mu_strerror (status));
       return EX_NOUSER;
     }
-  status = mu_url_parse (url);
-  if (status)
-    {
-      maidag_error (_("%s: cannot parse url: %s"), dest_id,
-		    mu_strerror (status));
-      mu_url_destroy (&url);
-      return EX_NOUSER;
-    }
   status = mu_url_sget_user (url, &name);
   if (status == MU_ERR_NOENT)
     name = NULL;
diff --git a/mu/wicket.c b/mu/wicket.c
index 2615cdf..2ea8eec 100644
--- a/mu/wicket.c
+++ b/mu/wicket.c
@@ -75,6 +75,10 @@ wicket_match (mu_stream_t stream, const char *str)
   int rc, ret;
   mu_url_t u, url;
   struct mu_debug_locus loc;
+  int flags = MU_URL_PARSE_ALL;
+
+  if (wicket_verbose > 2)
+    flags &= ~MU_URL_PARSE_HIDEPASS;
   
   rc = mu_url_create (&u, str);
   if (rc)
@@ -82,12 +86,6 @@ wicket_match (mu_stream_t stream, const char *str)
       mu_diag_funcall (MU_DIAG_ERROR, "mu_url_create", str, rc);
       return 2;
     }
-  rc = mu_url_parse (u);
-  if (rc)
-    {
-      mu_diag_funcall (MU_DIAG_ERROR, "mu_url_parse", str, rc);
-      return 2;
-    }
 
   rc = mu_stream_seek (stream, 0, MU_SEEK_SET, NULL);
   if (rc)
@@ -97,7 +95,7 @@ wicket_match (mu_stream_t stream, const char *str)
     }
   loc.file = wicket_file;
   loc.line = 0;
-  rc = mu_wicket_stream_match_url (stream, &loc, u, &url);
+  rc = mu_wicket_stream_match_url (stream, &loc, u, flags, &url);
   switch (rc)
     {
     case 0:
@@ -106,27 +104,7 @@ wicket_match (mu_stream_t stream, const char *str)
 	{
 	  printf ("%s: %s:%d", str, loc.file, loc.line);
 	  if (wicket_verbose > 1)
-	    {
-	      printf (": %s", mu_url_to_string (url));
-	      if (wicket_verbose > 2)
-		{
-		  mu_secret_t s;
-		  rc = mu_url_get_secret (url, &s);
-		  if (rc == 0)
-		    {
-		      printf (": %s", mu_secret_password (s));
-		      mu_secret_password_unref (s);
-		      mu_secret_unref (s);
-		    }
-		  else if (rc == MU_ERR_NOENT)
-		    printf (": [%s]", _("no password"));
-		  else
-		    {
-		      printf (": [error: %s]", mu_strerror (rc));
-		      ret = 2;
-		    }
-		}
-	    }
+	    printf (": %s", mu_url_to_string (url));
 	  putchar ('\n');
 	}
       break;
diff --git a/python/libmu_py/url.c b/python/libmu_py/url.c
index f1b7eee..0d7875e 100644
--- a/python/libmu_py/url.c
+++ b/python/libmu_py/url.c
@@ -102,6 +102,7 @@ api_url_destroy (PyObject *self, PyObject *args)
   return _ro (Py_None);
 }
 
+/* FIXME: Remove */
 static PyObject *
 api_url_parse (PyObject *self, PyObject *args)
 {
@@ -111,22 +112,21 @@ api_url_parse (PyObject *self, PyObject *args)
   if (!PyArg_ParseTuple (args, "O!", &PyUrlType, &py_url))
     return NULL;
 
-  status = mu_url_parse (py_url->url);
-  return _ro (PyInt_FromLong (status));
+  return _ro (0);
 }
 
 static PyObject *
 api_url_get_port (PyObject *self, PyObject *args)
 {
   int status;
-  long port;
+  unsigned port;
   PyUrl *py_url;
 
   if (!PyArg_ParseTuple (args, "O!", &PyUrlType, &py_url))
     return NULL;
 
   status = mu_url_get_port (py_url->url, &port);
-  return status_object (status, PyInt_FromLong (port));
+  return status_object (status, PyInt_FromLong ((long)port));
 }
 
 static PyObject *


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Wed Oct 27 12:08:09 2010
Received: from mailman by lists.gnu.org with archive (Exim 4.43)
	id 1PB8Xp-0008WO-HX
	for mharc-commit-mailutils@gnu.org; Wed, 27 Oct 2010 12:08:09 -0400
Received: from [140.186.70.92] (port=60557 helo=eggs.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.43) id 1PB8Xk-0008Rc-0F
	for commit-mailutils@gnu.org; Wed, 27 Oct 2010 12:08:07 -0400
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1PB8Xg-0007yV-Ut
	for commit-mailutils@gnu.org; Wed, 27 Oct 2010 12:08:03 -0400
Received: from colonialone.fsf.org ([140.186.70.51]:49058
	helo=internal.in.savannah.gnu.org)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1PB8Xg-0007yJ-Qj
	for commit-mailutils@gnu.org; Wed, 27 Oct 2010 12:08:00 -0400
Received: from [10.1.0.108] (helo=vcs-noshell.in.savannah.gnu.org)
	by internal.in.savannah.gnu.org with esmtp (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1PB8Xg-0002JP-OI
	for commit-mailutils@gnu.org; Wed, 27 Oct 2010 16:08:00 +0000
Received: from gray by vcs-noshell.in.savannah.gnu.org with local (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1PB8Xg-0007ZF-Fn
	for commit-mailutils@gnu.org; Wed, 27 Oct 2010 16:08:00 +0000
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: 3327a23a49e532c068972a0b2d64021361540f7b
X-Git-Newrev: 697384c64dadb0a4db97136a6cbcf2166c48897b
Message-Id: <E1PB8Xg-0007ZF-Fn@vcs-noshell.in.savannah.gnu.org>
Date: Wed, 27 Oct 2010 16:08:00 +0000
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
Subject: [SCM] GNU Mailutils branch, master,
	updated. release-2.2-163-g697384c
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.5
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=unsubscribe>
List-Archive: <http://lists.gnu.org/archive/html/commit-mailutils>
List-Post: <mailto:commit-mailutils@gnu.org>
List-Help: <mailto:commit-mailutils-request@gnu.org?subject=help>
List-Subscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Wed, 27 Oct 2010 16:08:07 -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=697384c64dadb0a4db97136a6cbcf2166c48897b

The branch, master has been updated
       via  697384c64dadb0a4db97136a6cbcf2166c48897b (commit)
       via  b24bf339e41212dfd1a0d88f76150ff0503b7bb2 (commit)
       via  48639a1c168835dd3d462b918f3720a1a70ef50a (commit)
      from  3327a23a49e532c068972a0b2d64021361540f7b (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 697384c64dadb0a4db97136a6cbcf2166c48897b
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Wed Oct 27 19:03:17 2010 +0300

    Use mu_asprintf and mu_vasnprintf instead of asprintf and vasprintf.
    
    *gnulib.modules (snprintf, vasprintf): Remove.
    *sieve/tests/reject.at: Use AT_PACKAGE_VERSION instead of
    the hardcoded version number.
    *lib/mu_asprintf.h: Remove.
    
    * imap4d/auth_gss.c: Replace asprintf with mu_asprintf and vasprintf by
    mu_vasnprintf.
    * guimb/guimb.h: Likewise.
    * imap4d/imap4d.h: Likewise.
    * imap4d/unsubscribe.c: Likewise.
    * lib/Makefile.am
    * lib/mailcap.c: Likewise.
    * libmu_argp/muinit.c: Likewise.
    * maidag/deliver.c: Likewise.
    * maidag/maidag.h: Likewise.
    * mail/escape.c: Likewise.
    * mail/mail.c: Likewise.
    * mail/mail.h: Likewise.
    * mail/util.c: Likewise.
    * mail/write.c: Likewise.
    * mh/burst.c: Likewise.
    * mh/forw.c: Likewise.
    * mh/mh.h: Likewise.
    * mh/mh_alias.l: Likewise.
    * mh/mh_format.c: Likewise.
    * mh/mh_getopt.c: Likewise.
    * mh/mh_init.c: Likewise.
    * mh/mh_msgset.c: Likewise.
    * mh/mh_sequence.c: Likewise.
    * mh/mh_whatnow.c: Likewise.
    * mh/mhn.c: Likewise.
    * mh/send.c: Likewise.
    * mimeview/mimetypes.l: Likewise.
    * mimeview/mimetypes.y: Likewise.
    * mimeview/mimeview.c: Likewise.
    * movemail/movemail.c: Likewise.
    * mu/ldflags.c: Likewise.
    * pop3d/expire.c: Likewise.
    * pop3d/pop3d.h: Likewise.
    * sieve/sieve.c: Likewise.

commit b24bf339e41212dfd1a0d88f76150ff0503b7bb2
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Wed Oct 27 16:31:35 2010 +0300

    Remove tcp wrapper configuration keywords that did not actually change anything.
    
    The allow_severity and deny_severity variables are valid only for tcpd and
    related applications, hosts_access does not use them.
    
    * lib/tcpwrap.c (mu_tcp_wrapper_cb_hosts_allow_syslog)
    (mu_tcp_wrapper_cb_hosts_deny_syslog): Remove functions.
    (allow_severity, deny_severity): Remove variables.
    (tcpwrapper_param): Remove allow-syslog-priority and
    deny-syslog-priority.

commit 48639a1c168835dd3d462b918f3720a1a70ef50a
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Wed Oct 27 16:28:55 2010 +0300

    Version 2.9.90
    
    * NEWS, configure: Set version number to 2.9.90 to avoid confusion
    with 2.2.x branch.

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

Summary of changes:
 NEWS                  |    4 ++--
 configure.ac          |    2 +-
 gnulib.modules        |    2 --
 guimb/guimb.h         |    2 +-
 imap4d/auth_gss.c     |    4 ++--
 imap4d/imap4d.h       |    2 +-
 imap4d/unsubscribe.c  |    2 +-
 lib/Makefile.am       |    1 -
 lib/mailcap.c         |    2 +-
 lib/mu_asprintf.h     |   38 --------------------------------------
 lib/tcpwrap.c         |   36 ------------------------------------
 libmu_argp/muinit.c   |   20 ++++++++++----------
 maidag/deliver.c      |   18 +++++++++---------
 maidag/maidag.h       |    2 +-
 mail/escape.c         |    2 +-
 mail/mail.c           |    4 ++--
 mail/mail.h           |    2 +-
 mail/util.c           |   22 ++++++++++++----------
 mail/write.c          |    2 +-
 mh/burst.c            |    2 +-
 mh/forw.c             |    4 ++--
 mh/mh.h               |    4 ++--
 mh/mh_alias.l         |   12 +++++++-----
 mh/mh_format.c        |    4 ++--
 mh/mh_getopt.c        |   10 +++++-----
 mh/mh_init.c          |    7 ++++---
 mh/mh_msgset.c        |    4 ++--
 mh/mh_sequence.c      |    2 +-
 mh/mh_whatnow.c       |    4 ++--
 mh/mhn.c              |   12 ++++++------
 mh/send.c             |    4 ++--
 mimeview/mimetypes.l  |    2 +-
 mimeview/mimetypes.y  |   12 ++++++------
 mimeview/mimeview.c   |    2 +-
 movemail/movemail.c   |    5 ++---
 mu/ldflags.c          |    4 ++--
 pop3d/expire.c        |    2 +-
 pop3d/pop3d.h         |    2 +-
 sieve/sieve.c         |    2 +-
 sieve/tests/reject.at |    6 +++---
 40 files changed, 100 insertions(+), 173 deletions(-)
 delete mode 100644 lib/mu_asprintf.h

diff --git a/NEWS b/NEWS
index 8b255a4..db37c55 100644
--- a/NEWS
+++ b/NEWS
@@ -1,4 +1,4 @@
-GNU mailutils NEWS -- history of user-visible changes. 2010-09-09
+GNU mailutils NEWS -- history of user-visible changes. 2010-10-27
 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free
 Software Foundation, Inc.
 See the end of file for copying conditions.
@@ -6,7 +6,7 @@ See the end of file for copying conditions.
 Please send mailutils bug reports to <bug-mailutils@gnu.org>.
 
 
-Version 2.2.90 (Git)
+Version 2.9.90 (Git)
 
 This version is highly experimental.  It features a rewrite from
 scratch of the stream subsystem and other vital parts of the
diff --git a/configure.ac b/configure.ac
index 690171b..e20652d 100644
--- a/configure.ac
+++ b/configure.ac
@@ -17,7 +17,7 @@ dnl You should have received a copy of the GNU General Public License along
 dnl with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
 AC_PREREQ(2.63)
-AC_INIT([GNU Mailutils], [2.2.90], [bug-mailutils@gnu.org], [mailutils])
+AC_INIT([GNU Mailutils], [2.9.90], [bug-mailutils@gnu.org], [mailutils])
 AC_CONFIG_SRCDIR([libmailutils/mailbox/mailbox.c])
 AC_CONFIG_AUX_DIR([build-aux])
 AM_INIT_AUTOMAKE([gnits 1.11 dist-bzip2 dist-xz std-options silent-rules])
diff --git a/gnulib.modules b/gnulib.modules
index 6639959..cedbcb7 100644
--- a/gnulib.modules
+++ b/gnulib.modules
@@ -20,8 +20,6 @@ mbswidth
 obstack
 realloc
 setenv
-snprintf
 stdint
 strtok_r
-vasprintf
 xalloc
diff --git a/guimb/guimb.h b/guimb/guimb.h
index 621a3ec..e0384e8 100644
--- a/guimb/guimb.h
+++ b/guimb/guimb.h
@@ -42,7 +42,7 @@
 #include <mailutils/util.h>
 #include <mailutils/attribute.h>
 #include <mailutils/envelope.h>
-#include <mu_asprintf.h>
+#include <mailutils/io.h>
 
 extern char *program_file;
 extern char *program_expr;
diff --git a/imap4d/auth_gss.c b/imap4d/auth_gss.c
index 11f0745..68f3bf4 100644
--- a/imap4d/auth_gss.c
+++ b/imap4d/auth_gss.c
@@ -62,7 +62,7 @@ display_status_1 (char *m, OM_uint32 code, int type)
 				     type, GSS_C_NO_OID, &msg_ctx, &msg);
       if (GSS_ERROR (maj_stat))
 	{
-	  asprintf ((char**)&msg.value, "code %d", code);
+	  mu_asprintf ((char**)&msg.value, "code %d", code);
 	  msg.length = strlen (msg.value);
 	}
 
@@ -136,7 +136,7 @@ auth_gssapi (struct imap4d_auth *ap)
      work (possibly due to a bug in krb5_gss_accept_sec_context()), so
      we acquire server credentials explicitly. */
 
-  asprintf ((char **) &tmp, "imap@%s", util_localname ());
+  mu_asprintf ((char **) &tmp, "imap@%s", util_localname ());
   tokbuf.value = tmp;
   tokbuf.length = strlen (tokbuf.value) + 1;
   maj_stat = gss_import_name (&min_stat, &tokbuf,
diff --git a/imap4d/imap4d.h b/imap4d/imap4d.h
index 6cf6f58..21d3b72 100644
--- a/imap4d/imap4d.h
+++ b/imap4d/imap4d.h
@@ -103,8 +103,8 @@
 #include <mailutils/vartab.h>
 #include <mailutils/cctype.h>
 #include <mailutils/cstr.h>
+#include <mailutils/io.h>
 
-#include <mu_asprintf.h>
 #include <mu_umaxtostr.h>
 #include <muaux.h>
 
diff --git a/imap4d/unsubscribe.c b/imap4d/unsubscribe.c
index 108f7f3..9379da5 100644
--- a/imap4d/unsubscribe.c
+++ b/imap4d/unsubscribe.c
@@ -115,7 +115,7 @@ imap4d_unsubscribe (struct imap4d_command *command, imap4d_tokbuf_t tok)
       if (sd.result)
 	{
 	  char *tmpname = NULL;
-	  asprintf (&tmpname, "%s.%d", file, getpid ());
+	  mu_asprintf (&tmpname, "%s.%d", file, getpid ());
 	  sd.tmp = fopen (tmpname, "a");
 	  if (!sd.tmp)
 	    rc = -1;
diff --git a/lib/Makefile.am b/lib/Makefile.am
index ed88cd0..504e463 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -35,7 +35,6 @@ noinst_HEADERS +=\
  mailcap.h\
  muaux.h\
  mu_dbm.h\
- mu_asprintf.h\
  tcpwrap.h
 
 EXTRA_DIST += utmp.c
diff --git a/lib/mailcap.c b/lib/mailcap.c
index e4f0b57..fc2c3ec 100644
--- a/lib/mailcap.c
+++ b/lib/mailcap.c
@@ -658,7 +658,7 @@ display_stream_mailcap (const char *ident, mu_stream_t stream, mu_header_t hdr,
   if (!mailcap_path)
     {
       char *home = mu_get_homedir ();
-      asprintf (&mailcap_path, "%s/.mailcap:%s", home, DEFAULT_MAILCAP);
+      mu_asprintf (&mailcap_path, "%s/.mailcap:%s", home, DEFAULT_MAILCAP);
       free (home);
       if (!mailcap_path)
         return 1;
diff --git a/lib/mu_asprintf.h b/lib/mu_asprintf.h
deleted file mode 100644
index 0f893e9..0000000
--- a/lib/mu_asprintf.h
+++ /dev/null
@@ -1,38 +0,0 @@
-/* Copyright (C) 2007, 2010 Free Software Foundation, Inc.
-
-   GNU Mailutils is free software; you can redistribute it and/or modify
-   it under the terms of the GNU Library General Public License as published by
-   the Free Software Foundation; either version 3 of the License, or
-   (at your option) any later version.
-
-   GNU Mailutils 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 Library General Public License for more details.
-
-   You should have received a copy of the GNU Library General
-   Public License along with GNU Mailutils.  If not, see 
-   <http://www.gnu.org/licenses/>. */
-
-#ifndef MUASPRINTF_H
-#define MUASPRINTF_H
-
-#ifndef _GNU_SOURCE
-# define _GNU_SOURCE
-#endif
-#include <stdarg.h>
-#include <stdio.h>
-
-#if !HAVE_DECL_VASPRINTF
-extern int vasprintf (char **result, const char *format, va_list args);
-#endif
-#if !HAVE_DECL_ASPRINTF
-#if __STDC__
-extern int asprintf (char **result, const char *format, ...);
-#else
-extern int asprintf ();
-#endif
-#endif
-
-#endif
-
diff --git a/lib/tcpwrap.c b/lib/tcpwrap.c
index 001fc9c..3f29b1e 100644
--- a/lib/tcpwrap.c
+++ b/lib/tcpwrap.c
@@ -33,34 +33,6 @@ char *mu_tcp_wrapper_daemon;
 
 #ifdef WITH_LIBWRAP
 # include <tcpd.h>
-int deny_severity = LOG_INFO;
-int allow_severity = LOG_INFO;
-
-int
-mu_tcp_wrapper_cb_hosts_allow_syslog (mu_debug_t debug, void *data,
-				      mu_config_value_t *val)
-{
-  if (mu_cfg_assert_value_type (val, MU_CFG_STRING, debug))
-    return 1;
-  if (mu_string_to_syslog_priority (val->v.string, &allow_severity))
-    mu_cfg_format_error (debug, MU_DEBUG_ERROR, 
-			 _("unknown syslog priority `%s'"), 
-			 val->v.string);
-  return 0;
-}
-
-int
-mu_tcp_wrapper_cb_hosts_deny_syslog (mu_debug_t debug, void *data,
-				     mu_config_value_t *val)
-{
-  if (mu_cfg_assert_value_type (val, MU_CFG_STRING, debug))
-    return 1;
-  if (mu_string_to_syslog_priority (val->v.string, &deny_severity))
-    mu_cfg_format_error (debug, MU_DEBUG_ERROR, 
-			 _("unknown syslog priority `%s'"), 
-			 val->v.string);
-  return 0;
-}
 
 int
 mu_tcpwrapper_access (int fd)
@@ -94,14 +66,6 @@ struct mu_cfg_param tcpwrapper_param[] = {
     N_("Use file for negative client address access control "		      
        "(default: /etc/hosts.deny)."),					      
     N_("file") },							      
-  { "allow-syslog-priority", mu_cfg_callback, NULL, 0,	       	      
-    mu_tcp_wrapper_cb_hosts_allow_syslog,				      
-    N_("Log host allows at this syslog priority."),
-    N_("level") },							      
-  { "deny-syslog-priority", mu_cfg_callback, NULL, 0,			      
-    mu_tcp_wrapper_cb_hosts_deny_syslog,				      
-    N_("Log host denies at this syslog priority."),
-    N_("level") },
   { NULL }
 };
 
diff --git a/libmu_argp/muinit.c b/libmu_argp/muinit.c
index 413389b..ab6e9c9 100644
--- a/libmu_argp/muinit.c
+++ b/libmu_argp/muinit.c
@@ -132,20 +132,20 @@ mu_app_init (struct argp *myargp, const char **capa,
       char *canonical_name = get_canonical_name ();
       mu_stream_t stream;
       mu_stdio_stream_create (&stream, MU_STDOUT_FD, 0);
-      asprintf (&comment,
-		"Configuration file structure for %s utility.",
-		mu_program_name);
+      mu_asprintf (&comment,
+		   "Configuration file structure for %s utility.",
+		   mu_program_name);
       mu_cfg_format_docstring (stream, comment, 0);
       free (comment);
-      asprintf (&comment,
-		"For use in global configuration file (%s), enclose it "
-		"in `program %s { ... };",
-		MU_CONFIG_FILE,
-		mu_program_name);		   
+      mu_asprintf (&comment,
+		   "For use in global configuration file (%s), enclose it "
+		   "in `program %s { ... };",
+		   MU_CONFIG_FILE,
+		   mu_program_name);		   
       mu_cfg_format_docstring (stream, comment, 0);
       free (comment);
-      asprintf (&comment, "For more information, use `info %s'.",
-		canonical_name);
+      mu_asprintf (&comment, "For more information, use `info %s'.",
+		   canonical_name);
       mu_cfg_format_docstring (stream, comment, 0);
       free (comment);
       
diff --git a/maidag/deliver.c b/maidag/deliver.c
index 25a7ab6..47d9a54 100644
--- a/maidag/deliver.c
+++ b/maidag/deliver.c
@@ -140,8 +140,8 @@ notify_action (mu_observer_t obs, size_t type, void *data, void *action_data)
       char *buf;
       
       mu_mailbox_get_url (mbox, &url);
-      asprintf (&buf, "%s@%s:%s", biff_user_name,
-		qid, mu_url_to_string (url));
+      mu_asprintf (&buf, "%s@%s:%s", biff_user_name,
+		   qid, mu_url_to_string (url));
       if (buf)
 	{
 	  sendto (biff_fd, buf, strlen (buf), 0,
@@ -251,8 +251,8 @@ deliver_to_mailbox (mu_mailbox_t mbox, mu_message_t msg,
 	  maidag_error (_("%s: mailbox quota exceeded for this recipient"),
 			auth->name);
 	  if (errp)
-	    asprintf (errp, "%s: mailbox quota exceeded for this recipient",
-		      auth->name);
+	    mu_asprintf (errp, "%s: mailbox quota exceeded for this recipient",
+		         auth->name);
 	  exit_code = EX_QUOTA();
 	  failed++;
 	  break;
@@ -274,10 +274,10 @@ deliver_to_mailbox (mu_mailbox_t mbox, mu_message_t msg,
 			      "this recipient"),
 			    auth->name);
 	      if (errp)
-		asprintf (errp,
-			  "%s: message would exceed maximum mailbox size "
-			  "for this recipient",
-			  auth->name);
+		mu_asprintf (errp,
+			     "%s: message would exceed maximum mailbox size "
+			     "for this recipient",
+			     auth->name);
 	      exit_code = EX_QUOTA();
 	      failed++;
 	    }
@@ -339,7 +339,7 @@ do_delivery (mu_url_t url, mu_message_t msg, const char *name, char **errp)
 	{
 	  maidag_error (_("%s: no such user"), name);
 	  if (errp)
-	    asprintf (errp, "%s: no such user", name);
+	    mu_asprintf (errp, "%s: no such user", name);
 	  exit_code = EX_NOUSER;
 	  return EX_NOUSER;
 	}
diff --git a/maidag/maidag.h b/maidag/maidag.h
index 6c7224d..b77491f 100644
--- a/maidag/maidag.h
+++ b/maidag/maidag.h
@@ -84,9 +84,9 @@
 #include <mailutils/acl.h>
 #include <mailutils/server.h>
 #include <mailutils/cctype.h>
+#include <mailutils/io.h>
 
 #include <mu_dbm.h>
-#include <mu_asprintf.h>
 
 #if defined (USE_DBM) || defined (USE_SQL)
 # define USE_MAILBOX_QUOTAS 1
diff --git a/mail/escape.c b/mail/escape.c
index f35bbe1..efa2d79 100644
--- a/mail/escape.c
+++ b/mail/escape.c
@@ -93,7 +93,7 @@ parse_headers (FILE *fp, compose_env_t *env)
 	      if (name)
 		{
 		  char *p = NULL;
-		  asprintf (&p, "%s\n%s", value, buf);
+		  mu_asprintf (&p, "%s\n%s", value, buf);
 		  free (value);
 		  value = p;
 		}
diff --git a/mail/mail.c b/mail/mail.c
index 6cd68f3..3f70e66 100644
--- a/mail/mail.c
+++ b/mail/mail.c
@@ -175,14 +175,14 @@ parse_opt (int key, char *arg, struct argp_state *state)
 			    _("-f requires at most one command line argument"));
 	    }
 	  else if (args->user)
-	    asprintf (&args->file, "~/%s/mbox", args->user);
+	    mu_asprintf (&args->file, "~/%s/mbox", args->user);
 	  else
 	    args->file = "~/mbox";
 	}
       else if (args->argc || (args->hint & HINT_SEND_MODE))
 	util_cache_command (&command_list, "setq mode=send");
       else if (args->user)
-	asprintf (&args->file, "%%%s", args->user);
+	mu_asprintf (&args->file, "%%%s", args->user);
       break;
       
     default:
diff --git a/mail/mail.h b/mail/mail.h
index cc2de6e..3b33a52 100644
--- a/mail/mail.h
+++ b/mail/mail.h
@@ -79,7 +79,7 @@
 #include <mailutils/argcv.h>
 #include <mailutils/cctype.h>
 #include <mailutils/cstr.h>
-#include <mu_asprintf.h>
+#include <mailutils/io.h>
 
 #ifdef __cplusplus
 extern "C" {
diff --git a/mail/util.c b/mail/util.c
index ae6625f..f337fe7 100644
--- a/mail/util.c
+++ b/mail/util.c
@@ -30,7 +30,7 @@
 #else
 # include <sys/fcntl.h>
 #endif
-#include <mu_asprintf.h>
+#include <mailutils/io.h>
 
 /*
  * expands command into its command and arguments, then runs command
@@ -38,20 +38,21 @@
  * returns exit status of the command
  */
 int
-util_do_command (const char *c, ...)
+util_do_command (const char *fmt, ...)
 {
   int argc = 0;
   char **argv = NULL;
   int status = 0;
   const struct mail_command_entry *entry = NULL;
   char *cmd = NULL;
+  size_t size = 0;
   va_list ap;
   
-  va_start (ap, c);
-  status = vasprintf (&cmd, c, ap);
+  va_start (ap, fmt);
+  status = mu_vasnprintf (&cmd, &size, fmt, ap);
   va_end (ap);
-  if (status < 0)
-    return 0;
+  if (status)
+    return status;
 
   if (cmd)
     {
@@ -84,7 +85,7 @@ util_do_command (const char *c, ...)
 	      && ((strtoul (argv[0], &p, 10) > 0 && *p == 0)
 		  || (argv[0][1] == 0 && strchr("^$", argv[0][0]))))
 	    {
-	      asprintf (&p, "print %s", argv[0]);
+	      mu_asprintf (&p, "print %s", argv[0]);
 	      mu_argcv_free (argc, argv);
 	      mu_argcv_get (p, NULL, NULL, &argc, &argv);
 	      free (p);
@@ -685,7 +686,7 @@ util_outfolder_name (char *str)
       if (mailvar_get (&outfolder, "outfolder", mailvar_type_string, 0) == 0)
 	{
 	  char *ns = NULL;
-	  asprintf (&ns, "%s/%s", outfolder, str);
+	  mu_asprintf (&ns, "%s/%s", outfolder, str);
 	  str = util_fullpath (ns);
 	  free (ns);
 	}
@@ -1035,11 +1036,12 @@ util_noapp ()
 void
 util_cache_command (mu_list_t *list, const char *fmt, ...)
 {
-  char *cmd;
+  char *cmd = NULL;
+  size_t size = 0;
   va_list ap;
 
   va_start (ap, fmt);
-  vasprintf (&cmd, fmt, ap);
+  mu_vasnprintf (&cmd, &size, fmt, ap);
   va_end (ap);
 
   if (!*list)
diff --git a/mail/write.c b/mail/write.c
index 4840873..1368d11 100644
--- a/mail/write.c
+++ b/mail/write.c
@@ -46,7 +46,7 @@ mail_write (int argc, char **argv)
           util_error (_("No applicable message"));
           return 1;
         }
-      asprintf (&p, "%lu", (unsigned long) n);
+      mu_asprintf (&p, "%lu", (unsigned long) n);
       filename = util_outfolder_name (p);
       free (p);
     }
diff --git a/mh/burst.c b/mh/burst.c
index 1078fda..8dc9163 100644
--- a/mh/burst.c
+++ b/mh/burst.c
@@ -594,7 +594,7 @@ main (int argc, char **argv)
 	last_uid += burst_map[i].count;
       VERBOSE ((_("Estimated last UID: %s"), mu_umaxtostr (0, last_uid)));
 
-      asprintf (&xargv[0], "%s-last", mu_umaxtostr (0, burst_map[0].msgno));
+      mu_asprintf (&xargv[0], "%s-last", mu_umaxtostr (0, burst_map[0].msgno));
       xargv[1] = NULL;
       mh_msgset_parse (mbox, &ms, 1, xargv, NULL);
       free (xargv[0]);
diff --git a/mh/forw.c b/mh/forw.c
index 3889601..06a9de5 100644
--- a/mh/forw.c
+++ b/mh/forw.c
@@ -291,7 +291,7 @@ format_message (mu_mailbox_t mbox, mu_message_t msg, size_t num, void *data)
   
   if (fp->num)
     {
-      asprintf (&s, "\n------- Message %d\n", fp->num++);
+      mu_asprintf (&s, "\n------- Message %d\n", fp->num++);
       rc = mu_stream_write (fp->stream, s, strlen (s), NULL);
       free (s);
     }
@@ -357,7 +357,7 @@ finish_draft ()
       mbox_path = mu_url_to_string (url);
       if (memcmp (mbox_path, "mh:", 3) == 0)
 	mbox_path += 3;
-      asprintf (&str, "#forw [] +%s", mbox_path);
+      mu_asprintf (&str, "#forw [] +%s", mbox_path);
       rc = mu_stream_write (stream, str, strlen (str), NULL);
       free (str);
       for (i = 0; rc == 0 && i < msgset.count; i++)
diff --git a/mh/mh.h b/mh/mh.h
index ec78d01..0ed009c 100644
--- a/mh/mh.h
+++ b/mh/mh.h
@@ -25,7 +25,7 @@
 #include <unistd.h>
 #include <mh_getopt.h>
 #include <xalloc.h>
-#include <mu_asprintf.h>
+#include <mailutils/io.h>
 
 #include <string.h>
 
@@ -53,8 +53,8 @@
 #include <mailutils/mailer.h>
 #include <mailutils/envelope.h>
 #include <mailutils/mime.h>
+#include <mailutils/io.h>
 
-#include <mu_asprintf.h>
 #include <mu_umaxtostr.h>
 
 #if !HAVE_DECL_STRCHRNUL
diff --git a/mh/mh_alias.l b/mh/mh_alias.l
index b002343..e997476 100644
--- a/mh/mh_alias.l
+++ b/mh/mh_alias.l
@@ -26,7 +26,7 @@
 #include <mh_alias.h>
 #include <sys/stat.h>
 #include <mailutils/cctype.h>
-#include <mu_asprintf.h>
+#include <mailutils/io.h>
   
 char *ali_filename;  
 size_t ali_line_num;  
@@ -36,9 +36,10 @@ void
 va_ali_parse_error_loc (const char *name, size_t line,
 			      const char *fmt, va_list ap)
 {
-  char *buf;
+  char *buf = NULL;
+  size_t size = 0;
 
-  vasprintf (&buf, fmt, ap);
+  mu_vasnprintf (&buf, &size, fmt, ap);
   if (name)
     mu_error ("%s:%lu: %s", name, (unsigned long) line,  buf);
   else
@@ -372,8 +373,9 @@ SPEC [,:;+=\*]
 		    yylval.string[yyleng] = 0;
 		    return STRING;}
 .              { char *p;
-                 asprintf (&p,
-			   _("Stray character %03o in alias file"), yytext[0]);
+                 mu_asprintf (&p,
+			      _("Stray character %03o in alias file"), 
+                              yytext[0]);
                  yyerror (p);
 		 free (p); }
 %%
diff --git a/mh/mh_format.c b/mh/mh_format.c
index f084f31..60b8d7b 100644
--- a/mh/mh_format.c
+++ b/mh/mh_format.c
@@ -1136,7 +1136,7 @@ static void
 builtin_putnum (struct mh_machine *mach)
 {
   char *p;
-  asprintf (&p, "%ld", mach->arg_num);
+  mu_asprintf (&p, "%ld", mach->arg_num);
   print_string (mach, 0, p);
   free (p);
 }
@@ -1609,7 +1609,7 @@ builtin_pers (struct mh_machine *mach)
   if (mu_address_sget_personal (addr, 1, &str) == 0 && str)
     {
       char *p;
-      asprintf (&p, "\"%s\"", str);
+      mu_asprintf (&p, "\"%s\"", str);
       strobj_create (&mach->arg_str, p);
       free (p);
     }
diff --git a/mh/mh_getopt.c b/mh/mh_getopt.c
index 09b8a3e..138baa7 100644
--- a/mh/mh_getopt.c
+++ b/mh/mh_getopt.c
@@ -25,7 +25,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <mh_getopt.h>
-#include <mu_asprintf.h>
+#include <mailutils/io.h>
 
 static int mh_optind = 1;
 static char *mh_optarg;
@@ -55,7 +55,7 @@ mh_getopt (int argc, char **argv, struct mh_option *mh_opt, const char *doc)
     }
 
   if (strcmp (mh_optptr, "-version") == 0)
-    asprintf (&argv[mh_optind], "--version");
+    mu_asprintf (&argv[mh_optind], "--version");
   else
     {
       optlen = strlen (mh_optptr+1);
@@ -81,16 +81,16 @@ mh_getopt (int argc, char **argv, struct mh_option *mh_opt, const char *doc)
 		mh_optarg = "no";
 	      else
 		mh_optarg = "yes";
-	      asprintf (&argv[mh_optind], "--%s=%s", longopt, mh_optarg);
+	      mu_asprintf (&argv[mh_optind], "--%s=%s", longopt, mh_optarg);
 	      break;
 	      
 	    case MH_OPT_ARG:
-	      asprintf (&argv[mh_optind], "--%s", longopt);
+	      mu_asprintf (&argv[mh_optind], "--%s", longopt);
 	      mh_optarg = argv[++mh_optind];
 	      break;
 	      
 	    default:
-	      asprintf (&argv[mh_optind], "--%s", longopt);
+	      mu_asprintf (&argv[mh_optind], "--%s", longopt);
 	      mh_optarg = NULL;
 	    }
 	  mh_optind++;
diff --git a/mh/mh_init.c b/mh/mh_init.c
index 9153476..1f64cc3 100644
--- a/mh/mh_init.c
+++ b/mh/mh_init.c
@@ -496,12 +496,13 @@ mh_expand_name (const char *base, const char *name, int is_folder)
       if (memcmp (namep, "mh:/", 4) == 0)
 	return namep;
       else if (namep[0] == '/')
-	asprintf (&p, "mh:%s", namep);
+	mu_asprintf (&p, "mh:%s", namep);
       else
-	asprintf (&p, "mh:%s/%s", base ? base : mu_folder_directory (), namep);
+	mu_asprintf (&p, "mh:%s/%s", base ? base : mu_folder_directory (), 
+                     namep);
     }
   else if (namep[0] != '/')
-    asprintf (&p, "%s/%s", base ? base : mu_folder_directory (), namep);
+    mu_asprintf (&p, "%s/%s", base ? base : mu_folder_directory (), namep);
   else
     return namep;
   
diff --git a/mh/mh_msgset.c b/mh/mh_msgset.c
index f4706fd..fdf5df2 100644
--- a/mh/mh_msgset.c
+++ b/mh/mh_msgset.c
@@ -201,7 +201,7 @@ msgset_preproc (mu_mailbox_t mbox, char *arg)
     {
       char *rest = msgset_preproc_part (mbox, tail+1, &tail);
       char *p = NULL;
-      asprintf (&p, "%s-%s", buf, rest);
+      mu_asprintf (&p, "%s-%s", buf, rest);
       free (rest);
       free (buf);
       buf = p;
@@ -210,7 +210,7 @@ msgset_preproc (mu_mailbox_t mbox, char *arg)
   if (tail[0])
     {
       char *p = NULL;
-      asprintf (&p, "%s%s", buf, tail);
+      mu_asprintf (&p, "%s%s", buf, tail);
       free (buf);
       buf = p;
     }
diff --git a/mh/mh_sequence.c b/mh/mh_sequence.c
index 4e7f5cf..737461a 100644
--- a/mh/mh_sequence.c
+++ b/mh/mh_sequence.c
@@ -22,7 +22,7 @@ private_sequence_name (const char *name)
 {
   char *p;
   char *mbox_dir = mh_expand_name (NULL, mh_current_folder (), 0);
-  asprintf (&p, "atr-%s-%s", name, mbox_dir);
+  mu_asprintf (&p, "atr-%s-%s", name, mbox_dir);
   free (mbox_dir);
   return p;
 }
diff --git a/mh/mh_whatnow.c b/mh/mh_whatnow.c
index 527e73a..6724810 100644
--- a/mh/mh_whatnow.c
+++ b/mh/mh_whatnow.c
@@ -375,7 +375,7 @@ edit (struct mh_whatnow_env *wh, int argc, char **argv, int *status)
 {
   char *name;
   
-  asprintf (&name, "%s-next", wh->editor);
+  mu_asprintf (&name, "%s-next", wh->editor);
   invoke (name, wh->editor, argc, argv, wh->file, NULL);
   free (name);
   
@@ -631,7 +631,7 @@ mh_usedraft (const char *filename)
   
   memset (&wh, 0, sizeof (wh));
   wh.file = xstrdup (filename);
-  asprintf (&wh.prompt, _("Use \"%s\"?"), filename);
+  mu_asprintf (&wh.prompt, _("Use \"%s\"?"), filename);
   rc = _whatnow (&wh, usedraft_tab);
   free (wh.prompt);
   free (wh.file);
diff --git a/mh/mhn.c b/mh/mhn.c
index 2f95838..5aef07f 100644
--- a/mh/mhn.c
+++ b/mh/mhn.c
@@ -583,7 +583,7 @@ _mhn_profile_get (char *prefix, char *type, char *subtype, char *defval)
   
   if (subtype)
     {
-      asprintf (&name, "mhn-%s-%s/%s", prefix, type, subtype);
+      mu_asprintf (&name, "mhn-%s-%s/%s", prefix, type, subtype);
       str = mh_global_profile_get (name, NULL);
       free (name);
       if (!str)
@@ -591,7 +591,7 @@ _mhn_profile_get (char *prefix, char *type, char *subtype, char *defval)
     }
   else
     {
-      asprintf (&name, "mhn-%s-%s", prefix, type);
+      mu_asprintf (&name, "mhn-%s-%s", prefix, type);
       str = mh_global_profile_get (name, defval);
       free (name);
     }
@@ -1596,7 +1596,7 @@ store_handler (mu_message_t msg, msg_part_t part, char *type, char *encoding,
       char *p;
       int rc;
 	
-      asprintf (&p, _("File %s already exists. Rewrite"), name);
+      mu_asprintf (&p, _("File %s already exists. Rewrite"), name);
       rc = mh_getyn (p);
       free (p);
       if (!rc)
@@ -2131,7 +2131,7 @@ edit_forw (char *cmd, struct compose_env *env, mu_message_t *pmsg, int level)
   sp = strchr (val, ';');
   if (!sp)
     abort ();
-  asprintf (&newval, "multipart/digest%s", sp);
+  mu_asprintf (&newval, "multipart/digest%s", sp);
   mu_header_set_value (hdr, MU_HEADER_CONTENT_TYPE, newval, 1);
   free (val);
   free (newval);
@@ -2576,13 +2576,13 @@ mhn_compose ()
     {
       *p = 0;
       name = mu_tempname (input_file);
-      asprintf (&backup, "%s/,%s.orig", input_file, p + 1);
+      mu_asprintf (&backup, "%s/,%s.orig", input_file, p + 1);
       *p = '/';
     }
   else
     {
       name = mu_tempname (NULL);
-      asprintf (&backup, ",%s.orig", input_file);
+      mu_asprintf (&backup, ",%s.orig", input_file);
     }
   
   /* Create working draft file */
diff --git a/mh/send.c b/mh/send.c
index a642d80..ef39d84 100644
--- a/mh/send.c
+++ b/mh/send.c
@@ -647,7 +647,7 @@ _action_send (void *item, void *data)
 	  const char *pers = get_sender_personal ();
 	  if (pers)
 	    {
-	      asprintf (&from, "\"%s\" <%s>", pers, email);
+	      mu_asprintf (&from, "\"%s\" <%s>", pers, email);
 	      free (email);
 	    }
 	  else
@@ -733,7 +733,7 @@ do_send (int argc, char **argv)
 
   /* Prepend url specifier to the folder dir. We won't need this
      when the default format becomes configurable */
-  asprintf (&p, "mh:%s", mu_folder_directory ());
+  mu_asprintf (&p, "mh:%s", mu_folder_directory ());
   mu_set_folder_directory (p);
   free (p);
   
diff --git a/mimeview/mimetypes.l b/mimeview/mimetypes.l
index 222f9bf..0d4c126 100644
--- a/mimeview/mimetypes.l
+++ b/mimeview/mimetypes.l
@@ -26,7 +26,7 @@
 #include <sys/stat.h>
 #include <mimeview.h>
 #include <mimetypes-decl.h>  
-#include <mu_asprintf.h>
+#include <mailutils/io.h>
 
 static int line_num;
 static char *file_name;
diff --git a/mimeview/mimetypes.y b/mimeview/mimetypes.y
index 50be133..44593c3 100644
--- a/mimeview/mimetypes.y
+++ b/mimeview/mimetypes.y
@@ -533,7 +533,7 @@ make_functional_node (char *ident, mu_list_t list)
       if (!p->name)
 	{
 	  char *s;
-	  asprintf (&s, _("%s: unknown function"), ident);
+	  mu_asprintf (&s, _("%s: unknown function"), ident);
 	  yyerror (s);
 	  free (s);
 	  return NULL;
@@ -549,7 +549,7 @@ make_functional_node (char *ident, mu_list_t list)
   if (count < i)
     {
       char *s;
-      asprintf (&s, _("too few arguments in call to `%s'"), ident);
+      mu_asprintf (&s, _("too few arguments in call to `%s'"), ident);
       yyerror (s);
       free (s);
       return NULL;
@@ -557,7 +557,7 @@ make_functional_node (char *ident, mu_list_t list)
   else if (count > i)
     {
       char *s;
-      asprintf (&s, _("too many arguments in call to `%s'"), ident);
+      mu_asprintf (&s, _("too many arguments in call to `%s'"), ident);
       yyerror (s);
       free (s);
       return NULL;
@@ -604,9 +604,9 @@ make_functional_node (char *ident, mu_list_t list)
  err:
   {
     char *s;
-    asprintf (&s,
-	      _("argument %lu has wrong type in call to `%s'"),
-	      (unsigned long) i, ident);
+    mu_asprintf (&s,
+	         _("argument %lu has wrong type in call to `%s'"),
+	         (unsigned long) i, ident);
     yyerror (s);
     free (s);
     return NULL;
diff --git a/mimeview/mimeview.c b/mimeview/mimeview.c
index 0601255..6523853 100644
--- a/mimeview/mimeview.c
+++ b/mimeview/mimeview.c
@@ -249,7 +249,7 @@ display_file (const char *type)
       mu_header_t hdr;
       char *text;
 
-      asprintf (&text, "Content-Type: %s\n", type);
+      mu_asprintf (&text, "Content-Type: %s\n", type);
       status = mu_header_create (&hdr, text, strlen (text));
       if (status)
 	mu_error (_("cannot create header: %s"), mu_strerror (status));
diff --git a/movemail/movemail.c b/movemail/movemail.c
index 914bd77..d030828 100644
--- a/movemail/movemail.c
+++ b/movemail/movemail.c
@@ -27,7 +27,6 @@
 #include <unistd.h>
 #include <mailutils/mailutils.h>
 #include <mailutils/tls.h>
-#include <mu_asprintf.h>
 #include "mailutils/libargp.h"
 #include <muaux.h>
 
@@ -484,7 +483,7 @@ compatibility_mode (mu_mailbox_t *mbx, char *source_name, char *password,
       mu_error (_("hostname of the POP3 server is unknown"));
       exit (1);
     }
-  asprintf (&tmp, "pop://%s@%s", user_name, host);
+  mu_asprintf (&tmp, "pop://%s@%s", user_name, host);
   open_mailbox (mbx, tmp, flags, password);
   free (tmp);
 }
@@ -711,7 +710,7 @@ set_program_id (const char *source_name, const char *dest_name)
       
   rc = mu_vartab_expand (vtab, program_id_option, &id);
   mu_vartab_destroy (&vtab);
-  /*      asprintf (&id, "%s: %s", mu_program_name, s);
+  /*      mu_asprintf (&id, "%s: %s", mu_program_name, s);
 	  free (s);*/
   /* FIXME: Don't use mu_set_program_name here, because it
      plays wise with its argument. We need a mu_set_diag_prefix
diff --git a/mu/ldflags.c b/mu/ldflags.c
index ba00bf8..59ac5eb 100644
--- a/mu/ldflags.c
+++ b/mu/ldflags.c
@@ -150,7 +150,7 @@ mutool_ldflags (int argc, char **argv)
 	    {
 	      if (p->flags & NOTALL)
 		continue;
-	      asprintf (&ptr, "-l%s", p->libname);
+	      mu_asprintf (&ptr, "-l%s", p->libname);
 	      add_entry (0, ptr);
 	      if (p->flags & NEEDAUTH)
 		add_entry (2, "-lmu_auth " AUTHLIBS);
@@ -166,7 +166,7 @@ mutool_ldflags (int argc, char **argv)
 
 	  if (p->name)
 	    {
-	      asprintf (&ptr, "-l%s", p->libname);
+	      mu_asprintf (&ptr, "-l%s", p->libname);
 	      add_entry (0, ptr);
 	      if (p->flags & NEEDAUTH)
 		add_entry (2, "-lmu_auth " AUTHLIBS);
diff --git a/pop3d/expire.c b/pop3d/expire.c
index a43ee4e..024360e 100644
--- a/pop3d/expire.c
+++ b/pop3d/expire.c
@@ -91,7 +91,7 @@ expire_mark_message (mu_message_t msg, char **value)
       mu_attribute_t attr = NULL;
       
       if (!*value)
-	asprintf (value, "%lu", (unsigned long) time (NULL));
+	mu_asprintf (value, "%lu", (unsigned long) time (NULL));
       
       mu_message_get_header (msg, &header);
       mu_message_get_attribute (msg, &attr);
diff --git a/pop3d/pop3d.h b/pop3d/pop3d.h
index 39ac438..4981b96 100644
--- a/pop3d/pop3d.h
+++ b/pop3d/pop3d.h
@@ -23,8 +23,8 @@
 #endif
 #include <mailutils/types.h>
 #include <mailutils/stream.h>
+#include <mailutils/io.h>
 #include <mu_dbm.h>
-#include <mu_asprintf.h>
 #include <mu_umaxtostr.h>
 #include <muaux.h>
 
diff --git a/sieve/sieve.c b/sieve/sieve.c
index 93e467a..e021a33 100644
--- a/sieve/sieve.c
+++ b/sieve/sieve.c
@@ -30,7 +30,7 @@
 #include <syslog.h>
 #include <sysexits.h>
 
-#include <mu_asprintf.h>
+#include <mailutils/io.h>
 #include <mailutils/argcv.h>
 #include <mailutils/sieve.h>
 #include <mailutils/auth.h>
diff --git a/sieve/tests/reject.at b/sieve/tests/reject.at
index 91a6cfb..56d5537 100644
--- a/sieve/tests/reject.at
+++ b/sieve/tests/reject.at
@@ -73,7 +73,7 @@ ENVELOPE TO: <coyote@desert.example.org>
   16: --(boundary)
   17: Content-Type: message/delivery-status
   18: 
-  19: Reporting-UA: sieve; GNU Mailutils 2.2.90
+  19: Reporting-UA: sieve; GNU Mailutils AT_PACKAGE_VERSION
   20: Arrival-Date: (date)
   21: Final-Recipient: RFC822; foobar@nonexistent.net
   22: Action: deleted
@@ -122,7 +122,7 @@ ENVELOPE TO: <b1ff@de.res.example.com>
   16: --(boundary)
   17: Content-Type: message/delivery-status
   18: 
-  19: Reporting-UA: sieve; GNU Mailutils 2.2.90
+  19: Reporting-UA: sieve; GNU Mailutils AT_PACKAGE_VERSION
   20: Arrival-Date: (date)
   21: Final-Recipient: RFC822; foobar@nonexistent.net
   22: Action: deleted
@@ -169,7 +169,7 @@ ENVELOPE TO: <bar@dontmailme.org>
   16: --(boundary)
   17: Content-Type: message/delivery-status
   18: 
-  19: Reporting-UA: sieve; GNU Mailutils 2.2.90
+  19: Reporting-UA: sieve; GNU Mailutils AT_PACKAGE_VERSION
   20: Arrival-Date: (date)
   21: Final-Recipient: RFC822; foobar@nonexistent.net
   22: Action: deleted


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Thu Oct 28 16:22:12 2010
Received: from mailman by lists.gnu.org with archive (Exim 4.43)
	id 1PBYzE-0005fT-Gp
	for mharc-commit-mailutils@gnu.org; Thu, 28 Oct 2010 16:22:12 -0400
Received: from [140.186.70.92] (port=52553 helo=eggs.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.43) id 1PBYz0-0005Xj-2k
	for commit-mailutils@gnu.org; Thu, 28 Oct 2010 16:22:10 -0400
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1PBYyo-0007hf-PO
	for commit-mailutils@gnu.org; Thu, 28 Oct 2010 16:21:58 -0400
Received: from colonialone.fsf.org ([140.186.70.51]:58650
	helo=internal.in.savannah.gnu.org)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1PBYyo-0007hL-JL
	for commit-mailutils@gnu.org; Thu, 28 Oct 2010 16:21:46 -0400
Received: from [10.1.0.108] (helo=vcs-noshell.in.savannah.gnu.org)
	by internal.in.savannah.gnu.org with esmtp (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1PBYyo-0004fM-1l
	for commit-mailutils@gnu.org; Thu, 28 Oct 2010 20:21:46 +0000
Received: from gray by vcs-noshell.in.savannah.gnu.org with local (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1PBYym-0001RA-1Q
	for commit-mailutils@gnu.org; Thu, 28 Oct 2010 20:21:46 +0000
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: 697384c64dadb0a4db97136a6cbcf2166c48897b
X-Git-Newrev: 4a49f5341ae9005c9ffbd7620efaecffcdfda79f
Message-Id: <E1PBYym-0001RA-1Q@vcs-noshell.in.savannah.gnu.org>
Date: Thu, 28 Oct 2010 20:21:44 +0000
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
Subject: [SCM] GNU Mailutils branch, master,
	updated. release-2.2-164-g4a49f53
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.5
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=unsubscribe>
List-Archive: <http://lists.gnu.org/archive/html/commit-mailutils>
List-Post: <mailto:commit-mailutils@gnu.org>
List-Help: <mailto:commit-mailutils-request@gnu.org?subject=help>
List-Subscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Thu, 28 Oct 2010 20:22:10 -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=4a49f5341ae9005c9ffbd7620efaecffcdfda79f

The branch, master has been updated
       via  4a49f5341ae9005c9ffbd7620efaecffcdfda79f (commit)
      from  697384c64dadb0a4db97136a6cbcf2166c48897b (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 4a49f5341ae9005c9ffbd7620efaecffcdfda79f
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Thu Oct 28 23:20:22 2010 +0300

    Add wordsplit module.
    
    The wordsplit module (borrowed from another project of mine, called 'grecs')
    provides a flexible and powerful mechanism for parsing input in a way
    similar to posix-shell.  In particular, the POSIX function wordexp is
    a subset of the wordsplit functionality.
    
    The argcv module is now deprecated.
    
    * include/mailutils/wordsplit.h: New file.
    * include/mailutils/Makefile.am (pkginclude_HEADERS): Add wordsplit.h
    * libmailutils/string/wordsplit.c: New file.
    * libmailutils/string/Makefile.am (libstring_la_SOURCES): Add wordsplit.c.
    * include/mailutils/argcv.h (mu_argcv_join): New proto.
    (mu_argcv_get, mu_argcv_get_n, mu_argcv_get_np)
    (mu_argcv_unquote_char, mu_argcv_quote_char)
    (mu_argcv_quoted_length, mu_argcv_unquote_copy)
    (mu_argcv_quote_copy): Mark as deprecated.
    * include/mailutils/mailutils.h: Include wordsplit.h.
    
    * libmailutils/tests/wsp.c: New file.
    * libmailutils/tests/.gitignore: List wsp.
    * libmailutils/tests/wordsplit.at: New file.
    * libmailutils/tests/Makefile.am (noinst_PROGRAMS): Add wsp
    (TESTSUITE_AT): Add wordsplit.at.
    * libmailutils/tests/argcv.c: Define MU_ARCGV_DEPRECATED to
    avoid deprecation warnings.
    * libmailutils/tests/testsuite.at: Include wordsplit.at.
    
    * libmailutils/base/argcvfree.c: New file.
    * libmailutils/base/argcvjoin.c: New file.
    * libmailutils/base/argcvrem.c: New file.
    * libmailutils/base/Makefile.am (libbase_la_SOURCES): Add new files.
    
    * libmailutils/base/argcv.c: Define MU_ARCGV_DEPRECATED to
    avoid deprecation warnings.
    (argcv_string, argcv_free, argv_free)
    (mu_argcv_remove): Rewrite in separate modules.
    
    * comsat/comsat.h: Include wordsplit.h.
    * imap4d/imap4d.h: Likewise.
    * mail/mail.h: Likewise.
    * mh/mh.h: Likewise.
    * readmsg/readmsg.h: Likewise.
    
    * comsat/action.c: Use wordsplit.
    * imap4d/fetch.c: Likewise.
    * lib/mailcap.c: Likewise.
    * libmailutils/auth/mu_auth.c: Likewise.
    * libmailutils/base/mutil.c: Likewise.
    * libmailutils/cfg/format.c: Likewise.
    * libmailutils/cfg/lexer.l: Likewise.
    * libmailutils/cfg/parser.y: Likewise.
    * libmailutils/diag/gdebug.c: Likewise.
    * libmailutils/mailer/mailer.c: Likewise.
    * libmailutils/server/acl.c: Likewise.
    * libmailutils/stream/prog_stream.c: Likewise.
    * libmailutils/tests/listop.c: Likewise.
    * libmailutils/url/create.c: Likewise.
    * libmu_auth/ldap.c: Likewise.
    * libmu_auth/radius.c: Likewise.
    * libmu_sieve/sieve.l: Likewise.
    * libproto/mailer/mbox.c: Likewise.
    * libproto/mailer/smtp.c: Likewise.
    * libproto/mailer/smtp_gsasl.c: Likewise.
    * mail/mailline.c: Likewise.
    * mail/mailvar.c: Likewise.
    * mail/send.c: Likewise.
    * mail/util.c: Likewise.
    * mh/folder.c: Likewise.
    * mh/mh_alias.y: Likewise.
    * mh/mh_argp.c: Likewise.
    * mh/mh_init.c: Likewise.
    * mh/mh_list.c: Likewise.
    * mh/mh_msgset.c: Likewise.
    * mh/mh_sequence.c: Likewise.
    * mh/mh_whatnow.c: Likewise.
    * mh/mh_sequence.c: Likewise.
    * mh/mh_whatnow.c: Likewise.
    * mh/mhn.c: Likewise.
    * mh/send.c: Likewise.
    * movemail/movemail.c: Likewise.
    * mu/shell.c: Likewise.
    * readmsg/readmsg.c: Likewise.
    * testsuite/smtpsend.c: Likewise.
    
    * pop3d/popauth.c: Use wordsplit.
    Use simplified input format (username and password delimited
    by any amount of whitespace).
    Read/produce old format if invoked with the --compatibility option.
    
    * examples/aclck.c: Use wordsplit instead of argcv.
    * examples/header.c: Likewise.
    * examples/mta.c: Likewise.
    * examples/.gitignore: Remove pop3client.
    * examples/Makefile.am: Remove pop3client.
    
    * po/POTFILES.in: Add new files.

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

Summary of changes:
 comsat/action.c                                    |   40 +-
 comsat/comsat.h                                    |    2 +-
 examples/.gitignore                                |    1 -
 examples/Makefile.am                               |   16 +-
 examples/aclck.c                                   |   29 +-
 examples/header.c                                  |   34 +-
 examples/mta.c                                     |   52 +-
 examples/pop3client.c                              | 1189 ----------------
 imap4d/fetch.c                                     |  106 +-
 imap4d/imap4d.h                                    |    2 +-
 include/mailutils/Makefile.am                      |    3 +-
 include/mailutils/argcv.h                          |   64 +-
 include/mailutils/mailutils.h                      |    1 +
 include/mailutils/wordsplit.h                      |  135 ++
 lib/mailcap.c                                      |   34 +-
 libmailutils/auth/mu_auth.c                        |   26 +-
 libmailutils/base/Makefile.am                      |    3 +
 libmailutils/base/argcv.c                          |  112 +--
 libmailutils/{url/urlstr.c => base/argcvfree.c}    |   36 +-
 libmailutils/base/argcvjoin.c                      |  116 ++
 libmailutils/{url/get-secret.c => base/argcvrem.c} |   53 +-
 libmailutils/base/mutil.c                          |   19 +-
 libmailutils/cfg/format.c                          |    7 +-
 libmailutils/cfg/lexer.l                           |    4 +-
 libmailutils/cfg/parser.y                          |   77 +-
 libmailutils/diag/gdebug.c                         |   29 +-
 libmailutils/mailer/mailer.c                       |   30 +-
 libmailutils/server/acl.c                          |   18 +-
 libmailutils/stream/prog_stream.c                  |   13 +-
 libmailutils/string/Makefile.am                    |    1 +
 libmailutils/string/wordsplit.c                    | 1469 ++++++++++++++++++++
 libmailutils/tests/.gitignore                      |    1 +
 libmailutils/tests/Makefile.am                     |    6 +-
 libmailutils/tests/argcv.c                         |    1 +
 libmailutils/tests/listop.c                        |   76 +-
 libmailutils/tests/testsuite.at                    |    1 +
 libmailutils/tests/wordsplit.at                    |  304 ++++
 libmailutils/tests/wsp.c                           |  285 ++++
 libmailutils/url/copy.c                            |    2 +-
 libmailutils/url/create.c                          |   21 +-
 libmu_argp/muinit.c                                |    1 +
 libmu_auth/ldap.c                                  |   38 +-
 libmu_auth/radius.c                                |   32 +-
 libmu_sieve/sieve.l                                |    4 +-
 libproto/mailer/mbox.c                             |   36 +-
 libproto/mailer/smtp.c                             |   28 +-
 libproto/mailer/smtp_gsasl.c                       |   29 +-
 mail/mail.h                                        |    1 +
 mail/mailline.c                                    |   26 +-
 mail/mailvar.c                                     |   23 +-
 mail/send.c                                        |   19 +-
 mail/util.c                                        |   37 +-
 mh/folder.c                                        |   37 +-
 mh/mh.h                                            |    1 +
 mh/mh_alias.y                                      |   20 +-
 mh/mh_argp.c                                       |   22 +-
 mh/mh_init.c                                       |   25 +-
 mh/mh_list.c                                       |   36 +-
 mh/mh_msgset.c                                     |   17 +-
 mh/mh_sequence.c                                   |   28 +-
 mh/mh_whatnow.c                                    |   16 +-
 mh/mhn.c                                           |   64 +-
 mh/send.c                                          |   60 +-
 movemail/movemail.c                                |   18 +-
 mu/shell.c                                         |   35 +-
 po/POTFILES.in                                     |    6 +
 pop3d/popauth.c                                    |  115 +-
 readmsg/readmsg.c                                  |   32 +-
 readmsg/readmsg.h                                  |    2 +-
 testsuite/smtpsend.c                               |   21 +-
 70 files changed, 3272 insertions(+), 1975 deletions(-)
 delete mode 100644 examples/pop3client.c
 create mode 100644 include/mailutils/wordsplit.h
 copy libmailutils/{url/urlstr.c => base/argcvfree.c} (59%)
 create mode 100644 libmailutils/base/argcvjoin.c
 copy libmailutils/{url/get-secret.c => base/argcvrem.c} (55%)
 create mode 100644 libmailutils/string/wordsplit.c
 create mode 100644 libmailutils/tests/wordsplit.at
 create mode 100644 libmailutils/tests/wsp.c

diff --git a/comsat/action.c b/comsat/action.c
index 0e1e679..4a87013 100644
--- a/comsat/action.c
+++ b/comsat/action.c
@@ -204,7 +204,7 @@ expand_line (const char *str, mu_message_t msg)
 	  p++;
 	  if (*p)
 	    {
-	      c = mu_argcv_unquote_char (*p);
+	      c = mu_wordsplit_c_unquote_char (*p);
 	      obstack_1grow (&stk, c);
 	    }
 	  break;
@@ -300,7 +300,8 @@ action_exec (FILE *tty, int argc, char **argv)
 
   if (argv[0][0] != '/')
     {
-      mu_diag_output (MU_DIAG_ERROR, _("not an absolute pathname: %s"), argv[0]);
+      mu_diag_output (MU_DIAG_ERROR, _("not an absolute pathname: %s"),
+		      argv[0]);
       return;
     }
 
@@ -388,15 +389,14 @@ run_user_action (FILE *tty, const char *cr, mu_message_t msg)
       
       while ((n = act_getline (fp, &stmt, &size)))
 	{
-	  int argc;
-	  char **argv;
+	  struct mu_wordsplit ws;
 
-	  if (mu_argcv_get (stmt, "", NULL, &argc, &argv) == 0
-	      && argc
-	      && argv[0][0] != '#')
+	  ws.ws_comment = "#";
+	  if (mu_wordsplit (stmt, &ws, MU_WRDSF_DEFFLAGS | MU_WRDSF_COMMENT)
+	      && ws.ws_wordc)
 	    {
 	      mu_debug_set_locus (debug, rcname, line);
-	      if (strcmp (argv[0], "beep") == 0)
+	      if (strcmp (ws.ws_wordv[0], "beep") == 0)
 		{
 		  /* FIXME: excess arguments are ignored */
 		  action_beep (tty);
@@ -406,25 +406,27 @@ run_user_action (FILE *tty, const char *cr, mu_message_t msg)
 		{
 		  /* Rest of actions require keyword expansion */
 		  int i;
-		  int n_option = argc > 1 && strcmp (argv[1], "-n") == 0;
+		  int n_option = ws.ws_wordc > 1 &&
+		                 strcmp (ws.ws_wordv[1], "-n") == 0;
 		  
-		  for (i = 1; i < argc; i++)
+		  for (i = 1; i < ws.ws_wordc; i++)
 		    {
-		      char *oldarg = argv[i];
-		      argv[i] = expand_line (argv[i], msg);
+		      char *oldarg = ws.ws_wordv[i];
+		      ws.ws_wordv[i] = expand_line (ws.ws_wordv[i], msg);
 		      free (oldarg);
-		      if (!argv[i])
+		      if (!ws.ws_wordv[i])
 			break;
 		    }
 		  
-		  if (strcmp (argv[0], "echo") == 0)
+		  if (strcmp (ws.ws_wordv[0], "echo") == 0)
 		    {
-		      action_echo (tty, cr, n_option, argc - 1, argv + 1);
+		      action_echo (tty, cr, n_option,
+				   ws.ws_wordc - 1, ws.ws_wordv + 1);
 		      nact++;
 		    }
-		  else if (strcmp (argv[0], "exec") == 0)
+		  else if (strcmp (ws.ws_wordv[0], "exec") == 0)
 		    {
-		      action_exec (tty, argc - 1, argv + 1);
+		      action_exec (tty, ws.ws_wordc - 1, ws.ws_wordv + 1);
 		      nact++;
 		    }
 		  else
@@ -432,12 +434,12 @@ run_user_action (FILE *tty, const char *cr, mu_message_t msg)
 		      fprintf (tty, _(".biffrc:%d: unknown keyword"), line);
 		      fprintf (tty, "\r\n");
 		      mu_diag_output (MU_DIAG_ERROR, _("unknown keyword %s"),
-				      argv[0]);
+				      ws.ws_wordv[0]);
 		      break;
 		    }
 		} 
 	    }
-	  mu_argcv_free (argc, argv);
+	  mu_wordsplit_free (&ws);
 	  line += n;
 	}
       fclose (fp);
diff --git a/comsat/comsat.h b/comsat/comsat.h
index 5148fa8..1c1381a 100644
--- a/comsat/comsat.h
+++ b/comsat/comsat.h
@@ -53,7 +53,7 @@
 #include <mailutils/registrar.h>
 #include <mailutils/stream.h>
 #include <mailutils/mu_auth.h>
-#include <mailutils/argcv.h>
+#include <mailutils/wordsplit.h>
 #include <mailutils/nls.h>
 #include <mailutils/daemon.h>
 #include <mailutils/acl.h>
diff --git a/examples/.gitignore b/examples/.gitignore
index b42a928..af4156b 100644
--- a/examples/.gitignore
+++ b/examples/.gitignore
@@ -20,6 +20,5 @@ muemail
 murun
 musocio
 nntpclient
-pop3client
 sfrom
 url-parse
diff --git a/examples/Makefile.am b/examples/Makefile.am
index 1addf44..45ee0fc 100644
--- a/examples/Makefile.am
+++ b/examples/Makefile.am
@@ -15,10 +15,6 @@
 ## You should have received a copy of the GNU General Public License
 ## along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
-if MU_COND_SUPPORT_POP
-  POP3CLIENT = pop3client
-endif
-
 if MU_COND_SUPPORT_NNTP
   NNTPCLIENT = nntpclient
 endif
@@ -46,10 +42,9 @@ noinst_PROGRAMS = \
  murun\
  musocio\
  $(NNTPCLIENT)\
- $(POP3CLIENT)\
  sfrom
 
-EXTRA_PROGRAMS = pop3client nntpclient
+EXTRA_PROGRAMS = nntpclient
 
 ## NOTE: Numaddr must be an installable target, otherwise libtool
 ## will not create a shared library and `make check' will fail in
@@ -94,15 +89,6 @@ sfrom_LDADD =\
  @MU_AUTHLIBS@\
  ${MU_LIB_MAILUTILS}
 
-pop3client_CPPFLAGS = @MU_APP_COMMON_INCLUDES@
-pop3client_LDADD = \
- ../lib/libmuaux.a\
- ${MU_LIB_POP}\
- ${MU_LIB_AUTH}\
- @MU_AUTHLIBS@\
- ${MU_LIB_MAILUTILS}\
- @READLINE_LIBS@
-
 nntpclient_LDADD = \
  ../lib/libmuaux.a\
  ${MU_LIB_NNTP}\
diff --git a/examples/aclck.c b/examples/aclck.c
index d4d9b95..404e87f 100644
--- a/examples/aclck.c
+++ b/examples/aclck.c
@@ -62,8 +62,8 @@ read_rules (FILE *fp)
 {
   char buf[512];
   int line = 0;
-  int argc = 0;
-  char **argv;
+  int wsflags = MU_WRDSF_DEFFLAGS | MU_WRDSF_COMMENT;
+  struct mu_wordsplit ws;
   int rc;
   
   rc = mu_acl_create (&acl);
@@ -72,7 +72,8 @@ read_rules (FILE *fp)
       mu_error ("cannot create acl: %s", mu_strerror (rc));
       exit (1);
     }
-  
+
+  ws.ws_comment = "#";
   while (fgets (buf, sizeof buf, fp))
     {
       unsigned long netmask;
@@ -95,17 +96,20 @@ read_rules (FILE *fp)
       if (buf[0] == '#')
 	continue;
 
-      if (argc)
-	mu_argcv_free (argc, argv);
-
-      mu_argcv_get (buf, " \t", "#", &argc, &argv);
-      if (argc < 2)
+      if (mu_wordsplit (buf, &ws, wsflags))
+	{
+	  mu_error ("cannot split line `%s': %s", buf,
+		    mu_wordsplit_strerror (&ws));
+	  continue;
+	}
+      wsflags |= MU_WRDSF_REUSE;
+      if (ws.ws_wordc < 2)
 	{
  	  mu_error ("%d: invalid input", line);
 	  continue;
 	}
 
-      p = strchr (argv[1], '/');
+      p = strchr (ws.ws_wordv[1], '/');
       if (p)
 	{
 	  char *q;
@@ -144,7 +148,7 @@ read_rules (FILE *fp)
       else
 	netmask = 0xfffffffful;
       
-      sa = parse_address (&salen, argv[1]);
+      sa = parse_address (&salen, ws.ws_wordv[1]);
       
       /* accept addr
 	 deny addr
@@ -152,7 +156,7 @@ read_rules (FILE *fp)
 	 exec addr [rest ...]
 	 execif addr rest ....]
       */
-      if (mu_acl_string_to_action (argv[0], &action))
+      if (mu_acl_string_to_action (ws.ws_wordv[0], &action))
 	{
 	  mu_error ("%d: invalid command", line);
 	  continue;
@@ -167,7 +171,7 @@ read_rules (FILE *fp)
 	case mu_acl_log:
 	case mu_acl_exec:
 	case mu_acl_ifexec:
-	  data = strdup (argv[2]);
+	  data = strdup (ws.ws_wordv[2]);
 	}
 
       rc = mu_acl_append (acl, action, data, sa, salen, netmask);
@@ -175,6 +179,7 @@ read_rules (FILE *fp)
 	mu_error ("%d: cannot append acl entry: %s", line,
 		  mu_strerror (rc));
     }
+  mu_wordsplit_free (&ws);
 }
 
 int
diff --git a/examples/header.c b/examples/header.c
index 1d6c790..18357a4 100644
--- a/examples/header.c
+++ b/examples/header.c
@@ -489,9 +489,9 @@ main (int argc, char **argv)
 {
   int c;
   char buf[512];
-  char **prevv;
+  char **prevv = NULL;
   int prevc = 0;
-  
+
   interactive = isatty (0);
   while ((c = getopt (argc, argv, "f:h")) != EOF)
     {
@@ -525,36 +525,36 @@ main (int argc, char **argv)
 	}
     }
   
-  while (prompt(0), fgets(buf, sizeof buf, stdin))
+  while (prompt (0), fgets (buf, sizeof buf, stdin))
     {
-      int c;
-      char **v;
-      int status;
+      struct mu_wordsplit ws;
 
       line_num++;
-      status = mu_argcv_get (buf, NULL, "#", &c, &v);
-      if (status)
+      ws.ws_comment = "#";
+      if (mu_wordsplit (buf, &ws, MU_WRDSF_DEFFLAGS | MU_WRDSF_COMMENT))
 	{
-	  mu_error ("%u: cannot parse: %s",
-		   line_num, mu_strerror (status));
+	  mu_error ("cannot split line `%s': %s", buf,
+		    mu_wordsplit_strerror (&ws));
 	  continue;
 	}
-
-      if (c == 0)
+      
+      if (ws.ws_wordc == 0)
 	{
 	  if (prevc)
 	    docmd (prevc, prevv);
-	  else
-	    mu_argcv_free (c, v);
 	}
       else
 	{
-	  docmd (c, v);
+	  docmd (ws.ws_wordc, ws.ws_wordv);
 	  mu_argcv_free (prevc, prevv);
-	  prevc = c;
-	  prevv = v;
+	  prevc = ws.ws_wordc;
+	  prevv = ws.ws_wordv;
+	  ws.ws_wordc = 0;
+	  ws.ws_wordv = NULL;
 	}
+      mu_wordsplit_free (&ws);
     }
+  mu_argcv_free (prevc, prevv);
   exit (0);
 }
 
diff --git a/examples/mta.c b/examples/mta.c
index 705a2b0..bff83d5 100644
--- a/examples/mta.c
+++ b/examples/mta.c
@@ -565,30 +565,31 @@ smtp (mu_stream_t str)
   char *buf = NULL;
   size_t size = 0;
   char *rcpt_addr;
+  int wsflags = MU_WRDSF_DEFFLAGS;
+  struct mu_wordsplit ws;
   
   smtp_reply (str, 220, "Ready");
   for (state = STATE_INIT; state != STATE_QUIT; )
     {
-      char *s;
-      int argc;
-      char **argv;
       int kw;
       size_t len;
       
       if (mu_stream_getline (str, &buf, &size, &len) || len == 0)
 	exit (EX_PROTOCOL);
 
-      s = mu_str_stripws (buf);
-      
-      if (mu_argcv_get (s, "", NULL, &argc, &argv))
-	exit (EX_UNAVAILABLE);
+      if (mu_wordsplit (buf, &ws, wsflags))
+	{
+	  mu_error ("cannot split line `%s': %s", buf,
+		    mu_wordsplit_strerror (&ws));
+	  exit (EX_UNAVAILABLE);
+	}
+      wsflags |= MU_WRDSF_REUSE;
 
-      kw = smtp_kw (argv[0]);
+      kw = smtp_kw (ws.ws_wordv[0]);
       if (kw == KW_QUIT)
 	{
 	  smtp_reply (str, 221, "Done");
 	  state = STATE_QUIT;
-	  mu_argcv_free (argc, argv);
 	  continue;
 	}
       
@@ -599,13 +600,14 @@ smtp (mu_stream_t str)
 	    {
 	    case KW_EHLO:
 	    case KW_HELO:
-	      if (argc == 2)
+	      if (ws.ws_wordc == 2)
 		{
 		  smtp_reply (str, 250, "pleased to meet you");
 		  state = STATE_EHLO;
 		}
 	      else
-		smtp_reply (str, 501, "%s requires domain address", argv[0]);
+		smtp_reply (str, 501, "%s requires domain address",
+			    ws.ws_wordv[0]);
 	      break;
 
 	    default:
@@ -618,10 +620,11 @@ smtp (mu_stream_t str)
 	  switch (kw)
 	    {
 	    case KW_MAIL:
-	      if (argc == 2)
-		from_person = check_prefix (argv[1], "from:");
-	      else if (argc == 3 && mu_c_strcasecmp (argv[1], "from:") == 0)
-		from_person = argv[2];
+	      if (ws.ws_wordc == 2)
+		from_person = check_prefix (ws.ws_wordv[1], "from:");
+	      else if (ws.ws_wordc == 3 &&
+		       mu_c_strcasecmp (ws.ws_wordv[1], "from:") == 0)
+		from_person = ws.ws_wordv[2];
 	      else
 		from_person = NULL;
 
@@ -644,10 +647,11 @@ smtp (mu_stream_t str)
 	  switch (kw)
 	    {
 	    case KW_RCPT:
-	      if (argc == 2)
-		rcpt_addr = check_prefix (argv[1], "to:");
-	      else if (argc == 3 && mu_c_strcasecmp (argv[1], "to:") == 0)
-		rcpt_addr = argv[2];
+	      if (ws.ws_wordc == 2)
+		rcpt_addr = check_prefix (ws.ws_wordv[1], "to:");
+	      else if (ws.ws_wordc == 3 &&
+		       mu_c_strcasecmp (ws.ws_wordv[1], "to:") == 0)
+		rcpt_addr = ws.ws_wordv[2];
 	      else
 		rcpt_addr = NULL;
 	      
@@ -674,10 +678,11 @@ smtp (mu_stream_t str)
 	  switch (kw)
 	    {
 	    case KW_RCPT:
-	      if (argc == 2)
-		rcpt_addr = check_prefix (argv[1], "to:");
-	      else if (argc == 3 && mu_c_strcasecmp (argv[1], "to:") == 0)
-		rcpt_addr = argv[2];
+	      if (ws.ws_wordc == 2)
+		rcpt_addr = check_prefix (ws.ws_wordv[1], "to:");
+	      else if (ws.ws_wordc == 3 &&
+		       mu_c_strcasecmp (ws.ws_wordv[1], "to:") == 0)
+		rcpt_addr = ws.ws_wordv[2];
 	      else
 		rcpt_addr = NULL;
 	      
@@ -734,7 +739,6 @@ smtp (mu_stream_t str)
 	  break;
 
 	}
-      mu_argcv_free (argc, argv);
     }
 }
 
diff --git a/examples/pop3client.c b/examples/pop3client.c
deleted file mode 100644
index 7b1f864..0000000
--- a/examples/pop3client.c
+++ /dev/null
@@ -1,1189 +0,0 @@
-/* pop3client.c -- An application which demonstrates how to use the
-   GNU Mailutils pop3 functions.  This application interactively allows users
-   to contact a pop3 server.
-
-   Copyright (C) 2003, 2004, 2005, 2007, 2008, 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.
-
-   GNU Mailutils 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 GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
-
-#ifdef HAVE_CONFIG_H
-# include <config.h>  
-#endif 
-#include <sys/types.h>
-#include <netinet/in.h>
-#include <netdb.h>
-#include <unistd.h>
-#include <stdio.h>
-#include <errno.h>
-#include <string.h>
-#include <strings.h>
-#include <stdlib.h>
-#include <termios.h>
-#include <signal.h>
-#include <xalloc.h>
-
-#ifdef WITH_READLINE
-# include <readline/readline.h>
-# include <readline/history.h>
-#endif
-
-#include <mailutils/pop3.h>
-#include <mailutils/iterator.h>
-#include <mailutils/error.h>
-#include <mailutils/errno.h>
-#include <mailutils/vartab.h>
-#include <mailutils/argcv.h>
-#include <mailutils/cctype.h>
-#include <mailutils/cstr.h>
-#include <mailutils/tls.h>
-#include <mailutils/util.h>
-
-/* A structure which contains information on the commands this program
-   can understand. */
-
-typedef struct
-{
-  const char *name;		/* User printable name of the function. */
-  int argmin;
-  int argmax;
-  int (*func) (int, char **);	/* Function to call to do the job. */
-  const char *doc;		/* Documentation for this function.  */
-}
-COMMAND;
-
-/* The names of functions that actually do the manipulation. */
-int com_apop (int, char **);
-int com_capa (int, char **);
-int com_disconnect (int, char **);
-int com_dele (int, char **);
-int com_exit (int, char **);
-int com_help (int, char **);
-int com_list (int, char **);
-int com_noop (int, char **);
-int com_connect (int, char **);
-int com_pass (int, char **);
-int com_quit (int, char **);
-int com_retr (int, char **);
-int com_rset (int, char **);
-int com_stat (int, char **);
-int com_top (int, char **);
-int com_uidl (int, char **);
-int com_user (int, char **);
-int com_verbose (int, char **);
-int com_prompt (int, char **);
-int com_stls (int, char **);
-int com_history (int, char **);
-
-COMMAND *find_command (char *);
-
-COMMAND commands[] = {
-  { "apop",       3,  3, com_apop,
-    "Authenticate with APOP: APOP user secret" },
-  { "capa",       1, -1, com_capa,
-    "List capabilities: capa [-reread] [names...]" },
-  { "disconnect", 1, 1,
-    com_disconnect, "Close connection: disconnect" },
-  { "dele",       2, 2, com_dele,
-    "Mark message: DELE msgno" },
-  { "exit",       1, 1, com_exit,
-    "exit program" },
-  { "help",       1, 1, com_help,
-    "Display this text" },
-  { "?",          1, 1, com_help,
-    "Synonym for `help'" },
-  { "list",       1, 2, com_list,
-    "List messages: LIST [msgno]" },
-  { "noop",       1, 1, com_noop,
-    "Send no operation: NOOP" },
-  { "pass",       1, 2, com_pass,
-    "Send passwd: PASS [passwd]" },
-  { "prompt",     -1, -1, com_prompt,
-    "Set command prompt" },
-  { "connect",    1, 4, com_connect,
-    "Open connection: connect [-tls] hostname port" },
-  { "quit",       1, 1, com_quit,
-    "Go to Update state : QUIT" },
-  { "retr",       2, 2, com_retr,
-    "Dowload message: RETR msgno" },
-  { "rset",       1, 1, com_rset,
-    "Unmark all messages: RSET" },
-  { "stat",       1, 1, com_stat,
-    "Get the size and count of mailbox : STAT" },
-  { "stls",       1, 1, com_stls,
-    "Start TLS negotiation" },
-  { "top",        2, 3, com_top,
-    "Get the header of message: TOP msgno [lines]" },
-  { "uidl",       1, 2, com_uidl,
-    "Get the unique id of message: UIDL [msgno]" },
-  { "user",       2, 2, com_user,
-    "send login: USER user" },
-  { "verbose",    1, 4, com_verbose,
-    "Enable Protocol tracing: verbose [on|off|mask|unmask] [x1 [x2]]" },
-#ifdef WITH_READLINE
-  { "history",    1, 1, com_history,
-    "Show command history" },
-#endif
-  { NULL }
-};
-
-/* Global handle for pop3.  */
-mu_pop3_t pop3;
-
-/* Flag if verbosity is needed.  */
-int verbose;
-#define VERBOSE_MASK(n) (1<<((n)+1))
-#define SET_VERBOSE_MASK(n) (verbose |= VERBOSE_MASK (n))
-#define CLR_VERBOSE_MASK(n) (verbose &= VERBOSE_MASK (n))
-#define QRY_VERBOSE_MASK(n) (verbose & VERBOSE_MASK (n))
-#define HAS_VERBOSE_MASK(n) (verbose & ~1)
-#define SET_VERBOSE() (verbose |= 1)
-#define CLR_VERBOSE() (verbose &= ~1)
-#define QRY_VERBOSE() (verbose & 1)
-
-/* When non-zero, this global means the user is done using this program. */
-int done;
-
-enum pop_session_status
-  {
-    pop_session_disconnected,
-    pop_session_connected,
-    pop_session_logged_in
-  };
-
-enum pop_session_status pop_session_status;
-
-int connect_argc;
-char **connect_argv;
-
-/* Host we are connected to. */
-#define host connect_argv[0]
-int port = 110;
-char *username;
-
-/* Command line prompt */
-#define DEFAULT_PROMPT "pop3> "
-char *prompt;
-
-const char *
-pop_session_str (enum pop_session_status stat)
-{
-  switch (stat)
-    {
-    case pop_session_disconnected:
-      return "disconnected";
-      
-    case pop_session_connected:
-      return "connected";
-      
-    case pop_session_logged_in:
-      return "logged in";
-    }
-  return "unknown";
-}
-
-char *
-expand_prompt ()
-{
-  mu_vartab_t vtab;
-  char *str;
-  
-  if (mu_vartab_create (&vtab))
-    return strdup (prompt);
-  mu_vartab_define (vtab, "user",
-		    (pop_session_status == pop_session_logged_in) ?
-		      username : "not logged in", 1);
-  mu_vartab_define (vtab, "host",
-		    (pop_session_status != pop_session_disconnected) ?
-		      host : "not connected", 1);
-  mu_vartab_define (vtab, "program-name", mu_program_name, 1);
-  mu_vartab_define (vtab, "canonical-program-name", "pop3client", 1);
-  mu_vartab_define (vtab, "package", PACKAGE, 1);
-  mu_vartab_define (vtab, "version", PACKAGE_VERSION, 1);
-  mu_vartab_define (vtab, "status", pop_session_str (pop_session_status), 1);
-  
-  if (mu_vartab_expand (vtab, prompt, &str))
-    str = strdup (prompt);
-  mu_vartab_destroy (&vtab);
-  return str;
-}
-
-
-
-#ifdef WITH_READLINE
-#define HISTFILE_SUFFIX "_history"
-
-static char *
-get_history_file_name ()
-{
-  static char *filename = NULL;
-
-  if (!filename)
-    {
-      char *hname;
-      
-      hname = xmalloc(3 + strlen (rl_readline_name) + sizeof HISTFILE_SUFFIX);
-      strcpy (hname, "~/.");
-      strcat (hname, rl_readline_name);
-      strcat (hname, HISTFILE_SUFFIX);
-      filename = mu_tilde_expansion (hname, "/", NULL);
-      free(hname);
-    }
-  return filename;
-}
-
-/* Interface to Readline Completion */
-
-char *command_generator (const char *, int);
-char **pop_completion (char *, int, int);
-
-/* Tell the GNU Readline library how to complete.  We want to try to complete
-   on command names if this is the first word in the line, or on filenames
-   if not. */
-void
-initialize_readline ()
-{
-  /* Allow conditional parsing of the ~/.inputrc file. */
-  rl_readline_name = (char *) "pop3client";
-
-  /* Tell the completer that we want a crack first. */
-  rl_attempted_completion_function = (CPPFunction *) pop_completion;
-
-  read_history (get_history_file_name ());
-}
-
-void
-finish_readline ()
-{
-  write_history (get_history_file_name());
-}
-
-/* Attempt to complete on the contents of TEXT.  START and END bound the
-   region of rl_line_buffer that contains the word to complete.  TEXT is
-   the word to complete.  We can use the entire contents of rl_line_buffer
-   in case we want to do some simple parsing.  Return the array of matches,
-   or NULL if there aren't any. */
-char **
-pop_completion (char *text, int start, int end MU_ARG_UNUSED)
-{
-  char **matches;
-
-  matches = (char **) NULL;
-
-  /* If this word is at the start of the line, then it is a command
-     to complete.  Otherwise it is the name of a file in the current
-     directory. */
-  if (start == 0)
-    matches = rl_completion_matches (text, command_generator);
-
-  return (matches);
-}
-
-/* Generator function for command completion.  STATE lets us know whether
-   to start from scratch; without any state (i.e. STATE == 0), then we
-   start at the top of the list. */
-char *
-command_generator (const char *text, int state)
-{
-  static int list_index, len;
-  const char *name;
-
-  /* If this is a new word to complete, initialize now.  This includes
-     saving the length of TEXT for efficiency, and initializing the index
-     variable to 0. */
-  if (!state)
-    {
-      list_index = 0;
-      len = strlen (text);
-    }
-
-  /* Return the next name which partially matches from the command list. */
-  while ((name = commands[list_index].name))
-    {
-      list_index++;
-
-      if (strncmp (name, text, len) == 0)
-	return xstrdup (name);
-    }
-
-  /* If no names matched, then return NULL. */
-  return NULL;
-}
-
-static char *pre_input_line;
-
-static int
-pre_input (void)
-{
-  rl_insert_text (pre_input_line);
-  free (pre_input_line);
-  rl_pre_input_hook = NULL;
-  rl_redisplay ();
-  return 0;
-}
-
-static int
-retrieve_history (char *str)
-{
-  char *out;
-  int rc;
-
-  rc = history_expand (str, &out);
-  switch (rc)
-    {
-    case -1:
-      mu_error ("%s", out);
-      free (out);
-      return 1;
-
-    case 0:
-      break;
-
-    case 1:
-      pre_input_line = out;
-      rl_pre_input_hook = pre_input;
-      return 1;
-
-    case 2:
-      printf ("%s\n", out);
-      free (out);
-      return 1;
-    }
-  return 0;
-}
-
-int
-com_history (int argc, char **argv)
-{
-  int i;
-  HIST_ENTRY **hlist;
-
-  hlist = history_list ();
-  for (i = 0; i < history_length; i++)
-    printf ("%4d) %s\n", i + 1, hlist[i]->line);
-
-  return 0;
-}
-
-#else
-# define initialize_readline()
-# define finish_readline()
-
-char *
-readline (char *prompt)
-{
-  char buf[255];
-
-  if (prompt)
-    {
-      printf ("%s", prompt);
-      fflush (stdout);
-    }
-
-  if (!fgets (buf, sizeof (buf), stdin))
-    return NULL;
-  return xstrdup (buf);
-}
-
-void
-add_history (const char *s MU_ARG_UNUSED)
-{
-}
-#endif
-
-int
-get_bool (const char *str, int *pb)
-{
-  if (mu_c_strcasecmp (str, "yes") == 0
-      || mu_c_strcasecmp (str, "on") == 0
-      || mu_c_strcasecmp (str, "true") == 0)
-    *pb = 1;
-  else if (mu_c_strcasecmp (str, "no") == 0
-      || mu_c_strcasecmp (str, "off") == 0
-      || mu_c_strcasecmp (str, "false") == 0)
-    *pb = 0;
-  else
-    return 1;
-
-  return 0;
-}
-
-int
-get_port (const char *port_str, int *pn)
-{
-  short port_num;
-  long num;
-  char *p;
-  
-  num = port_num = strtol (port_str, &p, 0);
-  if (*p == 0)
-    {
-      if (num != port_num)
-	{
-	  mu_error ("bad port number: %s", port_str);
-	  return 1;
-	}
-    }
-  else
-    {
-      struct servent *sp = getservbyname (port_str, "tcp");
-      if (!sp)
-	{
-	  mu_error ("unknown port name");
-	  return 1;
-	}
-      port_num = ntohs (sp->s_port);
-    }
-  *pn = port_num;
-  return 0;
-}
-
-/* Parse and execute a command line. */
-int
-execute_line (char *line)
-{
-  int argc;
-  char **argv;
-  int status = 0;
-
-  if (mu_argcv_get (line, NULL, "#", &argc, &argv))
-    {
-      mu_error("cannot parse input line");
-      return 0;
-    }
-
-  if (argc >= 0)
-    {
-      COMMAND *command = find_command (argv[0]);
-      
-      if (!command)
-	mu_error ("%s: no such command.", argv[0]);
-      else if (command->argmin > 0 && argc < command->argmin)
-	mu_error ("%s: too few arguments", argv[0]);
-      else if (command->argmax > 0 && argc > command->argmax)
-	mu_error ("%s: too many arguments", argv[0]);
-      else
-	{
-	  if (command->argmin <= 0 && argc != 2)
-	    {
-	      char *word = mu_str_skip_class (line, MU_CTYPE_SPACE);
-	      char *arg = mu_str_skip_class_comp (word, MU_CTYPE_SPACE);
-	      if (*arg)
-		{
-		  *arg++ = 0;
-		  arg = mu_str_skip_class (arg, MU_CTYPE_SPACE);
-		}
-	      
-	      mu_argcv_free (argc, argv);
-	      argc = 2;
-	      argv = xcalloc (argc + 1, sizeof (argv[0]));
-	      argv[0] = xstrdup (word);
-	      argv[1] = xstrdup (arg);
-	      argv[2] = NULL;
-	    }
-	  status = command->func (argc, argv);
-	}
-    }
-  mu_argcv_free (argc, argv);
-  return status;
-}
-
-/* Look up NAME as the name of a command, and return a pointer to that
-   command.  Return a NULL pointer if NAME isn't a command name. */
-COMMAND *
-find_command (char *name)
-{
-  COMMAND *cp;
-
-  for (cp = commands; cp->name; cp++)
-    if (strcmp (cp->name, name) == 0)
-      return cp;
-
-  return NULL;
-}
-
-static int
-string_to_xlev (const char *name, int *pv)
-{
-  if (strcmp (name, "secure") == 0)
-    *pv = MU_XSCRIPT_SECURE;
-  else if (strcmp (name, "payload") == 0)
-    *pv = MU_XSCRIPT_PAYLOAD;
-  else
-    return 1;
-  return 0;
-}
-
-static int
-change_verbose_mask (int set, int argc, char **argv)
-{
-  int i;
-  
-  for (i = 0; i < argc; i++)
-    {
-      int lev;
-      
-      if (string_to_xlev (argv[i], &lev))
-	{
-	  mu_error ("unknown level: %s", argv[i]);
-	  return 1;
-	}
-      if (set)
-	SET_VERBOSE_MASK (lev);
-      else
-	CLR_VERBOSE_MASK (lev);
-    }
-  return 0;
-}
-
-void
-set_verbose (mu_pop3_t p)
-{
-  if (p)
-    {
-      if (QRY_VERBOSE ())
-	{
-	  mu_pop3_trace (p, MU_POP3_TRACE_SET);
-	}
-      else
-	mu_pop3_trace (p, MU_POP3_TRACE_CLR);
-    }
-}
-
-void
-set_verbose_mask (mu_pop3_t p)
-{
-  if (p)
-    {
-      mu_pop3_trace_mask (p, QRY_VERBOSE_MASK (MU_XSCRIPT_SECURE)
-			          ? MU_POP3_TRACE_SET : MU_POP3_TRACE_CLR,
-			      MU_XSCRIPT_SECURE);
-      mu_pop3_trace_mask (p, QRY_VERBOSE_MASK (MU_XSCRIPT_PAYLOAD)
-			          ? MU_POP3_TRACE_SET : MU_POP3_TRACE_CLR,
-			      MU_XSCRIPT_PAYLOAD);
-    }
-}
-
-int
-com_verbose (int argc, char **argv)
-{
-  if (argc == 1)
-    {
-      if (QRY_VERBOSE ())
-	{
-	  printf ("verbose is on");
-	  if (HAS_VERBOSE_MASK ())
-	    {
-	      char *delim = " (";
-	    
-	      if (QRY_VERBOSE_MASK (MU_XSCRIPT_SECURE))
-		{
-		  printf("%ssecure", delim);
-		  delim = ", ";
-		}
-	      if (QRY_VERBOSE_MASK (MU_XSCRIPT_PAYLOAD))
-		printf("%spayload", delim);
-	      printf (")");
-	    }
-	  printf ("\n");
-	}
-      else
-	printf ("verbose is off\n");
-    }
-  else
-    {
-      int bv;
-
-      if (get_bool (argv[1], &bv) == 0)
-	{
-	  verbose |= bv;
-	  if (argc > 2)
-	    change_verbose_mask (verbose, argc - 2, argv + 2);
-	  set_verbose (pop3);
-	}
-      else if (strcmp (argv[1], "mask") == 0)
-	change_verbose_mask (1, argc - 2, argv + 2);
-      else if (strcmp (argv[1], "unmask") == 0)
-	change_verbose_mask (0, argc - 2, argv + 2);
-      else
-	mu_error ("unknown subcommand");
-      set_verbose_mask (pop3);
-    }
-
-  return 0;
-}
-
-int
-com_user (int argc, char **argv)
-{
-  int status;
-  
-  status = mu_pop3_user (pop3, argv[1]);
-  if (status == 0)
-    username = strdup (argv[1]);
-  return status;
-}
-
-int
-com_apop (int argc, char **argv)
-{
-  int status;
-
-  status = mu_pop3_apop (pop3, argv[1], argv[2]);
-  if (status == 0)
-    {
-      username = strdup (argv[1]);
-      pop_session_status = pop_session_logged_in;
-    }
-  return status;
-}
-
-int
-com_capa (int argc, char **argv)
-{
-  mu_iterator_t iterator = NULL;
-  int status = 0;
-  int reread = 0;
-  int i = 1;
-  
-  for (i = 1; i < argc; i++)
-    {
-      if (strcmp (argv[i], "-reread") == 0)
-	reread = 1;
-      else
-	break;
-    }
-
-  if (i < argc)
-    {
-      if (reread)
-	{
-	  status = mu_pop3_capa (pop3, 1, NULL);
-	  if (status)
-	    return status;
-	}
-      for (; i < argc; i++)
-	{
-	  const char *elt;
-	  int rc = mu_pop3_capa_test (pop3, argv[i], &elt);
-	  switch (rc)
-	    {
-	    case 0:
-	      if (*elt)
-		printf ("%s: %s\n", argv[i], elt);
-	      else
-		printf ("%s is set\n", argv[i]);
-	      break;
-
-	    case MU_ERR_NOENT:
-	      printf ("%s is not set\n", argv[i]);
-	      break;
-
-	    default:
-	      return rc;
-	    }
-	}
-    }
-  else
-    {
-      status = mu_pop3_capa (pop3, reread, &iterator);
-
-      if (status == 0)
-	{
-	  for (mu_iterator_first (iterator);
-	       !mu_iterator_is_done (iterator); mu_iterator_next (iterator))
-	    {
-	      char *capa = NULL;
-	      mu_iterator_current (iterator, (void **) &capa);
-	      printf ("CAPA: %s\n", capa ? capa : "");
-	    }
-	  mu_iterator_destroy (&iterator);
-	}
-    }
-  return status;
-}
-
-int
-com_uidl (int argc, char **argv)
-{
-  int status = 0;
-  if (argc == 1)
-    {
-      mu_iterator_t uidl_iterator = NULL;
-      status = mu_pop3_uidl_all (pop3, &uidl_iterator);
-      if (status == 0)
-	{
-	  for (mu_iterator_first (uidl_iterator);
-	       !mu_iterator_is_done (uidl_iterator);
-	       mu_iterator_next (uidl_iterator))
-	    {
-	      char *uidl = NULL;
-	      mu_iterator_current (uidl_iterator, (void **) &uidl);
-	      printf ("UIDL: %s\n", uidl ? uidl : "");
-	    }
-	  mu_iterator_destroy (&uidl_iterator);
-	}
-    }
-  else
-    {
-      char *uidl = NULL;
-      unsigned int msgno = strtoul (argv[1], NULL, 10);
-      status = mu_pop3_uidl (pop3, msgno, &uidl);
-      if (status == 0)
-	printf ("Msg: %d UIDL: %s\n", msgno, uidl ? uidl : "");
-      free (uidl);
-    }
-  return status;
-}
-
-int
-com_list (int argc, char **argv)
-{
-  int status = 0;
-  if (argc == 1)
-    {
-      mu_iterator_t list_iterator;
-      status = mu_pop3_list_all (pop3, &list_iterator);
-      if (status == 0)
-	{
-	  for (mu_iterator_first (list_iterator);
-	       !mu_iterator_is_done (list_iterator);
-	       mu_iterator_next (list_iterator))
-	    {
-	      char *list = NULL;
-	      mu_iterator_current (list_iterator, (void **) &list);
-	      printf ("LIST: %s\n", (list) ? list : "");
-	    }
-	  mu_iterator_destroy (&list_iterator);
-	}
-    }
-  else
-    {
-      size_t size = 0;
-      unsigned int msgno = strtoul (argv[1], NULL, 10);
-      status = mu_pop3_list (pop3, msgno, &size);
-      if (status == 0)
-	printf ("Msg: %u Size: %lu\n", msgno, (unsigned long) size);
-    }
-  return status;
-}
-
-int
-com_noop (int argc MU_ARG_UNUSED, char **argv MU_ARG_UNUSED)
-{
-  return mu_pop3_noop (pop3);
-}
-
-static void
-echo_off (struct termios *stored_settings)
-{
-  struct termios new_settings;
-  tcgetattr (0, stored_settings);
-  new_settings = *stored_settings;
-  new_settings.c_lflag &= (~ECHO);
-  tcsetattr (0, TCSANOW, &new_settings);
-}
-
-static void
-echo_on (struct termios *stored_settings)
-{
-  tcsetattr (0, TCSANOW, stored_settings);
-}
-
-int
-com_prompt (int argc, char **argv)
-{
-  int quote;
-  size_t size;
-  
-  free (prompt);
-  size = mu_argcv_quoted_length (argv[1], &quote);
-  prompt = malloc (size + 1);
-  if (!prompt)
-    {
-      mu_error ("Memory exhausted");
-      exit (1);
-    }
-  mu_argcv_unquote_copy (prompt, argv[1], size);
-  return 0;
-}
-
-int
-com_pass (int argc, char **argv)
-{
-  int status;
-  char pass[256];
-  char *pwd;
-  
-  if (argc == 1)
-    {
-      struct termios stored_settings;
-
-      printf ("passwd:");
-      fflush (stdout);
-      echo_off (&stored_settings);
-      fgets (pass, sizeof pass, stdin);
-      echo_on (&stored_settings);
-      putchar ('\n');
-      fflush (stdout);
-      pass[strlen (pass) - 1] = '\0';	/* nuke the trailing line.  */
-      pwd = pass;
-    }
-  else
-    pwd = argv[1];
-  status = mu_pop3_pass (pop3, pwd);
-  if (status == 0)
-    pop_session_status = pop_session_logged_in;
-  return status;
-}
-
-int
-com_stat (int argc MU_ARG_UNUSED, char **argv MU_ARG_UNUSED)
-{
-  size_t count = 0;
-  mu_off_t size = 0;
-  int status = 0;
-
-  status = mu_pop3_stat (pop3, &count, &size);
-  printf ("Mesgs: %lu Size %lu\n",
-	  (unsigned long) count, (unsigned long) size);
-  return status;
-}
-
-int
-com_stls (int argc MU_ARG_UNUSED, char **argv MU_ARG_UNUSED)
-{
-  return mu_pop3_stls (pop3);
-}
-
-int
-com_dele (int argc, char **argv)
-{
-  unsigned msgno;
-  msgno = strtoul (argv[1], NULL, 10);
-  return mu_pop3_dele (pop3, msgno);
-}
-
-/* Print out help for ARG, or for all of the commands if ARG is
-   not present. */
-int
-com_help (int argc, char **argv)
-{
-  int i;
-  int printed = 0;
-  char *name = argv[1];
-  
-  for (i = 0; commands[i].name; i++)
-    {
-      if (!name || (strcmp (name, commands[i].name) == 0))
-	{
-	  printf ("%s\t\t%s.\n", commands[i].name, commands[i].doc);
-	  printed++;
-	}
-    }
-
-  if (!printed)
-    {
-      printf ("No commands match `%s'.  Possibilties are:\n", name);
-
-      for (i = 0; commands[i].name; i++)
-	{
-	  /* Print in six columns. */
-	  if (printed == 6)
-	    {
-	      printed = 0;
-	      printf ("\n");
-	    }
-
-	  printf ("%s\t", commands[i].name);
-	  printed++;
-	}
-
-      if (printed)
-	printf ("\n");
-    }
-  return 0;
-}
-
-int
-com_rset (int argc MU_ARG_UNUSED, char **argv MU_ARG_UNUSED)
-{
-  return mu_pop3_rset (pop3);
-}
-
-int
-com_top (int argc, char **argv)
-{
-  mu_stream_t stream;
-  unsigned int msgno;
-  unsigned int lines;
-  int status;
-
-  msgno = strtoul (argv[1], NULL, 10);
-  if (argc == 3)
-    lines = strtoul (argv[2], NULL, 10);
-  else
-    lines = 5;
-
-  status = mu_pop3_top (pop3, msgno, lines, &stream);
-
-  if (status == 0)
-    {
-      size_t n = 0;
-      char buf[128];
-      while ((mu_stream_readline (stream, buf, sizeof buf, &n) == 0) && n)
-	printf ("%s", buf);
-      mu_stream_destroy (&stream);
-    }
-  return status;
-}
-
-int
-com_retr (int argc, char **argv)
-{
-  mu_stream_t stream;
-  unsigned int msgno;
-  int status;
-
-  msgno = strtoul (argv[1], NULL, 10);
-  status = mu_pop3_retr (pop3, msgno, &stream);
-
-  if (status == 0)
-    {
-      size_t n = 0;
-      char buf[128];
-      while ((mu_stream_readline (stream, buf, sizeof buf, &n) == 0) && n)
-	printf ("%s", buf);
-      mu_stream_destroy (&stream);
-    }
-  return status;
-}
-
-int
-com_connect (int argc, char **argv)
-{
-  int status;
-  int n = 0;
-  int tls = 0;
-  int i = 1;
-  
-  for (i = 1; i < argc; i++)
-    {
-      if (strcmp (argv[i], "-tls") == 0)
-	{
-	  if (WITH_TLS)
-	    tls = 1;
-	  else
-	    {
-	      mu_error ("TLS not supported");
-	      return 0;
-	    }
-	}
-      else
-	break;
-    }
-
-  argc -= i;
-  argv += i;
-  
-  if (argc >= 2)
-    {
-      if (get_port (argv[1], &n))
-	return 0;
-    }
-  else if (tls)
-    n = MU_POP3_DEFAULT_SSL_PORT;
-  else
-    n = MU_POP3_DEFAULT_PORT;
-  
-  if (pop_session_status != pop_session_disconnected)
-    com_disconnect (0, NULL);
-  
-  status = mu_pop3_create (&pop3);
-  if (status == 0)
-    {
-      mu_stream_t tcp;
-
-      if (QRY_VERBOSE ())
-	{
-	  set_verbose (pop3);
-	  set_verbose_mask (pop3);
-	}
-      status = mu_tcp_stream_create (&tcp, argv[0], n, MU_STREAM_READ);
-      if (status == 0)
-	{
-#ifdef WITH_TLS
-	  if (tls)
-	    {
-	      mu_stream_t tlsstream;
-	      
-	      status = mu_tls_client_stream_create (&tlsstream, tcp, tcp, 0);
-	      mu_stream_unref (tcp);
-	      if (status)
-		{
-		  mu_error ("cannot create TLS stream: %s",
-			    mu_strerror (status));
-		  return 0;
-		}
-	      tcp = tlsstream;
-	    }
-#endif
-	  mu_pop3_set_carrier (pop3, tcp);
-	  status = mu_pop3_connect (pop3);
-	}
-      else
-	{
-	  mu_pop3_destroy (&pop3);
-	  pop3 = NULL;
-	}
-    }
-
-  if (status)
-    mu_error ("Failed to create pop3: %s", mu_strerror (status));
-  else
-    {
-      connect_argc = argc;
-      connect_argv = xcalloc (argc, sizeof (*connect_argv));
-      for (i = 0; i < argc; i++)
-	connect_argv[i] = xstrdup (argv[i]);
-      connect_argv[i] = NULL;
-      port = n;
-      pop_session_status = pop_session_connected;
-    }
-  
-  return status;
-}
-
-int
-com_disconnect (int argc MU_ARG_UNUSED, char **argv MU_ARG_UNUSED)
-{
-  if (pop3)
-    {
-      mu_pop3_disconnect (pop3);
-      mu_pop3_destroy (&pop3);
-      pop3 = NULL;
-      
-      mu_argcv_free (connect_argc, connect_argv);
-      connect_argc = 0;
-      connect_argv = NULL;
-      pop_session_status = pop_session_disconnected;
-    }
-  return 0;
-}
-
-int
-com_quit (int argc MU_ARG_UNUSED, char **argv MU_ARG_UNUSED)
-{
-  int status = 0;
-  if (pop3)
-    {
-      if (mu_pop3_quit (pop3) == 0)
-	{
-	  status = com_disconnect (0, NULL);
-	}
-      else
-	{
-	  printf ("Try 'exit' to leave %s\n", mu_program_name);
-	}
-    }
-  else
-    printf ("Try 'exit' to leave %s\n", mu_program_name);
-  return status;
-}
-
-int
-com_exit (int argc MU_ARG_UNUSED, char **argv MU_ARG_UNUSED)
-{
-  if (pop3)
-    {
-      mu_pop3_disconnect (pop3);
-      mu_pop3_destroy (&pop3);
-    }
-  done = 1;
-  return 0;
-}
-
-
-static char *
-input_line_interactive ()
-{
-  char *p = expand_prompt ();
-  char *line = readline (p);
-  free (p);
-  return line;
-}
-
-static char *
-input_line_script ()
-{
-  size_t size = 0;
-  char *buf = NULL;
-  if (getline (&buf, &size, stdin) <= 0)
-    return NULL;
-  return buf;
-}
-
-int
-main (int argc MU_ARG_UNUSED, char **argv)
-{
-  char *line, *s;
-  int interactive = isatty (0);
-  char *(*input_line) () = interactive ?
-                             input_line_interactive : input_line_script;
-
-  mu_set_program_name (argv[0]);
-  prompt = strdup (DEFAULT_PROMPT);
-  if (interactive)
-    initialize_readline ();	/* Bind our completer. */
-
-#ifdef WITH_TLS
-  mu_init_tls_libs ();
-#endif  
-  /* Loop reading and executing lines until the user quits. */
-  while (!done)
-    {
-      line = input_line ();
-      if (!line)
-	break;
-
-      /* Remove leading and trailing whitespace from the line.
-         Then, if there is anything left, add it to the history list
-         and execute it. */
-      s = mu_str_stripws (line);
-
-      if (*s)
-	{
-	  int status;
-
-#ifdef WITH_READLINE
-	  if (interactive)
-	    {
-	      if (retrieve_history (s))
-		continue;
-	      add_history (s);
-	    }
-#endif
-
-	  status = execute_line (s);
-	  if (status != 0)
-	    mu_error ("Error: %s", mu_strerror (status));
-	}
-
-      free (line);
-    }
-  if (interactive)
-    finish_readline ();
-  exit (0);
-}
-
diff --git a/imap4d/fetch.c b/imap4d/fetch.c
index d0475cf..7f38e29 100644
--- a/imap4d/fetch.c
+++ b/imap4d/fetch.c
@@ -162,12 +162,21 @@ fetch_send_header_address (mu_header_t header, const char *name,
     fetch_send_address (defval);
 }
 
+static void
+imap4d_ws_alloc_die (struct mu_wordsplit *wsp)
+{
+  imap4d_bye (ERR_NO_MEM);
+}
+
+#define IMAP4D_WS_FLAGS \
+  (MU_WRDSF_DEFFLAGS | MU_WRDSF_DELIM | \
+   MU_WRDSF_ENOMEMABRT | MU_WRDSF_ALLOC_DIE)
+
 /* Send parameter list for the bodystructure.  */
 static void
 send_parameter_list (const char *buffer)
 {
-  int argc = 0;
-  char **argv;
+  struct mu_wordsplit ws;
   
   if (!buffer)
     {
@@ -175,9 +184,16 @@ send_parameter_list (const char *buffer)
       return;
     }
 
-  mu_argcv_get (buffer, " \t\r\n;=", NULL, &argc, &argv);
+  ws.ws_delim = " \t\r\n;=";
+  ws.ws_alloc_die = imap4d_ws_alloc_die;
+  if (mu_wordsplit (buffer, &ws, IMAP4D_WS_FLAGS))
+    {
+      mu_error (_("%s failed: %s"), "mu_wordsplit",
+		mu_wordsplit_strerror (&ws));
+      return; /* FIXME: a better error handling, maybe? */
+    }
   
-  if (argc == 0)
+  if (ws.ws_wordc == 0)
     io_sendf ("NIL");
   else
     {
@@ -185,16 +201,16 @@ send_parameter_list (const char *buffer)
       
       io_sendf ("(");
         
-      p = argv[0];
+      p = ws.ws_wordv[0];
       io_send_qstring (p);
 
-      if (argc > 1)
+      if (ws.ws_wordc > 1)
 	{
 	  int i, space = 0;
 	  char *lvalue = NULL;
 
 	  io_sendf ("(");
-	  for (i = 1; i < argc; i++)
+	  for (i = 1; i < ws.ws_wordc; i++)
 	    {
 	      if (lvalue)
 		{
@@ -205,22 +221,21 @@ send_parameter_list (const char *buffer)
 		  space = 1;
 		}
 	      
-	      switch (argv[i][0])
+	      switch (ws.ws_wordv[i][0])
 		{
 		case ';':
 		  continue;
 		  
 		case '=':
-		  if (++i < argc)
+		  if (++i < ws.ws_wordc)
 		    {
-		      char *p = argv[i];
 		      io_sendf (" ");
-		      io_send_qstring (p);
+		      io_send_qstring (ws.ws_wordv[i]);
 		    }
 		  break;
 		  
 		default:
-		  lvalue = argv[i];
+		  lvalue = ws.ws_wordv[i];
 		}
 	    }
 	  if (lvalue)
@@ -235,7 +250,7 @@ send_parameter_list (const char *buffer)
 	io_sendf (" NIL");
       io_sendf (")");
     }
-  mu_argcv_free (argc, argv);
+  mu_wordsplit_free (&ws);
 }
 
 static void
@@ -352,27 +367,33 @@ bodystructure (mu_message_t msg, int extension)
 
   if (mu_header_aget_value (header, MU_HEADER_CONTENT_TYPE, &buffer) == 0)
     {
-      int argc = 0;
-      char **argv;
+      struct mu_wordsplit ws;
       char *s, *p;
 	  
-      mu_argcv_get (buffer, " \t\r\n;=", NULL, &argc, &argv);
+      ws.ws_delim = " \t\r\n;=";
+      ws.ws_alloc_die = imap4d_ws_alloc_die;
+      if (mu_wordsplit (buffer, &ws, IMAP4D_WS_FLAGS))
+	{
+	  mu_error (_("%s failed: %s"), "mu_wordsplit",
+		    mu_wordsplit_strerror (&ws));
+	  return RESP_BAD; /* FIXME: a better error handling, maybe? */
+	}
 
-      if (mu_c_strcasecmp (argv[0], "MESSAGE/RFC822") == 0)
+      if (mu_c_strcasecmp (ws.ws_wordv[0], "MESSAGE/RFC822") == 0)
         message_rfc822 = 1;
-      else if (mu_c_strcasecmp (argv[0], "TEXT/PLAIN") == 0)
+      else if (mu_c_strcasecmp (ws.ws_wordv[0], "TEXT/PLAIN") == 0)
         text_plain = 1;
 
-      s = strchr (argv[0], '/');
+      s = strchr (ws.ws_wordv[0], '/');
       if (s)
 	*s++ = 0;
-      p = argv[0];
+      p = ws.ws_wordv[0];
       io_send_qstring (p);
       io_sendf (" ");
       io_send_qstring (s);
 
       /* body parameter parenthesized list: Content-type attributes */
-      if (argc > 1 || text_plain)
+      if (ws.ws_wordc > 1 || text_plain)
 	{
 	  int space = 0;
 	  char *lvalue = NULL;
@@ -380,7 +401,7 @@ bodystructure (mu_message_t msg, int extension)
 	  int i;
 	  
 	  io_sendf (" (");
-	  for (i = 1; i < argc; i++)
+	  for (i = 1; i < ws.ws_wordc; i++)
 	    {
 	      /* body parameter parenthesized list:
 		 Content-type parameter list. */
@@ -393,25 +414,23 @@ bodystructure (mu_message_t msg, int extension)
 		  space = 1;
 		}
 	      
-	      switch (argv[i][0])
+	      switch (ws.ws_wordv[i][0])
 		{
 		case ';':
 		  continue;
 		  
 		case '=':
-		  if (++i < argc)
+		  if (++i < ws.ws_wordc)
 		    {
-		      char *p = argv[i];
 		      io_sendf (" ");
-		      io_send_qstring (p);
+		      io_send_qstring (ws.ws_wordv[i]);
 		    }
 		  break;
 		  
 		default:
-		  lvalue = argv[i];
+		  lvalue = ws.ws_wordv[i];
 		  if (mu_c_strcasecmp (lvalue, "charset") == 0)
 		    have_charset = 1;
-
 		}
 	    }
 	  
@@ -432,7 +451,7 @@ bodystructure (mu_message_t msg, int extension)
 	}
       else
 	io_sendf (" NIL");
-      mu_argcv_free (argc, argv);
+      mu_wordsplit_free (&ws);
       free (buffer);
     }
   else
@@ -553,13 +572,19 @@ fetch_bodystructure0 (mu_message_t message, int extension)
       /* The subtype.  */
       if (mu_header_aget_value (header, MU_HEADER_CONTENT_TYPE, &buffer) == 0)
 	{
-	  int argc = 0;
-	  char **argv;
+	  struct mu_wordsplit ws;
 	  char *s;
-	  
-	  mu_argcv_get (buffer, " \t\r\n;=", NULL, &argc, &argv);
 
-	  s = strchr (argv[0], '/');
+	  ws.ws_delim = " \t\r\n;=";
+	  ws.ws_alloc_die = imap4d_ws_alloc_die;
+	  if (mu_wordsplit (buffer, &ws, IMAP4D_WS_FLAGS))
+	    {
+	      mu_error (_("%s failed: %s"), "mu_wordsplit",
+			mu_wordsplit_strerror (&ws));
+	      return RESP_BAD; /* FIXME: a better error handling, maybe? */
+	    }
+
+	  s = strchr (ws.ws_wordv[0], '/');
 	  if (s)
 	    s++;
 	  io_sendf (" ");
@@ -572,7 +597,7 @@ fetch_bodystructure0 (mu_message_t message, int extension)
 	      char *lvalue = NULL;
 	      
 	      io_sendf (" (");
-	      for (i = 1; i < argc; i++)
+	      for (i = 1; i < ws.ws_wordc; i++)
 		{
 		  /* body parameter parenthesized list:
 		     Content-type parameter list. */
@@ -585,22 +610,21 @@ fetch_bodystructure0 (mu_message_t message, int extension)
 		      space = 1;
 		    }
 
-		  switch (argv[i][0])
+		  switch (ws.ws_wordv[i][0])
 		    {
 		    case ';':
 		      continue;
 		      
 		    case '=':
-		      if (++i < argc)
+		      if (++i < ws.ws_wordc)
 			{
-			  char *p = argv[i];
 			  io_sendf (" ");
-			  io_send_qstring (p);
+			  io_send_qstring (ws.ws_wordv[i]);
 			}
 		      break;
 		      
 		    default:
-		      lvalue = argv[i];
+		      lvalue = ws.ws_wordv[i];
 		    }
 		}
 	      if (lvalue)
@@ -613,7 +637,7 @@ fetch_bodystructure0 (mu_message_t message, int extension)
 	    }
 	  else
 	    io_sendf (" NIL");
-	  mu_argcv_free (argc, argv);
+	  mu_wordsplit_free (&ws);
           free (buffer);
 	}
       else
diff --git a/imap4d/imap4d.h b/imap4d/imap4d.h
index 21d3b72..26e9fc3 100644
--- a/imap4d/imap4d.h
+++ b/imap4d/imap4d.h
@@ -98,7 +98,7 @@
 #include <mailutils/pam.h>
 #include <mailutils/acl.h>
 #include <mailutils/server.h>
-#include <mailutils/argcv.h>
+#include <mailutils/wordsplit.h>
 #include <mailutils/alloc.h>
 #include <mailutils/vartab.h>
 #include <mailutils/cctype.h>
diff --git a/include/mailutils/Makefile.am b/include/mailutils/Makefile.am
index 6f0a67f..787d04e 100644
--- a/include/mailutils/Makefile.am
+++ b/include/mailutils/Makefile.am
@@ -94,7 +94,8 @@ pkginclude_HEADERS = \
  tls.h\
  url.h\
  vartab.h\
- version.h
+ version.h\
+ wordsplit.h
 
 if MU_COND_SUPPORT_CXX
   CPP_DIR = cpp
diff --git a/include/mailutils/argcv.h b/include/mailutils/argcv.h
index dd9482f..49d3bfa 100644
--- a/include/mailutils/argcv.h
+++ b/include/mailutils/argcv.h
@@ -28,31 +28,51 @@
 extern "C" {
 #endif
 
-#define MU_ARGCV_RETURN_DELIMS 0x01
+void mu_argcv_free (size_t argc, char **argv);
+void mu_argv_free (char **argv);
+
+enum mu_argcv_escape
+  {
+    mu_argcv_escape_no,
+    mu_argcv_escape_c
+    /*    mu_argcv_escape_sh */
+  };
+int mu_argcv_join (int argc, char **argv, char *delim,
+		   enum mu_argcv_escape esc,
+		   char **pstring);
+int mu_argcv_string (int argc, char **argv, char **string);
+
+void mu_argcv_remove (int *pargc, char ***pargv,
+		      int (*sel) (const char *, void *), void *);
   
-extern int mu_argcv_get    (const char *command, const char *delim,
-			    const char *cmnt,
-			    int *argc, char ***argv);
-extern int mu_argcv_get_n (const char *command, int len,
-		        const char *delim, const char *cmnt,
-			int *argc, char ***argv);
-extern int mu_argcv_get_np (const char *command, int len,
-			    const char *delim, const char *cmnt,
-			    int flags,
-			    int *pargc, char ***pargv, char **endp);
+/* Deprecated interfaces */  
+#define MU_ARGCV_RETURN_DELIMS 0x01
+
+#ifndef MU_ARCGV_DEPRECATED
+# define MU_ARCGV_DEPRECATED __attribute__((deprecated))
+#endif  
   
-extern int mu_argcv_string (int argc, char **argv, char **string);
-extern void mu_argcv_free   (int argc, char **argv);
-extern void mu_argv_free (char **argv);
-
-extern int mu_argcv_unquote_char (int c);
-extern int mu_argcv_quote_char   (int c);
-extern size_t mu_argcv_quoted_length (const char *str, int *quote);
-extern void mu_argcv_unquote_copy (char *dst, const char *src, size_t n);
-extern void mu_argcv_quote_copy (char *dst, const char *src);
-extern void mu_argcv_remove (int *pargc, char ***pargv,
-			     int (*sel) (const char *, void *), void *);
+int mu_argcv_get    (const char *command, const char *delim,
+		     const char *cmnt,
+		     int *argc, char ***argv) MU_ARCGV_DEPRECATED;
+int mu_argcv_get_n (const char *command, int len,
+		    const char *delim, const char *cmnt,
+		    int *argc, char ***argv) MU_ARCGV_DEPRECATED;
+int mu_argcv_get_np (const char *command, int len,
+		     const char *delim, const char *cmnt,
+		     int flags,
+		     int *pargc, char ***pargv, char **endp)
+               MU_ARCGV_DEPRECATED;
   
+int mu_argcv_unquote_char (int c) MU_ARCGV_DEPRECATED;
+int mu_argcv_quote_char   (int c) MU_ARCGV_DEPRECATED;
+size_t mu_argcv_quoted_length (const char *str, int *quote)
+                           MU_ARCGV_DEPRECATED;
+void mu_argcv_unquote_copy (char *dst, const char *src, size_t n)
+                           MU_ARCGV_DEPRECATED;
+void mu_argcv_quote_copy (char *dst, const char *src)
+                           MU_ARCGV_DEPRECATED;
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/include/mailutils/mailutils.h b/include/mailutils/mailutils.h
index cad14b6..cec169f 100644
--- a/include/mailutils/mailutils.h
+++ b/include/mailutils/mailutils.h
@@ -64,5 +64,6 @@
 #include <mailutils/secret.h>
 #include <mailutils/cctype.h>
 #include <mailutils/cstr.h>
+#include <mailutils/wordsplit.h>
 
 /* EOF */
diff --git a/include/mailutils/wordsplit.h b/include/mailutils/wordsplit.h
new file mode 100644
index 0000000..13edb25
--- /dev/null
+++ b/include/mailutils/wordsplit.h
@@ -0,0 +1,135 @@
+/* wordsplit - a word splitter
+   Copyright (C) 2009, 2010 Sergey Poznyakoff
+
+   This program 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 of the License, 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, see <http://www.gnu.org/licenses/>. */
+
+#ifndef __MAILUTILS_WORDSPLIT_H
+#define __MAILUTILS_WORDSPLIT_H
+
+#include <stddef.h>
+
+struct mu_wordsplit
+{
+  size_t ws_wordc;
+  char **ws_wordv;
+  size_t ws_offs;
+  size_t ws_wordn;
+  int ws_flags;
+  const char *ws_delim;
+  const char *ws_comment;
+  void (*ws_alloc_die) (struct mu_wordsplit *wsp);
+  void (*ws_error) (const char *, ...)
+		    __attribute__ ((__format__ (__printf__, 1, 2)));
+  void (*ws_debug) (const char *, ...)
+		    __attribute__ ((__format__ (__printf__, 1, 2)));
+	
+  const char **ws_env;
+  char *(*ws_getvar) (const char *, size_t); 
+	
+  const char *ws_input;
+  size_t ws_len;
+  size_t ws_endp;
+  int ws_errno;
+  struct mu_wordsplit_node *ws_head, *ws_tail;
+};
+
+/* Append  the words found to the array resulting from a previous
+   call. */
+#define MU_WRDSF_APPEND            0x0000001
+/* Insert we_offs initial NULLs in the array ws_wordv.
+   (These are not counted in the returned ws_wordc.) */
+#define MU_WRDSF_DOOFFS            0x0000002
+/* Don't do command substitution. Reserved for future use. */
+#define MU_WRDSF_NOCMD             0x0000004
+/* The parameter p resulted from a previous call to
+   mu_wordsplit(), and mu_wordsplit_free() was not called. Reuse the
+   allocated storage. */
+#define MU_WRDSF_REUSE             0x0000008
+/* Print errors */
+#define MU_WRDSF_SHOWERR           0x0000010
+/* Consider it an error if an undefined shell variable
+   is expanded. */
+#define MU_WRDSF_UNDEF             0x0000020
+
+/* Don't do variable expansion. */
+#define MU_WRDSF_NOVAR             0x0000040
+/* Abort on ENOMEM error */
+#define MU_WRDSF_ENOMEMABRT        0x0000080
+/* Trim off any leading and trailind whitespace */
+#define MU_WRDSF_WS                0x0000100
+/* Handle quotes and escape directives */
+#define MU_WRDSF_QUOTE             0x0000200
+/* Replace each input sequence of repeated delimiters with a single
+   delimiter */
+#define MU_WRDSF_SQUEEZE_DELIMS    0x0000400
+/* Return delimiters */
+#define MU_WRDSF_RETURN_DELIMS     0x0000800
+/* Treat sed expressions as words */
+#define MU_WRDSF_SED_EXPR          0x0001000
+/* ws_delim field is initialized */
+#define MU_WRDSF_DELIM             0x0002000
+/* ws_comment field is initialized */
+#define MU_WRDSF_COMMENT           0x0004000
+/* ws_alloc_die field is initialized */
+#define MU_WRDSF_ALLOC_DIE         0x0008000
+/* ws_error field is initialized */
+#define MU_WRDSF_ERROR             0x0010000
+/* ws_debug field is initialized */
+#define MU_WRDSF_DEBUG             0x0020000
+/* ws_env field is initialized */
+#define MU_WRDSF_ENV               0x0040000
+/* ws_getvar field is initialized */
+#define MU_WRDSF_GETVAR            0x0080000
+/* enable debugging */
+#define MU_WRDSF_SHOWDBG           0x0100000
+/* Don't split input into words.  Useful for side effects. */
+#define MU_WRDSF_NOSPLIT           0x0200000
+/* Keep undefined variables in place, instead of expanding them to
+   empty string */
+#define MU_WRDSF_KEEPUNDEF         0x0400000
+/* Warn about undefined variables */
+#define MU_WRDSF_WARNUNDEF         0x0800000
+/* Handle C escapes */
+#define MU_WRDSF_CESCAPES          0x1000000
+
+#define MU_WRDSF_DEFFLAGS	       \
+  (MU_WRDSF_NOVAR | MU_WRDSF_NOCMD | \
+   MU_WRDSF_QUOTE | MU_WRDSF_SQUEEZE_DELIMS | MU_WRDSF_CESCAPES)
+
+#define MU_WRDSE_EOF        0
+#define MU_WRDSE_QUOTE      1
+#define MU_WRDSE_NOSPACE    2
+#define MU_WRDSE_NOSUPP     3
+#define MU_WRDSE_USAGE      4
+#define MU_WRDSE_CBRACE     5
+#define MU_WRDSE_UNDEF      6
+
+int mu_wordsplit (const char *s, struct mu_wordsplit *p, int flags);
+int mu_wordsplit_len (const char *s, size_t len,
+		      struct mu_wordsplit *p, int flags);
+void mu_wordsplit_free (struct mu_wordsplit *p);
+
+int mu_wordsplit_c_unquote_char (int c);
+int mu_wordsplit_c_quote_char (int c);
+size_t mu_wordsplit_c_quoted_length (const char *str, int quote_hex,
+				     int *quote);
+void mu_wordsplit_sh_unquote_copy (char *dst, const char *src, size_t n);
+void mu_wordsplit_c_unquote_copy (char *dst, const char *src, size_t n);
+void mu_wordsplit_c_quote_copy (char *dst, const char *src, int quote_hex);
+
+void mu_wordsplit_perror (struct mu_wordsplit *ws);
+const char *mu_wordsplit_strerror (struct mu_wordsplit *ws);
+
+
+#endif
diff --git a/lib/mailcap.c b/lib/mailcap.c
index fc2c3ec..c0396d2 100644
--- a/lib/mailcap.c
+++ b/lib/mailcap.c
@@ -414,21 +414,31 @@ create_filter (char *cmd, int outfd, int *infd)
   if (pid == 0)
     {
       /* Child process */
+      struct mu_wordsplit ws;
       int argc;
       char **argv;
-
+      
       if (need_shell_p (cmd))
 	{
+	  char *x_argv[4];
 	  argc = 3;
-	  argv = xmalloc ((argc + 1) * sizeof *argv);
+	  argv = x_argv;
 	  argv[0] = getenv ("SHELL");
 	  argv[1] = "-c";
 	  argv[2] = cmd;
 	  argv[3] = NULL;
 	}
       else
-	mu_argcv_get (cmd, "", NULL, &argc, &argv);
-      
+	{
+	  if (mu_wordsplit (cmd, &ws, MU_WRDSF_DEFFLAGS))
+	    {
+	      mu_error (_("%s failed: %s"), "mu_wordsplit",
+			mu_wordsplit_strerror (&ws));
+	      _exit (127);
+	    }
+	  argc = ws.ws_wordc;
+	  argv = ws.ws_wordv;
+	}      
       /* Create input channel: */
       if (infd)
 	{
@@ -440,7 +450,7 @@ create_filter (char *cmd, int outfd, int *infd)
       /* Create output channel */
       if (outfd != -1 && outfd != 1)
 	dup2 (outfd, 1);
-
+      
       execvp (argv[0], argv);
       mu_error (_("cannot execute `%s': %s"), cmd, mu_strerror (errno));
       _exit (127);
@@ -491,8 +501,7 @@ run_test (mu_mailcap_entry_t entry, struct mime_context *ctx)
   
   if (mu_mailcap_entry_get_test (entry, NULL, 0, &size) == 0)
     {
-      int argc;
-      char **argv;
+      struct mu_wordsplit ws;
       char *str;
 
       obstack_blank (&expand_stack, size + 1);
@@ -500,11 +509,16 @@ run_test (mu_mailcap_entry_t entry, struct mime_context *ctx)
       mu_mailcap_entry_get_test (entry, str, size + 1, NULL);
 
       expand_string (ctx, &str);
-      mu_argcv_get (str, "", NULL, &argc, &argv);
+      if (mu_wordsplit (str, &ws, MU_WRDSF_DEFFLAGS))
+	{
+	  mu_error (_("%s failed: %s"), "mu_wordsplit",
+		    mu_wordsplit_strerror (&ws));
+	  return 1;
+	}
       
-      if (mu_spawnvp (argv[0], argv, &status))
+      if (mu_spawnvp (ws.ws_wordv[0], ws.ws_wordv, &status))
 	status = 1;
-      mu_argcv_free (argc, argv);
+      mu_wordsplit_free (&ws);
     }
   return status;
 }
diff --git a/libmailutils/auth/mu_auth.c b/libmailutils/auth/mu_auth.c
index f9f33ca..f4ea60e 100644
--- a/libmailutils/auth/mu_auth.c
+++ b/libmailutils/auth/mu_auth.c
@@ -40,7 +40,7 @@
 # include <crypt.h>
 #endif
 
-#include <mailutils/argcv.h>
+#include <mailutils/wordsplit.h>
 #include <mailutils/list.h>
 #include <mailutils/iterator.h>
 #include <mailutils/mailbox.h>
@@ -376,32 +376,32 @@ _locate (const char *name)
 static void
 _add_module_list (const char *modlist, int (*fun)(const char *name))
 {
-  int argc;
-  char **argv;
-  int rc, i;
-  
-  rc = mu_argcv_get (modlist, ":", NULL, &argc, &argv);
-  if (rc)
+  struct mu_wordsplit ws;
+  int i;
+
+  ws.ws_delim = ":";
+  if (mu_wordsplit (modlist, &ws, MU_WRDSF_DEFFLAGS|MU_WRDSF_DELIM))
     {
-      mu_error (_("cannot split line `%s': %s"), modlist, mu_strerror (rc));
+      mu_error (_("cannot split line `%s': %s"), modlist,
+		mu_wordsplit_strerror (&ws));
       exit (1);
     }
 
-  for (i = 0; i < argc; i += 2)
+  for (i = 0; i < ws.ws_wordc; i++)
     {
-      if (fun (argv[i]))
+      if (fun (ws.ws_wordv[i]))
         {
           /* Historically,auth functions used ENOENT. We support this
              return value for backward compatibility. */
           if (errno == ENOENT || errno == MU_ERR_NOENT)
-            mu_error (_("no such module: %s"), argv[i]);
+            mu_error (_("no such module: %s"), ws.ws_wordv[i]);
           else
             mu_error (_("failed to add module %s: %s"),
-                      argv[i], strerror (errno));
+                      ws.ws_wordv[i], strerror (errno));
           exit (1);
         }
     }
-  mu_argcv_free (argc, argv);
+  mu_wordsplit_free (&ws);
 }
 
 
diff --git a/libmailutils/base/Makefile.am b/libmailutils/base/Makefile.am
index c51c947..b4f208e 100644
--- a/libmailutils/base/Makefile.am
+++ b/libmailutils/base/Makefile.am
@@ -21,6 +21,9 @@ libbase_la_SOURCES = \
  alloc.c\
  amd.c\
  argcv.c\
+ argcvfree.c\
+ argcvjoin.c\
+ argcvrem.c\
  assoc.c\
  daemon.c\
  date.c\
diff --git a/libmailutils/base/argcv.c b/libmailutils/base/argcv.c
index 6e63b23..142de6b 100644
--- a/libmailutils/base/argcv.c
+++ b/libmailutils/base/argcv.c
@@ -22,15 +22,13 @@
 
 #include <ctype.h>
 #include <errno.h>
+#define MU_ARCGV_DEPRECATED
 #include <mailutils/argcv.h>
 
 /* Keep mailutils namespace clean */
 #define argcv_get            mu_argcv_get 
 #define argcv_get_n          mu_argcv_get_n 
 #define argcv_get_np         mu_argcv_get_np 
-#define argcv_string         mu_argcv_string
-#define argcv_free           mu_argcv_free
-#define argv_free            mu_argv_free 
 #define argcv_unquote_char   mu_argcv_unquote_char
 #define argcv_quote_char     mu_argcv_quote_char  
 #define argcv_quoted_length  mu_argcv_quoted_length
@@ -404,7 +402,7 @@ argcv_get_np (const char *command, int len,
       argv[i] = calloc (n + 1,  sizeof (char));
       if (argv[i] == NULL)
 	{
-	  argcv_free (i, argv);
+	  mu_argcv_free (i, argv);
 	  return ENOMEM;
 	}
       if (unquote)
@@ -438,111 +436,5 @@ argcv_get (const char *command, const char *delim, const char *cmnt,
 }
 
 
-/*
- * frees all elements of an argv array
- * argc is the number of elements
- * argv is the array
- */
-void
-argcv_free (int argc, char **argv)
-{
-  if (argc <= 0)
-    return;
-  while (--argc >= 0)
-    if (argv[argc])
-      free (argv[argc]);
-  free (argv);
-}
-
-void
-argv_free (char **argv)
-{
-  int i;
-
-  for (i = 0; argv[i]; i++)
-    free (argv[i]);
-  free (argv);
-}
-
-/* Make a argv an make string separated by ' '.  */
-
-int
-argcv_string (int argc, char **argv, char **pstring)
-{
-  size_t i, j, len;
-  char *buffer;
-
-  /* No need.  */
-  if (pstring == NULL)
-    return EINVAL;
-
-  buffer = malloc (1);
-  if (buffer == NULL)
-    return ENOMEM;
-  *buffer = '\0';
-
-  for (len = i = j = 0; i < argc; i++)
-    {
-      int quote;
-      int toklen;
-
-      toklen = argcv_quoted_length (argv[i], &quote);
-      
-      len += toklen + 2;
-      if (quote)
-	len += 2;
-      
-      buffer = realloc (buffer, len);
-      if (buffer == NULL)
-        return ENOMEM;
-
-      if (i != 0)
-	buffer[j++] = ' ';
-      if (quote)
-	buffer[j++] = '"';
-      argcv_quote_copy (buffer + j, argv[i]);
-      j += toklen;
-      if (quote)
-	buffer[j++] = '"';
-    }
-
-  for (; j > 0 && isspace (buffer[j-1]); j--)
-    ;
-  buffer[j] = 0;
-  if (pstring)
-    *pstring = buffer;
-  return 0;
-}
-
-void
-mu_argcv_remove (int *pargc, char ***pargv,
-		 int (*sel) (const char *, void *), void *data)
-{
-  int i, j;
-  int argc = *pargc;
-  char **argv = *pargv;
-  int cnt = 0;
-  
-  for (i = j = 0; i < argc; i++)
-    {
-      if (sel (argv[i], data))
-	{
-	  free (argv[i]);
-	  cnt++;
-	}
-      else
-	{
-	  if (i != j)
-	    argv[j] = argv[i];
-	  j++;
-	}
-    }
-  if (i != j)
-    argv[j] = NULL;
-  argc -= cnt;
-
-  *pargc = argc;
-  *pargv = argv;
-}
       
  
diff --git a/libmailutils/url/urlstr.c b/libmailutils/base/argcvfree.c
similarity index 59%
copy from libmailutils/url/urlstr.c
copy to libmailutils/base/argcvfree.c
index 31e5197..737d67d 100644
--- a/libmailutils/url/urlstr.c
+++ b/libmailutils/base/argcvfree.c
@@ -1,5 +1,6 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 2010 Free Software Foundation, Inc.
+/* Free an array of string pointers.
+   Copyright (C) 1999, 2000, 2001, 2003, 2004, 2005, 2006, 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
@@ -20,12 +21,31 @@
 #endif
 #include <stdlib.h>
 #include <mailutils/types.h>
-#include <mailutils/sys/url.h>
+#include <mailutils/argcv.h>
 
-const char *
-mu_url_to_string (const mu_url_t url)
+/*
+ * frees all elements of an argv array
+ * argc is the number of elements
+ * argv is the array
+ */
+void
+mu_argcv_free (size_t argc, char **argv)
 {
-  if (url == NULL || url->name == NULL)
-    return "";
-  return url->name;
+  size_t i;
+
+  for (i = 0; i < argc; i++)
+    if (argv[i])
+      free (argv[i]);
+  free (argv);
+}
+
+void
+mu_argv_free (char **argv)
+{
+  int i;
+
+  for (i = 0; argv[i]; i++)
+    free (argv[i]);
+  free (argv);
 }
+
diff --git a/libmailutils/base/argcvjoin.c b/libmailutils/base/argcvjoin.c
new file mode 100644
index 0000000..a5877e8
--- /dev/null
+++ b/libmailutils/base/argcvjoin.c
@@ -0,0 +1,116 @@
+/* Join strings from an array into a single string.
+   Copyright (C) 1999, 2000, 2001, 2003, 2004, 2005, 2006, 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, see 
+   <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+#include <stdlib.h>
+#include <mailutils/types.h>
+#include <mailutils/argcv.h>
+#include <mailutils/errno.h>
+#include <mailutils/wordsplit.h>
+
+int
+mu_argcv_join (int argc, char **argv, char *delim, enum mu_argcv_escape esc,
+	       char **pstring)
+{
+  size_t i, j, len;
+  char *buffer;
+  size_t delimlen = strlen (delim);
+  int quote_hex = 0;
+  
+  if (pstring == NULL)
+    return EINVAL;
+
+  buffer = malloc (1);
+  if (buffer == NULL)
+    return ENOMEM;
+  *buffer = '\0';
+
+  for (len = i = j = 0; i < argc; i++)
+    {
+      int quote;
+      int toklen;
+
+      switch (esc)
+	{
+	case mu_argcv_escape_no:
+	  toklen = strlen (argv[i]);
+	  quote = 0;
+	  break;
+
+	case mu_argcv_escape_c:
+	  toklen = mu_wordsplit_c_quoted_length (argv[i], quote_hex, &quote);
+	  break;
+
+#if 0
+	case mu_argcv_escape_sh:
+	  FIXME
+	  toklen = mu_wordsplit_sh_quoted_length (argv[i], &quote);
+	  break;
+#endif
+	default:
+	  return EINVAL;
+	}
+	
+      len += toklen + delimlen;
+      if (quote)
+	len += 2;
+      
+      buffer = realloc (buffer, len);
+      if (buffer == NULL)
+        return ENOMEM;
+
+      if (i != 0)
+	{
+	  memcpy (buffer + j, delim, delimlen);
+	  j += delimlen;
+	}
+      if (quote)
+	buffer[j++] = '"';
+      
+      switch (esc)
+	{
+	case mu_argcv_escape_no:
+	  memcpy (buffer + j, argv[i], toklen);
+	  break;
+
+	case mu_argcv_escape_c:
+	  mu_wordsplit_c_quote_copy (buffer + j, argv[i], quote_hex);
+	  break;
+#if 0
+	case mu_argcv_escape_sh:
+	  mu_wordsplit_sh_quote_copy (buffer + j, argv[i]);
+#endif
+	}
+
+      j += toklen;
+      if (quote)
+	buffer[j++] = '"';
+    }
+
+  buffer[j] = 0;
+  *pstring = buffer;
+  return 0;
+}
+
+int
+mu_argcv_string (int argc, char **argv, char **pstring)
+{
+  return mu_argcv_join (argc, argv, " ", mu_argcv_escape_c, pstring);
+}
diff --git a/libmailutils/url/get-secret.c b/libmailutils/base/argcvrem.c
similarity index 55%
copy from libmailutils/url/get-secret.c
copy to libmailutils/base/argcvrem.c
index 34fb229..d6e994d 100644
--- a/libmailutils/url/get-secret.c
+++ b/libmailutils/base/argcvrem.c
@@ -1,5 +1,6 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 2010 Free Software Foundation, Inc.
+/* Selectively remove elements from an array of string pointers.
+   Copyright (C) 1999, 2000, 2001, 2003, 2004, 2005, 2006, 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
@@ -18,27 +19,37 @@
 #ifdef HAVE_CONFIG_H
 # include <config.h>
 #endif
-
-#include <errno.h>
 #include <stdlib.h>
-#include <string.h>
-#ifdef HAVE_STRINGS_H
-# include <strings.h>
-#endif
-
 #include <mailutils/types.h>
-#include <mailutils/errno.h>
-#include <mailutils/secret.h>
-#include <mailutils/sys/url.h>
+#include <mailutils/argcv.h>
 
-int
-mu_url_get_secret (const mu_url_t url, mu_secret_t *psecret)
+void
+mu_argcv_remove (int *pargc, char ***pargv,
+		 int (*sel) (const char *, void *), void *data)
 {
-  if (url->_get_secret)
-    return url->_get_secret (url, psecret);
-  if (url->secret == NULL)
-    return MU_ERR_NOENT;
-  mu_secret_ref (url->secret);
-  *psecret = url->secret;
-  return 0;
+  int i, j;
+  int argc = *pargc;
+  char **argv = *pargv;
+  int cnt = 0;
+  
+  for (i = j = 0; i < argc; i++)
+    {
+      if (sel (argv[i], data))
+	{
+	  free (argv[i]);
+	  cnt++;
+	}
+      else
+	{
+	  if (i != j)
+	    argv[j] = argv[i];
+	  j++;
+	}
+    }
+  if (i != j)
+    argv[j] = NULL;
+  argc -= cnt;
+
+  *pargc = argc;
+  *pargv = argv;
 }
diff --git a/libmailutils/base/mutil.c b/libmailutils/base/mutil.c
index ffa04dd..11232cf 100644
--- a/libmailutils/base/mutil.c
+++ b/libmailutils/base/mutil.c
@@ -22,8 +22,9 @@
 
 #include <sys/types.h>
 #include <sys/stat.h>
+#include <stdlib.h>
 #include <time.h>
-#include <mailutils/argcv.h>
+#include <mailutils/wordsplit.h>
 #include <mailutils/error.h>
 #include <mailutils/errno.h>
 #include <mailutils/nls.h>
@@ -78,20 +79,20 @@ mutil_parse_field_map (const char *map, mu_assoc_t *passoc_tab, int *perr)
 {
   int rc;
   int i;
-  int argc;
-  char **argv;
+  struct mu_wordsplit ws;
   mu_assoc_t assoc_tab = NULL;
 
-  rc = mu_argcv_get (map, ":", NULL, &argc, &argv);
-  if (rc)
+  ws.ws_delim = ":";
+  if (mu_wordsplit (map, &ws, MU_WRDSF_DEFFLAGS|MU_WRDSF_DELIM))
     {
-      mu_error (_("cannot split line `%s': %s"), map, mu_strerror (rc));
+      mu_error (_("cannot split line `%s': %s"), map,
+		mu_wordsplit_strerror (&ws));
       return rc;
     }
 
-  for (i = 0; i < argc; i += 2)
+  for (i = 0; i < ws.ws_wordc; i++)
     {
-      char *tok = argv[i];
+      char *tok = ws.ws_wordv[i];
       char *p = strchr (tok, '=');
       char *pptr;
       
@@ -123,7 +124,7 @@ mutil_parse_field_map (const char *map, mu_assoc_t *passoc_tab, int *perr)
 	}
     }
 
-  mu_argcv_free (argc, argv);
+  mu_wordsplit_free (&ws);
   if (rc && perr)
     *perr = i;
   return rc;
diff --git a/libmailutils/cfg/format.c b/libmailutils/cfg/format.c
index e7aaa7b..1d75b3b 100644
--- a/libmailutils/cfg/format.c
+++ b/libmailutils/cfg/format.c
@@ -18,11 +18,12 @@
 #ifdef HAVE_CONFIG_H
 # include <config.h>
 #endif
+#include <stdlib.h>
 #include <mailutils/alloc.h>
 #include <mailutils/stream.h>
 #include <mailutils/error.h>
 #include <mailutils/cfg.h>
-#include <mailutils/argcv.h>
+#include <mailutils/wordsplit.h>
 #include <mailutils/nls.h>
 #include <mailutils/iterator.h>
 #include <ctype.h>
@@ -50,7 +51,7 @@ format_string_value (struct tree_print *tp, const char *str)
   int quote;
   char *p;
 
-  size = mu_argcv_quoted_length (str, &quote);
+  size = mu_wordsplit_c_quoted_length (str, 1, &quote);
   if (quote)
     size += 2;
   size++;
@@ -69,7 +70,7 @@ format_string_value (struct tree_print *tp, const char *str)
       p++;
     }
   tp->buf[size-1] = 0;
-  mu_argcv_quote_copy (p, str);
+  mu_wordsplit_c_quote_copy (p, str, 1);
   mu_stream_write (tp->stream, tp->buf, size - 1, NULL);
 }
 
diff --git a/libmailutils/cfg/lexer.l b/libmailutils/cfg/lexer.l
index 67c1199..52be390 100644
--- a/libmailutils/cfg/lexer.l
+++ b/libmailutils/cfg/lexer.l
@@ -32,7 +32,7 @@
 #include <mailutils/errno.h>
 #include <mailutils/error.h>
 #include <mailutils/debug.h>  
-#include <mailutils/argcv.h>
+#include <mailutils/wordsplit.h>
 #include <mailutils/alloc.h>  
 #include <mailutils/nls.h>
 #include <mailutils/cfg.h>
@@ -172,7 +172,7 @@ unescape_to_line (int c)
 {
   if (c != '\n')
     {
-      char t = mu_argcv_unquote_char (c);
+      char t = mu_wordsplit_c_unquote_char (c);
       if (t == c && t != '\\' && t != '\"')
 	mu_cfg_parse_error (_("unknown escape sequence '\\%c'"), c);
       mu_opool_append_char (pool, t);
diff --git a/libmailutils/cfg/parser.y b/libmailutils/cfg/parser.y
index 9fe5b1b..cc77fb9 100644
--- a/libmailutils/cfg/parser.y
+++ b/libmailutils/cfg/parser.y
@@ -27,6 +27,7 @@
 #include <netdb.h>
 #include "intprops.h"
 #include <mailutils/argcv.h>
+#include <mailutils/wordsplit.h>
 #include <mailutils/nls.h>
 #include <mailutils/cfg.h>
 #include <mailutils/alloc.h>
@@ -1579,7 +1580,6 @@ mu_cfg_value_eq (mu_config_value_t *a, mu_config_value_t *b)
 static int
 split_cfg_path (const char *path, int *pargc, char ***pargv)
 {
-  int rc;
   int argc;
   char **argv;
   char *delim = MU_CFG_PATH_DELIM_STR;
@@ -1598,25 +1598,36 @@ split_cfg_path (const char *path, int *pargc, char ***pargv)
 	}
       argv[1] = NULL;
       argc = 1;
-      rc = 0;
     }
   else
     {
+      struct mu_wordsplit ws;
+      
       if (mu_ispunct (path[0]))
 	{
 	  delim = static_delim;
 	  delim[0] = path[0];
 	  path++;
 	}
-      rc = mu_argcv_get_np (path, strlen (path), delim, NULL, 0,
-			    &argc, &argv, NULL);
-    }
-  if (rc == 0)
-    {
-      *pargc = argc;
-      *pargv = argv;
+      ws.ws_delim = delim;
+
+      if (mu_wordsplit (path, &ws, MU_WRDSF_DEFFLAGS|MU_WRDSF_DELIM))
+	{
+	  mu_error (_("cannot split line `%s': %s"), path,
+		    mu_wordsplit_strerror (&ws));
+	  return errno;
+	}
+      argc = ws.ws_wordc;
+      argv = ws.ws_wordv;
+      ws.ws_wordc = 0;
+      ws.ws_wordv = NULL;
+      mu_wordsplit_free (&ws);
     }
-  return rc;
+
+  *pargc = argc;
+  *pargv = argv;
+
+  return 0;
 }
 
 struct find_data
@@ -1665,53 +1676,63 @@ static mu_config_value_t *
 parse_label (const char *str)
 {
   mu_config_value_t *val = NULL;
-  int count, i;
-  char **vect;
+  size_t i;
+  struct mu_wordsplit ws;
   size_t len = strlen (str);
   
   if (len > 1 && str[0] == '(' && str[len-1] == ')')
     {
       mu_list_t lst;
-      mu_argcv_get_np (str + 1, len - 2,
-		       ", \t", NULL,
-		       0,
-		       &count, &vect, NULL);
+
+      ws.ws_delim = ",";
+      if (mu_wordsplit_len (str + 1, len - 2, &ws,
+			    MU_WRDSF_DEFFLAGS|MU_WRDSF_DELIM|MU_WRDSF_WS))
+	{
+	  mu_error (_("cannot split line `%s': %s"), str,
+		    mu_wordsplit_strerror (&ws));
+	  return NULL;
+	}
+
       mu_list_create (&lst);
       mu_list_set_destroy_item (lst, destroy_value);
-      for (i = 0; i < count; i++)
+      for (i = 0; i < ws.ws_wordc; i++)
 	{
 	  mu_config_value_t *p = mu_alloc (sizeof (*p));
 	  p->type = MU_CFG_STRING;
-	  p->v.string = vect[i];
+	  p->v.string = ws.ws_wordv[i];
 	  mu_list_append (lst, p);
 	}
-      free (vect);
       val = mu_alloc (sizeof (*val));
       val->type = MU_CFG_LIST;
       val->v.list = lst;
     }
   else
     {      
-      mu_argcv_get (str, NULL, NULL, &count, &vect);
+      if (mu_wordsplit (str, &ws, MU_WRDSF_DEFFLAGS))
+	{
+	  mu_error (_("cannot split line `%s': %s"), str,
+		    mu_wordsplit_strerror (&ws));
+	  return NULL;
+	}
       val = mu_alloc (sizeof (*val));
-      if (count == 1)
+      if (ws.ws_wordc == 1)
 	{
 	  val->type = MU_CFG_STRING;
-	  val->v.string = vect[0];
-	  free (vect);
+	  val->v.string = ws.ws_wordv[0];
 	}
       else
 	{
 	  val->type = MU_CFG_ARRAY;
-	  val->v.arg.c = count;
-	  val->v.arg.v = mu_alloc (count * sizeof (val->v.arg.v[0]));
-	  for (i = 0; i < count; i++)
+	  val->v.arg.c = ws.ws_wordc;
+	  val->v.arg.v = mu_alloc (ws.ws_wordc * sizeof (val->v.arg.v[0]));
+	  for (i = 0; i < ws.ws_wordc; i++)
 	    {
 	      val->v.arg.v[i].type = MU_CFG_STRING;
-	      val->v.arg.v[i].v.string = vect[i];
+	      val->v.arg.v[i].v.string = ws.ws_wordv[i];
 	    }
-	  free (vect);
 	}
+      ws.ws_wordc = 0;
+      mu_wordsplit_free (&ws);
     }
   return val;
 }
diff --git a/libmailutils/diag/gdebug.c b/libmailutils/diag/gdebug.c
index 7e6f63c..d2d89d5 100644
--- a/libmailutils/diag/gdebug.c
+++ b/libmailutils/diag/gdebug.c
@@ -28,7 +28,7 @@
 #include <mailutils/assoc.h>
 #include <mailutils/error.h>
 #include <mailutils/errno.h>
-#include <mailutils/argcv.h>
+#include <mailutils/wordsplit.h>
 #include <mailutils/debug.h>
 #include <mailutils/cfg.h>
 #include <mailutils/nls.h>
@@ -167,20 +167,22 @@ mu_debug_level_from_string (const char *string, mu_log_level_t *plev,
 int
 mu_global_debug_from_string (const char *string, const char *errpfx)
 {
-  int rc;
-  int argc;
-  char **argv;
-  int i;
-  
-  rc = mu_argcv_get (string, ";", NULL, &argc, &argv);
-  if (rc)
-    return rc;
+  struct mu_wordsplit ws;
+  size_t i;
+
+  ws.ws_delim = ";";
+  if (mu_wordsplit (string, &ws, MU_WRDSF_DEFFLAGS|MU_WRDSF_DELIM|MU_WRDSF_WS))
+    {
+      mu_error (_("cannot split line `%s': %s"), string,
+		mu_wordsplit_strerror (&ws));
+      return errno;
+    }
 
-  for (i = 0; i < argc; i++)
+  for (i = 0; i < ws.ws_wordc; i++)
     {
       char *p;
       mu_log_level_t level = MU_DEBUG_INHERIT;
-      char *object_name = argv[i];
+      char *object_name = ws.ws_wordv[i];
       
       for (p = object_name; *p && *p != '='; p++)
 	;
@@ -199,7 +201,7 @@ mu_global_debug_from_string (const char *string, const char *errpfx)
 		  mu_error ("%s: invalid debugging specification `%s': "
 			    "expected levels or number after `=', "
 			    "but found `%s'",
-			    errpfx, argv[i], p);
+			    errpfx, ws.ws_wordv[i], p);
 		  break;
 		}
 	    }
@@ -254,8 +256,7 @@ mu_global_debug_from_string (const char *string, const char *errpfx)
       
       mu_global_debug_set_level (object_name, level);
     }
-  
-  mu_argcv_free (argc, argv);
+  mu_wordsplit_free (&ws);
   return 0;
 }
 
diff --git a/libmailutils/mailer/mailer.c b/libmailutils/mailer/mailer.c
index 01fb6d3..fec7cab 100644
--- a/libmailutils/mailer/mailer.c
+++ b/libmailutils/mailer/mailer.c
@@ -29,6 +29,7 @@
 
 #include <sys/time.h>
 
+#include <mailutils/nls.h>
 #include <mailutils/cstr.h>
 #include <mailutils/address.h>
 #include <mailutils/debug.h>
@@ -45,7 +46,7 @@
 #include <mailutils/body.h>
 #include <mailutils/mailbox.h>
 #include <mailutils/message.h>
-#include <mailutils/argcv.h>
+#include <mailutils/wordsplit.h>
 #include <mailutils/util.h>
 #include <mailutils/mime.h>
 #include <mailutils/io.h>
@@ -289,7 +290,8 @@ save_fcc (mu_message_t msg)
 {
   mu_header_t hdr;
   size_t count = 0, i;
-  char buf[512], *fcc;
+  const char *buf;
+  char *fcc;
   
   if (mu_message_get_header (msg, &hdr))
     return;
@@ -302,17 +304,25 @@ save_fcc (mu_message_t msg)
     {
       mu_mailbox_t mbox;
       
-      mu_header_get_field_name (hdr, i, buf, sizeof buf, NULL);
+      mu_header_sget_field_name (hdr, i, &buf);
       if (mu_c_strcasecmp (buf, MU_HEADER_FCC) == 0
 	  && mu_header_aget_field_value (hdr, i, &fcc) == 0)
 	{
-	  int i, argc;
-	  char **argv;
-	  
-	  mu_argcv_get (fcc, ",", NULL, &argc, &argv);
-	  for (i = 0; i < argc; i += 2)
+	  size_t i;
+	  struct mu_wordsplit ws;
+
+	  ws.ws_delim = ",";
+	  if (mu_wordsplit (fcc, &ws,
+			    MU_WRDSF_DEFFLAGS|MU_WRDSF_DELIM|MU_WRDSF_WS))
+	    {
+	      mu_error (_("cannot split line `%s': %s"), fcc,
+			mu_wordsplit_strerror (&ws));
+	      continue;
+	    }
+
+	  for (i = 0; i < ws.ws_wordc; i += 2)
 	    {
-	      if (mu_mailbox_create_default (&mbox, argv[i]))
+	      if (mu_mailbox_create_default (&mbox, ws.ws_wordv[i]))
 		continue; /*FIXME: error message?? */
 	      if (mu_mailbox_open (mbox,
 				   MU_STREAM_RDWR | MU_STREAM_CREAT
@@ -324,7 +334,7 @@ save_fcc (mu_message_t msg)
 	      mu_mailbox_close (mbox);
 	      mu_mailbox_destroy (&mbox);
 	    }
-	  mu_argcv_free (argc, argv);
+	  mu_wordsplit_free (&ws);
 	  free (fcc);
 	}
     }
diff --git a/libmailutils/server/acl.c b/libmailutils/server/acl.c
index a3e3764..d7a1362 100644
--- a/libmailutils/server/acl.c
+++ b/libmailutils/server/acl.c
@@ -28,8 +28,9 @@
 #include <unistd.h>
 #include <stdlib.h>
 #include <string.h>
+#include <mailutils/nls.h>
 #include <mailutils/acl.h>
-#include <mailutils/argcv.h>
+#include <mailutils/wordsplit.h>
 #include <mailutils/list.h>
 #include <mailutils/debug.h>
 #include <mailutils/error.h>
@@ -581,14 +582,19 @@ spawn_prog (const char *cmdline, int *pstatus, struct run_closure *rp)
   if (pid == 0)
     {
       int i;
-      int argc;
-      char **argv;
+      struct mu_wordsplit ws;
 
-      mu_argcv_get (s, " \t", NULL, &argc, &argv);
+      if (mu_wordsplit (s, &ws, MU_WRDSF_DEFFLAGS))
+	{
+	  mu_error (_("cannot split line `%s': %s"), s,
+		    mu_wordsplit_strerror (&ws));
+	  _exit (127);
+	}
+      
       for (i = getmaxfd (); i > 2; i--)
 	close (i);
-      execvp (argv[0], argv);
-      exit (127);
+      execvp (ws.ws_wordv[0], ws.ws_wordv);
+      _exit (127);
     }
 
   free (s);
diff --git a/libmailutils/stream/prog_stream.c b/libmailutils/stream/prog_stream.c
index 437bf01..c05c91e 100644
--- a/libmailutils/stream/prog_stream.c
+++ b/libmailutils/stream/prog_stream.c
@@ -29,6 +29,7 @@
 #include <mailutils/types.h>
 #include <mailutils/alloc.h>
 #include <mailutils/argcv.h>
+#include <mailutils/wordsplit.h>
 #include <mailutils/error.h>
 #include <mailutils/errno.h>
 #include <mailutils/nls.h>
@@ -390,17 +391,25 @@ struct _mu_prog_stream *
 _prog_stream_create (const char *progname, int flags)
 {
   struct _mu_prog_stream *fs;
+  struct mu_wordsplit ws;
   
   fs = (struct _mu_prog_stream *) _mu_stream_create (sizeof (*fs), flags);
   if (!fs)
     return NULL;
 
-  if (mu_argcv_get (progname, "", "#", &fs->argc, &fs->argv))
+  ws.ws_comment = "#";
+  if (mu_wordsplit (progname, &ws, MU_WRDSF_DEFFLAGS|MU_WRDSF_COMMENT))
     {
-      mu_argcv_free (fs->argc, fs->argv);
+      mu_error (_("cannot split line `%s': %s"), progname,
+		mu_wordsplit_strerror (&ws));
       free (fs);
       return NULL;
     }
+  fs->argc = ws.ws_wordc;
+  fs->argv = ws.ws_wordv;
+  ws.ws_wordc = 0;
+  ws.ws_wordv = NULL;
+  mu_wordsplit_free (&ws);
 
   fs->stream.read = _prog_read;
   fs->stream.write = _prog_write;
diff --git a/libmailutils/string/Makefile.am b/libmailutils/string/Makefile.am
index 485df4a..5cb6a23 100644
--- a/libmailutils/string/Makefile.am
+++ b/libmailutils/string/Makefile.am
@@ -35,6 +35,7 @@ libstring_la_SOURCES = \
  muctype.c\
  vasnprintf.c\
  mkfilename.c\
+ wordsplit.c\
  xdecode.c
 
 INCLUDES = @MU_LIB_COMMON_INCLUDES@ -I/libmailutils
diff --git a/libmailutils/string/wordsplit.c b/libmailutils/string/wordsplit.c
new file mode 100644
index 0000000..301ab00
--- /dev/null
+++ b/libmailutils/string/wordsplit.c
@@ -0,0 +1,1469 @@
+/* wordsplit - a word splitter
+   Copyright (C) 2009, 2010 Sergey Poznyakoff
+
+   This program 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 of the License, 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, see <http://www.gnu.org/licenses/>. */
+
+/* This module is shared with Grecs: http://puszcza.gnu.org.ua/projects/grecs.
+   The intent is to keep it as mu-independent as possible, so that the two
+   projects can be easily synchronized.  Some time in the future I'll think
+   about providing a better way of synching, perhaps by integrating Grecs to
+   Mailutils as a submodule. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <errno.h>
+#include <ctype.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include <stdarg.h>
+
+#include <mailutils/nls.h>
+#include <mailutils/wordsplit.h>
+
+#define ISWS(c) ((c)==' '||(c)=='\t'||(c)=='\n')
+#define ISDELIM(ws,c) \
+  (strchr ((ws)->ws_delim, (c)) != NULL)
+#define ISPUNCT(c) (strchr("!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~",(c))!=NULL)
+#define ISUPPER(c) ('A' <= ((unsigned) (c)) && ((unsigned) (c)) <= 'Z')
+#define ISLOWER(c) ('a' <= ((unsigned) (c)) && ((unsigned) (c)) <= 'z')
+#define ISALPHA(c) (ISUPPER(c) || ISLOWER(c))
+#define ISDIGIT(c) ('0' <= ((unsigned) (c)) && ((unsigned) (c)) <= '9')
+#define ISXDIGIT(c) (strchr("abcdefABCDEF", c)!=NULL)
+#define ISALNUM(c) (ISALPHA(c) || ISDIGIT(c))
+#define ISPRINT(c) (' ' <= ((unsigned) (c)) && ((unsigned) (c)) <= 127)
+
+#define ALLOC_INIT 128
+#define ALLOC_INCR 128
+
+static void
+_wsplt_alloc_die (struct mu_wordsplit *wsp)
+{
+  wsp->ws_error (_("memory exhausted"));
+  abort ();
+}
+
+static void
+_wsplt_error (const char *fmt, ...)
+{
+  va_list ap;
+
+  va_start (ap, fmt);
+  vfprintf (stderr, fmt, ap);
+  va_end (ap);
+  fputc ('\n', stderr);
+}
+
+static void mu_wordsplit_free_nodes (struct mu_wordsplit *);
+
+static int
+_wsplt_nomem (struct mu_wordsplit *wsp)
+{
+  errno = ENOMEM;
+  wsp->ws_errno = MU_WRDSE_NOSPACE;
+  if (wsp->ws_flags & MU_WRDSF_ENOMEMABRT)
+    wsp->ws_alloc_die (wsp);
+  if (wsp->ws_flags & MU_WRDSF_SHOWERR)
+    mu_wordsplit_perror (wsp);
+  if (!(wsp->ws_flags & MU_WRDSF_REUSE))
+    mu_wordsplit_free (wsp);
+  mu_wordsplit_free_nodes (wsp);
+  return wsp->ws_errno;
+}
+
+static int
+mu_wordsplit_init (struct mu_wordsplit *wsp, const char *input, size_t len,
+		   int flags)
+{
+  wsp->ws_flags = flags;
+
+  if (!(wsp->ws_flags & MU_WRDSF_ALLOC_DIE))
+    wsp->ws_alloc_die = _wsplt_alloc_die;
+  if (!(wsp->ws_flags & MU_WRDSF_ERROR))
+    wsp->ws_error = _wsplt_error;
+
+  if (!(wsp->ws_flags & MU_WRDSF_NOVAR)
+      && !(wsp->ws_flags & (MU_WRDSF_ENV | MU_WRDSF_GETVAR)))
+    {
+      errno = EINVAL;
+      wsp->ws_errno = MU_WRDSE_USAGE;
+      if (wsp->ws_flags & MU_WRDSF_SHOWERR)
+	mu_wordsplit_perror (wsp);
+      return wsp->ws_errno;
+    }
+
+  if (!(wsp->ws_flags & MU_WRDSF_NOCMD))
+    {
+      errno = EINVAL;
+      wsp->ws_errno = MU_WRDSE_NOSUPP;
+      if (wsp->ws_flags & MU_WRDSF_SHOWERR)
+	mu_wordsplit_perror (wsp);
+      return wsp->ws_errno;
+    }
+
+  if (wsp->ws_flags & MU_WRDSF_SHOWDBG)
+    {
+      if (!(wsp->ws_flags & MU_WRDSF_DEBUG))
+	{
+	  if (wsp->ws_flags & MU_WRDSF_ERROR)
+	    wsp->ws_debug = wsp->ws_error;
+	  else if (wsp->ws_flags & MU_WRDSF_SHOWERR)
+	    wsp->ws_debug = _wsplt_error;
+	  else
+	    wsp->ws_flags &= ~MU_WRDSF_SHOWDBG;
+	}
+    }
+
+  wsp->ws_input = input;
+  wsp->ws_len = len;
+
+  if (!(wsp->ws_flags & MU_WRDSF_DOOFFS))
+    wsp->ws_offs = 0;
+
+  if (!(wsp->ws_flags & MU_WRDSF_DELIM))
+    wsp->ws_delim = " \t\n";
+
+  if (!(wsp->ws_flags & MU_WRDSF_COMMENT))
+    wsp->ws_comment = NULL;
+
+  if (wsp->ws_flags & MU_WRDSF_REUSE)
+    {
+      if (!(wsp->ws_flags & MU_WRDSF_APPEND))
+	wsp->ws_wordc = 0;
+    }
+  else
+    {
+      wsp->ws_wordv = NULL;
+      wsp->ws_wordc = 0;
+      wsp->ws_wordn = 0;
+    }
+  if (wsp->ws_flags & MU_WRDSF_DOOFFS)
+    wsp->ws_wordn += wsp->ws_offs;
+
+  wsp->ws_endp = 0;
+  wsp->ws_errno = 0;
+  wsp->ws_head = wsp->ws_tail = NULL;
+  return 0;
+}
+
+static int
+alloc_space (struct mu_wordsplit *wsp, size_t count)
+{
+  size_t offs = (wsp->ws_flags & MU_WRDSF_DOOFFS) ? wsp->ws_offs : 0;
+  char **ptr;
+  size_t newalloc;
+
+  if (wsp->ws_wordv == NULL)
+    {
+      newalloc = offs + count > ALLOC_INIT ? count : ALLOC_INIT;
+      ptr = calloc (newalloc, sizeof (ptr[0]));
+    }
+  else if (wsp->ws_wordn < offs + wsp->ws_wordc + count)
+    {
+      newalloc = offs + wsp->ws_wordc +
+	count > ALLOC_INCR ? count : ALLOC_INCR;
+      ptr = realloc (wsp->ws_wordv, newalloc * sizeof (ptr[0]));
+    }
+  else
+    return 0;
+
+  if (ptr)
+    {
+      wsp->ws_wordn = newalloc;
+      wsp->ws_wordv = ptr;
+    }
+  else
+    return _wsplt_nomem (wsp);
+  return 0;
+}
+
+
+/* Node state flags */
+#define _WSNF_NULL     0x01     /* null node (a noop) */
+#define _WSNF_WORD     0x02	/* node contains word in v.word */
+#define _WSNF_QUOTE    0x04	/* text is quoted */
+#define _WSNF_NOEXPAND 0x08	/* text is not subject to expansion */
+#define _WSNF_JOIN     0x10	/* node must be joined with the next node */
+#define _WSNF_SEXP     0x20	/* is a sed expression */
+
+#define _WSNF_EMPTYOK  0x0100   /* special flag indicating that
+				   mu_wordsplit_add_segm must add the
+				   segment even if it is empty */
+
+struct mu_wordsplit_node
+{
+  struct mu_wordsplit_node *prev;	/* Previous element */
+  struct mu_wordsplit_node *next;	/* Next element */
+  int flags;			/* Node flags */
+  union
+  {
+    struct
+    {
+      size_t beg;		/* Start of word in ws_input */
+      size_t end;		/* End of word in ws_input */
+    } segm;
+    char *word;
+  } v;
+};
+
+static const char *
+wsnode_flagstr (int flags)
+{
+  static char retbuf[6];
+  char *p = retbuf;
+
+  if (flags & _WSNF_WORD)
+    *p++ = 'w';
+  else if (flags & _WSNF_NULL)
+    *p++ = 'n';
+  else
+    *p++ = '-';
+  if (flags & _WSNF_QUOTE)
+    *p++ = 'q';
+  else
+    *p++ = '-';
+  if (flags & _WSNF_NOEXPAND)
+    *p++ = 'E';
+  else
+    *p++ = '-';
+  if (flags & _WSNF_JOIN)
+    *p++ = 'j';
+  else
+    *p++ = '-';
+  if (flags & _WSNF_SEXP)
+    *p++ = 's';
+  else
+    *p++ = '-';
+  *p = 0;
+  return retbuf;
+}
+
+static const char *
+wsnode_ptr (struct mu_wordsplit *wsp, struct mu_wordsplit_node *p)
+{
+  if (p->flags & _WSNF_NULL)
+    return "";
+  else if (p->flags & _WSNF_WORD)
+    return p->v.word;
+  else
+    return wsp->ws_input + p->v.segm.beg;
+}
+
+static size_t
+wsnode_len (struct mu_wordsplit_node *p)
+{
+  if (p->flags & _WSNF_NULL)
+    return 0;
+  else if (p->flags & _WSNF_WORD)
+    return strlen (p->v.word);
+  else
+    return p->v.segm.end - p->v.segm.beg;
+}
+
+static int
+wsnode_new (struct mu_wordsplit *wsp, struct mu_wordsplit_node **pnode)
+{
+  struct mu_wordsplit_node *node = calloc (1, sizeof (*node));
+  if (!node)
+    return _wsplt_nomem (wsp);
+  *pnode = node;
+  return 0;
+}
+
+static void
+wsnode_free (struct mu_wordsplit_node *p)
+{
+  if (p->flags & _WSNF_WORD)
+    free (p->v.word);
+  free (p);
+}
+
+static void
+wsnode_append (struct mu_wordsplit *wsp, struct mu_wordsplit_node *node)
+{
+  node->next = NULL;
+  node->prev = wsp->ws_tail;
+  if (wsp->ws_tail)
+    wsp->ws_tail->next = node;
+  else
+    wsp->ws_head = node;
+  wsp->ws_tail = node;
+}
+
+static void
+wsnode_remove (struct mu_wordsplit *wsp, struct mu_wordsplit_node *node)
+{
+  struct mu_wordsplit_node *p;
+
+  p = node->prev;
+  if (p)
+    p->next = node->next;
+  else
+    wsp->ws_head = node->next;
+
+  p = node->next;
+  if (p)
+    p->prev = node->prev;
+  else
+    wsp->ws_tail = node->prev;
+
+  node->next = node->prev = NULL;
+}
+
+static void
+wsnode_insert (struct mu_wordsplit *wsp, struct mu_wordsplit_node *node,
+	       struct mu_wordsplit_node *anchor, int before)
+{
+  if (!wsp->ws_head)
+    {
+      node->next = node->prev = NULL;
+      wsp->ws_head = wsp->ws_tail = node;
+    }
+  else if (before)
+    {
+      if (anchor->prev)
+	wsnode_insert (wsp, node, anchor->prev, 0);
+      else
+	{
+	  node->prev = NULL;
+	  node->next = anchor;
+	  anchor->prev = node;
+	  wsp->ws_head = node;
+	}
+    }
+  else
+    {
+      struct mu_wordsplit_node *p;
+
+      p = anchor->next;
+      if (p)
+	p->prev = node;
+      else
+	wsp->ws_tail = node;
+      node->next = p;
+      node->prev = anchor;
+      anchor->next = node;
+    }
+}
+
+static int
+mu_wordsplit_add_segm (struct mu_wordsplit *wsp, size_t beg, size_t end,
+		       int flg)
+{
+  struct mu_wordsplit_node *node;
+  int rc;
+
+  if (end == beg && !(flg & _WSNF_EMPTYOK))
+    return 0;
+  rc = wsnode_new (wsp, &node);
+  if (rc)
+    return rc;
+  node->flags = flg & ~(_WSNF_WORD|_WSNF_EMPTYOK);
+  node->v.segm.beg = beg;
+  node->v.segm.end = end;
+  wsnode_append (wsp, node);
+  return 0;
+}
+
+static void
+mu_wordsplit_free_nodes (struct mu_wordsplit *wsp)
+{
+  struct mu_wordsplit_node *p;
+
+  for (p = wsp->ws_head; p;)
+    {
+      struct mu_wordsplit_node *next = p->next;
+      wsnode_free (p);
+      p = next;
+    }
+  wsp->ws_head = wsp->ws_tail = NULL;
+}
+
+static void
+mu_wordsplit_dump_nodes (struct mu_wordsplit *wsp)
+{
+  struct mu_wordsplit_node *p;
+  int n = 0;
+
+  for (p = wsp->ws_head, n = 0; p; p = p->next, n++)
+    {
+      if (p->flags & _WSNF_WORD)
+	wsp->ws_debug ("%4d: %p: %#04x (%s):%s;",
+		       n, p, p->flags, wsnode_flagstr (p->flags), p->v.word);
+      else
+	wsp->ws_debug ("%4d: %p: %#04x (%s):%.*s;",
+		       n, p, p->flags, wsnode_flagstr (p->flags),
+		       p->v.segm.end - p->v.segm.beg,
+		       wsp->ws_input + p->v.segm.beg);
+    }
+}
+
+static int
+coalesce_segment (struct mu_wordsplit *wsp, struct mu_wordsplit_node *node)
+{
+  struct mu_wordsplit_node *p, *end;
+  size_t len = 0;
+  char *buf, *cur;
+  int stop;
+
+  for (p = node; p && (p->flags & _WSNF_JOIN); p = p->next)
+    {
+      len += wsnode_len (p);
+    }
+  len += wsnode_len (p);
+  end = p;
+
+  buf = malloc (len + 1);
+  if (!buf)
+    return _wsplt_nomem (wsp);
+  cur = buf;
+
+  p = node;
+  for (stop = 0; !stop;)
+    {
+      struct mu_wordsplit_node *next = p->next;
+      const char *str = wsnode_ptr (wsp, p);
+      size_t slen = wsnode_len (p);
+
+      memcpy (cur, str, slen);
+      cur += slen;
+      if (p != node)
+	{
+	  wsnode_remove (wsp, p);
+	  stop = p == end;
+	  wsnode_free (p);
+	}
+      p = next;
+    }
+
+  *cur = 0;
+
+  node->flags &= ~_WSNF_JOIN;
+
+  if (node->flags & _WSNF_WORD)
+    free (node->v.word);
+  else
+    node->flags |= _WSNF_WORD;
+  node->v.word = buf;
+  return 0;
+}
+
+static int
+wsnode_quoteremoval (struct mu_wordsplit *wsp)
+{
+  struct mu_wordsplit_node *p;
+  void (*uqfn) (char *, const char *, size_t) =
+    (wsp->ws_flags & MU_WRDSF_CESCAPES) ?
+    mu_wordsplit_c_unquote_copy : mu_wordsplit_sh_unquote_copy;
+
+  for (p = wsp->ws_head; p; p = p->next)
+    {
+      const char *str = wsnode_ptr (wsp, p);
+      size_t slen = wsnode_len (p);
+      int unquote;
+
+      if (wsp->ws_flags & MU_WRDSF_QUOTE)
+	{
+	  unquote = !(p->flags & _WSNF_NOEXPAND);
+	}
+      else
+	unquote = 0;
+
+      if (unquote)
+	{
+	  if (!(p->flags & _WSNF_WORD))
+	    {
+	      char *newstr = malloc (slen + 1);
+	      if (!newstr)
+		return _wsplt_nomem (wsp);
+	      memcpy (newstr, str, slen);
+	      newstr[slen] = 0;
+	      p->v.word = newstr;
+	      p->flags |= _WSNF_WORD;
+	    }
+	  uqfn (p->v.word, str, slen);
+	}
+    }
+  return 0;
+}
+
+static int
+wsnode_coalesce (struct mu_wordsplit *wsp)
+{
+  struct mu_wordsplit_node *p;
+
+  for (p = wsp->ws_head; p; p = p->next)
+    {
+      if (p->flags & _WSNF_JOIN)
+	if (coalesce_segment (wsp, p))
+	  return 1;
+    }
+  return 0;
+}
+
+static int
+mu_wordsplit_finish (struct mu_wordsplit *wsp)
+{
+  struct mu_wordsplit_node *p;
+  size_t n;
+
+  n = 0;
+
+  for (p = wsp->ws_head; p; p = p->next)
+    n++;
+
+  if (alloc_space (wsp, n + 1))
+    return 1;
+
+  for (p = wsp->ws_head; p; p = p->next)
+    {
+      const char *str = wsnode_ptr (wsp, p);
+      size_t slen = wsnode_len (p);
+      char *newstr = malloc (slen + 1);
+
+      /* Assign newstr first, even if it is NULL.  This way
+         mu_wordsplit_free will work even if we return
+         nomem later. */
+      wsp->ws_wordv[wsp->ws_offs + wsp->ws_wordc] = newstr;
+      if (!newstr)
+	return _wsplt_nomem (wsp);
+      memcpy (newstr, str, slen);
+      newstr[slen] = 0;
+
+      wsp->ws_wordc++;
+
+    }
+  wsp->ws_wordv[wsp->ws_offs + wsp->ws_wordc] = NULL;
+  return 0;
+}
+
+
+/* Variable expansion */
+static int
+node_split_prefix (struct mu_wordsplit *wsp,
+		   struct mu_wordsplit_node **ptail,
+		   struct mu_wordsplit_node *node,
+		   size_t beg, size_t len, int flg)
+{
+  struct mu_wordsplit_node *newnode;
+
+  if (len == 0)
+    return 0;
+  if (wsnode_new (wsp, &newnode))
+    return 1;
+  wsnode_insert (wsp, newnode, *ptail, 0);
+  if (node->flags & _WSNF_WORD)
+    {
+      const char *str = wsnode_ptr (wsp, node);
+      char *newstr = malloc (len + 1);
+      if (!newstr)
+	return _wsplt_nomem (wsp);
+      memcpy (newstr, str + beg, len);
+      newstr[len] = 0;
+      newnode->flags = _WSNF_WORD;
+      newnode->v.word = newstr;
+    }
+  else
+    {
+      newnode->v.segm.beg = node->v.segm.beg + beg;
+      newnode->v.segm.end = newnode->v.segm.beg + len;
+    }
+  newnode->flags |= flg;
+  *ptail = newnode;
+  return 0;
+}
+
+static int
+find_closing_cbrace (const char *str, size_t i, size_t len, size_t * poff)
+{
+  enum
+  { st_init, st_squote, st_dquote } state = st_init;
+  size_t level = 1;
+
+  for (; i < len; i++)
+    {
+      switch (state)
+	{
+	case st_init:
+	  switch (str[i])
+	    {
+	    case '{':
+	      level++;
+	      break;
+
+	    case '}':
+	      if (--level == 0)
+		{
+		  *poff = i;
+		  return 0;
+		}
+	      break;
+
+	    case '"':
+	      state = st_dquote;
+	      break;
+
+	    case '\'':
+	      state = st_squote;
+	      break;
+	    }
+	  break;
+
+	case st_squote:
+	  if (str[i] == '\'')
+	    state = st_init;
+	  break;
+
+	case st_dquote:
+	  if (str[i] == '\\')
+	    i++;
+	  else if (str[i] == '"')
+	    state = st_init;
+	  break;
+	}
+    }
+  return 1;
+}
+
+static const char *
+mu_wordsplit_find_env (struct mu_wordsplit *wsp, const char *name, size_t len)
+{
+  size_t i;
+
+  if (!(wsp->ws_flags & MU_WRDSF_ENV))
+    return NULL;
+  for (i = 0; wsp->ws_env[i]; i++)
+    {
+      size_t j;
+      const char *var = wsp->ws_env[i];
+
+      for (j = 0; j < len; j++)
+	if (name[j] != var[j])
+	  break;
+      if (j == len && var[j] == '=')
+	return var + j + 1;
+    }
+  return NULL;
+}
+
+static int
+expvar (struct mu_wordsplit *wsp, const char *str, size_t len,
+	struct mu_wordsplit_node **ptail, const char **pend, int flg)
+{
+  size_t i = 0;
+  const char *defstr = NULL;
+  char *value;
+  const char *vptr;
+  struct mu_wordsplit_node *newnode;
+  const char *start = str - 1;
+
+  if (ISALPHA (str[0]) || str[0] == '_')
+    {
+      for (i = 1; i < len; i++)
+	if (!(ISALNUM (str[i]) || str[i] == '_'))
+	  break;
+      *pend = str + i - 1;
+    }
+  else if (str[0] == '{')
+    {
+      str++;
+      len--;
+      for (i = 1; i < len; i++)
+	if (str[i] == '}' || str[i] == ':')
+	  break;
+      if (str[i] == ':')
+	{
+	  size_t j;
+
+	  defstr = str + i + 1;
+	  if (find_closing_cbrace (str, i + 1, len, &j))
+	    {
+	      wsp->ws_errno = MU_WRDSE_CBRACE;
+	      return 1;
+	    }
+	  *pend = str + j;
+	}
+      else if (str[i] == '}')
+	{
+	  defstr = NULL;
+	  *pend = str + i;
+	}
+      else
+	{
+	  wsp->ws_errno = MU_WRDSE_CBRACE;
+	  return 1;
+	}
+    }
+  else
+    {
+      if (wsnode_new (wsp, &newnode))
+	return 1;
+      wsnode_insert (wsp, newnode, *ptail, 0);
+      *ptail = newnode;
+      newnode->flags = _WSNF_WORD | flg;
+      newnode->v.word = malloc (3);
+      if (!newnode->v.word)
+	return _wsplt_nomem (wsp);
+      newnode->v.word[0] = '$';
+      newnode->v.word[1] = str[0];
+      newnode->v.word[2] = 0;
+      *pend = str;
+      return 0;
+    }
+
+  /* Actually expand the variable */
+  /* str - start of the variable name
+     i   - its length
+     defstr - default replacement str */
+
+  vptr = mu_wordsplit_find_env (wsp, str, i);
+  if (vptr)
+    {
+      value = strdup (vptr);
+      if (!value)
+	return _wsplt_nomem (wsp);
+    }
+  else if (wsp->ws_flags & MU_WRDSF_GETVAR)
+    value = wsp->ws_getvar (str, i);
+  else if (wsp->ws_flags & MU_WRDSF_UNDEF)
+    {
+      wsp->ws_errno = MU_WRDSE_UNDEF;
+      if (wsp->ws_flags & MU_WRDSF_SHOWERR)
+	mu_wordsplit_perror (wsp);
+      return 1;
+    }
+  else
+    {
+      if (wsp->ws_flags & MU_WRDSF_WARNUNDEF)
+	wsp->ws_error (_("warning: undefined variable `%.*s'"), i, str);
+      if (wsp->ws_flags & MU_WRDSF_KEEPUNDEF)
+	value = NULL;
+      else
+	value = "";
+    }
+  /* FIXME: handle defstr */
+  if (value)
+    {
+      if (flg & _WSNF_QUOTE)
+	{
+	  if (wsnode_new (wsp, &newnode))
+	    return 1;
+	  wsnode_insert (wsp, newnode, *ptail, 0);
+	  *ptail = newnode;
+	  newnode->flags = _WSNF_WORD | _WSNF_NOEXPAND | flg;
+	  newnode->v.word = strdup (value);
+	  if (!newnode->v.word)
+	    return _wsplt_nomem (wsp);
+	}
+      else if (*value == 0)
+	{
+	  /* Empty string is a special case */
+	  if (wsnode_new (wsp, &newnode))
+	    return 1;
+	  wsnode_insert (wsp, newnode, *ptail, 0);
+	  *ptail = newnode;
+	  newnode->flags = _WSNF_NULL;
+	}
+      else
+	{
+	  struct mu_wordsplit ws;
+	  int i;
+
+	  ws.ws_delim = wsp->ws_delim;
+	  if (mu_wordsplit (value, &ws,
+			    MU_WRDSF_NOVAR | MU_WRDSF_NOCMD |
+			    MU_WRDSF_DELIM | MU_WRDSF_SQUEEZE_DELIMS))
+	    {
+	      mu_wordsplit_free (&ws);
+	      return 1;
+	    }
+	  for (i = 0; i < ws.ws_wordc; i++)
+	    {
+	      if (wsnode_new (wsp, &newnode))
+		return 1;
+	      wsnode_insert (wsp, newnode, *ptail, 0);
+	      *ptail = newnode;
+	      newnode->flags = _WSNF_WORD |
+		_WSNF_NOEXPAND |
+		(i + 1 < ws.ws_wordc ? (flg & ~_WSNF_JOIN) : flg);
+	      newnode->v.word = strdup (ws.ws_wordv[i]);
+	      if (!newnode->v.word)
+		return _wsplt_nomem (wsp);
+	    }
+	  mu_wordsplit_free (&ws);
+	}
+    }
+  else if (wsp->ws_flags & MU_WRDSF_KEEPUNDEF)
+    {
+      size_t size = *pend - start + 1;
+
+      if (wsnode_new (wsp, &newnode))
+	return 1;
+      wsnode_insert (wsp, newnode, *ptail, 0);
+      *ptail = newnode;
+      newnode->flags = _WSNF_WORD | _WSNF_NOEXPAND | flg;
+      newnode->v.word = malloc (size + 1);
+      if (!newnode->v.word)
+	return _wsplt_nomem (wsp);
+      memcpy (newnode->v.word, start, size);
+      newnode->v.word[size] = 0;
+    }
+  return 0;
+}
+
+static int
+node_expand_vars (struct mu_wordsplit *wsp, struct mu_wordsplit_node *node)
+{
+  const char *str = wsnode_ptr (wsp, node);
+  size_t slen = wsnode_len (node);
+  const char *end = str + slen;
+  const char *p;
+  size_t off = 0;
+  struct mu_wordsplit_node *tail = node;
+
+  for (p = str; p < end; p++)
+    {
+      if (*p == '\\')
+	{
+	  p++;
+	  continue;
+	}
+      if (*p == '$')
+	{
+	  size_t n = p - str;
+
+	  if (tail != node)
+	    tail->flags |= _WSNF_JOIN;
+	  if (node_split_prefix (wsp, &tail, node, off, n, _WSNF_JOIN))
+	    return 1;
+	  p++;
+	  if (expvar (wsp, p, slen - n, &tail, &p,
+		      node->flags & (_WSNF_JOIN | _WSNF_QUOTE)))
+	    return 1;
+	  off += p - str + 1;
+	  str = p + 1;
+	}
+    }
+  if (p > str)
+    {
+      if (tail != node)
+	tail->flags |= _WSNF_JOIN;
+      if (node_split_prefix (wsp, &tail, node, off, p - str,
+			     node->flags & _WSNF_JOIN))
+	return 1;
+    }
+  if (tail != node)
+    {
+      wsnode_remove (wsp, node);
+      wsnode_free (node);
+    }
+  return 0;
+}
+
+static int
+mu_wordsplit_varexp (struct mu_wordsplit *wsp)
+{
+  struct mu_wordsplit_node *p;
+
+  for (p = wsp->ws_head; p;)
+    {
+      struct mu_wordsplit_node *next = p->next;
+      if (!(p->flags & _WSNF_NOEXPAND))
+	if (node_expand_vars (wsp, p))
+	  return 1;
+      p = next;
+    }
+
+  /* Remove NULL lists */
+  for (p = wsp->ws_head; p;)
+    {
+      struct mu_wordsplit_node *next = p->next;
+      if (p->flags & _WSNF_NULL)
+	{
+	  wsnode_remove (wsp, p);
+	  wsnode_free (p);
+	}
+      p = next;
+    }
+
+  return 0;
+}
+
+/* Strip off any leading and trailing whitespace.  This function is called
+   right after the initial scanning, therefore it assumes that every
+   node in the list is a text reference node. */ 
+static void
+mu_wordsplit_trimws (struct mu_wordsplit *wsp)
+{
+  struct mu_wordsplit_node *p;
+
+  for (p = wsp->ws_head; p; p = p->next)
+    {
+      size_t n;
+      
+      /* Skip leading whitespace: */
+      for (n = p->v.segm.beg; n < p->v.segm.end && ISWS (wsp->ws_input[n]);
+	   n++)
+	;
+      p->v.segm.beg = n;
+      /* Trim trailing whitespace */
+      for (n = p->v.segm.end; n > p->v.segm.beg && ISWS (wsp->ws_input[n-1]);
+	   n--);
+      p->v.segm.end = n;
+    }
+}
+
+static int
+skip_sed_expr (const char *command, size_t i, size_t len)
+{
+  int state;
+
+  do
+    {
+      int delim;
+
+      if (command[i] == ';')
+	i++;
+      if (!(command[i] == 's' && i + 3 < len && ISPUNCT (command[i + 1])))
+	break;
+
+      delim = command[++i];
+      state = 1;
+      for (i++; i < len; i++)
+	{
+	  if (state == 3)
+	    {
+	      if (command[i] == delim || !ISALNUM (command[i]))
+		break;
+	    }
+	  else if (command[i] == '\\')
+	    i++;
+	  else if (command[i] == delim)
+	    state++;
+	}
+    }
+  while (state == 3 && i < len && command[i] == ';');
+  return i;
+}
+
+static size_t
+skip_delim (struct mu_wordsplit *wsp)
+{
+  size_t start = wsp->ws_endp;
+  if (wsp->ws_flags & MU_WRDSF_SQUEEZE_DELIMS)
+    {
+      do
+	start++;
+      while (start < wsp->ws_len
+	     && ISDELIM (wsp, wsp->ws_input[start]));
+      start--;
+    }
+
+  if (!(wsp->ws_flags & MU_WRDSF_RETURN_DELIMS))
+    start++;
+
+  return start;
+}
+
+#define _MU_WRDS_EOF  0
+#define _MU_WRDS_OK   1
+#define _MU_WRDS_ERR  2
+
+static int
+scan_qstring (struct mu_wordsplit *wsp, size_t start, size_t * end)
+{
+  size_t j;
+  const char *command = wsp->ws_input;
+  size_t len = wsp->ws_len;
+  char q = command[start];
+
+  for (j = start + 1; j < len && command[j] != q; j++)
+    if (q == '"' && command[j] == '\\')
+      j++;
+  if (j < len && command[j] == q)
+    {
+      int flags = _WSNF_QUOTE;
+      if (q == '\'')
+	flags |= _WSNF_NOEXPAND;
+      if (mu_wordsplit_add_segm (wsp, start + 1, j, flags))
+	return _MU_WRDS_ERR;
+      *end = j;
+    }
+  else
+    {
+      wsp->ws_endp = start;
+      wsp->ws_errno = MU_WRDSE_QUOTE;
+      if (wsp->ws_flags & MU_WRDSF_SHOWERR)
+	mu_wordsplit_perror (wsp);
+      return _MU_WRDS_ERR;
+    }
+  return 0;
+}
+
+static int
+scan_word (struct mu_wordsplit *wsp, size_t start)
+{
+  size_t len = wsp->ws_len;
+  const char *command = wsp->ws_input;
+  const char *comment = wsp->ws_comment;
+  int join = 0;
+  int flags = 0;
+
+  size_t i = start;
+
+  if (i >= len)
+    {
+      wsp->ws_errno = MU_WRDSE_EOF;
+      return _MU_WRDS_EOF;
+    }
+
+  start = i;
+
+  if (wsp->ws_flags & MU_WRDSF_SED_EXPR
+      && command[i] == 's' && i + 3 < len && ISPUNCT (command[i + 1]))
+    {
+      flags = _WSNF_SEXP;
+      i = skip_sed_expr (command, i, len);
+    }
+  else if (!ISDELIM (wsp, command[i]))
+    {
+      while (i < len)
+	{
+	  if (comment && strchr (comment, command[i]) != NULL)
+	    {
+	      size_t j;
+	      for (j = i + 1; j < len && command[j] != '\n'; j++)
+		;
+	      if (mu_wordsplit_add_segm (wsp, start, i, 0))
+		return _MU_WRDS_ERR;
+	      wsp->ws_endp = j;
+	      return _MU_WRDS_OK;
+	    }
+
+	  if (wsp->ws_flags & MU_WRDSF_QUOTE)
+	    {
+	      if (command[i] == '\\')
+		{
+		  if (++i == len)
+		    break;
+		  i++;
+		  continue;
+		}
+
+	      if (command[i] == '\'' || command[i] == '"')
+		{
+		  if (join && wsp->ws_tail)
+		    wsp->ws_tail->flags |= _WSNF_JOIN;
+		  if (mu_wordsplit_add_segm (wsp, start, i, _WSNF_JOIN))
+		    return _MU_WRDS_ERR;
+		  if (scan_qstring (wsp, i, &i))
+		    return _MU_WRDS_ERR;
+		  start = i + 1;
+		  join = 1;
+		}
+	    }
+
+	  if (ISDELIM (wsp, command[i]))
+	    break;
+	  else
+	    i++;
+	}
+    }
+  else if (wsp->ws_flags & MU_WRDSF_RETURN_DELIMS)
+    {
+      do
+	{
+	  i++;
+	}
+      while (i < len && ISDELIM (wsp, command[i]));
+    }
+  else if (!(wsp->ws_flags & MU_WRDSF_SQUEEZE_DELIMS))
+    flags |= _WSNF_EMPTYOK;
+
+  if (join && i > start && wsp->ws_tail)
+    wsp->ws_tail->flags |= _WSNF_JOIN;
+  if (mu_wordsplit_add_segm (wsp, start, i, flags))
+    return _MU_WRDS_ERR;
+  wsp->ws_endp = i;
+
+  return _MU_WRDS_OK;
+}
+
+static char quote_transtab[] = "\\\\a\ab\bf\fn\nr\rt\tv\v";
+
+int
+mu_wordsplit_c_unquote_char (int c)
+{
+  char *p;
+
+  for (p = quote_transtab; *p; p += 2)
+    {
+      if (*p == c)
+	return p[1];
+    }
+  return c;
+}
+
+int
+mu_wordsplit_c_quote_char (int c)
+{
+  char *p;
+
+  for (p = quote_transtab + sizeof (quote_transtab) - 2;
+       p > quote_transtab; p -= 2)
+    {
+      if (*p == c)
+	return p[-1];
+    }
+  return -1;
+}
+
+#define to_num(c) \
+  (ISDIGIT(c) ? c - '0' : (ISXDIGIT(c) ? toupper(c) - 'A' + 10 : 255 ))
+
+static int
+xtonum (int *pval, const char *src, int base, int cnt)
+{
+  int i, val;
+
+  for (i = 0, val = 0; i < cnt; i++, src++)
+    {
+      int n = *(unsigned char *) src;
+      if (n > 127 || (n = to_num (n)) >= base)
+	break;
+      val = val * base + n;
+    }
+  *pval = val;
+  return i;
+}
+
+size_t
+mu_wordsplit_c_quoted_length (const char *str, int quote_hex, int *quote)
+{
+  size_t len = 0;
+
+  *quote = 0;
+  for (; *str; str++)
+    {
+      if (strchr (" \"", *str))
+	*quote = 1;
+      
+      if (*str == ' ')
+	len++;
+      else if (*str == '"')
+	len += 2;
+      else if (*str != '\t' && *str != '\\' && ISPRINT (*str))
+	len++;
+      else if (quote_hex)
+	len += 3;
+      else
+	{
+	  if (mu_wordsplit_c_quote_char (*str) != -1)
+	    len += 2;
+	  else
+	    len += 4;
+	}
+    }
+  return len;
+}
+
+void
+mu_wordsplit_sh_unquote_copy (char *dst, const char *src, size_t n)
+{
+  int i;
+
+  for (i = 0; i < n;)
+    {
+      if (src[i] == '\\')
+	i++;
+      *dst++ = src[i++];
+    }
+  *dst = 0;
+}
+
+void
+mu_wordsplit_c_unquote_copy (char *dst, const char *src, size_t n)
+{
+  int i = 0;
+  int c;
+
+  while (i < n)
+    {
+      if (src[i] == '\\')
+	{
+	  ++i;
+	  if (src[i] == 'x' || src[i] == 'X')
+	    {
+	      if (n - i < 2)
+		{
+		  *dst++ = '\\';
+		  *dst++ = src[i++];
+		}
+	      else
+		{
+		  int off = xtonum (&c, src + i + 1,
+				    16, 2);
+		  if (off == 0)
+		    {
+		      *dst++ = '\\';
+		      *dst++ = src[i++];
+		    }
+		  else
+		    {
+		      *dst++ = c;
+		      i += off + 1;
+		    }
+		}
+	    }
+	  else if ((unsigned char) src[i] < 128 && ISDIGIT (src[i]))
+	    {
+	      if (n - i < 1)
+		{
+		  *dst++ = '\\';
+		  *dst++ = src[i++];
+		}
+	      else
+		{
+		  int off = xtonum (&c, src + i, 8, 3);
+		  if (off == 0)
+		    {
+		      *dst++ = '\\';
+		      *dst++ = src[i++];
+		    }
+		  else
+		    {
+		      *dst++ = c;
+		      i += off;
+		    }
+		}
+	    }
+	  else
+	    *dst++ = mu_wordsplit_c_unquote_char (src[i++]);
+	}
+      else
+	*dst++ = src[i++];
+    }
+  *dst = 0;
+}
+
+void
+mu_wordsplit_c_quote_copy (char *dst, const char *src, int quote_hex)
+{
+  for (; *src; src++)
+    {
+      if (*src == '"')
+	{
+	  *dst++ = '\\';
+	  *dst++ = *src;
+	}
+      else if (*src != '\t' && *src != '\\' && ISPRINT (*src))
+	*dst++ = *src;
+      else
+	{
+	  char tmp[4];
+
+	  if (quote_hex)
+	    {
+	      snprintf (tmp, sizeof tmp, "%%%02X", *(unsigned char *) src);
+	      memcpy (dst, tmp, 3);
+	      dst += 3;
+	    }
+	  else
+	    {
+	      int c = mu_wordsplit_c_quote_char (*src);
+	      *dst++ = '\\';
+	      if (c != -1)
+		*dst++ = c;
+	      else
+		{
+		  snprintf (tmp, sizeof tmp, "%03o", *(unsigned char *) src);
+		  memcpy (dst, tmp, 3);
+		  dst += 3;
+		}
+	    }
+	}
+    }
+}
+
+int
+mu_wordsplit_len (const char *command, size_t len, struct mu_wordsplit *wsp,
+		  int flags)
+{
+  int rc;
+  size_t start = 0;
+
+  rc = mu_wordsplit_init (wsp, command, len, flags);
+  if (rc)
+    return rc;
+
+  if (wsp->ws_flags & MU_WRDSF_SHOWDBG)
+    wsp->ws_debug ("Input:%.*s;", len, command);
+
+  if (wsp->ws_flags & MU_WRDSF_NOSPLIT)
+    {
+      /* Treat entire input as a quoted argument */
+      if (mu_wordsplit_add_segm (wsp, 0, len, _WSNF_QUOTE))
+	return wsp->ws_errno;
+    }
+  else
+    {
+      while ((rc = scan_word (wsp, start)) == _MU_WRDS_OK)
+	start = skip_delim (wsp);
+    }
+
+  if (wsp->ws_flags & MU_WRDSF_SHOWDBG)
+    {
+      wsp->ws_debug ("Initial list:");
+      mu_wordsplit_dump_nodes (wsp);
+    }
+  if (rc)
+    {
+      mu_wordsplit_free_nodes (wsp);
+      return wsp->ws_errno;
+    }
+
+  if (wsp->ws_flags & MU_WRDSF_WS)
+    {
+      /* Trim leading and trailing whitespace */
+      mu_wordsplit_trimws (wsp);
+      if (wsp->ws_flags & MU_WRDSF_SHOWDBG)
+	{
+	  wsp->ws_debug ("After WS trimming:");
+	  mu_wordsplit_dump_nodes (wsp);
+	}
+    }
+  
+  /* Expand variables (FIXME: & commands) */
+  if (!(wsp->ws_flags & MU_WRDSF_NOVAR))
+    {
+      if (mu_wordsplit_varexp (wsp))
+	{
+	  mu_wordsplit_free_nodes (wsp);
+	  return wsp->ws_errno;
+	}
+      if (wsp->ws_flags & MU_WRDSF_SHOWDBG)
+	{
+	  wsp->ws_debug ("Expanded list:");
+	  mu_wordsplit_dump_nodes (wsp);
+	}
+    }
+
+  do
+    {
+      if (wsnode_quoteremoval (wsp))
+	break;
+      if (wsp->ws_flags & MU_WRDSF_SHOWDBG)
+	{
+	  wsp->ws_debug ("After quote removal:");
+	  mu_wordsplit_dump_nodes (wsp);
+	}
+
+      if (wsnode_coalesce (wsp))
+	break;
+
+      if (wsp->ws_flags & MU_WRDSF_SHOWDBG)
+	{
+	  wsp->ws_debug ("Coalesced list:");
+	  mu_wordsplit_dump_nodes (wsp);
+	}
+
+      mu_wordsplit_finish (wsp);
+    }
+  while (0);
+  mu_wordsplit_free_nodes (wsp);
+  return wsp->ws_errno;
+}
+
+int
+mu_wordsplit (const char *command, struct mu_wordsplit *ws, int flags)
+{
+  return mu_wordsplit_len (command, strlen (command), ws, flags);
+}
+
+void
+mu_wordsplit_free (struct mu_wordsplit *ws)
+{
+  size_t i;
+
+  for (i = 0; i < ws->ws_wordc; i++)
+    {
+      char *p = ws->ws_wordv[ws->ws_offs + i];
+      if (p)
+	free (p);
+    }
+  free (ws->ws_wordv);
+  ws->ws_wordv = NULL;
+}
+
+void
+mu_wordsplit_perror (struct mu_wordsplit *wsp)
+{
+  switch (wsp->ws_errno)
+    {
+    case MU_WRDSE_EOF:
+      wsp->ws_error (_("no error"));
+      break;
+
+    case MU_WRDSE_QUOTE:
+      wsp->ws_error (_("missing closing %c (start near #%lu)"),
+		     wsp->ws_input[wsp->ws_endp],
+		     (unsigned long) wsp->ws_endp);
+      break;
+
+    case MU_WRDSE_NOSPACE:
+      wsp->ws_error (_("memory exhausted"));
+      break;
+
+    case MU_WRDSE_NOSUPP:
+      wsp->ws_error (_("command substitution is not yet supported"));
+
+    case MU_WRDSE_USAGE:
+      wsp->ws_error (_("invalid mu_wordsplit usage"));
+      break;
+
+    case MU_WRDSE_CBRACE:
+      wsp->ws_error (_("unbalanced curly brace"));
+      break;
+
+    case MU_WRDSE_UNDEF:
+      wsp->ws_error (_("undefined variable"));
+      break;
+
+    default:
+      wsp->ws_error (_("unknown error"));
+    }
+}
+
+const char *_mu_wordsplit_errstr[] = {
+  N_("no error"),
+  N_("missing closing quote"),
+  N_("memory exhausted"),
+  N_("variable expansion and command substitution " "are not yet supported"),
+  N_("invalid mu_wordsplit usage"),
+  N_("unbalanced curly brace"),
+  N_("undefined variable")
+};
+int _mu_wordsplit_nerrs =
+  sizeof (_mu_wordsplit_errstr) / sizeof (_mu_wordsplit_errstr[0]);
+
+const char *
+mu_wordsplit_strerror (struct mu_wordsplit *ws)
+{
+  if (ws->ws_errno < _mu_wordsplit_nerrs)
+    return _mu_wordsplit_errstr[ws->ws_errno];
+  return N_("unknown error");
+}
diff --git a/libmailutils/tests/.gitignore b/libmailutils/tests/.gitignore
index ec75020..0157222 100644
--- a/libmailutils/tests/.gitignore
+++ b/libmailutils/tests/.gitignore
@@ -13,3 +13,4 @@ listop
 mailcap
 url-parse
 wicket
+wsp
diff --git a/libmailutils/tests/Makefile.am b/libmailutils/tests/Makefile.am
index f714b58..e34d265 100644
--- a/libmailutils/tests/Makefile.am
+++ b/libmailutils/tests/Makefile.am
@@ -48,7 +48,8 @@ noinst_PROGRAMS = \
  listop\
  mailcap\
  url-parse\
- wicket
+ wicket\
+ wsp
 
 LDADD = ${MU_LIB_MAILUTILS}
 
@@ -70,7 +71,8 @@ TESTSUITE_AT = \
  mailcap.at\
  testsuite.at\
  url.at\
- wicket.at
+ wicket.at\
+ wordsplit.at
 
 TESTSUITE = $(srcdir)/testsuite
 M4=m4
diff --git a/libmailutils/tests/argcv.c b/libmailutils/tests/argcv.c
index 88c54a2..66e9060 100644
--- a/libmailutils/tests/argcv.c
+++ b/libmailutils/tests/argcv.c
@@ -22,6 +22,7 @@
 #include <string.h>
 #include <errno.h>
 #include <unistd.h>
+#define MU_ARCGV_DEPRECATED
 #include <mailutils/argcv.h>
 #include <mailutils/errno.h>
 
diff --git a/libmailutils/tests/listop.c b/libmailutils/tests/listop.c
index 564e1d3..89e72eb 100644
--- a/libmailutils/tests/listop.c
+++ b/libmailutils/tests/listop.c
@@ -21,7 +21,6 @@
 #include <unistd.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <mailutils/argcv.h>
 #include <mailutils/mailutils.h>
 
 static int interactive;
@@ -467,9 +466,8 @@ shell (mu_list_t list)
     {
       char *text = NULL;
       char buf[80];
-      int argc;
-      char **argv;
-
+      struct mu_wordsplit ws;
+      
       if (!itr[num])
 	{
 	  rc = mu_list_get_iterator (list, &itr[num]);
@@ -487,40 +485,44 @@ shell (mu_list_t list)
       if (fgets (buf, sizeof buf, stdin) == NULL)
 	return;
 
-      rc = mu_argcv_get (buf, "", "#", &argc, &argv);
-      if (rc)
-	lperror ("mu_argcv_get", rc);
+      ws.ws_comment = "#";
+      if (mu_wordsplit (buf, &ws, MU_WRDSF_DEFFLAGS|MU_WRDSF_COMMENT))
+	{
+	  mu_error ("cannot split line `%s': %s", buf,
+		    mu_wordsplit_strerror (&ws));
+	  exit (1);
+	}
 
-      if (argc > 0)
+      if (ws.ws_wordc > 0)
 	{
-	  if (strcmp (argv[0], "count") == 0)
+	  if (strcmp (ws.ws_wordv[0], "count") == 0)
 	    count (list);
-	  else if (strcmp (argv[0], "next") == 0)
-	    next (itr[num], argv[1]);
-	  else if (strcmp (argv[0], "first") == 0)
+	  else if (strcmp (ws.ws_wordv[0], "next") == 0)
+	    next (itr[num], ws.ws_wordv[1]);
+	  else if (strcmp (ws.ws_wordv[0], "first") == 0)
 	    mu_iterator_first (itr[num]);
-	  else if (strcmp (argv[0], "del") == 0)
-	    delete (list, argc, argv);
-	  else if (strcmp (argv[0], "add") == 0)
-	    add (list, argc, argv);
-	  else if (strcmp (argv[0], "prep") == 0)
-	    prep (list, argc, argv);
-	  else if (strcmp (argv[0], "ins") == 0)
-	    ins (list, argc, argv);
-	  else if (strcmp (argv[0], "repl") == 0)
-	    repl (list, argc, argv);
-	  else if (strcmp (argv[0], "ictl") == 0)
-	    ictl (itr[num], argc, argv);
-	  else if (strcmp (argv[0], "print") == 0)
+	  else if (strcmp (ws.ws_wordv[0], "del") == 0)
+	    delete (list, ws.ws_wordc, ws.ws_wordv);
+	  else if (strcmp (ws.ws_wordv[0], "add") == 0)
+	    add (list, ws.ws_wordc, ws.ws_wordv);
+	  else if (strcmp (ws.ws_wordv[0], "prep") == 0)
+	    prep (list, ws.ws_wordc, ws.ws_wordv);
+	  else if (strcmp (ws.ws_wordv[0], "ins") == 0)
+	    ins (list, ws.ws_wordc, ws.ws_wordv);
+	  else if (strcmp (ws.ws_wordv[0], "repl") == 0)
+	    repl (list, ws.ws_wordc, ws.ws_wordv);
+	  else if (strcmp (ws.ws_wordv[0], "ictl") == 0)
+	    ictl (itr[num], ws.ws_wordc, ws.ws_wordv);
+	  else if (strcmp (ws.ws_wordv[0], "print") == 0)
 	    print (list);
-	  else if (strcmp (argv[0], "cur") == 0)
+	  else if (strcmp (ws.ws_wordv[0], "cur") == 0)
 	    cur (num, itr[num]);
-	  else if (strcmp (argv[0], "quit") == 0)
+	  else if (strcmp (ws.ws_wordv[0], "quit") == 0)
 	    return;
-	  else if (strcmp (argv[0], "iter") == 0)
+	  else if (strcmp (ws.ws_wordv[0], "iter") == 0)
 	    {
 	      int n;
-	      if (iter (&n, argc, argv) == 0 && !itr[n])
+	      if (iter (&n, ws.ws_wordc, ws.ws_wordv) == 0 && !itr[n])
 		{
 		  rc = mu_list_get_iterator (list, &itr[n]);
 		  if (rc)
@@ -529,24 +531,24 @@ shell (mu_list_t list)
 		}
 	      num = n;
 	    }
-	  else if (strcmp (argv[0], "close") == 0)
+	  else if (strcmp (ws.ws_wordv[0], "close") == 0)
 	    {
 	      int n;
-	      if (iter (&n, argc, argv) == 0)
+	      if (iter (&n, ws.ws_wordc, ws.ws_wordv) == 0)
 		{
 		  mu_iterator_destroy (&itr[n]);
 		  if (n == num && ++num == NITR)
 		    num = 0;
 		}
 	    }
-	  else if (strcmp (argv[0], "find") == 0)
-	    find (itr[num], argv[1]);
-	  else if (strcmp (argv[0], "help") == 0)
+	  else if (strcmp (ws.ws_wordv[0], "find") == 0)
+	    find (itr[num], ws.ws_wordv[1]);
+	  else if (strcmp (ws.ws_wordv[0], "help") == 0)
 	    help ();
-	  else if (argc == 1)
+	  else if (ws.ws_wordc == 1)
 	    {
 	      char *p;
-	      size_t n = strtoul (argv[0], &p, 0);
+	      size_t n = strtoul (ws.ws_wordv[0], &p, 0);
 	      if (*p != 0)
 		fprintf (stderr, "?\n");
 	      else
@@ -561,7 +563,7 @@ shell (mu_list_t list)
 	  else
 	    fprintf (stderr, "?\n");
 	}
-      mu_argcv_free (argc, argv);
+      mu_wordsplit_free (&ws);
     }
 }
 
diff --git a/libmailutils/tests/testsuite.at b/libmailutils/tests/testsuite.at
index d5d02aa..78fb693 100644
--- a/libmailutils/tests/testsuite.at
+++ b/libmailutils/tests/testsuite.at
@@ -56,6 +56,7 @@ AT_INIT
 m4_include([list.at])
 m4_include([address.at])
 m4_include([argcv.at])
+m4_include([wordsplit.at])
 m4_include([url.at])
 m4_include([mailcap.at])
 m4_include([base64e.at])
diff --git a/libmailutils/tests/wordsplit.at b/libmailutils/tests/wordsplit.at
new file mode 100644
index 0000000..dca3792
--- /dev/null
+++ b/libmailutils/tests/wordsplit.at
@@ -0,0 +1,304 @@
+# This file is part of GNU Mailutils. -*- Autotest -*-
+# Copyright (C) 2007, 2008, 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.
+#
+# GNU Mailutils 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 GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
+
+dnl ------------------------------------------------------------
+dnl TESTWSP([NAME], [KW = `'], [OPTS], [INPUT], [STDOUT = `'],
+dnl         [STDERR = `'], [ENV])
+dnl
+m4_pushdef([TESTWSP],[
+m4_pushdef([MU_TEST_GROUP],[Wordsplit])
+m4_pushdef([MU_TEST_KEYWORDS],[wordsplit wsp])
+m4_pushdef([MU_TEST_COMMAND],[$7 wsp $3])
+MU_GENERIC_TEST([$1],[$2],[$4],[],[$5],[$6])
+m4_popdef([MU_TEST_COMMAND])
+m4_popdef([MU_TEST_KEYWORDS])
+m4_popdef([MU_TEST_GROUP])
+])
+
+dnl ------------------------------------------------------------
+dnl The first part reproduces legacy argcv tests
+dnl ------------------------------------------------------------
+
+TESTWSP([simple input],[],[],
+[1 2 3],
+[NF: 3
+0: 1
+1: 2
+2: 3
+])
+
+TESTWSP([quoted space],[],[],
+[quoted\ space],
+[NF: 1
+0: "quoted space"
+])
+
+TESTWSP([tab character],[],[],
+[a "tab	character"],
+[NF: 2
+0: a
+1: tab\tcharacter
+])
+
+TESTWSP([octal and hex escapes],[],[],
+[\157\143\164\141\154\40and\x20\x68\x65\x78],
+[NF: 1
+0: "octal and hex"
+])
+
+TESTWSP([octal and hex escapes 2],[],[],
+[\157\143\164\141\154\40 and \x20\x68\x65\x78],
+[NF: 3
+0: "octal "
+1: and
+2: " hex"
+])
+
+TESTWSP([escape representation],[],[],
+[A\x3-\48\39],
+[NF: 1
+0: A\003-\0048\0039
+])
+
+dnl ------------------------------------------------------------
+dnl Test worsplit-specific behavior
+dnl ------------------------------------------------------------
+TESTWSP([append],[],[append],
+[jeden dwa trzy
+cztery
+piec szesc],
+[NF: 3
+0: jeden
+1: dwa
+2: trzy
+NF: 4
+0: jeden
+1: dwa
+2: trzy
+3: cztery
+NF: 6
+0: jeden
+1: dwa
+2: trzy
+3: cztery
+4: piec
+5: szesc
+])
+
+TESTWSP([dooffs],[],[dooffs 3 jeden dwa trzy],
+[cztery piec],
+[NF: 2 (3)
+(0): jeden
+(1): dwa
+(2): trzy
+3: cztery
+4: piec
+])
+
+TESTWSP([variable substitutions: single var],[],[],
+[a $FOO test],
+[NF: 3
+0: a
+1: bar
+2: test
+],
+[],
+[FOO=bar])
+
+TESTWSP([variable substitutions: concatenated vars],[],[],
+[a $FOO${BAR}ent test],
+[NF: 3
+0: a
+1: stringent
+2: test
+],
+[],
+[FOO=str BAR=ing])
+
+TESTWSP([variable substitutions: field splitting],[],[],
+[a $FOO test],
+[NF: 4
+0: a
+1: variable
+2: substitution
+3: test
+],
+[],
+[FOO="variable substitution"])
+
+TESTWSP([variable substitutions: double-quoted variable],[],[],
+[a "$FOO" test],
+[NF: 3
+0: a
+1: "variable substitution"
+2: test
+],
+[],
+[FOO="variable substitution"])
+
+TESTWSP([variable substitutions: single-quoted variable],[],[],
+[a '$FOO' test],
+[NF: 3
+0: a
+1: $FOO
+2: test
+],
+[],
+[FOO="variable substitution"])
+
+TESTWSP([undefined variables 1],[],[],
+[a $FOO test a${FOO}b],
+[NF: 3
+0: a
+1: test
+2: ab
+],
+[],
+[unset FOO;])
+
+TESTWSP([undefined variables 2],[],[keepundef],
+[a $FOO test a${FOO}b],
+[NF: 4
+0: a
+1: $FOO
+2: test
+3: a${FOO}b
+],
+[],
+[unset FOO;])
+
+TESTWSP([warn about undefined variables],[],[warnundef],
+[$FOO],
+[NF: 0
+],
+[warning: undefined variable `FOO'
+],
+[unset FOO;])
+
+TESTWSP([bail out on undefined variables],[],[undef],
+[$FOO],
+[],
+[undefined variable
+],
+[unset FOO;])
+
+TESTWSP([disable variable expansion],[],[novar],
+[$FOO],
+[NF: 1
+0: $FOO
+],
+[],
+[FOO=bar])
+
+TESTWSP([nosplit with expansion],[],[nosplit],
+[a $FOO test],
+[NF: 1
+0: "a variable expansion test\n"
+],
+[],
+[FOO="variable expansion"])
+
+TESTWSP([nosplit without expansion],[],[nosplit novar],
+[a $FOO test],
+[NF: 1
+0: "a $FOO test\n"
+],
+[],
+[FOO="variable expansion"])
+
+TESTWSP([ignore quotes],[],[-quote],
+["a text"],
+[NF: 2
+0: "\"a"
+1: "text\""
+])
+
+TESTWSP([custom delimiters (squeeze)],[],[delim : -ws trimnl],
+[semicolon: separated::list: of :words],
+[NF: 5
+0: semicolon
+1: " separated"
+2: list
+3: " of "
+4: words
+])
+
+TESTWSP([custom delimiters (no squeeze)],[],[delim : -ws -squeeze_delims trimnl],
+[semicolon: separated::list: of :words],
+[NF: 6
+0: semicolon
+1: " separated"
+2: ""
+3: list
+4: " of "
+5: words
+])
+
+TESTWSP([custom, with returned delimiters],[],[delim : -ws trimnl return_delims],
+[semicolon: separated::list: of :words],
+[NF: 9
+0: semicolon
+1: :
+2: " separated"
+3: :
+4: list
+5: :
+6: " of "
+7: :
+8: words
+])
+
+TESTWSP([custom, with returned & squeezed delimiters],[],[delim : -ws trimnl return_delims -squeeze_delims],
+[semicolon: separated::list: of :words],
+[NF: 9
+0: semicolon
+1: :
+2: " separated"
+3: ::
+4: list
+5: :
+6: " of "
+7: :
+8: words
+])
+
+TESTWSP([sed expressions],[],[sed],
+[arg1 s/foo/bar/g;s/bar baz/quz quux/ arg2],
+[NF: 3
+0: arg1
+1: "s/foo/bar/g;s/bar baz/quz quux/"
+2: arg2
+])
+
+TESTWSP([C escapes on],[],[cescapes],
+[a\ttab form\ffeed and new\nline],
+[NF: 4
+0: a\ttab
+1: form\ffeed
+2: and
+3: new\nline
+])
+
+TESTWSP([C escapes off],[],[-cescapes],
+[a\ttab form\ffeed and new\nline],
+[NF: 4
+0: attab
+1: formffeed
+2: and
+3: newnline
+])
+
+m4_popdef([TESTWSP])
diff --git a/libmailutils/tests/wsp.c b/libmailutils/tests/wsp.c
new file mode 100644
index 0000000..095ce66
--- /dev/null
+++ b/libmailutils/tests/wsp.c
@@ -0,0 +1,285 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 2005, 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.
+
+   GNU Mailutils 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 GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif 
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <mailutils/wordsplit.h>
+#include <mailutils/kwd.h>
+#include <mailutils/errno.h>
+#include <mailutils/error.h>
+#include <mailutils/cstr.h>
+
+extern char **environ;
+
+struct mu_kwd bool_keytab[] = {
+  { "append", MU_WRDSF_APPEND },
+  /*{ "reuse",  MU_WRDSF_REUSE },*/
+  { "undef",  MU_WRDSF_UNDEF },
+  { "novar",  MU_WRDSF_NOVAR },
+  { "ws",     MU_WRDSF_WS },
+  { "quote",  MU_WRDSF_QUOTE },
+  { "squeeze_delims", MU_WRDSF_SQUEEZE_DELIMS },
+  { "return_delims",  MU_WRDSF_RETURN_DELIMS },
+  { "sed",     MU_WRDSF_SED_EXPR },
+  { "debug",   MU_WRDSF_SHOWDBG },
+  { "nosplit",  MU_WRDSF_NOSPLIT },
+  { "keepundef", MU_WRDSF_KEEPUNDEF },
+  { "warnundef", MU_WRDSF_WARNUNDEF },
+  { "cescapes", MU_WRDSF_CESCAPES },
+  { "default", MU_WRDSF_DEFFLAGS },
+  { NULL, 0 }
+};
+
+struct mu_kwd string_keytab[] = {
+  { "delim",   MU_WRDSF_DELIM },
+  { "comment", MU_WRDSF_COMMENT },
+  { NULL, 0 }
+};
+
+static void
+help ()
+{
+  size_t i;
+  
+  printf ("usage: wsp [options]\n");
+  printf ("options are:\n");
+  printf (" [-]trimnl\n");
+  printf (" [-]plaintext\n");
+  putchar ('\n');
+  for (i = 0; bool_keytab[i].name; i++)
+    printf (" [-]%s\n", bool_keytab[i].name);
+  putchar ('\n');
+  for (i = 0; string_keytab[i].name; i++)
+    {
+      printf (" -%s\n", bool_keytab[i].name);
+      printf (" %s ARG\n", bool_keytab[i].name);
+    }
+  putchar ('\n');
+  printf (" -dooffs\n");
+  printf (" dooffs COUNT ARGS...\n");
+  exit (0);
+}
+
+void
+print_qword (const char *word, int plaintext)
+{
+  static char *qbuf = NULL;
+  static size_t qlen = 0;
+  int quote;
+  size_t size = mu_wordsplit_c_quoted_length (word, 0, &quote);
+
+  if (plaintext)
+    {
+      printf ("%s", word);
+      return;
+    }
+
+  if (*word == 0)
+    quote = 1;
+  
+  if (size >= qlen)
+    {
+      qlen = size + 1;
+      qbuf = realloc (qbuf, qlen);
+      if (!qbuf)
+	{
+	  mu_error ("not enough memory");
+	  abort ();
+	}
+    }
+  mu_wordsplit_c_quote_copy (qbuf, word, 0);
+  qbuf[size] = 0;
+  if (quote)
+    printf ("\"%s\"", qbuf);
+  else
+    printf ("%s", qbuf);
+}
+
+int
+main (int argc, char **argv)
+{
+  char buf[1024];
+  int i, offarg = 0;
+  int trimnl_option = 0;
+  int plaintext_option = 0;
+  int wsflags = (MU_WRDSF_DEFFLAGS & ~MU_WRDSF_NOVAR) |
+                 MU_WRDSF_ENOMEMABRT |
+                 MU_WRDSF_ENV | MU_WRDSF_SHOWERR;
+  struct mu_wordsplit ws;
+
+  for (i = 1; i < argc; i++)
+    {
+      char *opt = argv[i];
+      int negate;
+      int flag;
+
+      if (opt[0] == '-')
+	{
+	  negate = 1;
+	  opt++;
+	}
+      else if (opt[0] == '+')
+	{
+	  negate = 0;
+	  opt++;
+	}
+      else
+	negate = 0;
+
+      if (strcmp (opt, "h") == 0 ||
+	  strcmp (opt, "help") == 0 ||
+	  strcmp (opt, "-help") == 0)
+	{
+	  help ();
+	}
+	  
+      if (strcmp (opt, "trimnl") == 0)
+	{
+	  trimnl_option = !negate;
+	  continue;
+	}
+
+      if (strcmp (opt, "plaintext") == 0)
+	{
+	  plaintext_option = !negate;
+	  continue;
+	}
+
+      if (mu_kwd_xlat_name (bool_keytab, opt, &flag) == 0)
+	{
+	  if (negate)
+	    wsflags &= ~flag;
+	  else
+	    wsflags |= flag;
+	  continue;
+	}
+
+      if (mu_kwd_xlat_name (string_keytab, opt, &flag) == 0)
+	{
+	  if (negate)
+	    wsflags &= ~flag;
+	  else
+	    {
+	      i++;
+	      if (i == argc)
+		{
+		  mu_error ("%s missing argument", opt);
+		  exit (1);
+		}
+	      
+	      switch (flag)
+		{
+		case MU_WRDSF_DELIM:
+		  ws.ws_delim = argv[i];
+		  break;
+
+		case MU_WRDSF_COMMENT:
+		  ws.ws_comment = argv[i];
+		  break;
+		}
+	      
+	      wsflags |= flag;
+	    }
+	  continue;
+	}
+
+      if (strcmp (opt, "dooffs") == 0)
+	{
+	  if (negate)
+	    wsflags &= ~MU_WRDSF_DOOFFS;
+	  else
+	    {
+	      char *p;
+
+	      i++;
+	      
+	      if (i == argc)
+		{
+		  mu_error ("%s missing arguments", opt);
+		  exit (1);
+		}
+	      ws.ws_offs = strtoul (argv[i], &p, 10);
+	      if (*p)
+		{
+		  mu_error ("invalid number: %s", argv[i]);
+		  exit (1);
+		}
+
+	      i++;
+	      if (i + ws.ws_offs > argc)
+		{
+		  mu_error ("%s: not enough arguments", opt);
+		  exit (1);
+		}
+	      offarg = i;
+	      i += ws.ws_offs - 1;
+	      wsflags |= MU_WRDSF_DOOFFS;
+	    }
+	  continue;
+	}
+
+      mu_error ("%s: unrecognized argument", opt);
+      exit (1);
+    }
+
+  ws.ws_env = (const char **) environ;
+	
+  while (fgets (buf, sizeof (buf), stdin))
+    {
+      int rc;
+      size_t i;
+      
+      if (trimnl_option)
+	mu_rtrim_cset (buf, "\n");
+      rc = mu_wordsplit (buf, &ws, wsflags);
+      if (rc)
+	{
+	  if (!(wsflags & MU_WRDSF_SHOWERR))
+	    mu_wordsplit_perror (&ws);
+	  continue;
+	}
+	  
+      if (offarg)
+	{
+	  for (i = 0; i < ws.ws_offs; i++)
+	    ws.ws_wordv[i] = argv[offarg + i];
+	  offarg = 0;
+	}
+      
+      wsflags |= MU_WRDSF_REUSE;
+      printf ("NF: %lu", (unsigned long) ws.ws_wordc);
+      if (wsflags & MU_WRDSF_DOOFFS)
+	printf (" (%lu)", (unsigned long) ws.ws_offs);
+      putchar ('\n');
+      for (i = 0; i < ws.ws_offs; i++)
+	{
+	  printf ("(%lu): ", (unsigned long) i);
+	  print_qword (ws.ws_wordv[i], plaintext_option);
+	  putchar ('\n');
+	}
+      for (; i < ws.ws_offs + ws.ws_wordc; i++)
+	{
+	  printf ("%lu: ", (unsigned long) i);
+	  print_qword (ws.ws_wordv[i], plaintext_option);
+	  putchar ('\n');
+	}
+    }
+  return 0;
+}
diff --git a/libmailutils/url/copy.c b/libmailutils/url/copy.c
index 7190b42..ee5ad47 100644
--- a/libmailutils/url/copy.c
+++ b/libmailutils/url/copy.c
@@ -24,7 +24,7 @@
 #ifdef HAVE_STRINGS_H
 # include <strings.h>
 #endif
-
+#include <stdlib.h>
 #include <mailutils/types.h>
 #include <mailutils/argcv.h>
 #include <mailutils/secret.h>
diff --git a/libmailutils/url/create.c b/libmailutils/url/create.c
index 674a524..0d0cb30 100644
--- a/libmailutils/url/create.c
+++ b/libmailutils/url/create.c
@@ -26,10 +26,12 @@
 #ifdef HAVE_STRINGS_H
 # include <strings.h>
 #endif
+#include <stdlib.h>
 #include <netdb.h>
 #include <arpa/inet.h>
 #include <limits.h>
 
+#include <mailutils/wordsplit.h>
 #include <mailutils/util.h>
 #include <mailutils/errno.h>
 #include <mailutils/argcv.h>
@@ -375,19 +377,24 @@ _mu_url_create_internal (struct mu_url_ctx *ctx, mu_url_t hint)
 
   if ((ctx->flags & MU_URL_PARSE_PIPE) && ctx->input[0] == '|')
     {
+      struct mu_wordsplit ws;
+      
       rc = str_assign (&url->scheme, "prog");
       if (rc)
 	return rc;
       url->flags |= MU_URL_SCHEME;
       ctx->flags &= ~MU_URL_PARSE_HEXCODE;
-      rc = mu_argcv_get (ctx->input + 1, NULL, NULL, &url->qargc, &url->qargv);
+      if (mu_wordsplit (ctx->input + 1, &ws, MU_WRDSF_DEFFLAGS))
+	return errno;
+      url->qargc = ws.ws_wordc;
+      url->qargv = ws.ws_wordv;
+      ws.ws_wordc = 0;
+      ws.ws_wordv = NULL;
+      mu_wordsplit_free (&ws);
+      url->flags |= MU_URL_QUERY;
+      rc = str_assign (&url->path, url->qargv[0]);
       if (rc == 0)
-	{
-	  url->flags |= MU_URL_QUERY;
-	  rc = str_assign (&url->path, url->qargv[0]);
-	  if (rc == 0)
-	    url->flags |= MU_URL_PATH;
-	}
+	url->flags |= MU_URL_PATH;
     }
   else if ((ctx->flags & MU_URL_PARSE_SLASH) && ctx->input[0] == '/')
     {
diff --git a/libmu_argp/muinit.c b/libmu_argp/muinit.c
index ab6e9c9..7bffdea 100644
--- a/libmu_argp/muinit.c
+++ b/libmu_argp/muinit.c
@@ -22,6 +22,7 @@
 #include <unistd.h>
 #include <stdlib.h>
 #include <mailutils/stream.h>
+#include <mailutils/io.h>
 #include "xalloc.h"
 #include <string.h>
 #ifdef MU_ALPHA_RELEASE
diff --git a/libmu_auth/ldap.c b/libmu_auth/ldap.c
index 250aa40..435c99d 100644
--- a/libmu_auth/ldap.c
+++ b/libmu_auth/ldap.c
@@ -19,15 +19,18 @@
 #ifdef HAVE_CONFIG_H
 # include <config.h>
 #endif
+#include <stdlib.h>
 #include <unistd.h>
 #include <stdio.h>
 #include <ctype.h>
 
 #include <mailutils/mu_auth.h>
 #include <mailutils/cstr.h>
+#include <mailutils/io.h>
 
 #ifdef WITH_LDAP
 #include "mailutils/argcv.h"
+#include "mailutils/wordsplit.h"
 #include "mailutils/assoc.h"
 #include "mailutils/list.h"
 #include "mailutils/iterator.h"
@@ -129,10 +132,9 @@ _mu_conn_setup (LDAP **pld)
 	    {
 	      /* if no host but a DN is provided, try DNS SRV to gather the
 		 host list */
-	      char	*domain = NULL, *hostlist = NULL, **hosts = NULL;
-	      int hostcnt;
-	      int i;
-	      int len_proto = strlen(lud->lud_scheme);
+	      char *domain = NULL, *hostlist = NULL;
+	      size_t i;
+	      struct mu_wordsplit ws;
 	      
 	      if (ldap_dn2domain (lud->lud_dn, &domain) || !domain)
 		{
@@ -148,16 +150,15 @@ _mu_conn_setup (LDAP **pld)
 			    domain);
 		  goto dnssrv_free;
 		}
-	      
-	      rc = mu_argcv_get (hostlist, " ", NULL, &hostcnt, &hosts);
-	      if (rc)
+
+	      if (mu_wordsplit (hostlist, &ws, MU_WRDSF_DEFFLAGS))
 		{
 		  mu_error (_("DNS SRV: could not parse hostlist=\"%s\": %s"),
-			    hostlist, mu_strerror (rc));
+			    hostlist, mu_wordsplit_strerror (&ws));
 		  goto dnssrv_free;
 		}
 	      
-	      tmp = realloc (urls, sizeof(char *) * (nurls + hostcnt + 1));
+	      tmp = realloc (urls, sizeof(char *) * (nurls + ws.ws_wordc + 1));
 	      if (!tmp)
 		{
 		  mu_error ("DNS SRV %s", mu_strerror (errno));
@@ -167,28 +168,23 @@ _mu_conn_setup (LDAP **pld)
 	      urls = tmp;
 	      urls[nurls] = NULL;
 	      
-	      for (i = 0; i < hostcnt; i++)
+	      for (i = 0; i < ws.ws_wordc; i++)
 		{
-		  size_t len = len_proto + sizeof "://" - 1
-		               + strlen (hosts[i])
-			       + 1;
-
 		  urls[nurls + i + 1] = NULL;
-		  urls[nurls + i] = malloc (len);
-		  if (!urls[nurls + i])
+		  rc = mu_asprintf (&urls[nurls + i],
+				    "%s://%s",
+				    lud->lud_scheme, ws.ws_wordv[i]);
+		  if (rc)
 		    {
-		      mu_error ("DNS SRV %s", mu_strerror (errno));
+		      mu_error ("DNS SRV %s", mu_strerror (rc));
 		      goto dnssrv_free;
 		    }
-		  
-		  snprintf (urls[nurls + i], len, "%s://%s",
-			    lud->lud_scheme, hosts[i]);
 		}
 	      
 	      nurls += i;
 	      
 	    dnssrv_free:
-	      mu_argcv_free (hostcnt, hosts);
+	      mu_wordsplit_free (&ws);
 	      ber_memfree (hostlist);
 	      ber_memfree (domain);
 	    }
diff --git a/libmu_auth/radius.c b/libmu_auth/radius.c
index 048a9c7..8c8a648 100644
--- a/libmu_auth/radius.c
+++ b/libmu_auth/radius.c
@@ -35,13 +35,14 @@
 #include <mailutils/iterator.h>
 #include <mailutils/mailbox.h>
 #include <mailutils/radius.h>
-#include <mailutils/argcv.h>
+#include <mailutils/wordsplit.h>
 #include <mailutils/mu_auth.h>
 #include <mailutils/error.h>
 #include <mailutils/errno.h>
 #include <mailutils/nls.h>
 #include <mailutils/vartab.h>
 #include <mailutils/io.h>
+#include <mailutils/cctype.h>
 
 #ifdef ENABLE_RADIUS
 
@@ -87,9 +88,8 @@ enum parse_state
 int
 parse_pairlist (grad_avp_t **plist, char *input)
 {
-  int rc;
-  int i, argc;
-  char **argv;
+  size_t i;
+  struct mu_wordsplit ws;
   enum parse_state state;
   grad_locus_t loc;
   char *name;
@@ -98,34 +98,38 @@ parse_pairlist (grad_avp_t **plist, char *input)
   if (!input)
     return 1;
 
-  if ((rc = mu_argcv_get (input, ",", NULL, &argc, &argv)))
+  ws.ws_delim = ",";
+  if (mu_wordsplit (input, &ws,
+		    MU_WRDSF_DEFFLAGS|MU_WRDSF_DELIM|MU_WRDSF_RETURN_DELIMS))
     {
-      mu_error (_("cannot parse input `%s': %s"), input, mu_strerror (rc));
+      mu_error (_("cannot parse input `%s': %s"), input,
+		mu_wordsplit_strerror (&ws));
       return 1;
     }
 
   loc.file = "<configuration>"; /*FIXME*/
   loc.line = 0;
 
-  for (i = 0, state = state_lhs; i < argc; i++)
+  for (i = 0, state = state_lhs; i < ws.ws_wordc; i++)
     {
       grad_avp_t *pair;
 
       switch (state)
 	{
 	case state_lhs:
-	  name = argv[i];
+	  name = ws.ws_wordv[i];
 	  state = state_op;
 	  break;
 
 	case state_op:
-	  op = argv[i];
+	  op = ws.ws_wordv[i];
 	  state = state_rhs;
 	  break;
 
 	case state_rhs:
 	  loc.line = i; /* Just to keep track of error location */
-	  pair = grad_create_pair (&loc, name, grad_operator_equal, argv[i]);
+	  pair = grad_create_pair (&loc, name, grad_operator_equal,
+				   ws.ws_wordv[i]);
 	  if (!pair)
 	    {
 	      mu_error (_("cannot create radius A/V pair `%s'"), name);
@@ -136,22 +140,22 @@ parse_pairlist (grad_avp_t **plist, char *input)
 	  break;
 
 	case state_delim:
-	  if (strcmp (argv[i], ","))
+	  if (strcmp (ws.ws_wordv[i], ","))
 	    {
-	      mu_error (_("expected `,' but found `%s'"), argv[i]);
+	      mu_error (_("expected `,' but found `%s'"), ws.ws_wordv[i]);
 	      return 1;
 	    }
 	  state = state_lhs;
 	}
     }
-
+  mu_wordsplit_free (&ws);
+  
   if (state != state_delim && state != state_delim)
     {
       mu_error (_("malformed radius A/V list"));
       return 1;
     }
 
-  mu_argcv_free (argc, argv);
   return 0;
 }
 
diff --git a/libmu_sieve/sieve.l b/libmu_sieve/sieve.l
index 2f60e13..27a92d4 100644
--- a/libmu_sieve/sieve.l
+++ b/libmu_sieve/sieve.l
@@ -31,7 +31,7 @@
 #include <errno.h>
 #include <string.h>
 #include <mailutils/cctype.h>
-#include <mailutils/argcv.h>
+#include <mailutils/wordsplit.h>
 #include <sieve-priv.h>
 #include <sieve-gram.h>
   
@@ -640,7 +640,7 @@ str_unescape (char *text, size_t len)
 {
   char *str = mu_sieve_alloc (len);
   memcpy (str, text, len - 2);
-  str[len - 2] = mu_argcv_unquote_char (text[len - 1]);
+  str[len - 2] = mu_wordsplit_c_unquote_char (text[len - 1]);
   str[len - 1] = 0;
   return str;
 }
diff --git a/libproto/mailer/mbox.c b/libproto/mailer/mbox.c
index 1353d42..132e3d8 100644
--- a/libproto/mailer/mbox.c
+++ b/libproto/mailer/mbox.c
@@ -29,7 +29,7 @@
 #include <mailutils/mailer.h>
 #include <mailutils/url.h>
 #include <mailutils/util.h>
-#include <mailutils/argcv.h>
+#include <mailutils/wordsplit.h>
 #include <mailutils/message.h>
 #include <mailutils/envelope.h>
 #include <mailutils/header.h>
@@ -124,8 +124,8 @@ static int
 parse_received (mu_header_t hdr, char **sptr)
 {
   const char *recv;
-  int wc, i;
-  char **ws;
+  size_t i;
+  struct mu_wordsplit ws;
   enum { rcv_init, rcv_from, rcv_by, rcv_for } state;
   int status;
   char *s;
@@ -134,27 +134,26 @@ parse_received (mu_header_t hdr, char **sptr)
   status = mu_header_sget_value (hdr, MU_HEADER_RECEIVED, &recv);
   if (status)
     return status;
-  status = mu_argcv_get (recv, NULL, NULL, &wc, &ws);
-  if (status)
+  if (mu_wordsplit (recv, &ws, MU_WRDSF_DEFFLAGS))
     return status;
 
   state = rcv_init;
-  for (i = 0; i < wc && state != rcv_for; i++)
+  for (i = 0; i < ws.ws_wordc && state != rcv_for; i++)
     {
       switch (state)
 	{
 	case rcv_init:
-	  if (strcmp (ws[i], "from") == 0)
+	  if (strcmp (ws.ws_wordv[i], "from") == 0)
 	    state = rcv_from;
 	  break;
 	  
 	case rcv_from:
-	  if (strcmp (ws[i], "by") == 0)
+	  if (strcmp (ws.ws_wordv[i], "by") == 0)
 	    state = rcv_by;
 	  break;
 	  
 	case rcv_by:
-	  if (strcmp (ws[i], "for") == 0)
+	  if (strcmp (ws.ws_wordv[i], "for") == 0)
 	    state = rcv_for;
 	  break;
 
@@ -163,10 +162,10 @@ parse_received (mu_header_t hdr, char **sptr)
 	}
     }
 
-  if (state != rcv_for || ws[i] == NULL)
+  if (state != rcv_for || ws.ws_wordv[i] == NULL)
     return MU_ERR_NOENT;
 
-  s = ws[i];
+  s = ws.ws_wordv[i];
   len = strlen (s);
   if (s[len - 1] == ';')
     len--;
@@ -183,7 +182,7 @@ parse_received (mu_header_t hdr, char **sptr)
       memcpy (*sptr, s, len);
       (*sptr)[len - 1] = 0;
     }
-  mu_argcv_free (wc, ws);
+  mu_wordsplit_free (&ws);
   return status;
 } 
   
@@ -262,6 +261,7 @@ remote_mbox_append_message (mu_mailbox_t mbox, mu_message_t msg)
 	  const char *hstr;
 	  int hc; 
 	  char **hv;
+	  struct mu_wordsplit ws;
 	  
 	  if (mu_url_sget_param (mbox->url, "recipient-headers", &hstr) == 0)
 	    {
@@ -272,10 +272,12 @@ remote_mbox_append_message (mu_mailbox_t mbox, mu_message_t msg)
 		}
 	      else
 		{
-		  status = mu_argcv_get_np (hstr, strlen (hstr), ",", NULL, 0,
-					    &hc, &hv, NULL);
-		  if (status)
-		    return status;
+		  ws.ws_delim = ",";
+		  if (mu_wordsplit (hstr, &ws,
+				    MU_WRDSF_DEFFLAGS|MU_WRDSF_DELIM|MU_WRDSF_WS))
+		    return errno;
+		  hc = 1;
+		  hv = ws.ws_wordv;
 		}
 	    }
 	  else
@@ -286,7 +288,7 @@ remote_mbox_append_message (mu_mailbox_t mbox, mu_message_t msg)
 	  
 	  status = guess_message_recipient (msg, hv, &rcpt);
 	  if (hc)
-	    mu_argcv_free (hc, hv);
+	    mu_wordsplit_free (&ws);
 	}
 
       if (status != MU_ERR_NOENT)
diff --git a/libproto/mailer/smtp.c b/libproto/mailer/smtp.c
index 7196288..d4c5879 100644
--- a/libproto/mailer/smtp.c
+++ b/libproto/mailer/smtp.c
@@ -31,8 +31,9 @@
 #include <string.h>
 #include <unistd.h>
 
+#include <mailutils/nls.h>
 #include <mailutils/address.h>
-#include <mailutils/argcv.h>
+#include <mailutils/wordsplit.h>
 #include <mailutils/debug.h>
 #include <mailutils/errno.h>
 #include <mailutils/header.h>
@@ -95,18 +96,19 @@ struct _smtp_mailer
 static void
 smtp_mailer_add_auth_mech (struct _smtp_mailer *smtp_mailer, const char *str)
 {
-  int mc, i, rc;
-  char **mv;
-  
-  rc = mu_argcv_get_np (str, strlen (str),
-			",", NULL,
-			0,
-			&mc, &mv, NULL);
-  if (rc == 0)
-    for (i = 0; i < mc; i++)
-      mu_smtp_add_auth_mech (smtp_mailer->smtp, mv[i]);
-  
-  free (mv);
+  struct mu_wordsplit ws;
+
+  ws.ws_delim = ",";
+  if (mu_wordsplit (str, &ws, MU_WRDSF_DEFFLAGS|MU_WRDSF_DELIM|MU_WRDSF_WS))
+    mu_error (_("cannot split line `%s': %s"), str,
+	      mu_wordsplit_strerror (&ws));
+  else
+    {
+      size_t i;
+      for (i = 0; i < ws.ws_wordc; i++)
+	mu_smtp_add_auth_mech (smtp_mailer->smtp, ws.ws_wordv[i]);
+      mu_wordsplit_free (&ws);
+    }
 }
 
 static int
diff --git a/libproto/mailer/smtp_gsasl.c b/libproto/mailer/smtp_gsasl.c
index b8e2638..ccf1a7f 100644
--- a/libproto/mailer/smtp_gsasl.c
+++ b/libproto/mailer/smtp_gsasl.c
@@ -21,7 +21,8 @@
 #include <errno.h>
 #include <stdlib.h>
 #include <string.h>
-#include <mailutils/argcv.h>
+#include <mailutils/nls.h>
+#include <mailutils/wordsplit.h>
 #include <mailutils/diag.h>
 #include <mailutils/errno.h>
 #include <mailutils/cctype.h>
@@ -39,9 +40,8 @@ get_implemented_mechs (Gsasl *ctx, mu_list_t *plist)
   char *listmech;
   mu_list_t supp = NULL;
   int rc;
-  int mechc;
-  char **mechv;
-    
+  struct mu_wordsplit ws;
+  
   rc =  gsasl_server_mechlist (ctx, &listmech);
   if (rc != GSASL_OK)
     {
@@ -51,19 +51,26 @@ get_implemented_mechs (Gsasl *ctx, mu_list_t *plist)
       return 1;
     }
 
-  rc = mu_argcv_get (listmech, "", NULL, &mechc, &mechv);
-  if (rc == 0)
+  if (mu_wordsplit (listmech, &ws, MU_WRDSF_DEFFLAGS))
     {
-      int i;
-
+      mu_error (_("cannot split line `%s': %s"), listmech,
+		mu_wordsplit_strerror (&ws));
+      rc = errno;
+    }
+  else
+    {
+      size_t i;
+      
       rc = mu_list_create (&supp);
       if (rc == 0)
 	{
 	  mu_list_set_destroy_item (supp, mu_list_free_item);
-	  for (i = 0; i < mechc; i++) 
-	    mu_list_append (supp, mechv[i]);
+	  for (i = 0; i < ws.ws_wordc; i++) 
+	    mu_list_append (supp, ws.ws_wordv[i]);
 	}
-      free (mechv);
+      ws.ws_wordc = 0;
+      ws.ws_wordv = NULL;
+      mu_wordsplit_free (&ws);
     }
   free (listmech);
   *plist = supp;
diff --git a/mail/mail.h b/mail/mail.h
index 3b33a52..0c344ea 100644
--- a/mail/mail.h
+++ b/mail/mail.h
@@ -77,6 +77,7 @@
 #include <mailutils/nls.h>
 #include <mailutils/tls.h>
 #include <mailutils/argcv.h>
+#include <mailutils/wordsplit.h>
 #include <mailutils/cctype.h>
 #include <mailutils/cstr.h>
 #include <mailutils/io.h>
diff --git a/mail/mailline.c b/mail/mailline.c
index 95e3243..c420757 100644
--- a/mail/mailline.c
+++ b/mail/mailline.c
@@ -230,32 +230,38 @@ ml_reread (const char *prompt, char **text)
 char **
 ml_command_completion (char *cmd, int start, int end)
 {
-  int argc;
-  char **argv;
   char **ret;
   char *p;
-
+  struct mu_wordsplit ws;
+  
   for (p = rl_line_buffer; p < rl_line_buffer + start && mu_isblank (*p); p++)
     ;
-  
-  if (mu_argcv_get_n (p, end, NULL, NULL, &argc, &argv))
-    return NULL;
+
+  if (mu_wordsplit_len (p, end, &ws, MU_WRDSF_DEFFLAGS))
+    {
+      mu_error (_("mu_wordsplit_len failed: %s"),
+		mu_wordsplit_strerror (&ws));
+      return NULL;
+    }
   rl_completion_append_character = ' ';
   
-  if (argc == 0 || (argc == 1 && strlen (argv[0]) <= end - start))
+  if (ws.ws_wordc == 0 ||
+      (ws.ws_wordc == 1 && strlen (ws.ws_wordv[0]) <= end - start))
     {
       ret = rl_completion_matches (cmd, ml_command_generator);
       rl_attempted_completion_over = 1;
     }
   else
     {
-      const struct mail_command_entry *entry = mail_find_command (argv[0]);
+      const struct mail_command_entry *entry =
+	mail_find_command (ws.ws_wordv[0]);
       if (entry && entry->command_completion)
-	ret = entry->command_completion (argc, argv, start == end);
+	ret = entry->command_completion (ws.ws_wordc, ws.ws_wordv,
+					 start == end);
       else
 	ret = NULL;
     }
-  mu_argcv_free (argc, argv);
+  mu_wordsplit_free (&ws);
   return ret;
 }
 
diff --git a/mail/mailvar.c b/mail/mailvar.c
index 7f163ae..4980d4c 100644
--- a/mail/mailvar.c
+++ b/mail/mailvar.c
@@ -579,9 +579,7 @@ set_mailbox_debug_level (mu_log_level_t level)
 static void
 set_debug (struct mailvar_variable *var)
 {
-  int rc;
-  int argc;
-  char **argv;
+  struct mu_wordsplit ws;
   int i;
   mu_debug_t dbg;
 
@@ -600,19 +598,25 @@ set_debug (struct mailvar_variable *var)
     }
   
   mu_diag_get_debug (&dbg);
-  
-  rc = mu_argcv_get (var->value.string, ";", NULL, &argc, &argv);
-  if (rc)
+
+  ws.ws_delim = ";";
+
+  if (mu_wordsplit (var->value.string, &ws,
+		    MU_WRDSF_NOCMD | MU_WRDSF_NOVAR |
+		    MU_WRDSF_SQUEEZE_DELIMS |
+		    MU_WRDSF_DELIM | MU_WRDSF_WS |
+		    MU_WRDSF_ERROR))
     {
-      mu_error (_("Cannot parse string: %s"), mu_strerror (rc));
+      mu_error (_("%s failed: %s"), "mu_wordsplit",
+		mu_wordsplit_strerror (&ws));
       return;
     }
 
-  for (i = 0; i < argc; i++)
+  for (i = 0; i < ws.ws_wordc; i++)
     {
       char *p;
       mu_log_level_t level = MU_DEBUG_INHERIT;
-      char *object_name = argv[i];
+      char *object_name = ws.ws_wordv[i];
       
       for (p = object_name; *p && *p != '='; p++)
 	;
@@ -629,6 +633,7 @@ set_debug (struct mailvar_variable *var)
 	set_mailbox_debug_level (level);
       mu_global_debug_set_level (object_name, level);
     }
+  mu_wordsplit_free (&ws);
 }
 
 
diff --git a/mail/send.c b/mail/send.c
index 618f6a8..4594309 100644
--- a/mail/send.c
+++ b/mail/send.c
@@ -504,32 +504,33 @@ mail_send0 (compose_env_t * env, int save_to)
 	    }
 	  else
 	    {
-	      int argc;
-	      char **argv;
+	      struct mu_wordsplit ws;
 	      int status;
 
 	      ofile = env->file;
 
-	      if (mu_argcv_get (buf + 1, "", NULL, &argc, &argv) == 0)
+	      if (mu_wordsplit (buf + 1, &ws, MU_WRDSF_DEFFLAGS) == 0)
 		{
-		  if (argc > 0)
+		  if (ws.ws_wordc > 0)
 		    {
 		      const struct mail_escape_entry *entry = 
-			            mail_find_escape (argv[0]);
+			mail_find_escape (ws.ws_wordv[0]);
 
 		      if (entry)
-			status = (*entry->escfunc) (argc, argv, env);
+			status = (*entry->escfunc) (ws.ws_wordc, ws.ws_wordv,
+						    env);
 		      else
-			util_error (_("Unknown escape %s"), argv[0]);
+			util_error (_("Unknown escape %s"), ws.ws_wordv[0]);
 		    }
 		  else
 		    util_error (_("Unfinished escape"));
+		  mu_wordsplit_free (&ws);
 		}
 	      else
 		{
-		  util_error (_("Cannot parse escape sequence"));
+		  util_error (_("Cannot parse escape sequence: %s"),
+			      mu_wordsplit_strerror (&ws));
 		}
-	      mu_argcv_free (argc, argv);
 
 	      ofile = env->ofile;
 	    }
diff --git a/mail/util.c b/mail/util.c
index f337fe7..393504a 100644
--- a/mail/util.c
+++ b/mail/util.c
@@ -40,8 +40,9 @@
 int
 util_do_command (const char *fmt, ...)
 {
-  int argc = 0;
-  char **argv = NULL;
+  struct mu_wordsplit ws;
+  int argc;
+  char **argv;
   int status = 0;
   const struct mail_command_entry *entry = NULL;
   char *cmd = NULL;
@@ -76,19 +77,29 @@ util_do_command (const char *fmt, ...)
 	    return 0;
 	}
 
-      if (mu_argcv_get (cmd, NULL, NULL, &argc, &argv) == 0 && argc > 0)
+      ws.ws_offs = 1; /* Keep one extra slot in case we need it
+			 for expansion (see below) */
+      if (mu_wordsplit (cmd, &ws, MU_WRDSF_DEFFLAGS|MU_WRDSF_DOOFFS))
+	{
+	  mu_error (_("%s failed: %s"), "mu_wordsplit",
+		    mu_wordsplit_strerror (&ws));
+	}
+      else
 	{
 	  char *p;
 
+	  argc = ws.ws_wordc;
+	  argv = ws.ws_wordv + 1;
+	  
 	  /* Special case: a number alone implies "print" */
 	  if (argc == 1
 	      && ((strtoul (argv[0], &p, 10) > 0 && *p == 0)
 		  || (argv[0][1] == 0 && strchr("^$", argv[0][0]))))
 	    {
-	      mu_asprintf (&p, "print %s", argv[0]);
-	      mu_argcv_free (argc, argv);
-	      mu_argcv_get (p, NULL, NULL, &argc, &argv);
-	      free (p);
+	      /* Use the extra slot for "print" command */
+	      argc++;
+	      argv--;
+	      argv[0] = "print";
 	    }
 
 	  entry = mail_find_command (argv[0]);
@@ -114,12 +125,16 @@ util_do_command (const char *fmt, ...)
       
       if (strlen (p))
 	{
+	  /* Expand contracted form.  That's what we have kept an extra
+	     ws slot for. */
 	  argc++;
-	  argv = xrealloc (argv, (argc + 1)*sizeof argv[0]);
-	  if (argc > 2)
-	    memmove (argv + 2, argv + 1, (argc - 2)*sizeof argv[0]);
+	  argv--;
+	  argv[0] = argv[1];
 	  argv[1] = xstrdup (p);
 	  *p = 0;
+	  /* Register the new entry in WS */
+	  ws.ws_wordc++;
+	  ws.ws_offs = 0;
 	}
       
       entry = mail_find_command (argv[0]);
@@ -140,7 +155,7 @@ util_do_command (const char *fmt, ...)
       status = 1;
     }
 
-  mu_argcv_free (argc, argv);
+  mu_wordsplit_free (&ws);
   return status;
 }
 
diff --git a/mh/folder.c b/mh/folder.c
index 6175594..5dba885 100644
--- a/mh/folder.c
+++ b/mh/folder.c
@@ -519,18 +519,27 @@ action_list ()
 static void
 get_stack (int *pc, char ***pv)
 {
-  int status;
+  struct mu_wordsplit ws;
   const char *stack = mh_global_context_get ("Folder-Stack", NULL);
   if (!stack)
     {
       *pc = 0;
       *pv = NULL;
     }
-  else if ((status = mu_argcv_get (stack, NULL, "#", pc, pv)) != 0)
+  else if (mu_wordsplit (stack, &ws, MU_WRDSF_DEFFLAGS))
     {
-      mu_diag_funcall (MU_DIAG_ERROR, "mu_argcv_get", stack, status);
+      mu_error (_("cannot split line `%s': %s"), stack,
+		mu_wordsplit_strerror (&ws));
       exit (1);
     }
+  else
+    {
+      *pc = ws.ws_wordc;
+      *pv = ws.ws_wordv;
+      ws.ws_wordc = 0;
+      ws.ws_wordv = NULL;
+      mu_wordsplit_free (&ws);
+    }
 }
 
 static void
@@ -762,26 +771,32 @@ pack_xlate (struct pack_tab *pack_tab, size_t count, size_t n)
 static int
 _fixup (const char *name, const char *value, struct fixup_data *fd, int flags)
 {
-  int i, j, argc;
-  char **argv;
+  size_t i, j;
+  struct mu_wordsplit ws;
   mh_msgset_t msgset;
 
   if (verbose)
     fprintf (stderr, "Sequence `%s'...\n", name);
-  
-  if (mu_argcv_get (value, "", NULL, &argc, &argv))
-    return 0;
 
-  msgset.list = xcalloc (argc, sizeof msgset.list[0]);
-  for (i = j = 0; i < argc; i++)
+  if (mu_wordsplit (value, &ws, MU_WRDSF_DEFFLAGS))
+    {
+      mu_error (_("cannot split line `%s': %s"), value,
+		mu_wordsplit_strerror (&ws));
+      return 0;
+    }
+
+  msgset.list = xcalloc (ws.ws_wordc, sizeof msgset.list[0]);
+  for (i = j = 0; i < ws.ws_wordc; i++)
     {
       size_t n = pack_xlate (fd->pack_tab, fd->count,
-			     strtoul (argv[i], NULL, 0));
+			     strtoul (ws.ws_wordv[i], NULL, 0));
       if (n)
 	msgset.list[j++] = n;
     }
   msgset.count = j;
 
+  mu_wordsplit_free (&ws);
+  
   mh_seq_add (name, &msgset, flags | SEQ_ZERO);
   free (msgset.list);
 
diff --git a/mh/mh.h b/mh/mh.h
index 0ed009c..17289c0 100644
--- a/mh/mh.h
+++ b/mh/mh.h
@@ -49,6 +49,7 @@
 #include <mailutils/errno.h>
 #include <mailutils/nls.h>
 #include <mailutils/argcv.h>
+#include <mailutils/wordsplit.h>
 #include <mailutils/debug.h>
 #include <mailutils/mailer.h>
 #include <mailutils/envelope.h>
diff --git a/mh/mh_alias.y b/mh/mh_alias.y
index e34509c..d89f24a 100644
--- a/mh/mh_alias.y
+++ b/mh/mh_alias.y
@@ -496,16 +496,20 @@ mh_read_aliases ()
   p = mh_global_profile_get ("Aliasfile", NULL);
   if (p)
     {
-      int argc;
-      char **argv;
-      int rc = mu_argcv_get (p, NULL, NULL, &argc, &argv);
-      if (rc == 0)
+      struct mu_wordsplit ws;
+
+      if (mu_wordsplit (p, &ws, MU_WRDSF_DEFFLAGS))
+	{
+	  mu_error (_("cannot split line `%s': %s"), p,
+		    mu_wordsplit_strerror (&ws));
+	}
+      else
 	{
-	  int i;
-	  for (i = 0; i < argc; i++) 
-	    mh_alias_read (argv[i], 1);
+	  size_t i;
+	  for (i = 0; i < ws.ws_wordc; i++) 
+	    mh_alias_read (ws.ws_wordv[i], 1);
+	  mu_wordsplit_free (&ws);
 	}
-      mu_argcv_free (argc, argv);
     }
   mh_alias_read (DEFAULT_ALIAS_FILE, 0);
   return 0;
diff --git a/mh/mh_argp.c b/mh/mh_argp.c
index 8978d4c..dff019b 100644
--- a/mh/mh_argp.c
+++ b/mh/mh_argp.c
@@ -219,24 +219,31 @@ mh_argp_parse (int *pargc, char **pargv[],
     {
       int argc;
       char **argv;
-      int xargc;
-      char **xargv;
       int i, j;
+      struct mu_wordsplit ws;
       
-      mu_argcv_get (val, "", NULL, &xargc, &xargv);
+      if (mu_wordsplit (val, &ws, MU_WRDSF_DEFFLAGS))
+	{
+	  mu_error (_("cannot split line `%s': %s"), val,
+		    mu_wordsplit_strerror (&ws));
+	  exit (1);
+	}
 
-      argc = *pargc + xargc;
-      argv = calloc (argc+1, sizeof *argv);
+      argc = *pargc + ws.ws_wordc;
+      argv = calloc (argc + 1, sizeof *argv);
       if (!argv)
         mh_err_memory (1);
 
       i = 0;
       argv[i++] = (*pargv)[0];
-      for (j = 0; j < xargc; i++, j++)
-	argv[i] = xargv[j];
+      for (j = 0; j < ws.ws_wordc; i++, j++)
+	argv[i] = ws.ws_wordv[j];
       for (j = 1; i < argc; i++, j++)
 	argv[i] = (*pargv)[j];
       argv[i] = NULL;
+
+      ws.ws_wordc = 0;
+      mu_wordsplit_free (&ws);
       
       mh_argv_preproc (argc, argv, &data);
 
@@ -246,7 +253,6 @@ mh_argp_parse (int *pargc, char **pargv[],
 
       *pargc = argc;
       *pargv = argv;
-      free (xargv);
     }
   else
     {
diff --git a/mh/mh_init.c b/mh/mh_init.c
index 1f64cc3..8fc0bda 100644
--- a/mh/mh_init.c
+++ b/mh/mh_init.c
@@ -539,33 +539,36 @@ mh_iterate (mu_mailbox_t mbox, mh_msgset_t *msgset,
 int
 mh_spawnp (const char *prog, const char *file)
 {
-  int argc, i, rc, status;
-  char **argv, **xargv;
+  struct mu_wordsplit ws;
+  size_t i;
+  int rc, status;
+  char **xargv;
 
-  if ((rc = mu_argcv_get (prog, "", "#", &argc, &argv)) != 0)
+  ws.ws_comment = "#";
+  if (mu_wordsplit (prog, &ws, MU_WRDSF_DEFFLAGS | MU_WRDSF_COMMENT))
     {
-      mu_diag_funcall (MU_DIAG_ERROR, "mu_argcv_get", prog, rc);
-      mu_argcv_free (argc, argv);
+      mu_error (_("cannot split line `%s': %s"), prog,
+		mu_wordsplit_strerror (&ws));
       return 1;
     }
 
-  xargv = calloc (argc + 2, sizeof (*xargv));
+  xargv = calloc (ws.ws_wordc + 2, sizeof (*xargv));
   if (!xargv)
     {
       mh_err_memory (0);
-      mu_argcv_free (argc, argv);
+      mu_wordsplit_free (&ws);
       return 1;
     }
 
-  for (i = 0; i < argc; i++)
-    xargv[i] = argv[i];
+  for (i = 0; i < ws.ws_wordc; i++)
+    xargv[i] = ws.ws_wordv[i];
   xargv[i++] = (char*) file;
   xargv[i++] = NULL;
-
+  
   rc = mu_spawnvp (xargv[0], xargv, &status);
 
   free (xargv);
-  mu_argcv_free (argc, argv);
+  mu_wordsplit_free (&ws);
 
   return rc;
 }
diff --git a/mh/mh_list.c b/mh/mh_list.c
index a414574..86b3c96 100644
--- a/mh/mh_list.c
+++ b/mh/mh_list.c
@@ -149,42 +149,45 @@ parse_component (locus_t *loc, mu_list_t formlist, char *compname, char *str)
 static void
 parse_variable (locus_t *loc, mu_list_t formlist, char *str)
 {
-  int i, rc;
-  int argc;
-  char **argv;
+  size_t i;
+  struct mu_wordsplit ws;
   mh_format_t fmt;
-  
-  if ((rc = mu_argcv_get (str, ",=", NULL, &argc, &argv)) != 0)
+
+  ws.ws_delim = ",=";
+  if (mu_wordsplit (str, &ws,
+		    MU_WRDSF_DEFFLAGS|MU_WRDSF_DELIM|
+		    MU_WRDSF_WS|MU_WRDSF_RETURN_DELIMS))
     {
-      mu_error ("%s:%d: mu_argcv_get(%s): %s",
+      mu_error ("%s:%d: mu_wordsplit(%s): %s",
 		loc->filename,
 		loc->line,
 		str,
-		mu_strerror (rc));
+		mu_wordsplit_strerror (&ws));
       exit (1);
     }
 
-  for (i = 0; i < argc; i++)
+  for (i = 0; i < ws.ws_wordc; i++)
     {
-      mhl_stmt_t *stmt = stmt_alloc (stmt_variable);
-      char *name = argv[i];
+      mhl_stmt_t *stmt;
+      char *name = ws.ws_wordv[i];
       char *value = NULL;
       mhl_variable_t *var;
-      
+
+      stmt = stmt_alloc (stmt_variable);
       var = variable_lookup (name);
       if (!var)
 	{
 	  mu_error (_("%s:%d: unknown variable: %s"),
 		    loc->filename,
 		    loc->line,
-		    argv[i]);
+		    name);
 	  exit (1);
 	}
 
-      if (i + 1 < argc && argv[i+1][0] == '=')
+      if (i + 1 < ws.ws_wordc && ws.ws_wordv[i+1][0] == '=')
 	{
 	  i++;
-	  value = argv[++i];
+	  value = ws.ws_wordv[++i];
 	}
 
       if ((var->type == dt_flag && value)
@@ -227,14 +230,13 @@ parse_variable (locus_t *loc, mu_list_t formlist, char *str)
       mu_list_append (formlist, stmt);
 
       i++;
-      if (i < argc && argv[i][0] != ',')
+      if (i < ws.ws_wordc && ws.ws_wordv[i][0] != ',')
 	{
 	  mu_error (_("%s:%d: syntax error"), loc->filename, loc->line);
 	  exit (1);
 	}
     }
-
-  mu_argcv_free (argc, argv);
+  mu_wordsplit_free (&ws);
 }
 
 static int
diff --git a/mh/mh_msgset.c b/mh/mh_msgset.c
index fdf5df2..41eb036 100644
--- a/mh/mh_msgset.c
+++ b/mh/mh_msgset.c
@@ -235,8 +235,7 @@ static int _mh_msgset_parse (mu_mailbox_t mbox, mh_msgset_t *msgset,
 int
 expand_user_seq (mu_mailbox_t mbox, mh_msgset_t *msgset, char *arg)
 {
-  int argc;
-  char **argv;
+  struct mu_wordsplit ws;
   char *p;
   const char *listp;
   int rc = 1;
@@ -260,10 +259,18 @@ expand_user_seq (mu_mailbox_t mbox, mh_msgset_t *msgset, char *arg)
       if (!listp)
 	return 1;
     }
+
+  if (mu_wordsplit (listp, &ws, MU_WRDSF_DEFFLAGS))
+    {
+      mu_error (_("cannot split line `%s': %s"), listp,
+		mu_wordsplit_strerror (&ws));
+    }
+  else
+    {
+      rc = _mh_msgset_parse (mbox, msgset, ws.ws_wordc, ws.ws_wordv);
+      mu_wordsplit_free (&ws);
+    }
   
-  if (mu_argcv_get (listp, "", NULL, &argc, &argv) == 0)
-    rc = _mh_msgset_parse (mbox, msgset, argc, argv);
-  mu_argcv_free (argc, argv);
   if (rc)
     return rc;
 
diff --git a/mh/mh_sequence.c b/mh/mh_sequence.c
index 737461a..f80b855 100644
--- a/mh/mh_sequence.c
+++ b/mh/mh_sequence.c
@@ -124,19 +124,23 @@ mh_seq_delete (const char *name, mh_msgset_t *mset, int flags)
   const char *value = mh_seq_read (name, flags);
   char *new_val;
   char *p;
-  int argc, i, count;
-  char **argv;
+  size_t i, count;
+  struct mu_wordsplit ws;
   
   if (!value)
     return 0;
 
-  if (mu_argcv_get (value, "", NULL, &argc, &argv))
-    return 0;
+  if (mu_wordsplit (value, &ws, MU_WRDSF_DEFFLAGS))
+    {
+      mu_error (_("cannot split line `%s': %s"), value,
+		mu_wordsplit_strerror (&ws));
+      return 0;
+    }
 
-  for (i = 0; i < argc; i++)
+  for (i = 0; i < ws.ws_wordc; i++)
     {
       char *p;
-      size_t num = strtoul (argv[i], &p, 10);
+      size_t num = strtoul (ws.ws_wordv[i], &p, 10);
 
       if (*p)
 	continue;
@@ -144,19 +148,19 @@ mh_seq_delete (const char *name, mh_msgset_t *mset, int flags)
       if (bsearch (&num, mset->list, mset->count, sizeof (mset->list[0]),
 		   cmp_msgnum))
 	{
-	  free (argv[i]);
-	  argv[i] = NULL;
+	  free (ws.ws_wordv[i]);
+	  ws.ws_wordv[i] = NULL;
 	}
     }
 
   new_val = xstrdup (value);
   p = new_val;
   count = 0;
-  for (i = 0; i < argc; i++)
+  for (i = 0; i < ws.ws_wordc; i++)
     {
-      if (argv[i])
+      if (ws.ws_wordv[i])
 	{
-	  strcpy (p, argv[i]);
+	  strcpy (p, ws.ws_wordv[i]);
 	  p += strlen (p);
 	  *p++ = ' ';
 	  count++;
@@ -164,7 +168,7 @@ mh_seq_delete (const char *name, mh_msgset_t *mset, int flags)
     }
   *p = 0;
   write_sequence (name, count > 0 ? new_val : NULL, flags & SEQ_PRIVATE);
-  mu_argcv_free (argc, argv);
+  mu_wordsplit_free (&ws);
   free (new_val);
   
   return 0;
diff --git a/mh/mh_whatnow.c b/mh/mh_whatnow.c
index 6724810..6582fd8 100644
--- a/mh/mh_whatnow.c
+++ b/mh/mh_whatnow.c
@@ -328,28 +328,30 @@ _whatnow (struct mh_whatnow_env *wh, struct action_tab *tab)
     {
       char *line = NULL;
       size_t size = 0;
-      int argc;
-      char **argv;
+      struct mu_wordsplit ws;
       handler_fp fun;
       
       printf ("%s ", wh->prompt);
       getline (&line, &size, stdin);
       if (!line)
 	continue;
-      rc = mu_argcv_get (line, "", "#", &argc, &argv);
+
+      ws.ws_comment = "#";
+      rc = mu_wordsplit (line, &ws, MU_WRDSF_DEFFLAGS|MU_WRDSF_COMMENT);
       free (line);
       if (rc)
 	{
-	  mu_argcv_free (argc, argv);
+	  mu_error (_("cannot split line `%s': %s"), line,
+		    mu_wordsplit_strerror (&ws));
 	  break;
 	}
 
-      fun = func (tab, argv[0]);
+      fun = func (tab, ws.ws_wordv[0]);
       if (fun)
-	rc = fun (wh, argc, argv, &status);
+	rc = fun (wh, ws.ws_wordc, ws.ws_wordv, &status);
       else
 	rc = 0;
-      mu_argcv_free (argc, argv);
+      mu_wordsplit_free (&ws);
     }
   while (rc == 0);
   return status;
diff --git a/mh/mhn.c b/mh/mhn.c
index 5aef07f..bc240cb 100644
--- a/mh/mhn.c
+++ b/mh/mhn.c
@@ -881,26 +881,24 @@ mhn_store_command (mu_message_t msg, msg_part_t part, char *name)
 static void
 split_args (char *argstr, int *pargc, char ***pargv)
 {
-  int argc;
-  char **argv;
+  struct mu_wordsplit ws;
 
-  if (mu_argcv_get (argstr, ";", NULL, &argc, &argv) == 0)
+  ws.ws_delim = ";";
+  if (mu_wordsplit (argstr, &ws,
+		    MU_WRDSF_DEFFLAGS | MU_WRDSF_DELIM | MU_WRDSF_WS))
     {
-      int i, j;
-      
-      for (i = j = 0; i < argc; i++)
-	{
-	  if (argv[i][0] != ';')
-	    argv[j++] = argv[i];
-	}
-      argv[j] = NULL;
-      *pargc = j;
-      *pargv = argv;
+      mu_error (_("cannot split line `%s': %s"), argstr,
+		mu_wordsplit_strerror (&ws));
+      *pargc = 0;
+      *pargv = NULL;
     }
   else
     {
-      *pargc = 0;
-      *pargv = NULL;
+      *pargc = ws.ws_wordc;
+      *pargv = ws.ws_wordv;
+      ws.ws_wordc = 0;
+      ws.ws_wordv = NULL;
+      mu_wordsplit_free (&ws);
     }
 }
 
@@ -1303,13 +1301,14 @@ mhn_run_command (mu_message_t msg, msg_part_t part,
     {
       /* pass content via a tempfile */
       int status;
-      int argc;
-      char **argv;
       mu_stream_t tmp;
-      
-      if (mu_argcv_get (cmd, "", "#", &argc, &argv))
+      struct mu_wordsplit ws;
+
+      ws.ws_comment = "#";
+      if (mu_wordsplit (cmd, &ws, MU_WRDSF_DEFFLAGS | MU_WRDSF_COMMENT))
 	{
-	  mu_error (_("cannot parse command line `%s'"), cmd);
+	  mu_error (_("cannot parse command line `%s': %s"), cmd,
+		    mu_wordsplit_strerror (&ws));
 	  return ENOSYS;
 	}
 
@@ -1318,15 +1317,15 @@ mhn_run_command (mu_message_t msg, msg_part_t part,
 	{
 	  mu_error (_("cannot create temporary stream (file %s): %s"),
 		    tempfile, mu_strerror (rc));
-	  mu_argcv_free (argc, argv);
+	  mu_wordsplit_free (&ws);
 	  return rc;
 	}
       show_internal (msg, part, encoding, tmp);      
       mu_stream_destroy (&tmp);
-      rc = mu_spawnvp (argv[0], argv, &status);
+      rc = mu_spawnvp (ws.ws_wordv[0], ws.ws_wordv, &status);
       if (status)
 	rc = status;
-      mu_argcv_free (argc, argv);
+      mu_wordsplit_free (&ws);
     }
   else
     rc = exec_internal (msg, part, encoding, cmd, flags);
@@ -2044,8 +2043,8 @@ edit_forw (char *cmd, struct compose_env *env, mu_message_t *pmsg, int level)
 {
   char *sp, *id = NULL, *descr = NULL;
   int stop = 0, status = 0;
-  int i, argc;
-  char **argv;
+  size_t i;
+  struct mu_wordsplit ws;
   mu_header_t hdr;
   mu_mime_t mime;
   mu_message_t msg;
@@ -2090,18 +2089,21 @@ edit_forw (char *cmd, struct compose_env *env, mu_message_t *pmsg, int level)
   if (status)
     return status;
 
-  if (mu_argcv_get (cmd, "\n", NULL, &argc, &argv))
+  ws.ws_delim = "\n";
+  if (mu_wordsplit (cmd, &ws,
+		    MU_WRDSF_DEFFLAGS | MU_WRDSF_DELIM | MU_WRDSF_WS))
     {
-      mu_error (_("%s:%lu: syntax error"),
+      mu_error (_("%s:%lu: cannot split line: %s"),
 		input_file,
-		(unsigned long) mhn_error_loc (env));
+		(unsigned long) mhn_error_loc (env),
+		mu_wordsplit_strerror (&ws));
       return 1;
     }
 
   mu_mime_create (&mime, NULL, 0);
   
-  mbox = mh_open_folder (argv[0], 0);
-  for (i = 1; i < argc; i++)
+  mbox = mh_open_folder (ws.ws_wordv[0], 0);
+  for (i = 1; i < ws.ws_wordc; i++)
     {
       mu_message_t input_msg;
       if (mh_get_message (mbox, i, &input_msg) == 0)
@@ -2117,7 +2119,7 @@ edit_forw (char *cmd, struct compose_env *env, mu_message_t *pmsg, int level)
 	break;
       mu_mime_add_part (mime, msg);
     }
-  mu_argcv_free (argc, argv);
+  mu_wordsplit_free (&ws);
 
   if (*pmsg)
     {
diff --git a/mh/send.c b/mh/send.c
index ef39d84..faece4e 100644
--- a/mh/send.c
+++ b/mh/send.c
@@ -497,49 +497,53 @@ fix_fcc (mu_message_t msg)
   mu_message_get_header (msg, &hdr);
   if (mu_header_aget_value (hdr, MU_HEADER_FCC, &fcc) == 0)
     {
-      int i, argc;
-      char **argv;
+      struct mu_wordsplit ws;
       int need_fixup = 0;
       size_t fixup_len = 0;
-      
-      mu_argcv_get (fcc, ",", NULL, &argc, &argv);
-      for (i = 0; i < argc; i += 2)
+
+      ws.ws_delim = ",";
+      if (mu_wordsplit (fcc, &ws,
+			MU_WRDSF_DEFFLAGS | MU_WRDSF_DELIM | MU_WRDSF_WS))
 	{
-	  if (strchr ("+%~/=", argv[i][0]) == NULL)
-	    {
-	      need_fixup++;
-	      fixup_len ++;
-	    }
-	  fixup_len += strlen (argv[i]);
+	  mu_error (_("cannot split line `%s': %s"), fcc,
+		    mu_wordsplit_strerror (&ws));
 	}
-
-      if (need_fixup)
+      else
 	{
-	  char *p;
-
-	  /* the new fcc string contains: folder names - fixup_len characters
-	     long, (argc - 2)/2 comma-space pairs and a terminating
-	     nul */
-	  fcc = realloc (fcc, fixup_len + argc - 2 + 1);
-	  for (i = 0, p = fcc; i < argc; i++)
+	  size_t i;
+	  
+	  for (i = 0; i < ws.ws_wordc; i += 2)
 	    {
-	      if (i % 2 == 0)
+	      if (strchr ("+%~/=", ws.ws_wordv[i][0]) == NULL)
 		{
-		  if (strchr ("+%~/=", argv[i][0]) == NULL)
-		    *p++ = '+';
-		  strcpy (p, argv[i]);
-		  p += strlen (argv[i]);
+		  need_fixup++;
+		  fixup_len ++;
 		}
-	      else
+	      fixup_len += strlen (ws.ws_wordv[i]);
+	    }
+
+	  if (need_fixup)
+	    {
+	      char *p;
+	      
+	      /* the new fcc string contains: folder names - fixup_len
+		 characters, ws.ws_wordc - 1 comma-space pairs and a
+		 terminating nul */
+	      fcc = realloc (fcc, fixup_len + ws.ws_wordc - 1 + 1);
+	      for (i = 0, p = fcc; i < ws.ws_wordc; i++)
 		{
+		  if (strchr ("+%~/=", ws.ws_wordv[i][0]) == NULL)
+		    *p++ = '+';
+		  strcpy (p, ws.ws_wordv[i]);
+		  p += strlen (p);
 		  *p++ = ',';
 		  *p++ = ' ';
 		}
+	      *p = 0;
 	    }
-	  *p = 0;
 	}
 
-      mu_argcv_free (argc, argv);
+      mu_wordsplit_free (&ws);
 
       if (need_fixup)
 	{
diff --git a/movemail/movemail.c b/movemail/movemail.c
index d030828..61daad6 100644
--- a/movemail/movemail.c
+++ b/movemail/movemail.c
@@ -267,23 +267,21 @@ cb_mailbox_ownership (mu_debug_t debug, void *data, mu_config_value_t *val)
 	return _cb_mailbox_ownership (debug, str);
       else
 	{
-	  int argc;
-	  char **argv;
+	  struct mu_wordsplit ws;
 
-	  if (mu_argcv_get_np (str, strlen (str), ",", NULL, 0,
-			       &argc, &argv, NULL))
+	  ws.ws_delim = ",";
+	  if (mu_wordsplit (str, &ws, MU_WRDSF_DEFFLAGS|MU_WRDSF_DELIM))
 	    {
 	      mu_cfg_format_error (debug, MU_DEBUG_ERROR, 
-				   _("cannot parse %s"),
-				   str);
+				   _("cannot parse %s: %s"),
+				   str, mu_wordsplit_strerror (&ws));
 	      return 1;
 	    }
 
-	  for (i = 0; i < argc; i++)
-	    if (_cb_mailbox_ownership (debug, argv[i]))
+	  for (i = 0; i < ws.ws_wordc; i++)
+	    if (_cb_mailbox_ownership (debug, ws.ws_wordv[i]))
 	      return 1;
-
-	  mu_argcv_free (argc, argv);
+	  mu_wordsplit_free (&ws);
 	  return 0;
 	}
     }
diff --git a/mu/shell.c b/mu/shell.c
index 9497b3d..910a13a 100644
--- a/mu/shell.c
+++ b/mu/shell.c
@@ -238,13 +238,12 @@ shell_help (int argc, char **argv)
 static int
 shell_prompt (int argc, char **argv)
 {
-  int quote;
   size_t size;
   
   free (mutool_shell_prompt);
-  size = mu_argcv_quoted_length (argv[1], &quote);
+  size = strlen (argv[1]);
   mutool_shell_prompt = xmalloc (size + 1);
-  mu_argcv_unquote_copy (mutool_shell_prompt, argv[1], size);
+  mu_wordsplit_c_unquote_copy (mutool_shell_prompt, argv[1], size);
   return 0;
 }
 
@@ -460,16 +459,22 @@ add_history (const char *s MU_ARG_UNUSED)
 int
 execute_line (char *line)
 {
+  struct mu_wordsplit ws;
   int argc;
   char **argv;
   int status = 0;
 
-  if (mu_argcv_get (line, NULL, "#", &argc, &argv))
+  ws.ws_comment = "#";
+  ws.ws_offs = 1; /* Keep extra slot for expansion in case when argmin == -1 */
+  if (mu_wordsplit (line, &ws,
+		    MU_WRDSF_DEFFLAGS|MU_WRDSF_COMMENT|MU_WRDSF_DOOFFS))
     {
-      mu_error("cannot parse input line");
+      mu_error("cannot parse input line: %s", mu_wordsplit_strerror (&ws));
       return 0;
     }
-
+  argc = ws.ws_wordc;
+  argv = ws.ws_wordv + 1;
+  
   if (argc >= 0)
     {
       struct mutool_command *cmd = find_command (argv[0]);
@@ -484,6 +489,7 @@ execute_line (char *line)
 	{
 	  if (cmd->argmin <= 0 && argc != 2)
 	    {
+	      size_t i;
 	      char *word = mu_str_skip_class (line, MU_CTYPE_SPACE);
 	      char *arg = mu_str_skip_class_comp (word, MU_CTYPE_SPACE);
 	      if (*arg)
@@ -491,18 +497,19 @@ execute_line (char *line)
 		  *arg++ = 0;
 		  arg = mu_str_skip_class (arg, MU_CTYPE_SPACE);
 		}
-	      
-	      mu_argcv_free (argc, argv);
-	      argc = 2;
-	      argv = xcalloc (argc + 1, sizeof (argv[0]));
-	      argv[0] = xstrdup (word);
-	      argv[1] = xstrdup (arg);
-	      argv[2] = NULL;
+	      for (i = 0; i < ws.ws_wordc; i++)
+		free (ws.ws_wordv[i + 1]);
+	      ws.ws_wordv[0] = xstrdup (word);
+	      ws.ws_wordv[1] = xstrdup (arg);
+	      ws.ws_wordv[2] = NULL;
+	      ws.ws_wordc = 2;
+	      argc = ws.ws_wordc;
+	      argv = ws.ws_wordv;
 	    }
 	  status = cmd->func (argc, argv);
 	}
     }
-  mu_argcv_free (argc, argv);
+  mu_wordsplit_free (&ws);
   return status;
 }
 
diff --git a/po/POTFILES.in b/po/POTFILES.in
index fc1e8b1..2201ad1 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -129,7 +129,10 @@ libmailutils/diag/diag.c
 libmailutils/diag/errors
 libmailutils/diag/gdebug.c
 
+libmailutils/server/acl.c
+
 libmailutils/stream/file_stream.c
+libmailutils/stream/prog_stream.c
 
 libmailutils/filter/filter_iconv.c
 
@@ -137,6 +140,9 @@ libmailutils/server/ipsrv.c
 libmailutils/server/msrv.c
 
 libmailutils/mailbox/message.c
+libmailutils/mailer/mailer.c
+libmailutils/mailer/smtp.c
+libmailutils/mailer/smtp_gsasl.c
 
 libmailutils/auth/mu_auth.c
 
diff --git a/pop3d/popauth.c b/pop3d/popauth.c
index b22cb66..2c9d75e 100644
--- a/pop3d/popauth.c
+++ b/pop3d/popauth.c
@@ -16,7 +16,6 @@
    along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "pop3d.h"
-#include <mailutils/argcv.h>
 #include <xalloc.h>
 #include "mailutils/libargp.h"
 
@@ -30,6 +29,7 @@ int db_make (char *input_name, char *output_name);
 #define ACT_CHPASS  4
 
 static int permissions = 0600;
+static int compatibility_option = 0;
 
 struct action_data {
   int action;
@@ -60,6 +60,8 @@ static error_t popauth_parse_opt  (int key, char *arg,
 
 void popauth_version (FILE *stream, struct argp_state *state);
 
+#define COMPATIBILITY_OPTION 256
+
 static struct argp_option options[] = 
 {
   { NULL, 0, NULL, 0, N_("Actions are:"), 1 },
@@ -80,6 +82,8 @@ static struct argp_option options[] =
   { "password", 'p', N_("STRING"), 0, N_("specify user's password"), 3 },
   { "user", 'u', N_("USERNAME"), 0, N_("specify user name"), 3 },
   { "permissions", 'P', N_("PERM"), 0, N_("force given permissions on the database"), 3 },
+  { "compatibility", COMPATIBILITY_OPTION, NULL, 0,
+    N_("backward compatibility mode") },
   { NULL, }
 };
 
@@ -171,7 +175,11 @@ popauth_parse_opt (int key, char *arg, struct argp_state *astate)
     case 'P':
       set_db_perms (astate, arg, &permissions);
       break;
-      
+
+    case COMPATIBILITY_OPTION:
+      compatibility_option = 1;
+      break;
+	
     case ARGP_KEY_FINI:
       if (ap->action == -1)
 	{
@@ -269,6 +277,23 @@ check_user_perm (int action, struct action_data *ap)
   return 1;
 }
 
+static void
+print_entry (FILE *fp, DBM_DATUM key, DBM_DATUM contents)
+{
+  if (compatibility_option)
+    fprintf (fp, "%.*s: %.*s\n",
+	     (int) MU_DATUM_SIZE (key),
+	     (char*) MU_DATUM_PTR (key),
+	     (int) MU_DATUM_SIZE (contents),
+	     (char*) MU_DATUM_PTR (contents));
+  else
+    fprintf (fp, "%.*s %.*s\n",
+	     (int) MU_DATUM_SIZE (key),
+	     (char*) MU_DATUM_PTR (key),
+	     (int) MU_DATUM_SIZE (contents),
+	     (char*) MU_DATUM_PTR (contents));
+}
+
 int
 action_list (struct action_data *ap)
 {
@@ -309,11 +334,7 @@ action_list (struct action_data *ap)
 	}
       else
 	{
-	  fprintf (fp, "%.*s: %.*s\n",
-		   (int) MU_DATUM_SIZE (key),
-		   (char*) MU_DATUM_PTR (key),
-		   (int) MU_DATUM_SIZE (contents),
-		   (char*) MU_DATUM_PTR (contents));
+	  print_entry (fp, key, contents);
 	  mu_dbm_datum_free (&contents);
 	}
     }
@@ -324,11 +345,7 @@ action_list (struct action_data *ap)
 	{
 	  memset (&contents, 0, sizeof contents);
 	  mu_dbm_fetch (db, key, &contents);
-	  fprintf (fp, "%.*s: %.*s\n",
-		   (int) MU_DATUM_SIZE (key),
-		   (char*) MU_DATUM_PTR (key),
-		   (int) MU_DATUM_SIZE (contents),
-		   (char*) MU_DATUM_PTR (contents));
+	  print_entry (fp, key, contents);
 	  mu_dbm_datum_free (&contents);
 	}
     }
@@ -341,85 +358,89 @@ action_list (struct action_data *ap)
 int
 action_create (struct action_data *ap)
 {
-  FILE *fp;
+  int rc;
+  mu_stream_t in;
   DBM_FILE db;
   DBM_DATUM key;
   DBM_DATUM contents;
-  char buf[256];
+  char *buf = NULL;
+  size_t size = 0, len;
   int line = 0;
-
+  
   /* Make sure we have proper privileges if popauth is setuid */
   setuid (getuid ());
   
   if (ap->input_name)
     {
-      fp = fopen (ap->input_name, "r");
-      if (!fp)
+      rc = mu_file_stream_create (&in, ap->input_name, MU_STREAM_READ);
+      if (rc)
 	{
 	  mu_error (_("cannot open file %s: %s"),
-		    ap->input_name, mu_strerror (errno));
+		    ap->input_name, mu_strerror (rc));
 	  return 1;
 	}
     }
   else
     {
       ap->input_name = "";
-      fp = stdin;
+      rc = mu_stdio_stream_create (&in, MU_STDIN_FD, MU_STREAM_READ);
+      if (rc)
+	{
+	  mu_error (_("cannot open standard input: %s"),
+		    mu_strerror (rc));
+	  return 1;
+	}
     }
   
   if (!ap->output_name)
     ap->output_name = APOP_PASSFILE;
   if (mu_dbm_open (ap->output_name, &db, MU_STREAM_CREAT, permissions))
     {
-      mu_error (_("cannot create database %s: %s"), ap->output_name, mu_strerror (errno));
+      mu_error (_("cannot create database %s: %s"),
+		ap->output_name, mu_strerror (errno));
       return 1;
     }
 
   line = 0;
-  while (fgets (buf, sizeof buf - 1, fp))
+  while ((rc = mu_stream_getline (in, &buf, &size, &len)) == 0
+	 && len > 0)
     {
-      int len;
-      int argc;
-      char **argv;
+      char *str, *pass;
 
-      len = strlen (buf);
-      if (buf[len-1] == '\n')
-	buf[--len] = 0;
-      
       line++;
-      if (mu_argcv_get (buf, ":", NULL, &argc, &argv))
-	{
-	  mu_argcv_free (argc, argv);
-	  continue;
-	}
-
-      if (argc == 0 || argv[0][0] == '#')
+      str = mu_str_stripws (buf);
+      if (*str == 0 || *str == '#')
+	continue;
+      pass = mu_str_skip_class_comp (str, MU_CTYPE_SPACE);
+      if (*pass == 0)
 	{
-	  mu_argcv_free (argc, argv);
+	  mu_error (_("%s:%d: malformed line"), ap->input_name, line);
 	  continue;
 	}
-      
-      if (argc != 3 || argv[1][0] != ':' || argv[1][1] != 0)
+      /* Strip trailing semicolon, when in compatibility mode. */
+      if (compatibility_option && pass > str && pass[-1] == ':')
+	pass[-1] = 0;
+      *pass++ = 0;
+      pass = mu_str_skip_class (pass, MU_CTYPE_SPACE);
+      if (*pass == 0)
 	{
 	  mu_error (_("%s:%d: malformed line"), ap->input_name, line);
-	  mu_argcv_free (argc, argv);
 	  continue;
 	}
-
+      
       memset (&key, 0, sizeof key);
       memset (&contents, 0, sizeof contents);
-      MU_DATUM_PTR (key) = argv[0];
-      MU_DATUM_SIZE (key) = strlen (argv[0]);
-      MU_DATUM_PTR (contents) = argv[2];
-      MU_DATUM_SIZE (contents) = strlen (argv[2]);
+      MU_DATUM_PTR (key) = str;
+      MU_DATUM_SIZE (key) = strlen (str);
+      MU_DATUM_PTR (contents) = pass;
+      MU_DATUM_SIZE (contents) = strlen (pass);
 
       if (mu_dbm_insert (db, key, contents, 1))
 	mu_error (_("%s:%d: cannot store datum"), ap->input_name, line);
-
-      mu_argcv_free (argc, argv);
     }
+  free (buf);
   mu_dbm_close (db);
-  fclose (fp);
+  mu_stream_destroy (&in);
   return 0;
 }
 
diff --git a/readmsg/readmsg.c b/readmsg/readmsg.c
index db60329..a354070 100644
--- a/readmsg/readmsg.c
+++ b/readmsg/readmsg.c
@@ -297,6 +297,7 @@ main (int argc, char **argv)
   int i;
   int index;
   mu_mailbox_t mbox = NULL;
+  struct mu_wordsplit ws;
   char **weedv;
   int weedc;
   int unix_header = 0;
@@ -351,27 +352,32 @@ main (int argc, char **argv)
   if (weedlist == NULL)
     weedlist = "Date To Cc Subject From Apparently-";
 
-  status = mu_argcv_get (weedlist, WEEDLIST_SEPARATOR, NULL,
-			 &weedc, &weedv);
+  ws.ws_delim = WEEDLIST_SEPARATOR;
+  status = mu_wordsplit (weedlist, &ws, MU_WRDSF_DEFFLAGS | MU_WRDSF_DELIM);
   if (status)
     {
-      mu_error (_("cannot parse weedlist: %s"), mu_strerror (status));
+      mu_error (_("cannot parse weedlist: %s"), mu_wordsplit_strerror (&ws));
       exit (2);
     }
 
-  for (i = 0; i < weedc; i++)
+  if (ws.ws_wordc)
     {
-      if (mu_c_strcasecmp (weedv[i], "From_") == 0)
+      for (i = 0; i < ws.ws_wordc; i++)
 	{
-	  int j;
-	  unix_header = 1;
-	  free (weedv[i]);
-	  for (j = i; j < weedc; j++)
-	    weedv[j] = weedv[j+1];
-	  weedc--;
-	  if (weedc == 0 && !all_header)
-	    no_header = 1;
+	  if (mu_c_strcasecmp (ws.ws_wordv[i], "From_") == 0)
+	    {
+	      int j;
+	      unix_header = 1;
+	      free (ws.ws_wordv[i]);
+	      for (j = i; j < ws.ws_wordc; j++)
+		ws.ws_wordv[j] = ws.ws_wordv[j+1];
+	      ws.ws_wordc--;
+	      if (ws.ws_wordc == 0 && !all_header)
+		no_header = 1;
+	    }
 	}
+      weedc = ws.ws_wordc;
+      weedv = ws.ws_wordv;
     }
   
   if (all_header)
diff --git a/readmsg/readmsg.h b/readmsg/readmsg.h
index e0fde21..1fc97da 100644
--- a/readmsg/readmsg.h
+++ b/readmsg/readmsg.h
@@ -48,7 +48,7 @@
 #include <mailutils/tls.h>
 #include <mailutils/error.h>
 #include <mailutils/envelope.h>
-#include <mailutils/argcv.h>
+#include <mailutils/wordsplit.h>
 #include <mailutils/util.h>
 
 int msglist (mu_mailbox_t mbox, int show_all, int argc, char **argv, int **set, int *n);
diff --git a/testsuite/smtpsend.c b/testsuite/smtpsend.c
index a716c3d..c77e703 100644
--- a/testsuite/smtpsend.c
+++ b/testsuite/smtpsend.c
@@ -51,8 +51,8 @@ send_rcpt_command (void *item, void *data)
 static void
 update_list (mu_list_t *plist, const char *arg)
 {
-  int mc, j;
-  char **mv;
+  size_t j;
+  struct mu_wordsplit ws;
   mu_list_t list = *plist;
   
   if (!list)
@@ -61,13 +61,16 @@ update_list (mu_list_t *plist, const char *arg)
       *plist = list;
     }
 
-  MU_ASSERT (mu_argcv_get_np (arg, strlen (arg),
-			      ",", NULL,
-			      0,
-			      &mc, &mv, NULL));
-  for (j = 0; j < mc; j++)
-    MU_ASSERT (mu_list_append (list, mv[j]));
-  free (mv);
+  ws.ws_delim = ",";
+  if (mu_wordsplit (arg, &ws, MU_WRDSF_DEFFLAGS | MU_WRDSF_DELIM))
+    {
+      mu_error ("mu_wordsplit: %s", mu_wordsplit_strerror (&ws));
+      exit (1);
+    }
+  for (j = 0; j < ws.ws_wordc; j++)
+    MU_ASSERT (mu_list_append (list, ws.ws_wordv[j]));
+  ws.ws_wordc = 0;
+  mu_wordsplit_free (&ws);
 }
 
 static int


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Fri Oct 29 05:06:01 2010
Received: from mailman by lists.gnu.org with archive (Exim 4.43)
	id 1PBkuO-00086C-To
	for mharc-commit-mailutils@gnu.org; Fri, 29 Oct 2010 05:06:00 -0400
Received: from [140.186.70.92] (port=48512 helo=eggs.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.43) id 1PBkuH-0007ux-DQ
	for commit-mailutils@gnu.org; Fri, 29 Oct 2010 05:05:57 -0400
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1PBkqu-0005o1-Tf
	for commit-mailutils@gnu.org; Fri, 29 Oct 2010 05:02:26 -0400
Received: from colonialone.fsf.org ([140.186.70.51]:39029
	helo=internal.in.savannah.gnu.org)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1PBkqu-0005nu-QR
	for commit-mailutils@gnu.org; Fri, 29 Oct 2010 05:02:24 -0400
Received: from [10.1.0.108] (helo=vcs-noshell.in.savannah.gnu.org)
	by internal.in.savannah.gnu.org with esmtp (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1PBkqu-0005E1-DF
	for commit-mailutils@gnu.org; Fri, 29 Oct 2010 09:02:24 +0000
Received: from gray by vcs-noshell.in.savannah.gnu.org with local (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1PBkqu-000261-2n
	for commit-mailutils@gnu.org; Fri, 29 Oct 2010 09:02:24 +0000
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: 4a49f5341ae9005c9ffbd7620efaecffcdfda79f
X-Git-Newrev: 63fec21357c175772a8ca153266919c12b6f8bd5
Message-Id: <E1PBkqu-000261-2n@vcs-noshell.in.savannah.gnu.org>
Date: Fri, 29 Oct 2010 09:02:24 +0000
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
Subject: [SCM] GNU Mailutils branch, master,
	updated. release-2.2-169-g63fec21
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.5
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=unsubscribe>
List-Archive: <http://lists.gnu.org/archive/html/commit-mailutils>
List-Post: <mailto:commit-mailutils@gnu.org>
List-Help: <mailto:commit-mailutils-request@gnu.org?subject=help>
List-Subscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Fri, 29 Oct 2010 09:05:57 -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=63fec21357c175772a8ca153266919c12b6f8bd5

The branch, master has been updated
       via  63fec21357c175772a8ca153266919c12b6f8bd5 (commit)
       via  90ba2e7e0c53957254719e46a7bd22e14b92a96c (commit)
       via  f49d4057c49022a646c8dbe2d203e8c374bb7427 (commit)
       via  5dd9e9c13bafea91ccdc97a3afcbc13a8762aee1 (commit)
       via  41a0da7a22995b23a3fec61d5b87ed880c87fe97 (commit)
      from  4a49f5341ae9005c9ffbd7620efaecffcdfda79f (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 63fec21357c175772a8ca153266919c12b6f8bd5
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Fri Oct 29 11:54:29 2010 +0300

    Use wordsplit to expand command prompt in mu.
    
    * mu/mu.h (mutool_prompt_vartab): Remove.
    (mutool_prompt_env): New extern.
    * mu/pop.c (pop_prompt_vartab): Rewrite as pop_prompt_env.
    All callers changed.
    * mu/shell.c (mutool_prompt_vartab): Remove variable.
    (mutool_prompt_env): New variable.
    (expand_prompt): Remove function.
    (input_line_interactive): Use mu_wordsplit to expand command prompt.

commit 90ba2e7e0c53957254719e46a7bd22e14b92a96c
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Fri Oct 29 11:32:46 2010 +0300

    Implement a Key/Value environment.
    
    K/V is a new type of environment where variable names (keys) and
    values are stored separately in adjacent environment elements.
    This should simplify wordsplit initialization in many cases.
    
    * include/mailutils/wordsplit.h (MU_WRDSF_ENV_KV): New flag.
    * libmailutils/string/wordsplit.c (mu_wordsplit_find_env): Implement
    search in a K/V environment.
    * libmailutils/tests/wordsplit.at: Test K/V environments.
    * libmailutils/tests/wsp.c (bool_keytab): Implement env_kv.
    (make_env_kv): New function.
    (main): Convert environment if env_kv is given.

commit f49d4057c49022a646c8dbe2d203e8c374bb7427
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Fri Oct 29 10:50:11 2010 +0300

    Fixes in wordsplit.  Use wordsplit instead of vartab in movemail.
    
    Eventually this will lead to vartab fading out until it is removed
    in some point in the future.  Wordsplit provides a much better
    variable substitution mechanism.
    
    * include/mailutils/wordsplit.h (mu_wordsplit) <ws_getvar>: Change
    signature.
    <ws_closure>: New member.
    (MU_WRDSF_CLOSURE): New flag.
    * libmailutils/string/wordsplit.c (mu_wordsplit_init): Initialize
    ws_closure to NULL, unless MU_WRDSF_CLOSURE is set.
    Do not modify ws_wordn if MU_WRDSF_DOOFFS is set.
    (wsnode_remove): Clear _WSNF_JOIN on the previous node if the node
    being deleted is the last one in chain.
    (expvar): Pass ws_closure as 3rd argument to ws_getvar.
    Insert a NULL node if the variable is not defined.
    
    * movemail/movemail.c: Use wordsplit instead of vartab. Note a side
    effect: the "source:*" and "dest:*" variables are now written as
    "source_*" and "dest_*", correspondingly.
    (movemail_getvar_closure): New structure.
    (DCL_VTX): Remove macro and its uses.
    (get_url_part, movemail_getvar): New functions.
    (set_program_id): Rewrite using wordsplit.

commit 5dd9e9c13bafea91ccdc97a3afcbc13a8762aee1
Merge: 41a0da7 4a49f53
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Thu Oct 28 23:24:27 2010 +0300

    Merge branch 'master' of ssh://git.savannah.gnu.org/srv/git/mailutils

commit 41a0da7a22995b23a3fec61d5b87ed880c87fe97
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Wed Oct 27 19:55:24 2010 +0300

    Fix allowed URL parts for local mailboxes.
    
    Parameters are allowed: they are used to expand hashed or indexed
    mailbox names.
    
    * libproto/maildir/folder.c (_maildir_record): Add MU_URL_PARAM.
    * libproto/mbox/folder.c (_mbox_record): Likewise.
    * libproto/mh/folder.c (_mh_record): Likewise.

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

Summary of changes:
 include/mailutils/wordsplit.h   |   11 +++-
 libmailutils/string/wordsplit.c |   60 +++++++++++++++-----
 libmailutils/tests/wordsplit.at |   10 +++
 libmailutils/tests/wsp.c        |   51 ++++++++++++++++-
 libproto/maildir/folder.c       |    2 +-
 libproto/mbox/folder.c          |    2 +-
 libproto/mh/folder.c            |    2 +-
 movemail/movemail.c             |  116 ++++++++++++++++++++++++---------------
 mu/mu.h                         |    2 +-
 mu/pop.c                        |   57 +++++++++++--------
 mu/shell.c                      |   32 ++++++-----
 11 files changed, 240 insertions(+), 105 deletions(-)

diff --git a/include/mailutils/wordsplit.h b/include/mailutils/wordsplit.h
index 13edb25..40e4289 100644
--- a/include/mailutils/wordsplit.h
+++ b/include/mailutils/wordsplit.h
@@ -35,8 +35,9 @@ struct mu_wordsplit
 		    __attribute__ ((__format__ (__printf__, 1, 2)));
 	
   const char **ws_env;
-  char *(*ws_getvar) (const char *, size_t); 
-	
+  const char *(*ws_getvar) (const char *, size_t, void *); 
+  void *ws_closure;
+  
   const char *ws_input;
   size_t ws_len;
   size_t ws_endp;
@@ -103,6 +104,12 @@ struct mu_wordsplit
 /* Handle C escapes */
 #define MU_WRDSF_CESCAPES          0x1000000
 
+/* ws_closure is set */
+#define MU_WRDSF_CLOSURE           0x2000000
+/* ws_env is a Key/Value environment, i.e. the value of a variable is
+   stored in the element that follows its name. */
+#define MU_WRDSF_ENV_KV            0x4000000
+
 #define MU_WRDSF_DEFFLAGS	       \
   (MU_WRDSF_NOVAR | MU_WRDSF_NOCMD | \
    MU_WRDSF_QUOTE | MU_WRDSF_SQUEEZE_DELIMS | MU_WRDSF_CESCAPES)
diff --git a/libmailutils/string/wordsplit.c b/libmailutils/string/wordsplit.c
index 301ab00..563752c 100644
--- a/libmailutils/string/wordsplit.c
+++ b/libmailutils/string/wordsplit.c
@@ -151,9 +151,10 @@ mu_wordsplit_init (struct mu_wordsplit *wsp, const char *input, size_t len,
       wsp->ws_wordc = 0;
       wsp->ws_wordn = 0;
     }
-  if (wsp->ws_flags & MU_WRDSF_DOOFFS)
-    wsp->ws_wordn += wsp->ws_offs;
 
+  if (!(wsp->ws_flags & MU_WRDSF_CLOSURE))
+    wsp->ws_closure = NULL;
+  
   wsp->ws_endp = 0;
   wsp->ws_errno = 0;
   wsp->ws_head = wsp->ws_tail = NULL;
@@ -311,7 +312,11 @@ wsnode_remove (struct mu_wordsplit *wsp, struct mu_wordsplit_node *node)
 
   p = node->prev;
   if (p)
-    p->next = node->next;
+    {
+      p->next = node->next;
+      if (!node->next)
+	p->flags &= ~_WSNF_JOIN;
+    }
   else
     wsp->ws_head = node->next;
 
@@ -646,16 +651,35 @@ mu_wordsplit_find_env (struct mu_wordsplit *wsp, const char *name, size_t len)
 
   if (!(wsp->ws_flags & MU_WRDSF_ENV))
     return NULL;
-  for (i = 0; wsp->ws_env[i]; i++)
-    {
-      size_t j;
-      const char *var = wsp->ws_env[i];
 
-      for (j = 0; j < len; j++)
-	if (name[j] != var[j])
-	  break;
-      if (j == len && var[j] == '=')
-	return var + j + 1;
+  if (wsp->ws_flags & MU_WRDSF_ENV_KV)
+    {
+      /* A key-value pair environment */
+      for (i = 0; wsp->ws_env[i]; i++)
+	{
+	  size_t elen = strlen (wsp->ws_env[i]);
+	  if (elen == len && memcmp (wsp->ws_env[i], name, elen) == 0)
+	    return wsp->ws_env[i + 1];
+	  /* Skip the value.  Break the loop if it is NULL. */
+	  i++;
+	  if (wsp->ws_env[i] == NULL)
+	    break;
+	}
+    }
+  else
+    {
+      /* Usual (A=B) environment. */
+      for (i = 0; wsp->ws_env[i]; i++)
+	{
+	  size_t j;
+	  const char *var = wsp->ws_env[i];
+	  
+	  for (j = 0; j < len; j++)
+	    if (name[j] != var[j])
+	      break;
+	  if (j == len && var[j] == '=')
+	    return var + j + 1;
+	}
     }
   return NULL;
 }
@@ -666,7 +690,7 @@ expvar (struct mu_wordsplit *wsp, const char *str, size_t len,
 {
   size_t i = 0;
   const char *defstr = NULL;
-  char *value;
+  const char *value;
   const char *vptr;
   struct mu_wordsplit_node *newnode;
   const char *start = str - 1;
@@ -738,7 +762,7 @@ expvar (struct mu_wordsplit *wsp, const char *str, size_t len,
 	return _wsplt_nomem (wsp);
     }
   else if (wsp->ws_flags & MU_WRDSF_GETVAR)
-    value = wsp->ws_getvar (str, i);
+    value = wsp->ws_getvar (str, i, wsp->ws_closure);
   else if (wsp->ws_flags & MU_WRDSF_UNDEF)
     {
       wsp->ws_errno = MU_WRDSE_UNDEF;
@@ -822,6 +846,14 @@ expvar (struct mu_wordsplit *wsp, const char *str, size_t len,
       memcpy (newnode->v.word, start, size);
       newnode->v.word[size] = 0;
     }
+  else
+    {
+      if (wsnode_new (wsp, &newnode))
+	return 1;
+      wsnode_insert (wsp, newnode, *ptail, 0);
+      *ptail = newnode;
+      newnode->flags = _WSNF_NULL;
+    }
   return 0;
 }
 
diff --git a/libmailutils/tests/wordsplit.at b/libmailutils/tests/wordsplit.at
index dca3792..2159b7f 100644
--- a/libmailutils/tests/wordsplit.at
+++ b/libmailutils/tests/wordsplit.at
@@ -203,6 +203,16 @@ TESTWSP([disable variable expansion],[],[novar],
 [],
 [FOO=bar])
 
+TESTWSP([K/V environment],[],[env_kv],
+[$FOO a$BAZ],
+[NF: 2
+0: bar
+1: aqux
+],
+[],
+[FOO=bar BAZ=qux])
+
+
 TESTWSP([nosplit with expansion],[],[nosplit],
 [a $FOO test],
 [NF: 1
diff --git a/libmailutils/tests/wsp.c b/libmailutils/tests/wsp.c
index 095ce66..bbb3416 100644
--- a/libmailutils/tests/wsp.c
+++ b/libmailutils/tests/wsp.c
@@ -44,6 +44,7 @@ struct mu_kwd bool_keytab[] = {
   { "warnundef", MU_WRDSF_WARNUNDEF },
   { "cescapes", MU_WRDSF_CESCAPES },
   { "default", MU_WRDSF_DEFFLAGS },
+  { "env_kv", MU_WRDSF_ENV_KV },
   { NULL, 0 }
 };
 
@@ -112,6 +113,49 @@ print_qword (const char *word, int plaintext)
     printf ("%s", qbuf);
 }
 
+/* Convert environment to K/V form */
+static char **
+make_env_kv ()
+{
+  size_t i, j, size;
+  char **newenv;
+  
+  /* Count the number of entries */
+  for (i = 0; environ[i]; i++)
+    ;
+
+  size = (i - 1) * 2 + 1;
+  newenv = calloc (size, sizeof (newenv[0]));
+  if (!newenv)
+    {
+      mu_error ("not enough memory");
+      exit (1);
+    }
+
+  for (i = j = 0; environ[i]; i++)
+    {
+      size_t len = strcspn (environ[i], "=");
+      char *p = malloc (len+1);
+      if (!p)
+	{
+	  mu_error ("not enough memory");
+	  exit (1);
+	}
+      memcpy (p, environ[i], len);
+      p[len] = 0;
+      newenv[j++] = p;
+      p = strdup (environ[i] + len + 1);
+      if (!p)
+	{
+	  mu_error ("not enough memory");
+	  exit (1);
+	}
+      newenv[j++] = p;
+    }
+  newenv[j] = NULL;
+  return newenv;
+}
+    
 int
 main (int argc, char **argv)
 {
@@ -161,7 +205,7 @@ main (int argc, char **argv)
 	  plaintext_option = !negate;
 	  continue;
 	}
-
+	  
       if (mu_kwd_xlat_name (bool_keytab, opt, &flag) == 0)
 	{
 	  if (negate)
@@ -239,7 +283,10 @@ main (int argc, char **argv)
       exit (1);
     }
 
-  ws.ws_env = (const char **) environ;
+  if (wsflags & MU_WRDSF_ENV_KV)
+    ws.ws_env = (const char **) make_env_kv ();
+  else
+    ws.ws_env = (const char **) environ;
 	
   while (fgets (buf, sizeof (buf), stdin))
     {
diff --git a/libproto/maildir/folder.c b/libproto/maildir/folder.c
index d53c526..8d8163e 100644
--- a/libproto/maildir/folder.c
+++ b/libproto/maildir/folder.c
@@ -99,7 +99,7 @@ static struct _mu_record _maildir_record =
   MU_MAILDIR_PRIO,
   MU_MAILDIR_SCHEME,
   MU_RECORD_LOCAL,
-  MU_URL_SCHEME | MU_URL_PATH,
+  MU_URL_SCHEME | MU_URL_PATH | MU_URL_PARAM,
   MU_URL_PATH,
   mu_url_expand_path, /* Url init.  */
   _mailbox_maildir_init, /* Mailbox init.  */
diff --git a/libproto/mbox/folder.c b/libproto/mbox/folder.c
index 95506e3..682ac24 100644
--- a/libproto/mbox/folder.c
+++ b/libproto/mbox/folder.c
@@ -101,7 +101,7 @@ static struct _mu_record _mbox_record =
   MU_MBOX_PRIO,
   MU_MBOX_SCHEME,
   MU_RECORD_LOCAL,
-  MU_URL_SCHEME | MU_URL_PATH,
+  MU_URL_SCHEME | MU_URL_PATH | MU_URL_PARAM,
   MU_URL_PATH,
   mu_url_expand_path, /* URL init.  */
   _mailbox_mbox_init, /* Mailbox init.  */
diff --git a/libproto/mh/folder.c b/libproto/mh/folder.c
index 90291d0..a00932a 100644
--- a/libproto/mh/folder.c
+++ b/libproto/mh/folder.c
@@ -139,7 +139,7 @@ static struct _mu_record _mh_record =
   MU_MH_PRIO,
   MU_MH_SCHEME,
   MU_RECORD_LOCAL,
-  MU_URL_SCHEME | MU_URL_PATH,
+  MU_URL_SCHEME | MU_URL_PATH | MU_URL_PARAM,
   MU_URL_PATH,
   mu_url_expand_path, /* Url init.  */
   _mailbox_mh_init, /* Mailbox init.  */
diff --git a/movemail/movemail.c b/movemail/movemail.c
index 61daad6..b8da17f 100644
--- a/movemail/movemail.c
+++ b/movemail/movemail.c
@@ -650,70 +650,98 @@ _compare_uidls (const void *item, const void *value)
 
   return strcmp (a->uidl, b->uidl);
 }
+
+struct movemail_getvar_closure
+{
+  const char *source_name;
+  const char *dest_name;
+  mu_url_t source_url;
+  mu_url_t dest_url;
+};
 
-#define __cat2__(a,b) a ## b
-#define DCL_VTX(what)						\
-static int							\
- __cat2__(_vtx_,what) (const char *name, void *data, char **p)	\
-{								\
-  mu_url_t url = data;			                        \
-  int rc = __cat2__(mu_url_aget_,what) (url, p);                \
-  if (rc == MU_ERR_NOENT)	                                \
-    {                                                           \
-      *p = strdup ("");						\
-      return 0;                                                 \
-    }                                                           \
-  return rc;                                                    \
-}
+#define SEQ(s, n, l) \
+  (((l) == (sizeof(s) - 1)) && memcmp (s, n, l) == 0)
 
-DCL_VTX (host)
-DCL_VTX (user)
-DCL_VTX (path)
+static const char *
+get_url_part (mu_url_t url, const char *name, size_t nlen)
+{
+  int rc = MU_ERR_NOENT;
+  const char *s;
+  
+  if (!url)
+    return NULL;
+  if (SEQ ("user", name, nlen))
+    rc = mu_url_sget_user (url, &s);
+  else if (SEQ ("host", name, nlen))
+    rc = mu_url_sget_host (url, &s);
+  else if (SEQ ("port", name, nlen))
+    rc = mu_url_sget_portstr (url, &s);
+  else if (SEQ ("path", name, nlen))
+    rc = mu_url_sget_path (url, &s);
+
+  if (rc)
+    return NULL;
+  return s;
+}
+    
+static const char *
+movemail_getvar (const char *name, size_t nlen, void *data)
+{
+  struct movemail_getvar_closure *pc = data;
+  
+  if (SEQ ("progname", name, nlen))
+    return mu_program_name;
+  if (SEQ ("source", name, nlen))
+    return pc->source_name;
+  if (SEQ ("dest", name, nlen))
+    return pc->dest_name;
+
+  if (nlen > 7 && memcmp ("source_", name, 7) == 0)
+    return get_url_part (pc->source_url, name + 7, nlen - 7);
+  if (nlen > 5 && memcmp ("dest_", name, 5) == 0)
+    return get_url_part (pc->dest_url, name + 5, nlen - 5);
+  
+  return NULL;
+}
 
 static void
 set_program_id (const char *source_name, const char *dest_name)
 {
   int rc;
-  mu_vartab_t vtab;
-  char *id;
-  mu_url_t url;
-      
-  mu_vartab_create (&vtab);
-  mu_vartab_define (vtab, "progname", mu_program_name, 1);
-  mu_vartab_define (vtab, "source", source_name, 1);
-  rc = mu_mailbox_get_url (source, &url);
+  struct mu_wordsplit ws;
+  struct movemail_getvar_closure clos;
+
+  clos.source_name = source_name;
+  clos.dest_name = dest_name;
+  rc = mu_mailbox_get_url (source, &clos.source_url);
   if (rc)
     mu_diag_output (MU_DIAG_INFO,
 		    _("cannot obtain source mailbox URL: %s"),
 		    mu_strerror (rc));
-  else
-    {
-      mu_vartab_define_exp (vtab, "source:user", _vtx_user, NULL, url);
-      mu_vartab_define_exp (vtab, "source:host", _vtx_host, NULL, url);
-      mu_vartab_define_exp (vtab, "source:path", _vtx_path, NULL, url);
-    }
-      
-  mu_vartab_define (vtab, "dest", dest_name, 1);
-  rc = mu_mailbox_get_url (dest, &url);
+  rc = mu_mailbox_get_url (dest, &clos.dest_url);
   if (rc)
     mu_diag_output (MU_DIAG_INFO,
 		    _("cannot obtain destination mailbox URL: %s"),
 		    mu_strerror (rc));
-  else
+  
+  ws.ws_getvar = movemail_getvar;
+  ws.ws_closure = &clos;
+  if (mu_wordsplit (program_id_option, &ws,
+		    MU_WRDSF_NOSPLIT | MU_WRDSF_NOCMD |
+		    MU_WRDSF_GETVAR | MU_WRDSF_CLOSURE))
     {
-      mu_vartab_define_exp (vtab, "dest:user", _vtx_user, NULL, url);
-      mu_vartab_define_exp (vtab, "dest:host", _vtx_host, NULL, url);
-      mu_vartab_define_exp (vtab, "dest:path", _vtx_path, NULL, url);
+      mu_error (_("cannot expand line `%s': %s"), program_id_option,
+		mu_wordsplit_strerror (&ws));
+      return;
     }
-      
-  rc = mu_vartab_expand (vtab, program_id_option, &id);
-  mu_vartab_destroy (&vtab);
-  /*      mu_asprintf (&id, "%s: %s", mu_program_name, s);
-	  free (s);*/
+  
   /* FIXME: Don't use mu_set_program_name here, because it
      plays wise with its argument. We need a mu_set_diag_prefix
      function. */
-  mu_program_name = id;
+  mu_program_name = ws.ws_wordv[0];
+  ws.ws_wordv[0] = NULL;
+  ws.ws_wordc = 0;
+  mu_wordsplit_free (&ws);
 }
 
 int
diff --git a/mu/mu.h b/mu/mu.h
index 22bcc33..4280b0e 100644
--- a/mu/mu.h
+++ b/mu/mu.h
@@ -40,7 +40,7 @@ int mutool_ldflags (int argc, char **argv);
 int mutool_cflags (int argc, char **argv);
   
 extern char *mutool_shell_prompt;
-extern mu_vartab_t mutool_prompt_vartab;
+extern char **mutool_prompt_env;
 extern int mutool_shell_interactive;
 extern mu_stream_t mustrin, mustrout;
 int mutool_shell (const char *name, struct mutool_command *cmd);
diff --git a/mu/pop.c b/mu/pop.c
index 70e5708..311bd67 100644
--- a/mu/pop.c
+++ b/mu/pop.c
@@ -152,26 +152,35 @@ pop_session_str (enum pop_session_status stat)
 }
 
 static void
-pop_prompt_vartab ()
+pop_prompt_env ()
 {
-  mu_vartab_t vtab;
-  
-  if (mu_vartab_create (&vtab))
-    return;
-  mu_vartab_define (vtab, "user",
-		    (pop_session_status == pop_session_logged_in) ?
-		      username : "not logged in", 1);
-  mu_vartab_define (vtab, "host",
-		    (pop_session_status != pop_session_disconnected) ?
-		      host : "not connected", 1);
-  mu_vartab_define (vtab, "program-name", mu_program_name, 1);
-  mu_vartab_define (vtab, "canonical-program-name", "mu", 1);
-  mu_vartab_define (vtab, "package", PACKAGE, 1);
-  mu_vartab_define (vtab, "version", PACKAGE_VERSION, 1);
-  mu_vartab_define (vtab, "status", pop_session_str (pop_session_status), 1);
-  
-  mu_vartab_destroy (&mutool_prompt_vartab);
-  mutool_prompt_vartab = vtab;
+  if (!mutool_prompt_env)
+    mutool_prompt_env = xcalloc (2*7 + 1, sizeof(mutool_prompt_env[0]));
+
+  mutool_prompt_env[0] = "user";
+  mutool_prompt_env[1] = (pop_session_status == pop_session_logged_in) ?
+                           username : "[nouser]";
+
+  mutool_prompt_env[2] = "host"; 
+  mutool_prompt_env[3] = (pop_session_status != pop_session_disconnected) ?
+                           host : "[nohost]";
+
+  mutool_prompt_env[4] = "program-name";
+  mutool_prompt_env[5] = (char*) mu_program_name;
+
+  mutool_prompt_env[6] = "canonical-program-name";
+  mutool_prompt_env[7] = "mu";
+
+  mutool_prompt_env[8] = "package";
+  mutool_prompt_env[9] = PACKAGE;
+
+  mutool_prompt_env[10] = "version";
+  mutool_prompt_env[11] = PACKAGE_VERSION;
+
+  mutool_prompt_env[12] = "status";
+  mutool_prompt_env[13] = (char*) pop_session_str (pop_session_status);
+
+  mutool_prompt_env[14] = NULL;
 }
 
 
@@ -295,7 +304,7 @@ com_user (int argc, char **argv)
   if (status == 0)
     {
       username = strdup (argv[1]);
-      pop_prompt_vartab ();
+      pop_prompt_env ();
     }
   return status;
 }
@@ -495,7 +504,7 @@ com_pass (int argc, char **argv)
   if (status == 0)
     {
       pop_session_status = pop_session_logged_in;
-      pop_prompt_vartab ();
+      pop_prompt_env ();
     }
   free (passbuf);
   return status;
@@ -593,7 +602,7 @@ com_disconnect (int argc MU_ARG_UNUSED, char **argv MU_ARG_UNUSED)
       connect_argc = 0;
       connect_argv = NULL;
       pop_session_status = pop_session_disconnected;
-      pop_prompt_vartab ();
+      pop_prompt_env ();
     }
   return 0;
 }
@@ -689,7 +698,7 @@ com_connect (int argc, char **argv)
       port = n;
       pop_session_status = pop_session_connected;
 
-      pop_prompt_vartab ();
+      pop_prompt_env ();
     }
   
   return status;
@@ -794,7 +803,7 @@ mutool_pop (int argc, char **argv)
 
   /* Command line prompt */
   mutool_shell_prompt = xstrdup ("pop> ");
-  pop_prompt_vartab ();
+  pop_prompt_env ();
   mutool_shell ("pop", pop_comtab);
   return 0;
 }
diff --git a/mu/shell.c b/mu/shell.c
index 910a13a..51ed25a 100644
--- a/mu/shell.c
+++ b/mu/shell.c
@@ -31,21 +31,10 @@
 #endif
 
 char *mutool_shell_prompt;
-mu_vartab_t mutool_prompt_vartab;
+char **mutool_prompt_env;
 int mutool_shell_interactive;
 mu_stream_t mustrin, mustrout;
 
-static char *
-expand_prompt ()
-{
-  char *str;
-  
-  if (!mutool_prompt_vartab
-      || mu_vartab_expand (mutool_prompt_vartab, mutool_shell_prompt, &str))
-    str = strdup (mutool_shell_prompt);
-  return str;
-}
-
 
 static int shell_exit (int, char **);
 static int shell_help (int, char **);
@@ -516,9 +505,22 @@ execute_line (char *line)
 static char *
 input_line_interactive ()
 {
-  char *p = expand_prompt ();
-  char *line = readline (p);
-  free (p);
+  char *line;
+  int wsflags = MU_WRDSF_NOSPLIT | MU_WRDSF_NOCMD;
+  struct mu_wordsplit ws;
+  
+  if (mutool_prompt_env)
+    {
+      ws.ws_env = (const char **)mutool_prompt_env;
+      wsflags |= MU_WRDSF_ENV | MU_WRDSF_ENV_KV;
+    }
+  if (mu_wordsplit (mutool_shell_prompt, &ws, wsflags))
+    line = readline (mutool_shell_prompt);
+  else
+    {
+      line = readline (ws.ws_wordv[0]);
+      mu_wordsplit_free (&ws);
+    }
   return line;
 }
 


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Sat Oct 30 11:32:58 2010
Received: from mailman by lists.gnu.org with archive (Exim 4.43)
	id 1PCDQQ-0006XA-Dl
	for mharc-commit-mailutils@gnu.org; Sat, 30 Oct 2010 11:32:58 -0400
Received: from [140.186.70.92] (port=53617 helo=eggs.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.43) id 1PCDQG-0006EC-ON
	for commit-mailutils@gnu.org; Sat, 30 Oct 2010 11:32:56 -0400
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1PCDEg-0005ee-G5
	for commit-mailutils@gnu.org; Sat, 30 Oct 2010 11:20:52 -0400
Received: from colonialone.fsf.org ([140.186.70.51]:53257
	helo=internal.in.savannah.gnu.org)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1PCDEg-0005eY-Dw
	for commit-mailutils@gnu.org; Sat, 30 Oct 2010 11:20:50 -0400
Received: from [10.1.0.108] (helo=vcs-noshell.in.savannah.gnu.org)
	by internal.in.savannah.gnu.org with esmtp (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1PCDEg-0001Dm-1V
	for commit-mailutils@gnu.org; Sat, 30 Oct 2010 15:20:50 +0000
Received: from gray by vcs-noshell.in.savannah.gnu.org with local (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1PCDEf-0007E4-Nw
	for commit-mailutils@gnu.org; Sat, 30 Oct 2010 15:20:50 +0000
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: 63fec21357c175772a8ca153266919c12b6f8bd5
X-Git-Newrev: c66e1b8de8fd661a34f03734cbb0a559324c3075
Message-Id: <E1PCDEf-0007E4-Nw@vcs-noshell.in.savannah.gnu.org>
Date: Sat, 30 Oct 2010 15:20:49 +0000
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
Subject: [SCM] GNU Mailutils branch, master,
	updated. release-2.2-170-gc66e1b8
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.5
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=unsubscribe>
List-Archive: <http://lists.gnu.org/archive/html/commit-mailutils>
List-Post: <mailto:commit-mailutils@gnu.org>
List-Help: <mailto:commit-mailutils-request@gnu.org?subject=help>
List-Subscribe: <http://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Sat, 30 Oct 2010 15:32: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=c66e1b8de8fd661a34f03734cbb0a559324c3075

The branch, master has been updated
       via  c66e1b8de8fd661a34f03734cbb0a559324c3075 (commit)
      from  63fec21357c175772a8ca153266919c12b6f8bd5 (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 c66e1b8de8fd661a34f03734cbb0a559324c3075
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Sat Oct 30 17:42:59 2010 +0300

    Remove all uses of vartab, replacing them with wordsplit.
    
    * libmailutils/string/wordsplit.c (mu_wordsplit_init): Call
    mu_wordsplit_free_words to reclaim the memory.
    (mu_wordsplit_free_words): New function.
    (mu_wordsplit_free): Use mu_wordsplit_free_words.
    
    * imap4d/imap4d.c (imap4d_session_setup0): Use wordsplit instead of
    vartab.
    * imap4d/imap4d.h: Don't include vartab.h.
    * imap4d/preauth.c (do_preauth_ident): Use mu_str_stripws instead of
    the static trimcrlf, which is removed.
    (do_preauth_program): Use wordsplit instead of
    vartab and mu_prog_stream_create instead of popen.
    * libmailutils/mailbox/mbx_default.c: Include nls.h
    Don't include vartab.h
    (mu_construct_user_mailbox_url): Use wordsplit instead of
    vartab.
    * libmailutils/server/acl.c (expand_arg): Use wordsplit instead of
    vartab.
    * libmu_auth/ldap.c (_mu_ldap_search):L Likewise.
    * libmu_auth/radius.c (_expand_query): Likewise.
    * libmu_auth/sql.c (mu_sql_expand_query): Likewise.
    * libproto/mailer/prog.c (_expand_sender, _expand_rcpt): Rewrite.
    (url_to_argv): : Use wordsplit instead of
    vartab.
    
    * po/POTFILES.in: Add more files.

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

Summary of changes:
 imap4d/imap4d.c                    |   39 ++++++++----
 imap4d/imap4d.h                    |    1 -
 imap4d/preauth.c                   |  114 +++++++++++++++++++--------------
 libmailutils/mailbox/mbx_default.c |   31 ++++++++--
 libmailutils/server/acl.c          |  121 +++++++++++++++++++++++++-----------
 libmailutils/string/wordsplit.c    |   16 ++++-
 libmu_auth/ldap.c                  |   35 +++++++----
 libmu_auth/radius.c                |   54 ++++++++---------
 libmu_auth/sql.c                   |   35 +++++++---
 libproto/mailer/prog.c             |  118 +++++++++++++++++++++--------------
 po/POTFILES.in                     |    1 +
 11 files changed, 356 insertions(+), 209 deletions(-)

diff --git a/imap4d/imap4d.c b/imap4d/imap4d.c
index cbadb83..844b953 100644
--- a/imap4d/imap4d.c
+++ b/imap4d/imap4d.c
@@ -308,21 +308,34 @@ imap4d_session_setup0 ()
 
   if (modify_homedir)
     {
-      int rc;
-      mu_vartab_t vtab;
       char *expr = mu_tilde_expansion (modify_homedir, "/", real_homedir);
-
-      mu_vartab_create (&vtab);
-      mu_vartab_define (vtab, "user", auth_data->name, 0);
-      mu_vartab_define (vtab, "home", real_homedir, 0);
-      rc = mu_vartab_expand (vtab, expr, &imap4d_homedir);
-      mu_vartab_destroy (&vtab);
-      free (expr);
-      if (rc)
+      struct mu_wordsplit ws;
+      const char *env[3];
+
+      env[0] = "user";
+      env[1] = auth_data->name;
+      env[2] = "home";
+      env[3] = real_homedir;
+      env[4] = NULL;
+
+      ws.ws_env = env;
+      if (mu_wordsplit (expr, &ws,
+			MU_WRDSF_NOSPLIT | MU_WRDSF_NOCMD |
+			MU_WRDSF_ENV | MU_WRDSF_ENV_KV))
+	{
+	  mu_error (_("cannot expand line `%s': %s"), expr,
+		    mu_wordsplit_strerror (&ws));
+	  return 1;
+	}
+      else if (ws.ws_wordc == 0)
+	{
+	  mu_error (_("expanding %s yields empty string"), expr);
+	  return 1;
+	}
+      imap4d_homedir = strdup (ws.ws_wordv[0]);
+      if (!imap4d_homedir)
 	{
-	  free (real_homedir);
-	  mu_diag_funcall (MU_DIAG_ERROR, "mu_vartab_expand",
-			   modify_homedir, rc);
+	  mu_error ("%s", mu_strerror (errno));
 	  return 1;
 	}
     }
diff --git a/imap4d/imap4d.h b/imap4d/imap4d.h
index 26e9fc3..598bc58 100644
--- a/imap4d/imap4d.h
+++ b/imap4d/imap4d.h
@@ -100,7 +100,6 @@
 #include <mailutils/server.h>
 #include <mailutils/wordsplit.h>
 #include <mailutils/alloc.h>
-#include <mailutils/vartab.h>
 #include <mailutils/cctype.h>
 #include <mailutils/cstr.h>
 #include <mailutils/io.h>
diff --git a/imap4d/preauth.c b/imap4d/preauth.c
index 1584df4..014da68 100644
--- a/imap4d/preauth.c
+++ b/imap4d/preauth.c
@@ -61,22 +61,6 @@ ident_extract_username (char *reply)
 }
 
 static int
-trimcrlf (char *buf)
-{
-  int len = strlen (buf);
-  if (len == 0)
-    return 0;
-  if (buf[len-1] == '\n')
-    {
-      len--;
-      if (buf[len-1] == '\r')
-	len--;
-      buf[len] = 0;
-    }
-  return len;
-}
-
-static int
 is_des_p (const char *name)
 {
   int len = strlen (name);
@@ -369,8 +353,7 @@ do_preauth_ident (struct sockaddr *clt_sa, struct sockaddr *srv_sa)
       return NULL;
     }
   mu_diag_output (MU_DIAG_INFO, "Got %s", buf);
-  trimcrlf (buf);
-  name = ident_extract_username (buf);
+  name = ident_extract_username (mu_str_stripws (buf));
   if (!name)
     mu_diag_output (MU_DIAG_INFO,
 		    _("malformed IDENT response: `%s', from %s:%d"),
@@ -405,48 +388,83 @@ do_preauth_ident (struct sockaddr *clt_sa, struct sockaddr *srv_sa)
 }
 
 
+#define SEQ(s, n, l) \
+  (((l) == (sizeof(s) - 1)) && memcmp (s, n, l) == 0)
+
+struct preauth_closure
+{
+  struct sockaddr_in *s_clt, *s_srv;
+};
+
+static const char *
+preauth_getvar (const char *name, size_t nlen, void *data)
+{
+  struct preauth_closure *clos = data;
+  
+  if (clos->s_clt && clos->s_clt->sin_family == AF_INET)
+    {
+      if (SEQ ("client_address", name, nlen))
+        return inet_ntoa (clos->s_clt->sin_addr);
+      if (SEQ ("client_prot", name, nlen))
+        return mu_umaxtostr (0, ntohs (clos->s_clt->sin_port));
+    }
+  if (clos->s_srv && clos->s_srv->sin_family == AF_INET)
+    {
+      if (SEQ ("server_address", name, nlen))
+        return inet_ntoa (clos->s_srv->sin_addr);
+      if (SEQ ("server_port", name, nlen))
+        return mu_umaxtostr (0, ntohs (clos->s_srv->sin_port));
+    }
+  return NULL;
+}
+  
 /* External (program) preauth */
 static char *
 do_preauth_program (struct sockaddr *pcs, struct sockaddr *sa)
 {
   int rc;
-  mu_vartab_t vtab;
-  char *cmd;
-  FILE *fp;
+  mu_stream_t str;
   char *buf = NULL;
-  size_t size = 0;
-  
-  mu_vartab_create (&vtab);
-  if (pcs && pcs->sa_family == AF_INET)
+  size_t size = 0, n;
+  struct mu_wordsplit ws;
+  struct preauth_closure clos;
+
+  clos.s_clt = (struct sockaddr_in *) pcs;
+  clos.s_srv = (struct sockaddr_in *) sa;
+
+  ws.ws_getvar = preauth_getvar;
+  ws.ws_closure = &clos;
+  if (mu_wordsplit (preauth_program, &ws,
+		    MU_WRDSF_NOSPLIT | MU_WRDSF_NOCMD |
+		    MU_WRDSF_GETVAR | MU_WRDSF_CLOSURE))
     {
-      struct sockaddr_in *s_in = (struct sockaddr_in *)pcs;
-      mu_vartab_define (vtab, "client_address", inet_ntoa (s_in->sin_addr), 0);
-      mu_vartab_define (vtab, "client_port",
-			mu_umaxtostr (0, ntohs (s_in->sin_port)), 0);
+      mu_error (_("cannot expand line `%s': %s"), preauth_program,
+		mu_wordsplit_strerror (&ws));
+      return NULL;
     }
-  if (sa && sa->sa_family == AF_INET)
+  else if (ws.ws_wordc == 0)
     {
-      struct sockaddr_in *s_in = (struct sockaddr_in *) sa;
-      mu_vartab_define (vtab, "server_address", inet_ntoa (s_in->sin_addr), 0);
-      mu_vartab_define (vtab, "server_port",
-			mu_umaxtostr (0, ntohs (s_in->sin_port)), 0);
+      mu_wordsplit_free (&ws);
+      mu_error (_("`%s' expands to an empty line"), preauth_program);
+      return NULL;
     }
-  rc = mu_vartab_expand (vtab, preauth_program, &cmd);
-  mu_vartab_destroy (&vtab);
-  if (rc)
-    return NULL;
 
-  fp = popen (cmd, "r");
-  free (cmd);
-  rc = getline (&buf, &size, fp);
-  pclose (fp);
-  if (rc > 0)
+  rc = mu_prog_stream_create (&str, ws.ws_wordv[0], MU_STREAM_READ);
+  mu_wordsplit_free (&ws);
+  if (rc)
     {
-      if (trimcrlf (buf) == 0)
-	{
-	  free (buf);
-	  return NULL;
-	}
+      mu_error (_("cannot open input pipe from %s"), preauth_program);
+      return NULL;
+    }
+  rc = mu_stream_getline (str, &buf, &size, &n);
+  mu_stream_destroy (&str);
+  if (rc)
+    {
+      mu_error (_("read from `%s' failed"), preauth_program);
+    }
+  else
+    {
+      mu_rtrim_cset (buf, "\r\n");
       return buf;
     }
   return NULL;
diff --git a/libmailutils/mailbox/mbx_default.c b/libmailutils/mailbox/mbx_default.c
index 468230a..c36e682 100644
--- a/libmailutils/mailbox/mbx_default.c
+++ b/libmailutils/mailbox/mbx_default.c
@@ -29,15 +29,16 @@
 
 #include <confpaths.h>
 
+#include <mailutils/nls.h>
 #include <mailutils/mailbox.h>
 #include <mailutils/util.h>
 #include <mailutils/debug.h>
 #include <mailutils/error.h>
 #include <mailutils/errno.h>
 #include <mailutils/mu_auth.h>
-#include <mailutils/vartab.h>
 #include <mailutils/folder.h>
 #include <mailutils/auth.h>
+#include <mailutils/wordsplit.h>
 
 #include <mailutils/sys/mailbox.h>
 
@@ -137,12 +138,30 @@ mu_construct_user_mailbox_url (char **pout, const char *name)
 {
   int rc;
   const char *pat = mu_mailbox_url ();
-  mu_vartab_t vtab;
+  const char *env[3];
+  struct mu_wordsplit ws;
+
+  env[0] = "user";
+  env[1] = (char*) name;
+  env[3] = NULL;
+  ws.ws_env = env;
+  if (mu_wordsplit (pat, &ws,
+		    MU_WRDSF_NOSPLIT | MU_WRDSF_NOCMD |
+		    MU_WRDSF_ENV | MU_WRDSF_ENV_KV))
+    {
+      mu_error (_("cannot expand line `%s': %s"), pat,
+		mu_wordsplit_strerror (&ws));
+      return errno;
+    }
 
-  mu_vartab_create (&vtab);
-  mu_vartab_define (vtab, "user", name, 1);
-  rc = mu_vartab_expand (vtab, pat, pout);
-  mu_vartab_destroy (&vtab);
+  if (ws.ws_wordc == 0)
+    /* FIXME: a special return code maybe? */
+    *pout = strdup ("");
+  else
+    *pout = strdup (ws.ws_wordv[0]);
+  mu_wordsplit_free (&ws);
+  if (!*pout)
+    return ENOMEM;
   return rc;
 }
 
diff --git a/libmailutils/server/acl.c b/libmailutils/server/acl.c
index d7a1362..27d2352 100644
--- a/libmailutils/server/acl.c
+++ b/libmailutils/server/acl.c
@@ -36,7 +36,6 @@
 #include <mailutils/error.h>
 #include <mailutils/errno.h>
 #include <mailutils/kwd.h>
-#include <mailutils/vartab.h>
 #include <mailutils/io.h>
 
 struct _mu_acl_entry
@@ -500,18 +499,12 @@ struct run_closure
   unsigned idx;
   mu_debug_t debug;
   struct sockaddr *sa;
+  char *numbuf;
+  char *portbuf;
   int salen;
   mu_acl_result_t *result;
 };
 
-static int
-_expand_aclno (const char *name, void *data, char **p)
-{
-  struct run_closure *rp = data;
-  /*FIXME: memory leak*/
-  return mu_asprintf (p, "%u", rp->idx);
-}
-
 #if defined (HAVE_SYSCONF) && defined (_SC_OPEN_MAX)
 # define getmaxfd() sysconf (_SC_OPEN_MAX)
 #elif defined (HAVE_GETDTABLESIZE)
@@ -520,52 +513,103 @@ _expand_aclno (const char *name, void *data, char **p)
 # define getmaxfd() 64
 #endif
 
-static int
-expand_arg (const char *cmdline, struct run_closure *rp, char **s)
+#define SEQ(s, n, l) \
+  (((l) == (sizeof(s) - 1)) && memcmp (s, n, l) == 0)
+
+static const char *
+acl_getvar (const char *name, size_t nlen, void *data)
 {
-  int rc;
-  mu_vartab_t vtab;
-  
-  MU_DEBUG1 (rp->debug, MU_DEBUG_TRACE0, "Expanding \"%s\" => ", cmdline);
+  struct run_closure *rp = data;
+
+  if (SEQ ("aclno", name, nlen))
+    {
+      if (!rp->numbuf && mu_asprintf (&rp->numbuf, "%u", rp->idx))
+	return NULL;
+      return rp->numbuf;
+    }
   
-  mu_vartab_create (&vtab);
-  mu_vartab_define_exp (vtab, "aclno", _expand_aclno, NULL, rp);
   switch (rp->sa->sa_family)
     {
     case AF_INET:
       {
 	struct sockaddr_in *s_in = (struct sockaddr_in *)rp->sa;
-	struct in_addr addr = s_in->sin_addr;
-	char *p;
+
+	if (SEQ ("address", name, nlen))
+	  {
+	    struct in_addr addr = s_in->sin_addr;
+	    addr.s_addr = htonl (addr.s_addr);
+	    return inet_ntoa (addr);
+	  }
+
+	if (SEQ ("port", name, nlen))
+	  {
+	    if (!rp->portbuf &&
+		mu_asprintf (&rp->portbuf, "%hu", ntohs (s_in->sin_port)))
+	      return NULL;
+	    return rp->portbuf;
+	  }
+	break;
 	
-	mu_vartab_define (vtab, "family", "AF_INET", 1);
-	addr.s_addr = htonl (addr.s_addr);
-	mu_vartab_define (vtab, "address", inet_ntoa (addr), 0);
-	if (mu_asprintf (&p, "%hu", ntohs (s_in->sin_port)) == 0)
+      case AF_UNIX:
+	if (SEQ ("address", name, nlen))
 	  {
-	    mu_vartab_define (vtab, "port", p, 0);
-	    free (p);
+	    struct sockaddr_un *s_un = (struct sockaddr_un *)rp->sa;
+	    if (rp->salen == sizeof (s_un->sun_family))
+	      return NULL;
+	    else
+	      return s_un->sun_path;
 	  }
       }
       break;
-      
+    }
+  return NULL;
+}
+
+static int
+expand_arg (const char *cmdline, struct run_closure *rp, char **s)
+{
+  int rc;
+  struct mu_wordsplit ws;
+  const char *env[3];
+  
+  MU_DEBUG1 (rp->debug, MU_DEBUG_TRACE0, "Expanding \"%s\" => ", cmdline);
+  env[0] = "family";
+  switch (rp->sa->sa_family)
+    {
+    case AF_INET:
+      env[1] = "AF_INET";
+      break;
+
     case AF_UNIX:
-      {
-	struct sockaddr_un *s_un = (struct sockaddr_un *)rp->sa;
-	
-	mu_vartab_define (vtab, "family", "AF_UNIX", 1);
-	mu_vartab_define (vtab, "address", s_un->sun_path, 1);
-      }
+      env[1] = "AF_UNIX";
       break;
     }
-  
-  rc = mu_vartab_expand (vtab, cmdline, s);
-  mu_vartab_destroy (&vtab);
+  env[2] = NULL;
+  ws.ws_env = env;
+  ws.ws_getvar = acl_getvar;
+  ws.ws_closure = rp;
+  rc = mu_wordsplit (cmdline, &ws,
+		     MU_WRDSF_NOSPLIT | MU_WRDSF_NOCMD |
+		     MU_WRDSF_ENV | MU_WRDSF_ENV_KV |
+		     MU_WRDSF_GETVAR | MU_WRDSF_CLOSURE);
 
   if (rc == 0)
-    MU_DEBUG1 (rp->debug, MU_DEBUG_TRACE0, "\"%s\". ", *s);
+    {
+      *s = strdup (ws.ws_wordv[0]);
+      mu_wordsplit_free (&ws);
+      if (!*s)
+	{
+	  MU_DEBUG (rp->debug, MU_DEBUG_TRACE0, "failed: not enough memory. ");
+	  return ENOMEM;
+	}
+      MU_DEBUG1 (rp->debug, MU_DEBUG_TRACE0, "\"%s\". ", *s);
+    }
   else
-    MU_DEBUG (rp->debug, MU_DEBUG_TRACE0, "failed. ");
+    {
+      MU_DEBUG1 (rp->debug, MU_DEBUG_TRACE0, "failed: %s",
+		 mu_wordsplit_strerror (&ws));
+      rc = errno;
+    }
   return rc;
 }
 
@@ -744,7 +788,10 @@ mu_acl_check_sockaddr (mu_acl_t acl, const struct sockaddr *sa, int salen,
   r.debug = acl->debug;
   r.result = pres;
   *r.result = mu_acl_result_undefined;
+  r.numbuf = r.portbuf = NULL;
   mu_list_do (acl->aclist, _run_entry, &r);
+  free (r.numbuf);
+  free (r.portbuf);
   free (r.sa);
   return 0;
 }
diff --git a/libmailutils/string/wordsplit.c b/libmailutils/string/wordsplit.c
index 563752c..a7ed939 100644
--- a/libmailutils/string/wordsplit.c
+++ b/libmailutils/string/wordsplit.c
@@ -143,7 +143,7 @@ mu_wordsplit_init (struct mu_wordsplit *wsp, const char *input, size_t len,
   if (wsp->ws_flags & MU_WRDSF_REUSE)
     {
       if (!(wsp->ws_flags & MU_WRDSF_APPEND))
-	wsp->ws_wordc = 0;
+	mu_wordsplit_free_words (wsp);
     }
   else
     {
@@ -1427,7 +1427,7 @@ mu_wordsplit (const char *command, struct mu_wordsplit *ws, int flags)
 }
 
 void
-mu_wordsplit_free (struct mu_wordsplit *ws)
+mu_wordsplit_free_words (struct mu_wordsplit *ws)
 {
   size_t i;
 
@@ -1435,8 +1435,18 @@ mu_wordsplit_free (struct mu_wordsplit *ws)
     {
       char *p = ws->ws_wordv[ws->ws_offs + i];
       if (p)
-	free (p);
+	{
+	  free (p);
+	  ws->ws_wordv[ws->ws_offs + i] = NULL;
+	}
     }
+  ws->ws_wordc = 0;
+}
+
+void
+mu_wordsplit_free (struct mu_wordsplit *ws)
+{
+  mu_wordsplit_free_words (ws);
   free (ws->ws_wordv);
   ws->ws_wordv = NULL;
 }
diff --git a/libmu_auth/ldap.c b/libmu_auth/ldap.c
index 435c99d..5de7834 100644
--- a/libmu_auth/ldap.c
+++ b/libmu_auth/ldap.c
@@ -46,7 +46,6 @@
 #include "mailutils/md5.h"
 #include "mailutils/sha1.h"
 #include "mailutils/ldap.h"
-#include "mailutils/vartab.h"
 
 #include <ldap.h>
 #include <lber.h>
@@ -504,33 +503,43 @@ _mu_ldap_search (LDAP *ld, const char *filter_pat, const char *key,
 		 struct mu_auth_data **return_data)
 {
   int rc;
-  char *filter;
   char **attrs;
   size_t nattrs;
   LDAPMessage *res, *msg;
   ber_int_t msgid;
-  mu_vartab_t vtab;
+  const char *env[3];
+  struct mu_wordsplit ws;
 
   rc = _construct_attr_array (&nattrs, &attrs);
   if (rc)
     return rc;
 
-  mu_vartab_create (&vtab);
-  mu_vartab_define (vtab, "user", key, 1);
-  mu_vartab_define (vtab, "u", key, 1);
-  rc = mu_vartab_expand (vtab, filter_pat, &filter);
-  mu_vartab_destroy (&vtab);
-  if (rc)
+  env[0] = "user";
+  env[1] = key;
+  env[3] = NULL;
+
+  ws.ws_env = env;
+  if (mu_wordsplit (filter_pat, &ws,
+		    MU_WRDSF_NOSPLIT | MU_WRDSF_NOCMD |
+		    MU_WRDSF_ENV | MU_WRDSF_ENV_KV))
+    {
+      mu_error (_("cannot expand line `%s': %s"), filter_pat,
+		mu_wordsplit_strerror (&ws));
+      return MU_ERR_FAILURE;
+    }
+  else if (ws.ws_wordc == 0)
     {
+      mu_error (_("expanding %s yields empty string"), filter_pat);
+      mu_wordsplit_free (&ws);
       mu_argcv_free (nattrs, attrs);
-      return ENOMEM;
+      return MU_ERR_FAILURE;
     }
-
+  
   rc = ldap_search_ext (ld, ldap_param.base, LDAP_SCOPE_SUBTREE,
-			filter, attrs, 0,
+			ws.ws_wordv[0], attrs, 0,
 			NULL, NULL, NULL, -1, &msgid);
+  mu_wordsplit_free (&ws);
   mu_argcv_free (nattrs, attrs);
-  free (filter);
 
   if (rc != LDAP_SUCCESS)
     {
diff --git a/libmu_auth/radius.c b/libmu_auth/radius.c
index 8c8a648..b7a7f2b 100644
--- a/libmu_auth/radius.c
+++ b/libmu_auth/radius.c
@@ -40,7 +40,6 @@
 #include <mailutils/error.h>
 #include <mailutils/errno.h>
 #include <mailutils/nls.h>
-#include <mailutils/vartab.h>
 #include <mailutils/io.h>
 #include <mailutils/cctype.h>
 
@@ -245,41 +244,38 @@ mu_radius_module_init (enum mu_gocs_op op, void *data)
 static char *
 _expand_query (const char *query, const char *ustr, const char *passwd)
 {
-  int rc;
-  mu_vartab_t vtab;
-  char *str, *ret;
-
-  if (!query)
-    return NULL;
-
-  mu_vartab_create (&vtab);
-  if (ustr)
-    {
-      mu_vartab_define (vtab, "user", ustr, 1);
-      mu_vartab_define (vtab, "u", ustr, 1);
-    }
-
-  if (passwd)
+  struct mu_wordsplit ws;
+  const char *env[2 * 2 + 1];
+  char *ret;
+  
+  env[0] = "user";
+  env[1] = (char*) ustr;
+  env[2] = "passwd";
+  env[3] = (char*) passwd;
+  env[4] = NULL;
+
+  ws.ws_env = env;
+  if (mu_wordsplit (query, &ws,
+		    MU_WRDSF_NOSPLIT | MU_WRDSF_NOCMD |
+		    MU_WRDSF_ENV | MU_WRDSF_ENV_KV))
     {
-      mu_vartab_define (vtab, "passwd", passwd, 1);
-      mu_vartab_define (vtab, "p", passwd, 1);
+      mu_error (_("cannot expand line `%s': %s"), query,
+		mu_wordsplit_strerror (&ws));
+      return NULL;
     }
-
-  rc = mu_vartab_expand (vtab, query, &str);
-  if (rc == 0)
+  else if (ws.ws_wordc == 0)
     {
-      ret = grad_emalloc (strlen (str) + 1);
-      strcpy (ret, str);
-      free (str);
+      mu_error (_("expanding %s yields empty string"), query);
+      mu_wordsplit_free (&ws);
+      return NULL;
     }
-  else
-    ret = NULL;
-
-  mu_vartab_destroy (&vtab);
+  
+  ret = grad_emalloc (strlen (ws.ws_wordv[0]) + 1);
+  strcpy (ret, ws.ws_wordv[0]);
+  mu_wordsplit_free (&ws);
   return ret;
 }
 
-
 
 static grad_avp_t *
 create_request (grad_avp_t *template, const char *ustr, const char *passwd)
diff --git a/libmu_auth/sql.c b/libmu_auth/sql.c
index 744701f..e622470 100644
--- a/libmu_auth/sql.c
+++ b/libmu_auth/sql.c
@@ -49,7 +49,6 @@
 #include <mailutils/nls.h>
 #include <mailutils/util.h>
 #include <mailutils/sql.h>
-#include <mailutils/vartab.h>
 #include <mailutils/cstr.h>
 #include "sql.h"
 
@@ -91,20 +90,34 @@ mu_sql_expand_query (const char *query, const char *ustr)
   int rc;
   char *res;
   char *esc_ustr;
-  mu_vartab_t vtab;
-  
+  struct mu_wordsplit ws;
+  const char *env[2 + 1];
+
   if (!query)
     return NULL;
 
   esc_ustr = sql_escape_string (ustr);
-  mu_vartab_create (&vtab);
-  mu_vartab_define (vtab, "user", ustr, 1);
-  mu_vartab_define (vtab, "u", ustr, 1);
-  rc = mu_vartab_expand (vtab, query, &res);
-  if (rc)
-    res = NULL;
-  mu_vartab_destroy (&vtab);
-
+  env[0] = "user";
+  env[1] = (char*) ustr;
+  env[2] = NULL;
+
+  ws.ws_env = env;
+  if (mu_wordsplit (query, &ws,
+		    MU_WRDSF_NOSPLIT | MU_WRDSF_NOCMD |
+		    MU_WRDSF_ENV | MU_WRDSF_ENV_KV))
+    {
+      mu_error (_("cannot expand line `%s': %s"), query,
+		mu_wordsplit_strerror (&ws));
+      return NULL;
+    }
+  else if (ws.ws_wordc == 0)
+    {
+      mu_error (_("expanding %s yields empty string"), query);
+      mu_wordsplit_free (&ws);
+      return NULL;
+    }
+  res = strdup (ws.ws_wordv[0]);
+  mu_wordsplit_free (&ws);
   free (esc_ustr);
   return res;
 }
diff --git a/libproto/mailer/prog.c b/libproto/mailer/prog.c
index 6a3c615..0a7428c 100644
--- a/libproto/mailer/prog.c
+++ b/libproto/mailer/prog.c
@@ -32,7 +32,7 @@
 #include <mailutils/message.h>
 #include <mailutils/observer.h>
 #include <mailutils/progmailer.h>
-#include <mailutils/vartab.h>
+#include <mailutils/wordsplit.h>
 
 #include <mailutils/sys/url.h>
 #include <mailutils/sys/mailer.h>
@@ -140,26 +140,24 @@ prog_close (mu_mailer_t mailer)
   return mu_progmailer_close (mailer->data);
 }
 
-static int
-_expand_sender (const char *name, void *data, char **p)
-{
-  mu_address_t addr = data;
-  char *email;
-  int status = mu_address_aget_email (addr, 1, &email);
-
-  if (status != 0)
-    return status;
-  *p = email;
-  return 0;
-}
-
-struct ex_rcpt
+struct prog_exp
 {
   mu_message_t msg;
-  mu_address_t addr;
-  char *string;
+  mu_address_t sender_addr;
+  char *sender_str;
+  mu_address_t rcpt_addr;
+  char *rcpt_str;
 };
 
+static const char *
+_expand_sender (struct prog_exp *pe)
+{
+  if (!pe->sender_str &&
+      mu_address_aget_email (pe->sender_addr, 1, &pe->sender_str))
+    return NULL;
+  return pe->sender_str;
+}
+
 static int
 address_add (mu_address_t *paddr, const char *value)
 {
@@ -206,28 +204,27 @@ message_read_rcpt (mu_message_t msg, mu_address_t *paddr)
   return 0;
 }
 
-static int
-_expand_rcpt (const char *name, void *data, char **p)
+static const char *
+_expand_rcpt (struct prog_exp *pe)
 {
-  struct ex_rcpt *exrcpt = data;
   int status;
 
-  if (!exrcpt->string)
+  if (!pe->rcpt_str)
     {
       size_t i, count = 0;
       size_t len = 0;
       char *str;
       mu_address_t tmp_addr = NULL, addr;
       
-      if (exrcpt->addr)
-	addr = exrcpt->addr;
+      if (pe->rcpt_addr)
+	addr = pe->rcpt_addr;
       else
 	{
-	  status = message_read_rcpt (exrcpt->msg, &tmp_addr);
+	  status = message_read_rcpt (pe->msg, &tmp_addr);
 	  if (status)
 	    {
 	      mu_address_destroy (&tmp_addr);
-	      return status;
+	      return NULL;
 	    }
 	  addr = tmp_addr;
 	}
@@ -241,7 +238,7 @@ _expand_rcpt (const char *name, void *data, char **p)
 	  if ((status = mu_address_sget_email (addr, i, &email)) != 0)
 	    {
 	      mu_address_destroy (&tmp_addr);
-	      return status;
+	      return NULL;
 	    }
 	  len += strlen (email);
 	}
@@ -250,9 +247,9 @@ _expand_rcpt (const char *name, void *data, char **p)
       if (!str)
 	{
 	  mu_address_destroy (&tmp_addr);
-	  return ENOMEM;
+	  return NULL;
 	}
-      exrcpt->string = str;
+      pe->rcpt_str = str;
       
       for (i = 1; i <= count; i++)
 	{
@@ -267,14 +264,22 @@ _expand_rcpt (const char *name, void *data, char **p)
       *str = 0;
       mu_address_destroy (&tmp_addr);
     }  
-  *p = exrcpt->string;
-  return 0;
+  return pe->rcpt_str;
 }
 
-void
-_free_rcpt (void *data, char *value)
+#define SEQ(s, n, l) \
+  (((l) == (sizeof(s) - 1)) && memcmp (s, n, l) == 0)
+
+static const char *
+prog_getvar (const char *name, size_t nlen, void *data)
 {
-  free (value);
+  struct prog_exp *pe = data;
+  
+  if (SEQ ("sender", name, nlen))
+    return _expand_sender (pe);
+  if (SEQ ("rcpt", name, nlen))
+    return _expand_rcpt (pe);
+  return NULL;
 }
 
 static int
@@ -283,41 +288,58 @@ url_to_argv (mu_url_t url, mu_message_t msg,
 	     int *pargc, char ***pargv)
 {
   int rc;
-  mu_vartab_t vtab;
-  struct ex_rcpt ex_rcpt;
+  struct prog_exp pe;
   char **query;
   size_t i;
   size_t argc;
   char **argv;
+  struct mu_wordsplit ws;
+  int wsflags;
   
-  ex_rcpt.msg = msg;
-  ex_rcpt.addr = to;
-  ex_rcpt.string = NULL;
-  mu_vartab_create (&vtab);
-  mu_vartab_define_exp (vtab, "sender", _expand_sender, NULL, from);
-  mu_vartab_define_exp (vtab, "rcpt", _expand_rcpt, _free_rcpt, &ex_rcpt);
+  pe.msg = msg;
+  pe.rcpt_addr = to;
+  pe.sender_addr = from;
+  pe.sender_str = pe.rcpt_str = NULL;
+
+  ws.ws_getvar = prog_getvar;
+  ws.ws_closure = &pe;
+  wsflags = MU_WRDSF_NOSPLIT | MU_WRDSF_NOCMD |
+            MU_WRDSF_GETVAR | MU_WRDSF_CLOSURE;
 
   rc = mu_url_sget_query (url, &argc, &query);
   if (rc)
     return rc;
 
-  argv = calloc (argc + 1, sizeof (argv[0]));
+  argv = calloc (argc + 2, sizeof (argv[0]));
   if (!argv)
     return ENOMEM;
 
+  rc = mu_url_aget_path (url, &argv[0]);
+  if (rc)
+    {
+      free (argv);
+      return rc;
+    }
+  
   for (i = 0; i < argc; i++)
     {
-      if ((rc = mu_vartab_expand (vtab, query[i], &argv[i])))
+      if (mu_wordsplit (query[i], &ws, wsflags))
 	{
 	  mu_argcv_free (i, argv);
-	  mu_vartab_destroy (&vtab);
-	  return rc;
+	  mu_wordsplit_free (&ws);
+	  return errno;
 	}
+      if (ws.ws_wordc == 0)
+	argv[i+1] = strdup ("");
+      else
+	argv[i+1] = strdup (ws.ws_wordv[0]);
+      wsflags |= MU_WRDSF_REUSE;
     }
-  argv[i] = NULL;
+  argv[i+1] = NULL;
+  mu_wordsplit_free (&ws);
+  free (pe.sender_str);
+  free (pe.rcpt_str);
   
-  mu_vartab_destroy (&vtab);
-
   *pargc = argc;
   *pargv = argv;
   return 0;
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 2201ad1..c23e197 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -140,6 +140,7 @@ libmailutils/server/ipsrv.c
 libmailutils/server/msrv.c
 
 libmailutils/mailbox/message.c
+libmailutils/mailbox/mbx_default.c
 libmailutils/mailer/mailer.c
 libmailutils/mailer/smtp.c
 libmailutils/mailer/smtp_gsasl.c


hooks/post-receive
-- 
GNU Mailutils


