From MAILER-DAEMON Thu Aug 12 09:59:32 2010
Received: from mailman by lists.gnu.org with archive (Exim 4.43)
	id 1OjYJf-0005ky-Tn
	for mharc-commit-mailutils@gnu.org; Thu, 12 Aug 2010 09:59:32 -0400
Received: from [140.186.70.92] (port=37214 helo=eggs.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.43) id 1OjYJd-0005jr-0W
	for commit-mailutils@gnu.org; Thu, 12 Aug 2010 09:59:30 -0400
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1OjYJb-0007t6-Rn
	for commit-mailutils@gnu.org; Thu, 12 Aug 2010 09:59:28 -0400
Received: from colonialone.fsf.org ([140.186.70.51]:59589
	helo=internal.in.savannah.gnu.org)
	by eggs.gnu.org with esmtp (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1OjYJb-0007t0-Lj
	for commit-mailutils@gnu.org; Thu, 12 Aug 2010 09:59:27 -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 1OjYJb-00018u-8f
	for commit-mailutils@gnu.org; Thu, 12 Aug 2010 13:59:27 +0000
Received: from gray by vcs-noshell.in.savannah.gnu.org with local (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1OjYJa-0004WC-Sg
	for commit-mailutils@gnu.org; Thu, 12 Aug 2010 13:59:27 +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: ce10588ad7e5f4656874d884ff3cedf4ef89165d
X-Git-Newrev: bf0d23962442075cf16eab955435de99bea961de
Message-Id: <E1OjYJa-0004WC-Sg@vcs-noshell.in.savannah.gnu.org>
Date: Thu, 12 Aug 2010 13:59:26 +0000
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
Subject: [SCM] GNU Mailutils branch, master, updated. rel-2_1-90-gbf0d239
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, 12 Aug 2010 13:59: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=bf0d23962442075cf16eab955435de99bea961de

The branch, master has been updated
       via  bf0d23962442075cf16eab955435de99bea961de (commit)
      from  ce10588ad7e5f4656874d884ff3cedf4ef89165d (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 bf0d23962442075cf16eab955435de99bea961de
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Thu Aug 12 16:34:49 2010 +0300

    Bugfixes.
    
    * mailbox/mime.c (mu_mime_get_num_parts): Assume MIME message
    is not scanned if nmtp_parts are 0 and boundary is NULL.
    * mailbox/rfc2047.c (mu_rfc2047_decode): Free the buffer prior
    to returning a non-zero status.
    * mailbox/message.c (mu_message_destroy): Install a kludge
    to work over the slopy ref semantics.
    * mailbox/auth.c (mu_authority_destroy): Free auth_methods
    list.
    * mailbox/locker.c (destroy_dotlock): Free data.dot.nfslock.

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

Summary of changes:
 mailbox/auth.c    |    1 +
 mailbox/locker.c  |    3 ++-
 mailbox/message.c |   17 ++++++++++++++---
 mailbox/mime.c    |    3 ++-
 mailbox/rfc2047.c |    7 +++++--
 5 files changed, 24 insertions(+), 7 deletions(-)

diff --git a/mailbox/auth.c b/mailbox/auth.c
index 7975050..95f5faf 100644
--- a/mailbox/auth.c
+++ b/mailbox/auth.c
@@ -69,6 +69,7 @@ mu_authority_destroy (mu_authority_t *pauthority, void *owner)
       if (authority->owner == owner)
 	{
 	  mu_ticket_destroy (&authority->ticket);
+	  mu_list_destroy (&authority->auth_methods);
 	  free (authority);
 	}
       *pauthority = NULL;
diff --git a/mailbox/locker.c b/mailbox/locker.c
index bbe41f9..f752c16 100644
--- a/mailbox/locker.c
+++ b/mailbox/locker.c
@@ -701,6 +701,7 @@ static void
 destroy_dotlock (mu_locker_t locker)
 {
   free (locker->data.dot.dotlock);
+  free (locker->data.dot.nfslock);
 }
 
 #ifndef MAXHOSTNAMELEN
@@ -721,7 +722,7 @@ lock_dotlock (mu_locker_t locker, enum mu_locker_mode mode)
     {
       unlink (locker->data.dot.nfslock);
       free (locker->data.dot.nfslock);
-      locker->data.dot.nfslock = 0;
+      locker->data.dot.nfslock = NULL;
     }
 
   expire_stale_lock (locker);
diff --git a/mailbox/message.c b/mailbox/message.c
index 33730a5..b05d2dd 100644
--- a/mailbox/message.c
+++ b/mailbox/message.c
@@ -105,7 +105,18 @@ mu_message_destroy (mu_message_t *pmsg, void *owner)
       int destroy_lock = 0;
 
       mu_monitor_wrlock (monitor);
-      msg->ref--;
+      /* 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))
 	{
@@ -155,8 +166,8 @@ mu_message_destroy (mu_message_t *pmsg, void *owner)
 	     if (msg->floating_mailbox && msg->mailbox)
 	     mu_mailbox_destroy (&(msg->mailbox));
 	  */
-
-	  if (msg->ref == 0)
+	  
+	  if (msg->ref <= 0)
 	    free (msg);
 	}
       mu_monitor_unlock (monitor);
diff --git a/mailbox/mime.c b/mailbox/mime.c
index 33186d9..d2f6aab 100644
--- a/mailbox/mime.c
+++ b/mailbox/mime.c
@@ -930,7 +930,8 @@ mu_mime_get_num_parts (mu_mime_t mime, size_t *nmtp_parts)
 {
   int             ret = 0;
 
-  if (mime->nmtp_parts == 0 || mime->flags & MIME_PARSER_ACTIVE)
+  if ((mime->nmtp_parts == 0 && !mime->boundary)
+      || mime->flags & MIME_PARSER_ACTIVE)
     {
       if (mu_mime_is_multipart (mime))
 	{
diff --git a/mailbox/rfc2047.c b/mailbox/rfc2047.c
index bc6972b..9e043d6 100644
--- a/mailbox/rfc2047.c
+++ b/mailbox/rfc2047.c
@@ -224,8 +224,11 @@ mu_rfc2047_decode (const char *tocode, const char *input, char **ptostr)
   free (fromcode);
   free (encoding_type);
   free (encoded_text);
-  
-  *ptostr = realloc (buffer, bufpos);
+
+  if (status)
+    free (buffer);
+  else
+    *ptostr = realloc (buffer, bufpos);
   return status;
 }
 


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Thu Aug 12 10:12:37 2010
Received: from mailman by lists.gnu.org with archive (Exim 4.43)
	id 1OjYWL-0005JW-Aw
	for mharc-commit-mailutils@gnu.org; Thu, 12 Aug 2010 10:12:37 -0400
Received: from [140.186.70.92] (port=59890 helo=eggs.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.43) id 1OjYWE-00035E-1A
	for commit-mailutils@gnu.org; Thu, 12 Aug 2010 10:12:33 -0400
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1OjYVC-0001B9-Eq
	for commit-mailutils@gnu.org; Thu, 12 Aug 2010 10:11:27 -0400
Received: from colonialone.fsf.org ([140.186.70.51]:39374
	helo=internal.in.savannah.gnu.org)
	by eggs.gnu.org with esmtp (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1OjYVC-0001B4-C8
	for commit-mailutils@gnu.org; Thu, 12 Aug 2010 10:11:26 -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 1OjYVB-0001LV-Qu
	for commit-mailutils@gnu.org; Thu, 12 Aug 2010 14:11:25 +0000
Received: from gray by vcs-noshell.in.savannah.gnu.org with local (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1OjYVB-00056f-Lx
	for commit-mailutils@gnu.org; Thu, 12 Aug 2010 14:11:25 +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: bf0d23962442075cf16eab955435de99bea961de
X-Git-Newrev: d66dc3789bac5ab2a5f23c996ca73c6d4d1626f7
Message-Id: <E1OjYVB-00056f-Lx@vcs-noshell.in.savannah.gnu.org>
Date: Thu, 12 Aug 2010 14:11:25 +0000
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
Subject: [SCM] GNU Mailutils branch, master, updated. rel-2_1-91-gd66dc37
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, 12 Aug 2010 14:12:34 -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=d66dc3789bac5ab2a5f23c996ca73c6d4d1626f7

The branch, master has been updated
       via  d66dc3789bac5ab2a5f23c996ca73c6d4d1626f7 (commit)
      from  bf0d23962442075cf16eab955435de99bea961de (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 d66dc3789bac5ab2a5f23c996ca73c6d4d1626f7
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Thu Aug 12 17:09:35 2010 +0300

    Minor change.
    
    * examples/mimetest.c (message_display_parts): Remove
    not needed locals.

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

Summary of changes:
 examples/mimetest.c |    3 +--
 1 files changed, 1 insertions(+), 2 deletions(-)

diff --git a/examples/mimetest.c b/examples/mimetest.c
index 923e4d0..9b1170c 100644
--- a/examples/mimetest.c
+++ b/examples/mimetest.c
@@ -177,7 +177,7 @@ void
 message_display_parts (mu_message_t msg, int indent)
 {
   int ret, j;
-  size_t nparts, nsubparts;
+  size_t nparts;
   mu_message_t part;
   mu_header_t hdr;
   mu_stream_t str;
@@ -239,7 +239,6 @@ message_display_parts (mu_message_t msg, int indent)
           printf ("%*.*sEncapsulated message : %s\t%s\n",
                   indent, indent, "", from, subject);
           printf ("%*.*sBegin\n", indent, indent, "");
-          MU_ASSERT (mu_message_get_num_parts (part, &nsubparts));
           message_display_parts (part, indent + indent_level);
           mu_message_destroy (&part, NULL);
         }


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Thu Aug 26 05:50:23 2010
Received: from mailman by lists.gnu.org with archive (Exim 4.43)
	id 1OoZ6E-0000qr-Ai
	for mharc-commit-mailutils@gnu.org; Thu, 26 Aug 2010 05:50:22 -0400
Received: from [140.186.70.92] (port=33440 helo=eggs.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.43) id 1OoZ65-0000ez-Ug
	for commit-mailutils@gnu.org; Thu, 26 Aug 2010 05:50:15 -0400
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1OoYyx-000859-8C
	for commit-mailutils@gnu.org; Thu, 26 Aug 2010 05:42:52 -0400
Received: from colonialone.fsf.org ([140.186.70.51]:46604
	helo=internal.in.savannah.gnu.org)
	by eggs.gnu.org with esmtp (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1OoYyx-000853-69
	for commit-mailutils@gnu.org; Thu, 26 Aug 2010 05:42:51 -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 1OoYyw-0005lB-LY
	for commit-mailutils@gnu.org; Thu, 26 Aug 2010 09:42:50 +0000
Received: from gray by vcs-noshell.in.savannah.gnu.org with local (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1OoYyw-0008DT-Is
	for commit-mailutils@gnu.org; Thu, 26 Aug 2010 09:42: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: d66dc3789bac5ab2a5f23c996ca73c6d4d1626f7
X-Git-Newrev: 1cb4b8d7631f3a7a675510ddf3332bc28ddc5d2d
Message-Id: <E1OoYyw-0008DT-Is@vcs-noshell.in.savannah.gnu.org>
Date: Thu, 26 Aug 2010 09:42:50 +0000
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
Subject: [SCM] GNU Mailutils branch, master, updated. rel-2_1-92-g1cb4b8d
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, 26 Aug 2010 09:50: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=1cb4b8d7631f3a7a675510ddf3332bc28ddc5d2d

The branch, master has been updated
       via  1cb4b8d7631f3a7a675510ddf3332bc28ddc5d2d (commit)
      from  d66dc3789bac5ab2a5f23c996ca73c6d4d1626f7 (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 1cb4b8d7631f3a7a675510ddf3332bc28ddc5d2d
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Thu Aug 26 12:42:04 2010 +0300

    Bugfix.
    
    * mailbox/acl.c (expand_arg): Fix a memory leak.

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

Summary of changes:
 mailbox/acl.c |    5 ++++-
 1 files changed, 4 insertions(+), 1 deletions(-)

diff --git a/mailbox/acl.c b/mailbox/acl.c
index 7fb2acd..a3e3764 100644
--- a/mailbox/acl.c
+++ b/mailbox/acl.c
@@ -541,7 +541,10 @@ expand_arg (const char *cmdline, struct run_closure *rp, char **s)
 	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)
-	  mu_vartab_define (vtab, "port", p, 0);
+	  {
+	    mu_vartab_define (vtab, "port", p, 0);
+	    free (p);
+	  }
       }
       break;
       


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Sat Aug 28 19:36:34 2010
Received: from mailman by lists.gnu.org with archive (Exim 4.43)
	id 1OpUws-0005qq-Jt
	for mharc-commit-mailutils@gnu.org; Sat, 28 Aug 2010 19:36:34 -0400
Received: from [140.186.70.92] (port=33151 helo=eggs.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.43) id 1OpUwm-0005qU-Ep
	for commit-mailutils@gnu.org; Sat, 28 Aug 2010 19:36:32 -0400
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1OpUwi-0007r1-4L
	for commit-mailutils@gnu.org; Sat, 28 Aug 2010 19:36:28 -0400
Received: from colonialone.fsf.org ([140.186.70.51]:36348
	helo=internal.in.savannah.gnu.org)
	by eggs.gnu.org with esmtp (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1OpUwh-0007qv-V4
	for commit-mailutils@gnu.org; Sat, 28 Aug 2010 19:36: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 1OpUwh-0000SH-Rb
	for commit-mailutils@gnu.org; Sat, 28 Aug 2010 23:36:23 +0000
Received: from gray by vcs-noshell.in.savannah.gnu.org with local (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1OpUwg-0005Wo-Vd
	for commit-mailutils@gnu.org; Sat, 28 Aug 2010 23:36:23 +0000
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
X-Git-Refname: refs/heads/stream-cleanup
X-Git-Reftype: branch
X-Git-Oldrev: 19f93a88c9a2cd1bbe66472514db16c13a333984
X-Git-Newrev: afbdbd89e1edd58916bf504ffc41f0884a6992c9
Message-Id: <E1OpUwg-0005Wo-Vd@vcs-noshell.in.savannah.gnu.org>
Date: Sat, 28 Aug 2010 23:36:23 +0000
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
Subject: [SCM] GNU Mailutils branch, stream-cleanup,
	updated. rel-2_1-106-gafbdbd8
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, 28 Aug 2010 23:36: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=afbdbd89e1edd58916bf504ffc41f0884a6992c9

The branch, stream-cleanup has been updated
       via  afbdbd89e1edd58916bf504ffc41f0884a6992c9 (commit)
       via  a9608e62e3a82b2d97f4c2594370dfeb217865c9 (commit)
       via  f8f17dc3083536dee2ea16e1d3ce1ee8cf0d2045 (commit)
       via  eea4191dbfe090237da49c01fb3b9924a1216621 (commit)
       via  54602578633b24e84b0caf5be588cdeda8a3bd5c (commit)
       via  446ba82c52a3b232b1dceb25633852cdf24d4b1a (commit)
       via  2bb67bb6814eb54b6e8466aeef5ce7cac80b853b (commit)
       via  dc579c0356464e7e9ce9d25ab60475703bde52d4 (commit)
      from  19f93a88c9a2cd1bbe66472514db16c13a333984 (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 afbdbd89e1edd58916bf504ffc41f0884a6992c9
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Sun Aug 29 02:36:03 2010 +0300

    Fix mime creation and Sieve reject/redirect actions.
    
    * examples/mta.c (finalize_option): New variable.
    (message_finalize): Don't modify the message if finalize_option
    is 0.
    * include/mailutils/sys/message.h (_mu_message)
    <orig_header_size>: New member.
    * include/mailutils/sys/mime.h (_mu_mime) <part_stream>: New member.
    * libmu_sieve/actions.c (mime_create_reason)
    (mime_create_ds): Use mu_body_get_streamref.
    (mime_create_quote): Use mu_body_get_streamref/mu_message_get_streamref.
    (sieve_action_reject): Set the To: header.
    * mailbox/message.c (mu_message_get_header): Set orig_header_size.
    (mu_message_get_body): Use orig_header_size instead of relying
    on mu_header_size.
    * mailbox/mime.c (_mime_part_size): New static.
    (_mime_body_stream_size): New method.
    (_mime_body_seek): Rename to _mime_body_stream_seek.
    (_mime_body_read): Rewrite and rename to _mime_body_stream_read.
    (_mime_body_ioctl): Rename to _mime_body_stream_ioctl.
    (create_mime_body_stream): Take mu_mime_t as the 2nd parameter.
    Initialize sp->stream.size and sp->mime.
    (_mime_body_size): Rewrite using _mime_part_size.
    (mu_mime_destroy): Destroy part_stream.
    * sieve/testsuite/Redirect: Reflect new mta behavior.
    * sieve/testsuite/Reject: Likewise.

commit a9608e62e3a82b2d97f4c2594370dfeb217865c9
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Sat Aug 28 23:10:40 2010 +0300

    Further bugfixes.
    
    * libproto/mbox/mbox.c (_msg_stream_setup): Fix abridgement limits.
    (mbox_expunge_unlocked): Don't write extra newline after each
    message.
    * mailbox/stream.c (mu_stream_seek): ESPIPE does not constitute
    a permanent error.

commit f8f17dc3083536dee2ea16e1d3ce1ee8cf0d2045
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Sat Aug 28 22:42:04 2010 +0300

    Fix mime test.
    
    * mailbox/mime.c (mu_mime_get_part): Fix the end limit for the
    part's abridgement.
    * mailbox/streamref.c (_streamref_seek): Fix ESPIPE condition.

commit eea4191dbfe090237da49c01fb3b9924a1216621
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Sat Aug 28 18:39:43 2010 +0300

    pop3d: bugfixes.
    
    * include/mailutils/message.h (mu_message_set_stream): Mark as deprecated.
    * pop3d/retr.c (pop3d_retr): Rewind the stream obtained from
    mu_message_get_stream.
    * pop3d/top.c (pop3d_top): Rewind the streams obtained from
    mu_body_get_stream and mu_header_get_stream.

commit 54602578633b24e84b0caf5be588cdeda8a3bd5c
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Sat Aug 28 16:58:08 2010 +0300

    Fix encode2047 test.
    
    * mailbox/base64.c (_base64_encoder): Fix the use
    of uninitialized variable.
    * mailbox/rfc2047.c (mu_rfc2047_encode): Rewind the
    input stream before reading from it.

commit 446ba82c52a3b232b1dceb25633852cdf24d4b1a
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Sat Aug 28 16:36:14 2010 +0300

    Fix decode2047 test.
    
    * mailbox/filter_iconv.c (mu_filter_iconv_create): Set filter flags.
    * mailbox/fltstream.c (mu_filter_stream_create): Likewise.
    * mailbox/qpflt.c (_qp_decoder): Handle '_'.

commit 2bb67bb6814eb54b6e8466aeef5ce7cac80b853b
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Sat Aug 28 16:08:07 2010 +0300

    Fix base64 test.
    
    * examples/base64.c (c_copy): Handle printable mode (-p).
    (reset_line_length): New function.
    (main): New option -lN sets the maximum length for output
    lines to N (0 means unlimited).
    * mailbox/testsuite/mailbox/base64.exp: Use -l0 in the
    Decode test.

commit dc579c0356464e7e9ce9d25ab60475703bde52d4
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Sat Aug 28 17:22:40 2010 +0300

    Bugfixes
    
    * include/mailutils/header.h (mu_header_invalidate): New proto.
    * mailbox/header.c (mu_header_invalidate): New function.
    
    * libmu_argp/muinit.c (get_canonical_name): Avoid coredumping
    if argp_program_version is NULL.
    * mailbox/base64.c (mu_base64_decode): Fix inconsistent return
    code.
    * mailbox/debug.c (mu_debug_destroy): Allow for debug->stream == NULL.
    
    * mailbox/mapfile_stream.c (_mapfile_truncate): Incorrect size
    was used when unmapping.
    * mailbox/message.c (mu_message_create_copy): Use a temporary
    memory stream.
    (mu_message_get_body): Comment out the check for MESSAGE_INTERNAL_STREAM.
    (_message_get_stream): Initialize message header and body.
    
    * mailbox/progmailer.c (mu_progmailer_send): Check  return
    from the mu_header_get_streamref.
    
    * mailbox/stream.c (_stream_scandelim, _stream_readdelim): If
    size is 0, return MU_ERR_BUFSPACE.

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

Summary of changes:
 examples/base64.c                    |   54 ++++++-
 examples/mta.c                       |   19 ++-
 include/mailutils/header.h           |    1 +
 include/mailutils/message.h          |    4 +-
 include/mailutils/sys/message.h      |    3 +-
 include/mailutils/sys/mime.h         |    1 +
 lib/allocsa.valgrind                 |    7 -
 libmu_argp/muinit.c                  |    6 +-
 libmu_sieve/actions.c                |   33 +++-
 libproto/mbox/mbox.c                 |   12 +--
 mailbox/base64.c                     |    4 +-
 mailbox/debug.c                      |   15 +-
 mailbox/filter_iconv.c               |    1 +
 mailbox/fltstream.c                  |    3 +-
 mailbox/header.c                     |    9 +
 mailbox/mapfile_stream.c             |    2 +-
 mailbox/message.c                    |   65 +++++---
 mailbox/mime.c                       |  190 +++++++++++++-------
 mailbox/progmailer.c                 |   17 ++-
 mailbox/qpflt.c                      |    6 +
 mailbox/rfc2047.c                    |    2 +-
 mailbox/stream.c                     |    6 +
 mailbox/streamref.c                  |    2 +-
 mailbox/testsuite/mailbox/base64.exp |    4 +-
 pop3d/retr.c                         |    5 +-
 pop3d/top.c                          |    8 +-
 sieve/testsuite/Redirect             |   94 +++++-----
 sieve/testsuite/Reject               |  328 +++++++++++++++++-----------------
 28 files changed, 540 insertions(+), 361 deletions(-)
 delete mode 100644 lib/allocsa.valgrind

diff --git a/examples/base64.c b/examples/base64.c
index 3785a6a..cc0a7e8 100644
--- a/examples/base64.c
+++ b/examples/base64.c
@@ -35,7 +35,28 @@ int printable = 0;
 static void
 c_copy (mu_stream_t out, mu_stream_t in)
 {
-  MU_ASSERT (mu_stream_copy (out, in, 0));
+  if (printable)
+    {
+      char c;
+      size_t size;
+      
+      while (mu_stream_read (in, &c, 1, &size) == 0 && size > 0)
+	{
+	  int rc;
+	  
+	  if (printable && !ISPRINT (c))
+	    {
+	      char outbuf[24];
+	      sprintf (outbuf, "\\%03o", (unsigned char) c);
+	      rc = mu_stream_write (out, outbuf, strlen (outbuf), NULL);
+	    }
+	  else
+	    rc = mu_stream_write (out, &c, 1, NULL);
+	}
+    }
+  else
+    MU_ASSERT (mu_stream_copy (out, in, 0));
+  mu_stream_write (out, "\n", 1, NULL);
   mu_stream_close (out);
   mu_stream_close (in);
   if (verbose)
@@ -44,6 +65,23 @@ c_copy (mu_stream_t out, mu_stream_t in)
 	     (unsigned long) mu_stream_bytes_out (out));
 }
 
+/* Set the maximum line length for the filter NAME to LENGTH.
+   FIXME: This is a kludge. Perhaps 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
 main (int argc, char * argv [])
 {
@@ -54,8 +92,10 @@ main (int argc, char * argv [])
   char *input = NULL, *output = NULL;
   char *encoding = "base64";
   mu_off_t shift = 0;
+  size_t line_length;
+  int line_length_option = 0;
   
-  while ((c = getopt (argc, argv, "deE:hi:o:ps:vw")) != EOF)
+  while ((c = getopt (argc, argv, "deE:hi:l:o:ps:vw")) != EOF)
     switch (c)
       {
       case 'i':
@@ -78,8 +118,13 @@ main (int argc, char * argv [])
 	mode = MU_FILTER_ENCODE;
 	break;
 
+      case 'l':
+	line_length = strtoul (optarg, NULL, 10);
+	line_length_option = 1;
+	break;
+	
       case 'p':
- 	printable = 1; /* FIXME: Not implemented */
+ 	printable = 1;
 	break;
 
       case 's':
@@ -115,6 +160,9 @@ main (int argc, char * argv [])
     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);
+  
   if (flags == MU_STREAM_READ)
     {
       MU_ASSERT (mu_filter_create (&flt, in, encoding, mode,
diff --git a/examples/mta.c b/examples/mta.c
index 1604955..4d27774 100644
--- a/examples/mta.c
+++ b/examples/mta.c
@@ -73,6 +73,18 @@ int dot = 1;             /* Message is terminated by a lone dot on a line */
 
 mu_address_t recipients = NULL;
 char *progname;
+/* FIXME: If finalize_option is set, mta should try to finalize
+   received messages the way sendmail does, i.e. to add To: or
+   Cc: headers, if they are missing, etc. The code to do so is
+   already included, but the modified message is not reproduced
+   on diagnostic output because mta_send reads it from the stream,
+   which does not reflect modifications to the header.
+
+   Ideally, the mu_message_get_streamref function should notice the
+   change in the header (and/or the body) and return a temporary
+   stream, which will read the modified values.  This is left as
+   as TODO for a later time.                  2010-09-29, Sergey */
+int finalize_option = 0; 
 
 int mta_stdin (int argc, char **argv);
 int mta_smtp (int argc, char **argv);
@@ -361,7 +373,7 @@ message_finalize (mu_message_t msg, int warn)
   
   mu_message_get_header (msg, &header);
 
-  if (warn && from_person)
+  if (finalize_option && warn && from_person)
     {
       struct passwd *pwd = getpwuid (getuid ());
       char *warn = malloc (strlen (pwd->pw_name) + 1 +
@@ -400,7 +412,8 @@ message_finalize (mu_message_t msg, int warn)
 	  free (value);
 	}  
 	  
-      if (mu_header_aget_value (header, MU_HEADER_BCC, &value) == 0)
+      if (finalize_option &&
+	  mu_header_aget_value (header, MU_HEADER_BCC, &value) == 0)
 	{
 	  if (add_recipient (value))
 	    {
@@ -412,7 +425,7 @@ message_finalize (mu_message_t msg, int warn)
 	}  
     }
 
-  if (!have_to)
+  if (finalize_option && !have_to)
     {
       size_t n;
       int c;
diff --git a/include/mailutils/header.h b/include/mailutils/header.h
index d162112..57a1107 100644
--- a/include/mailutils/header.h
+++ b/include/mailutils/header.h
@@ -78,6 +78,7 @@ extern "C" {
 
 extern int mu_header_create (mu_header_t *, const char *, size_t);
 extern void mu_header_destroy (mu_header_t *);
+extern int mu_header_invalidate (mu_header_t);
 
 extern int mu_header_is_modified (mu_header_t);
 extern int mu_header_clear_modified (mu_header_t);
diff --git a/include/mailutils/message.h b/include/mailutils/message.h
index fcc4e5d..8a2416f 100644
--- a/include/mailutils/message.h
+++ b/include/mailutils/message.h
@@ -55,7 +55,9 @@ extern int mu_message_set_body (mu_message_t, mu_body_t, void *);
 extern int mu_message_get_stream (mu_message_t, mu_stream_t *)
                                    __attribute__((deprecated));
 extern int mu_message_get_streamref (mu_message_t, mu_stream_t *);
-extern int mu_message_set_stream (mu_message_t, mu_stream_t, void *);
+
+extern int mu_message_set_stream (mu_message_t, mu_stream_t, void *)
+  __attribute__((deprecated));
 
 extern int mu_message_get_attribute (mu_message_t, mu_attribute_t *);
 extern int mu_message_set_attribute (mu_message_t, mu_attribute_t, void *);
diff --git a/include/mailutils/sys/message.h b/include/mailutils/sys/message.h
index 8e30382..ec91372 100644
--- a/include/mailutils/sys/message.h
+++ b/include/mailutils/sys/message.h
@@ -47,7 +47,8 @@ struct _mu_message
   mu_mime_t mime;
   mu_observable_t observable;
   mu_mailbox_t mailbox;
-
+  size_t orig_header_size;
+  
   /* Reference count.  */
   int ref;
 
diff --git a/include/mailutils/sys/mime.h b/include/mailutils/sys/mime.h
index 0feae4f..bc0d652 100644
--- a/include/mailutils/sys/mime.h
+++ b/include/mailutils/sys/mime.h
@@ -64,6 +64,7 @@ struct _mu_mime
   int             boundary_len;
   int             preamble;
   int             postamble;
+  mu_stream_t     part_stream;
   /* parser state */
   char           *cur_line;
   int             line_ndx;
diff --git a/lib/allocsa.valgrind b/lib/allocsa.valgrind
deleted file mode 100644
index f4c77d6..0000000
--- a/lib/allocsa.valgrind
+++ /dev/null
@@ -1,7 +0,0 @@
-# Suppress a valgrind message about use of uninitialized memory in freesa().
-# This use is OK because it provides only a speedup.
-{
-    freesa
-    Memcheck:Cond
-    fun:freesa
-}
diff --git a/libmu_argp/muinit.c b/libmu_argp/muinit.c
index 805fc30..9433fdd 100644
--- a/libmu_argp/muinit.c
+++ b/libmu_argp/muinit.c
@@ -75,8 +75,10 @@ get_canonical_name ()
 {
   char *name;
   size_t len;
-  char *p = strchr (argp_program_version, ' ');
-  if (!p)
+  char *p;
+
+  if (!argp_program_version ||
+      !(p = strchr (argp_program_version, ' ')))
     return strdup (mu_program_name);
   len = p - argp_program_version;
   name = malloc (len + 1);
diff --git a/libmu_sieve/actions.c b/libmu_sieve/actions.c
index 595b6e1..1beb8f9 100644
--- a/libmu_sieve/actions.c
+++ b/libmu_sieve/actions.c
@@ -154,7 +154,7 @@ mime_create_reason (mu_mime_t mime, mu_message_t msg, const char *text)
  
   mu_message_create (&newmsg, NULL);
   mu_message_get_body (newmsg, &body);
-  mu_body_get_stream (body, &stream);
+  mu_body_get_streamref (body, &stream);
 
   time (&t);
   tm = localtime (&t);
@@ -171,6 +171,8 @@ mime_create_reason (mu_mime_t mime, mu_message_t msg, const char *text)
   mu_stream_printf (stream, "Reason given was as follows:\n\n");
   mu_stream_printf (stream, "%s", text);
   mu_stream_close (stream);
+  mu_stream_destroy (&stream);
+  
   mu_header_create (&hdr, content_header, strlen (content_header));
   mu_message_set_header (newmsg, hdr, NULL);
   mu_mime_add_part (mime, newmsg);
@@ -196,7 +198,7 @@ mime_create_ds (mu_mime_t mime, mu_message_t orig)
   mu_message_get_header (newmsg, &hdr); 
   mu_header_set_value (hdr, "Content-Type", "message/delivery-status", 1);
   mu_message_get_body (newmsg, &body);
-  mu_body_get_stream (body, &stream);
+  mu_body_get_streamref (body, &stream);
   mu_stream_printf (stream, "Reporting-UA: sieve; %s\n", PACKAGE_STRING);
 
   mu_message_get_envelope (orig, &env);
@@ -226,7 +228,9 @@ mime_create_ds (mu_mime_t mime, mu_message_t orig)
   mu_stream_printf (stream, "Last-Attempt-Date: %s\n", datestr);
 
   mu_stream_close (stream);
-  mu_mime_add_part(mime, newmsg);
+  mu_stream_destroy (&stream);
+  
+  mu_mime_add_part (mime, newmsg);
   mu_message_unref (newmsg);
 }
 
@@ -241,25 +245,30 @@ mime_create_quote (mu_mime_t mime, mu_message_t msg)
   size_t n;
   char buffer[512];
   mu_body_t body;
-    
+  int rc;
+  
   mu_message_create (&newmsg, NULL);
   mu_message_get_header (newmsg, &hdr); 
   mu_header_set_value (hdr, "Content-Type", "message/rfc822", 1);
   mu_message_get_body (newmsg, &body);
-  mu_body_get_stream (body, &ostream);
-  mu_message_get_stream (msg, &istream);
-  
+  mu_body_get_streamref (body, &ostream);
+  mu_message_get_streamref (msg, &istream);
+
+  rc = 0;/* FIXME: use mu_stream_copy */
   while (mu_stream_read (istream, buffer, sizeof buffer - 1, &n) == 0
 	 && n != 0)
     {
-      int rc = mu_stream_write (ostream, buffer, n, NULL);
+      rc = mu_stream_write (ostream, buffer, n, NULL);
       if (rc)
-	return EIO;
+	break;
     }
+  mu_stream_destroy (&istream);
   mu_stream_close (ostream);
+  mu_stream_destroy (&ostream);
+  
   mu_mime_add_part (mime, newmsg);
   mu_message_unref (newmsg);
-  return 0;
+  return rc;
 }
   
 static int
@@ -292,6 +301,7 @@ sieve_action_reject (mu_sieve_machine_t mach, mu_list_t args, mu_list_t tags)
   mu_message_t newmsg;
   char *addrtext;
   mu_address_t from, to;
+  mu_header_t hdr;
   
   mu_sieve_value_t *val = mu_sieve_value_get (args, 0);
   if (!val)
@@ -308,6 +318,9 @@ sieve_action_reject (mu_sieve_machine_t mach, mu_list_t args, mu_list_t tags)
   mu_mime_get_message (mime, &newmsg);
 
   mu_sieve_get_message_sender (mach->msg, &addrtext);
+  mu_message_get_header (newmsg, &hdr);
+  mu_header_prepend (hdr, MU_HEADER_TO, addrtext);
+
   rc = mu_address_create (&to, addrtext);
   if (rc)
     {
diff --git a/libproto/mbox/mbox.c b/libproto/mbox/mbox.c
index 98d288d..c81701e 100644
--- a/libproto/mbox/mbox.c
+++ b/libproto/mbox/mbox.c
@@ -298,7 +298,7 @@ _msg_stream_setup (mu_message_t msg, mbox_message_t mum)
   status = mu_streamref_create_abridged (&stream,
 					 mum->mud->mailbox->stream,
 					 mum->envel_from_end,
-					 mum->body_end);
+					 mum->body_end - 1);
   if (status == 0)
     status = mu_message_set_stream (msg, stream, mum);
   return status;
@@ -1272,16 +1272,6 @@ mbox_expunge_unlocked (mu_mailbox_t mailbox, size_t dirty, int remove_deleted,
 	      return status;
 	    }
 	}
-
-      status = mu_stream_write (tempstr, "\n", 1, NULL);
-      if (status)
-	{
-	  mu_error (_("%s:%d: error writing to temporary stream: %s"),
-		    __FILE__, __LINE__,
-		    mu_strerror (status));
-	  return status;
-	}
-
     }
 
   /* Caution: before moving data back to the mailbox see
diff --git a/mailbox/base64.c b/mailbox/base64.c
index fd0131e..8ceba26 100644
--- a/mailbox/base64.c
+++ b/mailbox/base64.c
@@ -88,7 +88,7 @@ mu_base64_decode (const unsigned char *input, size_t input_len,
 	  || input[2] > 127 || ((input[2] != '=') && (b64val[input[2]] == -1))
 	  || input[3] > 127 || ((input[3] != '=')
 				&& (b64val[input[3]] == -1)))
-	return -1;
+	return EINVAL;
       *out++ = (b64val[input[0]] << 2) | (b64val[input[1]] >> 4);
       if (input[2] != '=')
 	{
@@ -197,7 +197,7 @@ _base64_encoder (void *xd MU_ARG_UNUSED,
       break;
     }
   
-  if (isize <= 3)
+  if (iobuf->isize <= 3)
     {
       if (cmd == mu_filter_lastbuf)
 	pad = 1;
diff --git a/mailbox/debug.c b/mailbox/debug.c
index 4c31674..93fab7c 100644
--- a/mailbox/debug.c
+++ b/mailbox/debug.c
@@ -55,13 +55,16 @@ mu_debug_destroy (mu_debug_t *pdebug, void *owner)
       mu_debug_t debug = *pdebug;
       if (debug->owner == owner)
 	{
-	  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");
+	  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);
+	      mu_stream_destroy (&debug->stream);
+	    }
 	  if (debug->destroy)
 	    debug->destroy (debug->data);
 	  free (*pdebug);
diff --git a/mailbox/filter_iconv.c b/mailbox/filter_iconv.c
index 8d34285..6674ba4 100644
--- a/mailbox/filter_iconv.c
+++ b/mailbox/filter_iconv.c
@@ -463,6 +463,7 @@ mu_filter_iconv_create (mu_stream_t *s, mu_stream_t transport,
   iptr->stream.ctl = _icvt_ioctl;
   iptr->stream.wait = _icvt_wait;
   iptr->stream.seek = _icvt_seek;
+  iptr->stream.flags = MU_STREAM_READ | MU_STREAM_SEEK;
   *s = (mu_stream_t)iptr;
   return 0;
 }
diff --git a/mailbox/fltstream.c b/mailbox/fltstream.c
index 90575a4..8f6260a 100644
--- a/mailbox/fltstream.c
+++ b/mailbox/fltstream.c
@@ -425,7 +425,8 @@ mu_filter_stream_create (mu_stream_t *pflt,
     fs->stream.seek = filter_seek;
   fs->stream.ctl = filter_ctl;
   fs->stream.error_string = filter_error_string;
-
+  fs->stream.flags = flags;
+  
   fs->transport = str;
   fs->xcode = xcode;
   fs->xdata = xdata;
diff --git a/mailbox/header.c b/mailbox/header.c
index 669093c..f49ea86 100644
--- a/mailbox/header.c
+++ b/mailbox/header.c
@@ -911,6 +911,15 @@ mu_header_size (mu_header_t header, size_t *psize)
   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)
diff --git a/mailbox/mapfile_stream.c b/mailbox/mapfile_stream.c
index 0b60147..89b3e47 100644
--- a/mailbox/mapfile_stream.c
+++ b/mailbox/mapfile_stream.c
@@ -129,7 +129,7 @@ _mapfile_truncate (mu_stream_t stream, mu_off_t len)
   if (mfs->ptr == MAP_FAILED)
     return EINVAL;
   /* Remap.  */
-  if (mfs->ptr && munmap (mfs->ptr, len) != 0)
+  if (mfs->ptr && munmap (mfs->ptr, mfs->size) != 0)
     {
       int err = errno;
       mfs->ptr = MAP_FAILED;
diff --git a/mailbox/message.c b/mailbox/message.c
index a792ebb..1d95c71 100644
--- a/mailbox/message.c
+++ b/mailbox/message.c
@@ -558,33 +558,36 @@ mu_message_create_copy (mu_message_t *to, mu_message_t from)
 {
   int status = 0;
   mu_stream_t fromstr = NULL;
-  mu_stream_t tostr = NULL;
-  size_t n = 0;
-  char buf[512];
+  mu_stream_t tmp = NULL;
 
   if (!to)
     return MU_ERR_OUT_PTR_NULL;
   if (!from)
     return EINVAL;
 
-  if ((status = mu_message_create (to, NULL)))
+  status = mu_memory_stream_create (&tmp, MU_STREAM_RDWR|MU_STREAM_SEEK);
+  if (status)
     return status;
 
-  mu_message_get_streamref (from, &fromstr);
-  mu_message_get_streamref (*to, &tostr);
+  status = mu_message_get_streamref (from, &fromstr);
+  if (status)
+    {
+      mu_stream_destroy (&tmp);
+      return status;
+    }
 
-  status = mu_stream_seek (fromstr, 0, MU_SEEK_SET, NULL);
+  status = mu_stream_copy (tmp, fromstr, 0);
   if (status == 0)
-    while ((status = mu_stream_readline (fromstr, buf, sizeof (buf), &n)) == 0
-	   && n > 0)
-      mu_stream_write (tostr, buf, n, NULL);
+    {
+      status = mu_message_create (to, NULL);
+      if (status == 0)
+	mu_message_set_stream (*to, tmp, NULL);
+    }
 
-  mu_stream_destroy (&fromstr);
-  mu_stream_destroy (&tostr);
-  
   if (status)
-    mu_message_destroy (to, NULL);
-  
+    mu_stream_destroy (&tmp);
+  mu_stream_destroy (&fromstr);
+
   return status;
 }
 
@@ -674,6 +677,9 @@ mu_message_get_header (mu_message_t msg, mu_header_t *phdr)
 	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;
@@ -710,21 +716,19 @@ mu_message_get_body (mu_message_t msg, mu_body_t *pbody)
       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.  */
+      /* 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))
+      if (msg->stream/* && (msg->flags & MESSAGE_INTERNAL_STREAM)*/)
 	{
-	  size_t size = 0;
 	  mu_stream_t stream;
 	  int flags = 0;
-	  
-	  status = mu_header_size (msg->header, &size);
-	  if (status)
-	    return status;
+
+	  /* 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,
-						 size, 0);
+						 msg->orig_header_size, 0);
 	  if (status)
 	    {
 	      mu_body_destroy (&body, msg);
@@ -779,7 +783,20 @@ _message_get_stream (mu_message_t msg, mu_stream_t *pstream, int ref)
 
   if (msg->stream == NULL)
     {
-      int status = _message_stream_create (&msg->stream, msg, MU_STREAM_RDWR);
+      int status;
+      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;
diff --git a/mailbox/mime.c b/mailbox/mime.c
index f6f6d75..e1e2145 100644
--- a/mailbox/mime.c
+++ b/mailbox/mime.c
@@ -529,15 +529,52 @@ _mime_set_content_type (mu_mime_t mime)
 }
 
 
+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;
+}
+
 /* FIXME: The seek method is defective */
 static int
-_mime_body_seek (mu_stream_t stream, mu_off_t off, mu_off_t *presult)
+_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;
@@ -558,30 +595,39 @@ _mime_body_seek (mu_stream_t stream, mu_off_t off, mu_off_t *presult)
   return 0;
 }
 
-#define ADD_CHAR(buf, c, offset, buflen, nbytes) do {\
- *(buf)++ = c;\
- (offset)++;\
- (nbytes)++;\
- if (--(buflen) == 0) return 0;\
-} while (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_read (mu_stream_t stream, char *buf, size_t buflen, size_t *nbytes)
+_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             part_nbytes = 0;
-  mu_stream_t        msg_stream = NULL;
-
+  size_t             total = 0;
+  
   if (mime->nmtp_parts == 0)
     return EINVAL;
-
+  
   if ((ret = _mime_set_content_type (mime)) == 0)
     {
       do
 	{
-	  mu_stream_destroy (&msg_stream);
+	  size_t             part_nbytes = 0;
+
+	  if (buflen == 0)
+	    break;
 	  if (mime->nmtp_parts > 1)
 	    {
 	      size_t len;
@@ -599,7 +645,8 @@ _mime_body_read (mu_stream_t stream, char *buf, size_t buflen, size_t *nbytes)
 		  while (mime->preamble)
 		    {
 		      mime->preamble--;
-		      ADD_CHAR (buf, '-', mime->cur_offset, buflen, *nbytes);
+		      ADD_CHAR (buf, '-', mime->cur_offset, buflen,
+				total, nbytes);
 		    }
 		  len = strlen (mime->boundary) - mime->boundary_len;
 		  while (mime->boundary_len)
@@ -607,57 +654,89 @@ _mime_body_read (mu_stream_t stream, char *buf, size_t buflen, size_t *nbytes)
 		      mime->boundary_len--;
 		      ADD_CHAR (buf,
 				mime->boundary[len++],
-				mime->cur_offset, buflen, *nbytes);
+				mime->cur_offset, buflen,
+				total, nbytes);
 		    }
 		  while (mime->postamble)
 		    {
 		      mime->postamble--;
-		      ADD_CHAR (buf, '-', mime->cur_offset, buflen, *nbytes);
+		      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, *nbytes);
+		  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);
 		}
-	      if (mime->cur_part >= mime->nmtp_parts)
-		return 0;
-	      mu_message_get_streamref (mime->mtp_parts[mime->cur_part]->msg,
-					&msg_stream);
 	    }
-	  else
+	  else if (!mime->part_stream)
 	    {
 	      mu_body_t part_body;
 
 	      if (mime->cur_part >= mime->nmtp_parts)
-		return 0;
+		{
+		  *nbytes = total;
+		  return 0;
+		}
 	      mu_message_get_body (mime->mtp_parts[mime->cur_part]->msg,
 				   &part_body);
-	      mu_body_get_streamref (part_body, &msg_stream);
+	      ret = mu_body_get_streamref (part_body, &mime->part_stream);
 	    }
-	  mu_stream_seek (msg_stream, mime->part_offset, MU_SEEK_SET, NULL);
-	  ret = mu_stream_read (msg_stream, buf, buflen, &part_nbytes);
-	  if (part_nbytes)
+	  if (ret)
+	    break;
+	  ret = mu_stream_seek (mime->part_stream, mime->part_offset,
+				MU_SEEK_SET, NULL);
+	  if (ret)
 	    {
-	      mime->part_offset += part_nbytes;
-	      mime->cur_offset += part_nbytes;
-	      if (nbytes)
-		*nbytes += part_nbytes;
+	      mu_stream_destroy (&mime->part_stream);
+	      break;
 	    }
-	  if (ret == 0 && part_nbytes == 0)
+	  while (buflen > 0 &&
+		 (ret = mu_stream_read (mime->part_stream, buf, buflen,
+					&part_nbytes)) == 0)
 	    {
-	      mime->flags |= MIME_INSERT_BOUNDARY;
-	      mime->cur_part++;
-	      ADD_CHAR (buf, '\n', mime->cur_offset, buflen, *nbytes);
+	      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 && part_nbytes == 0
-	     && mime->cur_part <= mime->nmtp_parts);
+      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_ioctl (mu_stream_t stream, int code, void *arg)
+_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;
@@ -687,16 +766,18 @@ _mime_body_ioctl (mu_stream_t stream, int code, void *arg)
 }
 
 static int
-create_mime_body_stream (mu_stream_t *pstr)
+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_read;
-  sp->stream.seek = _mime_body_seek;
-  sp->stream.ctl = _mime_body_ioctl;
+  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;
   *pstr = (mu_stream_t) sp;
   return 0;
 }
@@ -707,25 +788,7 @@ _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);
-  int                i, ret;
-  size_t             size;
-
-  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);
-      *psize += size;
-      if (mime->nmtp_parts > 1)	/* boundary line */
-	*psize += strlen (mime->boundary) + 3;
-    }
-  if (mime->nmtp_parts > 1)	/* ending boundary line */
-    *psize += 2;
-
-  return 0;
+  return _mime_part_size (mime, psize);
 }
 
 static int
@@ -843,6 +906,7 @@ mu_mime_destroy (mu_mime_t *pmime)
 	  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)
@@ -887,7 +951,7 @@ mu_mime_get_part (mu_mime_t mime, size_t part, mu_message_t *msg)
 	      ret = mu_streamref_create_abridged (&stream, mime->stream,
 						  mime_part->offset,
 						  mime_part->offset +
-						     mime_part->len);
+						    mime_part->len - 1);
 	      if (ret == 0)
 		{
 		  mu_stream_set_flags (stream,
@@ -964,7 +1028,7 @@ mu_mime_get_message (mu_mime_t mime, mu_message_t *msg)
 		      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);
+		      ret = create_mime_body_stream (&body_stream, mime);
 		      if (ret == 0)
 			{
 			  mu_body_set_stream (body, body_stream, mime->msg);
diff --git a/mailbox/progmailer.c b/mailbox/progmailer.c
index 75f0f59..f39440f 100644
--- a/mailbox/progmailer.c
+++ b/mailbox/progmailer.c
@@ -188,8 +188,13 @@ mu_progmailer_send (struct _mu_progmailer *pm, mu_message_t msg)
   if (!pm || !msg)
     return EINVAL;
   mu_message_get_header (msg, &hdr);
-  mu_header_get_streamref (hdr, &stream);
-
+  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),
@@ -225,7 +230,13 @@ mu_progmailer_send (struct _mu_progmailer *pm, mu_message_t msg)
   
   MU_DEBUG (pm->debug, MU_DEBUG_TRACE, "Sending body...\n");
   mu_message_get_body (msg, &body);
-  mu_body_get_streamref (body, &stream);
+  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),
diff --git a/mailbox/qpflt.c b/mailbox/qpflt.c
index 4d8db53..aaada1e 100644
--- a/mailbox/qpflt.c
+++ b/mailbox/qpflt.c
@@ -142,6 +142,12 @@ _qp_decoder (void *xd MU_ARG_UNUSED,
 		  consumed += 2;
 		}
 	    }
+	  else if (c == '_')
+	    {
+	      *optr++ = ' ';
+	      nbytes++;
+	      consumed++;
+	    }
 	  else
 	    {
 	      *optr++ = c;
diff --git a/mailbox/rfc2047.c b/mailbox/rfc2047.c
index df19d9f..06d94c2 100644
--- a/mailbox/rfc2047.c
+++ b/mailbox/rfc2047.c
@@ -267,7 +267,7 @@ mu_rfc2047_encode (const char *charset, const char *encoding,
     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);
   if (rc == 0)
diff --git a/mailbox/stream.c b/mailbox/stream.c
index b064421..32db7c6 100644
--- a/mailbox/stream.c
+++ b/mailbox/stream.c
@@ -324,6 +324,8 @@ mu_stream_seek (mu_stream_t stream, mu_off_t offset, int whence,
       if ((rc = _stream_flush_buffer (stream, 1)))
 	return rc;
       rc = stream->seek (stream, offset, &stream->offset);
+      if (rc == ESPIPE)
+	return rc;
       if (rc)
 	return _stream_seterror (stream, rc, 1);
       _stream_cleareof (stream);
@@ -610,6 +612,8 @@ _stream_scandelim (mu_stream_t stream, char *buf, size_t size, int delim,
   size_t nread = 0;
   
   size--;
+  if (size == 0)
+    return MU_ERR_BUFSPACE;
   while (size)
     {
       char *p;
@@ -647,6 +651,8 @@ _stream_readdelim (mu_stream_t stream, char *buf, size_t size,
   size_t n = 0, rdn;
     
   size--;
+  if (size == 0)
+    return MU_ERR_BUFSPACE;
   for (n = 0;
        n < size && (rc = mu_stream_read (stream, &c, 1, &rdn)) == 0 && rdn;)
     {
diff --git a/mailbox/streamref.c b/mailbox/streamref.c
index 4949a85..dad96c6 100644
--- a/mailbox/streamref.c
+++ b/mailbox/streamref.c
@@ -169,7 +169,7 @@ _streamref_seek (struct _mu_stream *str, mu_off_t off, mu_off_t *ppos)
       size -= sp->start;
     }
   
-  if (off < 0 || off >= size)
+  if (off < 0 || off > size)
     return sp->stream.last_err = ESPIPE;
   rc = mu_stream_seek (sp->transport, sp->start + off, MU_SEEK_SET,
 		       &sp->offset);
diff --git a/mailbox/testsuite/mailbox/base64.exp b/mailbox/testsuite/mailbox/base64.exp
index 64e8168..b86ca6f 100644
--- a/mailbox/testsuite/mailbox/base64.exp
+++ b/mailbox/testsuite/mailbox/base64.exp
@@ -29,7 +29,9 @@ mu_exec -message "encode" -arg -i${srcdir}/Encode \
 
 ## Expect chokes on binary data, so let's switch base64 to printable
 ## octal output.
-mu_exec -message "decode" -arg -i${srcdir}/Decode -arg -dp \
+## The -l0 option ensures that the output is not split by inserting
+## additional newlines.
+mu_exec -message "decode" -arg -i${srcdir}/Decode -arg -dpl0 \
 "\\000\\001\\002\\003\\004\\005\\006\\007\\010\\011\\012\\013\\014\\015\\016\\017\\020\\021\\022\\023\\024\\025\\026\\027\\030\\031\\032\\033\\034\\035\\036\\037 !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ\[\\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\\177\\200\\201\\202\\203\\204\\205\\206\\207\\210\\211\\212\\213\\214\\215\\216\\217\\220\\221\\222\\223\\224\\225\\226\\227\\230\\231\\232\\233\\234\\235\\236\\237\\240\\241\\242\\243\\244\\245\\246\\247\\250\\251\\252\\253\\254\\255\\256\\257\\260\\261\\262\\263\\264\\265\\266\\267\\270\\271\\272\\273\\274\\275\\276\\277\\300\\301\\302\\303\\304\\305\\306\\307\\310\\311\\312\\313\\314\\315\\316\\317\\320\\321\\322\\323\\324\\325\\326\\327\\330\\331\\332\\333\\334\\335\\336\\337\\340\\341\\342\\343\\344\\345\\346\\347\\350\\351\\352\\353\\354\\355\\356\\357\\360\\361\\362\\363\\364\\365\\366\\367\\370\\371\\372\\373\\374\\375\\376\\377"
 
 # End of base64.exp
diff --git a/pop3d/retr.c b/pop3d/retr.c
index 8d3030d..c5e44f5 100644
--- a/pop3d/retr.c
+++ b/pop3d/retr.c
@@ -45,7 +45,10 @@ pop3d_retr (char *arg)
     return ERR_MESG_DELE;
 
   /* FIXME: Use crlf filter + mu_stream_copy instead of the loop below */
-  mu_message_get_stream (msg, &stream);
+  if (mu_message_get_stream (msg, &stream)
+      || mu_stream_seek (stream, 0, MU_SEEK_SET, NULL))
+    return ERR_UNKNOWN;
+  
   pop3d_outf ("+OK\r\n");
 
   prev_nl = 1;
diff --git a/pop3d/top.c b/pop3d/top.c
index 0506a64..c51356d 100644
--- a/pop3d/top.c
+++ b/pop3d/top.c
@@ -60,7 +60,9 @@ pop3d_top (char *arg)
   /* Header.  */
   mu_message_get_header (msg, &hdr);
   /* FIXME: Use crlf filter + mu_stream_copy instead of the below loop */
-  mu_header_get_stream (hdr, &stream);
+  if (mu_header_get_stream (hdr, &stream)
+      || mu_stream_seek (stream, 0, MU_SEEK_SET, NULL))
+    return ERR_UNKNOWN;
   n = 0;
   while (mu_stream_readline (stream, buf, sizeof(buf), &n) == 0
 	 && n > 0)
@@ -82,7 +84,9 @@ pop3d_top (char *arg)
 
       mu_message_get_body (msg, &body);
       /* FIXME: Use the crlf filter + mu_stream_copy */
-      mu_body_get_stream (body, &stream);
+      if (mu_body_get_stream (body, &stream)
+	  || mu_stream_seek (stream, 0, MU_SEEK_SET, NULL))
+	return ERR_UNKNOWN;
       n = 0;
       while (mu_stream_readline (stream, buf, sizeof(buf), &n) == 0
 	     && n > 0 && lines > 0)
diff --git a/sieve/testsuite/Redirect b/sieve/testsuite/Redirect
index 806d86f..3063f3a 100644
--- a/sieve/testsuite/Redirect
+++ b/sieve/testsuite/Redirect
@@ -26,62 +26,56 @@ PATTERN END
 FILE BEGIN
 ENVELOPE FROM: coyote@desert.example.org
 ENVELOPE TO: <gray@gnu.org>
--re
-   0: X-Authentication-Warning: [^ \t]+ set sender using -f flag
-   1: X-Loop-Prevention: foobar@nonexistent.net
-   2: From: coyote@desert.example.org
-   3: To: roadrunner@acme.example.com
-   4: Subject: I have a present for you
-   5: X-Caffeine: C8H10N4O2
-   6: 
-   7: Look, I'm sorry about the whole anvil thing, and I really
-   8: didn't mean to try and drop it on you from the top of the
-   9: cliff.  I want to try to make it up to you.  I've got some
-  10: great birdseed over here at my place--top of the line
-  11: stuff--and if you come by, I'll have it all wrapped up
-  12: for you.  I'm really sorry for all the problems I've caused
-  13: for you over the years, but I know we can work this out.
-  14: 
-  15: -- 
-  16: Wile E. Coyote   "Super Genius"   coyote@desert.example.org
-  17: 
+   0: X-Loop-Prevention: foobar@nonexistent.net
+   1: From: coyote@desert.example.org
+   2: To: roadrunner@acme.example.com
+   3: Subject: I have a present for you
+   4: X-Caffeine: C8H10N4O2
+   5: 
+   6: Look, I'm sorry about the whole anvil thing, and I really
+   7: didn't mean to try and drop it on you from the top of the
+   8: cliff.  I want to try to make it up to you.  I've got some
+   9: great birdseed over here at my place--top of the line
+  10: stuff--and if you come by, I'll have it all wrapped up
+  11: for you.  I'm really sorry for all the problems I've caused
+  12: for you over the years, but I know we can work this out.
+  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>
--re
-   0: X-Authentication-Warning: [^ \t]+ set sender using -f flag
-   1: X-Loop-Prevention: foobar@nonexistent.net
-   2: From: youcouldberich!@reply-by-postal-mail.invalid
-   3: To: rube@landru.example.edu
-   4: Subject: $$$ YOU, TOO, CAN BE A MILLIONAIRE! $$$
-   5: Date: TBD
-   6: X-Number: 0015
-   7:
-   8: YOU MAY HAVE ALREADY WON TEN MILLION DOLLARS, BUT I DOUBT
-   9: IT!  SO JUST POST THIS TO SIX HUNDRED NEWSGROUPS!  IT WILL
-  10: GUARANTEE THAT YOU GET AT LEAST FIVE RESPONSES WITH MONEY!
-  11: MONEY! MONEY! COLD HARD CASH!  YOU WILL RECEIVE OVER
-  12: $20,000 IN LESS THAN TWO MONTHS!  AND IT'S LEGAL!!!!!!!!!
-  13: !!!!!!!!!!!!!!!!!!111111111!!!!!!!11111111111!!1  JUST
-  14: SEND $5 IN SMALL, UNMARKED BILLS TO THE ADDRESSES BELOW!
-  15: 
+   0: X-Loop-Prevention: foobar@nonexistent.net
+   1: From: youcouldberich!@reply-by-postal-mail.invalid
+   2: To: rube@landru.example.edu
+   3: Subject: $$$ YOU, TOO, CAN BE A MILLIONAIRE! $$$
+   4: Date: TBD
+   5: X-Number: 0015
+   6:
+   7: YOU MAY HAVE ALREADY WON TEN MILLION DOLLARS, BUT I DOUBT
+   8: IT!  SO JUST POST THIS TO SIX HUNDRED NEWSGROUPS!  IT WILL
+   9: GUARANTEE THAT YOU GET AT LEAST FIVE RESPONSES WITH MONEY!
+  10: MONEY! MONEY! COLD HARD CASH!  YOU WILL RECEIVE OVER
+  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>
--re
-   0: X-Authentication-Warning: [^ \t]+ set sender using -f flag
-   1: X-Loop-Prevention: foobar@nonexistent.net
-   2: Received: (from bar@dontmailme.org)
-   3: 	by dontmailme.org id fERKR9N16790
-   4: 	for foobar@nonexistent.net; Fri, 28 Dec 2001 22:18:08 +0200
-   5: Date: Fri, 28 Dec 2001 23:28:08 +0200
-   6: From: Bar <bar@dontmailme.org>
-   7: To: Foo Bar <foobar@nonexistent.net>
-   8: Message-Id: <200112232808.fERKR9N16790@dontmailme.org>
-   9: Subject: Coffee
-  10: 
-  11: How about some coffee?
-  12: 
+   0: X-Loop-Prevention: foobar@nonexistent.net
+   1: Received: (from bar@dontmailme.org)
+   2: 	by dontmailme.org id fERKR9N16790
+   3: 	for foobar@nonexistent.net; Fri, 28 Dec 2001 22:18:08 +0200
+   4: Date: Fri, 28 Dec 2001 23:28:08 +0200
+   5: From: Bar <bar@dontmailme.org>
+   6: To: Foo Bar <foobar@nonexistent.net>
+   7: Message-Id: <200112232808.fERKR9N16790@dontmailme.org>
+   8: Subject: Coffee
+   9: 
+  10: How about some coffee?
+  11: 
 END OF MESSAGE
 FILE END
 TEST END
diff --git a/sieve/testsuite/Reject b/sieve/testsuite/Reject
index d8ed5e8..0794cd3 100644
--- a/sieve/testsuite/Reject
+++ b/sieve/testsuite/Reject
@@ -26,179 +26,173 @@ PATTERN END
 FILE BEGIN
 ENVELOPE FROM: MAILER-DAEMON@nonexistent.net
 ENVELOPE TO: <coyote@desert.example.org>
-   0: To: <coyote@desert.example.org>
--re
-   1: X-Authentication-Warning: [^ \t]+ set sender using -f flag
--re
-   2: Content-Type: multipart/mixed; boundary="[0-9:=-]+"
-   3: MIME-Version: 1.0
-   4:
--re
-   5: [0-9:=-]+
-   6: Content-Type: text/plain;charset=UTF-8
-   7: Content-Transfer-Encoding: 8bit
-   8:
--re
-   9: The original message was received at [A-Z][a-z][a-z], [A-Z][a-z][a-z] [ 0-3][0-9] [ 0-2][0-9]:[0-6][0-9]:[0-6][0-9] [0-9][0-9][0-9][0-9] [a-zA-Z0-9]* from coyote@desert.example.org.
-  10: Message was refused by recipient's mail filtering program.
-  11: Reason given was as follows:
-  12: 
-  13: I don't want to read these messages.
-  14: 
-  15: Regards.
-  16:
--re
-  17: [0-9:=-]+
-  18: Content-Type: message/delivery-status
-  19:
--re
-  20: Reporting-UA: sieve; GNU Mailutils [0-9][0-9.]*
--re
-  21: Arrival-Date: [A-Z][a-z][a-z], [A-Z][a-z][a-z] [ 0-3][0-9] [ 0-2][0-9]:[0-6][0-9]:[0-6][0-9] [0-9][0-9][0-9][0-9] [a-zA-Z0-9]*
-  22: Final-Recipient: RFC822; foobar@nonexistent.net
-  23: Action: deleted
-  24: Disposition: automatic-action/MDN-sent-automatically;deleted
--re
-  25: Last-Attempt-Date: [A-Z][a-z][a-z], [A-Z][a-z][a-z] [ 0-3][0-9] [ 0-2][0-9]:[0-6][0-9]:[0-6][0-9] [0-9][0-9][0-9][0-9] [a-zA-Z0-9]*
-  26:
--re
-  27: [0-9:=-]+
-  28: Content-Type: message/rfc822
-  29: 
-  30: From: coyote@desert.example.org
-  31: To: roadrunner@acme.example.com
-  32: Subject: I have a present for you
-  33: X-Caffeine: C8H10N4O2
-  34: 
-  35: Look, I'm sorry about the whole anvil thing, and I really
-  36: didn't mean to try and drop it on you from the top of the
-  37: cliff.  I want to try to make it up to you.  I've got some
-  38: great birdseed over here at my place--top of the line
-  39: stuff--and if you come by, I'll have it all wrapped up
-  40: for you.  I'm really sorry for all the problems I've caused
-  41: for you over the years, but I know we can work this out.
-  42: 
-  43: -- 
-  44: Wile E. Coyote   "Super Genius"   coyote@desert.example.org
-  45:
--re
-  46: [0-9:=-]+
-  47: 
+   0: To: coyote@desert.example.org
+-re
+   1: Content-Type: multipart/mixed; boundary="[0-9:=-]+"
+   2: MIME-Version: 1.0
+   3:
+-re
+   4: [0-9:=-]+
+   5: Content-Type: text/plain;charset=UTF-8
+   6: Content-Transfer-Encoding: 8bit
+   7:
+-re
+   8: The original message was received at [A-Z][a-z][a-z], [A-Z][a-z][a-z] [ 0-3][0-9] [ 0-2][0-9]:[0-6][0-9]:[0-6][0-9] [0-9][0-9][0-9][0-9] [a-zA-Z0-9]* from coyote@desert.example.org.
+   9: Message was refused by recipient's mail filtering program.
+  10: Reason given was as follows:
+  11: 
+  12: I don't want to read these messages.
+  13: 
+  14: Regards.
+  15:
+-re
+  16: [0-9:=-]+
+  17: Content-Type: message/delivery-status
+  18:
+-re
+  19: Reporting-UA: sieve; GNU Mailutils [0-9][0-9.]*
+-re
+  20: Arrival-Date: [A-Z][a-z][a-z], [A-Z][a-z][a-z] [ 0-3][0-9] [ 0-2][0-9]:[0-6][0-9]:[0-6][0-9] [0-9][0-9][0-9][0-9] [a-zA-Z0-9]*
+  21: Final-Recipient: RFC822; foobar@nonexistent.net
+  22: Action: deleted
+  23: Disposition: automatic-action/MDN-sent-automatically;deleted
+-re
+  24: Last-Attempt-Date: [A-Z][a-z][a-z], [A-Z][a-z][a-z] [ 0-3][0-9] [ 0-2][0-9]:[0-6][0-9]:[0-6][0-9] [0-9][0-9][0-9][0-9] [a-zA-Z0-9]*
+  25:
+-re
+  26: [0-9:=-]+
+  27: Content-Type: message/rfc822
+  28: 
+  29: From: coyote@desert.example.org
+  30: To: roadrunner@acme.example.com
+  31: Subject: I have a present for you
+  32: X-Caffeine: C8H10N4O2
+  33: 
+  34: Look, I'm sorry about the whole anvil thing, and I really
+  35: didn't mean to try and drop it on you from the top of the
+  36: cliff.  I want to try to make it up to you.  I've got some
+  37: great birdseed over here at my place--top of the line
+  38: stuff--and if you come by, I'll have it all wrapped up
+  39: for you.  I'm really sorry for all the problems I've caused
+  40: for you over the years, but I know we can work this out.
+  41: 
+  42: -- 
+  43: Wile E. Coyote   "Super Genius"   coyote@desert.example.org
+  44:
+-re
+  45: [0-9:=-]+
+  46: 
 END OF MESSAGE
 ENVELOPE FROM: MAILER-DAEMON@nonexistent.net
 ENVELOPE TO: <b1ff@de.res.example.com>
-   0: To: <b1ff@de.res.example.com>
--re
-   1: X-Authentication-Warning: [^ \t]+ set sender using -f flag
--re
-   2: Content-Type: multipart/mixed; boundary="[0-9:=-]+"
-   3: MIME-Version: 1.0
-   4:
--re
-   5: [0-9:=-]+
-   6: Content-Type: text/plain;charset=UTF-8
-   7: Content-Transfer-Encoding: 8bit
-   8:
--re
-   9: The original message was received at [A-Z][a-z][a-z], [A-Z][a-z][a-z] [ 0-3][0-9] [ 0-2][0-9]:[0-6][0-9]:[0-6][0-9] [0-9][0-9][0-9][0-9] [a-zA-Z0-9]* from b1ff@de.res.example.com.
-  10: Message was refused by recipient's mail filtering program.
-  11: Reason given was as follows:
-  12: 
-  13: I don't want to read these messages.
-  14: 
-  15: Regards.
-  16:
--re
-  17: [0-9:=-]+
-  18: Content-Type: message/delivery-status
-  19:
--re
-  20: Reporting-UA: sieve; GNU Mailutils [0-9][0-9.]*
--re
-  21: Arrival-Date: [A-Z][a-z][a-z], [A-Z][a-z][a-z] [ 0-3][0-9] [ 0-2][0-9]:[0-6][0-9]:[0-6][0-9] [0-9][0-9][0-9][0-9] [a-zA-Z0-9]*
-  22: Final-Recipient: RFC822; foobar@nonexistent.net
-  23: Action: deleted
-  24: Disposition: automatic-action/MDN-sent-automatically;deleted
--re
-  25: Last-Attempt-Date: [A-Z][a-z][a-z], [A-Z][a-z][a-z] [ 0-3][0-9] [ 0-2][0-9]:[0-6][0-9]:[0-6][0-9] [0-9][0-9][0-9][0-9] [a-zA-Z0-9]*
-  26:
--re
-  27: [0-9:=-]+
-  28: Content-Type: message/rfc822
-  29: 
-  30: From: youcouldberich!@reply-by-postal-mail.invalid
-  31: To: rube@landru.example.edu
-  32: Subject: $$$ YOU, TOO, CAN BE A MILLIONAIRE! $$$
-  33: Date: TBD
-  34: X-Number: 0015
-  35:
-  36: YOU MAY HAVE ALREADY WON TEN MILLION DOLLARS, BUT I DOUBT
-  37: IT!  SO JUST POST THIS TO SIX HUNDRED NEWSGROUPS!  IT WILL
-  38: GUARANTEE THAT YOU GET AT LEAST FIVE RESPONSES WITH MONEY!
-  39: MONEY! MONEY! COLD HARD CASH!  YOU WILL RECEIVE OVER
-  40: $20,000 IN LESS THAN TWO MONTHS!  AND IT'S LEGAL!!!!!!!!!
-  41: !!!!!!!!!!!!!!!!!!111111111!!!!!!!11111111111!!1  JUST
-  42: SEND $5 IN SMALL, UNMARKED BILLS TO THE ADDRESSES BELOW!
-  43:
--re
-  44: [0-9:=-]+
-  45:
+   0: To: b1ff@de.res.example.com
+-re
+   1: Content-Type: multipart/mixed; boundary="[0-9:=-]+"
+   2: MIME-Version: 1.0
+   3:
+-re
+   4: [0-9:=-]+
+   5: Content-Type: text/plain;charset=UTF-8
+   6: Content-Transfer-Encoding: 8bit
+   7:
+-re
+   8: The original message was received at [A-Z][a-z][a-z], [A-Z][a-z][a-z] [ 0-3][0-9] [ 0-2][0-9]:[0-6][0-9]:[0-6][0-9] [0-9][0-9][0-9][0-9] [a-zA-Z0-9]* from b1ff@de.res.example.com.
+   9: Message was refused by recipient's mail filtering program.
+  10: Reason given was as follows:
+  11: 
+  12: I don't want to read these messages.
+  13: 
+  14: Regards.
+  15:
+-re
+  16: [0-9:=-]+
+  17: Content-Type: message/delivery-status
+  18:
+-re
+  19: Reporting-UA: sieve; GNU Mailutils [0-9][0-9.]*
+-re
+  20: Arrival-Date: [A-Z][a-z][a-z], [A-Z][a-z][a-z] [ 0-3][0-9] [ 0-2][0-9]:[0-6][0-9]:[0-6][0-9] [0-9][0-9][0-9][0-9] [a-zA-Z0-9]*
+  21: Final-Recipient: RFC822; foobar@nonexistent.net
+  22: Action: deleted
+  23: Disposition: automatic-action/MDN-sent-automatically;deleted
+-re
+  24: Last-Attempt-Date: [A-Z][a-z][a-z], [A-Z][a-z][a-z] [ 0-3][0-9] [ 0-2][0-9]:[0-6][0-9]:[0-6][0-9] [0-9][0-9][0-9][0-9] [a-zA-Z0-9]*
+  25:
+-re
+  26: [0-9:=-]+
+  27: Content-Type: message/rfc822
+  28: 
+  29: From: youcouldberich!@reply-by-postal-mail.invalid
+  30: To: rube@landru.example.edu
+  31: Subject: $$$ YOU, TOO, CAN BE A MILLIONAIRE! $$$
+  32: Date: TBD
+  33: X-Number: 0015
+  34:
+  35: YOU MAY HAVE ALREADY WON TEN MILLION DOLLARS, BUT I DOUBT
+  36: IT!  SO JUST POST THIS TO SIX HUNDRED NEWSGROUPS!  IT WILL
+  37: GUARANTEE THAT YOU GET AT LEAST FIVE RESPONSES WITH MONEY!
+  38: MONEY! MONEY! COLD HARD CASH!  YOU WILL RECEIVE OVER
+  39: $20,000 IN LESS THAN TWO MONTHS!  AND IT'S LEGAL!!!!!!!!!
+  40: !!!!!!!!!!!!!!!!!!111111111!!!!!!!11111111111!!1  JUST
+  41: SEND $5 IN SMALL, UNMARKED BILLS TO THE ADDRESSES BELOW!
+  42:
+-re
+  43: [0-9:=-]+
+  44:
 END OF MESSAGE
 ENVELOPE FROM: MAILER-DAEMON@nonexistent.net
 ENVELOPE TO: <bar@dontmailme.org>
-   0: To: <bar@dontmailme.org>
--re
-   1: X-Authentication-Warning: [^ \t]+ set sender using -f flag
--re
-   2: Content-Type: multipart/mixed; boundary="[0-9:=-]+"
-   3: MIME-Version: 1.0
-   4:
--re
-   5: [0-9:=-]+
-   6: Content-Type: text/plain;charset=UTF-8
-   7: Content-Transfer-Encoding: 8bit
-   8:
--re
-   9: The original message was received at [A-Z][a-z][a-z], [A-Z][a-z][a-z] [ 0-3][0-9] [ 0-2][0-9]:[0-6][0-9]:[0-6][0-9] [0-9][0-9][0-9][0-9] [a-zA-Z0-9]* from bar@dontmailme.org.
-  10: Message was refused by recipient's mail filtering program.
-  11: Reason given was as follows:
-  12: 
-  13: I don't want to read these messages.
-  14: 
-  15: Regards.
-  16:
--re
-  17: [0-9:=-]+
-  18: Content-Type: message/delivery-status
-  19:
--re
-  20: Reporting-UA: sieve; GNU Mailutils [0-9][0-9.]*
--re
-  21: Arrival-Date: [A-Z][a-z][a-z], [A-Z][a-z][a-z] [ 0-3][0-9] [ 0-2][0-9]:[0-6][0-9]:[0-6][0-9] [0-9][0-9][0-9][0-9] [a-zA-Z0-9]*
-  22: Final-Recipient: RFC822; foobar@nonexistent.net
-  23: Action: deleted
-  24: Disposition: automatic-action/MDN-sent-automatically;deleted
--re
-  25: Last-Attempt-Date: [A-Z][a-z][a-z], [A-Z][a-z][a-z] [ 0-3][0-9] [ 0-2][0-9]:[0-6][0-9]:[0-6][0-9] [0-9][0-9][0-9][0-9] [a-zA-Z0-9]*
-  26:
--re
-  27: [0-9:=-]+
-  28: Content-Type: message/rfc822
-  29: 
-  30: Received: (from bar@dontmailme.org)
-  31: 	by dontmailme.org id fERKR9N16790
-  32: 	for foobar@nonexistent.net; Fri, 28 Dec 2001 22:18:08 +0200
-  33: Date: Fri, 28 Dec 2001 23:28:08 +0200
-  34: From: Bar <bar@dontmailme.org>
-  35: To: Foo Bar <foobar@nonexistent.net>
-  36: Message-Id: <200112232808.fERKR9N16790@dontmailme.org>
-  37: Subject: Coffee
-  38: 
-  39: How about some coffee?
-  40:
--re
-  41: [0-9:=-]+
-  42: 
+   0: To: bar@dontmailme.org
+-re
+   1: Content-Type: multipart/mixed; boundary="[0-9:=-]+"
+   2: MIME-Version: 1.0
+   3:
+-re
+   4: [0-9:=-]+
+   5: Content-Type: text/plain;charset=UTF-8
+   6: Content-Transfer-Encoding: 8bit
+   7:
+-re
+   8: The original message was received at [A-Z][a-z][a-z], [A-Z][a-z][a-z] [ 0-3][0-9] [ 0-2][0-9]:[0-6][0-9]:[0-6][0-9] [0-9][0-9][0-9][0-9] [a-zA-Z0-9]* from bar@dontmailme.org.
+   9: Message was refused by recipient's mail filtering program.
+  10: Reason given was as follows:
+  11: 
+  12: I don't want to read these messages.
+  13: 
+  14: Regards.
+  15:
+-re
+  16: [0-9:=-]+
+  17: Content-Type: message/delivery-status
+  18:
+-re
+  19: Reporting-UA: sieve; GNU Mailutils [0-9][0-9.]*
+-re
+  20: Arrival-Date: [A-Z][a-z][a-z], [A-Z][a-z][a-z] [ 0-3][0-9] [ 0-2][0-9]:[0-6][0-9]:[0-6][0-9] [0-9][0-9][0-9][0-9] [a-zA-Z0-9]*
+  21: Final-Recipient: RFC822; foobar@nonexistent.net
+  22: Action: deleted
+  23: Disposition: automatic-action/MDN-sent-automatically;deleted
+-re
+  24: Last-Attempt-Date: [A-Z][a-z][a-z], [A-Z][a-z][a-z] [ 0-3][0-9] [ 0-2][0-9]:[0-6][0-9]:[0-6][0-9] [0-9][0-9][0-9][0-9] [a-zA-Z0-9]*
+  25:
+-re
+  26: [0-9:=-]+
+  27: Content-Type: message/rfc822
+  28: 
+  29: Received: (from bar@dontmailme.org)
+  30: 	by dontmailme.org id fERKR9N16790
+  31: 	for foobar@nonexistent.net; Fri, 28 Dec 2001 22:18:08 +0200
+  32: Date: Fri, 28 Dec 2001 23:28:08 +0200
+  33: From: Bar <bar@dontmailme.org>
+  34: To: Foo Bar <foobar@nonexistent.net>
+  35: Message-Id: <200112232808.fERKR9N16790@dontmailme.org>
+  36: Subject: Coffee
+  37: 
+  38: How about some coffee?
+  39:
+-re
+  40: [0-9:=-]+
+  41: 
 END OF MESSAGE
 FILE END
 TEST END


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Sun Aug 29 03:08:05 2010
Received: from mailman by lists.gnu.org with archive (Exim 4.43)
	id 1Opbzp-0006Pv-Ic
	for mharc-commit-mailutils@gnu.org; Sun, 29 Aug 2010 03:08:05 -0400
Received: from [140.186.70.92] (port=33060 helo=eggs.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.43) id 1Opbzm-0006Pl-R7
	for commit-mailutils@gnu.org; Sun, 29 Aug 2010 03:08:04 -0400
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1Opbzk-0003hk-Nk
	for commit-mailutils@gnu.org; Sun, 29 Aug 2010 03:08:02 -0400
Received: from colonialone.fsf.org ([140.186.70.51]:45548
	helo=internal.in.savannah.gnu.org)
	by eggs.gnu.org with esmtp (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1Opbzk-0003hf-Kr
	for commit-mailutils@gnu.org; Sun, 29 Aug 2010 03: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 1Opbzk-0005zp-J4
	for commit-mailutils@gnu.org; Sun, 29 Aug 2010 07: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 1Opbzj-0000Au-HM
	for commit-mailutils@gnu.org; Sun, 29 Aug 2010 07:08:00 +0000
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
X-Git-Refname: refs/heads/stream-cleanup
X-Git-Reftype: branch
X-Git-Oldrev: afbdbd89e1edd58916bf504ffc41f0884a6992c9
X-Git-Newrev: 4aa0382895299d0d773d7e6a7ee7fa12a5ca5e5d
Message-Id: <E1Opbzj-0000Au-HM@vcs-noshell.in.savannah.gnu.org>
Date: Sun, 29 Aug 2010 07:08:00 +0000
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
Subject: [SCM] GNU Mailutils branch, stream-cleanup,
	updated. rel-2_1-108-g4aa0382
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, 29 Aug 2010 07:08: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=4aa0382895299d0d773d7e6a7ee7fa12a5ca5e5d

The branch, stream-cleanup has been updated
       via  4aa0382895299d0d773d7e6a7ee7fa12a5ca5e5d (commit)
       via  99b2c31936f74efccd19e44147992dc0674f87e4 (commit)
      from  afbdbd89e1edd58916bf504ffc41f0884a6992c9 (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 4aa0382895299d0d773d7e6a7ee7fa12a5ca5e5d
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Sun Aug 29 10:06:59 2010 +0300

    libmu_sieve: cleanup
    
    * libmu_sieve/actions.c (mime_create_quote): Use mu_stream_copy.

commit 99b2c31936f74efccd19e44147992dc0674f87e4
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Sun Aug 29 10:04:38 2010 +0300

    pop3d: cleanup
    
    * pop3d/pop3d.h: Include filter.h.
    (istream, ostream): New externs.
    * pop3d/extra.c (pop3d_setio): Set a crlf filter on the
    output stream. This allows to forget about \r in the rest of
    the code.
    * pop3d/retr.c (pop3d_retr): Use mu_stream_copy instead of
    copying streams manually.
    * pop3d/top.c (pop3d_top): Rewrite using streamrefs and
    mu_stream_copy.
    * all sources: Use \n instead of \r\n in output strings.

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

Summary of changes:
 libmu_sieve/actions.c |   14 ++------
 pop3d/capa.c          |   24 +++++++-------
 pop3d/dele.c          |    2 +-
 pop3d/extra.c         |   20 +++++++----
 pop3d/list.c          |    8 ++--
 pop3d/logindelay.c    |    2 +-
 pop3d/noop.c          |    2 +-
 pop3d/pop3d.c         |    4 +-
 pop3d/pop3d.h         |    2 +
 pop3d/quit.c          |    2 +-
 pop3d/retr.c          |   39 ++++------------------
 pop3d/rset.c          |    2 +-
 pop3d/stat.c          |    2 +-
 pop3d/stls.c          |    2 +-
 pop3d/top.c           |   84 ++++++++++++++++--------------------------------
 pop3d/uidl.c          |    8 ++--
 pop3d/user.c          |    4 +-
 17 files changed, 84 insertions(+), 137 deletions(-)

diff --git a/libmu_sieve/actions.c b/libmu_sieve/actions.c
index 1beb8f9..940b691 100644
--- a/libmu_sieve/actions.c
+++ b/libmu_sieve/actions.c
@@ -242,10 +242,8 @@ mime_create_quote (mu_mime_t mime, mu_message_t msg)
   mu_message_t newmsg;
   mu_stream_t istream, ostream;
   mu_header_t hdr;
-  size_t n;
-  char buffer[512];
-  mu_body_t body;
   int rc;
+  mu_body_t body;
   
   mu_message_create (&newmsg, NULL);
   mu_message_get_header (newmsg, &hdr); 
@@ -254,14 +252,8 @@ mime_create_quote (mu_mime_t mime, mu_message_t msg)
   mu_body_get_streamref (body, &ostream);
   mu_message_get_streamref (msg, &istream);
 
-  rc = 0;/* FIXME: use mu_stream_copy */
-  while (mu_stream_read (istream, buffer, sizeof buffer - 1, &n) == 0
-	 && n != 0)
-    {
-      rc = mu_stream_write (ostream, buffer, n, NULL);
-      if (rc)
-	break;
-    }
+  rc = mu_stream_copy (ostream, istream, 0);
+
   mu_stream_destroy (&istream);
   mu_stream_close (ostream);
   mu_stream_destroy (&ostream);
diff --git a/pop3d/capa.c b/pop3d/capa.c
index 688b09d..ca58512 100644
--- a/pop3d/capa.c
+++ b/pop3d/capa.c
@@ -35,30 +35,30 @@ pop3d_capa (char *arg)
   if (state != initial_state && state != TRANSACTION)
     return ERR_WRONG_STATE;
 
-  pop3d_outf ("+OK Capability list follows\r\n");
-  pop3d_outf ("TOP\r\n");
-  pop3d_outf ("USER\r\n");
-  pop3d_outf ("UIDL\r\n");
-  pop3d_outf ("RESP-CODES\r\n");
-  pop3d_outf ("PIPELINING\r\n");
+  pop3d_outf ("+OK Capability list follows\n");
+  pop3d_outf ("TOP\n");
+  pop3d_outf ("USER\n");
+  pop3d_outf ("UIDL\n");
+  pop3d_outf ("RESP-CODES\n");
+  pop3d_outf ("PIPELINING\n");
 
 #ifdef WITH_TLS
   if (tls_available && tls_done == 0)
-    pop3d_outf ("STLS\r\n");
+    pop3d_outf ("STLS\n");
 #endif /* WITH_TLS */
 
   login_delay_capa ();
   /* This can be implemented by setting an header field on the message.  */
   if (expire == EXPIRE_NEVER)
-    pop3d_outf ("EXPIRE NEVER\r\n");
+    pop3d_outf ("EXPIRE NEVER\n");
   else 
-    pop3d_outf ("EXPIRE %s\r\n", mu_umaxtostr (0, expire));
+    pop3d_outf ("EXPIRE %s\n", mu_umaxtostr (0, expire));
 
   if (state == INITIAL)
-    pop3d_outf ("XTLSREQUIRED\r\n");
+    pop3d_outf ("XTLSREQUIRED\n");
   
   if (state == TRANSACTION)	/* let's not advertise to just anyone */
-    pop3d_outf ("IMPLEMENTATION %s\r\n", PACKAGE_STRING);
-  pop3d_outf (".\r\n");
+    pop3d_outf ("IMPLEMENTATION %s\n", PACKAGE_STRING);
+  pop3d_outf (".\n");
   return OK;
 }
diff --git a/pop3d/dele.c b/pop3d/dele.c
index f838f59..08e3ac1 100644
--- a/pop3d/dele.c
+++ b/pop3d/dele.c
@@ -39,6 +39,6 @@ pop3d_dele (char *arg)
 
   mu_message_get_attribute (msg, &attr);
   pop3d_mark_deleted (attr);
-  pop3d_outf ("+OK Message %s marked\r\n", mu_umaxtostr (0, num));
+  pop3d_outf ("+OK Message %s marked\n", mu_umaxtostr (0, num));
   return OK;
 }
diff --git a/pop3d/extra.c b/pop3d/extra.c
index 72f0e67..ca5089e 100644
--- a/pop3d/extra.c
+++ b/pop3d/extra.c
@@ -20,7 +20,7 @@
 #include "pop3d.h"
 #include "mailutils/libargp.h"
 
-static mu_stream_t istream, ostream;
+mu_stream_t istream, ostream;
 
 void
 pop3d_parse_command (char *cmd, char **pcmd, char **parg)
@@ -61,7 +61,7 @@ pop3d_abquit (int reason)
     {
     case ERR_NO_MEM:
       code = EX_SOFTWARE;
-      pop3d_outf ("-ERR %s\r\n", pop3d_error_string (reason));
+      pop3d_outf ("-ERR %s\n", pop3d_error_string (reason));
       mu_diag_output (MU_DIAG_ERROR, _("not enough memory"));
       break;
 
@@ -77,7 +77,7 @@ pop3d_abquit (int reason)
 
     case ERR_TIMEOUT:
       code = EX_TEMPFAIL;
-      pop3d_outf ("-ERR %s\r\n", pop3d_error_string (reason));
+      pop3d_outf ("-ERR %s\n", pop3d_error_string (reason));
       if (state == TRANSACTION)
 	mu_diag_output (MU_DIAG_INFO, _("session timed out for user: %s"),
 			username);
@@ -111,12 +111,12 @@ pop3d_abquit (int reason)
 		      _("mailbox was updated by other party: %s"),
 		      username);
       pop3d_outf
-	("-ERR [OUT-SYNC] Mailbox updated by other party or corrupt\r\n");
+	("-ERR [OUT-SYNC] Mailbox updated by other party or corrupt\n");
       break;
 
     default:
       code = EX_SOFTWARE;
-      pop3d_outf ("-ERR Quitting: %s\r\n", pop3d_error_string (reason));
+      pop3d_outf ("-ERR Quitting: %s\n", pop3d_error_string (reason));
       mu_diag_output (MU_DIAG_ERROR, _("quitting (numeric reason %d)"),
 		      reason);
       break;
@@ -129,6 +129,8 @@ pop3d_abquit (int reason)
 void
 pop3d_setio (FILE *in, FILE *out)
 {
+  mu_stream_t str;
+  
   if (!in)
     pop3d_abquit (ERR_NO_IFILE);
   if (!out)
@@ -136,9 +138,13 @@ pop3d_setio (FILE *in, FILE *out)
 
   if (mu_stdio_stream_create (&istream, fileno (in), MU_STREAM_NO_CLOSE))
     pop3d_abquit (ERR_NO_IFILE);
-  if (mu_stdio_stream_create (&ostream, fileno (out), MU_STREAM_NO_CLOSE))
-    pop3d_abquit (ERR_NO_OFILE);
   mu_stream_set_buffer (istream, mu_buffer_line, 1024);
+  
+  if (mu_stdio_stream_create (&str, fileno (out), MU_STREAM_NO_CLOSE))
+    pop3d_abquit (ERR_NO_OFILE);
+  if (mu_filter_create (&ostream, str, "rfc822", MU_FILTER_ENCODE,
+			MU_STREAM_WRITE|MU_STREAM_NO_CLOSE))
+    pop3d_abquit (ERR_NO_IFILE);
   mu_stream_set_buffer (ostream, mu_buffer_line, 1024);
 }
 
diff --git a/pop3d/list.c b/pop3d/list.c
index b192875..a212e41 100644
--- a/pop3d/list.c
+++ b/pop3d/list.c
@@ -37,7 +37,7 @@ pop3d_list (char *arg)
   if (strlen (arg) == 0)
     {
       size_t total = 0;
-      pop3d_outf ("+OK\r\n");
+      pop3d_outf ("+OK\n");
       mu_mailbox_messages_count (mbox, &total);
       for (mesgno = 1; mesgno <= total; mesgno++)
 	{
@@ -47,12 +47,12 @@ pop3d_list (char *arg)
 	    {
 	      mu_message_size (msg, &size);
 	      mu_message_lines (msg, &lines);
-	      pop3d_outf ("%s %s\r\n", 
+	      pop3d_outf ("%s %s\n", 
                           mu_umaxtostr (0, mesgno), 
                           mu_umaxtostr (1, size + lines));
 	    }
 	}
-      pop3d_outf (".\r\n");
+      pop3d_outf (".\n");
     }
   else
     {
@@ -64,7 +64,7 @@ pop3d_list (char *arg)
 	return ERR_MESG_DELE;
       mu_message_size (msg, &size);
       mu_message_lines (msg, &lines);
-      pop3d_outf ("+OK %s %s\r\n", 
+      pop3d_outf ("+OK %s %s\n", 
                   mu_umaxtostr (0, mesgno),
                   mu_umaxtostr (1, size + lines));
     }
diff --git a/pop3d/logindelay.c b/pop3d/logindelay.c
index 9c8020d..5cab22c 100644
--- a/pop3d/logindelay.c
+++ b/pop3d/logindelay.c
@@ -126,7 +126,7 @@ login_delay_capa ()
   
   if (login_delay && open_stat_db (&db, MU_STREAM_RDWR) == 0)
     {
-      pop3d_outf ("LOGIN-DELAY %s\r\n", mu_umaxtostr (0, login_delay));
+      pop3d_outf ("LOGIN-DELAY %s\n", mu_umaxtostr (0, login_delay));
       mu_dbm_close (db);
     }
 }
diff --git a/pop3d/noop.c b/pop3d/noop.c
index a3d8e64..55ecf18 100644
--- a/pop3d/noop.c
+++ b/pop3d/noop.c
@@ -26,6 +26,6 @@ pop3d_noop (char *arg)
     return ERR_BAD_ARGS;
   if (state != TRANSACTION)
     return ERR_WRONG_STATE;
-  pop3d_outf ("+OK\r\n");
+  pop3d_outf ("+OK\n");
   return OK;
 }
diff --git a/pop3d/pop3d.c b/pop3d/pop3d.c
index 62c871b..5a09efc 100644
--- a/pop3d/pop3d.c
+++ b/pop3d/pop3d.c
@@ -248,7 +248,7 @@ pop3d_mainloop (int fd, FILE *infile, FILE *outfile)
   }
 
   /* Lets boogie.  */
-  pop3d_outf ("+OK POP3 Ready %s\r\n", md5shared);
+  pop3d_outf ("+OK POP3 Ready %s\n", md5shared);
 
   while (state != UPDATE && state != ABORT)
     {
@@ -291,7 +291,7 @@ pop3d_mainloop (int fd, FILE *infile, FILE *outfile)
 	status = ERR_BAD_CMD;
 
       if (status != OK)
-	pop3d_outf ("-ERR %s\r\n", pop3d_error_string (status));
+	pop3d_outf ("-ERR %s\n", pop3d_error_string (status));
     }
 
   pop3d_bye ();
diff --git a/pop3d/pop3d.h b/pop3d/pop3d.h
index ad580b7..b7e593b 100644
--- a/pop3d/pop3d.h
+++ b/pop3d/pop3d.h
@@ -125,6 +125,7 @@ extern int expire_on_exit;
 #include <mailutils/md5.h>
 #include <mailutils/acl.h>
 #include <mailutils/server.h>
+#include <mailutils/filter.h>
 
 /* For Berkley DB2 APOP password file */
 #ifdef HAVE_DB_H
@@ -188,6 +189,7 @@ struct pop3d_command
   pop3d_command_handler_t handler;
 };
 
+extern mu_stream_t istream, ostream;
 extern mu_pop_server_t pop3srv;
 extern mu_mailbox_t mbox;
 extern int state;
diff --git a/pop3d/quit.c b/pop3d/quit.c
index 94857cb..0b0ca15 100644
--- a/pop3d/quit.c
+++ b/pop3d/quit.c
@@ -53,7 +53,7 @@ pop3d_quit (char *arg)
   free (md5shared);
 
   if (err == OK)
-    pop3d_outf ("+OK\r\n");
+    pop3d_outf ("+OK\n");
   return err;
 }
 
diff --git a/pop3d/retr.c b/pop3d/retr.c
index c5e44f5..53b25c0 100644
--- a/pop3d/retr.c
+++ b/pop3d/retr.c
@@ -22,12 +22,10 @@
 int
 pop3d_retr (char *arg)
 {
-  size_t mesgno, n;
-  char buf[BUFFERSIZE];
+  size_t mesgno;
   mu_message_t msg = NULL;
   mu_attribute_t attr = NULL;
-  mu_stream_t stream = NULL;
-  int prev_nl;
+  mu_stream_t stream;
   
   if ((strlen (arg) == 0) || (strchr (arg, ' ') != NULL))
     return ERR_BAD_ARGS;
@@ -44,42 +42,19 @@ pop3d_retr (char *arg)
   if (pop3d_is_deleted (attr))
     return ERR_MESG_DELE;
 
-  /* FIXME: Use crlf filter + mu_stream_copy instead of the loop below */
-  if (mu_message_get_stream (msg, &stream)
-      || mu_stream_seek (stream, 0, MU_SEEK_SET, NULL))
+  if (mu_message_get_streamref (msg, &stream))
     return ERR_UNKNOWN;
   
-  pop3d_outf ("+OK\r\n");
-
-  prev_nl = 1;
-  while (mu_stream_readline (stream, buf, sizeof(buf), &n) == 0
-	 && n > 0)
-    {
-      if (prev_nl && buf[0] == '.')
-	pop3d_outf (".");
-      
-      if (buf[n - 1] == '\n')
-	{
-	  buf[n - 1] = '\0';
-	  pop3d_outf ("%s\r\n", buf);
-	  prev_nl = 1;
-	}
-      else
-	{
-	  pop3d_outf ("%s", buf);
-	  prev_nl = 0;
-	}
-    }
-
-  if (!prev_nl)
-    pop3d_outf ("\r\n");
+  pop3d_outf ("+OK\n");
+  mu_stream_copy (ostream, stream, 0);
+  mu_stream_destroy (&stream);
 
   if (!mu_attribute_is_read (attr))
     mu_attribute_set_read (attr);
 
   pop3d_mark_retr (attr);
 
-  pop3d_outf (".\r\n");
+  pop3d_outf (".\n");
 
   return OK;
 }
diff --git a/pop3d/rset.c b/pop3d/rset.c
index 4a03acf..48df589 100644
--- a/pop3d/rset.c
+++ b/pop3d/rset.c
@@ -41,6 +41,6 @@ pop3d_rset (char *arg)
       mu_message_get_attribute (msg, &attr);
       pop3d_unset_deleted (attr);
     }
-  pop3d_outf ("+OK\r\n");
+  pop3d_outf ("+OK\n");
   return OK;
 }
diff --git a/pop3d/stat.c b/pop3d/stat.c
index 0b4699c..9daf3ed 100644
--- a/pop3d/stat.c
+++ b/pop3d/stat.c
@@ -57,7 +57,7 @@ pop3d_stat (char *arg)
 	  num++;
 	}
     }
-  pop3d_outf ("+OK %s %s\r\n", mu_umaxtostr (0, num), mu_umaxtostr (1, tsize));
+  pop3d_outf ("+OK %s %s\n", mu_umaxtostr (0, num), mu_umaxtostr (1, tsize));
 
   return OK;
 }
diff --git a/pop3d/stls.c b/pop3d/stls.c
index 0e35ba7..189bc7c 100644
--- a/pop3d/stls.c
+++ b/pop3d/stls.c
@@ -32,7 +32,7 @@ pop3d_stls (char *arg)
   if (tls_done)
     return ERR_TLS_ACTIVE;
 
-  pop3d_outf ("+OK Begin TLS negotiation\r\n");
+  pop3d_outf ("+OK Begin TLS negotiation\n");
   pop3d_flush_output ();
 
   tls_done = pop3d_init_tls_server ();
diff --git a/pop3d/top.c b/pop3d/top.c
index c51356d..bab0583 100644
--- a/pop3d/top.c
+++ b/pop3d/top.c
@@ -23,16 +23,14 @@ int
 pop3d_top (char *arg)
 {
   size_t mesgno;
-  int lines;
+  unsigned long lines;
   mu_message_t msg;
   mu_attribute_t attr;
   mu_header_t hdr;
   mu_body_t body;
   mu_stream_t stream;
-  char *mesgc, *linesc;
-  char buf[BUFFERSIZE];
-  size_t n;
-
+  char *mesgc, *linesc, *p;
+  
   if (strlen (arg) == 0)
     return ERR_BAD_ARGS;
 
@@ -40,11 +38,14 @@ pop3d_top (char *arg)
     return ERR_WRONG_STATE;
 
   pop3d_parse_command (arg, &mesgc, &linesc);
+  if (linesc[0] == 0)
+    return ERR_BAD_ARGS;
   
-  mesgno = strtoul (mesgc, NULL, 10);
-  lines = *linesc ? strtol (linesc, NULL, 10) : -1;
-
-  if (lines < 0)
+  mesgno = strtoul (mesgc, &p, 10);
+  if (*p)
+    return ERR_BAD_ARGS;
+  lines = strtoul (linesc, &p, 10);
+  if (*p)
     return ERR_BAD_ARGS;
 
   if (mu_mailbox_get_message (mbox, mesgno, &msg) != 0)
@@ -55,63 +56,34 @@ pop3d_top (char *arg)
     return ERR_MESG_DELE;
   pop3d_mark_retr (attr);
   
-  pop3d_outf ("+OK\r\n");
-
   /* Header.  */
   mu_message_get_header (msg, &hdr);
-  /* FIXME: Use crlf filter + mu_stream_copy instead of the below loop */
-  if (mu_header_get_stream (hdr, &stream)
-      || mu_stream_seek (stream, 0, MU_SEEK_SET, NULL))
+  if (mu_header_get_streamref (hdr, &stream))
     return ERR_UNKNOWN;
-  n = 0;
-  while (mu_stream_readline (stream, buf, sizeof(buf), &n) == 0
-	 && n > 0)
-    {
-      /* Nuke the trainline newline.  */
-      if (buf[n - 1] == '\n')
-	{
-	  buf[n - 1] = '\0';
-	  pop3d_outf ("%s\r\n", buf);
-	}
-      else
-	pop3d_outf ("%s", buf);
-    }
+  pop3d_outf ("+OK\n");
 
-  /* Lines of body.  */
-  if (lines)
+  mu_stream_copy (ostream, stream, 0);
+  pop3d_outf ("\n");
+  mu_stream_destroy (&stream);
+  
+  mu_message_get_body (msg, &body);
+  if (mu_body_get_streamref (body, &stream) == 0)
     {
-      int prev_nl = 1;
-
-      mu_message_get_body (msg, &body);
-      /* FIXME: Use the crlf filter + mu_stream_copy */
-      if (mu_body_get_stream (body, &stream)
-	  || mu_stream_seek (stream, 0, MU_SEEK_SET, NULL))
-	return ERR_UNKNOWN;
-      n = 0;
-      while (mu_stream_readline (stream, buf, sizeof(buf), &n) == 0
-	     && n > 0 && lines > 0)
+      char *buf = NULL;
+      size_t size = 0, n;
+      while (lines > 0 &&
+	     mu_stream_getline (stream, &buf, &size, &n) == 0 &&
+	     n > 0)
 	{
-	  if (prev_nl && buf[0] == '.')
+	  if (buf[0] == '.')
 	    pop3d_outf (".");
-      
-	  if (buf[n - 1] == '\n')
-	    {
-	      buf[n - 1] = '\0';
-	      pop3d_outf ("%s\r\n", buf);
-	      prev_nl = 1;
-	      lines--;
-	    }
-	  else
-	    {
-	      pop3d_outf ("%s", buf);
-	      prev_nl = 0;
-	    }
+	  pop3d_outf ("%s\n", buf);
 	}
-      if (!prev_nl)
-	pop3d_outf ("\r\n");
+      mu_stream_destroy (&stream);
+      free (buf);
     }
 
-  pop3d_outf (".\r\n");
+  pop3d_outf (".\n");
 
   return OK;
 }
diff --git a/pop3d/uidl.c b/pop3d/uidl.c
index d52cc41..fa3e248 100644
--- a/pop3d/uidl.c
+++ b/pop3d/uidl.c
@@ -34,7 +34,7 @@ pop3d_uidl (char *arg)
   if (strlen (arg) == 0)
     {
       size_t total = 0;
-      pop3d_outf ("+OK\r\n");
+      pop3d_outf ("+OK\n");
       mu_mailbox_messages_count (mbox, &total);
       for (mesgno = 1; mesgno <= total; mesgno++)
         {
@@ -43,10 +43,10 @@ pop3d_uidl (char *arg)
           if (!pop3d_is_deleted (attr))
             {
               mu_message_get_uidl (msg, uidl, sizeof (uidl), NULL);
-              pop3d_outf ("%s %s\r\n", mu_umaxtostr (0, mesgno), uidl);
+              pop3d_outf ("%s %s\n", mu_umaxtostr (0, mesgno), uidl);
             }
         }
-      pop3d_outf (".\r\n");
+      pop3d_outf (".\n");
     }
   else
     {
@@ -57,7 +57,7 @@ pop3d_uidl (char *arg)
       if (pop3d_is_deleted (attr))
         return ERR_MESG_DELE;
       mu_message_get_uidl (msg, uidl, sizeof (uidl), NULL);
-      pop3d_outf ("+OK %s %s\r\n", mu_umaxtostr (0, mesgno), uidl);
+      pop3d_outf ("+OK %s %s\n", mu_umaxtostr (0, mesgno), uidl);
     }
 
   return OK;
diff --git a/pop3d/user.c b/pop3d/user.c
index 6cb130c..6d0ef3f 100644
--- a/pop3d/user.c
+++ b/pop3d/user.c
@@ -63,7 +63,7 @@ pop3d_begin_session ()
     pop3d_abquit (ERR_NO_MEM);
   state = TRANSACTION;
 
-  pop3d_outf ("+OK opened mailbox for %s\r\n", username);
+  pop3d_outf ("+OK opened mailbox for %s\n", username);
 
   if (undelete_on_startup)
     pop3d_undelete_all ();
@@ -98,7 +98,7 @@ pop3d_user (char *arg)
   if ((strlen (arg) == 0) || (strchr (arg, ' ') != NULL))
     return ERR_BAD_ARGS;
 
-  pop3d_outf ("+OK\r\n");
+  pop3d_outf ("+OK\n");
   pop3d_flush_output ();
 
   buf = pop3d_readline (buffer, sizeof (buffer));


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Sun Aug 29 08:22:53 2010
Received: from mailman by lists.gnu.org with archive (Exim 4.43)
	id 1OpguT-0004bf-RD
	for mharc-commit-mailutils@gnu.org; Sun, 29 Aug 2010 08:22:53 -0400
Received: from [140.186.70.92] (port=45496 helo=eggs.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.43) id 1OpguR-0004ba-2l
	for commit-mailutils@gnu.org; Sun, 29 Aug 2010 08:22:53 -0400
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1OpguL-0006eG-Ae
	for commit-mailutils@gnu.org; Sun, 29 Aug 2010 08:22:50 -0400
Received: from colonialone.fsf.org ([140.186.70.51]:45418
	helo=internal.in.savannah.gnu.org)
	by eggs.gnu.org with esmtp (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1OpguL-0006eA-8p
	for commit-mailutils@gnu.org; Sun, 29 Aug 2010 08:22: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 1OpguL-0001QL-35
	for commit-mailutils@gnu.org; Sun, 29 Aug 2010 12:22:45 +0000
Received: from gray by vcs-noshell.in.savannah.gnu.org with local (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1OpguJ-0004U5-EC
	for commit-mailutils@gnu.org; Sun, 29 Aug 2010 12:22:44 +0000
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
X-Git-Refname: refs/heads/stream-cleanup
X-Git-Reftype: branch
X-Git-Oldrev: 4aa0382895299d0d773d7e6a7ee7fa12a5ca5e5d
X-Git-Newrev: f1625c353026c160c8db18ac9a4bd3bd35df150c
Message-Id: <E1OpguJ-0004U5-EC@vcs-noshell.in.savannah.gnu.org>
Date: Sun, 29 Aug 2010 12:22:43 +0000
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
Subject: [SCM] GNU Mailutils branch, stream-cleanup,
	updated. rel-2_1-109-gf1625c3
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, 29 Aug 2010 12:22: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=f1625c353026c160c8db18ac9a4bd3bd35df150c

The branch, stream-cleanup has been updated
       via  f1625c353026c160c8db18ac9a4bd3bd35df150c (commit)
      from  4aa0382895299d0d773d7e6a7ee7fa12a5ca5e5d (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 f1625c353026c160c8db18ac9a4bd3bd35df150c
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Sun Aug 29 14:30:58 2010 +0300

    Bugfix.
    
    * imap4d/append.c: Add a comment.
    * imap4d/imap4d.h (imap4d_child_signal_setup): New proto.
    * libmu_sieve/extensions/pipe.c (sieve_action_pipe): Use
    mu_stream_copy, rewrite error handling.
    * libmu_sieve/extensions/spamd.c (spamd_send_message): se
    mu_stream_copy.
    (spamd_test): Honor dry-run mode.
    * libmu_sieve/extensions/vacation.c (build_mime): Remove
    misleading FIXME. Use mu_stream_copy.
    * libmu_sieve/extensions/moderator.c (moderator_message_get_part): Use
    mu_body_get_streamref.
    * examples/header.c (hstream): New global.
    (cmd_load, cmd_free, cmd_remove, cmd_insert): Discard hstream.
    (cmd_readline): Use hstream. Obtain it using mu_stream_readline.
    * libproto/mbox/mbox.c (mbox_envelope_date): Remove trailing
    newline from the obtained envelope line.
    * mailbox/amd.c (amd_body_stream_seek): Allow for off == size
    (imprtant for empty bodies).

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

Summary of changes:
 examples/header.c                  |   17 +++-
 imap4d/append.c                    |    1 +
 imap4d/imap4d.h                    |    1 +
 libmu_sieve/extensions/moderator.c |    3 +-
 libmu_sieve/extensions/pipe.c      |  147 +++++++++++++++++------------------
 libmu_sieve/extensions/spamd.c     |   40 ++++++-----
 libmu_sieve/extensions/vacation.c  |   48 ++++++++----
 libproto/mbox/mbox.c               |    3 +-
 mailbox/amd.c                      |    2 +-
 9 files changed, 146 insertions(+), 116 deletions(-)

diff --git a/examples/header.c b/examples/header.c
index 90cce65..63733f8 100644
--- a/examples/header.c
+++ b/examples/header.c
@@ -28,6 +28,7 @@
 char *file;
 mu_header_t header;
 mu_iterator_t iterator;
+mu_stream_t hstream;
 
 char *ps[] = { "> ", ". " };
 int interactive;
@@ -124,6 +125,7 @@ cmd_load (int argc, char **argv)
 {
   if (check_args (argv[0], argc, 2, 2))
     return;
+  mu_stream_destroy (&hstream);
   mu_header_destroy (&header);
   load_file (argv[1]);
 }
@@ -134,6 +136,7 @@ cmd_free (int argc, char **argv)
   if (check_args (argv[0], argc, 1, 1))
     return;
   mu_iterator_destroy (&iterator);
+  mu_stream_destroy (&hstream);
   mu_header_destroy (&header);
 }
 
@@ -211,6 +214,7 @@ cmd_remove (int argc, char **argv)
   status = mu_header_remove (header, fn, num);
   if (status)
     mu_error ("%u: %s: %s", line_num, argv[0], mu_strerror (status));
+  mu_stream_destroy (&hstream);
 }
 
 /* insert header value [ref [num] [before|after] [replace]] */
@@ -260,6 +264,7 @@ cmd_insert (int argc, char **argv)
     }
   status = mu_header_insert (header, argv[1], argv[2],
 			     ref, num, flags);
+  mu_stream_destroy (&hstream);
   if (status)
     mu_error ("%u: %s: %s", line_num, argv[0], mu_strerror (status));
 }
@@ -289,6 +294,7 @@ cmd_write (int argc, char **argv)
 	break;
     }
   mu_stream_destroy (&str);
+  mu_stream_destroy (&hstream);
 }
 
 void
@@ -341,18 +347,19 @@ void
 cmd_readline (int argc, char **argv)
 {
   char *buf;
-  size_t size;
-  mu_stream_t stream;
+  size_t size = 128;
   size_t nbytes;
   
   if (check_args (argv[0], argc, 1, 2))
     return;
-  size = atoi (argv[1]);
+  if (argc == 2)
+    size = atoi (argv[1]);
   buf = malloc (size);
   if (!buf)
     abort ();
-  mu_header_get_stream (header, &stream);
-  mu_stream_readline (stream, buf, size, &nbytes);
+  if (!hstream)
+    mu_header_get_streamref (header, &hstream);
+  mu_stream_readline (hstream, buf, size, &nbytes);
   printf ("\"%*.*s\"", (int) nbytes, (int) nbytes, buf);
   free (buf);
 }
diff --git a/imap4d/append.c b/imap4d/append.c
index c8d5722..e549b59 100644
--- a/imap4d/append.c
+++ b/imap4d/append.c
@@ -45,6 +45,7 @@ _append_sender (mu_envelope_t envelope, char *buf, size_t len, size_t *pnwrite)
   return 0;
 }
 
+/* FIXME: Why not use mu_message_size instead? */
 static int
 _append_size (mu_message_t msg, size_t *psize)
 {
diff --git a/imap4d/imap4d.h b/imap4d/imap4d.h
index 6a01e46..17e7ef6 100644
--- a/imap4d/imap4d.h
+++ b/imap4d/imap4d.h
@@ -322,6 +322,7 @@ extern char *namespace_checkfullpath (const char *name, const char *pattern,
 				       const char *delim, int *pns);
 int imap4d_session_setup (char *username);
 int imap4d_session_setup0 (void);
+void imap4d_child_signal_setup (RETSIGTYPE (*handler) (int signo));
   
 /* Capability functions */
 extern void imap4d_capability_add (const char *str);
diff --git a/libmu_sieve/extensions/moderator.c b/libmu_sieve/extensions/moderator.c
index 9e0aecb..f3dc8c4 100644
--- a/libmu_sieve/extensions/moderator.c
+++ b/libmu_sieve/extensions/moderator.c
@@ -213,7 +213,7 @@ moderator_message_get_part (mu_sieve_machine_t mach,
 
       free (value);
       mu_message_get_body (tmp, &body);
-      mu_body_get_stream (body, &str);
+      mu_body_get_streamref (body, &str);
 
       rc = mu_stream_to_message (str, pmsg);
       if (rc)
@@ -223,6 +223,7 @@ moderator_message_get_part (mu_sieve_machine_t mach,
 			  mu_strerror (rc));
 	  return 1;
 	}
+      mu_stream_destroy (&str);
     }
   else if (value)
     {
diff --git a/libmu_sieve/extensions/pipe.c b/libmu_sieve/extensions/pipe.c
index 420fa40..717706a 100644
--- a/libmu_sieve/extensions/pipe.c
+++ b/libmu_sieve/extensions/pipe.c
@@ -21,9 +21,9 @@
    The pipe action executes a shell command specified by its
    argument and pipes the entire message to its standard input.
    The envelope of the message is included, if the :envelope tag is given.
-   
+
    Notes/FIXME: 1. it would be nice to implement meta-variables in
-                <program call> which would expand to various
+		<program call> which would expand to various
 		items from the message being handled.
 		2. :mime tag could be useful too.
 */
@@ -41,38 +41,6 @@
 #include <regex.h>
 #include <mailutils/sieve.h>
 
-#define ASSERT(expr, diag, ec)                                                \
- if (!(expr))                                                                 \
-   {                                                                          \
-     if (ec)                                                                  \
-       mu_sieve_error (mach, "%lu: %s: %s",                                   \
-	 	       (unsigned long) mu_sieve_get_message_num (mach),	      \
-		       diag,                                                  \
-		       mu_strerror (ec));                                     \
-     else                                                                     \
-       mu_sieve_error (mach, "%lu: %s",                                       \
-	 	       (unsigned long) mu_sieve_get_message_num (mach),	      \
-		       diag);                                                 \
-     mu_sieve_abort (mach);                                                   \
-   }
-    
-#define ASSERT2(expr, diag, arg, ec)                                          \
- if (!(expr))                                                                 \
-   {                                                                          \
-     if (ec)                                                                  \
-       mu_sieve_error (mach, "%lu: `%s': %s: %s",                             \
-	 	       (unsigned long) mu_sieve_get_message_num (mach),	      \
-		       arg,                                                   \
-		       diag,                                                  \
-		       mu_strerror (ec));                                     \
-     else                                                                     \
-       mu_sieve_error (mach, "%lu: `%s': %s",                                 \
-		       (unsigned long) mu_sieve_get_message_num (mach),	      \
-		       arg,                                                   \
-		       diag);                                                 \
-     mu_sieve_abort (mach);                                                   \
-   }
-
 int
 sieve_action_pipe (mu_sieve_machine_t mach, mu_list_t args, mu_list_t tags)
 {
@@ -81,12 +49,25 @@ sieve_action_pipe (mu_sieve_machine_t mach, mu_list_t args, mu_list_t tags)
   mu_sieve_value_t *val;
   char *cmd;
   mu_stream_t mstr, pstr;
-  char buf[512];
-  size_t n;
   mu_envelope_t env;
-  
+  const char *error_diag = NULL;
+  const char *error_arg = NULL;
+#define ONERR(rc, diag, arg)						\
+  if (rc)								\
+    {									\
+      error_diag = diag;						\
+      error_arg = arg;							\
+      break;								\
+    }
+
   val = mu_sieve_value_get (args, 0);
-  ASSERT (val, _("cannot get command!"), 0);
+  if (!val)
+    {
+      mu_sieve_error (mach, "%lu: %s",
+		      (unsigned long) mu_sieve_get_message_num (mach),
+		      _("cannot get command!"));
+      mu_sieve_abort (mach);
+    }
   cmd = val->v.string;
 
   mu_sieve_log_action (mach, "PIPE", NULL);
@@ -104,49 +85,64 @@ sieve_action_pipe (mu_sieve_machine_t mach, mu_list_t args, mu_list_t tags)
 
   msg = mu_sieve_get_message (mach);
   mu_message_get_envelope (msg, &env);
-  
-  rc = mu_message_get_stream (msg, &mstr);
-  ASSERT (rc == 0, _("cannot get message stream"), rc);
-  
-  rc = mu_prog_stream_create (&pstr, cmd, MU_STREAM_WRITE);
-  ASSERT2 (rc == 0, _("cannot create command stream"), cmd, rc);
 
-  rc = mu_stream_open (pstr);
-  ASSERT2 (rc == 0, _("cannot open command stream"), cmd, rc);
-
-  if (mu_sieve_tag_lookup (tags, "envelope", &val))
+  do
     {
-      char *p;
-
-      rc = mu_envelope_aget_sender (env, &p);
-      ASSERT (rc == 0, _("cannot get envelope sender"), rc);
-      rc = mu_stream_write (pstr, "From ", 5, NULL);
-      ASSERT (rc == 0, _("stream write failed"), rc);
-      mu_stream_write (pstr, p, strlen (p), NULL);
-      free (p);
-      rc = mu_stream_write (pstr, " ", 1, NULL);
-      ASSERT (rc == 0, _("stream write failed"), rc);
-      rc = mu_envelope_aget_date (env, &p);
-      ASSERT (rc == 0, _("cannot get envelope date"), rc);
-      rc = mu_stream_write (pstr, p, strlen (p), NULL);
-      ASSERT (rc == 0, _("stream write failed"), rc);
-      free (p);
-      rc = mu_stream_write (pstr, "\n", 1, NULL);
-      ASSERT (rc == 0, _("stream write failed"), rc);
+      rc = mu_message_get_streamref (msg, &mstr);
+      ONERR (rc, _("cannot get message stream"), NULL);
+
+      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;
+
+	  rc = mu_envelope_aget_sender (env, &p);
+	  ONERR (rc, _("cannot get envelope sender"), NULL);
+	  rc = mu_stream_write (pstr, "From ", 5, NULL);
+	  ONERR (rc, _("stream write failed"), NULL);
+	  mu_stream_write (pstr, p, strlen (p), NULL);
+	  free (p);
+	  rc = mu_stream_write (pstr, " ", 1, NULL);
+	  ONERR (rc, _("stream write failed"), NULL);
+	  rc = mu_envelope_aget_date (env, &p);
+	  ONERR (rc, _("cannot get envelope date"), NULL);
+	  rc = mu_stream_write (pstr, p, strlen (p), NULL);
+	  ONERR (rc, _("stream write failed"), NULL);
+	  free (p);
+	  rc = mu_stream_write (pstr, "\n", 1, NULL);
+	  ONERR (rc, _("stream write failed"), NULL);
+	}
+
+      rc = mu_stream_copy (pstr, mstr, 0);
+      ONERR (rc, _("command failed"), cmd);
     }
-  
-  rc = mu_stream_seek (mstr, 0, SEEK_SET, NULL);
-  while (rc == 0
-	 && mu_stream_read (mstr, buf, sizeof buf, &n) == 0
-	 && n > 0)
-    rc = mu_stream_write (pstr, buf, n, NULL);
+  while (0);
 
+  mu_stream_destroy (&mstr);
   mu_stream_close (pstr);
   mu_stream_destroy (&pstr);
 
+  if (rc)
+    {
+      if (error_arg)
+	mu_sieve_error (mach, "%lu: %s: %s: %s",
+			(unsigned long) mu_sieve_get_message_num (mach),
+			error_diag,
+			error_arg,
+			mu_strerror (rc));
+      else
+	mu_sieve_error (mach, "%lu: %s: %s",
+			(unsigned long) mu_sieve_get_message_num (mach),
+			error_diag,
+			mu_strerror (rc));
+      mu_sieve_abort (mach);
+    }
 
-  ASSERT2 (rc == 0, _("command failed"), cmd, rc);
-  
   return 0;
 }
 
@@ -155,9 +151,9 @@ static mu_sieve_tag_def_t pipe_tags[] = {
   { "envelope", SVT_VOID },
   { NULL }
 };
-  
+
 static mu_sieve_tag_group_t pipe_tag_groups[] = {
-  { pipe_tags, NULL }, 
+  { pipe_tags, NULL },
   { NULL }
 };
 
@@ -173,4 +169,3 @@ SIEVE_EXPORT (pipe, init) (mu_sieve_machine_t mach)
   return mu_sieve_register_action (mach, "pipe", sieve_action_pipe,
 				   pipe_args, pipe_tag_groups, 1);
 }
-
diff --git a/libmu_sieve/extensions/spamd.c b/libmu_sieve/extensions/spamd.c
index e2aef4a..f31809e 100644
--- a/libmu_sieve/extensions/spamd.c
+++ b/libmu_sieve/extensions/spamd.c
@@ -34,6 +34,8 @@
 #include <mailutils/sieve.h>
 #include <mailutils/mu_auth.h>
 #include <mailutils/nls.h>
+#include <mailutils/filter.h>
+#include <mailutils/stream.h>
 
 #define DEFAULT_SPAMD_PORT 783
 
@@ -98,29 +100,28 @@ spamd_send_command (mu_stream_t stream, const char *fmt, ...)
   mu_stream_writeline (stream, buf, n);
 }
 
-static void
+static int
 spamd_send_message (mu_stream_t stream, mu_message_t msg)
 {
-  size_t size;
-  char buf[512];
-  mu_stream_t mstr;
+  int rc;
+  mu_stream_t mstr, flt;
 
-  mu_message_get_stream (msg, &mstr);
-  mu_stream_seek (mstr, 0, SEEK_SET, NULL);
-  while (mu_stream_readline (mstr, buf, sizeof (buf), &size) == 0
-	 && size > 0)
+  rc = mu_message_get_streamref (msg, &mstr);
+  if (rc)
+    return rc;
+  rc = mu_filter_create (&flt, mstr, "rfc822", MU_FILTER_ENCODE,
+			 MU_STREAM_READ|MU_STREAM_SEEK|MU_STREAM_NO_CLOSE);
+  if (rc)
     {
-      char *nl = NULL;
-      
-      if (buf[size-1] == '\n')
-	{
-	  size--;
-	  nl = "\r\n";
-	}
-      mu_stream_write (stream, buf, size, NULL);
-      if (nl)
-	mu_stream_write (stream, nl, 2, NULL);
+      mu_stream_destroy (&mstr);
+      return rc;
     }
+
+  rc = mu_stream_copy (stream, flt, 0);
+
+  mu_stream_destroy (&mstr);
+  mu_stream_destroy (&flt);
+  return rc;
 }
 
 static size_t
@@ -284,6 +285,9 @@ spamd_test (mu_sieve_machine_t mach, mu_list_t args, mu_list_t tags)
 		   (u_long) mu_sieve_get_message_num (mach));
     }
   
+  if (mu_sieve_is_dry_run (mach))
+    return 0;
+  
   if (mu_sieve_tag_lookup (tags, "host", &arg))
     host = arg->v.string;
   else
diff --git a/libmu_sieve/extensions/vacation.c b/libmu_sieve/extensions/vacation.c
index 753c4b9..2b19e23 100644
--- a/libmu_sieve/extensions/vacation.c
+++ b/libmu_sieve/extensions/vacation.c
@@ -42,7 +42,6 @@
 
 /* Build a mime response message from original message MSG. TEXT
    is the message text.
-   FIXME: This is for future use, when I add :mime tag
 */
 static int
 build_mime (mu_sieve_machine_t mach, mu_list_t tags, mu_mime_t *pmime,
@@ -50,25 +49,25 @@ build_mime (mu_sieve_machine_t mach, mu_list_t tags, mu_mime_t *pmime,
 {
   mu_mime_t mime = NULL;
   mu_message_t newmsg;
-  mu_stream_t stream, input, save_input = NULL;
+  mu_stream_t stream, input;
   mu_header_t hdr;
   mu_body_t body;
-  char buf[512];
-  char *header = "Content-Type: text/plain;charset=" MU_SIEVE_CHARSET "\n"
- 	       "Content-Transfer-Encoding: 8bit\n\n";
+  const char *header =
+    "Content-Type: text/plain;charset=" MU_SIEVE_CHARSET "\n"
+    "Content-Transfer-Encoding: 8bit\n\n";
   int rc;
-  size_t n;
   
   mu_mime_create (&mime, NULL, 0);
   mu_message_create (&newmsg, NULL);
   mu_message_get_body (newmsg, &body);
-  mu_body_get_stream (body, &stream);
 
   if ((rc = mu_memory_stream_create (&input, MU_STREAM_RDWR)))
     {
       mu_sieve_error (mach,
 		      _("cannot create temporary stream: %s"),
 		      mu_strerror (rc));
+      mu_mime_destroy (&mime);
+      mu_message_destroy (&newmsg, NULL);
       return 1;
     }
 
@@ -77,6 +76,9 @@ build_mime (mu_sieve_machine_t mach, mu_list_t tags, mu_mime_t *pmime,
       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;
     }
   
@@ -91,19 +93,37 @@ build_mime (mu_sieve_machine_t mach, mu_list_t tags, mu_mime_t *pmime,
 	{
 	  header = "Content-Type: text/plain;charset=" MU_SIEVE_CHARSET "\n"
 	           "Content-Transfer-Encoding: base64\n\n";
-	  save_input = input;
 	  input = fstr;
 	}
     }
 
-  while (rc == 0
-	 && mu_stream_read (input, buf, sizeof buf, &n) == 0
-	 && n > 0)
-    rc = mu_stream_write (stream, buf, n, NULL);
+  rc = mu_body_get_streamref (body, &stream);
+  if (rc)
+    {
+      mu_sieve_error (mach,
+		      _("cannot get input body stream: %s"),
+		      mu_strerror (rc));
+      mu_mime_destroy (&mime);
+      mu_message_destroy (&newmsg, NULL);
+      mu_stream_destroy (&input);
+      return 1;
+    }
+
+  mu_stream_seek (input, 0, MU_SEEK_SET, NULL);
+  rc = mu_stream_copy (stream, input, 0);
+  if (rc)
+    {
+      mu_sieve_error (mach,
+		      _("stream copy failed: %s"),
+		      mu_strerror (rc));
+      mu_mime_destroy (&mime);
+      mu_message_destroy (&newmsg, NULL);
+      mu_stream_destroy (&input);
+      mu_stream_destroy (&stream);
+      return 1;
+    }
 
   mu_stream_destroy (&input);
-  if (save_input)
-    mu_stream_destroy (&save_input);
   
   mu_header_create (&hdr, header, strlen (header));
   mu_message_set_header (newmsg, hdr, NULL);
diff --git a/libproto/mbox/mbox.c b/libproto/mbox/mbox.c
index c81701e..51da14e 100644
--- a/libproto/mbox/mbox.c
+++ b/libproto/mbox/mbox.c
@@ -382,7 +382,8 @@ mbox_envelope_date (mu_envelope_t envelope, char *buf, size_t len,
 			       &n);
   if (status)
     return status;
-
+  mu_rtrim_cset (buffer, "\r\n");
+  
   /* Format:  "From [sender] [date]" */
   /* strlen ("From ") == 5 */
   if (n > 5 && (s = strchr (buffer + 5, ' ')) != NULL)
diff --git a/mailbox/amd.c b/mailbox/amd.c
index 450c7c0..6d1d855 100644
--- a/mailbox/amd.c
+++ b/mailbox/amd.c
@@ -1710,7 +1710,7 @@ amd_body_stream_seek (mu_stream_t str, mu_off_t off, mu_off_t *presult)
   
   amd_body_size (amdstr->body, &size);
 
-  if (off < 0 || off >= size)
+  if (off < 0 || off > size)
     return ESPIPE;
 
   amdstr->off = off;


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Sun Aug 29 17:09:32 2010
Received: from mailman by lists.gnu.org with archive (Exim 4.43)
	id 1Opp88-00064y-HI
	for mharc-commit-mailutils@gnu.org; Sun, 29 Aug 2010 17:09:32 -0400
Received: from [140.186.70.92] (port=34571 helo=eggs.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.43) id 1Opp84-000632-9J
	for commit-mailutils@gnu.org; Sun, 29 Aug 2010 17:09:30 -0400
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1Opp81-0000LV-Jv
	for commit-mailutils@gnu.org; Sun, 29 Aug 2010 17:09:28 -0400
Received: from colonialone.fsf.org ([140.186.70.51]:33153
	helo=internal.in.savannah.gnu.org)
	by eggs.gnu.org with esmtp (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1Opp81-0000LP-DW
	for commit-mailutils@gnu.org; Sun, 29 Aug 2010 17:09:25 -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 1Opp80-0007xh-Vb
	for commit-mailutils@gnu.org; Sun, 29 Aug 2010 21:09:25 +0000
Received: from gray by vcs-noshell.in.savannah.gnu.org with local (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1Opp7y-0005rZ-Ur
	for commit-mailutils@gnu.org; Sun, 29 Aug 2010 21:09:24 +0000
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
X-Git-Refname: refs/heads/stream-cleanup
X-Git-Reftype: branch
X-Git-Oldrev: f1625c353026c160c8db18ac9a4bd3bd35df150c
X-Git-Newrev: 286657e78705d061fc68c000cd3823608c522c04
Message-Id: <E1Opp7y-0005rZ-Ur@vcs-noshell.in.savannah.gnu.org>
Date: Sun, 29 Aug 2010 21:09:23 +0000
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
Subject: [SCM] GNU Mailutils branch, stream-cleanup,
	updated. rel-2_1-111-g286657e
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, 29 Aug 2010 21:09:31 -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=286657e78705d061fc68c000cd3823608c522c04

The branch, stream-cleanup has been updated
       via  286657e78705d061fc68c000cd3823608c522c04 (commit)
       via  f4e67b5e949b324617b7c737d63455f0c0da1371 (commit)
      from  f1625c353026c160c8db18ac9a4bd3bd35df150c (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 286657e78705d061fc68c000cd3823608c522c04
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Mon Aug 30 00:08:31 2010 +0300

    Re-implement server TLS support.
    
    STLS in pop3d is already working.
    
    * include/mailutils/sys/tls-stream.h: New header.
    * include/mailutils/tls.h (mu_tls_stream_create)
    (mu_tls_stream_create_client)
    (mu_tls_stream_create_client_from_tcp): Remove.
    (mu_tls_server_stream_create, mu_tls_client_stream_create): New
    protos.
    * libmu_auth/tls.c: Rewrite.
    
    * imap4d/util.c: Use mu_tls_server_stream_create.
    * libproto/pop/mbox.c: Use mu_tls_server_stream_create/
    mu_tls_client_stream_create.
    * libproto/pop/pop3_stls.c: Use mu_tls_client_stream_create.
    * libproto/imap/folder.c: Use mu_tls_client_stream_create.
    
    * pop3d/capa.c (pop3d_capa): CAPA is allowed in both states.
    * pop3d/extra.c (pop3d_setio): Rewrite.

commit f4e67b5e949b324617b7c737d63455f0c0da1371
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Sun Aug 29 18:01:27 2010 +0300

    amd, pop3d: bugfixes.
    
    * mailbox/amd.c (amd_body_stream_readdelim): Fix the logic.
    * mailbox/message.c (_message_stream_seek): Fix seeks in
    backward direction.
    * pop3d/top.c: Fix output of the body.

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

Summary of changes:
 imap4d/util.c                                      |    2 +-
 include/mailutils/sys/Makefile.am                  |    1 +
 .../sys/{message_stream.h => tls-stream.h}         |   53 ++-
 include/mailutils/tls.h                            |   11 +-
 libmu_auth/tls.c                                   |  541 +++++++++++---------
 libproto/imap/folder.c                             |    6 +-
 libproto/pop/mbox.c                                |   14 +-
 libproto/pop/pop3_stls.c                           |    4 +-
 mailbox/amd.c                                      |   36 +-
 mailbox/message.c                                  |   12 +-
 pop3d/capa.c                                       |    3 -
 pop3d/extra.c                                      |   28 +-
 pop3d/top.c                                        |    6 +-
 13 files changed, 393 insertions(+), 324 deletions(-)
 copy include/mailutils/sys/{message_stream.h => tls-stream.h} (53%)

diff --git a/imap4d/util.c b/imap4d/util.c
index 26a1a8d..2947823 100644
--- a/imap4d/util.c
+++ b/imap4d/util.c
@@ -893,7 +893,7 @@ imap4d_init_tls_server ()
   mu_stream_t stream;
   int rc;
  
-  rc = mu_tls_stream_create (&stream, istream, ostream, 0);
+  rc = mu_tls_server_stream_create (&stream, istream, ostream, 0);
   if (rc)
     return 0;
 
diff --git a/include/mailutils/sys/Makefile.am b/include/mailutils/sys/Makefile.am
index 79d94cd..34f995b 100644
--- a/include/mailutils/sys/Makefile.am
+++ b/include/mailutils/sys/Makefile.am
@@ -34,5 +34,6 @@ sysinclude_HEADERS = \
  streamref.h\
  streamtrans.h\
  stream.h\
+ tls-stream.h\
  pop3.h\
  nntp.h
diff --git a/include/mailutils/sys/message_stream.h b/include/mailutils/sys/tls-stream.h
similarity index 53%
copy from include/mailutils/sys/message_stream.h
copy to include/mailutils/sys/tls-stream.h
index 381e7c9..8a903b9 100644
--- a/include/mailutils/sys/message_stream.h
+++ b/include/mailutils/sys/tls-stream.h
@@ -1,5 +1,5 @@
 /* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 2009 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 License as published by
@@ -14,35 +14,44 @@
    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/>. */
 
-#ifndef _MAILUTILS_SYS_MESSAGE_STREAM_H
-#define _MAILUTILS_SYS_MESSAGE_STREAM_H
+#ifndef _MAILUTILS_SYS_TLS_STREAM_H
+# define _MAILUTILS_SYS_TLS_STREAM_H
 
-#include <mailutils/sys/stream.h>
+# include <mailutils/types.h>
+# include <mailutils/stream.h>
+# include <mailutils/sys/stream.h>
 
-struct _mu_message_stream
+enum _mu_tls_stream_state
+  {
+    state_init,
+    state_open,
+    state_closed,
+    state_destroyed
+  };
+
+struct _mu_tls_io_stream
 {
   struct _mu_stream stream;
-  mu_stream_t transport;  /* Actual stream */
-  mu_off_t offset;
-
-  char *envelope;
-  size_t envelope_length; 
-  char *from;
-  char *date;
-  size_t mark_offset;  /* Offset of the header separator */
-  size_t mark_length;  /* Length of the header separator (not counting the
-			  newline) */
-  mu_off_t body_start;
-  mu_off_t body_end;
+  mu_stream_t transport;
+  struct _mu_tls_stream *up;
 };
 
-struct _mu_body_stream
+struct _mu_tls_stream
 {
   struct _mu_stream stream;
-  mu_off_t offset;
-  struct _mu_message_stream *message_stream;
+  enum _mu_tls_stream_state state;
+  gnutls_session session;
+  int tls_err;
+  mu_stream_t transport[2];
 };
 
-#endif
-
+struct _mu_tls_stream_s
+{
+  struct _mu_stream stream;
+  enum _mu_tls_stream_state state;
+  gnutls_session session;
+  int tls_err;
+  mu_stream_t transport;
+};
 
+#endif
diff --git a/include/mailutils/tls.h b/include/mailutils/tls.h
index 9848f82..856e474 100644
--- a/include/mailutils/tls.h
+++ b/include/mailutils/tls.h
@@ -36,15 +36,12 @@ struct mu_tls_module_config
 
 extern int mu_tls_module_init (enum mu_gocs_op, void *);
 
-extern int mu_tls_stream_create (mu_stream_t *stream, 
-				 mu_stream_t strin, mu_stream_t strout,
-				 int flags);
-extern int mu_tls_stream_create_client (mu_stream_t *stream,
+extern int mu_tls_server_stream_create (mu_stream_t *stream, 
+			 	        mu_stream_t strin, mu_stream_t strout,
+				        int flags);
+extern int mu_tls_client_stream_create (mu_stream_t *stream,
 					mu_stream_t strin, mu_stream_t strout,
 					int flags);
-extern int mu_tls_stream_create_client_from_tcp (mu_stream_t *stream,
-						 mu_stream_t tcp_str,
-						 int flags);
 
 extern int mu_check_tls_environment (void);
 extern int mu_init_tls_libs (void);
diff --git a/libmu_auth/tls.c b/libmu_auth/tls.c
index 5ce9bc7..7dff478 100644
--- a/libmu_auth/tls.c
+++ b/libmu_auth/tls.c
@@ -35,8 +35,6 @@
 #include <mailutils/stream.h>
 #include <mailutils/errno.h>
 
-#include <lbuf.h>
-
 struct mu_tls_module_config mu_tls_module_config = { 1, NULL, NULL, NULL };
   
 int
@@ -48,7 +46,7 @@ mu_tls_module_init (enum mu_gocs_op op, void *data)
       if (data)
 	memcpy (&mu_tls_module_config, data, sizeof mu_tls_module_config);
       break;
-
+      
     case mu_gocs_op_flush:
 #ifdef WITH_TLS
       mu_init_tls_libs ();
@@ -61,6 +59,7 @@ mu_tls_module_init (enum mu_gocs_op op, void *data)
 #ifdef WITH_TLS
 
 #include <gnutls/gnutls.h>
+#include <mailutils/sys/tls-stream.h>
 
 #define DH_BITS 768
 
@@ -170,7 +169,7 @@ mu_tls_begin (void *iodata,
   for (i = 0; keywords[i]; i++)
     {
       switch (i)
-      {
+	{
         case 0:
           /*
            *  Send STLS/STARTTLS
@@ -190,10 +189,10 @@ mu_tls_begin (void *iodata,
 	    }
 
           stream_ctl (iodata, &oldstr, NULL);
-          status = mu_tls_stream_create_client_from_tcp (&newstr, oldstr, 0);
+          status = mu_tls_client_stream_create (&newstr, oldstr, oldstr, 0);
           if (status != 0)
 	    {
-	      mu_error ("mu_tls_begin: mu_tls_stream_create_client_from_tcp (0): %s",
+	      mu_error ("mu_tls_begin: mu_tls_client_stream_create(0): %s",
 			mu_strerror (status));
 	      return status;
 	    }
@@ -228,124 +227,67 @@ mu_tls_begin (void *iodata,
 	    }
           break;
           
-      default:
-	return 1;
-      }
+	default:
+	  return 1;
+	}
     }
   return 0;
 }
-
+
 /* ************************* TLS Stream Support **************************** */
 
-enum tls_stream_state {
-  state_init,
-  state_open,
-  state_closed,
-  state_destroyed
-};
-
-struct _tls_stream {
-  mu_stream_t strin;  /* Input stream */
-  mu_stream_t strout; /* Output stream */ 
-  int last_err;
-  struct _line_buffer *lb;
-  enum tls_stream_state state;
-  gnutls_session session;
-};
-
-
-static void
-_tls_destroy (mu_stream_t stream)
+static int
+_tls_io_close (mu_stream_t stream)
 {
-  struct _tls_stream *s = mu_stream_get_owner (stream);
-  int flags;
+  struct _mu_tls_io_stream *sp = (struct _mu_tls_io_stream *) stream;
   
-  if (x509_cred)
-    gnutls_certificate_free_credentials (x509_cred);
-  if (s->session && s->state == state_closed)
-    {
-      gnutls_deinit (s->session);
-      s->state = state_destroyed;
-    }
-  _auth_lb_destroy (&s->lb);
+  if (!(sp->stream.flags & MU_STREAM_NO_CLOSE))
+    return mu_stream_close (sp->transport);
+  return 0;
+}
 
-  mu_stream_get_flags (stream, &flags);
-  if (!(flags & MU_STREAM_NO_CLOSE))
-    {
-      int same_stream = s->strin == s->strout;
-      mu_stream_destroy (&s->strin, mu_stream_get_owner (s->strin));
-      if (!same_stream)
-	mu_stream_destroy (&s->strout, mu_stream_get_owner (s->strout));
-    }
-  free (s);
+static void
+_tls_io_done (struct _mu_stream *stream)
+{
+  struct _mu_tls_io_stream *sp = (struct _mu_tls_io_stream *) stream;
+  if (!(sp->stream.flags & MU_STREAM_NO_CLOSE))
+    mu_stream_unref (sp->transport);
 }
-    
+
 static int
-_tls_read (mu_stream_t stream, char *optr, size_t osize,
-	   mu_off_t offset, size_t *nbytes)
+_tls_io_flush (struct _mu_stream *stream)
 {
-  struct _tls_stream *s = mu_stream_get_owner (stream);
-  int rc;
-  
-  if (!stream || s->state != state_open)
-    return EINVAL;
-  rc = gnutls_record_recv (s->session, optr, osize);
-  if (rc >= 0)
-    {
-      *nbytes = rc;
-      return 0;
-    }
-  s->last_err = rc;
-  return EIO;
+  struct _mu_tls_io_stream *sp = (struct _mu_tls_io_stream *) stream;
+  return mu_stream_flush (sp->transport);
 }
 
 static int
-_tls_readline (mu_stream_t stream, char *optr, size_t osize,
-		mu_off_t offset, size_t *nbytes)
+_tls_io_read (struct _mu_stream *stream, char *buf, size_t bufsize,
+	      size_t *pnread)
 {
-  struct _tls_stream *s = mu_stream_get_owner (stream);
+  struct _mu_tls_io_stream *sp = (struct _mu_tls_io_stream *) stream;
   int rc;
-  char *ptr;
-  size_t rdsize;
   
-  if (!stream || s->state != state_open || osize < 2)
+  if (sp->up->state != state_open)
     return EINVAL;
-
-  if (_auth_lb_level (s->lb) == 0)
+  rc = gnutls_record_recv (sp->up->session, buf, bufsize);
+  if (rc >= 0)
     {
-      ptr = optr;
-      rdsize = 0;
-      do
-	{
-	  rc = gnutls_record_recv (s->session, ptr + rdsize, osize - rdsize);
-	  if (rc < 0)
-	    {
-	      s->last_err = rc;
-	      return EIO;
-	    }
-	  rdsize += rc;
-	}
-      while (osize > rdsize && rc > 0 && ptr[rdsize-1] != '\n');
-      
-      _auth_lb_grow (s->lb, ptr, rdsize);
+      *pnread = rc;
+      return 0;
     }
-  
-  osize--; /* Allow for terminating zero */
-  rdsize = _auth_lb_readline (s->lb, optr, osize);
-  optr[rdsize] = 0;
-  if (nbytes)
-    *nbytes = rdsize;
-  return 0;
+  sp->up->tls_err = rc;
+  return EIO;
 }
 
 static int
-_tls_write (mu_stream_t stream, const char *iptr, size_t isize,
-	    mu_off_t offset, size_t *nbytes)
+_tls_io_write (struct _mu_stream *stream, const char *buf, size_t bufsize,
+	    size_t *pnwrite)
 {
-  struct _tls_stream *s = mu_stream_get_owner (stream);
+  struct _mu_tls_io_stream *sp = (struct _mu_tls_io_stream *) stream;
   int rc;
   
-  if (!stream || s->state != state_open)
+  if (sp->up->state != state_open)
     return EINVAL;
 
   /* gnutls_record_send() docs say:
@@ -356,53 +298,103 @@ _tls_write (mu_stream_t stream, const char *iptr, size_t isize,
        corrupted and the connection will be terminated. */
     
   do
-    rc = gnutls_record_send (s->session, iptr, isize);
+    rc = gnutls_record_send (sp->up->session, buf, bufsize);
   while (rc == GNUTLS_E_INTERRUPTED || rc == GNUTLS_E_AGAIN);
 
   if (rc < 0)
     {
-      s->last_err = rc;
+      sp->up->tls_err = rc;
       return EIO;
     }
 
-  if (nbytes)
-    *nbytes = rc;
+  *pnwrite = rc;
 
   return 0;
 }
 
 static int
-_tls_flush (mu_stream_t stream)
+_tls_rd_wait (struct _mu_stream *stream, int *pflags, struct timeval *tvp)
 {
-  struct _tls_stream *s = mu_stream_get_owner (stream);
-  return mu_stream_flush (s->strout);
+  struct _mu_tls_io_stream *sp = (struct _mu_tls_io_stream *) stream;
+  int rc = EINVAL;
+  
+  if (*pflags == MU_STREAM_READY_RD)
+    rc = mu_stream_wait (sp->transport, pflags, tvp);
+  return rc;
 }
 
 static int
-_tls_close (mu_stream_t stream)
+_tls_wr_wait (struct _mu_stream *stream, int *pflags, struct timeval *tvp)
 {
-  struct _tls_stream *s = mu_stream_get_owner (stream);
-  int flags;
+  struct _mu_tls_io_stream *sp = (struct _mu_tls_io_stream *) stream;
+  int rc = EINVAL;
   
-  if (s->session && s->state == state_open)
+  if (*pflags == MU_STREAM_READY_WR)
+    rc = mu_stream_wait (sp->transport, pflags, tvp);
+  return rc;
+}
+
+static int
+_tls_io_ioctl (struct _mu_stream *stream, int op, void *arg)
+{
+  struct _mu_tls_io_stream *sp = (struct _mu_tls_io_stream *) stream;
+  mu_transport_t *ptrans;
+
+  switch (op)
+    {
+    case MU_IOCTL_GET_TRANSPORT:
+      if (!arg)
+	return EINVAL;
+      ptrans = arg;
+      ptrans[0] = (mu_transport_t) sp->transport;
+      ptrans[1] = NULL;
+      break;
+
+    default:
+      return EINVAL;
+    }
+  return 0;
+}
+
+static int
+_mu_tls_io_stream_create (mu_stream_t *pstream,
+			  mu_stream_t transport, int flags,
+			  struct _mu_tls_stream *master)
+{
+  struct _mu_tls_io_stream *sp;
+
+  sp = (struct _mu_tls_io_stream *)
+    _mu_stream_create (sizeof (*sp),
+		       flags & (MU_STREAM_RDWR | MU_STREAM_NO_CLOSE));
+  if (!sp)
+    return ENOMEM;
+
+  if (flags & MU_STREAM_READ)
     {
-      gnutls_bye (s->session, GNUTLS_SHUT_RDWR);
-      s->state = state_closed;
+      sp->stream.read = _tls_io_read; 
+      sp->stream.wait = _tls_rd_wait;
+      mu_stream_set_buffer ((mu_stream_t) sp, mu_buffer_full, 1024);
     }
-  
-  mu_stream_get_flags (stream, &flags);
-  if (!(flags & MU_STREAM_NO_CLOSE))
+  else
     {
-      mu_stream_close (s->strin);
-      if (s->strin != s->strout)
-	mu_stream_close (s->strout);
+      sp->stream.write = _tls_io_write;
+      sp->stream.wait = _tls_wr_wait;
+      mu_stream_set_buffer ((mu_stream_t) sp, mu_buffer_line, 1024);
     }
+  sp->stream.flush = _tls_io_flush;
+  sp->stream.close = _tls_io_close;
+  sp->stream.done = _tls_io_done; 
+  sp->stream.ctl = _tls_io_ioctl;
+  /* FIXME:
+     sp->stream.error_string = _tls_error_string;*/
+
+  sp->transport = transport;
+  sp->up = master;
+  *pstream = (mu_stream_t) sp;
   return 0;
 }
 
 
-/* Push & pull functions */
-
 static ssize_t
 _tls_stream_pull (gnutls_transport_ptr fd, void *buf, size_t size)
 {
@@ -424,7 +416,7 @@ _tls_stream_push (gnutls_transport_ptr fd, const void *buf, size_t size)
   mu_stream_t stream = fd;
   int rc;
 
-  rc = mu_stream_write (stream, buf, size);
+  rc = mu_stream_write (stream, buf, size, &size);
   if (rc)
     {
       mu_error ("_tls_stream_push: %s", mu_strerror (rc)); /* FIXME */
@@ -435,14 +427,14 @@ _tls_stream_push (gnutls_transport_ptr fd, const void *buf, size_t size)
 }
 
 
-
 static int
-_tls_open (mu_stream_t stream)
+_tls_server_open (mu_stream_t stream)
 {
-  struct _tls_stream *s = mu_stream_get_owner (stream);
+  struct _mu_tls_stream *sp = (struct _mu_tls_stream *) stream;
   int rc = 0;
+  mu_transport_t transport[2];
   
-  if (!stream || s->state != state_init)
+  if (!stream || sp->state != state_init)
     return EINVAL;
 
   gnutls_certificate_allocate_credentials (&x509_cred);
@@ -451,40 +443,41 @@ _tls_open (mu_stream_t stream)
     gnutls_certificate_set_x509_trust_file (x509_cred,
 					    mu_tls_module_config.ssl_cafile,
 					    GNUTLS_X509_FMT_PEM);
-
+  
   rc = gnutls_certificate_set_x509_key_file (x509_cred,
 					     mu_tls_module_config.ssl_cert, 
 					     mu_tls_module_config.ssl_key,
 					     GNUTLS_X509_FMT_PEM);
   if (rc < 0)
     {
-      s->last_err = rc;
+      sp->tls_err = rc;
       return EIO;
     }
   
   generate_dh_params ();
   gnutls_certificate_set_dh_params (x509_cred, dh_params);
 
-  s->session = initialize_tls_session ();
-  gnutls_transport_set_ptr2 (s->session,
-			     (gnutls_transport_ptr) s->strin,
-			     (gnutls_transport_ptr) s->strout);
-  gnutls_transport_set_pull_function (s->session, _tls_stream_pull);
-  gnutls_transport_set_push_function (s->session, _tls_stream_push);
+  sp->session = initialize_tls_session ();
+  mu_stream_ioctl (stream, MU_IOCTL_GET_TRANSPORT, transport);
+  gnutls_transport_set_ptr2 (sp->session,
+			     (gnutls_transport_ptr) transport[0],
+			     (gnutls_transport_ptr) transport[1]);
+  gnutls_transport_set_pull_function (sp->session, _tls_stream_pull);
+  gnutls_transport_set_push_function (sp->session, _tls_stream_push);
   
-  rc = gnutls_handshake (s->session);
+  rc = gnutls_handshake (sp->session);
   if (rc < 0)
     {
-      gnutls_deinit (s->session);
-      s->last_err = rc;
+      gnutls_deinit (sp->session);
+      sp->tls_err = rc;
       return EIO;
     }
-  s->state = state_open;
+  sp->state = state_open;
   return 0;
 }
 
 static int
-prepare_client_session (struct _tls_stream *s)
+prepare_client_session (struct _mu_tls_stream *sp)
 {
   int rc;
   static int protocol_priority[] = {GNUTLS_TLS1, GNUTLS_SSL3, 0};
@@ -495,12 +488,12 @@ prepare_client_session (struct _tls_stream *s)
   static int comp_priority[] = {GNUTLS_COMP_NULL, 0};
   static int mac_priority[] = {GNUTLS_MAC_SHA, GNUTLS_MAC_MD5, 0};
 
-  gnutls_init (&s->session, GNUTLS_CLIENT);
-  gnutls_protocol_set_priority (s->session, protocol_priority);
-  gnutls_cipher_set_priority (s->session, cipher_priority);
-  gnutls_compression_set_priority (s->session, comp_priority);
-  gnutls_kx_set_priority (s->session, kx_priority);
-  gnutls_mac_set_priority (s->session, mac_priority);
+  gnutls_init (&sp->session, GNUTLS_CLIENT);
+  gnutls_protocol_set_priority (sp->session, protocol_priority);
+  gnutls_cipher_set_priority (sp->session, cipher_priority);
+  gnutls_compression_set_priority (sp->session, comp_priority);
+  gnutls_kx_set_priority (sp->session, kx_priority);
+  gnutls_mac_set_priority (sp->session, mac_priority);
 
   gnutls_certificate_allocate_credentials (&x509_cred);
   if (mu_tls_module_config.ssl_cafile)
@@ -510,44 +503,44 @@ prepare_client_session (struct _tls_stream *s)
 						   GNUTLS_X509_FMT_PEM);
       if (rc < 0)
 	{
-	  s->last_err = rc;
+	  sp->tls_err = rc;
 	  return -1;
 	}
     }
 
-  gnutls_credentials_set (s->session, GNUTLS_CRD_CERTIFICATE, x509_cred);
+  gnutls_credentials_set (sp->session, GNUTLS_CRD_CERTIFICATE, x509_cred);
 
-  gnutls_transport_set_ptr2 (s->session,
-			     (gnutls_transport_ptr) s->strin,
-			     (gnutls_transport_ptr) s->strout);
-  gnutls_transport_set_pull_function (s->session, _tls_stream_pull);
-  gnutls_transport_set_push_function (s->session, _tls_stream_push);
+  gnutls_transport_set_ptr2 (sp->session,
+			     (gnutls_transport_ptr) sp->transport[0],
+			     (gnutls_transport_ptr) sp->transport[1]);
+  gnutls_transport_set_pull_function (sp->session, _tls_stream_pull);
+  gnutls_transport_set_push_function (sp->session, _tls_stream_push);
       
   return 0;
 }
-  
+
 static int
-_tls_open_client (mu_stream_t stream)
+_tls_client_open (mu_stream_t stream)
 {
-  struct _tls_stream *s = mu_stream_get_owner (stream);
+  struct _mu_tls_stream *sp = (struct _mu_tls_stream *) stream;
   int rc = 0;
   
-  switch (s->state)
+  switch (sp->state)
     {
     case state_closed:
       gnutls_certificate_free_credentials (x509_cred);
-      if (s->session)
-	gnutls_deinit (s->session);
+      if (sp->session)
+	gnutls_deinit (sp->session);
       /* FALLTHROUGH */
       
     case state_init:
-      prepare_client_session (s);
-      rc = gnutls_handshake (s->session);
+      prepare_client_session (sp);
+      rc = gnutls_handshake (sp->session);
       if (rc < 0)
 	{
-	  s->last_err = rc;
-	  gnutls_deinit (s->session);
-	  s->state = state_init;
+	  sp->tls_err = rc;
+	  gnutls_deinit (sp->session);
+	  sp->state = state_init;
 	  return MU_ERR_FAILURE;
 	}
       break;
@@ -557,133 +550,181 @@ _tls_open_client (mu_stream_t stream)
     }
 
   /* FIXME: if (ssl_cafile) verify_certificate (s->session); */
-  s->state = state_open;
+  sp->state = state_open;
   return 0;
 }
 
-int
-_tls_strerror (mu_stream_t stream, const char **pstr)
+static int
+_tls_read (struct _mu_stream *str, char *buf, size_t bufsize,
+	   size_t *pnread)
 {
-  struct _tls_stream *s = mu_stream_get_owner (stream);
-  *pstr = gnutls_strerror (s->last_err);
-  return 0;
+  struct _mu_tls_stream *sp = (struct _mu_tls_stream *)str;
+  return mu_stream_read (sp->transport[0], buf, bufsize, pnread);
 }
 
-int
-_tls_get_transport2 (mu_stream_t stream,
-		     mu_transport_t *pin, mu_transport_t *pout)
+static int
+_tls_write (struct _mu_stream *str, const char *buf, size_t bufsize,
+	    size_t *pnwrite)
 {
-  struct _tls_stream *s = mu_stream_get_owner (stream);
-  *pin = (mu_transport_t) s->strin;
-  *pout = (mu_transport_t) s->strout;
-  return 0;
+  struct _mu_tls_stream *sp = (struct _mu_tls_stream *)str;
+  return mu_stream_write (sp->transport[1], buf, bufsize, pnwrite);
 }
 
-int
-_tls_wait (mu_stream_t stream, int *pflags, struct timeval *tvp)
+static int
+_tls_ioctl (struct _mu_stream *stream, int op, void *arg)
 {
-  struct _tls_stream *s = mu_stream_get_owner (stream);
-  if ((*pflags & (MU_STREAM_READY_RD|MU_STREAM_READY_WR))
-      == (MU_STREAM_READY_RD|MU_STREAM_READY_WR))
-    return EINVAL; /* Sorry, can't wait for both input and output. */
-  if (*pflags & MU_STREAM_READY_RD)
-    return mu_stream_wait (s->strin, pflags, tvp);
-  if (*pflags & MU_STREAM_READY_WR)
-    return mu_stream_wait (s->strout, pflags, tvp);
-  return EINVAL;
-}
+  struct _mu_tls_stream *sp = (struct _mu_tls_stream *) stream;
+  mu_transport_t *ptrans, trans[2];
 
-/* FIXME: if strin == strout sequential reads may intefere with
-   sequential writes (they would share stream->offset). This should
-   be fixed either in stream.c or here. In particular,
-   mu_tls_stream_create_client will malfunction */
-int
-mu_tls_stream_create (mu_stream_t *stream,
-		      mu_stream_t strin, mu_stream_t strout, int flags)
-{
-  struct _tls_stream *s;
-  int rc;
+  switch (op)
+    {
+    case MU_IOCTL_GET_TRANSPORT:
+      if (!arg)
+	return EINVAL;
+      ptrans = arg;
+      mu_stream_ioctl (sp->transport[0], MU_IOCTL_GET_TRANSPORT, trans);
+      ptrans[0] = trans[0];
+      mu_stream_ioctl (sp->transport[1], MU_IOCTL_GET_TRANSPORT, trans);
+      ptrans[1] = trans[0];
+      break;
 
-  if (stream == NULL)
-    return MU_ERR_OUT_PTR_NULL;
+    default:
+      return EINVAL;
+    }
+  return 0;
+}
 
-  s = calloc (1, sizeof (*s));
-  if (s == NULL)
-    return ENOMEM;
+static int
+_tls_wait (struct _mu_stream *stream, int *pflags, struct timeval *tvp)
+{
+  struct _mu_tls_stream *sp = (struct _mu_tls_stream *) stream;
+  int rc = EINVAL;
+  
+  if (*pflags == MU_STREAM_READY_RD)
+    rc = mu_stream_wait (sp->transport[0], pflags, tvp);
+  else if (*pflags == MU_STREAM_READY_WR)
+    rc = mu_stream_wait (sp->transport[1], pflags, tvp);
+  return rc;
+}
 
-  s->strin = strin;
-  s->strout = strout;
+static int
+_tls_flush (struct _mu_stream *stream)
+{
+  struct _mu_tls_stream *sp = (struct _mu_tls_stream *) stream;
+  return mu_stream_flush (sp->transport[1]);
+}
 
-  rc = mu_stream_create (stream, flags|MU_STREAM_NO_CHECK, s);
-  if (rc)
+static int
+_tls_close (mu_stream_t stream)
+{
+  struct _mu_tls_stream *sp = (struct _mu_tls_stream *) stream;
+  
+  if (sp->session && sp->state == state_open)
     {
-      free (s);
-      return rc;
+      gnutls_bye (sp->session, GNUTLS_SHUT_RDWR);
+      sp->state = state_closed;
     }
-
-  mu_stream_set_open (*stream, _tls_open, s);
-  mu_stream_set_close (*stream, _tls_close, s);
-  mu_stream_set_read (*stream, _tls_read, s);
-  mu_stream_set_readline (*stream, _tls_readline, s);
-  mu_stream_set_write (*stream, _tls_write, s);
-  mu_stream_set_flush (*stream, _tls_flush, s);
-  mu_stream_set_destroy (*stream, _tls_destroy, s);
-  mu_stream_set_strerror (*stream, _tls_strerror, s);
-  mu_stream_set_get_transport2 (*stream, _tls_get_transport2, s);
-  mu_stream_set_wait (*stream, _tls_wait, s);
-  _auth_lb_create (&s->lb);
   
-  s->state = state_init;
+  if (!(sp->stream.flags & MU_STREAM_NO_CLOSE))
+    {
+      mu_stream_close (sp->transport[0]);
+      mu_stream_close (sp->transport[1]);
+    }
   return 0;
 }
 
-int
-mu_tls_stream_create_client (mu_stream_t *stream,
-			     mu_stream_t strin, mu_stream_t strout, int flags)
+static void
+_tls_done (struct _mu_stream *stream)
 {
-  struct _tls_stream *s;
-  int rc;
+  struct _mu_tls_stream *sp = (struct _mu_tls_stream *) stream;
+  
+  if (x509_cred)
+    gnutls_certificate_free_credentials (x509_cred);
+  if (sp->session && sp->state == state_closed)
+    {
+      gnutls_deinit (sp->session);
+      sp->state = state_destroyed;
+    }
 
-  if (stream == NULL)
-    return MU_ERR_OUT_PTR_NULL;
+  if (!(sp->stream.flags & MU_STREAM_NO_CLOSE))
+    {
+      mu_stream_unref (sp->transport[0]);
+      mu_stream_unref (sp->transport[1]);
+    }
+}
 
-  s = calloc (1, sizeof (*s));
-  if (s == NULL)
+static int
+_mu_tls_stream_create (mu_stream_t *pstream,
+		       int (*openfn) (mu_stream_t stream),
+		       mu_stream_t strin, mu_stream_t strout, int flags)
+{
+  struct _mu_tls_stream *sp;
+  int noclose = flags & MU_STREAM_NO_CLOSE;
+  int rc;
+  
+  sp = (struct _mu_tls_stream *)
+    _mu_stream_create (sizeof (*sp),
+		       MU_STREAM_RDWR | noclose);
+  if (!sp)
     return ENOMEM;
 
-  s->strin = strin;
-  s->strout = strout;
-  
-  rc = mu_stream_create (stream, flags|MU_STREAM_NO_CHECK, s);
+  sp->stream.read = _tls_read; 
+  sp->stream.write = _tls_write;
+  sp->stream.flush = _tls_flush;
+  sp->stream.open = openfn; 
+  sp->stream.close = _tls_close;
+  sp->stream.done = _tls_done; 
+  sp->stream.ctl = _tls_ioctl;
+  sp->stream.wait = _tls_wait;
+  /* FIXME:
+     sp->stream.error_string = _tls_error_string;*/
+
+  if (!noclose && strin == strout)
+    mu_stream_ref (strin);
+
+  mu_stream_set_buffer (strin, mu_buffer_none, 0);
+  mu_stream_set_buffer (strout, mu_buffer_none, 0);
+  rc = _mu_tls_io_stream_create (&sp->transport[0], strin,
+				 MU_STREAM_READ | noclose, sp);
   if (rc)
     {
-      free (s);
+      free (sp);
+      return rc;
+    }
+      
+  rc = _mu_tls_io_stream_create (&sp->transport[1], strout,
+				 MU_STREAM_WRITE | noclose, sp);
+  if (rc)
+    {
+      free (sp);
+      free (sp->transport[0]);
       return rc;
     }
-
-  mu_stream_set_open (*stream, _tls_open_client, s);
-  mu_stream_set_close (*stream, _tls_close, s);
-  mu_stream_set_read (*stream, _tls_read, s);
-  mu_stream_set_readline (*stream, _tls_readline, s);
-  mu_stream_set_write (*stream, _tls_write, s);
-  mu_stream_set_flush (*stream, _tls_flush, s);
-  mu_stream_set_destroy (*stream, _tls_destroy, s);
-  mu_stream_set_strerror (*stream, _tls_strerror, s);
-  mu_stream_set_get_transport2 (*stream, _tls_get_transport2, s);
-  mu_stream_set_wait (*stream, _tls_wait, s);
-  _auth_lb_create (&s->lb);
   
-  s->state = state_init;
+  mu_stream_set_buffer ((mu_stream_t) sp, mu_buffer_line, 1024);
+  *pstream = (mu_stream_t) sp;
   return 0;
 }
 
 int
-mu_tls_stream_create_client_from_tcp (mu_stream_t *stream, mu_stream_t tcp_str,
-				      int flags)
+mu_tls_server_stream_create (mu_stream_t *pstream,
+			     mu_stream_t strin, mu_stream_t strout, int flags)
+{
+  return _mu_tls_stream_create (pstream,
+				_tls_server_open,
+				strin, strout, flags);
+}
+
+int
+mu_tls_client_stream_create (mu_stream_t *pstream,
+			     mu_stream_t strin, mu_stream_t strout, int flags)
 {
-  return mu_tls_stream_create_client (stream, tcp_str, tcp_str, flags);
+  return _mu_tls_stream_create (pstream,
+				_tls_client_open,
+				strin, strout, flags);
 }
 
+
 #endif /* WITH_TLS */
 
 /* EOF */
diff --git a/libproto/imap/folder.c b/libproto/imap/folder.c
index 77e7919..e974ced 100644
--- a/libproto/imap/folder.c
+++ b/libproto/imap/folder.c
@@ -688,10 +688,12 @@ folder_imap_open (mu_folder_t folder, int flags)
 	      CHECK_EAGAIN (f_imap, status);
 	      CHECK_ERROR_CLOSE (folder, f_imap, status);
 
-	      status = mu_tls_stream_create_client_from_tcp (&newstr, folder->stream, 0);
+	      status = mu_tls_client_stream_create (&newstr, 
+	                                            folder->stream,
+	                                            folder->stream, 0);
 	      if (status != 0)
 		{
-		  mu_error ("folder_imap_open: mu_tls_stream_create_client_from_tcp: %s",
+		  mu_error ("folder_imap_open: mu_tls_client_stream_create: %s",
 			    mu_strerror (status));
 		  return status;
 		}
diff --git a/libproto/pop/mbox.c b/libproto/pop/mbox.c
index a9e28d5..b9a21d6 100644
--- a/libproto/pop/mbox.c
+++ b/libproto/pop/mbox.c
@@ -824,8 +824,11 @@ pop_open (mu_mailbox_t mbox, int flags)
       /* Create the networking stack.  */
       if (mbox->stream == NULL)
 	{
-	  status = mu_tcp_stream_create (&mbox->stream, host, port, mbox->flags);
+	  status = mu_tcp_stream_create (&mbox->stream, host, port,
+					 mbox->flags);
 	  CHECK_ERROR (mpd, status);
+	  /* FIXME: How to configure the buffer size? */
+	  mu_stream_set_buffer (mbox->stream, mu_buffer_line, 1024);
 
 #ifdef WITH_TLS
 	  if (mpd->pops)
@@ -836,19 +839,18 @@ pop_open (mu_mailbox_t mbox, int flags)
 	      CHECK_EAGAIN (mpd, status);
 	      CHECK_ERROR_CLOSE (mbox, mpd, status);
 
-	      status = mu_tls_stream_create_client_from_tcp (&newstr, mbox->stream, 0);
+	      status = mu_tls_client_stream_create (&newstr, 
+	                                            mbox->stream, 
+	                                            mbox->stream, 0);
 	      if (status != 0)
 		{
-		  mu_error ("pop_open: mu_tls_stream_create_client_from_tcp: %s",
+		  mu_error ("pop_open: mu_tls_client_stream_create: %s",
 			    mu_strerror (status));
 		  return status;
 		}
 	      mbox->stream = newstr;
 	    }
 #endif /* WITH_TLS */
-
-	  /* Using the awkward mu_stream_t buffering.  */
-	  mu_stream_setbufsiz (mbox->stream, BUFSIZ);
 	}
       else
 	{
diff --git a/libproto/pop/pop3_stls.c b/libproto/pop/pop3_stls.c
index b9984f6..2c09222 100644
--- a/libproto/pop/pop3_stls.c
+++ b/libproto/pop/pop3_stls.c
@@ -66,7 +66,9 @@ mu_pop3_stls (mu_pop3_t pop3)
         MU_POP3_CHECK_EAGAIN (pop3, status);
         mu_pop3_debug_ack (pop3);
         MU_POP3_CHECK_OK (pop3);
-        status = mu_tls_stream_create_client_from_tcp (&tls_stream, pop3->carrier, 0);
+        status = mu_tls_client_stream_create (&tls_stream, 
+                                              pop3->carrier, 
+                                              pop3->carrier, 0);
         MU_POP3_CHECK_ERROR (pop3, status);
         pop3->carrier = tls_stream; 
         pop3->state = MU_POP3_STLS_CONNECT;
diff --git a/mailbox/amd.c b/mailbox/amd.c
index 6d1d855..2b88ffc 100644
--- a/mailbox/amd.c
+++ b/mailbox/amd.c
@@ -1648,7 +1648,6 @@ amd_body_stream_readdelim (mu_stream_t is, char *buffer, size_t buflen,
   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;
 
   amd_pool_open (mhm);
@@ -1656,7 +1655,7 @@ amd_body_stream_readdelim (mu_stream_t is, char *buffer, size_t buflen,
   if (buffer == NULL || buflen == 0)
     {
       if (pnread)
-	*pnread = nread;
+	*pnread = 0;
       return 0;
     }
 
@@ -1669,27 +1668,22 @@ amd_body_stream_readdelim (mu_stream_t is, char *buffer, size_t buflen,
 
   status = mu_stream_seek (mhm->stream, mhm->body_start + amdstr->off,
 			   MU_SEEK_SET, NULL);
-  if (status)
+  if (status == 0)
     {
-      buflen--;
-      while (buflen)
-	{
-	  size_t ln, rdsize;
+      size_t nread = 0;
+      size_t ln;
 	  
-	  ln = mhm->body_end - (mhm->body_start + amdstr->off);
-	  if (ln > 0)
-	    {
-	      rdsize = ((size_t)ln < buflen) ? (size_t)ln : buflen;
-	      status = mu_stream_readdelim (mhm->stream, buffer, rdsize,
-					    delim, &rdsize);
-	      amdstr->off += nread;
-	      nread += rdsize;
-	      if (status)
-		break;
-	      buflen -= rdsize;
-	      buffer += rdsize;
-	    }
+      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 += rdsize;
 	}
+
+      if (pnread)
+	*pnread = nread;
     }
 
   mu_monitor_unlock (mhm->amd->mailbox->monitor);
@@ -1697,8 +1691,6 @@ amd_body_stream_readdelim (mu_stream_t is, char *buffer, size_t buflen,
   pthread_cleanup_pop (0);
 #endif
 
-  if (pnread)
-    *pnread = nread;
   return status;
 }
 
diff --git a/mailbox/message.c b/mailbox/message.c
index 1d95c71..8ac1258 100644
--- a/mailbox/message.c
+++ b/mailbox/message.c
@@ -182,7 +182,17 @@ _message_stream_seek (struct _mu_stream *str, mu_off_t off, mu_off_t *ppos)
 	return rc;
       /* fall through */
     case _mss_body:
-      off -= hsize;
+      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:
diff --git a/pop3d/capa.c b/pop3d/capa.c
index ca58512..38d5fed 100644
--- a/pop3d/capa.c
+++ b/pop3d/capa.c
@@ -32,9 +32,6 @@ pop3d_capa (char *arg)
   if (strlen (arg) != 0)
     return ERR_BAD_ARGS;
 
-  if (state != initial_state && state != TRANSACTION)
-    return ERR_WRONG_STATE;
-
   pop3d_outf ("+OK Capability list follows\n");
   pop3d_outf ("TOP\n");
   pop3d_outf ("USER\n");
diff --git a/pop3d/extra.c b/pop3d/extra.c
index ca5089e..2ab0f8a 100644
--- a/pop3d/extra.c
+++ b/pop3d/extra.c
@@ -126,6 +126,14 @@ pop3d_abquit (int reason)
   exit (code);
 }
 
+/* Keeps the *real* output stream.  Ostream is a RFC822 filter built over
+   real_ostream, or even over a TLS stream, which in turn is based on this
+   real_ostream.
+   FIXME: This is sorta kludge: we could use MU_IOCTL_GET_TRANSPORT call
+   to retrieve the bottom-level stream, if filter streams supported it.
+*/
+static mu_stream_t real_ostream;
+
 void
 pop3d_setio (FILE *in, FILE *out)
 {
@@ -136,14 +144,17 @@ pop3d_setio (FILE *in, FILE *out)
   if (!out)
     pop3d_abquit (ERR_NO_OFILE);
 
-  if (mu_stdio_stream_create (&istream, fileno (in), MU_STREAM_NO_CLOSE))
+  if (mu_stdio_stream_create (&istream, fileno (in),
+			      MU_STREAM_READ | MU_STREAM_NO_CLOSE))
     pop3d_abquit (ERR_NO_IFILE);
   mu_stream_set_buffer (istream, mu_buffer_line, 1024);
   
-  if (mu_stdio_stream_create (&str, fileno (out), MU_STREAM_NO_CLOSE))
+  if (mu_stdio_stream_create (&str, fileno (out),
+			      MU_STREAM_WRITE | MU_STREAM_NO_CLOSE))
     pop3d_abquit (ERR_NO_OFILE);
+  real_ostream = str;
   if (mu_filter_create (&ostream, str, "rfc822", MU_FILTER_ENCODE,
-			MU_STREAM_WRITE|MU_STREAM_NO_CLOSE))
+			MU_STREAM_WRITE | MU_STREAM_NO_CLOSE))
     pop3d_abquit (ERR_NO_IFILE);
   mu_stream_set_buffer (ostream, mu_buffer_line, 1024);
 }
@@ -154,8 +165,8 @@ pop3d_init_tls_server ()
 {
   mu_stream_t stream;
   int rc;
- 
-  rc = mu_tls_stream_create (&stream, istream, ostream, 0);
+
+  rc = mu_tls_server_stream_create (&stream, istream, real_ostream, 0);
   if (rc)
     return 0;
 
@@ -167,7 +178,12 @@ pop3d_init_tls_server ()
       return 0;
     }
   
-  istream = ostream = stream;
+  istream = stream;
+  mu_stream_destroy (&ostream);
+  if (mu_filter_create (&ostream, stream, "rfc822", MU_FILTER_ENCODE,
+			MU_STREAM_WRITE | MU_STREAM_NO_CLOSE))
+    pop3d_abquit (ERR_NO_IFILE);
+  mu_stream_set_buffer (ostream, mu_buffer_line, 1024);
   return 1;
 }
 #endif
diff --git a/pop3d/top.c b/pop3d/top.c
index bab0583..73d6a27 100644
--- a/pop3d/top.c
+++ b/pop3d/top.c
@@ -71,13 +71,13 @@ pop3d_top (char *arg)
     {
       char *buf = NULL;
       size_t size = 0, n;
-      while (lines > 0 &&
+      for (; lines > 0 &&
 	     mu_stream_getline (stream, &buf, &size, &n) == 0 &&
-	     n > 0)
+	     n > 0; lines--)
 	{
 	  if (buf[0] == '.')
 	    pop3d_outf (".");
-	  pop3d_outf ("%s\n", buf);
+	  pop3d_outf ("%s", buf);
 	}
       mu_stream_destroy (&stream);
       free (buf);


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Mon Aug 30 03:57:58 2010
Received: from mailman by lists.gnu.org with archive (Exim 4.43)
	id 1OpzFe-00058b-8U
	for mharc-commit-mailutils@gnu.org; Mon, 30 Aug 2010 03:57:58 -0400
Received: from [140.186.70.92] (port=59689 helo=eggs.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.43) id 1OpzFZ-00055g-7I
	for commit-mailutils@gnu.org; Mon, 30 Aug 2010 03:57:56 -0400
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1OpzFW-0007eK-Ck
	for commit-mailutils@gnu.org; Mon, 30 Aug 2010 03:57:53 -0400
Received: from colonialone.fsf.org ([140.186.70.51]:57188
	helo=internal.in.savannah.gnu.org)
	by eggs.gnu.org with esmtp (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1OpzFW-0007eC-AP
	for commit-mailutils@gnu.org; Mon, 30 Aug 2010 03:57: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 1OpzFV-0007Vi-Sm
	for commit-mailutils@gnu.org; Mon, 30 Aug 2010 07:57:49 +0000
Received: from gray by vcs-noshell.in.savannah.gnu.org with local (Exim 4.69)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1OpzFT-0006KX-MO
	for commit-mailutils@gnu.org; Mon, 30 Aug 2010 07:57:49 +0000
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
X-Git-Refname: refs/heads/stream-cleanup
X-Git-Reftype: branch
X-Git-Oldrev: 286657e78705d061fc68c000cd3823608c522c04
X-Git-Newrev: a783f8c35ed0fc9a7a18dc82f43bbe34a37aae37
Message-Id: <E1OpzFT-0006KX-MO@vcs-noshell.in.savannah.gnu.org>
Date: Mon, 30 Aug 2010 07:57:48 +0000
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
Subject: [SCM] GNU Mailutils branch, stream-cleanup,
	updated. rel-2_1-112-ga783f8c
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, 30 Aug 2010 07:57: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=a783f8c35ed0fc9a7a18dc82f43bbe34a37aae37

The branch, stream-cleanup has been updated
       via  a783f8c35ed0fc9a7a18dc82f43bbe34a37aae37 (commit)
      from  286657e78705d061fc68c000cd3823608c522c04 (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 a783f8c35ed0fc9a7a18dc82f43bbe34a37aae37
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Mon Aug 30 10:48:54 2010 +0300

    Add a framework for printing I/O transcripts; modify pop3d to use it.
    
    * include/mailutils/stream.h (MU_STREAM_RDTHRU)
    (MU_STREAM_WRTHRU,MU_IOCTL_SET_TRANSPORT): New flags.
    (mu_xscript_stream_create, mu_iostream_create)
    (mu_dbgstream_create): New prototypes.
    * include/mailutils/sys/dbgstream.h: New header.
    * include/mailutils/sys/iostream.h: New header.
    * include/mailutils/sys/xscript-stream.h: New header.
    * include/mailutils/sys/Makefile.am (sysinclude_HEADERS): Add
    dbgstream.h, iostream.h and xscript-stream.h
    * mailbox/dbgstream.c: New file.
    * mailbox/iostream.c: New file.
    * mailbox/xscript-stream.c: New file.
    * mailbox/Makefile.am (libmailutils_la_SOURCES): Add dbgstream.c,
    iostream.c and xscript-stream.c
    * mailbox/filter_iconv.c (_icvt_ioctl): Simplify the declaration
    of ptrans.
    * mailbox/mapfile_stream.c (_mapfile_ioctl): Likewise.
    * mailbox/memory_stream.c (_memory_ioctl): Likewise.
    * mailbox/prog_stream.c (_prog_ioctl): Likewise.
    * mailbox/tcp.c (_tcp_ioctl): Likewise.
    * mailbox/fltstream.c (filter_ctl): Likewise.
    (filter_read_through, filter_write_through): New methods.
    (mu_filter_stream_create): Allow for use of MU_STREAM_RDTHRU
    and MU_STREAM_WRTHRU to create two-way filters (writing
    triggers filtering while reading is transparent or vice versa).
    * pop3d/extra.c (istream, ostream): Remove globals.
    (iostream): New variable.
    (real_istream, real_ostream): New variables.
    (pop3d_setio): Set transcript stream on top of the I/O one,
    if required.
    (pop3d_init_tls_server): Rewrite. Revert the meaning of the
    return code to match the usual convention (0 - success).
    (transcript): Removed.
    (pop3d_outf): Remove calls to transcript.
    * pop3d/pop3d.h (istream, ostream): Remove externs.
    (iostream): New extern.
    * pop3d/retr.c: Use iostream, instear of ostream.
    * pop3d/top.c: Likewise.
    * pop3d/stls.c: Update the call to pop3d_init_tls_server.
    
    * mailbox/stream_vprintf.c (mu_stream_vprintf): Fix return
    value to match the usual convention.

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

Summary of changes:
 include/mailutils/stream.h                         |   12 +
 include/mailutils/sys/Makefile.am                  |    5 +-
 .../mailutils/sys/{streamtrans.h => dbgstream.h}   |   18 +-
 .../mailutils/sys/{streamtrans.h => iostream.h}    |   21 +-
 .../sys/{streamtrans.h => xscript-stream.h}        |   20 +-
 mailbox/Makefile.am                                |    5 +-
 mailbox/dbgstream.c                                |   86 ++++++
 mailbox/file_stream.c                              |    6 +-
 mailbox/filter_iconv.c                             |    6 +-
 mailbox/fltstream.c                                |   45 +++-
 mailbox/iostream.c                                 |  249 ++++++++++++++++
 mailbox/mapfile_stream.c                           |    6 +-
 mailbox/memory_stream.c                            |    6 +-
 mailbox/prog_stream.c                              |    6 +-
 mailbox/stream_vprintf.c                           |    2 +-
 mailbox/tcp.c                                      |    6 +-
 mailbox/xscript-stream.c                           |  300 ++++++++++++++++++++
 pop3d/extra.c                                      |  111 ++++----
 pop3d/pop3d.h                                      |    2 +-
 pop3d/retr.c                                       |    2 +-
 pop3d/stls.c                                       |    2 +-
 pop3d/top.c                                        |    2 +-
 22 files changed, 816 insertions(+), 102 deletions(-)
 copy include/mailutils/sys/{streamtrans.h => dbgstream.h} (74%)
 copy include/mailutils/sys/{streamtrans.h => iostream.h} (70%)
 copy include/mailutils/sys/{streamtrans.h => xscript-stream.h} (70%)
 create mode 100644 mailbox/dbgstream.c
 create mode 100644 mailbox/iostream.c
 create mode 100644 mailbox/xscript-stream.c

diff --git a/include/mailutils/stream.h b/include/mailutils/stream.h
index 1aee884..1b74651 100644
--- a/include/mailutils/stream.h
+++ b/include/mailutils/stream.h
@@ -47,6 +47,9 @@ enum mu_buffer_type
 /* FIXME: This one affects only mailboxes */  
 #define MU_STREAM_QACCESS     0x00000200
 
+#define MU_STREAM_RDTHRU      0x00000400
+#define MU_STREAM_WRTHRU      0x00000800
+
 #define MU_STREAM_IRGRP       0x00001000
 #define MU_STREAM_IWGRP       0x00002000
 #define MU_STREAM_IROTH       0x00004000
@@ -59,6 +62,7 @@ enum mu_buffer_type
 #define MU_IOCTL_SET_SEEK_LIMITS 4
 #define MU_IOCTL_ABRIDGE_SEEK MU_IOCTL_SET_SEEK_LIMITS
 #define MU_IOCTL_GET_SEEK_LIMITS 5
+#define MU_IOCTL_SET_TRANSPORT   6
 
 void mu_stream_ref (mu_stream_t stream);
 void mu_stream_unref (mu_stream_t stream);
@@ -145,4 +149,12 @@ int 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,
 			  int flags);
 
+int mu_xscript_stream_create(mu_stream_t *pref, mu_stream_t transport,
+			     mu_stream_t logstr,
+			     const char *prefix[]);
+int mu_iostream_create (mu_stream_t *pref, mu_stream_t in, mu_stream_t out,
+			int flags);
+int mu_dbgstream_create(mu_stream_t *pref, mu_debug_t debug,
+			mu_log_level_t level, int flags);
+
 #endif
diff --git a/include/mailutils/sys/Makefile.am b/include/mailutils/sys/Makefile.am
index 34f995b..ce37112 100644
--- a/include/mailutils/sys/Makefile.am
+++ b/include/mailutils/sys/Makefile.am
@@ -19,10 +19,12 @@
 
 sysincludedir=$(pkgincludedir)/sys
 sysinclude_HEADERS = \
+ dbgstream.h\
  file_stream.h\
  filter.h\
  header_stream.h\
  header.h\
+ iostream.h\
  mapfile_stream.h\
  memory_stream.h\
  message_stream.h\
@@ -36,4 +38,5 @@ sysinclude_HEADERS = \
  stream.h\
  tls-stream.h\
  pop3.h\
- nntp.h
+ nntp.h\
+ xscript-stream.h
diff --git a/include/mailutils/sys/streamtrans.h b/include/mailutils/sys/dbgstream.h
similarity index 74%
copy from include/mailutils/sys/streamtrans.h
copy to include/mailutils/sys/dbgstream.h
index 25bc0d9..6934372 100644
--- a/include/mailutils/sys/streamtrans.h
+++ b/include/mailutils/sys/dbgstream.h
@@ -14,14 +14,18 @@
    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/>. */
 
-#ifndef _MAILUTILS_SYS_STREAMTRANS_H
-#define _MAILUTILS_SYS_STREAMTRANS_H
+#ifndef _MAILUTILS_SYS_DBGSTREAM_H
+# define _MAILUTILS_SYS_DBGSTREAM_H
 
-#include <mailutils/types.h>
-#include <mailutils/sys/stream.h>
+# include <mailutils/types.h>
+# include <mailutils/stream.h>
+# include <mailutils/sys/stream.h>
 
-struct _mu_streamtrans
+struct _mu_dbgstream
 {
-  struct _mu_stream base;      /* Superclass */
-  mu_stream_t transport;       /* Transport stream */
+  struct _mu_stream stream;
+  mu_debug_t debug;
+  mu_log_level_t level;
 };
+
+#endif
diff --git a/include/mailutils/sys/streamtrans.h b/include/mailutils/sys/iostream.h
similarity index 70%
copy from include/mailutils/sys/streamtrans.h
copy to include/mailutils/sys/iostream.h
index 25bc0d9..d8922f6 100644
--- a/include/mailutils/sys/streamtrans.h
+++ b/include/mailutils/sys/iostream.h
@@ -14,14 +14,21 @@
    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/>. */
 
-#ifndef _MAILUTILS_SYS_STREAMTRANS_H
-#define _MAILUTILS_SYS_STREAMTRANS_H
+#ifndef _MAILUTILS_SYS_IOSTREAM_H
+# define _MAILUTILS_SYS_IOSTREAM_H
 
-#include <mailutils/types.h>
-#include <mailutils/sys/stream.h>
+# include <mailutils/types.h>
+# include <mailutils/stream.h>
+# include <mailutils/sys/stream.h>
 
-struct _mu_streamtrans
+# define _MU_STREAM_INPUT 0
+# define _MU_STREAM_OUTPUT 1
+
+struct _mu_iostream
 {
-  struct _mu_stream base;      /* Superclass */
-  mu_stream_t transport;       /* Transport stream */
+  struct _mu_stream stream;
+  mu_stream_t transport[2];
+  int last_err_str;
 };
+
+#endif
diff --git a/include/mailutils/sys/streamtrans.h b/include/mailutils/sys/xscript-stream.h
similarity index 70%
copy from include/mailutils/sys/streamtrans.h
copy to include/mailutils/sys/xscript-stream.h
index 25bc0d9..c6b4538 100644
--- a/include/mailutils/sys/streamtrans.h
+++ b/include/mailutils/sys/xscript-stream.h
@@ -14,14 +14,20 @@
    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/>. */
 
-#ifndef _MAILUTILS_SYS_STREAMTRANS_H
-#define _MAILUTILS_SYS_STREAMTRANS_H
+#ifndef _MAILUTILS_SYS_XSCRIPT_STREAM_H
+# define _MAILUTILS_SYS_XSCRIPT_STREAM_H
 
-#include <mailutils/types.h>
-#include <mailutils/sys/stream.h>
+# include <mailutils/types.h>
+# include <mailutils/stream.h>
+# include <mailutils/sys/stream.h>
 
-struct _mu_streamtrans
+struct _mu_xscript_stream
 {
-  struct _mu_stream base;      /* Superclass */
-  mu_stream_t transport;       /* Transport stream */
+  struct _mu_stream stream;
+  mu_stream_t transport;
+  mu_stream_t logstr;
+  int flags;
+  char *prefix[2];
 };
+
+#endif
diff --git a/mailbox/Makefile.am b/mailbox/Makefile.am
index 0d04930..1fd892d 100644
--- a/mailbox/Makefile.am
+++ b/mailbox/Makefile.am
@@ -69,6 +69,7 @@ libmailutils_la_SOURCES = \
  daemon.c\
  date.c\
  dbgstderr.c\
+ dbgstream.c\
  dbgsyslog.c\
  debug.c\
  diag.c\
@@ -84,6 +85,7 @@ libmailutils_la_SOURCES = \
  gocs.c\
  hdritr.c\
  header.c\
+ iostream.c\
  iterator.c\
  ipsrv.c\
  kwd.c\
@@ -150,7 +152,8 @@ libmailutils_la_SOURCES = \
  vartab.c\
  vasnprintf.c\
  version.c\
- wicket.c
+ wicket.c\
+ xscript-stream.c
  
 BUILT_SOURCES = parsedate.c muerrno.c cfg_parser.c cfg_parser.h cfg_lexer.c
 MOSTLYCLEANFILES=
diff --git a/mailbox/dbgstream.c b/mailbox/dbgstream.c
new file mode 100644
index 0000000..19c137d
--- /dev/null
+++ b/mailbox/dbgstream.c
@@ -0,0 +1,86 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 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, 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 GNU Mailutils.  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/types.h>
+#include <mailutils/alloc.h>
+#include <mailutils/errno.h>
+
+#include <mailutils/nls.h>
+#include <mailutils/stream.h>
+#include <mailutils/sys/stream.h>
+#include <mailutils/sys/dbgstream.h>
+#include <mailutils/debug.h>
+
+static int
+_dbg_write (struct _mu_stream *str, const char *buf, size_t size,
+	    size_t *pnwrite)
+{
+  struct _mu_dbgstream *sp = (struct _mu_dbgstream *)str;
+  if (pnwrite)
+    *pnwrite = size;
+  while (size > 0 && (buf[size-1] == '\n' || buf[size-1] == '\r'))
+    size--;
+  if (size)
+    mu_debug_printf (sp->debug, sp->level, "%.*s\n", size, buf);
+  return 0;
+}
+
+static int
+_dbg_flush (struct _mu_stream *str)
+{
+  struct _mu_dbgstream *sp = (struct _mu_dbgstream *)str;
+  mu_debug_printf (sp->debug, sp->level, "\n");
+  return 0;
+}
+
+static void
+_dbg_done (struct _mu_stream *str)
+{
+  struct _mu_dbgstream *sp = (struct _mu_dbgstream *)str;
+  if (!(str->flags & MU_STREAM_NO_CLOSE))
+    mu_debug_destroy (&sp->debug, NULL);
+}
+
+int
+mu_dbgstream_create(mu_stream_t *pref, mu_debug_t debug, mu_log_level_t level,
+		    int flags)
+{
+  struct _mu_dbgstream *sp;
+
+  sp = (struct _mu_dbgstream *)
+    _mu_stream_create (sizeof (*sp), MU_STREAM_WRITE |
+		       (flags & MU_STREAM_NO_CLOSE));
+  if (!sp)
+    return ENOMEM;
+  sp->stream.write = _dbg_write;
+  sp->stream.flush = _dbg_flush;
+  sp->stream.done = _dbg_done;
+  
+  sp->debug = debug;
+  sp->level = level;
+  mu_stream_set_buffer ((mu_stream_t) sp, mu_buffer_line, 1024);
+  *pref = (mu_stream_t) sp;
+  return 0;
+}
+  
diff --git a/mailbox/file_stream.c b/mailbox/file_stream.c
index c97075f..7dc8c90 100644
--- a/mailbox/file_stream.c
+++ b/mailbox/file_stream.c
@@ -192,7 +192,7 @@ static int
 fd_ioctl (struct _mu_stream *str, int code, void *ptr)
 {
   struct _mu_file_stream *fstr = (struct _mu_file_stream *) str;
-  mu_transport_t (*ptrans)[2];
+  mu_transport_t *ptrans;
   
   switch (code)
     {
@@ -200,8 +200,8 @@ fd_ioctl (struct _mu_stream *str, int code, void *ptr)
       if (!ptr)
 	return EINVAL;
       ptrans = ptr;
-      (*ptrans)[0] = (mu_transport_t) fstr->fd;
-      (*ptrans)[1] = NULL;
+      ptrans[0] = (mu_transport_t) fstr->fd;
+      ptrans[1] = NULL;
       break;
 
     default:
diff --git a/mailbox/filter_iconv.c b/mailbox/filter_iconv.c
index 6674ba4..c948df4 100644
--- a/mailbox/filter_iconv.c
+++ b/mailbox/filter_iconv.c
@@ -390,7 +390,7 @@ static int
 _icvt_ioctl (mu_stream_t stream, int code, void *ptr)
 {
   struct icvt_stream *s = (struct icvt_stream *)stream;
-  mu_transport_t (*ptrans)[2];
+  mu_transport_t *ptrans;
   
   switch (code)
     {
@@ -398,8 +398,8 @@ _icvt_ioctl (mu_stream_t stream, int code, void *ptr)
       if (!ptr)
 	return EINVAL;
       ptrans = ptr;
-      (*ptrans)[0] = (mu_transport_t) s->transport;
-      (*ptrans)[1] = NULL;
+      ptrans[0] = (mu_transport_t) s->transport;
+      ptrans[1] = NULL;
       break;
 
     default:
diff --git a/mailbox/fltstream.c b/mailbox/fltstream.c
index 8f6260a..c759134 100644
--- a/mailbox/fltstream.c
+++ b/mailbox/fltstream.c
@@ -335,7 +335,7 @@ static int
 filter_ctl (struct _mu_stream *stream, int op, void *ptr)
 {
   struct _mu_filter_stream *fs = (struct _mu_filter_stream *)stream;
-  mu_transport_t (*ptrans)[2];
+  mu_transport_t *ptrans;
 
   switch (op)
     {
@@ -343,8 +343,8 @@ filter_ctl (struct _mu_stream *stream, int op, void *ptr)
       if (!ptr)
 	return EINVAL;
       ptrans = ptr;
-      (*ptrans)[0] = (mu_transport_t) fs->transport;
-      (*ptrans)[1] = NULL;
+      ptrans[0] = (mu_transport_t) fs->transport;
+      ptrans[1] = NULL;
       break;
 
     default:
@@ -390,6 +390,26 @@ filter_close (mu_stream_t stream)
   return mu_stream_close (fs->transport);
 }
 
+
+static int
+filter_read_through (struct _mu_stream *stream,
+		     char *buf, size_t bufsize,
+		     size_t *pnread)
+{
+  struct _mu_filter_stream *fs = (struct _mu_filter_stream *)stream;
+  return mu_stream_read (fs->transport, buf, bufsize, pnread);
+}
+
+static int
+filter_write_through (struct _mu_stream *stream,
+		      const char *buf, size_t bufsize,
+		      size_t *pnwrite)
+{
+  struct _mu_filter_stream *fs = (struct _mu_filter_stream *)stream;
+  return mu_stream_write (fs->transport, buf, bufsize, pnwrite);
+}
+
+
 int
 mu_filter_stream_create (mu_stream_t *pflt,
 			 mu_stream_t str,
@@ -402,7 +422,14 @@ mu_filter_stream_create (mu_stream_t *pflt,
 
   if ((flags & MU_STREAM_RDWR) == MU_STREAM_RDWR
       || !(flags & MU_STREAM_RDWR)
-      || (flags & (MU_STREAM_WRITE|MU_STREAM_SEEK)) == (MU_STREAM_WRITE|MU_STREAM_SEEK))
+      || (flags & (MU_STREAM_WRITE|MU_STREAM_SEEK)) ==
+          (MU_STREAM_WRITE|MU_STREAM_SEEK)
+      || (flags & (MU_STREAM_RDTHRU|MU_STREAM_WRTHRU)) ==
+	  (MU_STREAM_RDTHRU|MU_STREAM_WRTHRU)
+      || (flags & (MU_STREAM_READ|MU_STREAM_RDTHRU)) ==
+	  (MU_STREAM_READ|MU_STREAM_RDTHRU)
+      || (flags & (MU_STREAM_WRITE|MU_STREAM_WRTHRU)) ==
+          (MU_STREAM_WRITE|MU_STREAM_WRTHRU))
     return EINVAL;
  
   fs = (struct _mu_filter_stream *) _mu_stream_create (sizeof (*fs), flags);
@@ -413,11 +440,21 @@ mu_filter_stream_create (mu_stream_t *pflt,
     {
       fs->stream.read = filter_read;
       fs->stream.flush = filter_rd_flush;
+      if (flags & MU_STREAM_WRTHRU)
+	{
+	  flags |= MU_STREAM_WRITE;
+	  fs->stream.write = filter_write_through;
+	}
     }
   else
     {
       fs->stream.write = filter_write;
       fs->stream.flush = filter_wr_flush;
+      if (flags & MU_STREAM_RDTHRU)
+	{
+	  flags |= MU_STREAM_READ;
+	  fs->stream.read = filter_read_through;
+	}
     }
   fs->stream.done = filter_done;
   fs->stream.close = filter_close;
diff --git a/mailbox/iostream.c b/mailbox/iostream.c
new file mode 100644
index 0000000..4b76d3a
--- /dev/null
+++ b/mailbox/iostream.c
@@ -0,0 +1,249 @@
+/* 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, 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 GNU Mailutils.  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/types.h>
+#include <mailutils/alloc.h>
+#include <mailutils/errno.h>
+
+#include <mailutils/nls.h>
+#include <mailutils/stream.h>
+#include <mailutils/sys/stream.h>
+#include <mailutils/sys/iostream.h>
+
+static int
+_iostream_read (struct _mu_stream *str, char *buf, size_t bufsize,
+		size_t *pnread)
+{
+  struct _mu_iostream *sp = (struct _mu_iostream *)str;
+  int rc = mu_stream_read (sp->transport[_MU_STREAM_INPUT], buf, bufsize,
+			   pnread);
+  if (rc)
+    sp->last_err_str = _MU_STREAM_INPUT;
+  return rc;
+}
+
+static int
+_iostream_readdelim (struct _mu_stream *str, char *buf, size_t bufsize,
+		    int delim, size_t *pnread)
+{
+  struct _mu_iostream *sp = (struct _mu_iostream *)str;
+  int rc = mu_stream_readdelim (sp->transport[_MU_STREAM_INPUT], buf,
+				bufsize, delim, pnread);
+  if (rc)
+    sp->last_err_str = _MU_STREAM_INPUT;
+  return rc;
+}
+
+static int
+_iostream_write (struct _mu_stream *str, const char *buf, size_t bufsize,
+		  size_t *pnwrite)
+{
+  struct _mu_iostream *sp = (struct _mu_iostream *)str;
+  int rc = mu_stream_write (sp->transport[_MU_STREAM_OUTPUT], buf, bufsize,
+			    pnwrite);
+  if (rc)
+    sp->last_err_str = _MU_STREAM_OUTPUT;
+  return rc;
+}
+
+static int
+_iostream_flush (struct _mu_stream *str)
+{
+  struct _mu_iostream *sp = (struct _mu_iostream *)str;
+  int rc = mu_stream_flush (sp->transport[_MU_STREAM_INPUT]);
+  if (rc)
+    {
+      sp->last_err_str = _MU_STREAM_INPUT;
+      return rc;
+    }
+  if (sp->transport[_MU_STREAM_INPUT] != sp->transport[_MU_STREAM_OUTPUT])
+    {
+      rc = mu_stream_flush (sp->transport[_MU_STREAM_OUTPUT]);
+      if (rc)
+	sp->last_err_str = _MU_STREAM_OUTPUT;
+    }
+  return rc;
+}
+
+static int
+_iostream_open (struct _mu_stream *str)
+{
+  struct _mu_iostream *sp = (struct _mu_iostream *)str;
+  int rc;
+  rc = mu_stream_open (sp->transport[_MU_STREAM_INPUT]);
+  if (rc)
+    {
+      sp->last_err_str = _MU_STREAM_INPUT;
+      return rc;
+    }
+  if (sp->transport[_MU_STREAM_INPUT] != sp->transport[_MU_STREAM_OUTPUT])
+    {
+      rc = mu_stream_open (sp->transport[_MU_STREAM_OUTPUT]);
+      if (rc)
+	{
+	  sp->last_err_str = _MU_STREAM_OUTPUT;
+	  mu_stream_close (sp->transport[_MU_STREAM_INPUT]);
+	}
+    }
+  return rc;
+}
+
+static int
+_iostream_close (struct _mu_stream *str)
+{
+  struct _mu_iostream *sp = (struct _mu_iostream *)str;
+  if (sp->stream.flags & MU_STREAM_NO_CLOSE)
+    return 0;
+  mu_stream_close (sp->transport[_MU_STREAM_INPUT]);
+  if (sp->transport[_MU_STREAM_INPUT] != sp->transport[_MU_STREAM_OUTPUT])
+    mu_stream_close (sp->transport[_MU_STREAM_OUTPUT]);
+  return 0;
+}
+
+static void
+_iostream_done (struct _mu_stream *str)
+{
+  struct _mu_iostream *sp = (struct _mu_iostream *)str;
+  mu_stream_unref (sp->transport[_MU_STREAM_INPUT]);
+  if (sp->transport[_MU_STREAM_INPUT] != sp->transport[_MU_STREAM_OUTPUT])
+    mu_stream_unref (sp->transport[_MU_STREAM_OUTPUT]);
+}
+
+static int
+_iostream_ctl (struct _mu_stream *str, int op, void *arg)
+{
+  struct _mu_iostream *sp = (struct _mu_iostream *)str;
+  mu_transport_t *ptrans;
+
+  switch (op)
+    {
+    case MU_IOCTL_GET_TRANSPORT:
+      if (!arg)
+	return EINVAL;
+      ptrans = arg;
+      ptrans[0] = (mu_transport_t) sp->transport[_MU_STREAM_INPUT];
+      ptrans[1] = (mu_transport_t) sp->transport[_MU_STREAM_OUTPUT];
+      break;
+
+    case MU_IOCTL_SET_TRANSPORT:
+      if (!arg)
+	return EINVAL;
+      ptrans = arg;
+      sp->transport[_MU_STREAM_INPUT] = (mu_stream_t) ptrans[0];
+      sp->transport[_MU_STREAM_OUTPUT] = (mu_stream_t) ptrans[1];
+      break;
+      
+    default:
+      return EINVAL;
+    }
+  return 0;
+}
+
+static int
+_iostream_wait (struct _mu_stream *str, int *pflags, struct timeval *tvp)
+{
+  struct _mu_iostream *sp = (struct _mu_iostream *)str;
+  int rc = EINVAL;
+  
+  if (*pflags == MU_STREAM_READY_RD)
+    {
+      rc = mu_stream_wait (sp->transport[_MU_STREAM_INPUT], pflags, tvp);
+      if (rc)
+	sp->last_err_str = _MU_STREAM_INPUT;
+    }
+  else if (*pflags == MU_STREAM_READY_WR)
+    {
+      rc = mu_stream_wait (sp->transport[_MU_STREAM_OUTPUT], pflags, tvp);
+      if (rc)
+	sp->last_err_str = _MU_STREAM_OUTPUT;
+    }
+  return rc;
+}
+
+static int
+_iostream_shutdown (struct _mu_stream *str, int how)
+{
+  struct _mu_iostream *sp = (struct _mu_iostream *)str;
+  int rc = EINVAL;
+  switch (how)
+    {
+    case MU_STREAM_READ:
+      rc = mu_stream_shutdown (sp->transport[_MU_STREAM_INPUT], how);
+      if (rc)
+	sp->last_err_str = _MU_STREAM_INPUT;
+      break;
+      
+    case MU_STREAM_WRITE:
+      rc = mu_stream_shutdown (sp->transport[_MU_STREAM_OUTPUT], how);
+      if (rc)
+	sp->last_err_str = _MU_STREAM_OUTPUT;
+    }
+  return rc;
+}
+
+static const char *
+_iostream_error_string (struct _mu_stream *str, int rc)
+{
+  struct _mu_iostream *sp = (struct _mu_iostream *)str;
+  mu_stream_t transport = sp->transport[sp->last_err_str];
+  if (transport)
+    return mu_stream_strerror (transport, rc);
+  return mu_strerror (rc);
+}
+
+int
+mu_iostream_create (mu_stream_t *pref, mu_stream_t in, mu_stream_t out,
+		    int flags)
+{
+  struct _mu_iostream *sp;
+
+  sp = (struct _mu_iostream *)
+    _mu_stream_create (sizeof (*sp),
+		       MU_STREAM_READ | MU_STREAM_WRITE |
+		       (flags & MU_STREAM_NO_CLOSE));
+  if (!sp)
+    return ENOMEM;
+
+  sp->stream.read = _iostream_read; 
+  sp->stream.readdelim = _iostream_readdelim; 
+  sp->stream.write = _iostream_write;
+  sp->stream.flush = _iostream_flush;
+  sp->stream.open = _iostream_open; 
+  sp->stream.close = _iostream_close;
+  sp->stream.done = _iostream_done; 
+  sp->stream.ctl = _iostream_ctl;
+  sp->stream.wait = _iostream_wait;
+  sp->stream.shutdown = _iostream_shutdown;
+  sp->stream.error_string = _iostream_error_string;
+
+  mu_stream_ref (in);
+  sp->transport[_MU_STREAM_INPUT] = in;
+  mu_stream_ref (out);
+  sp->transport[_MU_STREAM_OUTPUT] = out;
+
+  mu_stream_set_buffer ((mu_stream_t) sp, mu_buffer_line, 1024);
+  *pref = (mu_stream_t) sp;
+  return 0;
+}
+
+
diff --git a/mailbox/mapfile_stream.c b/mailbox/mapfile_stream.c
index 89b3e47..aeb85e5 100644
--- a/mailbox/mapfile_stream.c
+++ b/mailbox/mapfile_stream.c
@@ -209,13 +209,13 @@ static int
 _mapfile_ioctl (struct _mu_stream *str, int code, void *ptr)
 {
   struct _mu_mapfile_stream *mfs = (struct _mu_mapfile_stream *) str;
-  mu_transport_t (*ptrans)[2];
+  mu_transport_t ptrans[2];
   
   switch (code)
     {
     case MU_IOCTL_GET_TRANSPORT:
-      (*ptrans)[0] = (mu_transport_t) mfs->fd;
-      (*ptrans)[1] = NULL;
+      ptrans[0] = (mu_transport_t) mfs->fd;
+      ptrans[1] = NULL;
       break;
 
     default:
diff --git a/mailbox/memory_stream.c b/mailbox/memory_stream.c
index ecb091f..19d4590 100644
--- a/mailbox/memory_stream.c
+++ b/mailbox/memory_stream.c
@@ -147,7 +147,7 @@ static int
 _memory_ioctl (struct _mu_stream *stream, int code, void *ptr)
 {
   struct _mu_memory_stream *mfs = (struct _mu_memory_stream *) stream;
-  mu_transport_t (*ptrans)[2];
+  mu_transport_t *ptrans;
   
   switch (code)
     {
@@ -155,8 +155,8 @@ _memory_ioctl (struct _mu_stream *stream, int code, void *ptr)
       if (!ptr)
 	return EINVAL;
       ptrans = ptr;
-      (*ptrans)[0] = (mu_transport_t) mfs->ptr;
-      (*ptrans)[1] = NULL;
+      ptrans[0] = (mu_transport_t) mfs->ptr;
+      ptrans[1] = NULL;
       break;
 
     default:
diff --git a/mailbox/prog_stream.c b/mailbox/prog_stream.c
index 7c65940..4c0bb8e 100644
--- a/mailbox/prog_stream.c
+++ b/mailbox/prog_stream.c
@@ -367,7 +367,7 @@ _prog_ioctl (struct _mu_stream *str, int code, void *ptr)
 {
   struct _mu_prog_stream *fstr = (struct _mu_prog_stream *) str;
   mu_transport_t t[2];
-  mu_transport_t (*ptrans)[2];
+  mu_transport_t *ptrans;
   
   switch (code)
     {
@@ -375,9 +375,9 @@ _prog_ioctl (struct _mu_stream *str, int code, void *ptr)
       if (!ptr)
       	return EINVAL;
       mu_stream_ioctl (fstr->in, MU_IOCTL_GET_TRANSPORT, t);
-      (*ptrans)[0] = t[0];
+      ptrans[0] = t[0];
       mu_stream_ioctl (fstr->out, MU_IOCTL_GET_TRANSPORT, t);
-      (*ptrans)[1] = t[1];
+      ptrans[1] = t[1];
       break;
 
     case MU_IOCTL_GET_STATUS:
diff --git a/mailbox/stream_vprintf.c b/mailbox/stream_vprintf.c
index db0bff3..5bd9c09 100644
--- a/mailbox/stream_vprintf.c
+++ b/mailbox/stream_vprintf.c
@@ -37,6 +37,6 @@ mu_stream_vprintf (mu_stream_t str, const char *fmt, va_list ap)
   n = strlen (buf);
   rc = mu_stream_write (str, buf, n, NULL);
   free (buf);
-  return rc == 0 ? n : -1;
+  return rc;
 }
 
diff --git a/mailbox/tcp.c b/mailbox/tcp.c
index a03eedb..0810728 100644
--- a/mailbox/tcp.c
+++ b/mailbox/tcp.c
@@ -196,7 +196,7 @@ static int
 _tcp_ioctl (mu_stream_t stream, int code, void *ptr)
 {
   struct _tcp_instance *tcp = (struct _tcp_instance *)stream;
-  mu_transport_t (*ptrans)[2];
+  mu_transport_t *ptrans;
 
   switch (code)
     {
@@ -204,8 +204,8 @@ _tcp_ioctl (mu_stream_t stream, int code, void *ptr)
       if (!ptr)
 	return EINVAL;
       ptrans = ptr;
-      (*ptrans)[0] = (mu_transport_t) tcp->fd;
-      (*ptrans)[1] = NULL;
+      ptrans[0] = (mu_transport_t) tcp->fd;
+      ptrans[1] = NULL;
       break;
 
     default:
diff --git a/mailbox/xscript-stream.c b/mailbox/xscript-stream.c
new file mode 100644
index 0000000..c3a5c40
--- /dev/null
+++ b/mailbox/xscript-stream.c
@@ -0,0 +1,300 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 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, 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 GNU Mailutils.  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/types.h>
+#include <mailutils/alloc.h>
+#include <mailutils/errno.h>
+
+#include <mailutils/nls.h>
+#include <mailutils/stream.h>
+#include <mailutils/sys/stream.h>
+#include <mailutils/sys/xscript-stream.h>
+
+/* A "transcript stream" transparently writes data to and reads data from
+   an underlying transport stream, writing each lineful of data to a "log
+   stream". Writes to log stream are prefixed with a string indicating
+   direction of the data (read/write). Default prefixes are those used in
+   RFCs -- "S: ", for data written ("Server"), and "C: ", for data read
+   ("Client"). */
+
+#define TRANS_READ 0x1
+#define TRANS_WRITE 0x2
+#define FLAG_TO_PFX(c) ((c) - 1)
+
+static void
+print_transcript (struct _mu_xscript_stream *str, int flag,
+		  const char *buf, size_t size)
+{
+    while (size)
+      {
+	const char *p;
+	size_t len;
+	
+	if (str->flags & flag)
+	  {
+	    mu_stream_write (str->logstr,
+			     str->prefix[FLAG_TO_PFX(flag)],
+			     strlen (str->prefix[FLAG_TO_PFX (flag)]),
+			     NULL);
+	    str->flags &= ~flag;
+	  }
+	p = memchr (buf, '\n', size);
+	if (p)
+	  {
+	    len = p - buf;
+	    if (p > buf && p[-1] == '\r')
+	      len--;
+	    mu_stream_write (str->logstr, buf, len, NULL);
+	    mu_stream_write (str->logstr, "\n", 1, NULL);
+	    str->flags |= flag;
+
+	    len = p - buf + 1;
+	    buf = p + 1;
+	    size -= len;
+	  }
+	else
+	  {
+	    mu_stream_write (str->logstr, buf, size, NULL);
+	    break;
+	  }
+      }
+}
+
+static int
+_xscript_read (struct _mu_stream *str, char *buf, size_t bufsize,
+	       size_t *pnread)
+{
+  struct _mu_xscript_stream *sp = (struct _mu_xscript_stream *)str;
+  size_t nbytes;
+  int rc = mu_stream_read (sp->transport, buf, bufsize, &nbytes);
+
+  if (rc == 0)
+    {
+      print_transcript (sp, TRANS_READ, buf, nbytes);
+      if (pnread)
+	*pnread = nbytes;
+    }
+  return rc;
+}
+
+static int
+_xscript_readdelim (struct _mu_stream *str, char *buf, size_t bufsize,
+		    int delim, size_t *pnread)
+{
+  struct _mu_xscript_stream *sp = (struct _mu_xscript_stream *)str;
+  size_t nread;
+  int rc = mu_stream_readdelim (sp->transport, buf, bufsize, delim, &nread);
+  if (rc == 0)
+    {
+      print_transcript (sp, TRANS_READ, buf, nread);
+      if (pnread)
+	*pnread = nread;
+    }
+  return rc;
+}
+
+static int
+_xscript_write (struct _mu_stream *str, const char *buf, size_t bufsize,
+		  size_t *pnwrite)
+{
+  struct _mu_xscript_stream *sp = (struct _mu_xscript_stream *)str;
+  int rc = mu_stream_write (sp->transport, buf, bufsize, pnwrite);
+
+  if (rc == 0)
+    print_transcript (sp, TRANS_WRITE, buf, pnwrite ? *pnwrite : bufsize);
+  return rc;
+}
+
+static int
+_xscript_flush (struct _mu_stream *str)
+{
+  struct _mu_xscript_stream *sp = (struct _mu_xscript_stream *)str;
+  return mu_stream_flush (sp->transport);
+}
+
+static int
+_xscript_open (struct _mu_stream *str)
+{
+  struct _mu_xscript_stream *sp = (struct _mu_xscript_stream *)str;
+  return mu_stream_open (sp->transport);
+}
+
+static int
+_xscript_close (struct _mu_stream *str)
+{
+  struct _mu_xscript_stream *sp = (struct _mu_xscript_stream *)str;
+  if (sp->stream.flags & MU_STREAM_NO_CLOSE)
+    return 0;
+  return mu_stream_close (sp->transport);
+}
+
+static void
+_xscript_done (struct _mu_stream *str)
+{
+  struct _mu_xscript_stream *sp = (struct _mu_xscript_stream *)str;
+  free (sp->prefix[0]);
+  free (sp->prefix[1]);
+  mu_stream_unref (sp->transport);
+  mu_stream_unref (sp->logstr);
+}
+
+static int
+_xscript_seek (struct _mu_stream *str, mu_off_t off, mu_off_t *ppos)
+{
+  struct _mu_xscript_stream *sp = (struct _mu_xscript_stream *)str;
+  return mu_stream_seek (sp->transport, off, MU_SEEK_SET, ppos);
+}
+
+static int
+_xscript_size (struct _mu_stream *str, mu_off_t *psize)
+{
+  struct _mu_xscript_stream *sp = (struct _mu_xscript_stream *)str;
+  return mu_stream_size (sp->transport, psize);
+}
+
+static int
+_xscript_ctl (struct _mu_stream *str, int op, void *arg)
+{
+  struct _mu_xscript_stream *sp = (struct _mu_xscript_stream *)str;
+  mu_transport_t *ptrans;
+
+  switch (op)
+    {
+    case MU_IOCTL_GET_TRANSPORT:
+      if (!arg)
+	return EINVAL;
+      ptrans = arg;
+      ptrans[0] = (mu_transport_t) sp->transport;
+      ptrans[1] = (mu_transport_t) sp->logstr;
+      break;
+
+    case MU_IOCTL_SET_TRANSPORT:
+      if (!arg)
+	return EINVAL;
+      ptrans = arg;
+      if (ptrans[0])
+	sp->transport = (mu_stream_t) ptrans[0];
+      if (ptrans[1])
+	sp->logstr = (mu_stream_t) ptrans[1];
+      break;
+      
+    default:
+      return mu_stream_ioctl (sp->transport, op, arg);
+    }
+  return 0;
+}
+
+static int
+_xscript_wait (struct _mu_stream *str, int *pflags, struct timeval *tvp)
+{
+  struct _mu_xscript_stream *sp = (struct _mu_xscript_stream *)str;
+  return mu_stream_wait (sp->transport, pflags, tvp);
+}
+
+static int
+_xscript_truncate (struct _mu_stream *str, mu_off_t size)
+{
+  struct _mu_xscript_stream *sp = (struct _mu_xscript_stream *)str;
+  return mu_stream_truncate (sp->transport, size);
+}
+
+static int
+_xscript_shutdown (struct _mu_stream *str, int how)
+{
+  struct _mu_xscript_stream *sp = (struct _mu_xscript_stream *)str;
+  return mu_stream_shutdown (sp->transport, how);
+}
+
+static const char *
+_xscript_error_string (struct _mu_stream *str, int rc)
+{
+  struct _mu_xscript_stream *sp = (struct _mu_xscript_stream *)str;
+  const char *p = mu_stream_strerror (sp->transport, rc);
+  if (!p)
+    p = mu_strerror (rc);
+  return p;
+}
+
+const char *default_prefix[2] = {
+    "C: ", "S: "
+};
+
+int
+mu_xscript_stream_create(mu_stream_t *pref, mu_stream_t transport,
+			 mu_stream_t logstr,
+			 const char *prefix[])
+{
+  int flags;
+  struct _mu_xscript_stream *sp;
+
+  mu_stream_get_flags (transport, &flags);
+  sp = (struct _mu_xscript_stream *) _mu_stream_create (sizeof (*sp), flags);
+  if (!sp)
+    return ENOMEM;
+
+  sp->stream.read = _xscript_read; 
+  sp->stream.readdelim = _xscript_readdelim; 
+  sp->stream.write = _xscript_write;
+  sp->stream.flush = _xscript_flush;
+  sp->stream.open = _xscript_open; 
+  sp->stream.close = _xscript_close;
+  sp->stream.done = _xscript_done; 
+  sp->stream.seek = _xscript_seek; 
+  sp->stream.size = _xscript_size; 
+  sp->stream.ctl = _xscript_ctl;
+  sp->stream.wait = _xscript_wait;
+  sp->stream.truncate = _xscript_truncate;
+  sp->stream.shutdown = _xscript_shutdown;
+  sp->stream.error_string = _xscript_error_string;
+
+  mu_stream_ref (transport);
+  sp->transport = transport;
+  mu_stream_ref (logstr);
+  sp->logstr = logstr;
+  
+  sp->flags = TRANS_READ | TRANS_WRITE;
+  if (prefix)
+    {
+      sp->prefix[0] = strdup(prefix[0] ? prefix[0] : default_prefix[0]);
+      sp->prefix[1] = strdup(prefix[1] ? prefix[1] : default_prefix[1]);
+    }
+  else
+    {
+      sp->prefix[0] = strdup(default_prefix[0]);
+      sp->prefix[1] = strdup(default_prefix[1]);
+    }
+
+  if (sp->prefix[0] == NULL || sp->prefix[1] == 0)
+    {
+      free (sp->prefix[0]);
+      free (sp->prefix[1]);
+      free (sp);
+      return ENOMEM;
+    }
+  mu_stream_set_buffer ((mu_stream_t) sp, mu_buffer_line, 1024);
+  *pref = (mu_stream_t) sp;
+  return 0;
+}
+
+
diff --git a/pop3d/extra.c b/pop3d/extra.c
index 2ab0f8a..ea0929f 100644
--- a/pop3d/extra.c
+++ b/pop3d/extra.c
@@ -20,7 +20,7 @@
 #include "pop3d.h"
 #include "mailutils/libargp.h"
 
-mu_stream_t istream, ostream;
+mu_stream_t iostream;
 
 void
 pop3d_parse_command (char *cmd, char **pcmd, char **parg)
@@ -132,12 +132,12 @@ pop3d_abquit (int reason)
    FIXME: This is sorta kludge: we could use MU_IOCTL_GET_TRANSPORT call
    to retrieve the bottom-level stream, if filter streams supported it.
 */
-static mu_stream_t real_ostream;
+static mu_stream_t real_istream, real_ostream;
 
 void
 pop3d_setio (FILE *in, FILE *out)
 {
-  mu_stream_t str;
+  mu_stream_t str, istream, ostream;
   
   if (!in)
     pop3d_abquit (ERR_NO_IFILE);
@@ -147,6 +147,7 @@ pop3d_setio (FILE *in, FILE *out)
   if (mu_stdio_stream_create (&istream, fileno (in),
 			      MU_STREAM_READ | MU_STREAM_NO_CLOSE))
     pop3d_abquit (ERR_NO_IFILE);
+  real_istream = istream;
   mu_stream_set_buffer (istream, mu_buffer_line, 1024);
   
   if (mu_stdio_stream_create (&str, fileno (out),
@@ -157,6 +158,32 @@ pop3d_setio (FILE *in, FILE *out)
 			MU_STREAM_WRITE | MU_STREAM_NO_CLOSE))
     pop3d_abquit (ERR_NO_IFILE);
   mu_stream_set_buffer (ostream, mu_buffer_line, 1024);
+
+  if (mu_iostream_create (&iostream, istream, ostream, 0))
+    pop3d_abquit (ERR_FILE);
+  if (pop3d_transcript)
+    {
+      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,
+				MU_STREAM_NO_CLOSE);
+      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
+	    iostream = xstr;
+	}
+    }
 }
 
 #ifdef WITH_TLS
@@ -166,37 +193,44 @@ pop3d_init_tls_server ()
   mu_stream_t stream;
   int rc;
 
-  rc = mu_tls_server_stream_create (&stream, istream, real_ostream, 0);
+  rc = mu_tls_server_stream_create (&stream, real_istream, real_ostream,
+				    MU_STREAM_NO_CLOSE);
   if (rc)
-    return 0;
+    return 1;
 
   rc = mu_stream_open (stream);
   if (rc)
     {
       mu_diag_output (MU_DIAG_ERROR, _("cannot open TLS stream: %s"),
 		      mu_stream_strerror (stream, rc));
-      return 0;
+      mu_stream_destroy (&stream);
+      return 1;
     }
-  
-  istream = stream;
-  mu_stream_destroy (&ostream);
-  if (mu_filter_create (&ostream, stream, "rfc822", MU_FILTER_ENCODE,
-			MU_STREAM_WRITE | MU_STREAM_NO_CLOSE))
+
+  if (mu_filter_create (&stream, stream, "rfc822", MU_FILTER_ENCODE,
+			MU_STREAM_WRITE | MU_STREAM_RDTHRU |
+			MU_STREAM_NO_CLOSE))
     pop3d_abquit (ERR_NO_IFILE);
-  mu_stream_set_buffer (ostream, mu_buffer_line, 1024);
-  return 1;
+  
+  if (pop3d_transcript)
+    {
+      mu_transport_t trans[2];
+
+      trans[0] = (mu_transport_t) stream;
+      trans[1] = NULL;
+      mu_stream_ioctl (iostream, MU_IOCTL_SET_TRANSPORT, trans);
+    }
+  else
+    iostream = stream;
+  return 0;
 }
 #endif
 
 void
 pop3d_bye ()
 {
-  if (istream == ostream)
-    {
-      mu_stream_close (istream);
-      mu_stream_destroy (&istream);
-    }
-  /* There's no reason closing in/out streams otherwise */
+  mu_stream_close (iostream);
+  mu_stream_destroy (&iostream);
 #ifdef WITH_TLS
   if (tls_available)
     mu_deinit_tls_libs ();
@@ -206,53 +240,28 @@ pop3d_bye ()
 void
 pop3d_flush_output ()
 {
-  mu_stream_flush (ostream);
+  mu_stream_flush (iostream);
 }
 
 int
 pop3d_is_master ()
 {
-  return ostream == NULL;
-}
-
-static void
-transcript (const char *pfx, const char *buf)
-{
-  if (pop3d_transcript)
-    {
-      int len = strlen (buf);
-      if (len > 0 && buf[len-1] == '\n')
-	{
-	  len--;
-	  if (len > 0 && buf[len-1] == '\r')
-	    len--;
-	}
-      mu_diag_output (MU_DIAG_DEBUG, "%s: %-.*s", pfx, len, buf);
-    }
+  return iostream == NULL;
 }
 
 void
 pop3d_outf (const char *fmt, ...)
 {
   va_list ap;
-  char *buf;
   int rc;
   
   va_start (ap, fmt);
-  vasprintf (&buf, fmt, ap);
+  rc = mu_stream_vprintf (iostream, fmt, ap);
   va_end (ap);
-
-  if (!buf)
-    pop3d_abquit (ERR_NO_MEM);
-  
-  transcript ("sent", buf);
-
-  rc = mu_stream_write (ostream, buf, strlen (buf), NULL);
-  free (buf);
   if (rc)
     {
       mu_diag_output (MU_DIAG_ERROR, _("Write failed: %s"),
- 		      mu_stream_strerror (ostream, rc));
+ 		      mu_stream_strerror (iostream, rc));
       pop3d_abquit (ERR_IO);
     }
 }
@@ -265,13 +274,13 @@ pop3d_readline (char *buffer, size_t size)
   size_t nbytes;
   
   alarm (idle_timeout);
-  rc = mu_stream_readline (istream, buffer, size, &nbytes);
+  rc = mu_stream_readline (iostream, buffer, size, &nbytes);
   alarm (0);
 
   if (rc)
     {
       mu_diag_output (MU_DIAG_ERROR, _("Read failed: %s"),
- 		      mu_stream_strerror (ostream, rc));
+ 		      mu_stream_strerror (iostream, rc));
       pop3d_abquit (ERR_IO);
     }
   else if (nbytes == 0)
@@ -285,8 +294,6 @@ pop3d_readline (char *buffer, size_t size)
       pop3d_abquit (ERR_PROTO);
     }
 
-  transcript ("recv", buffer);
-
   return buffer;
 }
 
diff --git a/pop3d/pop3d.h b/pop3d/pop3d.h
index b7e593b..f77652f 100644
--- a/pop3d/pop3d.h
+++ b/pop3d/pop3d.h
@@ -189,7 +189,7 @@ struct pop3d_command
   pop3d_command_handler_t handler;
 };
 
-extern mu_stream_t istream, ostream;
+extern mu_stream_t iostream;
 extern mu_pop_server_t pop3srv;
 extern mu_mailbox_t mbox;
 extern int state;
diff --git a/pop3d/retr.c b/pop3d/retr.c
index 53b25c0..5c30643 100644
--- a/pop3d/retr.c
+++ b/pop3d/retr.c
@@ -46,7 +46,7 @@ pop3d_retr (char *arg)
     return ERR_UNKNOWN;
   
   pop3d_outf ("+OK\n");
-  mu_stream_copy (ostream, stream, 0);
+  mu_stream_copy (iostream, stream, 0);
   mu_stream_destroy (&stream);
 
   if (!mu_attribute_is_read (attr))
diff --git a/pop3d/stls.c b/pop3d/stls.c
index 189bc7c..6a8bbf6 100644
--- a/pop3d/stls.c
+++ b/pop3d/stls.c
@@ -35,7 +35,7 @@ pop3d_stls (char *arg)
   pop3d_outf ("+OK Begin TLS negotiation\n");
   pop3d_flush_output ();
 
-  tls_done = pop3d_init_tls_server ();
+  tls_done = pop3d_init_tls_server () == 0;
 
   if (!tls_done)
     {
diff --git a/pop3d/top.c b/pop3d/top.c
index 73d6a27..3716875 100644
--- a/pop3d/top.c
+++ b/pop3d/top.c
@@ -62,7 +62,7 @@ pop3d_top (char *arg)
     return ERR_UNKNOWN;
   pop3d_outf ("+OK\n");
 
-  mu_stream_copy (ostream, stream, 0);
+  mu_stream_copy (iostream, stream, 0);
   pop3d_outf ("\n");
   mu_stream_destroy (&stream);
   


hooks/post-receive
-- 
GNU Mailutils


